1 /* Main code for remote server for GDB.
2 Copyright (C) 1989-2015 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "gdbthread.h"
33 #include "btrace-common.h"
34 #include "filestuff.h"
35 #include "tracepoint.h"
39 /* The thread set with an `Hc' packet. `Hc' is deprecated in favor of
40 `vCont'. Note the multi-process extensions made `vCont' a
41 requirement, so `Hc pPID.TID' is pretty much undefined. So
42 CONT_THREAD can be null_ptid for no `Hc' thread, minus_one_ptid for
43 resuming all threads of the process (again, `Hc' isn't used for
44 multi-process), or a specific thread ptid_t. */
47 /* The thread set with an `Hg' packet. */
48 ptid_t general_thread
;
52 static int extended_protocol
;
53 static int response_needed
;
54 static int exit_requested
;
56 /* --once: Exit after the first connection has closed. */
64 /* Whether we should attempt to disable the operating system's address
65 space randomization feature before starting an inferior. */
66 int disable_randomization
= 1;
68 static char **program_argv
, **wrapper_argv
;
70 int pass_signals
[GDB_SIGNAL_LAST
];
71 int program_signals
[GDB_SIGNAL_LAST
];
72 int program_signals_p
;
74 /* The PID of the originally created or attached inferior. Used to
75 send signals to the process when GDB sends us an asynchronous interrupt
76 (user hitting Control-C in the client), and to wait for the child to exit
77 when no longer debugging it. */
79 unsigned long signal_pid
;
82 /* A file descriptor for the controlling terminal. */
85 /* TERMINAL_FD's original foreground group. */
86 pid_t old_foreground_pgrp
;
88 /* Hand back terminal ownership to the original foreground group. */
91 restore_old_foreground_pgrp (void)
93 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
97 /* Set if you want to disable optional thread related packets support
98 in gdbserver, for the sake of testing GDB against stubs that don't
100 int disable_packet_vCont
;
101 int disable_packet_Tthread
;
102 int disable_packet_qC
;
103 int disable_packet_qfThreadInfo
;
105 /* Last status reported to GDB. */
106 static struct target_waitstatus last_status
;
107 static ptid_t last_ptid
;
109 static char *own_buf
;
110 static unsigned char *mem_buf
;
112 /* A sub-class of 'struct notif_event' for stop, holding information
113 relative to a single stop reply. We keep a queue of these to
114 push to GDB in non-stop mode. */
118 struct notif_event base
;
120 /* Thread or process that got the event. */
124 struct target_waitstatus status
;
127 /* The current btrace configuration. This is gdbserver's mirror of GDB's
128 btrace configuration. */
129 static struct btrace_config current_btrace_conf
;
131 DEFINE_QUEUE_P (notif_event_p
);
133 /* Put a stop reply to the stop reply queue. */
136 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
138 struct vstop_notif
*new_notif
= xmalloc (sizeof (*new_notif
));
140 new_notif
->ptid
= ptid
;
141 new_notif
->status
= *status
;
143 notif_event_enque (¬if_stop
, (struct notif_event
*) new_notif
);
147 remove_all_on_match_pid (QUEUE (notif_event_p
) *q
,
148 QUEUE_ITER (notif_event_p
) *iter
,
149 struct notif_event
*event
,
155 || ptid_get_pid (((struct vstop_notif
*) event
)->ptid
) == *pid
)
157 if (q
->free_func
!= NULL
)
158 q
->free_func (event
);
160 QUEUE_remove_elem (notif_event_p
, q
, iter
);
166 /* Get rid of the currently pending stop replies for PID. If PID is
167 -1, then apply to all processes. */
170 discard_queued_stop_replies (int pid
)
172 QUEUE_iterate (notif_event_p
, notif_stop
.queue
,
173 remove_all_on_match_pid
, &pid
);
177 vstop_notif_reply (struct notif_event
*event
, char *own_buf
)
179 struct vstop_notif
*vstop
= (struct vstop_notif
*) event
;
181 prepare_resume_reply (own_buf
, vstop
->ptid
, &vstop
->status
);
184 struct notif_server notif_stop
=
186 "vStopped", "Stop", NULL
, vstop_notif_reply
,
190 target_running (void)
192 return get_first_thread () != NULL
;
196 start_inferior (char **argv
)
198 char **new_argv
= argv
;
200 if (wrapper_argv
!= NULL
)
204 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
206 for (i
= 0; argv
[i
] != NULL
; i
++)
208 new_argv
= alloca (sizeof (char *) * count
);
210 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
211 new_argv
[count
++] = wrapper_argv
[i
];
212 for (i
= 0; argv
[i
] != NULL
; i
++)
213 new_argv
[count
++] = argv
[i
];
214 new_argv
[count
] = NULL
;
220 for (i
= 0; new_argv
[i
]; ++i
)
221 debug_printf ("new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
226 signal (SIGTTOU
, SIG_DFL
);
227 signal (SIGTTIN
, SIG_DFL
);
230 signal_pid
= create_inferior (new_argv
[0], new_argv
);
232 /* FIXME: we don't actually know at this point that the create
233 actually succeeded. We won't know that until we wait. */
234 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
239 signal (SIGTTOU
, SIG_IGN
);
240 signal (SIGTTIN
, SIG_IGN
);
241 terminal_fd
= fileno (stderr
);
242 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
243 tcsetpgrp (terminal_fd
, signal_pid
);
244 atexit (restore_old_foreground_pgrp
);
247 if (wrapper_argv
!= NULL
)
249 struct thread_resume resume_info
;
251 memset (&resume_info
, 0, sizeof (resume_info
));
252 resume_info
.thread
= pid_to_ptid (signal_pid
);
253 resume_info
.kind
= resume_continue
;
256 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
258 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
263 (*the_target
->resume
) (&resume_info
, 1);
265 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
266 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
269 current_thread
->last_resume_kind
= resume_stop
;
270 current_thread
->last_status
= last_status
;
272 while (last_status
.value
.sig
!= GDB_SIGNAL_TRAP
);
277 /* Wait till we are at 1st instruction in program, return new pid
278 (assuming success). */
279 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
281 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
282 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
284 current_thread
->last_resume_kind
= resume_stop
;
285 current_thread
->last_status
= last_status
;
292 attach_inferior (int pid
)
294 /* myattach should return -1 if attaching is unsupported,
295 0 if it succeeded, and call error() otherwise. */
297 if (myattach (pid
) != 0)
300 fprintf (stderr
, "Attached; pid = %d\n", pid
);
303 /* FIXME - It may be that we should get the SIGNAL_PID from the
304 attach function, so that it can be the main thread instead of
305 whichever we were told to attach to. */
310 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
312 /* GDB knows to ignore the first SIGSTOP after attaching to a running
313 process using the "attach" command, but this is different; it's
314 just using "target remote". Pretend it's just starting up. */
315 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
316 && last_status
.value
.sig
== GDB_SIGNAL_STOP
)
317 last_status
.value
.sig
= GDB_SIGNAL_TRAP
;
319 current_thread
->last_resume_kind
= resume_stop
;
320 current_thread
->last_status
= last_status
;
326 extern int remote_debug
;
328 /* Decode a qXfer read request. Return 0 if everything looks OK,
332 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
334 /* After the read marker and annex, qXfer looks like a
335 traditional 'm' packet. */
336 decode_m_packet (buf
, ofs
, len
);
342 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
344 /* Extract and NUL-terminate the object. */
346 while (*buf
&& *buf
!= ':')
352 /* Extract and NUL-terminate the read/write action. */
354 while (*buf
&& *buf
!= ':')
360 /* Extract and NUL-terminate the annex. */
362 while (*buf
&& *buf
!= ':')
372 /* Write the response to a successful qXfer read. Returns the
373 length of the (binary) data stored in BUF, corresponding
374 to as much of DATA/LEN as we could fit. IS_MORE controls
375 the first character of the response. */
377 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
386 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
390 /* Handle btrace enabling in BTS format. */
393 handle_btrace_enable_bts (struct thread_info
*thread
)
395 if (thread
->btrace
!= NULL
)
396 return "E.Btrace already enabled.";
398 current_btrace_conf
.format
= BTRACE_FORMAT_BTS
;
399 thread
->btrace
= target_enable_btrace (thread
->entry
.id
,
400 ¤t_btrace_conf
);
401 if (thread
->btrace
== NULL
)
402 return "E.Could not enable btrace.";
407 /* Handle btrace disabling. */
410 handle_btrace_disable (struct thread_info
*thread
)
413 if (thread
->btrace
== NULL
)
414 return "E.Branch tracing not enabled.";
416 if (target_disable_btrace (thread
->btrace
) != 0)
417 return "E.Could not disable branch tracing.";
419 thread
->btrace
= NULL
;
423 /* Handle the "Qbtrace" packet. */
426 handle_btrace_general_set (char *own_buf
)
428 struct thread_info
*thread
;
432 if (!startswith (own_buf
, "Qbtrace:"))
435 op
= own_buf
+ strlen ("Qbtrace:");
437 if (ptid_equal (general_thread
, null_ptid
)
438 || ptid_equal (general_thread
, minus_one_ptid
))
440 strcpy (own_buf
, "E.Must select a single thread.");
444 thread
= find_thread_ptid (general_thread
);
447 strcpy (own_buf
, "E.No such thread.");
453 if (strcmp (op
, "bts") == 0)
454 err
= handle_btrace_enable_bts (thread
);
455 else if (strcmp (op
, "off") == 0)
456 err
= handle_btrace_disable (thread
);
458 err
= "E.Bad Qbtrace operation. Use bts or off.";
461 strcpy (own_buf
, err
);
468 /* Handle the "Qbtrace-conf" packet. */
471 handle_btrace_conf_general_set (char *own_buf
)
473 struct thread_info
*thread
;
476 if (!startswith (own_buf
, "Qbtrace-conf:"))
479 op
= own_buf
+ strlen ("Qbtrace-conf:");
481 if (ptid_equal (general_thread
, null_ptid
)
482 || ptid_equal (general_thread
, minus_one_ptid
))
484 strcpy (own_buf
, "E.Must select a single thread.");
488 thread
= find_thread_ptid (general_thread
);
491 strcpy (own_buf
, "E.No such thread.");
495 if (startswith (op
, "bts:size="))
501 size
= strtoul (op
+ strlen ("bts:size="), &endp
, 16);
502 if (endp
== NULL
|| *endp
!= 0 || errno
!= 0 || size
> UINT_MAX
)
504 strcpy (own_buf
, "E.Bad size value.");
508 current_btrace_conf
.bts
.size
= (unsigned int) size
;
512 strcpy (own_buf
, "E.Bad Qbtrace configuration option.");
520 /* Handle all of the extended 'Q' packets. */
523 handle_general_set (char *own_buf
)
525 if (startswith (own_buf
, "QPassSignals:"))
527 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
528 const char *p
= own_buf
+ strlen ("QPassSignals:");
531 p
= decode_address_to_semicolon (&cursig
, p
);
532 for (i
= 0; i
< numsigs
; i
++)
538 /* Keep looping, to clear the remaining signals. */
541 p
= decode_address_to_semicolon (&cursig
, p
);
546 strcpy (own_buf
, "OK");
550 if (startswith (own_buf
, "QProgramSignals:"))
552 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
553 const char *p
= own_buf
+ strlen ("QProgramSignals:");
556 program_signals_p
= 1;
558 p
= decode_address_to_semicolon (&cursig
, p
);
559 for (i
= 0; i
< numsigs
; i
++)
563 program_signals
[i
] = 1;
565 /* Keep looping, to clear the remaining signals. */
568 p
= decode_address_to_semicolon (&cursig
, p
);
571 program_signals
[i
] = 0;
573 strcpy (own_buf
, "OK");
577 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
581 fprintf (stderr
, "[noack mode enabled]\n");
590 if (startswith (own_buf
, "QNonStop:"))
592 char *mode
= own_buf
+ 9;
596 if (strcmp (mode
, "0") == 0)
598 else if (strcmp (mode
, "1") == 0)
602 /* We don't know what this mode is, so complain to
604 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
610 req_str
= req
? "non-stop" : "all-stop";
611 if (start_non_stop (req
) != 0)
613 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
621 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
627 if (startswith (own_buf
, "QDisableRandomization:"))
629 char *packet
= own_buf
+ strlen ("QDisableRandomization:");
632 unpack_varlen_hex (packet
, &setting
);
633 disable_randomization
= setting
;
637 if (disable_randomization
)
638 fprintf (stderr
, "[address space randomization disabled]\n");
640 fprintf (stderr
, "[address space randomization enabled]\n");
647 if (target_supports_tracepoints ()
648 && handle_tracepoint_general_set (own_buf
))
651 if (startswith (own_buf
, "QAgent:"))
653 char *mode
= own_buf
+ strlen ("QAgent:");
656 if (strcmp (mode
, "0") == 0)
658 else if (strcmp (mode
, "1") == 0)
662 /* We don't know what this value is, so complain to GDB. */
663 sprintf (own_buf
, "E.Unknown QAgent value");
667 /* Update the flag. */
670 fprintf (stderr
, "[%s agent]\n", req
? "Enable" : "Disable");
675 if (handle_btrace_general_set (own_buf
))
678 if (handle_btrace_conf_general_set (own_buf
))
681 /* Otherwise we didn't know what packet it was. Say we didn't
687 get_features_xml (const char *annex
)
689 const struct target_desc
*desc
= current_target_desc ();
691 /* `desc->xmltarget' defines what to return when looking for the
692 "target.xml" file. Its contents can either be verbatim XML code
693 (prefixed with a '@') or else the name of the actual XML file to
694 be used in place of "target.xml".
696 This variable is set up from the auto-generated
697 init_registers_... routine for the current target. */
699 if (desc
->xmltarget
!= NULL
&& strcmp (annex
, "target.xml") == 0)
701 if (*desc
->xmltarget
== '@')
702 return desc
->xmltarget
+ 1;
704 annex
= desc
->xmltarget
;
709 extern const char *const xml_builtin
[][2];
712 /* Look for the annex. */
713 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
714 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
717 if (xml_builtin
[i
][0] != NULL
)
718 return xml_builtin
[i
][1];
726 monitor_show_help (void)
728 monitor_output ("The following monitor commands are supported:\n");
729 monitor_output (" set debug <0|1>\n");
730 monitor_output (" Enable general debugging messages\n");
731 monitor_output (" set debug-hw-points <0|1>\n");
732 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
733 monitor_output (" set remote-debug <0|1>\n");
734 monitor_output (" Enable remote protocol debugging messages\n");
735 monitor_output (" set debug-format option1[,option2,...]\n");
736 monitor_output (" Add additional information to debugging messages\n");
737 monitor_output (" Options: all, none");
738 monitor_output (", timestamp");
739 monitor_output ("\n");
740 monitor_output (" exit\n");
741 monitor_output (" Quit GDBserver\n");
744 /* Read trace frame or inferior memory. Returns the number of bytes
745 actually read, zero when no further transfer is possible, and -1 on
746 error. Return of a positive value smaller than LEN does not
747 indicate there's no more to be read, only the end of the transfer.
748 E.g., when GDB reads memory from a traceframe, a first request may
749 be served from a memory block that does not cover the whole request
750 length. A following request gets the rest served from either
751 another block (of the same traceframe) or from the read-only
755 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
759 if (current_traceframe
>= 0)
762 ULONGEST length
= len
;
764 if (traceframe_read_mem (current_traceframe
,
765 memaddr
, myaddr
, len
, &nbytes
))
767 /* Data read from trace buffer, we're done. */
770 if (!in_readonly_region (memaddr
, length
))
772 /* Otherwise we have a valid readonly case, fall through. */
773 /* (assume no half-trace half-real blocks for now) */
776 res
= prepare_to_access_memory ();
779 res
= read_inferior_memory (memaddr
, myaddr
, len
);
780 done_accessing_memory ();
782 return res
== 0 ? len
: -1;
788 /* Write trace frame or inferior memory. Actually, writing to trace
789 frames is forbidden. */
792 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
794 if (current_traceframe
>= 0)
800 ret
= prepare_to_access_memory ();
803 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
804 done_accessing_memory ();
810 /* Subroutine of handle_search_memory to simplify it. */
813 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
814 gdb_byte
*pattern
, unsigned pattern_len
,
815 gdb_byte
*search_buf
,
816 unsigned chunk_size
, unsigned search_buf_size
,
817 CORE_ADDR
*found_addrp
)
819 /* Prime the search buffer. */
821 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
824 warning ("Unable to access %ld bytes of target "
825 "memory at 0x%lx, halting search.",
826 (long) search_buf_size
, (long) start_addr
);
830 /* Perform the search.
832 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
833 When we've scanned N bytes we copy the trailing bytes to the start and
834 read in another N bytes. */
836 while (search_space_len
>= pattern_len
)
839 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
843 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
845 if (found_ptr
!= NULL
)
847 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
848 *found_addrp
= found_addr
;
852 /* Not found in this chunk, skip to next chunk. */
854 /* Don't let search_space_len wrap here, it's unsigned. */
855 if (search_space_len
>= chunk_size
)
856 search_space_len
-= chunk_size
;
858 search_space_len
= 0;
860 if (search_space_len
>= pattern_len
)
862 unsigned keep_len
= search_buf_size
- chunk_size
;
863 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
866 /* Copy the trailing part of the previous iteration to the front
867 of the buffer for the next iteration. */
868 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
870 nr_to_read
= (search_space_len
- keep_len
< chunk_size
871 ? search_space_len
- keep_len
874 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
875 nr_to_read
) != search_buf_size
)
877 warning ("Unable to access %ld bytes of target memory "
878 "at 0x%lx, halting search.",
879 (long) nr_to_read
, (long) read_addr
);
883 start_addr
+= chunk_size
;
892 /* Handle qSearch:memory packets. */
895 handle_search_memory (char *own_buf
, int packet_len
)
897 CORE_ADDR start_addr
;
898 CORE_ADDR search_space_len
;
900 unsigned int pattern_len
;
901 /* NOTE: also defined in find.c testcase. */
902 #define SEARCH_CHUNK_SIZE 16000
903 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
904 /* Buffer to hold memory contents for searching. */
905 gdb_byte
*search_buf
;
906 unsigned search_buf_size
;
908 CORE_ADDR found_addr
;
909 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
911 pattern
= malloc (packet_len
);
914 error ("Unable to allocate memory to perform the search");
915 strcpy (own_buf
, "E00");
918 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
919 packet_len
- cmd_name_len
,
920 &start_addr
, &search_space_len
,
921 pattern
, &pattern_len
) < 0)
924 error ("Error in parsing qSearch:memory packet");
925 strcpy (own_buf
, "E00");
929 search_buf_size
= chunk_size
+ pattern_len
- 1;
931 /* No point in trying to allocate a buffer larger than the search space. */
932 if (search_space_len
< search_buf_size
)
933 search_buf_size
= search_space_len
;
935 search_buf
= malloc (search_buf_size
);
936 if (search_buf
== NULL
)
939 error ("Unable to allocate memory to perform the search");
940 strcpy (own_buf
, "E00");
944 found
= handle_search_memory_1 (start_addr
, search_space_len
,
945 pattern
, pattern_len
,
946 search_buf
, chunk_size
, search_buf_size
,
950 sprintf (own_buf
, "1,%lx", (long) found_addr
);
952 strcpy (own_buf
, "0");
954 strcpy (own_buf
, "E00");
960 #define require_running(BUF) \
961 if (!target_running ()) \
967 /* Parse options to --debug-format= and "monitor set debug-format".
968 ARG is the text after "--debug-format=" or "monitor set debug-format".
969 IS_MONITOR is non-zero if we're invoked via "monitor set debug-format".
970 This triggers calls to monitor_output.
971 The result is NULL if all options were parsed ok, otherwise an error
972 message which the caller must free.
974 N.B. These commands affect all debug format settings, they are not
975 cumulative. If a format is not specified, it is turned off.
976 However, we don't go to extra trouble with things like
977 "monitor set debug-format all,none,timestamp".
978 Instead we just parse them one at a time, in order.
980 The syntax for "monitor set debug" we support here is not identical
981 to gdb's "set debug foo on|off" because we also use this function to
982 parse "--debug-format=foo,bar". */
985 parse_debug_format_options (const char *arg
, int is_monitor
)
987 VEC (char_ptr
) *options
;
991 /* First turn all debug format options off. */
994 /* First remove leading spaces, for "monitor set debug-format". */
995 while (isspace (*arg
))
998 options
= delim_string_to_char_ptr_vec (arg
, ',');
1000 for (ix
= 0; VEC_iterate (char_ptr
, options
, ix
, option
); ++ix
)
1002 if (strcmp (option
, "all") == 0)
1004 debug_timestamp
= 1;
1006 monitor_output ("All extra debug format options enabled.\n");
1008 else if (strcmp (option
, "none") == 0)
1010 debug_timestamp
= 0;
1012 monitor_output ("All extra debug format options disabled.\n");
1014 else if (strcmp (option
, "timestamp") == 0)
1016 debug_timestamp
= 1;
1018 monitor_output ("Timestamps will be added to debug output.\n");
1020 else if (*option
== '\0')
1022 /* An empty option, e.g., "--debug-format=foo,,bar", is ignored. */
1027 char *msg
= xstrprintf ("Unknown debug-format argument: \"%s\"\n",
1030 free_char_ptr_vec (options
);
1035 free_char_ptr_vec (options
);
1039 /* Handle monitor commands not handled by target-specific handlers. */
1042 handle_monitor_command (char *mon
, char *own_buf
)
1044 if (strcmp (mon
, "set debug 1") == 0)
1047 monitor_output ("Debug output enabled.\n");
1049 else if (strcmp (mon
, "set debug 0") == 0)
1052 monitor_output ("Debug output disabled.\n");
1054 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
1056 show_debug_regs
= 1;
1057 monitor_output ("H/W point debugging output enabled.\n");
1059 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
1061 show_debug_regs
= 0;
1062 monitor_output ("H/W point debugging output disabled.\n");
1064 else if (strcmp (mon
, "set remote-debug 1") == 0)
1067 monitor_output ("Protocol debug output enabled.\n");
1069 else if (strcmp (mon
, "set remote-debug 0") == 0)
1072 monitor_output ("Protocol debug output disabled.\n");
1074 else if (startswith (mon
, "set debug-format "))
1077 = parse_debug_format_options (mon
+ sizeof ("set debug-format ") - 1,
1080 if (error_msg
!= NULL
)
1082 monitor_output (error_msg
);
1083 monitor_show_help ();
1084 write_enn (own_buf
);
1088 else if (strcmp (mon
, "help") == 0)
1089 monitor_show_help ();
1090 else if (strcmp (mon
, "exit") == 0)
1094 monitor_output ("Unknown monitor command.\n\n");
1095 monitor_show_help ();
1096 write_enn (own_buf
);
1100 /* Associates a callback with each supported qXfer'able object. */
1104 /* The object this handler handles. */
1107 /* Request that the target transfer up to LEN 8-bit bytes of the
1108 target's OBJECT. The OFFSET, for a seekable object, specifies
1109 the starting point. The ANNEX can be used to provide additional
1110 data-specific information to the target.
1112 Return the number of bytes actually transfered, zero when no
1113 further transfer is possible, -1 on error, -2 when the transfer
1114 is not supported, and -3 on a verbose error message that should
1115 be preserved. Return of a positive value smaller than LEN does
1116 not indicate the end of the object, only the end of the transfer.
1118 One, and only one, of readbuf or writebuf must be non-NULL. */
1119 int (*xfer
) (const char *annex
,
1120 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1121 ULONGEST offset
, LONGEST len
);
1124 /* Handle qXfer:auxv:read. */
1127 handle_qxfer_auxv (const char *annex
,
1128 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1129 ULONGEST offset
, LONGEST len
)
1131 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
1134 if (annex
[0] != '\0' || !target_running ())
1137 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
1140 /* Handle qXfer:exec-file:read. */
1143 handle_qxfer_exec_file (const char *const_annex
,
1144 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1145 ULONGEST offset
, LONGEST len
)
1151 if (the_target
->pid_to_exec_file
== NULL
|| writebuf
!= NULL
)
1154 annex
= alloca (strlen (const_annex
) + 1);
1155 strcpy (annex
, const_annex
);
1156 annex
= unpack_varlen_hex (annex
, &pid
);
1157 if (annex
[0] != '\0' || pid
== 0)
1160 file
= (*the_target
->pid_to_exec_file
) (pid
);
1164 total_len
= strlen (file
);
1166 if (offset
> total_len
)
1169 if (offset
+ len
> total_len
)
1170 len
= total_len
- offset
;
1172 memcpy (readbuf
, file
+ offset
, len
);
1176 /* Handle qXfer:features:read. */
1179 handle_qxfer_features (const char *annex
,
1180 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1181 ULONGEST offset
, LONGEST len
)
1183 const char *document
;
1186 if (writebuf
!= NULL
)
1189 if (!target_running ())
1192 /* Grab the correct annex. */
1193 document
= get_features_xml (annex
);
1194 if (document
== NULL
)
1197 total_len
= strlen (document
);
1199 if (offset
> total_len
)
1202 if (offset
+ len
> total_len
)
1203 len
= total_len
- offset
;
1205 memcpy (readbuf
, document
+ offset
, len
);
1209 /* Worker routine for handle_qxfer_libraries.
1210 Add to the length pointed to by ARG a conservative estimate of the
1211 length needed to transmit the file name of INF. */
1214 accumulate_file_name_length (struct inferior_list_entry
*inf
, void *arg
)
1216 struct dll_info
*dll
= (struct dll_info
*) inf
;
1217 unsigned int *total_len
= arg
;
1219 /* Over-estimate the necessary memory. Assume that every character
1220 in the library name must be escaped. */
1221 *total_len
+= 128 + 6 * strlen (dll
->name
);
1224 /* Worker routine for handle_qxfer_libraries.
1225 Emit the XML to describe the library in INF. */
1228 emit_dll_description (struct inferior_list_entry
*inf
, void *arg
)
1230 struct dll_info
*dll
= (struct dll_info
*) inf
;
1235 strcpy (p
, " <library name=\"");
1237 name
= xml_escape_text (dll
->name
);
1241 strcpy (p
, "\"><segment address=\"");
1243 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1245 strcpy (p
, "\"/></library>\n");
1251 /* Handle qXfer:libraries:read. */
1254 handle_qxfer_libraries (const char *annex
,
1255 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1256 ULONGEST offset
, LONGEST len
)
1258 unsigned int total_len
;
1261 if (writebuf
!= NULL
)
1264 if (annex
[0] != '\0' || !target_running ())
1268 for_each_inferior_with_data (&all_dlls
, accumulate_file_name_length
,
1271 document
= malloc (total_len
);
1272 if (document
== NULL
)
1275 strcpy (document
, "<library-list>\n");
1276 p
= document
+ strlen (document
);
1278 for_each_inferior_with_data (&all_dlls
, emit_dll_description
, &p
);
1280 strcpy (p
, "</library-list>\n");
1282 total_len
= strlen (document
);
1284 if (offset
> total_len
)
1290 if (offset
+ len
> total_len
)
1291 len
= total_len
- offset
;
1293 memcpy (readbuf
, document
+ offset
, len
);
1298 /* Handle qXfer:libraries-svr4:read. */
1301 handle_qxfer_libraries_svr4 (const char *annex
,
1302 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1303 ULONGEST offset
, LONGEST len
)
1305 if (writebuf
!= NULL
)
1308 if (!target_running () || the_target
->qxfer_libraries_svr4
== NULL
)
1311 return the_target
->qxfer_libraries_svr4 (annex
, readbuf
, writebuf
, offset
, len
);
1314 /* Handle qXfer:osadata:read. */
1317 handle_qxfer_osdata (const char *annex
,
1318 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1319 ULONGEST offset
, LONGEST len
)
1321 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
1324 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
1327 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1330 handle_qxfer_siginfo (const char *annex
,
1331 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1332 ULONGEST offset
, LONGEST len
)
1334 if (the_target
->qxfer_siginfo
== NULL
)
1337 if (annex
[0] != '\0' || !target_running ())
1340 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1343 /* Handle qXfer:spu:read and qXfer:spu:write. */
1346 handle_qxfer_spu (const char *annex
,
1347 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1348 ULONGEST offset
, LONGEST len
)
1350 if (the_target
->qxfer_spu
== NULL
)
1353 if (!target_running ())
1356 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1359 /* Handle qXfer:statictrace:read. */
1362 handle_qxfer_statictrace (const char *annex
,
1363 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1364 ULONGEST offset
, LONGEST len
)
1368 if (writebuf
!= NULL
)
1371 if (annex
[0] != '\0' || !target_running () || current_traceframe
== -1)
1374 if (traceframe_read_sdata (current_traceframe
, offset
,
1375 readbuf
, len
, &nbytes
))
1380 /* Helper for handle_qxfer_threads_proper.
1381 Emit the XML to describe the thread of INF. */
1384 handle_qxfer_threads_worker (struct inferior_list_entry
*inf
, void *arg
)
1386 struct thread_info
*thread
= (struct thread_info
*) inf
;
1387 struct buffer
*buffer
= arg
;
1388 ptid_t ptid
= thread_to_gdb_id (thread
);
1390 int core
= target_core_of_thread (ptid
);
1393 write_ptid (ptid_s
, ptid
);
1397 sprintf (core_s
, "%d", core
);
1398 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1403 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1408 /* Helper for handle_qxfer_threads. */
1411 handle_qxfer_threads_proper (struct buffer
*buffer
)
1413 buffer_grow_str (buffer
, "<threads>\n");
1415 for_each_inferior_with_data (&all_threads
, handle_qxfer_threads_worker
,
1418 buffer_grow_str0 (buffer
, "</threads>\n");
1421 /* Handle qXfer:threads:read. */
1424 handle_qxfer_threads (const char *annex
,
1425 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1426 ULONGEST offset
, LONGEST len
)
1428 static char *result
= 0;
1429 static unsigned int result_length
= 0;
1431 if (writebuf
!= NULL
)
1434 if (!target_running () || annex
[0] != '\0')
1439 struct buffer buffer
;
1440 /* When asked for data at offset 0, generate everything and store into
1441 'result'. Successive reads will be served off 'result'. */
1445 buffer_init (&buffer
);
1447 handle_qxfer_threads_proper (&buffer
);
1449 result
= buffer_finish (&buffer
);
1450 result_length
= strlen (result
);
1451 buffer_free (&buffer
);
1454 if (offset
>= result_length
)
1456 /* We're out of data. */
1463 if (len
> result_length
- offset
)
1464 len
= result_length
- offset
;
1466 memcpy (readbuf
, result
+ offset
, len
);
1471 /* Handle qXfer:traceframe-info:read. */
1474 handle_qxfer_traceframe_info (const char *annex
,
1475 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1476 ULONGEST offset
, LONGEST len
)
1478 static char *result
= 0;
1479 static unsigned int result_length
= 0;
1481 if (writebuf
!= NULL
)
1484 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1489 struct buffer buffer
;
1491 /* When asked for data at offset 0, generate everything and
1492 store into 'result'. Successive reads will be served off
1496 buffer_init (&buffer
);
1498 traceframe_read_info (current_traceframe
, &buffer
);
1500 result
= buffer_finish (&buffer
);
1501 result_length
= strlen (result
);
1502 buffer_free (&buffer
);
1505 if (offset
>= result_length
)
1507 /* We're out of data. */
1514 if (len
> result_length
- offset
)
1515 len
= result_length
- offset
;
1517 memcpy (readbuf
, result
+ offset
, len
);
1521 /* Handle qXfer:fdpic:read. */
1524 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1525 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1527 if (the_target
->read_loadmap
== NULL
)
1530 if (!target_running ())
1533 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1536 /* Handle qXfer:btrace:read. */
1539 handle_qxfer_btrace (const char *annex
,
1540 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1541 ULONGEST offset
, LONGEST len
)
1543 static struct buffer cache
;
1544 struct thread_info
*thread
;
1547 if (the_target
->read_btrace
== NULL
|| writebuf
!= NULL
)
1550 if (!target_running ())
1553 if (ptid_equal (general_thread
, null_ptid
)
1554 || ptid_equal (general_thread
, minus_one_ptid
))
1556 strcpy (own_buf
, "E.Must select a single thread.");
1560 thread
= find_thread_ptid (general_thread
);
1563 strcpy (own_buf
, "E.No such thread.");
1567 if (thread
->btrace
== NULL
)
1569 strcpy (own_buf
, "E.Btrace not enabled.");
1573 if (strcmp (annex
, "all") == 0)
1574 type
= BTRACE_READ_ALL
;
1575 else if (strcmp (annex
, "new") == 0)
1576 type
= BTRACE_READ_NEW
;
1577 else if (strcmp (annex
, "delta") == 0)
1578 type
= BTRACE_READ_DELTA
;
1581 strcpy (own_buf
, "E.Bad annex.");
1587 buffer_free (&cache
);
1589 result
= target_read_btrace (thread
->btrace
, &cache
, type
);
1592 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1596 else if (offset
> cache
.used_size
)
1598 buffer_free (&cache
);
1602 if (len
> cache
.used_size
- offset
)
1603 len
= cache
.used_size
- offset
;
1605 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1610 /* Handle qXfer:btrace-conf:read. */
1613 handle_qxfer_btrace_conf (const char *annex
,
1614 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1615 ULONGEST offset
, LONGEST len
)
1617 static struct buffer cache
;
1618 struct thread_info
*thread
;
1621 if (the_target
->read_btrace_conf
== NULL
|| writebuf
!= NULL
)
1624 if (annex
[0] != '\0' || !target_running ())
1627 if (ptid_equal (general_thread
, null_ptid
)
1628 || ptid_equal (general_thread
, minus_one_ptid
))
1630 strcpy (own_buf
, "E.Must select a single thread.");
1634 thread
= find_thread_ptid (general_thread
);
1637 strcpy (own_buf
, "E.No such thread.");
1641 if (thread
->btrace
== NULL
)
1643 strcpy (own_buf
, "E.Btrace not enabled.");
1649 buffer_free (&cache
);
1651 result
= target_read_btrace_conf (thread
->btrace
, &cache
);
1654 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1658 else if (offset
> cache
.used_size
)
1660 buffer_free (&cache
);
1664 if (len
> cache
.used_size
- offset
)
1665 len
= cache
.used_size
- offset
;
1667 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1672 static const struct qxfer qxfer_packets
[] =
1674 { "auxv", handle_qxfer_auxv
},
1675 { "btrace", handle_qxfer_btrace
},
1676 { "btrace-conf", handle_qxfer_btrace_conf
},
1677 { "exec-file", handle_qxfer_exec_file
},
1678 { "fdpic", handle_qxfer_fdpic
},
1679 { "features", handle_qxfer_features
},
1680 { "libraries", handle_qxfer_libraries
},
1681 { "libraries-svr4", handle_qxfer_libraries_svr4
},
1682 { "osdata", handle_qxfer_osdata
},
1683 { "siginfo", handle_qxfer_siginfo
},
1684 { "spu", handle_qxfer_spu
},
1685 { "statictrace", handle_qxfer_statictrace
},
1686 { "threads", handle_qxfer_threads
},
1687 { "traceframe-info", handle_qxfer_traceframe_info
},
1691 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1699 if (!startswith (own_buf
, "qXfer:"))
1702 /* Grab the object, r/w and annex. */
1703 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1705 write_enn (own_buf
);
1710 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1713 const struct qxfer
*q
= &qxfer_packets
[i
];
1715 if (strcmp (object
, q
->object
) == 0)
1717 if (strcmp (rw
, "read") == 0)
1719 unsigned char *data
;
1724 /* Grab the offset and length. */
1725 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1727 write_enn (own_buf
);
1731 /* Read one extra byte, as an indicator of whether there is
1733 if (len
> PBUFSIZ
- 2)
1735 data
= malloc (len
+ 1);
1738 write_enn (own_buf
);
1741 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1749 /* Preserve error message. */
1752 write_enn (own_buf
);
1754 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1756 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1761 else if (strcmp (rw
, "write") == 0)
1766 unsigned char *data
;
1768 strcpy (own_buf
, "E00");
1769 data
= malloc (packet_len
- (offset
- own_buf
));
1772 write_enn (own_buf
);
1775 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1776 &ofs
, &len
, data
) < 0)
1779 write_enn (own_buf
);
1783 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1791 /* Preserve error message. */
1794 write_enn (own_buf
);
1796 sprintf (own_buf
, "%x", n
);
1809 /* Table used by the crc32 function to calcuate the checksum. */
1811 static unsigned int crc32_table
[256] =
1814 /* Compute 32 bit CRC from inferior memory.
1816 On success, return 32 bit CRC.
1817 On failure, return (unsigned long long) -1. */
1819 static unsigned long long
1820 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1822 if (!crc32_table
[1])
1824 /* Initialize the CRC table and the decoding table. */
1828 for (i
= 0; i
< 256; i
++)
1830 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1831 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1838 unsigned char byte
= 0;
1840 /* Return failure if memory read fails. */
1841 if (read_inferior_memory (base
, &byte
, 1) != 0)
1842 return (unsigned long long) -1;
1844 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1847 return (unsigned long long) crc
;
1850 /* Add supported btrace packets to BUF. */
1853 supported_btrace_packets (char *buf
)
1855 if (target_supports_btrace (BTRACE_FORMAT_BTS
))
1857 strcat (buf
, ";Qbtrace:bts+");
1858 strcat (buf
, ";Qbtrace-conf:bts:size+");
1863 strcat (buf
, ";Qbtrace:off+");
1864 strcat (buf
, ";qXfer:btrace:read+");
1865 strcat (buf
, ";qXfer:btrace-conf:read+");
1868 /* Handle all of the extended 'q' packets. */
1871 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1873 static struct inferior_list_entry
*thread_ptr
;
1875 /* Reply the current thread id. */
1876 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1879 require_running (own_buf
);
1881 if (!ptid_equal (general_thread
, null_ptid
)
1882 && !ptid_equal (general_thread
, minus_one_ptid
))
1883 gdb_id
= general_thread
;
1886 thread_ptr
= get_first_inferior (&all_threads
);
1887 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1890 sprintf (own_buf
, "QC");
1892 write_ptid (own_buf
, gdb_id
);
1896 if (strcmp ("qSymbol::", own_buf
) == 0)
1898 /* GDB is suggesting new symbols have been loaded. This may
1899 mean a new shared library has been detected as loaded, so
1900 take the opportunity to check if breakpoints we think are
1901 inserted, still are. Note that it isn't guaranteed that
1902 we'll see this when a shared library is loaded, and nor will
1903 we see this for unloads (although breakpoints in unloaded
1904 libraries shouldn't trigger), as GDB may not find symbols for
1905 the library at all. We also re-validate breakpoints when we
1906 see a second GDB breakpoint for the same address, and or when
1907 we access breakpoint shadows. */
1908 validate_breakpoints ();
1910 if (target_supports_tracepoints ())
1911 tracepoint_look_up_symbols ();
1913 if (target_running () && the_target
->look_up_symbols
!= NULL
)
1914 (*the_target
->look_up_symbols
) ();
1916 strcpy (own_buf
, "OK");
1920 if (!disable_packet_qfThreadInfo
)
1922 if (strcmp ("qfThreadInfo", own_buf
) == 0)
1926 require_running (own_buf
);
1927 thread_ptr
= get_first_inferior (&all_threads
);
1930 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1931 write_ptid (own_buf
, gdb_id
);
1932 thread_ptr
= thread_ptr
->next
;
1936 if (strcmp ("qsThreadInfo", own_buf
) == 0)
1940 require_running (own_buf
);
1941 if (thread_ptr
!= NULL
)
1944 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1945 write_ptid (own_buf
, gdb_id
);
1946 thread_ptr
= thread_ptr
->next
;
1951 sprintf (own_buf
, "l");
1957 if (the_target
->read_offsets
!= NULL
1958 && strcmp ("qOffsets", own_buf
) == 0)
1960 CORE_ADDR text
, data
;
1962 require_running (own_buf
);
1963 if (the_target
->read_offsets (&text
, &data
))
1964 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
1965 (long)text
, (long)data
, (long)data
);
1967 write_enn (own_buf
);
1972 /* Protocol features query. */
1973 if (startswith (own_buf
, "qSupported")
1974 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1976 char *p
= &own_buf
[10];
1977 int gdb_supports_qRelocInsn
= 0;
1979 /* Start processing qSupported packet. */
1980 target_process_qsupported (NULL
);
1982 /* Process each feature being provided by GDB. The first
1983 feature will follow a ':', and latter features will follow
1987 char **qsupported
= NULL
;
1991 /* Two passes, to avoid nested strtok calls in
1992 target_process_qsupported. */
1993 for (p
= strtok (p
+ 1, ";");
1995 p
= strtok (NULL
, ";"))
1998 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
1999 qsupported
[count
- 1] = xstrdup (p
);
2002 for (i
= 0; i
< count
; i
++)
2005 if (strcmp (p
, "multiprocess+") == 0)
2007 /* GDB supports and wants multi-process support if
2009 if (target_supports_multi_process ())
2012 else if (strcmp (p
, "qRelocInsn+") == 0)
2014 /* GDB supports relocate instruction requests. */
2015 gdb_supports_qRelocInsn
= 1;
2017 else if (strcmp (p
, "swbreak+") == 0)
2019 /* GDB wants us to report whether a trap is caused
2020 by a software breakpoint and for us to handle PC
2021 adjustment if necessary on this target. */
2022 if (target_supports_stopped_by_sw_breakpoint ())
2023 swbreak_feature
= 1;
2025 else if (strcmp (p
, "hwbreak+") == 0)
2027 /* GDB wants us to report whether a trap is caused
2028 by a hardware breakpoint. */
2029 if (target_supports_stopped_by_hw_breakpoint ())
2030 hwbreak_feature
= 1;
2033 target_process_qsupported (p
);
2042 "PacketSize=%x;QPassSignals+;QProgramSignals+",
2045 if (the_target
->qxfer_libraries_svr4
!= NULL
)
2046 strcat (own_buf
, ";qXfer:libraries-svr4:read+"
2047 ";augmented-libraries-svr4-read+");
2050 /* We do not have any hook to indicate whether the non-SVR4 target
2051 backend supports qXfer:libraries:read, so always report it. */
2052 strcat (own_buf
, ";qXfer:libraries:read+");
2055 if (the_target
->read_auxv
!= NULL
)
2056 strcat (own_buf
, ";qXfer:auxv:read+");
2058 if (the_target
->qxfer_spu
!= NULL
)
2059 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
2061 if (the_target
->qxfer_siginfo
!= NULL
)
2062 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
2064 if (the_target
->read_loadmap
!= NULL
)
2065 strcat (own_buf
, ";qXfer:fdpic:read+");
2067 /* We always report qXfer:features:read, as targets may
2068 install XML files on a subsequent call to arch_setup.
2069 If we reported to GDB on startup that we don't support
2070 qXfer:feature:read at all, we will never be re-queried. */
2071 strcat (own_buf
, ";qXfer:features:read+");
2073 if (transport_is_reliable
)
2074 strcat (own_buf
, ";QStartNoAckMode+");
2076 if (the_target
->qxfer_osdata
!= NULL
)
2077 strcat (own_buf
, ";qXfer:osdata:read+");
2079 if (target_supports_multi_process ())
2080 strcat (own_buf
, ";multiprocess+");
2082 if (target_supports_non_stop ())
2083 strcat (own_buf
, ";QNonStop+");
2085 if (target_supports_disable_randomization ())
2086 strcat (own_buf
, ";QDisableRandomization+");
2088 strcat (own_buf
, ";qXfer:threads:read+");
2090 if (target_supports_tracepoints ())
2092 strcat (own_buf
, ";ConditionalTracepoints+");
2093 strcat (own_buf
, ";TraceStateVariables+");
2094 strcat (own_buf
, ";TracepointSource+");
2095 strcat (own_buf
, ";DisconnectedTracing+");
2096 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
2097 strcat (own_buf
, ";FastTracepoints+");
2098 strcat (own_buf
, ";StaticTracepoints+");
2099 strcat (own_buf
, ";InstallInTrace+");
2100 strcat (own_buf
, ";qXfer:statictrace:read+");
2101 strcat (own_buf
, ";qXfer:traceframe-info:read+");
2102 strcat (own_buf
, ";EnableDisableTracepoints+");
2103 strcat (own_buf
, ";QTBuffer:size+");
2104 strcat (own_buf
, ";tracenz+");
2107 /* Support target-side breakpoint conditions and commands. */
2108 strcat (own_buf
, ";ConditionalBreakpoints+");
2109 strcat (own_buf
, ";BreakpointCommands+");
2111 if (target_supports_agent ())
2112 strcat (own_buf
, ";QAgent+");
2114 supported_btrace_packets (own_buf
);
2116 if (target_supports_stopped_by_sw_breakpoint ())
2117 strcat (own_buf
, ";swbreak+");
2119 if (target_supports_stopped_by_hw_breakpoint ())
2120 strcat (own_buf
, ";hwbreak+");
2122 if (the_target
->pid_to_exec_file
!= NULL
)
2123 strcat (own_buf
, ";qXfer:exec-file:read+");
2128 /* Thread-local storage support. */
2129 if (the_target
->get_tls_address
!= NULL
2130 && startswith (own_buf
, "qGetTLSAddr:"))
2132 char *p
= own_buf
+ 12;
2133 CORE_ADDR parts
[2], address
= 0;
2135 ptid_t ptid
= null_ptid
;
2137 require_running (own_buf
);
2139 for (i
= 0; i
< 3; i
++)
2147 p2
= strchr (p
, ',');
2160 ptid
= read_ptid (p
, NULL
);
2162 decode_address (&parts
[i
- 1], p
, len
);
2166 if (p
!= NULL
|| i
< 3)
2170 struct thread_info
*thread
= find_thread_ptid (ptid
);
2175 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
2181 strcpy (own_buf
, paddress(address
));
2186 write_enn (own_buf
);
2190 /* Otherwise, pretend we do not understand this packet. */
2193 /* Windows OS Thread Information Block address support. */
2194 if (the_target
->get_tib_address
!= NULL
2195 && startswith (own_buf
, "qGetTIBAddr:"))
2200 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
2202 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
2205 strcpy (own_buf
, paddress(tlb
));
2210 write_enn (own_buf
);
2216 /* Handle "monitor" commands. */
2217 if (startswith (own_buf
, "qRcmd,"))
2219 char *mon
= malloc (PBUFSIZ
);
2220 int len
= strlen (own_buf
+ 6);
2224 write_enn (own_buf
);
2229 || hex2bin (own_buf
+ 6, (gdb_byte
*) mon
, len
/ 2) != len
/ 2)
2231 write_enn (own_buf
);
2235 mon
[len
/ 2] = '\0';
2239 if (the_target
->handle_monitor_command
== NULL
2240 || (*the_target
->handle_monitor_command
) (mon
) == 0)
2241 /* Default processing. */
2242 handle_monitor_command (mon
, own_buf
);
2248 if (startswith (own_buf
, "qSearch:memory:"))
2250 require_running (own_buf
);
2251 handle_search_memory (own_buf
, packet_len
);
2255 if (strcmp (own_buf
, "qAttached") == 0
2256 || startswith (own_buf
, "qAttached:"))
2258 struct process_info
*process
;
2260 if (own_buf
[sizeof ("qAttached") - 1])
2262 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
2263 process
= (struct process_info
*)
2264 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
2268 require_running (own_buf
);
2269 process
= current_process ();
2272 if (process
== NULL
)
2274 write_enn (own_buf
);
2278 strcpy (own_buf
, process
->attached
? "1" : "0");
2282 if (startswith (own_buf
, "qCRC:"))
2284 /* CRC check (compare-section). */
2288 unsigned long long crc
;
2290 require_running (own_buf
);
2291 comma
= unpack_varlen_hex (own_buf
+ 5, &base
);
2292 if (*comma
++ != ',')
2294 write_enn (own_buf
);
2297 len
= strtoul (comma
, NULL
, 16);
2298 crc
= crc32 (base
, len
, 0xffffffff);
2299 /* Check for memory failure. */
2300 if (crc
== (unsigned long long) -1)
2302 write_enn (own_buf
);
2305 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
2309 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
2312 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
2315 /* Otherwise we didn't know what packet it was. Say we didn't
2320 static void gdb_wants_all_threads_stopped (void);
2321 static void resume (struct thread_resume
*actions
, size_t n
);
2323 /* The callback that is passed to visit_actioned_threads. */
2324 typedef int (visit_actioned_threads_callback_ftype
)
2325 (const struct thread_resume
*, struct thread_info
*);
2327 /* Struct to pass data to visit_actioned_threads. */
2329 struct visit_actioned_threads_data
2331 const struct thread_resume
*actions
;
2333 visit_actioned_threads_callback_ftype
*callback
;
2336 /* Call CALLBACK for any thread to which ACTIONS applies to. Returns
2337 true if CALLBACK returns true. Returns false if no matching thread
2338 is found or CALLBACK results false.
2339 Note: This function is itself a callback for find_inferior. */
2342 visit_actioned_threads (struct inferior_list_entry
*entry
, void *datap
)
2344 struct visit_actioned_threads_data
*data
= datap
;
2345 const struct thread_resume
*actions
= data
->actions
;
2346 size_t num_actions
= data
->num_actions
;
2347 visit_actioned_threads_callback_ftype
*callback
= data
->callback
;
2350 for (i
= 0; i
< num_actions
; i
++)
2352 const struct thread_resume
*action
= &actions
[i
];
2354 if (ptid_equal (action
->thread
, minus_one_ptid
)
2355 || ptid_equal (action
->thread
, entry
->id
)
2356 || ((ptid_get_pid (action
->thread
)
2357 == ptid_get_pid (entry
->id
))
2358 && ptid_get_lwp (action
->thread
) == -1))
2360 struct thread_info
*thread
= (struct thread_info
*) entry
;
2362 if ((*callback
) (action
, thread
))
2370 /* Callback for visit_actioned_threads. If the thread has a pending
2371 status to report, report it now. */
2374 handle_pending_status (const struct thread_resume
*resumption
,
2375 struct thread_info
*thread
)
2377 if (thread
->status_pending_p
)
2379 thread
->status_pending_p
= 0;
2381 last_status
= thread
->last_status
;
2382 last_ptid
= thread
->entry
.id
;
2383 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2389 /* Parse vCont packets. */
2391 handle_v_cont (char *own_buf
)
2395 struct thread_resume
*resume_info
;
2396 struct thread_resume default_action
= {{0}};
2398 /* Count the number of semicolons in the packet. There should be one
2399 for every action. */
2405 p
= strchr (p
, ';');
2408 resume_info
= malloc (n
* sizeof (resume_info
[0]));
2409 if (resume_info
== NULL
)
2417 memset (&resume_info
[i
], 0, sizeof resume_info
[i
]);
2419 if (p
[0] == 's' || p
[0] == 'S')
2420 resume_info
[i
].kind
= resume_step
;
2421 else if (p
[0] == 'r')
2422 resume_info
[i
].kind
= resume_step
;
2423 else if (p
[0] == 'c' || p
[0] == 'C')
2424 resume_info
[i
].kind
= resume_continue
;
2425 else if (p
[0] == 't')
2426 resume_info
[i
].kind
= resume_stop
;
2430 if (p
[0] == 'S' || p
[0] == 'C')
2433 sig
= strtol (p
+ 1, &q
, 16);
2438 if (!gdb_signal_to_host_p (sig
))
2440 resume_info
[i
].sig
= gdb_signal_to_host (sig
);
2442 else if (p
[0] == 'r')
2446 p
= unpack_varlen_hex (p
+ 1, &addr
);
2447 resume_info
[i
].step_range_start
= addr
;
2452 p
= unpack_varlen_hex (p
+ 1, &addr
);
2453 resume_info
[i
].step_range_end
= addr
;
2462 resume_info
[i
].thread
= minus_one_ptid
;
2463 default_action
= resume_info
[i
];
2465 /* Note: we don't increment i here, we'll overwrite this entry
2466 the next time through. */
2468 else if (p
[0] == ':')
2470 ptid_t ptid
= read_ptid (p
+ 1, &q
);
2475 if (p
[0] != ';' && p
[0] != 0)
2478 resume_info
[i
].thread
= ptid
;
2485 resume_info
[i
] = default_action
;
2487 set_desired_thread (0);
2489 resume (resume_info
, n
);
2494 write_enn (own_buf
);
2499 /* Resume target with ACTIONS, an array of NUM_ACTIONS elements. */
2502 resume (struct thread_resume
*actions
, size_t num_actions
)
2506 /* Check if among the threads that GDB wants actioned, there's
2507 one with a pending status to report. If so, skip actually
2508 resuming/stopping and report the pending event
2510 struct visit_actioned_threads_data data
;
2512 data
.actions
= actions
;
2513 data
.num_actions
= num_actions
;
2514 data
.callback
= handle_pending_status
;
2515 if (find_inferior (&all_threads
, visit_actioned_threads
, &data
) != NULL
)
2521 (*the_target
->resume
) (actions
, num_actions
);
2527 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2529 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
)
2531 /* No proper RSP support for this yet. At least return
2533 sprintf (own_buf
, "E.No unwaited-for children left.");
2534 disable_async_io ();
2538 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2539 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
2540 && last_status
.kind
!= TARGET_WAITKIND_NO_RESUMED
)
2541 current_thread
->last_status
= last_status
;
2543 /* From the client's perspective, all-stop mode always stops all
2544 threads implicitly (and the target backend has already done
2545 so by now). Tag all threads as "want-stopped", so we don't
2546 resume them implicitly without the client telling us to. */
2547 gdb_wants_all_threads_stopped ();
2548 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2549 disable_async_io ();
2551 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2552 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2553 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2557 /* Attach to a new program. Return 1 if successful, 0 if failure. */
2559 handle_v_attach (char *own_buf
)
2563 pid
= strtol (own_buf
+ 8, NULL
, 16);
2564 if (pid
!= 0 && attach_inferior (pid
) == 0)
2566 /* Don't report shared library events after attaching, even if
2567 some libraries are preloaded. GDB will always poll the
2568 library list. Avoids the "stopped by shared library event"
2569 notice on the GDB side. */
2574 /* In non-stop, we don't send a resume reply. Stop events
2575 will follow up using the normal notification
2580 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2586 write_enn (own_buf
);
2591 /* Run a new program. Return 1 if successful, 0 if failure. */
2593 handle_v_run (char *own_buf
)
2595 char *p
, *next_p
, **new_argv
;
2599 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
2605 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
2606 if (new_argv
== NULL
)
2608 write_enn (own_buf
);
2613 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
2615 next_p
= strchr (p
, ';');
2617 next_p
= p
+ strlen (p
);
2619 if (i
== 0 && p
== next_p
)
2623 /* FIXME: Fail request if out of memory instead of dying. */
2624 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
2625 hex2bin (p
, (gdb_byte
*) new_argv
[i
], (next_p
- p
) / 2);
2626 new_argv
[i
][(next_p
- p
) / 2] = '\0';
2635 if (new_argv
[0] == NULL
)
2637 /* GDB didn't specify a program to run. Use the program from the
2638 last run with the new argument list. */
2640 if (program_argv
== NULL
)
2642 write_enn (own_buf
);
2643 freeargv (new_argv
);
2647 new_argv
[0] = strdup (program_argv
[0]);
2648 if (new_argv
[0] == NULL
)
2650 write_enn (own_buf
);
2651 freeargv (new_argv
);
2656 /* Free the old argv and install the new one. */
2657 freeargv (program_argv
);
2658 program_argv
= new_argv
;
2660 start_inferior (program_argv
);
2661 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
2663 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2665 /* In non-stop, sending a resume reply doesn't set the general
2666 thread, but GDB assumes a vRun sets it (this is so GDB can
2667 query which is the main thread of the new inferior. */
2669 general_thread
= last_ptid
;
2675 write_enn (own_buf
);
2680 /* Kill process. Return 1 if successful, 0 if failure. */
2682 handle_v_kill (char *own_buf
)
2685 char *p
= &own_buf
[6];
2687 pid
= strtol (p
, NULL
, 16);
2690 if (pid
!= 0 && kill_inferior (pid
) == 0)
2692 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2693 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
2694 last_ptid
= pid_to_ptid (pid
);
2695 discard_queued_stop_replies (pid
);
2701 write_enn (own_buf
);
2706 /* Handle all of the extended 'v' packets. */
2708 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2710 if (!disable_packet_vCont
)
2712 if (startswith (own_buf
, "vCont;"))
2714 require_running (own_buf
);
2715 handle_v_cont (own_buf
);
2719 if (startswith (own_buf
, "vCont?"))
2721 strcpy (own_buf
, "vCont;c;C;s;S;t");
2722 if (target_supports_range_stepping ())
2724 own_buf
= own_buf
+ strlen (own_buf
);
2725 strcpy (own_buf
, ";r");
2731 if (startswith (own_buf
, "vFile:")
2732 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2735 if (startswith (own_buf
, "vAttach;"))
2737 if ((!extended_protocol
|| !multi_process
) && target_running ())
2739 fprintf (stderr
, "Already debugging a process\n");
2740 write_enn (own_buf
);
2743 handle_v_attach (own_buf
);
2747 if (startswith (own_buf
, "vRun;"))
2749 if ((!extended_protocol
|| !multi_process
) && target_running ())
2751 fprintf (stderr
, "Already debugging a process\n");
2752 write_enn (own_buf
);
2755 handle_v_run (own_buf
);
2759 if (startswith (own_buf
, "vKill;"))
2761 if (!target_running ())
2763 fprintf (stderr
, "No process to kill\n");
2764 write_enn (own_buf
);
2767 handle_v_kill (own_buf
);
2771 if (handle_notif_ack (own_buf
, packet_len
))
2774 /* Otherwise we didn't know what packet it was. Say we didn't
2780 /* Resume thread and wait for another event. In non-stop mode,
2781 don't really wait here, but return immediatelly to the event
2784 myresume (char *own_buf
, int step
, int sig
)
2786 struct thread_resume resume_info
[2];
2788 int valid_cont_thread
;
2790 set_desired_thread (0);
2792 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2793 && !ptid_equal (cont_thread
, minus_one_ptid
));
2795 if (step
|| sig
|| valid_cont_thread
)
2797 resume_info
[0].thread
= current_ptid
;
2799 resume_info
[0].kind
= resume_step
;
2801 resume_info
[0].kind
= resume_continue
;
2802 resume_info
[0].sig
= sig
;
2806 if (!valid_cont_thread
)
2808 resume_info
[n
].thread
= minus_one_ptid
;
2809 resume_info
[n
].kind
= resume_continue
;
2810 resume_info
[n
].sig
= 0;
2814 resume (resume_info
, n
);
2817 /* Callback for for_each_inferior. Make a new stop reply for each
2821 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2823 struct thread_info
*thread
= (struct thread_info
*) entry
;
2825 /* For now, assume targets that don't have this callback also don't
2826 manage the thread's last_status field. */
2827 if (the_target
->thread_stopped
== NULL
)
2829 struct vstop_notif
*new_notif
= xmalloc (sizeof (*new_notif
));
2831 new_notif
->ptid
= entry
->id
;
2832 new_notif
->status
= thread
->last_status
;
2833 /* Pass the last stop reply back to GDB, but don't notify
2835 notif_event_enque (¬if_stop
,
2836 (struct notif_event
*) new_notif
);
2840 if (thread_stopped (thread
))
2845 = target_waitstatus_to_string (&thread
->last_status
);
2847 debug_printf ("Reporting thread %s as already stopped with %s\n",
2848 target_pid_to_str (entry
->id
),
2851 xfree (status_string
);
2854 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2856 /* Pass the last stop reply back to GDB, but don't notify
2858 queue_stop_reply (entry
->id
, &thread
->last_status
);
2865 /* Set this inferior threads's state as "want-stopped". We won't
2866 resume this thread until the client gives us another action for
2870 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2872 struct thread_info
*thread
= (struct thread_info
*) entry
;
2874 thread
->last_resume_kind
= resume_stop
;
2876 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2878 /* Most threads are stopped implicitly (all-stop); tag that with
2880 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2881 thread
->last_status
.value
.sig
= GDB_SIGNAL_0
;
2885 /* Set all threads' states as "want-stopped". */
2888 gdb_wants_all_threads_stopped (void)
2890 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2893 /* Clear the gdb_detached flag of every process. */
2896 gdb_reattached_process (struct inferior_list_entry
*entry
)
2898 struct process_info
*process
= (struct process_info
*) entry
;
2900 process
->gdb_detached
= 0;
2903 /* Callback for for_each_inferior. Clear the thread's pending status
2907 clear_pending_status_callback (struct inferior_list_entry
*entry
)
2909 struct thread_info
*thread
= (struct thread_info
*) entry
;
2911 thread
->status_pending_p
= 0;
2914 /* Callback for for_each_inferior. If the thread is stopped with an
2915 interesting event, mark it as having a pending event. */
2918 set_pending_status_callback (struct inferior_list_entry
*entry
)
2920 struct thread_info
*thread
= (struct thread_info
*) entry
;
2922 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
2923 || (thread
->last_status
.value
.sig
!= GDB_SIGNAL_0
2924 /* A breakpoint, watchpoint or finished step from a previous
2925 GDB run isn't considered interesting for a new GDB run.
2926 If we left those pending, the new GDB could consider them
2927 random SIGTRAPs. This leaves out real async traps. We'd
2928 have to peek into the (target-specific) siginfo to
2929 distinguish those. */
2930 && thread
->last_status
.value
.sig
!= GDB_SIGNAL_TRAP
))
2931 thread
->status_pending_p
= 1;
2934 /* Callback for find_inferior. Return true if ENTRY (a thread) has a
2935 pending status to report to GDB. */
2938 find_status_pending_thread_callback (struct inferior_list_entry
*entry
, void *data
)
2940 struct thread_info
*thread
= (struct thread_info
*) entry
;
2942 return thread
->status_pending_p
;
2945 /* Status handler for the '?' packet. */
2948 handle_status (char *own_buf
)
2950 /* GDB is connected, don't forward events to the target anymore. */
2951 for_each_inferior (&all_processes
, gdb_reattached_process
);
2953 /* In non-stop mode, we must send a stop reply for each stopped
2954 thread. In all-stop mode, just send one for the first stopped
2959 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2961 /* The first is sent immediatly. OK is sent if there is no
2962 stopped thread, which is the same handling of the vStopped
2963 packet (by design). */
2964 notif_write_event (¬if_stop
, own_buf
);
2968 struct inferior_list_entry
*thread
= NULL
;
2971 stabilize_threads ();
2972 gdb_wants_all_threads_stopped ();
2974 /* We can only report one status, but we might be coming out of
2975 non-stop -- if more than one thread is stopped with
2976 interesting events, leave events for the threads we're not
2977 reporting now pending. They'll be reported the next time the
2978 threads are resumed. Start by marking all interesting events
2980 for_each_inferior (&all_threads
, set_pending_status_callback
);
2982 /* Prefer the last thread that reported an event to GDB (even if
2983 that was a GDB_SIGNAL_TRAP). */
2984 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
2985 && last_status
.kind
!= TARGET_WAITKIND_EXITED
2986 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2987 thread
= find_inferior_id (&all_threads
, last_ptid
);
2989 /* If the last event thread is not found for some reason, look
2990 for some other thread that might have an event to report. */
2992 thread
= find_inferior (&all_threads
,
2993 find_status_pending_thread_callback
, NULL
);
2995 /* If we're still out of luck, simply pick the first thread in
2998 thread
= get_first_inferior (&all_threads
);
3002 struct thread_info
*tp
= (struct thread_info
*) thread
;
3004 /* We're reporting this event, so it's no longer
3006 tp
->status_pending_p
= 0;
3008 /* GDB assumes the current thread is the thread we're
3009 reporting the status for. */
3010 general_thread
= thread
->id
;
3011 set_desired_thread (1);
3013 gdb_assert (tp
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
3014 prepare_resume_reply (own_buf
, tp
->entry
.id
, &tp
->last_status
);
3017 strcpy (own_buf
, "W00");
3022 gdbserver_version (void)
3024 printf ("GNU gdbserver %s%s\n"
3025 "Copyright (C) 2015 Free Software Foundation, Inc.\n"
3026 "gdbserver is free software, covered by the "
3027 "GNU General Public License.\n"
3028 "This gdbserver was configured as \"%s\"\n",
3029 PKGVERSION
, version
, host_name
);
3033 gdbserver_usage (FILE *stream
)
3035 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
3036 "\tgdbserver [OPTIONS] --attach COMM PID\n"
3037 "\tgdbserver [OPTIONS] --multi COMM\n"
3039 "COMM may either be a tty device (for serial debugging),\n"
3040 "HOST:PORT to listen for a TCP connection, or '-' or 'stdio' to use \n"
3041 "stdin/stdout of gdbserver.\n"
3042 "PROG is the executable program. ARGS are arguments passed to inferior.\n"
3043 "PID is the process ID to attach to, when --attach is specified.\n"
3045 "Operating modes:\n"
3047 " --attach Attach to running process PID.\n"
3048 " --multi Start server without a specific program, and\n"
3049 " only quit when explicitly commanded.\n"
3050 " --once Exit after the first connection has closed.\n"
3051 " --help Print this message and then exit.\n"
3052 " --version Display version information and exit.\n"
3056 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
3057 " --disable-randomization\n"
3058 " Run PROG with address space randomization disabled.\n"
3059 " --no-disable-randomization\n"
3060 " Don't disable address space randomization when\n"
3065 " --debug Enable general debugging output.\n"
3066 " --debug-format=opt1[,opt2,...]\n"
3067 " Specify extra content in debugging output.\n"
3072 " --remote-debug Enable remote protocol debugging output.\n"
3073 " --disable-packet=opt1[,opt2,...]\n"
3074 " Disable support for RSP packets or features.\n"
3076 " vCont, Tthread, qC, qfThreadInfo and \n"
3077 " threads (disable all threading packets).\n"
3079 "For more information, consult the GDB manual (available as on-line \n"
3080 "info or a printed manual).\n");
3081 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
3082 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
3086 gdbserver_show_disableable (FILE *stream
)
3088 fprintf (stream
, "Disableable packets:\n"
3089 " vCont \tAll vCont packets\n"
3090 " qC \tQuerying the current thread\n"
3091 " qfThreadInfo\tThread listing\n"
3092 " Tthread \tPassing the thread specifier in the "
3093 "T stop reply packet\n"
3094 " threads \tAll of the above\n");
3098 #undef require_running
3099 #define require_running(BUF) \
3100 if (!target_running ()) \
3107 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
3109 int pid
= * (int *) args
;
3111 if (ptid_get_pid (entry
->id
) == pid
)
3118 kill_inferior_callback (struct inferior_list_entry
*entry
)
3120 struct process_info
*process
= (struct process_info
*) entry
;
3121 int pid
= ptid_get_pid (process
->entry
.id
);
3123 kill_inferior (pid
);
3124 discard_queued_stop_replies (pid
);
3127 /* Callback for for_each_inferior to detach or kill the inferior,
3128 depending on whether we attached to it or not.
3129 We inform the user whether we're detaching or killing the process
3130 as this is only called when gdbserver is about to exit. */
3133 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
3135 struct process_info
*process
= (struct process_info
*) entry
;
3136 int pid
= ptid_get_pid (process
->entry
.id
);
3138 if (process
->attached
)
3139 detach_inferior (pid
);
3141 kill_inferior (pid
);
3143 discard_queued_stop_replies (pid
);
3146 /* for_each_inferior callback for detach_or_kill_for_exit to print
3147 the pids of started inferiors. */
3150 print_started_pid (struct inferior_list_entry
*entry
)
3152 struct process_info
*process
= (struct process_info
*) entry
;
3154 if (! process
->attached
)
3156 int pid
= ptid_get_pid (process
->entry
.id
);
3157 fprintf (stderr
, " %d", pid
);
3161 /* for_each_inferior callback for detach_or_kill_for_exit to print
3162 the pids of attached inferiors. */
3165 print_attached_pid (struct inferior_list_entry
*entry
)
3167 struct process_info
*process
= (struct process_info
*) entry
;
3169 if (process
->attached
)
3171 int pid
= ptid_get_pid (process
->entry
.id
);
3172 fprintf (stderr
, " %d", pid
);
3176 /* Call this when exiting gdbserver with possible inferiors that need
3177 to be killed or detached from. */
3180 detach_or_kill_for_exit (void)
3182 /* First print a list of the inferiors we will be killing/detaching.
3183 This is to assist the user, for example, in case the inferior unexpectedly
3184 dies after we exit: did we screw up or did the inferior exit on its own?
3185 Having this info will save some head-scratching. */
3187 if (have_started_inferiors_p ())
3189 fprintf (stderr
, "Killing process(es):");
3190 for_each_inferior (&all_processes
, print_started_pid
);
3191 fprintf (stderr
, "\n");
3193 if (have_attached_inferiors_p ())
3195 fprintf (stderr
, "Detaching process(es):");
3196 for_each_inferior (&all_processes
, print_attached_pid
);
3197 fprintf (stderr
, "\n");
3200 /* Now we can kill or detach the inferiors. */
3202 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
3205 /* Value that will be passed to exit(3) when gdbserver exits. */
3206 static int exit_code
;
3208 /* Cleanup version of detach_or_kill_for_exit. */
3211 detach_or_kill_for_exit_cleanup (void *ignore
)
3216 detach_or_kill_for_exit ();
3219 CATCH (exception
, RETURN_MASK_ALL
)
3222 fprintf (stderr
, "Detach or kill failed: %s\n", exception
.message
);
3228 /* Main function. This is called by the real "main" function,
3229 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
3231 static void ATTRIBUTE_NORETURN
3232 captured_main (int argc
, char *argv
[])
3236 char *arg_end
, *port
;
3237 char **next_arg
= &argv
[1];
3238 volatile int multi_mode
= 0;
3239 volatile int attach
= 0;
3242 while (*next_arg
!= NULL
&& **next_arg
== '-')
3244 if (strcmp (*next_arg
, "--version") == 0)
3246 gdbserver_version ();
3249 else if (strcmp (*next_arg
, "--help") == 0)
3251 gdbserver_usage (stdout
);
3254 else if (strcmp (*next_arg
, "--attach") == 0)
3256 else if (strcmp (*next_arg
, "--multi") == 0)
3258 else if (strcmp (*next_arg
, "--wrapper") == 0)
3262 wrapper_argv
= next_arg
;
3263 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
3266 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
3268 gdbserver_usage (stderr
);
3272 /* Consume the "--". */
3275 else if (strcmp (*next_arg
, "--debug") == 0)
3277 else if (startswith (*next_arg
, "--debug-format="))
3280 = parse_debug_format_options ((*next_arg
)
3281 + sizeof ("--debug-format=") - 1, 0);
3283 if (error_msg
!= NULL
)
3285 fprintf (stderr
, "%s", error_msg
);
3289 else if (strcmp (*next_arg
, "--remote-debug") == 0)
3291 else if (strcmp (*next_arg
, "--disable-packet") == 0)
3293 gdbserver_show_disableable (stdout
);
3296 else if (startswith (*next_arg
, "--disable-packet="))
3298 char *packets
, *tok
;
3300 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
3301 for (tok
= strtok (packets
, ",");
3303 tok
= strtok (NULL
, ","))
3305 if (strcmp ("vCont", tok
) == 0)
3306 disable_packet_vCont
= 1;
3307 else if (strcmp ("Tthread", tok
) == 0)
3308 disable_packet_Tthread
= 1;
3309 else if (strcmp ("qC", tok
) == 0)
3310 disable_packet_qC
= 1;
3311 else if (strcmp ("qfThreadInfo", tok
) == 0)
3312 disable_packet_qfThreadInfo
= 1;
3313 else if (strcmp ("threads", tok
) == 0)
3315 disable_packet_vCont
= 1;
3316 disable_packet_Tthread
= 1;
3317 disable_packet_qC
= 1;
3318 disable_packet_qfThreadInfo
= 1;
3322 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
3324 gdbserver_show_disableable (stderr
);
3329 else if (strcmp (*next_arg
, "-") == 0)
3331 /* "-" specifies a stdio connection and is a form of port
3333 *next_arg
= STDIO_CONNECTION_NAME
;
3336 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
3337 disable_randomization
= 1;
3338 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
3339 disable_randomization
= 0;
3340 else if (strcmp (*next_arg
, "--once") == 0)
3344 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
3354 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
3356 gdbserver_usage (stderr
);
3360 /* Remember stdio descriptors. LISTEN_DESC must not be listed, it will be
3361 opened by remote_prepare. */
3364 /* We need to know whether the remote connection is stdio before
3365 starting the inferior. Inferiors created in this scenario have
3366 stdin,stdout redirected. So do this here before we call
3368 remote_prepare (port
);
3373 /* --attach used to come after PORT, so allow it there for
3375 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
3382 && (*next_arg
== NULL
3383 || (*next_arg
)[0] == '\0'
3384 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
3386 || next_arg
[1] != NULL
))
3391 gdbserver_usage (stderr
);
3395 initialize_async_io ();
3397 initialize_event_loop ();
3398 if (target_supports_tracepoints ())
3399 initialize_tracepoint ();
3401 own_buf
= xmalloc (PBUFSIZ
+ 1);
3402 mem_buf
= xmalloc (PBUFSIZ
);
3404 if (pid
== 0 && *next_arg
!= NULL
)
3408 n
= argc
- (next_arg
- argv
);
3409 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
3410 for (i
= 0; i
< n
; i
++)
3411 program_argv
[i
] = xstrdup (next_arg
[i
]);
3412 program_argv
[i
] = NULL
;
3414 /* Wait till we are at first instruction in program. */
3415 start_inferior (program_argv
);
3417 /* We are now (hopefully) stopped at the first instruction of
3418 the target process. This assumes that the target process was
3419 successfully created. */
3423 if (attach_inferior (pid
) == -1)
3424 error ("Attaching not supported on this target");
3426 /* Otherwise succeeded. */
3430 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3431 last_status
.value
.integer
= 0;
3432 last_ptid
= minus_one_ptid
;
3434 make_cleanup (detach_or_kill_for_exit_cleanup
, NULL
);
3436 initialize_notif ();
3438 /* Don't report shared library events on the initial connection,
3439 even if some libraries are preloaded. Avoids the "stopped by
3440 shared library event" notice on gdb side. */
3443 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3444 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3449 if (!was_running
&& !multi_mode
)
3450 error ("No program to debug");
3457 /* Be sure we're out of tfind mode. */
3458 current_traceframe
= -1;
3459 cont_thread
= null_ptid
;
3460 swbreak_feature
= 0;
3461 hwbreak_feature
= 0;
3467 /* Wait for events. This will return when all event sources
3468 are removed from the event loop. */
3469 start_event_loop ();
3471 /* If an exit was requested (using the "monitor exit"
3472 command), terminate now. The only other way to get
3473 here is for getpkt to fail; close the connection
3474 and reopen it at the top of the loop. */
3476 if (exit_requested
|| run_once
)
3477 throw_quit ("Quit");
3480 "Remote side has terminated connection. "
3481 "GDBserver will reopen the connection.\n");
3483 /* Get rid of any pending statuses. An eventual reconnection
3484 (by the same GDB instance or another) will refresh all its
3485 state from scratch. */
3486 discard_queued_stop_replies (-1);
3487 for_each_inferior (&all_threads
,
3488 clear_pending_status_callback
);
3492 if (disconnected_tracing
)
3494 /* Try to enable non-stop/async mode, so we we can
3495 both wait for an async socket accept, and handle
3496 async target events simultaneously. There's also
3497 no point either in having the target always stop
3498 all threads, when we're going to pass signals
3499 down without informing GDB. */
3502 if (start_non_stop (1))
3505 /* Detaching implicitly resumes all threads;
3506 simply disconnecting does not. */
3512 "Disconnected tracing disabled; "
3513 "stopping trace run.\n");
3518 CATCH (exception
, RETURN_MASK_ERROR
)
3520 if (response_needed
)
3522 write_enn (own_buf
);
3530 /* Main function. */
3533 main (int argc
, char *argv
[])
3538 captured_main (argc
, argv
);
3540 CATCH (exception
, RETURN_MASK_ALL
)
3542 if (exception
.reason
== RETURN_ERROR
)
3545 fprintf (stderr
, "%s\n", exception
.message
);
3546 fprintf (stderr
, "Exiting\n");
3554 gdb_assert_not_reached ("captured_main should never return");
3557 /* Skip PACKET until the next semi-colon (or end of string). */
3560 skip_to_semicolon (char **packet
)
3562 while (**packet
!= '\0' && **packet
!= ';')
3566 /* Process options coming from Z packets for a breakpoint. PACKET is
3567 the packet buffer. *PACKET is updated to point to the first char
3568 after the last processed option. */
3571 process_point_options (struct breakpoint
*bp
, char **packet
)
3573 char *dataptr
= *packet
;
3576 /* Check if data has the correct format. */
3577 if (*dataptr
!= ';')
3584 if (*dataptr
== ';')
3587 if (*dataptr
== 'X')
3589 /* Conditional expression. */
3591 debug_printf ("Found breakpoint condition.\n");
3592 if (!add_breakpoint_condition (bp
, &dataptr
))
3593 skip_to_semicolon (&dataptr
);
3595 else if (startswith (dataptr
, "cmds:"))
3597 dataptr
+= strlen ("cmds:");
3599 debug_printf ("Found breakpoint commands %s.\n", dataptr
);
3600 persist
= (*dataptr
== '1');
3602 if (add_breakpoint_commands (bp
, &dataptr
, persist
))
3603 skip_to_semicolon (&dataptr
);
3607 fprintf (stderr
, "Unknown token %c, ignoring.\n",
3609 /* Skip tokens until we find one that we recognize. */
3610 skip_to_semicolon (&dataptr
);
3616 /* Event loop callback that handles a serial event. The first byte in
3617 the serial buffer gets us here. We expect characters to arrive at
3618 a brisk pace, so we read the rest of the packet with a blocking
3622 process_serial_event (void)
3633 int new_packet_len
= -1;
3635 /* Used to decide when gdbserver should exit in
3636 multi-mode/remote. */
3637 static int have_ran
= 0;
3640 have_ran
= target_running ();
3642 disable_async_io ();
3644 response_needed
= 0;
3645 packet_len
= getpkt (own_buf
);
3646 if (packet_len
<= 0)
3649 /* Force an event loop break. */
3652 response_needed
= 1;
3659 handle_query (own_buf
, packet_len
, &new_packet_len
);
3662 handle_general_set (own_buf
);
3665 require_running (own_buf
);
3670 pid
= strtol (&own_buf
[i
], NULL
, 16);
3673 pid
= ptid_get_pid (current_ptid
);
3675 if ((tracing
&& disconnected_tracing
) || any_persistent_commands ())
3677 struct thread_resume resume_info
;
3678 struct process_info
*process
= find_process_pid (pid
);
3680 if (process
== NULL
)
3682 write_enn (own_buf
);
3686 if (tracing
&& disconnected_tracing
)
3688 "Disconnected tracing in effect, "
3689 "leaving gdbserver attached to the process\n");
3691 if (any_persistent_commands ())
3693 "Persistent commands are present, "
3694 "leaving gdbserver attached to the process\n");
3696 /* Make sure we're in non-stop/async mode, so we we can both
3697 wait for an async socket accept, and handle async target
3698 events simultaneously. There's also no point either in
3699 having the target stop all threads, when we're going to
3700 pass signals down without informing GDB. */
3704 debug_printf ("Forcing non-stop mode\n");
3710 process
->gdb_detached
= 1;
3712 /* Detaching implicitly resumes all threads. */
3713 resume_info
.thread
= minus_one_ptid
;
3714 resume_info
.kind
= resume_continue
;
3715 resume_info
.sig
= 0;
3716 (*the_target
->resume
) (&resume_info
, 1);
3719 break; /* from switch/case */
3722 fprintf (stderr
, "Detaching from process %d\n", pid
);
3724 if (detach_inferior (pid
) != 0)
3725 write_enn (own_buf
);
3728 discard_queued_stop_replies (pid
);
3731 if (extended_protocol
)
3733 /* Treat this like a normal program exit. */
3734 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3735 last_status
.value
.integer
= 0;
3736 last_ptid
= pid_to_ptid (pid
);
3738 current_thread
= NULL
;
3745 /* If we are attached, then we can exit. Otherwise, we
3746 need to hang around doing nothing, until the child is
3748 join_inferior (pid
);
3754 extended_protocol
= 1;
3758 handle_status (own_buf
);
3761 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
3763 ptid_t gdb_id
, thread_id
;
3766 require_running (own_buf
);
3768 gdb_id
= read_ptid (&own_buf
[2], NULL
);
3770 pid
= ptid_get_pid (gdb_id
);
3772 if (ptid_equal (gdb_id
, null_ptid
)
3773 || ptid_equal (gdb_id
, minus_one_ptid
))
3774 thread_id
= null_ptid
;
3776 && ptid_equal (pid_to_ptid (pid
),
3779 struct thread_info
*thread
=
3780 (struct thread_info
*) find_inferior (&all_threads
,
3785 write_enn (own_buf
);
3789 thread_id
= thread
->entry
.id
;
3793 thread_id
= gdb_id_to_thread_id (gdb_id
);
3794 if (ptid_equal (thread_id
, null_ptid
))
3796 write_enn (own_buf
);
3801 if (own_buf
[1] == 'g')
3803 if (ptid_equal (thread_id
, null_ptid
))
3805 /* GDB is telling us to choose any thread. Check if
3806 the currently selected thread is still valid. If
3807 it is not, select the first available. */
3808 struct thread_info
*thread
=
3809 (struct thread_info
*) find_inferior_id (&all_threads
,
3813 thread
= get_first_thread ();
3814 thread_id
= thread
->entry
.id
;
3818 general_thread
= thread_id
;
3819 set_desired_thread (1);
3821 else if (own_buf
[1] == 'c')
3822 cont_thread
= thread_id
;
3828 /* Silently ignore it so that gdb can extend the protocol
3829 without compatibility headaches. */
3834 require_running (own_buf
);
3835 if (current_traceframe
>= 0)
3837 struct regcache
*regcache
3838 = new_register_cache (current_target_desc ());
3840 if (fetch_traceframe_registers (current_traceframe
,
3842 registers_to_string (regcache
, own_buf
);
3844 write_enn (own_buf
);
3845 free_register_cache (regcache
);
3849 struct regcache
*regcache
;
3851 set_desired_thread (1);
3852 regcache
= get_thread_regcache (current_thread
, 1);
3853 registers_to_string (regcache
, own_buf
);
3857 require_running (own_buf
);
3858 if (current_traceframe
>= 0)
3859 write_enn (own_buf
);
3862 struct regcache
*regcache
;
3864 set_desired_thread (1);
3865 regcache
= get_thread_regcache (current_thread
, 1);
3866 registers_from_string (regcache
, &own_buf
[1]);
3871 require_running (own_buf
);
3872 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
3873 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
3875 write_enn (own_buf
);
3877 bin2hex (mem_buf
, own_buf
, res
);
3880 require_running (own_buf
);
3881 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
3882 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
3885 write_enn (own_buf
);
3888 require_running (own_buf
);
3889 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
3890 &mem_addr
, &len
, &mem_buf
) < 0
3891 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
3892 write_enn (own_buf
);
3897 require_running (own_buf
);
3898 hex2bin (own_buf
+ 1, &sig
, 1);
3899 if (gdb_signal_to_host_p (sig
))
3900 signal
= gdb_signal_to_host (sig
);
3903 myresume (own_buf
, 0, signal
);
3906 require_running (own_buf
);
3907 hex2bin (own_buf
+ 1, &sig
, 1);
3908 if (gdb_signal_to_host_p (sig
))
3909 signal
= gdb_signal_to_host (sig
);
3912 myresume (own_buf
, 1, signal
);
3915 require_running (own_buf
);
3917 myresume (own_buf
, 0, signal
);
3920 require_running (own_buf
);
3922 myresume (own_buf
, 1, signal
);
3924 case 'Z': /* insert_ ... */
3926 case 'z': /* remove_ ... */
3931 char type
= own_buf
[1];
3933 const int insert
= ch
== 'Z';
3934 char *p
= &own_buf
[3];
3936 p
= unpack_varlen_hex (p
, &addr
);
3937 len
= strtol (p
+ 1, &dataptr
, 16);
3941 struct breakpoint
*bp
;
3943 bp
= set_gdb_breakpoint (type
, addr
, len
, &res
);
3948 /* GDB may have sent us a list of *point parameters to
3949 be evaluated on the target's side. Read such list
3950 here. If we already have a list of parameters, GDB
3951 is telling us to drop that list and use this one
3953 clear_breakpoint_conditions_and_commands (bp
);
3954 process_point_options (bp
, &dataptr
);
3958 res
= delete_gdb_breakpoint (type
, addr
, len
);
3966 write_enn (own_buf
);
3970 response_needed
= 0;
3971 if (!target_running ())
3972 /* The packet we received doesn't make sense - but we can't
3973 reply to it, either. */
3976 fprintf (stderr
, "Killing all inferiors\n");
3977 for_each_inferior (&all_processes
, kill_inferior_callback
);
3979 /* When using the extended protocol, we wait with no program
3980 running. The traditional protocol will exit instead. */
3981 if (extended_protocol
)
3983 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3984 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
3992 ptid_t gdb_id
, thread_id
;
3994 require_running (own_buf
);
3996 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3997 thread_id
= gdb_id_to_thread_id (gdb_id
);
3998 if (ptid_equal (thread_id
, null_ptid
))
4000 write_enn (own_buf
);
4004 if (mythread_alive (thread_id
))
4007 write_enn (own_buf
);
4011 response_needed
= 0;
4013 /* Restarting the inferior is only supported in the extended
4015 if (extended_protocol
)
4017 if (target_running ())
4018 for_each_inferior (&all_processes
,
4019 kill_inferior_callback
);
4020 fprintf (stderr
, "GDBserver restarting\n");
4022 /* Wait till we are at 1st instruction in prog. */
4023 if (program_argv
!= NULL
)
4024 start_inferior (program_argv
);
4027 last_status
.kind
= TARGET_WAITKIND_EXITED
;
4028 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
4034 /* It is a request we don't understand. Respond with an
4035 empty packet so that gdb knows that we don't support this
4041 /* Extended (long) request. */
4042 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
4046 /* It is a request we don't understand. Respond with an empty
4047 packet so that gdb knows that we don't support this
4053 if (new_packet_len
!= -1)
4054 putpkt_binary (own_buf
, new_packet_len
);
4058 response_needed
= 0;
4060 if (!extended_protocol
&& have_ran
&& !target_running ())
4062 /* In non-stop, defer exiting until GDB had a chance to query
4063 the whole vStopped list (until it gets an OK). */
4064 if (QUEUE_is_empty (notif_event_p
, notif_stop
.queue
))
4066 /* Be transparent when GDB is connected through stdio -- no
4067 need to spam GDB's console. */
4068 if (!remote_connection_is_stdio ())
4069 fprintf (stderr
, "GDBserver exiting\n");
4081 /* Event-loop callback for serial events. */
4084 handle_serial_event (int err
, gdb_client_data client_data
)
4087 debug_printf ("handling possible serial event\n");
4089 /* Really handle it. */
4090 if (process_serial_event () < 0)
4093 /* Be sure to not change the selected thread behind GDB's back.
4094 Important in the non-stop mode asynchronous protocol. */
4095 set_desired_thread (1);
4100 /* Event-loop callback for target events. */
4103 handle_target_event (int err
, gdb_client_data client_data
)
4106 debug_printf ("handling possible target event\n");
4108 last_ptid
= mywait (minus_one_ptid
, &last_status
,
4111 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
)
4113 /* No RSP support for this yet. */
4115 else if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
4117 int pid
= ptid_get_pid (last_ptid
);
4118 struct process_info
*process
= find_process_pid (pid
);
4119 int forward_event
= !gdb_connected () || process
->gdb_detached
;
4121 if (last_status
.kind
== TARGET_WAITKIND_EXITED
4122 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
4124 mark_breakpoints_out (process
);
4125 mourn_inferior (process
);
4129 /* We're reporting this thread as stopped. Update its
4130 "want-stopped" state to what the client wants, until it
4131 gets a new resume action. */
4132 current_thread
->last_resume_kind
= resume_stop
;
4133 current_thread
->last_status
= last_status
;
4138 if (!target_running ())
4140 /* The last process exited. We're done. */
4144 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
4146 /* A thread stopped with a signal, but gdb isn't
4147 connected to handle it. Pass it down to the
4148 inferior, as if it wasn't being traced. */
4149 struct thread_resume resume_info
;
4152 debug_printf ("GDB not connected; forwarding event %d for"
4154 (int) last_status
.kind
,
4155 target_pid_to_str (last_ptid
));
4157 resume_info
.thread
= last_ptid
;
4158 resume_info
.kind
= resume_continue
;
4159 resume_info
.sig
= gdb_signal_to_host (last_status
.value
.sig
);
4160 (*the_target
->resume
) (&resume_info
, 1);
4162 else if (debug_threads
)
4163 debug_printf ("GDB not connected; ignoring event %d for [%s]\n",
4164 (int) last_status
.kind
,
4165 target_pid_to_str (last_ptid
));
4169 struct vstop_notif
*vstop_notif
4170 = xmalloc (sizeof (struct vstop_notif
));
4172 vstop_notif
->status
= last_status
;
4173 vstop_notif
->ptid
= last_ptid
;
4174 /* Push Stop notification. */
4175 notif_push (¬if_stop
,
4176 (struct notif_event
*) vstop_notif
);
4180 /* Be sure to not change the selected thread behind GDB's back.
4181 Important in the non-stop mode asynchronous protocol. */
4182 set_desired_thread (1);