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
;
63 int report_thread_events
;
68 /* True if the "vContSupported" feature is active. In that case, GDB
69 wants us to report whether single step is supported in the reply to
71 static int vCont_supported
;
73 /* Whether we should attempt to disable the operating system's address
74 space randomization feature before starting an inferior. */
75 int disable_randomization
= 1;
77 static char **program_argv
, **wrapper_argv
;
79 int pass_signals
[GDB_SIGNAL_LAST
];
80 int program_signals
[GDB_SIGNAL_LAST
];
81 int program_signals_p
;
83 /* The PID of the originally created or attached inferior. Used to
84 send signals to the process when GDB sends us an asynchronous interrupt
85 (user hitting Control-C in the client), and to wait for the child to exit
86 when no longer debugging it. */
88 unsigned long signal_pid
;
91 /* A file descriptor for the controlling terminal. */
94 /* TERMINAL_FD's original foreground group. */
95 pid_t old_foreground_pgrp
;
97 /* Hand back terminal ownership to the original foreground group. */
100 restore_old_foreground_pgrp (void)
102 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
106 /* Set if you want to disable optional thread related packets support
107 in gdbserver, for the sake of testing GDB against stubs that don't
109 int disable_packet_vCont
;
110 int disable_packet_Tthread
;
111 int disable_packet_qC
;
112 int disable_packet_qfThreadInfo
;
114 /* Last status reported to GDB. */
115 static struct target_waitstatus last_status
;
116 static ptid_t last_ptid
;
118 static char *own_buf
;
119 static unsigned char *mem_buf
;
121 /* A sub-class of 'struct notif_event' for stop, holding information
122 relative to a single stop reply. We keep a queue of these to
123 push to GDB in non-stop mode. */
127 struct notif_event base
;
129 /* Thread or process that got the event. */
133 struct target_waitstatus status
;
136 /* The current btrace configuration. This is gdbserver's mirror of GDB's
137 btrace configuration. */
138 static struct btrace_config current_btrace_conf
;
140 DEFINE_QUEUE_P (notif_event_p
);
142 /* Put a stop reply to the stop reply queue. */
145 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
147 struct vstop_notif
*new_notif
= XNEW (struct vstop_notif
);
149 new_notif
->ptid
= ptid
;
150 new_notif
->status
= *status
;
152 notif_event_enque (¬if_stop
, (struct notif_event
*) new_notif
);
156 remove_all_on_match_ptid (QUEUE (notif_event_p
) *q
,
157 QUEUE_ITER (notif_event_p
) *iter
,
158 struct notif_event
*event
,
161 ptid_t filter_ptid
= *(ptid_t
*) data
;
162 struct vstop_notif
*vstop_event
= (struct vstop_notif
*) event
;
164 if (ptid_match (vstop_event
->ptid
, filter_ptid
))
166 if (q
->free_func
!= NULL
)
167 q
->free_func (event
);
169 QUEUE_remove_elem (notif_event_p
, q
, iter
);
178 discard_queued_stop_replies (ptid_t ptid
)
180 QUEUE_iterate (notif_event_p
, notif_stop
.queue
,
181 remove_all_on_match_ptid
, &ptid
);
185 vstop_notif_reply (struct notif_event
*event
, char *own_buf
)
187 struct vstop_notif
*vstop
= (struct vstop_notif
*) event
;
189 prepare_resume_reply (own_buf
, vstop
->ptid
, &vstop
->status
);
192 struct notif_server notif_stop
=
194 "vStopped", "Stop", NULL
, vstop_notif_reply
,
198 target_running (void)
200 return get_first_thread () != NULL
;
204 start_inferior (char **argv
)
206 char **new_argv
= argv
;
208 if (wrapper_argv
!= NULL
)
212 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
214 for (i
= 0; argv
[i
] != NULL
; i
++)
216 new_argv
= XALLOCAVEC (char *, count
);
218 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
219 new_argv
[count
++] = wrapper_argv
[i
];
220 for (i
= 0; argv
[i
] != NULL
; i
++)
221 new_argv
[count
++] = argv
[i
];
222 new_argv
[count
] = NULL
;
228 for (i
= 0; new_argv
[i
]; ++i
)
229 debug_printf ("new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
234 signal (SIGTTOU
, SIG_DFL
);
235 signal (SIGTTIN
, SIG_DFL
);
238 signal_pid
= create_inferior (new_argv
[0], new_argv
);
240 /* FIXME: we don't actually know at this point that the create
241 actually succeeded. We won't know that until we wait. */
242 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
247 signal (SIGTTOU
, SIG_IGN
);
248 signal (SIGTTIN
, SIG_IGN
);
249 terminal_fd
= fileno (stderr
);
250 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
251 tcsetpgrp (terminal_fd
, signal_pid
);
252 atexit (restore_old_foreground_pgrp
);
255 if (wrapper_argv
!= NULL
)
257 struct thread_resume resume_info
;
259 memset (&resume_info
, 0, sizeof (resume_info
));
260 resume_info
.thread
= pid_to_ptid (signal_pid
);
261 resume_info
.kind
= resume_continue
;
264 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
266 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
270 (*the_target
->resume
) (&resume_info
, 1);
272 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
273 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
276 current_thread
->last_resume_kind
= resume_stop
;
277 current_thread
->last_status
= last_status
;
279 while (last_status
.value
.sig
!= GDB_SIGNAL_TRAP
);
281 target_arch_setup ();
285 /* Wait till we are at 1st instruction in program, return new pid
286 (assuming success). */
287 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
289 target_arch_setup ();
291 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
292 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
294 current_thread
->last_resume_kind
= resume_stop
;
295 current_thread
->last_status
= last_status
;
298 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
304 attach_inferior (int pid
)
306 /* myattach should return -1 if attaching is unsupported,
307 0 if it succeeded, and call error() otherwise. */
309 if (myattach (pid
) != 0)
312 fprintf (stderr
, "Attached; pid = %d\n", pid
);
315 /* FIXME - It may be that we should get the SIGNAL_PID from the
316 attach function, so that it can be the main thread instead of
317 whichever we were told to attach to. */
322 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
324 /* GDB knows to ignore the first SIGSTOP after attaching to a running
325 process using the "attach" command, but this is different; it's
326 just using "target remote". Pretend it's just starting up. */
327 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
328 && last_status
.value
.sig
== GDB_SIGNAL_STOP
)
329 last_status
.value
.sig
= GDB_SIGNAL_TRAP
;
331 current_thread
->last_resume_kind
= resume_stop
;
332 current_thread
->last_status
= last_status
;
338 extern int remote_debug
;
340 /* Decode a qXfer read request. Return 0 if everything looks OK,
344 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
346 /* After the read marker and annex, qXfer looks like a
347 traditional 'm' packet. */
348 decode_m_packet (buf
, ofs
, len
);
354 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
356 /* Extract and NUL-terminate the object. */
358 while (*buf
&& *buf
!= ':')
364 /* Extract and NUL-terminate the read/write action. */
366 while (*buf
&& *buf
!= ':')
372 /* Extract and NUL-terminate the annex. */
374 while (*buf
&& *buf
!= ':')
384 /* Write the response to a successful qXfer read. Returns the
385 length of the (binary) data stored in BUF, corresponding
386 to as much of DATA/LEN as we could fit. IS_MORE controls
387 the first character of the response. */
389 write_qxfer_response (char *buf
, const gdb_byte
*data
, int len
, int is_more
)
398 return remote_escape_output (data
, len
, 1, (unsigned char *) buf
+ 1,
399 &out_len
, PBUFSIZ
- 2) + 1;
402 /* Handle btrace enabling in BTS format. */
405 handle_btrace_enable_bts (struct thread_info
*thread
)
407 if (thread
->btrace
!= NULL
)
408 return "E.Btrace already enabled.";
410 current_btrace_conf
.format
= BTRACE_FORMAT_BTS
;
411 thread
->btrace
= target_enable_btrace (thread
->entry
.id
,
412 ¤t_btrace_conf
);
413 if (thread
->btrace
== NULL
)
414 return "E.Could not enable btrace.";
419 /* Handle btrace enabling in Intel(R) Processor Trace format. */
422 handle_btrace_enable_pt (struct thread_info
*thread
)
424 if (thread
->btrace
!= NULL
)
425 return "E.Btrace already enabled.";
427 current_btrace_conf
.format
= BTRACE_FORMAT_PT
;
428 thread
->btrace
= target_enable_btrace (thread
->entry
.id
,
429 ¤t_btrace_conf
);
430 if (thread
->btrace
== NULL
)
431 return "E.Could not enable btrace.";
436 /* Handle btrace disabling. */
439 handle_btrace_disable (struct thread_info
*thread
)
442 if (thread
->btrace
== NULL
)
443 return "E.Branch tracing not enabled.";
445 if (target_disable_btrace (thread
->btrace
) != 0)
446 return "E.Could not disable branch tracing.";
448 thread
->btrace
= NULL
;
452 /* Handle the "Qbtrace" packet. */
455 handle_btrace_general_set (char *own_buf
)
457 struct thread_info
*thread
;
461 if (!startswith (own_buf
, "Qbtrace:"))
464 op
= own_buf
+ strlen ("Qbtrace:");
466 if (ptid_equal (general_thread
, null_ptid
)
467 || ptid_equal (general_thread
, minus_one_ptid
))
469 strcpy (own_buf
, "E.Must select a single thread.");
473 thread
= find_thread_ptid (general_thread
);
476 strcpy (own_buf
, "E.No such thread.");
482 if (strcmp (op
, "bts") == 0)
483 err
= handle_btrace_enable_bts (thread
);
484 else if (strcmp (op
, "pt") == 0)
485 err
= handle_btrace_enable_pt (thread
);
486 else if (strcmp (op
, "off") == 0)
487 err
= handle_btrace_disable (thread
);
489 err
= "E.Bad Qbtrace operation. Use bts, pt, or off.";
492 strcpy (own_buf
, err
);
499 /* Handle the "Qbtrace-conf" packet. */
502 handle_btrace_conf_general_set (char *own_buf
)
504 struct thread_info
*thread
;
507 if (!startswith (own_buf
, "Qbtrace-conf:"))
510 op
= own_buf
+ strlen ("Qbtrace-conf:");
512 if (ptid_equal (general_thread
, null_ptid
)
513 || ptid_equal (general_thread
, minus_one_ptid
))
515 strcpy (own_buf
, "E.Must select a single thread.");
519 thread
= find_thread_ptid (general_thread
);
522 strcpy (own_buf
, "E.No such thread.");
526 if (startswith (op
, "bts:size="))
532 size
= strtoul (op
+ strlen ("bts:size="), &endp
, 16);
533 if (endp
== NULL
|| *endp
!= 0 || errno
!= 0 || size
> UINT_MAX
)
535 strcpy (own_buf
, "E.Bad size value.");
539 current_btrace_conf
.bts
.size
= (unsigned int) size
;
541 else if (strncmp (op
, "pt:size=", strlen ("pt:size=")) == 0)
547 size
= strtoul (op
+ strlen ("pt:size="), &endp
, 16);
548 if (endp
== NULL
|| *endp
!= 0 || errno
!= 0 || size
> UINT_MAX
)
550 strcpy (own_buf
, "E.Bad size value.");
554 current_btrace_conf
.pt
.size
= (unsigned int) size
;
558 strcpy (own_buf
, "E.Bad Qbtrace configuration option.");
566 /* Handle all of the extended 'Q' packets. */
569 handle_general_set (char *own_buf
)
571 if (startswith (own_buf
, "QPassSignals:"))
573 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
574 const char *p
= own_buf
+ strlen ("QPassSignals:");
577 p
= decode_address_to_semicolon (&cursig
, p
);
578 for (i
= 0; i
< numsigs
; i
++)
584 /* Keep looping, to clear the remaining signals. */
587 p
= decode_address_to_semicolon (&cursig
, p
);
592 strcpy (own_buf
, "OK");
596 if (startswith (own_buf
, "QProgramSignals:"))
598 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
599 const char *p
= own_buf
+ strlen ("QProgramSignals:");
602 program_signals_p
= 1;
604 p
= decode_address_to_semicolon (&cursig
, p
);
605 for (i
= 0; i
< numsigs
; i
++)
609 program_signals
[i
] = 1;
611 /* Keep looping, to clear the remaining signals. */
614 p
= decode_address_to_semicolon (&cursig
, p
);
617 program_signals
[i
] = 0;
619 strcpy (own_buf
, "OK");
623 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
627 fprintf (stderr
, "[noack mode enabled]\n");
636 if (startswith (own_buf
, "QNonStop:"))
638 char *mode
= own_buf
+ 9;
642 if (strcmp (mode
, "0") == 0)
644 else if (strcmp (mode
, "1") == 0)
648 /* We don't know what this mode is, so complain to
650 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
656 req_str
= req
? "non-stop" : "all-stop";
657 if (start_non_stop (req
) != 0)
659 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
667 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
673 if (startswith (own_buf
, "QDisableRandomization:"))
675 char *packet
= own_buf
+ strlen ("QDisableRandomization:");
678 unpack_varlen_hex (packet
, &setting
);
679 disable_randomization
= setting
;
683 if (disable_randomization
)
684 fprintf (stderr
, "[address space randomization disabled]\n");
686 fprintf (stderr
, "[address space randomization enabled]\n");
693 if (target_supports_tracepoints ()
694 && handle_tracepoint_general_set (own_buf
))
697 if (startswith (own_buf
, "QAgent:"))
699 char *mode
= own_buf
+ strlen ("QAgent:");
702 if (strcmp (mode
, "0") == 0)
704 else if (strcmp (mode
, "1") == 0)
708 /* We don't know what this value is, so complain to GDB. */
709 sprintf (own_buf
, "E.Unknown QAgent value");
713 /* Update the flag. */
716 fprintf (stderr
, "[%s agent]\n", req
? "Enable" : "Disable");
721 if (handle_btrace_general_set (own_buf
))
724 if (handle_btrace_conf_general_set (own_buf
))
727 if (startswith (own_buf
, "QThreadEvents:"))
729 char *mode
= own_buf
+ strlen ("QThreadEvents:");
730 enum tribool req
= TRIBOOL_UNKNOWN
;
732 if (strcmp (mode
, "0") == 0)
734 else if (strcmp (mode
, "1") == 0)
738 char *mode_copy
= xstrdup (mode
);
740 /* We don't know what this mode is, so complain to GDB. */
741 sprintf (own_buf
, "E.Unknown thread-events mode requested: %s\n",
747 report_thread_events
= (req
== TRIBOOL_TRUE
);
751 const char *req_str
= report_thread_events
? "enabled" : "disabled";
753 fprintf (stderr
, "[thread events are now %s]\n", req_str
);
760 /* Otherwise we didn't know what packet it was. Say we didn't
766 get_features_xml (const char *annex
)
768 const struct target_desc
*desc
= current_target_desc ();
770 /* `desc->xmltarget' defines what to return when looking for the
771 "target.xml" file. Its contents can either be verbatim XML code
772 (prefixed with a '@') or else the name of the actual XML file to
773 be used in place of "target.xml".
775 This variable is set up from the auto-generated
776 init_registers_... routine for the current target. */
778 if (desc
->xmltarget
!= NULL
&& strcmp (annex
, "target.xml") == 0)
780 if (*desc
->xmltarget
== '@')
781 return desc
->xmltarget
+ 1;
783 annex
= desc
->xmltarget
;
788 extern const char *const xml_builtin
[][2];
791 /* Look for the annex. */
792 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
793 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
796 if (xml_builtin
[i
][0] != NULL
)
797 return xml_builtin
[i
][1];
805 monitor_show_help (void)
807 monitor_output ("The following monitor commands are supported:\n");
808 monitor_output (" set debug <0|1>\n");
809 monitor_output (" Enable general debugging messages\n");
810 monitor_output (" set debug-hw-points <0|1>\n");
811 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
812 monitor_output (" set remote-debug <0|1>\n");
813 monitor_output (" Enable remote protocol debugging messages\n");
814 monitor_output (" set debug-format option1[,option2,...]\n");
815 monitor_output (" Add additional information to debugging messages\n");
816 monitor_output (" Options: all, none");
817 monitor_output (", timestamp");
818 monitor_output ("\n");
819 monitor_output (" exit\n");
820 monitor_output (" Quit GDBserver\n");
823 /* Read trace frame or inferior memory. Returns the number of bytes
824 actually read, zero when no further transfer is possible, and -1 on
825 error. Return of a positive value smaller than LEN does not
826 indicate there's no more to be read, only the end of the transfer.
827 E.g., when GDB reads memory from a traceframe, a first request may
828 be served from a memory block that does not cover the whole request
829 length. A following request gets the rest served from either
830 another block (of the same traceframe) or from the read-only
834 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
838 if (current_traceframe
>= 0)
841 ULONGEST length
= len
;
843 if (traceframe_read_mem (current_traceframe
,
844 memaddr
, myaddr
, len
, &nbytes
))
846 /* Data read from trace buffer, we're done. */
849 if (!in_readonly_region (memaddr
, length
))
851 /* Otherwise we have a valid readonly case, fall through. */
852 /* (assume no half-trace half-real blocks for now) */
855 res
= prepare_to_access_memory ();
858 if (set_desired_thread (1))
859 res
= read_inferior_memory (memaddr
, myaddr
, len
);
862 done_accessing_memory ();
864 return res
== 0 ? len
: -1;
870 /* Write trace frame or inferior memory. Actually, writing to trace
871 frames is forbidden. */
874 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
876 if (current_traceframe
>= 0)
882 ret
= prepare_to_access_memory ();
885 if (set_desired_thread (1))
886 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
889 done_accessing_memory ();
895 /* Subroutine of handle_search_memory to simplify it. */
898 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
899 gdb_byte
*pattern
, unsigned pattern_len
,
900 gdb_byte
*search_buf
,
901 unsigned chunk_size
, unsigned search_buf_size
,
902 CORE_ADDR
*found_addrp
)
904 /* Prime the search buffer. */
906 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
909 warning ("Unable to access %ld bytes of target "
910 "memory at 0x%lx, halting search.",
911 (long) search_buf_size
, (long) start_addr
);
915 /* Perform the search.
917 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
918 When we've scanned N bytes we copy the trailing bytes to the start and
919 read in another N bytes. */
921 while (search_space_len
>= pattern_len
)
924 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
928 found_ptr
= (gdb_byte
*) memmem (search_buf
, nr_search_bytes
, pattern
,
931 if (found_ptr
!= NULL
)
933 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
934 *found_addrp
= found_addr
;
938 /* Not found in this chunk, skip to next chunk. */
940 /* Don't let search_space_len wrap here, it's unsigned. */
941 if (search_space_len
>= chunk_size
)
942 search_space_len
-= chunk_size
;
944 search_space_len
= 0;
946 if (search_space_len
>= pattern_len
)
948 unsigned keep_len
= search_buf_size
- chunk_size
;
949 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
952 /* Copy the trailing part of the previous iteration to the front
953 of the buffer for the next iteration. */
954 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
956 nr_to_read
= (search_space_len
- keep_len
< chunk_size
957 ? search_space_len
- keep_len
960 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
961 nr_to_read
) != search_buf_size
)
963 warning ("Unable to access %ld bytes of target memory "
964 "at 0x%lx, halting search.",
965 (long) nr_to_read
, (long) read_addr
);
969 start_addr
+= chunk_size
;
978 /* Handle qSearch:memory packets. */
981 handle_search_memory (char *own_buf
, int packet_len
)
983 CORE_ADDR start_addr
;
984 CORE_ADDR search_space_len
;
986 unsigned int pattern_len
;
987 /* NOTE: also defined in find.c testcase. */
988 #define SEARCH_CHUNK_SIZE 16000
989 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
990 /* Buffer to hold memory contents for searching. */
991 gdb_byte
*search_buf
;
992 unsigned search_buf_size
;
994 CORE_ADDR found_addr
;
995 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
997 pattern
= (gdb_byte
*) malloc (packet_len
);
1000 error ("Unable to allocate memory to perform the search");
1001 strcpy (own_buf
, "E00");
1004 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
1005 packet_len
- cmd_name_len
,
1006 &start_addr
, &search_space_len
,
1007 pattern
, &pattern_len
) < 0)
1010 error ("Error in parsing qSearch:memory packet");
1011 strcpy (own_buf
, "E00");
1015 search_buf_size
= chunk_size
+ pattern_len
- 1;
1017 /* No point in trying to allocate a buffer larger than the search space. */
1018 if (search_space_len
< search_buf_size
)
1019 search_buf_size
= search_space_len
;
1021 search_buf
= (gdb_byte
*) malloc (search_buf_size
);
1022 if (search_buf
== NULL
)
1025 error ("Unable to allocate memory to perform the search");
1026 strcpy (own_buf
, "E00");
1030 found
= handle_search_memory_1 (start_addr
, search_space_len
,
1031 pattern
, pattern_len
,
1032 search_buf
, chunk_size
, search_buf_size
,
1036 sprintf (own_buf
, "1,%lx", (long) found_addr
);
1037 else if (found
== 0)
1038 strcpy (own_buf
, "0");
1040 strcpy (own_buf
, "E00");
1046 #define require_running(BUF) \
1047 if (!target_running ()) \
1053 /* Parse options to --debug-format= and "monitor set debug-format".
1054 ARG is the text after "--debug-format=" or "monitor set debug-format".
1055 IS_MONITOR is non-zero if we're invoked via "monitor set debug-format".
1056 This triggers calls to monitor_output.
1057 The result is NULL if all options were parsed ok, otherwise an error
1058 message which the caller must free.
1060 N.B. These commands affect all debug format settings, they are not
1061 cumulative. If a format is not specified, it is turned off.
1062 However, we don't go to extra trouble with things like
1063 "monitor set debug-format all,none,timestamp".
1064 Instead we just parse them one at a time, in order.
1066 The syntax for "monitor set debug" we support here is not identical
1067 to gdb's "set debug foo on|off" because we also use this function to
1068 parse "--debug-format=foo,bar". */
1071 parse_debug_format_options (const char *arg
, int is_monitor
)
1073 VEC (char_ptr
) *options
;
1077 /* First turn all debug format options off. */
1078 debug_timestamp
= 0;
1080 /* First remove leading spaces, for "monitor set debug-format". */
1081 while (isspace (*arg
))
1084 options
= delim_string_to_char_ptr_vec (arg
, ',');
1086 for (ix
= 0; VEC_iterate (char_ptr
, options
, ix
, option
); ++ix
)
1088 if (strcmp (option
, "all") == 0)
1090 debug_timestamp
= 1;
1092 monitor_output ("All extra debug format options enabled.\n");
1094 else if (strcmp (option
, "none") == 0)
1096 debug_timestamp
= 0;
1098 monitor_output ("All extra debug format options disabled.\n");
1100 else if (strcmp (option
, "timestamp") == 0)
1102 debug_timestamp
= 1;
1104 monitor_output ("Timestamps will be added to debug output.\n");
1106 else if (*option
== '\0')
1108 /* An empty option, e.g., "--debug-format=foo,,bar", is ignored. */
1113 char *msg
= xstrprintf ("Unknown debug-format argument: \"%s\"\n",
1116 free_char_ptr_vec (options
);
1121 free_char_ptr_vec (options
);
1125 /* Handle monitor commands not handled by target-specific handlers. */
1128 handle_monitor_command (char *mon
, char *own_buf
)
1130 if (strcmp (mon
, "set debug 1") == 0)
1133 monitor_output ("Debug output enabled.\n");
1135 else if (strcmp (mon
, "set debug 0") == 0)
1138 monitor_output ("Debug output disabled.\n");
1140 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
1142 show_debug_regs
= 1;
1143 monitor_output ("H/W point debugging output enabled.\n");
1145 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
1147 show_debug_regs
= 0;
1148 monitor_output ("H/W point debugging output disabled.\n");
1150 else if (strcmp (mon
, "set remote-debug 1") == 0)
1153 monitor_output ("Protocol debug output enabled.\n");
1155 else if (strcmp (mon
, "set remote-debug 0") == 0)
1158 monitor_output ("Protocol debug output disabled.\n");
1160 else if (startswith (mon
, "set debug-format "))
1163 = parse_debug_format_options (mon
+ sizeof ("set debug-format ") - 1,
1166 if (error_msg
!= NULL
)
1168 monitor_output (error_msg
);
1169 monitor_show_help ();
1170 write_enn (own_buf
);
1174 else if (strcmp (mon
, "help") == 0)
1175 monitor_show_help ();
1176 else if (strcmp (mon
, "exit") == 0)
1180 monitor_output ("Unknown monitor command.\n\n");
1181 monitor_show_help ();
1182 write_enn (own_buf
);
1186 /* Associates a callback with each supported qXfer'able object. */
1190 /* The object this handler handles. */
1193 /* Request that the target transfer up to LEN 8-bit bytes of the
1194 target's OBJECT. The OFFSET, for a seekable object, specifies
1195 the starting point. The ANNEX can be used to provide additional
1196 data-specific information to the target.
1198 Return the number of bytes actually transfered, zero when no
1199 further transfer is possible, -1 on error, -2 when the transfer
1200 is not supported, and -3 on a verbose error message that should
1201 be preserved. Return of a positive value smaller than LEN does
1202 not indicate the end of the object, only the end of the transfer.
1204 One, and only one, of readbuf or writebuf must be non-NULL. */
1205 int (*xfer
) (const char *annex
,
1206 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1207 ULONGEST offset
, LONGEST len
);
1210 /* Handle qXfer:auxv:read. */
1213 handle_qxfer_auxv (const char *annex
,
1214 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1215 ULONGEST offset
, LONGEST len
)
1217 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
1220 if (annex
[0] != '\0' || current_thread
== NULL
)
1223 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
1226 /* Handle qXfer:exec-file:read. */
1229 handle_qxfer_exec_file (const char *const_annex
,
1230 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1231 ULONGEST offset
, LONGEST len
)
1237 if (the_target
->pid_to_exec_file
== NULL
|| writebuf
!= NULL
)
1240 if (const_annex
[0] == '\0')
1242 if (current_thread
== NULL
)
1245 pid
= pid_of (current_thread
);
1249 char *annex
= (char *) alloca (strlen (const_annex
) + 1);
1251 strcpy (annex
, const_annex
);
1252 annex
= unpack_varlen_hex (annex
, &pid
);
1254 if (annex
[0] != '\0')
1261 file
= (*the_target
->pid_to_exec_file
) (pid
);
1265 total_len
= strlen (file
);
1267 if (offset
> total_len
)
1270 if (offset
+ len
> total_len
)
1271 len
= total_len
- offset
;
1273 memcpy (readbuf
, file
+ offset
, len
);
1277 /* Handle qXfer:features:read. */
1280 handle_qxfer_features (const char *annex
,
1281 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1282 ULONGEST offset
, LONGEST len
)
1284 const char *document
;
1287 if (writebuf
!= NULL
)
1290 if (!target_running ())
1293 /* Grab the correct annex. */
1294 document
= get_features_xml (annex
);
1295 if (document
== NULL
)
1298 total_len
= strlen (document
);
1300 if (offset
> total_len
)
1303 if (offset
+ len
> total_len
)
1304 len
= total_len
- offset
;
1306 memcpy (readbuf
, document
+ offset
, len
);
1310 /* Worker routine for handle_qxfer_libraries.
1311 Add to the length pointed to by ARG a conservative estimate of the
1312 length needed to transmit the file name of INF. */
1315 accumulate_file_name_length (struct inferior_list_entry
*inf
, void *arg
)
1317 struct dll_info
*dll
= (struct dll_info
*) inf
;
1318 unsigned int *total_len
= (unsigned int *) arg
;
1320 /* Over-estimate the necessary memory. Assume that every character
1321 in the library name must be escaped. */
1322 *total_len
+= 128 + 6 * strlen (dll
->name
);
1325 /* Worker routine for handle_qxfer_libraries.
1326 Emit the XML to describe the library in INF. */
1329 emit_dll_description (struct inferior_list_entry
*inf
, void *arg
)
1331 struct dll_info
*dll
= (struct dll_info
*) inf
;
1332 char **p_ptr
= (char **) arg
;
1336 strcpy (p
, " <library name=\"");
1338 name
= xml_escape_text (dll
->name
);
1342 strcpy (p
, "\"><segment address=\"");
1344 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1346 strcpy (p
, "\"/></library>\n");
1352 /* Handle qXfer:libraries:read. */
1355 handle_qxfer_libraries (const char *annex
,
1356 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1357 ULONGEST offset
, LONGEST len
)
1359 unsigned int total_len
;
1362 if (writebuf
!= NULL
)
1365 if (annex
[0] != '\0' || current_thread
== NULL
)
1369 for_each_inferior_with_data (&all_dlls
, accumulate_file_name_length
,
1372 document
= (char *) malloc (total_len
);
1373 if (document
== NULL
)
1376 strcpy (document
, "<library-list version=\"1.0\">\n");
1377 p
= document
+ strlen (document
);
1379 for_each_inferior_with_data (&all_dlls
, emit_dll_description
, &p
);
1381 strcpy (p
, "</library-list>\n");
1383 total_len
= strlen (document
);
1385 if (offset
> total_len
)
1391 if (offset
+ len
> total_len
)
1392 len
= total_len
- offset
;
1394 memcpy (readbuf
, document
+ offset
, len
);
1399 /* Handle qXfer:libraries-svr4:read. */
1402 handle_qxfer_libraries_svr4 (const char *annex
,
1403 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1404 ULONGEST offset
, LONGEST len
)
1406 if (writebuf
!= NULL
)
1409 if (current_thread
== NULL
|| the_target
->qxfer_libraries_svr4
== NULL
)
1412 return the_target
->qxfer_libraries_svr4 (annex
, readbuf
, writebuf
, offset
, len
);
1415 /* Handle qXfer:osadata:read. */
1418 handle_qxfer_osdata (const char *annex
,
1419 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1420 ULONGEST offset
, LONGEST len
)
1422 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
1425 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
1428 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1431 handle_qxfer_siginfo (const char *annex
,
1432 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1433 ULONGEST offset
, LONGEST len
)
1435 if (the_target
->qxfer_siginfo
== NULL
)
1438 if (annex
[0] != '\0' || current_thread
== NULL
)
1441 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1444 /* Handle qXfer:spu:read and qXfer:spu:write. */
1447 handle_qxfer_spu (const char *annex
,
1448 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1449 ULONGEST offset
, LONGEST len
)
1451 if (the_target
->qxfer_spu
== NULL
)
1454 if (current_thread
== NULL
)
1457 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1460 /* Handle qXfer:statictrace:read. */
1463 handle_qxfer_statictrace (const char *annex
,
1464 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1465 ULONGEST offset
, LONGEST len
)
1469 if (writebuf
!= NULL
)
1472 if (annex
[0] != '\0' || current_thread
== NULL
|| current_traceframe
== -1)
1475 if (traceframe_read_sdata (current_traceframe
, offset
,
1476 readbuf
, len
, &nbytes
))
1481 /* Helper for handle_qxfer_threads_proper.
1482 Emit the XML to describe the thread of INF. */
1485 handle_qxfer_threads_worker (struct inferior_list_entry
*inf
, void *arg
)
1487 struct thread_info
*thread
= (struct thread_info
*) inf
;
1488 struct buffer
*buffer
= (struct buffer
*) arg
;
1489 ptid_t ptid
= thread_to_gdb_id (thread
);
1491 int core
= target_core_of_thread (ptid
);
1493 const char *name
= target_thread_name (ptid
);
1495 write_ptid (ptid_s
, ptid
);
1497 buffer_xml_printf (buffer
, "<thread id=\"%s\"", ptid_s
);
1501 sprintf (core_s
, "%d", core
);
1502 buffer_xml_printf (buffer
, " core=\"%s\"", core_s
);
1506 buffer_xml_printf (buffer
, " name=\"%s\"", name
);
1508 buffer_xml_printf (buffer
, "/>\n");
1511 /* Helper for handle_qxfer_threads. */
1514 handle_qxfer_threads_proper (struct buffer
*buffer
)
1516 buffer_grow_str (buffer
, "<threads>\n");
1518 for_each_inferior_with_data (&all_threads
, handle_qxfer_threads_worker
,
1521 buffer_grow_str0 (buffer
, "</threads>\n");
1524 /* Handle qXfer:threads:read. */
1527 handle_qxfer_threads (const char *annex
,
1528 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1529 ULONGEST offset
, LONGEST len
)
1531 static char *result
= 0;
1532 static unsigned int result_length
= 0;
1534 if (writebuf
!= NULL
)
1537 if (annex
[0] != '\0')
1542 struct buffer buffer
;
1543 /* When asked for data at offset 0, generate everything and store into
1544 'result'. Successive reads will be served off 'result'. */
1548 buffer_init (&buffer
);
1550 handle_qxfer_threads_proper (&buffer
);
1552 result
= buffer_finish (&buffer
);
1553 result_length
= strlen (result
);
1554 buffer_free (&buffer
);
1557 if (offset
>= result_length
)
1559 /* We're out of data. */
1566 if (len
> result_length
- offset
)
1567 len
= result_length
- offset
;
1569 memcpy (readbuf
, result
+ offset
, len
);
1574 /* Handle qXfer:traceframe-info:read. */
1577 handle_qxfer_traceframe_info (const char *annex
,
1578 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1579 ULONGEST offset
, LONGEST len
)
1581 static char *result
= 0;
1582 static unsigned int result_length
= 0;
1584 if (writebuf
!= NULL
)
1587 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1592 struct buffer buffer
;
1594 /* When asked for data at offset 0, generate everything and
1595 store into 'result'. Successive reads will be served off
1599 buffer_init (&buffer
);
1601 traceframe_read_info (current_traceframe
, &buffer
);
1603 result
= buffer_finish (&buffer
);
1604 result_length
= strlen (result
);
1605 buffer_free (&buffer
);
1608 if (offset
>= result_length
)
1610 /* We're out of data. */
1617 if (len
> result_length
- offset
)
1618 len
= result_length
- offset
;
1620 memcpy (readbuf
, result
+ offset
, len
);
1624 /* Handle qXfer:fdpic:read. */
1627 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1628 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1630 if (the_target
->read_loadmap
== NULL
)
1633 if (current_thread
== NULL
)
1636 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1639 /* Handle qXfer:btrace:read. */
1642 handle_qxfer_btrace (const char *annex
,
1643 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1644 ULONGEST offset
, LONGEST len
)
1646 static struct buffer cache
;
1647 struct thread_info
*thread
;
1648 enum btrace_read_type type
;
1651 if (the_target
->read_btrace
== NULL
|| writebuf
!= NULL
)
1654 if (ptid_equal (general_thread
, null_ptid
)
1655 || ptid_equal (general_thread
, minus_one_ptid
))
1657 strcpy (own_buf
, "E.Must select a single thread.");
1661 thread
= find_thread_ptid (general_thread
);
1664 strcpy (own_buf
, "E.No such thread.");
1668 if (thread
->btrace
== NULL
)
1670 strcpy (own_buf
, "E.Btrace not enabled.");
1674 if (strcmp (annex
, "all") == 0)
1675 type
= BTRACE_READ_ALL
;
1676 else if (strcmp (annex
, "new") == 0)
1677 type
= BTRACE_READ_NEW
;
1678 else if (strcmp (annex
, "delta") == 0)
1679 type
= BTRACE_READ_DELTA
;
1682 strcpy (own_buf
, "E.Bad annex.");
1688 buffer_free (&cache
);
1690 result
= target_read_btrace (thread
->btrace
, &cache
, type
);
1693 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1697 else if (offset
> cache
.used_size
)
1699 buffer_free (&cache
);
1703 if (len
> cache
.used_size
- offset
)
1704 len
= cache
.used_size
- offset
;
1706 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1711 /* Handle qXfer:btrace-conf:read. */
1714 handle_qxfer_btrace_conf (const char *annex
,
1715 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1716 ULONGEST offset
, LONGEST len
)
1718 static struct buffer cache
;
1719 struct thread_info
*thread
;
1722 if (the_target
->read_btrace_conf
== NULL
|| writebuf
!= NULL
)
1725 if (annex
[0] != '\0')
1728 if (ptid_equal (general_thread
, null_ptid
)
1729 || ptid_equal (general_thread
, minus_one_ptid
))
1731 strcpy (own_buf
, "E.Must select a single thread.");
1735 thread
= find_thread_ptid (general_thread
);
1738 strcpy (own_buf
, "E.No such thread.");
1742 if (thread
->btrace
== NULL
)
1744 strcpy (own_buf
, "E.Btrace not enabled.");
1750 buffer_free (&cache
);
1752 result
= target_read_btrace_conf (thread
->btrace
, &cache
);
1755 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1759 else if (offset
> cache
.used_size
)
1761 buffer_free (&cache
);
1765 if (len
> cache
.used_size
- offset
)
1766 len
= cache
.used_size
- offset
;
1768 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1773 static const struct qxfer qxfer_packets
[] =
1775 { "auxv", handle_qxfer_auxv
},
1776 { "btrace", handle_qxfer_btrace
},
1777 { "btrace-conf", handle_qxfer_btrace_conf
},
1778 { "exec-file", handle_qxfer_exec_file
},
1779 { "fdpic", handle_qxfer_fdpic
},
1780 { "features", handle_qxfer_features
},
1781 { "libraries", handle_qxfer_libraries
},
1782 { "libraries-svr4", handle_qxfer_libraries_svr4
},
1783 { "osdata", handle_qxfer_osdata
},
1784 { "siginfo", handle_qxfer_siginfo
},
1785 { "spu", handle_qxfer_spu
},
1786 { "statictrace", handle_qxfer_statictrace
},
1787 { "threads", handle_qxfer_threads
},
1788 { "traceframe-info", handle_qxfer_traceframe_info
},
1792 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1800 if (!startswith (own_buf
, "qXfer:"))
1803 /* Grab the object, r/w and annex. */
1804 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1806 write_enn (own_buf
);
1811 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1814 const struct qxfer
*q
= &qxfer_packets
[i
];
1816 if (strcmp (object
, q
->object
) == 0)
1818 if (strcmp (rw
, "read") == 0)
1820 unsigned char *data
;
1825 /* Grab the offset and length. */
1826 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1828 write_enn (own_buf
);
1832 /* Read one extra byte, as an indicator of whether there is
1834 if (len
> PBUFSIZ
- 2)
1836 data
= (unsigned char *) malloc (len
+ 1);
1839 write_enn (own_buf
);
1842 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1850 /* Preserve error message. */
1853 write_enn (own_buf
);
1855 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1857 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1862 else if (strcmp (rw
, "write") == 0)
1867 unsigned char *data
;
1869 strcpy (own_buf
, "E00");
1870 data
= (unsigned char *) malloc (packet_len
- (offset
- own_buf
));
1873 write_enn (own_buf
);
1876 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1877 &ofs
, &len
, data
) < 0)
1880 write_enn (own_buf
);
1884 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1892 /* Preserve error message. */
1895 write_enn (own_buf
);
1897 sprintf (own_buf
, "%x", n
);
1910 /* Table used by the crc32 function to calcuate the checksum. */
1912 static unsigned int crc32_table
[256] =
1915 /* Compute 32 bit CRC from inferior memory.
1917 On success, return 32 bit CRC.
1918 On failure, return (unsigned long long) -1. */
1920 static unsigned long long
1921 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1923 if (!crc32_table
[1])
1925 /* Initialize the CRC table and the decoding table. */
1929 for (i
= 0; i
< 256; i
++)
1931 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1932 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1939 unsigned char byte
= 0;
1941 /* Return failure if memory read fails. */
1942 if (read_inferior_memory (base
, &byte
, 1) != 0)
1943 return (unsigned long long) -1;
1945 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1948 return (unsigned long long) crc
;
1951 /* Add supported btrace packets to BUF. */
1954 supported_btrace_packets (char *buf
)
1956 int btrace_supported
= 0;
1958 if (target_supports_btrace (BTRACE_FORMAT_BTS
))
1960 strcat (buf
, ";Qbtrace:bts+");
1961 strcat (buf
, ";Qbtrace-conf:bts:size+");
1963 btrace_supported
= 1;
1966 if (target_supports_btrace (BTRACE_FORMAT_PT
))
1968 strcat (buf
, ";Qbtrace:pt+");
1969 strcat (buf
, ";Qbtrace-conf:pt:size+");
1971 btrace_supported
= 1;
1974 if (!btrace_supported
)
1977 strcat (buf
, ";Qbtrace:off+");
1978 strcat (buf
, ";qXfer:btrace:read+");
1979 strcat (buf
, ";qXfer:btrace-conf:read+");
1982 /* Handle all of the extended 'q' packets. */
1985 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1987 static struct inferior_list_entry
*thread_ptr
;
1989 /* Reply the current thread id. */
1990 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1993 require_running (own_buf
);
1995 if (!ptid_equal (general_thread
, null_ptid
)
1996 && !ptid_equal (general_thread
, minus_one_ptid
))
1997 gdb_id
= general_thread
;
2000 thread_ptr
= get_first_inferior (&all_threads
);
2001 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
2004 sprintf (own_buf
, "QC");
2006 write_ptid (own_buf
, gdb_id
);
2010 if (strcmp ("qSymbol::", own_buf
) == 0)
2012 struct thread_info
*save_thread
= current_thread
;
2014 /* For qSymbol, GDB only changes the current thread if the
2015 previous current thread was of a different process. So if
2016 the previous thread is gone, we need to pick another one of
2017 the same process. This can happen e.g., if we followed an
2018 exec in a non-leader thread. */
2019 if (current_thread
== NULL
)
2022 = find_any_thread_of_pid (ptid_get_pid (general_thread
));
2024 /* Just in case, if we didn't find a thread, then bail out
2025 instead of crashing. */
2026 if (current_thread
== NULL
)
2028 write_enn (own_buf
);
2029 current_thread
= save_thread
;
2034 /* GDB is suggesting new symbols have been loaded. This may
2035 mean a new shared library has been detected as loaded, so
2036 take the opportunity to check if breakpoints we think are
2037 inserted, still are. Note that it isn't guaranteed that
2038 we'll see this when a shared library is loaded, and nor will
2039 we see this for unloads (although breakpoints in unloaded
2040 libraries shouldn't trigger), as GDB may not find symbols for
2041 the library at all. We also re-validate breakpoints when we
2042 see a second GDB breakpoint for the same address, and or when
2043 we access breakpoint shadows. */
2044 validate_breakpoints ();
2046 if (target_supports_tracepoints ())
2047 tracepoint_look_up_symbols ();
2049 if (current_thread
!= NULL
&& the_target
->look_up_symbols
!= NULL
)
2050 (*the_target
->look_up_symbols
) ();
2052 current_thread
= save_thread
;
2054 strcpy (own_buf
, "OK");
2058 if (!disable_packet_qfThreadInfo
)
2060 if (strcmp ("qfThreadInfo", own_buf
) == 0)
2064 require_running (own_buf
);
2065 thread_ptr
= get_first_inferior (&all_threads
);
2068 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
2069 write_ptid (own_buf
, gdb_id
);
2070 thread_ptr
= thread_ptr
->next
;
2074 if (strcmp ("qsThreadInfo", own_buf
) == 0)
2078 require_running (own_buf
);
2079 if (thread_ptr
!= NULL
)
2082 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
2083 write_ptid (own_buf
, gdb_id
);
2084 thread_ptr
= thread_ptr
->next
;
2089 sprintf (own_buf
, "l");
2095 if (the_target
->read_offsets
!= NULL
2096 && strcmp ("qOffsets", own_buf
) == 0)
2098 CORE_ADDR text
, data
;
2100 require_running (own_buf
);
2101 if (the_target
->read_offsets (&text
, &data
))
2102 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
2103 (long)text
, (long)data
, (long)data
);
2105 write_enn (own_buf
);
2110 /* Protocol features query. */
2111 if (startswith (own_buf
, "qSupported")
2112 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
2114 char *p
= &own_buf
[10];
2115 int gdb_supports_qRelocInsn
= 0;
2117 /* Process each feature being provided by GDB. The first
2118 feature will follow a ':', and latter features will follow
2122 char **qsupported
= NULL
;
2127 /* Two passes, to avoid nested strtok calls in
2128 target_process_qsupported. */
2129 for (p
= strtok (p
+ 1, ";");
2131 p
= strtok (NULL
, ";"))
2134 qsupported
= XRESIZEVEC (char *, qsupported
, count
);
2135 qsupported
[count
- 1] = xstrdup (p
);
2138 for (i
= 0; i
< count
; i
++)
2141 if (strcmp (p
, "multiprocess+") == 0)
2143 /* GDB supports and wants multi-process support if
2145 if (target_supports_multi_process ())
2148 else if (strcmp (p
, "qRelocInsn+") == 0)
2150 /* GDB supports relocate instruction requests. */
2151 gdb_supports_qRelocInsn
= 1;
2153 else if (strcmp (p
, "swbreak+") == 0)
2155 /* GDB wants us to report whether a trap is caused
2156 by a software breakpoint and for us to handle PC
2157 adjustment if necessary on this target. */
2158 if (target_supports_stopped_by_sw_breakpoint ())
2159 swbreak_feature
= 1;
2161 else if (strcmp (p
, "hwbreak+") == 0)
2163 /* GDB wants us to report whether a trap is caused
2164 by a hardware breakpoint. */
2165 if (target_supports_stopped_by_hw_breakpoint ())
2166 hwbreak_feature
= 1;
2168 else if (strcmp (p
, "fork-events+") == 0)
2170 /* GDB supports and wants fork events if possible. */
2171 if (target_supports_fork_events ())
2172 report_fork_events
= 1;
2174 else if (strcmp (p
, "vfork-events+") == 0)
2176 /* GDB supports and wants vfork events if possible. */
2177 if (target_supports_vfork_events ())
2178 report_vfork_events
= 1;
2180 else if (strcmp (p
, "exec-events+") == 0)
2182 /* GDB supports and wants exec events if possible. */
2183 if (target_supports_exec_events ())
2184 report_exec_events
= 1;
2186 else if (strcmp (p
, "vContSupported+") == 0)
2187 vCont_supported
= 1;
2188 else if (strcmp (p
, "QThreadEvents+") == 0)
2192 /* Move the unknown features all together. */
2193 qsupported
[i
] = NULL
;
2194 qsupported
[unknown
] = p
;
2199 /* Give the target backend a chance to process the unknown
2201 target_process_qsupported (qsupported
, unknown
);
2203 for (i
= 0; i
< count
; i
++)
2204 free (qsupported
[i
]);
2209 "PacketSize=%x;QPassSignals+;QProgramSignals+",
2212 if (the_target
->qxfer_libraries_svr4
!= NULL
)
2213 strcat (own_buf
, ";qXfer:libraries-svr4:read+"
2214 ";augmented-libraries-svr4-read+");
2217 /* We do not have any hook to indicate whether the non-SVR4 target
2218 backend supports qXfer:libraries:read, so always report it. */
2219 strcat (own_buf
, ";qXfer:libraries:read+");
2222 if (the_target
->read_auxv
!= NULL
)
2223 strcat (own_buf
, ";qXfer:auxv:read+");
2225 if (the_target
->qxfer_spu
!= NULL
)
2226 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
2228 if (the_target
->qxfer_siginfo
!= NULL
)
2229 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
2231 if (the_target
->read_loadmap
!= NULL
)
2232 strcat (own_buf
, ";qXfer:fdpic:read+");
2234 /* We always report qXfer:features:read, as targets may
2235 install XML files on a subsequent call to arch_setup.
2236 If we reported to GDB on startup that we don't support
2237 qXfer:feature:read at all, we will never be re-queried. */
2238 strcat (own_buf
, ";qXfer:features:read+");
2240 if (transport_is_reliable
)
2241 strcat (own_buf
, ";QStartNoAckMode+");
2243 if (the_target
->qxfer_osdata
!= NULL
)
2244 strcat (own_buf
, ";qXfer:osdata:read+");
2246 if (target_supports_multi_process ())
2247 strcat (own_buf
, ";multiprocess+");
2249 if (target_supports_fork_events ())
2250 strcat (own_buf
, ";fork-events+");
2252 if (target_supports_vfork_events ())
2253 strcat (own_buf
, ";vfork-events+");
2255 if (target_supports_exec_events ())
2256 strcat (own_buf
, ";exec-events+");
2258 if (target_supports_non_stop ())
2259 strcat (own_buf
, ";QNonStop+");
2261 if (target_supports_disable_randomization ())
2262 strcat (own_buf
, ";QDisableRandomization+");
2264 strcat (own_buf
, ";qXfer:threads:read+");
2266 if (target_supports_tracepoints ())
2268 strcat (own_buf
, ";ConditionalTracepoints+");
2269 strcat (own_buf
, ";TraceStateVariables+");
2270 strcat (own_buf
, ";TracepointSource+");
2271 strcat (own_buf
, ";DisconnectedTracing+");
2272 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
2273 strcat (own_buf
, ";FastTracepoints+");
2274 strcat (own_buf
, ";StaticTracepoints+");
2275 strcat (own_buf
, ";InstallInTrace+");
2276 strcat (own_buf
, ";qXfer:statictrace:read+");
2277 strcat (own_buf
, ";qXfer:traceframe-info:read+");
2278 strcat (own_buf
, ";EnableDisableTracepoints+");
2279 strcat (own_buf
, ";QTBuffer:size+");
2280 strcat (own_buf
, ";tracenz+");
2283 if (target_supports_hardware_single_step ())
2285 /* Support target-side breakpoint conditions and commands.
2286 GDBserver needs to step over the breakpoint if the condition
2287 is false. GDBserver software single step is too simple, so
2288 disable conditional breakpoints if the target doesn't have
2289 hardware single step. */
2290 strcat (own_buf
, ";ConditionalBreakpoints+");
2292 strcat (own_buf
, ";BreakpointCommands+");
2294 if (target_supports_agent ())
2295 strcat (own_buf
, ";QAgent+");
2297 supported_btrace_packets (own_buf
);
2299 if (target_supports_stopped_by_sw_breakpoint ())
2300 strcat (own_buf
, ";swbreak+");
2302 if (target_supports_stopped_by_hw_breakpoint ())
2303 strcat (own_buf
, ";hwbreak+");
2305 if (the_target
->pid_to_exec_file
!= NULL
)
2306 strcat (own_buf
, ";qXfer:exec-file:read+");
2308 strcat (own_buf
, ";vContSupported+");
2310 strcat (own_buf
, ";QThreadEvents+");
2312 /* Reinitialize components as needed for the new connection. */
2313 hostio_handle_new_gdb_connection ();
2314 target_handle_new_gdb_connection ();
2319 /* Thread-local storage support. */
2320 if (the_target
->get_tls_address
!= NULL
2321 && startswith (own_buf
, "qGetTLSAddr:"))
2323 char *p
= own_buf
+ 12;
2324 CORE_ADDR parts
[2], address
= 0;
2326 ptid_t ptid
= null_ptid
;
2328 require_running (own_buf
);
2330 for (i
= 0; i
< 3; i
++)
2338 p2
= strchr (p
, ',');
2351 ptid
= read_ptid (p
, NULL
);
2353 decode_address (&parts
[i
- 1], p
, len
);
2357 if (p
!= NULL
|| i
< 3)
2361 struct thread_info
*thread
= find_thread_ptid (ptid
);
2366 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
2372 strcpy (own_buf
, paddress(address
));
2377 write_enn (own_buf
);
2381 /* Otherwise, pretend we do not understand this packet. */
2384 /* Windows OS Thread Information Block address support. */
2385 if (the_target
->get_tib_address
!= NULL
2386 && startswith (own_buf
, "qGetTIBAddr:"))
2391 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
2393 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
2396 strcpy (own_buf
, paddress(tlb
));
2401 write_enn (own_buf
);
2407 /* Handle "monitor" commands. */
2408 if (startswith (own_buf
, "qRcmd,"))
2410 char *mon
= (char *) malloc (PBUFSIZ
);
2411 int len
= strlen (own_buf
+ 6);
2415 write_enn (own_buf
);
2420 || hex2bin (own_buf
+ 6, (gdb_byte
*) mon
, len
/ 2) != len
/ 2)
2422 write_enn (own_buf
);
2426 mon
[len
/ 2] = '\0';
2430 if (the_target
->handle_monitor_command
== NULL
2431 || (*the_target
->handle_monitor_command
) (mon
) == 0)
2432 /* Default processing. */
2433 handle_monitor_command (mon
, own_buf
);
2439 if (startswith (own_buf
, "qSearch:memory:"))
2441 require_running (own_buf
);
2442 handle_search_memory (own_buf
, packet_len
);
2446 if (strcmp (own_buf
, "qAttached") == 0
2447 || startswith (own_buf
, "qAttached:"))
2449 struct process_info
*process
;
2451 if (own_buf
[sizeof ("qAttached") - 1])
2453 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
2454 process
= (struct process_info
*)
2455 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
2459 require_running (own_buf
);
2460 process
= current_process ();
2463 if (process
== NULL
)
2465 write_enn (own_buf
);
2469 strcpy (own_buf
, process
->attached
? "1" : "0");
2473 if (startswith (own_buf
, "qCRC:"))
2475 /* CRC check (compare-section). */
2479 unsigned long long crc
;
2481 require_running (own_buf
);
2482 comma
= unpack_varlen_hex (own_buf
+ 5, &base
);
2483 if (*comma
++ != ',')
2485 write_enn (own_buf
);
2488 len
= strtoul (comma
, NULL
, 16);
2489 crc
= crc32 (base
, len
, 0xffffffff);
2490 /* Check for memory failure. */
2491 if (crc
== (unsigned long long) -1)
2493 write_enn (own_buf
);
2496 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
2500 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
2503 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
2506 /* Otherwise we didn't know what packet it was. Say we didn't
2511 static void gdb_wants_all_threads_stopped (void);
2512 static void resume (struct thread_resume
*actions
, size_t n
);
2514 /* The callback that is passed to visit_actioned_threads. */
2515 typedef int (visit_actioned_threads_callback_ftype
)
2516 (const struct thread_resume
*, struct thread_info
*);
2518 /* Struct to pass data to visit_actioned_threads. */
2520 struct visit_actioned_threads_data
2522 const struct thread_resume
*actions
;
2524 visit_actioned_threads_callback_ftype
*callback
;
2527 /* Call CALLBACK for any thread to which ACTIONS applies to. Returns
2528 true if CALLBACK returns true. Returns false if no matching thread
2529 is found or CALLBACK results false.
2530 Note: This function is itself a callback for find_inferior. */
2533 visit_actioned_threads (struct inferior_list_entry
*entry
, void *datap
)
2535 struct visit_actioned_threads_data
*data
2536 = (struct visit_actioned_threads_data
*) datap
;
2537 const struct thread_resume
*actions
= data
->actions
;
2538 size_t num_actions
= data
->num_actions
;
2539 visit_actioned_threads_callback_ftype
*callback
= data
->callback
;
2542 for (i
= 0; i
< num_actions
; i
++)
2544 const struct thread_resume
*action
= &actions
[i
];
2546 if (ptid_equal (action
->thread
, minus_one_ptid
)
2547 || ptid_equal (action
->thread
, entry
->id
)
2548 || ((ptid_get_pid (action
->thread
)
2549 == ptid_get_pid (entry
->id
))
2550 && ptid_get_lwp (action
->thread
) == -1))
2552 struct thread_info
*thread
= (struct thread_info
*) entry
;
2554 if ((*callback
) (action
, thread
))
2562 /* Callback for visit_actioned_threads. If the thread has a pending
2563 status to report, report it now. */
2566 handle_pending_status (const struct thread_resume
*resumption
,
2567 struct thread_info
*thread
)
2569 if (thread
->status_pending_p
)
2571 thread
->status_pending_p
= 0;
2573 last_status
= thread
->last_status
;
2574 last_ptid
= thread
->entry
.id
;
2575 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2581 /* Parse vCont packets. */
2583 handle_v_cont (char *own_buf
)
2587 struct thread_resume
*resume_info
;
2588 struct thread_resume default_action
= {{0}};
2590 /* Count the number of semicolons in the packet. There should be one
2591 for every action. */
2597 p
= strchr (p
, ';');
2600 resume_info
= (struct thread_resume
*) malloc (n
* sizeof (resume_info
[0]));
2601 if (resume_info
== NULL
)
2609 memset (&resume_info
[i
], 0, sizeof resume_info
[i
]);
2611 if (p
[0] == 's' || p
[0] == 'S')
2612 resume_info
[i
].kind
= resume_step
;
2613 else if (p
[0] == 'r')
2614 resume_info
[i
].kind
= resume_step
;
2615 else if (p
[0] == 'c' || p
[0] == 'C')
2616 resume_info
[i
].kind
= resume_continue
;
2617 else if (p
[0] == 't')
2618 resume_info
[i
].kind
= resume_stop
;
2622 if (p
[0] == 'S' || p
[0] == 'C')
2625 sig
= strtol (p
+ 1, &q
, 16);
2630 if (!gdb_signal_to_host_p ((enum gdb_signal
) sig
))
2632 resume_info
[i
].sig
= gdb_signal_to_host ((enum gdb_signal
) sig
);
2634 else if (p
[0] == 'r')
2638 p
= unpack_varlen_hex (p
+ 1, &addr
);
2639 resume_info
[i
].step_range_start
= addr
;
2644 p
= unpack_varlen_hex (p
+ 1, &addr
);
2645 resume_info
[i
].step_range_end
= addr
;
2654 resume_info
[i
].thread
= minus_one_ptid
;
2655 default_action
= resume_info
[i
];
2657 /* Note: we don't increment i here, we'll overwrite this entry
2658 the next time through. */
2660 else if (p
[0] == ':')
2662 ptid_t ptid
= read_ptid (p
+ 1, &q
);
2667 if (p
[0] != ';' && p
[0] != 0)
2670 resume_info
[i
].thread
= ptid
;
2677 resume_info
[i
] = default_action
;
2679 resume (resume_info
, n
);
2684 write_enn (own_buf
);
2689 /* Resume target with ACTIONS, an array of NUM_ACTIONS elements. */
2692 resume (struct thread_resume
*actions
, size_t num_actions
)
2696 /* Check if among the threads that GDB wants actioned, there's
2697 one with a pending status to report. If so, skip actually
2698 resuming/stopping and report the pending event
2700 struct visit_actioned_threads_data data
;
2702 data
.actions
= actions
;
2703 data
.num_actions
= num_actions
;
2704 data
.callback
= handle_pending_status
;
2705 if (find_inferior (&all_threads
, visit_actioned_threads
, &data
) != NULL
)
2711 (*the_target
->resume
) (actions
, num_actions
);
2717 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2719 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
)
2721 /* No proper RSP support for this yet. At least return
2723 sprintf (own_buf
, "E.No unwaited-for children left.");
2724 disable_async_io ();
2728 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2729 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
2730 && last_status
.kind
!= TARGET_WAITKIND_NO_RESUMED
)
2731 current_thread
->last_status
= last_status
;
2733 /* From the client's perspective, all-stop mode always stops all
2734 threads implicitly (and the target backend has already done
2735 so by now). Tag all threads as "want-stopped", so we don't
2736 resume them implicitly without the client telling us to. */
2737 gdb_wants_all_threads_stopped ();
2738 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2739 disable_async_io ();
2741 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2742 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2743 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2747 /* Attach to a new program. Return 1 if successful, 0 if failure. */
2749 handle_v_attach (char *own_buf
)
2753 pid
= strtol (own_buf
+ 8, NULL
, 16);
2754 if (pid
!= 0 && attach_inferior (pid
) == 0)
2756 /* Don't report shared library events after attaching, even if
2757 some libraries are preloaded. GDB will always poll the
2758 library list. Avoids the "stopped by shared library event"
2759 notice on the GDB side. */
2764 /* In non-stop, we don't send a resume reply. Stop events
2765 will follow up using the normal notification
2770 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2776 write_enn (own_buf
);
2781 /* Run a new program. Return 1 if successful, 0 if failure. */
2783 handle_v_run (char *own_buf
)
2785 char *p
, *next_p
, **new_argv
;
2789 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
2795 new_argv
= (char **) calloc (new_argc
+ 2, sizeof (char *));
2796 if (new_argv
== NULL
)
2798 write_enn (own_buf
);
2803 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
2805 next_p
= strchr (p
, ';');
2807 next_p
= p
+ strlen (p
);
2809 if (i
== 0 && p
== next_p
)
2813 /* FIXME: Fail request if out of memory instead of dying. */
2814 new_argv
[i
] = (char *) xmalloc (1 + (next_p
- p
) / 2);
2815 hex2bin (p
, (gdb_byte
*) new_argv
[i
], (next_p
- p
) / 2);
2816 new_argv
[i
][(next_p
- p
) / 2] = '\0';
2825 if (new_argv
[0] == NULL
)
2827 /* GDB didn't specify a program to run. Use the program from the
2828 last run with the new argument list. */
2830 if (program_argv
== NULL
)
2832 write_enn (own_buf
);
2833 freeargv (new_argv
);
2837 new_argv
[0] = strdup (program_argv
[0]);
2838 if (new_argv
[0] == NULL
)
2840 write_enn (own_buf
);
2841 freeargv (new_argv
);
2846 /* Free the old argv and install the new one. */
2847 freeargv (program_argv
);
2848 program_argv
= new_argv
;
2850 start_inferior (program_argv
);
2851 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
2853 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2855 /* In non-stop, sending a resume reply doesn't set the general
2856 thread, but GDB assumes a vRun sets it (this is so GDB can
2857 query which is the main thread of the new inferior. */
2859 general_thread
= last_ptid
;
2865 write_enn (own_buf
);
2870 /* Kill process. Return 1 if successful, 0 if failure. */
2872 handle_v_kill (char *own_buf
)
2875 char *p
= &own_buf
[6];
2877 pid
= strtol (p
, NULL
, 16);
2880 if (pid
!= 0 && kill_inferior (pid
) == 0)
2882 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2883 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
2884 last_ptid
= pid_to_ptid (pid
);
2885 discard_queued_stop_replies (last_ptid
);
2891 write_enn (own_buf
);
2896 /* Handle all of the extended 'v' packets. */
2898 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2900 if (!disable_packet_vCont
)
2902 if (strcmp (own_buf
, "vCtrlC") == 0)
2904 (*the_target
->request_interrupt
) ();
2909 if (startswith (own_buf
, "vCont;"))
2911 require_running (own_buf
);
2912 handle_v_cont (own_buf
);
2916 if (startswith (own_buf
, "vCont?"))
2918 strcpy (own_buf
, "vCont;c;C;t");
2920 if (target_supports_hardware_single_step () || !vCont_supported
)
2922 /* If target supports hardware single step, add actions s
2923 and S to the list of supported actions. On the other
2924 hand, if GDB doesn't request the supported vCont actions
2925 in qSupported packet, add s and S to the list too. */
2926 own_buf
= own_buf
+ strlen (own_buf
);
2927 strcpy (own_buf
, ";s;S");
2930 if (target_supports_range_stepping ())
2932 own_buf
= own_buf
+ strlen (own_buf
);
2933 strcpy (own_buf
, ";r");
2939 if (startswith (own_buf
, "vFile:")
2940 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2943 if (startswith (own_buf
, "vAttach;"))
2945 if ((!extended_protocol
|| !multi_process
) && target_running ())
2947 fprintf (stderr
, "Already debugging a process\n");
2948 write_enn (own_buf
);
2951 handle_v_attach (own_buf
);
2955 if (startswith (own_buf
, "vRun;"))
2957 if ((!extended_protocol
|| !multi_process
) && target_running ())
2959 fprintf (stderr
, "Already debugging a process\n");
2960 write_enn (own_buf
);
2963 handle_v_run (own_buf
);
2967 if (startswith (own_buf
, "vKill;"))
2969 if (!target_running ())
2971 fprintf (stderr
, "No process to kill\n");
2972 write_enn (own_buf
);
2975 handle_v_kill (own_buf
);
2979 if (handle_notif_ack (own_buf
, packet_len
))
2982 /* Otherwise we didn't know what packet it was. Say we didn't
2988 /* Resume thread and wait for another event. In non-stop mode,
2989 don't really wait here, but return immediatelly to the event
2992 myresume (char *own_buf
, int step
, int sig
)
2994 struct thread_resume resume_info
[2];
2996 int valid_cont_thread
;
2998 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2999 && !ptid_equal (cont_thread
, minus_one_ptid
));
3001 if (step
|| sig
|| valid_cont_thread
)
3003 resume_info
[0].thread
= current_ptid
;
3005 resume_info
[0].kind
= resume_step
;
3007 resume_info
[0].kind
= resume_continue
;
3008 resume_info
[0].sig
= sig
;
3012 if (!valid_cont_thread
)
3014 resume_info
[n
].thread
= minus_one_ptid
;
3015 resume_info
[n
].kind
= resume_continue
;
3016 resume_info
[n
].sig
= 0;
3020 resume (resume_info
, n
);
3023 /* Callback for for_each_inferior. Make a new stop reply for each
3027 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
3029 struct thread_info
*thread
= (struct thread_info
*) entry
;
3031 /* For now, assume targets that don't have this callback also don't
3032 manage the thread's last_status field. */
3033 if (the_target
->thread_stopped
== NULL
)
3035 struct vstop_notif
*new_notif
= XNEW (struct vstop_notif
);
3037 new_notif
->ptid
= entry
->id
;
3038 new_notif
->status
= thread
->last_status
;
3039 /* Pass the last stop reply back to GDB, but don't notify
3041 notif_event_enque (¬if_stop
,
3042 (struct notif_event
*) new_notif
);
3046 if (thread_stopped (thread
))
3051 = target_waitstatus_to_string (&thread
->last_status
);
3053 debug_printf ("Reporting thread %s as already stopped with %s\n",
3054 target_pid_to_str (entry
->id
),
3057 xfree (status_string
);
3060 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
3062 /* Pass the last stop reply back to GDB, but don't notify
3064 queue_stop_reply (entry
->id
, &thread
->last_status
);
3071 /* Set this inferior threads's state as "want-stopped". We won't
3072 resume this thread until the client gives us another action for
3076 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
3078 struct thread_info
*thread
= (struct thread_info
*) entry
;
3080 thread
->last_resume_kind
= resume_stop
;
3082 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
3084 /* Most threads are stopped implicitly (all-stop); tag that with
3086 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
3087 thread
->last_status
.value
.sig
= GDB_SIGNAL_0
;
3091 /* Set all threads' states as "want-stopped". */
3094 gdb_wants_all_threads_stopped (void)
3096 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
3099 /* Clear the gdb_detached flag of every process. */
3102 gdb_reattached_process (struct inferior_list_entry
*entry
)
3104 struct process_info
*process
= (struct process_info
*) entry
;
3106 process
->gdb_detached
= 0;
3109 /* Callback for for_each_inferior. Clear the thread's pending status
3113 clear_pending_status_callback (struct inferior_list_entry
*entry
)
3115 struct thread_info
*thread
= (struct thread_info
*) entry
;
3117 thread
->status_pending_p
= 0;
3120 /* Callback for for_each_inferior. If the thread is stopped with an
3121 interesting event, mark it as having a pending event. */
3124 set_pending_status_callback (struct inferior_list_entry
*entry
)
3126 struct thread_info
*thread
= (struct thread_info
*) entry
;
3128 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
3129 || (thread
->last_status
.value
.sig
!= GDB_SIGNAL_0
3130 /* A breakpoint, watchpoint or finished step from a previous
3131 GDB run isn't considered interesting for a new GDB run.
3132 If we left those pending, the new GDB could consider them
3133 random SIGTRAPs. This leaves out real async traps. We'd
3134 have to peek into the (target-specific) siginfo to
3135 distinguish those. */
3136 && thread
->last_status
.value
.sig
!= GDB_SIGNAL_TRAP
))
3137 thread
->status_pending_p
= 1;
3140 /* Callback for find_inferior. Return true if ENTRY (a thread) has a
3141 pending status to report to GDB. */
3144 find_status_pending_thread_callback (struct inferior_list_entry
*entry
, void *data
)
3146 struct thread_info
*thread
= (struct thread_info
*) entry
;
3148 return thread
->status_pending_p
;
3151 /* Status handler for the '?' packet. */
3154 handle_status (char *own_buf
)
3156 /* GDB is connected, don't forward events to the target anymore. */
3157 for_each_inferior (&all_processes
, gdb_reattached_process
);
3159 /* In non-stop mode, we must send a stop reply for each stopped
3160 thread. In all-stop mode, just send one for the first stopped
3165 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
3167 /* The first is sent immediatly. OK is sent if there is no
3168 stopped thread, which is the same handling of the vStopped
3169 packet (by design). */
3170 notif_write_event (¬if_stop
, own_buf
);
3174 struct inferior_list_entry
*thread
= NULL
;
3177 stabilize_threads ();
3178 gdb_wants_all_threads_stopped ();
3180 /* We can only report one status, but we might be coming out of
3181 non-stop -- if more than one thread is stopped with
3182 interesting events, leave events for the threads we're not
3183 reporting now pending. They'll be reported the next time the
3184 threads are resumed. Start by marking all interesting events
3186 for_each_inferior (&all_threads
, set_pending_status_callback
);
3188 /* Prefer the last thread that reported an event to GDB (even if
3189 that was a GDB_SIGNAL_TRAP). */
3190 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
3191 && last_status
.kind
!= TARGET_WAITKIND_EXITED
3192 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
3193 thread
= find_inferior_id (&all_threads
, last_ptid
);
3195 /* If the last event thread is not found for some reason, look
3196 for some other thread that might have an event to report. */
3198 thread
= find_inferior (&all_threads
,
3199 find_status_pending_thread_callback
, NULL
);
3201 /* If we're still out of luck, simply pick the first thread in
3204 thread
= get_first_inferior (&all_threads
);
3208 struct thread_info
*tp
= (struct thread_info
*) thread
;
3210 /* We're reporting this event, so it's no longer
3212 tp
->status_pending_p
= 0;
3214 /* GDB assumes the current thread is the thread we're
3215 reporting the status for. */
3216 general_thread
= thread
->id
;
3217 set_desired_thread (1);
3219 gdb_assert (tp
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
3220 prepare_resume_reply (own_buf
, tp
->entry
.id
, &tp
->last_status
);
3223 strcpy (own_buf
, "W00");
3228 gdbserver_version (void)
3230 printf ("GNU gdbserver %s%s\n"
3231 "Copyright (C) 2015 Free Software Foundation, Inc.\n"
3232 "gdbserver is free software, covered by the "
3233 "GNU General Public License.\n"
3234 "This gdbserver was configured as \"%s\"\n",
3235 PKGVERSION
, version
, host_name
);
3239 gdbserver_usage (FILE *stream
)
3241 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
3242 "\tgdbserver [OPTIONS] --attach COMM PID\n"
3243 "\tgdbserver [OPTIONS] --multi COMM\n"
3245 "COMM may either be a tty device (for serial debugging),\n"
3246 "HOST:PORT to listen for a TCP connection, or '-' or 'stdio' to use \n"
3247 "stdin/stdout of gdbserver.\n"
3248 "PROG is the executable program. ARGS are arguments passed to inferior.\n"
3249 "PID is the process ID to attach to, when --attach is specified.\n"
3251 "Operating modes:\n"
3253 " --attach Attach to running process PID.\n"
3254 " --multi Start server without a specific program, and\n"
3255 " only quit when explicitly commanded.\n"
3256 " --once Exit after the first connection has closed.\n"
3257 " --help Print this message and then exit.\n"
3258 " --version Display version information and exit.\n"
3262 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
3263 " --disable-randomization\n"
3264 " Run PROG with address space randomization disabled.\n"
3265 " --no-disable-randomization\n"
3266 " Don't disable address space randomization when\n"
3271 " --debug Enable general debugging output.\n"
3272 " --debug-format=opt1[,opt2,...]\n"
3273 " Specify extra content in debugging output.\n"
3278 " --remote-debug Enable remote protocol debugging output.\n"
3279 " --disable-packet=opt1[,opt2,...]\n"
3280 " Disable support for RSP packets or features.\n"
3282 " vCont, Tthread, qC, qfThreadInfo and \n"
3283 " threads (disable all threading packets).\n"
3285 "For more information, consult the GDB manual (available as on-line \n"
3286 "info or a printed manual).\n");
3287 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
3288 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
3292 gdbserver_show_disableable (FILE *stream
)
3294 fprintf (stream
, "Disableable packets:\n"
3295 " vCont \tAll vCont packets\n"
3296 " qC \tQuerying the current thread\n"
3297 " qfThreadInfo\tThread listing\n"
3298 " Tthread \tPassing the thread specifier in the "
3299 "T stop reply packet\n"
3300 " threads \tAll of the above\n");
3304 #undef require_running
3305 #define require_running(BUF) \
3306 if (!target_running ()) \
3313 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
3315 int pid
= * (int *) args
;
3317 if (ptid_get_pid (entry
->id
) == pid
)
3324 kill_inferior_callback (struct inferior_list_entry
*entry
)
3326 struct process_info
*process
= (struct process_info
*) entry
;
3327 int pid
= ptid_get_pid (process
->entry
.id
);
3329 kill_inferior (pid
);
3330 discard_queued_stop_replies (pid_to_ptid (pid
));
3333 /* Callback for for_each_inferior to detach or kill the inferior,
3334 depending on whether we attached to it or not.
3335 We inform the user whether we're detaching or killing the process
3336 as this is only called when gdbserver is about to exit. */
3339 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
3341 struct process_info
*process
= (struct process_info
*) entry
;
3342 int pid
= ptid_get_pid (process
->entry
.id
);
3344 if (process
->attached
)
3345 detach_inferior (pid
);
3347 kill_inferior (pid
);
3349 discard_queued_stop_replies (pid_to_ptid (pid
));
3352 /* for_each_inferior callback for detach_or_kill_for_exit to print
3353 the pids of started inferiors. */
3356 print_started_pid (struct inferior_list_entry
*entry
)
3358 struct process_info
*process
= (struct process_info
*) entry
;
3360 if (! process
->attached
)
3362 int pid
= ptid_get_pid (process
->entry
.id
);
3363 fprintf (stderr
, " %d", pid
);
3367 /* for_each_inferior callback for detach_or_kill_for_exit to print
3368 the pids of attached inferiors. */
3371 print_attached_pid (struct inferior_list_entry
*entry
)
3373 struct process_info
*process
= (struct process_info
*) entry
;
3375 if (process
->attached
)
3377 int pid
= ptid_get_pid (process
->entry
.id
);
3378 fprintf (stderr
, " %d", pid
);
3382 /* Call this when exiting gdbserver with possible inferiors that need
3383 to be killed or detached from. */
3386 detach_or_kill_for_exit (void)
3388 /* First print a list of the inferiors we will be killing/detaching.
3389 This is to assist the user, for example, in case the inferior unexpectedly
3390 dies after we exit: did we screw up or did the inferior exit on its own?
3391 Having this info will save some head-scratching. */
3393 if (have_started_inferiors_p ())
3395 fprintf (stderr
, "Killing process(es):");
3396 for_each_inferior (&all_processes
, print_started_pid
);
3397 fprintf (stderr
, "\n");
3399 if (have_attached_inferiors_p ())
3401 fprintf (stderr
, "Detaching process(es):");
3402 for_each_inferior (&all_processes
, print_attached_pid
);
3403 fprintf (stderr
, "\n");
3406 /* Now we can kill or detach the inferiors. */
3408 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
3411 /* Value that will be passed to exit(3) when gdbserver exits. */
3412 static int exit_code
;
3414 /* Cleanup version of detach_or_kill_for_exit. */
3417 detach_or_kill_for_exit_cleanup (void *ignore
)
3422 detach_or_kill_for_exit ();
3425 CATCH (exception
, RETURN_MASK_ALL
)
3428 fprintf (stderr
, "Detach or kill failed: %s\n", exception
.message
);
3434 /* Main function. This is called by the real "main" function,
3435 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
3437 static void ATTRIBUTE_NORETURN
3438 captured_main (int argc
, char *argv
[])
3442 char *arg_end
, *port
;
3443 char **next_arg
= &argv
[1];
3444 volatile int multi_mode
= 0;
3445 volatile int attach
= 0;
3448 while (*next_arg
!= NULL
&& **next_arg
== '-')
3450 if (strcmp (*next_arg
, "--version") == 0)
3452 gdbserver_version ();
3455 else if (strcmp (*next_arg
, "--help") == 0)
3457 gdbserver_usage (stdout
);
3460 else if (strcmp (*next_arg
, "--attach") == 0)
3462 else if (strcmp (*next_arg
, "--multi") == 0)
3464 else if (strcmp (*next_arg
, "--wrapper") == 0)
3468 wrapper_argv
= next_arg
;
3469 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
3472 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
3474 gdbserver_usage (stderr
);
3478 /* Consume the "--". */
3481 else if (strcmp (*next_arg
, "--debug") == 0)
3483 else if (startswith (*next_arg
, "--debug-format="))
3486 = parse_debug_format_options ((*next_arg
)
3487 + sizeof ("--debug-format=") - 1, 0);
3489 if (error_msg
!= NULL
)
3491 fprintf (stderr
, "%s", error_msg
);
3495 else if (strcmp (*next_arg
, "--remote-debug") == 0)
3497 else if (strcmp (*next_arg
, "--disable-packet") == 0)
3499 gdbserver_show_disableable (stdout
);
3502 else if (startswith (*next_arg
, "--disable-packet="))
3504 char *packets
, *tok
;
3506 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
3507 for (tok
= strtok (packets
, ",");
3509 tok
= strtok (NULL
, ","))
3511 if (strcmp ("vCont", tok
) == 0)
3512 disable_packet_vCont
= 1;
3513 else if (strcmp ("Tthread", tok
) == 0)
3514 disable_packet_Tthread
= 1;
3515 else if (strcmp ("qC", tok
) == 0)
3516 disable_packet_qC
= 1;
3517 else if (strcmp ("qfThreadInfo", tok
) == 0)
3518 disable_packet_qfThreadInfo
= 1;
3519 else if (strcmp ("threads", tok
) == 0)
3521 disable_packet_vCont
= 1;
3522 disable_packet_Tthread
= 1;
3523 disable_packet_qC
= 1;
3524 disable_packet_qfThreadInfo
= 1;
3528 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
3530 gdbserver_show_disableable (stderr
);
3535 else if (strcmp (*next_arg
, "-") == 0)
3537 /* "-" specifies a stdio connection and is a form of port
3539 *next_arg
= STDIO_CONNECTION_NAME
;
3542 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
3543 disable_randomization
= 1;
3544 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
3545 disable_randomization
= 0;
3546 else if (strcmp (*next_arg
, "--once") == 0)
3550 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
3560 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
3562 gdbserver_usage (stderr
);
3566 /* Remember stdio descriptors. LISTEN_DESC must not be listed, it will be
3567 opened by remote_prepare. */
3570 /* We need to know whether the remote connection is stdio before
3571 starting the inferior. Inferiors created in this scenario have
3572 stdin,stdout redirected. So do this here before we call
3574 remote_prepare (port
);
3579 /* --attach used to come after PORT, so allow it there for
3581 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
3588 && (*next_arg
== NULL
3589 || (*next_arg
)[0] == '\0'
3590 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
3592 || next_arg
[1] != NULL
))
3597 gdbserver_usage (stderr
);
3601 initialize_async_io ();
3603 initialize_event_loop ();
3604 if (target_supports_tracepoints ())
3605 initialize_tracepoint ();
3606 initialize_notif ();
3608 own_buf
= (char *) xmalloc (PBUFSIZ
+ 1);
3609 mem_buf
= (unsigned char *) xmalloc (PBUFSIZ
);
3611 if (pid
== 0 && *next_arg
!= NULL
)
3615 n
= argc
- (next_arg
- argv
);
3616 program_argv
= XNEWVEC (char *, n
+ 1);
3617 for (i
= 0; i
< n
; i
++)
3618 program_argv
[i
] = xstrdup (next_arg
[i
]);
3619 program_argv
[i
] = NULL
;
3621 /* Wait till we are at first instruction in program. */
3622 start_inferior (program_argv
);
3624 /* We are now (hopefully) stopped at the first instruction of
3625 the target process. This assumes that the target process was
3626 successfully created. */
3630 if (attach_inferior (pid
) == -1)
3631 error ("Attaching not supported on this target");
3633 /* Otherwise succeeded. */
3637 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3638 last_status
.value
.integer
= 0;
3639 last_ptid
= minus_one_ptid
;
3641 make_cleanup (detach_or_kill_for_exit_cleanup
, NULL
);
3643 /* Don't report shared library events on the initial connection,
3644 even if some libraries are preloaded. Avoids the "stopped by
3645 shared library event" notice on gdb side. */
3648 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3649 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3654 if (!was_running
&& !multi_mode
)
3655 error ("No program to debug");
3662 report_fork_events
= 0;
3663 report_vfork_events
= 0;
3664 report_exec_events
= 0;
3665 /* Be sure we're out of tfind mode. */
3666 current_traceframe
= -1;
3667 cont_thread
= null_ptid
;
3668 swbreak_feature
= 0;
3669 hwbreak_feature
= 0;
3670 vCont_supported
= 0;
3676 /* Wait for events. This will return when all event sources
3677 are removed from the event loop. */
3678 start_event_loop ();
3680 /* If an exit was requested (using the "monitor exit"
3681 command), terminate now. The only other way to get
3682 here is for getpkt to fail; close the connection
3683 and reopen it at the top of the loop. */
3685 if (exit_requested
|| run_once
)
3686 throw_quit ("Quit");
3689 "Remote side has terminated connection. "
3690 "GDBserver will reopen the connection.\n");
3692 /* Get rid of any pending statuses. An eventual reconnection
3693 (by the same GDB instance or another) will refresh all its
3694 state from scratch. */
3695 discard_queued_stop_replies (minus_one_ptid
);
3696 for_each_inferior (&all_threads
,
3697 clear_pending_status_callback
);
3701 if (disconnected_tracing
)
3703 /* Try to enable non-stop/async mode, so we we can
3704 both wait for an async socket accept, and handle
3705 async target events simultaneously. There's also
3706 no point either in having the target always stop
3707 all threads, when we're going to pass signals
3708 down without informing GDB. */
3711 if (start_non_stop (1))
3714 /* Detaching implicitly resumes all threads;
3715 simply disconnecting does not. */
3721 "Disconnected tracing disabled; "
3722 "stopping trace run.\n");
3727 CATCH (exception
, RETURN_MASK_ERROR
)
3730 fprintf (stderr
, "gdbserver: %s\n", exception
.message
);
3732 if (response_needed
)
3734 write_enn (own_buf
);
3739 throw_quit ("Quit");
3745 /* Main function. */
3748 main (int argc
, char *argv
[])
3753 captured_main (argc
, argv
);
3755 CATCH (exception
, RETURN_MASK_ALL
)
3757 if (exception
.reason
== RETURN_ERROR
)
3760 fprintf (stderr
, "%s\n", exception
.message
);
3761 fprintf (stderr
, "Exiting\n");
3769 gdb_assert_not_reached ("captured_main should never return");
3772 /* Skip PACKET until the next semi-colon (or end of string). */
3775 skip_to_semicolon (char **packet
)
3777 while (**packet
!= '\0' && **packet
!= ';')
3781 /* Process options coming from Z packets for a breakpoint. PACKET is
3782 the packet buffer. *PACKET is updated to point to the first char
3783 after the last processed option. */
3786 process_point_options (struct breakpoint
*bp
, char **packet
)
3788 char *dataptr
= *packet
;
3791 /* Check if data has the correct format. */
3792 if (*dataptr
!= ';')
3799 if (*dataptr
== ';')
3802 if (*dataptr
== 'X')
3804 /* Conditional expression. */
3806 debug_printf ("Found breakpoint condition.\n");
3807 if (!add_breakpoint_condition (bp
, &dataptr
))
3808 skip_to_semicolon (&dataptr
);
3810 else if (startswith (dataptr
, "cmds:"))
3812 dataptr
+= strlen ("cmds:");
3814 debug_printf ("Found breakpoint commands %s.\n", dataptr
);
3815 persist
= (*dataptr
== '1');
3817 if (add_breakpoint_commands (bp
, &dataptr
, persist
))
3818 skip_to_semicolon (&dataptr
);
3822 fprintf (stderr
, "Unknown token %c, ignoring.\n",
3824 /* Skip tokens until we find one that we recognize. */
3825 skip_to_semicolon (&dataptr
);
3831 /* Event loop callback that handles a serial event. The first byte in
3832 the serial buffer gets us here. We expect characters to arrive at
3833 a brisk pace, so we read the rest of the packet with a blocking
3837 process_serial_event (void)
3848 int new_packet_len
= -1;
3850 /* Used to decide when gdbserver should exit in
3851 multi-mode/remote. */
3852 static int have_ran
= 0;
3855 have_ran
= target_running ();
3857 disable_async_io ();
3859 response_needed
= 0;
3860 packet_len
= getpkt (own_buf
);
3861 if (packet_len
<= 0)
3864 /* Force an event loop break. */
3867 response_needed
= 1;
3874 handle_query (own_buf
, packet_len
, &new_packet_len
);
3877 handle_general_set (own_buf
);
3880 require_running (own_buf
);
3885 pid
= strtol (&own_buf
[i
], NULL
, 16);
3888 pid
= ptid_get_pid (current_ptid
);
3890 if ((tracing
&& disconnected_tracing
) || any_persistent_commands ())
3892 struct thread_resume resume_info
;
3893 struct process_info
*process
= find_process_pid (pid
);
3895 if (process
== NULL
)
3897 write_enn (own_buf
);
3901 if (tracing
&& disconnected_tracing
)
3903 "Disconnected tracing in effect, "
3904 "leaving gdbserver attached to the process\n");
3906 if (any_persistent_commands ())
3908 "Persistent commands are present, "
3909 "leaving gdbserver attached to the process\n");
3911 /* Make sure we're in non-stop/async mode, so we we can both
3912 wait for an async socket accept, and handle async target
3913 events simultaneously. There's also no point either in
3914 having the target stop all threads, when we're going to
3915 pass signals down without informing GDB. */
3919 debug_printf ("Forcing non-stop mode\n");
3925 process
->gdb_detached
= 1;
3927 /* Detaching implicitly resumes all threads. */
3928 resume_info
.thread
= minus_one_ptid
;
3929 resume_info
.kind
= resume_continue
;
3930 resume_info
.sig
= 0;
3931 (*the_target
->resume
) (&resume_info
, 1);
3934 break; /* from switch/case */
3937 fprintf (stderr
, "Detaching from process %d\n", pid
);
3939 if (detach_inferior (pid
) != 0)
3940 write_enn (own_buf
);
3943 discard_queued_stop_replies (pid_to_ptid (pid
));
3946 if (extended_protocol
)
3948 /* Treat this like a normal program exit. */
3949 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3950 last_status
.value
.integer
= 0;
3951 last_ptid
= pid_to_ptid (pid
);
3953 current_thread
= NULL
;
3960 /* If we are attached, then we can exit. Otherwise, we
3961 need to hang around doing nothing, until the child is
3963 join_inferior (pid
);
3969 extended_protocol
= 1;
3973 handle_status (own_buf
);
3976 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
3978 ptid_t gdb_id
, thread_id
;
3981 require_running (own_buf
);
3983 gdb_id
= read_ptid (&own_buf
[2], NULL
);
3985 pid
= ptid_get_pid (gdb_id
);
3987 if (ptid_equal (gdb_id
, null_ptid
)
3988 || ptid_equal (gdb_id
, minus_one_ptid
))
3989 thread_id
= null_ptid
;
3991 && ptid_equal (pid_to_ptid (pid
),
3994 struct thread_info
*thread
=
3995 (struct thread_info
*) find_inferior (&all_threads
,
4000 write_enn (own_buf
);
4004 thread_id
= thread
->entry
.id
;
4008 thread_id
= gdb_id_to_thread_id (gdb_id
);
4009 if (ptid_equal (thread_id
, null_ptid
))
4011 write_enn (own_buf
);
4016 if (own_buf
[1] == 'g')
4018 if (ptid_equal (thread_id
, null_ptid
))
4020 /* GDB is telling us to choose any thread. Check if
4021 the currently selected thread is still valid. If
4022 it is not, select the first available. */
4023 struct thread_info
*thread
=
4024 (struct thread_info
*) find_inferior_id (&all_threads
,
4027 thread
= get_first_thread ();
4028 thread_id
= thread
->entry
.id
;
4031 general_thread
= thread_id
;
4032 set_desired_thread (1);
4033 gdb_assert (current_thread
!= NULL
);
4035 else if (own_buf
[1] == 'c')
4036 cont_thread
= thread_id
;
4042 /* Silently ignore it so that gdb can extend the protocol
4043 without compatibility headaches. */
4048 require_running (own_buf
);
4049 if (current_traceframe
>= 0)
4051 struct regcache
*regcache
4052 = new_register_cache (current_target_desc ());
4054 if (fetch_traceframe_registers (current_traceframe
,
4056 registers_to_string (regcache
, own_buf
);
4058 write_enn (own_buf
);
4059 free_register_cache (regcache
);
4063 struct regcache
*regcache
;
4065 if (!set_desired_thread (1))
4066 write_enn (own_buf
);
4069 regcache
= get_thread_regcache (current_thread
, 1);
4070 registers_to_string (regcache
, own_buf
);
4075 require_running (own_buf
);
4076 if (current_traceframe
>= 0)
4077 write_enn (own_buf
);
4080 struct regcache
*regcache
;
4082 if (!set_desired_thread (1))
4083 write_enn (own_buf
);
4086 regcache
= get_thread_regcache (current_thread
, 1);
4087 registers_from_string (regcache
, &own_buf
[1]);
4093 require_running (own_buf
);
4094 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
4095 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
4097 write_enn (own_buf
);
4099 bin2hex (mem_buf
, own_buf
, res
);
4102 require_running (own_buf
);
4103 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
4104 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
4107 write_enn (own_buf
);
4110 require_running (own_buf
);
4111 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
4112 &mem_addr
, &len
, &mem_buf
) < 0
4113 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
4114 write_enn (own_buf
);
4119 require_running (own_buf
);
4120 hex2bin (own_buf
+ 1, &sig
, 1);
4121 if (gdb_signal_to_host_p ((enum gdb_signal
) sig
))
4122 signal
= gdb_signal_to_host ((enum gdb_signal
) sig
);
4125 myresume (own_buf
, 0, signal
);
4128 require_running (own_buf
);
4129 hex2bin (own_buf
+ 1, &sig
, 1);
4130 if (gdb_signal_to_host_p ((enum gdb_signal
) sig
))
4131 signal
= gdb_signal_to_host ((enum gdb_signal
) sig
);
4134 myresume (own_buf
, 1, signal
);
4137 require_running (own_buf
);
4139 myresume (own_buf
, 0, signal
);
4142 require_running (own_buf
);
4144 myresume (own_buf
, 1, signal
);
4146 case 'Z': /* insert_ ... */
4148 case 'z': /* remove_ ... */
4153 char type
= own_buf
[1];
4155 const int insert
= ch
== 'Z';
4156 char *p
= &own_buf
[3];
4158 p
= unpack_varlen_hex (p
, &addr
);
4159 kind
= strtol (p
+ 1, &dataptr
, 16);
4163 struct breakpoint
*bp
;
4165 bp
= set_gdb_breakpoint (type
, addr
, kind
, &res
);
4170 /* GDB may have sent us a list of *point parameters to
4171 be evaluated on the target's side. Read such list
4172 here. If we already have a list of parameters, GDB
4173 is telling us to drop that list and use this one
4175 clear_breakpoint_conditions_and_commands (bp
);
4176 process_point_options (bp
, &dataptr
);
4180 res
= delete_gdb_breakpoint (type
, addr
, kind
);
4188 write_enn (own_buf
);
4192 response_needed
= 0;
4193 if (!target_running ())
4194 /* The packet we received doesn't make sense - but we can't
4195 reply to it, either. */
4198 fprintf (stderr
, "Killing all inferiors\n");
4199 for_each_inferior (&all_processes
, kill_inferior_callback
);
4201 /* When using the extended protocol, we wait with no program
4202 running. The traditional protocol will exit instead. */
4203 if (extended_protocol
)
4205 last_status
.kind
= TARGET_WAITKIND_EXITED
;
4206 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
4214 ptid_t gdb_id
, thread_id
;
4216 require_running (own_buf
);
4218 gdb_id
= read_ptid (&own_buf
[1], NULL
);
4219 thread_id
= gdb_id_to_thread_id (gdb_id
);
4220 if (ptid_equal (thread_id
, null_ptid
))
4222 write_enn (own_buf
);
4226 if (mythread_alive (thread_id
))
4229 write_enn (own_buf
);
4233 response_needed
= 0;
4235 /* Restarting the inferior is only supported in the extended
4237 if (extended_protocol
)
4239 if (target_running ())
4240 for_each_inferior (&all_processes
,
4241 kill_inferior_callback
);
4242 fprintf (stderr
, "GDBserver restarting\n");
4244 /* Wait till we are at 1st instruction in prog. */
4245 if (program_argv
!= NULL
)
4247 start_inferior (program_argv
);
4248 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
4250 /* Stopped at the first instruction of the target
4252 general_thread
= last_ptid
;
4256 /* Something went wrong. */
4257 general_thread
= null_ptid
;
4262 last_status
.kind
= TARGET_WAITKIND_EXITED
;
4263 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
4269 /* It is a request we don't understand. Respond with an
4270 empty packet so that gdb knows that we don't support this
4276 /* Extended (long) request. */
4277 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
4281 /* It is a request we don't understand. Respond with an empty
4282 packet so that gdb knows that we don't support this
4288 if (new_packet_len
!= -1)
4289 putpkt_binary (own_buf
, new_packet_len
);
4293 response_needed
= 0;
4295 if (!extended_protocol
&& have_ran
&& !target_running ())
4297 /* In non-stop, defer exiting until GDB had a chance to query
4298 the whole vStopped list (until it gets an OK). */
4299 if (QUEUE_is_empty (notif_event_p
, notif_stop
.queue
))
4301 /* Be transparent when GDB is connected through stdio -- no
4302 need to spam GDB's console. */
4303 if (!remote_connection_is_stdio ())
4304 fprintf (stderr
, "GDBserver exiting\n");
4316 /* Event-loop callback for serial events. */
4319 handle_serial_event (int err
, gdb_client_data client_data
)
4322 debug_printf ("handling possible serial event\n");
4324 /* Really handle it. */
4325 if (process_serial_event () < 0)
4328 /* Be sure to not change the selected thread behind GDB's back.
4329 Important in the non-stop mode asynchronous protocol. */
4330 set_desired_thread (1);
4335 /* Event-loop callback for target events. */
4338 handle_target_event (int err
, gdb_client_data client_data
)
4341 debug_printf ("handling possible target event\n");
4343 last_ptid
= mywait (minus_one_ptid
, &last_status
,
4346 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
)
4348 /* No RSP support for this yet. */
4350 else if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
4352 int pid
= ptid_get_pid (last_ptid
);
4353 struct process_info
*process
= find_process_pid (pid
);
4354 int forward_event
= !gdb_connected () || process
->gdb_detached
;
4356 if (last_status
.kind
== TARGET_WAITKIND_EXITED
4357 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
4359 mark_breakpoints_out (process
);
4360 mourn_inferior (process
);
4362 else if (last_status
.kind
== TARGET_WAITKIND_THREAD_EXITED
)
4366 /* We're reporting this thread as stopped. Update its
4367 "want-stopped" state to what the client wants, until it
4368 gets a new resume action. */
4369 current_thread
->last_resume_kind
= resume_stop
;
4370 current_thread
->last_status
= last_status
;
4375 if (!target_running ())
4377 /* The last process exited. We're done. */
4381 if (last_status
.kind
== TARGET_WAITKIND_EXITED
4382 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
4383 || last_status
.kind
== TARGET_WAITKIND_THREAD_EXITED
)
4387 /* A thread stopped with a signal, but gdb isn't
4388 connected to handle it. Pass it down to the
4389 inferior, as if it wasn't being traced. */
4390 struct thread_resume resume_info
;
4393 debug_printf ("GDB not connected; forwarding event %d for"
4395 (int) last_status
.kind
,
4396 target_pid_to_str (last_ptid
));
4398 resume_info
.thread
= last_ptid
;
4399 resume_info
.kind
= resume_continue
;
4400 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
4401 resume_info
.sig
= gdb_signal_to_host (last_status
.value
.sig
);
4403 resume_info
.sig
= 0;
4404 (*the_target
->resume
) (&resume_info
, 1);
4409 struct vstop_notif
*vstop_notif
= XNEW (struct vstop_notif
);
4411 vstop_notif
->status
= last_status
;
4412 vstop_notif
->ptid
= last_ptid
;
4413 /* Push Stop notification. */
4414 notif_push (¬if_stop
,
4415 (struct notif_event
*) vstop_notif
);
4419 /* Be sure to not change the selected thread behind GDB's back.
4420 Important in the non-stop mode asynchronous protocol. */
4421 set_desired_thread (1);