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/>. */
33 ptid_t general_thread
;
38 static int extended_protocol
;
39 static int response_needed
;
40 static int exit_requested
;
45 static char **program_argv
, **wrapper_argv
;
47 /* Enable miscellaneous debugging output. The name is historical - it
48 was originally used to debug LinuxThreads support. */
51 /* Enable debugging of h/w breakpoint/watchpoint support. */
54 int pass_signals
[TARGET_SIGNAL_LAST
];
58 const char *gdbserver_xmltarget
;
60 /* The PID of the originally created or attached inferior. Used to
61 send signals to the process when GDB sends us an asynchronous interrupt
62 (user hitting Control-C in the client), and to wait for the child to exit
63 when no longer debugging it. */
65 unsigned long signal_pid
;
68 /* A file descriptor for the controlling terminal. */
71 /* TERMINAL_FD's original foreground group. */
72 pid_t old_foreground_pgrp
;
74 /* Hand back terminal ownership to the original foreground group. */
77 restore_old_foreground_pgrp (void)
79 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
83 /* Set if you want to disable optional thread related packets support
84 in gdbserver, for the sake of testing GDB against stubs that don't
86 int disable_packet_vCont
;
87 int disable_packet_Tthread
;
88 int disable_packet_qC
;
89 int disable_packet_qfThreadInfo
;
91 /* Last status reported to GDB. */
92 static struct target_waitstatus last_status
;
93 static ptid_t last_ptid
;
96 static unsigned char *mem_buf
;
98 /* Structure holding information relative to a single stop reply. We
99 keep a queue of these (really a singly-linked list) to push to GDB
103 /* Pointer to next in list. */
104 struct vstop_notif
*next
;
106 /* Thread or process that got the event. */
110 struct target_waitstatus status
;
113 /* The pending stop replies list head. */
114 static struct vstop_notif
*notif_queue
= NULL
;
116 /* Put a stop reply to the stop reply queue. */
119 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
121 struct vstop_notif
*new_notif
;
123 new_notif
= malloc (sizeof (*new_notif
));
124 new_notif
->next
= NULL
;
125 new_notif
->ptid
= ptid
;
126 new_notif
->status
= *status
;
130 struct vstop_notif
*tail
;
131 for (tail
= notif_queue
;
135 tail
->next
= new_notif
;
138 notif_queue
= new_notif
;
143 struct vstop_notif
*n
;
145 for (n
= notif_queue
; n
; n
= n
->next
)
148 fprintf (stderr
, "pending stop replies: %d\n", i
);
152 /* Place an event in the stop reply queue, and push a notification if
153 we aren't sending one yet. */
156 push_event (ptid_t ptid
, struct target_waitstatus
*status
)
158 gdb_assert (status
->kind
!= TARGET_WAITKIND_IGNORE
);
160 queue_stop_reply (ptid
, status
);
162 /* If this is the first stop reply in the queue, then inform GDB
163 about it, by sending a Stop notification. */
164 if (notif_queue
->next
== NULL
)
169 prepare_resume_reply (p
,
170 notif_queue
->ptid
, ¬if_queue
->status
);
171 putpkt_notif (own_buf
);
175 /* Get rid of the currently pending stop replies for PID. If PID is
176 -1, then apply to all processes. */
179 discard_queued_stop_replies (int pid
)
181 struct vstop_notif
*prev
= NULL
, *reply
, *next
;
183 for (reply
= notif_queue
; reply
; reply
= next
)
188 || ptid_get_pid (reply
->ptid
) == pid
)
190 if (reply
== notif_queue
)
193 prev
->next
= reply
->next
;
202 /* If there are more stop replies to push, push one now. */
205 send_next_stop_reply (char *own_buf
)
208 prepare_resume_reply (own_buf
,
210 ¬if_queue
->status
);
216 target_running (void)
218 return all_threads
.head
!= NULL
;
222 start_inferior (char **argv
)
224 char **new_argv
= argv
;
226 if (wrapper_argv
!= NULL
)
230 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
232 for (i
= 0; argv
[i
] != NULL
; i
++)
234 new_argv
= alloca (sizeof (char *) * count
);
236 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
237 new_argv
[count
++] = wrapper_argv
[i
];
238 for (i
= 0; argv
[i
] != NULL
; i
++)
239 new_argv
[count
++] = argv
[i
];
240 new_argv
[count
] = NULL
;
246 for (i
= 0; new_argv
[i
]; ++i
)
247 fprintf (stderr
, "new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
252 signal (SIGTTOU
, SIG_DFL
);
253 signal (SIGTTIN
, SIG_DFL
);
256 signal_pid
= create_inferior (new_argv
[0], new_argv
);
258 /* FIXME: we don't actually know at this point that the create
259 actually succeeded. We won't know that until we wait. */
260 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
265 signal (SIGTTOU
, SIG_IGN
);
266 signal (SIGTTIN
, SIG_IGN
);
267 terminal_fd
= fileno (stderr
);
268 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
269 tcsetpgrp (terminal_fd
, signal_pid
);
270 atexit (restore_old_foreground_pgrp
);
273 if (wrapper_argv
!= NULL
)
275 struct thread_resume resume_info
;
278 resume_info
.thread
= pid_to_ptid (signal_pid
);
279 resume_info
.kind
= resume_continue
;
282 ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
284 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
289 (*the_target
->resume
) (&resume_info
, 1);
291 mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
292 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
295 current_inferior
->last_resume_kind
= resume_stop
;
296 current_inferior
->last_status
= last_status
;
298 while (last_status
.value
.sig
!= TARGET_SIGNAL_TRAP
);
300 current_inferior
->last_resume_kind
= resume_stop
;
301 current_inferior
->last_status
= last_status
;
305 /* Wait till we are at 1st instruction in program, return new pid
306 (assuming success). */
307 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
309 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
310 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
312 current_inferior
->last_resume_kind
= resume_stop
;
313 current_inferior
->last_status
= last_status
;
320 attach_inferior (int pid
)
322 /* myattach should return -1 if attaching is unsupported,
323 0 if it succeeded, and call error() otherwise. */
325 if (myattach (pid
) != 0)
328 fprintf (stderr
, "Attached; pid = %d\n", pid
);
331 /* FIXME - It may be that we should get the SIGNAL_PID from the
332 attach function, so that it can be the main thread instead of
333 whichever we were told to attach to. */
338 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
340 /* GDB knows to ignore the first SIGSTOP after attaching to a running
341 process using the "attach" command, but this is different; it's
342 just using "target remote". Pretend it's just starting up. */
343 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
344 && last_status
.value
.sig
== TARGET_SIGNAL_STOP
)
345 last_status
.value
.sig
= TARGET_SIGNAL_TRAP
;
347 current_inferior
->last_resume_kind
= resume_stop
;
348 current_inferior
->last_status
= last_status
;
354 extern int remote_debug
;
356 /* Decode a qXfer read request. Return 0 if everything looks OK,
360 decode_xfer_read (char *buf
, char **annex
, CORE_ADDR
*ofs
, unsigned int *len
)
362 /* Extract and NUL-terminate the annex. */
364 while (*buf
&& *buf
!= ':')
370 /* After the read marker and annex, qXfer looks like a
371 traditional 'm' packet. */
372 decode_m_packet (buf
, ofs
, len
);
377 /* Write the response to a successful qXfer read. Returns the
378 length of the (binary) data stored in BUF, corresponding
379 to as much of DATA/LEN as we could fit. IS_MORE controls
380 the first character of the response. */
382 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
391 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
395 /* Handle all of the extended 'Q' packets. */
398 handle_general_set (char *own_buf
)
400 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
402 int numsigs
= (int) TARGET_SIGNAL_LAST
, i
;
403 const char *p
= own_buf
+ strlen ("QPassSignals:");
406 p
= decode_address_to_semicolon (&cursig
, p
);
407 for (i
= 0; i
< numsigs
; i
++)
413 /* Keep looping, to clear the remaining signals. */
416 p
= decode_address_to_semicolon (&cursig
, p
);
421 strcpy (own_buf
, "OK");
425 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
429 fprintf (stderr
, "[noack mode enabled]\n");
438 if (strncmp (own_buf
, "QNonStop:", 9) == 0)
440 char *mode
= own_buf
+ 9;
444 if (strcmp (mode
, "0") == 0)
446 else if (strcmp (mode
, "1") == 0)
450 /* We don't know what this mode is, so complain to
452 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
458 req_str
= req
? "non-stop" : "all-stop";
459 if (start_non_stop (req
) != 0)
461 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
469 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
475 if (target_supports_tracepoints ()
476 && handle_tracepoint_general_set (own_buf
))
479 /* Otherwise we didn't know what packet it was. Say we didn't
485 get_features_xml (const char *annex
)
487 /* gdbserver_xmltarget defines what to return when looking
488 for the "target.xml" file. Its contents can either be
489 verbatim XML code (prefixed with a '@') or else the name
490 of the actual XML file to be used in place of "target.xml".
492 This variable is set up from the auto-generated
493 init_registers_... routine for the current target. */
495 if (gdbserver_xmltarget
496 && strcmp (annex
, "target.xml") == 0)
498 if (*gdbserver_xmltarget
== '@')
499 return gdbserver_xmltarget
+ 1;
501 annex
= gdbserver_xmltarget
;
506 extern const char *const xml_builtin
[][2];
509 /* Look for the annex. */
510 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
511 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
514 if (xml_builtin
[i
][0] != NULL
)
515 return xml_builtin
[i
][1];
523 monitor_show_help (void)
525 monitor_output ("The following monitor commands are supported:\n");
526 monitor_output (" set debug <0|1>\n");
527 monitor_output (" Enable general debugging messages\n");
528 monitor_output (" set debug-hw-points <0|1>\n");
529 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
530 monitor_output (" set remote-debug <0|1>\n");
531 monitor_output (" Enable remote protocol debugging messages\n");
532 monitor_output (" exit\n");
533 monitor_output (" Quit GDBserver\n");
536 /* Read trace frame or inferior memory. */
539 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
543 if (current_traceframe
>= 0)
546 ULONGEST length
= len
;
548 if (traceframe_read_mem (current_traceframe
,
549 memaddr
, myaddr
, len
, &nbytes
))
551 /* Data read from trace buffer, we're done. */
552 if (nbytes
== length
)
554 if (!in_readonly_region (memaddr
, length
))
556 /* Otherwise we have a valid readonly case, fall through. */
557 /* (assume no half-trace half-real blocks for now) */
560 ret
= prepare_to_access_memory ();
563 ret
= read_inferior_memory (memaddr
, myaddr
, len
);
564 done_accessing_memory ();
570 /* Write trace frame or inferior memory. Actually, writing to trace
571 frames is forbidden. */
574 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
576 if (current_traceframe
>= 0)
582 ret
= prepare_to_access_memory ();
585 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
586 done_accessing_memory ();
592 /* Subroutine of handle_search_memory to simplify it. */
595 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
596 gdb_byte
*pattern
, unsigned pattern_len
,
597 gdb_byte
*search_buf
,
598 unsigned chunk_size
, unsigned search_buf_size
,
599 CORE_ADDR
*found_addrp
)
601 /* Prime the search buffer. */
603 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
) != 0)
605 warning ("Unable to access target memory at 0x%lx, halting search.",
610 /* Perform the search.
612 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
613 When we've scanned N bytes we copy the trailing bytes to the start and
614 read in another N bytes. */
616 while (search_space_len
>= pattern_len
)
619 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
623 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
625 if (found_ptr
!= NULL
)
627 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
628 *found_addrp
= found_addr
;
632 /* Not found in this chunk, skip to next chunk. */
634 /* Don't let search_space_len wrap here, it's unsigned. */
635 if (search_space_len
>= chunk_size
)
636 search_space_len
-= chunk_size
;
638 search_space_len
= 0;
640 if (search_space_len
>= pattern_len
)
642 unsigned keep_len
= search_buf_size
- chunk_size
;
643 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
646 /* Copy the trailing part of the previous iteration to the front
647 of the buffer for the next iteration. */
648 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
650 nr_to_read
= (search_space_len
- keep_len
< chunk_size
651 ? search_space_len
- keep_len
654 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
657 warning ("Unable to access target memory at 0x%lx, halting search.",
662 start_addr
+= chunk_size
;
671 /* Handle qSearch:memory packets. */
674 handle_search_memory (char *own_buf
, int packet_len
)
676 CORE_ADDR start_addr
;
677 CORE_ADDR search_space_len
;
679 unsigned int pattern_len
;
680 /* NOTE: also defined in find.c testcase. */
681 #define SEARCH_CHUNK_SIZE 16000
682 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
683 /* Buffer to hold memory contents for searching. */
684 gdb_byte
*search_buf
;
685 unsigned search_buf_size
;
687 CORE_ADDR found_addr
;
688 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
690 pattern
= malloc (packet_len
);
693 error ("Unable to allocate memory to perform the search");
694 strcpy (own_buf
, "E00");
697 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
698 packet_len
- cmd_name_len
,
699 &start_addr
, &search_space_len
,
700 pattern
, &pattern_len
) < 0)
703 error ("Error in parsing qSearch:memory packet");
704 strcpy (own_buf
, "E00");
708 search_buf_size
= chunk_size
+ pattern_len
- 1;
710 /* No point in trying to allocate a buffer larger than the search space. */
711 if (search_space_len
< search_buf_size
)
712 search_buf_size
= search_space_len
;
714 search_buf
= malloc (search_buf_size
);
715 if (search_buf
== NULL
)
718 error ("Unable to allocate memory to perform the search");
719 strcpy (own_buf
, "E00");
723 found
= handle_search_memory_1 (start_addr
, search_space_len
,
724 pattern
, pattern_len
,
725 search_buf
, chunk_size
, search_buf_size
,
729 sprintf (own_buf
, "1,%lx", (long) found_addr
);
731 strcpy (own_buf
, "0");
733 strcpy (own_buf
, "E00");
739 #define require_running(BUF) \
740 if (!target_running ()) \
746 /* Handle monitor commands not handled by target-specific handlers. */
749 handle_monitor_command (char *mon
)
751 if (strcmp (mon
, "set debug 1") == 0)
754 monitor_output ("Debug output enabled.\n");
756 else if (strcmp (mon
, "set debug 0") == 0)
759 monitor_output ("Debug output disabled.\n");
761 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
764 monitor_output ("H/W point debugging output enabled.\n");
766 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
769 monitor_output ("H/W point debugging output disabled.\n");
771 else if (strcmp (mon
, "set remote-debug 1") == 0)
774 monitor_output ("Protocol debug output enabled.\n");
776 else if (strcmp (mon
, "set remote-debug 0") == 0)
779 monitor_output ("Protocol debug output disabled.\n");
781 else if (strcmp (mon
, "help") == 0)
782 monitor_show_help ();
783 else if (strcmp (mon
, "exit") == 0)
787 monitor_output ("Unknown monitor command.\n\n");
788 monitor_show_help ();
794 handle_threads_qxfer_proper (struct buffer
*buffer
)
796 struct inferior_list_entry
*thread
;
798 buffer_grow_str (buffer
, "<threads>\n");
800 for (thread
= all_threads
.head
; thread
; thread
= thread
->next
)
802 ptid_t ptid
= thread_to_gdb_id ((struct thread_info
*)thread
);
807 write_ptid (ptid_s
, ptid
);
809 if (the_target
->core_of_thread
)
810 core
= (*the_target
->core_of_thread
) (ptid
);
814 sprintf (core_s
, "%d", core
);
815 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
820 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
825 buffer_grow_str0 (buffer
, "</threads>\n");
829 handle_threads_qxfer (const char *annex
,
830 unsigned char *readbuf
,
831 CORE_ADDR offset
, int length
)
833 static char *result
= 0;
834 static unsigned int result_length
= 0;
836 if (annex
&& strcmp (annex
, "") != 0)
841 struct buffer buffer
;
842 /* When asked for data at offset 0, generate everything and store into
843 'result'. Successive reads will be served off 'result'. */
847 buffer_init (&buffer
);
849 handle_threads_qxfer_proper (&buffer
);
851 result
= buffer_finish (&buffer
);
852 result_length
= strlen (result
);
853 buffer_free (&buffer
);
856 if (offset
>= result_length
)
858 /* We're out of data. */
865 if (length
> result_length
- offset
)
866 length
= result_length
- offset
;
868 memcpy (readbuf
, result
+ offset
, length
);
874 /* Table used by the crc32 function to calcuate the checksum. */
876 static unsigned int crc32_table
[256] =
879 /* Compute 32 bit CRC from inferior memory.
881 On success, return 32 bit CRC.
882 On failure, return (unsigned long long) -1. */
884 static unsigned long long
885 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
889 /* Initialize the CRC table and the decoding table. */
893 for (i
= 0; i
< 256; i
++)
895 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
896 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
903 unsigned char byte
= 0;
905 /* Return failure if memory read fails. */
906 if (read_inferior_memory (base
, &byte
, 1) != 0)
907 return (unsigned long long) -1;
909 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
912 return (unsigned long long) crc
;
915 /* Handle all of the extended 'q' packets. */
917 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
919 static struct inferior_list_entry
*thread_ptr
;
921 /* Reply the current thread id. */
922 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
925 require_running (own_buf
);
927 if (!ptid_equal (general_thread
, null_ptid
)
928 && !ptid_equal (general_thread
, minus_one_ptid
))
929 gdb_id
= general_thread
;
932 thread_ptr
= all_threads
.head
;
933 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
936 sprintf (own_buf
, "QC");
938 own_buf
= write_ptid (own_buf
, gdb_id
);
942 if (strcmp ("qSymbol::", own_buf
) == 0)
944 /* GDB is suggesting new symbols have been loaded. This may
945 mean a new shared library has been detected as loaded, so
946 take the opportunity to check if breakpoints we think are
947 inserted, still are. Note that it isn't guaranteed that
948 we'll see this when a shared library is loaded, and nor will
949 we see this for unloads (although breakpoints in unloaded
950 libraries shouldn't trigger), as GDB may not find symbols for
951 the library at all. We also re-validate breakpoints when we
952 see a second GDB breakpoint for the same address, and or when
953 we access breakpoint shadows. */
954 validate_breakpoints ();
956 if (target_supports_tracepoints ())
957 tracepoint_look_up_symbols ();
959 if (target_running () && the_target
->look_up_symbols
!= NULL
)
960 (*the_target
->look_up_symbols
) ();
962 strcpy (own_buf
, "OK");
966 if (!disable_packet_qfThreadInfo
)
968 if (strcmp ("qfThreadInfo", own_buf
) == 0)
972 require_running (own_buf
);
973 thread_ptr
= all_threads
.head
;
976 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
977 write_ptid (own_buf
, gdb_id
);
978 thread_ptr
= thread_ptr
->next
;
982 if (strcmp ("qsThreadInfo", own_buf
) == 0)
986 require_running (own_buf
);
987 if (thread_ptr
!= NULL
)
990 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
991 write_ptid (own_buf
, gdb_id
);
992 thread_ptr
= thread_ptr
->next
;
997 sprintf (own_buf
, "l");
1003 if (the_target
->read_offsets
!= NULL
1004 && strcmp ("qOffsets", own_buf
) == 0)
1006 CORE_ADDR text
, data
;
1008 require_running (own_buf
);
1009 if (the_target
->read_offsets (&text
, &data
))
1010 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
1011 (long)text
, (long)data
, (long)data
);
1013 write_enn (own_buf
);
1018 if (the_target
->qxfer_spu
!= NULL
1019 && strncmp ("qXfer:spu:read:", own_buf
, 15) == 0)
1025 unsigned char *spu_buf
;
1027 require_running (own_buf
);
1028 strcpy (own_buf
, "E00");
1029 if (decode_xfer_read (own_buf
+ 15, &annex
, &ofs
, &len
) < 0)
1031 if (len
> PBUFSIZ
- 2)
1033 spu_buf
= malloc (len
+ 1);
1037 n
= (*the_target
->qxfer_spu
) (annex
, spu_buf
, NULL
, ofs
, len
+ 1);
1039 write_enn (own_buf
);
1041 *new_packet_len_p
= write_qxfer_response (own_buf
, spu_buf
, len
, 1);
1043 *new_packet_len_p
= write_qxfer_response (own_buf
, spu_buf
, n
, 0);
1049 if (the_target
->qxfer_spu
!= NULL
1050 && strncmp ("qXfer:spu:write:", own_buf
, 16) == 0)
1056 unsigned char *spu_buf
;
1058 require_running (own_buf
);
1059 strcpy (own_buf
, "E00");
1060 spu_buf
= malloc (packet_len
- 15);
1063 if (decode_xfer_write (own_buf
+ 16, packet_len
- 16, &annex
,
1064 &ofs
, &len
, spu_buf
) < 0)
1070 n
= (*the_target
->qxfer_spu
)
1071 (annex
, NULL
, (unsigned const char *)spu_buf
, ofs
, len
);
1073 write_enn (own_buf
);
1075 sprintf (own_buf
, "%x", n
);
1081 if (the_target
->read_auxv
!= NULL
1082 && strncmp ("qXfer:auxv:read:", own_buf
, 16) == 0)
1084 unsigned char *data
;
1090 require_running (own_buf
);
1092 /* Reject any annex; grab the offset and length. */
1093 if (decode_xfer_read (own_buf
+ 16, &annex
, &ofs
, &len
) < 0
1094 || annex
[0] != '\0')
1096 strcpy (own_buf
, "E00");
1100 /* Read one extra byte, as an indicator of whether there is
1102 if (len
> PBUFSIZ
- 2)
1104 data
= malloc (len
+ 1);
1107 write_enn (own_buf
);
1110 n
= (*the_target
->read_auxv
) (ofs
, data
, len
+ 1);
1112 write_enn (own_buf
);
1114 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1116 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1123 if (strncmp ("qXfer:features:read:", own_buf
, 20) == 0)
1126 unsigned int len
, total_len
;
1127 const char *document
;
1130 require_running (own_buf
);
1132 /* Grab the annex, offset, and length. */
1133 if (decode_xfer_read (own_buf
+ 20, &annex
, &ofs
, &len
) < 0)
1135 strcpy (own_buf
, "E00");
1139 /* Now grab the correct annex. */
1140 document
= get_features_xml (annex
);
1141 if (document
== NULL
)
1143 strcpy (own_buf
, "E00");
1147 total_len
= strlen (document
);
1148 if (len
> PBUFSIZ
- 2)
1151 if (ofs
> total_len
)
1152 write_enn (own_buf
);
1153 else if (len
< total_len
- ofs
)
1154 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
1157 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
1158 total_len
- ofs
, 0);
1163 if (strncmp ("qXfer:libraries:read:", own_buf
, 21) == 0)
1166 unsigned int len
, total_len
;
1168 struct inferior_list_entry
*dll_ptr
;
1171 require_running (own_buf
);
1173 /* Reject any annex; grab the offset and length. */
1174 if (decode_xfer_read (own_buf
+ 21, &annex
, &ofs
, &len
) < 0
1175 || annex
[0] != '\0')
1177 strcpy (own_buf
, "E00");
1181 /* Over-estimate the necessary memory. Assume that every character
1182 in the library name must be escaped. */
1184 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
1185 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
1187 document
= malloc (total_len
);
1188 if (document
== NULL
)
1190 write_enn (own_buf
);
1193 strcpy (document
, "<library-list>\n");
1194 p
= document
+ strlen (document
);
1196 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
1198 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
1201 strcpy (p
, " <library name=\"");
1203 name
= xml_escape_text (dll
->name
);
1207 strcpy (p
, "\"><segment address=\"");
1209 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1211 strcpy (p
, "\"/></library>\n");
1215 strcpy (p
, "</library-list>\n");
1217 total_len
= strlen (document
);
1218 if (len
> PBUFSIZ
- 2)
1221 if (ofs
> total_len
)
1222 write_enn (own_buf
);
1223 else if (len
< total_len
- ofs
)
1224 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
1227 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
1228 total_len
- ofs
, 0);
1234 if (the_target
->qxfer_osdata
!= NULL
1235 && strncmp ("qXfer:osdata:read:", own_buf
, 18) == 0)
1241 unsigned char *workbuf
;
1243 strcpy (own_buf
, "E00");
1244 if (decode_xfer_read (own_buf
+ 18, &annex
, &ofs
, &len
) < 0)
1246 if (len
> PBUFSIZ
- 2)
1248 workbuf
= malloc (len
+ 1);
1252 n
= (*the_target
->qxfer_osdata
) (annex
, workbuf
, NULL
, ofs
, len
+ 1);
1254 write_enn (own_buf
);
1256 *new_packet_len_p
= write_qxfer_response (own_buf
, workbuf
, len
, 1);
1258 *new_packet_len_p
= write_qxfer_response (own_buf
, workbuf
, n
, 0);
1264 if (the_target
->qxfer_siginfo
!= NULL
1265 && strncmp ("qXfer:siginfo:read:", own_buf
, 19) == 0)
1267 unsigned char *data
;
1273 require_running (own_buf
);
1275 /* Reject any annex; grab the offset and length. */
1276 if (decode_xfer_read (own_buf
+ 19, &annex
, &ofs
, &len
) < 0
1277 || annex
[0] != '\0')
1279 strcpy (own_buf
, "E00");
1283 /* Read one extra byte, as an indicator of whether there is
1285 if (len
> PBUFSIZ
- 2)
1287 data
= malloc (len
+ 1);
1290 n
= (*the_target
->qxfer_siginfo
) (annex
, data
, NULL
, ofs
, len
+ 1);
1292 write_enn (own_buf
);
1294 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1296 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1302 if (the_target
->qxfer_siginfo
!= NULL
1303 && strncmp ("qXfer:siginfo:write:", own_buf
, 20) == 0)
1309 unsigned char *data
;
1311 require_running (own_buf
);
1313 strcpy (own_buf
, "E00");
1314 data
= malloc (packet_len
- 19);
1317 if (decode_xfer_write (own_buf
+ 20, packet_len
- 20, &annex
,
1318 &ofs
, &len
, data
) < 0)
1324 n
= (*the_target
->qxfer_siginfo
)
1325 (annex
, NULL
, (unsigned const char *)data
, ofs
, len
);
1327 write_enn (own_buf
);
1329 sprintf (own_buf
, "%x", n
);
1335 if (strncmp ("qXfer:threads:read:", own_buf
, 19) == 0)
1337 unsigned char *data
;
1343 require_running (own_buf
);
1345 /* Reject any annex; grab the offset and length. */
1346 if (decode_xfer_read (own_buf
+ 19, &annex
, &ofs
, &len
) < 0
1347 || annex
[0] != '\0')
1349 strcpy (own_buf
, "E00");
1353 /* Read one extra byte, as an indicator of whether there is
1355 if (len
> PBUFSIZ
- 2)
1357 data
= malloc (len
+ 1);
1360 n
= handle_threads_qxfer (annex
, data
, ofs
, len
+ 1);
1362 write_enn (own_buf
);
1364 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1366 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1372 if (strncmp ("qXfer:statictrace:read:", own_buf
,
1373 sizeof ("qXfer:statictrace:read:") -1) == 0)
1375 unsigned char *data
;
1381 require_running (own_buf
);
1383 if (current_traceframe
== -1)
1385 write_enn (own_buf
);
1389 /* Reject any annex; grab the offset and length. */
1390 if (decode_xfer_read (own_buf
+ sizeof ("qXfer:statictrace:read:") -1,
1391 &annex
, &ofs
, &len
) < 0
1392 || annex
[0] != '\0')
1394 strcpy (own_buf
, "E00");
1398 /* Read one extra byte, as an indicator of whether there is
1400 if (len
> PBUFSIZ
- 2)
1402 data
= malloc (len
+ 1);
1406 if (traceframe_read_sdata (current_traceframe
, ofs
,
1407 data
, len
+ 1, &nbytes
))
1408 write_enn (own_buf
);
1409 else if (nbytes
> len
)
1410 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1412 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, nbytes
, 0);
1418 /* Protocol features query. */
1419 if (strncmp ("qSupported", own_buf
, 10) == 0
1420 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1422 char *p
= &own_buf
[10];
1423 int gdb_supports_qRelocInsn
= 0;
1425 /* Start processing qSupported packet. */
1426 target_process_qsupported (NULL
);
1428 /* Process each feature being provided by GDB. The first
1429 feature will follow a ':', and latter features will follow
1433 char **qsupported
= NULL
;
1437 /* Two passes, to avoid nested strtok calls in
1438 target_process_qsupported. */
1439 for (p
= strtok (p
+ 1, ";");
1441 p
= strtok (NULL
, ";"))
1444 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
1445 qsupported
[count
- 1] = xstrdup (p
);
1448 for (i
= 0; i
< count
; i
++)
1451 if (strcmp (p
, "multiprocess+") == 0)
1453 /* GDB supports and wants multi-process support if
1455 if (target_supports_multi_process ())
1458 else if (strcmp (p
, "qRelocInsn+") == 0)
1460 /* GDB supports relocate instruction requests. */
1461 gdb_supports_qRelocInsn
= 1;
1464 target_process_qsupported (p
);
1472 sprintf (own_buf
, "PacketSize=%x;QPassSignals+", PBUFSIZ
- 1);
1474 /* We do not have any hook to indicate whether the target backend
1475 supports qXfer:libraries:read, so always report it. */
1476 strcat (own_buf
, ";qXfer:libraries:read+");
1478 if (the_target
->read_auxv
!= NULL
)
1479 strcat (own_buf
, ";qXfer:auxv:read+");
1481 if (the_target
->qxfer_spu
!= NULL
)
1482 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
1484 if (the_target
->qxfer_siginfo
!= NULL
)
1485 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1487 /* We always report qXfer:features:read, as targets may
1488 install XML files on a subsequent call to arch_setup.
1489 If we reported to GDB on startup that we don't support
1490 qXfer:feature:read at all, we will never be re-queried. */
1491 strcat (own_buf
, ";qXfer:features:read+");
1493 if (transport_is_reliable
)
1494 strcat (own_buf
, ";QStartNoAckMode+");
1496 if (the_target
->qxfer_osdata
!= NULL
)
1497 strcat (own_buf
, ";qXfer:osdata:read+");
1499 if (target_supports_multi_process ())
1500 strcat (own_buf
, ";multiprocess+");
1502 if (target_supports_non_stop ())
1503 strcat (own_buf
, ";QNonStop+");
1505 strcat (own_buf
, ";qXfer:threads:read+");
1507 if (target_supports_tracepoints ())
1509 strcat (own_buf
, ";ConditionalTracepoints+");
1510 strcat (own_buf
, ";TraceStateVariables+");
1511 strcat (own_buf
, ";TracepointSource+");
1512 strcat (own_buf
, ";DisconnectedTracing+");
1513 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
1514 strcat (own_buf
, ";FastTracepoints+");
1515 strcat (own_buf
, ";StaticTracepoints+");
1516 strcat (own_buf
, ";qXfer:statictrace:read+");
1522 /* Thread-local storage support. */
1523 if (the_target
->get_tls_address
!= NULL
1524 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
1526 char *p
= own_buf
+ 12;
1527 CORE_ADDR parts
[2], address
= 0;
1529 ptid_t ptid
= null_ptid
;
1531 require_running (own_buf
);
1533 for (i
= 0; i
< 3; i
++)
1541 p2
= strchr (p
, ',');
1554 ptid
= read_ptid (p
, NULL
);
1556 decode_address (&parts
[i
- 1], p
, len
);
1560 if (p
!= NULL
|| i
< 3)
1564 struct thread_info
*thread
= find_thread_ptid (ptid
);
1569 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
1575 strcpy (own_buf
, paddress(address
));
1580 write_enn (own_buf
);
1584 /* Otherwise, pretend we do not understand this packet. */
1587 /* Windows OS Thread Information Block address support. */
1588 if (the_target
->get_tib_address
!= NULL
1589 && strncmp ("qGetTIBAddr:", own_buf
, 12) == 0)
1594 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
1596 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
1599 strcpy (own_buf
, paddress(tlb
));
1604 write_enn (own_buf
);
1610 /* Handle "monitor" commands. */
1611 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
1613 char *mon
= malloc (PBUFSIZ
);
1614 int len
= strlen (own_buf
+ 6);
1618 write_enn (own_buf
);
1622 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
1624 write_enn (own_buf
);
1628 mon
[len
/ 2] = '\0';
1632 if (the_target
->handle_monitor_command
== NULL
1633 || (*the_target
->handle_monitor_command
) (mon
) == 0)
1634 /* Default processing. */
1635 handle_monitor_command (mon
);
1641 if (strncmp ("qSearch:memory:", own_buf
, sizeof ("qSearch:memory:") - 1) == 0)
1643 require_running (own_buf
);
1644 handle_search_memory (own_buf
, packet_len
);
1648 if (strcmp (own_buf
, "qAttached") == 0
1649 || strncmp (own_buf
, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1651 struct process_info
*process
;
1653 if (own_buf
[sizeof ("qAttached") - 1])
1655 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
1656 process
= (struct process_info
*)
1657 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
1661 require_running (own_buf
);
1662 process
= current_process ();
1665 if (process
== NULL
)
1667 write_enn (own_buf
);
1671 strcpy (own_buf
, process
->attached
? "1" : "0");
1675 if (strncmp ("qCRC:", own_buf
, 5) == 0)
1677 /* CRC check (compare-section). */
1681 unsigned long long crc
;
1683 require_running (own_buf
);
1684 base
= strtoul (own_buf
+ 5, &comma
, 16);
1685 if (*comma
++ != ',')
1687 write_enn (own_buf
);
1690 len
= strtoul (comma
, NULL
, 16);
1691 crc
= crc32 (base
, len
, 0xffffffff);
1692 /* Check for memory failure. */
1693 if (crc
== (unsigned long long) -1)
1695 write_enn (own_buf
);
1698 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
1702 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
1705 /* Otherwise we didn't know what packet it was. Say we didn't
1710 static void gdb_wants_all_threads_stopped (void);
1712 /* Parse vCont packets. */
1714 handle_v_cont (char *own_buf
)
1718 struct thread_resume
*resume_info
;
1719 struct thread_resume default_action
= {{0}};
1721 /* Count the number of semicolons in the packet. There should be one
1722 for every action. */
1728 p
= strchr (p
, ';');
1731 resume_info
= malloc (n
* sizeof (resume_info
[0]));
1732 if (resume_info
== NULL
)
1740 if (p
[0] == 's' || p
[0] == 'S')
1741 resume_info
[i
].kind
= resume_step
;
1742 else if (p
[0] == 'c' || p
[0] == 'C')
1743 resume_info
[i
].kind
= resume_continue
;
1744 else if (p
[0] == 't')
1745 resume_info
[i
].kind
= resume_stop
;
1749 if (p
[0] == 'S' || p
[0] == 'C')
1752 sig
= strtol (p
+ 1, &q
, 16);
1757 if (!target_signal_to_host_p (sig
))
1759 resume_info
[i
].sig
= target_signal_to_host (sig
);
1763 resume_info
[i
].sig
= 0;
1769 resume_info
[i
].thread
= minus_one_ptid
;
1770 default_action
= resume_info
[i
];
1772 /* Note: we don't increment i here, we'll overwrite this entry
1773 the next time through. */
1775 else if (p
[0] == ':')
1777 ptid_t ptid
= read_ptid (p
+ 1, &q
);
1782 if (p
[0] != ';' && p
[0] != 0)
1785 resume_info
[i
].thread
= ptid
;
1792 resume_info
[i
] = default_action
;
1794 /* Still used in occasional places in the backend. */
1796 && !ptid_equal (resume_info
[0].thread
, minus_one_ptid
)
1797 && resume_info
[0].kind
!= resume_stop
)
1798 cont_thread
= resume_info
[0].thread
;
1800 cont_thread
= minus_one_ptid
;
1801 set_desired_inferior (0);
1806 (*the_target
->resume
) (resume_info
, n
);
1814 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
1816 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
1817 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
1818 current_inferior
->last_status
= last_status
;
1820 /* From the client's perspective, all-stop mode always stops all
1821 threads implicitly (and the target backend has already done
1822 so by now). Tag all threads as "want-stopped", so we don't
1823 resume them implicitly without the client telling us to. */
1824 gdb_wants_all_threads_stopped ();
1825 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1826 disable_async_io ();
1828 if (last_status
.kind
== TARGET_WAITKIND_EXITED
1829 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
1830 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
1835 write_enn (own_buf
);
1840 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1842 handle_v_attach (char *own_buf
)
1846 pid
= strtol (own_buf
+ 8, NULL
, 16);
1847 if (pid
!= 0 && attach_inferior (pid
) == 0)
1849 /* Don't report shared library events after attaching, even if
1850 some libraries are preloaded. GDB will always poll the
1851 library list. Avoids the "stopped by shared library event"
1852 notice on the GDB side. */
1857 /* In non-stop, we don't send a resume reply. Stop events
1858 will follow up using the normal notification
1863 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1869 write_enn (own_buf
);
1874 /* Run a new program. Return 1 if successful, 0 if failure. */
1876 handle_v_run (char *own_buf
)
1878 char *p
, *next_p
, **new_argv
;
1882 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
1888 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
1889 if (new_argv
== NULL
)
1891 write_enn (own_buf
);
1896 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
1898 next_p
= strchr (p
, ';');
1900 next_p
= p
+ strlen (p
);
1902 if (i
== 0 && p
== next_p
)
1906 /* FIXME: Fail request if out of memory instead of dying. */
1907 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
1908 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
1909 new_argv
[i
][(next_p
- p
) / 2] = '\0';
1918 if (new_argv
[0] == NULL
)
1920 /* GDB didn't specify a program to run. Use the program from the
1921 last run with the new argument list. */
1923 if (program_argv
== NULL
)
1925 /* FIXME: new_argv memory leak */
1926 write_enn (own_buf
);
1930 new_argv
[0] = strdup (program_argv
[0]);
1931 if (new_argv
[0] == NULL
)
1933 /* FIXME: new_argv memory leak */
1934 write_enn (own_buf
);
1939 /* Free the old argv and install the new one. */
1940 freeargv (program_argv
);
1941 program_argv
= new_argv
;
1943 start_inferior (program_argv
);
1944 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
1946 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1948 /* In non-stop, sending a resume reply doesn't set the general
1949 thread, but GDB assumes a vRun sets it (this is so GDB can
1950 query which is the main thread of the new inferior. */
1952 general_thread
= last_ptid
;
1958 write_enn (own_buf
);
1963 /* Kill process. Return 1 if successful, 0 if failure. */
1965 handle_v_kill (char *own_buf
)
1968 char *p
= &own_buf
[6];
1970 pid
= strtol (p
, NULL
, 16);
1973 if (pid
!= 0 && kill_inferior (pid
) == 0)
1975 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
1976 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
1977 last_ptid
= pid_to_ptid (pid
);
1978 discard_queued_stop_replies (pid
);
1984 write_enn (own_buf
);
1989 /* Handle a 'vStopped' packet. */
1991 handle_v_stopped (char *own_buf
)
1993 /* If we're waiting for GDB to acknowledge a pending stop reply,
1994 consider that done. */
1997 struct vstop_notif
*head
;
2000 fprintf (stderr
, "vStopped: acking %s\n",
2001 target_pid_to_str (notif_queue
->ptid
));
2004 notif_queue
= notif_queue
->next
;
2008 /* Push another stop reply, or if there are no more left, an OK. */
2009 send_next_stop_reply (own_buf
);
2012 /* Handle all of the extended 'v' packets. */
2014 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2016 if (!disable_packet_vCont
)
2018 if (strncmp (own_buf
, "vCont;", 6) == 0)
2020 require_running (own_buf
);
2021 handle_v_cont (own_buf
);
2025 if (strncmp (own_buf
, "vCont?", 6) == 0)
2027 strcpy (own_buf
, "vCont;c;C;s;S;t");
2032 if (strncmp (own_buf
, "vFile:", 6) == 0
2033 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2036 if (strncmp (own_buf
, "vAttach;", 8) == 0)
2038 if (!multi_process
&& target_running ())
2040 fprintf (stderr
, "Already debugging a process\n");
2041 write_enn (own_buf
);
2044 handle_v_attach (own_buf
);
2048 if (strncmp (own_buf
, "vRun;", 5) == 0)
2050 if (!multi_process
&& target_running ())
2052 fprintf (stderr
, "Already debugging a process\n");
2053 write_enn (own_buf
);
2056 handle_v_run (own_buf
);
2060 if (strncmp (own_buf
, "vKill;", 6) == 0)
2062 if (!target_running ())
2064 fprintf (stderr
, "No process to kill\n");
2065 write_enn (own_buf
);
2068 handle_v_kill (own_buf
);
2072 if (strncmp (own_buf
, "vStopped", 8) == 0)
2074 handle_v_stopped (own_buf
);
2078 /* Otherwise we didn't know what packet it was. Say we didn't
2084 /* Resume inferior and wait for another event. In non-stop mode,
2085 don't really wait here, but return immediatelly to the event
2088 myresume (char *own_buf
, int step
, int sig
)
2090 struct thread_resume resume_info
[2];
2092 int valid_cont_thread
;
2094 set_desired_inferior (0);
2096 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2097 && !ptid_equal (cont_thread
, minus_one_ptid
));
2099 if (step
|| sig
|| valid_cont_thread
)
2101 resume_info
[0].thread
2102 = ((struct inferior_list_entry
*) current_inferior
)->id
;
2104 resume_info
[0].kind
= resume_step
;
2106 resume_info
[0].kind
= resume_continue
;
2107 resume_info
[0].sig
= sig
;
2111 if (!valid_cont_thread
)
2113 resume_info
[n
].thread
= minus_one_ptid
;
2114 resume_info
[n
].kind
= resume_continue
;
2115 resume_info
[n
].sig
= 0;
2122 (*the_target
->resume
) (resume_info
, n
);
2128 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2130 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2131 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2133 current_inferior
->last_resume_kind
= resume_stop
;
2134 current_inferior
->last_status
= last_status
;
2137 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2138 disable_async_io ();
2140 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2141 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2142 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2146 /* Callback for for_each_inferior. Make a new stop reply for each
2150 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2152 struct thread_info
*thread
= (struct thread_info
*) entry
;
2154 /* For now, assume targets that don't have this callback also don't
2155 manage the thread's last_status field. */
2156 if (the_target
->thread_stopped
== NULL
)
2158 struct target_waitstatus status
;
2160 status
.kind
= TARGET_WAITKIND_STOPPED
;
2161 status
.value
.sig
= TARGET_SIGNAL_TRAP
;
2163 /* Pass the last stop reply back to GDB, but don't notify
2165 queue_stop_reply (entry
->id
, &thread
->last_status
);
2169 if (thread_stopped (thread
))
2172 fprintf (stderr
, "Reporting thread %s as already stopped with %s\n",
2173 target_pid_to_str (entry
->id
),
2174 target_waitstatus_to_string (&thread
->last_status
));
2176 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2178 /* Pass the last stop reply back to GDB, but don't notify
2180 queue_stop_reply (entry
->id
, &thread
->last_status
);
2187 /* Set this inferior threads's state as "want-stopped". We won't
2188 resume this thread until the client gives us another action for
2192 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2194 struct thread_info
*thread
= (struct thread_info
*) entry
;
2196 thread
->last_resume_kind
= resume_stop
;
2198 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2200 /* Most threads are stopped implicitly (all-stop); tag that with
2202 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2203 thread
->last_status
.value
.sig
= TARGET_SIGNAL_0
;
2207 /* Set all threads' states as "want-stopped". */
2210 gdb_wants_all_threads_stopped (void)
2212 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2215 /* Clear the gdb_detached flag of every process. */
2218 gdb_reattached_process (struct inferior_list_entry
*entry
)
2220 struct process_info
*process
= (struct process_info
*) entry
;
2222 process
->gdb_detached
= 0;
2225 /* Status handler for the '?' packet. */
2228 handle_status (char *own_buf
)
2230 /* GDB is connected, don't forward events to the target anymore. */
2231 for_each_inferior (&all_processes
, gdb_reattached_process
);
2233 /* In non-stop mode, we must send a stop reply for each stopped
2234 thread. In all-stop mode, just send one for the first stopped
2239 discard_queued_stop_replies (-1);
2240 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2242 /* The first is sent immediatly. OK is sent if there is no
2243 stopped thread, which is the same handling of the vStopped
2244 packet (by design). */
2245 send_next_stop_reply (own_buf
);
2250 stabilize_threads ();
2251 gdb_wants_all_threads_stopped ();
2253 if (all_threads
.head
)
2255 struct target_waitstatus status
;
2257 status
.kind
= TARGET_WAITKIND_STOPPED
;
2258 status
.value
.sig
= TARGET_SIGNAL_TRAP
;
2259 prepare_resume_reply (own_buf
,
2260 all_threads
.head
->id
, &status
);
2263 strcpy (own_buf
, "W00");
2268 gdbserver_version (void)
2270 printf ("GNU gdbserver %s%s\n"
2271 "Copyright (C) 2010 Free Software Foundation, Inc.\n"
2272 "gdbserver is free software, covered by the GNU General Public License.\n"
2273 "This gdbserver was configured as \"%s\"\n",
2274 PKGVERSION
, version
, host_name
);
2278 gdbserver_usage (FILE *stream
)
2280 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2281 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2282 "\tgdbserver [OPTIONS] --multi COMM\n"
2284 "COMM may either be a tty device (for serial debugging), or \n"
2285 "HOST:PORT to listen for a TCP connection.\n"
2288 " --debug Enable general debugging output.\n"
2289 " --remote-debug Enable remote protocol debugging output.\n"
2290 " --version Display version information and exit.\n"
2291 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n");
2292 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
2293 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
2297 gdbserver_show_disableable (FILE *stream
)
2299 fprintf (stream
, "Disableable packets:\n"
2300 " vCont \tAll vCont packets\n"
2301 " qC \tQuerying the current thread\n"
2302 " qfThreadInfo\tThread listing\n"
2303 " Tthread \tPassing the thread specifier in the T stop reply packet\n"
2304 " threads \tAll of the above\n");
2308 #undef require_running
2309 #define require_running(BUF) \
2310 if (!target_running ()) \
2317 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
2319 int pid
= * (int *) args
;
2321 if (ptid_get_pid (entry
->id
) == pid
)
2328 kill_inferior_callback (struct inferior_list_entry
*entry
)
2330 struct process_info
*process
= (struct process_info
*) entry
;
2331 int pid
= ptid_get_pid (process
->head
.id
);
2333 kill_inferior (pid
);
2334 discard_queued_stop_replies (pid
);
2337 /* Callback for for_each_inferior to detach or kill the inferior,
2338 depending on whether we attached to it or not.
2339 We inform the user whether we're detaching or killing the process
2340 as this is only called when gdbserver is about to exit. */
2343 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
2345 struct process_info
*process
= (struct process_info
*) entry
;
2346 int pid
= ptid_get_pid (process
->head
.id
);
2348 if (process
->attached
)
2349 detach_inferior (pid
);
2351 kill_inferior (pid
);
2353 discard_queued_stop_replies (pid
);
2356 /* for_each_inferior callback for detach_or_kill_for_exit to print
2357 the pids of started inferiors. */
2360 print_started_pid (struct inferior_list_entry
*entry
)
2362 struct process_info
*process
= (struct process_info
*) entry
;
2364 if (! process
->attached
)
2366 int pid
= ptid_get_pid (process
->head
.id
);
2367 fprintf (stderr
, " %d", pid
);
2371 /* for_each_inferior callback for detach_or_kill_for_exit to print
2372 the pids of attached inferiors. */
2375 print_attached_pid (struct inferior_list_entry
*entry
)
2377 struct process_info
*process
= (struct process_info
*) entry
;
2379 if (process
->attached
)
2381 int pid
= ptid_get_pid (process
->head
.id
);
2382 fprintf (stderr
, " %d", pid
);
2386 /* Call this when exiting gdbserver with possible inferiors that need
2387 to be killed or detached from. */
2390 detach_or_kill_for_exit (void)
2392 /* First print a list of the inferiors we will be killing/detaching.
2393 This is to assist the user, for example, in case the inferior unexpectedly
2394 dies after we exit: did we screw up or did the inferior exit on its own?
2395 Having this info will save some head-scratching. */
2397 if (have_started_inferiors_p ())
2399 fprintf (stderr
, "Killing process(es):");
2400 for_each_inferior (&all_processes
, print_started_pid
);
2401 fprintf (stderr
, "\n");
2403 if (have_attached_inferiors_p ())
2405 fprintf (stderr
, "Detaching process(es):");
2406 for_each_inferior (&all_processes
, print_attached_pid
);
2407 fprintf (stderr
, "\n");
2410 /* Now we can kill or detach the inferiors. */
2412 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
2416 join_inferiors_callback (struct inferior_list_entry
*entry
)
2418 struct process_info
*process
= (struct process_info
*) entry
;
2420 /* If we are attached, then we can exit. Otherwise, we need to hang
2421 around doing nothing, until the child is gone. */
2422 if (!process
->attached
)
2423 join_inferior (ptid_get_pid (process
->head
.id
));
2427 main (int argc
, char *argv
[])
2431 char *arg_end
, *port
;
2432 char **next_arg
= &argv
[1];
2437 while (*next_arg
!= NULL
&& **next_arg
== '-')
2439 if (strcmp (*next_arg
, "--version") == 0)
2441 gdbserver_version ();
2444 else if (strcmp (*next_arg
, "--help") == 0)
2446 gdbserver_usage (stdout
);
2449 else if (strcmp (*next_arg
, "--attach") == 0)
2451 else if (strcmp (*next_arg
, "--multi") == 0)
2453 else if (strcmp (*next_arg
, "--wrapper") == 0)
2457 wrapper_argv
= next_arg
;
2458 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
2461 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
2463 gdbserver_usage (stderr
);
2467 /* Consume the "--". */
2470 else if (strcmp (*next_arg
, "--debug") == 0)
2472 else if (strcmp (*next_arg
, "--remote-debug") == 0)
2474 else if (strcmp (*next_arg
, "--disable-packet") == 0)
2476 gdbserver_show_disableable (stdout
);
2479 else if (strncmp (*next_arg
,
2480 "--disable-packet=",
2481 sizeof ("--disable-packet=") - 1) == 0)
2483 char *packets
, *tok
;
2485 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
2486 for (tok
= strtok (packets
, ",");
2488 tok
= strtok (NULL
, ","))
2490 if (strcmp ("vCont", tok
) == 0)
2491 disable_packet_vCont
= 1;
2492 else if (strcmp ("Tthread", tok
) == 0)
2493 disable_packet_Tthread
= 1;
2494 else if (strcmp ("qC", tok
) == 0)
2495 disable_packet_qC
= 1;
2496 else if (strcmp ("qfThreadInfo", tok
) == 0)
2497 disable_packet_qfThreadInfo
= 1;
2498 else if (strcmp ("threads", tok
) == 0)
2500 disable_packet_vCont
= 1;
2501 disable_packet_Tthread
= 1;
2502 disable_packet_qC
= 1;
2503 disable_packet_qfThreadInfo
= 1;
2507 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
2509 gdbserver_show_disableable (stderr
);
2516 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
2524 if (setjmp (toplevel
))
2526 fprintf (stderr
, "Exiting\n");
2532 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
2534 gdbserver_usage (stderr
);
2541 /* --attach used to come after PORT, so allow it there for
2543 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
2550 && (*next_arg
== NULL
2551 || (*next_arg
)[0] == '\0'
2552 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
2554 || next_arg
[1] != NULL
))
2559 gdbserver_usage (stderr
);
2563 initialize_inferiors ();
2564 initialize_async_io ();
2566 if (target_supports_tracepoints ())
2567 initialize_tracepoint ();
2569 own_buf
= xmalloc (PBUFSIZ
+ 1);
2570 mem_buf
= xmalloc (PBUFSIZ
);
2572 if (pid
== 0 && *next_arg
!= NULL
)
2576 n
= argc
- (next_arg
- argv
);
2577 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
2578 for (i
= 0; i
< n
; i
++)
2579 program_argv
[i
] = xstrdup (next_arg
[i
]);
2580 program_argv
[i
] = NULL
;
2582 /* Wait till we are at first instruction in program. */
2583 start_inferior (program_argv
);
2585 /* We are now (hopefully) stopped at the first instruction of
2586 the target process. This assumes that the target process was
2587 successfully created. */
2591 if (attach_inferior (pid
) == -1)
2592 error ("Attaching not supported on this target");
2594 /* Otherwise succeeded. */
2598 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2599 last_status
.value
.integer
= 0;
2600 last_ptid
= minus_one_ptid
;
2603 /* Don't report shared library events on the initial connection,
2604 even if some libraries are preloaded. Avoids the "stopped by
2605 shared library event" notice on gdb side. */
2608 if (setjmp (toplevel
))
2610 detach_or_kill_for_exit ();
2614 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2615 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2620 if (!was_running
&& !multi_mode
)
2622 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
2630 /* Be sure we're out of tfind mode. */
2631 current_traceframe
= -1;
2635 if (setjmp (toplevel
) != 0)
2637 /* An error occurred. */
2638 if (response_needed
)
2640 write_enn (own_buf
);
2645 /* Wait for events. This will return when all event sources are
2646 removed from the event loop. */
2647 start_event_loop ();
2649 /* If an exit was requested (using the "monitor exit" command),
2650 terminate now. The only other way to get here is for
2651 getpkt to fail; close the connection and reopen it at the
2656 detach_or_kill_for_exit ();
2661 "Remote side has terminated connection. "
2662 "GDBserver will reopen the connection.\n");
2666 if (disconnected_tracing
)
2668 /* Try to enable non-stop/async mode, so we we can both
2669 wait for an async socket accept, and handle async
2670 target events simultaneously. There's also no point
2671 either in having the target always stop all threads,
2672 when we're going to pass signals down without
2676 if (start_non_stop (1))
2679 /* Detaching implicitly resumes all threads; simply
2680 disconnecting does not. */
2686 "Disconnected tracing disabled; stopping trace run.\n");
2693 /* Event loop callback that handles a serial event. The first byte in
2694 the serial buffer gets us here. We expect characters to arrive at
2695 a brisk pace, so we read the rest of the packet with a blocking
2699 process_serial_event (void)
2709 int new_packet_len
= -1;
2711 /* Used to decide when gdbserver should exit in
2712 multi-mode/remote. */
2713 static int have_ran
= 0;
2716 have_ran
= target_running ();
2718 disable_async_io ();
2720 response_needed
= 0;
2721 packet_len
= getpkt (own_buf
);
2722 if (packet_len
<= 0)
2725 /* Force an event loop break. */
2728 response_needed
= 1;
2735 handle_query (own_buf
, packet_len
, &new_packet_len
);
2738 handle_general_set (own_buf
);
2741 require_running (own_buf
);
2746 pid
= strtol (&own_buf
[i
], NULL
, 16);
2750 ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
2752 if (tracing
&& disconnected_tracing
)
2754 struct thread_resume resume_info
;
2755 struct process_info
*process
= find_process_pid (pid
);
2757 if (process
== NULL
)
2759 write_enn (own_buf
);
2764 "Disconnected tracing in effect, "
2765 "leaving gdbserver attached to the process\n");
2767 /* Make sure we're in non-stop/async mode, so we we can both
2768 wait for an async socket accept, and handle async target
2769 events simultaneously. There's also no point either in
2770 having the target stop all threads, when we're going to
2771 pass signals down without informing GDB. */
2775 fprintf (stderr
, "Forcing non-stop mode\n");
2781 process
->gdb_detached
= 1;
2783 /* Detaching implicitly resumes all threads. */
2784 resume_info
.thread
= minus_one_ptid
;
2785 resume_info
.kind
= resume_continue
;
2786 resume_info
.sig
= 0;
2787 (*the_target
->resume
) (&resume_info
, 1);
2790 break; /* from switch/case */
2793 fprintf (stderr
, "Detaching from process %d\n", pid
);
2795 if (detach_inferior (pid
) != 0)
2796 write_enn (own_buf
);
2799 discard_queued_stop_replies (pid
);
2802 if (extended_protocol
)
2804 /* Treat this like a normal program exit. */
2805 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2806 last_status
.value
.integer
= 0;
2807 last_ptid
= pid_to_ptid (pid
);
2809 current_inferior
= NULL
;
2816 /* If we are attached, then we can exit. Otherwise, we
2817 need to hang around doing nothing, until the child is
2819 for_each_inferior (&all_processes
,
2820 join_inferiors_callback
);
2826 extended_protocol
= 1;
2830 handle_status (own_buf
);
2833 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
2835 ptid_t gdb_id
, thread_id
;
2838 require_running (own_buf
);
2840 gdb_id
= read_ptid (&own_buf
[2], NULL
);
2842 pid
= ptid_get_pid (gdb_id
);
2844 if (ptid_equal (gdb_id
, null_ptid
)
2845 || ptid_equal (gdb_id
, minus_one_ptid
))
2846 thread_id
= null_ptid
;
2848 && ptid_equal (pid_to_ptid (pid
),
2851 struct thread_info
*thread
=
2852 (struct thread_info
*) find_inferior (&all_threads
,
2857 write_enn (own_buf
);
2861 thread_id
= ((struct inferior_list_entry
*)thread
)->id
;
2865 thread_id
= gdb_id_to_thread_id (gdb_id
);
2866 if (ptid_equal (thread_id
, null_ptid
))
2868 write_enn (own_buf
);
2873 if (own_buf
[1] == 'g')
2875 if (ptid_equal (thread_id
, null_ptid
))
2877 /* GDB is telling us to choose any thread. Check if
2878 the currently selected thread is still valid. If
2879 it is not, select the first available. */
2880 struct thread_info
*thread
=
2881 (struct thread_info
*) find_inferior_id (&all_threads
,
2884 thread_id
= all_threads
.head
->id
;
2887 general_thread
= thread_id
;
2888 set_desired_inferior (1);
2890 else if (own_buf
[1] == 'c')
2891 cont_thread
= thread_id
;
2892 else if (own_buf
[1] == 's')
2893 step_thread
= thread_id
;
2899 /* Silently ignore it so that gdb can extend the protocol
2900 without compatibility headaches. */
2905 require_running (own_buf
);
2906 if (current_traceframe
>= 0)
2908 struct regcache
*regcache
= new_register_cache ();
2910 if (fetch_traceframe_registers (current_traceframe
,
2912 registers_to_string (regcache
, own_buf
);
2914 write_enn (own_buf
);
2915 free_register_cache (regcache
);
2919 struct regcache
*regcache
;
2921 set_desired_inferior (1);
2922 regcache
= get_thread_regcache (current_inferior
, 1);
2923 registers_to_string (regcache
, own_buf
);
2927 require_running (own_buf
);
2928 if (current_traceframe
>= 0)
2929 write_enn (own_buf
);
2932 struct regcache
*regcache
;
2934 set_desired_inferior (1);
2935 regcache
= get_thread_regcache (current_inferior
, 1);
2936 registers_from_string (regcache
, &own_buf
[1]);
2941 require_running (own_buf
);
2942 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
2943 if (gdb_read_memory (mem_addr
, mem_buf
, len
) == 0)
2944 convert_int_to_ascii (mem_buf
, own_buf
, len
);
2946 write_enn (own_buf
);
2949 require_running (own_buf
);
2950 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
2951 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
2954 write_enn (own_buf
);
2957 require_running (own_buf
);
2958 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
2959 &mem_addr
, &len
, &mem_buf
) < 0
2960 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
2961 write_enn (own_buf
);
2966 require_running (own_buf
);
2967 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
2968 if (target_signal_to_host_p (sig
))
2969 signal
= target_signal_to_host (sig
);
2972 myresume (own_buf
, 0, signal
);
2975 require_running (own_buf
);
2976 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
2977 if (target_signal_to_host_p (sig
))
2978 signal
= target_signal_to_host (sig
);
2981 myresume (own_buf
, 1, signal
);
2984 require_running (own_buf
);
2986 myresume (own_buf
, 0, signal
);
2989 require_running (own_buf
);
2991 myresume (own_buf
, 1, signal
);
2993 case 'Z': /* insert_ ... */
2995 case 'z': /* remove_ ... */
2999 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
3000 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
3001 char type
= own_buf
[1];
3003 const int insert
= ch
== 'Z';
3005 /* Default to unrecognized/unsupported. */
3009 case '0': /* software-breakpoint */
3010 case '1': /* hardware-breakpoint */
3011 case '2': /* write watchpoint */
3012 case '3': /* read watchpoint */
3013 case '4': /* access watchpoint */
3014 require_running (own_buf
);
3015 if (insert
&& the_target
->insert_point
!= NULL
)
3016 res
= (*the_target
->insert_point
) (type
, addr
, len
);
3017 else if (!insert
&& the_target
->remove_point
!= NULL
)
3018 res
= (*the_target
->remove_point
) (type
, addr
, len
);
3030 write_enn (own_buf
);
3034 response_needed
= 0;
3035 if (!target_running ())
3036 /* The packet we received doesn't make sense - but we can't
3037 reply to it, either. */
3040 fprintf (stderr
, "Killing all inferiors\n");
3041 for_each_inferior (&all_processes
, kill_inferior_callback
);
3043 /* When using the extended protocol, we wait with no program
3044 running. The traditional protocol will exit instead. */
3045 if (extended_protocol
)
3047 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3048 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3056 ptid_t gdb_id
, thread_id
;
3058 require_running (own_buf
);
3060 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3061 thread_id
= gdb_id_to_thread_id (gdb_id
);
3062 if (ptid_equal (thread_id
, null_ptid
))
3064 write_enn (own_buf
);
3068 if (mythread_alive (thread_id
))
3071 write_enn (own_buf
);
3075 response_needed
= 0;
3077 /* Restarting the inferior is only supported in the extended
3079 if (extended_protocol
)
3081 if (target_running ())
3082 for_each_inferior (&all_processes
,
3083 kill_inferior_callback
);
3084 fprintf (stderr
, "GDBserver restarting\n");
3086 /* Wait till we are at 1st instruction in prog. */
3087 if (program_argv
!= NULL
)
3088 start_inferior (program_argv
);
3091 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3092 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3098 /* It is a request we don't understand. Respond with an
3099 empty packet so that gdb knows that we don't support this
3105 /* Extended (long) request. */
3106 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
3110 /* It is a request we don't understand. Respond with an empty
3111 packet so that gdb knows that we don't support this
3117 if (new_packet_len
!= -1)
3118 putpkt_binary (own_buf
, new_packet_len
);
3122 response_needed
= 0;
3124 if (!extended_protocol
&& have_ran
&& !target_running ())
3126 /* In non-stop, defer exiting until GDB had a chance to query
3127 the whole vStopped list (until it gets an OK). */
3130 fprintf (stderr
, "GDBserver exiting\n");
3142 /* Event-loop callback for serial events. */
3145 handle_serial_event (int err
, gdb_client_data client_data
)
3148 fprintf (stderr
, "handling possible serial event\n");
3150 /* Really handle it. */
3151 if (process_serial_event () < 0)
3154 /* Be sure to not change the selected inferior behind GDB's back.
3155 Important in the non-stop mode asynchronous protocol. */
3156 set_desired_inferior (1);
3161 /* Event-loop callback for target events. */
3164 handle_target_event (int err
, gdb_client_data client_data
)
3167 fprintf (stderr
, "handling possible target event\n");
3169 last_ptid
= mywait (minus_one_ptid
, &last_status
,
3172 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3174 int pid
= ptid_get_pid (last_ptid
);
3175 struct process_info
*process
= find_process_pid (pid
);
3176 int forward_event
= !gdb_connected () || process
->gdb_detached
;
3178 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3179 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3181 mark_breakpoints_out (process
);
3182 mourn_inferior (process
);
3186 /* We're reporting this thread as stopped. Update its
3187 "want-stopped" state to what the client wants, until it
3188 gets a new resume action. */
3189 current_inferior
->last_resume_kind
= resume_stop
;
3190 current_inferior
->last_status
= last_status
;
3195 if (!target_running ())
3197 /* The last process exited. We're done. */
3201 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
3203 /* A thread stopped with a signal, but gdb isn't
3204 connected to handle it. Pass it down to the
3205 inferior, as if it wasn't being traced. */
3206 struct thread_resume resume_info
;
3210 "GDB not connected; forwarding event %d for [%s]\n",
3211 (int) last_status
.kind
,
3212 target_pid_to_str (last_ptid
));
3214 resume_info
.thread
= last_ptid
;
3215 resume_info
.kind
= resume_continue
;
3216 resume_info
.sig
= target_signal_to_host (last_status
.value
.sig
);
3217 (*the_target
->resume
) (&resume_info
, 1);
3219 else if (debug_threads
)
3220 fprintf (stderr
, "GDB not connected; ignoring event %d for [%s]\n",
3221 (int) last_status
.kind
,
3222 target_pid_to_str (last_ptid
));
3226 /* Something interesting. Tell GDB about it. */
3227 push_event (last_ptid
, &last_status
);
3231 /* Be sure to not change the selected inferior behind GDB's back.
3232 Important in the non-stop mode asynchronous protocol. */
3233 set_desired_inferior (1);