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 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
;
62 int report_exec_events
;
67 /* True if the "vContSupported" feature is active. In that case, GDB
68 wants us to report whether single step is supported in the reply to
70 static int vCont_supported
;
72 /* Whether we should attempt to disable the operating system's address
73 space randomization feature before starting an inferior. */
74 int disable_randomization
= 1;
76 static char **program_argv
, **wrapper_argv
;
78 int pass_signals
[GDB_SIGNAL_LAST
];
79 int program_signals
[GDB_SIGNAL_LAST
];
80 int program_signals_p
;
82 /* The PID of the originally created or attached inferior. Used to
83 send signals to the process when GDB sends us an asynchronous interrupt
84 (user hitting Control-C in the client), and to wait for the child to exit
85 when no longer debugging it. */
87 unsigned long signal_pid
;
90 /* A file descriptor for the controlling terminal. */
93 /* TERMINAL_FD's original foreground group. */
94 pid_t old_foreground_pgrp
;
96 /* Hand back terminal ownership to the original foreground group. */
99 restore_old_foreground_pgrp (void)
101 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
105 /* Set if you want to disable optional thread related packets support
106 in gdbserver, for the sake of testing GDB against stubs that don't
108 int disable_packet_vCont
;
109 int disable_packet_Tthread
;
110 int disable_packet_qC
;
111 int disable_packet_qfThreadInfo
;
113 /* Last status reported to GDB. */
114 static struct target_waitstatus last_status
;
115 static ptid_t last_ptid
;
117 static char *own_buf
;
118 static unsigned char *mem_buf
;
120 /* A sub-class of 'struct notif_event' for stop, holding information
121 relative to a single stop reply. We keep a queue of these to
122 push to GDB in non-stop mode. */
126 struct notif_event base
;
128 /* Thread or process that got the event. */
132 struct target_waitstatus status
;
135 /* The current btrace configuration. This is gdbserver's mirror of GDB's
136 btrace configuration. */
137 static struct btrace_config current_btrace_conf
;
139 DEFINE_QUEUE_P (notif_event_p
);
141 /* Put a stop reply to the stop reply queue. */
144 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
146 struct vstop_notif
*new_notif
= XNEW (struct vstop_notif
);
148 new_notif
->ptid
= ptid
;
149 new_notif
->status
= *status
;
151 notif_event_enque (¬if_stop
, (struct notif_event
*) new_notif
);
155 remove_all_on_match_ptid (QUEUE (notif_event_p
) *q
,
156 QUEUE_ITER (notif_event_p
) *iter
,
157 struct notif_event
*event
,
160 ptid_t filter_ptid
= *(ptid_t
*) data
;
161 struct vstop_notif
*vstop_event
= (struct vstop_notif
*) event
;
163 if (ptid_match (vstop_event
->ptid
, filter_ptid
))
165 if (q
->free_func
!= NULL
)
166 q
->free_func (event
);
168 QUEUE_remove_elem (notif_event_p
, q
, iter
);
177 discard_queued_stop_replies (ptid_t ptid
)
179 QUEUE_iterate (notif_event_p
, notif_stop
.queue
,
180 remove_all_on_match_ptid
, &ptid
);
184 vstop_notif_reply (struct notif_event
*event
, char *own_buf
)
186 struct vstop_notif
*vstop
= (struct vstop_notif
*) event
;
188 prepare_resume_reply (own_buf
, vstop
->ptid
, &vstop
->status
);
191 struct notif_server notif_stop
=
193 "vStopped", "Stop", NULL
, vstop_notif_reply
,
197 target_running (void)
199 return get_first_thread () != NULL
;
203 start_inferior (char **argv
)
205 char **new_argv
= argv
;
207 if (wrapper_argv
!= NULL
)
211 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
213 for (i
= 0; argv
[i
] != NULL
; i
++)
215 new_argv
= XALLOCAVEC (char *, count
);
217 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
218 new_argv
[count
++] = wrapper_argv
[i
];
219 for (i
= 0; argv
[i
] != NULL
; i
++)
220 new_argv
[count
++] = argv
[i
];
221 new_argv
[count
] = NULL
;
227 for (i
= 0; new_argv
[i
]; ++i
)
228 debug_printf ("new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
233 signal (SIGTTOU
, SIG_DFL
);
234 signal (SIGTTIN
, SIG_DFL
);
237 signal_pid
= create_inferior (new_argv
[0], new_argv
);
239 /* FIXME: we don't actually know at this point that the create
240 actually succeeded. We won't know that until we wait. */
241 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
246 signal (SIGTTOU
, SIG_IGN
);
247 signal (SIGTTIN
, SIG_IGN
);
248 terminal_fd
= fileno (stderr
);
249 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
250 tcsetpgrp (terminal_fd
, signal_pid
);
251 atexit (restore_old_foreground_pgrp
);
254 if (wrapper_argv
!= NULL
)
256 struct thread_resume resume_info
;
258 memset (&resume_info
, 0, sizeof (resume_info
));
259 resume_info
.thread
= pid_to_ptid (signal_pid
);
260 resume_info
.kind
= resume_continue
;
263 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
265 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
269 (*the_target
->resume
) (&resume_info
, 1);
271 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
272 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
275 current_thread
->last_resume_kind
= resume_stop
;
276 current_thread
->last_status
= last_status
;
278 while (last_status
.value
.sig
!= GDB_SIGNAL_TRAP
);
280 target_arch_setup ();
284 /* Wait till we are at 1st instruction in program, return new pid
285 (assuming success). */
286 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
288 target_arch_setup ();
290 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
291 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
293 current_thread
->last_resume_kind
= resume_stop
;
294 current_thread
->last_status
= last_status
;
297 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
303 attach_inferior (int pid
)
305 /* myattach should return -1 if attaching is unsupported,
306 0 if it succeeded, and call error() otherwise. */
308 if (myattach (pid
) != 0)
311 fprintf (stderr
, "Attached; pid = %d\n", pid
);
314 /* FIXME - It may be that we should get the SIGNAL_PID from the
315 attach function, so that it can be the main thread instead of
316 whichever we were told to attach to. */
321 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
323 /* GDB knows to ignore the first SIGSTOP after attaching to a running
324 process using the "attach" command, but this is different; it's
325 just using "target remote". Pretend it's just starting up. */
326 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
327 && last_status
.value
.sig
== GDB_SIGNAL_STOP
)
328 last_status
.value
.sig
= GDB_SIGNAL_TRAP
;
330 current_thread
->last_resume_kind
= resume_stop
;
331 current_thread
->last_status
= last_status
;
337 extern int remote_debug
;
339 /* Decode a qXfer read request. Return 0 if everything looks OK,
343 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
345 /* After the read marker and annex, qXfer looks like a
346 traditional 'm' packet. */
347 decode_m_packet (buf
, ofs
, len
);
353 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
355 /* Extract and NUL-terminate the object. */
357 while (*buf
&& *buf
!= ':')
363 /* Extract and NUL-terminate the read/write action. */
365 while (*buf
&& *buf
!= ':')
371 /* Extract and NUL-terminate the annex. */
373 while (*buf
&& *buf
!= ':')
383 /* Write the response to a successful qXfer read. Returns the
384 length of the (binary) data stored in BUF, corresponding
385 to as much of DATA/LEN as we could fit. IS_MORE controls
386 the first character of the response. */
388 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
397 return remote_escape_output (data
, len
, 1, (unsigned char *) buf
+ 1,
398 &out_len
, PBUFSIZ
- 2) + 1;
401 /* Handle btrace enabling in BTS format. */
404 handle_btrace_enable_bts (struct thread_info
*thread
)
406 if (thread
->btrace
!= NULL
)
407 return "E.Btrace already enabled.";
409 current_btrace_conf
.format
= BTRACE_FORMAT_BTS
;
410 thread
->btrace
= target_enable_btrace (thread
->entry
.id
,
411 ¤t_btrace_conf
);
412 if (thread
->btrace
== NULL
)
413 return "E.Could not enable btrace.";
418 /* Handle btrace enabling in Intel(R) Processor Trace format. */
421 handle_btrace_enable_pt (struct thread_info
*thread
)
423 if (thread
->btrace
!= NULL
)
424 return "E.Btrace already enabled.";
426 current_btrace_conf
.format
= BTRACE_FORMAT_PT
;
427 thread
->btrace
= target_enable_btrace (thread
->entry
.id
,
428 ¤t_btrace_conf
);
429 if (thread
->btrace
== NULL
)
430 return "E.Could not enable btrace.";
435 /* Handle btrace disabling. */
438 handle_btrace_disable (struct thread_info
*thread
)
441 if (thread
->btrace
== NULL
)
442 return "E.Branch tracing not enabled.";
444 if (target_disable_btrace (thread
->btrace
) != 0)
445 return "E.Could not disable branch tracing.";
447 thread
->btrace
= NULL
;
451 /* Handle the "Qbtrace" packet. */
454 handle_btrace_general_set (char *own_buf
)
456 struct thread_info
*thread
;
460 if (!startswith (own_buf
, "Qbtrace:"))
463 op
= own_buf
+ strlen ("Qbtrace:");
465 if (ptid_equal (general_thread
, null_ptid
)
466 || ptid_equal (general_thread
, minus_one_ptid
))
468 strcpy (own_buf
, "E.Must select a single thread.");
472 thread
= find_thread_ptid (general_thread
);
475 strcpy (own_buf
, "E.No such thread.");
481 if (strcmp (op
, "bts") == 0)
482 err
= handle_btrace_enable_bts (thread
);
483 else if (strcmp (op
, "pt") == 0)
484 err
= handle_btrace_enable_pt (thread
);
485 else if (strcmp (op
, "off") == 0)
486 err
= handle_btrace_disable (thread
);
488 err
= "E.Bad Qbtrace operation. Use bts, pt, or off.";
491 strcpy (own_buf
, err
);
498 /* Handle the "Qbtrace-conf" packet. */
501 handle_btrace_conf_general_set (char *own_buf
)
503 struct thread_info
*thread
;
506 if (!startswith (own_buf
, "Qbtrace-conf:"))
509 op
= own_buf
+ strlen ("Qbtrace-conf:");
511 if (ptid_equal (general_thread
, null_ptid
)
512 || ptid_equal (general_thread
, minus_one_ptid
))
514 strcpy (own_buf
, "E.Must select a single thread.");
518 thread
= find_thread_ptid (general_thread
);
521 strcpy (own_buf
, "E.No such thread.");
525 if (startswith (op
, "bts:size="))
531 size
= strtoul (op
+ strlen ("bts:size="), &endp
, 16);
532 if (endp
== NULL
|| *endp
!= 0 || errno
!= 0 || size
> UINT_MAX
)
534 strcpy (own_buf
, "E.Bad size value.");
538 current_btrace_conf
.bts
.size
= (unsigned int) size
;
540 else if (strncmp (op
, "pt:size=", strlen ("pt:size=")) == 0)
546 size
= strtoul (op
+ strlen ("pt:size="), &endp
, 16);
547 if (endp
== NULL
|| *endp
!= 0 || errno
!= 0 || size
> UINT_MAX
)
549 strcpy (own_buf
, "E.Bad size value.");
553 current_btrace_conf
.pt
.size
= (unsigned int) size
;
557 strcpy (own_buf
, "E.Bad Qbtrace configuration option.");
565 /* Handle all of the extended 'Q' packets. */
568 handle_general_set (char *own_buf
)
570 if (startswith (own_buf
, "QPassSignals:"))
572 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
573 const char *p
= own_buf
+ strlen ("QPassSignals:");
576 p
= decode_address_to_semicolon (&cursig
, p
);
577 for (i
= 0; i
< numsigs
; i
++)
583 /* Keep looping, to clear the remaining signals. */
586 p
= decode_address_to_semicolon (&cursig
, p
);
591 strcpy (own_buf
, "OK");
595 if (startswith (own_buf
, "QProgramSignals:"))
597 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
598 const char *p
= own_buf
+ strlen ("QProgramSignals:");
601 program_signals_p
= 1;
603 p
= decode_address_to_semicolon (&cursig
, p
);
604 for (i
= 0; i
< numsigs
; i
++)
608 program_signals
[i
] = 1;
610 /* Keep looping, to clear the remaining signals. */
613 p
= decode_address_to_semicolon (&cursig
, p
);
616 program_signals
[i
] = 0;
618 strcpy (own_buf
, "OK");
622 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
626 fprintf (stderr
, "[noack mode enabled]\n");
635 if (startswith (own_buf
, "QNonStop:"))
637 char *mode
= own_buf
+ 9;
641 if (strcmp (mode
, "0") == 0)
643 else if (strcmp (mode
, "1") == 0)
647 /* We don't know what this mode is, so complain to
649 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
655 req_str
= req
? "non-stop" : "all-stop";
656 if (start_non_stop (req
) != 0)
658 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
666 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
672 if (startswith (own_buf
, "QDisableRandomization:"))
674 char *packet
= own_buf
+ strlen ("QDisableRandomization:");
677 unpack_varlen_hex (packet
, &setting
);
678 disable_randomization
= setting
;
682 if (disable_randomization
)
683 fprintf (stderr
, "[address space randomization disabled]\n");
685 fprintf (stderr
, "[address space randomization enabled]\n");
692 if (target_supports_tracepoints ()
693 && handle_tracepoint_general_set (own_buf
))
696 if (startswith (own_buf
, "QAgent:"))
698 char *mode
= own_buf
+ strlen ("QAgent:");
701 if (strcmp (mode
, "0") == 0)
703 else if (strcmp (mode
, "1") == 0)
707 /* We don't know what this value is, so complain to GDB. */
708 sprintf (own_buf
, "E.Unknown QAgent value");
712 /* Update the flag. */
715 fprintf (stderr
, "[%s agent]\n", req
? "Enable" : "Disable");
720 if (handle_btrace_general_set (own_buf
))
723 if (handle_btrace_conf_general_set (own_buf
))
726 /* Otherwise we didn't know what packet it was. Say we didn't
732 get_features_xml (const char *annex
)
734 const struct target_desc
*desc
= current_target_desc ();
736 /* `desc->xmltarget' defines what to return when looking for the
737 "target.xml" file. Its contents can either be verbatim XML code
738 (prefixed with a '@') or else the name of the actual XML file to
739 be used in place of "target.xml".
741 This variable is set up from the auto-generated
742 init_registers_... routine for the current target. */
744 if (desc
->xmltarget
!= NULL
&& strcmp (annex
, "target.xml") == 0)
746 if (*desc
->xmltarget
== '@')
747 return desc
->xmltarget
+ 1;
749 annex
= desc
->xmltarget
;
754 extern const char *const xml_builtin
[][2];
757 /* Look for the annex. */
758 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
759 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
762 if (xml_builtin
[i
][0] != NULL
)
763 return xml_builtin
[i
][1];
771 monitor_show_help (void)
773 monitor_output ("The following monitor commands are supported:\n");
774 monitor_output (" set debug <0|1>\n");
775 monitor_output (" Enable general debugging messages\n");
776 monitor_output (" set debug-hw-points <0|1>\n");
777 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
778 monitor_output (" set remote-debug <0|1>\n");
779 monitor_output (" Enable remote protocol debugging messages\n");
780 monitor_output (" set debug-format option1[,option2,...]\n");
781 monitor_output (" Add additional information to debugging messages\n");
782 monitor_output (" Options: all, none");
783 monitor_output (", timestamp");
784 monitor_output ("\n");
785 monitor_output (" exit\n");
786 monitor_output (" Quit GDBserver\n");
789 /* Read trace frame or inferior memory. Returns the number of bytes
790 actually read, zero when no further transfer is possible, and -1 on
791 error. Return of a positive value smaller than LEN does not
792 indicate there's no more to be read, only the end of the transfer.
793 E.g., when GDB reads memory from a traceframe, a first request may
794 be served from a memory block that does not cover the whole request
795 length. A following request gets the rest served from either
796 another block (of the same traceframe) or from the read-only
800 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
804 if (current_traceframe
>= 0)
807 ULONGEST length
= len
;
809 if (traceframe_read_mem (current_traceframe
,
810 memaddr
, myaddr
, len
, &nbytes
))
812 /* Data read from trace buffer, we're done. */
815 if (!in_readonly_region (memaddr
, length
))
817 /* Otherwise we have a valid readonly case, fall through. */
818 /* (assume no half-trace half-real blocks for now) */
821 res
= prepare_to_access_memory ();
824 if (set_desired_thread (1))
825 res
= read_inferior_memory (memaddr
, myaddr
, len
);
828 done_accessing_memory ();
830 return res
== 0 ? len
: -1;
836 /* Write trace frame or inferior memory. Actually, writing to trace
837 frames is forbidden. */
840 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
842 if (current_traceframe
>= 0)
848 ret
= prepare_to_access_memory ();
851 if (set_desired_thread (1))
852 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
855 done_accessing_memory ();
861 /* Subroutine of handle_search_memory to simplify it. */
864 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
865 gdb_byte
*pattern
, unsigned pattern_len
,
866 gdb_byte
*search_buf
,
867 unsigned chunk_size
, unsigned search_buf_size
,
868 CORE_ADDR
*found_addrp
)
870 /* Prime the search buffer. */
872 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
875 warning ("Unable to access %ld bytes of target "
876 "memory at 0x%lx, halting search.",
877 (long) search_buf_size
, (long) start_addr
);
881 /* Perform the search.
883 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
884 When we've scanned N bytes we copy the trailing bytes to the start and
885 read in another N bytes. */
887 while (search_space_len
>= pattern_len
)
890 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
894 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
896 if (found_ptr
!= NULL
)
898 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
899 *found_addrp
= found_addr
;
903 /* Not found in this chunk, skip to next chunk. */
905 /* Don't let search_space_len wrap here, it's unsigned. */
906 if (search_space_len
>= chunk_size
)
907 search_space_len
-= chunk_size
;
909 search_space_len
= 0;
911 if (search_space_len
>= pattern_len
)
913 unsigned keep_len
= search_buf_size
- chunk_size
;
914 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
917 /* Copy the trailing part of the previous iteration to the front
918 of the buffer for the next iteration. */
919 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
921 nr_to_read
= (search_space_len
- keep_len
< chunk_size
922 ? search_space_len
- keep_len
925 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
926 nr_to_read
) != search_buf_size
)
928 warning ("Unable to access %ld bytes of target memory "
929 "at 0x%lx, halting search.",
930 (long) nr_to_read
, (long) read_addr
);
934 start_addr
+= chunk_size
;
943 /* Handle qSearch:memory packets. */
946 handle_search_memory (char *own_buf
, int packet_len
)
948 CORE_ADDR start_addr
;
949 CORE_ADDR search_space_len
;
951 unsigned int pattern_len
;
952 /* NOTE: also defined in find.c testcase. */
953 #define SEARCH_CHUNK_SIZE 16000
954 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
955 /* Buffer to hold memory contents for searching. */
956 gdb_byte
*search_buf
;
957 unsigned search_buf_size
;
959 CORE_ADDR found_addr
;
960 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
962 pattern
= (gdb_byte
*) malloc (packet_len
);
965 error ("Unable to allocate memory to perform the search");
966 strcpy (own_buf
, "E00");
969 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
970 packet_len
- cmd_name_len
,
971 &start_addr
, &search_space_len
,
972 pattern
, &pattern_len
) < 0)
975 error ("Error in parsing qSearch:memory packet");
976 strcpy (own_buf
, "E00");
980 search_buf_size
= chunk_size
+ pattern_len
- 1;
982 /* No point in trying to allocate a buffer larger than the search space. */
983 if (search_space_len
< search_buf_size
)
984 search_buf_size
= search_space_len
;
986 search_buf
= (gdb_byte
*) malloc (search_buf_size
);
987 if (search_buf
== NULL
)
990 error ("Unable to allocate memory to perform the search");
991 strcpy (own_buf
, "E00");
995 found
= handle_search_memory_1 (start_addr
, search_space_len
,
996 pattern
, pattern_len
,
997 search_buf
, chunk_size
, search_buf_size
,
1001 sprintf (own_buf
, "1,%lx", (long) found_addr
);
1002 else if (found
== 0)
1003 strcpy (own_buf
, "0");
1005 strcpy (own_buf
, "E00");
1011 #define require_running(BUF) \
1012 if (!target_running ()) \
1018 /* Parse options to --debug-format= and "monitor set debug-format".
1019 ARG is the text after "--debug-format=" or "monitor set debug-format".
1020 IS_MONITOR is non-zero if we're invoked via "monitor set debug-format".
1021 This triggers calls to monitor_output.
1022 The result is NULL if all options were parsed ok, otherwise an error
1023 message which the caller must free.
1025 N.B. These commands affect all debug format settings, they are not
1026 cumulative. If a format is not specified, it is turned off.
1027 However, we don't go to extra trouble with things like
1028 "monitor set debug-format all,none,timestamp".
1029 Instead we just parse them one at a time, in order.
1031 The syntax for "monitor set debug" we support here is not identical
1032 to gdb's "set debug foo on|off" because we also use this function to
1033 parse "--debug-format=foo,bar". */
1036 parse_debug_format_options (const char *arg
, int is_monitor
)
1038 VEC (char_ptr
) *options
;
1042 /* First turn all debug format options off. */
1043 debug_timestamp
= 0;
1045 /* First remove leading spaces, for "monitor set debug-format". */
1046 while (isspace (*arg
))
1049 options
= delim_string_to_char_ptr_vec (arg
, ',');
1051 for (ix
= 0; VEC_iterate (char_ptr
, options
, ix
, option
); ++ix
)
1053 if (strcmp (option
, "all") == 0)
1055 debug_timestamp
= 1;
1057 monitor_output ("All extra debug format options enabled.\n");
1059 else if (strcmp (option
, "none") == 0)
1061 debug_timestamp
= 0;
1063 monitor_output ("All extra debug format options disabled.\n");
1065 else if (strcmp (option
, "timestamp") == 0)
1067 debug_timestamp
= 1;
1069 monitor_output ("Timestamps will be added to debug output.\n");
1071 else if (*option
== '\0')
1073 /* An empty option, e.g., "--debug-format=foo,,bar", is ignored. */
1078 char *msg
= xstrprintf ("Unknown debug-format argument: \"%s\"\n",
1081 free_char_ptr_vec (options
);
1086 free_char_ptr_vec (options
);
1090 /* Handle monitor commands not handled by target-specific handlers. */
1093 handle_monitor_command (char *mon
, char *own_buf
)
1095 if (strcmp (mon
, "set debug 1") == 0)
1098 monitor_output ("Debug output enabled.\n");
1100 else if (strcmp (mon
, "set debug 0") == 0)
1103 monitor_output ("Debug output disabled.\n");
1105 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
1107 show_debug_regs
= 1;
1108 monitor_output ("H/W point debugging output enabled.\n");
1110 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
1112 show_debug_regs
= 0;
1113 monitor_output ("H/W point debugging output disabled.\n");
1115 else if (strcmp (mon
, "set remote-debug 1") == 0)
1118 monitor_output ("Protocol debug output enabled.\n");
1120 else if (strcmp (mon
, "set remote-debug 0") == 0)
1123 monitor_output ("Protocol debug output disabled.\n");
1125 else if (startswith (mon
, "set debug-format "))
1128 = parse_debug_format_options (mon
+ sizeof ("set debug-format ") - 1,
1131 if (error_msg
!= NULL
)
1133 monitor_output (error_msg
);
1134 monitor_show_help ();
1135 write_enn (own_buf
);
1139 else if (strcmp (mon
, "help") == 0)
1140 monitor_show_help ();
1141 else if (strcmp (mon
, "exit") == 0)
1145 monitor_output ("Unknown monitor command.\n\n");
1146 monitor_show_help ();
1147 write_enn (own_buf
);
1151 /* Associates a callback with each supported qXfer'able object. */
1155 /* The object this handler handles. */
1158 /* Request that the target transfer up to LEN 8-bit bytes of the
1159 target's OBJECT. The OFFSET, for a seekable object, specifies
1160 the starting point. The ANNEX can be used to provide additional
1161 data-specific information to the target.
1163 Return the number of bytes actually transfered, zero when no
1164 further transfer is possible, -1 on error, -2 when the transfer
1165 is not supported, and -3 on a verbose error message that should
1166 be preserved. Return of a positive value smaller than LEN does
1167 not indicate the end of the object, only the end of the transfer.
1169 One, and only one, of readbuf or writebuf must be non-NULL. */
1170 int (*xfer
) (const char *annex
,
1171 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1172 ULONGEST offset
, LONGEST len
);
1175 /* Handle qXfer:auxv:read. */
1178 handle_qxfer_auxv (const char *annex
,
1179 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1180 ULONGEST offset
, LONGEST len
)
1182 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
1185 if (annex
[0] != '\0' || current_thread
== NULL
)
1188 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
1191 /* Handle qXfer:exec-file:read. */
1194 handle_qxfer_exec_file (const char *const_annex
,
1195 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1196 ULONGEST offset
, LONGEST len
)
1202 if (the_target
->pid_to_exec_file
== NULL
|| writebuf
!= NULL
)
1205 if (const_annex
[0] == '\0')
1207 if (current_thread
== NULL
)
1210 pid
= pid_of (current_thread
);
1214 char *annex
= (char *) alloca (strlen (const_annex
) + 1);
1216 strcpy (annex
, const_annex
);
1217 annex
= unpack_varlen_hex (annex
, &pid
);
1219 if (annex
[0] != '\0')
1226 file
= (*the_target
->pid_to_exec_file
) (pid
);
1230 total_len
= strlen (file
);
1232 if (offset
> total_len
)
1235 if (offset
+ len
> total_len
)
1236 len
= total_len
- offset
;
1238 memcpy (readbuf
, file
+ offset
, len
);
1242 /* Handle qXfer:features:read. */
1245 handle_qxfer_features (const char *annex
,
1246 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1247 ULONGEST offset
, LONGEST len
)
1249 const char *document
;
1252 if (writebuf
!= NULL
)
1255 if (!target_running ())
1258 /* Grab the correct annex. */
1259 document
= get_features_xml (annex
);
1260 if (document
== NULL
)
1263 total_len
= strlen (document
);
1265 if (offset
> total_len
)
1268 if (offset
+ len
> total_len
)
1269 len
= total_len
- offset
;
1271 memcpy (readbuf
, document
+ offset
, len
);
1275 /* Worker routine for handle_qxfer_libraries.
1276 Add to the length pointed to by ARG a conservative estimate of the
1277 length needed to transmit the file name of INF. */
1280 accumulate_file_name_length (struct inferior_list_entry
*inf
, void *arg
)
1282 struct dll_info
*dll
= (struct dll_info
*) inf
;
1283 unsigned int *total_len
= (unsigned int *) arg
;
1285 /* Over-estimate the necessary memory. Assume that every character
1286 in the library name must be escaped. */
1287 *total_len
+= 128 + 6 * strlen (dll
->name
);
1290 /* Worker routine for handle_qxfer_libraries.
1291 Emit the XML to describe the library in INF. */
1294 emit_dll_description (struct inferior_list_entry
*inf
, void *arg
)
1296 struct dll_info
*dll
= (struct dll_info
*) inf
;
1297 char **p_ptr
= (char **) arg
;
1301 strcpy (p
, " <library name=\"");
1303 name
= xml_escape_text (dll
->name
);
1307 strcpy (p
, "\"><segment address=\"");
1309 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1311 strcpy (p
, "\"/></library>\n");
1317 /* Handle qXfer:libraries:read. */
1320 handle_qxfer_libraries (const char *annex
,
1321 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1322 ULONGEST offset
, LONGEST len
)
1324 unsigned int total_len
;
1327 if (writebuf
!= NULL
)
1330 if (annex
[0] != '\0' || current_thread
== NULL
)
1334 for_each_inferior_with_data (&all_dlls
, accumulate_file_name_length
,
1337 document
= (char *) malloc (total_len
);
1338 if (document
== NULL
)
1341 strcpy (document
, "<library-list version=\"1.0\">\n");
1342 p
= document
+ strlen (document
);
1344 for_each_inferior_with_data (&all_dlls
, emit_dll_description
, &p
);
1346 strcpy (p
, "</library-list>\n");
1348 total_len
= strlen (document
);
1350 if (offset
> total_len
)
1356 if (offset
+ len
> total_len
)
1357 len
= total_len
- offset
;
1359 memcpy (readbuf
, document
+ offset
, len
);
1364 /* Handle qXfer:libraries-svr4:read. */
1367 handle_qxfer_libraries_svr4 (const char *annex
,
1368 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1369 ULONGEST offset
, LONGEST len
)
1371 if (writebuf
!= NULL
)
1374 if (current_thread
== NULL
|| the_target
->qxfer_libraries_svr4
== NULL
)
1377 return the_target
->qxfer_libraries_svr4 (annex
, readbuf
, writebuf
, offset
, len
);
1380 /* Handle qXfer:osadata:read. */
1383 handle_qxfer_osdata (const char *annex
,
1384 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1385 ULONGEST offset
, LONGEST len
)
1387 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
1390 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
1393 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1396 handle_qxfer_siginfo (const char *annex
,
1397 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1398 ULONGEST offset
, LONGEST len
)
1400 if (the_target
->qxfer_siginfo
== NULL
)
1403 if (annex
[0] != '\0' || current_thread
== NULL
)
1406 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1409 /* Handle qXfer:spu:read and qXfer:spu:write. */
1412 handle_qxfer_spu (const char *annex
,
1413 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1414 ULONGEST offset
, LONGEST len
)
1416 if (the_target
->qxfer_spu
== NULL
)
1419 if (current_thread
== NULL
)
1422 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1425 /* Handle qXfer:statictrace:read. */
1428 handle_qxfer_statictrace (const char *annex
,
1429 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1430 ULONGEST offset
, LONGEST len
)
1434 if (writebuf
!= NULL
)
1437 if (annex
[0] != '\0' || current_thread
== NULL
|| current_traceframe
== -1)
1440 if (traceframe_read_sdata (current_traceframe
, offset
,
1441 readbuf
, len
, &nbytes
))
1446 /* Helper for handle_qxfer_threads_proper.
1447 Emit the XML to describe the thread of INF. */
1450 handle_qxfer_threads_worker (struct inferior_list_entry
*inf
, void *arg
)
1452 struct thread_info
*thread
= (struct thread_info
*) inf
;
1453 struct buffer
*buffer
= (struct buffer
*) arg
;
1454 ptid_t ptid
= thread_to_gdb_id (thread
);
1456 int core
= target_core_of_thread (ptid
);
1459 write_ptid (ptid_s
, ptid
);
1463 sprintf (core_s
, "%d", core
);
1464 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1469 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1474 /* Helper for handle_qxfer_threads. */
1477 handle_qxfer_threads_proper (struct buffer
*buffer
)
1479 buffer_grow_str (buffer
, "<threads>\n");
1481 for_each_inferior_with_data (&all_threads
, handle_qxfer_threads_worker
,
1484 buffer_grow_str0 (buffer
, "</threads>\n");
1487 /* Handle qXfer:threads:read. */
1490 handle_qxfer_threads (const char *annex
,
1491 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1492 ULONGEST offset
, LONGEST len
)
1494 static char *result
= 0;
1495 static unsigned int result_length
= 0;
1497 if (writebuf
!= NULL
)
1500 if (annex
[0] != '\0')
1505 struct buffer buffer
;
1506 /* When asked for data at offset 0, generate everything and store into
1507 'result'. Successive reads will be served off 'result'. */
1511 buffer_init (&buffer
);
1513 handle_qxfer_threads_proper (&buffer
);
1515 result
= buffer_finish (&buffer
);
1516 result_length
= strlen (result
);
1517 buffer_free (&buffer
);
1520 if (offset
>= result_length
)
1522 /* We're out of data. */
1529 if (len
> result_length
- offset
)
1530 len
= result_length
- offset
;
1532 memcpy (readbuf
, result
+ offset
, len
);
1537 /* Handle qXfer:traceframe-info:read. */
1540 handle_qxfer_traceframe_info (const char *annex
,
1541 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1542 ULONGEST offset
, LONGEST len
)
1544 static char *result
= 0;
1545 static unsigned int result_length
= 0;
1547 if (writebuf
!= NULL
)
1550 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1555 struct buffer buffer
;
1557 /* When asked for data at offset 0, generate everything and
1558 store into 'result'. Successive reads will be served off
1562 buffer_init (&buffer
);
1564 traceframe_read_info (current_traceframe
, &buffer
);
1566 result
= buffer_finish (&buffer
);
1567 result_length
= strlen (result
);
1568 buffer_free (&buffer
);
1571 if (offset
>= result_length
)
1573 /* We're out of data. */
1580 if (len
> result_length
- offset
)
1581 len
= result_length
- offset
;
1583 memcpy (readbuf
, result
+ offset
, len
);
1587 /* Handle qXfer:fdpic:read. */
1590 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1591 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1593 if (the_target
->read_loadmap
== NULL
)
1596 if (current_thread
== NULL
)
1599 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1602 /* Handle qXfer:btrace:read. */
1605 handle_qxfer_btrace (const char *annex
,
1606 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1607 ULONGEST offset
, LONGEST len
)
1609 static struct buffer cache
;
1610 struct thread_info
*thread
;
1611 enum btrace_read_type type
;
1614 if (the_target
->read_btrace
== NULL
|| writebuf
!= NULL
)
1617 if (ptid_equal (general_thread
, null_ptid
)
1618 || ptid_equal (general_thread
, minus_one_ptid
))
1620 strcpy (own_buf
, "E.Must select a single thread.");
1624 thread
= find_thread_ptid (general_thread
);
1627 strcpy (own_buf
, "E.No such thread.");
1631 if (thread
->btrace
== NULL
)
1633 strcpy (own_buf
, "E.Btrace not enabled.");
1637 if (strcmp (annex
, "all") == 0)
1638 type
= BTRACE_READ_ALL
;
1639 else if (strcmp (annex
, "new") == 0)
1640 type
= BTRACE_READ_NEW
;
1641 else if (strcmp (annex
, "delta") == 0)
1642 type
= BTRACE_READ_DELTA
;
1645 strcpy (own_buf
, "E.Bad annex.");
1651 buffer_free (&cache
);
1653 result
= target_read_btrace (thread
->btrace
, &cache
, type
);
1656 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1660 else if (offset
> cache
.used_size
)
1662 buffer_free (&cache
);
1666 if (len
> cache
.used_size
- offset
)
1667 len
= cache
.used_size
- offset
;
1669 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1674 /* Handle qXfer:btrace-conf:read. */
1677 handle_qxfer_btrace_conf (const char *annex
,
1678 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1679 ULONGEST offset
, LONGEST len
)
1681 static struct buffer cache
;
1682 struct thread_info
*thread
;
1685 if (the_target
->read_btrace_conf
== NULL
|| writebuf
!= NULL
)
1688 if (annex
[0] != '\0')
1691 if (ptid_equal (general_thread
, null_ptid
)
1692 || ptid_equal (general_thread
, minus_one_ptid
))
1694 strcpy (own_buf
, "E.Must select a single thread.");
1698 thread
= find_thread_ptid (general_thread
);
1701 strcpy (own_buf
, "E.No such thread.");
1705 if (thread
->btrace
== NULL
)
1707 strcpy (own_buf
, "E.Btrace not enabled.");
1713 buffer_free (&cache
);
1715 result
= target_read_btrace_conf (thread
->btrace
, &cache
);
1718 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1722 else if (offset
> cache
.used_size
)
1724 buffer_free (&cache
);
1728 if (len
> cache
.used_size
- offset
)
1729 len
= cache
.used_size
- offset
;
1731 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1736 static const struct qxfer qxfer_packets
[] =
1738 { "auxv", handle_qxfer_auxv
},
1739 { "btrace", handle_qxfer_btrace
},
1740 { "btrace-conf", handle_qxfer_btrace_conf
},
1741 { "exec-file", handle_qxfer_exec_file
},
1742 { "fdpic", handle_qxfer_fdpic
},
1743 { "features", handle_qxfer_features
},
1744 { "libraries", handle_qxfer_libraries
},
1745 { "libraries-svr4", handle_qxfer_libraries_svr4
},
1746 { "osdata", handle_qxfer_osdata
},
1747 { "siginfo", handle_qxfer_siginfo
},
1748 { "spu", handle_qxfer_spu
},
1749 { "statictrace", handle_qxfer_statictrace
},
1750 { "threads", handle_qxfer_threads
},
1751 { "traceframe-info", handle_qxfer_traceframe_info
},
1755 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1763 if (!startswith (own_buf
, "qXfer:"))
1766 /* Grab the object, r/w and annex. */
1767 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1769 write_enn (own_buf
);
1774 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1777 const struct qxfer
*q
= &qxfer_packets
[i
];
1779 if (strcmp (object
, q
->object
) == 0)
1781 if (strcmp (rw
, "read") == 0)
1783 unsigned char *data
;
1788 /* Grab the offset and length. */
1789 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1791 write_enn (own_buf
);
1795 /* Read one extra byte, as an indicator of whether there is
1797 if (len
> PBUFSIZ
- 2)
1799 data
= (unsigned char *) malloc (len
+ 1);
1802 write_enn (own_buf
);
1805 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1813 /* Preserve error message. */
1816 write_enn (own_buf
);
1818 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1820 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1825 else if (strcmp (rw
, "write") == 0)
1830 unsigned char *data
;
1832 strcpy (own_buf
, "E00");
1833 data
= (unsigned char *) malloc (packet_len
- (offset
- own_buf
));
1836 write_enn (own_buf
);
1839 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1840 &ofs
, &len
, data
) < 0)
1843 write_enn (own_buf
);
1847 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1855 /* Preserve error message. */
1858 write_enn (own_buf
);
1860 sprintf (own_buf
, "%x", n
);
1873 /* Table used by the crc32 function to calcuate the checksum. */
1875 static unsigned int crc32_table
[256] =
1878 /* Compute 32 bit CRC from inferior memory.
1880 On success, return 32 bit CRC.
1881 On failure, return (unsigned long long) -1. */
1883 static unsigned long long
1884 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1886 if (!crc32_table
[1])
1888 /* Initialize the CRC table and the decoding table. */
1892 for (i
= 0; i
< 256; i
++)
1894 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1895 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1902 unsigned char byte
= 0;
1904 /* Return failure if memory read fails. */
1905 if (read_inferior_memory (base
, &byte
, 1) != 0)
1906 return (unsigned long long) -1;
1908 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1911 return (unsigned long long) crc
;
1914 /* Add supported btrace packets to BUF. */
1917 supported_btrace_packets (char *buf
)
1919 int btrace_supported
= 0;
1921 if (target_supports_btrace (BTRACE_FORMAT_BTS
))
1923 strcat (buf
, ";Qbtrace:bts+");
1924 strcat (buf
, ";Qbtrace-conf:bts:size+");
1926 btrace_supported
= 1;
1929 if (target_supports_btrace (BTRACE_FORMAT_PT
))
1931 strcat (buf
, ";Qbtrace:pt+");
1932 strcat (buf
, ";Qbtrace-conf:pt:size+");
1934 btrace_supported
= 1;
1937 if (!btrace_supported
)
1940 strcat (buf
, ";Qbtrace:off+");
1941 strcat (buf
, ";qXfer:btrace:read+");
1942 strcat (buf
, ";qXfer:btrace-conf:read+");
1945 /* Handle all of the extended 'q' packets. */
1948 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1950 static struct inferior_list_entry
*thread_ptr
;
1952 /* Reply the current thread id. */
1953 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1956 require_running (own_buf
);
1958 if (!ptid_equal (general_thread
, null_ptid
)
1959 && !ptid_equal (general_thread
, minus_one_ptid
))
1960 gdb_id
= general_thread
;
1963 thread_ptr
= get_first_inferior (&all_threads
);
1964 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1967 sprintf (own_buf
, "QC");
1969 write_ptid (own_buf
, gdb_id
);
1973 if (strcmp ("qSymbol::", own_buf
) == 0)
1975 /* GDB is suggesting new symbols have been loaded. This may
1976 mean a new shared library has been detected as loaded, so
1977 take the opportunity to check if breakpoints we think are
1978 inserted, still are. Note that it isn't guaranteed that
1979 we'll see this when a shared library is loaded, and nor will
1980 we see this for unloads (although breakpoints in unloaded
1981 libraries shouldn't trigger), as GDB may not find symbols for
1982 the library at all. We also re-validate breakpoints when we
1983 see a second GDB breakpoint for the same address, and or when
1984 we access breakpoint shadows. */
1985 validate_breakpoints ();
1987 if (target_supports_tracepoints ())
1988 tracepoint_look_up_symbols ();
1990 if (current_thread
!= NULL
&& the_target
->look_up_symbols
!= NULL
)
1991 (*the_target
->look_up_symbols
) ();
1993 strcpy (own_buf
, "OK");
1997 if (!disable_packet_qfThreadInfo
)
1999 if (strcmp ("qfThreadInfo", own_buf
) == 0)
2003 require_running (own_buf
);
2004 thread_ptr
= get_first_inferior (&all_threads
);
2007 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
2008 write_ptid (own_buf
, gdb_id
);
2009 thread_ptr
= thread_ptr
->next
;
2013 if (strcmp ("qsThreadInfo", own_buf
) == 0)
2017 require_running (own_buf
);
2018 if (thread_ptr
!= NULL
)
2021 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
2022 write_ptid (own_buf
, gdb_id
);
2023 thread_ptr
= thread_ptr
->next
;
2028 sprintf (own_buf
, "l");
2034 if (the_target
->read_offsets
!= NULL
2035 && strcmp ("qOffsets", own_buf
) == 0)
2037 CORE_ADDR text
, data
;
2039 require_running (own_buf
);
2040 if (the_target
->read_offsets (&text
, &data
))
2041 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
2042 (long)text
, (long)data
, (long)data
);
2044 write_enn (own_buf
);
2049 /* Protocol features query. */
2050 if (startswith (own_buf
, "qSupported")
2051 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
2053 char *p
= &own_buf
[10];
2054 int gdb_supports_qRelocInsn
= 0;
2056 /* Start processing qSupported packet. */
2057 target_process_qsupported (NULL
);
2059 /* Process each feature being provided by GDB. The first
2060 feature will follow a ':', and latter features will follow
2064 char **qsupported
= NULL
;
2068 /* Two passes, to avoid nested strtok calls in
2069 target_process_qsupported. */
2070 for (p
= strtok (p
+ 1, ";");
2072 p
= strtok (NULL
, ";"))
2075 qsupported
= XRESIZEVEC (char *, qsupported
, count
);
2076 qsupported
[count
- 1] = xstrdup (p
);
2079 for (i
= 0; i
< count
; i
++)
2082 if (strcmp (p
, "multiprocess+") == 0)
2084 /* GDB supports and wants multi-process support if
2086 if (target_supports_multi_process ())
2089 else if (strcmp (p
, "qRelocInsn+") == 0)
2091 /* GDB supports relocate instruction requests. */
2092 gdb_supports_qRelocInsn
= 1;
2094 else if (strcmp (p
, "swbreak+") == 0)
2096 /* GDB wants us to report whether a trap is caused
2097 by a software breakpoint and for us to handle PC
2098 adjustment if necessary on this target. */
2099 if (target_supports_stopped_by_sw_breakpoint ())
2100 swbreak_feature
= 1;
2102 else if (strcmp (p
, "hwbreak+") == 0)
2104 /* GDB wants us to report whether a trap is caused
2105 by a hardware breakpoint. */
2106 if (target_supports_stopped_by_hw_breakpoint ())
2107 hwbreak_feature
= 1;
2109 else if (strcmp (p
, "fork-events+") == 0)
2111 /* GDB supports and wants fork events if possible. */
2112 if (target_supports_fork_events ())
2113 report_fork_events
= 1;
2115 else if (strcmp (p
, "vfork-events+") == 0)
2117 /* GDB supports and wants vfork events if possible. */
2118 if (target_supports_vfork_events ())
2119 report_vfork_events
= 1;
2121 else if (strcmp (p
, "exec-events+") == 0)
2123 /* GDB supports and wants exec events if possible. */
2124 if (target_supports_exec_events ())
2125 report_exec_events
= 1;
2127 else if (strcmp (p
, "vContSupported+") == 0)
2128 vCont_supported
= 1;
2130 target_process_qsupported (p
);
2139 "PacketSize=%x;QPassSignals+;QProgramSignals+",
2142 if (the_target
->qxfer_libraries_svr4
!= NULL
)
2143 strcat (own_buf
, ";qXfer:libraries-svr4:read+"
2144 ";augmented-libraries-svr4-read+");
2147 /* We do not have any hook to indicate whether the non-SVR4 target
2148 backend supports qXfer:libraries:read, so always report it. */
2149 strcat (own_buf
, ";qXfer:libraries:read+");
2152 if (the_target
->read_auxv
!= NULL
)
2153 strcat (own_buf
, ";qXfer:auxv:read+");
2155 if (the_target
->qxfer_spu
!= NULL
)
2156 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
2158 if (the_target
->qxfer_siginfo
!= NULL
)
2159 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
2161 if (the_target
->read_loadmap
!= NULL
)
2162 strcat (own_buf
, ";qXfer:fdpic:read+");
2164 /* We always report qXfer:features:read, as targets may
2165 install XML files on a subsequent call to arch_setup.
2166 If we reported to GDB on startup that we don't support
2167 qXfer:feature:read at all, we will never be re-queried. */
2168 strcat (own_buf
, ";qXfer:features:read+");
2170 if (transport_is_reliable
)
2171 strcat (own_buf
, ";QStartNoAckMode+");
2173 if (the_target
->qxfer_osdata
!= NULL
)
2174 strcat (own_buf
, ";qXfer:osdata:read+");
2176 if (target_supports_multi_process ())
2177 strcat (own_buf
, ";multiprocess+");
2179 if (target_supports_fork_events ())
2180 strcat (own_buf
, ";fork-events+");
2182 if (target_supports_vfork_events ())
2183 strcat (own_buf
, ";vfork-events+");
2185 if (target_supports_exec_events ())
2186 strcat (own_buf
, ";exec-events+");
2188 if (target_supports_non_stop ())
2189 strcat (own_buf
, ";QNonStop+");
2191 if (target_supports_disable_randomization ())
2192 strcat (own_buf
, ";QDisableRandomization+");
2194 strcat (own_buf
, ";qXfer:threads:read+");
2196 if (target_supports_tracepoints ())
2198 strcat (own_buf
, ";ConditionalTracepoints+");
2199 strcat (own_buf
, ";TraceStateVariables+");
2200 strcat (own_buf
, ";TracepointSource+");
2201 strcat (own_buf
, ";DisconnectedTracing+");
2202 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
2203 strcat (own_buf
, ";FastTracepoints+");
2204 strcat (own_buf
, ";StaticTracepoints+");
2205 strcat (own_buf
, ";InstallInTrace+");
2206 strcat (own_buf
, ";qXfer:statictrace:read+");
2207 strcat (own_buf
, ";qXfer:traceframe-info:read+");
2208 strcat (own_buf
, ";EnableDisableTracepoints+");
2209 strcat (own_buf
, ";QTBuffer:size+");
2210 strcat (own_buf
, ";tracenz+");
2213 if (target_supports_hardware_single_step ())
2215 /* Support target-side breakpoint conditions and commands.
2216 GDBserver needs to step over the breakpoint if the condition
2217 is false. GDBserver software single step is too simple, so
2218 disable conditional breakpoints if the target doesn't have
2219 hardware single step. */
2220 strcat (own_buf
, ";ConditionalBreakpoints+");
2222 strcat (own_buf
, ";BreakpointCommands+");
2224 if (target_supports_agent ())
2225 strcat (own_buf
, ";QAgent+");
2227 supported_btrace_packets (own_buf
);
2229 if (target_supports_stopped_by_sw_breakpoint ())
2230 strcat (own_buf
, ";swbreak+");
2232 if (target_supports_stopped_by_hw_breakpoint ())
2233 strcat (own_buf
, ";hwbreak+");
2235 if (the_target
->pid_to_exec_file
!= NULL
)
2236 strcat (own_buf
, ";qXfer:exec-file:read+");
2238 strcat (own_buf
, ";vContSupported+");
2240 /* Reinitialize components as needed for the new connection. */
2241 hostio_handle_new_gdb_connection ();
2242 target_handle_new_gdb_connection ();
2247 /* Thread-local storage support. */
2248 if (the_target
->get_tls_address
!= NULL
2249 && startswith (own_buf
, "qGetTLSAddr:"))
2251 char *p
= own_buf
+ 12;
2252 CORE_ADDR parts
[2], address
= 0;
2254 ptid_t ptid
= null_ptid
;
2256 require_running (own_buf
);
2258 for (i
= 0; i
< 3; i
++)
2266 p2
= strchr (p
, ',');
2279 ptid
= read_ptid (p
, NULL
);
2281 decode_address (&parts
[i
- 1], p
, len
);
2285 if (p
!= NULL
|| i
< 3)
2289 struct thread_info
*thread
= find_thread_ptid (ptid
);
2294 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
2300 strcpy (own_buf
, paddress(address
));
2305 write_enn (own_buf
);
2309 /* Otherwise, pretend we do not understand this packet. */
2312 /* Windows OS Thread Information Block address support. */
2313 if (the_target
->get_tib_address
!= NULL
2314 && startswith (own_buf
, "qGetTIBAddr:"))
2319 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
2321 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
2324 strcpy (own_buf
, paddress(tlb
));
2329 write_enn (own_buf
);
2335 /* Handle "monitor" commands. */
2336 if (startswith (own_buf
, "qRcmd,"))
2338 char *mon
= (char *) malloc (PBUFSIZ
);
2339 int len
= strlen (own_buf
+ 6);
2343 write_enn (own_buf
);
2348 || hex2bin (own_buf
+ 6, (gdb_byte
*) mon
, len
/ 2) != len
/ 2)
2350 write_enn (own_buf
);
2354 mon
[len
/ 2] = '\0';
2358 if (the_target
->handle_monitor_command
== NULL
2359 || (*the_target
->handle_monitor_command
) (mon
) == 0)
2360 /* Default processing. */
2361 handle_monitor_command (mon
, own_buf
);
2367 if (startswith (own_buf
, "qSearch:memory:"))
2369 require_running (own_buf
);
2370 handle_search_memory (own_buf
, packet_len
);
2374 if (strcmp (own_buf
, "qAttached") == 0
2375 || startswith (own_buf
, "qAttached:"))
2377 struct process_info
*process
;
2379 if (own_buf
[sizeof ("qAttached") - 1])
2381 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
2382 process
= (struct process_info
*)
2383 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
2387 require_running (own_buf
);
2388 process
= current_process ();
2391 if (process
== NULL
)
2393 write_enn (own_buf
);
2397 strcpy (own_buf
, process
->attached
? "1" : "0");
2401 if (startswith (own_buf
, "qCRC:"))
2403 /* CRC check (compare-section). */
2407 unsigned long long crc
;
2409 require_running (own_buf
);
2410 comma
= unpack_varlen_hex (own_buf
+ 5, &base
);
2411 if (*comma
++ != ',')
2413 write_enn (own_buf
);
2416 len
= strtoul (comma
, NULL
, 16);
2417 crc
= crc32 (base
, len
, 0xffffffff);
2418 /* Check for memory failure. */
2419 if (crc
== (unsigned long long) -1)
2421 write_enn (own_buf
);
2424 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
2428 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
2431 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
2434 /* Otherwise we didn't know what packet it was. Say we didn't
2439 static void gdb_wants_all_threads_stopped (void);
2440 static void resume (struct thread_resume
*actions
, size_t n
);
2442 /* The callback that is passed to visit_actioned_threads. */
2443 typedef int (visit_actioned_threads_callback_ftype
)
2444 (const struct thread_resume
*, struct thread_info
*);
2446 /* Struct to pass data to visit_actioned_threads. */
2448 struct visit_actioned_threads_data
2450 const struct thread_resume
*actions
;
2452 visit_actioned_threads_callback_ftype
*callback
;
2455 /* Call CALLBACK for any thread to which ACTIONS applies to. Returns
2456 true if CALLBACK returns true. Returns false if no matching thread
2457 is found or CALLBACK results false.
2458 Note: This function is itself a callback for find_inferior. */
2461 visit_actioned_threads (struct inferior_list_entry
*entry
, void *datap
)
2463 struct visit_actioned_threads_data
*data
2464 = (struct visit_actioned_threads_data
*) datap
;
2465 const struct thread_resume
*actions
= data
->actions
;
2466 size_t num_actions
= data
->num_actions
;
2467 visit_actioned_threads_callback_ftype
*callback
= data
->callback
;
2470 for (i
= 0; i
< num_actions
; i
++)
2472 const struct thread_resume
*action
= &actions
[i
];
2474 if (ptid_equal (action
->thread
, minus_one_ptid
)
2475 || ptid_equal (action
->thread
, entry
->id
)
2476 || ((ptid_get_pid (action
->thread
)
2477 == ptid_get_pid (entry
->id
))
2478 && ptid_get_lwp (action
->thread
) == -1))
2480 struct thread_info
*thread
= (struct thread_info
*) entry
;
2482 if ((*callback
) (action
, thread
))
2490 /* Callback for visit_actioned_threads. If the thread has a pending
2491 status to report, report it now. */
2494 handle_pending_status (const struct thread_resume
*resumption
,
2495 struct thread_info
*thread
)
2497 if (thread
->status_pending_p
)
2499 thread
->status_pending_p
= 0;
2501 last_status
= thread
->last_status
;
2502 last_ptid
= thread
->entry
.id
;
2503 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2509 /* Parse vCont packets. */
2511 handle_v_cont (char *own_buf
)
2515 struct thread_resume
*resume_info
;
2516 struct thread_resume default_action
= {{0}};
2518 /* Count the number of semicolons in the packet. There should be one
2519 for every action. */
2525 p
= strchr (p
, ';');
2528 resume_info
= (struct thread_resume
*) malloc (n
* sizeof (resume_info
[0]));
2529 if (resume_info
== NULL
)
2537 memset (&resume_info
[i
], 0, sizeof resume_info
[i
]);
2539 if (p
[0] == 's' || p
[0] == 'S')
2540 resume_info
[i
].kind
= resume_step
;
2541 else if (p
[0] == 'r')
2542 resume_info
[i
].kind
= resume_step
;
2543 else if (p
[0] == 'c' || p
[0] == 'C')
2544 resume_info
[i
].kind
= resume_continue
;
2545 else if (p
[0] == 't')
2546 resume_info
[i
].kind
= resume_stop
;
2550 if (p
[0] == 'S' || p
[0] == 'C')
2553 sig
= strtol (p
+ 1, &q
, 16);
2558 if (!gdb_signal_to_host_p (sig
))
2560 resume_info
[i
].sig
= gdb_signal_to_host (sig
);
2562 else if (p
[0] == 'r')
2566 p
= unpack_varlen_hex (p
+ 1, &addr
);
2567 resume_info
[i
].step_range_start
= addr
;
2572 p
= unpack_varlen_hex (p
+ 1, &addr
);
2573 resume_info
[i
].step_range_end
= addr
;
2582 resume_info
[i
].thread
= minus_one_ptid
;
2583 default_action
= resume_info
[i
];
2585 /* Note: we don't increment i here, we'll overwrite this entry
2586 the next time through. */
2588 else if (p
[0] == ':')
2590 ptid_t ptid
= read_ptid (p
+ 1, &q
);
2595 if (p
[0] != ';' && p
[0] != 0)
2598 resume_info
[i
].thread
= ptid
;
2605 resume_info
[i
] = default_action
;
2607 resume (resume_info
, n
);
2612 write_enn (own_buf
);
2617 /* Resume target with ACTIONS, an array of NUM_ACTIONS elements. */
2620 resume (struct thread_resume
*actions
, size_t num_actions
)
2624 /* Check if among the threads that GDB wants actioned, there's
2625 one with a pending status to report. If so, skip actually
2626 resuming/stopping and report the pending event
2628 struct visit_actioned_threads_data data
;
2630 data
.actions
= actions
;
2631 data
.num_actions
= num_actions
;
2632 data
.callback
= handle_pending_status
;
2633 if (find_inferior (&all_threads
, visit_actioned_threads
, &data
) != NULL
)
2639 (*the_target
->resume
) (actions
, num_actions
);
2645 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2647 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
)
2649 /* No proper RSP support for this yet. At least return
2651 sprintf (own_buf
, "E.No unwaited-for children left.");
2652 disable_async_io ();
2656 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2657 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
2658 && last_status
.kind
!= TARGET_WAITKIND_NO_RESUMED
)
2659 current_thread
->last_status
= last_status
;
2661 /* From the client's perspective, all-stop mode always stops all
2662 threads implicitly (and the target backend has already done
2663 so by now). Tag all threads as "want-stopped", so we don't
2664 resume them implicitly without the client telling us to. */
2665 gdb_wants_all_threads_stopped ();
2666 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2667 disable_async_io ();
2669 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2670 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2671 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2675 /* Attach to a new program. Return 1 if successful, 0 if failure. */
2677 handle_v_attach (char *own_buf
)
2681 pid
= strtol (own_buf
+ 8, NULL
, 16);
2682 if (pid
!= 0 && attach_inferior (pid
) == 0)
2684 /* Don't report shared library events after attaching, even if
2685 some libraries are preloaded. GDB will always poll the
2686 library list. Avoids the "stopped by shared library event"
2687 notice on the GDB side. */
2692 /* In non-stop, we don't send a resume reply. Stop events
2693 will follow up using the normal notification
2698 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2704 write_enn (own_buf
);
2709 /* Run a new program. Return 1 if successful, 0 if failure. */
2711 handle_v_run (char *own_buf
)
2713 char *p
, *next_p
, **new_argv
;
2717 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
2723 new_argv
= (char **) calloc (new_argc
+ 2, sizeof (char *));
2724 if (new_argv
== NULL
)
2726 write_enn (own_buf
);
2731 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
2733 next_p
= strchr (p
, ';');
2735 next_p
= p
+ strlen (p
);
2737 if (i
== 0 && p
== next_p
)
2741 /* FIXME: Fail request if out of memory instead of dying. */
2742 new_argv
[i
] = (char *) xmalloc (1 + (next_p
- p
) / 2);
2743 hex2bin (p
, (gdb_byte
*) new_argv
[i
], (next_p
- p
) / 2);
2744 new_argv
[i
][(next_p
- p
) / 2] = '\0';
2753 if (new_argv
[0] == NULL
)
2755 /* GDB didn't specify a program to run. Use the program from the
2756 last run with the new argument list. */
2758 if (program_argv
== NULL
)
2760 write_enn (own_buf
);
2761 freeargv (new_argv
);
2765 new_argv
[0] = strdup (program_argv
[0]);
2766 if (new_argv
[0] == NULL
)
2768 write_enn (own_buf
);
2769 freeargv (new_argv
);
2774 /* Free the old argv and install the new one. */
2775 freeargv (program_argv
);
2776 program_argv
= new_argv
;
2778 start_inferior (program_argv
);
2779 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
2781 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2783 /* In non-stop, sending a resume reply doesn't set the general
2784 thread, but GDB assumes a vRun sets it (this is so GDB can
2785 query which is the main thread of the new inferior. */
2787 general_thread
= last_ptid
;
2793 write_enn (own_buf
);
2798 /* Kill process. Return 1 if successful, 0 if failure. */
2800 handle_v_kill (char *own_buf
)
2803 char *p
= &own_buf
[6];
2805 pid
= strtol (p
, NULL
, 16);
2808 if (pid
!= 0 && kill_inferior (pid
) == 0)
2810 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2811 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
2812 last_ptid
= pid_to_ptid (pid
);
2813 discard_queued_stop_replies (last_ptid
);
2819 write_enn (own_buf
);
2824 /* Handle all of the extended 'v' packets. */
2826 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2828 if (!disable_packet_vCont
)
2830 if (startswith (own_buf
, "vCont;"))
2832 require_running (own_buf
);
2833 handle_v_cont (own_buf
);
2837 if (startswith (own_buf
, "vCont?"))
2839 strcpy (own_buf
, "vCont;c;C;t");
2841 if (target_supports_hardware_single_step () || !vCont_supported
)
2843 /* If target supports hardware single step, add actions s
2844 and S to the list of supported actions. On the other
2845 hand, if GDB doesn't request the supported vCont actions
2846 in qSupported packet, add s and S to the list too. */
2847 own_buf
= own_buf
+ strlen (own_buf
);
2848 strcpy (own_buf
, ";s;S");
2851 if (target_supports_range_stepping ())
2853 own_buf
= own_buf
+ strlen (own_buf
);
2854 strcpy (own_buf
, ";r");
2860 if (startswith (own_buf
, "vFile:")
2861 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2864 if (startswith (own_buf
, "vAttach;"))
2866 if ((!extended_protocol
|| !multi_process
) && target_running ())
2868 fprintf (stderr
, "Already debugging a process\n");
2869 write_enn (own_buf
);
2872 handle_v_attach (own_buf
);
2876 if (startswith (own_buf
, "vRun;"))
2878 if ((!extended_protocol
|| !multi_process
) && target_running ())
2880 fprintf (stderr
, "Already debugging a process\n");
2881 write_enn (own_buf
);
2884 handle_v_run (own_buf
);
2888 if (startswith (own_buf
, "vKill;"))
2890 if (!target_running ())
2892 fprintf (stderr
, "No process to kill\n");
2893 write_enn (own_buf
);
2896 handle_v_kill (own_buf
);
2900 if (handle_notif_ack (own_buf
, packet_len
))
2903 /* Otherwise we didn't know what packet it was. Say we didn't
2909 /* Resume thread and wait for another event. In non-stop mode,
2910 don't really wait here, but return immediatelly to the event
2913 myresume (char *own_buf
, int step
, int sig
)
2915 struct thread_resume resume_info
[2];
2917 int valid_cont_thread
;
2919 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2920 && !ptid_equal (cont_thread
, minus_one_ptid
));
2922 if (step
|| sig
|| valid_cont_thread
)
2924 resume_info
[0].thread
= current_ptid
;
2926 resume_info
[0].kind
= resume_step
;
2928 resume_info
[0].kind
= resume_continue
;
2929 resume_info
[0].sig
= sig
;
2933 if (!valid_cont_thread
)
2935 resume_info
[n
].thread
= minus_one_ptid
;
2936 resume_info
[n
].kind
= resume_continue
;
2937 resume_info
[n
].sig
= 0;
2941 resume (resume_info
, n
);
2944 /* Callback for for_each_inferior. Make a new stop reply for each
2948 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2950 struct thread_info
*thread
= (struct thread_info
*) entry
;
2952 /* For now, assume targets that don't have this callback also don't
2953 manage the thread's last_status field. */
2954 if (the_target
->thread_stopped
== NULL
)
2956 struct vstop_notif
*new_notif
= XNEW (struct vstop_notif
);
2958 new_notif
->ptid
= entry
->id
;
2959 new_notif
->status
= thread
->last_status
;
2960 /* Pass the last stop reply back to GDB, but don't notify
2962 notif_event_enque (¬if_stop
,
2963 (struct notif_event
*) new_notif
);
2967 if (thread_stopped (thread
))
2972 = target_waitstatus_to_string (&thread
->last_status
);
2974 debug_printf ("Reporting thread %s as already stopped with %s\n",
2975 target_pid_to_str (entry
->id
),
2978 xfree (status_string
);
2981 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2983 /* Pass the last stop reply back to GDB, but don't notify
2985 queue_stop_reply (entry
->id
, &thread
->last_status
);
2992 /* Set this inferior threads's state as "want-stopped". We won't
2993 resume this thread until the client gives us another action for
2997 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2999 struct thread_info
*thread
= (struct thread_info
*) entry
;
3001 thread
->last_resume_kind
= resume_stop
;
3003 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
3005 /* Most threads are stopped implicitly (all-stop); tag that with
3007 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
3008 thread
->last_status
.value
.sig
= GDB_SIGNAL_0
;
3012 /* Set all threads' states as "want-stopped". */
3015 gdb_wants_all_threads_stopped (void)
3017 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
3020 /* Clear the gdb_detached flag of every process. */
3023 gdb_reattached_process (struct inferior_list_entry
*entry
)
3025 struct process_info
*process
= (struct process_info
*) entry
;
3027 process
->gdb_detached
= 0;
3030 /* Callback for for_each_inferior. Clear the thread's pending status
3034 clear_pending_status_callback (struct inferior_list_entry
*entry
)
3036 struct thread_info
*thread
= (struct thread_info
*) entry
;
3038 thread
->status_pending_p
= 0;
3041 /* Callback for for_each_inferior. If the thread is stopped with an
3042 interesting event, mark it as having a pending event. */
3045 set_pending_status_callback (struct inferior_list_entry
*entry
)
3047 struct thread_info
*thread
= (struct thread_info
*) entry
;
3049 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
3050 || (thread
->last_status
.value
.sig
!= GDB_SIGNAL_0
3051 /* A breakpoint, watchpoint or finished step from a previous
3052 GDB run isn't considered interesting for a new GDB run.
3053 If we left those pending, the new GDB could consider them
3054 random SIGTRAPs. This leaves out real async traps. We'd
3055 have to peek into the (target-specific) siginfo to
3056 distinguish those. */
3057 && thread
->last_status
.value
.sig
!= GDB_SIGNAL_TRAP
))
3058 thread
->status_pending_p
= 1;
3061 /* Callback for find_inferior. Return true if ENTRY (a thread) has a
3062 pending status to report to GDB. */
3065 find_status_pending_thread_callback (struct inferior_list_entry
*entry
, void *data
)
3067 struct thread_info
*thread
= (struct thread_info
*) entry
;
3069 return thread
->status_pending_p
;
3072 /* Status handler for the '?' packet. */
3075 handle_status (char *own_buf
)
3077 /* GDB is connected, don't forward events to the target anymore. */
3078 for_each_inferior (&all_processes
, gdb_reattached_process
);
3080 /* In non-stop mode, we must send a stop reply for each stopped
3081 thread. In all-stop mode, just send one for the first stopped
3086 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
3088 /* The first is sent immediatly. OK is sent if there is no
3089 stopped thread, which is the same handling of the vStopped
3090 packet (by design). */
3091 notif_write_event (¬if_stop
, own_buf
);
3095 struct inferior_list_entry
*thread
= NULL
;
3098 stabilize_threads ();
3099 gdb_wants_all_threads_stopped ();
3101 /* We can only report one status, but we might be coming out of
3102 non-stop -- if more than one thread is stopped with
3103 interesting events, leave events for the threads we're not
3104 reporting now pending. They'll be reported the next time the
3105 threads are resumed. Start by marking all interesting events
3107 for_each_inferior (&all_threads
, set_pending_status_callback
);
3109 /* Prefer the last thread that reported an event to GDB (even if
3110 that was a GDB_SIGNAL_TRAP). */
3111 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
3112 && last_status
.kind
!= TARGET_WAITKIND_EXITED
3113 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
3114 thread
= find_inferior_id (&all_threads
, last_ptid
);
3116 /* If the last event thread is not found for some reason, look
3117 for some other thread that might have an event to report. */
3119 thread
= find_inferior (&all_threads
,
3120 find_status_pending_thread_callback
, NULL
);
3122 /* If we're still out of luck, simply pick the first thread in
3125 thread
= get_first_inferior (&all_threads
);
3129 struct thread_info
*tp
= (struct thread_info
*) thread
;
3131 /* We're reporting this event, so it's no longer
3133 tp
->status_pending_p
= 0;
3135 /* GDB assumes the current thread is the thread we're
3136 reporting the status for. */
3137 general_thread
= thread
->id
;
3138 set_desired_thread (1);
3140 gdb_assert (tp
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
3141 prepare_resume_reply (own_buf
, tp
->entry
.id
, &tp
->last_status
);
3144 strcpy (own_buf
, "W00");
3149 gdbserver_version (void)
3151 printf ("GNU gdbserver %s%s\n"
3152 "Copyright (C) 2015 Free Software Foundation, Inc.\n"
3153 "gdbserver is free software, covered by the "
3154 "GNU General Public License.\n"
3155 "This gdbserver was configured as \"%s\"\n",
3156 PKGVERSION
, version
, host_name
);
3160 gdbserver_usage (FILE *stream
)
3162 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
3163 "\tgdbserver [OPTIONS] --attach COMM PID\n"
3164 "\tgdbserver [OPTIONS] --multi COMM\n"
3166 "COMM may either be a tty device (for serial debugging),\n"
3167 "HOST:PORT to listen for a TCP connection, or '-' or 'stdio' to use \n"
3168 "stdin/stdout of gdbserver.\n"
3169 "PROG is the executable program. ARGS are arguments passed to inferior.\n"
3170 "PID is the process ID to attach to, when --attach is specified.\n"
3172 "Operating modes:\n"
3174 " --attach Attach to running process PID.\n"
3175 " --multi Start server without a specific program, and\n"
3176 " only quit when explicitly commanded.\n"
3177 " --once Exit after the first connection has closed.\n"
3178 " --help Print this message and then exit.\n"
3179 " --version Display version information and exit.\n"
3183 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
3184 " --disable-randomization\n"
3185 " Run PROG with address space randomization disabled.\n"
3186 " --no-disable-randomization\n"
3187 " Don't disable address space randomization when\n"
3192 " --debug Enable general debugging output.\n"
3193 " --debug-format=opt1[,opt2,...]\n"
3194 " Specify extra content in debugging output.\n"
3199 " --remote-debug Enable remote protocol debugging output.\n"
3200 " --disable-packet=opt1[,opt2,...]\n"
3201 " Disable support for RSP packets or features.\n"
3203 " vCont, Tthread, qC, qfThreadInfo and \n"
3204 " threads (disable all threading packets).\n"
3206 "For more information, consult the GDB manual (available as on-line \n"
3207 "info or a printed manual).\n");
3208 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
3209 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
3213 gdbserver_show_disableable (FILE *stream
)
3215 fprintf (stream
, "Disableable packets:\n"
3216 " vCont \tAll vCont packets\n"
3217 " qC \tQuerying the current thread\n"
3218 " qfThreadInfo\tThread listing\n"
3219 " Tthread \tPassing the thread specifier in the "
3220 "T stop reply packet\n"
3221 " threads \tAll of the above\n");
3225 #undef require_running
3226 #define require_running(BUF) \
3227 if (!target_running ()) \
3234 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
3236 int pid
= * (int *) args
;
3238 if (ptid_get_pid (entry
->id
) == pid
)
3245 kill_inferior_callback (struct inferior_list_entry
*entry
)
3247 struct process_info
*process
= (struct process_info
*) entry
;
3248 int pid
= ptid_get_pid (process
->entry
.id
);
3250 kill_inferior (pid
);
3251 discard_queued_stop_replies (pid_to_ptid (pid
));
3254 /* Callback for for_each_inferior to detach or kill the inferior,
3255 depending on whether we attached to it or not.
3256 We inform the user whether we're detaching or killing the process
3257 as this is only called when gdbserver is about to exit. */
3260 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
3262 struct process_info
*process
= (struct process_info
*) entry
;
3263 int pid
= ptid_get_pid (process
->entry
.id
);
3265 if (process
->attached
)
3266 detach_inferior (pid
);
3268 kill_inferior (pid
);
3270 discard_queued_stop_replies (pid_to_ptid (pid
));
3273 /* for_each_inferior callback for detach_or_kill_for_exit to print
3274 the pids of started inferiors. */
3277 print_started_pid (struct inferior_list_entry
*entry
)
3279 struct process_info
*process
= (struct process_info
*) entry
;
3281 if (! process
->attached
)
3283 int pid
= ptid_get_pid (process
->entry
.id
);
3284 fprintf (stderr
, " %d", pid
);
3288 /* for_each_inferior callback for detach_or_kill_for_exit to print
3289 the pids of attached inferiors. */
3292 print_attached_pid (struct inferior_list_entry
*entry
)
3294 struct process_info
*process
= (struct process_info
*) entry
;
3296 if (process
->attached
)
3298 int pid
= ptid_get_pid (process
->entry
.id
);
3299 fprintf (stderr
, " %d", pid
);
3303 /* Call this when exiting gdbserver with possible inferiors that need
3304 to be killed or detached from. */
3307 detach_or_kill_for_exit (void)
3309 /* First print a list of the inferiors we will be killing/detaching.
3310 This is to assist the user, for example, in case the inferior unexpectedly
3311 dies after we exit: did we screw up or did the inferior exit on its own?
3312 Having this info will save some head-scratching. */
3314 if (have_started_inferiors_p ())
3316 fprintf (stderr
, "Killing process(es):");
3317 for_each_inferior (&all_processes
, print_started_pid
);
3318 fprintf (stderr
, "\n");
3320 if (have_attached_inferiors_p ())
3322 fprintf (stderr
, "Detaching process(es):");
3323 for_each_inferior (&all_processes
, print_attached_pid
);
3324 fprintf (stderr
, "\n");
3327 /* Now we can kill or detach the inferiors. */
3329 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
3332 /* Value that will be passed to exit(3) when gdbserver exits. */
3333 static int exit_code
;
3335 /* Cleanup version of detach_or_kill_for_exit. */
3338 detach_or_kill_for_exit_cleanup (void *ignore
)
3343 detach_or_kill_for_exit ();
3346 CATCH (exception
, RETURN_MASK_ALL
)
3349 fprintf (stderr
, "Detach or kill failed: %s\n", exception
.message
);
3355 /* Main function. This is called by the real "main" function,
3356 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
3358 static void ATTRIBUTE_NORETURN
3359 captured_main (int argc
, char *argv
[])
3363 char *arg_end
, *port
;
3364 char **next_arg
= &argv
[1];
3365 volatile int multi_mode
= 0;
3366 volatile int attach
= 0;
3369 while (*next_arg
!= NULL
&& **next_arg
== '-')
3371 if (strcmp (*next_arg
, "--version") == 0)
3373 gdbserver_version ();
3376 else if (strcmp (*next_arg
, "--help") == 0)
3378 gdbserver_usage (stdout
);
3381 else if (strcmp (*next_arg
, "--attach") == 0)
3383 else if (strcmp (*next_arg
, "--multi") == 0)
3385 else if (strcmp (*next_arg
, "--wrapper") == 0)
3389 wrapper_argv
= next_arg
;
3390 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
3393 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
3395 gdbserver_usage (stderr
);
3399 /* Consume the "--". */
3402 else if (strcmp (*next_arg
, "--debug") == 0)
3404 else if (startswith (*next_arg
, "--debug-format="))
3407 = parse_debug_format_options ((*next_arg
)
3408 + sizeof ("--debug-format=") - 1, 0);
3410 if (error_msg
!= NULL
)
3412 fprintf (stderr
, "%s", error_msg
);
3416 else if (strcmp (*next_arg
, "--remote-debug") == 0)
3418 else if (strcmp (*next_arg
, "--disable-packet") == 0)
3420 gdbserver_show_disableable (stdout
);
3423 else if (startswith (*next_arg
, "--disable-packet="))
3425 char *packets
, *tok
;
3427 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
3428 for (tok
= strtok (packets
, ",");
3430 tok
= strtok (NULL
, ","))
3432 if (strcmp ("vCont", tok
) == 0)
3433 disable_packet_vCont
= 1;
3434 else if (strcmp ("Tthread", tok
) == 0)
3435 disable_packet_Tthread
= 1;
3436 else if (strcmp ("qC", tok
) == 0)
3437 disable_packet_qC
= 1;
3438 else if (strcmp ("qfThreadInfo", tok
) == 0)
3439 disable_packet_qfThreadInfo
= 1;
3440 else if (strcmp ("threads", tok
) == 0)
3442 disable_packet_vCont
= 1;
3443 disable_packet_Tthread
= 1;
3444 disable_packet_qC
= 1;
3445 disable_packet_qfThreadInfo
= 1;
3449 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
3451 gdbserver_show_disableable (stderr
);
3456 else if (strcmp (*next_arg
, "-") == 0)
3458 /* "-" specifies a stdio connection and is a form of port
3460 *next_arg
= STDIO_CONNECTION_NAME
;
3463 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
3464 disable_randomization
= 1;
3465 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
3466 disable_randomization
= 0;
3467 else if (strcmp (*next_arg
, "--once") == 0)
3471 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
3481 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
3483 gdbserver_usage (stderr
);
3487 /* Remember stdio descriptors. LISTEN_DESC must not be listed, it will be
3488 opened by remote_prepare. */
3491 /* We need to know whether the remote connection is stdio before
3492 starting the inferior. Inferiors created in this scenario have
3493 stdin,stdout redirected. So do this here before we call
3495 remote_prepare (port
);
3500 /* --attach used to come after PORT, so allow it there for
3502 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
3509 && (*next_arg
== NULL
3510 || (*next_arg
)[0] == '\0'
3511 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
3513 || next_arg
[1] != NULL
))
3518 gdbserver_usage (stderr
);
3522 initialize_async_io ();
3524 initialize_event_loop ();
3525 if (target_supports_tracepoints ())
3526 initialize_tracepoint ();
3527 initialize_notif ();
3529 own_buf
= (char *) xmalloc (PBUFSIZ
+ 1);
3530 mem_buf
= (unsigned char *) xmalloc (PBUFSIZ
);
3532 if (pid
== 0 && *next_arg
!= NULL
)
3536 n
= argc
- (next_arg
- argv
);
3537 program_argv
= XNEWVEC (char *, n
+ 1);
3538 for (i
= 0; i
< n
; i
++)
3539 program_argv
[i
] = xstrdup (next_arg
[i
]);
3540 program_argv
[i
] = NULL
;
3542 /* Wait till we are at first instruction in program. */
3543 start_inferior (program_argv
);
3545 /* We are now (hopefully) stopped at the first instruction of
3546 the target process. This assumes that the target process was
3547 successfully created. */
3551 if (attach_inferior (pid
) == -1)
3552 error ("Attaching not supported on this target");
3554 /* Otherwise succeeded. */
3558 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3559 last_status
.value
.integer
= 0;
3560 last_ptid
= minus_one_ptid
;
3562 make_cleanup (detach_or_kill_for_exit_cleanup
, NULL
);
3564 /* Don't report shared library events on the initial connection,
3565 even if some libraries are preloaded. Avoids the "stopped by
3566 shared library event" notice on gdb side. */
3569 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3570 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3575 if (!was_running
&& !multi_mode
)
3576 error ("No program to debug");
3583 report_fork_events
= 0;
3584 report_vfork_events
= 0;
3585 report_exec_events
= 0;
3586 /* Be sure we're out of tfind mode. */
3587 current_traceframe
= -1;
3588 cont_thread
= null_ptid
;
3589 swbreak_feature
= 0;
3590 hwbreak_feature
= 0;
3591 vCont_supported
= 0;
3597 /* Wait for events. This will return when all event sources
3598 are removed from the event loop. */
3599 start_event_loop ();
3601 /* If an exit was requested (using the "monitor exit"
3602 command), terminate now. The only other way to get
3603 here is for getpkt to fail; close the connection
3604 and reopen it at the top of the loop. */
3606 if (exit_requested
|| run_once
)
3607 throw_quit ("Quit");
3610 "Remote side has terminated connection. "
3611 "GDBserver will reopen the connection.\n");
3613 /* Get rid of any pending statuses. An eventual reconnection
3614 (by the same GDB instance or another) will refresh all its
3615 state from scratch. */
3616 discard_queued_stop_replies (minus_one_ptid
);
3617 for_each_inferior (&all_threads
,
3618 clear_pending_status_callback
);
3622 if (disconnected_tracing
)
3624 /* Try to enable non-stop/async mode, so we we can
3625 both wait for an async socket accept, and handle
3626 async target events simultaneously. There's also
3627 no point either in having the target always stop
3628 all threads, when we're going to pass signals
3629 down without informing GDB. */
3632 if (start_non_stop (1))
3635 /* Detaching implicitly resumes all threads;
3636 simply disconnecting does not. */
3642 "Disconnected tracing disabled; "
3643 "stopping trace run.\n");
3648 CATCH (exception
, RETURN_MASK_ERROR
)
3651 fprintf (stderr
, "gdbserver: %s\n", exception
.message
);
3653 if (response_needed
)
3655 write_enn (own_buf
);
3660 throw_quit ("Quit");
3666 /* Main function. */
3669 main (int argc
, char *argv
[])
3674 captured_main (argc
, argv
);
3676 CATCH (exception
, RETURN_MASK_ALL
)
3678 if (exception
.reason
== RETURN_ERROR
)
3681 fprintf (stderr
, "%s\n", exception
.message
);
3682 fprintf (stderr
, "Exiting\n");
3690 gdb_assert_not_reached ("captured_main should never return");
3693 /* Skip PACKET until the next semi-colon (or end of string). */
3696 skip_to_semicolon (char **packet
)
3698 while (**packet
!= '\0' && **packet
!= ';')
3702 /* Process options coming from Z packets for a breakpoint. PACKET is
3703 the packet buffer. *PACKET is updated to point to the first char
3704 after the last processed option. */
3707 process_point_options (struct breakpoint
*bp
, char **packet
)
3709 char *dataptr
= *packet
;
3712 /* Check if data has the correct format. */
3713 if (*dataptr
!= ';')
3720 if (*dataptr
== ';')
3723 if (*dataptr
== 'X')
3725 /* Conditional expression. */
3727 debug_printf ("Found breakpoint condition.\n");
3728 if (!add_breakpoint_condition (bp
, &dataptr
))
3729 skip_to_semicolon (&dataptr
);
3731 else if (startswith (dataptr
, "cmds:"))
3733 dataptr
+= strlen ("cmds:");
3735 debug_printf ("Found breakpoint commands %s.\n", dataptr
);
3736 persist
= (*dataptr
== '1');
3738 if (add_breakpoint_commands (bp
, &dataptr
, persist
))
3739 skip_to_semicolon (&dataptr
);
3743 fprintf (stderr
, "Unknown token %c, ignoring.\n",
3745 /* Skip tokens until we find one that we recognize. */
3746 skip_to_semicolon (&dataptr
);
3752 /* Event loop callback that handles a serial event. The first byte in
3753 the serial buffer gets us here. We expect characters to arrive at
3754 a brisk pace, so we read the rest of the packet with a blocking
3758 process_serial_event (void)
3769 int new_packet_len
= -1;
3771 /* Used to decide when gdbserver should exit in
3772 multi-mode/remote. */
3773 static int have_ran
= 0;
3776 have_ran
= target_running ();
3778 disable_async_io ();
3780 response_needed
= 0;
3781 packet_len
= getpkt (own_buf
);
3782 if (packet_len
<= 0)
3785 /* Force an event loop break. */
3788 response_needed
= 1;
3795 handle_query (own_buf
, packet_len
, &new_packet_len
);
3798 handle_general_set (own_buf
);
3801 require_running (own_buf
);
3806 pid
= strtol (&own_buf
[i
], NULL
, 16);
3809 pid
= ptid_get_pid (current_ptid
);
3811 if ((tracing
&& disconnected_tracing
) || any_persistent_commands ())
3813 struct thread_resume resume_info
;
3814 struct process_info
*process
= find_process_pid (pid
);
3816 if (process
== NULL
)
3818 write_enn (own_buf
);
3822 if (tracing
&& disconnected_tracing
)
3824 "Disconnected tracing in effect, "
3825 "leaving gdbserver attached to the process\n");
3827 if (any_persistent_commands ())
3829 "Persistent commands are present, "
3830 "leaving gdbserver attached to the process\n");
3832 /* Make sure we're in non-stop/async mode, so we we can both
3833 wait for an async socket accept, and handle async target
3834 events simultaneously. There's also no point either in
3835 having the target stop all threads, when we're going to
3836 pass signals down without informing GDB. */
3840 debug_printf ("Forcing non-stop mode\n");
3846 process
->gdb_detached
= 1;
3848 /* Detaching implicitly resumes all threads. */
3849 resume_info
.thread
= minus_one_ptid
;
3850 resume_info
.kind
= resume_continue
;
3851 resume_info
.sig
= 0;
3852 (*the_target
->resume
) (&resume_info
, 1);
3855 break; /* from switch/case */
3858 fprintf (stderr
, "Detaching from process %d\n", pid
);
3860 if (detach_inferior (pid
) != 0)
3861 write_enn (own_buf
);
3864 discard_queued_stop_replies (pid_to_ptid (pid
));
3867 if (extended_protocol
)
3869 /* Treat this like a normal program exit. */
3870 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3871 last_status
.value
.integer
= 0;
3872 last_ptid
= pid_to_ptid (pid
);
3874 current_thread
= NULL
;
3881 /* If we are attached, then we can exit. Otherwise, we
3882 need to hang around doing nothing, until the child is
3884 join_inferior (pid
);
3890 extended_protocol
= 1;
3894 handle_status (own_buf
);
3897 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
3899 ptid_t gdb_id
, thread_id
;
3902 require_running (own_buf
);
3904 gdb_id
= read_ptid (&own_buf
[2], NULL
);
3906 pid
= ptid_get_pid (gdb_id
);
3908 if (ptid_equal (gdb_id
, null_ptid
)
3909 || ptid_equal (gdb_id
, minus_one_ptid
))
3910 thread_id
= null_ptid
;
3912 && ptid_equal (pid_to_ptid (pid
),
3915 struct thread_info
*thread
=
3916 (struct thread_info
*) find_inferior (&all_threads
,
3921 write_enn (own_buf
);
3925 thread_id
= thread
->entry
.id
;
3929 thread_id
= gdb_id_to_thread_id (gdb_id
);
3930 if (ptid_equal (thread_id
, null_ptid
))
3932 write_enn (own_buf
);
3937 if (own_buf
[1] == 'g')
3939 if (ptid_equal (thread_id
, null_ptid
))
3941 /* GDB is telling us to choose any thread. Check if
3942 the currently selected thread is still valid. If
3943 it is not, select the first available. */
3944 struct thread_info
*thread
=
3945 (struct thread_info
*) find_inferior_id (&all_threads
,
3948 thread
= get_first_thread ();
3949 thread_id
= thread
->entry
.id
;
3952 general_thread
= thread_id
;
3953 set_desired_thread (1);
3954 gdb_assert (current_thread
!= NULL
);
3956 else if (own_buf
[1] == 'c')
3957 cont_thread
= thread_id
;
3963 /* Silently ignore it so that gdb can extend the protocol
3964 without compatibility headaches. */
3969 require_running (own_buf
);
3970 if (current_traceframe
>= 0)
3972 struct regcache
*regcache
3973 = new_register_cache (current_target_desc ());
3975 if (fetch_traceframe_registers (current_traceframe
,
3977 registers_to_string (regcache
, own_buf
);
3979 write_enn (own_buf
);
3980 free_register_cache (regcache
);
3984 struct regcache
*regcache
;
3986 if (!set_desired_thread (1))
3987 write_enn (own_buf
);
3990 regcache
= get_thread_regcache (current_thread
, 1);
3991 registers_to_string (regcache
, own_buf
);
3996 require_running (own_buf
);
3997 if (current_traceframe
>= 0)
3998 write_enn (own_buf
);
4001 struct regcache
*regcache
;
4003 if (!set_desired_thread (1))
4004 write_enn (own_buf
);
4007 regcache
= get_thread_regcache (current_thread
, 1);
4008 registers_from_string (regcache
, &own_buf
[1]);
4014 require_running (own_buf
);
4015 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
4016 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
4018 write_enn (own_buf
);
4020 bin2hex (mem_buf
, own_buf
, res
);
4023 require_running (own_buf
);
4024 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
4025 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
4028 write_enn (own_buf
);
4031 require_running (own_buf
);
4032 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
4033 &mem_addr
, &len
, &mem_buf
) < 0
4034 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
4035 write_enn (own_buf
);
4040 require_running (own_buf
);
4041 hex2bin (own_buf
+ 1, &sig
, 1);
4042 if (gdb_signal_to_host_p (sig
))
4043 signal
= gdb_signal_to_host (sig
);
4046 myresume (own_buf
, 0, signal
);
4049 require_running (own_buf
);
4050 hex2bin (own_buf
+ 1, &sig
, 1);
4051 if (gdb_signal_to_host_p (sig
))
4052 signal
= gdb_signal_to_host (sig
);
4055 myresume (own_buf
, 1, signal
);
4058 require_running (own_buf
);
4060 myresume (own_buf
, 0, signal
);
4063 require_running (own_buf
);
4065 myresume (own_buf
, 1, signal
);
4067 case 'Z': /* insert_ ... */
4069 case 'z': /* remove_ ... */
4074 char type
= own_buf
[1];
4076 const int insert
= ch
== 'Z';
4077 char *p
= &own_buf
[3];
4079 p
= unpack_varlen_hex (p
, &addr
);
4080 kind
= strtol (p
+ 1, &dataptr
, 16);
4084 struct breakpoint
*bp
;
4086 bp
= set_gdb_breakpoint (type
, addr
, kind
, &res
);
4091 /* GDB may have sent us a list of *point parameters to
4092 be evaluated on the target's side. Read such list
4093 here. If we already have a list of parameters, GDB
4094 is telling us to drop that list and use this one
4096 clear_breakpoint_conditions_and_commands (bp
);
4097 process_point_options (bp
, &dataptr
);
4101 res
= delete_gdb_breakpoint (type
, addr
, kind
);
4109 write_enn (own_buf
);
4113 response_needed
= 0;
4114 if (!target_running ())
4115 /* The packet we received doesn't make sense - but we can't
4116 reply to it, either. */
4119 fprintf (stderr
, "Killing all inferiors\n");
4120 for_each_inferior (&all_processes
, kill_inferior_callback
);
4122 /* When using the extended protocol, we wait with no program
4123 running. The traditional protocol will exit instead. */
4124 if (extended_protocol
)
4126 last_status
.kind
= TARGET_WAITKIND_EXITED
;
4127 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
4135 ptid_t gdb_id
, thread_id
;
4137 require_running (own_buf
);
4139 gdb_id
= read_ptid (&own_buf
[1], NULL
);
4140 thread_id
= gdb_id_to_thread_id (gdb_id
);
4141 if (ptid_equal (thread_id
, null_ptid
))
4143 write_enn (own_buf
);
4147 if (mythread_alive (thread_id
))
4150 write_enn (own_buf
);
4154 response_needed
= 0;
4156 /* Restarting the inferior is only supported in the extended
4158 if (extended_protocol
)
4160 if (target_running ())
4161 for_each_inferior (&all_processes
,
4162 kill_inferior_callback
);
4163 fprintf (stderr
, "GDBserver restarting\n");
4165 /* Wait till we are at 1st instruction in prog. */
4166 if (program_argv
!= NULL
)
4168 start_inferior (program_argv
);
4169 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
4171 /* Stopped at the first instruction of the target
4173 general_thread
= last_ptid
;
4177 /* Something went wrong. */
4178 general_thread
= null_ptid
;
4183 last_status
.kind
= TARGET_WAITKIND_EXITED
;
4184 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
4190 /* It is a request we don't understand. Respond with an
4191 empty packet so that gdb knows that we don't support this
4197 /* Extended (long) request. */
4198 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
4202 /* It is a request we don't understand. Respond with an empty
4203 packet so that gdb knows that we don't support this
4209 if (new_packet_len
!= -1)
4210 putpkt_binary (own_buf
, new_packet_len
);
4214 response_needed
= 0;
4216 if (!extended_protocol
&& have_ran
&& !target_running ())
4218 /* In non-stop, defer exiting until GDB had a chance to query
4219 the whole vStopped list (until it gets an OK). */
4220 if (QUEUE_is_empty (notif_event_p
, notif_stop
.queue
))
4222 /* Be transparent when GDB is connected through stdio -- no
4223 need to spam GDB's console. */
4224 if (!remote_connection_is_stdio ())
4225 fprintf (stderr
, "GDBserver exiting\n");
4237 /* Event-loop callback for serial events. */
4240 handle_serial_event (int err
, gdb_client_data client_data
)
4243 debug_printf ("handling possible serial event\n");
4245 /* Really handle it. */
4246 if (process_serial_event () < 0)
4249 /* Be sure to not change the selected thread behind GDB's back.
4250 Important in the non-stop mode asynchronous protocol. */
4251 set_desired_thread (1);
4256 /* Event-loop callback for target events. */
4259 handle_target_event (int err
, gdb_client_data client_data
)
4262 debug_printf ("handling possible target event\n");
4264 last_ptid
= mywait (minus_one_ptid
, &last_status
,
4267 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
)
4269 /* No RSP support for this yet. */
4271 else if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
4273 int pid
= ptid_get_pid (last_ptid
);
4274 struct process_info
*process
= find_process_pid (pid
);
4275 int forward_event
= !gdb_connected () || process
->gdb_detached
;
4277 if (last_status
.kind
== TARGET_WAITKIND_EXITED
4278 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
4280 mark_breakpoints_out (process
);
4281 mourn_inferior (process
);
4285 /* We're reporting this thread as stopped. Update its
4286 "want-stopped" state to what the client wants, until it
4287 gets a new resume action. */
4288 current_thread
->last_resume_kind
= resume_stop
;
4289 current_thread
->last_status
= last_status
;
4294 if (!target_running ())
4296 /* The last process exited. We're done. */
4300 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
4302 /* A thread stopped with a signal, but gdb isn't
4303 connected to handle it. Pass it down to the
4304 inferior, as if it wasn't being traced. */
4305 struct thread_resume resume_info
;
4308 debug_printf ("GDB not connected; forwarding event %d for"
4310 (int) last_status
.kind
,
4311 target_pid_to_str (last_ptid
));
4313 resume_info
.thread
= last_ptid
;
4314 resume_info
.kind
= resume_continue
;
4315 resume_info
.sig
= gdb_signal_to_host (last_status
.value
.sig
);
4316 (*the_target
->resume
) (&resume_info
, 1);
4318 else if (debug_threads
)
4319 debug_printf ("GDB not connected; ignoring event %d for [%s]\n",
4320 (int) last_status
.kind
,
4321 target_pid_to_str (last_ptid
));
4325 struct vstop_notif
*vstop_notif
= XNEW (struct vstop_notif
);
4327 vstop_notif
->status
= last_status
;
4328 vstop_notif
->ptid
= last_ptid
;
4329 /* Push Stop notification. */
4330 notif_push (¬if_stop
,
4331 (struct notif_event
*) vstop_notif
);
4335 /* Be sure to not change the selected thread behind GDB's back.
4336 Important in the non-stop mode asynchronous protocol. */
4337 set_desired_thread (1);