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
;
277 resume_info
.thread
= pid_to_ptid (signal_pid
);
278 resume_info
.kind
= resume_continue
;
281 mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
283 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
288 (*the_target
->resume
) (&resume_info
, 1);
290 mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
291 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
294 current_inferior
->last_resume_kind
= resume_stop
;
295 current_inferior
->last_status
= last_status
;
297 while (last_status
.value
.sig
!= TARGET_SIGNAL_TRAP
);
299 current_inferior
->last_resume_kind
= resume_stop
;
300 current_inferior
->last_status
= last_status
;
304 /* Wait till we are at 1st instruction in program, return new pid
305 (assuming success). */
306 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
308 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
309 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
311 current_inferior
->last_resume_kind
= resume_stop
;
312 current_inferior
->last_status
= last_status
;
319 attach_inferior (int pid
)
321 /* myattach should return -1 if attaching is unsupported,
322 0 if it succeeded, and call error() otherwise. */
324 if (myattach (pid
) != 0)
327 fprintf (stderr
, "Attached; pid = %d\n", pid
);
330 /* FIXME - It may be that we should get the SIGNAL_PID from the
331 attach function, so that it can be the main thread instead of
332 whichever we were told to attach to. */
337 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
339 /* GDB knows to ignore the first SIGSTOP after attaching to a running
340 process using the "attach" command, but this is different; it's
341 just using "target remote". Pretend it's just starting up. */
342 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
343 && last_status
.value
.sig
== TARGET_SIGNAL_STOP
)
344 last_status
.value
.sig
= TARGET_SIGNAL_TRAP
;
346 current_inferior
->last_resume_kind
= resume_stop
;
347 current_inferior
->last_status
= last_status
;
353 extern int remote_debug
;
355 /* Decode a qXfer read request. Return 0 if everything looks OK,
359 decode_xfer_read (char *buf
, char **annex
, CORE_ADDR
*ofs
, unsigned int *len
)
361 /* Extract and NUL-terminate the annex. */
363 while (*buf
&& *buf
!= ':')
369 /* After the read marker and annex, qXfer looks like a
370 traditional 'm' packet. */
371 decode_m_packet (buf
, ofs
, len
);
376 /* Write the response to a successful qXfer read. Returns the
377 length of the (binary) data stored in BUF, corresponding
378 to as much of DATA/LEN as we could fit. IS_MORE controls
379 the first character of the response. */
381 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
390 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
394 /* Handle all of the extended 'Q' packets. */
397 handle_general_set (char *own_buf
)
399 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
401 int numsigs
= (int) TARGET_SIGNAL_LAST
, i
;
402 const char *p
= own_buf
+ strlen ("QPassSignals:");
405 p
= decode_address_to_semicolon (&cursig
, p
);
406 for (i
= 0; i
< numsigs
; i
++)
412 /* Keep looping, to clear the remaining signals. */
415 p
= decode_address_to_semicolon (&cursig
, p
);
420 strcpy (own_buf
, "OK");
424 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
428 fprintf (stderr
, "[noack mode enabled]\n");
437 if (strncmp (own_buf
, "QNonStop:", 9) == 0)
439 char *mode
= own_buf
+ 9;
443 if (strcmp (mode
, "0") == 0)
445 else if (strcmp (mode
, "1") == 0)
449 /* We don't know what this mode is, so complain to
451 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
457 req_str
= req
? "non-stop" : "all-stop";
458 if (start_non_stop (req
) != 0)
460 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
468 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
474 if (target_supports_tracepoints ()
475 && handle_tracepoint_general_set (own_buf
))
478 /* Otherwise we didn't know what packet it was. Say we didn't
484 get_features_xml (const char *annex
)
486 /* gdbserver_xmltarget defines what to return when looking
487 for the "target.xml" file. Its contents can either be
488 verbatim XML code (prefixed with a '@') or else the name
489 of the actual XML file to be used in place of "target.xml".
491 This variable is set up from the auto-generated
492 init_registers_... routine for the current target. */
494 if (gdbserver_xmltarget
495 && strcmp (annex
, "target.xml") == 0)
497 if (*gdbserver_xmltarget
== '@')
498 return gdbserver_xmltarget
+ 1;
500 annex
= gdbserver_xmltarget
;
505 extern const char *const xml_builtin
[][2];
508 /* Look for the annex. */
509 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
510 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
513 if (xml_builtin
[i
][0] != NULL
)
514 return xml_builtin
[i
][1];
522 monitor_show_help (void)
524 monitor_output ("The following monitor commands are supported:\n");
525 monitor_output (" set debug <0|1>\n");
526 monitor_output (" Enable general debugging messages\n");
527 monitor_output (" set debug-hw-points <0|1>\n");
528 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
529 monitor_output (" set remote-debug <0|1>\n");
530 monitor_output (" Enable remote protocol debugging messages\n");
531 monitor_output (" exit\n");
532 monitor_output (" Quit GDBserver\n");
535 /* Read trace frame or inferior memory. */
538 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
542 if (current_traceframe
>= 0)
545 ULONGEST length
= len
;
547 if (traceframe_read_mem (current_traceframe
,
548 memaddr
, myaddr
, len
, &nbytes
))
550 /* Data read from trace buffer, we're done. */
551 if (nbytes
== length
)
553 if (!in_readonly_region (memaddr
, length
))
555 /* Otherwise we have a valid readonly case, fall through. */
556 /* (assume no half-trace half-real blocks for now) */
559 ret
= prepare_to_access_memory ();
562 ret
= read_inferior_memory (memaddr
, myaddr
, len
);
563 done_accessing_memory ();
569 /* Write trace frame or inferior memory. Actually, writing to trace
570 frames is forbidden. */
573 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
575 if (current_traceframe
>= 0)
581 ret
= prepare_to_access_memory ();
584 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
585 done_accessing_memory ();
591 /* Subroutine of handle_search_memory to simplify it. */
594 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
595 gdb_byte
*pattern
, unsigned pattern_len
,
596 gdb_byte
*search_buf
,
597 unsigned chunk_size
, unsigned search_buf_size
,
598 CORE_ADDR
*found_addrp
)
600 /* Prime the search buffer. */
602 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
) != 0)
604 warning ("Unable to access target memory at 0x%lx, halting search.",
609 /* Perform the search.
611 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
612 When we've scanned N bytes we copy the trailing bytes to the start and
613 read in another N bytes. */
615 while (search_space_len
>= pattern_len
)
618 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
622 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
624 if (found_ptr
!= NULL
)
626 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
627 *found_addrp
= found_addr
;
631 /* Not found in this chunk, skip to next chunk. */
633 /* Don't let search_space_len wrap here, it's unsigned. */
634 if (search_space_len
>= chunk_size
)
635 search_space_len
-= chunk_size
;
637 search_space_len
= 0;
639 if (search_space_len
>= pattern_len
)
641 unsigned keep_len
= search_buf_size
- chunk_size
;
642 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
645 /* Copy the trailing part of the previous iteration to the front
646 of the buffer for the next iteration. */
647 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
649 nr_to_read
= (search_space_len
- keep_len
< chunk_size
650 ? search_space_len
- keep_len
653 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
656 warning ("Unable to access target memory at 0x%lx, halting search.",
661 start_addr
+= chunk_size
;
670 /* Handle qSearch:memory packets. */
673 handle_search_memory (char *own_buf
, int packet_len
)
675 CORE_ADDR start_addr
;
676 CORE_ADDR search_space_len
;
678 unsigned int pattern_len
;
679 /* NOTE: also defined in find.c testcase. */
680 #define SEARCH_CHUNK_SIZE 16000
681 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
682 /* Buffer to hold memory contents for searching. */
683 gdb_byte
*search_buf
;
684 unsigned search_buf_size
;
686 CORE_ADDR found_addr
;
687 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
689 pattern
= malloc (packet_len
);
692 error ("Unable to allocate memory to perform the search");
693 strcpy (own_buf
, "E00");
696 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
697 packet_len
- cmd_name_len
,
698 &start_addr
, &search_space_len
,
699 pattern
, &pattern_len
) < 0)
702 error ("Error in parsing qSearch:memory packet");
703 strcpy (own_buf
, "E00");
707 search_buf_size
= chunk_size
+ pattern_len
- 1;
709 /* No point in trying to allocate a buffer larger than the search space. */
710 if (search_space_len
< search_buf_size
)
711 search_buf_size
= search_space_len
;
713 search_buf
= malloc (search_buf_size
);
714 if (search_buf
== NULL
)
717 error ("Unable to allocate memory to perform the search");
718 strcpy (own_buf
, "E00");
722 found
= handle_search_memory_1 (start_addr
, search_space_len
,
723 pattern
, pattern_len
,
724 search_buf
, chunk_size
, search_buf_size
,
728 sprintf (own_buf
, "1,%lx", (long) found_addr
);
730 strcpy (own_buf
, "0");
732 strcpy (own_buf
, "E00");
738 #define require_running(BUF) \
739 if (!target_running ()) \
745 /* Handle monitor commands not handled by target-specific handlers. */
748 handle_monitor_command (char *mon
)
750 if (strcmp (mon
, "set debug 1") == 0)
753 monitor_output ("Debug output enabled.\n");
755 else if (strcmp (mon
, "set debug 0") == 0)
758 monitor_output ("Debug output disabled.\n");
760 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
763 monitor_output ("H/W point debugging output enabled.\n");
765 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
768 monitor_output ("H/W point debugging output disabled.\n");
770 else if (strcmp (mon
, "set remote-debug 1") == 0)
773 monitor_output ("Protocol debug output enabled.\n");
775 else if (strcmp (mon
, "set remote-debug 0") == 0)
778 monitor_output ("Protocol debug output disabled.\n");
780 else if (strcmp (mon
, "help") == 0)
781 monitor_show_help ();
782 else if (strcmp (mon
, "exit") == 0)
786 monitor_output ("Unknown monitor command.\n\n");
787 monitor_show_help ();
793 handle_threads_qxfer_proper (struct buffer
*buffer
)
795 struct inferior_list_entry
*thread
;
797 buffer_grow_str (buffer
, "<threads>\n");
799 for (thread
= all_threads
.head
; thread
; thread
= thread
->next
)
801 ptid_t ptid
= thread_to_gdb_id ((struct thread_info
*)thread
);
806 write_ptid (ptid_s
, ptid
);
808 if (the_target
->core_of_thread
)
809 core
= (*the_target
->core_of_thread
) (ptid
);
813 sprintf (core_s
, "%d", core
);
814 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
819 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
824 buffer_grow_str0 (buffer
, "</threads>\n");
828 handle_threads_qxfer (const char *annex
,
829 unsigned char *readbuf
,
830 CORE_ADDR offset
, int length
)
832 static char *result
= 0;
833 static unsigned int result_length
= 0;
835 if (annex
&& strcmp (annex
, "") != 0)
840 struct buffer buffer
;
841 /* When asked for data at offset 0, generate everything and store into
842 'result'. Successive reads will be served off 'result'. */
846 buffer_init (&buffer
);
848 handle_threads_qxfer_proper (&buffer
);
850 result
= buffer_finish (&buffer
);
851 result_length
= strlen (result
);
852 buffer_free (&buffer
);
855 if (offset
>= result_length
)
857 /* We're out of data. */
864 if (length
> result_length
- offset
)
865 length
= result_length
- offset
;
867 memcpy (readbuf
, result
+ offset
, length
);
873 /* Table used by the crc32 function to calcuate the checksum. */
875 static unsigned int crc32_table
[256] =
878 /* Compute 32 bit CRC from inferior memory.
880 On success, return 32 bit CRC.
881 On failure, return (unsigned long long) -1. */
883 static unsigned long long
884 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
888 /* Initialize the CRC table and the decoding table. */
892 for (i
= 0; i
< 256; i
++)
894 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
895 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
902 unsigned char byte
= 0;
904 /* Return failure if memory read fails. */
905 if (read_inferior_memory (base
, &byte
, 1) != 0)
906 return (unsigned long long) -1;
908 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
911 return (unsigned long long) crc
;
914 /* Handle all of the extended 'q' packets. */
916 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
918 static struct inferior_list_entry
*thread_ptr
;
920 /* Reply the current thread id. */
921 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
924 require_running (own_buf
);
926 if (!ptid_equal (general_thread
, null_ptid
)
927 && !ptid_equal (general_thread
, minus_one_ptid
))
928 gdb_id
= general_thread
;
931 thread_ptr
= all_threads
.head
;
932 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
935 sprintf (own_buf
, "QC");
937 own_buf
= write_ptid (own_buf
, gdb_id
);
941 if (strcmp ("qSymbol::", own_buf
) == 0)
943 /* GDB is suggesting new symbols have been loaded. This may
944 mean a new shared library has been detected as loaded, so
945 take the opportunity to check if breakpoints we think are
946 inserted, still are. Note that it isn't guaranteed that
947 we'll see this when a shared library is loaded, and nor will
948 we see this for unloads (although breakpoints in unloaded
949 libraries shouldn't trigger), as GDB may not find symbols for
950 the library at all. We also re-validate breakpoints when we
951 see a second GDB breakpoint for the same address, and or when
952 we access breakpoint shadows. */
953 validate_breakpoints ();
955 if (target_supports_tracepoints ())
956 tracepoint_look_up_symbols ();
958 if (target_running () && the_target
->look_up_symbols
!= NULL
)
959 (*the_target
->look_up_symbols
) ();
961 strcpy (own_buf
, "OK");
965 if (!disable_packet_qfThreadInfo
)
967 if (strcmp ("qfThreadInfo", own_buf
) == 0)
971 require_running (own_buf
);
972 thread_ptr
= all_threads
.head
;
975 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
976 write_ptid (own_buf
, gdb_id
);
977 thread_ptr
= thread_ptr
->next
;
981 if (strcmp ("qsThreadInfo", own_buf
) == 0)
985 require_running (own_buf
);
986 if (thread_ptr
!= NULL
)
989 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
990 write_ptid (own_buf
, gdb_id
);
991 thread_ptr
= thread_ptr
->next
;
996 sprintf (own_buf
, "l");
1002 if (the_target
->read_offsets
!= NULL
1003 && strcmp ("qOffsets", own_buf
) == 0)
1005 CORE_ADDR text
, data
;
1007 require_running (own_buf
);
1008 if (the_target
->read_offsets (&text
, &data
))
1009 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
1010 (long)text
, (long)data
, (long)data
);
1012 write_enn (own_buf
);
1017 if (the_target
->qxfer_spu
!= NULL
1018 && strncmp ("qXfer:spu:read:", own_buf
, 15) == 0)
1024 unsigned char *spu_buf
;
1026 require_running (own_buf
);
1027 strcpy (own_buf
, "E00");
1028 if (decode_xfer_read (own_buf
+ 15, &annex
, &ofs
, &len
) < 0)
1030 if (len
> PBUFSIZ
- 2)
1032 spu_buf
= malloc (len
+ 1);
1036 n
= (*the_target
->qxfer_spu
) (annex
, spu_buf
, NULL
, ofs
, len
+ 1);
1038 write_enn (own_buf
);
1040 *new_packet_len_p
= write_qxfer_response (own_buf
, spu_buf
, len
, 1);
1042 *new_packet_len_p
= write_qxfer_response (own_buf
, spu_buf
, n
, 0);
1048 if (the_target
->qxfer_spu
!= NULL
1049 && strncmp ("qXfer:spu:write:", own_buf
, 16) == 0)
1055 unsigned char *spu_buf
;
1057 require_running (own_buf
);
1058 strcpy (own_buf
, "E00");
1059 spu_buf
= malloc (packet_len
- 15);
1062 if (decode_xfer_write (own_buf
+ 16, packet_len
- 16, &annex
,
1063 &ofs
, &len
, spu_buf
) < 0)
1069 n
= (*the_target
->qxfer_spu
)
1070 (annex
, NULL
, (unsigned const char *)spu_buf
, ofs
, len
);
1072 write_enn (own_buf
);
1074 sprintf (own_buf
, "%x", n
);
1080 if (the_target
->read_auxv
!= NULL
1081 && strncmp ("qXfer:auxv:read:", own_buf
, 16) == 0)
1083 unsigned char *data
;
1089 require_running (own_buf
);
1091 /* Reject any annex; grab the offset and length. */
1092 if (decode_xfer_read (own_buf
+ 16, &annex
, &ofs
, &len
) < 0
1093 || annex
[0] != '\0')
1095 strcpy (own_buf
, "E00");
1099 /* Read one extra byte, as an indicator of whether there is
1101 if (len
> PBUFSIZ
- 2)
1103 data
= malloc (len
+ 1);
1106 write_enn (own_buf
);
1109 n
= (*the_target
->read_auxv
) (ofs
, data
, len
+ 1);
1111 write_enn (own_buf
);
1113 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1115 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1122 if (strncmp ("qXfer:features:read:", own_buf
, 20) == 0)
1125 unsigned int len
, total_len
;
1126 const char *document
;
1129 require_running (own_buf
);
1131 /* Grab the annex, offset, and length. */
1132 if (decode_xfer_read (own_buf
+ 20, &annex
, &ofs
, &len
) < 0)
1134 strcpy (own_buf
, "E00");
1138 /* Now grab the correct annex. */
1139 document
= get_features_xml (annex
);
1140 if (document
== NULL
)
1142 strcpy (own_buf
, "E00");
1146 total_len
= strlen (document
);
1147 if (len
> PBUFSIZ
- 2)
1150 if (ofs
> total_len
)
1151 write_enn (own_buf
);
1152 else if (len
< total_len
- ofs
)
1153 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
1156 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
1157 total_len
- ofs
, 0);
1162 if (strncmp ("qXfer:libraries:read:", own_buf
, 21) == 0)
1165 unsigned int len
, total_len
;
1167 struct inferior_list_entry
*dll_ptr
;
1170 require_running (own_buf
);
1172 /* Reject any annex; grab the offset and length. */
1173 if (decode_xfer_read (own_buf
+ 21, &annex
, &ofs
, &len
) < 0
1174 || annex
[0] != '\0')
1176 strcpy (own_buf
, "E00");
1180 /* Over-estimate the necessary memory. Assume that every character
1181 in the library name must be escaped. */
1183 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
1184 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
1186 document
= malloc (total_len
);
1187 if (document
== NULL
)
1189 write_enn (own_buf
);
1192 strcpy (document
, "<library-list>\n");
1193 p
= document
+ strlen (document
);
1195 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
1197 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
1200 strcpy (p
, " <library name=\"");
1202 name
= xml_escape_text (dll
->name
);
1206 strcpy (p
, "\"><segment address=\"");
1208 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1210 strcpy (p
, "\"/></library>\n");
1214 strcpy (p
, "</library-list>\n");
1216 total_len
= strlen (document
);
1217 if (len
> PBUFSIZ
- 2)
1220 if (ofs
> total_len
)
1221 write_enn (own_buf
);
1222 else if (len
< total_len
- ofs
)
1223 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
1226 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
1227 total_len
- ofs
, 0);
1233 if (the_target
->qxfer_osdata
!= NULL
1234 && strncmp ("qXfer:osdata:read:", own_buf
, 18) == 0)
1240 unsigned char *workbuf
;
1242 strcpy (own_buf
, "E00");
1243 if (decode_xfer_read (own_buf
+ 18, &annex
, &ofs
, &len
) < 0)
1245 if (len
> PBUFSIZ
- 2)
1247 workbuf
= malloc (len
+ 1);
1251 n
= (*the_target
->qxfer_osdata
) (annex
, workbuf
, NULL
, ofs
, len
+ 1);
1253 write_enn (own_buf
);
1255 *new_packet_len_p
= write_qxfer_response (own_buf
, workbuf
, len
, 1);
1257 *new_packet_len_p
= write_qxfer_response (own_buf
, workbuf
, n
, 0);
1263 if (the_target
->qxfer_siginfo
!= NULL
1264 && strncmp ("qXfer:siginfo:read:", own_buf
, 19) == 0)
1266 unsigned char *data
;
1272 require_running (own_buf
);
1274 /* Reject any annex; grab the offset and length. */
1275 if (decode_xfer_read (own_buf
+ 19, &annex
, &ofs
, &len
) < 0
1276 || annex
[0] != '\0')
1278 strcpy (own_buf
, "E00");
1282 /* Read one extra byte, as an indicator of whether there is
1284 if (len
> PBUFSIZ
- 2)
1286 data
= malloc (len
+ 1);
1289 n
= (*the_target
->qxfer_siginfo
) (annex
, data
, NULL
, ofs
, len
+ 1);
1291 write_enn (own_buf
);
1293 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1295 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1301 if (the_target
->qxfer_siginfo
!= NULL
1302 && strncmp ("qXfer:siginfo:write:", own_buf
, 20) == 0)
1308 unsigned char *data
;
1310 require_running (own_buf
);
1312 strcpy (own_buf
, "E00");
1313 data
= malloc (packet_len
- 19);
1316 if (decode_xfer_write (own_buf
+ 20, packet_len
- 20, &annex
,
1317 &ofs
, &len
, data
) < 0)
1323 n
= (*the_target
->qxfer_siginfo
)
1324 (annex
, NULL
, (unsigned const char *)data
, ofs
, len
);
1326 write_enn (own_buf
);
1328 sprintf (own_buf
, "%x", n
);
1334 if (strncmp ("qXfer:threads:read:", own_buf
, 19) == 0)
1336 unsigned char *data
;
1342 require_running (own_buf
);
1344 /* Reject any annex; grab the offset and length. */
1345 if (decode_xfer_read (own_buf
+ 19, &annex
, &ofs
, &len
) < 0
1346 || annex
[0] != '\0')
1348 strcpy (own_buf
, "E00");
1352 /* Read one extra byte, as an indicator of whether there is
1354 if (len
> PBUFSIZ
- 2)
1356 data
= malloc (len
+ 1);
1359 n
= handle_threads_qxfer (annex
, data
, ofs
, len
+ 1);
1361 write_enn (own_buf
);
1363 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1365 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1371 if (strncmp ("qXfer:statictrace:read:", own_buf
,
1372 sizeof ("qXfer:statictrace:read:") -1) == 0)
1374 unsigned char *data
;
1380 require_running (own_buf
);
1382 if (current_traceframe
== -1)
1384 write_enn (own_buf
);
1388 /* Reject any annex; grab the offset and length. */
1389 if (decode_xfer_read (own_buf
+ sizeof ("qXfer:statictrace:read:") -1,
1390 &annex
, &ofs
, &len
) < 0
1391 || annex
[0] != '\0')
1393 strcpy (own_buf
, "E00");
1397 /* Read one extra byte, as an indicator of whether there is
1399 if (len
> PBUFSIZ
- 2)
1401 data
= malloc (len
+ 1);
1405 if (traceframe_read_sdata (current_traceframe
, ofs
,
1406 data
, len
+ 1, &nbytes
))
1407 write_enn (own_buf
);
1408 else if (nbytes
> len
)
1409 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1411 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, nbytes
, 0);
1417 /* Protocol features query. */
1418 if (strncmp ("qSupported", own_buf
, 10) == 0
1419 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1421 char *p
= &own_buf
[10];
1422 int gdb_supports_qRelocInsn
= 0;
1424 /* Start processing qSupported packet. */
1425 target_process_qsupported (NULL
);
1427 /* Process each feature being provided by GDB. The first
1428 feature will follow a ':', and latter features will follow
1432 char **qsupported
= NULL
;
1436 /* Two passes, to avoid nested strtok calls in
1437 target_process_qsupported. */
1438 for (p
= strtok (p
+ 1, ";");
1440 p
= strtok (NULL
, ";"))
1443 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
1444 qsupported
[count
- 1] = xstrdup (p
);
1447 for (i
= 0; i
< count
; i
++)
1450 if (strcmp (p
, "multiprocess+") == 0)
1452 /* GDB supports and wants multi-process support if
1454 if (target_supports_multi_process ())
1457 else if (strcmp (p
, "qRelocInsn+") == 0)
1459 /* GDB supports relocate instruction requests. */
1460 gdb_supports_qRelocInsn
= 1;
1463 target_process_qsupported (p
);
1471 sprintf (own_buf
, "PacketSize=%x;QPassSignals+", PBUFSIZ
- 1);
1473 /* We do not have any hook to indicate whether the target backend
1474 supports qXfer:libraries:read, so always report it. */
1475 strcat (own_buf
, ";qXfer:libraries:read+");
1477 if (the_target
->read_auxv
!= NULL
)
1478 strcat (own_buf
, ";qXfer:auxv:read+");
1480 if (the_target
->qxfer_spu
!= NULL
)
1481 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
1483 if (the_target
->qxfer_siginfo
!= NULL
)
1484 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1486 /* We always report qXfer:features:read, as targets may
1487 install XML files on a subsequent call to arch_setup.
1488 If we reported to GDB on startup that we don't support
1489 qXfer:feature:read at all, we will never be re-queried. */
1490 strcat (own_buf
, ";qXfer:features:read+");
1492 if (transport_is_reliable
)
1493 strcat (own_buf
, ";QStartNoAckMode+");
1495 if (the_target
->qxfer_osdata
!= NULL
)
1496 strcat (own_buf
, ";qXfer:osdata:read+");
1498 if (target_supports_multi_process ())
1499 strcat (own_buf
, ";multiprocess+");
1501 if (target_supports_non_stop ())
1502 strcat (own_buf
, ";QNonStop+");
1504 strcat (own_buf
, ";qXfer:threads:read+");
1506 if (target_supports_tracepoints ())
1508 strcat (own_buf
, ";ConditionalTracepoints+");
1509 strcat (own_buf
, ";TraceStateVariables+");
1510 strcat (own_buf
, ";TracepointSource+");
1511 strcat (own_buf
, ";DisconnectedTracing+");
1512 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
1513 strcat (own_buf
, ";FastTracepoints+");
1514 strcat (own_buf
, ";StaticTracepoints+");
1515 strcat (own_buf
, ";qXfer:statictrace:read+");
1521 /* Thread-local storage support. */
1522 if (the_target
->get_tls_address
!= NULL
1523 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
1525 char *p
= own_buf
+ 12;
1526 CORE_ADDR parts
[2], address
= 0;
1528 ptid_t ptid
= null_ptid
;
1530 require_running (own_buf
);
1532 for (i
= 0; i
< 3; i
++)
1540 p2
= strchr (p
, ',');
1553 ptid
= read_ptid (p
, NULL
);
1555 decode_address (&parts
[i
- 1], p
, len
);
1559 if (p
!= NULL
|| i
< 3)
1563 struct thread_info
*thread
= find_thread_ptid (ptid
);
1568 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
1574 strcpy (own_buf
, paddress(address
));
1579 write_enn (own_buf
);
1583 /* Otherwise, pretend we do not understand this packet. */
1586 /* Windows OS Thread Information Block address support. */
1587 if (the_target
->get_tib_address
!= NULL
1588 && strncmp ("qGetTIBAddr:", own_buf
, 12) == 0)
1593 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
1595 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
1598 strcpy (own_buf
, paddress(tlb
));
1603 write_enn (own_buf
);
1609 /* Handle "monitor" commands. */
1610 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
1612 char *mon
= malloc (PBUFSIZ
);
1613 int len
= strlen (own_buf
+ 6);
1617 write_enn (own_buf
);
1621 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
1623 write_enn (own_buf
);
1627 mon
[len
/ 2] = '\0';
1631 if (the_target
->handle_monitor_command
== NULL
1632 || (*the_target
->handle_monitor_command
) (mon
) == 0)
1633 /* Default processing. */
1634 handle_monitor_command (mon
);
1640 if (strncmp ("qSearch:memory:", own_buf
, sizeof ("qSearch:memory:") - 1) == 0)
1642 require_running (own_buf
);
1643 handle_search_memory (own_buf
, packet_len
);
1647 if (strcmp (own_buf
, "qAttached") == 0
1648 || strncmp (own_buf
, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1650 struct process_info
*process
;
1652 if (own_buf
[sizeof ("qAttached") - 1])
1654 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
1655 process
= (struct process_info
*)
1656 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
1660 require_running (own_buf
);
1661 process
= current_process ();
1664 if (process
== NULL
)
1666 write_enn (own_buf
);
1670 strcpy (own_buf
, process
->attached
? "1" : "0");
1674 if (strncmp ("qCRC:", own_buf
, 5) == 0)
1676 /* CRC check (compare-section). */
1680 unsigned long long crc
;
1682 require_running (own_buf
);
1683 base
= strtoul (own_buf
+ 5, &comma
, 16);
1684 if (*comma
++ != ',')
1686 write_enn (own_buf
);
1689 len
= strtoul (comma
, NULL
, 16);
1690 crc
= crc32 (base
, len
, 0xffffffff);
1691 /* Check for memory failure. */
1692 if (crc
== (unsigned long long) -1)
1694 write_enn (own_buf
);
1697 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
1701 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
1704 /* Otherwise we didn't know what packet it was. Say we didn't
1709 static void gdb_wants_all_threads_stopped (void);
1711 /* Parse vCont packets. */
1713 handle_v_cont (char *own_buf
)
1717 struct thread_resume
*resume_info
;
1718 struct thread_resume default_action
= {{0}};
1720 /* Count the number of semicolons in the packet. There should be one
1721 for every action. */
1727 p
= strchr (p
, ';');
1730 resume_info
= malloc (n
* sizeof (resume_info
[0]));
1731 if (resume_info
== NULL
)
1739 if (p
[0] == 's' || p
[0] == 'S')
1740 resume_info
[i
].kind
= resume_step
;
1741 else if (p
[0] == 'c' || p
[0] == 'C')
1742 resume_info
[i
].kind
= resume_continue
;
1743 else if (p
[0] == 't')
1744 resume_info
[i
].kind
= resume_stop
;
1748 if (p
[0] == 'S' || p
[0] == 'C')
1751 sig
= strtol (p
+ 1, &q
, 16);
1756 if (!target_signal_to_host_p (sig
))
1758 resume_info
[i
].sig
= target_signal_to_host (sig
);
1762 resume_info
[i
].sig
= 0;
1768 resume_info
[i
].thread
= minus_one_ptid
;
1769 default_action
= resume_info
[i
];
1771 /* Note: we don't increment i here, we'll overwrite this entry
1772 the next time through. */
1774 else if (p
[0] == ':')
1776 ptid_t ptid
= read_ptid (p
+ 1, &q
);
1781 if (p
[0] != ';' && p
[0] != 0)
1784 resume_info
[i
].thread
= ptid
;
1791 resume_info
[i
] = default_action
;
1793 /* Still used in occasional places in the backend. */
1795 && !ptid_equal (resume_info
[0].thread
, minus_one_ptid
)
1796 && resume_info
[0].kind
!= resume_stop
)
1797 cont_thread
= resume_info
[0].thread
;
1799 cont_thread
= minus_one_ptid
;
1800 set_desired_inferior (0);
1805 (*the_target
->resume
) (resume_info
, n
);
1813 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
1815 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
1816 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
1817 current_inferior
->last_status
= last_status
;
1819 /* From the client's perspective, all-stop mode always stops all
1820 threads implicitly (and the target backend has already done
1821 so by now). Tag all threads as "want-stopped", so we don't
1822 resume them implicitly without the client telling us to. */
1823 gdb_wants_all_threads_stopped ();
1824 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1825 disable_async_io ();
1827 if (last_status
.kind
== TARGET_WAITKIND_EXITED
1828 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
1829 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
1834 write_enn (own_buf
);
1839 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1841 handle_v_attach (char *own_buf
)
1845 pid
= strtol (own_buf
+ 8, NULL
, 16);
1846 if (pid
!= 0 && attach_inferior (pid
) == 0)
1848 /* Don't report shared library events after attaching, even if
1849 some libraries are preloaded. GDB will always poll the
1850 library list. Avoids the "stopped by shared library event"
1851 notice on the GDB side. */
1856 /* In non-stop, we don't send a resume reply. Stop events
1857 will follow up using the normal notification
1862 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1868 write_enn (own_buf
);
1873 /* Run a new program. Return 1 if successful, 0 if failure. */
1875 handle_v_run (char *own_buf
)
1877 char *p
, *next_p
, **new_argv
;
1881 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
1887 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
1888 if (new_argv
== NULL
)
1890 write_enn (own_buf
);
1895 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
1897 next_p
= strchr (p
, ';');
1899 next_p
= p
+ strlen (p
);
1901 if (i
== 0 && p
== next_p
)
1905 /* FIXME: Fail request if out of memory instead of dying. */
1906 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
1907 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
1908 new_argv
[i
][(next_p
- p
) / 2] = '\0';
1917 if (new_argv
[0] == NULL
)
1919 /* GDB didn't specify a program to run. Use the program from the
1920 last run with the new argument list. */
1922 if (program_argv
== NULL
)
1924 /* FIXME: new_argv memory leak */
1925 write_enn (own_buf
);
1929 new_argv
[0] = strdup (program_argv
[0]);
1930 if (new_argv
[0] == NULL
)
1932 /* FIXME: new_argv memory leak */
1933 write_enn (own_buf
);
1938 /* Free the old argv and install the new one. */
1939 freeargv (program_argv
);
1940 program_argv
= new_argv
;
1942 start_inferior (program_argv
);
1943 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
1945 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1947 /* In non-stop, sending a resume reply doesn't set the general
1948 thread, but GDB assumes a vRun sets it (this is so GDB can
1949 query which is the main thread of the new inferior. */
1951 general_thread
= last_ptid
;
1957 write_enn (own_buf
);
1962 /* Kill process. Return 1 if successful, 0 if failure. */
1964 handle_v_kill (char *own_buf
)
1967 char *p
= &own_buf
[6];
1969 pid
= strtol (p
, NULL
, 16);
1972 if (pid
!= 0 && kill_inferior (pid
) == 0)
1974 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
1975 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
1976 last_ptid
= pid_to_ptid (pid
);
1977 discard_queued_stop_replies (pid
);
1983 write_enn (own_buf
);
1988 /* Handle a 'vStopped' packet. */
1990 handle_v_stopped (char *own_buf
)
1992 /* If we're waiting for GDB to acknowledge a pending stop reply,
1993 consider that done. */
1996 struct vstop_notif
*head
;
1999 fprintf (stderr
, "vStopped: acking %s\n",
2000 target_pid_to_str (notif_queue
->ptid
));
2003 notif_queue
= notif_queue
->next
;
2007 /* Push another stop reply, or if there are no more left, an OK. */
2008 send_next_stop_reply (own_buf
);
2011 /* Handle all of the extended 'v' packets. */
2013 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2015 if (!disable_packet_vCont
)
2017 if (strncmp (own_buf
, "vCont;", 6) == 0)
2019 require_running (own_buf
);
2020 handle_v_cont (own_buf
);
2024 if (strncmp (own_buf
, "vCont?", 6) == 0)
2026 strcpy (own_buf
, "vCont;c;C;s;S;t");
2031 if (strncmp (own_buf
, "vFile:", 6) == 0
2032 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2035 if (strncmp (own_buf
, "vAttach;", 8) == 0)
2037 if (!multi_process
&& target_running ())
2039 fprintf (stderr
, "Already debugging a process\n");
2040 write_enn (own_buf
);
2043 handle_v_attach (own_buf
);
2047 if (strncmp (own_buf
, "vRun;", 5) == 0)
2049 if (!multi_process
&& target_running ())
2051 fprintf (stderr
, "Already debugging a process\n");
2052 write_enn (own_buf
);
2055 handle_v_run (own_buf
);
2059 if (strncmp (own_buf
, "vKill;", 6) == 0)
2061 if (!target_running ())
2063 fprintf (stderr
, "No process to kill\n");
2064 write_enn (own_buf
);
2067 handle_v_kill (own_buf
);
2071 if (strncmp (own_buf
, "vStopped", 8) == 0)
2073 handle_v_stopped (own_buf
);
2077 /* Otherwise we didn't know what packet it was. Say we didn't
2083 /* Resume inferior and wait for another event. In non-stop mode,
2084 don't really wait here, but return immediatelly to the event
2087 myresume (char *own_buf
, int step
, int sig
)
2089 struct thread_resume resume_info
[2];
2091 int valid_cont_thread
;
2093 set_desired_inferior (0);
2095 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2096 && !ptid_equal (cont_thread
, minus_one_ptid
));
2098 if (step
|| sig
|| valid_cont_thread
)
2100 resume_info
[0].thread
2101 = ((struct inferior_list_entry
*) current_inferior
)->id
;
2103 resume_info
[0].kind
= resume_step
;
2105 resume_info
[0].kind
= resume_continue
;
2106 resume_info
[0].sig
= sig
;
2110 if (!valid_cont_thread
)
2112 resume_info
[n
].thread
= minus_one_ptid
;
2113 resume_info
[n
].kind
= resume_continue
;
2114 resume_info
[n
].sig
= 0;
2121 (*the_target
->resume
) (resume_info
, n
);
2127 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2129 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2130 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2132 current_inferior
->last_resume_kind
= resume_stop
;
2133 current_inferior
->last_status
= last_status
;
2136 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2137 disable_async_io ();
2139 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2140 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2141 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2145 /* Callback for for_each_inferior. Make a new stop reply for each
2149 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2151 struct thread_info
*thread
= (struct thread_info
*) entry
;
2153 /* For now, assume targets that don't have this callback also don't
2154 manage the thread's last_status field. */
2155 if (the_target
->thread_stopped
== NULL
)
2157 /* Pass the last stop reply back to GDB, but don't notify
2159 queue_stop_reply (entry
->id
, &thread
->last_status
);
2163 if (thread_stopped (thread
))
2166 fprintf (stderr
, "Reporting thread %s as already stopped with %s\n",
2167 target_pid_to_str (entry
->id
),
2168 target_waitstatus_to_string (&thread
->last_status
));
2170 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2172 /* Pass the last stop reply back to GDB, but don't notify
2174 queue_stop_reply (entry
->id
, &thread
->last_status
);
2181 /* Set this inferior threads's state as "want-stopped". We won't
2182 resume this thread until the client gives us another action for
2186 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2188 struct thread_info
*thread
= (struct thread_info
*) entry
;
2190 thread
->last_resume_kind
= resume_stop
;
2192 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2194 /* Most threads are stopped implicitly (all-stop); tag that with
2196 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2197 thread
->last_status
.value
.sig
= TARGET_SIGNAL_0
;
2201 /* Set all threads' states as "want-stopped". */
2204 gdb_wants_all_threads_stopped (void)
2206 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2209 /* Clear the gdb_detached flag of every process. */
2212 gdb_reattached_process (struct inferior_list_entry
*entry
)
2214 struct process_info
*process
= (struct process_info
*) entry
;
2216 process
->gdb_detached
= 0;
2219 /* Status handler for the '?' packet. */
2222 handle_status (char *own_buf
)
2224 /* GDB is connected, don't forward events to the target anymore. */
2225 for_each_inferior (&all_processes
, gdb_reattached_process
);
2227 /* In non-stop mode, we must send a stop reply for each stopped
2228 thread. In all-stop mode, just send one for the first stopped
2233 discard_queued_stop_replies (-1);
2234 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2236 /* The first is sent immediatly. OK is sent if there is no
2237 stopped thread, which is the same handling of the vStopped
2238 packet (by design). */
2239 send_next_stop_reply (own_buf
);
2244 stabilize_threads ();
2245 gdb_wants_all_threads_stopped ();
2247 if (all_threads
.head
)
2249 struct target_waitstatus status
;
2251 status
.kind
= TARGET_WAITKIND_STOPPED
;
2252 status
.value
.sig
= TARGET_SIGNAL_TRAP
;
2253 prepare_resume_reply (own_buf
,
2254 all_threads
.head
->id
, &status
);
2257 strcpy (own_buf
, "W00");
2262 gdbserver_version (void)
2264 printf ("GNU gdbserver %s%s\n"
2265 "Copyright (C) 2010 Free Software Foundation, Inc.\n"
2266 "gdbserver is free software, covered by the GNU General Public License.\n"
2267 "This gdbserver was configured as \"%s\"\n",
2268 PKGVERSION
, version
, host_name
);
2272 gdbserver_usage (FILE *stream
)
2274 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2275 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2276 "\tgdbserver [OPTIONS] --multi COMM\n"
2278 "COMM may either be a tty device (for serial debugging), or \n"
2279 "HOST:PORT to listen for a TCP connection.\n"
2282 " --debug Enable general debugging output.\n"
2283 " --remote-debug Enable remote protocol debugging output.\n"
2284 " --version Display version information and exit.\n"
2285 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n");
2286 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
2287 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
2291 gdbserver_show_disableable (FILE *stream
)
2293 fprintf (stream
, "Disableable packets:\n"
2294 " vCont \tAll vCont packets\n"
2295 " qC \tQuerying the current thread\n"
2296 " qfThreadInfo\tThread listing\n"
2297 " Tthread \tPassing the thread specifier in the T stop reply packet\n"
2298 " threads \tAll of the above\n");
2302 #undef require_running
2303 #define require_running(BUF) \
2304 if (!target_running ()) \
2311 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
2313 int pid
= * (int *) args
;
2315 if (ptid_get_pid (entry
->id
) == pid
)
2322 kill_inferior_callback (struct inferior_list_entry
*entry
)
2324 struct process_info
*process
= (struct process_info
*) entry
;
2325 int pid
= ptid_get_pid (process
->head
.id
);
2327 kill_inferior (pid
);
2328 discard_queued_stop_replies (pid
);
2331 /* Callback for for_each_inferior to detach or kill the inferior,
2332 depending on whether we attached to it or not.
2333 We inform the user whether we're detaching or killing the process
2334 as this is only called when gdbserver is about to exit. */
2337 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
2339 struct process_info
*process
= (struct process_info
*) entry
;
2340 int pid
= ptid_get_pid (process
->head
.id
);
2342 if (process
->attached
)
2343 detach_inferior (pid
);
2345 kill_inferior (pid
);
2347 discard_queued_stop_replies (pid
);
2350 /* for_each_inferior callback for detach_or_kill_for_exit to print
2351 the pids of started inferiors. */
2354 print_started_pid (struct inferior_list_entry
*entry
)
2356 struct process_info
*process
= (struct process_info
*) entry
;
2358 if (! process
->attached
)
2360 int pid
= ptid_get_pid (process
->head
.id
);
2361 fprintf (stderr
, " %d", pid
);
2365 /* for_each_inferior callback for detach_or_kill_for_exit to print
2366 the pids of attached inferiors. */
2369 print_attached_pid (struct inferior_list_entry
*entry
)
2371 struct process_info
*process
= (struct process_info
*) entry
;
2373 if (process
->attached
)
2375 int pid
= ptid_get_pid (process
->head
.id
);
2376 fprintf (stderr
, " %d", pid
);
2380 /* Call this when exiting gdbserver with possible inferiors that need
2381 to be killed or detached from. */
2384 detach_or_kill_for_exit (void)
2386 /* First print a list of the inferiors we will be killing/detaching.
2387 This is to assist the user, for example, in case the inferior unexpectedly
2388 dies after we exit: did we screw up or did the inferior exit on its own?
2389 Having this info will save some head-scratching. */
2391 if (have_started_inferiors_p ())
2393 fprintf (stderr
, "Killing process(es):");
2394 for_each_inferior (&all_processes
, print_started_pid
);
2395 fprintf (stderr
, "\n");
2397 if (have_attached_inferiors_p ())
2399 fprintf (stderr
, "Detaching process(es):");
2400 for_each_inferior (&all_processes
, print_attached_pid
);
2401 fprintf (stderr
, "\n");
2404 /* Now we can kill or detach the inferiors. */
2406 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
2410 join_inferiors_callback (struct inferior_list_entry
*entry
)
2412 struct process_info
*process
= (struct process_info
*) entry
;
2414 /* If we are attached, then we can exit. Otherwise, we need to hang
2415 around doing nothing, until the child is gone. */
2416 if (!process
->attached
)
2417 join_inferior (ptid_get_pid (process
->head
.id
));
2421 main (int argc
, char *argv
[])
2425 char *arg_end
, *port
;
2426 char **next_arg
= &argv
[1];
2431 while (*next_arg
!= NULL
&& **next_arg
== '-')
2433 if (strcmp (*next_arg
, "--version") == 0)
2435 gdbserver_version ();
2438 else if (strcmp (*next_arg
, "--help") == 0)
2440 gdbserver_usage (stdout
);
2443 else if (strcmp (*next_arg
, "--attach") == 0)
2445 else if (strcmp (*next_arg
, "--multi") == 0)
2447 else if (strcmp (*next_arg
, "--wrapper") == 0)
2451 wrapper_argv
= next_arg
;
2452 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
2455 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
2457 gdbserver_usage (stderr
);
2461 /* Consume the "--". */
2464 else if (strcmp (*next_arg
, "--debug") == 0)
2466 else if (strcmp (*next_arg
, "--remote-debug") == 0)
2468 else if (strcmp (*next_arg
, "--disable-packet") == 0)
2470 gdbserver_show_disableable (stdout
);
2473 else if (strncmp (*next_arg
,
2474 "--disable-packet=",
2475 sizeof ("--disable-packet=") - 1) == 0)
2477 char *packets
, *tok
;
2479 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
2480 for (tok
= strtok (packets
, ",");
2482 tok
= strtok (NULL
, ","))
2484 if (strcmp ("vCont", tok
) == 0)
2485 disable_packet_vCont
= 1;
2486 else if (strcmp ("Tthread", tok
) == 0)
2487 disable_packet_Tthread
= 1;
2488 else if (strcmp ("qC", tok
) == 0)
2489 disable_packet_qC
= 1;
2490 else if (strcmp ("qfThreadInfo", tok
) == 0)
2491 disable_packet_qfThreadInfo
= 1;
2492 else if (strcmp ("threads", tok
) == 0)
2494 disable_packet_vCont
= 1;
2495 disable_packet_Tthread
= 1;
2496 disable_packet_qC
= 1;
2497 disable_packet_qfThreadInfo
= 1;
2501 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
2503 gdbserver_show_disableable (stderr
);
2510 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
2518 if (setjmp (toplevel
))
2520 fprintf (stderr
, "Exiting\n");
2526 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
2528 gdbserver_usage (stderr
);
2535 /* --attach used to come after PORT, so allow it there for
2537 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
2544 && (*next_arg
== NULL
2545 || (*next_arg
)[0] == '\0'
2546 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
2548 || next_arg
[1] != NULL
))
2553 gdbserver_usage (stderr
);
2557 initialize_inferiors ();
2558 initialize_async_io ();
2560 if (target_supports_tracepoints ())
2561 initialize_tracepoint ();
2563 own_buf
= xmalloc (PBUFSIZ
+ 1);
2564 mem_buf
= xmalloc (PBUFSIZ
);
2566 if (pid
== 0 && *next_arg
!= NULL
)
2570 n
= argc
- (next_arg
- argv
);
2571 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
2572 for (i
= 0; i
< n
; i
++)
2573 program_argv
[i
] = xstrdup (next_arg
[i
]);
2574 program_argv
[i
] = NULL
;
2576 /* Wait till we are at first instruction in program. */
2577 start_inferior (program_argv
);
2579 /* We are now (hopefully) stopped at the first instruction of
2580 the target process. This assumes that the target process was
2581 successfully created. */
2585 if (attach_inferior (pid
) == -1)
2586 error ("Attaching not supported on this target");
2588 /* Otherwise succeeded. */
2592 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2593 last_status
.value
.integer
= 0;
2594 last_ptid
= minus_one_ptid
;
2597 /* Don't report shared library events on the initial connection,
2598 even if some libraries are preloaded. Avoids the "stopped by
2599 shared library event" notice on gdb side. */
2602 if (setjmp (toplevel
))
2604 detach_or_kill_for_exit ();
2608 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2609 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2614 if (!was_running
&& !multi_mode
)
2616 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
2624 /* Be sure we're out of tfind mode. */
2625 current_traceframe
= -1;
2629 if (setjmp (toplevel
) != 0)
2631 /* An error occurred. */
2632 if (response_needed
)
2634 write_enn (own_buf
);
2639 /* Wait for events. This will return when all event sources are
2640 removed from the event loop. */
2641 start_event_loop ();
2643 /* If an exit was requested (using the "monitor exit" command),
2644 terminate now. The only other way to get here is for
2645 getpkt to fail; close the connection and reopen it at the
2650 detach_or_kill_for_exit ();
2655 "Remote side has terminated connection. "
2656 "GDBserver will reopen the connection.\n");
2660 if (disconnected_tracing
)
2662 /* Try to enable non-stop/async mode, so we we can both
2663 wait for an async socket accept, and handle async
2664 target events simultaneously. There's also no point
2665 either in having the target always stop all threads,
2666 when we're going to pass signals down without
2670 if (start_non_stop (1))
2673 /* Detaching implicitly resumes all threads; simply
2674 disconnecting does not. */
2680 "Disconnected tracing disabled; stopping trace run.\n");
2687 /* Event loop callback that handles a serial event. The first byte in
2688 the serial buffer gets us here. We expect characters to arrive at
2689 a brisk pace, so we read the rest of the packet with a blocking
2693 process_serial_event (void)
2703 int new_packet_len
= -1;
2705 /* Used to decide when gdbserver should exit in
2706 multi-mode/remote. */
2707 static int have_ran
= 0;
2710 have_ran
= target_running ();
2712 disable_async_io ();
2714 response_needed
= 0;
2715 packet_len
= getpkt (own_buf
);
2716 if (packet_len
<= 0)
2719 /* Force an event loop break. */
2722 response_needed
= 1;
2729 handle_query (own_buf
, packet_len
, &new_packet_len
);
2732 handle_general_set (own_buf
);
2735 require_running (own_buf
);
2740 pid
= strtol (&own_buf
[i
], NULL
, 16);
2744 ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
2746 if (tracing
&& disconnected_tracing
)
2748 struct thread_resume resume_info
;
2749 struct process_info
*process
= find_process_pid (pid
);
2751 if (process
== NULL
)
2753 write_enn (own_buf
);
2758 "Disconnected tracing in effect, "
2759 "leaving gdbserver attached to the process\n");
2761 /* Make sure we're in non-stop/async mode, so we we can both
2762 wait for an async socket accept, and handle async target
2763 events simultaneously. There's also no point either in
2764 having the target stop all threads, when we're going to
2765 pass signals down without informing GDB. */
2769 fprintf (stderr
, "Forcing non-stop mode\n");
2775 process
->gdb_detached
= 1;
2777 /* Detaching implicitly resumes all threads. */
2778 resume_info
.thread
= minus_one_ptid
;
2779 resume_info
.kind
= resume_continue
;
2780 resume_info
.sig
= 0;
2781 (*the_target
->resume
) (&resume_info
, 1);
2784 break; /* from switch/case */
2787 fprintf (stderr
, "Detaching from process %d\n", pid
);
2789 if (detach_inferior (pid
) != 0)
2790 write_enn (own_buf
);
2793 discard_queued_stop_replies (pid
);
2796 if (extended_protocol
)
2798 /* Treat this like a normal program exit. */
2799 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2800 last_status
.value
.integer
= 0;
2801 last_ptid
= pid_to_ptid (pid
);
2803 current_inferior
= NULL
;
2810 /* If we are attached, then we can exit. Otherwise, we
2811 need to hang around doing nothing, until the child is
2813 for_each_inferior (&all_processes
,
2814 join_inferiors_callback
);
2820 extended_protocol
= 1;
2824 handle_status (own_buf
);
2827 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
2829 ptid_t gdb_id
, thread_id
;
2832 require_running (own_buf
);
2834 gdb_id
= read_ptid (&own_buf
[2], NULL
);
2836 pid
= ptid_get_pid (gdb_id
);
2838 if (ptid_equal (gdb_id
, null_ptid
)
2839 || ptid_equal (gdb_id
, minus_one_ptid
))
2840 thread_id
= null_ptid
;
2842 && ptid_equal (pid_to_ptid (pid
),
2845 struct thread_info
*thread
=
2846 (struct thread_info
*) find_inferior (&all_threads
,
2851 write_enn (own_buf
);
2855 thread_id
= ((struct inferior_list_entry
*)thread
)->id
;
2859 thread_id
= gdb_id_to_thread_id (gdb_id
);
2860 if (ptid_equal (thread_id
, null_ptid
))
2862 write_enn (own_buf
);
2867 if (own_buf
[1] == 'g')
2869 if (ptid_equal (thread_id
, null_ptid
))
2871 /* GDB is telling us to choose any thread. Check if
2872 the currently selected thread is still valid. If
2873 it is not, select the first available. */
2874 struct thread_info
*thread
=
2875 (struct thread_info
*) find_inferior_id (&all_threads
,
2878 thread_id
= all_threads
.head
->id
;
2881 general_thread
= thread_id
;
2882 set_desired_inferior (1);
2884 else if (own_buf
[1] == 'c')
2885 cont_thread
= thread_id
;
2886 else if (own_buf
[1] == 's')
2887 step_thread
= thread_id
;
2893 /* Silently ignore it so that gdb can extend the protocol
2894 without compatibility headaches. */
2899 require_running (own_buf
);
2900 if (current_traceframe
>= 0)
2902 struct regcache
*regcache
= new_register_cache ();
2904 if (fetch_traceframe_registers (current_traceframe
,
2906 registers_to_string (regcache
, own_buf
);
2908 write_enn (own_buf
);
2909 free_register_cache (regcache
);
2913 struct regcache
*regcache
;
2915 set_desired_inferior (1);
2916 regcache
= get_thread_regcache (current_inferior
, 1);
2917 registers_to_string (regcache
, own_buf
);
2921 require_running (own_buf
);
2922 if (current_traceframe
>= 0)
2923 write_enn (own_buf
);
2926 struct regcache
*regcache
;
2928 set_desired_inferior (1);
2929 regcache
= get_thread_regcache (current_inferior
, 1);
2930 registers_from_string (regcache
, &own_buf
[1]);
2935 require_running (own_buf
);
2936 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
2937 if (gdb_read_memory (mem_addr
, mem_buf
, len
) == 0)
2938 convert_int_to_ascii (mem_buf
, own_buf
, len
);
2940 write_enn (own_buf
);
2943 require_running (own_buf
);
2944 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
2945 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
2948 write_enn (own_buf
);
2951 require_running (own_buf
);
2952 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
2953 &mem_addr
, &len
, &mem_buf
) < 0
2954 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
2955 write_enn (own_buf
);
2960 require_running (own_buf
);
2961 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
2962 if (target_signal_to_host_p (sig
))
2963 signal
= target_signal_to_host (sig
);
2966 myresume (own_buf
, 0, signal
);
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
, 1, signal
);
2978 require_running (own_buf
);
2980 myresume (own_buf
, 0, signal
);
2983 require_running (own_buf
);
2985 myresume (own_buf
, 1, signal
);
2987 case 'Z': /* insert_ ... */
2989 case 'z': /* remove_ ... */
2993 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
2994 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
2995 char type
= own_buf
[1];
2997 const int insert
= ch
== 'Z';
2999 /* Default to unrecognized/unsupported. */
3003 case '0': /* software-breakpoint */
3004 case '1': /* hardware-breakpoint */
3005 case '2': /* write watchpoint */
3006 case '3': /* read watchpoint */
3007 case '4': /* access watchpoint */
3008 require_running (own_buf
);
3009 if (insert
&& the_target
->insert_point
!= NULL
)
3010 res
= (*the_target
->insert_point
) (type
, addr
, len
);
3011 else if (!insert
&& the_target
->remove_point
!= NULL
)
3012 res
= (*the_target
->remove_point
) (type
, addr
, len
);
3024 write_enn (own_buf
);
3028 response_needed
= 0;
3029 if (!target_running ())
3030 /* The packet we received doesn't make sense - but we can't
3031 reply to it, either. */
3034 fprintf (stderr
, "Killing all inferiors\n");
3035 for_each_inferior (&all_processes
, kill_inferior_callback
);
3037 /* When using the extended protocol, we wait with no program
3038 running. The traditional protocol will exit instead. */
3039 if (extended_protocol
)
3041 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3042 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3050 ptid_t gdb_id
, thread_id
;
3052 require_running (own_buf
);
3054 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3055 thread_id
= gdb_id_to_thread_id (gdb_id
);
3056 if (ptid_equal (thread_id
, null_ptid
))
3058 write_enn (own_buf
);
3062 if (mythread_alive (thread_id
))
3065 write_enn (own_buf
);
3069 response_needed
= 0;
3071 /* Restarting the inferior is only supported in the extended
3073 if (extended_protocol
)
3075 if (target_running ())
3076 for_each_inferior (&all_processes
,
3077 kill_inferior_callback
);
3078 fprintf (stderr
, "GDBserver restarting\n");
3080 /* Wait till we are at 1st instruction in prog. */
3081 if (program_argv
!= NULL
)
3082 start_inferior (program_argv
);
3085 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3086 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3092 /* It is a request we don't understand. Respond with an
3093 empty packet so that gdb knows that we don't support this
3099 /* Extended (long) request. */
3100 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
3104 /* It is a request we don't understand. Respond with an empty
3105 packet so that gdb knows that we don't support this
3111 if (new_packet_len
!= -1)
3112 putpkt_binary (own_buf
, new_packet_len
);
3116 response_needed
= 0;
3118 if (!extended_protocol
&& have_ran
&& !target_running ())
3120 /* In non-stop, defer exiting until GDB had a chance to query
3121 the whole vStopped list (until it gets an OK). */
3124 fprintf (stderr
, "GDBserver exiting\n");
3136 /* Event-loop callback for serial events. */
3139 handle_serial_event (int err
, gdb_client_data client_data
)
3142 fprintf (stderr
, "handling possible serial event\n");
3144 /* Really handle it. */
3145 if (process_serial_event () < 0)
3148 /* Be sure to not change the selected inferior behind GDB's back.
3149 Important in the non-stop mode asynchronous protocol. */
3150 set_desired_inferior (1);
3155 /* Event-loop callback for target events. */
3158 handle_target_event (int err
, gdb_client_data client_data
)
3161 fprintf (stderr
, "handling possible target event\n");
3163 last_ptid
= mywait (minus_one_ptid
, &last_status
,
3166 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3168 int pid
= ptid_get_pid (last_ptid
);
3169 struct process_info
*process
= find_process_pid (pid
);
3170 int forward_event
= !gdb_connected () || process
->gdb_detached
;
3172 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3173 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3175 mark_breakpoints_out (process
);
3176 mourn_inferior (process
);
3180 /* We're reporting this thread as stopped. Update its
3181 "want-stopped" state to what the client wants, until it
3182 gets a new resume action. */
3183 current_inferior
->last_resume_kind
= resume_stop
;
3184 current_inferior
->last_status
= last_status
;
3189 if (!target_running ())
3191 /* The last process exited. We're done. */
3195 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
3197 /* A thread stopped with a signal, but gdb isn't
3198 connected to handle it. Pass it down to the
3199 inferior, as if it wasn't being traced. */
3200 struct thread_resume resume_info
;
3204 "GDB not connected; forwarding event %d for [%s]\n",
3205 (int) last_status
.kind
,
3206 target_pid_to_str (last_ptid
));
3208 resume_info
.thread
= last_ptid
;
3209 resume_info
.kind
= resume_continue
;
3210 resume_info
.sig
= target_signal_to_host (last_status
.value
.sig
);
3211 (*the_target
->resume
) (&resume_info
, 1);
3213 else if (debug_threads
)
3214 fprintf (stderr
, "GDB not connected; ignoring event %d for [%s]\n",
3215 (int) last_status
.kind
,
3216 target_pid_to_str (last_ptid
));
3220 /* Something interesting. Tell GDB about it. */
3221 push_event (last_ptid
, &last_status
);
3225 /* Be sure to not change the selected inferior behind GDB's back.
3226 Important in the non-stop mode asynchronous protocol. */
3227 set_desired_inferior (1);