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. */
62 /* Whether we should attempt to disable the operating system's address
63 space randomization feature before starting an inferior. */
64 int disable_randomization
= 1;
66 static char **program_argv
, **wrapper_argv
;
68 int pass_signals
[GDB_SIGNAL_LAST
];
69 int program_signals
[GDB_SIGNAL_LAST
];
70 int program_signals_p
;
72 /* The PID of the originally created or attached inferior. Used to
73 send signals to the process when GDB sends us an asynchronous interrupt
74 (user hitting Control-C in the client), and to wait for the child to exit
75 when no longer debugging it. */
77 unsigned long signal_pid
;
80 /* A file descriptor for the controlling terminal. */
83 /* TERMINAL_FD's original foreground group. */
84 pid_t old_foreground_pgrp
;
86 /* Hand back terminal ownership to the original foreground group. */
89 restore_old_foreground_pgrp (void)
91 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
95 /* Set if you want to disable optional thread related packets support
96 in gdbserver, for the sake of testing GDB against stubs that don't
98 int disable_packet_vCont
;
99 int disable_packet_Tthread
;
100 int disable_packet_qC
;
101 int disable_packet_qfThreadInfo
;
103 /* Last status reported to GDB. */
104 static struct target_waitstatus last_status
;
105 static ptid_t last_ptid
;
107 static char *own_buf
;
108 static unsigned char *mem_buf
;
110 /* A sub-class of 'struct notif_event' for stop, holding information
111 relative to a single stop reply. We keep a queue of these to
112 push to GDB in non-stop mode. */
116 struct notif_event base
;
118 /* Thread or process that got the event. */
122 struct target_waitstatus status
;
125 /* The current btrace configuration. This is gdbserver's mirror of GDB's
126 btrace configuration. */
127 static struct btrace_config current_btrace_conf
;
129 DEFINE_QUEUE_P (notif_event_p
);
131 /* Put a stop reply to the stop reply queue. */
134 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
136 struct vstop_notif
*new_notif
= xmalloc (sizeof (*new_notif
));
138 new_notif
->ptid
= ptid
;
139 new_notif
->status
= *status
;
141 notif_event_enque (¬if_stop
, (struct notif_event
*) new_notif
);
145 remove_all_on_match_pid (QUEUE (notif_event_p
) *q
,
146 QUEUE_ITER (notif_event_p
) *iter
,
147 struct notif_event
*event
,
153 || ptid_get_pid (((struct vstop_notif
*) event
)->ptid
) == *pid
)
155 if (q
->free_func
!= NULL
)
156 q
->free_func (event
);
158 QUEUE_remove_elem (notif_event_p
, q
, iter
);
164 /* Get rid of the currently pending stop replies for PID. If PID is
165 -1, then apply to all processes. */
168 discard_queued_stop_replies (int pid
)
170 QUEUE_iterate (notif_event_p
, notif_stop
.queue
,
171 remove_all_on_match_pid
, &pid
);
175 vstop_notif_reply (struct notif_event
*event
, char *own_buf
)
177 struct vstop_notif
*vstop
= (struct vstop_notif
*) event
;
179 prepare_resume_reply (own_buf
, vstop
->ptid
, &vstop
->status
);
182 struct notif_server notif_stop
=
184 "vStopped", "Stop", NULL
, vstop_notif_reply
,
188 target_running (void)
190 return get_first_thread () != NULL
;
194 start_inferior (char **argv
)
196 char **new_argv
= argv
;
198 if (wrapper_argv
!= NULL
)
202 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
204 for (i
= 0; argv
[i
] != NULL
; i
++)
206 new_argv
= alloca (sizeof (char *) * count
);
208 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
209 new_argv
[count
++] = wrapper_argv
[i
];
210 for (i
= 0; argv
[i
] != NULL
; i
++)
211 new_argv
[count
++] = argv
[i
];
212 new_argv
[count
] = NULL
;
218 for (i
= 0; new_argv
[i
]; ++i
)
219 debug_printf ("new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
224 signal (SIGTTOU
, SIG_DFL
);
225 signal (SIGTTIN
, SIG_DFL
);
228 signal_pid
= create_inferior (new_argv
[0], new_argv
);
230 /* FIXME: we don't actually know at this point that the create
231 actually succeeded. We won't know that until we wait. */
232 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
237 signal (SIGTTOU
, SIG_IGN
);
238 signal (SIGTTIN
, SIG_IGN
);
239 terminal_fd
= fileno (stderr
);
240 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
241 tcsetpgrp (terminal_fd
, signal_pid
);
242 atexit (restore_old_foreground_pgrp
);
245 if (wrapper_argv
!= NULL
)
247 struct thread_resume resume_info
;
249 memset (&resume_info
, 0, sizeof (resume_info
));
250 resume_info
.thread
= pid_to_ptid (signal_pid
);
251 resume_info
.kind
= resume_continue
;
254 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
256 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
261 (*the_target
->resume
) (&resume_info
, 1);
263 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
264 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
267 current_thread
->last_resume_kind
= resume_stop
;
268 current_thread
->last_status
= last_status
;
270 while (last_status
.value
.sig
!= GDB_SIGNAL_TRAP
);
275 /* Wait till we are at 1st instruction in program, return new pid
276 (assuming success). */
277 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
279 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
280 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
282 current_thread
->last_resume_kind
= resume_stop
;
283 current_thread
->last_status
= last_status
;
290 attach_inferior (int pid
)
292 /* myattach should return -1 if attaching is unsupported,
293 0 if it succeeded, and call error() otherwise. */
295 if (myattach (pid
) != 0)
298 fprintf (stderr
, "Attached; pid = %d\n", pid
);
301 /* FIXME - It may be that we should get the SIGNAL_PID from the
302 attach function, so that it can be the main thread instead of
303 whichever we were told to attach to. */
308 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
310 /* GDB knows to ignore the first SIGSTOP after attaching to a running
311 process using the "attach" command, but this is different; it's
312 just using "target remote". Pretend it's just starting up. */
313 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
314 && last_status
.value
.sig
== GDB_SIGNAL_STOP
)
315 last_status
.value
.sig
= GDB_SIGNAL_TRAP
;
317 current_thread
->last_resume_kind
= resume_stop
;
318 current_thread
->last_status
= last_status
;
324 extern int remote_debug
;
326 /* Decode a qXfer read request. Return 0 if everything looks OK,
330 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
332 /* After the read marker and annex, qXfer looks like a
333 traditional 'm' packet. */
334 decode_m_packet (buf
, ofs
, len
);
340 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
342 /* Extract and NUL-terminate the object. */
344 while (*buf
&& *buf
!= ':')
350 /* Extract and NUL-terminate the read/write action. */
352 while (*buf
&& *buf
!= ':')
358 /* Extract and NUL-terminate the annex. */
360 while (*buf
&& *buf
!= ':')
370 /* Write the response to a successful qXfer read. Returns the
371 length of the (binary) data stored in BUF, corresponding
372 to as much of DATA/LEN as we could fit. IS_MORE controls
373 the first character of the response. */
375 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
384 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
388 /* Handle btrace enabling in BTS format. */
391 handle_btrace_enable_bts (struct thread_info
*thread
)
393 if (thread
->btrace
!= NULL
)
394 return "E.Btrace already enabled.";
396 current_btrace_conf
.format
= BTRACE_FORMAT_BTS
;
397 thread
->btrace
= target_enable_btrace (thread
->entry
.id
,
398 ¤t_btrace_conf
);
399 if (thread
->btrace
== NULL
)
400 return "E.Could not enable btrace.";
405 /* Handle btrace disabling. */
408 handle_btrace_disable (struct thread_info
*thread
)
411 if (thread
->btrace
== NULL
)
412 return "E.Branch tracing not enabled.";
414 if (target_disable_btrace (thread
->btrace
) != 0)
415 return "E.Could not disable branch tracing.";
417 thread
->btrace
= NULL
;
421 /* Handle the "Qbtrace" packet. */
424 handle_btrace_general_set (char *own_buf
)
426 struct thread_info
*thread
;
430 if (strncmp ("Qbtrace:", own_buf
, strlen ("Qbtrace:")) != 0)
433 op
= own_buf
+ strlen ("Qbtrace:");
435 if (ptid_equal (general_thread
, null_ptid
)
436 || ptid_equal (general_thread
, minus_one_ptid
))
438 strcpy (own_buf
, "E.Must select a single thread.");
442 thread
= find_thread_ptid (general_thread
);
445 strcpy (own_buf
, "E.No such thread.");
451 if (strcmp (op
, "bts") == 0)
452 err
= handle_btrace_enable_bts (thread
);
453 else if (strcmp (op
, "off") == 0)
454 err
= handle_btrace_disable (thread
);
456 err
= "E.Bad Qbtrace operation. Use bts or off.";
459 strcpy (own_buf
, err
);
466 /* Handle the "Qbtrace-conf" packet. */
469 handle_btrace_conf_general_set (char *own_buf
)
471 struct thread_info
*thread
;
474 if (strncmp ("Qbtrace-conf:", own_buf
, strlen ("Qbtrace-conf:")) != 0)
477 op
= own_buf
+ strlen ("Qbtrace-conf:");
479 if (ptid_equal (general_thread
, null_ptid
)
480 || ptid_equal (general_thread
, minus_one_ptid
))
482 strcpy (own_buf
, "E.Must select a single thread.");
486 thread
= find_thread_ptid (general_thread
);
489 strcpy (own_buf
, "E.No such thread.");
493 if (strncmp (op
, "bts:size=", strlen ("bts:size=")) == 0)
499 size
= strtoul (op
+ strlen ("bts:size="), &endp
, 16);
500 if (endp
== NULL
|| *endp
!= 0 || errno
!= 0 || size
> UINT_MAX
)
502 strcpy (own_buf
, "E.Bad size value.");
506 current_btrace_conf
.bts
.size
= (unsigned int) size
;
510 strcpy (own_buf
, "E.Bad Qbtrace configuration option.");
518 /* Handle all of the extended 'Q' packets. */
521 handle_general_set (char *own_buf
)
523 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
525 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
526 const char *p
= own_buf
+ strlen ("QPassSignals:");
529 p
= decode_address_to_semicolon (&cursig
, p
);
530 for (i
= 0; i
< numsigs
; i
++)
536 /* Keep looping, to clear the remaining signals. */
539 p
= decode_address_to_semicolon (&cursig
, p
);
544 strcpy (own_buf
, "OK");
548 if (strncmp ("QProgramSignals:", own_buf
, strlen ("QProgramSignals:")) == 0)
550 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
551 const char *p
= own_buf
+ strlen ("QProgramSignals:");
554 program_signals_p
= 1;
556 p
= decode_address_to_semicolon (&cursig
, p
);
557 for (i
= 0; i
< numsigs
; i
++)
561 program_signals
[i
] = 1;
563 /* Keep looping, to clear the remaining signals. */
566 p
= decode_address_to_semicolon (&cursig
, p
);
569 program_signals
[i
] = 0;
571 strcpy (own_buf
, "OK");
575 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
579 fprintf (stderr
, "[noack mode enabled]\n");
588 if (strncmp (own_buf
, "QNonStop:", 9) == 0)
590 char *mode
= own_buf
+ 9;
594 if (strcmp (mode
, "0") == 0)
596 else if (strcmp (mode
, "1") == 0)
600 /* We don't know what this mode is, so complain to
602 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
608 req_str
= req
? "non-stop" : "all-stop";
609 if (start_non_stop (req
) != 0)
611 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
619 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
625 if (strncmp ("QDisableRandomization:", own_buf
,
626 strlen ("QDisableRandomization:")) == 0)
628 char *packet
= own_buf
+ strlen ("QDisableRandomization:");
631 unpack_varlen_hex (packet
, &setting
);
632 disable_randomization
= setting
;
636 if (disable_randomization
)
637 fprintf (stderr
, "[address space randomization disabled]\n");
639 fprintf (stderr
, "[address space randomization enabled]\n");
646 if (target_supports_tracepoints ()
647 && handle_tracepoint_general_set (own_buf
))
650 if (strncmp ("QAgent:", own_buf
, strlen ("QAgent:")) == 0)
652 char *mode
= own_buf
+ strlen ("QAgent:");
655 if (strcmp (mode
, "0") == 0)
657 else if (strcmp (mode
, "1") == 0)
661 /* We don't know what this value is, so complain to GDB. */
662 sprintf (own_buf
, "E.Unknown QAgent value");
666 /* Update the flag. */
669 fprintf (stderr
, "[%s agent]\n", req
? "Enable" : "Disable");
674 if (handle_btrace_general_set (own_buf
))
677 if (handle_btrace_conf_general_set (own_buf
))
680 /* Otherwise we didn't know what packet it was. Say we didn't
686 get_features_xml (const char *annex
)
688 const struct target_desc
*desc
= current_target_desc ();
690 /* `desc->xmltarget' defines what to return when looking for the
691 "target.xml" file. Its contents can either be verbatim XML code
692 (prefixed with a '@') or else the name of the actual XML file to
693 be used in place of "target.xml".
695 This variable is set up from the auto-generated
696 init_registers_... routine for the current target. */
698 if (desc
->xmltarget
!= NULL
&& strcmp (annex
, "target.xml") == 0)
700 if (*desc
->xmltarget
== '@')
701 return desc
->xmltarget
+ 1;
703 annex
= desc
->xmltarget
;
708 extern const char *const xml_builtin
[][2];
711 /* Look for the annex. */
712 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
713 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
716 if (xml_builtin
[i
][0] != NULL
)
717 return xml_builtin
[i
][1];
725 monitor_show_help (void)
727 monitor_output ("The following monitor commands are supported:\n");
728 monitor_output (" set debug <0|1>\n");
729 monitor_output (" Enable general debugging messages\n");
730 monitor_output (" set debug-hw-points <0|1>\n");
731 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
732 monitor_output (" set remote-debug <0|1>\n");
733 monitor_output (" Enable remote protocol debugging messages\n");
734 monitor_output (" set debug-format option1[,option2,...]\n");
735 monitor_output (" Add additional information to debugging messages\n");
736 monitor_output (" Options: all, none");
737 monitor_output (", timestamp");
738 monitor_output ("\n");
739 monitor_output (" exit\n");
740 monitor_output (" Quit GDBserver\n");
743 /* Read trace frame or inferior memory. Returns the number of bytes
744 actually read, zero when no further transfer is possible, and -1 on
745 error. Return of a positive value smaller than LEN does not
746 indicate there's no more to be read, only the end of the transfer.
747 E.g., when GDB reads memory from a traceframe, a first request may
748 be served from a memory block that does not cover the whole request
749 length. A following request gets the rest served from either
750 another block (of the same traceframe) or from the read-only
754 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
758 if (current_traceframe
>= 0)
761 ULONGEST length
= len
;
763 if (traceframe_read_mem (current_traceframe
,
764 memaddr
, myaddr
, len
, &nbytes
))
766 /* Data read from trace buffer, we're done. */
769 if (!in_readonly_region (memaddr
, length
))
771 /* Otherwise we have a valid readonly case, fall through. */
772 /* (assume no half-trace half-real blocks for now) */
775 res
= prepare_to_access_memory ();
778 res
= read_inferior_memory (memaddr
, myaddr
, len
);
779 done_accessing_memory ();
781 return res
== 0 ? len
: -1;
787 /* Write trace frame or inferior memory. Actually, writing to trace
788 frames is forbidden. */
791 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
793 if (current_traceframe
>= 0)
799 ret
= prepare_to_access_memory ();
802 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
803 done_accessing_memory ();
809 /* Subroutine of handle_search_memory to simplify it. */
812 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
813 gdb_byte
*pattern
, unsigned pattern_len
,
814 gdb_byte
*search_buf
,
815 unsigned chunk_size
, unsigned search_buf_size
,
816 CORE_ADDR
*found_addrp
)
818 /* Prime the search buffer. */
820 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
823 warning ("Unable to access %ld bytes of target "
824 "memory at 0x%lx, halting search.",
825 (long) search_buf_size
, (long) start_addr
);
829 /* Perform the search.
831 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
832 When we've scanned N bytes we copy the trailing bytes to the start and
833 read in another N bytes. */
835 while (search_space_len
>= pattern_len
)
838 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
842 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
844 if (found_ptr
!= NULL
)
846 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
847 *found_addrp
= found_addr
;
851 /* Not found in this chunk, skip to next chunk. */
853 /* Don't let search_space_len wrap here, it's unsigned. */
854 if (search_space_len
>= chunk_size
)
855 search_space_len
-= chunk_size
;
857 search_space_len
= 0;
859 if (search_space_len
>= pattern_len
)
861 unsigned keep_len
= search_buf_size
- chunk_size
;
862 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
865 /* Copy the trailing part of the previous iteration to the front
866 of the buffer for the next iteration. */
867 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
869 nr_to_read
= (search_space_len
- keep_len
< chunk_size
870 ? search_space_len
- keep_len
873 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
874 nr_to_read
) != search_buf_size
)
876 warning ("Unable to access %ld bytes of target memory "
877 "at 0x%lx, halting search.",
878 (long) nr_to_read
, (long) read_addr
);
882 start_addr
+= chunk_size
;
891 /* Handle qSearch:memory packets. */
894 handle_search_memory (char *own_buf
, int packet_len
)
896 CORE_ADDR start_addr
;
897 CORE_ADDR search_space_len
;
899 unsigned int pattern_len
;
900 /* NOTE: also defined in find.c testcase. */
901 #define SEARCH_CHUNK_SIZE 16000
902 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
903 /* Buffer to hold memory contents for searching. */
904 gdb_byte
*search_buf
;
905 unsigned search_buf_size
;
907 CORE_ADDR found_addr
;
908 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
910 pattern
= malloc (packet_len
);
913 error ("Unable to allocate memory to perform the search");
914 strcpy (own_buf
, "E00");
917 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
918 packet_len
- cmd_name_len
,
919 &start_addr
, &search_space_len
,
920 pattern
, &pattern_len
) < 0)
923 error ("Error in parsing qSearch:memory packet");
924 strcpy (own_buf
, "E00");
928 search_buf_size
= chunk_size
+ pattern_len
- 1;
930 /* No point in trying to allocate a buffer larger than the search space. */
931 if (search_space_len
< search_buf_size
)
932 search_buf_size
= search_space_len
;
934 search_buf
= malloc (search_buf_size
);
935 if (search_buf
== NULL
)
938 error ("Unable to allocate memory to perform the search");
939 strcpy (own_buf
, "E00");
943 found
= handle_search_memory_1 (start_addr
, search_space_len
,
944 pattern
, pattern_len
,
945 search_buf
, chunk_size
, search_buf_size
,
949 sprintf (own_buf
, "1,%lx", (long) found_addr
);
951 strcpy (own_buf
, "0");
953 strcpy (own_buf
, "E00");
959 #define require_running(BUF) \
960 if (!target_running ()) \
966 /* Parse options to --debug-format= and "monitor set debug-format".
967 ARG is the text after "--debug-format=" or "monitor set debug-format".
968 IS_MONITOR is non-zero if we're invoked via "monitor set debug-format".
969 This triggers calls to monitor_output.
970 The result is NULL if all options were parsed ok, otherwise an error
971 message which the caller must free.
973 N.B. These commands affect all debug format settings, they are not
974 cumulative. If a format is not specified, it is turned off.
975 However, we don't go to extra trouble with things like
976 "monitor set debug-format all,none,timestamp".
977 Instead we just parse them one at a time, in order.
979 The syntax for "monitor set debug" we support here is not identical
980 to gdb's "set debug foo on|off" because we also use this function to
981 parse "--debug-format=foo,bar". */
984 parse_debug_format_options (const char *arg
, int is_monitor
)
986 VEC (char_ptr
) *options
;
990 /* First turn all debug format options off. */
993 /* First remove leading spaces, for "monitor set debug-format". */
994 while (isspace (*arg
))
997 options
= delim_string_to_char_ptr_vec (arg
, ',');
999 for (ix
= 0; VEC_iterate (char_ptr
, options
, ix
, option
); ++ix
)
1001 if (strcmp (option
, "all") == 0)
1003 debug_timestamp
= 1;
1005 monitor_output ("All extra debug format options enabled.\n");
1007 else if (strcmp (option
, "none") == 0)
1009 debug_timestamp
= 0;
1011 monitor_output ("All extra debug format options disabled.\n");
1013 else if (strcmp (option
, "timestamp") == 0)
1015 debug_timestamp
= 1;
1017 monitor_output ("Timestamps will be added to debug output.\n");
1019 else if (*option
== '\0')
1021 /* An empty option, e.g., "--debug-format=foo,,bar", is ignored. */
1026 char *msg
= xstrprintf ("Unknown debug-format argument: \"%s\"\n",
1029 free_char_ptr_vec (options
);
1034 free_char_ptr_vec (options
);
1038 /* Handle monitor commands not handled by target-specific handlers. */
1041 handle_monitor_command (char *mon
, char *own_buf
)
1043 if (strcmp (mon
, "set debug 1") == 0)
1046 monitor_output ("Debug output enabled.\n");
1048 else if (strcmp (mon
, "set debug 0") == 0)
1051 monitor_output ("Debug output disabled.\n");
1053 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
1055 show_debug_regs
= 1;
1056 monitor_output ("H/W point debugging output enabled.\n");
1058 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
1060 show_debug_regs
= 0;
1061 monitor_output ("H/W point debugging output disabled.\n");
1063 else if (strcmp (mon
, "set remote-debug 1") == 0)
1066 monitor_output ("Protocol debug output enabled.\n");
1068 else if (strcmp (mon
, "set remote-debug 0") == 0)
1071 monitor_output ("Protocol debug output disabled.\n");
1073 else if (strncmp (mon
, "set debug-format ",
1074 sizeof ("set debug-format ") - 1) == 0)
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:features:read. */
1143 handle_qxfer_features (const char *annex
,
1144 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1145 ULONGEST offset
, LONGEST len
)
1147 const char *document
;
1150 if (writebuf
!= NULL
)
1153 if (!target_running ())
1156 /* Grab the correct annex. */
1157 document
= get_features_xml (annex
);
1158 if (document
== NULL
)
1161 total_len
= strlen (document
);
1163 if (offset
> total_len
)
1166 if (offset
+ len
> total_len
)
1167 len
= total_len
- offset
;
1169 memcpy (readbuf
, document
+ offset
, len
);
1173 /* Worker routine for handle_qxfer_libraries.
1174 Add to the length pointed to by ARG a conservative estimate of the
1175 length needed to transmit the file name of INF. */
1178 accumulate_file_name_length (struct inferior_list_entry
*inf
, void *arg
)
1180 struct dll_info
*dll
= (struct dll_info
*) inf
;
1181 unsigned int *total_len
= arg
;
1183 /* Over-estimate the necessary memory. Assume that every character
1184 in the library name must be escaped. */
1185 *total_len
+= 128 + 6 * strlen (dll
->name
);
1188 /* Worker routine for handle_qxfer_libraries.
1189 Emit the XML to describe the library in INF. */
1192 emit_dll_description (struct inferior_list_entry
*inf
, void *arg
)
1194 struct dll_info
*dll
= (struct dll_info
*) inf
;
1199 strcpy (p
, " <library name=\"");
1201 name
= xml_escape_text (dll
->name
);
1205 strcpy (p
, "\"><segment address=\"");
1207 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1209 strcpy (p
, "\"/></library>\n");
1215 /* Handle qXfer:libraries:read. */
1218 handle_qxfer_libraries (const char *annex
,
1219 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1220 ULONGEST offset
, LONGEST len
)
1222 unsigned int total_len
;
1225 if (writebuf
!= NULL
)
1228 if (annex
[0] != '\0' || !target_running ())
1232 for_each_inferior_with_data (&all_dlls
, accumulate_file_name_length
,
1235 document
= malloc (total_len
);
1236 if (document
== NULL
)
1239 strcpy (document
, "<library-list>\n");
1240 p
= document
+ strlen (document
);
1242 for_each_inferior_with_data (&all_dlls
, emit_dll_description
, &p
);
1244 strcpy (p
, "</library-list>\n");
1246 total_len
= strlen (document
);
1248 if (offset
> total_len
)
1254 if (offset
+ len
> total_len
)
1255 len
= total_len
- offset
;
1257 memcpy (readbuf
, document
+ offset
, len
);
1262 /* Handle qXfer:libraries-svr4:read. */
1265 handle_qxfer_libraries_svr4 (const char *annex
,
1266 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1267 ULONGEST offset
, LONGEST len
)
1269 if (writebuf
!= NULL
)
1272 if (!target_running () || the_target
->qxfer_libraries_svr4
== NULL
)
1275 return the_target
->qxfer_libraries_svr4 (annex
, readbuf
, writebuf
, offset
, len
);
1278 /* Handle qXfer:osadata:read. */
1281 handle_qxfer_osdata (const char *annex
,
1282 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1283 ULONGEST offset
, LONGEST len
)
1285 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
1288 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
1291 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1294 handle_qxfer_siginfo (const char *annex
,
1295 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1296 ULONGEST offset
, LONGEST len
)
1298 if (the_target
->qxfer_siginfo
== NULL
)
1301 if (annex
[0] != '\0' || !target_running ())
1304 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1307 /* Handle qXfer:spu:read and qXfer:spu:write. */
1310 handle_qxfer_spu (const char *annex
,
1311 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1312 ULONGEST offset
, LONGEST len
)
1314 if (the_target
->qxfer_spu
== NULL
)
1317 if (!target_running ())
1320 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1323 /* Handle qXfer:statictrace:read. */
1326 handle_qxfer_statictrace (const char *annex
,
1327 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1328 ULONGEST offset
, LONGEST len
)
1332 if (writebuf
!= NULL
)
1335 if (annex
[0] != '\0' || !target_running () || current_traceframe
== -1)
1338 if (traceframe_read_sdata (current_traceframe
, offset
,
1339 readbuf
, len
, &nbytes
))
1344 /* Helper for handle_qxfer_threads_proper.
1345 Emit the XML to describe the thread of INF. */
1348 handle_qxfer_threads_worker (struct inferior_list_entry
*inf
, void *arg
)
1350 struct thread_info
*thread
= (struct thread_info
*) inf
;
1351 struct buffer
*buffer
= arg
;
1352 ptid_t ptid
= thread_to_gdb_id (thread
);
1354 int core
= target_core_of_thread (ptid
);
1357 write_ptid (ptid_s
, ptid
);
1361 sprintf (core_s
, "%d", core
);
1362 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1367 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1372 /* Helper for handle_qxfer_threads. */
1375 handle_qxfer_threads_proper (struct buffer
*buffer
)
1377 buffer_grow_str (buffer
, "<threads>\n");
1379 for_each_inferior_with_data (&all_threads
, handle_qxfer_threads_worker
,
1382 buffer_grow_str0 (buffer
, "</threads>\n");
1385 /* Handle qXfer:threads:read. */
1388 handle_qxfer_threads (const char *annex
,
1389 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1390 ULONGEST offset
, LONGEST len
)
1392 static char *result
= 0;
1393 static unsigned int result_length
= 0;
1395 if (writebuf
!= NULL
)
1398 if (!target_running () || annex
[0] != '\0')
1403 struct buffer buffer
;
1404 /* When asked for data at offset 0, generate everything and store into
1405 'result'. Successive reads will be served off 'result'. */
1409 buffer_init (&buffer
);
1411 handle_qxfer_threads_proper (&buffer
);
1413 result
= buffer_finish (&buffer
);
1414 result_length
= strlen (result
);
1415 buffer_free (&buffer
);
1418 if (offset
>= result_length
)
1420 /* We're out of data. */
1427 if (len
> result_length
- offset
)
1428 len
= result_length
- offset
;
1430 memcpy (readbuf
, result
+ offset
, len
);
1435 /* Handle qXfer:traceframe-info:read. */
1438 handle_qxfer_traceframe_info (const char *annex
,
1439 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1440 ULONGEST offset
, LONGEST len
)
1442 static char *result
= 0;
1443 static unsigned int result_length
= 0;
1445 if (writebuf
!= NULL
)
1448 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1453 struct buffer buffer
;
1455 /* When asked for data at offset 0, generate everything and
1456 store into 'result'. Successive reads will be served off
1460 buffer_init (&buffer
);
1462 traceframe_read_info (current_traceframe
, &buffer
);
1464 result
= buffer_finish (&buffer
);
1465 result_length
= strlen (result
);
1466 buffer_free (&buffer
);
1469 if (offset
>= result_length
)
1471 /* We're out of data. */
1478 if (len
> result_length
- offset
)
1479 len
= result_length
- offset
;
1481 memcpy (readbuf
, result
+ offset
, len
);
1485 /* Handle qXfer:fdpic:read. */
1488 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1489 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1491 if (the_target
->read_loadmap
== NULL
)
1494 if (!target_running ())
1497 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1500 /* Handle qXfer:btrace:read. */
1503 handle_qxfer_btrace (const char *annex
,
1504 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1505 ULONGEST offset
, LONGEST len
)
1507 static struct buffer cache
;
1508 struct thread_info
*thread
;
1511 if (the_target
->read_btrace
== NULL
|| writebuf
!= NULL
)
1514 if (!target_running ())
1517 if (ptid_equal (general_thread
, null_ptid
)
1518 || ptid_equal (general_thread
, minus_one_ptid
))
1520 strcpy (own_buf
, "E.Must select a single thread.");
1524 thread
= find_thread_ptid (general_thread
);
1527 strcpy (own_buf
, "E.No such thread.");
1531 if (thread
->btrace
== NULL
)
1533 strcpy (own_buf
, "E.Btrace not enabled.");
1537 if (strcmp (annex
, "all") == 0)
1538 type
= BTRACE_READ_ALL
;
1539 else if (strcmp (annex
, "new") == 0)
1540 type
= BTRACE_READ_NEW
;
1541 else if (strcmp (annex
, "delta") == 0)
1542 type
= BTRACE_READ_DELTA
;
1545 strcpy (own_buf
, "E.Bad annex.");
1551 buffer_free (&cache
);
1553 result
= target_read_btrace (thread
->btrace
, &cache
, type
);
1556 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1560 else if (offset
> cache
.used_size
)
1562 buffer_free (&cache
);
1566 if (len
> cache
.used_size
- offset
)
1567 len
= cache
.used_size
- offset
;
1569 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1574 /* Handle qXfer:btrace-conf:read. */
1577 handle_qxfer_btrace_conf (const char *annex
,
1578 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1579 ULONGEST offset
, LONGEST len
)
1581 static struct buffer cache
;
1582 struct thread_info
*thread
;
1585 if (the_target
->read_btrace_conf
== NULL
|| writebuf
!= NULL
)
1588 if (annex
[0] != '\0' || !target_running ())
1591 if (ptid_equal (general_thread
, null_ptid
)
1592 || ptid_equal (general_thread
, minus_one_ptid
))
1594 strcpy (own_buf
, "E.Must select a single thread.");
1598 thread
= find_thread_ptid (general_thread
);
1601 strcpy (own_buf
, "E.No such thread.");
1605 if (thread
->btrace
== NULL
)
1607 strcpy (own_buf
, "E.Btrace not enabled.");
1613 buffer_free (&cache
);
1615 result
= target_read_btrace_conf (thread
->btrace
, &cache
);
1618 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1622 else if (offset
> cache
.used_size
)
1624 buffer_free (&cache
);
1628 if (len
> cache
.used_size
- offset
)
1629 len
= cache
.used_size
- offset
;
1631 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1636 static const struct qxfer qxfer_packets
[] =
1638 { "auxv", handle_qxfer_auxv
},
1639 { "btrace", handle_qxfer_btrace
},
1640 { "btrace-conf", handle_qxfer_btrace_conf
},
1641 { "fdpic", handle_qxfer_fdpic
},
1642 { "features", handle_qxfer_features
},
1643 { "libraries", handle_qxfer_libraries
},
1644 { "libraries-svr4", handle_qxfer_libraries_svr4
},
1645 { "osdata", handle_qxfer_osdata
},
1646 { "siginfo", handle_qxfer_siginfo
},
1647 { "spu", handle_qxfer_spu
},
1648 { "statictrace", handle_qxfer_statictrace
},
1649 { "threads", handle_qxfer_threads
},
1650 { "traceframe-info", handle_qxfer_traceframe_info
},
1654 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1662 if (strncmp (own_buf
, "qXfer:", 6) != 0)
1665 /* Grab the object, r/w and annex. */
1666 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1668 write_enn (own_buf
);
1673 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1676 const struct qxfer
*q
= &qxfer_packets
[i
];
1678 if (strcmp (object
, q
->object
) == 0)
1680 if (strcmp (rw
, "read") == 0)
1682 unsigned char *data
;
1687 /* Grab the offset and length. */
1688 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1690 write_enn (own_buf
);
1694 /* Read one extra byte, as an indicator of whether there is
1696 if (len
> PBUFSIZ
- 2)
1698 data
= malloc (len
+ 1);
1701 write_enn (own_buf
);
1704 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1712 /* Preserve error message. */
1715 write_enn (own_buf
);
1717 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1719 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1724 else if (strcmp (rw
, "write") == 0)
1729 unsigned char *data
;
1731 strcpy (own_buf
, "E00");
1732 data
= malloc (packet_len
- (offset
- own_buf
));
1735 write_enn (own_buf
);
1738 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1739 &ofs
, &len
, data
) < 0)
1742 write_enn (own_buf
);
1746 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1754 /* Preserve error message. */
1757 write_enn (own_buf
);
1759 sprintf (own_buf
, "%x", n
);
1772 /* Table used by the crc32 function to calcuate the checksum. */
1774 static unsigned int crc32_table
[256] =
1777 /* Compute 32 bit CRC from inferior memory.
1779 On success, return 32 bit CRC.
1780 On failure, return (unsigned long long) -1. */
1782 static unsigned long long
1783 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1785 if (!crc32_table
[1])
1787 /* Initialize the CRC table and the decoding table. */
1791 for (i
= 0; i
< 256; i
++)
1793 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1794 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1801 unsigned char byte
= 0;
1803 /* Return failure if memory read fails. */
1804 if (read_inferior_memory (base
, &byte
, 1) != 0)
1805 return (unsigned long long) -1;
1807 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1810 return (unsigned long long) crc
;
1813 /* Add supported btrace packets to BUF. */
1816 supported_btrace_packets (char *buf
)
1818 if (target_supports_btrace (BTRACE_FORMAT_BTS
))
1820 strcat (buf
, ";Qbtrace:bts+");
1821 strcat (buf
, ";Qbtrace-conf:bts:size+");
1826 strcat (buf
, ";Qbtrace:off+");
1827 strcat (buf
, ";qXfer:btrace:read+");
1828 strcat (buf
, ";qXfer:btrace-conf:read+");
1831 /* Handle all of the extended 'q' packets. */
1834 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1836 static struct inferior_list_entry
*thread_ptr
;
1838 /* Reply the current thread id. */
1839 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1842 require_running (own_buf
);
1844 if (!ptid_equal (general_thread
, null_ptid
)
1845 && !ptid_equal (general_thread
, minus_one_ptid
))
1846 gdb_id
= general_thread
;
1849 thread_ptr
= get_first_inferior (&all_threads
);
1850 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1853 sprintf (own_buf
, "QC");
1855 write_ptid (own_buf
, gdb_id
);
1859 if (strcmp ("qSymbol::", own_buf
) == 0)
1861 /* GDB is suggesting new symbols have been loaded. This may
1862 mean a new shared library has been detected as loaded, so
1863 take the opportunity to check if breakpoints we think are
1864 inserted, still are. Note that it isn't guaranteed that
1865 we'll see this when a shared library is loaded, and nor will
1866 we see this for unloads (although breakpoints in unloaded
1867 libraries shouldn't trigger), as GDB may not find symbols for
1868 the library at all. We also re-validate breakpoints when we
1869 see a second GDB breakpoint for the same address, and or when
1870 we access breakpoint shadows. */
1871 validate_breakpoints ();
1873 if (target_supports_tracepoints ())
1874 tracepoint_look_up_symbols ();
1876 if (target_running () && the_target
->look_up_symbols
!= NULL
)
1877 (*the_target
->look_up_symbols
) ();
1879 strcpy (own_buf
, "OK");
1883 if (!disable_packet_qfThreadInfo
)
1885 if (strcmp ("qfThreadInfo", own_buf
) == 0)
1889 require_running (own_buf
);
1890 thread_ptr
= get_first_inferior (&all_threads
);
1893 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1894 write_ptid (own_buf
, gdb_id
);
1895 thread_ptr
= thread_ptr
->next
;
1899 if (strcmp ("qsThreadInfo", own_buf
) == 0)
1903 require_running (own_buf
);
1904 if (thread_ptr
!= NULL
)
1907 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1908 write_ptid (own_buf
, gdb_id
);
1909 thread_ptr
= thread_ptr
->next
;
1914 sprintf (own_buf
, "l");
1920 if (the_target
->read_offsets
!= NULL
1921 && strcmp ("qOffsets", own_buf
) == 0)
1923 CORE_ADDR text
, data
;
1925 require_running (own_buf
);
1926 if (the_target
->read_offsets (&text
, &data
))
1927 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
1928 (long)text
, (long)data
, (long)data
);
1930 write_enn (own_buf
);
1935 /* Protocol features query. */
1936 if (strncmp ("qSupported", own_buf
, 10) == 0
1937 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1939 char *p
= &own_buf
[10];
1940 int gdb_supports_qRelocInsn
= 0;
1942 /* Start processing qSupported packet. */
1943 target_process_qsupported (NULL
);
1945 /* Process each feature being provided by GDB. The first
1946 feature will follow a ':', and latter features will follow
1950 char **qsupported
= NULL
;
1954 /* Two passes, to avoid nested strtok calls in
1955 target_process_qsupported. */
1956 for (p
= strtok (p
+ 1, ";");
1958 p
= strtok (NULL
, ";"))
1961 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
1962 qsupported
[count
- 1] = xstrdup (p
);
1965 for (i
= 0; i
< count
; i
++)
1968 if (strcmp (p
, "multiprocess+") == 0)
1970 /* GDB supports and wants multi-process support if
1972 if (target_supports_multi_process ())
1975 else if (strcmp (p
, "qRelocInsn+") == 0)
1977 /* GDB supports relocate instruction requests. */
1978 gdb_supports_qRelocInsn
= 1;
1981 target_process_qsupported (p
);
1990 "PacketSize=%x;QPassSignals+;QProgramSignals+",
1993 if (the_target
->qxfer_libraries_svr4
!= NULL
)
1994 strcat (own_buf
, ";qXfer:libraries-svr4:read+"
1995 ";augmented-libraries-svr4-read+");
1998 /* We do not have any hook to indicate whether the non-SVR4 target
1999 backend supports qXfer:libraries:read, so always report it. */
2000 strcat (own_buf
, ";qXfer:libraries:read+");
2003 if (the_target
->read_auxv
!= NULL
)
2004 strcat (own_buf
, ";qXfer:auxv:read+");
2006 if (the_target
->qxfer_spu
!= NULL
)
2007 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
2009 if (the_target
->qxfer_siginfo
!= NULL
)
2010 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
2012 if (the_target
->read_loadmap
!= NULL
)
2013 strcat (own_buf
, ";qXfer:fdpic:read+");
2015 /* We always report qXfer:features:read, as targets may
2016 install XML files on a subsequent call to arch_setup.
2017 If we reported to GDB on startup that we don't support
2018 qXfer:feature:read at all, we will never be re-queried. */
2019 strcat (own_buf
, ";qXfer:features:read+");
2021 if (transport_is_reliable
)
2022 strcat (own_buf
, ";QStartNoAckMode+");
2024 if (the_target
->qxfer_osdata
!= NULL
)
2025 strcat (own_buf
, ";qXfer:osdata:read+");
2027 if (target_supports_multi_process ())
2028 strcat (own_buf
, ";multiprocess+");
2030 if (target_supports_non_stop ())
2031 strcat (own_buf
, ";QNonStop+");
2033 if (target_supports_disable_randomization ())
2034 strcat (own_buf
, ";QDisableRandomization+");
2036 strcat (own_buf
, ";qXfer:threads:read+");
2038 if (target_supports_tracepoints ())
2040 strcat (own_buf
, ";ConditionalTracepoints+");
2041 strcat (own_buf
, ";TraceStateVariables+");
2042 strcat (own_buf
, ";TracepointSource+");
2043 strcat (own_buf
, ";DisconnectedTracing+");
2044 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
2045 strcat (own_buf
, ";FastTracepoints+");
2046 strcat (own_buf
, ";StaticTracepoints+");
2047 strcat (own_buf
, ";InstallInTrace+");
2048 strcat (own_buf
, ";qXfer:statictrace:read+");
2049 strcat (own_buf
, ";qXfer:traceframe-info:read+");
2050 strcat (own_buf
, ";EnableDisableTracepoints+");
2051 strcat (own_buf
, ";QTBuffer:size+");
2052 strcat (own_buf
, ";tracenz+");
2055 /* Support target-side breakpoint conditions and commands. */
2056 strcat (own_buf
, ";ConditionalBreakpoints+");
2057 strcat (own_buf
, ";BreakpointCommands+");
2059 if (target_supports_agent ())
2060 strcat (own_buf
, ";QAgent+");
2062 supported_btrace_packets (own_buf
);
2067 /* Thread-local storage support. */
2068 if (the_target
->get_tls_address
!= NULL
2069 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
2071 char *p
= own_buf
+ 12;
2072 CORE_ADDR parts
[2], address
= 0;
2074 ptid_t ptid
= null_ptid
;
2076 require_running (own_buf
);
2078 for (i
= 0; i
< 3; i
++)
2086 p2
= strchr (p
, ',');
2099 ptid
= read_ptid (p
, NULL
);
2101 decode_address (&parts
[i
- 1], p
, len
);
2105 if (p
!= NULL
|| i
< 3)
2109 struct thread_info
*thread
= find_thread_ptid (ptid
);
2114 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
2120 strcpy (own_buf
, paddress(address
));
2125 write_enn (own_buf
);
2129 /* Otherwise, pretend we do not understand this packet. */
2132 /* Windows OS Thread Information Block address support. */
2133 if (the_target
->get_tib_address
!= NULL
2134 && strncmp ("qGetTIBAddr:", own_buf
, 12) == 0)
2139 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
2141 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
2144 strcpy (own_buf
, paddress(tlb
));
2149 write_enn (own_buf
);
2155 /* Handle "monitor" commands. */
2156 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
2158 char *mon
= malloc (PBUFSIZ
);
2159 int len
= strlen (own_buf
+ 6);
2163 write_enn (own_buf
);
2168 || hex2bin (own_buf
+ 6, (gdb_byte
*) mon
, len
/ 2) != len
/ 2)
2170 write_enn (own_buf
);
2174 mon
[len
/ 2] = '\0';
2178 if (the_target
->handle_monitor_command
== NULL
2179 || (*the_target
->handle_monitor_command
) (mon
) == 0)
2180 /* Default processing. */
2181 handle_monitor_command (mon
, own_buf
);
2187 if (strncmp ("qSearch:memory:", own_buf
,
2188 sizeof ("qSearch:memory:") - 1) == 0)
2190 require_running (own_buf
);
2191 handle_search_memory (own_buf
, packet_len
);
2195 if (strcmp (own_buf
, "qAttached") == 0
2196 || strncmp (own_buf
, "qAttached:", sizeof ("qAttached:") - 1) == 0)
2198 struct process_info
*process
;
2200 if (own_buf
[sizeof ("qAttached") - 1])
2202 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
2203 process
= (struct process_info
*)
2204 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
2208 require_running (own_buf
);
2209 process
= current_process ();
2212 if (process
== NULL
)
2214 write_enn (own_buf
);
2218 strcpy (own_buf
, process
->attached
? "1" : "0");
2222 if (strncmp ("qCRC:", own_buf
, 5) == 0)
2224 /* CRC check (compare-section). */
2228 unsigned long long crc
;
2230 require_running (own_buf
);
2231 comma
= unpack_varlen_hex (own_buf
+ 5, &base
);
2232 if (*comma
++ != ',')
2234 write_enn (own_buf
);
2237 len
= strtoul (comma
, NULL
, 16);
2238 crc
= crc32 (base
, len
, 0xffffffff);
2239 /* Check for memory failure. */
2240 if (crc
== (unsigned long long) -1)
2242 write_enn (own_buf
);
2245 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
2249 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
2252 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
2255 /* Otherwise we didn't know what packet it was. Say we didn't
2260 static void gdb_wants_all_threads_stopped (void);
2261 static void resume (struct thread_resume
*actions
, size_t n
);
2263 /* The callback that is passed to visit_actioned_threads. */
2264 typedef int (visit_actioned_threads_callback_ftype
)
2265 (const struct thread_resume
*, struct thread_info
*);
2267 /* Struct to pass data to visit_actioned_threads. */
2269 struct visit_actioned_threads_data
2271 const struct thread_resume
*actions
;
2273 visit_actioned_threads_callback_ftype
*callback
;
2276 /* Call CALLBACK for any thread to which ACTIONS applies to. Returns
2277 true if CALLBACK returns true. Returns false if no matching thread
2278 is found or CALLBACK results false.
2279 Note: This function is itself a callback for find_inferior. */
2282 visit_actioned_threads (struct inferior_list_entry
*entry
, void *datap
)
2284 struct visit_actioned_threads_data
*data
= datap
;
2285 const struct thread_resume
*actions
= data
->actions
;
2286 size_t num_actions
= data
->num_actions
;
2287 visit_actioned_threads_callback_ftype
*callback
= data
->callback
;
2290 for (i
= 0; i
< num_actions
; i
++)
2292 const struct thread_resume
*action
= &actions
[i
];
2294 if (ptid_equal (action
->thread
, minus_one_ptid
)
2295 || ptid_equal (action
->thread
, entry
->id
)
2296 || ((ptid_get_pid (action
->thread
)
2297 == ptid_get_pid (entry
->id
))
2298 && ptid_get_lwp (action
->thread
) == -1))
2300 struct thread_info
*thread
= (struct thread_info
*) entry
;
2302 if ((*callback
) (action
, thread
))
2310 /* Callback for visit_actioned_threads. If the thread has a pending
2311 status to report, report it now. */
2314 handle_pending_status (const struct thread_resume
*resumption
,
2315 struct thread_info
*thread
)
2317 if (thread
->status_pending_p
)
2319 thread
->status_pending_p
= 0;
2321 last_status
= thread
->last_status
;
2322 last_ptid
= thread
->entry
.id
;
2323 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2329 /* Parse vCont packets. */
2331 handle_v_cont (char *own_buf
)
2335 struct thread_resume
*resume_info
;
2336 struct thread_resume default_action
= {{0}};
2338 /* Count the number of semicolons in the packet. There should be one
2339 for every action. */
2345 p
= strchr (p
, ';');
2348 resume_info
= malloc (n
* sizeof (resume_info
[0]));
2349 if (resume_info
== NULL
)
2357 memset (&resume_info
[i
], 0, sizeof resume_info
[i
]);
2359 if (p
[0] == 's' || p
[0] == 'S')
2360 resume_info
[i
].kind
= resume_step
;
2361 else if (p
[0] == 'r')
2362 resume_info
[i
].kind
= resume_step
;
2363 else if (p
[0] == 'c' || p
[0] == 'C')
2364 resume_info
[i
].kind
= resume_continue
;
2365 else if (p
[0] == 't')
2366 resume_info
[i
].kind
= resume_stop
;
2370 if (p
[0] == 'S' || p
[0] == 'C')
2373 sig
= strtol (p
+ 1, &q
, 16);
2378 if (!gdb_signal_to_host_p (sig
))
2380 resume_info
[i
].sig
= gdb_signal_to_host (sig
);
2382 else if (p
[0] == 'r')
2386 p
= unpack_varlen_hex (p
+ 1, &addr
);
2387 resume_info
[i
].step_range_start
= addr
;
2392 p
= unpack_varlen_hex (p
+ 1, &addr
);
2393 resume_info
[i
].step_range_end
= addr
;
2402 resume_info
[i
].thread
= minus_one_ptid
;
2403 default_action
= resume_info
[i
];
2405 /* Note: we don't increment i here, we'll overwrite this entry
2406 the next time through. */
2408 else if (p
[0] == ':')
2410 ptid_t ptid
= read_ptid (p
+ 1, &q
);
2415 if (p
[0] != ';' && p
[0] != 0)
2418 resume_info
[i
].thread
= ptid
;
2425 resume_info
[i
] = default_action
;
2427 set_desired_thread (0);
2429 resume (resume_info
, n
);
2434 write_enn (own_buf
);
2439 /* Resume target with ACTIONS, an array of NUM_ACTIONS elements. */
2442 resume (struct thread_resume
*actions
, size_t num_actions
)
2446 /* Check if among the threads that GDB wants actioned, there's
2447 one with a pending status to report. If so, skip actually
2448 resuming/stopping and report the pending event
2450 struct visit_actioned_threads_data data
;
2452 data
.actions
= actions
;
2453 data
.num_actions
= num_actions
;
2454 data
.callback
= handle_pending_status
;
2455 if (find_inferior (&all_threads
, visit_actioned_threads
, &data
) != NULL
)
2461 (*the_target
->resume
) (actions
, num_actions
);
2467 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2469 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
)
2471 /* No proper RSP support for this yet. At least return
2473 sprintf (own_buf
, "E.No unwaited-for children left.");
2474 disable_async_io ();
2478 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2479 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
2480 && last_status
.kind
!= TARGET_WAITKIND_NO_RESUMED
)
2481 current_thread
->last_status
= last_status
;
2483 /* From the client's perspective, all-stop mode always stops all
2484 threads implicitly (and the target backend has already done
2485 so by now). Tag all threads as "want-stopped", so we don't
2486 resume them implicitly without the client telling us to. */
2487 gdb_wants_all_threads_stopped ();
2488 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2489 disable_async_io ();
2491 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2492 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2493 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2497 /* Attach to a new program. Return 1 if successful, 0 if failure. */
2499 handle_v_attach (char *own_buf
)
2503 pid
= strtol (own_buf
+ 8, NULL
, 16);
2504 if (pid
!= 0 && attach_inferior (pid
) == 0)
2506 /* Don't report shared library events after attaching, even if
2507 some libraries are preloaded. GDB will always poll the
2508 library list. Avoids the "stopped by shared library event"
2509 notice on the GDB side. */
2514 /* In non-stop, we don't send a resume reply. Stop events
2515 will follow up using the normal notification
2520 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2526 write_enn (own_buf
);
2531 /* Run a new program. Return 1 if successful, 0 if failure. */
2533 handle_v_run (char *own_buf
)
2535 char *p
, *next_p
, **new_argv
;
2539 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
2545 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
2546 if (new_argv
== NULL
)
2548 write_enn (own_buf
);
2553 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
2555 next_p
= strchr (p
, ';');
2557 next_p
= p
+ strlen (p
);
2559 if (i
== 0 && p
== next_p
)
2563 /* FIXME: Fail request if out of memory instead of dying. */
2564 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
2565 hex2bin (p
, (gdb_byte
*) new_argv
[i
], (next_p
- p
) / 2);
2566 new_argv
[i
][(next_p
- p
) / 2] = '\0';
2575 if (new_argv
[0] == NULL
)
2577 /* GDB didn't specify a program to run. Use the program from the
2578 last run with the new argument list. */
2580 if (program_argv
== NULL
)
2582 write_enn (own_buf
);
2583 freeargv (new_argv
);
2587 new_argv
[0] = strdup (program_argv
[0]);
2588 if (new_argv
[0] == NULL
)
2590 write_enn (own_buf
);
2591 freeargv (new_argv
);
2596 /* Free the old argv and install the new one. */
2597 freeargv (program_argv
);
2598 program_argv
= new_argv
;
2600 start_inferior (program_argv
);
2601 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
2603 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2605 /* In non-stop, sending a resume reply doesn't set the general
2606 thread, but GDB assumes a vRun sets it (this is so GDB can
2607 query which is the main thread of the new inferior. */
2609 general_thread
= last_ptid
;
2615 write_enn (own_buf
);
2620 /* Kill process. Return 1 if successful, 0 if failure. */
2622 handle_v_kill (char *own_buf
)
2625 char *p
= &own_buf
[6];
2627 pid
= strtol (p
, NULL
, 16);
2630 if (pid
!= 0 && kill_inferior (pid
) == 0)
2632 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2633 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
2634 last_ptid
= pid_to_ptid (pid
);
2635 discard_queued_stop_replies (pid
);
2641 write_enn (own_buf
);
2646 /* Handle all of the extended 'v' packets. */
2648 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2650 if (!disable_packet_vCont
)
2652 if (strncmp (own_buf
, "vCont;", 6) == 0)
2654 require_running (own_buf
);
2655 handle_v_cont (own_buf
);
2659 if (strncmp (own_buf
, "vCont?", 6) == 0)
2661 strcpy (own_buf
, "vCont;c;C;s;S;t");
2662 if (target_supports_range_stepping ())
2664 own_buf
= own_buf
+ strlen (own_buf
);
2665 strcpy (own_buf
, ";r");
2671 if (strncmp (own_buf
, "vFile:", 6) == 0
2672 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2675 if (strncmp (own_buf
, "vAttach;", 8) == 0)
2677 if ((!extended_protocol
|| !multi_process
) && target_running ())
2679 fprintf (stderr
, "Already debugging a process\n");
2680 write_enn (own_buf
);
2683 handle_v_attach (own_buf
);
2687 if (strncmp (own_buf
, "vRun;", 5) == 0)
2689 if ((!extended_protocol
|| !multi_process
) && target_running ())
2691 fprintf (stderr
, "Already debugging a process\n");
2692 write_enn (own_buf
);
2695 handle_v_run (own_buf
);
2699 if (strncmp (own_buf
, "vKill;", 6) == 0)
2701 if (!target_running ())
2703 fprintf (stderr
, "No process to kill\n");
2704 write_enn (own_buf
);
2707 handle_v_kill (own_buf
);
2711 if (handle_notif_ack (own_buf
, packet_len
))
2714 /* Otherwise we didn't know what packet it was. Say we didn't
2720 /* Resume thread and wait for another event. In non-stop mode,
2721 don't really wait here, but return immediatelly to the event
2724 myresume (char *own_buf
, int step
, int sig
)
2726 struct thread_resume resume_info
[2];
2728 int valid_cont_thread
;
2730 set_desired_thread (0);
2732 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2733 && !ptid_equal (cont_thread
, minus_one_ptid
));
2735 if (step
|| sig
|| valid_cont_thread
)
2737 resume_info
[0].thread
= current_ptid
;
2739 resume_info
[0].kind
= resume_step
;
2741 resume_info
[0].kind
= resume_continue
;
2742 resume_info
[0].sig
= sig
;
2746 if (!valid_cont_thread
)
2748 resume_info
[n
].thread
= minus_one_ptid
;
2749 resume_info
[n
].kind
= resume_continue
;
2750 resume_info
[n
].sig
= 0;
2754 resume (resume_info
, n
);
2757 /* Callback for for_each_inferior. Make a new stop reply for each
2761 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2763 struct thread_info
*thread
= (struct thread_info
*) entry
;
2765 /* For now, assume targets that don't have this callback also don't
2766 manage the thread's last_status field. */
2767 if (the_target
->thread_stopped
== NULL
)
2769 struct vstop_notif
*new_notif
= xmalloc (sizeof (*new_notif
));
2771 new_notif
->ptid
= entry
->id
;
2772 new_notif
->status
= thread
->last_status
;
2773 /* Pass the last stop reply back to GDB, but don't notify
2775 notif_event_enque (¬if_stop
,
2776 (struct notif_event
*) new_notif
);
2780 if (thread_stopped (thread
))
2785 = target_waitstatus_to_string (&thread
->last_status
);
2787 debug_printf ("Reporting thread %s as already stopped with %s\n",
2788 target_pid_to_str (entry
->id
),
2791 xfree (status_string
);
2794 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2796 /* Pass the last stop reply back to GDB, but don't notify
2798 queue_stop_reply (entry
->id
, &thread
->last_status
);
2805 /* Set this inferior threads's state as "want-stopped". We won't
2806 resume this thread until the client gives us another action for
2810 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2812 struct thread_info
*thread
= (struct thread_info
*) entry
;
2814 thread
->last_resume_kind
= resume_stop
;
2816 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2818 /* Most threads are stopped implicitly (all-stop); tag that with
2820 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2821 thread
->last_status
.value
.sig
= GDB_SIGNAL_0
;
2825 /* Set all threads' states as "want-stopped". */
2828 gdb_wants_all_threads_stopped (void)
2830 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2833 /* Clear the gdb_detached flag of every process. */
2836 gdb_reattached_process (struct inferior_list_entry
*entry
)
2838 struct process_info
*process
= (struct process_info
*) entry
;
2840 process
->gdb_detached
= 0;
2843 /* Callback for for_each_inferior. Clear the thread's pending status
2847 clear_pending_status_callback (struct inferior_list_entry
*entry
)
2849 struct thread_info
*thread
= (struct thread_info
*) entry
;
2851 thread
->status_pending_p
= 0;
2854 /* Callback for for_each_inferior. If the thread is stopped with an
2855 interesting event, mark it as having a pending event. */
2858 set_pending_status_callback (struct inferior_list_entry
*entry
)
2860 struct thread_info
*thread
= (struct thread_info
*) entry
;
2862 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
2863 || (thread
->last_status
.value
.sig
!= GDB_SIGNAL_0
2864 /* A breakpoint, watchpoint or finished step from a previous
2865 GDB run isn't considered interesting for a new GDB run.
2866 If we left those pending, the new GDB could consider them
2867 random SIGTRAPs. This leaves out real async traps. We'd
2868 have to peek into the (target-specific) siginfo to
2869 distinguish those. */
2870 && thread
->last_status
.value
.sig
!= GDB_SIGNAL_TRAP
))
2871 thread
->status_pending_p
= 1;
2874 /* Callback for find_inferior. Return true if ENTRY (a thread) has a
2875 pending status to report to GDB. */
2878 find_status_pending_thread_callback (struct inferior_list_entry
*entry
, void *data
)
2880 struct thread_info
*thread
= (struct thread_info
*) entry
;
2882 return thread
->status_pending_p
;
2885 /* Status handler for the '?' packet. */
2888 handle_status (char *own_buf
)
2890 /* GDB is connected, don't forward events to the target anymore. */
2891 for_each_inferior (&all_processes
, gdb_reattached_process
);
2893 /* In non-stop mode, we must send a stop reply for each stopped
2894 thread. In all-stop mode, just send one for the first stopped
2899 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2901 /* The first is sent immediatly. OK is sent if there is no
2902 stopped thread, which is the same handling of the vStopped
2903 packet (by design). */
2904 notif_write_event (¬if_stop
, own_buf
);
2908 struct inferior_list_entry
*thread
= NULL
;
2911 stabilize_threads ();
2912 gdb_wants_all_threads_stopped ();
2914 /* We can only report one status, but we might be coming out of
2915 non-stop -- if more than one thread is stopped with
2916 interesting events, leave events for the threads we're not
2917 reporting now pending. They'll be reported the next time the
2918 threads are resumed. Start by marking all interesting events
2920 for_each_inferior (&all_threads
, set_pending_status_callback
);
2922 /* Prefer the last thread that reported an event to GDB (even if
2923 that was a GDB_SIGNAL_TRAP). */
2924 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
2925 && last_status
.kind
!= TARGET_WAITKIND_EXITED
2926 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2927 thread
= find_inferior_id (&all_threads
, last_ptid
);
2929 /* If the last event thread is not found for some reason, look
2930 for some other thread that might have an event to report. */
2932 thread
= find_inferior (&all_threads
,
2933 find_status_pending_thread_callback
, NULL
);
2935 /* If we're still out of luck, simply pick the first thread in
2938 thread
= get_first_inferior (&all_threads
);
2942 struct thread_info
*tp
= (struct thread_info
*) thread
;
2944 /* We're reporting this event, so it's no longer
2946 tp
->status_pending_p
= 0;
2948 /* GDB assumes the current thread is the thread we're
2949 reporting the status for. */
2950 general_thread
= thread
->id
;
2951 set_desired_thread (1);
2953 gdb_assert (tp
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2954 prepare_resume_reply (own_buf
, tp
->entry
.id
, &tp
->last_status
);
2957 strcpy (own_buf
, "W00");
2962 gdbserver_version (void)
2964 printf ("GNU gdbserver %s%s\n"
2965 "Copyright (C) 2015 Free Software Foundation, Inc.\n"
2966 "gdbserver is free software, covered by the "
2967 "GNU General Public License.\n"
2968 "This gdbserver was configured as \"%s\"\n",
2969 PKGVERSION
, version
, host_name
);
2973 gdbserver_usage (FILE *stream
)
2975 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2976 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2977 "\tgdbserver [OPTIONS] --multi COMM\n"
2979 "COMM may either be a tty device (for serial debugging), or \n"
2980 "HOST:PORT to listen for a TCP connection.\n"
2983 " --debug Enable general debugging output.\n"
2984 " --debug-format=opt1[,opt2,...]\n"
2985 " Specify extra content in debugging output.\n"
2990 " --remote-debug Enable remote protocol debugging output.\n"
2991 " --version Display version information and exit.\n"
2992 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
2993 " --once Exit after the first connection has "
2995 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
2996 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
3000 gdbserver_show_disableable (FILE *stream
)
3002 fprintf (stream
, "Disableable packets:\n"
3003 " vCont \tAll vCont packets\n"
3004 " qC \tQuerying the current thread\n"
3005 " qfThreadInfo\tThread listing\n"
3006 " Tthread \tPassing the thread specifier in the "
3007 "T stop reply packet\n"
3008 " threads \tAll of the above\n");
3012 #undef require_running
3013 #define require_running(BUF) \
3014 if (!target_running ()) \
3021 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
3023 int pid
= * (int *) args
;
3025 if (ptid_get_pid (entry
->id
) == pid
)
3032 kill_inferior_callback (struct inferior_list_entry
*entry
)
3034 struct process_info
*process
= (struct process_info
*) entry
;
3035 int pid
= ptid_get_pid (process
->entry
.id
);
3037 kill_inferior (pid
);
3038 discard_queued_stop_replies (pid
);
3041 /* Callback for for_each_inferior to detach or kill the inferior,
3042 depending on whether we attached to it or not.
3043 We inform the user whether we're detaching or killing the process
3044 as this is only called when gdbserver is about to exit. */
3047 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
3049 struct process_info
*process
= (struct process_info
*) entry
;
3050 int pid
= ptid_get_pid (process
->entry
.id
);
3052 if (process
->attached
)
3053 detach_inferior (pid
);
3055 kill_inferior (pid
);
3057 discard_queued_stop_replies (pid
);
3060 /* for_each_inferior callback for detach_or_kill_for_exit to print
3061 the pids of started inferiors. */
3064 print_started_pid (struct inferior_list_entry
*entry
)
3066 struct process_info
*process
= (struct process_info
*) entry
;
3068 if (! process
->attached
)
3070 int pid
= ptid_get_pid (process
->entry
.id
);
3071 fprintf (stderr
, " %d", pid
);
3075 /* for_each_inferior callback for detach_or_kill_for_exit to print
3076 the pids of attached inferiors. */
3079 print_attached_pid (struct inferior_list_entry
*entry
)
3081 struct process_info
*process
= (struct process_info
*) entry
;
3083 if (process
->attached
)
3085 int pid
= ptid_get_pid (process
->entry
.id
);
3086 fprintf (stderr
, " %d", pid
);
3090 /* Call this when exiting gdbserver with possible inferiors that need
3091 to be killed or detached from. */
3094 detach_or_kill_for_exit (void)
3096 /* First print a list of the inferiors we will be killing/detaching.
3097 This is to assist the user, for example, in case the inferior unexpectedly
3098 dies after we exit: did we screw up or did the inferior exit on its own?
3099 Having this info will save some head-scratching. */
3101 if (have_started_inferiors_p ())
3103 fprintf (stderr
, "Killing process(es):");
3104 for_each_inferior (&all_processes
, print_started_pid
);
3105 fprintf (stderr
, "\n");
3107 if (have_attached_inferiors_p ())
3109 fprintf (stderr
, "Detaching process(es):");
3110 for_each_inferior (&all_processes
, print_attached_pid
);
3111 fprintf (stderr
, "\n");
3114 /* Now we can kill or detach the inferiors. */
3116 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
3119 /* Value that will be passed to exit(3) when gdbserver exits. */
3120 static int exit_code
;
3122 /* Cleanup version of detach_or_kill_for_exit. */
3125 detach_or_kill_for_exit_cleanup (void *ignore
)
3127 volatile struct gdb_exception exception
;
3129 TRY_CATCH (exception
, RETURN_MASK_ALL
)
3131 detach_or_kill_for_exit ();
3134 if (exception
.reason
< 0)
3137 fprintf (stderr
, "Detach or kill failed: %s\n", exception
.message
);
3142 /* Main function. This is called by the real "main" function,
3143 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
3145 static void ATTRIBUTE_NORETURN
3146 captured_main (int argc
, char *argv
[])
3150 char *arg_end
, *port
;
3151 char **next_arg
= &argv
[1];
3152 volatile int multi_mode
= 0;
3153 volatile int attach
= 0;
3156 while (*next_arg
!= NULL
&& **next_arg
== '-')
3158 if (strcmp (*next_arg
, "--version") == 0)
3160 gdbserver_version ();
3163 else if (strcmp (*next_arg
, "--help") == 0)
3165 gdbserver_usage (stdout
);
3168 else if (strcmp (*next_arg
, "--attach") == 0)
3170 else if (strcmp (*next_arg
, "--multi") == 0)
3172 else if (strcmp (*next_arg
, "--wrapper") == 0)
3176 wrapper_argv
= next_arg
;
3177 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
3180 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
3182 gdbserver_usage (stderr
);
3186 /* Consume the "--". */
3189 else if (strcmp (*next_arg
, "--debug") == 0)
3191 else if (strncmp (*next_arg
,
3193 sizeof ("--debug-format=") - 1) == 0)
3196 = parse_debug_format_options ((*next_arg
)
3197 + sizeof ("--debug-format=") - 1, 0);
3199 if (error_msg
!= NULL
)
3201 fprintf (stderr
, "%s", error_msg
);
3205 else if (strcmp (*next_arg
, "--remote-debug") == 0)
3207 else if (strcmp (*next_arg
, "--disable-packet") == 0)
3209 gdbserver_show_disableable (stdout
);
3212 else if (strncmp (*next_arg
,
3213 "--disable-packet=",
3214 sizeof ("--disable-packet=") - 1) == 0)
3216 char *packets
, *tok
;
3218 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
3219 for (tok
= strtok (packets
, ",");
3221 tok
= strtok (NULL
, ","))
3223 if (strcmp ("vCont", tok
) == 0)
3224 disable_packet_vCont
= 1;
3225 else if (strcmp ("Tthread", tok
) == 0)
3226 disable_packet_Tthread
= 1;
3227 else if (strcmp ("qC", tok
) == 0)
3228 disable_packet_qC
= 1;
3229 else if (strcmp ("qfThreadInfo", tok
) == 0)
3230 disable_packet_qfThreadInfo
= 1;
3231 else if (strcmp ("threads", tok
) == 0)
3233 disable_packet_vCont
= 1;
3234 disable_packet_Tthread
= 1;
3235 disable_packet_qC
= 1;
3236 disable_packet_qfThreadInfo
= 1;
3240 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
3242 gdbserver_show_disableable (stderr
);
3247 else if (strcmp (*next_arg
, "-") == 0)
3249 /* "-" specifies a stdio connection and is a form of port
3251 *next_arg
= STDIO_CONNECTION_NAME
;
3254 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
3255 disable_randomization
= 1;
3256 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
3257 disable_randomization
= 0;
3258 else if (strcmp (*next_arg
, "--once") == 0)
3262 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
3272 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
3274 gdbserver_usage (stderr
);
3278 /* Remember stdio descriptors. LISTEN_DESC must not be listed, it will be
3279 opened by remote_prepare. */
3282 /* We need to know whether the remote connection is stdio before
3283 starting the inferior. Inferiors created in this scenario have
3284 stdin,stdout redirected. So do this here before we call
3286 remote_prepare (port
);
3291 /* --attach used to come after PORT, so allow it there for
3293 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
3300 && (*next_arg
== NULL
3301 || (*next_arg
)[0] == '\0'
3302 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
3304 || next_arg
[1] != NULL
))
3309 gdbserver_usage (stderr
);
3313 initialize_async_io ();
3315 initialize_event_loop ();
3316 if (target_supports_tracepoints ())
3317 initialize_tracepoint ();
3319 own_buf
= xmalloc (PBUFSIZ
+ 1);
3320 mem_buf
= xmalloc (PBUFSIZ
);
3322 if (pid
== 0 && *next_arg
!= NULL
)
3326 n
= argc
- (next_arg
- argv
);
3327 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
3328 for (i
= 0; i
< n
; i
++)
3329 program_argv
[i
] = xstrdup (next_arg
[i
]);
3330 program_argv
[i
] = NULL
;
3332 /* Wait till we are at first instruction in program. */
3333 start_inferior (program_argv
);
3335 /* We are now (hopefully) stopped at the first instruction of
3336 the target process. This assumes that the target process was
3337 successfully created. */
3341 if (attach_inferior (pid
) == -1)
3342 error ("Attaching not supported on this target");
3344 /* Otherwise succeeded. */
3348 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3349 last_status
.value
.integer
= 0;
3350 last_ptid
= minus_one_ptid
;
3352 make_cleanup (detach_or_kill_for_exit_cleanup
, NULL
);
3354 initialize_notif ();
3356 /* Don't report shared library events on the initial connection,
3357 even if some libraries are preloaded. Avoids the "stopped by
3358 shared library event" notice on gdb side. */
3361 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3362 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3367 if (!was_running
&& !multi_mode
)
3368 error ("No program to debug");
3372 volatile struct gdb_exception exception
;
3376 /* Be sure we're out of tfind mode. */
3377 current_traceframe
= -1;
3378 cont_thread
= null_ptid
;
3382 TRY_CATCH (exception
, RETURN_MASK_ERROR
)
3384 /* Wait for events. This will return when all event sources
3385 are removed from the event loop. */
3386 start_event_loop ();
3388 /* If an exit was requested (using the "monitor exit"
3389 command), terminate now. The only other way to get
3390 here is for getpkt to fail; close the connection
3391 and reopen it at the top of the loop. */
3393 if (exit_requested
|| run_once
)
3394 throw_quit ("Quit");
3397 "Remote side has terminated connection. "
3398 "GDBserver will reopen the connection.\n");
3400 /* Get rid of any pending statuses. An eventual reconnection
3401 (by the same GDB instance or another) will refresh all its
3402 state from scratch. */
3403 discard_queued_stop_replies (-1);
3404 for_each_inferior (&all_threads
,
3405 clear_pending_status_callback
);
3409 if (disconnected_tracing
)
3411 /* Try to enable non-stop/async mode, so we we can
3412 both wait for an async socket accept, and handle
3413 async target events simultaneously. There's also
3414 no point either in having the target always stop
3415 all threads, when we're going to pass signals
3416 down without informing GDB. */
3419 if (start_non_stop (1))
3422 /* Detaching implicitly resumes all threads;
3423 simply disconnecting does not. */
3429 "Disconnected tracing disabled; "
3430 "stopping trace run.\n");
3436 if (exception
.reason
== RETURN_ERROR
)
3438 if (response_needed
)
3440 write_enn (own_buf
);
3447 /* Main function. */
3450 main (int argc
, char *argv
[])
3452 volatile struct gdb_exception exception
;
3454 TRY_CATCH (exception
, RETURN_MASK_ALL
)
3456 captured_main (argc
, argv
);
3459 /* captured_main should never return. */
3460 gdb_assert (exception
.reason
< 0);
3462 if (exception
.reason
== RETURN_ERROR
)
3465 fprintf (stderr
, "%s\n", exception
.message
);
3466 fprintf (stderr
, "Exiting\n");
3473 /* Skip PACKET until the next semi-colon (or end of string). */
3476 skip_to_semicolon (char **packet
)
3478 while (**packet
!= '\0' && **packet
!= ';')
3482 /* Process options coming from Z packets for a breakpoint. PACKET is
3483 the packet buffer. *PACKET is updated to point to the first char
3484 after the last processed option. */
3487 process_point_options (struct breakpoint
*bp
, char **packet
)
3489 char *dataptr
= *packet
;
3492 /* Check if data has the correct format. */
3493 if (*dataptr
!= ';')
3500 if (*dataptr
== ';')
3503 if (*dataptr
== 'X')
3505 /* Conditional expression. */
3507 debug_printf ("Found breakpoint condition.\n");
3508 if (!add_breakpoint_condition (bp
, &dataptr
))
3509 skip_to_semicolon (&dataptr
);
3511 else if (strncmp (dataptr
, "cmds:", strlen ("cmds:")) == 0)
3513 dataptr
+= strlen ("cmds:");
3515 debug_printf ("Found breakpoint commands %s.\n", dataptr
);
3516 persist
= (*dataptr
== '1');
3518 if (add_breakpoint_commands (bp
, &dataptr
, persist
))
3519 skip_to_semicolon (&dataptr
);
3523 fprintf (stderr
, "Unknown token %c, ignoring.\n",
3525 /* Skip tokens until we find one that we recognize. */
3526 skip_to_semicolon (&dataptr
);
3532 /* Event loop callback that handles a serial event. The first byte in
3533 the serial buffer gets us here. We expect characters to arrive at
3534 a brisk pace, so we read the rest of the packet with a blocking
3538 process_serial_event (void)
3549 int new_packet_len
= -1;
3551 /* Used to decide when gdbserver should exit in
3552 multi-mode/remote. */
3553 static int have_ran
= 0;
3556 have_ran
= target_running ();
3558 disable_async_io ();
3560 response_needed
= 0;
3561 packet_len
= getpkt (own_buf
);
3562 if (packet_len
<= 0)
3565 /* Force an event loop break. */
3568 response_needed
= 1;
3575 handle_query (own_buf
, packet_len
, &new_packet_len
);
3578 handle_general_set (own_buf
);
3581 require_running (own_buf
);
3586 pid
= strtol (&own_buf
[i
], NULL
, 16);
3589 pid
= ptid_get_pid (current_ptid
);
3591 if ((tracing
&& disconnected_tracing
) || any_persistent_commands ())
3593 struct thread_resume resume_info
;
3594 struct process_info
*process
= find_process_pid (pid
);
3596 if (process
== NULL
)
3598 write_enn (own_buf
);
3602 if (tracing
&& disconnected_tracing
)
3604 "Disconnected tracing in effect, "
3605 "leaving gdbserver attached to the process\n");
3607 if (any_persistent_commands ())
3609 "Persistent commands are present, "
3610 "leaving gdbserver attached to the process\n");
3612 /* Make sure we're in non-stop/async mode, so we we can both
3613 wait for an async socket accept, and handle async target
3614 events simultaneously. There's also no point either in
3615 having the target stop all threads, when we're going to
3616 pass signals down without informing GDB. */
3620 debug_printf ("Forcing non-stop mode\n");
3626 process
->gdb_detached
= 1;
3628 /* Detaching implicitly resumes all threads. */
3629 resume_info
.thread
= minus_one_ptid
;
3630 resume_info
.kind
= resume_continue
;
3631 resume_info
.sig
= 0;
3632 (*the_target
->resume
) (&resume_info
, 1);
3635 break; /* from switch/case */
3638 fprintf (stderr
, "Detaching from process %d\n", pid
);
3640 if (detach_inferior (pid
) != 0)
3641 write_enn (own_buf
);
3644 discard_queued_stop_replies (pid
);
3647 if (extended_protocol
)
3649 /* Treat this like a normal program exit. */
3650 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3651 last_status
.value
.integer
= 0;
3652 last_ptid
= pid_to_ptid (pid
);
3654 current_thread
= NULL
;
3661 /* If we are attached, then we can exit. Otherwise, we
3662 need to hang around doing nothing, until the child is
3664 join_inferior (pid
);
3670 extended_protocol
= 1;
3674 handle_status (own_buf
);
3677 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
3679 ptid_t gdb_id
, thread_id
;
3682 require_running (own_buf
);
3684 gdb_id
= read_ptid (&own_buf
[2], NULL
);
3686 pid
= ptid_get_pid (gdb_id
);
3688 if (ptid_equal (gdb_id
, null_ptid
)
3689 || ptid_equal (gdb_id
, minus_one_ptid
))
3690 thread_id
= null_ptid
;
3692 && ptid_equal (pid_to_ptid (pid
),
3695 struct thread_info
*thread
=
3696 (struct thread_info
*) find_inferior (&all_threads
,
3701 write_enn (own_buf
);
3705 thread_id
= thread
->entry
.id
;
3709 thread_id
= gdb_id_to_thread_id (gdb_id
);
3710 if (ptid_equal (thread_id
, null_ptid
))
3712 write_enn (own_buf
);
3717 if (own_buf
[1] == 'g')
3719 if (ptid_equal (thread_id
, null_ptid
))
3721 /* GDB is telling us to choose any thread. Check if
3722 the currently selected thread is still valid. If
3723 it is not, select the first available. */
3724 struct thread_info
*thread
=
3725 (struct thread_info
*) find_inferior_id (&all_threads
,
3729 thread
= get_first_thread ();
3730 thread_id
= thread
->entry
.id
;
3734 general_thread
= thread_id
;
3735 set_desired_thread (1);
3737 else if (own_buf
[1] == 'c')
3738 cont_thread
= thread_id
;
3744 /* Silently ignore it so that gdb can extend the protocol
3745 without compatibility headaches. */
3750 require_running (own_buf
);
3751 if (current_traceframe
>= 0)
3753 struct regcache
*regcache
3754 = new_register_cache (current_target_desc ());
3756 if (fetch_traceframe_registers (current_traceframe
,
3758 registers_to_string (regcache
, own_buf
);
3760 write_enn (own_buf
);
3761 free_register_cache (regcache
);
3765 struct regcache
*regcache
;
3767 set_desired_thread (1);
3768 regcache
= get_thread_regcache (current_thread
, 1);
3769 registers_to_string (regcache
, own_buf
);
3773 require_running (own_buf
);
3774 if (current_traceframe
>= 0)
3775 write_enn (own_buf
);
3778 struct regcache
*regcache
;
3780 set_desired_thread (1);
3781 regcache
= get_thread_regcache (current_thread
, 1);
3782 registers_from_string (regcache
, &own_buf
[1]);
3787 require_running (own_buf
);
3788 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
3789 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
3791 write_enn (own_buf
);
3793 bin2hex (mem_buf
, own_buf
, res
);
3796 require_running (own_buf
);
3797 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
3798 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
3801 write_enn (own_buf
);
3804 require_running (own_buf
);
3805 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
3806 &mem_addr
, &len
, &mem_buf
) < 0
3807 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
3808 write_enn (own_buf
);
3813 require_running (own_buf
);
3814 hex2bin (own_buf
+ 1, &sig
, 1);
3815 if (gdb_signal_to_host_p (sig
))
3816 signal
= gdb_signal_to_host (sig
);
3819 myresume (own_buf
, 0, signal
);
3822 require_running (own_buf
);
3823 hex2bin (own_buf
+ 1, &sig
, 1);
3824 if (gdb_signal_to_host_p (sig
))
3825 signal
= gdb_signal_to_host (sig
);
3828 myresume (own_buf
, 1, signal
);
3831 require_running (own_buf
);
3833 myresume (own_buf
, 0, signal
);
3836 require_running (own_buf
);
3838 myresume (own_buf
, 1, signal
);
3840 case 'Z': /* insert_ ... */
3842 case 'z': /* remove_ ... */
3847 char type
= own_buf
[1];
3849 const int insert
= ch
== 'Z';
3850 char *p
= &own_buf
[3];
3852 p
= unpack_varlen_hex (p
, &addr
);
3853 len
= strtol (p
+ 1, &dataptr
, 16);
3857 struct breakpoint
*bp
;
3859 bp
= set_gdb_breakpoint (type
, addr
, len
, &res
);
3864 /* GDB may have sent us a list of *point parameters to
3865 be evaluated on the target's side. Read such list
3866 here. If we already have a list of parameters, GDB
3867 is telling us to drop that list and use this one
3869 clear_breakpoint_conditions_and_commands (bp
);
3870 process_point_options (bp
, &dataptr
);
3874 res
= delete_gdb_breakpoint (type
, addr
, len
);
3882 write_enn (own_buf
);
3886 response_needed
= 0;
3887 if (!target_running ())
3888 /* The packet we received doesn't make sense - but we can't
3889 reply to it, either. */
3892 fprintf (stderr
, "Killing all inferiors\n");
3893 for_each_inferior (&all_processes
, kill_inferior_callback
);
3895 /* When using the extended protocol, we wait with no program
3896 running. The traditional protocol will exit instead. */
3897 if (extended_protocol
)
3899 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3900 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
3908 ptid_t gdb_id
, thread_id
;
3910 require_running (own_buf
);
3912 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3913 thread_id
= gdb_id_to_thread_id (gdb_id
);
3914 if (ptid_equal (thread_id
, null_ptid
))
3916 write_enn (own_buf
);
3920 if (mythread_alive (thread_id
))
3923 write_enn (own_buf
);
3927 response_needed
= 0;
3929 /* Restarting the inferior is only supported in the extended
3931 if (extended_protocol
)
3933 if (target_running ())
3934 for_each_inferior (&all_processes
,
3935 kill_inferior_callback
);
3936 fprintf (stderr
, "GDBserver restarting\n");
3938 /* Wait till we are at 1st instruction in prog. */
3939 if (program_argv
!= NULL
)
3940 start_inferior (program_argv
);
3943 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3944 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
3950 /* It is a request we don't understand. Respond with an
3951 empty packet so that gdb knows that we don't support this
3957 /* Extended (long) request. */
3958 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
3962 /* It is a request we don't understand. Respond with an empty
3963 packet so that gdb knows that we don't support this
3969 if (new_packet_len
!= -1)
3970 putpkt_binary (own_buf
, new_packet_len
);
3974 response_needed
= 0;
3976 if (!extended_protocol
&& have_ran
&& !target_running ())
3978 /* In non-stop, defer exiting until GDB had a chance to query
3979 the whole vStopped list (until it gets an OK). */
3980 if (QUEUE_is_empty (notif_event_p
, notif_stop
.queue
))
3982 /* Be transparent when GDB is connected through stdio -- no
3983 need to spam GDB's console. */
3984 if (!remote_connection_is_stdio ())
3985 fprintf (stderr
, "GDBserver exiting\n");
3997 /* Event-loop callback for serial events. */
4000 handle_serial_event (int err
, gdb_client_data client_data
)
4003 debug_printf ("handling possible serial event\n");
4005 /* Really handle it. */
4006 if (process_serial_event () < 0)
4009 /* Be sure to not change the selected thread behind GDB's back.
4010 Important in the non-stop mode asynchronous protocol. */
4011 set_desired_thread (1);
4016 /* Event-loop callback for target events. */
4019 handle_target_event (int err
, gdb_client_data client_data
)
4022 debug_printf ("handling possible target event\n");
4024 last_ptid
= mywait (minus_one_ptid
, &last_status
,
4027 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
)
4029 /* No RSP support for this yet. */
4031 else if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
4033 int pid
= ptid_get_pid (last_ptid
);
4034 struct process_info
*process
= find_process_pid (pid
);
4035 int forward_event
= !gdb_connected () || process
->gdb_detached
;
4037 if (last_status
.kind
== TARGET_WAITKIND_EXITED
4038 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
4040 mark_breakpoints_out (process
);
4041 mourn_inferior (process
);
4045 /* We're reporting this thread as stopped. Update its
4046 "want-stopped" state to what the client wants, until it
4047 gets a new resume action. */
4048 current_thread
->last_resume_kind
= resume_stop
;
4049 current_thread
->last_status
= last_status
;
4054 if (!target_running ())
4056 /* The last process exited. We're done. */
4060 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
4062 /* A thread stopped with a signal, but gdb isn't
4063 connected to handle it. Pass it down to the
4064 inferior, as if it wasn't being traced. */
4065 struct thread_resume resume_info
;
4068 debug_printf ("GDB not connected; forwarding event %d for"
4070 (int) last_status
.kind
,
4071 target_pid_to_str (last_ptid
));
4073 resume_info
.thread
= last_ptid
;
4074 resume_info
.kind
= resume_continue
;
4075 resume_info
.sig
= gdb_signal_to_host (last_status
.value
.sig
);
4076 (*the_target
->resume
) (&resume_info
, 1);
4078 else if (debug_threads
)
4079 debug_printf ("GDB not connected; ignoring event %d for [%s]\n",
4080 (int) last_status
.kind
,
4081 target_pid_to_str (last_ptid
));
4085 struct vstop_notif
*vstop_notif
4086 = xmalloc (sizeof (struct vstop_notif
));
4088 vstop_notif
->status
= last_status
;
4089 vstop_notif
->ptid
= last_ptid
;
4090 /* Push Stop notification. */
4091 notif_push (¬if_stop
,
4092 (struct notif_event
*) vstop_notif
);
4096 /* Be sure to not change the selected thread behind GDB's back.
4097 Important in the non-stop mode asynchronous protocol. */
4098 set_desired_thread (1);