1 /* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
36 ptid_t general_thread
;
41 static int extended_protocol
;
42 static int response_needed
;
43 static int exit_requested
;
48 static char **program_argv
, **wrapper_argv
;
50 /* Enable miscellaneous debugging output. The name is historical - it
51 was originally used to debug LinuxThreads support. */
54 /* Enable debugging of h/w breakpoint/watchpoint support. */
57 int pass_signals
[TARGET_SIGNAL_LAST
];
61 const char *gdbserver_xmltarget
;
63 /* The PID of the originally created or attached inferior. Used to
64 send signals to the process when GDB sends us an asynchronous interrupt
65 (user hitting Control-C in the client), and to wait for the child to exit
66 when no longer debugging it. */
68 unsigned long signal_pid
;
71 /* A file descriptor for the controlling terminal. */
74 /* TERMINAL_FD's original foreground group. */
75 pid_t old_foreground_pgrp
;
77 /* Hand back terminal ownership to the original foreground group. */
80 restore_old_foreground_pgrp (void)
82 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
86 /* Set if you want to disable optional thread related packets support
87 in gdbserver, for the sake of testing GDB against stubs that don't
89 int disable_packet_vCont
;
90 int disable_packet_Tthread
;
91 int disable_packet_qC
;
92 int disable_packet_qfThreadInfo
;
94 /* Last status reported to GDB. */
95 static struct target_waitstatus last_status
;
96 static ptid_t last_ptid
;
99 static unsigned char *mem_buf
;
101 /* Structure holding information relative to a single stop reply. We
102 keep a queue of these (really a singly-linked list) to push to GDB
106 /* Pointer to next in list. */
107 struct vstop_notif
*next
;
109 /* Thread or process that got the event. */
113 struct target_waitstatus status
;
116 /* The pending stop replies list head. */
117 static struct vstop_notif
*notif_queue
= NULL
;
119 /* Put a stop reply to the stop reply queue. */
122 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
124 struct vstop_notif
*new_notif
;
126 new_notif
= malloc (sizeof (*new_notif
));
127 new_notif
->next
= NULL
;
128 new_notif
->ptid
= ptid
;
129 new_notif
->status
= *status
;
133 struct vstop_notif
*tail
;
134 for (tail
= notif_queue
;
138 tail
->next
= new_notif
;
141 notif_queue
= new_notif
;
146 struct vstop_notif
*n
;
148 for (n
= notif_queue
; n
; n
= n
->next
)
151 fprintf (stderr
, "pending stop replies: %d\n", i
);
155 /* Place an event in the stop reply queue, and push a notification if
156 we aren't sending one yet. */
159 push_event (ptid_t ptid
, struct target_waitstatus
*status
)
161 gdb_assert (status
->kind
!= TARGET_WAITKIND_IGNORE
);
163 queue_stop_reply (ptid
, status
);
165 /* If this is the first stop reply in the queue, then inform GDB
166 about it, by sending a Stop notification. */
167 if (notif_queue
->next
== NULL
)
172 prepare_resume_reply (p
,
173 notif_queue
->ptid
, ¬if_queue
->status
);
174 putpkt_notif (own_buf
);
178 /* Get rid of the currently pending stop replies for PID. If PID is
179 -1, then apply to all processes. */
182 discard_queued_stop_replies (int pid
)
184 struct vstop_notif
*prev
= NULL
, *reply
, *next
;
186 for (reply
= notif_queue
; reply
; reply
= next
)
191 || ptid_get_pid (reply
->ptid
) == pid
)
193 if (reply
== notif_queue
)
196 prev
->next
= reply
->next
;
205 /* If there are more stop replies to push, push one now. */
208 send_next_stop_reply (char *own_buf
)
211 prepare_resume_reply (own_buf
,
213 ¬if_queue
->status
);
219 target_running (void)
221 return all_threads
.head
!= NULL
;
225 start_inferior (char **argv
)
227 char **new_argv
= argv
;
229 if (wrapper_argv
!= NULL
)
233 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
235 for (i
= 0; argv
[i
] != NULL
; i
++)
237 new_argv
= alloca (sizeof (char *) * count
);
239 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
240 new_argv
[count
++] = wrapper_argv
[i
];
241 for (i
= 0; argv
[i
] != NULL
; i
++)
242 new_argv
[count
++] = argv
[i
];
243 new_argv
[count
] = NULL
;
249 for (i
= 0; new_argv
[i
]; ++i
)
250 fprintf (stderr
, "new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
255 signal (SIGTTOU
, SIG_DFL
);
256 signal (SIGTTIN
, SIG_DFL
);
259 signal_pid
= create_inferior (new_argv
[0], new_argv
);
261 /* FIXME: we don't actually know at this point that the create
262 actually succeeded. We won't know that until we wait. */
263 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
268 signal (SIGTTOU
, SIG_IGN
);
269 signal (SIGTTIN
, SIG_IGN
);
270 terminal_fd
= fileno (stderr
);
271 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
272 tcsetpgrp (terminal_fd
, signal_pid
);
273 atexit (restore_old_foreground_pgrp
);
276 if (wrapper_argv
!= NULL
)
278 struct thread_resume resume_info
;
281 resume_info
.thread
= pid_to_ptid (signal_pid
);
282 resume_info
.kind
= resume_continue
;
285 ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
287 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
292 (*the_target
->resume
) (&resume_info
, 1);
294 mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
295 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
298 current_inferior
->last_resume_kind
= resume_stop
;
299 current_inferior
->last_status
= last_status
;
301 while (last_status
.value
.sig
!= TARGET_SIGNAL_TRAP
);
303 current_inferior
->last_resume_kind
= resume_stop
;
304 current_inferior
->last_status
= last_status
;
308 /* Wait till we are at 1st instruction in program, return new pid
309 (assuming success). */
310 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
312 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
313 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
315 current_inferior
->last_resume_kind
= resume_stop
;
316 current_inferior
->last_status
= last_status
;
323 attach_inferior (int pid
)
325 /* myattach should return -1 if attaching is unsupported,
326 0 if it succeeded, and call error() otherwise. */
328 if (myattach (pid
) != 0)
331 fprintf (stderr
, "Attached; pid = %d\n", pid
);
334 /* FIXME - It may be that we should get the SIGNAL_PID from the
335 attach function, so that it can be the main thread instead of
336 whichever we were told to attach to. */
341 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
343 /* GDB knows to ignore the first SIGSTOP after attaching to a running
344 process using the "attach" command, but this is different; it's
345 just using "target remote". Pretend it's just starting up. */
346 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
347 && last_status
.value
.sig
== TARGET_SIGNAL_STOP
)
348 last_status
.value
.sig
= TARGET_SIGNAL_TRAP
;
350 current_inferior
->last_resume_kind
= resume_stop
;
351 current_inferior
->last_status
= last_status
;
357 extern int remote_debug
;
359 /* Decode a qXfer read request. Return 0 if everything looks OK,
363 decode_xfer_read (char *buf
, char **annex
, CORE_ADDR
*ofs
, unsigned int *len
)
365 /* Extract and NUL-terminate the annex. */
367 while (*buf
&& *buf
!= ':')
373 /* After the read marker and annex, qXfer looks like a
374 traditional 'm' packet. */
375 decode_m_packet (buf
, ofs
, len
);
380 /* Write the response to a successful qXfer read. Returns the
381 length of the (binary) data stored in BUF, corresponding
382 to as much of DATA/LEN as we could fit. IS_MORE controls
383 the first character of the response. */
385 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
394 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
398 /* Handle all of the extended 'Q' packets. */
401 handle_general_set (char *own_buf
)
403 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
405 int numsigs
= (int) TARGET_SIGNAL_LAST
, i
;
406 const char *p
= own_buf
+ strlen ("QPassSignals:");
409 p
= decode_address_to_semicolon (&cursig
, p
);
410 for (i
= 0; i
< numsigs
; i
++)
416 /* Keep looping, to clear the remaining signals. */
419 p
= decode_address_to_semicolon (&cursig
, p
);
424 strcpy (own_buf
, "OK");
428 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
432 fprintf (stderr
, "[noack mode enabled]\n");
441 if (strncmp (own_buf
, "QNonStop:", 9) == 0)
443 char *mode
= own_buf
+ 9;
447 if (strcmp (mode
, "0") == 0)
449 else if (strcmp (mode
, "1") == 0)
453 /* We don't know what this mode is, so complain to
455 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
461 req_str
= req
? "non-stop" : "all-stop";
462 if (start_non_stop (req
) != 0)
464 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
472 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
478 if (target_supports_tracepoints ()
479 && handle_tracepoint_general_set (own_buf
))
482 /* Otherwise we didn't know what packet it was. Say we didn't
488 get_features_xml (const char *annex
)
490 /* gdbserver_xmltarget defines what to return when looking
491 for the "target.xml" file. Its contents can either be
492 verbatim XML code (prefixed with a '@') or else the name
493 of the actual XML file to be used in place of "target.xml".
495 This variable is set up from the auto-generated
496 init_registers_... routine for the current target. */
498 if (gdbserver_xmltarget
499 && strcmp (annex
, "target.xml") == 0)
501 if (*gdbserver_xmltarget
== '@')
502 return gdbserver_xmltarget
+ 1;
504 annex
= gdbserver_xmltarget
;
509 extern const char *const xml_builtin
[][2];
512 /* Look for the annex. */
513 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
514 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
517 if (xml_builtin
[i
][0] != NULL
)
518 return xml_builtin
[i
][1];
526 monitor_show_help (void)
528 monitor_output ("The following monitor commands are supported:\n");
529 monitor_output (" set debug <0|1>\n");
530 monitor_output (" Enable general debugging messages\n");
531 monitor_output (" set debug-hw-points <0|1>\n");
532 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
533 monitor_output (" set remote-debug <0|1>\n");
534 monitor_output (" Enable remote protocol debugging messages\n");
535 monitor_output (" exit\n");
536 monitor_output (" Quit GDBserver\n");
539 /* Read trace frame or inferior memory. */
542 read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
544 if (current_traceframe
>= 0)
547 ULONGEST length
= len
;
549 if (traceframe_read_mem (current_traceframe
,
550 memaddr
, myaddr
, len
, &nbytes
))
552 /* Data read from trace buffer, we're done. */
553 if (nbytes
== length
)
555 if (!in_readonly_region (memaddr
, length
))
557 /* Otherwise we have a valid readonly case, fall through. */
558 /* (assume no half-trace half-real blocks for now) */
561 return read_inferior_memory (memaddr
, myaddr
, len
);
564 /* Write trace frame or inferior memory. Actually, writing to trace
565 frames is forbidden. */
568 write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
570 if (current_traceframe
>= 0)
573 return write_inferior_memory (memaddr
, myaddr
, len
);
576 /* Subroutine of handle_search_memory to simplify it. */
579 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
580 gdb_byte
*pattern
, unsigned pattern_len
,
581 gdb_byte
*search_buf
,
582 unsigned chunk_size
, unsigned search_buf_size
,
583 CORE_ADDR
*found_addrp
)
585 /* Prime the search buffer. */
587 if (read_memory (start_addr
, search_buf
, search_buf_size
) != 0)
589 warning ("Unable to access target memory at 0x%lx, halting search.",
594 /* Perform the search.
596 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
597 When we've scanned N bytes we copy the trailing bytes to the start and
598 read in another N bytes. */
600 while (search_space_len
>= pattern_len
)
603 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
607 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
609 if (found_ptr
!= NULL
)
611 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
612 *found_addrp
= found_addr
;
616 /* Not found in this chunk, skip to next chunk. */
618 /* Don't let search_space_len wrap here, it's unsigned. */
619 if (search_space_len
>= chunk_size
)
620 search_space_len
-= chunk_size
;
622 search_space_len
= 0;
624 if (search_space_len
>= pattern_len
)
626 unsigned keep_len
= search_buf_size
- chunk_size
;
627 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
630 /* Copy the trailing part of the previous iteration to the front
631 of the buffer for the next iteration. */
632 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
634 nr_to_read
= (search_space_len
- keep_len
< chunk_size
635 ? search_space_len
- keep_len
638 if (read_memory (read_addr
, search_buf
+ keep_len
,
641 warning ("Unable to access target memory at 0x%lx, halting search.",
646 start_addr
+= chunk_size
;
655 /* Handle qSearch:memory packets. */
658 handle_search_memory (char *own_buf
, int packet_len
)
660 CORE_ADDR start_addr
;
661 CORE_ADDR search_space_len
;
663 unsigned int pattern_len
;
664 /* NOTE: also defined in find.c testcase. */
665 #define SEARCH_CHUNK_SIZE 16000
666 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
667 /* Buffer to hold memory contents for searching. */
668 gdb_byte
*search_buf
;
669 unsigned search_buf_size
;
671 CORE_ADDR found_addr
;
672 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
674 pattern
= malloc (packet_len
);
677 error ("Unable to allocate memory to perform the search");
678 strcpy (own_buf
, "E00");
681 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
682 packet_len
- cmd_name_len
,
683 &start_addr
, &search_space_len
,
684 pattern
, &pattern_len
) < 0)
687 error ("Error in parsing qSearch:memory packet");
688 strcpy (own_buf
, "E00");
692 search_buf_size
= chunk_size
+ pattern_len
- 1;
694 /* No point in trying to allocate a buffer larger than the search space. */
695 if (search_space_len
< search_buf_size
)
696 search_buf_size
= search_space_len
;
698 search_buf
= malloc (search_buf_size
);
699 if (search_buf
== NULL
)
702 error ("Unable to allocate memory to perform the search");
703 strcpy (own_buf
, "E00");
707 found
= handle_search_memory_1 (start_addr
, search_space_len
,
708 pattern
, pattern_len
,
709 search_buf
, chunk_size
, search_buf_size
,
713 sprintf (own_buf
, "1,%lx", (long) found_addr
);
715 strcpy (own_buf
, "0");
717 strcpy (own_buf
, "E00");
723 #define require_running(BUF) \
724 if (!target_running ()) \
730 /* Handle monitor commands not handled by target-specific handlers. */
733 handle_monitor_command (char *mon
)
735 if (strcmp (mon
, "set debug 1") == 0)
738 monitor_output ("Debug output enabled.\n");
740 else if (strcmp (mon
, "set debug 0") == 0)
743 monitor_output ("Debug output disabled.\n");
745 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
748 monitor_output ("H/W point debugging output enabled.\n");
750 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
753 monitor_output ("H/W point debugging output disabled.\n");
755 else if (strcmp (mon
, "set remote-debug 1") == 0)
758 monitor_output ("Protocol debug output enabled.\n");
760 else if (strcmp (mon
, "set remote-debug 0") == 0)
763 monitor_output ("Protocol debug output disabled.\n");
765 else if (strcmp (mon
, "help") == 0)
766 monitor_show_help ();
767 else if (strcmp (mon
, "exit") == 0)
771 monitor_output ("Unknown monitor command.\n\n");
772 monitor_show_help ();
778 handle_threads_qxfer_proper (struct buffer
*buffer
)
780 struct inferior_list_entry
*thread
;
782 buffer_grow_str (buffer
, "<threads>\n");
784 for (thread
= all_threads
.head
; thread
; thread
= thread
->next
)
786 ptid_t ptid
= thread_to_gdb_id ((struct thread_info
*)thread
);
791 write_ptid (ptid_s
, ptid
);
793 if (the_target
->core_of_thread
)
794 core
= (*the_target
->core_of_thread
) (ptid
);
798 sprintf (core_s
, "%d", core
);
799 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
804 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
809 buffer_grow_str0 (buffer
, "</threads>\n");
813 handle_threads_qxfer (const char *annex
,
814 unsigned char *readbuf
,
815 CORE_ADDR offset
, int length
)
817 static char *result
= 0;
818 static unsigned int result_length
= 0;
820 if (annex
&& strcmp (annex
, "") != 0)
825 struct buffer buffer
;
826 /* When asked for data at offset 0, generate everything and store into
827 'result'. Successive reads will be served off 'result'. */
831 buffer_init (&buffer
);
833 handle_threads_qxfer_proper (&buffer
);
835 result
= buffer_finish (&buffer
);
836 result_length
= strlen (result
);
837 buffer_free (&buffer
);
840 if (offset
>= result_length
)
842 /* We're out of data. */
849 if (length
> result_length
- offset
)
850 length
= result_length
- offset
;
852 memcpy (readbuf
, result
+ offset
, length
);
858 /* Table used by the crc32 function to calcuate the checksum. */
860 static unsigned int crc32_table
[256] =
863 /* Compute 32 bit CRC from inferior memory.
865 On success, return 32 bit CRC.
866 On failure, return (unsigned long long) -1. */
868 static unsigned long long
869 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
873 /* Initialize the CRC table and the decoding table. */
877 for (i
= 0; i
< 256; i
++)
879 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
880 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
887 unsigned char byte
= 0;
889 /* Return failure if memory read fails. */
890 if (read_inferior_memory (base
, &byte
, 1) != 0)
891 return (unsigned long long) -1;
893 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
896 return (unsigned long long) crc
;
899 /* Handle all of the extended 'q' packets. */
901 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
903 static struct inferior_list_entry
*thread_ptr
;
905 /* Reply the current thread id. */
906 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
909 require_running (own_buf
);
911 if (!ptid_equal (general_thread
, null_ptid
)
912 && !ptid_equal (general_thread
, minus_one_ptid
))
913 gdb_id
= general_thread
;
916 thread_ptr
= all_threads
.head
;
917 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
920 sprintf (own_buf
, "QC");
922 own_buf
= write_ptid (own_buf
, gdb_id
);
926 if (strcmp ("qSymbol::", own_buf
) == 0)
928 /* GDB is suggesting new symbols have been loaded. This may
929 mean a new shared library has been detected as loaded, so
930 take the opportunity to check if breakpoints we think are
931 inserted, still are. Note that it isn't guaranteed that
932 we'll see this when a shared library is loaded, and nor will
933 we see this for unloads (although breakpoints in unloaded
934 libraries shouldn't trigger), as GDB may not find symbols for
935 the library at all. We also re-validate breakpoints when we
936 see a second GDB breakpoint for the same address, and or when
937 we access breakpoint shadows. */
938 validate_breakpoints ();
940 if (target_supports_tracepoints ())
941 tracepoint_look_up_symbols ();
943 if (target_running () && the_target
->look_up_symbols
!= NULL
)
944 (*the_target
->look_up_symbols
) ();
946 strcpy (own_buf
, "OK");
950 if (!disable_packet_qfThreadInfo
)
952 if (strcmp ("qfThreadInfo", own_buf
) == 0)
956 require_running (own_buf
);
957 thread_ptr
= all_threads
.head
;
960 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
961 write_ptid (own_buf
, gdb_id
);
962 thread_ptr
= thread_ptr
->next
;
966 if (strcmp ("qsThreadInfo", own_buf
) == 0)
970 require_running (own_buf
);
971 if (thread_ptr
!= NULL
)
974 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
975 write_ptid (own_buf
, gdb_id
);
976 thread_ptr
= thread_ptr
->next
;
981 sprintf (own_buf
, "l");
987 if (the_target
->read_offsets
!= NULL
988 && strcmp ("qOffsets", own_buf
) == 0)
990 CORE_ADDR text
, data
;
992 require_running (own_buf
);
993 if (the_target
->read_offsets (&text
, &data
))
994 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
995 (long)text
, (long)data
, (long)data
);
1002 if (the_target
->qxfer_spu
!= NULL
1003 && strncmp ("qXfer:spu:read:", own_buf
, 15) == 0)
1009 unsigned char *spu_buf
;
1011 require_running (own_buf
);
1012 strcpy (own_buf
, "E00");
1013 if (decode_xfer_read (own_buf
+ 15, &annex
, &ofs
, &len
) < 0)
1015 if (len
> PBUFSIZ
- 2)
1017 spu_buf
= malloc (len
+ 1);
1021 n
= (*the_target
->qxfer_spu
) (annex
, spu_buf
, NULL
, ofs
, len
+ 1);
1023 write_enn (own_buf
);
1025 *new_packet_len_p
= write_qxfer_response (own_buf
, spu_buf
, len
, 1);
1027 *new_packet_len_p
= write_qxfer_response (own_buf
, spu_buf
, n
, 0);
1033 if (the_target
->qxfer_spu
!= NULL
1034 && strncmp ("qXfer:spu:write:", own_buf
, 16) == 0)
1040 unsigned char *spu_buf
;
1042 require_running (own_buf
);
1043 strcpy (own_buf
, "E00");
1044 spu_buf
= malloc (packet_len
- 15);
1047 if (decode_xfer_write (own_buf
+ 16, packet_len
- 16, &annex
,
1048 &ofs
, &len
, spu_buf
) < 0)
1054 n
= (*the_target
->qxfer_spu
)
1055 (annex
, NULL
, (unsigned const char *)spu_buf
, ofs
, len
);
1057 write_enn (own_buf
);
1059 sprintf (own_buf
, "%x", n
);
1065 if (the_target
->read_auxv
!= NULL
1066 && strncmp ("qXfer:auxv:read:", own_buf
, 16) == 0)
1068 unsigned char *data
;
1074 require_running (own_buf
);
1076 /* Reject any annex; grab the offset and length. */
1077 if (decode_xfer_read (own_buf
+ 16, &annex
, &ofs
, &len
) < 0
1078 || annex
[0] != '\0')
1080 strcpy (own_buf
, "E00");
1084 /* Read one extra byte, as an indicator of whether there is
1086 if (len
> PBUFSIZ
- 2)
1088 data
= malloc (len
+ 1);
1091 write_enn (own_buf
);
1094 n
= (*the_target
->read_auxv
) (ofs
, data
, len
+ 1);
1096 write_enn (own_buf
);
1098 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1100 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1107 if (strncmp ("qXfer:features:read:", own_buf
, 20) == 0)
1110 unsigned int len
, total_len
;
1111 const char *document
;
1114 require_running (own_buf
);
1116 /* Grab the annex, offset, and length. */
1117 if (decode_xfer_read (own_buf
+ 20, &annex
, &ofs
, &len
) < 0)
1119 strcpy (own_buf
, "E00");
1123 /* Now grab the correct annex. */
1124 document
= get_features_xml (annex
);
1125 if (document
== NULL
)
1127 strcpy (own_buf
, "E00");
1131 total_len
= strlen (document
);
1132 if (len
> PBUFSIZ
- 2)
1135 if (ofs
> total_len
)
1136 write_enn (own_buf
);
1137 else if (len
< total_len
- ofs
)
1138 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
1141 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
1142 total_len
- ofs
, 0);
1147 if (strncmp ("qXfer:libraries:read:", own_buf
, 21) == 0)
1150 unsigned int len
, total_len
;
1152 struct inferior_list_entry
*dll_ptr
;
1155 require_running (own_buf
);
1157 /* Reject any annex; grab the offset and length. */
1158 if (decode_xfer_read (own_buf
+ 21, &annex
, &ofs
, &len
) < 0
1159 || annex
[0] != '\0')
1161 strcpy (own_buf
, "E00");
1165 /* Over-estimate the necessary memory. Assume that every character
1166 in the library name must be escaped. */
1168 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
1169 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
1171 document
= malloc (total_len
);
1172 if (document
== NULL
)
1174 write_enn (own_buf
);
1177 strcpy (document
, "<library-list>\n");
1178 p
= document
+ strlen (document
);
1180 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
1182 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
1185 strcpy (p
, " <library name=\"");
1187 name
= xml_escape_text (dll
->name
);
1191 strcpy (p
, "\"><segment address=\"");
1193 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1195 strcpy (p
, "\"/></library>\n");
1199 strcpy (p
, "</library-list>\n");
1201 total_len
= strlen (document
);
1202 if (len
> PBUFSIZ
- 2)
1205 if (ofs
> total_len
)
1206 write_enn (own_buf
);
1207 else if (len
< total_len
- ofs
)
1208 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
1211 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
1212 total_len
- ofs
, 0);
1218 if (the_target
->qxfer_osdata
!= NULL
1219 && strncmp ("qXfer:osdata:read:", own_buf
, 18) == 0)
1225 unsigned char *workbuf
;
1227 strcpy (own_buf
, "E00");
1228 if (decode_xfer_read (own_buf
+ 18, &annex
, &ofs
, &len
) < 0)
1230 if (len
> PBUFSIZ
- 2)
1232 workbuf
= malloc (len
+ 1);
1236 n
= (*the_target
->qxfer_osdata
) (annex
, workbuf
, NULL
, ofs
, len
+ 1);
1238 write_enn (own_buf
);
1240 *new_packet_len_p
= write_qxfer_response (own_buf
, workbuf
, len
, 1);
1242 *new_packet_len_p
= write_qxfer_response (own_buf
, workbuf
, n
, 0);
1248 if (the_target
->qxfer_siginfo
!= NULL
1249 && strncmp ("qXfer:siginfo:read:", own_buf
, 19) == 0)
1251 unsigned char *data
;
1257 require_running (own_buf
);
1259 /* Reject any annex; grab the offset and length. */
1260 if (decode_xfer_read (own_buf
+ 19, &annex
, &ofs
, &len
) < 0
1261 || annex
[0] != '\0')
1263 strcpy (own_buf
, "E00");
1267 /* Read one extra byte, as an indicator of whether there is
1269 if (len
> PBUFSIZ
- 2)
1271 data
= malloc (len
+ 1);
1274 n
= (*the_target
->qxfer_siginfo
) (annex
, data
, NULL
, ofs
, len
+ 1);
1276 write_enn (own_buf
);
1278 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1280 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1286 if (the_target
->qxfer_siginfo
!= NULL
1287 && strncmp ("qXfer:siginfo:write:", own_buf
, 20) == 0)
1293 unsigned char *data
;
1295 require_running (own_buf
);
1297 strcpy (own_buf
, "E00");
1298 data
= malloc (packet_len
- 19);
1301 if (decode_xfer_write (own_buf
+ 20, packet_len
- 20, &annex
,
1302 &ofs
, &len
, data
) < 0)
1308 n
= (*the_target
->qxfer_siginfo
)
1309 (annex
, NULL
, (unsigned const char *)data
, ofs
, len
);
1311 write_enn (own_buf
);
1313 sprintf (own_buf
, "%x", n
);
1319 if (strncmp ("qXfer:threads:read:", own_buf
, 19) == 0)
1321 unsigned char *data
;
1327 require_running (own_buf
);
1329 /* Reject any annex; grab the offset and length. */
1330 if (decode_xfer_read (own_buf
+ 19, &annex
, &ofs
, &len
) < 0
1331 || annex
[0] != '\0')
1333 strcpy (own_buf
, "E00");
1337 /* Read one extra byte, as an indicator of whether there is
1339 if (len
> PBUFSIZ
- 2)
1341 data
= malloc (len
+ 1);
1344 n
= handle_threads_qxfer (annex
, data
, ofs
, len
+ 1);
1346 write_enn (own_buf
);
1348 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1350 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1356 if (strncmp ("qXfer:statictrace:read:", own_buf
,
1357 sizeof ("qXfer:statictrace:read:") -1) == 0)
1359 unsigned char *data
;
1365 require_running (own_buf
);
1367 if (current_traceframe
== -1)
1369 write_enn (own_buf
);
1373 /* Reject any annex; grab the offset and length. */
1374 if (decode_xfer_read (own_buf
+ sizeof ("qXfer:statictrace:read:") -1,
1375 &annex
, &ofs
, &len
) < 0
1376 || annex
[0] != '\0')
1378 strcpy (own_buf
, "E00");
1382 /* Read one extra byte, as an indicator of whether there is
1384 if (len
> PBUFSIZ
- 2)
1386 data
= malloc (len
+ 1);
1390 if (traceframe_read_sdata (current_traceframe
, ofs
,
1391 data
, len
+ 1, &nbytes
))
1392 write_enn (own_buf
);
1393 else if (nbytes
> len
)
1394 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1396 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, nbytes
, 0);
1402 /* Protocol features query. */
1403 if (strncmp ("qSupported", own_buf
, 10) == 0
1404 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1406 char *p
= &own_buf
[10];
1407 int gdb_supports_qRelocInsn
= 0;
1409 /* Start processing qSupported packet. */
1410 target_process_qsupported (NULL
);
1412 /* Process each feature being provided by GDB. The first
1413 feature will follow a ':', and latter features will follow
1417 char **qsupported
= NULL
;
1421 /* Two passes, to avoid nested strtok calls in
1422 target_process_qsupported. */
1423 for (p
= strtok (p
+ 1, ";");
1425 p
= strtok (NULL
, ";"))
1428 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
1429 qsupported
[count
- 1] = xstrdup (p
);
1432 for (i
= 0; i
< count
; i
++)
1435 if (strcmp (p
, "multiprocess+") == 0)
1437 /* GDB supports and wants multi-process support if
1439 if (target_supports_multi_process ())
1442 else if (strcmp (p
, "qRelocInsn+") == 0)
1444 /* GDB supports relocate instruction requests. */
1445 gdb_supports_qRelocInsn
= 1;
1448 target_process_qsupported (p
);
1456 sprintf (own_buf
, "PacketSize=%x;QPassSignals+", PBUFSIZ
- 1);
1458 /* We do not have any hook to indicate whether the target backend
1459 supports qXfer:libraries:read, so always report it. */
1460 strcat (own_buf
, ";qXfer:libraries:read+");
1462 if (the_target
->read_auxv
!= NULL
)
1463 strcat (own_buf
, ";qXfer:auxv:read+");
1465 if (the_target
->qxfer_spu
!= NULL
)
1466 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
1468 if (the_target
->qxfer_siginfo
!= NULL
)
1469 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1471 /* We always report qXfer:features:read, as targets may
1472 install XML files on a subsequent call to arch_setup.
1473 If we reported to GDB on startup that we don't support
1474 qXfer:feature:read at all, we will never be re-queried. */
1475 strcat (own_buf
, ";qXfer:features:read+");
1477 if (transport_is_reliable
)
1478 strcat (own_buf
, ";QStartNoAckMode+");
1480 if (the_target
->qxfer_osdata
!= NULL
)
1481 strcat (own_buf
, ";qXfer:osdata:read+");
1483 if (target_supports_multi_process ())
1484 strcat (own_buf
, ";multiprocess+");
1486 if (target_supports_non_stop ())
1487 strcat (own_buf
, ";QNonStop+");
1489 strcat (own_buf
, ";qXfer:threads:read+");
1491 if (target_supports_tracepoints ())
1493 strcat (own_buf
, ";ConditionalTracepoints+");
1494 strcat (own_buf
, ";TraceStateVariables+");
1495 strcat (own_buf
, ";TracepointSource+");
1496 strcat (own_buf
, ";DisconnectedTracing+");
1497 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
1498 strcat (own_buf
, ";FastTracepoints+");
1499 strcat (own_buf
, ";StaticTracepoints+");
1500 strcat (own_buf
, ";qXfer:statictrace:read+");
1506 /* Thread-local storage support. */
1507 if (the_target
->get_tls_address
!= NULL
1508 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
1510 char *p
= own_buf
+ 12;
1511 CORE_ADDR parts
[2], address
= 0;
1513 ptid_t ptid
= null_ptid
;
1515 require_running (own_buf
);
1517 for (i
= 0; i
< 3; i
++)
1525 p2
= strchr (p
, ',');
1538 ptid
= read_ptid (p
, NULL
);
1540 decode_address (&parts
[i
- 1], p
, len
);
1544 if (p
!= NULL
|| i
< 3)
1548 struct thread_info
*thread
= find_thread_ptid (ptid
);
1553 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
1559 strcpy (own_buf
, paddress(address
));
1564 write_enn (own_buf
);
1568 /* Otherwise, pretend we do not understand this packet. */
1571 /* Windows OS Thread Information Block address support. */
1572 if (the_target
->get_tib_address
!= NULL
1573 && strncmp ("qGetTIBAddr:", own_buf
, 12) == 0)
1578 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
1580 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
1583 strcpy (own_buf
, paddress(tlb
));
1588 write_enn (own_buf
);
1594 /* Handle "monitor" commands. */
1595 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
1597 char *mon
= malloc (PBUFSIZ
);
1598 int len
= strlen (own_buf
+ 6);
1602 write_enn (own_buf
);
1606 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
1608 write_enn (own_buf
);
1612 mon
[len
/ 2] = '\0';
1616 if (the_target
->handle_monitor_command
== NULL
1617 || (*the_target
->handle_monitor_command
) (mon
) == 0)
1618 /* Default processing. */
1619 handle_monitor_command (mon
);
1625 if (strncmp ("qSearch:memory:", own_buf
, sizeof ("qSearch:memory:") - 1) == 0)
1627 require_running (own_buf
);
1628 handle_search_memory (own_buf
, packet_len
);
1632 if (strcmp (own_buf
, "qAttached") == 0
1633 || strncmp (own_buf
, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1635 struct process_info
*process
;
1637 if (own_buf
[sizeof ("qAttached") - 1])
1639 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
1640 process
= (struct process_info
*)
1641 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
1645 require_running (own_buf
);
1646 process
= current_process ();
1649 if (process
== NULL
)
1651 write_enn (own_buf
);
1655 strcpy (own_buf
, process
->attached
? "1" : "0");
1659 if (strncmp ("qCRC:", own_buf
, 5) == 0)
1661 /* CRC check (compare-section). */
1665 unsigned long long crc
;
1667 require_running (own_buf
);
1668 base
= strtoul (own_buf
+ 5, &comma
, 16);
1669 if (*comma
++ != ',')
1671 write_enn (own_buf
);
1674 len
= strtoul (comma
, NULL
, 16);
1675 crc
= crc32 (base
, len
, 0xffffffff);
1676 /* Check for memory failure. */
1677 if (crc
== (unsigned long long) -1)
1679 write_enn (own_buf
);
1682 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
1686 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
1689 /* Otherwise we didn't know what packet it was. Say we didn't
1694 static void gdb_wants_all_threads_stopped (void);
1696 /* Parse vCont packets. */
1698 handle_v_cont (char *own_buf
)
1702 struct thread_resume
*resume_info
;
1703 struct thread_resume default_action
= {{0}};
1705 /* Count the number of semicolons in the packet. There should be one
1706 for every action. */
1712 p
= strchr (p
, ';');
1715 resume_info
= malloc (n
* sizeof (resume_info
[0]));
1716 if (resume_info
== NULL
)
1724 if (p
[0] == 's' || p
[0] == 'S')
1725 resume_info
[i
].kind
= resume_step
;
1726 else if (p
[0] == 'c' || p
[0] == 'C')
1727 resume_info
[i
].kind
= resume_continue
;
1728 else if (p
[0] == 't')
1729 resume_info
[i
].kind
= resume_stop
;
1733 if (p
[0] == 'S' || p
[0] == 'C')
1736 sig
= strtol (p
+ 1, &q
, 16);
1741 if (!target_signal_to_host_p (sig
))
1743 resume_info
[i
].sig
= target_signal_to_host (sig
);
1747 resume_info
[i
].sig
= 0;
1753 resume_info
[i
].thread
= minus_one_ptid
;
1754 default_action
= resume_info
[i
];
1756 /* Note: we don't increment i here, we'll overwrite this entry
1757 the next time through. */
1759 else if (p
[0] == ':')
1761 ptid_t ptid
= read_ptid (p
+ 1, &q
);
1766 if (p
[0] != ';' && p
[0] != 0)
1769 resume_info
[i
].thread
= ptid
;
1776 resume_info
[i
] = default_action
;
1778 /* Still used in occasional places in the backend. */
1780 && !ptid_equal (resume_info
[0].thread
, minus_one_ptid
)
1781 && resume_info
[0].kind
!= resume_stop
)
1782 cont_thread
= resume_info
[0].thread
;
1784 cont_thread
= minus_one_ptid
;
1785 set_desired_inferior (0);
1790 (*the_target
->resume
) (resume_info
, n
);
1798 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
1800 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
1801 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
1802 current_inferior
->last_status
= last_status
;
1804 /* From the client's perspective, all-stop mode always stops all
1805 threads implicitly (and the target backend has already done
1806 so by now). Tag all threads as "want-stopped", so we don't
1807 resume them implicitly without the client telling us to. */
1808 gdb_wants_all_threads_stopped ();
1809 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1810 disable_async_io ();
1812 if (last_status
.kind
== TARGET_WAITKIND_EXITED
1813 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
1814 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
1819 write_enn (own_buf
);
1824 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1826 handle_v_attach (char *own_buf
)
1830 pid
= strtol (own_buf
+ 8, NULL
, 16);
1831 if (pid
!= 0 && attach_inferior (pid
) == 0)
1833 /* Don't report shared library events after attaching, even if
1834 some libraries are preloaded. GDB will always poll the
1835 library list. Avoids the "stopped by shared library event"
1836 notice on the GDB side. */
1841 /* In non-stop, we don't send a resume reply. Stop events
1842 will follow up using the normal notification
1847 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1853 write_enn (own_buf
);
1858 /* Run a new program. Return 1 if successful, 0 if failure. */
1860 handle_v_run (char *own_buf
)
1862 char *p
, *next_p
, **new_argv
;
1866 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
1872 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
1873 if (new_argv
== NULL
)
1875 write_enn (own_buf
);
1880 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
1882 next_p
= strchr (p
, ';');
1884 next_p
= p
+ strlen (p
);
1886 if (i
== 0 && p
== next_p
)
1890 /* FIXME: Fail request if out of memory instead of dying. */
1891 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
1892 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
1893 new_argv
[i
][(next_p
- p
) / 2] = '\0';
1902 if (new_argv
[0] == NULL
)
1904 /* GDB didn't specify a program to run. Use the program from the
1905 last run with the new argument list. */
1907 if (program_argv
== NULL
)
1909 /* FIXME: new_argv memory leak */
1910 write_enn (own_buf
);
1914 new_argv
[0] = strdup (program_argv
[0]);
1915 if (new_argv
[0] == NULL
)
1917 /* FIXME: new_argv memory leak */
1918 write_enn (own_buf
);
1923 /* Free the old argv and install the new one. */
1924 freeargv (program_argv
);
1925 program_argv
= new_argv
;
1927 start_inferior (program_argv
);
1928 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
1930 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1932 /* In non-stop, sending a resume reply doesn't set the general
1933 thread, but GDB assumes a vRun sets it (this is so GDB can
1934 query which is the main thread of the new inferior. */
1936 general_thread
= last_ptid
;
1942 write_enn (own_buf
);
1947 /* Kill process. Return 1 if successful, 0 if failure. */
1949 handle_v_kill (char *own_buf
)
1952 char *p
= &own_buf
[6];
1954 pid
= strtol (p
, NULL
, 16);
1957 if (pid
!= 0 && kill_inferior (pid
) == 0)
1959 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
1960 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
1961 last_ptid
= pid_to_ptid (pid
);
1962 discard_queued_stop_replies (pid
);
1968 write_enn (own_buf
);
1973 /* Handle a 'vStopped' packet. */
1975 handle_v_stopped (char *own_buf
)
1977 /* If we're waiting for GDB to acknowledge a pending stop reply,
1978 consider that done. */
1981 struct vstop_notif
*head
;
1984 fprintf (stderr
, "vStopped: acking %s\n",
1985 target_pid_to_str (notif_queue
->ptid
));
1988 notif_queue
= notif_queue
->next
;
1992 /* Push another stop reply, or if there are no more left, an OK. */
1993 send_next_stop_reply (own_buf
);
1996 /* Handle all of the extended 'v' packets. */
1998 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2000 if (!disable_packet_vCont
)
2002 if (strncmp (own_buf
, "vCont;", 6) == 0)
2004 require_running (own_buf
);
2005 handle_v_cont (own_buf
);
2009 if (strncmp (own_buf
, "vCont?", 6) == 0)
2011 strcpy (own_buf
, "vCont;c;C;s;S;t");
2016 if (strncmp (own_buf
, "vFile:", 6) == 0
2017 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2020 if (strncmp (own_buf
, "vAttach;", 8) == 0)
2022 if (!multi_process
&& target_running ())
2024 fprintf (stderr
, "Already debugging a process\n");
2025 write_enn (own_buf
);
2028 handle_v_attach (own_buf
);
2032 if (strncmp (own_buf
, "vRun;", 5) == 0)
2034 if (!multi_process
&& target_running ())
2036 fprintf (stderr
, "Already debugging a process\n");
2037 write_enn (own_buf
);
2040 handle_v_run (own_buf
);
2044 if (strncmp (own_buf
, "vKill;", 6) == 0)
2046 if (!target_running ())
2048 fprintf (stderr
, "No process to kill\n");
2049 write_enn (own_buf
);
2052 handle_v_kill (own_buf
);
2056 if (strncmp (own_buf
, "vStopped", 8) == 0)
2058 handle_v_stopped (own_buf
);
2062 /* Otherwise we didn't know what packet it was. Say we didn't
2068 /* Resume inferior and wait for another event. In non-stop mode,
2069 don't really wait here, but return immediatelly to the event
2072 myresume (char *own_buf
, int step
, int sig
)
2074 struct thread_resume resume_info
[2];
2076 int valid_cont_thread
;
2078 set_desired_inferior (0);
2080 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2081 && !ptid_equal (cont_thread
, minus_one_ptid
));
2083 if (step
|| sig
|| valid_cont_thread
)
2085 resume_info
[0].thread
2086 = ((struct inferior_list_entry
*) current_inferior
)->id
;
2088 resume_info
[0].kind
= resume_step
;
2090 resume_info
[0].kind
= resume_continue
;
2091 resume_info
[0].sig
= sig
;
2095 if (!valid_cont_thread
)
2097 resume_info
[n
].thread
= minus_one_ptid
;
2098 resume_info
[n
].kind
= resume_continue
;
2099 resume_info
[n
].sig
= 0;
2106 (*the_target
->resume
) (resume_info
, n
);
2112 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2114 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2115 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2117 current_inferior
->last_resume_kind
= resume_stop
;
2118 current_inferior
->last_status
= last_status
;
2121 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2122 disable_async_io ();
2124 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2125 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2126 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2130 /* Callback for for_each_inferior. Make a new stop reply for each
2134 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2136 struct thread_info
*thread
= (struct thread_info
*) entry
;
2138 /* For now, assume targets that don't have this callback also don't
2139 manage the thread's last_status field. */
2140 if (the_target
->thread_stopped
== NULL
)
2142 struct target_waitstatus status
;
2144 status
.kind
= TARGET_WAITKIND_STOPPED
;
2145 status
.value
.sig
= TARGET_SIGNAL_TRAP
;
2147 /* Pass the last stop reply back to GDB, but don't notify
2149 queue_stop_reply (entry
->id
, &thread
->last_status
);
2153 if (thread_stopped (thread
))
2156 fprintf (stderr
, "Reporting thread %s as already stopped with %s\n",
2157 target_pid_to_str (entry
->id
),
2158 target_waitstatus_to_string (&thread
->last_status
));
2160 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2162 /* Pass the last stop reply back to GDB, but don't notify
2164 queue_stop_reply (entry
->id
, &thread
->last_status
);
2171 /* Set this inferior threads's state as "want-stopped". We won't
2172 resume this thread until the client gives us another action for
2176 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2178 struct thread_info
*thread
= (struct thread_info
*) entry
;
2180 thread
->last_resume_kind
= resume_stop
;
2182 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2184 /* Most threads are stopped implicitly (all-stop); tag that with
2186 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2187 thread
->last_status
.value
.sig
= TARGET_SIGNAL_0
;
2191 /* Set all threads' states as "want-stopped". */
2194 gdb_wants_all_threads_stopped (void)
2196 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2199 /* Clear the gdb_detached flag of every process. */
2202 gdb_reattached_process (struct inferior_list_entry
*entry
)
2204 struct process_info
*process
= (struct process_info
*) entry
;
2206 process
->gdb_detached
= 0;
2209 /* Status handler for the '?' packet. */
2212 handle_status (char *own_buf
)
2214 /* GDB is connected, don't forward events to the target anymore. */
2215 for_each_inferior (&all_processes
, gdb_reattached_process
);
2217 /* In non-stop mode, we must send a stop reply for each stopped
2218 thread. In all-stop mode, just send one for the first stopped
2223 discard_queued_stop_replies (-1);
2224 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2226 /* The first is sent immediatly. OK is sent if there is no
2227 stopped thread, which is the same handling of the vStopped
2228 packet (by design). */
2229 send_next_stop_reply (own_buf
);
2234 stabilize_threads ();
2235 gdb_wants_all_threads_stopped ();
2237 if (all_threads
.head
)
2239 struct target_waitstatus status
;
2241 status
.kind
= TARGET_WAITKIND_STOPPED
;
2242 status
.value
.sig
= TARGET_SIGNAL_TRAP
;
2243 prepare_resume_reply (own_buf
,
2244 all_threads
.head
->id
, &status
);
2247 strcpy (own_buf
, "W00");
2252 gdbserver_version (void)
2254 printf ("GNU gdbserver %s%s\n"
2255 "Copyright (C) 2010 Free Software Foundation, Inc.\n"
2256 "gdbserver is free software, covered by the GNU General Public License.\n"
2257 "This gdbserver was configured as \"%s\"\n",
2258 PKGVERSION
, version
, host_name
);
2262 gdbserver_usage (FILE *stream
)
2264 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2265 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2266 "\tgdbserver [OPTIONS] --multi COMM\n"
2268 "COMM may either be a tty device (for serial debugging), or \n"
2269 "HOST:PORT to listen for a TCP connection.\n"
2272 " --debug Enable general debugging output.\n"
2273 " --remote-debug Enable remote protocol debugging output.\n"
2274 " --version Display version information and exit.\n"
2275 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n");
2276 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
2277 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
2281 gdbserver_show_disableable (FILE *stream
)
2283 fprintf (stream
, "Disableable packets:\n"
2284 " vCont \tAll vCont packets\n"
2285 " qC \tQuerying the current thread\n"
2286 " qfThreadInfo\tThread listing\n"
2287 " Tthread \tPassing the thread specifier in the T stop reply packet\n"
2288 " threads \tAll of the above\n");
2292 #undef require_running
2293 #define require_running(BUF) \
2294 if (!target_running ()) \
2301 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
2303 int pid
= * (int *) args
;
2305 if (ptid_get_pid (entry
->id
) == pid
)
2312 kill_inferior_callback (struct inferior_list_entry
*entry
)
2314 struct process_info
*process
= (struct process_info
*) entry
;
2315 int pid
= ptid_get_pid (process
->head
.id
);
2317 kill_inferior (pid
);
2318 discard_queued_stop_replies (pid
);
2321 /* Callback for for_each_inferior to detach or kill the inferior,
2322 depending on whether we attached to it or not.
2323 We inform the user whether we're detaching or killing the process
2324 as this is only called when gdbserver is about to exit. */
2327 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
2329 struct process_info
*process
= (struct process_info
*) entry
;
2330 int pid
= ptid_get_pid (process
->head
.id
);
2332 if (process
->attached
)
2333 detach_inferior (pid
);
2335 kill_inferior (pid
);
2337 discard_queued_stop_replies (pid
);
2340 /* for_each_inferior callback for detach_or_kill_for_exit to print
2341 the pids of started inferiors. */
2344 print_started_pid (struct inferior_list_entry
*entry
)
2346 struct process_info
*process
= (struct process_info
*) entry
;
2348 if (! process
->attached
)
2350 int pid
= ptid_get_pid (process
->head
.id
);
2351 fprintf (stderr
, " %d", pid
);
2355 /* for_each_inferior callback for detach_or_kill_for_exit to print
2356 the pids of attached inferiors. */
2359 print_attached_pid (struct inferior_list_entry
*entry
)
2361 struct process_info
*process
= (struct process_info
*) entry
;
2363 if (process
->attached
)
2365 int pid
= ptid_get_pid (process
->head
.id
);
2366 fprintf (stderr
, " %d", pid
);
2370 /* Call this when exiting gdbserver with possible inferiors that need
2371 to be killed or detached from. */
2374 detach_or_kill_for_exit (void)
2376 /* First print a list of the inferiors we will be killing/detaching.
2377 This is to assist the user, for example, in case the inferior unexpectedly
2378 dies after we exit: did we screw up or did the inferior exit on its own?
2379 Having this info will save some head-scratching. */
2381 if (have_started_inferiors_p ())
2383 fprintf (stderr
, "Killing process(es):");
2384 for_each_inferior (&all_processes
, print_started_pid
);
2385 fprintf (stderr
, "\n");
2387 if (have_attached_inferiors_p ())
2389 fprintf (stderr
, "Detaching process(es):");
2390 for_each_inferior (&all_processes
, print_attached_pid
);
2391 fprintf (stderr
, "\n");
2394 /* Now we can kill or detach the inferiors. */
2396 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
2400 join_inferiors_callback (struct inferior_list_entry
*entry
)
2402 struct process_info
*process
= (struct process_info
*) entry
;
2404 /* If we are attached, then we can exit. Otherwise, we need to hang
2405 around doing nothing, until the child is gone. */
2406 if (!process
->attached
)
2407 join_inferior (ptid_get_pid (process
->head
.id
));
2411 main (int argc
, char *argv
[])
2415 char *arg_end
, *port
;
2416 char **next_arg
= &argv
[1];
2421 while (*next_arg
!= NULL
&& **next_arg
== '-')
2423 if (strcmp (*next_arg
, "--version") == 0)
2425 gdbserver_version ();
2428 else if (strcmp (*next_arg
, "--help") == 0)
2430 gdbserver_usage (stdout
);
2433 else if (strcmp (*next_arg
, "--attach") == 0)
2435 else if (strcmp (*next_arg
, "--multi") == 0)
2437 else if (strcmp (*next_arg
, "--wrapper") == 0)
2441 wrapper_argv
= next_arg
;
2442 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
2445 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
2447 gdbserver_usage (stderr
);
2451 /* Consume the "--". */
2454 else if (strcmp (*next_arg
, "--debug") == 0)
2456 else if (strcmp (*next_arg
, "--remote-debug") == 0)
2458 else if (strcmp (*next_arg
, "--disable-packet") == 0)
2460 gdbserver_show_disableable (stdout
);
2463 else if (strncmp (*next_arg
,
2464 "--disable-packet=",
2465 sizeof ("--disable-packet=") - 1) == 0)
2467 char *packets
, *tok
;
2469 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
2470 for (tok
= strtok (packets
, ",");
2472 tok
= strtok (NULL
, ","))
2474 if (strcmp ("vCont", tok
) == 0)
2475 disable_packet_vCont
= 1;
2476 else if (strcmp ("Tthread", tok
) == 0)
2477 disable_packet_Tthread
= 1;
2478 else if (strcmp ("qC", tok
) == 0)
2479 disable_packet_qC
= 1;
2480 else if (strcmp ("qfThreadInfo", tok
) == 0)
2481 disable_packet_qfThreadInfo
= 1;
2482 else if (strcmp ("threads", tok
) == 0)
2484 disable_packet_vCont
= 1;
2485 disable_packet_Tthread
= 1;
2486 disable_packet_qC
= 1;
2487 disable_packet_qfThreadInfo
= 1;
2491 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
2493 gdbserver_show_disableable (stderr
);
2500 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
2508 if (setjmp (toplevel
))
2510 fprintf (stderr
, "Exiting\n");
2516 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
2518 gdbserver_usage (stderr
);
2525 /* --attach used to come after PORT, so allow it there for
2527 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
2534 && (*next_arg
== NULL
2535 || (*next_arg
)[0] == '\0'
2536 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
2538 || next_arg
[1] != NULL
))
2543 gdbserver_usage (stderr
);
2547 initialize_inferiors ();
2548 initialize_async_io ();
2550 if (target_supports_tracepoints ())
2551 initialize_tracepoint ();
2553 own_buf
= xmalloc (PBUFSIZ
+ 1);
2554 mem_buf
= xmalloc (PBUFSIZ
);
2556 if (pid
== 0 && *next_arg
!= NULL
)
2560 n
= argc
- (next_arg
- argv
);
2561 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
2562 for (i
= 0; i
< n
; i
++)
2563 program_argv
[i
] = xstrdup (next_arg
[i
]);
2564 program_argv
[i
] = NULL
;
2566 /* Wait till we are at first instruction in program. */
2567 start_inferior (program_argv
);
2569 /* We are now (hopefully) stopped at the first instruction of
2570 the target process. This assumes that the target process was
2571 successfully created. */
2575 if (attach_inferior (pid
) == -1)
2576 error ("Attaching not supported on this target");
2578 /* Otherwise succeeded. */
2582 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2583 last_status
.value
.integer
= 0;
2584 last_ptid
= minus_one_ptid
;
2587 /* Don't report shared library events on the initial connection,
2588 even if some libraries are preloaded. Avoids the "stopped by
2589 shared library event" notice on gdb side. */
2592 if (setjmp (toplevel
))
2594 detach_or_kill_for_exit ();
2598 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2599 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2604 if (!was_running
&& !multi_mode
)
2606 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
2614 /* Be sure we're out of tfind mode. */
2615 current_traceframe
= -1;
2619 if (setjmp (toplevel
) != 0)
2621 /* An error occurred. */
2622 if (response_needed
)
2624 write_enn (own_buf
);
2629 /* Wait for events. This will return when all event sources are
2630 removed from the event loop. */
2631 start_event_loop ();
2633 /* If an exit was requested (using the "monitor exit" command),
2634 terminate now. The only other way to get here is for
2635 getpkt to fail; close the connection and reopen it at the
2640 detach_or_kill_for_exit ();
2645 "Remote side has terminated connection. "
2646 "GDBserver will reopen the connection.\n");
2650 if (disconnected_tracing
)
2652 /* Try to enable non-stop/async mode, so we we can both
2653 wait for an async socket accept, and handle async
2654 target events simultaneously. There's also no point
2655 either in having the target always stop all threads,
2656 when we're going to pass signals down without
2660 if (start_non_stop (1))
2663 /* Detaching implicitly resumes all threads; simply
2664 disconnecting does not. */
2670 "Disconnected tracing disabled; stopping trace run.\n");
2677 /* Event loop callback that handles a serial event. The first byte in
2678 the serial buffer gets us here. We expect characters to arrive at
2679 a brisk pace, so we read the rest of the packet with a blocking
2683 process_serial_event (void)
2693 int new_packet_len
= -1;
2695 /* Used to decide when gdbserver should exit in
2696 multi-mode/remote. */
2697 static int have_ran
= 0;
2700 have_ran
= target_running ();
2702 disable_async_io ();
2704 response_needed
= 0;
2705 packet_len
= getpkt (own_buf
);
2706 if (packet_len
<= 0)
2709 /* Force an event loop break. */
2712 response_needed
= 1;
2719 handle_query (own_buf
, packet_len
, &new_packet_len
);
2722 handle_general_set (own_buf
);
2725 require_running (own_buf
);
2730 pid
= strtol (&own_buf
[i
], NULL
, 16);
2734 ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
2736 if (tracing
&& disconnected_tracing
)
2738 struct thread_resume resume_info
;
2739 struct process_info
*process
= find_process_pid (pid
);
2741 if (process
== NULL
)
2743 write_enn (own_buf
);
2748 "Disconnected tracing in effect, "
2749 "leaving gdbserver attached to the process\n");
2751 /* Make sure we're in non-stop/async mode, so we we can both
2752 wait for an async socket accept, and handle async target
2753 events simultaneously. There's also no point either in
2754 having the target stop all threads, when we're going to
2755 pass signals down without informing GDB. */
2759 fprintf (stderr
, "Forcing non-stop mode\n");
2765 process
->gdb_detached
= 1;
2767 /* Detaching implicitly resumes all threads. */
2768 resume_info
.thread
= minus_one_ptid
;
2769 resume_info
.kind
= resume_continue
;
2770 resume_info
.sig
= 0;
2771 (*the_target
->resume
) (&resume_info
, 1);
2774 break; /* from switch/case */
2777 fprintf (stderr
, "Detaching from process %d\n", pid
);
2779 if (detach_inferior (pid
) != 0)
2780 write_enn (own_buf
);
2783 discard_queued_stop_replies (pid
);
2786 if (extended_protocol
)
2788 /* Treat this like a normal program exit. */
2789 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2790 last_status
.value
.integer
= 0;
2791 last_ptid
= pid_to_ptid (pid
);
2793 current_inferior
= NULL
;
2800 /* If we are attached, then we can exit. Otherwise, we
2801 need to hang around doing nothing, until the child is
2803 for_each_inferior (&all_processes
,
2804 join_inferiors_callback
);
2810 extended_protocol
= 1;
2814 handle_status (own_buf
);
2817 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
2819 ptid_t gdb_id
, thread_id
;
2822 require_running (own_buf
);
2824 gdb_id
= read_ptid (&own_buf
[2], NULL
);
2826 pid
= ptid_get_pid (gdb_id
);
2828 if (ptid_equal (gdb_id
, null_ptid
)
2829 || ptid_equal (gdb_id
, minus_one_ptid
))
2830 thread_id
= null_ptid
;
2832 && ptid_equal (pid_to_ptid (pid
),
2835 struct thread_info
*thread
=
2836 (struct thread_info
*) find_inferior (&all_threads
,
2841 write_enn (own_buf
);
2845 thread_id
= ((struct inferior_list_entry
*)thread
)->id
;
2849 thread_id
= gdb_id_to_thread_id (gdb_id
);
2850 if (ptid_equal (thread_id
, null_ptid
))
2852 write_enn (own_buf
);
2857 if (own_buf
[1] == 'g')
2859 if (ptid_equal (thread_id
, null_ptid
))
2861 /* GDB is telling us to choose any thread. Check if
2862 the currently selected thread is still valid. If
2863 it is not, select the first available. */
2864 struct thread_info
*thread
=
2865 (struct thread_info
*) find_inferior_id (&all_threads
,
2868 thread_id
= all_threads
.head
->id
;
2871 general_thread
= thread_id
;
2872 set_desired_inferior (1);
2874 else if (own_buf
[1] == 'c')
2875 cont_thread
= thread_id
;
2876 else if (own_buf
[1] == 's')
2877 step_thread
= thread_id
;
2883 /* Silently ignore it so that gdb can extend the protocol
2884 without compatibility headaches. */
2889 require_running (own_buf
);
2890 if (current_traceframe
>= 0)
2892 struct regcache
*regcache
= new_register_cache ();
2894 if (fetch_traceframe_registers (current_traceframe
,
2896 registers_to_string (regcache
, own_buf
);
2898 write_enn (own_buf
);
2899 free_register_cache (regcache
);
2903 struct regcache
*regcache
;
2905 set_desired_inferior (1);
2906 regcache
= get_thread_regcache (current_inferior
, 1);
2907 registers_to_string (regcache
, own_buf
);
2911 require_running (own_buf
);
2912 if (current_traceframe
>= 0)
2913 write_enn (own_buf
);
2916 struct regcache
*regcache
;
2918 set_desired_inferior (1);
2919 regcache
= get_thread_regcache (current_inferior
, 1);
2920 registers_from_string (regcache
, &own_buf
[1]);
2925 require_running (own_buf
);
2926 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
2927 if (read_memory (mem_addr
, mem_buf
, len
) == 0)
2928 convert_int_to_ascii (mem_buf
, own_buf
, len
);
2930 write_enn (own_buf
);
2933 require_running (own_buf
);
2934 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
2935 if (write_memory (mem_addr
, mem_buf
, len
) == 0)
2938 write_enn (own_buf
);
2941 require_running (own_buf
);
2942 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
2943 &mem_addr
, &len
, &mem_buf
) < 0
2944 || write_memory (mem_addr
, mem_buf
, len
) != 0)
2945 write_enn (own_buf
);
2950 require_running (own_buf
);
2951 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
2952 if (target_signal_to_host_p (sig
))
2953 signal
= target_signal_to_host (sig
);
2956 myresume (own_buf
, 0, signal
);
2959 require_running (own_buf
);
2960 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
2961 if (target_signal_to_host_p (sig
))
2962 signal
= target_signal_to_host (sig
);
2965 myresume (own_buf
, 1, signal
);
2968 require_running (own_buf
);
2970 myresume (own_buf
, 0, signal
);
2973 require_running (own_buf
);
2975 myresume (own_buf
, 1, signal
);
2977 case 'Z': /* insert_ ... */
2979 case 'z': /* remove_ ... */
2983 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
2984 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
2985 char type
= own_buf
[1];
2987 const int insert
= ch
== 'Z';
2989 /* Default to unrecognized/unsupported. */
2993 case '0': /* software-breakpoint */
2994 case '1': /* hardware-breakpoint */
2995 case '2': /* write watchpoint */
2996 case '3': /* read watchpoint */
2997 case '4': /* access watchpoint */
2998 require_running (own_buf
);
2999 if (insert
&& the_target
->insert_point
!= NULL
)
3000 res
= (*the_target
->insert_point
) (type
, addr
, len
);
3001 else if (!insert
&& the_target
->remove_point
!= NULL
)
3002 res
= (*the_target
->remove_point
) (type
, addr
, len
);
3014 write_enn (own_buf
);
3018 response_needed
= 0;
3019 if (!target_running ())
3020 /* The packet we received doesn't make sense - but we can't
3021 reply to it, either. */
3024 fprintf (stderr
, "Killing all inferiors\n");
3025 for_each_inferior (&all_processes
, kill_inferior_callback
);
3027 /* When using the extended protocol, we wait with no program
3028 running. The traditional protocol will exit instead. */
3029 if (extended_protocol
)
3031 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3032 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3040 ptid_t gdb_id
, thread_id
;
3042 require_running (own_buf
);
3044 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3045 thread_id
= gdb_id_to_thread_id (gdb_id
);
3046 if (ptid_equal (thread_id
, null_ptid
))
3048 write_enn (own_buf
);
3052 if (mythread_alive (thread_id
))
3055 write_enn (own_buf
);
3059 response_needed
= 0;
3061 /* Restarting the inferior is only supported in the extended
3063 if (extended_protocol
)
3065 if (target_running ())
3066 for_each_inferior (&all_processes
,
3067 kill_inferior_callback
);
3068 fprintf (stderr
, "GDBserver restarting\n");
3070 /* Wait till we are at 1st instruction in prog. */
3071 if (program_argv
!= NULL
)
3072 start_inferior (program_argv
);
3075 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3076 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3082 /* It is a request we don't understand. Respond with an
3083 empty packet so that gdb knows that we don't support this
3089 /* Extended (long) request. */
3090 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
3094 /* It is a request we don't understand. Respond with an empty
3095 packet so that gdb knows that we don't support this
3101 if (new_packet_len
!= -1)
3102 putpkt_binary (own_buf
, new_packet_len
);
3106 response_needed
= 0;
3108 if (!extended_protocol
&& have_ran
&& !target_running ())
3110 /* In non-stop, defer exiting until GDB had a chance to query
3111 the whole vStopped list (until it gets an OK). */
3114 fprintf (stderr
, "GDBserver exiting\n");
3126 /* Event-loop callback for serial events. */
3129 handle_serial_event (int err
, gdb_client_data client_data
)
3132 fprintf (stderr
, "handling possible serial event\n");
3134 /* Really handle it. */
3135 if (process_serial_event () < 0)
3138 /* Be sure to not change the selected inferior behind GDB's back.
3139 Important in the non-stop mode asynchronous protocol. */
3140 set_desired_inferior (1);
3145 /* Event-loop callback for target events. */
3148 handle_target_event (int err
, gdb_client_data client_data
)
3151 fprintf (stderr
, "handling possible target event\n");
3153 last_ptid
= mywait (minus_one_ptid
, &last_status
,
3156 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3158 int pid
= ptid_get_pid (last_ptid
);
3159 struct process_info
*process
= find_process_pid (pid
);
3160 int forward_event
= !gdb_connected () || process
->gdb_detached
;
3162 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3163 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3165 mark_breakpoints_out (process
);
3166 mourn_inferior (process
);
3170 /* We're reporting this thread as stopped. Update its
3171 "want-stopped" state to what the client wants, until it
3172 gets a new resume action. */
3173 current_inferior
->last_resume_kind
= resume_stop
;
3174 current_inferior
->last_status
= last_status
;
3179 if (!target_running ())
3181 /* The last process exited. We're done. */
3185 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
3187 /* A thread stopped with a signal, but gdb isn't
3188 connected to handle it. Pass it down to the
3189 inferior, as if it wasn't being traced. */
3190 struct thread_resume resume_info
;
3194 "GDB not connected; forwarding event %d for [%s]\n",
3195 (int) last_status
.kind
,
3196 target_pid_to_str (last_ptid
));
3198 resume_info
.thread
= last_ptid
;
3199 resume_info
.kind
= resume_continue
;
3200 resume_info
.sig
= target_signal_to_host (last_status
.value
.sig
);
3201 (*the_target
->resume
) (&resume_info
, 1);
3203 else if (debug_threads
)
3204 fprintf (stderr
, "GDB not connected; ignoring event %d for [%s]\n",
3205 (int) last_status
.kind
,
3206 target_pid_to_str (last_ptid
));
3210 /* Something interesting. Tell GDB about it. */
3211 push_event (last_ptid
, &last_status
);
3215 /* Be sure to not change the selected inferior behind GDB's back.
3216 Important in the non-stop mode asynchronous protocol. */
3217 set_desired_inferior (1);