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
;
66 /* Whether we should attempt to disable the operating system's address
67 space randomization feature before starting an inferior. */
68 int disable_randomization
= 1;
70 static char **program_argv
, **wrapper_argv
;
72 int pass_signals
[GDB_SIGNAL_LAST
];
73 int program_signals
[GDB_SIGNAL_LAST
];
74 int program_signals_p
;
76 /* The PID of the originally created or attached inferior. Used to
77 send signals to the process when GDB sends us an asynchronous interrupt
78 (user hitting Control-C in the client), and to wait for the child to exit
79 when no longer debugging it. */
81 unsigned long signal_pid
;
84 /* A file descriptor for the controlling terminal. */
87 /* TERMINAL_FD's original foreground group. */
88 pid_t old_foreground_pgrp
;
90 /* Hand back terminal ownership to the original foreground group. */
93 restore_old_foreground_pgrp (void)
95 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
99 /* Set if you want to disable optional thread related packets support
100 in gdbserver, for the sake of testing GDB against stubs that don't
102 int disable_packet_vCont
;
103 int disable_packet_Tthread
;
104 int disable_packet_qC
;
105 int disable_packet_qfThreadInfo
;
107 /* Last status reported to GDB. */
108 static struct target_waitstatus last_status
;
109 static ptid_t last_ptid
;
111 static char *own_buf
;
112 static unsigned char *mem_buf
;
114 /* A sub-class of 'struct notif_event' for stop, holding information
115 relative to a single stop reply. We keep a queue of these to
116 push to GDB in non-stop mode. */
120 struct notif_event base
;
122 /* Thread or process that got the event. */
126 struct target_waitstatus status
;
129 /* The current btrace configuration. This is gdbserver's mirror of GDB's
130 btrace configuration. */
131 static struct btrace_config current_btrace_conf
;
133 DEFINE_QUEUE_P (notif_event_p
);
135 /* Put a stop reply to the stop reply queue. */
138 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
140 struct vstop_notif
*new_notif
= xmalloc (sizeof (*new_notif
));
142 new_notif
->ptid
= ptid
;
143 new_notif
->status
= *status
;
145 notif_event_enque (¬if_stop
, (struct notif_event
*) new_notif
);
149 remove_all_on_match_pid (QUEUE (notif_event_p
) *q
,
150 QUEUE_ITER (notif_event_p
) *iter
,
151 struct notif_event
*event
,
157 || ptid_get_pid (((struct vstop_notif
*) event
)->ptid
) == *pid
)
159 if (q
->free_func
!= NULL
)
160 q
->free_func (event
);
162 QUEUE_remove_elem (notif_event_p
, q
, iter
);
168 /* Get rid of the currently pending stop replies for PID. If PID is
169 -1, then apply to all processes. */
172 discard_queued_stop_replies (int pid
)
174 QUEUE_iterate (notif_event_p
, notif_stop
.queue
,
175 remove_all_on_match_pid
, &pid
);
179 vstop_notif_reply (struct notif_event
*event
, char *own_buf
)
181 struct vstop_notif
*vstop
= (struct vstop_notif
*) event
;
183 prepare_resume_reply (own_buf
, vstop
->ptid
, &vstop
->status
);
186 struct notif_server notif_stop
=
188 "vStopped", "Stop", NULL
, vstop_notif_reply
,
192 target_running (void)
194 return get_first_thread () != NULL
;
198 start_inferior (char **argv
)
200 char **new_argv
= argv
;
202 if (wrapper_argv
!= NULL
)
206 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
208 for (i
= 0; argv
[i
] != NULL
; i
++)
210 new_argv
= alloca (sizeof (char *) * count
);
212 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
213 new_argv
[count
++] = wrapper_argv
[i
];
214 for (i
= 0; argv
[i
] != NULL
; i
++)
215 new_argv
[count
++] = argv
[i
];
216 new_argv
[count
] = NULL
;
222 for (i
= 0; new_argv
[i
]; ++i
)
223 debug_printf ("new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
228 signal (SIGTTOU
, SIG_DFL
);
229 signal (SIGTTIN
, SIG_DFL
);
232 signal_pid
= create_inferior (new_argv
[0], new_argv
);
234 /* FIXME: we don't actually know at this point that the create
235 actually succeeded. We won't know that until we wait. */
236 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
241 signal (SIGTTOU
, SIG_IGN
);
242 signal (SIGTTIN
, SIG_IGN
);
243 terminal_fd
= fileno (stderr
);
244 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
245 tcsetpgrp (terminal_fd
, signal_pid
);
246 atexit (restore_old_foreground_pgrp
);
249 if (wrapper_argv
!= NULL
)
251 struct thread_resume resume_info
;
253 memset (&resume_info
, 0, sizeof (resume_info
));
254 resume_info
.thread
= pid_to_ptid (signal_pid
);
255 resume_info
.kind
= resume_continue
;
258 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
260 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
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 res
= read_inferior_memory (memaddr
, myaddr
, len
);
820 done_accessing_memory ();
822 return res
== 0 ? len
: -1;
828 /* Write trace frame or inferior memory. Actually, writing to trace
829 frames is forbidden. */
832 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
834 if (current_traceframe
>= 0)
840 ret
= prepare_to_access_memory ();
843 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
844 done_accessing_memory ();
850 /* Subroutine of handle_search_memory to simplify it. */
853 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
854 gdb_byte
*pattern
, unsigned pattern_len
,
855 gdb_byte
*search_buf
,
856 unsigned chunk_size
, unsigned search_buf_size
,
857 CORE_ADDR
*found_addrp
)
859 /* Prime the search buffer. */
861 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
864 warning ("Unable to access %ld bytes of target "
865 "memory at 0x%lx, halting search.",
866 (long) search_buf_size
, (long) start_addr
);
870 /* Perform the search.
872 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
873 When we've scanned N bytes we copy the trailing bytes to the start and
874 read in another N bytes. */
876 while (search_space_len
>= pattern_len
)
879 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
883 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
885 if (found_ptr
!= NULL
)
887 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
888 *found_addrp
= found_addr
;
892 /* Not found in this chunk, skip to next chunk. */
894 /* Don't let search_space_len wrap here, it's unsigned. */
895 if (search_space_len
>= chunk_size
)
896 search_space_len
-= chunk_size
;
898 search_space_len
= 0;
900 if (search_space_len
>= pattern_len
)
902 unsigned keep_len
= search_buf_size
- chunk_size
;
903 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
906 /* Copy the trailing part of the previous iteration to the front
907 of the buffer for the next iteration. */
908 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
910 nr_to_read
= (search_space_len
- keep_len
< chunk_size
911 ? search_space_len
- keep_len
914 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
915 nr_to_read
) != search_buf_size
)
917 warning ("Unable to access %ld bytes of target memory "
918 "at 0x%lx, halting search.",
919 (long) nr_to_read
, (long) read_addr
);
923 start_addr
+= chunk_size
;
932 /* Handle qSearch:memory packets. */
935 handle_search_memory (char *own_buf
, int packet_len
)
937 CORE_ADDR start_addr
;
938 CORE_ADDR search_space_len
;
940 unsigned int pattern_len
;
941 /* NOTE: also defined in find.c testcase. */
942 #define SEARCH_CHUNK_SIZE 16000
943 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
944 /* Buffer to hold memory contents for searching. */
945 gdb_byte
*search_buf
;
946 unsigned search_buf_size
;
948 CORE_ADDR found_addr
;
949 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
951 pattern
= malloc (packet_len
);
954 error ("Unable to allocate memory to perform the search");
955 strcpy (own_buf
, "E00");
958 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
959 packet_len
- cmd_name_len
,
960 &start_addr
, &search_space_len
,
961 pattern
, &pattern_len
) < 0)
964 error ("Error in parsing qSearch:memory packet");
965 strcpy (own_buf
, "E00");
969 search_buf_size
= chunk_size
+ pattern_len
- 1;
971 /* No point in trying to allocate a buffer larger than the search space. */
972 if (search_space_len
< search_buf_size
)
973 search_buf_size
= search_space_len
;
975 search_buf
= malloc (search_buf_size
);
976 if (search_buf
== NULL
)
979 error ("Unable to allocate memory to perform the search");
980 strcpy (own_buf
, "E00");
984 found
= handle_search_memory_1 (start_addr
, search_space_len
,
985 pattern
, pattern_len
,
986 search_buf
, chunk_size
, search_buf_size
,
990 sprintf (own_buf
, "1,%lx", (long) found_addr
);
992 strcpy (own_buf
, "0");
994 strcpy (own_buf
, "E00");
1000 #define require_running(BUF) \
1001 if (!target_running ()) \
1007 /* Parse options to --debug-format= and "monitor set debug-format".
1008 ARG is the text after "--debug-format=" or "monitor set debug-format".
1009 IS_MONITOR is non-zero if we're invoked via "monitor set debug-format".
1010 This triggers calls to monitor_output.
1011 The result is NULL if all options were parsed ok, otherwise an error
1012 message which the caller must free.
1014 N.B. These commands affect all debug format settings, they are not
1015 cumulative. If a format is not specified, it is turned off.
1016 However, we don't go to extra trouble with things like
1017 "monitor set debug-format all,none,timestamp".
1018 Instead we just parse them one at a time, in order.
1020 The syntax for "monitor set debug" we support here is not identical
1021 to gdb's "set debug foo on|off" because we also use this function to
1022 parse "--debug-format=foo,bar". */
1025 parse_debug_format_options (const char *arg
, int is_monitor
)
1027 VEC (char_ptr
) *options
;
1031 /* First turn all debug format options off. */
1032 debug_timestamp
= 0;
1034 /* First remove leading spaces, for "monitor set debug-format". */
1035 while (isspace (*arg
))
1038 options
= delim_string_to_char_ptr_vec (arg
, ',');
1040 for (ix
= 0; VEC_iterate (char_ptr
, options
, ix
, option
); ++ix
)
1042 if (strcmp (option
, "all") == 0)
1044 debug_timestamp
= 1;
1046 monitor_output ("All extra debug format options enabled.\n");
1048 else if (strcmp (option
, "none") == 0)
1050 debug_timestamp
= 0;
1052 monitor_output ("All extra debug format options disabled.\n");
1054 else if (strcmp (option
, "timestamp") == 0)
1056 debug_timestamp
= 1;
1058 monitor_output ("Timestamps will be added to debug output.\n");
1060 else if (*option
== '\0')
1062 /* An empty option, e.g., "--debug-format=foo,,bar", is ignored. */
1067 char *msg
= xstrprintf ("Unknown debug-format argument: \"%s\"\n",
1070 free_char_ptr_vec (options
);
1075 free_char_ptr_vec (options
);
1079 /* Handle monitor commands not handled by target-specific handlers. */
1082 handle_monitor_command (char *mon
, char *own_buf
)
1084 if (strcmp (mon
, "set debug 1") == 0)
1087 monitor_output ("Debug output enabled.\n");
1089 else if (strcmp (mon
, "set debug 0") == 0)
1092 monitor_output ("Debug output disabled.\n");
1094 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
1096 show_debug_regs
= 1;
1097 monitor_output ("H/W point debugging output enabled.\n");
1099 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
1101 show_debug_regs
= 0;
1102 monitor_output ("H/W point debugging output disabled.\n");
1104 else if (strcmp (mon
, "set remote-debug 1") == 0)
1107 monitor_output ("Protocol debug output enabled.\n");
1109 else if (strcmp (mon
, "set remote-debug 0") == 0)
1112 monitor_output ("Protocol debug output disabled.\n");
1114 else if (startswith (mon
, "set debug-format "))
1117 = parse_debug_format_options (mon
+ sizeof ("set debug-format ") - 1,
1120 if (error_msg
!= NULL
)
1122 monitor_output (error_msg
);
1123 monitor_show_help ();
1124 write_enn (own_buf
);
1128 else if (strcmp (mon
, "help") == 0)
1129 monitor_show_help ();
1130 else if (strcmp (mon
, "exit") == 0)
1134 monitor_output ("Unknown monitor command.\n\n");
1135 monitor_show_help ();
1136 write_enn (own_buf
);
1140 /* Associates a callback with each supported qXfer'able object. */
1144 /* The object this handler handles. */
1147 /* Request that the target transfer up to LEN 8-bit bytes of the
1148 target's OBJECT. The OFFSET, for a seekable object, specifies
1149 the starting point. The ANNEX can be used to provide additional
1150 data-specific information to the target.
1152 Return the number of bytes actually transfered, zero when no
1153 further transfer is possible, -1 on error, -2 when the transfer
1154 is not supported, and -3 on a verbose error message that should
1155 be preserved. Return of a positive value smaller than LEN does
1156 not indicate the end of the object, only the end of the transfer.
1158 One, and only one, of readbuf or writebuf must be non-NULL. */
1159 int (*xfer
) (const char *annex
,
1160 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1161 ULONGEST offset
, LONGEST len
);
1164 /* Handle qXfer:auxv:read. */
1167 handle_qxfer_auxv (const char *annex
,
1168 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1169 ULONGEST offset
, LONGEST len
)
1171 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
1174 if (annex
[0] != '\0' || !target_running ())
1177 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
1180 /* Handle qXfer:exec-file:read. */
1183 handle_qxfer_exec_file (const char *const_annex
,
1184 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1185 ULONGEST offset
, LONGEST len
)
1191 if (the_target
->pid_to_exec_file
== NULL
|| writebuf
!= NULL
)
1194 if (const_annex
[0] == '\0')
1196 if (current_thread
== NULL
)
1199 pid
= pid_of (current_thread
);
1203 char *annex
= alloca (strlen (const_annex
) + 1);
1205 strcpy (annex
, const_annex
);
1206 annex
= unpack_varlen_hex (annex
, &pid
);
1208 if (annex
[0] != '\0')
1215 file
= (*the_target
->pid_to_exec_file
) (pid
);
1219 total_len
= strlen (file
);
1221 if (offset
> total_len
)
1224 if (offset
+ len
> total_len
)
1225 len
= total_len
- offset
;
1227 memcpy (readbuf
, file
+ offset
, len
);
1231 /* Handle qXfer:features:read. */
1234 handle_qxfer_features (const char *annex
,
1235 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1236 ULONGEST offset
, LONGEST len
)
1238 const char *document
;
1241 if (writebuf
!= NULL
)
1244 if (!target_running ())
1247 /* Grab the correct annex. */
1248 document
= get_features_xml (annex
);
1249 if (document
== NULL
)
1252 total_len
= strlen (document
);
1254 if (offset
> total_len
)
1257 if (offset
+ len
> total_len
)
1258 len
= total_len
- offset
;
1260 memcpy (readbuf
, document
+ offset
, len
);
1264 /* Worker routine for handle_qxfer_libraries.
1265 Add to the length pointed to by ARG a conservative estimate of the
1266 length needed to transmit the file name of INF. */
1269 accumulate_file_name_length (struct inferior_list_entry
*inf
, void *arg
)
1271 struct dll_info
*dll
= (struct dll_info
*) inf
;
1272 unsigned int *total_len
= arg
;
1274 /* Over-estimate the necessary memory. Assume that every character
1275 in the library name must be escaped. */
1276 *total_len
+= 128 + 6 * strlen (dll
->name
);
1279 /* Worker routine for handle_qxfer_libraries.
1280 Emit the XML to describe the library in INF. */
1283 emit_dll_description (struct inferior_list_entry
*inf
, void *arg
)
1285 struct dll_info
*dll
= (struct dll_info
*) inf
;
1290 strcpy (p
, " <library name=\"");
1292 name
= xml_escape_text (dll
->name
);
1296 strcpy (p
, "\"><segment address=\"");
1298 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1300 strcpy (p
, "\"/></library>\n");
1306 /* Handle qXfer:libraries:read. */
1309 handle_qxfer_libraries (const char *annex
,
1310 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1311 ULONGEST offset
, LONGEST len
)
1313 unsigned int total_len
;
1316 if (writebuf
!= NULL
)
1319 if (annex
[0] != '\0' || !target_running ())
1323 for_each_inferior_with_data (&all_dlls
, accumulate_file_name_length
,
1326 document
= malloc (total_len
);
1327 if (document
== NULL
)
1330 strcpy (document
, "<library-list version=\"1.0\">\n");
1331 p
= document
+ strlen (document
);
1333 for_each_inferior_with_data (&all_dlls
, emit_dll_description
, &p
);
1335 strcpy (p
, "</library-list>\n");
1337 total_len
= strlen (document
);
1339 if (offset
> total_len
)
1345 if (offset
+ len
> total_len
)
1346 len
= total_len
- offset
;
1348 memcpy (readbuf
, document
+ offset
, len
);
1353 /* Handle qXfer:libraries-svr4:read. */
1356 handle_qxfer_libraries_svr4 (const char *annex
,
1357 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1358 ULONGEST offset
, LONGEST len
)
1360 if (writebuf
!= NULL
)
1363 if (!target_running () || the_target
->qxfer_libraries_svr4
== NULL
)
1366 return the_target
->qxfer_libraries_svr4 (annex
, readbuf
, writebuf
, offset
, len
);
1369 /* Handle qXfer:osadata:read. */
1372 handle_qxfer_osdata (const char *annex
,
1373 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1374 ULONGEST offset
, LONGEST len
)
1376 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
1379 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
1382 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1385 handle_qxfer_siginfo (const char *annex
,
1386 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1387 ULONGEST offset
, LONGEST len
)
1389 if (the_target
->qxfer_siginfo
== NULL
)
1392 if (annex
[0] != '\0' || !target_running ())
1395 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1398 /* Handle qXfer:spu:read and qXfer:spu:write. */
1401 handle_qxfer_spu (const char *annex
,
1402 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1403 ULONGEST offset
, LONGEST len
)
1405 if (the_target
->qxfer_spu
== NULL
)
1408 if (!target_running ())
1411 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1414 /* Handle qXfer:statictrace:read. */
1417 handle_qxfer_statictrace (const char *annex
,
1418 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1419 ULONGEST offset
, LONGEST len
)
1423 if (writebuf
!= NULL
)
1426 if (annex
[0] != '\0' || !target_running () || current_traceframe
== -1)
1429 if (traceframe_read_sdata (current_traceframe
, offset
,
1430 readbuf
, len
, &nbytes
))
1435 /* Helper for handle_qxfer_threads_proper.
1436 Emit the XML to describe the thread of INF. */
1439 handle_qxfer_threads_worker (struct inferior_list_entry
*inf
, void *arg
)
1441 struct thread_info
*thread
= (struct thread_info
*) inf
;
1442 struct buffer
*buffer
= arg
;
1443 ptid_t ptid
= thread_to_gdb_id (thread
);
1445 int core
= target_core_of_thread (ptid
);
1448 write_ptid (ptid_s
, ptid
);
1452 sprintf (core_s
, "%d", core
);
1453 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1458 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1463 /* Helper for handle_qxfer_threads. */
1466 handle_qxfer_threads_proper (struct buffer
*buffer
)
1468 buffer_grow_str (buffer
, "<threads>\n");
1470 for_each_inferior_with_data (&all_threads
, handle_qxfer_threads_worker
,
1473 buffer_grow_str0 (buffer
, "</threads>\n");
1476 /* Handle qXfer:threads:read. */
1479 handle_qxfer_threads (const char *annex
,
1480 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1481 ULONGEST offset
, LONGEST len
)
1483 static char *result
= 0;
1484 static unsigned int result_length
= 0;
1486 if (writebuf
!= NULL
)
1489 if (!target_running () || annex
[0] != '\0')
1494 struct buffer buffer
;
1495 /* When asked for data at offset 0, generate everything and store into
1496 'result'. Successive reads will be served off 'result'. */
1500 buffer_init (&buffer
);
1502 handle_qxfer_threads_proper (&buffer
);
1504 result
= buffer_finish (&buffer
);
1505 result_length
= strlen (result
);
1506 buffer_free (&buffer
);
1509 if (offset
>= result_length
)
1511 /* We're out of data. */
1518 if (len
> result_length
- offset
)
1519 len
= result_length
- offset
;
1521 memcpy (readbuf
, result
+ offset
, len
);
1526 /* Handle qXfer:traceframe-info:read. */
1529 handle_qxfer_traceframe_info (const char *annex
,
1530 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1531 ULONGEST offset
, LONGEST len
)
1533 static char *result
= 0;
1534 static unsigned int result_length
= 0;
1536 if (writebuf
!= NULL
)
1539 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1544 struct buffer buffer
;
1546 /* When asked for data at offset 0, generate everything and
1547 store into 'result'. Successive reads will be served off
1551 buffer_init (&buffer
);
1553 traceframe_read_info (current_traceframe
, &buffer
);
1555 result
= buffer_finish (&buffer
);
1556 result_length
= strlen (result
);
1557 buffer_free (&buffer
);
1560 if (offset
>= result_length
)
1562 /* We're out of data. */
1569 if (len
> result_length
- offset
)
1570 len
= result_length
- offset
;
1572 memcpy (readbuf
, result
+ offset
, len
);
1576 /* Handle qXfer:fdpic:read. */
1579 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1580 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1582 if (the_target
->read_loadmap
== NULL
)
1585 if (!target_running ())
1588 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1591 /* Handle qXfer:btrace:read. */
1594 handle_qxfer_btrace (const char *annex
,
1595 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1596 ULONGEST offset
, LONGEST len
)
1598 static struct buffer cache
;
1599 struct thread_info
*thread
;
1602 if (the_target
->read_btrace
== NULL
|| writebuf
!= NULL
)
1605 if (!target_running ())
1608 if (ptid_equal (general_thread
, null_ptid
)
1609 || ptid_equal (general_thread
, minus_one_ptid
))
1611 strcpy (own_buf
, "E.Must select a single thread.");
1615 thread
= find_thread_ptid (general_thread
);
1618 strcpy (own_buf
, "E.No such thread.");
1622 if (thread
->btrace
== NULL
)
1624 strcpy (own_buf
, "E.Btrace not enabled.");
1628 if (strcmp (annex
, "all") == 0)
1629 type
= BTRACE_READ_ALL
;
1630 else if (strcmp (annex
, "new") == 0)
1631 type
= BTRACE_READ_NEW
;
1632 else if (strcmp (annex
, "delta") == 0)
1633 type
= BTRACE_READ_DELTA
;
1636 strcpy (own_buf
, "E.Bad annex.");
1642 buffer_free (&cache
);
1644 result
= target_read_btrace (thread
->btrace
, &cache
, type
);
1647 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1651 else if (offset
> cache
.used_size
)
1653 buffer_free (&cache
);
1657 if (len
> cache
.used_size
- offset
)
1658 len
= cache
.used_size
- offset
;
1660 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1665 /* Handle qXfer:btrace-conf:read. */
1668 handle_qxfer_btrace_conf (const char *annex
,
1669 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1670 ULONGEST offset
, LONGEST len
)
1672 static struct buffer cache
;
1673 struct thread_info
*thread
;
1676 if (the_target
->read_btrace_conf
== NULL
|| writebuf
!= NULL
)
1679 if (annex
[0] != '\0' || !target_running ())
1682 if (ptid_equal (general_thread
, null_ptid
)
1683 || ptid_equal (general_thread
, minus_one_ptid
))
1685 strcpy (own_buf
, "E.Must select a single thread.");
1689 thread
= find_thread_ptid (general_thread
);
1692 strcpy (own_buf
, "E.No such thread.");
1696 if (thread
->btrace
== NULL
)
1698 strcpy (own_buf
, "E.Btrace not enabled.");
1704 buffer_free (&cache
);
1706 result
= target_read_btrace_conf (thread
->btrace
, &cache
);
1709 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1713 else if (offset
> cache
.used_size
)
1715 buffer_free (&cache
);
1719 if (len
> cache
.used_size
- offset
)
1720 len
= cache
.used_size
- offset
;
1722 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1727 static const struct qxfer qxfer_packets
[] =
1729 { "auxv", handle_qxfer_auxv
},
1730 { "btrace", handle_qxfer_btrace
},
1731 { "btrace-conf", handle_qxfer_btrace_conf
},
1732 { "exec-file", handle_qxfer_exec_file
},
1733 { "fdpic", handle_qxfer_fdpic
},
1734 { "features", handle_qxfer_features
},
1735 { "libraries", handle_qxfer_libraries
},
1736 { "libraries-svr4", handle_qxfer_libraries_svr4
},
1737 { "osdata", handle_qxfer_osdata
},
1738 { "siginfo", handle_qxfer_siginfo
},
1739 { "spu", handle_qxfer_spu
},
1740 { "statictrace", handle_qxfer_statictrace
},
1741 { "threads", handle_qxfer_threads
},
1742 { "traceframe-info", handle_qxfer_traceframe_info
},
1746 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1754 if (!startswith (own_buf
, "qXfer:"))
1757 /* Grab the object, r/w and annex. */
1758 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1760 write_enn (own_buf
);
1765 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1768 const struct qxfer
*q
= &qxfer_packets
[i
];
1770 if (strcmp (object
, q
->object
) == 0)
1772 if (strcmp (rw
, "read") == 0)
1774 unsigned char *data
;
1779 /* Grab the offset and length. */
1780 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1782 write_enn (own_buf
);
1786 /* Read one extra byte, as an indicator of whether there is
1788 if (len
> PBUFSIZ
- 2)
1790 data
= malloc (len
+ 1);
1793 write_enn (own_buf
);
1796 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1804 /* Preserve error message. */
1807 write_enn (own_buf
);
1809 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1811 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1816 else if (strcmp (rw
, "write") == 0)
1821 unsigned char *data
;
1823 strcpy (own_buf
, "E00");
1824 data
= malloc (packet_len
- (offset
- own_buf
));
1827 write_enn (own_buf
);
1830 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1831 &ofs
, &len
, data
) < 0)
1834 write_enn (own_buf
);
1838 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1846 /* Preserve error message. */
1849 write_enn (own_buf
);
1851 sprintf (own_buf
, "%x", n
);
1864 /* Table used by the crc32 function to calcuate the checksum. */
1866 static unsigned int crc32_table
[256] =
1869 /* Compute 32 bit CRC from inferior memory.
1871 On success, return 32 bit CRC.
1872 On failure, return (unsigned long long) -1. */
1874 static unsigned long long
1875 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1877 if (!crc32_table
[1])
1879 /* Initialize the CRC table and the decoding table. */
1883 for (i
= 0; i
< 256; i
++)
1885 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1886 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1893 unsigned char byte
= 0;
1895 /* Return failure if memory read fails. */
1896 if (read_inferior_memory (base
, &byte
, 1) != 0)
1897 return (unsigned long long) -1;
1899 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1902 return (unsigned long long) crc
;
1905 /* Add supported btrace packets to BUF. */
1908 supported_btrace_packets (char *buf
)
1910 int btrace_supported
= 0;
1912 if (target_supports_btrace (BTRACE_FORMAT_BTS
))
1914 strcat (buf
, ";Qbtrace:bts+");
1915 strcat (buf
, ";Qbtrace-conf:bts:size+");
1917 btrace_supported
= 1;
1920 if (target_supports_btrace (BTRACE_FORMAT_PT
))
1922 strcat (buf
, ";Qbtrace:pt+");
1923 strcat (buf
, ";Qbtrace-conf:pt:size+");
1925 btrace_supported
= 1;
1928 if (!btrace_supported
)
1931 strcat (buf
, ";Qbtrace:off+");
1932 strcat (buf
, ";qXfer:btrace:read+");
1933 strcat (buf
, ";qXfer:btrace-conf:read+");
1936 /* Handle all of the extended 'q' packets. */
1939 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1941 static struct inferior_list_entry
*thread_ptr
;
1943 /* Reply the current thread id. */
1944 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1947 require_running (own_buf
);
1949 if (!ptid_equal (general_thread
, null_ptid
)
1950 && !ptid_equal (general_thread
, minus_one_ptid
))
1951 gdb_id
= general_thread
;
1954 thread_ptr
= get_first_inferior (&all_threads
);
1955 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1958 sprintf (own_buf
, "QC");
1960 write_ptid (own_buf
, gdb_id
);
1964 if (strcmp ("qSymbol::", own_buf
) == 0)
1966 /* GDB is suggesting new symbols have been loaded. This may
1967 mean a new shared library has been detected as loaded, so
1968 take the opportunity to check if breakpoints we think are
1969 inserted, still are. Note that it isn't guaranteed that
1970 we'll see this when a shared library is loaded, and nor will
1971 we see this for unloads (although breakpoints in unloaded
1972 libraries shouldn't trigger), as GDB may not find symbols for
1973 the library at all. We also re-validate breakpoints when we
1974 see a second GDB breakpoint for the same address, and or when
1975 we access breakpoint shadows. */
1976 validate_breakpoints ();
1978 if (target_supports_tracepoints ())
1979 tracepoint_look_up_symbols ();
1981 if (target_running () && the_target
->look_up_symbols
!= NULL
)
1982 (*the_target
->look_up_symbols
) ();
1984 strcpy (own_buf
, "OK");
1988 if (!disable_packet_qfThreadInfo
)
1990 if (strcmp ("qfThreadInfo", own_buf
) == 0)
1994 require_running (own_buf
);
1995 thread_ptr
= get_first_inferior (&all_threads
);
1998 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1999 write_ptid (own_buf
, gdb_id
);
2000 thread_ptr
= thread_ptr
->next
;
2004 if (strcmp ("qsThreadInfo", own_buf
) == 0)
2008 require_running (own_buf
);
2009 if (thread_ptr
!= NULL
)
2012 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
2013 write_ptid (own_buf
, gdb_id
);
2014 thread_ptr
= thread_ptr
->next
;
2019 sprintf (own_buf
, "l");
2025 if (the_target
->read_offsets
!= NULL
2026 && strcmp ("qOffsets", own_buf
) == 0)
2028 CORE_ADDR text
, data
;
2030 require_running (own_buf
);
2031 if (the_target
->read_offsets (&text
, &data
))
2032 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
2033 (long)text
, (long)data
, (long)data
);
2035 write_enn (own_buf
);
2040 /* Protocol features query. */
2041 if (startswith (own_buf
, "qSupported")
2042 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
2044 char *p
= &own_buf
[10];
2045 int gdb_supports_qRelocInsn
= 0;
2047 /* Start processing qSupported packet. */
2048 target_process_qsupported (NULL
);
2050 /* Process each feature being provided by GDB. The first
2051 feature will follow a ':', and latter features will follow
2055 char **qsupported
= NULL
;
2059 /* Two passes, to avoid nested strtok calls in
2060 target_process_qsupported. */
2061 for (p
= strtok (p
+ 1, ";");
2063 p
= strtok (NULL
, ";"))
2066 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
2067 qsupported
[count
- 1] = xstrdup (p
);
2070 for (i
= 0; i
< count
; i
++)
2073 if (strcmp (p
, "multiprocess+") == 0)
2075 /* GDB supports and wants multi-process support if
2077 if (target_supports_multi_process ())
2080 else if (strcmp (p
, "qRelocInsn+") == 0)
2082 /* GDB supports relocate instruction requests. */
2083 gdb_supports_qRelocInsn
= 1;
2085 else if (strcmp (p
, "swbreak+") == 0)
2087 /* GDB wants us to report whether a trap is caused
2088 by a software breakpoint and for us to handle PC
2089 adjustment if necessary on this target. */
2090 if (target_supports_stopped_by_sw_breakpoint ())
2091 swbreak_feature
= 1;
2093 else if (strcmp (p
, "hwbreak+") == 0)
2095 /* GDB wants us to report whether a trap is caused
2096 by a hardware breakpoint. */
2097 if (target_supports_stopped_by_hw_breakpoint ())
2098 hwbreak_feature
= 1;
2100 else if (strcmp (p
, "fork-events+") == 0)
2102 /* GDB supports and wants fork events if possible. */
2103 if (target_supports_fork_events ())
2104 report_fork_events
= 1;
2106 else if (strcmp (p
, "vfork-events+") == 0)
2108 /* GDB supports and wants vfork events if possible. */
2109 if (target_supports_vfork_events ())
2110 report_vfork_events
= 1;
2113 target_process_qsupported (p
);
2122 "PacketSize=%x;QPassSignals+;QProgramSignals+",
2125 if (the_target
->qxfer_libraries_svr4
!= NULL
)
2126 strcat (own_buf
, ";qXfer:libraries-svr4:read+"
2127 ";augmented-libraries-svr4-read+");
2130 /* We do not have any hook to indicate whether the non-SVR4 target
2131 backend supports qXfer:libraries:read, so always report it. */
2132 strcat (own_buf
, ";qXfer:libraries:read+");
2135 if (the_target
->read_auxv
!= NULL
)
2136 strcat (own_buf
, ";qXfer:auxv:read+");
2138 if (the_target
->qxfer_spu
!= NULL
)
2139 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
2141 if (the_target
->qxfer_siginfo
!= NULL
)
2142 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
2144 if (the_target
->read_loadmap
!= NULL
)
2145 strcat (own_buf
, ";qXfer:fdpic:read+");
2147 /* We always report qXfer:features:read, as targets may
2148 install XML files on a subsequent call to arch_setup.
2149 If we reported to GDB on startup that we don't support
2150 qXfer:feature:read at all, we will never be re-queried. */
2151 strcat (own_buf
, ";qXfer:features:read+");
2153 if (transport_is_reliable
)
2154 strcat (own_buf
, ";QStartNoAckMode+");
2156 if (the_target
->qxfer_osdata
!= NULL
)
2157 strcat (own_buf
, ";qXfer:osdata:read+");
2159 if (target_supports_multi_process ())
2160 strcat (own_buf
, ";multiprocess+");
2162 if (target_supports_fork_events ())
2163 strcat (own_buf
, ";fork-events+");
2165 if (target_supports_vfork_events ())
2166 strcat (own_buf
, ";vfork-events+");
2168 if (target_supports_non_stop ())
2169 strcat (own_buf
, ";QNonStop+");
2171 if (target_supports_disable_randomization ())
2172 strcat (own_buf
, ";QDisableRandomization+");
2174 strcat (own_buf
, ";qXfer:threads:read+");
2176 if (target_supports_tracepoints ())
2178 strcat (own_buf
, ";ConditionalTracepoints+");
2179 strcat (own_buf
, ";TraceStateVariables+");
2180 strcat (own_buf
, ";TracepointSource+");
2181 strcat (own_buf
, ";DisconnectedTracing+");
2182 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
2183 strcat (own_buf
, ";FastTracepoints+");
2184 strcat (own_buf
, ";StaticTracepoints+");
2185 strcat (own_buf
, ";InstallInTrace+");
2186 strcat (own_buf
, ";qXfer:statictrace:read+");
2187 strcat (own_buf
, ";qXfer:traceframe-info:read+");
2188 strcat (own_buf
, ";EnableDisableTracepoints+");
2189 strcat (own_buf
, ";QTBuffer:size+");
2190 strcat (own_buf
, ";tracenz+");
2193 /* Support target-side breakpoint conditions and commands. */
2194 if (target_supports_conditional_breakpoints ())
2195 strcat (own_buf
, ";ConditionalBreakpoints+");
2196 strcat (own_buf
, ";BreakpointCommands+");
2198 if (target_supports_agent ())
2199 strcat (own_buf
, ";QAgent+");
2201 supported_btrace_packets (own_buf
);
2203 if (target_supports_stopped_by_sw_breakpoint ())
2204 strcat (own_buf
, ";swbreak+");
2206 if (target_supports_stopped_by_hw_breakpoint ())
2207 strcat (own_buf
, ";hwbreak+");
2209 if (the_target
->pid_to_exec_file
!= NULL
)
2210 strcat (own_buf
, ";qXfer:exec-file:read+");
2212 /* Reinitialize components as needed for the new connection. */
2213 hostio_handle_new_gdb_connection ();
2214 target_handle_new_gdb_connection ();
2219 /* Thread-local storage support. */
2220 if (the_target
->get_tls_address
!= NULL
2221 && startswith (own_buf
, "qGetTLSAddr:"))
2223 char *p
= own_buf
+ 12;
2224 CORE_ADDR parts
[2], address
= 0;
2226 ptid_t ptid
= null_ptid
;
2228 require_running (own_buf
);
2230 for (i
= 0; i
< 3; i
++)
2238 p2
= strchr (p
, ',');
2251 ptid
= read_ptid (p
, NULL
);
2253 decode_address (&parts
[i
- 1], p
, len
);
2257 if (p
!= NULL
|| i
< 3)
2261 struct thread_info
*thread
= find_thread_ptid (ptid
);
2266 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
2272 strcpy (own_buf
, paddress(address
));
2277 write_enn (own_buf
);
2281 /* Otherwise, pretend we do not understand this packet. */
2284 /* Windows OS Thread Information Block address support. */
2285 if (the_target
->get_tib_address
!= NULL
2286 && startswith (own_buf
, "qGetTIBAddr:"))
2291 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
2293 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
2296 strcpy (own_buf
, paddress(tlb
));
2301 write_enn (own_buf
);
2307 /* Handle "monitor" commands. */
2308 if (startswith (own_buf
, "qRcmd,"))
2310 char *mon
= malloc (PBUFSIZ
);
2311 int len
= strlen (own_buf
+ 6);
2315 write_enn (own_buf
);
2320 || hex2bin (own_buf
+ 6, (gdb_byte
*) mon
, len
/ 2) != len
/ 2)
2322 write_enn (own_buf
);
2326 mon
[len
/ 2] = '\0';
2330 if (the_target
->handle_monitor_command
== NULL
2331 || (*the_target
->handle_monitor_command
) (mon
) == 0)
2332 /* Default processing. */
2333 handle_monitor_command (mon
, own_buf
);
2339 if (startswith (own_buf
, "qSearch:memory:"))
2341 require_running (own_buf
);
2342 handle_search_memory (own_buf
, packet_len
);
2346 if (strcmp (own_buf
, "qAttached") == 0
2347 || startswith (own_buf
, "qAttached:"))
2349 struct process_info
*process
;
2351 if (own_buf
[sizeof ("qAttached") - 1])
2353 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
2354 process
= (struct process_info
*)
2355 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
2359 require_running (own_buf
);
2360 process
= current_process ();
2363 if (process
== NULL
)
2365 write_enn (own_buf
);
2369 strcpy (own_buf
, process
->attached
? "1" : "0");
2373 if (startswith (own_buf
, "qCRC:"))
2375 /* CRC check (compare-section). */
2379 unsigned long long crc
;
2381 require_running (own_buf
);
2382 comma
= unpack_varlen_hex (own_buf
+ 5, &base
);
2383 if (*comma
++ != ',')
2385 write_enn (own_buf
);
2388 len
= strtoul (comma
, NULL
, 16);
2389 crc
= crc32 (base
, len
, 0xffffffff);
2390 /* Check for memory failure. */
2391 if (crc
== (unsigned long long) -1)
2393 write_enn (own_buf
);
2396 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
2400 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
2403 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
2406 /* Otherwise we didn't know what packet it was. Say we didn't
2411 static void gdb_wants_all_threads_stopped (void);
2412 static void resume (struct thread_resume
*actions
, size_t n
);
2414 /* The callback that is passed to visit_actioned_threads. */
2415 typedef int (visit_actioned_threads_callback_ftype
)
2416 (const struct thread_resume
*, struct thread_info
*);
2418 /* Struct to pass data to visit_actioned_threads. */
2420 struct visit_actioned_threads_data
2422 const struct thread_resume
*actions
;
2424 visit_actioned_threads_callback_ftype
*callback
;
2427 /* Call CALLBACK for any thread to which ACTIONS applies to. Returns
2428 true if CALLBACK returns true. Returns false if no matching thread
2429 is found or CALLBACK results false.
2430 Note: This function is itself a callback for find_inferior. */
2433 visit_actioned_threads (struct inferior_list_entry
*entry
, void *datap
)
2435 struct visit_actioned_threads_data
*data
= datap
;
2436 const struct thread_resume
*actions
= data
->actions
;
2437 size_t num_actions
= data
->num_actions
;
2438 visit_actioned_threads_callback_ftype
*callback
= data
->callback
;
2441 for (i
= 0; i
< num_actions
; i
++)
2443 const struct thread_resume
*action
= &actions
[i
];
2445 if (ptid_equal (action
->thread
, minus_one_ptid
)
2446 || ptid_equal (action
->thread
, entry
->id
)
2447 || ((ptid_get_pid (action
->thread
)
2448 == ptid_get_pid (entry
->id
))
2449 && ptid_get_lwp (action
->thread
) == -1))
2451 struct thread_info
*thread
= (struct thread_info
*) entry
;
2453 if ((*callback
) (action
, thread
))
2461 /* Callback for visit_actioned_threads. If the thread has a pending
2462 status to report, report it now. */
2465 handle_pending_status (const struct thread_resume
*resumption
,
2466 struct thread_info
*thread
)
2468 if (thread
->status_pending_p
)
2470 thread
->status_pending_p
= 0;
2472 last_status
= thread
->last_status
;
2473 last_ptid
= thread
->entry
.id
;
2474 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2480 /* Parse vCont packets. */
2482 handle_v_cont (char *own_buf
)
2486 struct thread_resume
*resume_info
;
2487 struct thread_resume default_action
= {{0}};
2489 /* Count the number of semicolons in the packet. There should be one
2490 for every action. */
2496 p
= strchr (p
, ';');
2499 resume_info
= malloc (n
* sizeof (resume_info
[0]));
2500 if (resume_info
== NULL
)
2508 memset (&resume_info
[i
], 0, sizeof resume_info
[i
]);
2510 if (p
[0] == 's' || p
[0] == 'S')
2511 resume_info
[i
].kind
= resume_step
;
2512 else if (p
[0] == 'r')
2513 resume_info
[i
].kind
= resume_step
;
2514 else if (p
[0] == 'c' || p
[0] == 'C')
2515 resume_info
[i
].kind
= resume_continue
;
2516 else if (p
[0] == 't')
2517 resume_info
[i
].kind
= resume_stop
;
2521 if (p
[0] == 'S' || p
[0] == 'C')
2524 sig
= strtol (p
+ 1, &q
, 16);
2529 if (!gdb_signal_to_host_p (sig
))
2531 resume_info
[i
].sig
= gdb_signal_to_host (sig
);
2533 else if (p
[0] == 'r')
2537 p
= unpack_varlen_hex (p
+ 1, &addr
);
2538 resume_info
[i
].step_range_start
= addr
;
2543 p
= unpack_varlen_hex (p
+ 1, &addr
);
2544 resume_info
[i
].step_range_end
= addr
;
2553 resume_info
[i
].thread
= minus_one_ptid
;
2554 default_action
= resume_info
[i
];
2556 /* Note: we don't increment i here, we'll overwrite this entry
2557 the next time through. */
2559 else if (p
[0] == ':')
2561 ptid_t ptid
= read_ptid (p
+ 1, &q
);
2566 if (p
[0] != ';' && p
[0] != 0)
2569 resume_info
[i
].thread
= ptid
;
2576 resume_info
[i
] = default_action
;
2578 set_desired_thread (0);
2580 resume (resume_info
, n
);
2585 write_enn (own_buf
);
2590 /* Resume target with ACTIONS, an array of NUM_ACTIONS elements. */
2593 resume (struct thread_resume
*actions
, size_t num_actions
)
2597 /* Check if among the threads that GDB wants actioned, there's
2598 one with a pending status to report. If so, skip actually
2599 resuming/stopping and report the pending event
2601 struct visit_actioned_threads_data data
;
2603 data
.actions
= actions
;
2604 data
.num_actions
= num_actions
;
2605 data
.callback
= handle_pending_status
;
2606 if (find_inferior (&all_threads
, visit_actioned_threads
, &data
) != NULL
)
2612 (*the_target
->resume
) (actions
, num_actions
);
2618 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2620 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
)
2622 /* No proper RSP support for this yet. At least return
2624 sprintf (own_buf
, "E.No unwaited-for children left.");
2625 disable_async_io ();
2629 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2630 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
2631 && last_status
.kind
!= TARGET_WAITKIND_NO_RESUMED
)
2632 current_thread
->last_status
= last_status
;
2634 /* From the client's perspective, all-stop mode always stops all
2635 threads implicitly (and the target backend has already done
2636 so by now). Tag all threads as "want-stopped", so we don't
2637 resume them implicitly without the client telling us to. */
2638 gdb_wants_all_threads_stopped ();
2639 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2640 disable_async_io ();
2642 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2643 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2644 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2648 /* Attach to a new program. Return 1 if successful, 0 if failure. */
2650 handle_v_attach (char *own_buf
)
2654 pid
= strtol (own_buf
+ 8, NULL
, 16);
2655 if (pid
!= 0 && attach_inferior (pid
) == 0)
2657 /* Don't report shared library events after attaching, even if
2658 some libraries are preloaded. GDB will always poll the
2659 library list. Avoids the "stopped by shared library event"
2660 notice on the GDB side. */
2665 /* In non-stop, we don't send a resume reply. Stop events
2666 will follow up using the normal notification
2671 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2677 write_enn (own_buf
);
2682 /* Run a new program. Return 1 if successful, 0 if failure. */
2684 handle_v_run (char *own_buf
)
2686 char *p
, *next_p
, **new_argv
;
2690 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
2696 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
2697 if (new_argv
== NULL
)
2699 write_enn (own_buf
);
2704 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
2706 next_p
= strchr (p
, ';');
2708 next_p
= p
+ strlen (p
);
2710 if (i
== 0 && p
== next_p
)
2714 /* FIXME: Fail request if out of memory instead of dying. */
2715 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
2716 hex2bin (p
, (gdb_byte
*) new_argv
[i
], (next_p
- p
) / 2);
2717 new_argv
[i
][(next_p
- p
) / 2] = '\0';
2726 if (new_argv
[0] == NULL
)
2728 /* GDB didn't specify a program to run. Use the program from the
2729 last run with the new argument list. */
2731 if (program_argv
== NULL
)
2733 write_enn (own_buf
);
2734 freeargv (new_argv
);
2738 new_argv
[0] = strdup (program_argv
[0]);
2739 if (new_argv
[0] == NULL
)
2741 write_enn (own_buf
);
2742 freeargv (new_argv
);
2747 /* Free the old argv and install the new one. */
2748 freeargv (program_argv
);
2749 program_argv
= new_argv
;
2751 start_inferior (program_argv
);
2752 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
2754 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2756 /* In non-stop, sending a resume reply doesn't set the general
2757 thread, but GDB assumes a vRun sets it (this is so GDB can
2758 query which is the main thread of the new inferior. */
2760 general_thread
= last_ptid
;
2766 write_enn (own_buf
);
2771 /* Kill process. Return 1 if successful, 0 if failure. */
2773 handle_v_kill (char *own_buf
)
2776 char *p
= &own_buf
[6];
2778 pid
= strtol (p
, NULL
, 16);
2781 if (pid
!= 0 && kill_inferior (pid
) == 0)
2783 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2784 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
2785 last_ptid
= pid_to_ptid (pid
);
2786 discard_queued_stop_replies (pid
);
2792 write_enn (own_buf
);
2797 /* Handle all of the extended 'v' packets. */
2799 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2801 if (!disable_packet_vCont
)
2803 if (startswith (own_buf
, "vCont;"))
2805 require_running (own_buf
);
2806 handle_v_cont (own_buf
);
2810 if (startswith (own_buf
, "vCont?"))
2812 strcpy (own_buf
, "vCont;c;C;s;S;t");
2813 if (target_supports_range_stepping ())
2815 own_buf
= own_buf
+ strlen (own_buf
);
2816 strcpy (own_buf
, ";r");
2822 if (startswith (own_buf
, "vFile:")
2823 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2826 if (startswith (own_buf
, "vAttach;"))
2828 if ((!extended_protocol
|| !multi_process
) && target_running ())
2830 fprintf (stderr
, "Already debugging a process\n");
2831 write_enn (own_buf
);
2834 handle_v_attach (own_buf
);
2838 if (startswith (own_buf
, "vRun;"))
2840 if ((!extended_protocol
|| !multi_process
) && target_running ())
2842 fprintf (stderr
, "Already debugging a process\n");
2843 write_enn (own_buf
);
2846 handle_v_run (own_buf
);
2850 if (startswith (own_buf
, "vKill;"))
2852 if (!target_running ())
2854 fprintf (stderr
, "No process to kill\n");
2855 write_enn (own_buf
);
2858 handle_v_kill (own_buf
);
2862 if (handle_notif_ack (own_buf
, packet_len
))
2865 /* Otherwise we didn't know what packet it was. Say we didn't
2871 /* Resume thread and wait for another event. In non-stop mode,
2872 don't really wait here, but return immediatelly to the event
2875 myresume (char *own_buf
, int step
, int sig
)
2877 struct thread_resume resume_info
[2];
2879 int valid_cont_thread
;
2881 set_desired_thread (0);
2883 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2884 && !ptid_equal (cont_thread
, minus_one_ptid
));
2886 if (step
|| sig
|| valid_cont_thread
)
2888 resume_info
[0].thread
= current_ptid
;
2890 resume_info
[0].kind
= resume_step
;
2892 resume_info
[0].kind
= resume_continue
;
2893 resume_info
[0].sig
= sig
;
2897 if (!valid_cont_thread
)
2899 resume_info
[n
].thread
= minus_one_ptid
;
2900 resume_info
[n
].kind
= resume_continue
;
2901 resume_info
[n
].sig
= 0;
2905 resume (resume_info
, n
);
2908 /* Callback for for_each_inferior. Make a new stop reply for each
2912 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2914 struct thread_info
*thread
= (struct thread_info
*) entry
;
2916 /* For now, assume targets that don't have this callback also don't
2917 manage the thread's last_status field. */
2918 if (the_target
->thread_stopped
== NULL
)
2920 struct vstop_notif
*new_notif
= xmalloc (sizeof (*new_notif
));
2922 new_notif
->ptid
= entry
->id
;
2923 new_notif
->status
= thread
->last_status
;
2924 /* Pass the last stop reply back to GDB, but don't notify
2926 notif_event_enque (¬if_stop
,
2927 (struct notif_event
*) new_notif
);
2931 if (thread_stopped (thread
))
2936 = target_waitstatus_to_string (&thread
->last_status
);
2938 debug_printf ("Reporting thread %s as already stopped with %s\n",
2939 target_pid_to_str (entry
->id
),
2942 xfree (status_string
);
2945 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2947 /* Pass the last stop reply back to GDB, but don't notify
2949 queue_stop_reply (entry
->id
, &thread
->last_status
);
2956 /* Set this inferior threads's state as "want-stopped". We won't
2957 resume this thread until the client gives us another action for
2961 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2963 struct thread_info
*thread
= (struct thread_info
*) entry
;
2965 thread
->last_resume_kind
= resume_stop
;
2967 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2969 /* Most threads are stopped implicitly (all-stop); tag that with
2971 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2972 thread
->last_status
.value
.sig
= GDB_SIGNAL_0
;
2976 /* Set all threads' states as "want-stopped". */
2979 gdb_wants_all_threads_stopped (void)
2981 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2984 /* Clear the gdb_detached flag of every process. */
2987 gdb_reattached_process (struct inferior_list_entry
*entry
)
2989 struct process_info
*process
= (struct process_info
*) entry
;
2991 process
->gdb_detached
= 0;
2994 /* Callback for for_each_inferior. Clear the thread's pending status
2998 clear_pending_status_callback (struct inferior_list_entry
*entry
)
3000 struct thread_info
*thread
= (struct thread_info
*) entry
;
3002 thread
->status_pending_p
= 0;
3005 /* Callback for for_each_inferior. If the thread is stopped with an
3006 interesting event, mark it as having a pending event. */
3009 set_pending_status_callback (struct inferior_list_entry
*entry
)
3011 struct thread_info
*thread
= (struct thread_info
*) entry
;
3013 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
3014 || (thread
->last_status
.value
.sig
!= GDB_SIGNAL_0
3015 /* A breakpoint, watchpoint or finished step from a previous
3016 GDB run isn't considered interesting for a new GDB run.
3017 If we left those pending, the new GDB could consider them
3018 random SIGTRAPs. This leaves out real async traps. We'd
3019 have to peek into the (target-specific) siginfo to
3020 distinguish those. */
3021 && thread
->last_status
.value
.sig
!= GDB_SIGNAL_TRAP
))
3022 thread
->status_pending_p
= 1;
3025 /* Callback for find_inferior. Return true if ENTRY (a thread) has a
3026 pending status to report to GDB. */
3029 find_status_pending_thread_callback (struct inferior_list_entry
*entry
, void *data
)
3031 struct thread_info
*thread
= (struct thread_info
*) entry
;
3033 return thread
->status_pending_p
;
3036 /* Status handler for the '?' packet. */
3039 handle_status (char *own_buf
)
3041 /* GDB is connected, don't forward events to the target anymore. */
3042 for_each_inferior (&all_processes
, gdb_reattached_process
);
3044 /* In non-stop mode, we must send a stop reply for each stopped
3045 thread. In all-stop mode, just send one for the first stopped
3050 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
3052 /* The first is sent immediatly. OK is sent if there is no
3053 stopped thread, which is the same handling of the vStopped
3054 packet (by design). */
3055 notif_write_event (¬if_stop
, own_buf
);
3059 struct inferior_list_entry
*thread
= NULL
;
3062 stabilize_threads ();
3063 gdb_wants_all_threads_stopped ();
3065 /* We can only report one status, but we might be coming out of
3066 non-stop -- if more than one thread is stopped with
3067 interesting events, leave events for the threads we're not
3068 reporting now pending. They'll be reported the next time the
3069 threads are resumed. Start by marking all interesting events
3071 for_each_inferior (&all_threads
, set_pending_status_callback
);
3073 /* Prefer the last thread that reported an event to GDB (even if
3074 that was a GDB_SIGNAL_TRAP). */
3075 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
3076 && last_status
.kind
!= TARGET_WAITKIND_EXITED
3077 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
3078 thread
= find_inferior_id (&all_threads
, last_ptid
);
3080 /* If the last event thread is not found for some reason, look
3081 for some other thread that might have an event to report. */
3083 thread
= find_inferior (&all_threads
,
3084 find_status_pending_thread_callback
, NULL
);
3086 /* If we're still out of luck, simply pick the first thread in
3089 thread
= get_first_inferior (&all_threads
);
3093 struct thread_info
*tp
= (struct thread_info
*) thread
;
3095 /* We're reporting this event, so it's no longer
3097 tp
->status_pending_p
= 0;
3099 /* GDB assumes the current thread is the thread we're
3100 reporting the status for. */
3101 general_thread
= thread
->id
;
3102 set_desired_thread (1);
3104 gdb_assert (tp
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
3105 prepare_resume_reply (own_buf
, tp
->entry
.id
, &tp
->last_status
);
3108 strcpy (own_buf
, "W00");
3113 gdbserver_version (void)
3115 printf ("GNU gdbserver %s%s\n"
3116 "Copyright (C) 2015 Free Software Foundation, Inc.\n"
3117 "gdbserver is free software, covered by the "
3118 "GNU General Public License.\n"
3119 "This gdbserver was configured as \"%s\"\n",
3120 PKGVERSION
, version
, host_name
);
3124 gdbserver_usage (FILE *stream
)
3126 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
3127 "\tgdbserver [OPTIONS] --attach COMM PID\n"
3128 "\tgdbserver [OPTIONS] --multi COMM\n"
3130 "COMM may either be a tty device (for serial debugging),\n"
3131 "HOST:PORT to listen for a TCP connection, or '-' or 'stdio' to use \n"
3132 "stdin/stdout of gdbserver.\n"
3133 "PROG is the executable program. ARGS are arguments passed to inferior.\n"
3134 "PID is the process ID to attach to, when --attach is specified.\n"
3136 "Operating modes:\n"
3138 " --attach Attach to running process PID.\n"
3139 " --multi Start server without a specific program, and\n"
3140 " only quit when explicitly commanded.\n"
3141 " --once Exit after the first connection has closed.\n"
3142 " --help Print this message and then exit.\n"
3143 " --version Display version information and exit.\n"
3147 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
3148 " --disable-randomization\n"
3149 " Run PROG with address space randomization disabled.\n"
3150 " --no-disable-randomization\n"
3151 " Don't disable address space randomization when\n"
3156 " --debug Enable general debugging output.\n"
3157 " --debug-format=opt1[,opt2,...]\n"
3158 " Specify extra content in debugging output.\n"
3163 " --remote-debug Enable remote protocol debugging output.\n"
3164 " --disable-packet=opt1[,opt2,...]\n"
3165 " Disable support for RSP packets or features.\n"
3167 " vCont, Tthread, qC, qfThreadInfo and \n"
3168 " threads (disable all threading packets).\n"
3170 "For more information, consult the GDB manual (available as on-line \n"
3171 "info or a printed manual).\n");
3172 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
3173 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
3177 gdbserver_show_disableable (FILE *stream
)
3179 fprintf (stream
, "Disableable packets:\n"
3180 " vCont \tAll vCont packets\n"
3181 " qC \tQuerying the current thread\n"
3182 " qfThreadInfo\tThread listing\n"
3183 " Tthread \tPassing the thread specifier in the "
3184 "T stop reply packet\n"
3185 " threads \tAll of the above\n");
3189 #undef require_running
3190 #define require_running(BUF) \
3191 if (!target_running ()) \
3198 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
3200 int pid
= * (int *) args
;
3202 if (ptid_get_pid (entry
->id
) == pid
)
3209 kill_inferior_callback (struct inferior_list_entry
*entry
)
3211 struct process_info
*process
= (struct process_info
*) entry
;
3212 int pid
= ptid_get_pid (process
->entry
.id
);
3214 kill_inferior (pid
);
3215 discard_queued_stop_replies (pid
);
3218 /* Callback for for_each_inferior to detach or kill the inferior,
3219 depending on whether we attached to it or not.
3220 We inform the user whether we're detaching or killing the process
3221 as this is only called when gdbserver is about to exit. */
3224 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
3226 struct process_info
*process
= (struct process_info
*) entry
;
3227 int pid
= ptid_get_pid (process
->entry
.id
);
3229 if (process
->attached
)
3230 detach_inferior (pid
);
3232 kill_inferior (pid
);
3234 discard_queued_stop_replies (pid
);
3237 /* for_each_inferior callback for detach_or_kill_for_exit to print
3238 the pids of started inferiors. */
3241 print_started_pid (struct inferior_list_entry
*entry
)
3243 struct process_info
*process
= (struct process_info
*) entry
;
3245 if (! process
->attached
)
3247 int pid
= ptid_get_pid (process
->entry
.id
);
3248 fprintf (stderr
, " %d", pid
);
3252 /* for_each_inferior callback for detach_or_kill_for_exit to print
3253 the pids of attached inferiors. */
3256 print_attached_pid (struct inferior_list_entry
*entry
)
3258 struct process_info
*process
= (struct process_info
*) entry
;
3260 if (process
->attached
)
3262 int pid
= ptid_get_pid (process
->entry
.id
);
3263 fprintf (stderr
, " %d", pid
);
3267 /* Call this when exiting gdbserver with possible inferiors that need
3268 to be killed or detached from. */
3271 detach_or_kill_for_exit (void)
3273 /* First print a list of the inferiors we will be killing/detaching.
3274 This is to assist the user, for example, in case the inferior unexpectedly
3275 dies after we exit: did we screw up or did the inferior exit on its own?
3276 Having this info will save some head-scratching. */
3278 if (have_started_inferiors_p ())
3280 fprintf (stderr
, "Killing process(es):");
3281 for_each_inferior (&all_processes
, print_started_pid
);
3282 fprintf (stderr
, "\n");
3284 if (have_attached_inferiors_p ())
3286 fprintf (stderr
, "Detaching process(es):");
3287 for_each_inferior (&all_processes
, print_attached_pid
);
3288 fprintf (stderr
, "\n");
3291 /* Now we can kill or detach the inferiors. */
3293 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
3296 /* Value that will be passed to exit(3) when gdbserver exits. */
3297 static int exit_code
;
3299 /* Cleanup version of detach_or_kill_for_exit. */
3302 detach_or_kill_for_exit_cleanup (void *ignore
)
3307 detach_or_kill_for_exit ();
3310 CATCH (exception
, RETURN_MASK_ALL
)
3313 fprintf (stderr
, "Detach or kill failed: %s\n", exception
.message
);
3319 /* Main function. This is called by the real "main" function,
3320 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
3322 static void ATTRIBUTE_NORETURN
3323 captured_main (int argc
, char *argv
[])
3327 char *arg_end
, *port
;
3328 char **next_arg
= &argv
[1];
3329 volatile int multi_mode
= 0;
3330 volatile int attach
= 0;
3333 while (*next_arg
!= NULL
&& **next_arg
== '-')
3335 if (strcmp (*next_arg
, "--version") == 0)
3337 gdbserver_version ();
3340 else if (strcmp (*next_arg
, "--help") == 0)
3342 gdbserver_usage (stdout
);
3345 else if (strcmp (*next_arg
, "--attach") == 0)
3347 else if (strcmp (*next_arg
, "--multi") == 0)
3349 else if (strcmp (*next_arg
, "--wrapper") == 0)
3353 wrapper_argv
= next_arg
;
3354 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
3357 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
3359 gdbserver_usage (stderr
);
3363 /* Consume the "--". */
3366 else if (strcmp (*next_arg
, "--debug") == 0)
3368 else if (startswith (*next_arg
, "--debug-format="))
3371 = parse_debug_format_options ((*next_arg
)
3372 + sizeof ("--debug-format=") - 1, 0);
3374 if (error_msg
!= NULL
)
3376 fprintf (stderr
, "%s", error_msg
);
3380 else if (strcmp (*next_arg
, "--remote-debug") == 0)
3382 else if (strcmp (*next_arg
, "--disable-packet") == 0)
3384 gdbserver_show_disableable (stdout
);
3387 else if (startswith (*next_arg
, "--disable-packet="))
3389 char *packets
, *tok
;
3391 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
3392 for (tok
= strtok (packets
, ",");
3394 tok
= strtok (NULL
, ","))
3396 if (strcmp ("vCont", tok
) == 0)
3397 disable_packet_vCont
= 1;
3398 else if (strcmp ("Tthread", tok
) == 0)
3399 disable_packet_Tthread
= 1;
3400 else if (strcmp ("qC", tok
) == 0)
3401 disable_packet_qC
= 1;
3402 else if (strcmp ("qfThreadInfo", tok
) == 0)
3403 disable_packet_qfThreadInfo
= 1;
3404 else if (strcmp ("threads", tok
) == 0)
3406 disable_packet_vCont
= 1;
3407 disable_packet_Tthread
= 1;
3408 disable_packet_qC
= 1;
3409 disable_packet_qfThreadInfo
= 1;
3413 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
3415 gdbserver_show_disableable (stderr
);
3420 else if (strcmp (*next_arg
, "-") == 0)
3422 /* "-" specifies a stdio connection and is a form of port
3424 *next_arg
= STDIO_CONNECTION_NAME
;
3427 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
3428 disable_randomization
= 1;
3429 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
3430 disable_randomization
= 0;
3431 else if (strcmp (*next_arg
, "--once") == 0)
3435 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
3445 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
3447 gdbserver_usage (stderr
);
3451 /* Remember stdio descriptors. LISTEN_DESC must not be listed, it will be
3452 opened by remote_prepare. */
3455 /* We need to know whether the remote connection is stdio before
3456 starting the inferior. Inferiors created in this scenario have
3457 stdin,stdout redirected. So do this here before we call
3459 remote_prepare (port
);
3464 /* --attach used to come after PORT, so allow it there for
3466 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
3473 && (*next_arg
== NULL
3474 || (*next_arg
)[0] == '\0'
3475 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
3477 || next_arg
[1] != NULL
))
3482 gdbserver_usage (stderr
);
3486 initialize_async_io ();
3488 initialize_event_loop ();
3489 if (target_supports_tracepoints ())
3490 initialize_tracepoint ();
3492 own_buf
= xmalloc (PBUFSIZ
+ 1);
3493 mem_buf
= xmalloc (PBUFSIZ
);
3495 if (pid
== 0 && *next_arg
!= NULL
)
3499 n
= argc
- (next_arg
- argv
);
3500 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
3501 for (i
= 0; i
< n
; i
++)
3502 program_argv
[i
] = xstrdup (next_arg
[i
]);
3503 program_argv
[i
] = NULL
;
3505 /* Wait till we are at first instruction in program. */
3506 start_inferior (program_argv
);
3508 /* We are now (hopefully) stopped at the first instruction of
3509 the target process. This assumes that the target process was
3510 successfully created. */
3514 if (attach_inferior (pid
) == -1)
3515 error ("Attaching not supported on this target");
3517 /* Otherwise succeeded. */
3521 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3522 last_status
.value
.integer
= 0;
3523 last_ptid
= minus_one_ptid
;
3525 make_cleanup (detach_or_kill_for_exit_cleanup
, NULL
);
3527 initialize_notif ();
3529 /* Don't report shared library events on the initial connection,
3530 even if some libraries are preloaded. Avoids the "stopped by
3531 shared library event" notice on gdb side. */
3534 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3535 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3540 if (!was_running
&& !multi_mode
)
3541 error ("No program to debug");
3548 report_fork_events
= 0;
3549 report_vfork_events
= 0;
3550 /* Be sure we're out of tfind mode. */
3551 current_traceframe
= -1;
3552 cont_thread
= null_ptid
;
3553 swbreak_feature
= 0;
3554 hwbreak_feature
= 0;
3560 /* Wait for events. This will return when all event sources
3561 are removed from the event loop. */
3562 start_event_loop ();
3564 /* If an exit was requested (using the "monitor exit"
3565 command), terminate now. The only other way to get
3566 here is for getpkt to fail; close the connection
3567 and reopen it at the top of the loop. */
3569 if (exit_requested
|| run_once
)
3570 throw_quit ("Quit");
3573 "Remote side has terminated connection. "
3574 "GDBserver will reopen the connection.\n");
3576 /* Get rid of any pending statuses. An eventual reconnection
3577 (by the same GDB instance or another) will refresh all its
3578 state from scratch. */
3579 discard_queued_stop_replies (-1);
3580 for_each_inferior (&all_threads
,
3581 clear_pending_status_callback
);
3585 if (disconnected_tracing
)
3587 /* Try to enable non-stop/async mode, so we we can
3588 both wait for an async socket accept, and handle
3589 async target events simultaneously. There's also
3590 no point either in having the target always stop
3591 all threads, when we're going to pass signals
3592 down without informing GDB. */
3595 if (start_non_stop (1))
3598 /* Detaching implicitly resumes all threads;
3599 simply disconnecting does not. */
3605 "Disconnected tracing disabled; "
3606 "stopping trace run.\n");
3611 CATCH (exception
, RETURN_MASK_ERROR
)
3613 if (response_needed
)
3615 write_enn (own_buf
);
3623 /* Main function. */
3626 main (int argc
, char *argv
[])
3631 captured_main (argc
, argv
);
3633 CATCH (exception
, RETURN_MASK_ALL
)
3635 if (exception
.reason
== RETURN_ERROR
)
3638 fprintf (stderr
, "%s\n", exception
.message
);
3639 fprintf (stderr
, "Exiting\n");
3647 gdb_assert_not_reached ("captured_main should never return");
3650 /* Skip PACKET until the next semi-colon (or end of string). */
3653 skip_to_semicolon (char **packet
)
3655 while (**packet
!= '\0' && **packet
!= ';')
3659 /* Process options coming from Z packets for a breakpoint. PACKET is
3660 the packet buffer. *PACKET is updated to point to the first char
3661 after the last processed option. */
3664 process_point_options (struct breakpoint
*bp
, char **packet
)
3666 char *dataptr
= *packet
;
3669 /* Check if data has the correct format. */
3670 if (*dataptr
!= ';')
3677 if (*dataptr
== ';')
3680 if (*dataptr
== 'X')
3682 /* Conditional expression. */
3684 debug_printf ("Found breakpoint condition.\n");
3685 if (!add_breakpoint_condition (bp
, &dataptr
))
3686 skip_to_semicolon (&dataptr
);
3688 else if (startswith (dataptr
, "cmds:"))
3690 dataptr
+= strlen ("cmds:");
3692 debug_printf ("Found breakpoint commands %s.\n", dataptr
);
3693 persist
= (*dataptr
== '1');
3695 if (add_breakpoint_commands (bp
, &dataptr
, persist
))
3696 skip_to_semicolon (&dataptr
);
3700 fprintf (stderr
, "Unknown token %c, ignoring.\n",
3702 /* Skip tokens until we find one that we recognize. */
3703 skip_to_semicolon (&dataptr
);
3709 /* Event loop callback that handles a serial event. The first byte in
3710 the serial buffer gets us here. We expect characters to arrive at
3711 a brisk pace, so we read the rest of the packet with a blocking
3715 process_serial_event (void)
3726 int new_packet_len
= -1;
3728 /* Used to decide when gdbserver should exit in
3729 multi-mode/remote. */
3730 static int have_ran
= 0;
3733 have_ran
= target_running ();
3735 disable_async_io ();
3737 response_needed
= 0;
3738 packet_len
= getpkt (own_buf
);
3739 if (packet_len
<= 0)
3742 /* Force an event loop break. */
3745 response_needed
= 1;
3752 handle_query (own_buf
, packet_len
, &new_packet_len
);
3755 handle_general_set (own_buf
);
3758 require_running (own_buf
);
3763 pid
= strtol (&own_buf
[i
], NULL
, 16);
3766 pid
= ptid_get_pid (current_ptid
);
3768 if ((tracing
&& disconnected_tracing
) || any_persistent_commands ())
3770 struct thread_resume resume_info
;
3771 struct process_info
*process
= find_process_pid (pid
);
3773 if (process
== NULL
)
3775 write_enn (own_buf
);
3779 if (tracing
&& disconnected_tracing
)
3781 "Disconnected tracing in effect, "
3782 "leaving gdbserver attached to the process\n");
3784 if (any_persistent_commands ())
3786 "Persistent commands are present, "
3787 "leaving gdbserver attached to the process\n");
3789 /* Make sure we're in non-stop/async mode, so we we can both
3790 wait for an async socket accept, and handle async target
3791 events simultaneously. There's also no point either in
3792 having the target stop all threads, when we're going to
3793 pass signals down without informing GDB. */
3797 debug_printf ("Forcing non-stop mode\n");
3803 process
->gdb_detached
= 1;
3805 /* Detaching implicitly resumes all threads. */
3806 resume_info
.thread
= minus_one_ptid
;
3807 resume_info
.kind
= resume_continue
;
3808 resume_info
.sig
= 0;
3809 (*the_target
->resume
) (&resume_info
, 1);
3812 break; /* from switch/case */
3815 fprintf (stderr
, "Detaching from process %d\n", pid
);
3817 if (detach_inferior (pid
) != 0)
3818 write_enn (own_buf
);
3821 discard_queued_stop_replies (pid
);
3824 if (extended_protocol
)
3826 /* Treat this like a normal program exit. */
3827 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3828 last_status
.value
.integer
= 0;
3829 last_ptid
= pid_to_ptid (pid
);
3831 current_thread
= NULL
;
3838 /* If we are attached, then we can exit. Otherwise, we
3839 need to hang around doing nothing, until the child is
3841 join_inferior (pid
);
3847 extended_protocol
= 1;
3851 handle_status (own_buf
);
3854 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
3856 ptid_t gdb_id
, thread_id
;
3859 require_running (own_buf
);
3861 gdb_id
= read_ptid (&own_buf
[2], NULL
);
3863 pid
= ptid_get_pid (gdb_id
);
3865 if (ptid_equal (gdb_id
, null_ptid
)
3866 || ptid_equal (gdb_id
, minus_one_ptid
))
3867 thread_id
= null_ptid
;
3869 && ptid_equal (pid_to_ptid (pid
),
3872 struct thread_info
*thread
=
3873 (struct thread_info
*) find_inferior (&all_threads
,
3878 write_enn (own_buf
);
3882 thread_id
= thread
->entry
.id
;
3886 thread_id
= gdb_id_to_thread_id (gdb_id
);
3887 if (ptid_equal (thread_id
, null_ptid
))
3889 write_enn (own_buf
);
3894 if (own_buf
[1] == 'g')
3896 if (ptid_equal (thread_id
, null_ptid
))
3898 /* GDB is telling us to choose any thread. Check if
3899 the currently selected thread is still valid. If
3900 it is not, select the first available. */
3901 struct thread_info
*thread
=
3902 (struct thread_info
*) find_inferior_id (&all_threads
,
3906 thread
= get_first_thread ();
3907 thread_id
= thread
->entry
.id
;
3911 general_thread
= thread_id
;
3912 set_desired_thread (1);
3914 else if (own_buf
[1] == 'c')
3915 cont_thread
= thread_id
;
3921 /* Silently ignore it so that gdb can extend the protocol
3922 without compatibility headaches. */
3927 require_running (own_buf
);
3928 if (current_traceframe
>= 0)
3930 struct regcache
*regcache
3931 = new_register_cache (current_target_desc ());
3933 if (fetch_traceframe_registers (current_traceframe
,
3935 registers_to_string (regcache
, own_buf
);
3937 write_enn (own_buf
);
3938 free_register_cache (regcache
);
3942 struct regcache
*regcache
;
3944 set_desired_thread (1);
3945 regcache
= get_thread_regcache (current_thread
, 1);
3946 registers_to_string (regcache
, own_buf
);
3950 require_running (own_buf
);
3951 if (current_traceframe
>= 0)
3952 write_enn (own_buf
);
3955 struct regcache
*regcache
;
3957 set_desired_thread (1);
3958 regcache
= get_thread_regcache (current_thread
, 1);
3959 registers_from_string (regcache
, &own_buf
[1]);
3964 require_running (own_buf
);
3965 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
3966 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
3968 write_enn (own_buf
);
3970 bin2hex (mem_buf
, own_buf
, res
);
3973 require_running (own_buf
);
3974 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
3975 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
3978 write_enn (own_buf
);
3981 require_running (own_buf
);
3982 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
3983 &mem_addr
, &len
, &mem_buf
) < 0
3984 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
3985 write_enn (own_buf
);
3990 require_running (own_buf
);
3991 hex2bin (own_buf
+ 1, &sig
, 1);
3992 if (gdb_signal_to_host_p (sig
))
3993 signal
= gdb_signal_to_host (sig
);
3996 myresume (own_buf
, 0, signal
);
3999 require_running (own_buf
);
4000 hex2bin (own_buf
+ 1, &sig
, 1);
4001 if (gdb_signal_to_host_p (sig
))
4002 signal
= gdb_signal_to_host (sig
);
4005 myresume (own_buf
, 1, signal
);
4008 require_running (own_buf
);
4010 myresume (own_buf
, 0, signal
);
4013 require_running (own_buf
);
4015 myresume (own_buf
, 1, signal
);
4017 case 'Z': /* insert_ ... */
4019 case 'z': /* remove_ ... */
4024 char type
= own_buf
[1];
4026 const int insert
= ch
== 'Z';
4027 char *p
= &own_buf
[3];
4029 p
= unpack_varlen_hex (p
, &addr
);
4030 len
= strtol (p
+ 1, &dataptr
, 16);
4034 struct breakpoint
*bp
;
4036 bp
= set_gdb_breakpoint (type
, addr
, len
, &res
);
4041 /* GDB may have sent us a list of *point parameters to
4042 be evaluated on the target's side. Read such list
4043 here. If we already have a list of parameters, GDB
4044 is telling us to drop that list and use this one
4046 clear_breakpoint_conditions_and_commands (bp
);
4047 process_point_options (bp
, &dataptr
);
4051 res
= delete_gdb_breakpoint (type
, addr
, len
);
4059 write_enn (own_buf
);
4063 response_needed
= 0;
4064 if (!target_running ())
4065 /* The packet we received doesn't make sense - but we can't
4066 reply to it, either. */
4069 fprintf (stderr
, "Killing all inferiors\n");
4070 for_each_inferior (&all_processes
, kill_inferior_callback
);
4072 /* When using the extended protocol, we wait with no program
4073 running. The traditional protocol will exit instead. */
4074 if (extended_protocol
)
4076 last_status
.kind
= TARGET_WAITKIND_EXITED
;
4077 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
4085 ptid_t gdb_id
, thread_id
;
4087 require_running (own_buf
);
4089 gdb_id
= read_ptid (&own_buf
[1], NULL
);
4090 thread_id
= gdb_id_to_thread_id (gdb_id
);
4091 if (ptid_equal (thread_id
, null_ptid
))
4093 write_enn (own_buf
);
4097 if (mythread_alive (thread_id
))
4100 write_enn (own_buf
);
4104 response_needed
= 0;
4106 /* Restarting the inferior is only supported in the extended
4108 if (extended_protocol
)
4110 if (target_running ())
4111 for_each_inferior (&all_processes
,
4112 kill_inferior_callback
);
4113 fprintf (stderr
, "GDBserver restarting\n");
4115 /* Wait till we are at 1st instruction in prog. */
4116 if (program_argv
!= NULL
)
4118 start_inferior (program_argv
);
4119 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
4121 /* Stopped at the first instruction of the target
4123 general_thread
= last_ptid
;
4127 /* Something went wrong. */
4128 general_thread
= null_ptid
;
4133 last_status
.kind
= TARGET_WAITKIND_EXITED
;
4134 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
4140 /* It is a request we don't understand. Respond with an
4141 empty packet so that gdb knows that we don't support this
4147 /* Extended (long) request. */
4148 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
4152 /* It is a request we don't understand. Respond with an empty
4153 packet so that gdb knows that we don't support this
4159 if (new_packet_len
!= -1)
4160 putpkt_binary (own_buf
, new_packet_len
);
4164 response_needed
= 0;
4166 if (!extended_protocol
&& have_ran
&& !target_running ())
4168 /* In non-stop, defer exiting until GDB had a chance to query
4169 the whole vStopped list (until it gets an OK). */
4170 if (QUEUE_is_empty (notif_event_p
, notif_stop
.queue
))
4172 /* Be transparent when GDB is connected through stdio -- no
4173 need to spam GDB's console. */
4174 if (!remote_connection_is_stdio ())
4175 fprintf (stderr
, "GDBserver exiting\n");
4187 /* Event-loop callback for serial events. */
4190 handle_serial_event (int err
, gdb_client_data client_data
)
4193 debug_printf ("handling possible serial event\n");
4195 /* Really handle it. */
4196 if (process_serial_event () < 0)
4199 /* Be sure to not change the selected thread behind GDB's back.
4200 Important in the non-stop mode asynchronous protocol. */
4201 set_desired_thread (1);
4206 /* Event-loop callback for target events. */
4209 handle_target_event (int err
, gdb_client_data client_data
)
4212 debug_printf ("handling possible target event\n");
4214 last_ptid
= mywait (minus_one_ptid
, &last_status
,
4217 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
)
4219 /* No RSP support for this yet. */
4221 else if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
4223 int pid
= ptid_get_pid (last_ptid
);
4224 struct process_info
*process
= find_process_pid (pid
);
4225 int forward_event
= !gdb_connected () || process
->gdb_detached
;
4227 if (last_status
.kind
== TARGET_WAITKIND_EXITED
4228 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
4230 mark_breakpoints_out (process
);
4231 mourn_inferior (process
);
4235 /* We're reporting this thread as stopped. Update its
4236 "want-stopped" state to what the client wants, until it
4237 gets a new resume action. */
4238 current_thread
->last_resume_kind
= resume_stop
;
4239 current_thread
->last_status
= last_status
;
4244 if (!target_running ())
4246 /* The last process exited. We're done. */
4250 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
4252 /* A thread stopped with a signal, but gdb isn't
4253 connected to handle it. Pass it down to the
4254 inferior, as if it wasn't being traced. */
4255 struct thread_resume resume_info
;
4258 debug_printf ("GDB not connected; forwarding event %d for"
4260 (int) last_status
.kind
,
4261 target_pid_to_str (last_ptid
));
4263 resume_info
.thread
= last_ptid
;
4264 resume_info
.kind
= resume_continue
;
4265 resume_info
.sig
= gdb_signal_to_host (last_status
.value
.sig
);
4266 (*the_target
->resume
) (&resume_info
, 1);
4268 else if (debug_threads
)
4269 debug_printf ("GDB not connected; ignoring event %d for [%s]\n",
4270 (int) last_status
.kind
,
4271 target_pid_to_str (last_ptid
));
4275 struct vstop_notif
*vstop_notif
4276 = xmalloc (sizeof (struct vstop_notif
));
4278 vstop_notif
->status
= last_status
;
4279 vstop_notif
->ptid
= last_ptid
;
4280 /* Push Stop notification. */
4281 notif_push (¬if_stop
,
4282 (struct notif_event
*) vstop_notif
);
4286 /* Be sure to not change the selected thread behind GDB's back.
4287 Important in the non-stop mode asynchronous protocol. */
4288 set_desired_thread (1);