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. */
60 int report_fork_events
;
61 int report_vfork_events
;
66 /* Whether we should attempt to disable the operating system's address
67 space randomization feature before starting an inferior. */
68 int disable_randomization
= 1;
70 static char **program_argv
, **wrapper_argv
;
72 int pass_signals
[GDB_SIGNAL_LAST
];
73 int program_signals
[GDB_SIGNAL_LAST
];
74 int program_signals_p
;
76 /* The PID of the originally created or attached inferior. Used to
77 send signals to the process when GDB sends us an asynchronous interrupt
78 (user hitting Control-C in the client), and to wait for the child to exit
79 when no longer debugging it. */
81 unsigned long signal_pid
;
84 /* A file descriptor for the controlling terminal. */
87 /* TERMINAL_FD's original foreground group. */
88 pid_t old_foreground_pgrp
;
90 /* Hand back terminal ownership to the original foreground group. */
93 restore_old_foreground_pgrp (void)
95 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
99 /* Set if you want to disable optional thread related packets support
100 in gdbserver, for the sake of testing GDB against stubs that don't
102 int disable_packet_vCont
;
103 int disable_packet_Tthread
;
104 int disable_packet_qC
;
105 int disable_packet_qfThreadInfo
;
107 /* Last status reported to GDB. */
108 static struct target_waitstatus last_status
;
109 static ptid_t last_ptid
;
111 static char *own_buf
;
112 static unsigned char *mem_buf
;
114 /* A sub-class of 'struct notif_event' for stop, holding information
115 relative to a single stop reply. We keep a queue of these to
116 push to GDB in non-stop mode. */
120 struct notif_event base
;
122 /* Thread or process that got the event. */
126 struct target_waitstatus status
;
129 /* The current btrace configuration. This is gdbserver's mirror of GDB's
130 btrace configuration. */
131 static struct btrace_config current_btrace_conf
;
133 DEFINE_QUEUE_P (notif_event_p
);
135 /* Put a stop reply to the stop reply queue. */
138 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
140 struct vstop_notif
*new_notif
= xmalloc (sizeof (*new_notif
));
142 new_notif
->ptid
= ptid
;
143 new_notif
->status
= *status
;
145 notif_event_enque (¬if_stop
, (struct notif_event
*) new_notif
);
149 remove_all_on_match_pid (QUEUE (notif_event_p
) *q
,
150 QUEUE_ITER (notif_event_p
) *iter
,
151 struct notif_event
*event
,
157 || ptid_get_pid (((struct vstop_notif
*) event
)->ptid
) == *pid
)
159 if (q
->free_func
!= NULL
)
160 q
->free_func (event
);
162 QUEUE_remove_elem (notif_event_p
, q
, iter
);
168 /* Get rid of the currently pending stop replies for PID. If PID is
169 -1, then apply to all processes. */
172 discard_queued_stop_replies (int pid
)
174 QUEUE_iterate (notif_event_p
, notif_stop
.queue
,
175 remove_all_on_match_pid
, &pid
);
179 vstop_notif_reply (struct notif_event
*event
, char *own_buf
)
181 struct vstop_notif
*vstop
= (struct vstop_notif
*) event
;
183 prepare_resume_reply (own_buf
, vstop
->ptid
, &vstop
->status
);
186 struct notif_server notif_stop
=
188 "vStopped", "Stop", NULL
, vstop_notif_reply
,
192 target_running (void)
194 return get_first_thread () != NULL
;
198 start_inferior (char **argv
)
200 char **new_argv
= argv
;
202 if (wrapper_argv
!= NULL
)
206 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
208 for (i
= 0; argv
[i
] != NULL
; i
++)
210 new_argv
= alloca (sizeof (char *) * count
);
212 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
213 new_argv
[count
++] = wrapper_argv
[i
];
214 for (i
= 0; argv
[i
] != NULL
; i
++)
215 new_argv
[count
++] = argv
[i
];
216 new_argv
[count
] = NULL
;
222 for (i
= 0; new_argv
[i
]; ++i
)
223 debug_printf ("new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
228 signal (SIGTTOU
, SIG_DFL
);
229 signal (SIGTTIN
, SIG_DFL
);
232 signal_pid
= create_inferior (new_argv
[0], new_argv
);
234 /* FIXME: we don't actually know at this point that the create
235 actually succeeded. We won't know that until we wait. */
236 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
241 signal (SIGTTOU
, SIG_IGN
);
242 signal (SIGTTIN
, SIG_IGN
);
243 terminal_fd
= fileno (stderr
);
244 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
245 tcsetpgrp (terminal_fd
, signal_pid
);
246 atexit (restore_old_foreground_pgrp
);
249 if (wrapper_argv
!= NULL
)
251 struct thread_resume resume_info
;
253 memset (&resume_info
, 0, sizeof (resume_info
));
254 resume_info
.thread
= pid_to_ptid (signal_pid
);
255 resume_info
.kind
= resume_continue
;
258 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
260 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
265 (*the_target
->resume
) (&resume_info
, 1);
267 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
268 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
271 current_thread
->last_resume_kind
= resume_stop
;
272 current_thread
->last_status
= last_status
;
274 while (last_status
.value
.sig
!= GDB_SIGNAL_TRAP
);
279 /* Wait till we are at 1st instruction in program, return new pid
280 (assuming success). */
281 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
283 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
284 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
286 current_thread
->last_resume_kind
= resume_stop
;
287 current_thread
->last_status
= last_status
;
290 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
296 attach_inferior (int pid
)
298 /* myattach should return -1 if attaching is unsupported,
299 0 if it succeeded, and call error() otherwise. */
301 if (myattach (pid
) != 0)
304 fprintf (stderr
, "Attached; pid = %d\n", pid
);
307 /* FIXME - It may be that we should get the SIGNAL_PID from the
308 attach function, so that it can be the main thread instead of
309 whichever we were told to attach to. */
314 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
316 /* GDB knows to ignore the first SIGSTOP after attaching to a running
317 process using the "attach" command, but this is different; it's
318 just using "target remote". Pretend it's just starting up. */
319 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
320 && last_status
.value
.sig
== GDB_SIGNAL_STOP
)
321 last_status
.value
.sig
= GDB_SIGNAL_TRAP
;
323 current_thread
->last_resume_kind
= resume_stop
;
324 current_thread
->last_status
= last_status
;
330 extern int remote_debug
;
332 /* Decode a qXfer read request. Return 0 if everything looks OK,
336 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
338 /* After the read marker and annex, qXfer looks like a
339 traditional 'm' packet. */
340 decode_m_packet (buf
, ofs
, len
);
346 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
348 /* Extract and NUL-terminate the object. */
350 while (*buf
&& *buf
!= ':')
356 /* Extract and NUL-terminate the read/write action. */
358 while (*buf
&& *buf
!= ':')
364 /* Extract and NUL-terminate the annex. */
366 while (*buf
&& *buf
!= ':')
376 /* Write the response to a successful qXfer read. Returns the
377 length of the (binary) data stored in BUF, corresponding
378 to as much of DATA/LEN as we could fit. IS_MORE controls
379 the first character of the response. */
381 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
390 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
394 /* Handle btrace enabling in BTS format. */
397 handle_btrace_enable_bts (struct thread_info
*thread
)
399 if (thread
->btrace
!= NULL
)
400 return "E.Btrace already enabled.";
402 current_btrace_conf
.format
= BTRACE_FORMAT_BTS
;
403 thread
->btrace
= target_enable_btrace (thread
->entry
.id
,
404 ¤t_btrace_conf
);
405 if (thread
->btrace
== NULL
)
406 return "E.Could not enable btrace.";
411 /* Handle btrace disabling. */
414 handle_btrace_disable (struct thread_info
*thread
)
417 if (thread
->btrace
== NULL
)
418 return "E.Branch tracing not enabled.";
420 if (target_disable_btrace (thread
->btrace
) != 0)
421 return "E.Could not disable branch tracing.";
423 thread
->btrace
= NULL
;
427 /* Handle the "Qbtrace" packet. */
430 handle_btrace_general_set (char *own_buf
)
432 struct thread_info
*thread
;
436 if (!startswith (own_buf
, "Qbtrace:"))
439 op
= own_buf
+ strlen ("Qbtrace:");
441 if (ptid_equal (general_thread
, null_ptid
)
442 || ptid_equal (general_thread
, minus_one_ptid
))
444 strcpy (own_buf
, "E.Must select a single thread.");
448 thread
= find_thread_ptid (general_thread
);
451 strcpy (own_buf
, "E.No such thread.");
457 if (strcmp (op
, "bts") == 0)
458 err
= handle_btrace_enable_bts (thread
);
459 else if (strcmp (op
, "off") == 0)
460 err
= handle_btrace_disable (thread
);
462 err
= "E.Bad Qbtrace operation. Use bts or off.";
465 strcpy (own_buf
, err
);
472 /* Handle the "Qbtrace-conf" packet. */
475 handle_btrace_conf_general_set (char *own_buf
)
477 struct thread_info
*thread
;
480 if (!startswith (own_buf
, "Qbtrace-conf:"))
483 op
= own_buf
+ strlen ("Qbtrace-conf:");
485 if (ptid_equal (general_thread
, null_ptid
)
486 || ptid_equal (general_thread
, minus_one_ptid
))
488 strcpy (own_buf
, "E.Must select a single thread.");
492 thread
= find_thread_ptid (general_thread
);
495 strcpy (own_buf
, "E.No such thread.");
499 if (startswith (op
, "bts:size="))
505 size
= strtoul (op
+ strlen ("bts:size="), &endp
, 16);
506 if (endp
== NULL
|| *endp
!= 0 || errno
!= 0 || size
> UINT_MAX
)
508 strcpy (own_buf
, "E.Bad size value.");
512 current_btrace_conf
.bts
.size
= (unsigned int) size
;
516 strcpy (own_buf
, "E.Bad Qbtrace configuration option.");
524 /* Handle all of the extended 'Q' packets. */
527 handle_general_set (char *own_buf
)
529 if (startswith (own_buf
, "QPassSignals:"))
531 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
532 const char *p
= own_buf
+ strlen ("QPassSignals:");
535 p
= decode_address_to_semicolon (&cursig
, p
);
536 for (i
= 0; i
< numsigs
; i
++)
542 /* Keep looping, to clear the remaining signals. */
545 p
= decode_address_to_semicolon (&cursig
, p
);
550 strcpy (own_buf
, "OK");
554 if (startswith (own_buf
, "QProgramSignals:"))
556 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
557 const char *p
= own_buf
+ strlen ("QProgramSignals:");
560 program_signals_p
= 1;
562 p
= decode_address_to_semicolon (&cursig
, p
);
563 for (i
= 0; i
< numsigs
; i
++)
567 program_signals
[i
] = 1;
569 /* Keep looping, to clear the remaining signals. */
572 p
= decode_address_to_semicolon (&cursig
, p
);
575 program_signals
[i
] = 0;
577 strcpy (own_buf
, "OK");
581 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
585 fprintf (stderr
, "[noack mode enabled]\n");
594 if (startswith (own_buf
, "QNonStop:"))
596 char *mode
= own_buf
+ 9;
600 if (strcmp (mode
, "0") == 0)
602 else if (strcmp (mode
, "1") == 0)
606 /* We don't know what this mode is, so complain to
608 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
614 req_str
= req
? "non-stop" : "all-stop";
615 if (start_non_stop (req
) != 0)
617 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
625 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
631 if (startswith (own_buf
, "QDisableRandomization:"))
633 char *packet
= own_buf
+ strlen ("QDisableRandomization:");
636 unpack_varlen_hex (packet
, &setting
);
637 disable_randomization
= setting
;
641 if (disable_randomization
)
642 fprintf (stderr
, "[address space randomization disabled]\n");
644 fprintf (stderr
, "[address space randomization enabled]\n");
651 if (target_supports_tracepoints ()
652 && handle_tracepoint_general_set (own_buf
))
655 if (startswith (own_buf
, "QAgent:"))
657 char *mode
= own_buf
+ strlen ("QAgent:");
660 if (strcmp (mode
, "0") == 0)
662 else if (strcmp (mode
, "1") == 0)
666 /* We don't know what this value is, so complain to GDB. */
667 sprintf (own_buf
, "E.Unknown QAgent value");
671 /* Update the flag. */
674 fprintf (stderr
, "[%s agent]\n", req
? "Enable" : "Disable");
679 if (handle_btrace_general_set (own_buf
))
682 if (handle_btrace_conf_general_set (own_buf
))
685 /* Otherwise we didn't know what packet it was. Say we didn't
691 get_features_xml (const char *annex
)
693 const struct target_desc
*desc
= current_target_desc ();
695 /* `desc->xmltarget' defines what to return when looking for the
696 "target.xml" file. Its contents can either be verbatim XML code
697 (prefixed with a '@') or else the name of the actual XML file to
698 be used in place of "target.xml".
700 This variable is set up from the auto-generated
701 init_registers_... routine for the current target. */
703 if (desc
->xmltarget
!= NULL
&& strcmp (annex
, "target.xml") == 0)
705 if (*desc
->xmltarget
== '@')
706 return desc
->xmltarget
+ 1;
708 annex
= desc
->xmltarget
;
713 extern const char *const xml_builtin
[][2];
716 /* Look for the annex. */
717 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
718 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
721 if (xml_builtin
[i
][0] != NULL
)
722 return xml_builtin
[i
][1];
730 monitor_show_help (void)
732 monitor_output ("The following monitor commands are supported:\n");
733 monitor_output (" set debug <0|1>\n");
734 monitor_output (" Enable general debugging messages\n");
735 monitor_output (" set debug-hw-points <0|1>\n");
736 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
737 monitor_output (" set remote-debug <0|1>\n");
738 monitor_output (" Enable remote protocol debugging messages\n");
739 monitor_output (" set debug-format option1[,option2,...]\n");
740 monitor_output (" Add additional information to debugging messages\n");
741 monitor_output (" Options: all, none");
742 monitor_output (", timestamp");
743 monitor_output ("\n");
744 monitor_output (" exit\n");
745 monitor_output (" Quit GDBserver\n");
748 /* Read trace frame or inferior memory. Returns the number of bytes
749 actually read, zero when no further transfer is possible, and -1 on
750 error. Return of a positive value smaller than LEN does not
751 indicate there's no more to be read, only the end of the transfer.
752 E.g., when GDB reads memory from a traceframe, a first request may
753 be served from a memory block that does not cover the whole request
754 length. A following request gets the rest served from either
755 another block (of the same traceframe) or from the read-only
759 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
763 if (current_traceframe
>= 0)
766 ULONGEST length
= len
;
768 if (traceframe_read_mem (current_traceframe
,
769 memaddr
, myaddr
, len
, &nbytes
))
771 /* Data read from trace buffer, we're done. */
774 if (!in_readonly_region (memaddr
, length
))
776 /* Otherwise we have a valid readonly case, fall through. */
777 /* (assume no half-trace half-real blocks for now) */
780 res
= prepare_to_access_memory ();
783 res
= read_inferior_memory (memaddr
, myaddr
, len
);
784 done_accessing_memory ();
786 return res
== 0 ? len
: -1;
792 /* Write trace frame or inferior memory. Actually, writing to trace
793 frames is forbidden. */
796 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
798 if (current_traceframe
>= 0)
804 ret
= prepare_to_access_memory ();
807 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
808 done_accessing_memory ();
814 /* Subroutine of handle_search_memory to simplify it. */
817 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
818 gdb_byte
*pattern
, unsigned pattern_len
,
819 gdb_byte
*search_buf
,
820 unsigned chunk_size
, unsigned search_buf_size
,
821 CORE_ADDR
*found_addrp
)
823 /* Prime the search buffer. */
825 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
828 warning ("Unable to access %ld bytes of target "
829 "memory at 0x%lx, halting search.",
830 (long) search_buf_size
, (long) start_addr
);
834 /* Perform the search.
836 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
837 When we've scanned N bytes we copy the trailing bytes to the start and
838 read in another N bytes. */
840 while (search_space_len
>= pattern_len
)
843 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
847 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
849 if (found_ptr
!= NULL
)
851 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
852 *found_addrp
= found_addr
;
856 /* Not found in this chunk, skip to next chunk. */
858 /* Don't let search_space_len wrap here, it's unsigned. */
859 if (search_space_len
>= chunk_size
)
860 search_space_len
-= chunk_size
;
862 search_space_len
= 0;
864 if (search_space_len
>= pattern_len
)
866 unsigned keep_len
= search_buf_size
- chunk_size
;
867 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
870 /* Copy the trailing part of the previous iteration to the front
871 of the buffer for the next iteration. */
872 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
874 nr_to_read
= (search_space_len
- keep_len
< chunk_size
875 ? search_space_len
- keep_len
878 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
879 nr_to_read
) != search_buf_size
)
881 warning ("Unable to access %ld bytes of target memory "
882 "at 0x%lx, halting search.",
883 (long) nr_to_read
, (long) read_addr
);
887 start_addr
+= chunk_size
;
896 /* Handle qSearch:memory packets. */
899 handle_search_memory (char *own_buf
, int packet_len
)
901 CORE_ADDR start_addr
;
902 CORE_ADDR search_space_len
;
904 unsigned int pattern_len
;
905 /* NOTE: also defined in find.c testcase. */
906 #define SEARCH_CHUNK_SIZE 16000
907 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
908 /* Buffer to hold memory contents for searching. */
909 gdb_byte
*search_buf
;
910 unsigned search_buf_size
;
912 CORE_ADDR found_addr
;
913 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
915 pattern
= malloc (packet_len
);
918 error ("Unable to allocate memory to perform the search");
919 strcpy (own_buf
, "E00");
922 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
923 packet_len
- cmd_name_len
,
924 &start_addr
, &search_space_len
,
925 pattern
, &pattern_len
) < 0)
928 error ("Error in parsing qSearch:memory packet");
929 strcpy (own_buf
, "E00");
933 search_buf_size
= chunk_size
+ pattern_len
- 1;
935 /* No point in trying to allocate a buffer larger than the search space. */
936 if (search_space_len
< search_buf_size
)
937 search_buf_size
= search_space_len
;
939 search_buf
= malloc (search_buf_size
);
940 if (search_buf
== NULL
)
943 error ("Unable to allocate memory to perform the search");
944 strcpy (own_buf
, "E00");
948 found
= handle_search_memory_1 (start_addr
, search_space_len
,
949 pattern
, pattern_len
,
950 search_buf
, chunk_size
, search_buf_size
,
954 sprintf (own_buf
, "1,%lx", (long) found_addr
);
956 strcpy (own_buf
, "0");
958 strcpy (own_buf
, "E00");
964 #define require_running(BUF) \
965 if (!target_running ()) \
971 /* Parse options to --debug-format= and "monitor set debug-format".
972 ARG is the text after "--debug-format=" or "monitor set debug-format".
973 IS_MONITOR is non-zero if we're invoked via "monitor set debug-format".
974 This triggers calls to monitor_output.
975 The result is NULL if all options were parsed ok, otherwise an error
976 message which the caller must free.
978 N.B. These commands affect all debug format settings, they are not
979 cumulative. If a format is not specified, it is turned off.
980 However, we don't go to extra trouble with things like
981 "monitor set debug-format all,none,timestamp".
982 Instead we just parse them one at a time, in order.
984 The syntax for "monitor set debug" we support here is not identical
985 to gdb's "set debug foo on|off" because we also use this function to
986 parse "--debug-format=foo,bar". */
989 parse_debug_format_options (const char *arg
, int is_monitor
)
991 VEC (char_ptr
) *options
;
995 /* First turn all debug format options off. */
998 /* First remove leading spaces, for "monitor set debug-format". */
999 while (isspace (*arg
))
1002 options
= delim_string_to_char_ptr_vec (arg
, ',');
1004 for (ix
= 0; VEC_iterate (char_ptr
, options
, ix
, option
); ++ix
)
1006 if (strcmp (option
, "all") == 0)
1008 debug_timestamp
= 1;
1010 monitor_output ("All extra debug format options enabled.\n");
1012 else if (strcmp (option
, "none") == 0)
1014 debug_timestamp
= 0;
1016 monitor_output ("All extra debug format options disabled.\n");
1018 else if (strcmp (option
, "timestamp") == 0)
1020 debug_timestamp
= 1;
1022 monitor_output ("Timestamps will be added to debug output.\n");
1024 else if (*option
== '\0')
1026 /* An empty option, e.g., "--debug-format=foo,,bar", is ignored. */
1031 char *msg
= xstrprintf ("Unknown debug-format argument: \"%s\"\n",
1034 free_char_ptr_vec (options
);
1039 free_char_ptr_vec (options
);
1043 /* Handle monitor commands not handled by target-specific handlers. */
1046 handle_monitor_command (char *mon
, char *own_buf
)
1048 if (strcmp (mon
, "set debug 1") == 0)
1051 monitor_output ("Debug output enabled.\n");
1053 else if (strcmp (mon
, "set debug 0") == 0)
1056 monitor_output ("Debug output disabled.\n");
1058 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
1060 show_debug_regs
= 1;
1061 monitor_output ("H/W point debugging output enabled.\n");
1063 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
1065 show_debug_regs
= 0;
1066 monitor_output ("H/W point debugging output disabled.\n");
1068 else if (strcmp (mon
, "set remote-debug 1") == 0)
1071 monitor_output ("Protocol debug output enabled.\n");
1073 else if (strcmp (mon
, "set remote-debug 0") == 0)
1076 monitor_output ("Protocol debug output disabled.\n");
1078 else if (startswith (mon
, "set debug-format "))
1081 = parse_debug_format_options (mon
+ sizeof ("set debug-format ") - 1,
1084 if (error_msg
!= NULL
)
1086 monitor_output (error_msg
);
1087 monitor_show_help ();
1088 write_enn (own_buf
);
1092 else if (strcmp (mon
, "help") == 0)
1093 monitor_show_help ();
1094 else if (strcmp (mon
, "exit") == 0)
1098 monitor_output ("Unknown monitor command.\n\n");
1099 monitor_show_help ();
1100 write_enn (own_buf
);
1104 /* Associates a callback with each supported qXfer'able object. */
1108 /* The object this handler handles. */
1111 /* Request that the target transfer up to LEN 8-bit bytes of the
1112 target's OBJECT. The OFFSET, for a seekable object, specifies
1113 the starting point. The ANNEX can be used to provide additional
1114 data-specific information to the target.
1116 Return the number of bytes actually transfered, zero when no
1117 further transfer is possible, -1 on error, -2 when the transfer
1118 is not supported, and -3 on a verbose error message that should
1119 be preserved. Return of a positive value smaller than LEN does
1120 not indicate the end of the object, only the end of the transfer.
1122 One, and only one, of readbuf or writebuf must be non-NULL. */
1123 int (*xfer
) (const char *annex
,
1124 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1125 ULONGEST offset
, LONGEST len
);
1128 /* Handle qXfer:auxv:read. */
1131 handle_qxfer_auxv (const char *annex
,
1132 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1133 ULONGEST offset
, LONGEST len
)
1135 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
1138 if (annex
[0] != '\0' || !target_running ())
1141 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
1144 /* Handle qXfer:exec-file:read. */
1147 handle_qxfer_exec_file (const char *const_annex
,
1148 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1149 ULONGEST offset
, LONGEST len
)
1155 if (the_target
->pid_to_exec_file
== NULL
|| writebuf
!= NULL
)
1158 if (const_annex
[0] == '\0')
1160 if (current_thread
== NULL
)
1163 pid
= pid_of (current_thread
);
1167 char *annex
= alloca (strlen (const_annex
) + 1);
1169 strcpy (annex
, const_annex
);
1170 annex
= unpack_varlen_hex (annex
, &pid
);
1172 if (annex
[0] != '\0')
1179 file
= (*the_target
->pid_to_exec_file
) (pid
);
1183 total_len
= strlen (file
);
1185 if (offset
> total_len
)
1188 if (offset
+ len
> total_len
)
1189 len
= total_len
- offset
;
1191 memcpy (readbuf
, file
+ offset
, len
);
1195 /* Handle qXfer:features:read. */
1198 handle_qxfer_features (const char *annex
,
1199 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1200 ULONGEST offset
, LONGEST len
)
1202 const char *document
;
1205 if (writebuf
!= NULL
)
1208 if (!target_running ())
1211 /* Grab the correct annex. */
1212 document
= get_features_xml (annex
);
1213 if (document
== NULL
)
1216 total_len
= strlen (document
);
1218 if (offset
> total_len
)
1221 if (offset
+ len
> total_len
)
1222 len
= total_len
- offset
;
1224 memcpy (readbuf
, document
+ offset
, len
);
1228 /* Worker routine for handle_qxfer_libraries.
1229 Add to the length pointed to by ARG a conservative estimate of the
1230 length needed to transmit the file name of INF. */
1233 accumulate_file_name_length (struct inferior_list_entry
*inf
, void *arg
)
1235 struct dll_info
*dll
= (struct dll_info
*) inf
;
1236 unsigned int *total_len
= arg
;
1238 /* Over-estimate the necessary memory. Assume that every character
1239 in the library name must be escaped. */
1240 *total_len
+= 128 + 6 * strlen (dll
->name
);
1243 /* Worker routine for handle_qxfer_libraries.
1244 Emit the XML to describe the library in INF. */
1247 emit_dll_description (struct inferior_list_entry
*inf
, void *arg
)
1249 struct dll_info
*dll
= (struct dll_info
*) inf
;
1254 strcpy (p
, " <library name=\"");
1256 name
= xml_escape_text (dll
->name
);
1260 strcpy (p
, "\"><segment address=\"");
1262 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1264 strcpy (p
, "\"/></library>\n");
1270 /* Handle qXfer:libraries:read. */
1273 handle_qxfer_libraries (const char *annex
,
1274 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1275 ULONGEST offset
, LONGEST len
)
1277 unsigned int total_len
;
1280 if (writebuf
!= NULL
)
1283 if (annex
[0] != '\0' || !target_running ())
1287 for_each_inferior_with_data (&all_dlls
, accumulate_file_name_length
,
1290 document
= malloc (total_len
);
1291 if (document
== NULL
)
1294 strcpy (document
, "<library-list>\n");
1295 p
= document
+ strlen (document
);
1297 for_each_inferior_with_data (&all_dlls
, emit_dll_description
, &p
);
1299 strcpy (p
, "</library-list>\n");
1301 total_len
= strlen (document
);
1303 if (offset
> total_len
)
1309 if (offset
+ len
> total_len
)
1310 len
= total_len
- offset
;
1312 memcpy (readbuf
, document
+ offset
, len
);
1317 /* Handle qXfer:libraries-svr4:read. */
1320 handle_qxfer_libraries_svr4 (const char *annex
,
1321 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1322 ULONGEST offset
, LONGEST len
)
1324 if (writebuf
!= NULL
)
1327 if (!target_running () || the_target
->qxfer_libraries_svr4
== NULL
)
1330 return the_target
->qxfer_libraries_svr4 (annex
, readbuf
, writebuf
, offset
, len
);
1333 /* Handle qXfer:osadata:read. */
1336 handle_qxfer_osdata (const char *annex
,
1337 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1338 ULONGEST offset
, LONGEST len
)
1340 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
1343 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
1346 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1349 handle_qxfer_siginfo (const char *annex
,
1350 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1351 ULONGEST offset
, LONGEST len
)
1353 if (the_target
->qxfer_siginfo
== NULL
)
1356 if (annex
[0] != '\0' || !target_running ())
1359 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1362 /* Handle qXfer:spu:read and qXfer:spu:write. */
1365 handle_qxfer_spu (const char *annex
,
1366 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1367 ULONGEST offset
, LONGEST len
)
1369 if (the_target
->qxfer_spu
== NULL
)
1372 if (!target_running ())
1375 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1378 /* Handle qXfer:statictrace:read. */
1381 handle_qxfer_statictrace (const char *annex
,
1382 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1383 ULONGEST offset
, LONGEST len
)
1387 if (writebuf
!= NULL
)
1390 if (annex
[0] != '\0' || !target_running () || current_traceframe
== -1)
1393 if (traceframe_read_sdata (current_traceframe
, offset
,
1394 readbuf
, len
, &nbytes
))
1399 /* Helper for handle_qxfer_threads_proper.
1400 Emit the XML to describe the thread of INF. */
1403 handle_qxfer_threads_worker (struct inferior_list_entry
*inf
, void *arg
)
1405 struct thread_info
*thread
= (struct thread_info
*) inf
;
1406 struct buffer
*buffer
= arg
;
1407 ptid_t ptid
= thread_to_gdb_id (thread
);
1409 int core
= target_core_of_thread (ptid
);
1412 write_ptid (ptid_s
, ptid
);
1416 sprintf (core_s
, "%d", core
);
1417 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1422 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1427 /* Helper for handle_qxfer_threads. */
1430 handle_qxfer_threads_proper (struct buffer
*buffer
)
1432 buffer_grow_str (buffer
, "<threads>\n");
1434 for_each_inferior_with_data (&all_threads
, handle_qxfer_threads_worker
,
1437 buffer_grow_str0 (buffer
, "</threads>\n");
1440 /* Handle qXfer:threads:read. */
1443 handle_qxfer_threads (const char *annex
,
1444 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1445 ULONGEST offset
, LONGEST len
)
1447 static char *result
= 0;
1448 static unsigned int result_length
= 0;
1450 if (writebuf
!= NULL
)
1453 if (!target_running () || annex
[0] != '\0')
1458 struct buffer buffer
;
1459 /* When asked for data at offset 0, generate everything and store into
1460 'result'. Successive reads will be served off 'result'. */
1464 buffer_init (&buffer
);
1466 handle_qxfer_threads_proper (&buffer
);
1468 result
= buffer_finish (&buffer
);
1469 result_length
= strlen (result
);
1470 buffer_free (&buffer
);
1473 if (offset
>= result_length
)
1475 /* We're out of data. */
1482 if (len
> result_length
- offset
)
1483 len
= result_length
- offset
;
1485 memcpy (readbuf
, result
+ offset
, len
);
1490 /* Handle qXfer:traceframe-info:read. */
1493 handle_qxfer_traceframe_info (const char *annex
,
1494 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1495 ULONGEST offset
, LONGEST len
)
1497 static char *result
= 0;
1498 static unsigned int result_length
= 0;
1500 if (writebuf
!= NULL
)
1503 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1508 struct buffer buffer
;
1510 /* When asked for data at offset 0, generate everything and
1511 store into 'result'. Successive reads will be served off
1515 buffer_init (&buffer
);
1517 traceframe_read_info (current_traceframe
, &buffer
);
1519 result
= buffer_finish (&buffer
);
1520 result_length
= strlen (result
);
1521 buffer_free (&buffer
);
1524 if (offset
>= result_length
)
1526 /* We're out of data. */
1533 if (len
> result_length
- offset
)
1534 len
= result_length
- offset
;
1536 memcpy (readbuf
, result
+ offset
, len
);
1540 /* Handle qXfer:fdpic:read. */
1543 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1544 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1546 if (the_target
->read_loadmap
== NULL
)
1549 if (!target_running ())
1552 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1555 /* Handle qXfer:btrace:read. */
1558 handle_qxfer_btrace (const char *annex
,
1559 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1560 ULONGEST offset
, LONGEST len
)
1562 static struct buffer cache
;
1563 struct thread_info
*thread
;
1566 if (the_target
->read_btrace
== NULL
|| writebuf
!= NULL
)
1569 if (!target_running ())
1572 if (ptid_equal (general_thread
, null_ptid
)
1573 || ptid_equal (general_thread
, minus_one_ptid
))
1575 strcpy (own_buf
, "E.Must select a single thread.");
1579 thread
= find_thread_ptid (general_thread
);
1582 strcpy (own_buf
, "E.No such thread.");
1586 if (thread
->btrace
== NULL
)
1588 strcpy (own_buf
, "E.Btrace not enabled.");
1592 if (strcmp (annex
, "all") == 0)
1593 type
= BTRACE_READ_ALL
;
1594 else if (strcmp (annex
, "new") == 0)
1595 type
= BTRACE_READ_NEW
;
1596 else if (strcmp (annex
, "delta") == 0)
1597 type
= BTRACE_READ_DELTA
;
1600 strcpy (own_buf
, "E.Bad annex.");
1606 buffer_free (&cache
);
1608 result
= target_read_btrace (thread
->btrace
, &cache
, type
);
1611 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1615 else if (offset
> cache
.used_size
)
1617 buffer_free (&cache
);
1621 if (len
> cache
.used_size
- offset
)
1622 len
= cache
.used_size
- offset
;
1624 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1629 /* Handle qXfer:btrace-conf:read. */
1632 handle_qxfer_btrace_conf (const char *annex
,
1633 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1634 ULONGEST offset
, LONGEST len
)
1636 static struct buffer cache
;
1637 struct thread_info
*thread
;
1640 if (the_target
->read_btrace_conf
== NULL
|| writebuf
!= NULL
)
1643 if (annex
[0] != '\0' || !target_running ())
1646 if (ptid_equal (general_thread
, null_ptid
)
1647 || ptid_equal (general_thread
, minus_one_ptid
))
1649 strcpy (own_buf
, "E.Must select a single thread.");
1653 thread
= find_thread_ptid (general_thread
);
1656 strcpy (own_buf
, "E.No such thread.");
1660 if (thread
->btrace
== NULL
)
1662 strcpy (own_buf
, "E.Btrace not enabled.");
1668 buffer_free (&cache
);
1670 result
= target_read_btrace_conf (thread
->btrace
, &cache
);
1673 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1677 else if (offset
> cache
.used_size
)
1679 buffer_free (&cache
);
1683 if (len
> cache
.used_size
- offset
)
1684 len
= cache
.used_size
- offset
;
1686 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1691 static const struct qxfer qxfer_packets
[] =
1693 { "auxv", handle_qxfer_auxv
},
1694 { "btrace", handle_qxfer_btrace
},
1695 { "btrace-conf", handle_qxfer_btrace_conf
},
1696 { "exec-file", handle_qxfer_exec_file
},
1697 { "fdpic", handle_qxfer_fdpic
},
1698 { "features", handle_qxfer_features
},
1699 { "libraries", handle_qxfer_libraries
},
1700 { "libraries-svr4", handle_qxfer_libraries_svr4
},
1701 { "osdata", handle_qxfer_osdata
},
1702 { "siginfo", handle_qxfer_siginfo
},
1703 { "spu", handle_qxfer_spu
},
1704 { "statictrace", handle_qxfer_statictrace
},
1705 { "threads", handle_qxfer_threads
},
1706 { "traceframe-info", handle_qxfer_traceframe_info
},
1710 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1718 if (!startswith (own_buf
, "qXfer:"))
1721 /* Grab the object, r/w and annex. */
1722 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1724 write_enn (own_buf
);
1729 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1732 const struct qxfer
*q
= &qxfer_packets
[i
];
1734 if (strcmp (object
, q
->object
) == 0)
1736 if (strcmp (rw
, "read") == 0)
1738 unsigned char *data
;
1743 /* Grab the offset and length. */
1744 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1746 write_enn (own_buf
);
1750 /* Read one extra byte, as an indicator of whether there is
1752 if (len
> PBUFSIZ
- 2)
1754 data
= malloc (len
+ 1);
1757 write_enn (own_buf
);
1760 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1768 /* Preserve error message. */
1771 write_enn (own_buf
);
1773 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1775 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1780 else if (strcmp (rw
, "write") == 0)
1785 unsigned char *data
;
1787 strcpy (own_buf
, "E00");
1788 data
= malloc (packet_len
- (offset
- own_buf
));
1791 write_enn (own_buf
);
1794 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1795 &ofs
, &len
, data
) < 0)
1798 write_enn (own_buf
);
1802 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1810 /* Preserve error message. */
1813 write_enn (own_buf
);
1815 sprintf (own_buf
, "%x", n
);
1828 /* Table used by the crc32 function to calcuate the checksum. */
1830 static unsigned int crc32_table
[256] =
1833 /* Compute 32 bit CRC from inferior memory.
1835 On success, return 32 bit CRC.
1836 On failure, return (unsigned long long) -1. */
1838 static unsigned long long
1839 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1841 if (!crc32_table
[1])
1843 /* Initialize the CRC table and the decoding table. */
1847 for (i
= 0; i
< 256; i
++)
1849 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1850 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1857 unsigned char byte
= 0;
1859 /* Return failure if memory read fails. */
1860 if (read_inferior_memory (base
, &byte
, 1) != 0)
1861 return (unsigned long long) -1;
1863 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1866 return (unsigned long long) crc
;
1869 /* Add supported btrace packets to BUF. */
1872 supported_btrace_packets (char *buf
)
1874 if (target_supports_btrace (BTRACE_FORMAT_BTS
))
1876 strcat (buf
, ";Qbtrace:bts+");
1877 strcat (buf
, ";Qbtrace-conf:bts:size+");
1882 strcat (buf
, ";Qbtrace:off+");
1883 strcat (buf
, ";qXfer:btrace:read+");
1884 strcat (buf
, ";qXfer:btrace-conf:read+");
1887 /* Handle all of the extended 'q' packets. */
1890 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1892 static struct inferior_list_entry
*thread_ptr
;
1894 /* Reply the current thread id. */
1895 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1898 require_running (own_buf
);
1900 if (!ptid_equal (general_thread
, null_ptid
)
1901 && !ptid_equal (general_thread
, minus_one_ptid
))
1902 gdb_id
= general_thread
;
1905 thread_ptr
= get_first_inferior (&all_threads
);
1906 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1909 sprintf (own_buf
, "QC");
1911 write_ptid (own_buf
, gdb_id
);
1915 if (strcmp ("qSymbol::", own_buf
) == 0)
1917 /* GDB is suggesting new symbols have been loaded. This may
1918 mean a new shared library has been detected as loaded, so
1919 take the opportunity to check if breakpoints we think are
1920 inserted, still are. Note that it isn't guaranteed that
1921 we'll see this when a shared library is loaded, and nor will
1922 we see this for unloads (although breakpoints in unloaded
1923 libraries shouldn't trigger), as GDB may not find symbols for
1924 the library at all. We also re-validate breakpoints when we
1925 see a second GDB breakpoint for the same address, and or when
1926 we access breakpoint shadows. */
1927 validate_breakpoints ();
1929 if (target_supports_tracepoints ())
1930 tracepoint_look_up_symbols ();
1932 if (target_running () && the_target
->look_up_symbols
!= NULL
)
1933 (*the_target
->look_up_symbols
) ();
1935 strcpy (own_buf
, "OK");
1939 if (!disable_packet_qfThreadInfo
)
1941 if (strcmp ("qfThreadInfo", own_buf
) == 0)
1945 require_running (own_buf
);
1946 thread_ptr
= get_first_inferior (&all_threads
);
1949 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1950 write_ptid (own_buf
, gdb_id
);
1951 thread_ptr
= thread_ptr
->next
;
1955 if (strcmp ("qsThreadInfo", own_buf
) == 0)
1959 require_running (own_buf
);
1960 if (thread_ptr
!= NULL
)
1963 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1964 write_ptid (own_buf
, gdb_id
);
1965 thread_ptr
= thread_ptr
->next
;
1970 sprintf (own_buf
, "l");
1976 if (the_target
->read_offsets
!= NULL
1977 && strcmp ("qOffsets", own_buf
) == 0)
1979 CORE_ADDR text
, data
;
1981 require_running (own_buf
);
1982 if (the_target
->read_offsets (&text
, &data
))
1983 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
1984 (long)text
, (long)data
, (long)data
);
1986 write_enn (own_buf
);
1991 /* Protocol features query. */
1992 if (startswith (own_buf
, "qSupported")
1993 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1995 char *p
= &own_buf
[10];
1996 int gdb_supports_qRelocInsn
= 0;
1998 /* Start processing qSupported packet. */
1999 target_process_qsupported (NULL
);
2001 /* Process each feature being provided by GDB. The first
2002 feature will follow a ':', and latter features will follow
2006 char **qsupported
= NULL
;
2010 /* Two passes, to avoid nested strtok calls in
2011 target_process_qsupported. */
2012 for (p
= strtok (p
+ 1, ";");
2014 p
= strtok (NULL
, ";"))
2017 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
2018 qsupported
[count
- 1] = xstrdup (p
);
2021 for (i
= 0; i
< count
; i
++)
2024 if (strcmp (p
, "multiprocess+") == 0)
2026 /* GDB supports and wants multi-process support if
2028 if (target_supports_multi_process ())
2031 else if (strcmp (p
, "qRelocInsn+") == 0)
2033 /* GDB supports relocate instruction requests. */
2034 gdb_supports_qRelocInsn
= 1;
2036 else if (strcmp (p
, "swbreak+") == 0)
2038 /* GDB wants us to report whether a trap is caused
2039 by a software breakpoint and for us to handle PC
2040 adjustment if necessary on this target. */
2041 if (target_supports_stopped_by_sw_breakpoint ())
2042 swbreak_feature
= 1;
2044 else if (strcmp (p
, "hwbreak+") == 0)
2046 /* GDB wants us to report whether a trap is caused
2047 by a hardware breakpoint. */
2048 if (target_supports_stopped_by_hw_breakpoint ())
2049 hwbreak_feature
= 1;
2051 else if (strcmp (p
, "fork-events+") == 0)
2053 /* GDB supports and wants fork events if possible. */
2054 if (target_supports_fork_events ())
2055 report_fork_events
= 1;
2057 else if (strcmp (p
, "vfork-events+") == 0)
2059 /* GDB supports and wants vfork events if possible. */
2060 if (target_supports_vfork_events ())
2061 report_vfork_events
= 1;
2064 target_process_qsupported (p
);
2073 "PacketSize=%x;QPassSignals+;QProgramSignals+",
2076 if (the_target
->qxfer_libraries_svr4
!= NULL
)
2077 strcat (own_buf
, ";qXfer:libraries-svr4:read+"
2078 ";augmented-libraries-svr4-read+");
2081 /* We do not have any hook to indicate whether the non-SVR4 target
2082 backend supports qXfer:libraries:read, so always report it. */
2083 strcat (own_buf
, ";qXfer:libraries:read+");
2086 if (the_target
->read_auxv
!= NULL
)
2087 strcat (own_buf
, ";qXfer:auxv:read+");
2089 if (the_target
->qxfer_spu
!= NULL
)
2090 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
2092 if (the_target
->qxfer_siginfo
!= NULL
)
2093 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
2095 if (the_target
->read_loadmap
!= NULL
)
2096 strcat (own_buf
, ";qXfer:fdpic:read+");
2098 /* We always report qXfer:features:read, as targets may
2099 install XML files on a subsequent call to arch_setup.
2100 If we reported to GDB on startup that we don't support
2101 qXfer:feature:read at all, we will never be re-queried. */
2102 strcat (own_buf
, ";qXfer:features:read+");
2104 if (transport_is_reliable
)
2105 strcat (own_buf
, ";QStartNoAckMode+");
2107 if (the_target
->qxfer_osdata
!= NULL
)
2108 strcat (own_buf
, ";qXfer:osdata:read+");
2110 if (target_supports_multi_process ())
2111 strcat (own_buf
, ";multiprocess+");
2113 if (target_supports_fork_events ())
2114 strcat (own_buf
, ";fork-events+");
2116 if (target_supports_vfork_events ())
2117 strcat (own_buf
, ";vfork-events+");
2119 if (target_supports_non_stop ())
2120 strcat (own_buf
, ";QNonStop+");
2122 if (target_supports_disable_randomization ())
2123 strcat (own_buf
, ";QDisableRandomization+");
2125 strcat (own_buf
, ";qXfer:threads:read+");
2127 if (target_supports_tracepoints ())
2129 strcat (own_buf
, ";ConditionalTracepoints+");
2130 strcat (own_buf
, ";TraceStateVariables+");
2131 strcat (own_buf
, ";TracepointSource+");
2132 strcat (own_buf
, ";DisconnectedTracing+");
2133 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
2134 strcat (own_buf
, ";FastTracepoints+");
2135 strcat (own_buf
, ";StaticTracepoints+");
2136 strcat (own_buf
, ";InstallInTrace+");
2137 strcat (own_buf
, ";qXfer:statictrace:read+");
2138 strcat (own_buf
, ";qXfer:traceframe-info:read+");
2139 strcat (own_buf
, ";EnableDisableTracepoints+");
2140 strcat (own_buf
, ";QTBuffer:size+");
2141 strcat (own_buf
, ";tracenz+");
2144 /* Support target-side breakpoint conditions and commands. */
2145 if (target_supports_conditional_breakpoints ())
2146 strcat (own_buf
, ";ConditionalBreakpoints+");
2147 strcat (own_buf
, ";BreakpointCommands+");
2149 if (target_supports_agent ())
2150 strcat (own_buf
, ";QAgent+");
2152 supported_btrace_packets (own_buf
);
2154 if (target_supports_stopped_by_sw_breakpoint ())
2155 strcat (own_buf
, ";swbreak+");
2157 if (target_supports_stopped_by_hw_breakpoint ())
2158 strcat (own_buf
, ";hwbreak+");
2160 if (the_target
->pid_to_exec_file
!= NULL
)
2161 strcat (own_buf
, ";qXfer:exec-file:read+");
2163 /* Reinitialize components as needed for the new connection. */
2164 hostio_handle_new_gdb_connection ();
2165 target_handle_new_gdb_connection ();
2170 /* Thread-local storage support. */
2171 if (the_target
->get_tls_address
!= NULL
2172 && startswith (own_buf
, "qGetTLSAddr:"))
2174 char *p
= own_buf
+ 12;
2175 CORE_ADDR parts
[2], address
= 0;
2177 ptid_t ptid
= null_ptid
;
2179 require_running (own_buf
);
2181 for (i
= 0; i
< 3; i
++)
2189 p2
= strchr (p
, ',');
2202 ptid
= read_ptid (p
, NULL
);
2204 decode_address (&parts
[i
- 1], p
, len
);
2208 if (p
!= NULL
|| i
< 3)
2212 struct thread_info
*thread
= find_thread_ptid (ptid
);
2217 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
2223 strcpy (own_buf
, paddress(address
));
2228 write_enn (own_buf
);
2232 /* Otherwise, pretend we do not understand this packet. */
2235 /* Windows OS Thread Information Block address support. */
2236 if (the_target
->get_tib_address
!= NULL
2237 && startswith (own_buf
, "qGetTIBAddr:"))
2242 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
2244 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
2247 strcpy (own_buf
, paddress(tlb
));
2252 write_enn (own_buf
);
2258 /* Handle "monitor" commands. */
2259 if (startswith (own_buf
, "qRcmd,"))
2261 char *mon
= malloc (PBUFSIZ
);
2262 int len
= strlen (own_buf
+ 6);
2266 write_enn (own_buf
);
2271 || hex2bin (own_buf
+ 6, (gdb_byte
*) mon
, len
/ 2) != len
/ 2)
2273 write_enn (own_buf
);
2277 mon
[len
/ 2] = '\0';
2281 if (the_target
->handle_monitor_command
== NULL
2282 || (*the_target
->handle_monitor_command
) (mon
) == 0)
2283 /* Default processing. */
2284 handle_monitor_command (mon
, own_buf
);
2290 if (startswith (own_buf
, "qSearch:memory:"))
2292 require_running (own_buf
);
2293 handle_search_memory (own_buf
, packet_len
);
2297 if (strcmp (own_buf
, "qAttached") == 0
2298 || startswith (own_buf
, "qAttached:"))
2300 struct process_info
*process
;
2302 if (own_buf
[sizeof ("qAttached") - 1])
2304 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
2305 process
= (struct process_info
*)
2306 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
2310 require_running (own_buf
);
2311 process
= current_process ();
2314 if (process
== NULL
)
2316 write_enn (own_buf
);
2320 strcpy (own_buf
, process
->attached
? "1" : "0");
2324 if (startswith (own_buf
, "qCRC:"))
2326 /* CRC check (compare-section). */
2330 unsigned long long crc
;
2332 require_running (own_buf
);
2333 comma
= unpack_varlen_hex (own_buf
+ 5, &base
);
2334 if (*comma
++ != ',')
2336 write_enn (own_buf
);
2339 len
= strtoul (comma
, NULL
, 16);
2340 crc
= crc32 (base
, len
, 0xffffffff);
2341 /* Check for memory failure. */
2342 if (crc
== (unsigned long long) -1)
2344 write_enn (own_buf
);
2347 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
2351 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
2354 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
2357 /* Otherwise we didn't know what packet it was. Say we didn't
2362 static void gdb_wants_all_threads_stopped (void);
2363 static void resume (struct thread_resume
*actions
, size_t n
);
2365 /* The callback that is passed to visit_actioned_threads. */
2366 typedef int (visit_actioned_threads_callback_ftype
)
2367 (const struct thread_resume
*, struct thread_info
*);
2369 /* Struct to pass data to visit_actioned_threads. */
2371 struct visit_actioned_threads_data
2373 const struct thread_resume
*actions
;
2375 visit_actioned_threads_callback_ftype
*callback
;
2378 /* Call CALLBACK for any thread to which ACTIONS applies to. Returns
2379 true if CALLBACK returns true. Returns false if no matching thread
2380 is found or CALLBACK results false.
2381 Note: This function is itself a callback for find_inferior. */
2384 visit_actioned_threads (struct inferior_list_entry
*entry
, void *datap
)
2386 struct visit_actioned_threads_data
*data
= datap
;
2387 const struct thread_resume
*actions
= data
->actions
;
2388 size_t num_actions
= data
->num_actions
;
2389 visit_actioned_threads_callback_ftype
*callback
= data
->callback
;
2392 for (i
= 0; i
< num_actions
; i
++)
2394 const struct thread_resume
*action
= &actions
[i
];
2396 if (ptid_equal (action
->thread
, minus_one_ptid
)
2397 || ptid_equal (action
->thread
, entry
->id
)
2398 || ((ptid_get_pid (action
->thread
)
2399 == ptid_get_pid (entry
->id
))
2400 && ptid_get_lwp (action
->thread
) == -1))
2402 struct thread_info
*thread
= (struct thread_info
*) entry
;
2404 if ((*callback
) (action
, thread
))
2412 /* Callback for visit_actioned_threads. If the thread has a pending
2413 status to report, report it now. */
2416 handle_pending_status (const struct thread_resume
*resumption
,
2417 struct thread_info
*thread
)
2419 if (thread
->status_pending_p
)
2421 thread
->status_pending_p
= 0;
2423 last_status
= thread
->last_status
;
2424 last_ptid
= thread
->entry
.id
;
2425 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2431 /* Parse vCont packets. */
2433 handle_v_cont (char *own_buf
)
2437 struct thread_resume
*resume_info
;
2438 struct thread_resume default_action
= {{0}};
2440 /* Count the number of semicolons in the packet. There should be one
2441 for every action. */
2447 p
= strchr (p
, ';');
2450 resume_info
= malloc (n
* sizeof (resume_info
[0]));
2451 if (resume_info
== NULL
)
2459 memset (&resume_info
[i
], 0, sizeof resume_info
[i
]);
2461 if (p
[0] == 's' || p
[0] == 'S')
2462 resume_info
[i
].kind
= resume_step
;
2463 else if (p
[0] == 'r')
2464 resume_info
[i
].kind
= resume_step
;
2465 else if (p
[0] == 'c' || p
[0] == 'C')
2466 resume_info
[i
].kind
= resume_continue
;
2467 else if (p
[0] == 't')
2468 resume_info
[i
].kind
= resume_stop
;
2472 if (p
[0] == 'S' || p
[0] == 'C')
2475 sig
= strtol (p
+ 1, &q
, 16);
2480 if (!gdb_signal_to_host_p (sig
))
2482 resume_info
[i
].sig
= gdb_signal_to_host (sig
);
2484 else if (p
[0] == 'r')
2488 p
= unpack_varlen_hex (p
+ 1, &addr
);
2489 resume_info
[i
].step_range_start
= addr
;
2494 p
= unpack_varlen_hex (p
+ 1, &addr
);
2495 resume_info
[i
].step_range_end
= addr
;
2504 resume_info
[i
].thread
= minus_one_ptid
;
2505 default_action
= resume_info
[i
];
2507 /* Note: we don't increment i here, we'll overwrite this entry
2508 the next time through. */
2510 else if (p
[0] == ':')
2512 ptid_t ptid
= read_ptid (p
+ 1, &q
);
2517 if (p
[0] != ';' && p
[0] != 0)
2520 resume_info
[i
].thread
= ptid
;
2527 resume_info
[i
] = default_action
;
2529 set_desired_thread (0);
2531 resume (resume_info
, n
);
2536 write_enn (own_buf
);
2541 /* Resume target with ACTIONS, an array of NUM_ACTIONS elements. */
2544 resume (struct thread_resume
*actions
, size_t num_actions
)
2548 /* Check if among the threads that GDB wants actioned, there's
2549 one with a pending status to report. If so, skip actually
2550 resuming/stopping and report the pending event
2552 struct visit_actioned_threads_data data
;
2554 data
.actions
= actions
;
2555 data
.num_actions
= num_actions
;
2556 data
.callback
= handle_pending_status
;
2557 if (find_inferior (&all_threads
, visit_actioned_threads
, &data
) != NULL
)
2563 (*the_target
->resume
) (actions
, num_actions
);
2569 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2571 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
)
2573 /* No proper RSP support for this yet. At least return
2575 sprintf (own_buf
, "E.No unwaited-for children left.");
2576 disable_async_io ();
2580 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2581 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
2582 && last_status
.kind
!= TARGET_WAITKIND_NO_RESUMED
)
2583 current_thread
->last_status
= last_status
;
2585 /* From the client's perspective, all-stop mode always stops all
2586 threads implicitly (and the target backend has already done
2587 so by now). Tag all threads as "want-stopped", so we don't
2588 resume them implicitly without the client telling us to. */
2589 gdb_wants_all_threads_stopped ();
2590 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2591 disable_async_io ();
2593 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2594 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2595 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2599 /* Attach to a new program. Return 1 if successful, 0 if failure. */
2601 handle_v_attach (char *own_buf
)
2605 pid
= strtol (own_buf
+ 8, NULL
, 16);
2606 if (pid
!= 0 && attach_inferior (pid
) == 0)
2608 /* Don't report shared library events after attaching, even if
2609 some libraries are preloaded. GDB will always poll the
2610 library list. Avoids the "stopped by shared library event"
2611 notice on the GDB side. */
2616 /* In non-stop, we don't send a resume reply. Stop events
2617 will follow up using the normal notification
2622 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2628 write_enn (own_buf
);
2633 /* Run a new program. Return 1 if successful, 0 if failure. */
2635 handle_v_run (char *own_buf
)
2637 char *p
, *next_p
, **new_argv
;
2641 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
2647 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
2648 if (new_argv
== NULL
)
2650 write_enn (own_buf
);
2655 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
2657 next_p
= strchr (p
, ';');
2659 next_p
= p
+ strlen (p
);
2661 if (i
== 0 && p
== next_p
)
2665 /* FIXME: Fail request if out of memory instead of dying. */
2666 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
2667 hex2bin (p
, (gdb_byte
*) new_argv
[i
], (next_p
- p
) / 2);
2668 new_argv
[i
][(next_p
- p
) / 2] = '\0';
2677 if (new_argv
[0] == NULL
)
2679 /* GDB didn't specify a program to run. Use the program from the
2680 last run with the new argument list. */
2682 if (program_argv
== NULL
)
2684 write_enn (own_buf
);
2685 freeargv (new_argv
);
2689 new_argv
[0] = strdup (program_argv
[0]);
2690 if (new_argv
[0] == NULL
)
2692 write_enn (own_buf
);
2693 freeargv (new_argv
);
2698 /* Free the old argv and install the new one. */
2699 freeargv (program_argv
);
2700 program_argv
= new_argv
;
2702 start_inferior (program_argv
);
2703 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
2705 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2707 /* In non-stop, sending a resume reply doesn't set the general
2708 thread, but GDB assumes a vRun sets it (this is so GDB can
2709 query which is the main thread of the new inferior. */
2711 general_thread
= last_ptid
;
2717 write_enn (own_buf
);
2722 /* Kill process. Return 1 if successful, 0 if failure. */
2724 handle_v_kill (char *own_buf
)
2727 char *p
= &own_buf
[6];
2729 pid
= strtol (p
, NULL
, 16);
2732 if (pid
!= 0 && kill_inferior (pid
) == 0)
2734 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2735 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
2736 last_ptid
= pid_to_ptid (pid
);
2737 discard_queued_stop_replies (pid
);
2743 write_enn (own_buf
);
2748 /* Handle all of the extended 'v' packets. */
2750 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2752 if (!disable_packet_vCont
)
2754 if (startswith (own_buf
, "vCont;"))
2756 require_running (own_buf
);
2757 handle_v_cont (own_buf
);
2761 if (startswith (own_buf
, "vCont?"))
2763 strcpy (own_buf
, "vCont;c;C;s;S;t");
2764 if (target_supports_range_stepping ())
2766 own_buf
= own_buf
+ strlen (own_buf
);
2767 strcpy (own_buf
, ";r");
2773 if (startswith (own_buf
, "vFile:")
2774 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2777 if (startswith (own_buf
, "vAttach;"))
2779 if ((!extended_protocol
|| !multi_process
) && target_running ())
2781 fprintf (stderr
, "Already debugging a process\n");
2782 write_enn (own_buf
);
2785 handle_v_attach (own_buf
);
2789 if (startswith (own_buf
, "vRun;"))
2791 if ((!extended_protocol
|| !multi_process
) && target_running ())
2793 fprintf (stderr
, "Already debugging a process\n");
2794 write_enn (own_buf
);
2797 handle_v_run (own_buf
);
2801 if (startswith (own_buf
, "vKill;"))
2803 if (!target_running ())
2805 fprintf (stderr
, "No process to kill\n");
2806 write_enn (own_buf
);
2809 handle_v_kill (own_buf
);
2813 if (handle_notif_ack (own_buf
, packet_len
))
2816 /* Otherwise we didn't know what packet it was. Say we didn't
2822 /* Resume thread and wait for another event. In non-stop mode,
2823 don't really wait here, but return immediatelly to the event
2826 myresume (char *own_buf
, int step
, int sig
)
2828 struct thread_resume resume_info
[2];
2830 int valid_cont_thread
;
2832 set_desired_thread (0);
2834 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2835 && !ptid_equal (cont_thread
, minus_one_ptid
));
2837 if (step
|| sig
|| valid_cont_thread
)
2839 resume_info
[0].thread
= current_ptid
;
2841 resume_info
[0].kind
= resume_step
;
2843 resume_info
[0].kind
= resume_continue
;
2844 resume_info
[0].sig
= sig
;
2848 if (!valid_cont_thread
)
2850 resume_info
[n
].thread
= minus_one_ptid
;
2851 resume_info
[n
].kind
= resume_continue
;
2852 resume_info
[n
].sig
= 0;
2856 resume (resume_info
, n
);
2859 /* Callback for for_each_inferior. Make a new stop reply for each
2863 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2865 struct thread_info
*thread
= (struct thread_info
*) entry
;
2867 /* For now, assume targets that don't have this callback also don't
2868 manage the thread's last_status field. */
2869 if (the_target
->thread_stopped
== NULL
)
2871 struct vstop_notif
*new_notif
= xmalloc (sizeof (*new_notif
));
2873 new_notif
->ptid
= entry
->id
;
2874 new_notif
->status
= thread
->last_status
;
2875 /* Pass the last stop reply back to GDB, but don't notify
2877 notif_event_enque (¬if_stop
,
2878 (struct notif_event
*) new_notif
);
2882 if (thread_stopped (thread
))
2887 = target_waitstatus_to_string (&thread
->last_status
);
2889 debug_printf ("Reporting thread %s as already stopped with %s\n",
2890 target_pid_to_str (entry
->id
),
2893 xfree (status_string
);
2896 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2898 /* Pass the last stop reply back to GDB, but don't notify
2900 queue_stop_reply (entry
->id
, &thread
->last_status
);
2907 /* Set this inferior threads's state as "want-stopped". We won't
2908 resume this thread until the client gives us another action for
2912 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2914 struct thread_info
*thread
= (struct thread_info
*) entry
;
2916 thread
->last_resume_kind
= resume_stop
;
2918 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2920 /* Most threads are stopped implicitly (all-stop); tag that with
2922 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2923 thread
->last_status
.value
.sig
= GDB_SIGNAL_0
;
2927 /* Set all threads' states as "want-stopped". */
2930 gdb_wants_all_threads_stopped (void)
2932 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2935 /* Clear the gdb_detached flag of every process. */
2938 gdb_reattached_process (struct inferior_list_entry
*entry
)
2940 struct process_info
*process
= (struct process_info
*) entry
;
2942 process
->gdb_detached
= 0;
2945 /* Callback for for_each_inferior. Clear the thread's pending status
2949 clear_pending_status_callback (struct inferior_list_entry
*entry
)
2951 struct thread_info
*thread
= (struct thread_info
*) entry
;
2953 thread
->status_pending_p
= 0;
2956 /* Callback for for_each_inferior. If the thread is stopped with an
2957 interesting event, mark it as having a pending event. */
2960 set_pending_status_callback (struct inferior_list_entry
*entry
)
2962 struct thread_info
*thread
= (struct thread_info
*) entry
;
2964 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
2965 || (thread
->last_status
.value
.sig
!= GDB_SIGNAL_0
2966 /* A breakpoint, watchpoint or finished step from a previous
2967 GDB run isn't considered interesting for a new GDB run.
2968 If we left those pending, the new GDB could consider them
2969 random SIGTRAPs. This leaves out real async traps. We'd
2970 have to peek into the (target-specific) siginfo to
2971 distinguish those. */
2972 && thread
->last_status
.value
.sig
!= GDB_SIGNAL_TRAP
))
2973 thread
->status_pending_p
= 1;
2976 /* Callback for find_inferior. Return true if ENTRY (a thread) has a
2977 pending status to report to GDB. */
2980 find_status_pending_thread_callback (struct inferior_list_entry
*entry
, void *data
)
2982 struct thread_info
*thread
= (struct thread_info
*) entry
;
2984 return thread
->status_pending_p
;
2987 /* Status handler for the '?' packet. */
2990 handle_status (char *own_buf
)
2992 /* GDB is connected, don't forward events to the target anymore. */
2993 for_each_inferior (&all_processes
, gdb_reattached_process
);
2995 /* In non-stop mode, we must send a stop reply for each stopped
2996 thread. In all-stop mode, just send one for the first stopped
3001 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
3003 /* The first is sent immediatly. OK is sent if there is no
3004 stopped thread, which is the same handling of the vStopped
3005 packet (by design). */
3006 notif_write_event (¬if_stop
, own_buf
);
3010 struct inferior_list_entry
*thread
= NULL
;
3013 stabilize_threads ();
3014 gdb_wants_all_threads_stopped ();
3016 /* We can only report one status, but we might be coming out of
3017 non-stop -- if more than one thread is stopped with
3018 interesting events, leave events for the threads we're not
3019 reporting now pending. They'll be reported the next time the
3020 threads are resumed. Start by marking all interesting events
3022 for_each_inferior (&all_threads
, set_pending_status_callback
);
3024 /* Prefer the last thread that reported an event to GDB (even if
3025 that was a GDB_SIGNAL_TRAP). */
3026 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
3027 && last_status
.kind
!= TARGET_WAITKIND_EXITED
3028 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
3029 thread
= find_inferior_id (&all_threads
, last_ptid
);
3031 /* If the last event thread is not found for some reason, look
3032 for some other thread that might have an event to report. */
3034 thread
= find_inferior (&all_threads
,
3035 find_status_pending_thread_callback
, NULL
);
3037 /* If we're still out of luck, simply pick the first thread in
3040 thread
= get_first_inferior (&all_threads
);
3044 struct thread_info
*tp
= (struct thread_info
*) thread
;
3046 /* We're reporting this event, so it's no longer
3048 tp
->status_pending_p
= 0;
3050 /* GDB assumes the current thread is the thread we're
3051 reporting the status for. */
3052 general_thread
= thread
->id
;
3053 set_desired_thread (1);
3055 gdb_assert (tp
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
3056 prepare_resume_reply (own_buf
, tp
->entry
.id
, &tp
->last_status
);
3059 strcpy (own_buf
, "W00");
3064 gdbserver_version (void)
3066 printf ("GNU gdbserver %s%s\n"
3067 "Copyright (C) 2015 Free Software Foundation, Inc.\n"
3068 "gdbserver is free software, covered by the "
3069 "GNU General Public License.\n"
3070 "This gdbserver was configured as \"%s\"\n",
3071 PKGVERSION
, version
, host_name
);
3075 gdbserver_usage (FILE *stream
)
3077 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
3078 "\tgdbserver [OPTIONS] --attach COMM PID\n"
3079 "\tgdbserver [OPTIONS] --multi COMM\n"
3081 "COMM may either be a tty device (for serial debugging),\n"
3082 "HOST:PORT to listen for a TCP connection, or '-' or 'stdio' to use \n"
3083 "stdin/stdout of gdbserver.\n"
3084 "PROG is the executable program. ARGS are arguments passed to inferior.\n"
3085 "PID is the process ID to attach to, when --attach is specified.\n"
3087 "Operating modes:\n"
3089 " --attach Attach to running process PID.\n"
3090 " --multi Start server without a specific program, and\n"
3091 " only quit when explicitly commanded.\n"
3092 " --once Exit after the first connection has closed.\n"
3093 " --help Print this message and then exit.\n"
3094 " --version Display version information and exit.\n"
3098 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
3099 " --disable-randomization\n"
3100 " Run PROG with address space randomization disabled.\n"
3101 " --no-disable-randomization\n"
3102 " Don't disable address space randomization when\n"
3107 " --debug Enable general debugging output.\n"
3108 " --debug-format=opt1[,opt2,...]\n"
3109 " Specify extra content in debugging output.\n"
3114 " --remote-debug Enable remote protocol debugging output.\n"
3115 " --disable-packet=opt1[,opt2,...]\n"
3116 " Disable support for RSP packets or features.\n"
3118 " vCont, Tthread, qC, qfThreadInfo and \n"
3119 " threads (disable all threading packets).\n"
3121 "For more information, consult the GDB manual (available as on-line \n"
3122 "info or a printed manual).\n");
3123 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
3124 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
3128 gdbserver_show_disableable (FILE *stream
)
3130 fprintf (stream
, "Disableable packets:\n"
3131 " vCont \tAll vCont packets\n"
3132 " qC \tQuerying the current thread\n"
3133 " qfThreadInfo\tThread listing\n"
3134 " Tthread \tPassing the thread specifier in the "
3135 "T stop reply packet\n"
3136 " threads \tAll of the above\n");
3140 #undef require_running
3141 #define require_running(BUF) \
3142 if (!target_running ()) \
3149 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
3151 int pid
= * (int *) args
;
3153 if (ptid_get_pid (entry
->id
) == pid
)
3160 kill_inferior_callback (struct inferior_list_entry
*entry
)
3162 struct process_info
*process
= (struct process_info
*) entry
;
3163 int pid
= ptid_get_pid (process
->entry
.id
);
3165 kill_inferior (pid
);
3166 discard_queued_stop_replies (pid
);
3169 /* Callback for for_each_inferior to detach or kill the inferior,
3170 depending on whether we attached to it or not.
3171 We inform the user whether we're detaching or killing the process
3172 as this is only called when gdbserver is about to exit. */
3175 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
3177 struct process_info
*process
= (struct process_info
*) entry
;
3178 int pid
= ptid_get_pid (process
->entry
.id
);
3180 if (process
->attached
)
3181 detach_inferior (pid
);
3183 kill_inferior (pid
);
3185 discard_queued_stop_replies (pid
);
3188 /* for_each_inferior callback for detach_or_kill_for_exit to print
3189 the pids of started inferiors. */
3192 print_started_pid (struct inferior_list_entry
*entry
)
3194 struct process_info
*process
= (struct process_info
*) entry
;
3196 if (! process
->attached
)
3198 int pid
= ptid_get_pid (process
->entry
.id
);
3199 fprintf (stderr
, " %d", pid
);
3203 /* for_each_inferior callback for detach_or_kill_for_exit to print
3204 the pids of attached inferiors. */
3207 print_attached_pid (struct inferior_list_entry
*entry
)
3209 struct process_info
*process
= (struct process_info
*) entry
;
3211 if (process
->attached
)
3213 int pid
= ptid_get_pid (process
->entry
.id
);
3214 fprintf (stderr
, " %d", pid
);
3218 /* Call this when exiting gdbserver with possible inferiors that need
3219 to be killed or detached from. */
3222 detach_or_kill_for_exit (void)
3224 /* First print a list of the inferiors we will be killing/detaching.
3225 This is to assist the user, for example, in case the inferior unexpectedly
3226 dies after we exit: did we screw up or did the inferior exit on its own?
3227 Having this info will save some head-scratching. */
3229 if (have_started_inferiors_p ())
3231 fprintf (stderr
, "Killing process(es):");
3232 for_each_inferior (&all_processes
, print_started_pid
);
3233 fprintf (stderr
, "\n");
3235 if (have_attached_inferiors_p ())
3237 fprintf (stderr
, "Detaching process(es):");
3238 for_each_inferior (&all_processes
, print_attached_pid
);
3239 fprintf (stderr
, "\n");
3242 /* Now we can kill or detach the inferiors. */
3244 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
3247 /* Value that will be passed to exit(3) when gdbserver exits. */
3248 static int exit_code
;
3250 /* Cleanup version of detach_or_kill_for_exit. */
3253 detach_or_kill_for_exit_cleanup (void *ignore
)
3258 detach_or_kill_for_exit ();
3261 CATCH (exception
, RETURN_MASK_ALL
)
3264 fprintf (stderr
, "Detach or kill failed: %s\n", exception
.message
);
3270 /* Main function. This is called by the real "main" function,
3271 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
3273 static void ATTRIBUTE_NORETURN
3274 captured_main (int argc
, char *argv
[])
3278 char *arg_end
, *port
;
3279 char **next_arg
= &argv
[1];
3280 volatile int multi_mode
= 0;
3281 volatile int attach
= 0;
3284 while (*next_arg
!= NULL
&& **next_arg
== '-')
3286 if (strcmp (*next_arg
, "--version") == 0)
3288 gdbserver_version ();
3291 else if (strcmp (*next_arg
, "--help") == 0)
3293 gdbserver_usage (stdout
);
3296 else if (strcmp (*next_arg
, "--attach") == 0)
3298 else if (strcmp (*next_arg
, "--multi") == 0)
3300 else if (strcmp (*next_arg
, "--wrapper") == 0)
3304 wrapper_argv
= next_arg
;
3305 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
3308 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
3310 gdbserver_usage (stderr
);
3314 /* Consume the "--". */
3317 else if (strcmp (*next_arg
, "--debug") == 0)
3319 else if (startswith (*next_arg
, "--debug-format="))
3322 = parse_debug_format_options ((*next_arg
)
3323 + sizeof ("--debug-format=") - 1, 0);
3325 if (error_msg
!= NULL
)
3327 fprintf (stderr
, "%s", error_msg
);
3331 else if (strcmp (*next_arg
, "--remote-debug") == 0)
3333 else if (strcmp (*next_arg
, "--disable-packet") == 0)
3335 gdbserver_show_disableable (stdout
);
3338 else if (startswith (*next_arg
, "--disable-packet="))
3340 char *packets
, *tok
;
3342 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
3343 for (tok
= strtok (packets
, ",");
3345 tok
= strtok (NULL
, ","))
3347 if (strcmp ("vCont", tok
) == 0)
3348 disable_packet_vCont
= 1;
3349 else if (strcmp ("Tthread", tok
) == 0)
3350 disable_packet_Tthread
= 1;
3351 else if (strcmp ("qC", tok
) == 0)
3352 disable_packet_qC
= 1;
3353 else if (strcmp ("qfThreadInfo", tok
) == 0)
3354 disable_packet_qfThreadInfo
= 1;
3355 else if (strcmp ("threads", tok
) == 0)
3357 disable_packet_vCont
= 1;
3358 disable_packet_Tthread
= 1;
3359 disable_packet_qC
= 1;
3360 disable_packet_qfThreadInfo
= 1;
3364 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
3366 gdbserver_show_disableable (stderr
);
3371 else if (strcmp (*next_arg
, "-") == 0)
3373 /* "-" specifies a stdio connection and is a form of port
3375 *next_arg
= STDIO_CONNECTION_NAME
;
3378 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
3379 disable_randomization
= 1;
3380 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
3381 disable_randomization
= 0;
3382 else if (strcmp (*next_arg
, "--once") == 0)
3386 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
3396 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
3398 gdbserver_usage (stderr
);
3402 /* Remember stdio descriptors. LISTEN_DESC must not be listed, it will be
3403 opened by remote_prepare. */
3406 /* We need to know whether the remote connection is stdio before
3407 starting the inferior. Inferiors created in this scenario have
3408 stdin,stdout redirected. So do this here before we call
3410 remote_prepare (port
);
3415 /* --attach used to come after PORT, so allow it there for
3417 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
3424 && (*next_arg
== NULL
3425 || (*next_arg
)[0] == '\0'
3426 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
3428 || next_arg
[1] != NULL
))
3433 gdbserver_usage (stderr
);
3437 initialize_async_io ();
3439 initialize_event_loop ();
3440 if (target_supports_tracepoints ())
3441 initialize_tracepoint ();
3443 own_buf
= xmalloc (PBUFSIZ
+ 1);
3444 mem_buf
= xmalloc (PBUFSIZ
);
3446 if (pid
== 0 && *next_arg
!= NULL
)
3450 n
= argc
- (next_arg
- argv
);
3451 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
3452 for (i
= 0; i
< n
; i
++)
3453 program_argv
[i
] = xstrdup (next_arg
[i
]);
3454 program_argv
[i
] = NULL
;
3456 /* Wait till we are at first instruction in program. */
3457 start_inferior (program_argv
);
3459 /* We are now (hopefully) stopped at the first instruction of
3460 the target process. This assumes that the target process was
3461 successfully created. */
3465 if (attach_inferior (pid
) == -1)
3466 error ("Attaching not supported on this target");
3468 /* Otherwise succeeded. */
3472 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3473 last_status
.value
.integer
= 0;
3474 last_ptid
= minus_one_ptid
;
3476 make_cleanup (detach_or_kill_for_exit_cleanup
, NULL
);
3478 initialize_notif ();
3480 /* Don't report shared library events on the initial connection,
3481 even if some libraries are preloaded. Avoids the "stopped by
3482 shared library event" notice on gdb side. */
3485 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3486 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3491 if (!was_running
&& !multi_mode
)
3492 error ("No program to debug");
3499 report_fork_events
= 0;
3500 report_vfork_events
= 0;
3501 /* Be sure we're out of tfind mode. */
3502 current_traceframe
= -1;
3503 cont_thread
= null_ptid
;
3504 swbreak_feature
= 0;
3505 hwbreak_feature
= 0;
3511 /* Wait for events. This will return when all event sources
3512 are removed from the event loop. */
3513 start_event_loop ();
3515 /* If an exit was requested (using the "monitor exit"
3516 command), terminate now. The only other way to get
3517 here is for getpkt to fail; close the connection
3518 and reopen it at the top of the loop. */
3520 if (exit_requested
|| run_once
)
3521 throw_quit ("Quit");
3524 "Remote side has terminated connection. "
3525 "GDBserver will reopen the connection.\n");
3527 /* Get rid of any pending statuses. An eventual reconnection
3528 (by the same GDB instance or another) will refresh all its
3529 state from scratch. */
3530 discard_queued_stop_replies (-1);
3531 for_each_inferior (&all_threads
,
3532 clear_pending_status_callback
);
3536 if (disconnected_tracing
)
3538 /* Try to enable non-stop/async mode, so we we can
3539 both wait for an async socket accept, and handle
3540 async target events simultaneously. There's also
3541 no point either in having the target always stop
3542 all threads, when we're going to pass signals
3543 down without informing GDB. */
3546 if (start_non_stop (1))
3549 /* Detaching implicitly resumes all threads;
3550 simply disconnecting does not. */
3556 "Disconnected tracing disabled; "
3557 "stopping trace run.\n");
3562 CATCH (exception
, RETURN_MASK_ERROR
)
3564 if (response_needed
)
3566 write_enn (own_buf
);
3574 /* Main function. */
3577 main (int argc
, char *argv
[])
3582 captured_main (argc
, argv
);
3584 CATCH (exception
, RETURN_MASK_ALL
)
3586 if (exception
.reason
== RETURN_ERROR
)
3589 fprintf (stderr
, "%s\n", exception
.message
);
3590 fprintf (stderr
, "Exiting\n");
3598 gdb_assert_not_reached ("captured_main should never return");
3601 /* Skip PACKET until the next semi-colon (or end of string). */
3604 skip_to_semicolon (char **packet
)
3606 while (**packet
!= '\0' && **packet
!= ';')
3610 /* Process options coming from Z packets for a breakpoint. PACKET is
3611 the packet buffer. *PACKET is updated to point to the first char
3612 after the last processed option. */
3615 process_point_options (struct breakpoint
*bp
, char **packet
)
3617 char *dataptr
= *packet
;
3620 /* Check if data has the correct format. */
3621 if (*dataptr
!= ';')
3628 if (*dataptr
== ';')
3631 if (*dataptr
== 'X')
3633 /* Conditional expression. */
3635 debug_printf ("Found breakpoint condition.\n");
3636 if (!add_breakpoint_condition (bp
, &dataptr
))
3637 skip_to_semicolon (&dataptr
);
3639 else if (startswith (dataptr
, "cmds:"))
3641 dataptr
+= strlen ("cmds:");
3643 debug_printf ("Found breakpoint commands %s.\n", dataptr
);
3644 persist
= (*dataptr
== '1');
3646 if (add_breakpoint_commands (bp
, &dataptr
, persist
))
3647 skip_to_semicolon (&dataptr
);
3651 fprintf (stderr
, "Unknown token %c, ignoring.\n",
3653 /* Skip tokens until we find one that we recognize. */
3654 skip_to_semicolon (&dataptr
);
3660 /* Event loop callback that handles a serial event. The first byte in
3661 the serial buffer gets us here. We expect characters to arrive at
3662 a brisk pace, so we read the rest of the packet with a blocking
3666 process_serial_event (void)
3677 int new_packet_len
= -1;
3679 /* Used to decide when gdbserver should exit in
3680 multi-mode/remote. */
3681 static int have_ran
= 0;
3684 have_ran
= target_running ();
3686 disable_async_io ();
3688 response_needed
= 0;
3689 packet_len
= getpkt (own_buf
);
3690 if (packet_len
<= 0)
3693 /* Force an event loop break. */
3696 response_needed
= 1;
3703 handle_query (own_buf
, packet_len
, &new_packet_len
);
3706 handle_general_set (own_buf
);
3709 require_running (own_buf
);
3714 pid
= strtol (&own_buf
[i
], NULL
, 16);
3717 pid
= ptid_get_pid (current_ptid
);
3719 if ((tracing
&& disconnected_tracing
) || any_persistent_commands ())
3721 struct thread_resume resume_info
;
3722 struct process_info
*process
= find_process_pid (pid
);
3724 if (process
== NULL
)
3726 write_enn (own_buf
);
3730 if (tracing
&& disconnected_tracing
)
3732 "Disconnected tracing in effect, "
3733 "leaving gdbserver attached to the process\n");
3735 if (any_persistent_commands ())
3737 "Persistent commands are present, "
3738 "leaving gdbserver attached to the process\n");
3740 /* Make sure we're in non-stop/async mode, so we we can both
3741 wait for an async socket accept, and handle async target
3742 events simultaneously. There's also no point either in
3743 having the target stop all threads, when we're going to
3744 pass signals down without informing GDB. */
3748 debug_printf ("Forcing non-stop mode\n");
3754 process
->gdb_detached
= 1;
3756 /* Detaching implicitly resumes all threads. */
3757 resume_info
.thread
= minus_one_ptid
;
3758 resume_info
.kind
= resume_continue
;
3759 resume_info
.sig
= 0;
3760 (*the_target
->resume
) (&resume_info
, 1);
3763 break; /* from switch/case */
3766 fprintf (stderr
, "Detaching from process %d\n", pid
);
3768 if (detach_inferior (pid
) != 0)
3769 write_enn (own_buf
);
3772 discard_queued_stop_replies (pid
);
3775 if (extended_protocol
)
3777 /* Treat this like a normal program exit. */
3778 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3779 last_status
.value
.integer
= 0;
3780 last_ptid
= pid_to_ptid (pid
);
3782 current_thread
= NULL
;
3789 /* If we are attached, then we can exit. Otherwise, we
3790 need to hang around doing nothing, until the child is
3792 join_inferior (pid
);
3798 extended_protocol
= 1;
3802 handle_status (own_buf
);
3805 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
3807 ptid_t gdb_id
, thread_id
;
3810 require_running (own_buf
);
3812 gdb_id
= read_ptid (&own_buf
[2], NULL
);
3814 pid
= ptid_get_pid (gdb_id
);
3816 if (ptid_equal (gdb_id
, null_ptid
)
3817 || ptid_equal (gdb_id
, minus_one_ptid
))
3818 thread_id
= null_ptid
;
3820 && ptid_equal (pid_to_ptid (pid
),
3823 struct thread_info
*thread
=
3824 (struct thread_info
*) find_inferior (&all_threads
,
3829 write_enn (own_buf
);
3833 thread_id
= thread
->entry
.id
;
3837 thread_id
= gdb_id_to_thread_id (gdb_id
);
3838 if (ptid_equal (thread_id
, null_ptid
))
3840 write_enn (own_buf
);
3845 if (own_buf
[1] == 'g')
3847 if (ptid_equal (thread_id
, null_ptid
))
3849 /* GDB is telling us to choose any thread. Check if
3850 the currently selected thread is still valid. If
3851 it is not, select the first available. */
3852 struct thread_info
*thread
=
3853 (struct thread_info
*) find_inferior_id (&all_threads
,
3857 thread
= get_first_thread ();
3858 thread_id
= thread
->entry
.id
;
3862 general_thread
= thread_id
;
3863 set_desired_thread (1);
3865 else if (own_buf
[1] == 'c')
3866 cont_thread
= thread_id
;
3872 /* Silently ignore it so that gdb can extend the protocol
3873 without compatibility headaches. */
3878 require_running (own_buf
);
3879 if (current_traceframe
>= 0)
3881 struct regcache
*regcache
3882 = new_register_cache (current_target_desc ());
3884 if (fetch_traceframe_registers (current_traceframe
,
3886 registers_to_string (regcache
, own_buf
);
3888 write_enn (own_buf
);
3889 free_register_cache (regcache
);
3893 struct regcache
*regcache
;
3895 set_desired_thread (1);
3896 regcache
= get_thread_regcache (current_thread
, 1);
3897 registers_to_string (regcache
, own_buf
);
3901 require_running (own_buf
);
3902 if (current_traceframe
>= 0)
3903 write_enn (own_buf
);
3906 struct regcache
*regcache
;
3908 set_desired_thread (1);
3909 regcache
= get_thread_regcache (current_thread
, 1);
3910 registers_from_string (regcache
, &own_buf
[1]);
3915 require_running (own_buf
);
3916 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
3917 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
3919 write_enn (own_buf
);
3921 bin2hex (mem_buf
, own_buf
, res
);
3924 require_running (own_buf
);
3925 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
3926 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
3929 write_enn (own_buf
);
3932 require_running (own_buf
);
3933 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
3934 &mem_addr
, &len
, &mem_buf
) < 0
3935 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
3936 write_enn (own_buf
);
3941 require_running (own_buf
);
3942 hex2bin (own_buf
+ 1, &sig
, 1);
3943 if (gdb_signal_to_host_p (sig
))
3944 signal
= gdb_signal_to_host (sig
);
3947 myresume (own_buf
, 0, signal
);
3950 require_running (own_buf
);
3951 hex2bin (own_buf
+ 1, &sig
, 1);
3952 if (gdb_signal_to_host_p (sig
))
3953 signal
= gdb_signal_to_host (sig
);
3956 myresume (own_buf
, 1, signal
);
3959 require_running (own_buf
);
3961 myresume (own_buf
, 0, signal
);
3964 require_running (own_buf
);
3966 myresume (own_buf
, 1, signal
);
3968 case 'Z': /* insert_ ... */
3970 case 'z': /* remove_ ... */
3975 char type
= own_buf
[1];
3977 const int insert
= ch
== 'Z';
3978 char *p
= &own_buf
[3];
3980 p
= unpack_varlen_hex (p
, &addr
);
3981 len
= strtol (p
+ 1, &dataptr
, 16);
3985 struct breakpoint
*bp
;
3987 bp
= set_gdb_breakpoint (type
, addr
, len
, &res
);
3992 /* GDB may have sent us a list of *point parameters to
3993 be evaluated on the target's side. Read such list
3994 here. If we already have a list of parameters, GDB
3995 is telling us to drop that list and use this one
3997 clear_breakpoint_conditions_and_commands (bp
);
3998 process_point_options (bp
, &dataptr
);
4002 res
= delete_gdb_breakpoint (type
, addr
, len
);
4010 write_enn (own_buf
);
4014 response_needed
= 0;
4015 if (!target_running ())
4016 /* The packet we received doesn't make sense - but we can't
4017 reply to it, either. */
4020 fprintf (stderr
, "Killing all inferiors\n");
4021 for_each_inferior (&all_processes
, kill_inferior_callback
);
4023 /* When using the extended protocol, we wait with no program
4024 running. The traditional protocol will exit instead. */
4025 if (extended_protocol
)
4027 last_status
.kind
= TARGET_WAITKIND_EXITED
;
4028 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
4036 ptid_t gdb_id
, thread_id
;
4038 require_running (own_buf
);
4040 gdb_id
= read_ptid (&own_buf
[1], NULL
);
4041 thread_id
= gdb_id_to_thread_id (gdb_id
);
4042 if (ptid_equal (thread_id
, null_ptid
))
4044 write_enn (own_buf
);
4048 if (mythread_alive (thread_id
))
4051 write_enn (own_buf
);
4055 response_needed
= 0;
4057 /* Restarting the inferior is only supported in the extended
4059 if (extended_protocol
)
4061 if (target_running ())
4062 for_each_inferior (&all_processes
,
4063 kill_inferior_callback
);
4064 fprintf (stderr
, "GDBserver restarting\n");
4066 /* Wait till we are at 1st instruction in prog. */
4067 if (program_argv
!= NULL
)
4068 start_inferior (program_argv
);
4071 last_status
.kind
= TARGET_WAITKIND_EXITED
;
4072 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
4078 /* It is a request we don't understand. Respond with an
4079 empty packet so that gdb knows that we don't support this
4085 /* Extended (long) request. */
4086 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
4090 /* It is a request we don't understand. Respond with an empty
4091 packet so that gdb knows that we don't support this
4097 if (new_packet_len
!= -1)
4098 putpkt_binary (own_buf
, new_packet_len
);
4102 response_needed
= 0;
4104 if (!extended_protocol
&& have_ran
&& !target_running ())
4106 /* In non-stop, defer exiting until GDB had a chance to query
4107 the whole vStopped list (until it gets an OK). */
4108 if (QUEUE_is_empty (notif_event_p
, notif_stop
.queue
))
4110 /* Be transparent when GDB is connected through stdio -- no
4111 need to spam GDB's console. */
4112 if (!remote_connection_is_stdio ())
4113 fprintf (stderr
, "GDBserver exiting\n");
4125 /* Event-loop callback for serial events. */
4128 handle_serial_event (int err
, gdb_client_data client_data
)
4131 debug_printf ("handling possible serial event\n");
4133 /* Really handle it. */
4134 if (process_serial_event () < 0)
4137 /* Be sure to not change the selected thread behind GDB's back.
4138 Important in the non-stop mode asynchronous protocol. */
4139 set_desired_thread (1);
4144 /* Event-loop callback for target events. */
4147 handle_target_event (int err
, gdb_client_data client_data
)
4150 debug_printf ("handling possible target event\n");
4152 last_ptid
= mywait (minus_one_ptid
, &last_status
,
4155 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
)
4157 /* No RSP support for this yet. */
4159 else if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
4161 int pid
= ptid_get_pid (last_ptid
);
4162 struct process_info
*process
= find_process_pid (pid
);
4163 int forward_event
= !gdb_connected () || process
->gdb_detached
;
4165 if (last_status
.kind
== TARGET_WAITKIND_EXITED
4166 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
4168 mark_breakpoints_out (process
);
4169 mourn_inferior (process
);
4173 /* We're reporting this thread as stopped. Update its
4174 "want-stopped" state to what the client wants, until it
4175 gets a new resume action. */
4176 current_thread
->last_resume_kind
= resume_stop
;
4177 current_thread
->last_status
= last_status
;
4182 if (!target_running ())
4184 /* The last process exited. We're done. */
4188 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
4190 /* A thread stopped with a signal, but gdb isn't
4191 connected to handle it. Pass it down to the
4192 inferior, as if it wasn't being traced. */
4193 struct thread_resume resume_info
;
4196 debug_printf ("GDB not connected; forwarding event %d for"
4198 (int) last_status
.kind
,
4199 target_pid_to_str (last_ptid
));
4201 resume_info
.thread
= last_ptid
;
4202 resume_info
.kind
= resume_continue
;
4203 resume_info
.sig
= gdb_signal_to_host (last_status
.value
.sig
);
4204 (*the_target
->resume
) (&resume_info
, 1);
4206 else if (debug_threads
)
4207 debug_printf ("GDB not connected; ignoring event %d for [%s]\n",
4208 (int) last_status
.kind
,
4209 target_pid_to_str (last_ptid
));
4213 struct vstop_notif
*vstop_notif
4214 = xmalloc (sizeof (struct vstop_notif
));
4216 vstop_notif
->status
= last_status
;
4217 vstop_notif
->ptid
= last_ptid
;
4218 /* Push Stop notification. */
4219 notif_push (¬if_stop
,
4220 (struct notif_event
*) vstop_notif
);
4224 /* Be sure to not change the selected thread behind GDB's back.
4225 Important in the non-stop mode asynchronous protocol. */
4226 set_desired_thread (1);