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 /* Whether we should attempt to disable the operating system's address
68 space randomization feature before starting an inferior. */
69 int disable_randomization
= 1;
71 static char **program_argv
, **wrapper_argv
;
73 int pass_signals
[GDB_SIGNAL_LAST
];
74 int program_signals
[GDB_SIGNAL_LAST
];
75 int program_signals_p
;
77 /* The PID of the originally created or attached inferior. Used to
78 send signals to the process when GDB sends us an asynchronous interrupt
79 (user hitting Control-C in the client), and to wait for the child to exit
80 when no longer debugging it. */
82 unsigned long signal_pid
;
85 /* A file descriptor for the controlling terminal. */
88 /* TERMINAL_FD's original foreground group. */
89 pid_t old_foreground_pgrp
;
91 /* Hand back terminal ownership to the original foreground group. */
94 restore_old_foreground_pgrp (void)
96 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
100 /* Set if you want to disable optional thread related packets support
101 in gdbserver, for the sake of testing GDB against stubs that don't
103 int disable_packet_vCont
;
104 int disable_packet_Tthread
;
105 int disable_packet_qC
;
106 int disable_packet_qfThreadInfo
;
108 /* Last status reported to GDB. */
109 static struct target_waitstatus last_status
;
110 static ptid_t last_ptid
;
112 static char *own_buf
;
113 static unsigned char *mem_buf
;
115 /* A sub-class of 'struct notif_event' for stop, holding information
116 relative to a single stop reply. We keep a queue of these to
117 push to GDB in non-stop mode. */
121 struct notif_event base
;
123 /* Thread or process that got the event. */
127 struct target_waitstatus status
;
130 /* The current btrace configuration. This is gdbserver's mirror of GDB's
131 btrace configuration. */
132 static struct btrace_config current_btrace_conf
;
134 DEFINE_QUEUE_P (notif_event_p
);
136 /* Put a stop reply to the stop reply queue. */
139 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
141 struct vstop_notif
*new_notif
= XNEW (struct vstop_notif
);
143 new_notif
->ptid
= ptid
;
144 new_notif
->status
= *status
;
146 notif_event_enque (¬if_stop
, (struct notif_event
*) new_notif
);
150 remove_all_on_match_ptid (QUEUE (notif_event_p
) *q
,
151 QUEUE_ITER (notif_event_p
) *iter
,
152 struct notif_event
*event
,
155 ptid_t filter_ptid
= *(ptid_t
*) data
;
156 struct vstop_notif
*vstop_event
= (struct vstop_notif
*) event
;
158 if (ptid_match (vstop_event
->ptid
, filter_ptid
))
160 if (q
->free_func
!= NULL
)
161 q
->free_func (event
);
163 QUEUE_remove_elem (notif_event_p
, q
, iter
);
172 discard_queued_stop_replies (ptid_t ptid
)
174 QUEUE_iterate (notif_event_p
, notif_stop
.queue
,
175 remove_all_on_match_ptid
, &ptid
);
179 vstop_notif_reply (struct notif_event
*event
, char *own_buf
)
181 struct vstop_notif
*vstop
= (struct vstop_notif
*) event
;
183 prepare_resume_reply (own_buf
, vstop
->ptid
, &vstop
->status
);
186 struct notif_server notif_stop
=
188 "vStopped", "Stop", NULL
, vstop_notif_reply
,
192 target_running (void)
194 return get_first_thread () != NULL
;
198 start_inferior (char **argv
)
200 char **new_argv
= argv
;
202 if (wrapper_argv
!= NULL
)
206 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
208 for (i
= 0; argv
[i
] != NULL
; i
++)
210 new_argv
= XALLOCAVEC (char *, count
);
212 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
213 new_argv
[count
++] = wrapper_argv
[i
];
214 for (i
= 0; argv
[i
] != NULL
; i
++)
215 new_argv
[count
++] = argv
[i
];
216 new_argv
[count
] = NULL
;
222 for (i
= 0; new_argv
[i
]; ++i
)
223 debug_printf ("new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
228 signal (SIGTTOU
, SIG_DFL
);
229 signal (SIGTTIN
, SIG_DFL
);
232 signal_pid
= create_inferior (new_argv
[0], new_argv
);
234 /* FIXME: we don't actually know at this point that the create
235 actually succeeded. We won't know that until we wait. */
236 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
241 signal (SIGTTOU
, SIG_IGN
);
242 signal (SIGTTIN
, SIG_IGN
);
243 terminal_fd
= fileno (stderr
);
244 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
245 tcsetpgrp (terminal_fd
, signal_pid
);
246 atexit (restore_old_foreground_pgrp
);
249 if (wrapper_argv
!= NULL
)
251 struct thread_resume resume_info
;
253 memset (&resume_info
, 0, sizeof (resume_info
));
254 resume_info
.thread
= pid_to_ptid (signal_pid
);
255 resume_info
.kind
= resume_continue
;
258 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
260 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
264 (*the_target
->resume
) (&resume_info
, 1);
266 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
267 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
270 current_thread
->last_resume_kind
= resume_stop
;
271 current_thread
->last_status
= last_status
;
273 while (last_status
.value
.sig
!= GDB_SIGNAL_TRAP
);
275 target_arch_setup ();
279 /* Wait till we are at 1st instruction in program, return new pid
280 (assuming success). */
281 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
283 target_arch_setup ();
285 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
286 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
288 current_thread
->last_resume_kind
= resume_stop
;
289 current_thread
->last_status
= last_status
;
292 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
298 attach_inferior (int pid
)
300 /* myattach should return -1 if attaching is unsupported,
301 0 if it succeeded, and call error() otherwise. */
303 if (myattach (pid
) != 0)
306 fprintf (stderr
, "Attached; pid = %d\n", pid
);
309 /* FIXME - It may be that we should get the SIGNAL_PID from the
310 attach function, so that it can be the main thread instead of
311 whichever we were told to attach to. */
316 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
318 /* GDB knows to ignore the first SIGSTOP after attaching to a running
319 process using the "attach" command, but this is different; it's
320 just using "target remote". Pretend it's just starting up. */
321 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
322 && last_status
.value
.sig
== GDB_SIGNAL_STOP
)
323 last_status
.value
.sig
= GDB_SIGNAL_TRAP
;
325 current_thread
->last_resume_kind
= resume_stop
;
326 current_thread
->last_status
= last_status
;
332 extern int remote_debug
;
334 /* Decode a qXfer read request. Return 0 if everything looks OK,
338 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
340 /* After the read marker and annex, qXfer looks like a
341 traditional 'm' packet. */
342 decode_m_packet (buf
, ofs
, len
);
348 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
350 /* Extract and NUL-terminate the object. */
352 while (*buf
&& *buf
!= ':')
358 /* Extract and NUL-terminate the read/write action. */
360 while (*buf
&& *buf
!= ':')
366 /* Extract and NUL-terminate the annex. */
368 while (*buf
&& *buf
!= ':')
378 /* Write the response to a successful qXfer read. Returns the
379 length of the (binary) data stored in BUF, corresponding
380 to as much of DATA/LEN as we could fit. IS_MORE controls
381 the first character of the response. */
383 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
392 return remote_escape_output (data
, len
, 1, (unsigned char *) buf
+ 1,
393 &out_len
, PBUFSIZ
- 2) + 1;
396 /* Handle btrace enabling in BTS format. */
399 handle_btrace_enable_bts (struct thread_info
*thread
)
401 if (thread
->btrace
!= NULL
)
402 return "E.Btrace already enabled.";
404 current_btrace_conf
.format
= BTRACE_FORMAT_BTS
;
405 thread
->btrace
= target_enable_btrace (thread
->entry
.id
,
406 ¤t_btrace_conf
);
407 if (thread
->btrace
== NULL
)
408 return "E.Could not enable btrace.";
413 /* Handle btrace enabling in Intel(R) Processor Trace format. */
416 handle_btrace_enable_pt (struct thread_info
*thread
)
418 if (thread
->btrace
!= NULL
)
419 return "E.Btrace already enabled.";
421 current_btrace_conf
.format
= BTRACE_FORMAT_PT
;
422 thread
->btrace
= target_enable_btrace (thread
->entry
.id
,
423 ¤t_btrace_conf
);
424 if (thread
->btrace
== NULL
)
425 return "E.Could not enable btrace.";
430 /* Handle btrace disabling. */
433 handle_btrace_disable (struct thread_info
*thread
)
436 if (thread
->btrace
== NULL
)
437 return "E.Branch tracing not enabled.";
439 if (target_disable_btrace (thread
->btrace
) != 0)
440 return "E.Could not disable branch tracing.";
442 thread
->btrace
= NULL
;
446 /* Handle the "Qbtrace" packet. */
449 handle_btrace_general_set (char *own_buf
)
451 struct thread_info
*thread
;
455 if (!startswith (own_buf
, "Qbtrace:"))
458 op
= own_buf
+ strlen ("Qbtrace:");
460 if (ptid_equal (general_thread
, null_ptid
)
461 || ptid_equal (general_thread
, minus_one_ptid
))
463 strcpy (own_buf
, "E.Must select a single thread.");
467 thread
= find_thread_ptid (general_thread
);
470 strcpy (own_buf
, "E.No such thread.");
476 if (strcmp (op
, "bts") == 0)
477 err
= handle_btrace_enable_bts (thread
);
478 else if (strcmp (op
, "pt") == 0)
479 err
= handle_btrace_enable_pt (thread
);
480 else if (strcmp (op
, "off") == 0)
481 err
= handle_btrace_disable (thread
);
483 err
= "E.Bad Qbtrace operation. Use bts, pt, or off.";
486 strcpy (own_buf
, err
);
493 /* Handle the "Qbtrace-conf" packet. */
496 handle_btrace_conf_general_set (char *own_buf
)
498 struct thread_info
*thread
;
501 if (!startswith (own_buf
, "Qbtrace-conf:"))
504 op
= own_buf
+ strlen ("Qbtrace-conf:");
506 if (ptid_equal (general_thread
, null_ptid
)
507 || ptid_equal (general_thread
, minus_one_ptid
))
509 strcpy (own_buf
, "E.Must select a single thread.");
513 thread
= find_thread_ptid (general_thread
);
516 strcpy (own_buf
, "E.No such thread.");
520 if (startswith (op
, "bts:size="))
526 size
= strtoul (op
+ strlen ("bts:size="), &endp
, 16);
527 if (endp
== NULL
|| *endp
!= 0 || errno
!= 0 || size
> UINT_MAX
)
529 strcpy (own_buf
, "E.Bad size value.");
533 current_btrace_conf
.bts
.size
= (unsigned int) size
;
535 else if (strncmp (op
, "pt:size=", strlen ("pt:size=")) == 0)
541 size
= strtoul (op
+ strlen ("pt:size="), &endp
, 16);
542 if (endp
== NULL
|| *endp
!= 0 || errno
!= 0 || size
> UINT_MAX
)
544 strcpy (own_buf
, "E.Bad size value.");
548 current_btrace_conf
.pt
.size
= (unsigned int) size
;
552 strcpy (own_buf
, "E.Bad Qbtrace configuration option.");
560 /* Handle all of the extended 'Q' packets. */
563 handle_general_set (char *own_buf
)
565 if (startswith (own_buf
, "QPassSignals:"))
567 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
568 const char *p
= own_buf
+ strlen ("QPassSignals:");
571 p
= decode_address_to_semicolon (&cursig
, p
);
572 for (i
= 0; i
< numsigs
; i
++)
578 /* Keep looping, to clear the remaining signals. */
581 p
= decode_address_to_semicolon (&cursig
, p
);
586 strcpy (own_buf
, "OK");
590 if (startswith (own_buf
, "QProgramSignals:"))
592 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
593 const char *p
= own_buf
+ strlen ("QProgramSignals:");
596 program_signals_p
= 1;
598 p
= decode_address_to_semicolon (&cursig
, p
);
599 for (i
= 0; i
< numsigs
; i
++)
603 program_signals
[i
] = 1;
605 /* Keep looping, to clear the remaining signals. */
608 p
= decode_address_to_semicolon (&cursig
, p
);
611 program_signals
[i
] = 0;
613 strcpy (own_buf
, "OK");
617 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
621 fprintf (stderr
, "[noack mode enabled]\n");
630 if (startswith (own_buf
, "QNonStop:"))
632 char *mode
= own_buf
+ 9;
636 if (strcmp (mode
, "0") == 0)
638 else if (strcmp (mode
, "1") == 0)
642 /* We don't know what this mode is, so complain to
644 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
650 req_str
= req
? "non-stop" : "all-stop";
651 if (start_non_stop (req
) != 0)
653 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
661 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
667 if (startswith (own_buf
, "QDisableRandomization:"))
669 char *packet
= own_buf
+ strlen ("QDisableRandomization:");
672 unpack_varlen_hex (packet
, &setting
);
673 disable_randomization
= setting
;
677 if (disable_randomization
)
678 fprintf (stderr
, "[address space randomization disabled]\n");
680 fprintf (stderr
, "[address space randomization enabled]\n");
687 if (target_supports_tracepoints ()
688 && handle_tracepoint_general_set (own_buf
))
691 if (startswith (own_buf
, "QAgent:"))
693 char *mode
= own_buf
+ strlen ("QAgent:");
696 if (strcmp (mode
, "0") == 0)
698 else if (strcmp (mode
, "1") == 0)
702 /* We don't know what this value is, so complain to GDB. */
703 sprintf (own_buf
, "E.Unknown QAgent value");
707 /* Update the flag. */
710 fprintf (stderr
, "[%s agent]\n", req
? "Enable" : "Disable");
715 if (handle_btrace_general_set (own_buf
))
718 if (handle_btrace_conf_general_set (own_buf
))
721 /* Otherwise we didn't know what packet it was. Say we didn't
727 get_features_xml (const char *annex
)
729 const struct target_desc
*desc
= current_target_desc ();
731 /* `desc->xmltarget' defines what to return when looking for the
732 "target.xml" file. Its contents can either be verbatim XML code
733 (prefixed with a '@') or else the name of the actual XML file to
734 be used in place of "target.xml".
736 This variable is set up from the auto-generated
737 init_registers_... routine for the current target. */
739 if (desc
->xmltarget
!= NULL
&& strcmp (annex
, "target.xml") == 0)
741 if (*desc
->xmltarget
== '@')
742 return desc
->xmltarget
+ 1;
744 annex
= desc
->xmltarget
;
749 extern const char *const xml_builtin
[][2];
752 /* Look for the annex. */
753 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
754 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
757 if (xml_builtin
[i
][0] != NULL
)
758 return xml_builtin
[i
][1];
766 monitor_show_help (void)
768 monitor_output ("The following monitor commands are supported:\n");
769 monitor_output (" set debug <0|1>\n");
770 monitor_output (" Enable general debugging messages\n");
771 monitor_output (" set debug-hw-points <0|1>\n");
772 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
773 monitor_output (" set remote-debug <0|1>\n");
774 monitor_output (" Enable remote protocol debugging messages\n");
775 monitor_output (" set debug-format option1[,option2,...]\n");
776 monitor_output (" Add additional information to debugging messages\n");
777 monitor_output (" Options: all, none");
778 monitor_output (", timestamp");
779 monitor_output ("\n");
780 monitor_output (" exit\n");
781 monitor_output (" Quit GDBserver\n");
784 /* Read trace frame or inferior memory. Returns the number of bytes
785 actually read, zero when no further transfer is possible, and -1 on
786 error. Return of a positive value smaller than LEN does not
787 indicate there's no more to be read, only the end of the transfer.
788 E.g., when GDB reads memory from a traceframe, a first request may
789 be served from a memory block that does not cover the whole request
790 length. A following request gets the rest served from either
791 another block (of the same traceframe) or from the read-only
795 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
799 if (current_traceframe
>= 0)
802 ULONGEST length
= len
;
804 if (traceframe_read_mem (current_traceframe
,
805 memaddr
, myaddr
, len
, &nbytes
))
807 /* Data read from trace buffer, we're done. */
810 if (!in_readonly_region (memaddr
, length
))
812 /* Otherwise we have a valid readonly case, fall through. */
813 /* (assume no half-trace half-real blocks for now) */
816 res
= prepare_to_access_memory ();
819 if (set_desired_thread (1))
820 res
= read_inferior_memory (memaddr
, myaddr
, len
);
823 done_accessing_memory ();
825 return res
== 0 ? len
: -1;
831 /* Write trace frame or inferior memory. Actually, writing to trace
832 frames is forbidden. */
835 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
837 if (current_traceframe
>= 0)
843 ret
= prepare_to_access_memory ();
846 if (set_desired_thread (1))
847 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
850 done_accessing_memory ();
856 /* Subroutine of handle_search_memory to simplify it. */
859 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
860 gdb_byte
*pattern
, unsigned pattern_len
,
861 gdb_byte
*search_buf
,
862 unsigned chunk_size
, unsigned search_buf_size
,
863 CORE_ADDR
*found_addrp
)
865 /* Prime the search buffer. */
867 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
870 warning ("Unable to access %ld bytes of target "
871 "memory at 0x%lx, halting search.",
872 (long) search_buf_size
, (long) start_addr
);
876 /* Perform the search.
878 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
879 When we've scanned N bytes we copy the trailing bytes to the start and
880 read in another N bytes. */
882 while (search_space_len
>= pattern_len
)
885 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
889 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
891 if (found_ptr
!= NULL
)
893 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
894 *found_addrp
= found_addr
;
898 /* Not found in this chunk, skip to next chunk. */
900 /* Don't let search_space_len wrap here, it's unsigned. */
901 if (search_space_len
>= chunk_size
)
902 search_space_len
-= chunk_size
;
904 search_space_len
= 0;
906 if (search_space_len
>= pattern_len
)
908 unsigned keep_len
= search_buf_size
- chunk_size
;
909 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
912 /* Copy the trailing part of the previous iteration to the front
913 of the buffer for the next iteration. */
914 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
916 nr_to_read
= (search_space_len
- keep_len
< chunk_size
917 ? search_space_len
- keep_len
920 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
921 nr_to_read
) != search_buf_size
)
923 warning ("Unable to access %ld bytes of target memory "
924 "at 0x%lx, halting search.",
925 (long) nr_to_read
, (long) read_addr
);
929 start_addr
+= chunk_size
;
938 /* Handle qSearch:memory packets. */
941 handle_search_memory (char *own_buf
, int packet_len
)
943 CORE_ADDR start_addr
;
944 CORE_ADDR search_space_len
;
946 unsigned int pattern_len
;
947 /* NOTE: also defined in find.c testcase. */
948 #define SEARCH_CHUNK_SIZE 16000
949 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
950 /* Buffer to hold memory contents for searching. */
951 gdb_byte
*search_buf
;
952 unsigned search_buf_size
;
954 CORE_ADDR found_addr
;
955 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
957 pattern
= malloc (packet_len
);
960 error ("Unable to allocate memory to perform the search");
961 strcpy (own_buf
, "E00");
964 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
965 packet_len
- cmd_name_len
,
966 &start_addr
, &search_space_len
,
967 pattern
, &pattern_len
) < 0)
970 error ("Error in parsing qSearch:memory packet");
971 strcpy (own_buf
, "E00");
975 search_buf_size
= chunk_size
+ pattern_len
- 1;
977 /* No point in trying to allocate a buffer larger than the search space. */
978 if (search_space_len
< search_buf_size
)
979 search_buf_size
= search_space_len
;
981 search_buf
= malloc (search_buf_size
);
982 if (search_buf
== NULL
)
985 error ("Unable to allocate memory to perform the search");
986 strcpy (own_buf
, "E00");
990 found
= handle_search_memory_1 (start_addr
, search_space_len
,
991 pattern
, pattern_len
,
992 search_buf
, chunk_size
, search_buf_size
,
996 sprintf (own_buf
, "1,%lx", (long) found_addr
);
998 strcpy (own_buf
, "0");
1000 strcpy (own_buf
, "E00");
1006 #define require_running(BUF) \
1007 if (!target_running ()) \
1013 /* Parse options to --debug-format= and "monitor set debug-format".
1014 ARG is the text after "--debug-format=" or "monitor set debug-format".
1015 IS_MONITOR is non-zero if we're invoked via "monitor set debug-format".
1016 This triggers calls to monitor_output.
1017 The result is NULL if all options were parsed ok, otherwise an error
1018 message which the caller must free.
1020 N.B. These commands affect all debug format settings, they are not
1021 cumulative. If a format is not specified, it is turned off.
1022 However, we don't go to extra trouble with things like
1023 "monitor set debug-format all,none,timestamp".
1024 Instead we just parse them one at a time, in order.
1026 The syntax for "monitor set debug" we support here is not identical
1027 to gdb's "set debug foo on|off" because we also use this function to
1028 parse "--debug-format=foo,bar". */
1031 parse_debug_format_options (const char *arg
, int is_monitor
)
1033 VEC (char_ptr
) *options
;
1037 /* First turn all debug format options off. */
1038 debug_timestamp
= 0;
1040 /* First remove leading spaces, for "monitor set debug-format". */
1041 while (isspace (*arg
))
1044 options
= delim_string_to_char_ptr_vec (arg
, ',');
1046 for (ix
= 0; VEC_iterate (char_ptr
, options
, ix
, option
); ++ix
)
1048 if (strcmp (option
, "all") == 0)
1050 debug_timestamp
= 1;
1052 monitor_output ("All extra debug format options enabled.\n");
1054 else if (strcmp (option
, "none") == 0)
1056 debug_timestamp
= 0;
1058 monitor_output ("All extra debug format options disabled.\n");
1060 else if (strcmp (option
, "timestamp") == 0)
1062 debug_timestamp
= 1;
1064 monitor_output ("Timestamps will be added to debug output.\n");
1066 else if (*option
== '\0')
1068 /* An empty option, e.g., "--debug-format=foo,,bar", is ignored. */
1073 char *msg
= xstrprintf ("Unknown debug-format argument: \"%s\"\n",
1076 free_char_ptr_vec (options
);
1081 free_char_ptr_vec (options
);
1085 /* Handle monitor commands not handled by target-specific handlers. */
1088 handle_monitor_command (char *mon
, char *own_buf
)
1090 if (strcmp (mon
, "set debug 1") == 0)
1093 monitor_output ("Debug output enabled.\n");
1095 else if (strcmp (mon
, "set debug 0") == 0)
1098 monitor_output ("Debug output disabled.\n");
1100 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
1102 show_debug_regs
= 1;
1103 monitor_output ("H/W point debugging output enabled.\n");
1105 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
1107 show_debug_regs
= 0;
1108 monitor_output ("H/W point debugging output disabled.\n");
1110 else if (strcmp (mon
, "set remote-debug 1") == 0)
1113 monitor_output ("Protocol debug output enabled.\n");
1115 else if (strcmp (mon
, "set remote-debug 0") == 0)
1118 monitor_output ("Protocol debug output disabled.\n");
1120 else if (startswith (mon
, "set debug-format "))
1123 = parse_debug_format_options (mon
+ sizeof ("set debug-format ") - 1,
1126 if (error_msg
!= NULL
)
1128 monitor_output (error_msg
);
1129 monitor_show_help ();
1130 write_enn (own_buf
);
1134 else if (strcmp (mon
, "help") == 0)
1135 monitor_show_help ();
1136 else if (strcmp (mon
, "exit") == 0)
1140 monitor_output ("Unknown monitor command.\n\n");
1141 monitor_show_help ();
1142 write_enn (own_buf
);
1146 /* Associates a callback with each supported qXfer'able object. */
1150 /* The object this handler handles. */
1153 /* Request that the target transfer up to LEN 8-bit bytes of the
1154 target's OBJECT. The OFFSET, for a seekable object, specifies
1155 the starting point. The ANNEX can be used to provide additional
1156 data-specific information to the target.
1158 Return the number of bytes actually transfered, zero when no
1159 further transfer is possible, -1 on error, -2 when the transfer
1160 is not supported, and -3 on a verbose error message that should
1161 be preserved. Return of a positive value smaller than LEN does
1162 not indicate the end of the object, only the end of the transfer.
1164 One, and only one, of readbuf or writebuf must be non-NULL. */
1165 int (*xfer
) (const char *annex
,
1166 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1167 ULONGEST offset
, LONGEST len
);
1170 /* Handle qXfer:auxv:read. */
1173 handle_qxfer_auxv (const char *annex
,
1174 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1175 ULONGEST offset
, LONGEST len
)
1177 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
1180 if (annex
[0] != '\0' || current_thread
== NULL
)
1183 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
1186 /* Handle qXfer:exec-file:read. */
1189 handle_qxfer_exec_file (const char *const_annex
,
1190 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1191 ULONGEST offset
, LONGEST len
)
1197 if (the_target
->pid_to_exec_file
== NULL
|| writebuf
!= NULL
)
1200 if (const_annex
[0] == '\0')
1202 if (current_thread
== NULL
)
1205 pid
= pid_of (current_thread
);
1209 char *annex
= alloca (strlen (const_annex
) + 1);
1211 strcpy (annex
, const_annex
);
1212 annex
= unpack_varlen_hex (annex
, &pid
);
1214 if (annex
[0] != '\0')
1221 file
= (*the_target
->pid_to_exec_file
) (pid
);
1225 total_len
= strlen (file
);
1227 if (offset
> total_len
)
1230 if (offset
+ len
> total_len
)
1231 len
= total_len
- offset
;
1233 memcpy (readbuf
, file
+ offset
, len
);
1237 /* Handle qXfer:features:read. */
1240 handle_qxfer_features (const char *annex
,
1241 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1242 ULONGEST offset
, LONGEST len
)
1244 const char *document
;
1247 if (writebuf
!= NULL
)
1250 if (!target_running ())
1253 /* Grab the correct annex. */
1254 document
= get_features_xml (annex
);
1255 if (document
== NULL
)
1258 total_len
= strlen (document
);
1260 if (offset
> total_len
)
1263 if (offset
+ len
> total_len
)
1264 len
= total_len
- offset
;
1266 memcpy (readbuf
, document
+ offset
, len
);
1270 /* Worker routine for handle_qxfer_libraries.
1271 Add to the length pointed to by ARG a conservative estimate of the
1272 length needed to transmit the file name of INF. */
1275 accumulate_file_name_length (struct inferior_list_entry
*inf
, void *arg
)
1277 struct dll_info
*dll
= (struct dll_info
*) inf
;
1278 unsigned int *total_len
= arg
;
1280 /* Over-estimate the necessary memory. Assume that every character
1281 in the library name must be escaped. */
1282 *total_len
+= 128 + 6 * strlen (dll
->name
);
1285 /* Worker routine for handle_qxfer_libraries.
1286 Emit the XML to describe the library in INF. */
1289 emit_dll_description (struct inferior_list_entry
*inf
, void *arg
)
1291 struct dll_info
*dll
= (struct dll_info
*) inf
;
1296 strcpy (p
, " <library name=\"");
1298 name
= xml_escape_text (dll
->name
);
1302 strcpy (p
, "\"><segment address=\"");
1304 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1306 strcpy (p
, "\"/></library>\n");
1312 /* Handle qXfer:libraries:read. */
1315 handle_qxfer_libraries (const char *annex
,
1316 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1317 ULONGEST offset
, LONGEST len
)
1319 unsigned int total_len
;
1322 if (writebuf
!= NULL
)
1325 if (annex
[0] != '\0' || current_thread
== NULL
)
1329 for_each_inferior_with_data (&all_dlls
, accumulate_file_name_length
,
1332 document
= malloc (total_len
);
1333 if (document
== NULL
)
1336 strcpy (document
, "<library-list version=\"1.0\">\n");
1337 p
= document
+ strlen (document
);
1339 for_each_inferior_with_data (&all_dlls
, emit_dll_description
, &p
);
1341 strcpy (p
, "</library-list>\n");
1343 total_len
= strlen (document
);
1345 if (offset
> total_len
)
1351 if (offset
+ len
> total_len
)
1352 len
= total_len
- offset
;
1354 memcpy (readbuf
, document
+ offset
, len
);
1359 /* Handle qXfer:libraries-svr4:read. */
1362 handle_qxfer_libraries_svr4 (const char *annex
,
1363 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1364 ULONGEST offset
, LONGEST len
)
1366 if (writebuf
!= NULL
)
1369 if (current_thread
== NULL
|| the_target
->qxfer_libraries_svr4
== NULL
)
1372 return the_target
->qxfer_libraries_svr4 (annex
, readbuf
, writebuf
, offset
, len
);
1375 /* Handle qXfer:osadata:read. */
1378 handle_qxfer_osdata (const char *annex
,
1379 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1380 ULONGEST offset
, LONGEST len
)
1382 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
1385 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
1388 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1391 handle_qxfer_siginfo (const char *annex
,
1392 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1393 ULONGEST offset
, LONGEST len
)
1395 if (the_target
->qxfer_siginfo
== NULL
)
1398 if (annex
[0] != '\0' || current_thread
== NULL
)
1401 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1404 /* Handle qXfer:spu:read and qXfer:spu:write. */
1407 handle_qxfer_spu (const char *annex
,
1408 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1409 ULONGEST offset
, LONGEST len
)
1411 if (the_target
->qxfer_spu
== NULL
)
1414 if (current_thread
== NULL
)
1417 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1420 /* Handle qXfer:statictrace:read. */
1423 handle_qxfer_statictrace (const char *annex
,
1424 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1425 ULONGEST offset
, LONGEST len
)
1429 if (writebuf
!= NULL
)
1432 if (annex
[0] != '\0' || current_thread
== NULL
|| current_traceframe
== -1)
1435 if (traceframe_read_sdata (current_traceframe
, offset
,
1436 readbuf
, len
, &nbytes
))
1441 /* Helper for handle_qxfer_threads_proper.
1442 Emit the XML to describe the thread of INF. */
1445 handle_qxfer_threads_worker (struct inferior_list_entry
*inf
, void *arg
)
1447 struct thread_info
*thread
= (struct thread_info
*) inf
;
1448 struct buffer
*buffer
= arg
;
1449 ptid_t ptid
= thread_to_gdb_id (thread
);
1451 int core
= target_core_of_thread (ptid
);
1454 write_ptid (ptid_s
, ptid
);
1458 sprintf (core_s
, "%d", core
);
1459 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1464 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1469 /* Helper for handle_qxfer_threads. */
1472 handle_qxfer_threads_proper (struct buffer
*buffer
)
1474 buffer_grow_str (buffer
, "<threads>\n");
1476 for_each_inferior_with_data (&all_threads
, handle_qxfer_threads_worker
,
1479 buffer_grow_str0 (buffer
, "</threads>\n");
1482 /* Handle qXfer:threads:read. */
1485 handle_qxfer_threads (const char *annex
,
1486 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1487 ULONGEST offset
, LONGEST len
)
1489 static char *result
= 0;
1490 static unsigned int result_length
= 0;
1492 if (writebuf
!= NULL
)
1495 if (annex
[0] != '\0')
1500 struct buffer buffer
;
1501 /* When asked for data at offset 0, generate everything and store into
1502 'result'. Successive reads will be served off 'result'. */
1506 buffer_init (&buffer
);
1508 handle_qxfer_threads_proper (&buffer
);
1510 result
= buffer_finish (&buffer
);
1511 result_length
= strlen (result
);
1512 buffer_free (&buffer
);
1515 if (offset
>= result_length
)
1517 /* We're out of data. */
1524 if (len
> result_length
- offset
)
1525 len
= result_length
- offset
;
1527 memcpy (readbuf
, result
+ offset
, len
);
1532 /* Handle qXfer:traceframe-info:read. */
1535 handle_qxfer_traceframe_info (const char *annex
,
1536 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1537 ULONGEST offset
, LONGEST len
)
1539 static char *result
= 0;
1540 static unsigned int result_length
= 0;
1542 if (writebuf
!= NULL
)
1545 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1550 struct buffer buffer
;
1552 /* When asked for data at offset 0, generate everything and
1553 store into 'result'. Successive reads will be served off
1557 buffer_init (&buffer
);
1559 traceframe_read_info (current_traceframe
, &buffer
);
1561 result
= buffer_finish (&buffer
);
1562 result_length
= strlen (result
);
1563 buffer_free (&buffer
);
1566 if (offset
>= result_length
)
1568 /* We're out of data. */
1575 if (len
> result_length
- offset
)
1576 len
= result_length
- offset
;
1578 memcpy (readbuf
, result
+ offset
, len
);
1582 /* Handle qXfer:fdpic:read. */
1585 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1586 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1588 if (the_target
->read_loadmap
== NULL
)
1591 if (current_thread
== NULL
)
1594 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1597 /* Handle qXfer:btrace:read. */
1600 handle_qxfer_btrace (const char *annex
,
1601 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1602 ULONGEST offset
, LONGEST len
)
1604 static struct buffer cache
;
1605 struct thread_info
*thread
;
1608 if (the_target
->read_btrace
== NULL
|| writebuf
!= NULL
)
1611 if (ptid_equal (general_thread
, null_ptid
)
1612 || ptid_equal (general_thread
, minus_one_ptid
))
1614 strcpy (own_buf
, "E.Must select a single thread.");
1618 thread
= find_thread_ptid (general_thread
);
1621 strcpy (own_buf
, "E.No such thread.");
1625 if (thread
->btrace
== NULL
)
1627 strcpy (own_buf
, "E.Btrace not enabled.");
1631 if (strcmp (annex
, "all") == 0)
1632 type
= BTRACE_READ_ALL
;
1633 else if (strcmp (annex
, "new") == 0)
1634 type
= BTRACE_READ_NEW
;
1635 else if (strcmp (annex
, "delta") == 0)
1636 type
= BTRACE_READ_DELTA
;
1639 strcpy (own_buf
, "E.Bad annex.");
1645 buffer_free (&cache
);
1647 result
= target_read_btrace (thread
->btrace
, &cache
, type
);
1650 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1654 else if (offset
> cache
.used_size
)
1656 buffer_free (&cache
);
1660 if (len
> cache
.used_size
- offset
)
1661 len
= cache
.used_size
- offset
;
1663 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1668 /* Handle qXfer:btrace-conf:read. */
1671 handle_qxfer_btrace_conf (const char *annex
,
1672 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1673 ULONGEST offset
, LONGEST len
)
1675 static struct buffer cache
;
1676 struct thread_info
*thread
;
1679 if (the_target
->read_btrace_conf
== NULL
|| writebuf
!= NULL
)
1682 if (annex
[0] != '\0')
1685 if (ptid_equal (general_thread
, null_ptid
)
1686 || ptid_equal (general_thread
, minus_one_ptid
))
1688 strcpy (own_buf
, "E.Must select a single thread.");
1692 thread
= find_thread_ptid (general_thread
);
1695 strcpy (own_buf
, "E.No such thread.");
1699 if (thread
->btrace
== NULL
)
1701 strcpy (own_buf
, "E.Btrace not enabled.");
1707 buffer_free (&cache
);
1709 result
= target_read_btrace_conf (thread
->btrace
, &cache
);
1712 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1716 else if (offset
> cache
.used_size
)
1718 buffer_free (&cache
);
1722 if (len
> cache
.used_size
- offset
)
1723 len
= cache
.used_size
- offset
;
1725 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1730 static const struct qxfer qxfer_packets
[] =
1732 { "auxv", handle_qxfer_auxv
},
1733 { "btrace", handle_qxfer_btrace
},
1734 { "btrace-conf", handle_qxfer_btrace_conf
},
1735 { "exec-file", handle_qxfer_exec_file
},
1736 { "fdpic", handle_qxfer_fdpic
},
1737 { "features", handle_qxfer_features
},
1738 { "libraries", handle_qxfer_libraries
},
1739 { "libraries-svr4", handle_qxfer_libraries_svr4
},
1740 { "osdata", handle_qxfer_osdata
},
1741 { "siginfo", handle_qxfer_siginfo
},
1742 { "spu", handle_qxfer_spu
},
1743 { "statictrace", handle_qxfer_statictrace
},
1744 { "threads", handle_qxfer_threads
},
1745 { "traceframe-info", handle_qxfer_traceframe_info
},
1749 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1757 if (!startswith (own_buf
, "qXfer:"))
1760 /* Grab the object, r/w and annex. */
1761 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1763 write_enn (own_buf
);
1768 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1771 const struct qxfer
*q
= &qxfer_packets
[i
];
1773 if (strcmp (object
, q
->object
) == 0)
1775 if (strcmp (rw
, "read") == 0)
1777 unsigned char *data
;
1782 /* Grab the offset and length. */
1783 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1785 write_enn (own_buf
);
1789 /* Read one extra byte, as an indicator of whether there is
1791 if (len
> PBUFSIZ
- 2)
1793 data
= malloc (len
+ 1);
1796 write_enn (own_buf
);
1799 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1807 /* Preserve error message. */
1810 write_enn (own_buf
);
1812 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1814 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1819 else if (strcmp (rw
, "write") == 0)
1824 unsigned char *data
;
1826 strcpy (own_buf
, "E00");
1827 data
= malloc (packet_len
- (offset
- own_buf
));
1830 write_enn (own_buf
);
1833 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1834 &ofs
, &len
, data
) < 0)
1837 write_enn (own_buf
);
1841 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1849 /* Preserve error message. */
1852 write_enn (own_buf
);
1854 sprintf (own_buf
, "%x", n
);
1867 /* Table used by the crc32 function to calcuate the checksum. */
1869 static unsigned int crc32_table
[256] =
1872 /* Compute 32 bit CRC from inferior memory.
1874 On success, return 32 bit CRC.
1875 On failure, return (unsigned long long) -1. */
1877 static unsigned long long
1878 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1880 if (!crc32_table
[1])
1882 /* Initialize the CRC table and the decoding table. */
1886 for (i
= 0; i
< 256; i
++)
1888 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1889 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1896 unsigned char byte
= 0;
1898 /* Return failure if memory read fails. */
1899 if (read_inferior_memory (base
, &byte
, 1) != 0)
1900 return (unsigned long long) -1;
1902 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1905 return (unsigned long long) crc
;
1908 /* Add supported btrace packets to BUF. */
1911 supported_btrace_packets (char *buf
)
1913 int btrace_supported
= 0;
1915 if (target_supports_btrace (BTRACE_FORMAT_BTS
))
1917 strcat (buf
, ";Qbtrace:bts+");
1918 strcat (buf
, ";Qbtrace-conf:bts:size+");
1920 btrace_supported
= 1;
1923 if (target_supports_btrace (BTRACE_FORMAT_PT
))
1925 strcat (buf
, ";Qbtrace:pt+");
1926 strcat (buf
, ";Qbtrace-conf:pt:size+");
1928 btrace_supported
= 1;
1931 if (!btrace_supported
)
1934 strcat (buf
, ";Qbtrace:off+");
1935 strcat (buf
, ";qXfer:btrace:read+");
1936 strcat (buf
, ";qXfer:btrace-conf:read+");
1939 /* Handle all of the extended 'q' packets. */
1942 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1944 static struct inferior_list_entry
*thread_ptr
;
1946 /* Reply the current thread id. */
1947 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1950 require_running (own_buf
);
1952 if (!ptid_equal (general_thread
, null_ptid
)
1953 && !ptid_equal (general_thread
, minus_one_ptid
))
1954 gdb_id
= general_thread
;
1957 thread_ptr
= get_first_inferior (&all_threads
);
1958 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1961 sprintf (own_buf
, "QC");
1963 write_ptid (own_buf
, gdb_id
);
1967 if (strcmp ("qSymbol::", own_buf
) == 0)
1969 /* GDB is suggesting new symbols have been loaded. This may
1970 mean a new shared library has been detected as loaded, so
1971 take the opportunity to check if breakpoints we think are
1972 inserted, still are. Note that it isn't guaranteed that
1973 we'll see this when a shared library is loaded, and nor will
1974 we see this for unloads (although breakpoints in unloaded
1975 libraries shouldn't trigger), as GDB may not find symbols for
1976 the library at all. We also re-validate breakpoints when we
1977 see a second GDB breakpoint for the same address, and or when
1978 we access breakpoint shadows. */
1979 validate_breakpoints ();
1981 if (target_supports_tracepoints ())
1982 tracepoint_look_up_symbols ();
1984 if (current_thread
!= NULL
&& the_target
->look_up_symbols
!= NULL
)
1985 (*the_target
->look_up_symbols
) ();
1987 strcpy (own_buf
, "OK");
1991 if (!disable_packet_qfThreadInfo
)
1993 if (strcmp ("qfThreadInfo", own_buf
) == 0)
1997 require_running (own_buf
);
1998 thread_ptr
= get_first_inferior (&all_threads
);
2001 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
2002 write_ptid (own_buf
, gdb_id
);
2003 thread_ptr
= thread_ptr
->next
;
2007 if (strcmp ("qsThreadInfo", own_buf
) == 0)
2011 require_running (own_buf
);
2012 if (thread_ptr
!= NULL
)
2015 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
2016 write_ptid (own_buf
, gdb_id
);
2017 thread_ptr
= thread_ptr
->next
;
2022 sprintf (own_buf
, "l");
2028 if (the_target
->read_offsets
!= NULL
2029 && strcmp ("qOffsets", own_buf
) == 0)
2031 CORE_ADDR text
, data
;
2033 require_running (own_buf
);
2034 if (the_target
->read_offsets (&text
, &data
))
2035 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
2036 (long)text
, (long)data
, (long)data
);
2038 write_enn (own_buf
);
2043 /* Protocol features query. */
2044 if (startswith (own_buf
, "qSupported")
2045 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
2047 char *p
= &own_buf
[10];
2048 int gdb_supports_qRelocInsn
= 0;
2050 /* Start processing qSupported packet. */
2051 target_process_qsupported (NULL
);
2053 /* Process each feature being provided by GDB. The first
2054 feature will follow a ':', and latter features will follow
2058 char **qsupported
= NULL
;
2062 /* Two passes, to avoid nested strtok calls in
2063 target_process_qsupported. */
2064 for (p
= strtok (p
+ 1, ";");
2066 p
= strtok (NULL
, ";"))
2069 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
2070 qsupported
[count
- 1] = xstrdup (p
);
2073 for (i
= 0; i
< count
; i
++)
2076 if (strcmp (p
, "multiprocess+") == 0)
2078 /* GDB supports and wants multi-process support if
2080 if (target_supports_multi_process ())
2083 else if (strcmp (p
, "qRelocInsn+") == 0)
2085 /* GDB supports relocate instruction requests. */
2086 gdb_supports_qRelocInsn
= 1;
2088 else if (strcmp (p
, "swbreak+") == 0)
2090 /* GDB wants us to report whether a trap is caused
2091 by a software breakpoint and for us to handle PC
2092 adjustment if necessary on this target. */
2093 if (target_supports_stopped_by_sw_breakpoint ())
2094 swbreak_feature
= 1;
2096 else if (strcmp (p
, "hwbreak+") == 0)
2098 /* GDB wants us to report whether a trap is caused
2099 by a hardware breakpoint. */
2100 if (target_supports_stopped_by_hw_breakpoint ())
2101 hwbreak_feature
= 1;
2103 else if (strcmp (p
, "fork-events+") == 0)
2105 /* GDB supports and wants fork events if possible. */
2106 if (target_supports_fork_events ())
2107 report_fork_events
= 1;
2109 else if (strcmp (p
, "vfork-events+") == 0)
2111 /* GDB supports and wants vfork events if possible. */
2112 if (target_supports_vfork_events ())
2113 report_vfork_events
= 1;
2115 if (strcmp (p
, "exec-events+") == 0)
2117 /* GDB supports and wants exec events if possible. */
2118 if (target_supports_exec_events ())
2119 report_exec_events
= 1;
2122 target_process_qsupported (p
);
2131 "PacketSize=%x;QPassSignals+;QProgramSignals+",
2134 if (the_target
->qxfer_libraries_svr4
!= NULL
)
2135 strcat (own_buf
, ";qXfer:libraries-svr4:read+"
2136 ";augmented-libraries-svr4-read+");
2139 /* We do not have any hook to indicate whether the non-SVR4 target
2140 backend supports qXfer:libraries:read, so always report it. */
2141 strcat (own_buf
, ";qXfer:libraries:read+");
2144 if (the_target
->read_auxv
!= NULL
)
2145 strcat (own_buf
, ";qXfer:auxv:read+");
2147 if (the_target
->qxfer_spu
!= NULL
)
2148 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
2150 if (the_target
->qxfer_siginfo
!= NULL
)
2151 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
2153 if (the_target
->read_loadmap
!= NULL
)
2154 strcat (own_buf
, ";qXfer:fdpic:read+");
2156 /* We always report qXfer:features:read, as targets may
2157 install XML files on a subsequent call to arch_setup.
2158 If we reported to GDB on startup that we don't support
2159 qXfer:feature:read at all, we will never be re-queried. */
2160 strcat (own_buf
, ";qXfer:features:read+");
2162 if (transport_is_reliable
)
2163 strcat (own_buf
, ";QStartNoAckMode+");
2165 if (the_target
->qxfer_osdata
!= NULL
)
2166 strcat (own_buf
, ";qXfer:osdata:read+");
2168 if (target_supports_multi_process ())
2169 strcat (own_buf
, ";multiprocess+");
2171 if (target_supports_fork_events ())
2172 strcat (own_buf
, ";fork-events+");
2174 if (target_supports_vfork_events ())
2175 strcat (own_buf
, ";vfork-events+");
2177 if (target_supports_exec_events ())
2178 strcat (own_buf
, ";exec-events+");
2180 if (target_supports_non_stop ())
2181 strcat (own_buf
, ";QNonStop+");
2183 if (target_supports_disable_randomization ())
2184 strcat (own_buf
, ";QDisableRandomization+");
2186 strcat (own_buf
, ";qXfer:threads:read+");
2188 if (target_supports_tracepoints ())
2190 strcat (own_buf
, ";ConditionalTracepoints+");
2191 strcat (own_buf
, ";TraceStateVariables+");
2192 strcat (own_buf
, ";TracepointSource+");
2193 strcat (own_buf
, ";DisconnectedTracing+");
2194 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
2195 strcat (own_buf
, ";FastTracepoints+");
2196 strcat (own_buf
, ";StaticTracepoints+");
2197 strcat (own_buf
, ";InstallInTrace+");
2198 strcat (own_buf
, ";qXfer:statictrace:read+");
2199 strcat (own_buf
, ";qXfer:traceframe-info:read+");
2200 strcat (own_buf
, ";EnableDisableTracepoints+");
2201 strcat (own_buf
, ";QTBuffer:size+");
2202 strcat (own_buf
, ";tracenz+");
2205 /* Support target-side breakpoint conditions and commands. */
2206 if (target_supports_conditional_breakpoints ())
2207 strcat (own_buf
, ";ConditionalBreakpoints+");
2208 strcat (own_buf
, ";BreakpointCommands+");
2210 if (target_supports_agent ())
2211 strcat (own_buf
, ";QAgent+");
2213 supported_btrace_packets (own_buf
);
2215 if (target_supports_stopped_by_sw_breakpoint ())
2216 strcat (own_buf
, ";swbreak+");
2218 if (target_supports_stopped_by_hw_breakpoint ())
2219 strcat (own_buf
, ";hwbreak+");
2221 if (the_target
->pid_to_exec_file
!= NULL
)
2222 strcat (own_buf
, ";qXfer:exec-file:read+");
2224 /* Reinitialize components as needed for the new connection. */
2225 hostio_handle_new_gdb_connection ();
2226 target_handle_new_gdb_connection ();
2231 /* Thread-local storage support. */
2232 if (the_target
->get_tls_address
!= NULL
2233 && startswith (own_buf
, "qGetTLSAddr:"))
2235 char *p
= own_buf
+ 12;
2236 CORE_ADDR parts
[2], address
= 0;
2238 ptid_t ptid
= null_ptid
;
2240 require_running (own_buf
);
2242 for (i
= 0; i
< 3; i
++)
2250 p2
= strchr (p
, ',');
2263 ptid
= read_ptid (p
, NULL
);
2265 decode_address (&parts
[i
- 1], p
, len
);
2269 if (p
!= NULL
|| i
< 3)
2273 struct thread_info
*thread
= find_thread_ptid (ptid
);
2278 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
2284 strcpy (own_buf
, paddress(address
));
2289 write_enn (own_buf
);
2293 /* Otherwise, pretend we do not understand this packet. */
2296 /* Windows OS Thread Information Block address support. */
2297 if (the_target
->get_tib_address
!= NULL
2298 && startswith (own_buf
, "qGetTIBAddr:"))
2303 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
2305 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
2308 strcpy (own_buf
, paddress(tlb
));
2313 write_enn (own_buf
);
2319 /* Handle "monitor" commands. */
2320 if (startswith (own_buf
, "qRcmd,"))
2322 char *mon
= malloc (PBUFSIZ
);
2323 int len
= strlen (own_buf
+ 6);
2327 write_enn (own_buf
);
2332 || hex2bin (own_buf
+ 6, (gdb_byte
*) mon
, len
/ 2) != len
/ 2)
2334 write_enn (own_buf
);
2338 mon
[len
/ 2] = '\0';
2342 if (the_target
->handle_monitor_command
== NULL
2343 || (*the_target
->handle_monitor_command
) (mon
) == 0)
2344 /* Default processing. */
2345 handle_monitor_command (mon
, own_buf
);
2351 if (startswith (own_buf
, "qSearch:memory:"))
2353 require_running (own_buf
);
2354 handle_search_memory (own_buf
, packet_len
);
2358 if (strcmp (own_buf
, "qAttached") == 0
2359 || startswith (own_buf
, "qAttached:"))
2361 struct process_info
*process
;
2363 if (own_buf
[sizeof ("qAttached") - 1])
2365 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
2366 process
= (struct process_info
*)
2367 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
2371 require_running (own_buf
);
2372 process
= current_process ();
2375 if (process
== NULL
)
2377 write_enn (own_buf
);
2381 strcpy (own_buf
, process
->attached
? "1" : "0");
2385 if (startswith (own_buf
, "qCRC:"))
2387 /* CRC check (compare-section). */
2391 unsigned long long crc
;
2393 require_running (own_buf
);
2394 comma
= unpack_varlen_hex (own_buf
+ 5, &base
);
2395 if (*comma
++ != ',')
2397 write_enn (own_buf
);
2400 len
= strtoul (comma
, NULL
, 16);
2401 crc
= crc32 (base
, len
, 0xffffffff);
2402 /* Check for memory failure. */
2403 if (crc
== (unsigned long long) -1)
2405 write_enn (own_buf
);
2408 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
2412 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
2415 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
2418 /* Otherwise we didn't know what packet it was. Say we didn't
2423 static void gdb_wants_all_threads_stopped (void);
2424 static void resume (struct thread_resume
*actions
, size_t n
);
2426 /* The callback that is passed to visit_actioned_threads. */
2427 typedef int (visit_actioned_threads_callback_ftype
)
2428 (const struct thread_resume
*, struct thread_info
*);
2430 /* Struct to pass data to visit_actioned_threads. */
2432 struct visit_actioned_threads_data
2434 const struct thread_resume
*actions
;
2436 visit_actioned_threads_callback_ftype
*callback
;
2439 /* Call CALLBACK for any thread to which ACTIONS applies to. Returns
2440 true if CALLBACK returns true. Returns false if no matching thread
2441 is found or CALLBACK results false.
2442 Note: This function is itself a callback for find_inferior. */
2445 visit_actioned_threads (struct inferior_list_entry
*entry
, void *datap
)
2447 struct visit_actioned_threads_data
*data
= datap
;
2448 const struct thread_resume
*actions
= data
->actions
;
2449 size_t num_actions
= data
->num_actions
;
2450 visit_actioned_threads_callback_ftype
*callback
= data
->callback
;
2453 for (i
= 0; i
< num_actions
; i
++)
2455 const struct thread_resume
*action
= &actions
[i
];
2457 if (ptid_equal (action
->thread
, minus_one_ptid
)
2458 || ptid_equal (action
->thread
, entry
->id
)
2459 || ((ptid_get_pid (action
->thread
)
2460 == ptid_get_pid (entry
->id
))
2461 && ptid_get_lwp (action
->thread
) == -1))
2463 struct thread_info
*thread
= (struct thread_info
*) entry
;
2465 if ((*callback
) (action
, thread
))
2473 /* Callback for visit_actioned_threads. If the thread has a pending
2474 status to report, report it now. */
2477 handle_pending_status (const struct thread_resume
*resumption
,
2478 struct thread_info
*thread
)
2480 if (thread
->status_pending_p
)
2482 thread
->status_pending_p
= 0;
2484 last_status
= thread
->last_status
;
2485 last_ptid
= thread
->entry
.id
;
2486 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2492 /* Parse vCont packets. */
2494 handle_v_cont (char *own_buf
)
2498 struct thread_resume
*resume_info
;
2499 struct thread_resume default_action
= {{0}};
2501 /* Count the number of semicolons in the packet. There should be one
2502 for every action. */
2508 p
= strchr (p
, ';');
2511 resume_info
= malloc (n
* sizeof (resume_info
[0]));
2512 if (resume_info
== NULL
)
2520 memset (&resume_info
[i
], 0, sizeof resume_info
[i
]);
2522 if (p
[0] == 's' || p
[0] == 'S')
2523 resume_info
[i
].kind
= resume_step
;
2524 else if (p
[0] == 'r')
2525 resume_info
[i
].kind
= resume_step
;
2526 else if (p
[0] == 'c' || p
[0] == 'C')
2527 resume_info
[i
].kind
= resume_continue
;
2528 else if (p
[0] == 't')
2529 resume_info
[i
].kind
= resume_stop
;
2533 if (p
[0] == 'S' || p
[0] == 'C')
2536 sig
= strtol (p
+ 1, &q
, 16);
2541 if (!gdb_signal_to_host_p (sig
))
2543 resume_info
[i
].sig
= gdb_signal_to_host (sig
);
2545 else if (p
[0] == 'r')
2549 p
= unpack_varlen_hex (p
+ 1, &addr
);
2550 resume_info
[i
].step_range_start
= addr
;
2555 p
= unpack_varlen_hex (p
+ 1, &addr
);
2556 resume_info
[i
].step_range_end
= addr
;
2565 resume_info
[i
].thread
= minus_one_ptid
;
2566 default_action
= resume_info
[i
];
2568 /* Note: we don't increment i here, we'll overwrite this entry
2569 the next time through. */
2571 else if (p
[0] == ':')
2573 ptid_t ptid
= read_ptid (p
+ 1, &q
);
2578 if (p
[0] != ';' && p
[0] != 0)
2581 resume_info
[i
].thread
= ptid
;
2588 resume_info
[i
] = default_action
;
2590 resume (resume_info
, n
);
2595 write_enn (own_buf
);
2600 /* Resume target with ACTIONS, an array of NUM_ACTIONS elements. */
2603 resume (struct thread_resume
*actions
, size_t num_actions
)
2607 /* Check if among the threads that GDB wants actioned, there's
2608 one with a pending status to report. If so, skip actually
2609 resuming/stopping and report the pending event
2611 struct visit_actioned_threads_data data
;
2613 data
.actions
= actions
;
2614 data
.num_actions
= num_actions
;
2615 data
.callback
= handle_pending_status
;
2616 if (find_inferior (&all_threads
, visit_actioned_threads
, &data
) != NULL
)
2622 (*the_target
->resume
) (actions
, num_actions
);
2628 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2630 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
)
2632 /* No proper RSP support for this yet. At least return
2634 sprintf (own_buf
, "E.No unwaited-for children left.");
2635 disable_async_io ();
2639 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2640 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
2641 && last_status
.kind
!= TARGET_WAITKIND_NO_RESUMED
)
2642 current_thread
->last_status
= last_status
;
2644 /* From the client's perspective, all-stop mode always stops all
2645 threads implicitly (and the target backend has already done
2646 so by now). Tag all threads as "want-stopped", so we don't
2647 resume them implicitly without the client telling us to. */
2648 gdb_wants_all_threads_stopped ();
2649 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2650 disable_async_io ();
2652 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2653 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2654 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2658 /* Attach to a new program. Return 1 if successful, 0 if failure. */
2660 handle_v_attach (char *own_buf
)
2664 pid
= strtol (own_buf
+ 8, NULL
, 16);
2665 if (pid
!= 0 && attach_inferior (pid
) == 0)
2667 /* Don't report shared library events after attaching, even if
2668 some libraries are preloaded. GDB will always poll the
2669 library list. Avoids the "stopped by shared library event"
2670 notice on the GDB side. */
2675 /* In non-stop, we don't send a resume reply. Stop events
2676 will follow up using the normal notification
2681 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2687 write_enn (own_buf
);
2692 /* Run a new program. Return 1 if successful, 0 if failure. */
2694 handle_v_run (char *own_buf
)
2696 char *p
, *next_p
, **new_argv
;
2700 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
2706 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
2707 if (new_argv
== NULL
)
2709 write_enn (own_buf
);
2714 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
2716 next_p
= strchr (p
, ';');
2718 next_p
= p
+ strlen (p
);
2720 if (i
== 0 && p
== next_p
)
2724 /* FIXME: Fail request if out of memory instead of dying. */
2725 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
2726 hex2bin (p
, (gdb_byte
*) new_argv
[i
], (next_p
- p
) / 2);
2727 new_argv
[i
][(next_p
- p
) / 2] = '\0';
2736 if (new_argv
[0] == NULL
)
2738 /* GDB didn't specify a program to run. Use the program from the
2739 last run with the new argument list. */
2741 if (program_argv
== NULL
)
2743 write_enn (own_buf
);
2744 freeargv (new_argv
);
2748 new_argv
[0] = strdup (program_argv
[0]);
2749 if (new_argv
[0] == NULL
)
2751 write_enn (own_buf
);
2752 freeargv (new_argv
);
2757 /* Free the old argv and install the new one. */
2758 freeargv (program_argv
);
2759 program_argv
= new_argv
;
2761 start_inferior (program_argv
);
2762 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
2764 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2766 /* In non-stop, sending a resume reply doesn't set the general
2767 thread, but GDB assumes a vRun sets it (this is so GDB can
2768 query which is the main thread of the new inferior. */
2770 general_thread
= last_ptid
;
2776 write_enn (own_buf
);
2781 /* Kill process. Return 1 if successful, 0 if failure. */
2783 handle_v_kill (char *own_buf
)
2786 char *p
= &own_buf
[6];
2788 pid
= strtol (p
, NULL
, 16);
2791 if (pid
!= 0 && kill_inferior (pid
) == 0)
2793 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2794 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
2795 last_ptid
= pid_to_ptid (pid
);
2796 discard_queued_stop_replies (last_ptid
);
2802 write_enn (own_buf
);
2807 /* Handle all of the extended 'v' packets. */
2809 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2811 if (!disable_packet_vCont
)
2813 if (startswith (own_buf
, "vCont;"))
2815 require_running (own_buf
);
2816 handle_v_cont (own_buf
);
2820 if (startswith (own_buf
, "vCont?"))
2822 strcpy (own_buf
, "vCont;c;C;s;S;t");
2823 if (target_supports_range_stepping ())
2825 own_buf
= own_buf
+ strlen (own_buf
);
2826 strcpy (own_buf
, ";r");
2832 if (startswith (own_buf
, "vFile:")
2833 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2836 if (startswith (own_buf
, "vAttach;"))
2838 if ((!extended_protocol
|| !multi_process
) && target_running ())
2840 fprintf (stderr
, "Already debugging a process\n");
2841 write_enn (own_buf
);
2844 handle_v_attach (own_buf
);
2848 if (startswith (own_buf
, "vRun;"))
2850 if ((!extended_protocol
|| !multi_process
) && target_running ())
2852 fprintf (stderr
, "Already debugging a process\n");
2853 write_enn (own_buf
);
2856 handle_v_run (own_buf
);
2860 if (startswith (own_buf
, "vKill;"))
2862 if (!target_running ())
2864 fprintf (stderr
, "No process to kill\n");
2865 write_enn (own_buf
);
2868 handle_v_kill (own_buf
);
2872 if (handle_notif_ack (own_buf
, packet_len
))
2875 /* Otherwise we didn't know what packet it was. Say we didn't
2881 /* Resume thread and wait for another event. In non-stop mode,
2882 don't really wait here, but return immediatelly to the event
2885 myresume (char *own_buf
, int step
, int sig
)
2887 struct thread_resume resume_info
[2];
2889 int valid_cont_thread
;
2891 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2892 && !ptid_equal (cont_thread
, minus_one_ptid
));
2894 if (step
|| sig
|| valid_cont_thread
)
2896 resume_info
[0].thread
= current_ptid
;
2898 resume_info
[0].kind
= resume_step
;
2900 resume_info
[0].kind
= resume_continue
;
2901 resume_info
[0].sig
= sig
;
2905 if (!valid_cont_thread
)
2907 resume_info
[n
].thread
= minus_one_ptid
;
2908 resume_info
[n
].kind
= resume_continue
;
2909 resume_info
[n
].sig
= 0;
2913 resume (resume_info
, n
);
2916 /* Callback for for_each_inferior. Make a new stop reply for each
2920 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2922 struct thread_info
*thread
= (struct thread_info
*) entry
;
2924 /* For now, assume targets that don't have this callback also don't
2925 manage the thread's last_status field. */
2926 if (the_target
->thread_stopped
== NULL
)
2928 struct vstop_notif
*new_notif
= XNEW (struct vstop_notif
);
2930 new_notif
->ptid
= entry
->id
;
2931 new_notif
->status
= thread
->last_status
;
2932 /* Pass the last stop reply back to GDB, but don't notify
2934 notif_event_enque (¬if_stop
,
2935 (struct notif_event
*) new_notif
);
2939 if (thread_stopped (thread
))
2944 = target_waitstatus_to_string (&thread
->last_status
);
2946 debug_printf ("Reporting thread %s as already stopped with %s\n",
2947 target_pid_to_str (entry
->id
),
2950 xfree (status_string
);
2953 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2955 /* Pass the last stop reply back to GDB, but don't notify
2957 queue_stop_reply (entry
->id
, &thread
->last_status
);
2964 /* Set this inferior threads's state as "want-stopped". We won't
2965 resume this thread until the client gives us another action for
2969 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2971 struct thread_info
*thread
= (struct thread_info
*) entry
;
2973 thread
->last_resume_kind
= resume_stop
;
2975 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2977 /* Most threads are stopped implicitly (all-stop); tag that with
2979 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2980 thread
->last_status
.value
.sig
= GDB_SIGNAL_0
;
2984 /* Set all threads' states as "want-stopped". */
2987 gdb_wants_all_threads_stopped (void)
2989 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2992 /* Clear the gdb_detached flag of every process. */
2995 gdb_reattached_process (struct inferior_list_entry
*entry
)
2997 struct process_info
*process
= (struct process_info
*) entry
;
2999 process
->gdb_detached
= 0;
3002 /* Callback for for_each_inferior. Clear the thread's pending status
3006 clear_pending_status_callback (struct inferior_list_entry
*entry
)
3008 struct thread_info
*thread
= (struct thread_info
*) entry
;
3010 thread
->status_pending_p
= 0;
3013 /* Callback for for_each_inferior. If the thread is stopped with an
3014 interesting event, mark it as having a pending event. */
3017 set_pending_status_callback (struct inferior_list_entry
*entry
)
3019 struct thread_info
*thread
= (struct thread_info
*) entry
;
3021 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
3022 || (thread
->last_status
.value
.sig
!= GDB_SIGNAL_0
3023 /* A breakpoint, watchpoint or finished step from a previous
3024 GDB run isn't considered interesting for a new GDB run.
3025 If we left those pending, the new GDB could consider them
3026 random SIGTRAPs. This leaves out real async traps. We'd
3027 have to peek into the (target-specific) siginfo to
3028 distinguish those. */
3029 && thread
->last_status
.value
.sig
!= GDB_SIGNAL_TRAP
))
3030 thread
->status_pending_p
= 1;
3033 /* Callback for find_inferior. Return true if ENTRY (a thread) has a
3034 pending status to report to GDB. */
3037 find_status_pending_thread_callback (struct inferior_list_entry
*entry
, void *data
)
3039 struct thread_info
*thread
= (struct thread_info
*) entry
;
3041 return thread
->status_pending_p
;
3044 /* Status handler for the '?' packet. */
3047 handle_status (char *own_buf
)
3049 /* GDB is connected, don't forward events to the target anymore. */
3050 for_each_inferior (&all_processes
, gdb_reattached_process
);
3052 /* In non-stop mode, we must send a stop reply for each stopped
3053 thread. In all-stop mode, just send one for the first stopped
3058 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
3060 /* The first is sent immediatly. OK is sent if there is no
3061 stopped thread, which is the same handling of the vStopped
3062 packet (by design). */
3063 notif_write_event (¬if_stop
, own_buf
);
3067 struct inferior_list_entry
*thread
= NULL
;
3070 stabilize_threads ();
3071 gdb_wants_all_threads_stopped ();
3073 /* We can only report one status, but we might be coming out of
3074 non-stop -- if more than one thread is stopped with
3075 interesting events, leave events for the threads we're not
3076 reporting now pending. They'll be reported the next time the
3077 threads are resumed. Start by marking all interesting events
3079 for_each_inferior (&all_threads
, set_pending_status_callback
);
3081 /* Prefer the last thread that reported an event to GDB (even if
3082 that was a GDB_SIGNAL_TRAP). */
3083 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
3084 && last_status
.kind
!= TARGET_WAITKIND_EXITED
3085 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
3086 thread
= find_inferior_id (&all_threads
, last_ptid
);
3088 /* If the last event thread is not found for some reason, look
3089 for some other thread that might have an event to report. */
3091 thread
= find_inferior (&all_threads
,
3092 find_status_pending_thread_callback
, NULL
);
3094 /* If we're still out of luck, simply pick the first thread in
3097 thread
= get_first_inferior (&all_threads
);
3101 struct thread_info
*tp
= (struct thread_info
*) thread
;
3103 /* We're reporting this event, so it's no longer
3105 tp
->status_pending_p
= 0;
3107 /* GDB assumes the current thread is the thread we're
3108 reporting the status for. */
3109 general_thread
= thread
->id
;
3110 set_desired_thread (1);
3112 gdb_assert (tp
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
3113 prepare_resume_reply (own_buf
, tp
->entry
.id
, &tp
->last_status
);
3116 strcpy (own_buf
, "W00");
3121 gdbserver_version (void)
3123 printf ("GNU gdbserver %s%s\n"
3124 "Copyright (C) 2015 Free Software Foundation, Inc.\n"
3125 "gdbserver is free software, covered by the "
3126 "GNU General Public License.\n"
3127 "This gdbserver was configured as \"%s\"\n",
3128 PKGVERSION
, version
, host_name
);
3132 gdbserver_usage (FILE *stream
)
3134 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
3135 "\tgdbserver [OPTIONS] --attach COMM PID\n"
3136 "\tgdbserver [OPTIONS] --multi COMM\n"
3138 "COMM may either be a tty device (for serial debugging),\n"
3139 "HOST:PORT to listen for a TCP connection, or '-' or 'stdio' to use \n"
3140 "stdin/stdout of gdbserver.\n"
3141 "PROG is the executable program. ARGS are arguments passed to inferior.\n"
3142 "PID is the process ID to attach to, when --attach is specified.\n"
3144 "Operating modes:\n"
3146 " --attach Attach to running process PID.\n"
3147 " --multi Start server without a specific program, and\n"
3148 " only quit when explicitly commanded.\n"
3149 " --once Exit after the first connection has closed.\n"
3150 " --help Print this message and then exit.\n"
3151 " --version Display version information and exit.\n"
3155 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
3156 " --disable-randomization\n"
3157 " Run PROG with address space randomization disabled.\n"
3158 " --no-disable-randomization\n"
3159 " Don't disable address space randomization when\n"
3164 " --debug Enable general debugging output.\n"
3165 " --debug-format=opt1[,opt2,...]\n"
3166 " Specify extra content in debugging output.\n"
3171 " --remote-debug Enable remote protocol debugging output.\n"
3172 " --disable-packet=opt1[,opt2,...]\n"
3173 " Disable support for RSP packets or features.\n"
3175 " vCont, Tthread, qC, qfThreadInfo and \n"
3176 " threads (disable all threading packets).\n"
3178 "For more information, consult the GDB manual (available as on-line \n"
3179 "info or a printed manual).\n");
3180 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
3181 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
3185 gdbserver_show_disableable (FILE *stream
)
3187 fprintf (stream
, "Disableable packets:\n"
3188 " vCont \tAll vCont packets\n"
3189 " qC \tQuerying the current thread\n"
3190 " qfThreadInfo\tThread listing\n"
3191 " Tthread \tPassing the thread specifier in the "
3192 "T stop reply packet\n"
3193 " threads \tAll of the above\n");
3197 #undef require_running
3198 #define require_running(BUF) \
3199 if (!target_running ()) \
3206 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
3208 int pid
= * (int *) args
;
3210 if (ptid_get_pid (entry
->id
) == pid
)
3217 kill_inferior_callback (struct inferior_list_entry
*entry
)
3219 struct process_info
*process
= (struct process_info
*) entry
;
3220 int pid
= ptid_get_pid (process
->entry
.id
);
3222 kill_inferior (pid
);
3223 discard_queued_stop_replies (pid_to_ptid (pid
));
3226 /* Callback for for_each_inferior to detach or kill the inferior,
3227 depending on whether we attached to it or not.
3228 We inform the user whether we're detaching or killing the process
3229 as this is only called when gdbserver is about to exit. */
3232 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
3234 struct process_info
*process
= (struct process_info
*) entry
;
3235 int pid
= ptid_get_pid (process
->entry
.id
);
3237 if (process
->attached
)
3238 detach_inferior (pid
);
3240 kill_inferior (pid
);
3242 discard_queued_stop_replies (pid_to_ptid (pid
));
3245 /* for_each_inferior callback for detach_or_kill_for_exit to print
3246 the pids of started inferiors. */
3249 print_started_pid (struct inferior_list_entry
*entry
)
3251 struct process_info
*process
= (struct process_info
*) entry
;
3253 if (! process
->attached
)
3255 int pid
= ptid_get_pid (process
->entry
.id
);
3256 fprintf (stderr
, " %d", pid
);
3260 /* for_each_inferior callback for detach_or_kill_for_exit to print
3261 the pids of attached inferiors. */
3264 print_attached_pid (struct inferior_list_entry
*entry
)
3266 struct process_info
*process
= (struct process_info
*) entry
;
3268 if (process
->attached
)
3270 int pid
= ptid_get_pid (process
->entry
.id
);
3271 fprintf (stderr
, " %d", pid
);
3275 /* Call this when exiting gdbserver with possible inferiors that need
3276 to be killed or detached from. */
3279 detach_or_kill_for_exit (void)
3281 /* First print a list of the inferiors we will be killing/detaching.
3282 This is to assist the user, for example, in case the inferior unexpectedly
3283 dies after we exit: did we screw up or did the inferior exit on its own?
3284 Having this info will save some head-scratching. */
3286 if (have_started_inferiors_p ())
3288 fprintf (stderr
, "Killing process(es):");
3289 for_each_inferior (&all_processes
, print_started_pid
);
3290 fprintf (stderr
, "\n");
3292 if (have_attached_inferiors_p ())
3294 fprintf (stderr
, "Detaching process(es):");
3295 for_each_inferior (&all_processes
, print_attached_pid
);
3296 fprintf (stderr
, "\n");
3299 /* Now we can kill or detach the inferiors. */
3301 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
3304 /* Value that will be passed to exit(3) when gdbserver exits. */
3305 static int exit_code
;
3307 /* Cleanup version of detach_or_kill_for_exit. */
3310 detach_or_kill_for_exit_cleanup (void *ignore
)
3315 detach_or_kill_for_exit ();
3318 CATCH (exception
, RETURN_MASK_ALL
)
3321 fprintf (stderr
, "Detach or kill failed: %s\n", exception
.message
);
3327 /* Main function. This is called by the real "main" function,
3328 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
3330 static void ATTRIBUTE_NORETURN
3331 captured_main (int argc
, char *argv
[])
3335 char *arg_end
, *port
;
3336 char **next_arg
= &argv
[1];
3337 volatile int multi_mode
= 0;
3338 volatile int attach
= 0;
3341 while (*next_arg
!= NULL
&& **next_arg
== '-')
3343 if (strcmp (*next_arg
, "--version") == 0)
3345 gdbserver_version ();
3348 else if (strcmp (*next_arg
, "--help") == 0)
3350 gdbserver_usage (stdout
);
3353 else if (strcmp (*next_arg
, "--attach") == 0)
3355 else if (strcmp (*next_arg
, "--multi") == 0)
3357 else if (strcmp (*next_arg
, "--wrapper") == 0)
3361 wrapper_argv
= next_arg
;
3362 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
3365 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
3367 gdbserver_usage (stderr
);
3371 /* Consume the "--". */
3374 else if (strcmp (*next_arg
, "--debug") == 0)
3376 else if (startswith (*next_arg
, "--debug-format="))
3379 = parse_debug_format_options ((*next_arg
)
3380 + sizeof ("--debug-format=") - 1, 0);
3382 if (error_msg
!= NULL
)
3384 fprintf (stderr
, "%s", error_msg
);
3388 else if (strcmp (*next_arg
, "--remote-debug") == 0)
3390 else if (strcmp (*next_arg
, "--disable-packet") == 0)
3392 gdbserver_show_disableable (stdout
);
3395 else if (startswith (*next_arg
, "--disable-packet="))
3397 char *packets
, *tok
;
3399 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
3400 for (tok
= strtok (packets
, ",");
3402 tok
= strtok (NULL
, ","))
3404 if (strcmp ("vCont", tok
) == 0)
3405 disable_packet_vCont
= 1;
3406 else if (strcmp ("Tthread", tok
) == 0)
3407 disable_packet_Tthread
= 1;
3408 else if (strcmp ("qC", tok
) == 0)
3409 disable_packet_qC
= 1;
3410 else if (strcmp ("qfThreadInfo", tok
) == 0)
3411 disable_packet_qfThreadInfo
= 1;
3412 else if (strcmp ("threads", tok
) == 0)
3414 disable_packet_vCont
= 1;
3415 disable_packet_Tthread
= 1;
3416 disable_packet_qC
= 1;
3417 disable_packet_qfThreadInfo
= 1;
3421 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
3423 gdbserver_show_disableable (stderr
);
3428 else if (strcmp (*next_arg
, "-") == 0)
3430 /* "-" specifies a stdio connection and is a form of port
3432 *next_arg
= STDIO_CONNECTION_NAME
;
3435 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
3436 disable_randomization
= 1;
3437 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
3438 disable_randomization
= 0;
3439 else if (strcmp (*next_arg
, "--once") == 0)
3443 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
3453 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
3455 gdbserver_usage (stderr
);
3459 /* Remember stdio descriptors. LISTEN_DESC must not be listed, it will be
3460 opened by remote_prepare. */
3463 /* We need to know whether the remote connection is stdio before
3464 starting the inferior. Inferiors created in this scenario have
3465 stdin,stdout redirected. So do this here before we call
3467 remote_prepare (port
);
3472 /* --attach used to come after PORT, so allow it there for
3474 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
3481 && (*next_arg
== NULL
3482 || (*next_arg
)[0] == '\0'
3483 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
3485 || next_arg
[1] != NULL
))
3490 gdbserver_usage (stderr
);
3494 initialize_async_io ();
3496 initialize_event_loop ();
3497 if (target_supports_tracepoints ())
3498 initialize_tracepoint ();
3499 initialize_notif ();
3501 own_buf
= xmalloc (PBUFSIZ
+ 1);
3502 mem_buf
= xmalloc (PBUFSIZ
);
3504 if (pid
== 0 && *next_arg
!= NULL
)
3508 n
= argc
- (next_arg
- argv
);
3509 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
3510 for (i
= 0; i
< n
; i
++)
3511 program_argv
[i
] = xstrdup (next_arg
[i
]);
3512 program_argv
[i
] = NULL
;
3514 /* Wait till we are at first instruction in program. */
3515 start_inferior (program_argv
);
3517 /* We are now (hopefully) stopped at the first instruction of
3518 the target process. This assumes that the target process was
3519 successfully created. */
3523 if (attach_inferior (pid
) == -1)
3524 error ("Attaching not supported on this target");
3526 /* Otherwise succeeded. */
3530 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3531 last_status
.value
.integer
= 0;
3532 last_ptid
= minus_one_ptid
;
3534 make_cleanup (detach_or_kill_for_exit_cleanup
, NULL
);
3536 /* Don't report shared library events on the initial connection,
3537 even if some libraries are preloaded. Avoids the "stopped by
3538 shared library event" notice on gdb side. */
3541 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3542 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3547 if (!was_running
&& !multi_mode
)
3548 error ("No program to debug");
3555 report_fork_events
= 0;
3556 report_vfork_events
= 0;
3557 report_exec_events
= 0;
3558 /* Be sure we're out of tfind mode. */
3559 current_traceframe
= -1;
3560 cont_thread
= null_ptid
;
3561 swbreak_feature
= 0;
3562 hwbreak_feature
= 0;
3568 /* Wait for events. This will return when all event sources
3569 are removed from the event loop. */
3570 start_event_loop ();
3572 /* If an exit was requested (using the "monitor exit"
3573 command), terminate now. The only other way to get
3574 here is for getpkt to fail; close the connection
3575 and reopen it at the top of the loop. */
3577 if (exit_requested
|| run_once
)
3578 throw_quit ("Quit");
3581 "Remote side has terminated connection. "
3582 "GDBserver will reopen the connection.\n");
3584 /* Get rid of any pending statuses. An eventual reconnection
3585 (by the same GDB instance or another) will refresh all its
3586 state from scratch. */
3587 discard_queued_stop_replies (minus_one_ptid
);
3588 for_each_inferior (&all_threads
,
3589 clear_pending_status_callback
);
3593 if (disconnected_tracing
)
3595 /* Try to enable non-stop/async mode, so we we can
3596 both wait for an async socket accept, and handle
3597 async target events simultaneously. There's also
3598 no point either in having the target always stop
3599 all threads, when we're going to pass signals
3600 down without informing GDB. */
3603 if (start_non_stop (1))
3606 /* Detaching implicitly resumes all threads;
3607 simply disconnecting does not. */
3613 "Disconnected tracing disabled; "
3614 "stopping trace run.\n");
3619 CATCH (exception
, RETURN_MASK_ERROR
)
3622 fprintf (stderr
, "gdbserver: %s\n", exception
.message
);
3624 if (response_needed
)
3626 write_enn (own_buf
);
3631 throw_quit ("Quit");
3637 /* Main function. */
3640 main (int argc
, char *argv
[])
3645 captured_main (argc
, argv
);
3647 CATCH (exception
, RETURN_MASK_ALL
)
3649 if (exception
.reason
== RETURN_ERROR
)
3652 fprintf (stderr
, "%s\n", exception
.message
);
3653 fprintf (stderr
, "Exiting\n");
3661 gdb_assert_not_reached ("captured_main should never return");
3664 /* Skip PACKET until the next semi-colon (or end of string). */
3667 skip_to_semicolon (char **packet
)
3669 while (**packet
!= '\0' && **packet
!= ';')
3673 /* Process options coming from Z packets for a breakpoint. PACKET is
3674 the packet buffer. *PACKET is updated to point to the first char
3675 after the last processed option. */
3678 process_point_options (struct breakpoint
*bp
, char **packet
)
3680 char *dataptr
= *packet
;
3683 /* Check if data has the correct format. */
3684 if (*dataptr
!= ';')
3691 if (*dataptr
== ';')
3694 if (*dataptr
== 'X')
3696 /* Conditional expression. */
3698 debug_printf ("Found breakpoint condition.\n");
3699 if (!add_breakpoint_condition (bp
, &dataptr
))
3700 skip_to_semicolon (&dataptr
);
3702 else if (startswith (dataptr
, "cmds:"))
3704 dataptr
+= strlen ("cmds:");
3706 debug_printf ("Found breakpoint commands %s.\n", dataptr
);
3707 persist
= (*dataptr
== '1');
3709 if (add_breakpoint_commands (bp
, &dataptr
, persist
))
3710 skip_to_semicolon (&dataptr
);
3714 fprintf (stderr
, "Unknown token %c, ignoring.\n",
3716 /* Skip tokens until we find one that we recognize. */
3717 skip_to_semicolon (&dataptr
);
3723 /* Event loop callback that handles a serial event. The first byte in
3724 the serial buffer gets us here. We expect characters to arrive at
3725 a brisk pace, so we read the rest of the packet with a blocking
3729 process_serial_event (void)
3740 int new_packet_len
= -1;
3742 /* Used to decide when gdbserver should exit in
3743 multi-mode/remote. */
3744 static int have_ran
= 0;
3747 have_ran
= target_running ();
3749 disable_async_io ();
3751 response_needed
= 0;
3752 packet_len
= getpkt (own_buf
);
3753 if (packet_len
<= 0)
3756 /* Force an event loop break. */
3759 response_needed
= 1;
3766 handle_query (own_buf
, packet_len
, &new_packet_len
);
3769 handle_general_set (own_buf
);
3772 require_running (own_buf
);
3777 pid
= strtol (&own_buf
[i
], NULL
, 16);
3780 pid
= ptid_get_pid (current_ptid
);
3782 if ((tracing
&& disconnected_tracing
) || any_persistent_commands ())
3784 struct thread_resume resume_info
;
3785 struct process_info
*process
= find_process_pid (pid
);
3787 if (process
== NULL
)
3789 write_enn (own_buf
);
3793 if (tracing
&& disconnected_tracing
)
3795 "Disconnected tracing in effect, "
3796 "leaving gdbserver attached to the process\n");
3798 if (any_persistent_commands ())
3800 "Persistent commands are present, "
3801 "leaving gdbserver attached to the process\n");
3803 /* Make sure we're in non-stop/async mode, so we we can both
3804 wait for an async socket accept, and handle async target
3805 events simultaneously. There's also no point either in
3806 having the target stop all threads, when we're going to
3807 pass signals down without informing GDB. */
3811 debug_printf ("Forcing non-stop mode\n");
3817 process
->gdb_detached
= 1;
3819 /* Detaching implicitly resumes all threads. */
3820 resume_info
.thread
= minus_one_ptid
;
3821 resume_info
.kind
= resume_continue
;
3822 resume_info
.sig
= 0;
3823 (*the_target
->resume
) (&resume_info
, 1);
3826 break; /* from switch/case */
3829 fprintf (stderr
, "Detaching from process %d\n", pid
);
3831 if (detach_inferior (pid
) != 0)
3832 write_enn (own_buf
);
3835 discard_queued_stop_replies (pid_to_ptid (pid
));
3838 if (extended_protocol
)
3840 /* Treat this like a normal program exit. */
3841 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3842 last_status
.value
.integer
= 0;
3843 last_ptid
= pid_to_ptid (pid
);
3845 current_thread
= NULL
;
3852 /* If we are attached, then we can exit. Otherwise, we
3853 need to hang around doing nothing, until the child is
3855 join_inferior (pid
);
3861 extended_protocol
= 1;
3865 handle_status (own_buf
);
3868 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
3870 ptid_t gdb_id
, thread_id
;
3873 require_running (own_buf
);
3875 gdb_id
= read_ptid (&own_buf
[2], NULL
);
3877 pid
= ptid_get_pid (gdb_id
);
3879 if (ptid_equal (gdb_id
, null_ptid
)
3880 || ptid_equal (gdb_id
, minus_one_ptid
))
3881 thread_id
= null_ptid
;
3883 && ptid_equal (pid_to_ptid (pid
),
3886 struct thread_info
*thread
=
3887 (struct thread_info
*) find_inferior (&all_threads
,
3892 write_enn (own_buf
);
3896 thread_id
= thread
->entry
.id
;
3900 thread_id
= gdb_id_to_thread_id (gdb_id
);
3901 if (ptid_equal (thread_id
, null_ptid
))
3903 write_enn (own_buf
);
3908 if (own_buf
[1] == 'g')
3910 if (ptid_equal (thread_id
, null_ptid
))
3912 /* GDB is telling us to choose any thread. Check if
3913 the currently selected thread is still valid. If
3914 it is not, select the first available. */
3915 struct thread_info
*thread
=
3916 (struct thread_info
*) find_inferior_id (&all_threads
,
3919 thread
= get_first_thread ();
3920 thread_id
= thread
->entry
.id
;
3923 general_thread
= thread_id
;
3924 set_desired_thread (1);
3925 gdb_assert (current_thread
!= NULL
);
3927 else if (own_buf
[1] == 'c')
3928 cont_thread
= thread_id
;
3934 /* Silently ignore it so that gdb can extend the protocol
3935 without compatibility headaches. */
3940 require_running (own_buf
);
3941 if (current_traceframe
>= 0)
3943 struct regcache
*regcache
3944 = new_register_cache (current_target_desc ());
3946 if (fetch_traceframe_registers (current_traceframe
,
3948 registers_to_string (regcache
, own_buf
);
3950 write_enn (own_buf
);
3951 free_register_cache (regcache
);
3955 struct regcache
*regcache
;
3957 if (!set_desired_thread (1))
3958 write_enn (own_buf
);
3961 regcache
= get_thread_regcache (current_thread
, 1);
3962 registers_to_string (regcache
, own_buf
);
3967 require_running (own_buf
);
3968 if (current_traceframe
>= 0)
3969 write_enn (own_buf
);
3972 struct regcache
*regcache
;
3974 if (!set_desired_thread (1))
3975 write_enn (own_buf
);
3978 regcache
= get_thread_regcache (current_thread
, 1);
3979 registers_from_string (regcache
, &own_buf
[1]);
3985 require_running (own_buf
);
3986 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
3987 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
3989 write_enn (own_buf
);
3991 bin2hex (mem_buf
, own_buf
, res
);
3994 require_running (own_buf
);
3995 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
3996 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
3999 write_enn (own_buf
);
4002 require_running (own_buf
);
4003 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
4004 &mem_addr
, &len
, &mem_buf
) < 0
4005 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
4006 write_enn (own_buf
);
4011 require_running (own_buf
);
4012 hex2bin (own_buf
+ 1, &sig
, 1);
4013 if (gdb_signal_to_host_p (sig
))
4014 signal
= gdb_signal_to_host (sig
);
4017 myresume (own_buf
, 0, signal
);
4020 require_running (own_buf
);
4021 hex2bin (own_buf
+ 1, &sig
, 1);
4022 if (gdb_signal_to_host_p (sig
))
4023 signal
= gdb_signal_to_host (sig
);
4026 myresume (own_buf
, 1, signal
);
4029 require_running (own_buf
);
4031 myresume (own_buf
, 0, signal
);
4034 require_running (own_buf
);
4036 myresume (own_buf
, 1, signal
);
4038 case 'Z': /* insert_ ... */
4040 case 'z': /* remove_ ... */
4045 char type
= own_buf
[1];
4047 const int insert
= ch
== 'Z';
4048 char *p
= &own_buf
[3];
4050 p
= unpack_varlen_hex (p
, &addr
);
4051 len
= strtol (p
+ 1, &dataptr
, 16);
4055 struct breakpoint
*bp
;
4057 bp
= set_gdb_breakpoint (type
, addr
, len
, &res
);
4062 /* GDB may have sent us a list of *point parameters to
4063 be evaluated on the target's side. Read such list
4064 here. If we already have a list of parameters, GDB
4065 is telling us to drop that list and use this one
4067 clear_breakpoint_conditions_and_commands (bp
);
4068 process_point_options (bp
, &dataptr
);
4072 res
= delete_gdb_breakpoint (type
, addr
, len
);
4080 write_enn (own_buf
);
4084 response_needed
= 0;
4085 if (!target_running ())
4086 /* The packet we received doesn't make sense - but we can't
4087 reply to it, either. */
4090 fprintf (stderr
, "Killing all inferiors\n");
4091 for_each_inferior (&all_processes
, kill_inferior_callback
);
4093 /* When using the extended protocol, we wait with no program
4094 running. The traditional protocol will exit instead. */
4095 if (extended_protocol
)
4097 last_status
.kind
= TARGET_WAITKIND_EXITED
;
4098 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
4106 ptid_t gdb_id
, thread_id
;
4108 require_running (own_buf
);
4110 gdb_id
= read_ptid (&own_buf
[1], NULL
);
4111 thread_id
= gdb_id_to_thread_id (gdb_id
);
4112 if (ptid_equal (thread_id
, null_ptid
))
4114 write_enn (own_buf
);
4118 if (mythread_alive (thread_id
))
4121 write_enn (own_buf
);
4125 response_needed
= 0;
4127 /* Restarting the inferior is only supported in the extended
4129 if (extended_protocol
)
4131 if (target_running ())
4132 for_each_inferior (&all_processes
,
4133 kill_inferior_callback
);
4134 fprintf (stderr
, "GDBserver restarting\n");
4136 /* Wait till we are at 1st instruction in prog. */
4137 if (program_argv
!= NULL
)
4139 start_inferior (program_argv
);
4140 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
4142 /* Stopped at the first instruction of the target
4144 general_thread
= last_ptid
;
4148 /* Something went wrong. */
4149 general_thread
= null_ptid
;
4154 last_status
.kind
= TARGET_WAITKIND_EXITED
;
4155 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
4161 /* It is a request we don't understand. Respond with an
4162 empty packet so that gdb knows that we don't support this
4168 /* Extended (long) request. */
4169 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
4173 /* It is a request we don't understand. Respond with an empty
4174 packet so that gdb knows that we don't support this
4180 if (new_packet_len
!= -1)
4181 putpkt_binary (own_buf
, new_packet_len
);
4185 response_needed
= 0;
4187 if (!extended_protocol
&& have_ran
&& !target_running ())
4189 /* In non-stop, defer exiting until GDB had a chance to query
4190 the whole vStopped list (until it gets an OK). */
4191 if (QUEUE_is_empty (notif_event_p
, notif_stop
.queue
))
4193 /* Be transparent when GDB is connected through stdio -- no
4194 need to spam GDB's console. */
4195 if (!remote_connection_is_stdio ())
4196 fprintf (stderr
, "GDBserver exiting\n");
4208 /* Event-loop callback for serial events. */
4211 handle_serial_event (int err
, gdb_client_data client_data
)
4214 debug_printf ("handling possible serial event\n");
4216 /* Really handle it. */
4217 if (process_serial_event () < 0)
4220 /* Be sure to not change the selected thread behind GDB's back.
4221 Important in the non-stop mode asynchronous protocol. */
4222 set_desired_thread (1);
4227 /* Event-loop callback for target events. */
4230 handle_target_event (int err
, gdb_client_data client_data
)
4233 debug_printf ("handling possible target event\n");
4235 last_ptid
= mywait (minus_one_ptid
, &last_status
,
4238 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
)
4240 /* No RSP support for this yet. */
4242 else if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
4244 int pid
= ptid_get_pid (last_ptid
);
4245 struct process_info
*process
= find_process_pid (pid
);
4246 int forward_event
= !gdb_connected () || process
->gdb_detached
;
4248 if (last_status
.kind
== TARGET_WAITKIND_EXITED
4249 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
4251 mark_breakpoints_out (process
);
4252 mourn_inferior (process
);
4256 /* We're reporting this thread as stopped. Update its
4257 "want-stopped" state to what the client wants, until it
4258 gets a new resume action. */
4259 current_thread
->last_resume_kind
= resume_stop
;
4260 current_thread
->last_status
= last_status
;
4265 if (!target_running ())
4267 /* The last process exited. We're done. */
4271 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
4273 /* A thread stopped with a signal, but gdb isn't
4274 connected to handle it. Pass it down to the
4275 inferior, as if it wasn't being traced. */
4276 struct thread_resume resume_info
;
4279 debug_printf ("GDB not connected; forwarding event %d for"
4281 (int) last_status
.kind
,
4282 target_pid_to_str (last_ptid
));
4284 resume_info
.thread
= last_ptid
;
4285 resume_info
.kind
= resume_continue
;
4286 resume_info
.sig
= gdb_signal_to_host (last_status
.value
.sig
);
4287 (*the_target
->resume
) (&resume_info
, 1);
4289 else if (debug_threads
)
4290 debug_printf ("GDB not connected; ignoring event %d for [%s]\n",
4291 (int) last_status
.kind
,
4292 target_pid_to_str (last_ptid
));
4296 struct vstop_notif
*vstop_notif
= XNEW (struct vstop_notif
);
4298 vstop_notif
->status
= last_status
;
4299 vstop_notif
->ptid
= last_ptid
;
4300 /* Push Stop notification. */
4301 notif_push (¬if_stop
,
4302 (struct notif_event
*) vstop_notif
);
4306 /* Be sure to not change the selected thread behind GDB's back.
4307 Important in the non-stop mode asynchronous protocol. */
4308 set_desired_thread (1);