1 /* Main code for remote server for GDB.
2 Copyright (C) 1989-2017 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"
25 #include "signals-state-save-restore.h"
33 #include "btrace-common.h"
34 #include "filestuff.h"
35 #include "tracepoint.h"
39 #include "common-inferior.h"
40 #include "job-control.h"
43 #include "common/selftest.h"
45 /* The environment to pass to the inferior when creating it. */
47 static gdb_environ our_environ
;
49 /* Start the inferior using a shell. */
51 /* We always try to start the inferior using a shell. */
53 int startup_with_shell
= 1;
55 /* The thread set with an `Hc' packet. `Hc' is deprecated in favor of
56 `vCont'. Note the multi-process extensions made `vCont' a
57 requirement, so `Hc pPID.TID' is pretty much undefined. So
58 CONT_THREAD can be null_ptid for no `Hc' thread, minus_one_ptid for
59 resuming all threads of the process (again, `Hc' isn't used for
60 multi-process), or a specific thread ptid_t. */
63 /* The thread set with an `Hg' packet. */
64 ptid_t general_thread
;
68 static int extended_protocol
;
69 static int response_needed
;
70 static int exit_requested
;
72 /* --once: Exit after the first connection has closed. */
76 int report_fork_events
;
77 int report_vfork_events
;
78 int report_exec_events
;
79 int report_thread_events
;
81 /* Whether to report TARGET_WAITKING_NO_RESUMED events. */
82 static int report_no_resumed
;
88 /* True if the "vContSupported" feature is active. In that case, GDB
89 wants us to report whether single step is supported in the reply to
91 static int vCont_supported
;
93 /* Whether we should attempt to disable the operating system's address
94 space randomization feature before starting an inferior. */
95 int disable_randomization
= 1;
97 static char *program_name
= NULL
;
98 static std::vector
<char *> program_args
;
99 static std::string wrapper_argv
;
101 int pass_signals
[GDB_SIGNAL_LAST
];
102 int program_signals
[GDB_SIGNAL_LAST
];
103 int program_signals_p
;
105 /* The PID of the originally created or attached inferior. Used to
106 send signals to the process when GDB sends us an asynchronous interrupt
107 (user hitting Control-C in the client), and to wait for the child to exit
108 when no longer debugging it. */
110 unsigned long signal_pid
;
112 /* Set if you want to disable optional thread related packets support
113 in gdbserver, for the sake of testing GDB against stubs that don't
115 int disable_packet_vCont
;
116 int disable_packet_Tthread
;
117 int disable_packet_qC
;
118 int disable_packet_qfThreadInfo
;
120 /* Last status reported to GDB. */
121 struct target_waitstatus last_status
;
125 static unsigned char *mem_buf
;
127 /* A sub-class of 'struct notif_event' for stop, holding information
128 relative to a single stop reply. We keep a queue of these to
129 push to GDB in non-stop mode. */
133 struct notif_event base
;
135 /* Thread or process that got the event. */
139 struct target_waitstatus status
;
142 /* The current btrace configuration. This is gdbserver's mirror of GDB's
143 btrace configuration. */
144 static struct btrace_config current_btrace_conf
;
146 DEFINE_QUEUE_P (notif_event_p
);
148 /* Put a stop reply to the stop reply queue. */
151 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
153 struct vstop_notif
*new_notif
= XNEW (struct vstop_notif
);
155 new_notif
->ptid
= ptid
;
156 new_notif
->status
= *status
;
158 notif_event_enque (¬if_stop
, (struct notif_event
*) new_notif
);
162 remove_all_on_match_ptid (QUEUE (notif_event_p
) *q
,
163 QUEUE_ITER (notif_event_p
) *iter
,
164 struct notif_event
*event
,
167 ptid_t filter_ptid
= *(ptid_t
*) data
;
168 struct vstop_notif
*vstop_event
= (struct vstop_notif
*) event
;
170 if (ptid_match (vstop_event
->ptid
, filter_ptid
))
172 if (q
->free_func
!= NULL
)
173 q
->free_func (event
);
175 QUEUE_remove_elem (notif_event_p
, q
, iter
);
184 discard_queued_stop_replies (ptid_t ptid
)
186 QUEUE_iterate (notif_event_p
, notif_stop
.queue
,
187 remove_all_on_match_ptid
, &ptid
);
191 vstop_notif_reply (struct notif_event
*event
, char *own_buf
)
193 struct vstop_notif
*vstop
= (struct vstop_notif
*) event
;
195 prepare_resume_reply (own_buf
, vstop
->ptid
, &vstop
->status
);
198 /* QUEUE_iterate callback helper for in_queued_stop_replies. */
201 in_queued_stop_replies_ptid (QUEUE (notif_event_p
) *q
,
202 QUEUE_ITER (notif_event_p
) *iter
,
203 struct notif_event
*event
,
206 ptid_t filter_ptid
= *(ptid_t
*) data
;
207 struct vstop_notif
*vstop_event
= (struct vstop_notif
*) event
;
209 if (ptid_match (vstop_event
->ptid
, filter_ptid
))
212 /* Don't resume fork children that GDB does not know about yet. */
213 if ((vstop_event
->status
.kind
== TARGET_WAITKIND_FORKED
214 || vstop_event
->status
.kind
== TARGET_WAITKIND_VFORKED
)
215 && ptid_match (vstop_event
->status
.value
.related_pid
, filter_ptid
))
224 in_queued_stop_replies (ptid_t ptid
)
226 return !QUEUE_iterate (notif_event_p
, notif_stop
.queue
,
227 in_queued_stop_replies_ptid
, &ptid
);
230 struct notif_server notif_stop
=
232 "vStopped", "Stop", NULL
, vstop_notif_reply
,
236 target_running (void)
238 return get_first_thread () != NULL
;
241 /* See common/common-inferior.h. */
246 return !wrapper_argv
.empty () ? wrapper_argv
.c_str () : NULL
;
249 /* See common/common-inferior.h. */
252 get_exec_file (int err
)
254 if (err
&& program_name
== NULL
)
255 error (_("No executable file specified."));
269 attach_inferior (int pid
)
271 /* myattach should return -1 if attaching is unsupported,
272 0 if it succeeded, and call error() otherwise. */
274 if (myattach (pid
) != 0)
277 fprintf (stderr
, "Attached; pid = %d\n", pid
);
280 /* FIXME - It may be that we should get the SIGNAL_PID from the
281 attach function, so that it can be the main thread instead of
282 whichever we were told to attach to. */
287 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
289 /* GDB knows to ignore the first SIGSTOP after attaching to a running
290 process using the "attach" command, but this is different; it's
291 just using "target remote". Pretend it's just starting up. */
292 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
293 && last_status
.value
.sig
== GDB_SIGNAL_STOP
)
294 last_status
.value
.sig
= GDB_SIGNAL_TRAP
;
296 current_thread
->last_resume_kind
= resume_stop
;
297 current_thread
->last_status
= last_status
;
303 extern int remote_debug
;
305 /* Decode a qXfer read request. Return 0 if everything looks OK,
309 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
311 /* After the read marker and annex, qXfer looks like a
312 traditional 'm' packet. */
313 decode_m_packet (buf
, ofs
, len
);
319 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
321 /* Extract and NUL-terminate the object. */
323 while (*buf
&& *buf
!= ':')
329 /* Extract and NUL-terminate the read/write action. */
331 while (*buf
&& *buf
!= ':')
337 /* Extract and NUL-terminate the annex. */
339 while (*buf
&& *buf
!= ':')
349 /* Write the response to a successful qXfer read. Returns the
350 length of the (binary) data stored in BUF, corresponding
351 to as much of DATA/LEN as we could fit. IS_MORE controls
352 the first character of the response. */
354 write_qxfer_response (char *buf
, const gdb_byte
*data
, int len
, int is_more
)
363 return remote_escape_output (data
, len
, 1, (unsigned char *) buf
+ 1,
364 &out_len
, PBUFSIZ
- 2) + 1;
367 /* Handle btrace enabling in BTS format. */
370 handle_btrace_enable_bts (struct thread_info
*thread
)
372 if (thread
->btrace
!= NULL
)
373 return "E.Btrace already enabled.";
375 current_btrace_conf
.format
= BTRACE_FORMAT_BTS
;
376 thread
->btrace
= target_enable_btrace (thread
->entry
.id
,
377 ¤t_btrace_conf
);
378 if (thread
->btrace
== NULL
)
379 return "E.Could not enable btrace.";
384 /* Handle btrace enabling in Intel Processor Trace format. */
387 handle_btrace_enable_pt (struct thread_info
*thread
)
389 if (thread
->btrace
!= NULL
)
390 return "E.Btrace already enabled.";
392 current_btrace_conf
.format
= BTRACE_FORMAT_PT
;
393 thread
->btrace
= target_enable_btrace (thread
->entry
.id
,
394 ¤t_btrace_conf
);
395 if (thread
->btrace
== NULL
)
396 return "E.Could not enable btrace.";
401 /* Handle btrace disabling. */
404 handle_btrace_disable (struct thread_info
*thread
)
407 if (thread
->btrace
== NULL
)
408 return "E.Branch tracing not enabled.";
410 if (target_disable_btrace (thread
->btrace
) != 0)
411 return "E.Could not disable branch tracing.";
413 thread
->btrace
= NULL
;
417 /* Handle the "Qbtrace" packet. */
420 handle_btrace_general_set (char *own_buf
)
422 struct thread_info
*thread
;
426 if (!startswith (own_buf
, "Qbtrace:"))
429 op
= own_buf
+ strlen ("Qbtrace:");
431 if (ptid_equal (general_thread
, null_ptid
)
432 || ptid_equal (general_thread
, minus_one_ptid
))
434 strcpy (own_buf
, "E.Must select a single thread.");
438 thread
= find_thread_ptid (general_thread
);
441 strcpy (own_buf
, "E.No such thread.");
447 if (strcmp (op
, "bts") == 0)
448 err
= handle_btrace_enable_bts (thread
);
449 else if (strcmp (op
, "pt") == 0)
450 err
= handle_btrace_enable_pt (thread
);
451 else if (strcmp (op
, "off") == 0)
452 err
= handle_btrace_disable (thread
);
454 err
= "E.Bad Qbtrace operation. Use bts, pt, or off.";
457 strcpy (own_buf
, err
);
464 /* Handle the "Qbtrace-conf" packet. */
467 handle_btrace_conf_general_set (char *own_buf
)
469 struct thread_info
*thread
;
472 if (!startswith (own_buf
, "Qbtrace-conf:"))
475 op
= own_buf
+ strlen ("Qbtrace-conf:");
477 if (ptid_equal (general_thread
, null_ptid
)
478 || ptid_equal (general_thread
, minus_one_ptid
))
480 strcpy (own_buf
, "E.Must select a single thread.");
484 thread
= find_thread_ptid (general_thread
);
487 strcpy (own_buf
, "E.No such thread.");
491 if (startswith (op
, "bts:size="))
497 size
= strtoul (op
+ strlen ("bts:size="), &endp
, 16);
498 if (endp
== NULL
|| *endp
!= 0 || errno
!= 0 || size
> UINT_MAX
)
500 strcpy (own_buf
, "E.Bad size value.");
504 current_btrace_conf
.bts
.size
= (unsigned int) size
;
506 else if (strncmp (op
, "pt:size=", strlen ("pt:size=")) == 0)
512 size
= strtoul (op
+ strlen ("pt:size="), &endp
, 16);
513 if (endp
== NULL
|| *endp
!= 0 || errno
!= 0 || size
> UINT_MAX
)
515 strcpy (own_buf
, "E.Bad size value.");
519 current_btrace_conf
.pt
.size
= (unsigned int) size
;
523 strcpy (own_buf
, "E.Bad Qbtrace configuration option.");
531 /* Handle all of the extended 'Q' packets. */
534 handle_general_set (char *own_buf
)
536 if (startswith (own_buf
, "QPassSignals:"))
538 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
539 const char *p
= own_buf
+ strlen ("QPassSignals:");
542 p
= decode_address_to_semicolon (&cursig
, p
);
543 for (i
= 0; i
< numsigs
; i
++)
549 /* Keep looping, to clear the remaining signals. */
552 p
= decode_address_to_semicolon (&cursig
, p
);
557 strcpy (own_buf
, "OK");
561 if (startswith (own_buf
, "QProgramSignals:"))
563 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
564 const char *p
= own_buf
+ strlen ("QProgramSignals:");
567 program_signals_p
= 1;
569 p
= decode_address_to_semicolon (&cursig
, p
);
570 for (i
= 0; i
< numsigs
; i
++)
574 program_signals
[i
] = 1;
576 /* Keep looping, to clear the remaining signals. */
579 p
= decode_address_to_semicolon (&cursig
, p
);
582 program_signals
[i
] = 0;
584 strcpy (own_buf
, "OK");
588 if (startswith (own_buf
, "QCatchSyscalls:"))
590 const char *p
= own_buf
+ sizeof ("QCatchSyscalls:") - 1;
593 struct process_info
*process
;
595 if (!target_running () || !target_supports_catch_syscall ())
601 if (strcmp (p
, "0") == 0)
603 else if (p
[0] == '1' && (p
[1] == ';' || p
[1] == '\0'))
607 fprintf (stderr
, "Unknown catch-syscalls mode requested: %s\n",
613 process
= current_process ();
614 VEC_truncate (int, process
->syscalls_to_catch
, 0);
624 p
= decode_address_to_semicolon (&sysno
, p
);
625 VEC_safe_push (int, process
->syscalls_to_catch
, (int) sysno
);
629 VEC_safe_push (int, process
->syscalls_to_catch
, ANY_SYSCALL
);
636 if (strcmp (own_buf
, "QEnvironmentReset") == 0)
638 our_environ
= gdb_environ::from_host_environ ();
644 if (startswith (own_buf
, "QEnvironmentHexEncoded:"))
646 const char *p
= own_buf
+ sizeof ("QEnvironmentHexEncoded:") - 1;
647 /* The final form of the environment variable. FINAL_VAR will
648 hold the 'VAR=VALUE' format. */
649 std::string final_var
= hex2str (p
);
650 std::string var_name
, var_value
;
654 debug_printf (_("[QEnvironmentHexEncoded received '%s']\n"), p
);
655 debug_printf (_("[Environment variable to be set: '%s']\n"),
660 size_t pos
= final_var
.find ('=');
661 if (pos
== std::string::npos
)
663 warning (_("Unexpected format for environment variable: '%s'"),
669 var_name
= final_var
.substr (0, pos
);
670 var_value
= final_var
.substr (pos
+ 1, std::string::npos
);
672 our_environ
.set (var_name
.c_str (), var_value
.c_str ());
678 if (startswith (own_buf
, "QEnvironmentUnset:"))
680 const char *p
= own_buf
+ sizeof ("QEnvironmentUnset:") - 1;
681 std::string varname
= hex2str (p
);
685 debug_printf (_("[QEnvironmentUnset received '%s']\n"), p
);
686 debug_printf (_("[Environment variable to be unset: '%s']\n"),
691 our_environ
.unset (varname
.c_str ());
697 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
701 debug_printf ("[noack mode enabled]\n");
710 if (startswith (own_buf
, "QNonStop:"))
712 char *mode
= own_buf
+ 9;
716 if (strcmp (mode
, "0") == 0)
718 else if (strcmp (mode
, "1") == 0)
722 /* We don't know what this mode is, so complain to
724 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
730 req_str
= req
? "non-stop" : "all-stop";
731 if (start_non_stop (req
) != 0)
733 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
741 debug_printf ("[%s mode enabled]\n", req_str
);
747 if (startswith (own_buf
, "QDisableRandomization:"))
749 char *packet
= own_buf
+ strlen ("QDisableRandomization:");
752 unpack_varlen_hex (packet
, &setting
);
753 disable_randomization
= setting
;
757 debug_printf (disable_randomization
758 ? "[address space randomization disabled]\n"
759 : "[address space randomization enabled]\n");
766 if (target_supports_tracepoints ()
767 && handle_tracepoint_general_set (own_buf
))
770 if (startswith (own_buf
, "QAgent:"))
772 char *mode
= own_buf
+ strlen ("QAgent:");
775 if (strcmp (mode
, "0") == 0)
777 else if (strcmp (mode
, "1") == 0)
781 /* We don't know what this value is, so complain to GDB. */
782 sprintf (own_buf
, "E.Unknown QAgent value");
786 /* Update the flag. */
789 debug_printf ("[%s agent]\n", req
? "Enable" : "Disable");
794 if (handle_btrace_general_set (own_buf
))
797 if (handle_btrace_conf_general_set (own_buf
))
800 if (startswith (own_buf
, "QThreadEvents:"))
802 char *mode
= own_buf
+ strlen ("QThreadEvents:");
803 enum tribool req
= TRIBOOL_UNKNOWN
;
805 if (strcmp (mode
, "0") == 0)
807 else if (strcmp (mode
, "1") == 0)
811 char *mode_copy
= xstrdup (mode
);
813 /* We don't know what this mode is, so complain to GDB. */
814 sprintf (own_buf
, "E.Unknown thread-events mode requested: %s\n",
820 report_thread_events
= (req
== TRIBOOL_TRUE
);
824 const char *req_str
= report_thread_events
? "enabled" : "disabled";
826 debug_printf ("[thread events are now %s]\n", req_str
);
833 if (startswith (own_buf
, "QStartupWithShell:"))
835 const char *value
= own_buf
+ strlen ("QStartupWithShell:");
837 if (strcmp (value
, "1") == 0)
838 startup_with_shell
= true;
839 else if (strcmp (value
, "0") == 0)
840 startup_with_shell
= false;
844 fprintf (stderr
, "Unknown value to startup-with-shell: %s\n",
851 debug_printf (_("[Inferior will %s started with shell]"),
852 startup_with_shell
? "be" : "not be");
858 /* Otherwise we didn't know what packet it was. Say we didn't
864 get_features_xml (const char *annex
)
866 const struct target_desc
*desc
= current_target_desc ();
868 /* `desc->xmltarget' defines what to return when looking for the
869 "target.xml" file. Its contents can either be verbatim XML code
870 (prefixed with a '@') or else the name of the actual XML file to
871 be used in place of "target.xml".
873 This variable is set up from the auto-generated
874 init_registers_... routine for the current target. */
876 if (strcmp (annex
, "target.xml") == 0)
878 const char *ret
= tdesc_get_features_xml ((target_desc
*) desc
);
888 extern const char *const xml_builtin
[][2];
891 /* Look for the annex. */
892 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
893 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
896 if (xml_builtin
[i
][0] != NULL
)
897 return xml_builtin
[i
][1];
905 monitor_show_help (void)
907 monitor_output ("The following monitor commands are supported:\n");
908 monitor_output (" set debug <0|1>\n");
909 monitor_output (" Enable general debugging messages\n");
910 monitor_output (" set debug-hw-points <0|1>\n");
911 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
912 monitor_output (" set remote-debug <0|1>\n");
913 monitor_output (" Enable remote protocol debugging messages\n");
914 monitor_output (" set debug-format option1[,option2,...]\n");
915 monitor_output (" Add additional information to debugging messages\n");
916 monitor_output (" Options: all, none");
917 monitor_output (", timestamp");
918 monitor_output ("\n");
919 monitor_output (" exit\n");
920 monitor_output (" Quit GDBserver\n");
923 /* Read trace frame or inferior memory. Returns the number of bytes
924 actually read, zero when no further transfer is possible, and -1 on
925 error. Return of a positive value smaller than LEN does not
926 indicate there's no more to be read, only the end of the transfer.
927 E.g., when GDB reads memory from a traceframe, a first request may
928 be served from a memory block that does not cover the whole request
929 length. A following request gets the rest served from either
930 another block (of the same traceframe) or from the read-only
934 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
938 if (current_traceframe
>= 0)
941 ULONGEST length
= len
;
943 if (traceframe_read_mem (current_traceframe
,
944 memaddr
, myaddr
, len
, &nbytes
))
946 /* Data read from trace buffer, we're done. */
949 if (!in_readonly_region (memaddr
, length
))
951 /* Otherwise we have a valid readonly case, fall through. */
952 /* (assume no half-trace half-real blocks for now) */
955 res
= prepare_to_access_memory ();
958 if (set_desired_thread (1))
959 res
= read_inferior_memory (memaddr
, myaddr
, len
);
962 done_accessing_memory ();
964 return res
== 0 ? len
: -1;
970 /* Write trace frame or inferior memory. Actually, writing to trace
971 frames is forbidden. */
974 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
976 if (current_traceframe
>= 0)
982 ret
= prepare_to_access_memory ();
985 if (set_desired_thread (1))
986 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
989 done_accessing_memory ();
995 /* Subroutine of handle_search_memory to simplify it. */
998 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
999 gdb_byte
*pattern
, unsigned pattern_len
,
1000 gdb_byte
*search_buf
,
1001 unsigned chunk_size
, unsigned search_buf_size
,
1002 CORE_ADDR
*found_addrp
)
1004 /* Prime the search buffer. */
1006 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
1009 warning ("Unable to access %ld bytes of target "
1010 "memory at 0x%lx, halting search.",
1011 (long) search_buf_size
, (long) start_addr
);
1015 /* Perform the search.
1017 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
1018 When we've scanned N bytes we copy the trailing bytes to the start and
1019 read in another N bytes. */
1021 while (search_space_len
>= pattern_len
)
1023 gdb_byte
*found_ptr
;
1024 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
1028 found_ptr
= (gdb_byte
*) memmem (search_buf
, nr_search_bytes
, pattern
,
1031 if (found_ptr
!= NULL
)
1033 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
1034 *found_addrp
= found_addr
;
1038 /* Not found in this chunk, skip to next chunk. */
1040 /* Don't let search_space_len wrap here, it's unsigned. */
1041 if (search_space_len
>= chunk_size
)
1042 search_space_len
-= chunk_size
;
1044 search_space_len
= 0;
1046 if (search_space_len
>= pattern_len
)
1048 unsigned keep_len
= search_buf_size
- chunk_size
;
1049 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
1052 /* Copy the trailing part of the previous iteration to the front
1053 of the buffer for the next iteration. */
1054 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
1056 nr_to_read
= (search_space_len
- keep_len
< chunk_size
1057 ? search_space_len
- keep_len
1060 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
1061 nr_to_read
) != search_buf_size
)
1063 warning ("Unable to access %ld bytes of target memory "
1064 "at 0x%lx, halting search.",
1065 (long) nr_to_read
, (long) read_addr
);
1069 start_addr
+= chunk_size
;
1078 /* Handle qSearch:memory packets. */
1081 handle_search_memory (char *own_buf
, int packet_len
)
1083 CORE_ADDR start_addr
;
1084 CORE_ADDR search_space_len
;
1086 unsigned int pattern_len
;
1087 /* NOTE: also defined in find.c testcase. */
1088 #define SEARCH_CHUNK_SIZE 16000
1089 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
1090 /* Buffer to hold memory contents for searching. */
1091 gdb_byte
*search_buf
;
1092 unsigned search_buf_size
;
1094 CORE_ADDR found_addr
;
1095 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
1097 pattern
= (gdb_byte
*) malloc (packet_len
);
1098 if (pattern
== NULL
)
1100 error ("Unable to allocate memory to perform the search");
1101 strcpy (own_buf
, "E00");
1104 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
1105 packet_len
- cmd_name_len
,
1106 &start_addr
, &search_space_len
,
1107 pattern
, &pattern_len
) < 0)
1110 error ("Error in parsing qSearch:memory packet");
1111 strcpy (own_buf
, "E00");
1115 search_buf_size
= chunk_size
+ pattern_len
- 1;
1117 /* No point in trying to allocate a buffer larger than the search space. */
1118 if (search_space_len
< search_buf_size
)
1119 search_buf_size
= search_space_len
;
1121 search_buf
= (gdb_byte
*) malloc (search_buf_size
);
1122 if (search_buf
== NULL
)
1125 error ("Unable to allocate memory to perform the search");
1126 strcpy (own_buf
, "E00");
1130 found
= handle_search_memory_1 (start_addr
, search_space_len
,
1131 pattern
, pattern_len
,
1132 search_buf
, chunk_size
, search_buf_size
,
1136 sprintf (own_buf
, "1,%lx", (long) found_addr
);
1137 else if (found
== 0)
1138 strcpy (own_buf
, "0");
1140 strcpy (own_buf
, "E00");
1146 #define require_running(BUF) \
1147 if (!target_running ()) \
1153 /* Parse options to --debug-format= and "monitor set debug-format".
1154 ARG is the text after "--debug-format=" or "monitor set debug-format".
1155 IS_MONITOR is non-zero if we're invoked via "monitor set debug-format".
1156 This triggers calls to monitor_output.
1157 The result is NULL if all options were parsed ok, otherwise an error
1158 message which the caller must free.
1160 N.B. These commands affect all debug format settings, they are not
1161 cumulative. If a format is not specified, it is turned off.
1162 However, we don't go to extra trouble with things like
1163 "monitor set debug-format all,none,timestamp".
1164 Instead we just parse them one at a time, in order.
1166 The syntax for "monitor set debug" we support here is not identical
1167 to gdb's "set debug foo on|off" because we also use this function to
1168 parse "--debug-format=foo,bar". */
1171 parse_debug_format_options (const char *arg
, int is_monitor
)
1173 VEC (char_ptr
) *options
;
1177 /* First turn all debug format options off. */
1178 debug_timestamp
= 0;
1180 /* First remove leading spaces, for "monitor set debug-format". */
1181 while (isspace (*arg
))
1184 options
= delim_string_to_char_ptr_vec (arg
, ',');
1186 for (ix
= 0; VEC_iterate (char_ptr
, options
, ix
, option
); ++ix
)
1188 if (strcmp (option
, "all") == 0)
1190 debug_timestamp
= 1;
1192 monitor_output ("All extra debug format options enabled.\n");
1194 else if (strcmp (option
, "none") == 0)
1196 debug_timestamp
= 0;
1198 monitor_output ("All extra debug format options disabled.\n");
1200 else if (strcmp (option
, "timestamp") == 0)
1202 debug_timestamp
= 1;
1204 monitor_output ("Timestamps will be added to debug output.\n");
1206 else if (*option
== '\0')
1208 /* An empty option, e.g., "--debug-format=foo,,bar", is ignored. */
1213 char *msg
= xstrprintf ("Unknown debug-format argument: \"%s\"\n",
1216 free_char_ptr_vec (options
);
1221 free_char_ptr_vec (options
);
1225 /* Handle monitor commands not handled by target-specific handlers. */
1228 handle_monitor_command (char *mon
, char *own_buf
)
1230 if (strcmp (mon
, "set debug 1") == 0)
1233 monitor_output ("Debug output enabled.\n");
1235 else if (strcmp (mon
, "set debug 0") == 0)
1238 monitor_output ("Debug output disabled.\n");
1240 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
1242 show_debug_regs
= 1;
1243 monitor_output ("H/W point debugging output enabled.\n");
1245 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
1247 show_debug_regs
= 0;
1248 monitor_output ("H/W point debugging output disabled.\n");
1250 else if (strcmp (mon
, "set remote-debug 1") == 0)
1253 monitor_output ("Protocol debug output enabled.\n");
1255 else if (strcmp (mon
, "set remote-debug 0") == 0)
1258 monitor_output ("Protocol debug output disabled.\n");
1260 else if (startswith (mon
, "set debug-format "))
1263 = parse_debug_format_options (mon
+ sizeof ("set debug-format ") - 1,
1266 if (error_msg
!= NULL
)
1268 monitor_output (error_msg
);
1269 monitor_show_help ();
1270 write_enn (own_buf
);
1274 else if (strcmp (mon
, "help") == 0)
1275 monitor_show_help ();
1276 else if (strcmp (mon
, "exit") == 0)
1280 monitor_output ("Unknown monitor command.\n\n");
1281 monitor_show_help ();
1282 write_enn (own_buf
);
1286 /* Associates a callback with each supported qXfer'able object. */
1290 /* The object this handler handles. */
1293 /* Request that the target transfer up to LEN 8-bit bytes of the
1294 target's OBJECT. The OFFSET, for a seekable object, specifies
1295 the starting point. The ANNEX can be used to provide additional
1296 data-specific information to the target.
1298 Return the number of bytes actually transfered, zero when no
1299 further transfer is possible, -1 on error, -2 when the transfer
1300 is not supported, and -3 on a verbose error message that should
1301 be preserved. Return of a positive value smaller than LEN does
1302 not indicate the end of the object, only the end of the transfer.
1304 One, and only one, of readbuf or writebuf must be non-NULL. */
1305 int (*xfer
) (const char *annex
,
1306 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1307 ULONGEST offset
, LONGEST len
);
1310 /* Handle qXfer:auxv:read. */
1313 handle_qxfer_auxv (const char *annex
,
1314 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1315 ULONGEST offset
, LONGEST len
)
1317 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
1320 if (annex
[0] != '\0' || current_thread
== NULL
)
1323 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
1326 /* Handle qXfer:exec-file:read. */
1329 handle_qxfer_exec_file (const char *const_annex
,
1330 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1331 ULONGEST offset
, LONGEST len
)
1337 if (the_target
->pid_to_exec_file
== NULL
|| writebuf
!= NULL
)
1340 if (const_annex
[0] == '\0')
1342 if (current_thread
== NULL
)
1345 pid
= pid_of (current_thread
);
1349 char *annex
= (char *) alloca (strlen (const_annex
) + 1);
1351 strcpy (annex
, const_annex
);
1352 annex
= unpack_varlen_hex (annex
, &pid
);
1354 if (annex
[0] != '\0')
1361 file
= (*the_target
->pid_to_exec_file
) (pid
);
1365 total_len
= strlen (file
);
1367 if (offset
> total_len
)
1370 if (offset
+ len
> total_len
)
1371 len
= total_len
- offset
;
1373 memcpy (readbuf
, file
+ offset
, len
);
1377 /* Handle qXfer:features:read. */
1380 handle_qxfer_features (const char *annex
,
1381 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1382 ULONGEST offset
, LONGEST len
)
1384 const char *document
;
1387 if (writebuf
!= NULL
)
1390 if (!target_running ())
1393 /* Grab the correct annex. */
1394 document
= get_features_xml (annex
);
1395 if (document
== NULL
)
1398 total_len
= strlen (document
);
1400 if (offset
> total_len
)
1403 if (offset
+ len
> total_len
)
1404 len
= total_len
- offset
;
1406 memcpy (readbuf
, document
+ offset
, len
);
1410 /* Worker routine for handle_qxfer_libraries.
1411 Add to the length pointed to by ARG a conservative estimate of the
1412 length needed to transmit the file name of INF. */
1415 accumulate_file_name_length (struct inferior_list_entry
*inf
, void *arg
)
1417 struct dll_info
*dll
= (struct dll_info
*) inf
;
1418 unsigned int *total_len
= (unsigned int *) arg
;
1420 /* Over-estimate the necessary memory. Assume that every character
1421 in the library name must be escaped. */
1422 *total_len
+= 128 + 6 * strlen (dll
->name
);
1425 /* Worker routine for handle_qxfer_libraries.
1426 Emit the XML to describe the library in INF. */
1429 emit_dll_description (struct inferior_list_entry
*inf
, void *arg
)
1431 struct dll_info
*dll
= (struct dll_info
*) inf
;
1432 char **p_ptr
= (char **) arg
;
1436 strcpy (p
, " <library name=\"");
1438 name
= xml_escape_text (dll
->name
);
1442 strcpy (p
, "\"><segment address=\"");
1444 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1446 strcpy (p
, "\"/></library>\n");
1452 /* Handle qXfer:libraries:read. */
1455 handle_qxfer_libraries (const char *annex
,
1456 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1457 ULONGEST offset
, LONGEST len
)
1459 unsigned int total_len
;
1462 if (writebuf
!= NULL
)
1465 if (annex
[0] != '\0' || current_thread
== NULL
)
1469 for_each_inferior_with_data (&all_dlls
, accumulate_file_name_length
,
1472 document
= (char *) malloc (total_len
);
1473 if (document
== NULL
)
1476 strcpy (document
, "<library-list version=\"1.0\">\n");
1477 p
= document
+ strlen (document
);
1479 for_each_inferior_with_data (&all_dlls
, emit_dll_description
, &p
);
1481 strcpy (p
, "</library-list>\n");
1483 total_len
= strlen (document
);
1485 if (offset
> total_len
)
1491 if (offset
+ len
> total_len
)
1492 len
= total_len
- offset
;
1494 memcpy (readbuf
, document
+ offset
, len
);
1499 /* Handle qXfer:libraries-svr4:read. */
1502 handle_qxfer_libraries_svr4 (const char *annex
,
1503 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1504 ULONGEST offset
, LONGEST len
)
1506 if (writebuf
!= NULL
)
1509 if (current_thread
== NULL
|| the_target
->qxfer_libraries_svr4
== NULL
)
1512 return the_target
->qxfer_libraries_svr4 (annex
, readbuf
, writebuf
, offset
, len
);
1515 /* Handle qXfer:osadata:read. */
1518 handle_qxfer_osdata (const char *annex
,
1519 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1520 ULONGEST offset
, LONGEST len
)
1522 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
1525 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
1528 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1531 handle_qxfer_siginfo (const char *annex
,
1532 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1533 ULONGEST offset
, LONGEST len
)
1535 if (the_target
->qxfer_siginfo
== NULL
)
1538 if (annex
[0] != '\0' || current_thread
== NULL
)
1541 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1544 /* Handle qXfer:spu:read and qXfer:spu:write. */
1547 handle_qxfer_spu (const char *annex
,
1548 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1549 ULONGEST offset
, LONGEST len
)
1551 if (the_target
->qxfer_spu
== NULL
)
1554 if (current_thread
== NULL
)
1557 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1560 /* Handle qXfer:statictrace:read. */
1563 handle_qxfer_statictrace (const char *annex
,
1564 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1565 ULONGEST offset
, LONGEST len
)
1569 if (writebuf
!= NULL
)
1572 if (annex
[0] != '\0' || current_thread
== NULL
|| current_traceframe
== -1)
1575 if (traceframe_read_sdata (current_traceframe
, offset
,
1576 readbuf
, len
, &nbytes
))
1581 /* Helper for handle_qxfer_threads_proper.
1582 Emit the XML to describe the thread of INF. */
1585 handle_qxfer_threads_worker (struct inferior_list_entry
*inf
, void *arg
)
1587 struct thread_info
*thread
= (struct thread_info
*) inf
;
1588 struct buffer
*buffer
= (struct buffer
*) arg
;
1589 ptid_t ptid
= thread_to_gdb_id (thread
);
1591 int core
= target_core_of_thread (ptid
);
1593 const char *name
= target_thread_name (ptid
);
1595 write_ptid (ptid_s
, ptid
);
1597 buffer_xml_printf (buffer
, "<thread id=\"%s\"", ptid_s
);
1601 sprintf (core_s
, "%d", core
);
1602 buffer_xml_printf (buffer
, " core=\"%s\"", core_s
);
1606 buffer_xml_printf (buffer
, " name=\"%s\"", name
);
1608 buffer_xml_printf (buffer
, "/>\n");
1611 /* Helper for handle_qxfer_threads. */
1614 handle_qxfer_threads_proper (struct buffer
*buffer
)
1616 buffer_grow_str (buffer
, "<threads>\n");
1618 for_each_inferior_with_data (&all_threads
, handle_qxfer_threads_worker
,
1621 buffer_grow_str0 (buffer
, "</threads>\n");
1624 /* Handle qXfer:threads:read. */
1627 handle_qxfer_threads (const char *annex
,
1628 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1629 ULONGEST offset
, LONGEST len
)
1631 static char *result
= 0;
1632 static unsigned int result_length
= 0;
1634 if (writebuf
!= NULL
)
1637 if (annex
[0] != '\0')
1642 struct buffer buffer
;
1643 /* When asked for data at offset 0, generate everything and store into
1644 'result'. Successive reads will be served off 'result'. */
1648 buffer_init (&buffer
);
1650 handle_qxfer_threads_proper (&buffer
);
1652 result
= buffer_finish (&buffer
);
1653 result_length
= strlen (result
);
1654 buffer_free (&buffer
);
1657 if (offset
>= result_length
)
1659 /* We're out of data. */
1666 if (len
> result_length
- offset
)
1667 len
= result_length
- offset
;
1669 memcpy (readbuf
, result
+ offset
, len
);
1674 /* Handle qXfer:traceframe-info:read. */
1677 handle_qxfer_traceframe_info (const char *annex
,
1678 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1679 ULONGEST offset
, LONGEST len
)
1681 static char *result
= 0;
1682 static unsigned int result_length
= 0;
1684 if (writebuf
!= NULL
)
1687 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1692 struct buffer buffer
;
1694 /* When asked for data at offset 0, generate everything and
1695 store into 'result'. Successive reads will be served off
1699 buffer_init (&buffer
);
1701 traceframe_read_info (current_traceframe
, &buffer
);
1703 result
= buffer_finish (&buffer
);
1704 result_length
= strlen (result
);
1705 buffer_free (&buffer
);
1708 if (offset
>= result_length
)
1710 /* We're out of data. */
1717 if (len
> result_length
- offset
)
1718 len
= result_length
- offset
;
1720 memcpy (readbuf
, result
+ offset
, len
);
1724 /* Handle qXfer:fdpic:read. */
1727 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1728 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1730 if (the_target
->read_loadmap
== NULL
)
1733 if (current_thread
== NULL
)
1736 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1739 /* Handle qXfer:btrace:read. */
1742 handle_qxfer_btrace (const char *annex
,
1743 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1744 ULONGEST offset
, LONGEST len
)
1746 static struct buffer cache
;
1747 struct thread_info
*thread
;
1748 enum btrace_read_type type
;
1751 if (the_target
->read_btrace
== NULL
|| writebuf
!= NULL
)
1754 if (ptid_equal (general_thread
, null_ptid
)
1755 || ptid_equal (general_thread
, minus_one_ptid
))
1757 strcpy (own_buf
, "E.Must select a single thread.");
1761 thread
= find_thread_ptid (general_thread
);
1764 strcpy (own_buf
, "E.No such thread.");
1768 if (thread
->btrace
== NULL
)
1770 strcpy (own_buf
, "E.Btrace not enabled.");
1774 if (strcmp (annex
, "all") == 0)
1775 type
= BTRACE_READ_ALL
;
1776 else if (strcmp (annex
, "new") == 0)
1777 type
= BTRACE_READ_NEW
;
1778 else if (strcmp (annex
, "delta") == 0)
1779 type
= BTRACE_READ_DELTA
;
1782 strcpy (own_buf
, "E.Bad annex.");
1788 buffer_free (&cache
);
1790 result
= target_read_btrace (thread
->btrace
, &cache
, type
);
1793 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1797 else if (offset
> cache
.used_size
)
1799 buffer_free (&cache
);
1803 if (len
> cache
.used_size
- offset
)
1804 len
= cache
.used_size
- offset
;
1806 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1811 /* Handle qXfer:btrace-conf:read. */
1814 handle_qxfer_btrace_conf (const char *annex
,
1815 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1816 ULONGEST offset
, LONGEST len
)
1818 static struct buffer cache
;
1819 struct thread_info
*thread
;
1822 if (the_target
->read_btrace_conf
== NULL
|| writebuf
!= NULL
)
1825 if (annex
[0] != '\0')
1828 if (ptid_equal (general_thread
, null_ptid
)
1829 || ptid_equal (general_thread
, minus_one_ptid
))
1831 strcpy (own_buf
, "E.Must select a single thread.");
1835 thread
= find_thread_ptid (general_thread
);
1838 strcpy (own_buf
, "E.No such thread.");
1842 if (thread
->btrace
== NULL
)
1844 strcpy (own_buf
, "E.Btrace not enabled.");
1850 buffer_free (&cache
);
1852 result
= target_read_btrace_conf (thread
->btrace
, &cache
);
1855 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1859 else if (offset
> cache
.used_size
)
1861 buffer_free (&cache
);
1865 if (len
> cache
.used_size
- offset
)
1866 len
= cache
.used_size
- offset
;
1868 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1873 static const struct qxfer qxfer_packets
[] =
1875 { "auxv", handle_qxfer_auxv
},
1876 { "btrace", handle_qxfer_btrace
},
1877 { "btrace-conf", handle_qxfer_btrace_conf
},
1878 { "exec-file", handle_qxfer_exec_file
},
1879 { "fdpic", handle_qxfer_fdpic
},
1880 { "features", handle_qxfer_features
},
1881 { "libraries", handle_qxfer_libraries
},
1882 { "libraries-svr4", handle_qxfer_libraries_svr4
},
1883 { "osdata", handle_qxfer_osdata
},
1884 { "siginfo", handle_qxfer_siginfo
},
1885 { "spu", handle_qxfer_spu
},
1886 { "statictrace", handle_qxfer_statictrace
},
1887 { "threads", handle_qxfer_threads
},
1888 { "traceframe-info", handle_qxfer_traceframe_info
},
1892 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1900 if (!startswith (own_buf
, "qXfer:"))
1903 /* Grab the object, r/w and annex. */
1904 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1906 write_enn (own_buf
);
1911 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1914 const struct qxfer
*q
= &qxfer_packets
[i
];
1916 if (strcmp (object
, q
->object
) == 0)
1918 if (strcmp (rw
, "read") == 0)
1920 unsigned char *data
;
1925 /* Grab the offset and length. */
1926 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1928 write_enn (own_buf
);
1932 /* Read one extra byte, as an indicator of whether there is
1934 if (len
> PBUFSIZ
- 2)
1936 data
= (unsigned char *) malloc (len
+ 1);
1939 write_enn (own_buf
);
1942 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1950 /* Preserve error message. */
1953 write_enn (own_buf
);
1955 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1957 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1962 else if (strcmp (rw
, "write") == 0)
1967 unsigned char *data
;
1969 strcpy (own_buf
, "E00");
1970 data
= (unsigned char *) malloc (packet_len
- (offset
- own_buf
));
1973 write_enn (own_buf
);
1976 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1977 &ofs
, &len
, data
) < 0)
1980 write_enn (own_buf
);
1984 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1992 /* Preserve error message. */
1995 write_enn (own_buf
);
1997 sprintf (own_buf
, "%x", n
);
2010 /* Compute 32 bit CRC from inferior memory.
2012 On success, return 32 bit CRC.
2013 On failure, return (unsigned long long) -1. */
2015 static unsigned long long
2016 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
2020 unsigned char byte
= 0;
2022 /* Return failure if memory read fails. */
2023 if (read_inferior_memory (base
, &byte
, 1) != 0)
2024 return (unsigned long long) -1;
2026 crc
= xcrc32 (&byte
, 1, crc
);
2029 return (unsigned long long) crc
;
2032 /* Add supported btrace packets to BUF. */
2035 supported_btrace_packets (char *buf
)
2037 int btrace_supported
= 0;
2039 if (target_supports_btrace (BTRACE_FORMAT_BTS
))
2041 strcat (buf
, ";Qbtrace:bts+");
2042 strcat (buf
, ";Qbtrace-conf:bts:size+");
2044 btrace_supported
= 1;
2047 if (target_supports_btrace (BTRACE_FORMAT_PT
))
2049 strcat (buf
, ";Qbtrace:pt+");
2050 strcat (buf
, ";Qbtrace-conf:pt:size+");
2052 btrace_supported
= 1;
2055 if (!btrace_supported
)
2058 strcat (buf
, ";Qbtrace:off+");
2059 strcat (buf
, ";qXfer:btrace:read+");
2060 strcat (buf
, ";qXfer:btrace-conf:read+");
2063 /* Handle all of the extended 'q' packets. */
2066 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
2068 static struct inferior_list_entry
*thread_ptr
;
2070 /* Reply the current thread id. */
2071 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
2074 require_running (own_buf
);
2076 if (!ptid_equal (general_thread
, null_ptid
)
2077 && !ptid_equal (general_thread
, minus_one_ptid
))
2078 gdb_id
= general_thread
;
2081 thread_ptr
= get_first_inferior (&all_threads
);
2082 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
2085 sprintf (own_buf
, "QC");
2087 write_ptid (own_buf
, gdb_id
);
2091 if (strcmp ("qSymbol::", own_buf
) == 0)
2093 struct thread_info
*save_thread
= current_thread
;
2095 /* For qSymbol, GDB only changes the current thread if the
2096 previous current thread was of a different process. So if
2097 the previous thread is gone, we need to pick another one of
2098 the same process. This can happen e.g., if we followed an
2099 exec in a non-leader thread. */
2100 if (current_thread
== NULL
)
2103 = find_any_thread_of_pid (ptid_get_pid (general_thread
));
2105 /* Just in case, if we didn't find a thread, then bail out
2106 instead of crashing. */
2107 if (current_thread
== NULL
)
2109 write_enn (own_buf
);
2110 current_thread
= save_thread
;
2115 /* GDB is suggesting new symbols have been loaded. This may
2116 mean a new shared library has been detected as loaded, so
2117 take the opportunity to check if breakpoints we think are
2118 inserted, still are. Note that it isn't guaranteed that
2119 we'll see this when a shared library is loaded, and nor will
2120 we see this for unloads (although breakpoints in unloaded
2121 libraries shouldn't trigger), as GDB may not find symbols for
2122 the library at all. We also re-validate breakpoints when we
2123 see a second GDB breakpoint for the same address, and or when
2124 we access breakpoint shadows. */
2125 validate_breakpoints ();
2127 if (target_supports_tracepoints ())
2128 tracepoint_look_up_symbols ();
2130 if (current_thread
!= NULL
&& the_target
->look_up_symbols
!= NULL
)
2131 (*the_target
->look_up_symbols
) ();
2133 current_thread
= save_thread
;
2135 strcpy (own_buf
, "OK");
2139 if (!disable_packet_qfThreadInfo
)
2141 if (strcmp ("qfThreadInfo", own_buf
) == 0)
2145 require_running (own_buf
);
2146 thread_ptr
= get_first_inferior (&all_threads
);
2149 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
2150 write_ptid (own_buf
, gdb_id
);
2151 thread_ptr
= thread_ptr
->next
;
2155 if (strcmp ("qsThreadInfo", own_buf
) == 0)
2159 require_running (own_buf
);
2160 if (thread_ptr
!= NULL
)
2163 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
2164 write_ptid (own_buf
, gdb_id
);
2165 thread_ptr
= thread_ptr
->next
;
2170 sprintf (own_buf
, "l");
2176 if (the_target
->read_offsets
!= NULL
2177 && strcmp ("qOffsets", own_buf
) == 0)
2179 CORE_ADDR text
, data
;
2181 require_running (own_buf
);
2182 if (the_target
->read_offsets (&text
, &data
))
2183 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
2184 (long)text
, (long)data
, (long)data
);
2186 write_enn (own_buf
);
2191 /* Protocol features query. */
2192 if (startswith (own_buf
, "qSupported")
2193 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
2195 char *p
= &own_buf
[10];
2196 int gdb_supports_qRelocInsn
= 0;
2198 /* Process each feature being provided by GDB. The first
2199 feature will follow a ':', and latter features will follow
2203 char **qsupported
= NULL
;
2208 /* Two passes, to avoid nested strtok calls in
2209 target_process_qsupported. */
2210 for (p
= strtok (p
+ 1, ";");
2212 p
= strtok (NULL
, ";"))
2215 qsupported
= XRESIZEVEC (char *, qsupported
, count
);
2216 qsupported
[count
- 1] = xstrdup (p
);
2219 for (i
= 0; i
< count
; i
++)
2222 if (strcmp (p
, "multiprocess+") == 0)
2224 /* GDB supports and wants multi-process support if
2226 if (target_supports_multi_process ())
2229 else if (strcmp (p
, "qRelocInsn+") == 0)
2231 /* GDB supports relocate instruction requests. */
2232 gdb_supports_qRelocInsn
= 1;
2234 else if (strcmp (p
, "swbreak+") == 0)
2236 /* GDB wants us to report whether a trap is caused
2237 by a software breakpoint and for us to handle PC
2238 adjustment if necessary on this target. */
2239 if (target_supports_stopped_by_sw_breakpoint ())
2240 swbreak_feature
= 1;
2242 else if (strcmp (p
, "hwbreak+") == 0)
2244 /* GDB wants us to report whether a trap is caused
2245 by a hardware breakpoint. */
2246 if (target_supports_stopped_by_hw_breakpoint ())
2247 hwbreak_feature
= 1;
2249 else if (strcmp (p
, "fork-events+") == 0)
2251 /* GDB supports and wants fork events if possible. */
2252 if (target_supports_fork_events ())
2253 report_fork_events
= 1;
2255 else if (strcmp (p
, "vfork-events+") == 0)
2257 /* GDB supports and wants vfork events if possible. */
2258 if (target_supports_vfork_events ())
2259 report_vfork_events
= 1;
2261 else if (strcmp (p
, "exec-events+") == 0)
2263 /* GDB supports and wants exec events if possible. */
2264 if (target_supports_exec_events ())
2265 report_exec_events
= 1;
2267 else if (strcmp (p
, "vContSupported+") == 0)
2268 vCont_supported
= 1;
2269 else if (strcmp (p
, "QThreadEvents+") == 0)
2271 else if (strcmp (p
, "no-resumed+") == 0)
2273 /* GDB supports and wants TARGET_WAITKIND_NO_RESUMED
2275 report_no_resumed
= 1;
2279 /* Move the unknown features all together. */
2280 qsupported
[i
] = NULL
;
2281 qsupported
[unknown
] = p
;
2286 /* Give the target backend a chance to process the unknown
2288 target_process_qsupported (qsupported
, unknown
);
2290 for (i
= 0; i
< count
; i
++)
2291 free (qsupported
[i
]);
2296 "PacketSize=%x;QPassSignals+;QProgramSignals+;"
2297 "QStartupWithShell+;QEnvironmentHexEncoded+;"
2298 "QEnvironmentReset+;QEnvironmentUnset+",
2301 if (target_supports_catch_syscall ())
2302 strcat (own_buf
, ";QCatchSyscalls+");
2304 if (the_target
->qxfer_libraries_svr4
!= NULL
)
2305 strcat (own_buf
, ";qXfer:libraries-svr4:read+"
2306 ";augmented-libraries-svr4-read+");
2309 /* We do not have any hook to indicate whether the non-SVR4 target
2310 backend supports qXfer:libraries:read, so always report it. */
2311 strcat (own_buf
, ";qXfer:libraries:read+");
2314 if (the_target
->read_auxv
!= NULL
)
2315 strcat (own_buf
, ";qXfer:auxv:read+");
2317 if (the_target
->qxfer_spu
!= NULL
)
2318 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
2320 if (the_target
->qxfer_siginfo
!= NULL
)
2321 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
2323 if (the_target
->read_loadmap
!= NULL
)
2324 strcat (own_buf
, ";qXfer:fdpic:read+");
2326 /* We always report qXfer:features:read, as targets may
2327 install XML files on a subsequent call to arch_setup.
2328 If we reported to GDB on startup that we don't support
2329 qXfer:feature:read at all, we will never be re-queried. */
2330 strcat (own_buf
, ";qXfer:features:read+");
2332 if (transport_is_reliable
)
2333 strcat (own_buf
, ";QStartNoAckMode+");
2335 if (the_target
->qxfer_osdata
!= NULL
)
2336 strcat (own_buf
, ";qXfer:osdata:read+");
2338 if (target_supports_multi_process ())
2339 strcat (own_buf
, ";multiprocess+");
2341 if (target_supports_fork_events ())
2342 strcat (own_buf
, ";fork-events+");
2344 if (target_supports_vfork_events ())
2345 strcat (own_buf
, ";vfork-events+");
2347 if (target_supports_exec_events ())
2348 strcat (own_buf
, ";exec-events+");
2350 if (target_supports_non_stop ())
2351 strcat (own_buf
, ";QNonStop+");
2353 if (target_supports_disable_randomization ())
2354 strcat (own_buf
, ";QDisableRandomization+");
2356 strcat (own_buf
, ";qXfer:threads:read+");
2358 if (target_supports_tracepoints ())
2360 strcat (own_buf
, ";ConditionalTracepoints+");
2361 strcat (own_buf
, ";TraceStateVariables+");
2362 strcat (own_buf
, ";TracepointSource+");
2363 strcat (own_buf
, ";DisconnectedTracing+");
2364 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
2365 strcat (own_buf
, ";FastTracepoints+");
2366 strcat (own_buf
, ";StaticTracepoints+");
2367 strcat (own_buf
, ";InstallInTrace+");
2368 strcat (own_buf
, ";qXfer:statictrace:read+");
2369 strcat (own_buf
, ";qXfer:traceframe-info:read+");
2370 strcat (own_buf
, ";EnableDisableTracepoints+");
2371 strcat (own_buf
, ";QTBuffer:size+");
2372 strcat (own_buf
, ";tracenz+");
2375 if (target_supports_hardware_single_step ()
2376 || target_supports_software_single_step () )
2378 strcat (own_buf
, ";ConditionalBreakpoints+");
2380 strcat (own_buf
, ";BreakpointCommands+");
2382 if (target_supports_agent ())
2383 strcat (own_buf
, ";QAgent+");
2385 supported_btrace_packets (own_buf
);
2387 if (target_supports_stopped_by_sw_breakpoint ())
2388 strcat (own_buf
, ";swbreak+");
2390 if (target_supports_stopped_by_hw_breakpoint ())
2391 strcat (own_buf
, ";hwbreak+");
2393 if (the_target
->pid_to_exec_file
!= NULL
)
2394 strcat (own_buf
, ";qXfer:exec-file:read+");
2396 strcat (own_buf
, ";vContSupported+");
2398 strcat (own_buf
, ";QThreadEvents+");
2400 strcat (own_buf
, ";no-resumed+");
2402 /* Reinitialize components as needed for the new connection. */
2403 hostio_handle_new_gdb_connection ();
2404 target_handle_new_gdb_connection ();
2409 /* Thread-local storage support. */
2410 if (the_target
->get_tls_address
!= NULL
2411 && startswith (own_buf
, "qGetTLSAddr:"))
2413 char *p
= own_buf
+ 12;
2414 CORE_ADDR parts
[2], address
= 0;
2416 ptid_t ptid
= null_ptid
;
2418 require_running (own_buf
);
2420 for (i
= 0; i
< 3; i
++)
2428 p2
= strchr (p
, ',');
2441 ptid
= read_ptid (p
, NULL
);
2443 decode_address (&parts
[i
- 1], p
, len
);
2447 if (p
!= NULL
|| i
< 3)
2451 struct thread_info
*thread
= find_thread_ptid (ptid
);
2456 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
2462 strcpy (own_buf
, paddress(address
));
2467 write_enn (own_buf
);
2471 /* Otherwise, pretend we do not understand this packet. */
2474 /* Windows OS Thread Information Block address support. */
2475 if (the_target
->get_tib_address
!= NULL
2476 && startswith (own_buf
, "qGetTIBAddr:"))
2481 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
2483 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
2486 strcpy (own_buf
, paddress(tlb
));
2491 write_enn (own_buf
);
2497 /* Handle "monitor" commands. */
2498 if (startswith (own_buf
, "qRcmd,"))
2500 char *mon
= (char *) malloc (PBUFSIZ
);
2501 int len
= strlen (own_buf
+ 6);
2505 write_enn (own_buf
);
2510 || hex2bin (own_buf
+ 6, (gdb_byte
*) mon
, len
/ 2) != len
/ 2)
2512 write_enn (own_buf
);
2516 mon
[len
/ 2] = '\0';
2520 if (the_target
->handle_monitor_command
== NULL
2521 || (*the_target
->handle_monitor_command
) (mon
) == 0)
2522 /* Default processing. */
2523 handle_monitor_command (mon
, own_buf
);
2529 if (startswith (own_buf
, "qSearch:memory:"))
2531 require_running (own_buf
);
2532 handle_search_memory (own_buf
, packet_len
);
2536 if (strcmp (own_buf
, "qAttached") == 0
2537 || startswith (own_buf
, "qAttached:"))
2539 struct process_info
*process
;
2541 if (own_buf
[sizeof ("qAttached") - 1])
2543 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
2544 process
= (struct process_info
*)
2545 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
2549 require_running (own_buf
);
2550 process
= current_process ();
2553 if (process
== NULL
)
2555 write_enn (own_buf
);
2559 strcpy (own_buf
, process
->attached
? "1" : "0");
2563 if (startswith (own_buf
, "qCRC:"))
2565 /* CRC check (compare-section). */
2569 unsigned long long crc
;
2571 require_running (own_buf
);
2572 comma
= unpack_varlen_hex (own_buf
+ 5, &base
);
2573 if (*comma
++ != ',')
2575 write_enn (own_buf
);
2578 len
= strtoul (comma
, NULL
, 16);
2579 crc
= crc32 (base
, len
, 0xffffffff);
2580 /* Check for memory failure. */
2581 if (crc
== (unsigned long long) -1)
2583 write_enn (own_buf
);
2586 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
2590 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
2593 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
2596 /* Otherwise we didn't know what packet it was. Say we didn't
2601 static void gdb_wants_all_threads_stopped (void);
2602 static void resume (struct thread_resume
*actions
, size_t n
);
2604 /* The callback that is passed to visit_actioned_threads. */
2605 typedef int (visit_actioned_threads_callback_ftype
)
2606 (const struct thread_resume
*, struct thread_info
*);
2608 /* Struct to pass data to visit_actioned_threads. */
2610 struct visit_actioned_threads_data
2612 const struct thread_resume
*actions
;
2614 visit_actioned_threads_callback_ftype
*callback
;
2617 /* Call CALLBACK for any thread to which ACTIONS applies to. Returns
2618 true if CALLBACK returns true. Returns false if no matching thread
2619 is found or CALLBACK results false.
2620 Note: This function is itself a callback for find_inferior. */
2623 visit_actioned_threads (struct inferior_list_entry
*entry
, void *datap
)
2625 struct visit_actioned_threads_data
*data
2626 = (struct visit_actioned_threads_data
*) datap
;
2627 const struct thread_resume
*actions
= data
->actions
;
2628 size_t num_actions
= data
->num_actions
;
2629 visit_actioned_threads_callback_ftype
*callback
= data
->callback
;
2632 for (i
= 0; i
< num_actions
; i
++)
2634 const struct thread_resume
*action
= &actions
[i
];
2636 if (ptid_equal (action
->thread
, minus_one_ptid
)
2637 || ptid_equal (action
->thread
, entry
->id
)
2638 || ((ptid_get_pid (action
->thread
)
2639 == ptid_get_pid (entry
->id
))
2640 && ptid_get_lwp (action
->thread
) == -1))
2642 struct thread_info
*thread
= (struct thread_info
*) entry
;
2644 if ((*callback
) (action
, thread
))
2652 /* Callback for visit_actioned_threads. If the thread has a pending
2653 status to report, report it now. */
2656 handle_pending_status (const struct thread_resume
*resumption
,
2657 struct thread_info
*thread
)
2659 if (thread
->status_pending_p
)
2661 thread
->status_pending_p
= 0;
2663 last_status
= thread
->last_status
;
2664 last_ptid
= thread
->entry
.id
;
2665 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2671 /* Parse vCont packets. */
2673 handle_v_cont (char *own_buf
)
2677 struct thread_resume
*resume_info
;
2678 struct thread_resume default_action
{ null_ptid
};
2680 /* Count the number of semicolons in the packet. There should be one
2681 for every action. */
2687 p
= strchr (p
, ';');
2690 resume_info
= (struct thread_resume
*) malloc (n
* sizeof (resume_info
[0]));
2691 if (resume_info
== NULL
)
2699 memset (&resume_info
[i
], 0, sizeof resume_info
[i
]);
2701 if (p
[0] == 's' || p
[0] == 'S')
2702 resume_info
[i
].kind
= resume_step
;
2703 else if (p
[0] == 'r')
2704 resume_info
[i
].kind
= resume_step
;
2705 else if (p
[0] == 'c' || p
[0] == 'C')
2706 resume_info
[i
].kind
= resume_continue
;
2707 else if (p
[0] == 't')
2708 resume_info
[i
].kind
= resume_stop
;
2712 if (p
[0] == 'S' || p
[0] == 'C')
2715 sig
= strtol (p
+ 1, &q
, 16);
2720 if (!gdb_signal_to_host_p ((enum gdb_signal
) sig
))
2722 resume_info
[i
].sig
= gdb_signal_to_host ((enum gdb_signal
) sig
);
2724 else if (p
[0] == 'r')
2728 p
= unpack_varlen_hex (p
+ 1, &addr
);
2729 resume_info
[i
].step_range_start
= addr
;
2734 p
= unpack_varlen_hex (p
+ 1, &addr
);
2735 resume_info
[i
].step_range_end
= addr
;
2744 resume_info
[i
].thread
= minus_one_ptid
;
2745 default_action
= resume_info
[i
];
2747 /* Note: we don't increment i here, we'll overwrite this entry
2748 the next time through. */
2750 else if (p
[0] == ':')
2752 ptid_t ptid
= read_ptid (p
+ 1, &q
);
2757 if (p
[0] != ';' && p
[0] != 0)
2760 resume_info
[i
].thread
= ptid
;
2767 resume_info
[i
] = default_action
;
2769 resume (resume_info
, n
);
2774 write_enn (own_buf
);
2779 /* Resume target with ACTIONS, an array of NUM_ACTIONS elements. */
2782 resume (struct thread_resume
*actions
, size_t num_actions
)
2786 /* Check if among the threads that GDB wants actioned, there's
2787 one with a pending status to report. If so, skip actually
2788 resuming/stopping and report the pending event
2790 struct visit_actioned_threads_data data
;
2792 data
.actions
= actions
;
2793 data
.num_actions
= num_actions
;
2794 data
.callback
= handle_pending_status
;
2795 if (find_inferior (&all_threads
, visit_actioned_threads
, &data
) != NULL
)
2801 (*the_target
->resume
) (actions
, num_actions
);
2807 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2809 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
2810 && !report_no_resumed
)
2812 /* The client does not support this stop reply. At least
2814 sprintf (own_buf
, "E.No unwaited-for children left.");
2815 disable_async_io ();
2819 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2820 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
2821 && last_status
.kind
!= TARGET_WAITKIND_NO_RESUMED
)
2822 current_thread
->last_status
= last_status
;
2824 /* From the client's perspective, all-stop mode always stops all
2825 threads implicitly (and the target backend has already done
2826 so by now). Tag all threads as "want-stopped", so we don't
2827 resume them implicitly without the client telling us to. */
2828 gdb_wants_all_threads_stopped ();
2829 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2830 disable_async_io ();
2832 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2833 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2834 target_mourn_inferior (last_ptid
);
2838 /* Attach to a new program. Return 1 if successful, 0 if failure. */
2840 handle_v_attach (char *own_buf
)
2844 pid
= strtol (own_buf
+ 8, NULL
, 16);
2845 if (pid
!= 0 && attach_inferior (pid
) == 0)
2847 /* Don't report shared library events after attaching, even if
2848 some libraries are preloaded. GDB will always poll the
2849 library list. Avoids the "stopped by shared library event"
2850 notice on the GDB side. */
2855 /* In non-stop, we don't send a resume reply. Stop events
2856 will follow up using the normal notification
2861 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2867 write_enn (own_buf
);
2872 /* Run a new program. Return 1 if successful, 0 if failure. */
2874 handle_v_run (char *own_buf
)
2877 std::vector
<char *> new_argv
;
2878 char *new_program_name
= NULL
;
2882 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
2888 for (i
= 0, p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
, ++i
)
2890 next_p
= strchr (p
, ';');
2892 next_p
= p
+ strlen (p
);
2894 if (i
== 0 && p
== next_p
)
2896 /* No program specified. */
2897 new_program_name
= NULL
;
2899 else if (p
== next_p
)
2901 /* Empty argument. */
2902 new_argv
.push_back (xstrdup ("''"));
2906 size_t len
= (next_p
- p
) / 2;
2907 /* ARG is the unquoted argument received via the RSP. */
2908 char *arg
= (char *) xmalloc (len
+ 1);
2909 /* FULL_ARGS will contain the quoted version of ARG. */
2910 char *full_arg
= (char *) xmalloc ((len
+ 1) * 2);
2911 /* These are pointers used to navigate the strings above. */
2912 char *tmp_arg
= arg
;
2913 char *tmp_full_arg
= full_arg
;
2916 hex2bin (p
, (gdb_byte
*) arg
, len
);
2919 while (*tmp_arg
!= '\0')
2925 *tmp_full_arg
= '\'';
2931 /* Quote single quote. */
2932 *tmp_full_arg
= '\\';
2940 *tmp_full_arg
= *tmp_arg
;
2946 *tmp_full_arg
++ = '\'';
2948 /* Finish FULL_ARG and push it into the vector containing
2950 *tmp_full_arg
= '\0';
2952 new_program_name
= full_arg
;
2954 new_argv
.push_back (full_arg
);
2960 new_argv
.push_back (NULL
);
2962 if (new_program_name
== NULL
)
2964 /* GDB didn't specify a program to run. Use the program from the
2965 last run with the new argument list. */
2966 if (program_name
== NULL
)
2968 write_enn (own_buf
);
2969 free_vector_argv (new_argv
);
2975 xfree (program_name
);
2976 program_name
= new_program_name
;
2979 /* Free the old argv and install the new one. */
2980 free_vector_argv (program_args
);
2981 program_args
= new_argv
;
2983 create_inferior (program_name
, program_args
);
2985 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
2987 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2989 /* In non-stop, sending a resume reply doesn't set the general
2990 thread, but GDB assumes a vRun sets it (this is so GDB can
2991 query which is the main thread of the new inferior. */
2993 general_thread
= last_ptid
;
2999 write_enn (own_buf
);
3004 /* Kill process. Return 1 if successful, 0 if failure. */
3006 handle_v_kill (char *own_buf
)
3009 char *p
= &own_buf
[6];
3011 pid
= strtol (p
, NULL
, 16);
3014 if (pid
!= 0 && kill_inferior (pid
) == 0)
3016 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
3017 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
3018 last_ptid
= pid_to_ptid (pid
);
3019 discard_queued_stop_replies (last_ptid
);
3025 write_enn (own_buf
);
3030 /* Handle all of the extended 'v' packets. */
3032 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
3034 if (!disable_packet_vCont
)
3036 if (strcmp (own_buf
, "vCtrlC") == 0)
3038 (*the_target
->request_interrupt
) ();
3043 if (startswith (own_buf
, "vCont;"))
3045 handle_v_cont (own_buf
);
3049 if (startswith (own_buf
, "vCont?"))
3051 strcpy (own_buf
, "vCont;c;C;t");
3053 if (target_supports_hardware_single_step ()
3054 || target_supports_software_single_step ()
3055 || !vCont_supported
)
3057 /* If target supports single step either by hardware or by
3058 software, add actions s and S to the list of supported
3059 actions. On the other hand, if GDB doesn't request the
3060 supported vCont actions in qSupported packet, add s and
3061 S to the list too. */
3062 own_buf
= own_buf
+ strlen (own_buf
);
3063 strcpy (own_buf
, ";s;S");
3066 if (target_supports_range_stepping ())
3068 own_buf
= own_buf
+ strlen (own_buf
);
3069 strcpy (own_buf
, ";r");
3075 if (startswith (own_buf
, "vFile:")
3076 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
3079 if (startswith (own_buf
, "vAttach;"))
3081 if ((!extended_protocol
|| !multi_process
) && target_running ())
3083 fprintf (stderr
, "Already debugging a process\n");
3084 write_enn (own_buf
);
3087 handle_v_attach (own_buf
);
3091 if (startswith (own_buf
, "vRun;"))
3093 if ((!extended_protocol
|| !multi_process
) && target_running ())
3095 fprintf (stderr
, "Already debugging a process\n");
3096 write_enn (own_buf
);
3099 handle_v_run (own_buf
);
3103 if (startswith (own_buf
, "vKill;"))
3105 if (!target_running ())
3107 fprintf (stderr
, "No process to kill\n");
3108 write_enn (own_buf
);
3111 handle_v_kill (own_buf
);
3115 if (handle_notif_ack (own_buf
, packet_len
))
3118 /* Otherwise we didn't know what packet it was. Say we didn't
3124 /* Resume thread and wait for another event. In non-stop mode,
3125 don't really wait here, but return immediatelly to the event
3128 myresume (char *own_buf
, int step
, int sig
)
3130 struct thread_resume resume_info
[2];
3132 int valid_cont_thread
;
3134 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
3135 && !ptid_equal (cont_thread
, minus_one_ptid
));
3137 if (step
|| sig
|| valid_cont_thread
)
3139 resume_info
[0].thread
= current_ptid
;
3141 resume_info
[0].kind
= resume_step
;
3143 resume_info
[0].kind
= resume_continue
;
3144 resume_info
[0].sig
= sig
;
3148 if (!valid_cont_thread
)
3150 resume_info
[n
].thread
= minus_one_ptid
;
3151 resume_info
[n
].kind
= resume_continue
;
3152 resume_info
[n
].sig
= 0;
3156 resume (resume_info
, n
);
3159 /* Callback for for_each_inferior. Make a new stop reply for each
3163 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
3165 struct thread_info
*thread
= (struct thread_info
*) entry
;
3167 /* For now, assume targets that don't have this callback also don't
3168 manage the thread's last_status field. */
3169 if (the_target
->thread_stopped
== NULL
)
3171 struct vstop_notif
*new_notif
= XNEW (struct vstop_notif
);
3173 new_notif
->ptid
= entry
->id
;
3174 new_notif
->status
= thread
->last_status
;
3175 /* Pass the last stop reply back to GDB, but don't notify
3177 notif_event_enque (¬if_stop
,
3178 (struct notif_event
*) new_notif
);
3182 if (thread_stopped (thread
))
3186 std::string status_string
3187 = target_waitstatus_to_string (&thread
->last_status
);
3189 debug_printf ("Reporting thread %s as already stopped with %s\n",
3190 target_pid_to_str (entry
->id
),
3191 status_string
.c_str ());
3194 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
3196 /* Pass the last stop reply back to GDB, but don't notify
3198 queue_stop_reply (entry
->id
, &thread
->last_status
);
3205 /* Set this inferior threads's state as "want-stopped". We won't
3206 resume this thread until the client gives us another action for
3210 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
3212 struct thread_info
*thread
= (struct thread_info
*) entry
;
3214 thread
->last_resume_kind
= resume_stop
;
3216 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
3218 /* Most threads are stopped implicitly (all-stop); tag that with
3220 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
3221 thread
->last_status
.value
.sig
= GDB_SIGNAL_0
;
3225 /* Set all threads' states as "want-stopped". */
3228 gdb_wants_all_threads_stopped (void)
3230 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
3233 /* Clear the gdb_detached flag of every process. */
3236 gdb_reattached_process (struct inferior_list_entry
*entry
)
3238 struct process_info
*process
= (struct process_info
*) entry
;
3240 process
->gdb_detached
= 0;
3243 /* Callback for for_each_inferior. Clear the thread's pending status
3247 clear_pending_status_callback (struct inferior_list_entry
*entry
)
3249 struct thread_info
*thread
= (struct thread_info
*) entry
;
3251 thread
->status_pending_p
= 0;
3254 /* Callback for for_each_inferior. If the thread is stopped with an
3255 interesting event, mark it as having a pending event. */
3258 set_pending_status_callback (struct inferior_list_entry
*entry
)
3260 struct thread_info
*thread
= (struct thread_info
*) entry
;
3262 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
3263 || (thread
->last_status
.value
.sig
!= GDB_SIGNAL_0
3264 /* A breakpoint, watchpoint or finished step from a previous
3265 GDB run isn't considered interesting for a new GDB run.
3266 If we left those pending, the new GDB could consider them
3267 random SIGTRAPs. This leaves out real async traps. We'd
3268 have to peek into the (target-specific) siginfo to
3269 distinguish those. */
3270 && thread
->last_status
.value
.sig
!= GDB_SIGNAL_TRAP
))
3271 thread
->status_pending_p
= 1;
3274 /* Callback for find_inferior. Return true if ENTRY (a thread) has a
3275 pending status to report to GDB. */
3278 find_status_pending_thread_callback (struct inferior_list_entry
*entry
, void *data
)
3280 struct thread_info
*thread
= (struct thread_info
*) entry
;
3282 return thread
->status_pending_p
;
3285 /* Status handler for the '?' packet. */
3288 handle_status (char *own_buf
)
3290 /* GDB is connected, don't forward events to the target anymore. */
3291 for_each_inferior (&all_processes
, gdb_reattached_process
);
3293 /* In non-stop mode, we must send a stop reply for each stopped
3294 thread. In all-stop mode, just send one for the first stopped
3299 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
3301 /* The first is sent immediatly. OK is sent if there is no
3302 stopped thread, which is the same handling of the vStopped
3303 packet (by design). */
3304 notif_write_event (¬if_stop
, own_buf
);
3308 struct inferior_list_entry
*thread
= NULL
;
3311 stabilize_threads ();
3312 gdb_wants_all_threads_stopped ();
3314 /* We can only report one status, but we might be coming out of
3315 non-stop -- if more than one thread is stopped with
3316 interesting events, leave events for the threads we're not
3317 reporting now pending. They'll be reported the next time the
3318 threads are resumed. Start by marking all interesting events
3320 for_each_inferior (&all_threads
, set_pending_status_callback
);
3322 /* Prefer the last thread that reported an event to GDB (even if
3323 that was a GDB_SIGNAL_TRAP). */
3324 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
3325 && last_status
.kind
!= TARGET_WAITKIND_EXITED
3326 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
3327 thread
= find_inferior_id (&all_threads
, last_ptid
);
3329 /* If the last event thread is not found for some reason, look
3330 for some other thread that might have an event to report. */
3332 thread
= find_inferior (&all_threads
,
3333 find_status_pending_thread_callback
, NULL
);
3335 /* If we're still out of luck, simply pick the first thread in
3338 thread
= get_first_inferior (&all_threads
);
3342 struct thread_info
*tp
= (struct thread_info
*) thread
;
3344 /* We're reporting this event, so it's no longer
3346 tp
->status_pending_p
= 0;
3348 /* GDB assumes the current thread is the thread we're
3349 reporting the status for. */
3350 general_thread
= thread
->id
;
3351 set_desired_thread (1);
3353 gdb_assert (tp
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
3354 prepare_resume_reply (own_buf
, tp
->entry
.id
, &tp
->last_status
);
3357 strcpy (own_buf
, "W00");
3362 gdbserver_version (void)
3364 printf ("GNU gdbserver %s%s\n"
3365 "Copyright (C) 2017 Free Software Foundation, Inc.\n"
3366 "gdbserver is free software, covered by the "
3367 "GNU General Public License.\n"
3368 "This gdbserver was configured as \"%s\"\n",
3369 PKGVERSION
, version
, host_name
);
3373 gdbserver_usage (FILE *stream
)
3375 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
3376 "\tgdbserver [OPTIONS] --attach COMM PID\n"
3377 "\tgdbserver [OPTIONS] --multi COMM\n"
3379 "COMM may either be a tty device (for serial debugging),\n"
3380 "HOST:PORT to listen for a TCP connection, or '-' or 'stdio' to use \n"
3381 "stdin/stdout of gdbserver.\n"
3382 "PROG is the executable program. ARGS are arguments passed to inferior.\n"
3383 "PID is the process ID to attach to, when --attach is specified.\n"
3385 "Operating modes:\n"
3387 " --attach Attach to running process PID.\n"
3388 " --multi Start server without a specific program, and\n"
3389 " only quit when explicitly commanded.\n"
3390 " --once Exit after the first connection has closed.\n"
3391 " --help Print this message and then exit.\n"
3392 " --version Display version information and exit.\n"
3396 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
3397 " --disable-randomization\n"
3398 " Run PROG with address space randomization disabled.\n"
3399 " --no-disable-randomization\n"
3400 " Don't disable address space randomization when\n"
3402 " --startup-with-shell\n"
3403 " Start PROG using a shell. I.e., execs a shell that\n"
3404 " then execs PROG. (default)\n"
3405 " --no-startup-with-shell\n"
3406 " Exec PROG directly instead of using a shell.\n"
3407 " Disables argument globbing and variable substitution\n"
3408 " on UNIX-like systems.\n"
3412 " --debug Enable general debugging output.\n"
3413 " --debug-format=opt1[,opt2,...]\n"
3414 " Specify extra content in debugging output.\n"
3419 " --remote-debug Enable remote protocol debugging output.\n"
3420 " --disable-packet=opt1[,opt2,...]\n"
3421 " Disable support for RSP packets or features.\n"
3423 " vCont, Tthread, qC, qfThreadInfo and \n"
3424 " threads (disable all threading packets).\n"
3426 "For more information, consult the GDB manual (available as on-line \n"
3427 "info or a printed manual).\n");
3428 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
3429 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
3433 gdbserver_show_disableable (FILE *stream
)
3435 fprintf (stream
, "Disableable packets:\n"
3436 " vCont \tAll vCont packets\n"
3437 " qC \tQuerying the current thread\n"
3438 " qfThreadInfo\tThread listing\n"
3439 " Tthread \tPassing the thread specifier in the "
3440 "T stop reply packet\n"
3441 " threads \tAll of the above\n");
3445 #undef require_running
3446 #define require_running(BUF) \
3447 if (!target_running ()) \
3454 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
3456 int pid
= * (int *) args
;
3458 if (ptid_get_pid (entry
->id
) == pid
)
3465 kill_inferior_callback (struct inferior_list_entry
*entry
)
3467 struct process_info
*process
= (struct process_info
*) entry
;
3468 int pid
= ptid_get_pid (process
->entry
.id
);
3470 kill_inferior (pid
);
3471 discard_queued_stop_replies (pid_to_ptid (pid
));
3474 /* Callback for for_each_inferior to detach or kill the inferior,
3475 depending on whether we attached to it or not.
3476 We inform the user whether we're detaching or killing the process
3477 as this is only called when gdbserver is about to exit. */
3480 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
3482 struct process_info
*process
= (struct process_info
*) entry
;
3483 int pid
= ptid_get_pid (process
->entry
.id
);
3485 if (process
->attached
)
3486 detach_inferior (pid
);
3488 kill_inferior (pid
);
3490 discard_queued_stop_replies (pid_to_ptid (pid
));
3493 /* for_each_inferior callback for detach_or_kill_for_exit to print
3494 the pids of started inferiors. */
3497 print_started_pid (struct inferior_list_entry
*entry
)
3499 struct process_info
*process
= (struct process_info
*) entry
;
3501 if (! process
->attached
)
3503 int pid
= ptid_get_pid (process
->entry
.id
);
3504 fprintf (stderr
, " %d", pid
);
3508 /* for_each_inferior callback for detach_or_kill_for_exit to print
3509 the pids of attached inferiors. */
3512 print_attached_pid (struct inferior_list_entry
*entry
)
3514 struct process_info
*process
= (struct process_info
*) entry
;
3516 if (process
->attached
)
3518 int pid
= ptid_get_pid (process
->entry
.id
);
3519 fprintf (stderr
, " %d", pid
);
3523 /* Call this when exiting gdbserver with possible inferiors that need
3524 to be killed or detached from. */
3527 detach_or_kill_for_exit (void)
3529 /* First print a list of the inferiors we will be killing/detaching.
3530 This is to assist the user, for example, in case the inferior unexpectedly
3531 dies after we exit: did we screw up or did the inferior exit on its own?
3532 Having this info will save some head-scratching. */
3534 if (have_started_inferiors_p ())
3536 fprintf (stderr
, "Killing process(es):");
3537 for_each_inferior (&all_processes
, print_started_pid
);
3538 fprintf (stderr
, "\n");
3540 if (have_attached_inferiors_p ())
3542 fprintf (stderr
, "Detaching process(es):");
3543 for_each_inferior (&all_processes
, print_attached_pid
);
3544 fprintf (stderr
, "\n");
3547 /* Now we can kill or detach the inferiors. */
3549 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
3552 /* Value that will be passed to exit(3) when gdbserver exits. */
3553 static int exit_code
;
3555 /* Cleanup version of detach_or_kill_for_exit. */
3558 detach_or_kill_for_exit_cleanup (void *ignore
)
3563 detach_or_kill_for_exit ();
3566 CATCH (exception
, RETURN_MASK_ALL
)
3569 fprintf (stderr
, "Detach or kill failed: %s\n", exception
.message
);
3575 /* Main function. This is called by the real "main" function,
3576 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
3578 static void ATTRIBUTE_NORETURN
3579 captured_main (int argc
, char *argv
[])
3584 const char *port
= NULL
;
3585 char **next_arg
= &argv
[1];
3586 volatile int multi_mode
= 0;
3587 volatile int attach
= 0;
3589 bool selftest
= false;
3591 while (*next_arg
!= NULL
&& **next_arg
== '-')
3593 if (strcmp (*next_arg
, "--version") == 0)
3595 gdbserver_version ();
3598 else if (strcmp (*next_arg
, "--help") == 0)
3600 gdbserver_usage (stdout
);
3603 else if (strcmp (*next_arg
, "--attach") == 0)
3605 else if (strcmp (*next_arg
, "--multi") == 0)
3607 else if (strcmp (*next_arg
, "--wrapper") == 0)
3614 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
3616 wrapper_argv
+= *next_arg
;
3617 wrapper_argv
+= ' ';
3621 if (!wrapper_argv
.empty ())
3623 /* Erase the last whitespace. */
3624 wrapper_argv
.erase (wrapper_argv
.end () - 1);
3627 if (next_arg
== tmp
|| *next_arg
== NULL
)
3629 gdbserver_usage (stderr
);
3633 /* Consume the "--". */
3636 else if (strcmp (*next_arg
, "--debug") == 0)
3638 else if (startswith (*next_arg
, "--debug-format="))
3641 = parse_debug_format_options ((*next_arg
)
3642 + sizeof ("--debug-format=") - 1, 0);
3644 if (error_msg
!= NULL
)
3646 fprintf (stderr
, "%s", error_msg
);
3650 else if (strcmp (*next_arg
, "--remote-debug") == 0)
3652 else if (strcmp (*next_arg
, "--disable-packet") == 0)
3654 gdbserver_show_disableable (stdout
);
3657 else if (startswith (*next_arg
, "--disable-packet="))
3659 char *packets
, *tok
;
3661 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
3662 for (tok
= strtok (packets
, ",");
3664 tok
= strtok (NULL
, ","))
3666 if (strcmp ("vCont", tok
) == 0)
3667 disable_packet_vCont
= 1;
3668 else if (strcmp ("Tthread", tok
) == 0)
3669 disable_packet_Tthread
= 1;
3670 else if (strcmp ("qC", tok
) == 0)
3671 disable_packet_qC
= 1;
3672 else if (strcmp ("qfThreadInfo", tok
) == 0)
3673 disable_packet_qfThreadInfo
= 1;
3674 else if (strcmp ("threads", tok
) == 0)
3676 disable_packet_vCont
= 1;
3677 disable_packet_Tthread
= 1;
3678 disable_packet_qC
= 1;
3679 disable_packet_qfThreadInfo
= 1;
3683 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
3685 gdbserver_show_disableable (stderr
);
3690 else if (strcmp (*next_arg
, "-") == 0)
3692 /* "-" specifies a stdio connection and is a form of port
3694 port
= STDIO_CONNECTION_NAME
;
3698 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
3699 disable_randomization
= 1;
3700 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
3701 disable_randomization
= 0;
3702 else if (strcmp (*next_arg
, "--startup-with-shell") == 0)
3703 startup_with_shell
= true;
3704 else if (strcmp (*next_arg
, "--no-startup-with-shell") == 0)
3705 startup_with_shell
= false;
3706 else if (strcmp (*next_arg
, "--once") == 0)
3708 else if (strcmp (*next_arg
, "--selftest") == 0)
3712 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
3725 if ((port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
3728 gdbserver_usage (stderr
);
3732 /* Remember stdio descriptors. LISTEN_DESC must not be listed, it will be
3733 opened by remote_prepare. */
3736 save_original_signals_state ();
3738 /* We need to know whether the remote connection is stdio before
3739 starting the inferior. Inferiors created in this scenario have
3740 stdin,stdout redirected. So do this here before we call
3743 remote_prepare (port
);
3748 /* --attach used to come after PORT, so allow it there for
3750 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
3757 && (*next_arg
== NULL
3758 || (*next_arg
)[0] == '\0'
3759 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
3761 || next_arg
[1] != NULL
))
3766 gdbserver_usage (stderr
);
3770 /* Gather information about the environment. */
3771 our_environ
= gdb_environ::from_host_environ ();
3773 initialize_async_io ();
3775 have_job_control ();
3776 initialize_event_loop ();
3777 if (target_supports_tracepoints ())
3778 initialize_tracepoint ();
3779 initialize_notif ();
3781 own_buf
= (char *) xmalloc (PBUFSIZ
+ 1);
3782 mem_buf
= (unsigned char *) xmalloc (PBUFSIZ
);
3786 selftests::run_tests ();
3787 throw_quit ("Quit");
3790 if (pid
== 0 && *next_arg
!= NULL
)
3794 n
= argc
- (next_arg
- argv
);
3795 program_name
= xstrdup (next_arg
[0]);
3796 for (i
= 1; i
< n
; i
++)
3797 program_args
.push_back (xstrdup (next_arg
[i
]));
3798 program_args
.push_back (NULL
);
3800 /* Wait till we are at first instruction in program. */
3801 create_inferior (program_name
, program_args
);
3803 /* We are now (hopefully) stopped at the first instruction of
3804 the target process. This assumes that the target process was
3805 successfully created. */
3809 if (attach_inferior (pid
) == -1)
3810 error ("Attaching not supported on this target");
3812 /* Otherwise succeeded. */
3816 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3817 last_status
.value
.integer
= 0;
3818 last_ptid
= minus_one_ptid
;
3820 make_cleanup (detach_or_kill_for_exit_cleanup
, NULL
);
3822 /* Don't report shared library events on the initial connection,
3823 even if some libraries are preloaded. Avoids the "stopped by
3824 shared library event" notice on gdb side. */
3827 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3828 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3833 if (!was_running
&& !multi_mode
)
3834 error ("No program to debug");
3841 report_fork_events
= 0;
3842 report_vfork_events
= 0;
3843 report_exec_events
= 0;
3844 /* Be sure we're out of tfind mode. */
3845 current_traceframe
= -1;
3846 cont_thread
= null_ptid
;
3847 swbreak_feature
= 0;
3848 hwbreak_feature
= 0;
3849 vCont_supported
= 0;
3855 /* Wait for events. This will return when all event sources
3856 are removed from the event loop. */
3857 start_event_loop ();
3859 /* If an exit was requested (using the "monitor exit"
3860 command), terminate now. */
3862 throw_quit ("Quit");
3864 /* The only other way to get here is for getpkt to fail:
3866 - If --once was specified, we're done.
3868 - If not in extended-remote mode, and we're no longer
3869 debugging anything, simply exit: GDB has disconnected
3870 after processing the last process exit.
3872 - Otherwise, close the connection and reopen it at the
3874 if (run_once
|| (!extended_protocol
&& !target_running ()))
3875 throw_quit ("Quit");
3878 "Remote side has terminated connection. "
3879 "GDBserver will reopen the connection.\n");
3881 /* Get rid of any pending statuses. An eventual reconnection
3882 (by the same GDB instance or another) will refresh all its
3883 state from scratch. */
3884 discard_queued_stop_replies (minus_one_ptid
);
3885 for_each_inferior (&all_threads
,
3886 clear_pending_status_callback
);
3890 if (disconnected_tracing
)
3892 /* Try to enable non-stop/async mode, so we we can
3893 both wait for an async socket accept, and handle
3894 async target events simultaneously. There's also
3895 no point either in having the target always stop
3896 all threads, when we're going to pass signals
3897 down without informing GDB. */
3900 if (start_non_stop (1))
3903 /* Detaching implicitly resumes all threads;
3904 simply disconnecting does not. */
3910 "Disconnected tracing disabled; "
3911 "stopping trace run.\n");
3916 CATCH (exception
, RETURN_MASK_ERROR
)
3919 fprintf (stderr
, "gdbserver: %s\n", exception
.message
);
3921 if (response_needed
)
3923 write_enn (own_buf
);
3928 throw_quit ("Quit");
3934 /* Main function. */
3937 main (int argc
, char *argv
[])
3942 captured_main (argc
, argv
);
3944 CATCH (exception
, RETURN_MASK_ALL
)
3946 if (exception
.reason
== RETURN_ERROR
)
3949 fprintf (stderr
, "%s\n", exception
.message
);
3950 fprintf (stderr
, "Exiting\n");
3958 gdb_assert_not_reached ("captured_main should never return");
3961 /* Process options coming from Z packets for a breakpoint. PACKET is
3962 the packet buffer. *PACKET is updated to point to the first char
3963 after the last processed option. */
3966 process_point_options (struct gdb_breakpoint
*bp
, char **packet
)
3968 char *dataptr
= *packet
;
3971 /* Check if data has the correct format. */
3972 if (*dataptr
!= ';')
3979 if (*dataptr
== ';')
3982 if (*dataptr
== 'X')
3984 /* Conditional expression. */
3986 debug_printf ("Found breakpoint condition.\n");
3987 if (!add_breakpoint_condition (bp
, &dataptr
))
3988 dataptr
= strchrnul (dataptr
, ';');
3990 else if (startswith (dataptr
, "cmds:"))
3992 dataptr
+= strlen ("cmds:");
3994 debug_printf ("Found breakpoint commands %s.\n", dataptr
);
3995 persist
= (*dataptr
== '1');
3997 if (add_breakpoint_commands (bp
, &dataptr
, persist
))
3998 dataptr
= strchrnul (dataptr
, ';');
4002 fprintf (stderr
, "Unknown token %c, ignoring.\n",
4004 /* Skip tokens until we find one that we recognize. */
4005 dataptr
= strchrnul (dataptr
, ';');
4011 /* Event loop callback that handles a serial event. The first byte in
4012 the serial buffer gets us here. We expect characters to arrive at
4013 a brisk pace, so we read the rest of the packet with a blocking
4017 process_serial_event (void)
4028 int new_packet_len
= -1;
4030 disable_async_io ();
4032 response_needed
= 0;
4033 packet_len
= getpkt (own_buf
);
4034 if (packet_len
<= 0)
4037 /* Force an event loop break. */
4040 response_needed
= 1;
4047 handle_query (own_buf
, packet_len
, &new_packet_len
);
4050 handle_general_set (own_buf
);
4053 require_running (own_buf
);
4058 pid
= strtol (&own_buf
[i
], NULL
, 16);
4061 pid
= ptid_get_pid (current_ptid
);
4063 if ((tracing
&& disconnected_tracing
) || any_persistent_commands ())
4065 struct process_info
*process
= find_process_pid (pid
);
4067 if (process
== NULL
)
4069 write_enn (own_buf
);
4073 if (tracing
&& disconnected_tracing
)
4075 "Disconnected tracing in effect, "
4076 "leaving gdbserver attached to the process\n");
4078 if (any_persistent_commands ())
4080 "Persistent commands are present, "
4081 "leaving gdbserver attached to the process\n");
4083 /* Make sure we're in non-stop/async mode, so we we can both
4084 wait for an async socket accept, and handle async target
4085 events simultaneously. There's also no point either in
4086 having the target stop all threads, when we're going to
4087 pass signals down without informing GDB. */
4091 debug_printf ("Forcing non-stop mode\n");
4097 process
->gdb_detached
= 1;
4099 /* Detaching implicitly resumes all threads. */
4100 target_continue_no_signal (minus_one_ptid
);
4103 break; /* from switch/case */
4106 fprintf (stderr
, "Detaching from process %d\n", pid
);
4108 if (detach_inferior (pid
) != 0)
4109 write_enn (own_buf
);
4112 discard_queued_stop_replies (pid_to_ptid (pid
));
4115 if (extended_protocol
|| target_running ())
4117 /* There is still at least one inferior remaining or
4118 we are in extended mode, so don't terminate gdbserver,
4119 and instead treat this like a normal program exit. */
4120 last_status
.kind
= TARGET_WAITKIND_EXITED
;
4121 last_status
.value
.integer
= 0;
4122 last_ptid
= pid_to_ptid (pid
);
4124 current_thread
= NULL
;
4131 /* If we are attached, then we can exit. Otherwise, we
4132 need to hang around doing nothing, until the child is
4134 join_inferior (pid
);
4140 extended_protocol
= 1;
4144 handle_status (own_buf
);
4147 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
4149 ptid_t gdb_id
, thread_id
;
4152 require_running (own_buf
);
4154 gdb_id
= read_ptid (&own_buf
[2], NULL
);
4156 pid
= ptid_get_pid (gdb_id
);
4158 if (ptid_equal (gdb_id
, null_ptid
)
4159 || ptid_equal (gdb_id
, minus_one_ptid
))
4160 thread_id
= null_ptid
;
4162 && ptid_equal (pid_to_ptid (pid
),
4165 struct thread_info
*thread
=
4166 (struct thread_info
*) find_inferior (&all_threads
,
4171 write_enn (own_buf
);
4175 thread_id
= thread
->entry
.id
;
4179 thread_id
= gdb_id_to_thread_id (gdb_id
);
4180 if (ptid_equal (thread_id
, null_ptid
))
4182 write_enn (own_buf
);
4187 if (own_buf
[1] == 'g')
4189 if (ptid_equal (thread_id
, null_ptid
))
4191 /* GDB is telling us to choose any thread. Check if
4192 the currently selected thread is still valid. If
4193 it is not, select the first available. */
4194 struct thread_info
*thread
=
4195 (struct thread_info
*) find_inferior_id (&all_threads
,
4198 thread
= get_first_thread ();
4199 thread_id
= thread
->entry
.id
;
4202 general_thread
= thread_id
;
4203 set_desired_thread (1);
4204 gdb_assert (current_thread
!= NULL
);
4206 else if (own_buf
[1] == 'c')
4207 cont_thread
= thread_id
;
4213 /* Silently ignore it so that gdb can extend the protocol
4214 without compatibility headaches. */
4219 require_running (own_buf
);
4220 if (current_traceframe
>= 0)
4222 struct regcache
*regcache
4223 = new_register_cache (current_target_desc ());
4225 if (fetch_traceframe_registers (current_traceframe
,
4227 registers_to_string (regcache
, own_buf
);
4229 write_enn (own_buf
);
4230 free_register_cache (regcache
);
4234 struct regcache
*regcache
;
4236 if (!set_desired_thread (1))
4237 write_enn (own_buf
);
4240 regcache
= get_thread_regcache (current_thread
, 1);
4241 registers_to_string (regcache
, own_buf
);
4246 require_running (own_buf
);
4247 if (current_traceframe
>= 0)
4248 write_enn (own_buf
);
4251 struct regcache
*regcache
;
4253 if (!set_desired_thread (1))
4254 write_enn (own_buf
);
4257 regcache
= get_thread_regcache (current_thread
, 1);
4258 registers_from_string (regcache
, &own_buf
[1]);
4264 require_running (own_buf
);
4265 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
4266 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
4268 write_enn (own_buf
);
4270 bin2hex (mem_buf
, own_buf
, res
);
4273 require_running (own_buf
);
4274 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
4275 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
4278 write_enn (own_buf
);
4281 require_running (own_buf
);
4282 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
4283 &mem_addr
, &len
, &mem_buf
) < 0
4284 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
4285 write_enn (own_buf
);
4290 require_running (own_buf
);
4291 hex2bin (own_buf
+ 1, &sig
, 1);
4292 if (gdb_signal_to_host_p ((enum gdb_signal
) sig
))
4293 signal
= gdb_signal_to_host ((enum gdb_signal
) sig
);
4296 myresume (own_buf
, 0, signal
);
4299 require_running (own_buf
);
4300 hex2bin (own_buf
+ 1, &sig
, 1);
4301 if (gdb_signal_to_host_p ((enum gdb_signal
) sig
))
4302 signal
= gdb_signal_to_host ((enum gdb_signal
) sig
);
4305 myresume (own_buf
, 1, signal
);
4308 require_running (own_buf
);
4310 myresume (own_buf
, 0, signal
);
4313 require_running (own_buf
);
4315 myresume (own_buf
, 1, signal
);
4317 case 'Z': /* insert_ ... */
4319 case 'z': /* remove_ ... */
4324 char type
= own_buf
[1];
4326 const int insert
= ch
== 'Z';
4327 char *p
= &own_buf
[3];
4329 p
= unpack_varlen_hex (p
, &addr
);
4330 kind
= strtol (p
+ 1, &dataptr
, 16);
4334 struct gdb_breakpoint
*bp
;
4336 bp
= set_gdb_breakpoint (type
, addr
, kind
, &res
);
4341 /* GDB may have sent us a list of *point parameters to
4342 be evaluated on the target's side. Read such list
4343 here. If we already have a list of parameters, GDB
4344 is telling us to drop that list and use this one
4346 clear_breakpoint_conditions_and_commands (bp
);
4347 process_point_options (bp
, &dataptr
);
4351 res
= delete_gdb_breakpoint (type
, addr
, kind
);
4359 write_enn (own_buf
);
4363 response_needed
= 0;
4364 if (!target_running ())
4365 /* The packet we received doesn't make sense - but we can't
4366 reply to it, either. */
4369 fprintf (stderr
, "Killing all inferiors\n");
4370 for_each_inferior (&all_processes
, kill_inferior_callback
);
4372 /* When using the extended protocol, we wait with no program
4373 running. The traditional protocol will exit instead. */
4374 if (extended_protocol
)
4376 last_status
.kind
= TARGET_WAITKIND_EXITED
;
4377 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
4385 ptid_t gdb_id
, thread_id
;
4387 require_running (own_buf
);
4389 gdb_id
= read_ptid (&own_buf
[1], NULL
);
4390 thread_id
= gdb_id_to_thread_id (gdb_id
);
4391 if (ptid_equal (thread_id
, null_ptid
))
4393 write_enn (own_buf
);
4397 if (mythread_alive (thread_id
))
4400 write_enn (own_buf
);
4404 response_needed
= 0;
4406 /* Restarting the inferior is only supported in the extended
4408 if (extended_protocol
)
4410 if (target_running ())
4411 for_each_inferior (&all_processes
,
4412 kill_inferior_callback
);
4413 fprintf (stderr
, "GDBserver restarting\n");
4415 /* Wait till we are at 1st instruction in prog. */
4416 if (program_name
!= NULL
)
4418 create_inferior (program_name
, program_args
);
4420 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
4422 /* Stopped at the first instruction of the target
4424 general_thread
= last_ptid
;
4428 /* Something went wrong. */
4429 general_thread
= null_ptid
;
4434 last_status
.kind
= TARGET_WAITKIND_EXITED
;
4435 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
4441 /* It is a request we don't understand. Respond with an
4442 empty packet so that gdb knows that we don't support this
4448 /* Extended (long) request. */
4449 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
4453 /* It is a request we don't understand. Respond with an empty
4454 packet so that gdb knows that we don't support this
4460 if (new_packet_len
!= -1)
4461 putpkt_binary (own_buf
, new_packet_len
);
4465 response_needed
= 0;
4473 /* Event-loop callback for serial events. */
4476 handle_serial_event (int err
, gdb_client_data client_data
)
4479 debug_printf ("handling possible serial event\n");
4481 /* Really handle it. */
4482 if (process_serial_event () < 0)
4485 /* Be sure to not change the selected thread behind GDB's back.
4486 Important in the non-stop mode asynchronous protocol. */
4487 set_desired_thread (1);
4492 /* Push a stop notification on the notification queue. */
4495 push_stop_notification (ptid_t ptid
, struct target_waitstatus
*status
)
4497 struct vstop_notif
*vstop_notif
= XNEW (struct vstop_notif
);
4499 vstop_notif
->status
= *status
;
4500 vstop_notif
->ptid
= ptid
;
4501 /* Push Stop notification. */
4502 notif_push (¬if_stop
, (struct notif_event
*) vstop_notif
);
4505 /* Event-loop callback for target events. */
4508 handle_target_event (int err
, gdb_client_data client_data
)
4511 debug_printf ("handling possible target event\n");
4513 last_ptid
= mywait (minus_one_ptid
, &last_status
,
4516 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
)
4518 if (gdb_connected () && report_no_resumed
)
4519 push_stop_notification (null_ptid
, &last_status
);
4521 else if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
4523 int pid
= ptid_get_pid (last_ptid
);
4524 struct process_info
*process
= find_process_pid (pid
);
4525 int forward_event
= !gdb_connected () || process
->gdb_detached
;
4527 if (last_status
.kind
== TARGET_WAITKIND_EXITED
4528 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
4530 mark_breakpoints_out (process
);
4531 target_mourn_inferior (last_ptid
);
4533 else if (last_status
.kind
== TARGET_WAITKIND_THREAD_EXITED
)
4537 /* We're reporting this thread as stopped. Update its
4538 "want-stopped" state to what the client wants, until it
4539 gets a new resume action. */
4540 current_thread
->last_resume_kind
= resume_stop
;
4541 current_thread
->last_status
= last_status
;
4546 if (!target_running ())
4548 /* The last process exited. We're done. */
4552 if (last_status
.kind
== TARGET_WAITKIND_EXITED
4553 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
4554 || last_status
.kind
== TARGET_WAITKIND_THREAD_EXITED
)
4558 /* A thread stopped with a signal, but gdb isn't
4559 connected to handle it. Pass it down to the
4560 inferior, as if it wasn't being traced. */
4561 enum gdb_signal signal
;
4564 debug_printf ("GDB not connected; forwarding event %d for"
4566 (int) last_status
.kind
,
4567 target_pid_to_str (last_ptid
));
4569 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
4570 signal
= last_status
.value
.sig
;
4572 signal
= GDB_SIGNAL_0
;
4573 target_continue (last_ptid
, signal
);
4577 push_stop_notification (last_ptid
, &last_status
);
4580 /* Be sure to not change the selected thread behind GDB's back.
4581 Important in the non-stop mode asynchronous protocol. */
4582 set_desired_thread (1);
4595 } // namespace selftests
4596 #endif /* GDB_SELF_TEST */