1 /* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
36 ptid_t general_thread
;
41 static int extended_protocol
;
42 static int response_needed
;
43 static int exit_requested
;
48 static char **program_argv
, **wrapper_argv
;
50 /* Enable miscellaneous debugging output. The name is historical - it
51 was originally used to debug LinuxThreads support. */
54 /* Enable debugging of h/w breakpoint/watchpoint support. */
57 int pass_signals
[TARGET_SIGNAL_LAST
];
61 const char *gdbserver_xmltarget
;
63 /* The PID of the originally created or attached inferior. Used to
64 send signals to the process when GDB sends us an asynchronous interrupt
65 (user hitting Control-C in the client), and to wait for the child to exit
66 when no longer debugging it. */
68 unsigned long signal_pid
;
71 /* A file descriptor for the controlling terminal. */
74 /* TERMINAL_FD's original foreground group. */
75 pid_t old_foreground_pgrp
;
77 /* Hand back terminal ownership to the original foreground group. */
80 restore_old_foreground_pgrp (void)
82 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
86 /* Set if you want to disable optional thread related packets support
87 in gdbserver, for the sake of testing GDB against stubs that don't
89 int disable_packet_vCont
;
90 int disable_packet_Tthread
;
91 int disable_packet_qC
;
92 int disable_packet_qfThreadInfo
;
94 /* Last status reported to GDB. */
95 static struct target_waitstatus last_status
;
96 static ptid_t last_ptid
;
99 static unsigned char *mem_buf
;
101 /* Structure holding information relative to a single stop reply. We
102 keep a queue of these (really a singly-linked list) to push to GDB
106 /* Pointer to next in list. */
107 struct vstop_notif
*next
;
109 /* Thread or process that got the event. */
113 struct target_waitstatus status
;
116 /* The pending stop replies list head. */
117 static struct vstop_notif
*notif_queue
= NULL
;
119 /* Put a stop reply to the stop reply queue. */
122 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
124 struct vstop_notif
*new_notif
;
126 new_notif
= malloc (sizeof (*new_notif
));
127 new_notif
->next
= NULL
;
128 new_notif
->ptid
= ptid
;
129 new_notif
->status
= *status
;
133 struct vstop_notif
*tail
;
134 for (tail
= notif_queue
;
138 tail
->next
= new_notif
;
141 notif_queue
= new_notif
;
146 struct vstop_notif
*n
;
148 for (n
= notif_queue
; n
; n
= n
->next
)
151 fprintf (stderr
, "pending stop replies: %d\n", i
);
155 /* Place an event in the stop reply queue, and push a notification if
156 we aren't sending one yet. */
159 push_event (ptid_t ptid
, struct target_waitstatus
*status
)
161 gdb_assert (status
->kind
!= TARGET_WAITKIND_IGNORE
);
163 queue_stop_reply (ptid
, status
);
165 /* If this is the first stop reply in the queue, then inform GDB
166 about it, by sending a Stop notification. */
167 if (notif_queue
->next
== NULL
)
172 prepare_resume_reply (p
,
173 notif_queue
->ptid
, ¬if_queue
->status
);
174 putpkt_notif (own_buf
);
178 /* Get rid of the currently pending stop replies for PID. If PID is
179 -1, then apply to all processes. */
182 discard_queued_stop_replies (int pid
)
184 struct vstop_notif
*prev
= NULL
, *reply
, *next
;
186 for (reply
= notif_queue
; reply
; reply
= next
)
191 || ptid_get_pid (reply
->ptid
) == pid
)
193 if (reply
== notif_queue
)
196 prev
->next
= reply
->next
;
205 /* If there are more stop replies to push, push one now. */
208 send_next_stop_reply (char *own_buf
)
211 prepare_resume_reply (own_buf
,
213 ¬if_queue
->status
);
219 target_running (void)
221 return all_threads
.head
!= NULL
;
225 start_inferior (char **argv
)
227 char **new_argv
= argv
;
229 if (wrapper_argv
!= NULL
)
233 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
235 for (i
= 0; argv
[i
] != NULL
; i
++)
237 new_argv
= alloca (sizeof (char *) * count
);
239 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
240 new_argv
[count
++] = wrapper_argv
[i
];
241 for (i
= 0; argv
[i
] != NULL
; i
++)
242 new_argv
[count
++] = argv
[i
];
243 new_argv
[count
] = NULL
;
249 for (i
= 0; new_argv
[i
]; ++i
)
250 fprintf (stderr
, "new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
255 signal (SIGTTOU
, SIG_DFL
);
256 signal (SIGTTIN
, SIG_DFL
);
259 signal_pid
= create_inferior (new_argv
[0], new_argv
);
261 /* FIXME: we don't actually know at this point that the create
262 actually succeeded. We won't know that until we wait. */
263 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
268 signal (SIGTTOU
, SIG_IGN
);
269 signal (SIGTTIN
, SIG_IGN
);
270 terminal_fd
= fileno (stderr
);
271 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
272 tcsetpgrp (terminal_fd
, signal_pid
);
273 atexit (restore_old_foreground_pgrp
);
276 if (wrapper_argv
!= NULL
)
278 struct thread_resume resume_info
;
281 resume_info
.thread
= pid_to_ptid (signal_pid
);
282 resume_info
.kind
= resume_continue
;
285 ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
287 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
292 (*the_target
->resume
) (&resume_info
, 1);
294 mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
295 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
298 current_inferior
->last_resume_kind
= resume_stop
;
299 current_inferior
->last_status
= last_status
;
301 while (last_status
.value
.sig
!= TARGET_SIGNAL_TRAP
);
303 current_inferior
->last_resume_kind
= resume_stop
;
304 current_inferior
->last_status
= last_status
;
308 /* Wait till we are at 1st instruction in program, return new pid
309 (assuming success). */
310 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
312 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
313 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
315 current_inferior
->last_resume_kind
= resume_stop
;
316 current_inferior
->last_status
= last_status
;
323 attach_inferior (int pid
)
325 /* myattach should return -1 if attaching is unsupported,
326 0 if it succeeded, and call error() otherwise. */
328 if (myattach (pid
) != 0)
331 fprintf (stderr
, "Attached; pid = %d\n", pid
);
334 /* FIXME - It may be that we should get the SIGNAL_PID from the
335 attach function, so that it can be the main thread instead of
336 whichever we were told to attach to. */
341 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
343 /* GDB knows to ignore the first SIGSTOP after attaching to a running
344 process using the "attach" command, but this is different; it's
345 just using "target remote". Pretend it's just starting up. */
346 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
347 && last_status
.value
.sig
== TARGET_SIGNAL_STOP
)
348 last_status
.value
.sig
= TARGET_SIGNAL_TRAP
;
350 current_inferior
->last_resume_kind
= resume_stop
;
351 current_inferior
->last_status
= last_status
;
357 extern int remote_debug
;
359 /* Decode a qXfer read request. Return 0 if everything looks OK,
363 decode_xfer_read (char *buf
, char **annex
, CORE_ADDR
*ofs
, unsigned int *len
)
365 /* Extract and NUL-terminate the annex. */
367 while (*buf
&& *buf
!= ':')
373 /* After the read marker and annex, qXfer looks like a
374 traditional 'm' packet. */
375 decode_m_packet (buf
, ofs
, len
);
380 /* Write the response to a successful qXfer read. Returns the
381 length of the (binary) data stored in BUF, corresponding
382 to as much of DATA/LEN as we could fit. IS_MORE controls
383 the first character of the response. */
385 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
394 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
398 /* Handle all of the extended 'Q' packets. */
401 handle_general_set (char *own_buf
)
403 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
405 int numsigs
= (int) TARGET_SIGNAL_LAST
, i
;
406 const char *p
= own_buf
+ strlen ("QPassSignals:");
409 p
= decode_address_to_semicolon (&cursig
, p
);
410 for (i
= 0; i
< numsigs
; i
++)
416 /* Keep looping, to clear the remaining signals. */
419 p
= decode_address_to_semicolon (&cursig
, p
);
424 strcpy (own_buf
, "OK");
428 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
432 fprintf (stderr
, "[noack mode enabled]\n");
441 if (strncmp (own_buf
, "QNonStop:", 9) == 0)
443 char *mode
= own_buf
+ 9;
447 if (strcmp (mode
, "0") == 0)
449 else if (strcmp (mode
, "1") == 0)
453 /* We don't know what this mode is, so complain to
455 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
461 req_str
= req
? "non-stop" : "all-stop";
462 if (start_non_stop (req
) != 0)
464 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
472 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
478 if (target_supports_tracepoints ()
479 && handle_tracepoint_general_set (own_buf
))
482 /* Otherwise we didn't know what packet it was. Say we didn't
488 get_features_xml (const char *annex
)
490 /* gdbserver_xmltarget defines what to return when looking
491 for the "target.xml" file. Its contents can either be
492 verbatim XML code (prefixed with a '@') or else the name
493 of the actual XML file to be used in place of "target.xml".
495 This variable is set up from the auto-generated
496 init_registers_... routine for the current target. */
498 if (gdbserver_xmltarget
499 && strcmp (annex
, "target.xml") == 0)
501 if (*gdbserver_xmltarget
== '@')
502 return gdbserver_xmltarget
+ 1;
504 annex
= gdbserver_xmltarget
;
509 extern const char *const xml_builtin
[][2];
512 /* Look for the annex. */
513 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
514 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
517 if (xml_builtin
[i
][0] != NULL
)
518 return xml_builtin
[i
][1];
526 monitor_show_help (void)
528 monitor_output ("The following monitor commands are supported:\n");
529 monitor_output (" set debug <0|1>\n");
530 monitor_output (" Enable general debugging messages\n");
531 monitor_output (" set debug-hw-points <0|1>\n");
532 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
533 monitor_output (" set remote-debug <0|1>\n");
534 monitor_output (" Enable remote protocol debugging messages\n");
535 monitor_output (" exit\n");
536 monitor_output (" Quit GDBserver\n");
539 /* Read trace frame or inferior memory. */
542 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
546 if (current_traceframe
>= 0)
549 ULONGEST length
= len
;
551 if (traceframe_read_mem (current_traceframe
,
552 memaddr
, myaddr
, len
, &nbytes
))
554 /* Data read from trace buffer, we're done. */
555 if (nbytes
== length
)
557 if (!in_readonly_region (memaddr
, length
))
559 /* Otherwise we have a valid readonly case, fall through. */
560 /* (assume no half-trace half-real blocks for now) */
563 ret
= prepare_to_access_memory ();
566 ret
= read_inferior_memory (memaddr
, myaddr
, len
);
567 done_accessing_memory ();
573 /* Write trace frame or inferior memory. Actually, writing to trace
574 frames is forbidden. */
577 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
579 if (current_traceframe
>= 0)
585 ret
= prepare_to_access_memory ();
588 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
589 done_accessing_memory ();
595 /* Subroutine of handle_search_memory to simplify it. */
598 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
599 gdb_byte
*pattern
, unsigned pattern_len
,
600 gdb_byte
*search_buf
,
601 unsigned chunk_size
, unsigned search_buf_size
,
602 CORE_ADDR
*found_addrp
)
604 /* Prime the search buffer. */
606 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
) != 0)
608 warning ("Unable to access target memory at 0x%lx, halting search.",
613 /* Perform the search.
615 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
616 When we've scanned N bytes we copy the trailing bytes to the start and
617 read in another N bytes. */
619 while (search_space_len
>= pattern_len
)
622 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
626 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
628 if (found_ptr
!= NULL
)
630 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
631 *found_addrp
= found_addr
;
635 /* Not found in this chunk, skip to next chunk. */
637 /* Don't let search_space_len wrap here, it's unsigned. */
638 if (search_space_len
>= chunk_size
)
639 search_space_len
-= chunk_size
;
641 search_space_len
= 0;
643 if (search_space_len
>= pattern_len
)
645 unsigned keep_len
= search_buf_size
- chunk_size
;
646 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
649 /* Copy the trailing part of the previous iteration to the front
650 of the buffer for the next iteration. */
651 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
653 nr_to_read
= (search_space_len
- keep_len
< chunk_size
654 ? search_space_len
- keep_len
657 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
660 warning ("Unable to access target memory at 0x%lx, halting search.",
665 start_addr
+= chunk_size
;
674 /* Handle qSearch:memory packets. */
677 handle_search_memory (char *own_buf
, int packet_len
)
679 CORE_ADDR start_addr
;
680 CORE_ADDR search_space_len
;
682 unsigned int pattern_len
;
683 /* NOTE: also defined in find.c testcase. */
684 #define SEARCH_CHUNK_SIZE 16000
685 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
686 /* Buffer to hold memory contents for searching. */
687 gdb_byte
*search_buf
;
688 unsigned search_buf_size
;
690 CORE_ADDR found_addr
;
691 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
693 pattern
= malloc (packet_len
);
696 error ("Unable to allocate memory to perform the search");
697 strcpy (own_buf
, "E00");
700 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
701 packet_len
- cmd_name_len
,
702 &start_addr
, &search_space_len
,
703 pattern
, &pattern_len
) < 0)
706 error ("Error in parsing qSearch:memory packet");
707 strcpy (own_buf
, "E00");
711 search_buf_size
= chunk_size
+ pattern_len
- 1;
713 /* No point in trying to allocate a buffer larger than the search space. */
714 if (search_space_len
< search_buf_size
)
715 search_buf_size
= search_space_len
;
717 search_buf
= malloc (search_buf_size
);
718 if (search_buf
== NULL
)
721 error ("Unable to allocate memory to perform the search");
722 strcpy (own_buf
, "E00");
726 found
= handle_search_memory_1 (start_addr
, search_space_len
,
727 pattern
, pattern_len
,
728 search_buf
, chunk_size
, search_buf_size
,
732 sprintf (own_buf
, "1,%lx", (long) found_addr
);
734 strcpy (own_buf
, "0");
736 strcpy (own_buf
, "E00");
742 #define require_running(BUF) \
743 if (!target_running ()) \
749 /* Handle monitor commands not handled by target-specific handlers. */
752 handle_monitor_command (char *mon
)
754 if (strcmp (mon
, "set debug 1") == 0)
757 monitor_output ("Debug output enabled.\n");
759 else if (strcmp (mon
, "set debug 0") == 0)
762 monitor_output ("Debug output disabled.\n");
764 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
767 monitor_output ("H/W point debugging output enabled.\n");
769 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
772 monitor_output ("H/W point debugging output disabled.\n");
774 else if (strcmp (mon
, "set remote-debug 1") == 0)
777 monitor_output ("Protocol debug output enabled.\n");
779 else if (strcmp (mon
, "set remote-debug 0") == 0)
782 monitor_output ("Protocol debug output disabled.\n");
784 else if (strcmp (mon
, "help") == 0)
785 monitor_show_help ();
786 else if (strcmp (mon
, "exit") == 0)
790 monitor_output ("Unknown monitor command.\n\n");
791 monitor_show_help ();
797 handle_threads_qxfer_proper (struct buffer
*buffer
)
799 struct inferior_list_entry
*thread
;
801 buffer_grow_str (buffer
, "<threads>\n");
803 for (thread
= all_threads
.head
; thread
; thread
= thread
->next
)
805 ptid_t ptid
= thread_to_gdb_id ((struct thread_info
*)thread
);
810 write_ptid (ptid_s
, ptid
);
812 if (the_target
->core_of_thread
)
813 core
= (*the_target
->core_of_thread
) (ptid
);
817 sprintf (core_s
, "%d", core
);
818 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
823 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
828 buffer_grow_str0 (buffer
, "</threads>\n");
832 handle_threads_qxfer (const char *annex
,
833 unsigned char *readbuf
,
834 CORE_ADDR offset
, int length
)
836 static char *result
= 0;
837 static unsigned int result_length
= 0;
839 if (annex
&& strcmp (annex
, "") != 0)
844 struct buffer buffer
;
845 /* When asked for data at offset 0, generate everything and store into
846 'result'. Successive reads will be served off 'result'. */
850 buffer_init (&buffer
);
852 handle_threads_qxfer_proper (&buffer
);
854 result
= buffer_finish (&buffer
);
855 result_length
= strlen (result
);
856 buffer_free (&buffer
);
859 if (offset
>= result_length
)
861 /* We're out of data. */
868 if (length
> result_length
- offset
)
869 length
= result_length
- offset
;
871 memcpy (readbuf
, result
+ offset
, length
);
877 /* Table used by the crc32 function to calcuate the checksum. */
879 static unsigned int crc32_table
[256] =
882 /* Compute 32 bit CRC from inferior memory.
884 On success, return 32 bit CRC.
885 On failure, return (unsigned long long) -1. */
887 static unsigned long long
888 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
892 /* Initialize the CRC table and the decoding table. */
896 for (i
= 0; i
< 256; i
++)
898 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
899 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
906 unsigned char byte
= 0;
908 /* Return failure if memory read fails. */
909 if (read_inferior_memory (base
, &byte
, 1) != 0)
910 return (unsigned long long) -1;
912 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
915 return (unsigned long long) crc
;
918 /* Handle all of the extended 'q' packets. */
920 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
922 static struct inferior_list_entry
*thread_ptr
;
924 /* Reply the current thread id. */
925 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
928 require_running (own_buf
);
930 if (!ptid_equal (general_thread
, null_ptid
)
931 && !ptid_equal (general_thread
, minus_one_ptid
))
932 gdb_id
= general_thread
;
935 thread_ptr
= all_threads
.head
;
936 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
939 sprintf (own_buf
, "QC");
941 own_buf
= write_ptid (own_buf
, gdb_id
);
945 if (strcmp ("qSymbol::", own_buf
) == 0)
947 /* GDB is suggesting new symbols have been loaded. This may
948 mean a new shared library has been detected as loaded, so
949 take the opportunity to check if breakpoints we think are
950 inserted, still are. Note that it isn't guaranteed that
951 we'll see this when a shared library is loaded, and nor will
952 we see this for unloads (although breakpoints in unloaded
953 libraries shouldn't trigger), as GDB may not find symbols for
954 the library at all. We also re-validate breakpoints when we
955 see a second GDB breakpoint for the same address, and or when
956 we access breakpoint shadows. */
957 validate_breakpoints ();
959 if (target_supports_tracepoints ())
960 tracepoint_look_up_symbols ();
962 if (target_running () && the_target
->look_up_symbols
!= NULL
)
963 (*the_target
->look_up_symbols
) ();
965 strcpy (own_buf
, "OK");
969 if (!disable_packet_qfThreadInfo
)
971 if (strcmp ("qfThreadInfo", own_buf
) == 0)
975 require_running (own_buf
);
976 thread_ptr
= all_threads
.head
;
979 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
980 write_ptid (own_buf
, gdb_id
);
981 thread_ptr
= thread_ptr
->next
;
985 if (strcmp ("qsThreadInfo", own_buf
) == 0)
989 require_running (own_buf
);
990 if (thread_ptr
!= NULL
)
993 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
994 write_ptid (own_buf
, gdb_id
);
995 thread_ptr
= thread_ptr
->next
;
1000 sprintf (own_buf
, "l");
1006 if (the_target
->read_offsets
!= NULL
1007 && strcmp ("qOffsets", own_buf
) == 0)
1009 CORE_ADDR text
, data
;
1011 require_running (own_buf
);
1012 if (the_target
->read_offsets (&text
, &data
))
1013 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
1014 (long)text
, (long)data
, (long)data
);
1016 write_enn (own_buf
);
1021 if (the_target
->qxfer_spu
!= NULL
1022 && strncmp ("qXfer:spu:read:", own_buf
, 15) == 0)
1028 unsigned char *spu_buf
;
1030 require_running (own_buf
);
1031 strcpy (own_buf
, "E00");
1032 if (decode_xfer_read (own_buf
+ 15, &annex
, &ofs
, &len
) < 0)
1034 if (len
> PBUFSIZ
- 2)
1036 spu_buf
= malloc (len
+ 1);
1040 n
= (*the_target
->qxfer_spu
) (annex
, spu_buf
, NULL
, ofs
, len
+ 1);
1042 write_enn (own_buf
);
1044 *new_packet_len_p
= write_qxfer_response (own_buf
, spu_buf
, len
, 1);
1046 *new_packet_len_p
= write_qxfer_response (own_buf
, spu_buf
, n
, 0);
1052 if (the_target
->qxfer_spu
!= NULL
1053 && strncmp ("qXfer:spu:write:", own_buf
, 16) == 0)
1059 unsigned char *spu_buf
;
1061 require_running (own_buf
);
1062 strcpy (own_buf
, "E00");
1063 spu_buf
= malloc (packet_len
- 15);
1066 if (decode_xfer_write (own_buf
+ 16, packet_len
- 16, &annex
,
1067 &ofs
, &len
, spu_buf
) < 0)
1073 n
= (*the_target
->qxfer_spu
)
1074 (annex
, NULL
, (unsigned const char *)spu_buf
, ofs
, len
);
1076 write_enn (own_buf
);
1078 sprintf (own_buf
, "%x", n
);
1084 if (the_target
->read_auxv
!= NULL
1085 && strncmp ("qXfer:auxv:read:", own_buf
, 16) == 0)
1087 unsigned char *data
;
1093 require_running (own_buf
);
1095 /* Reject any annex; grab the offset and length. */
1096 if (decode_xfer_read (own_buf
+ 16, &annex
, &ofs
, &len
) < 0
1097 || annex
[0] != '\0')
1099 strcpy (own_buf
, "E00");
1103 /* Read one extra byte, as an indicator of whether there is
1105 if (len
> PBUFSIZ
- 2)
1107 data
= malloc (len
+ 1);
1110 write_enn (own_buf
);
1113 n
= (*the_target
->read_auxv
) (ofs
, data
, len
+ 1);
1115 write_enn (own_buf
);
1117 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1119 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1126 if (strncmp ("qXfer:features:read:", own_buf
, 20) == 0)
1129 unsigned int len
, total_len
;
1130 const char *document
;
1133 require_running (own_buf
);
1135 /* Grab the annex, offset, and length. */
1136 if (decode_xfer_read (own_buf
+ 20, &annex
, &ofs
, &len
) < 0)
1138 strcpy (own_buf
, "E00");
1142 /* Now grab the correct annex. */
1143 document
= get_features_xml (annex
);
1144 if (document
== NULL
)
1146 strcpy (own_buf
, "E00");
1150 total_len
= strlen (document
);
1151 if (len
> PBUFSIZ
- 2)
1154 if (ofs
> total_len
)
1155 write_enn (own_buf
);
1156 else if (len
< total_len
- ofs
)
1157 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
1160 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
1161 total_len
- ofs
, 0);
1166 if (strncmp ("qXfer:libraries:read:", own_buf
, 21) == 0)
1169 unsigned int len
, total_len
;
1171 struct inferior_list_entry
*dll_ptr
;
1174 require_running (own_buf
);
1176 /* Reject any annex; grab the offset and length. */
1177 if (decode_xfer_read (own_buf
+ 21, &annex
, &ofs
, &len
) < 0
1178 || annex
[0] != '\0')
1180 strcpy (own_buf
, "E00");
1184 /* Over-estimate the necessary memory. Assume that every character
1185 in the library name must be escaped. */
1187 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
1188 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
1190 document
= malloc (total_len
);
1191 if (document
== NULL
)
1193 write_enn (own_buf
);
1196 strcpy (document
, "<library-list>\n");
1197 p
= document
+ strlen (document
);
1199 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
1201 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
1204 strcpy (p
, " <library name=\"");
1206 name
= xml_escape_text (dll
->name
);
1210 strcpy (p
, "\"><segment address=\"");
1212 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1214 strcpy (p
, "\"/></library>\n");
1218 strcpy (p
, "</library-list>\n");
1220 total_len
= strlen (document
);
1221 if (len
> PBUFSIZ
- 2)
1224 if (ofs
> total_len
)
1225 write_enn (own_buf
);
1226 else if (len
< total_len
- ofs
)
1227 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
1230 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
1231 total_len
- ofs
, 0);
1237 if (the_target
->qxfer_osdata
!= NULL
1238 && strncmp ("qXfer:osdata:read:", own_buf
, 18) == 0)
1244 unsigned char *workbuf
;
1246 strcpy (own_buf
, "E00");
1247 if (decode_xfer_read (own_buf
+ 18, &annex
, &ofs
, &len
) < 0)
1249 if (len
> PBUFSIZ
- 2)
1251 workbuf
= malloc (len
+ 1);
1255 n
= (*the_target
->qxfer_osdata
) (annex
, workbuf
, NULL
, ofs
, len
+ 1);
1257 write_enn (own_buf
);
1259 *new_packet_len_p
= write_qxfer_response (own_buf
, workbuf
, len
, 1);
1261 *new_packet_len_p
= write_qxfer_response (own_buf
, workbuf
, n
, 0);
1267 if (the_target
->qxfer_siginfo
!= NULL
1268 && strncmp ("qXfer:siginfo:read:", own_buf
, 19) == 0)
1270 unsigned char *data
;
1276 require_running (own_buf
);
1278 /* Reject any annex; grab the offset and length. */
1279 if (decode_xfer_read (own_buf
+ 19, &annex
, &ofs
, &len
) < 0
1280 || annex
[0] != '\0')
1282 strcpy (own_buf
, "E00");
1286 /* Read one extra byte, as an indicator of whether there is
1288 if (len
> PBUFSIZ
- 2)
1290 data
= malloc (len
+ 1);
1293 n
= (*the_target
->qxfer_siginfo
) (annex
, data
, NULL
, ofs
, len
+ 1);
1295 write_enn (own_buf
);
1297 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1299 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1305 if (the_target
->qxfer_siginfo
!= NULL
1306 && strncmp ("qXfer:siginfo:write:", own_buf
, 20) == 0)
1312 unsigned char *data
;
1314 require_running (own_buf
);
1316 strcpy (own_buf
, "E00");
1317 data
= malloc (packet_len
- 19);
1320 if (decode_xfer_write (own_buf
+ 20, packet_len
- 20, &annex
,
1321 &ofs
, &len
, data
) < 0)
1327 n
= (*the_target
->qxfer_siginfo
)
1328 (annex
, NULL
, (unsigned const char *)data
, ofs
, len
);
1330 write_enn (own_buf
);
1332 sprintf (own_buf
, "%x", n
);
1338 if (strncmp ("qXfer:threads:read:", own_buf
, 19) == 0)
1340 unsigned char *data
;
1346 require_running (own_buf
);
1348 /* Reject any annex; grab the offset and length. */
1349 if (decode_xfer_read (own_buf
+ 19, &annex
, &ofs
, &len
) < 0
1350 || annex
[0] != '\0')
1352 strcpy (own_buf
, "E00");
1356 /* Read one extra byte, as an indicator of whether there is
1358 if (len
> PBUFSIZ
- 2)
1360 data
= malloc (len
+ 1);
1363 n
= handle_threads_qxfer (annex
, data
, ofs
, len
+ 1);
1365 write_enn (own_buf
);
1367 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1369 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1375 if (strncmp ("qXfer:statictrace:read:", own_buf
,
1376 sizeof ("qXfer:statictrace:read:") -1) == 0)
1378 unsigned char *data
;
1384 require_running (own_buf
);
1386 if (current_traceframe
== -1)
1388 write_enn (own_buf
);
1392 /* Reject any annex; grab the offset and length. */
1393 if (decode_xfer_read (own_buf
+ sizeof ("qXfer:statictrace:read:") -1,
1394 &annex
, &ofs
, &len
) < 0
1395 || annex
[0] != '\0')
1397 strcpy (own_buf
, "E00");
1401 /* Read one extra byte, as an indicator of whether there is
1403 if (len
> PBUFSIZ
- 2)
1405 data
= malloc (len
+ 1);
1409 if (traceframe_read_sdata (current_traceframe
, ofs
,
1410 data
, len
+ 1, &nbytes
))
1411 write_enn (own_buf
);
1412 else if (nbytes
> len
)
1413 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1415 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, nbytes
, 0);
1421 /* Protocol features query. */
1422 if (strncmp ("qSupported", own_buf
, 10) == 0
1423 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1425 char *p
= &own_buf
[10];
1426 int gdb_supports_qRelocInsn
= 0;
1428 /* Start processing qSupported packet. */
1429 target_process_qsupported (NULL
);
1431 /* Process each feature being provided by GDB. The first
1432 feature will follow a ':', and latter features will follow
1436 char **qsupported
= NULL
;
1440 /* Two passes, to avoid nested strtok calls in
1441 target_process_qsupported. */
1442 for (p
= strtok (p
+ 1, ";");
1444 p
= strtok (NULL
, ";"))
1447 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
1448 qsupported
[count
- 1] = xstrdup (p
);
1451 for (i
= 0; i
< count
; i
++)
1454 if (strcmp (p
, "multiprocess+") == 0)
1456 /* GDB supports and wants multi-process support if
1458 if (target_supports_multi_process ())
1461 else if (strcmp (p
, "qRelocInsn+") == 0)
1463 /* GDB supports relocate instruction requests. */
1464 gdb_supports_qRelocInsn
= 1;
1467 target_process_qsupported (p
);
1475 sprintf (own_buf
, "PacketSize=%x;QPassSignals+", PBUFSIZ
- 1);
1477 /* We do not have any hook to indicate whether the target backend
1478 supports qXfer:libraries:read, so always report it. */
1479 strcat (own_buf
, ";qXfer:libraries:read+");
1481 if (the_target
->read_auxv
!= NULL
)
1482 strcat (own_buf
, ";qXfer:auxv:read+");
1484 if (the_target
->qxfer_spu
!= NULL
)
1485 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
1487 if (the_target
->qxfer_siginfo
!= NULL
)
1488 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1490 /* We always report qXfer:features:read, as targets may
1491 install XML files on a subsequent call to arch_setup.
1492 If we reported to GDB on startup that we don't support
1493 qXfer:feature:read at all, we will never be re-queried. */
1494 strcat (own_buf
, ";qXfer:features:read+");
1496 if (transport_is_reliable
)
1497 strcat (own_buf
, ";QStartNoAckMode+");
1499 if (the_target
->qxfer_osdata
!= NULL
)
1500 strcat (own_buf
, ";qXfer:osdata:read+");
1502 if (target_supports_multi_process ())
1503 strcat (own_buf
, ";multiprocess+");
1505 if (target_supports_non_stop ())
1506 strcat (own_buf
, ";QNonStop+");
1508 strcat (own_buf
, ";qXfer:threads:read+");
1510 if (target_supports_tracepoints ())
1512 strcat (own_buf
, ";ConditionalTracepoints+");
1513 strcat (own_buf
, ";TraceStateVariables+");
1514 strcat (own_buf
, ";TracepointSource+");
1515 strcat (own_buf
, ";DisconnectedTracing+");
1516 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
1517 strcat (own_buf
, ";FastTracepoints+");
1518 strcat (own_buf
, ";StaticTracepoints+");
1519 strcat (own_buf
, ";qXfer:statictrace:read+");
1525 /* Thread-local storage support. */
1526 if (the_target
->get_tls_address
!= NULL
1527 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
1529 char *p
= own_buf
+ 12;
1530 CORE_ADDR parts
[2], address
= 0;
1532 ptid_t ptid
= null_ptid
;
1534 require_running (own_buf
);
1536 for (i
= 0; i
< 3; i
++)
1544 p2
= strchr (p
, ',');
1557 ptid
= read_ptid (p
, NULL
);
1559 decode_address (&parts
[i
- 1], p
, len
);
1563 if (p
!= NULL
|| i
< 3)
1567 struct thread_info
*thread
= find_thread_ptid (ptid
);
1572 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
1578 strcpy (own_buf
, paddress(address
));
1583 write_enn (own_buf
);
1587 /* Otherwise, pretend we do not understand this packet. */
1590 /* Windows OS Thread Information Block address support. */
1591 if (the_target
->get_tib_address
!= NULL
1592 && strncmp ("qGetTIBAddr:", own_buf
, 12) == 0)
1597 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
1599 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
1602 strcpy (own_buf
, paddress(tlb
));
1607 write_enn (own_buf
);
1613 /* Handle "monitor" commands. */
1614 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
1616 char *mon
= malloc (PBUFSIZ
);
1617 int len
= strlen (own_buf
+ 6);
1621 write_enn (own_buf
);
1625 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
1627 write_enn (own_buf
);
1631 mon
[len
/ 2] = '\0';
1635 if (the_target
->handle_monitor_command
== NULL
1636 || (*the_target
->handle_monitor_command
) (mon
) == 0)
1637 /* Default processing. */
1638 handle_monitor_command (mon
);
1644 if (strncmp ("qSearch:memory:", own_buf
, sizeof ("qSearch:memory:") - 1) == 0)
1646 require_running (own_buf
);
1647 handle_search_memory (own_buf
, packet_len
);
1651 if (strcmp (own_buf
, "qAttached") == 0
1652 || strncmp (own_buf
, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1654 struct process_info
*process
;
1656 if (own_buf
[sizeof ("qAttached") - 1])
1658 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
1659 process
= (struct process_info
*)
1660 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
1664 require_running (own_buf
);
1665 process
= current_process ();
1668 if (process
== NULL
)
1670 write_enn (own_buf
);
1674 strcpy (own_buf
, process
->attached
? "1" : "0");
1678 if (strncmp ("qCRC:", own_buf
, 5) == 0)
1680 /* CRC check (compare-section). */
1684 unsigned long long crc
;
1686 require_running (own_buf
);
1687 base
= strtoul (own_buf
+ 5, &comma
, 16);
1688 if (*comma
++ != ',')
1690 write_enn (own_buf
);
1693 len
= strtoul (comma
, NULL
, 16);
1694 crc
= crc32 (base
, len
, 0xffffffff);
1695 /* Check for memory failure. */
1696 if (crc
== (unsigned long long) -1)
1698 write_enn (own_buf
);
1701 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
1705 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
1708 /* Otherwise we didn't know what packet it was. Say we didn't
1713 static void gdb_wants_all_threads_stopped (void);
1715 /* Parse vCont packets. */
1717 handle_v_cont (char *own_buf
)
1721 struct thread_resume
*resume_info
;
1722 struct thread_resume default_action
= {{0}};
1724 /* Count the number of semicolons in the packet. There should be one
1725 for every action. */
1731 p
= strchr (p
, ';');
1734 resume_info
= malloc (n
* sizeof (resume_info
[0]));
1735 if (resume_info
== NULL
)
1743 if (p
[0] == 's' || p
[0] == 'S')
1744 resume_info
[i
].kind
= resume_step
;
1745 else if (p
[0] == 'c' || p
[0] == 'C')
1746 resume_info
[i
].kind
= resume_continue
;
1747 else if (p
[0] == 't')
1748 resume_info
[i
].kind
= resume_stop
;
1752 if (p
[0] == 'S' || p
[0] == 'C')
1755 sig
= strtol (p
+ 1, &q
, 16);
1760 if (!target_signal_to_host_p (sig
))
1762 resume_info
[i
].sig
= target_signal_to_host (sig
);
1766 resume_info
[i
].sig
= 0;
1772 resume_info
[i
].thread
= minus_one_ptid
;
1773 default_action
= resume_info
[i
];
1775 /* Note: we don't increment i here, we'll overwrite this entry
1776 the next time through. */
1778 else if (p
[0] == ':')
1780 ptid_t ptid
= read_ptid (p
+ 1, &q
);
1785 if (p
[0] != ';' && p
[0] != 0)
1788 resume_info
[i
].thread
= ptid
;
1795 resume_info
[i
] = default_action
;
1797 /* Still used in occasional places in the backend. */
1799 && !ptid_equal (resume_info
[0].thread
, minus_one_ptid
)
1800 && resume_info
[0].kind
!= resume_stop
)
1801 cont_thread
= resume_info
[0].thread
;
1803 cont_thread
= minus_one_ptid
;
1804 set_desired_inferior (0);
1809 (*the_target
->resume
) (resume_info
, n
);
1817 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
1819 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
1820 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
1821 current_inferior
->last_status
= last_status
;
1823 /* From the client's perspective, all-stop mode always stops all
1824 threads implicitly (and the target backend has already done
1825 so by now). Tag all threads as "want-stopped", so we don't
1826 resume them implicitly without the client telling us to. */
1827 gdb_wants_all_threads_stopped ();
1828 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1829 disable_async_io ();
1831 if (last_status
.kind
== TARGET_WAITKIND_EXITED
1832 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
1833 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
1838 write_enn (own_buf
);
1843 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1845 handle_v_attach (char *own_buf
)
1849 pid
= strtol (own_buf
+ 8, NULL
, 16);
1850 if (pid
!= 0 && attach_inferior (pid
) == 0)
1852 /* Don't report shared library events after attaching, even if
1853 some libraries are preloaded. GDB will always poll the
1854 library list. Avoids the "stopped by shared library event"
1855 notice on the GDB side. */
1860 /* In non-stop, we don't send a resume reply. Stop events
1861 will follow up using the normal notification
1866 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1872 write_enn (own_buf
);
1877 /* Run a new program. Return 1 if successful, 0 if failure. */
1879 handle_v_run (char *own_buf
)
1881 char *p
, *next_p
, **new_argv
;
1885 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
1891 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
1892 if (new_argv
== NULL
)
1894 write_enn (own_buf
);
1899 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
1901 next_p
= strchr (p
, ';');
1903 next_p
= p
+ strlen (p
);
1905 if (i
== 0 && p
== next_p
)
1909 /* FIXME: Fail request if out of memory instead of dying. */
1910 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
1911 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
1912 new_argv
[i
][(next_p
- p
) / 2] = '\0';
1921 if (new_argv
[0] == NULL
)
1923 /* GDB didn't specify a program to run. Use the program from the
1924 last run with the new argument list. */
1926 if (program_argv
== NULL
)
1928 /* FIXME: new_argv memory leak */
1929 write_enn (own_buf
);
1933 new_argv
[0] = strdup (program_argv
[0]);
1934 if (new_argv
[0] == NULL
)
1936 /* FIXME: new_argv memory leak */
1937 write_enn (own_buf
);
1942 /* Free the old argv and install the new one. */
1943 freeargv (program_argv
);
1944 program_argv
= new_argv
;
1946 start_inferior (program_argv
);
1947 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
1949 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1951 /* In non-stop, sending a resume reply doesn't set the general
1952 thread, but GDB assumes a vRun sets it (this is so GDB can
1953 query which is the main thread of the new inferior. */
1955 general_thread
= last_ptid
;
1961 write_enn (own_buf
);
1966 /* Kill process. Return 1 if successful, 0 if failure. */
1968 handle_v_kill (char *own_buf
)
1971 char *p
= &own_buf
[6];
1973 pid
= strtol (p
, NULL
, 16);
1976 if (pid
!= 0 && kill_inferior (pid
) == 0)
1978 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
1979 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
1980 last_ptid
= pid_to_ptid (pid
);
1981 discard_queued_stop_replies (pid
);
1987 write_enn (own_buf
);
1992 /* Handle a 'vStopped' packet. */
1994 handle_v_stopped (char *own_buf
)
1996 /* If we're waiting for GDB to acknowledge a pending stop reply,
1997 consider that done. */
2000 struct vstop_notif
*head
;
2003 fprintf (stderr
, "vStopped: acking %s\n",
2004 target_pid_to_str (notif_queue
->ptid
));
2007 notif_queue
= notif_queue
->next
;
2011 /* Push another stop reply, or if there are no more left, an OK. */
2012 send_next_stop_reply (own_buf
);
2015 /* Handle all of the extended 'v' packets. */
2017 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2019 if (!disable_packet_vCont
)
2021 if (strncmp (own_buf
, "vCont;", 6) == 0)
2023 require_running (own_buf
);
2024 handle_v_cont (own_buf
);
2028 if (strncmp (own_buf
, "vCont?", 6) == 0)
2030 strcpy (own_buf
, "vCont;c;C;s;S;t");
2035 if (strncmp (own_buf
, "vFile:", 6) == 0
2036 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2039 if (strncmp (own_buf
, "vAttach;", 8) == 0)
2041 if (!multi_process
&& target_running ())
2043 fprintf (stderr
, "Already debugging a process\n");
2044 write_enn (own_buf
);
2047 handle_v_attach (own_buf
);
2051 if (strncmp (own_buf
, "vRun;", 5) == 0)
2053 if (!multi_process
&& target_running ())
2055 fprintf (stderr
, "Already debugging a process\n");
2056 write_enn (own_buf
);
2059 handle_v_run (own_buf
);
2063 if (strncmp (own_buf
, "vKill;", 6) == 0)
2065 if (!target_running ())
2067 fprintf (stderr
, "No process to kill\n");
2068 write_enn (own_buf
);
2071 handle_v_kill (own_buf
);
2075 if (strncmp (own_buf
, "vStopped", 8) == 0)
2077 handle_v_stopped (own_buf
);
2081 /* Otherwise we didn't know what packet it was. Say we didn't
2087 /* Resume inferior and wait for another event. In non-stop mode,
2088 don't really wait here, but return immediatelly to the event
2091 myresume (char *own_buf
, int step
, int sig
)
2093 struct thread_resume resume_info
[2];
2095 int valid_cont_thread
;
2097 set_desired_inferior (0);
2099 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2100 && !ptid_equal (cont_thread
, minus_one_ptid
));
2102 if (step
|| sig
|| valid_cont_thread
)
2104 resume_info
[0].thread
2105 = ((struct inferior_list_entry
*) current_inferior
)->id
;
2107 resume_info
[0].kind
= resume_step
;
2109 resume_info
[0].kind
= resume_continue
;
2110 resume_info
[0].sig
= sig
;
2114 if (!valid_cont_thread
)
2116 resume_info
[n
].thread
= minus_one_ptid
;
2117 resume_info
[n
].kind
= resume_continue
;
2118 resume_info
[n
].sig
= 0;
2125 (*the_target
->resume
) (resume_info
, n
);
2131 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2133 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2134 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2136 current_inferior
->last_resume_kind
= resume_stop
;
2137 current_inferior
->last_status
= last_status
;
2140 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2141 disable_async_io ();
2143 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2144 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2145 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2149 /* Callback for for_each_inferior. Make a new stop reply for each
2153 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2155 struct thread_info
*thread
= (struct thread_info
*) entry
;
2157 /* For now, assume targets that don't have this callback also don't
2158 manage the thread's last_status field. */
2159 if (the_target
->thread_stopped
== NULL
)
2161 struct target_waitstatus status
;
2163 status
.kind
= TARGET_WAITKIND_STOPPED
;
2164 status
.value
.sig
= TARGET_SIGNAL_TRAP
;
2166 /* Pass the last stop reply back to GDB, but don't notify
2168 queue_stop_reply (entry
->id
, &thread
->last_status
);
2172 if (thread_stopped (thread
))
2175 fprintf (stderr
, "Reporting thread %s as already stopped with %s\n",
2176 target_pid_to_str (entry
->id
),
2177 target_waitstatus_to_string (&thread
->last_status
));
2179 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2181 /* Pass the last stop reply back to GDB, but don't notify
2183 queue_stop_reply (entry
->id
, &thread
->last_status
);
2190 /* Set this inferior threads's state as "want-stopped". We won't
2191 resume this thread until the client gives us another action for
2195 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2197 struct thread_info
*thread
= (struct thread_info
*) entry
;
2199 thread
->last_resume_kind
= resume_stop
;
2201 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2203 /* Most threads are stopped implicitly (all-stop); tag that with
2205 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2206 thread
->last_status
.value
.sig
= TARGET_SIGNAL_0
;
2210 /* Set all threads' states as "want-stopped". */
2213 gdb_wants_all_threads_stopped (void)
2215 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2218 /* Clear the gdb_detached flag of every process. */
2221 gdb_reattached_process (struct inferior_list_entry
*entry
)
2223 struct process_info
*process
= (struct process_info
*) entry
;
2225 process
->gdb_detached
= 0;
2228 /* Status handler for the '?' packet. */
2231 handle_status (char *own_buf
)
2233 /* GDB is connected, don't forward events to the target anymore. */
2234 for_each_inferior (&all_processes
, gdb_reattached_process
);
2236 /* In non-stop mode, we must send a stop reply for each stopped
2237 thread. In all-stop mode, just send one for the first stopped
2242 discard_queued_stop_replies (-1);
2243 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2245 /* The first is sent immediatly. OK is sent if there is no
2246 stopped thread, which is the same handling of the vStopped
2247 packet (by design). */
2248 send_next_stop_reply (own_buf
);
2253 stabilize_threads ();
2254 gdb_wants_all_threads_stopped ();
2256 if (all_threads
.head
)
2258 struct target_waitstatus status
;
2260 status
.kind
= TARGET_WAITKIND_STOPPED
;
2261 status
.value
.sig
= TARGET_SIGNAL_TRAP
;
2262 prepare_resume_reply (own_buf
,
2263 all_threads
.head
->id
, &status
);
2266 strcpy (own_buf
, "W00");
2271 gdbserver_version (void)
2273 printf ("GNU gdbserver %s%s\n"
2274 "Copyright (C) 2010 Free Software Foundation, Inc.\n"
2275 "gdbserver is free software, covered by the GNU General Public License.\n"
2276 "This gdbserver was configured as \"%s\"\n",
2277 PKGVERSION
, version
, host_name
);
2281 gdbserver_usage (FILE *stream
)
2283 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2284 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2285 "\tgdbserver [OPTIONS] --multi COMM\n"
2287 "COMM may either be a tty device (for serial debugging), or \n"
2288 "HOST:PORT to listen for a TCP connection.\n"
2291 " --debug Enable general debugging output.\n"
2292 " --remote-debug Enable remote protocol debugging output.\n"
2293 " --version Display version information and exit.\n"
2294 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n");
2295 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
2296 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
2300 gdbserver_show_disableable (FILE *stream
)
2302 fprintf (stream
, "Disableable packets:\n"
2303 " vCont \tAll vCont packets\n"
2304 " qC \tQuerying the current thread\n"
2305 " qfThreadInfo\tThread listing\n"
2306 " Tthread \tPassing the thread specifier in the T stop reply packet\n"
2307 " threads \tAll of the above\n");
2311 #undef require_running
2312 #define require_running(BUF) \
2313 if (!target_running ()) \
2320 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
2322 int pid
= * (int *) args
;
2324 if (ptid_get_pid (entry
->id
) == pid
)
2331 kill_inferior_callback (struct inferior_list_entry
*entry
)
2333 struct process_info
*process
= (struct process_info
*) entry
;
2334 int pid
= ptid_get_pid (process
->head
.id
);
2336 kill_inferior (pid
);
2337 discard_queued_stop_replies (pid
);
2340 /* Callback for for_each_inferior to detach or kill the inferior,
2341 depending on whether we attached to it or not.
2342 We inform the user whether we're detaching or killing the process
2343 as this is only called when gdbserver is about to exit. */
2346 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
2348 struct process_info
*process
= (struct process_info
*) entry
;
2349 int pid
= ptid_get_pid (process
->head
.id
);
2351 if (process
->attached
)
2352 detach_inferior (pid
);
2354 kill_inferior (pid
);
2356 discard_queued_stop_replies (pid
);
2359 /* for_each_inferior callback for detach_or_kill_for_exit to print
2360 the pids of started inferiors. */
2363 print_started_pid (struct inferior_list_entry
*entry
)
2365 struct process_info
*process
= (struct process_info
*) entry
;
2367 if (! process
->attached
)
2369 int pid
= ptid_get_pid (process
->head
.id
);
2370 fprintf (stderr
, " %d", pid
);
2374 /* for_each_inferior callback for detach_or_kill_for_exit to print
2375 the pids of attached inferiors. */
2378 print_attached_pid (struct inferior_list_entry
*entry
)
2380 struct process_info
*process
= (struct process_info
*) entry
;
2382 if (process
->attached
)
2384 int pid
= ptid_get_pid (process
->head
.id
);
2385 fprintf (stderr
, " %d", pid
);
2389 /* Call this when exiting gdbserver with possible inferiors that need
2390 to be killed or detached from. */
2393 detach_or_kill_for_exit (void)
2395 /* First print a list of the inferiors we will be killing/detaching.
2396 This is to assist the user, for example, in case the inferior unexpectedly
2397 dies after we exit: did we screw up or did the inferior exit on its own?
2398 Having this info will save some head-scratching. */
2400 if (have_started_inferiors_p ())
2402 fprintf (stderr
, "Killing process(es):");
2403 for_each_inferior (&all_processes
, print_started_pid
);
2404 fprintf (stderr
, "\n");
2406 if (have_attached_inferiors_p ())
2408 fprintf (stderr
, "Detaching process(es):");
2409 for_each_inferior (&all_processes
, print_attached_pid
);
2410 fprintf (stderr
, "\n");
2413 /* Now we can kill or detach the inferiors. */
2415 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
2419 join_inferiors_callback (struct inferior_list_entry
*entry
)
2421 struct process_info
*process
= (struct process_info
*) entry
;
2423 /* If we are attached, then we can exit. Otherwise, we need to hang
2424 around doing nothing, until the child is gone. */
2425 if (!process
->attached
)
2426 join_inferior (ptid_get_pid (process
->head
.id
));
2430 main (int argc
, char *argv
[])
2434 char *arg_end
, *port
;
2435 char **next_arg
= &argv
[1];
2440 while (*next_arg
!= NULL
&& **next_arg
== '-')
2442 if (strcmp (*next_arg
, "--version") == 0)
2444 gdbserver_version ();
2447 else if (strcmp (*next_arg
, "--help") == 0)
2449 gdbserver_usage (stdout
);
2452 else if (strcmp (*next_arg
, "--attach") == 0)
2454 else if (strcmp (*next_arg
, "--multi") == 0)
2456 else if (strcmp (*next_arg
, "--wrapper") == 0)
2460 wrapper_argv
= next_arg
;
2461 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
2464 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
2466 gdbserver_usage (stderr
);
2470 /* Consume the "--". */
2473 else if (strcmp (*next_arg
, "--debug") == 0)
2475 else if (strcmp (*next_arg
, "--remote-debug") == 0)
2477 else if (strcmp (*next_arg
, "--disable-packet") == 0)
2479 gdbserver_show_disableable (stdout
);
2482 else if (strncmp (*next_arg
,
2483 "--disable-packet=",
2484 sizeof ("--disable-packet=") - 1) == 0)
2486 char *packets
, *tok
;
2488 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
2489 for (tok
= strtok (packets
, ",");
2491 tok
= strtok (NULL
, ","))
2493 if (strcmp ("vCont", tok
) == 0)
2494 disable_packet_vCont
= 1;
2495 else if (strcmp ("Tthread", tok
) == 0)
2496 disable_packet_Tthread
= 1;
2497 else if (strcmp ("qC", tok
) == 0)
2498 disable_packet_qC
= 1;
2499 else if (strcmp ("qfThreadInfo", tok
) == 0)
2500 disable_packet_qfThreadInfo
= 1;
2501 else if (strcmp ("threads", tok
) == 0)
2503 disable_packet_vCont
= 1;
2504 disable_packet_Tthread
= 1;
2505 disable_packet_qC
= 1;
2506 disable_packet_qfThreadInfo
= 1;
2510 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
2512 gdbserver_show_disableable (stderr
);
2519 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
2527 if (setjmp (toplevel
))
2529 fprintf (stderr
, "Exiting\n");
2535 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
2537 gdbserver_usage (stderr
);
2544 /* --attach used to come after PORT, so allow it there for
2546 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
2553 && (*next_arg
== NULL
2554 || (*next_arg
)[0] == '\0'
2555 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
2557 || next_arg
[1] != NULL
))
2562 gdbserver_usage (stderr
);
2566 initialize_inferiors ();
2567 initialize_async_io ();
2569 if (target_supports_tracepoints ())
2570 initialize_tracepoint ();
2572 own_buf
= xmalloc (PBUFSIZ
+ 1);
2573 mem_buf
= xmalloc (PBUFSIZ
);
2575 if (pid
== 0 && *next_arg
!= NULL
)
2579 n
= argc
- (next_arg
- argv
);
2580 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
2581 for (i
= 0; i
< n
; i
++)
2582 program_argv
[i
] = xstrdup (next_arg
[i
]);
2583 program_argv
[i
] = NULL
;
2585 /* Wait till we are at first instruction in program. */
2586 start_inferior (program_argv
);
2588 /* We are now (hopefully) stopped at the first instruction of
2589 the target process. This assumes that the target process was
2590 successfully created. */
2594 if (attach_inferior (pid
) == -1)
2595 error ("Attaching not supported on this target");
2597 /* Otherwise succeeded. */
2601 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2602 last_status
.value
.integer
= 0;
2603 last_ptid
= minus_one_ptid
;
2606 /* Don't report shared library events on the initial connection,
2607 even if some libraries are preloaded. Avoids the "stopped by
2608 shared library event" notice on gdb side. */
2611 if (setjmp (toplevel
))
2613 detach_or_kill_for_exit ();
2617 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2618 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2623 if (!was_running
&& !multi_mode
)
2625 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
2633 /* Be sure we're out of tfind mode. */
2634 current_traceframe
= -1;
2638 if (setjmp (toplevel
) != 0)
2640 /* An error occurred. */
2641 if (response_needed
)
2643 write_enn (own_buf
);
2648 /* Wait for events. This will return when all event sources are
2649 removed from the event loop. */
2650 start_event_loop ();
2652 /* If an exit was requested (using the "monitor exit" command),
2653 terminate now. The only other way to get here is for
2654 getpkt to fail; close the connection and reopen it at the
2659 detach_or_kill_for_exit ();
2664 "Remote side has terminated connection. "
2665 "GDBserver will reopen the connection.\n");
2669 if (disconnected_tracing
)
2671 /* Try to enable non-stop/async mode, so we we can both
2672 wait for an async socket accept, and handle async
2673 target events simultaneously. There's also no point
2674 either in having the target always stop all threads,
2675 when we're going to pass signals down without
2679 if (start_non_stop (1))
2682 /* Detaching implicitly resumes all threads; simply
2683 disconnecting does not. */
2689 "Disconnected tracing disabled; stopping trace run.\n");
2696 /* Event loop callback that handles a serial event. The first byte in
2697 the serial buffer gets us here. We expect characters to arrive at
2698 a brisk pace, so we read the rest of the packet with a blocking
2702 process_serial_event (void)
2712 int new_packet_len
= -1;
2714 /* Used to decide when gdbserver should exit in
2715 multi-mode/remote. */
2716 static int have_ran
= 0;
2719 have_ran
= target_running ();
2721 disable_async_io ();
2723 response_needed
= 0;
2724 packet_len
= getpkt (own_buf
);
2725 if (packet_len
<= 0)
2728 /* Force an event loop break. */
2731 response_needed
= 1;
2738 handle_query (own_buf
, packet_len
, &new_packet_len
);
2741 handle_general_set (own_buf
);
2744 require_running (own_buf
);
2749 pid
= strtol (&own_buf
[i
], NULL
, 16);
2753 ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
2755 if (tracing
&& disconnected_tracing
)
2757 struct thread_resume resume_info
;
2758 struct process_info
*process
= find_process_pid (pid
);
2760 if (process
== NULL
)
2762 write_enn (own_buf
);
2767 "Disconnected tracing in effect, "
2768 "leaving gdbserver attached to the process\n");
2770 /* Make sure we're in non-stop/async mode, so we we can both
2771 wait for an async socket accept, and handle async target
2772 events simultaneously. There's also no point either in
2773 having the target stop all threads, when we're going to
2774 pass signals down without informing GDB. */
2778 fprintf (stderr
, "Forcing non-stop mode\n");
2784 process
->gdb_detached
= 1;
2786 /* Detaching implicitly resumes all threads. */
2787 resume_info
.thread
= minus_one_ptid
;
2788 resume_info
.kind
= resume_continue
;
2789 resume_info
.sig
= 0;
2790 (*the_target
->resume
) (&resume_info
, 1);
2793 break; /* from switch/case */
2796 fprintf (stderr
, "Detaching from process %d\n", pid
);
2798 if (detach_inferior (pid
) != 0)
2799 write_enn (own_buf
);
2802 discard_queued_stop_replies (pid
);
2805 if (extended_protocol
)
2807 /* Treat this like a normal program exit. */
2808 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2809 last_status
.value
.integer
= 0;
2810 last_ptid
= pid_to_ptid (pid
);
2812 current_inferior
= NULL
;
2819 /* If we are attached, then we can exit. Otherwise, we
2820 need to hang around doing nothing, until the child is
2822 for_each_inferior (&all_processes
,
2823 join_inferiors_callback
);
2829 extended_protocol
= 1;
2833 handle_status (own_buf
);
2836 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
2838 ptid_t gdb_id
, thread_id
;
2841 require_running (own_buf
);
2843 gdb_id
= read_ptid (&own_buf
[2], NULL
);
2845 pid
= ptid_get_pid (gdb_id
);
2847 if (ptid_equal (gdb_id
, null_ptid
)
2848 || ptid_equal (gdb_id
, minus_one_ptid
))
2849 thread_id
= null_ptid
;
2851 && ptid_equal (pid_to_ptid (pid
),
2854 struct thread_info
*thread
=
2855 (struct thread_info
*) find_inferior (&all_threads
,
2860 write_enn (own_buf
);
2864 thread_id
= ((struct inferior_list_entry
*)thread
)->id
;
2868 thread_id
= gdb_id_to_thread_id (gdb_id
);
2869 if (ptid_equal (thread_id
, null_ptid
))
2871 write_enn (own_buf
);
2876 if (own_buf
[1] == 'g')
2878 if (ptid_equal (thread_id
, null_ptid
))
2880 /* GDB is telling us to choose any thread. Check if
2881 the currently selected thread is still valid. If
2882 it is not, select the first available. */
2883 struct thread_info
*thread
=
2884 (struct thread_info
*) find_inferior_id (&all_threads
,
2887 thread_id
= all_threads
.head
->id
;
2890 general_thread
= thread_id
;
2891 set_desired_inferior (1);
2893 else if (own_buf
[1] == 'c')
2894 cont_thread
= thread_id
;
2895 else if (own_buf
[1] == 's')
2896 step_thread
= thread_id
;
2902 /* Silently ignore it so that gdb can extend the protocol
2903 without compatibility headaches. */
2908 require_running (own_buf
);
2909 if (current_traceframe
>= 0)
2911 struct regcache
*regcache
= new_register_cache ();
2913 if (fetch_traceframe_registers (current_traceframe
,
2915 registers_to_string (regcache
, own_buf
);
2917 write_enn (own_buf
);
2918 free_register_cache (regcache
);
2922 struct regcache
*regcache
;
2924 set_desired_inferior (1);
2925 regcache
= get_thread_regcache (current_inferior
, 1);
2926 registers_to_string (regcache
, own_buf
);
2930 require_running (own_buf
);
2931 if (current_traceframe
>= 0)
2932 write_enn (own_buf
);
2935 struct regcache
*regcache
;
2937 set_desired_inferior (1);
2938 regcache
= get_thread_regcache (current_inferior
, 1);
2939 registers_from_string (regcache
, &own_buf
[1]);
2944 require_running (own_buf
);
2945 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
2946 if (gdb_read_memory (mem_addr
, mem_buf
, len
) == 0)
2947 convert_int_to_ascii (mem_buf
, own_buf
, len
);
2949 write_enn (own_buf
);
2952 require_running (own_buf
);
2953 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
2954 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
2957 write_enn (own_buf
);
2960 require_running (own_buf
);
2961 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
2962 &mem_addr
, &len
, &mem_buf
) < 0
2963 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
2964 write_enn (own_buf
);
2969 require_running (own_buf
);
2970 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
2971 if (target_signal_to_host_p (sig
))
2972 signal
= target_signal_to_host (sig
);
2975 myresume (own_buf
, 0, signal
);
2978 require_running (own_buf
);
2979 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
2980 if (target_signal_to_host_p (sig
))
2981 signal
= target_signal_to_host (sig
);
2984 myresume (own_buf
, 1, signal
);
2987 require_running (own_buf
);
2989 myresume (own_buf
, 0, signal
);
2992 require_running (own_buf
);
2994 myresume (own_buf
, 1, signal
);
2996 case 'Z': /* insert_ ... */
2998 case 'z': /* remove_ ... */
3002 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
3003 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
3004 char type
= own_buf
[1];
3006 const int insert
= ch
== 'Z';
3008 /* Default to unrecognized/unsupported. */
3012 case '0': /* software-breakpoint */
3013 case '1': /* hardware-breakpoint */
3014 case '2': /* write watchpoint */
3015 case '3': /* read watchpoint */
3016 case '4': /* access watchpoint */
3017 require_running (own_buf
);
3018 if (insert
&& the_target
->insert_point
!= NULL
)
3019 res
= (*the_target
->insert_point
) (type
, addr
, len
);
3020 else if (!insert
&& the_target
->remove_point
!= NULL
)
3021 res
= (*the_target
->remove_point
) (type
, addr
, len
);
3033 write_enn (own_buf
);
3037 response_needed
= 0;
3038 if (!target_running ())
3039 /* The packet we received doesn't make sense - but we can't
3040 reply to it, either. */
3043 fprintf (stderr
, "Killing all inferiors\n");
3044 for_each_inferior (&all_processes
, kill_inferior_callback
);
3046 /* When using the extended protocol, we wait with no program
3047 running. The traditional protocol will exit instead. */
3048 if (extended_protocol
)
3050 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3051 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3059 ptid_t gdb_id
, thread_id
;
3061 require_running (own_buf
);
3063 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3064 thread_id
= gdb_id_to_thread_id (gdb_id
);
3065 if (ptid_equal (thread_id
, null_ptid
))
3067 write_enn (own_buf
);
3071 if (mythread_alive (thread_id
))
3074 write_enn (own_buf
);
3078 response_needed
= 0;
3080 /* Restarting the inferior is only supported in the extended
3082 if (extended_protocol
)
3084 if (target_running ())
3085 for_each_inferior (&all_processes
,
3086 kill_inferior_callback
);
3087 fprintf (stderr
, "GDBserver restarting\n");
3089 /* Wait till we are at 1st instruction in prog. */
3090 if (program_argv
!= NULL
)
3091 start_inferior (program_argv
);
3094 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3095 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3101 /* It is a request we don't understand. Respond with an
3102 empty packet so that gdb knows that we don't support this
3108 /* Extended (long) request. */
3109 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
3113 /* It is a request we don't understand. Respond with an empty
3114 packet so that gdb knows that we don't support this
3120 if (new_packet_len
!= -1)
3121 putpkt_binary (own_buf
, new_packet_len
);
3125 response_needed
= 0;
3127 if (!extended_protocol
&& have_ran
&& !target_running ())
3129 /* In non-stop, defer exiting until GDB had a chance to query
3130 the whole vStopped list (until it gets an OK). */
3133 fprintf (stderr
, "GDBserver exiting\n");
3145 /* Event-loop callback for serial events. */
3148 handle_serial_event (int err
, gdb_client_data client_data
)
3151 fprintf (stderr
, "handling possible serial event\n");
3153 /* Really handle it. */
3154 if (process_serial_event () < 0)
3157 /* Be sure to not change the selected inferior behind GDB's back.
3158 Important in the non-stop mode asynchronous protocol. */
3159 set_desired_inferior (1);
3164 /* Event-loop callback for target events. */
3167 handle_target_event (int err
, gdb_client_data client_data
)
3170 fprintf (stderr
, "handling possible target event\n");
3172 last_ptid
= mywait (minus_one_ptid
, &last_status
,
3175 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3177 int pid
= ptid_get_pid (last_ptid
);
3178 struct process_info
*process
= find_process_pid (pid
);
3179 int forward_event
= !gdb_connected () || process
->gdb_detached
;
3181 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3182 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3184 mark_breakpoints_out (process
);
3185 mourn_inferior (process
);
3189 /* We're reporting this thread as stopped. Update its
3190 "want-stopped" state to what the client wants, until it
3191 gets a new resume action. */
3192 current_inferior
->last_resume_kind
= resume_stop
;
3193 current_inferior
->last_status
= last_status
;
3198 if (!target_running ())
3200 /* The last process exited. We're done. */
3204 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
3206 /* A thread stopped with a signal, but gdb isn't
3207 connected to handle it. Pass it down to the
3208 inferior, as if it wasn't being traced. */
3209 struct thread_resume resume_info
;
3213 "GDB not connected; forwarding event %d for [%s]\n",
3214 (int) last_status
.kind
,
3215 target_pid_to_str (last_ptid
));
3217 resume_info
.thread
= last_ptid
;
3218 resume_info
.kind
= resume_continue
;
3219 resume_info
.sig
= target_signal_to_host (last_status
.value
.sig
);
3220 (*the_target
->resume
) (&resume_info
, 1);
3222 else if (debug_threads
)
3223 fprintf (stderr
, "GDB not connected; ignoring event %d for [%s]\n",
3224 (int) last_status
.kind
,
3225 target_pid_to_str (last_ptid
));
3229 /* Something interesting. Tell GDB about it. */
3230 push_event (last_ptid
, &last_status
);
3234 /* Be sure to not change the selected inferior behind GDB's back.
3235 Important in the non-stop mode asynchronous protocol. */
3236 set_desired_inferior (1);