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 (desc
->xmltarget
!= NULL
&& strcmp (annex
, "target.xml") == 0)
878 if (*desc
->xmltarget
== '@')
879 return desc
->xmltarget
+ 1;
881 annex
= desc
->xmltarget
;
886 extern const char *const xml_builtin
[][2];
889 /* Look for the annex. */
890 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
891 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
894 if (xml_builtin
[i
][0] != NULL
)
895 return xml_builtin
[i
][1];
903 monitor_show_help (void)
905 monitor_output ("The following monitor commands are supported:\n");
906 monitor_output (" set debug <0|1>\n");
907 monitor_output (" Enable general debugging messages\n");
908 monitor_output (" set debug-hw-points <0|1>\n");
909 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
910 monitor_output (" set remote-debug <0|1>\n");
911 monitor_output (" Enable remote protocol debugging messages\n");
912 monitor_output (" set debug-format option1[,option2,...]\n");
913 monitor_output (" Add additional information to debugging messages\n");
914 monitor_output (" Options: all, none");
915 monitor_output (", timestamp");
916 monitor_output ("\n");
917 monitor_output (" exit\n");
918 monitor_output (" Quit GDBserver\n");
921 /* Read trace frame or inferior memory. Returns the number of bytes
922 actually read, zero when no further transfer is possible, and -1 on
923 error. Return of a positive value smaller than LEN does not
924 indicate there's no more to be read, only the end of the transfer.
925 E.g., when GDB reads memory from a traceframe, a first request may
926 be served from a memory block that does not cover the whole request
927 length. A following request gets the rest served from either
928 another block (of the same traceframe) or from the read-only
932 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
936 if (current_traceframe
>= 0)
939 ULONGEST length
= len
;
941 if (traceframe_read_mem (current_traceframe
,
942 memaddr
, myaddr
, len
, &nbytes
))
944 /* Data read from trace buffer, we're done. */
947 if (!in_readonly_region (memaddr
, length
))
949 /* Otherwise we have a valid readonly case, fall through. */
950 /* (assume no half-trace half-real blocks for now) */
953 res
= prepare_to_access_memory ();
956 if (set_desired_thread (1))
957 res
= read_inferior_memory (memaddr
, myaddr
, len
);
960 done_accessing_memory ();
962 return res
== 0 ? len
: -1;
968 /* Write trace frame or inferior memory. Actually, writing to trace
969 frames is forbidden. */
972 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
974 if (current_traceframe
>= 0)
980 ret
= prepare_to_access_memory ();
983 if (set_desired_thread (1))
984 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
987 done_accessing_memory ();
993 /* Subroutine of handle_search_memory to simplify it. */
996 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
997 gdb_byte
*pattern
, unsigned pattern_len
,
998 gdb_byte
*search_buf
,
999 unsigned chunk_size
, unsigned search_buf_size
,
1000 CORE_ADDR
*found_addrp
)
1002 /* Prime the search buffer. */
1004 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
1007 warning ("Unable to access %ld bytes of target "
1008 "memory at 0x%lx, halting search.",
1009 (long) search_buf_size
, (long) start_addr
);
1013 /* Perform the search.
1015 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
1016 When we've scanned N bytes we copy the trailing bytes to the start and
1017 read in another N bytes. */
1019 while (search_space_len
>= pattern_len
)
1021 gdb_byte
*found_ptr
;
1022 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
1026 found_ptr
= (gdb_byte
*) memmem (search_buf
, nr_search_bytes
, pattern
,
1029 if (found_ptr
!= NULL
)
1031 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
1032 *found_addrp
= found_addr
;
1036 /* Not found in this chunk, skip to next chunk. */
1038 /* Don't let search_space_len wrap here, it's unsigned. */
1039 if (search_space_len
>= chunk_size
)
1040 search_space_len
-= chunk_size
;
1042 search_space_len
= 0;
1044 if (search_space_len
>= pattern_len
)
1046 unsigned keep_len
= search_buf_size
- chunk_size
;
1047 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
1050 /* Copy the trailing part of the previous iteration to the front
1051 of the buffer for the next iteration. */
1052 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
1054 nr_to_read
= (search_space_len
- keep_len
< chunk_size
1055 ? search_space_len
- keep_len
1058 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
1059 nr_to_read
) != search_buf_size
)
1061 warning ("Unable to access %ld bytes of target memory "
1062 "at 0x%lx, halting search.",
1063 (long) nr_to_read
, (long) read_addr
);
1067 start_addr
+= chunk_size
;
1076 /* Handle qSearch:memory packets. */
1079 handle_search_memory (char *own_buf
, int packet_len
)
1081 CORE_ADDR start_addr
;
1082 CORE_ADDR search_space_len
;
1084 unsigned int pattern_len
;
1085 /* NOTE: also defined in find.c testcase. */
1086 #define SEARCH_CHUNK_SIZE 16000
1087 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
1088 /* Buffer to hold memory contents for searching. */
1089 gdb_byte
*search_buf
;
1090 unsigned search_buf_size
;
1092 CORE_ADDR found_addr
;
1093 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
1095 pattern
= (gdb_byte
*) malloc (packet_len
);
1096 if (pattern
== NULL
)
1098 error ("Unable to allocate memory to perform the search");
1099 strcpy (own_buf
, "E00");
1102 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
1103 packet_len
- cmd_name_len
,
1104 &start_addr
, &search_space_len
,
1105 pattern
, &pattern_len
) < 0)
1108 error ("Error in parsing qSearch:memory packet");
1109 strcpy (own_buf
, "E00");
1113 search_buf_size
= chunk_size
+ pattern_len
- 1;
1115 /* No point in trying to allocate a buffer larger than the search space. */
1116 if (search_space_len
< search_buf_size
)
1117 search_buf_size
= search_space_len
;
1119 search_buf
= (gdb_byte
*) malloc (search_buf_size
);
1120 if (search_buf
== NULL
)
1123 error ("Unable to allocate memory to perform the search");
1124 strcpy (own_buf
, "E00");
1128 found
= handle_search_memory_1 (start_addr
, search_space_len
,
1129 pattern
, pattern_len
,
1130 search_buf
, chunk_size
, search_buf_size
,
1134 sprintf (own_buf
, "1,%lx", (long) found_addr
);
1135 else if (found
== 0)
1136 strcpy (own_buf
, "0");
1138 strcpy (own_buf
, "E00");
1144 #define require_running(BUF) \
1145 if (!target_running ()) \
1151 /* Parse options to --debug-format= and "monitor set debug-format".
1152 ARG is the text after "--debug-format=" or "monitor set debug-format".
1153 IS_MONITOR is non-zero if we're invoked via "monitor set debug-format".
1154 This triggers calls to monitor_output.
1155 The result is NULL if all options were parsed ok, otherwise an error
1156 message which the caller must free.
1158 N.B. These commands affect all debug format settings, they are not
1159 cumulative. If a format is not specified, it is turned off.
1160 However, we don't go to extra trouble with things like
1161 "monitor set debug-format all,none,timestamp".
1162 Instead we just parse them one at a time, in order.
1164 The syntax for "monitor set debug" we support here is not identical
1165 to gdb's "set debug foo on|off" because we also use this function to
1166 parse "--debug-format=foo,bar". */
1169 parse_debug_format_options (const char *arg
, int is_monitor
)
1171 VEC (char_ptr
) *options
;
1175 /* First turn all debug format options off. */
1176 debug_timestamp
= 0;
1178 /* First remove leading spaces, for "monitor set debug-format". */
1179 while (isspace (*arg
))
1182 options
= delim_string_to_char_ptr_vec (arg
, ',');
1184 for (ix
= 0; VEC_iterate (char_ptr
, options
, ix
, option
); ++ix
)
1186 if (strcmp (option
, "all") == 0)
1188 debug_timestamp
= 1;
1190 monitor_output ("All extra debug format options enabled.\n");
1192 else if (strcmp (option
, "none") == 0)
1194 debug_timestamp
= 0;
1196 monitor_output ("All extra debug format options disabled.\n");
1198 else if (strcmp (option
, "timestamp") == 0)
1200 debug_timestamp
= 1;
1202 monitor_output ("Timestamps will be added to debug output.\n");
1204 else if (*option
== '\0')
1206 /* An empty option, e.g., "--debug-format=foo,,bar", is ignored. */
1211 char *msg
= xstrprintf ("Unknown debug-format argument: \"%s\"\n",
1214 free_char_ptr_vec (options
);
1219 free_char_ptr_vec (options
);
1223 /* Handle monitor commands not handled by target-specific handlers. */
1226 handle_monitor_command (char *mon
, char *own_buf
)
1228 if (strcmp (mon
, "set debug 1") == 0)
1231 monitor_output ("Debug output enabled.\n");
1233 else if (strcmp (mon
, "set debug 0") == 0)
1236 monitor_output ("Debug output disabled.\n");
1238 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
1240 show_debug_regs
= 1;
1241 monitor_output ("H/W point debugging output enabled.\n");
1243 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
1245 show_debug_regs
= 0;
1246 monitor_output ("H/W point debugging output disabled.\n");
1248 else if (strcmp (mon
, "set remote-debug 1") == 0)
1251 monitor_output ("Protocol debug output enabled.\n");
1253 else if (strcmp (mon
, "set remote-debug 0") == 0)
1256 monitor_output ("Protocol debug output disabled.\n");
1258 else if (startswith (mon
, "set debug-format "))
1261 = parse_debug_format_options (mon
+ sizeof ("set debug-format ") - 1,
1264 if (error_msg
!= NULL
)
1266 monitor_output (error_msg
);
1267 monitor_show_help ();
1268 write_enn (own_buf
);
1272 else if (strcmp (mon
, "help") == 0)
1273 monitor_show_help ();
1274 else if (strcmp (mon
, "exit") == 0)
1278 monitor_output ("Unknown monitor command.\n\n");
1279 monitor_show_help ();
1280 write_enn (own_buf
);
1284 /* Associates a callback with each supported qXfer'able object. */
1288 /* The object this handler handles. */
1291 /* Request that the target transfer up to LEN 8-bit bytes of the
1292 target's OBJECT. The OFFSET, for a seekable object, specifies
1293 the starting point. The ANNEX can be used to provide additional
1294 data-specific information to the target.
1296 Return the number of bytes actually transfered, zero when no
1297 further transfer is possible, -1 on error, -2 when the transfer
1298 is not supported, and -3 on a verbose error message that should
1299 be preserved. Return of a positive value smaller than LEN does
1300 not indicate the end of the object, only the end of the transfer.
1302 One, and only one, of readbuf or writebuf must be non-NULL. */
1303 int (*xfer
) (const char *annex
,
1304 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1305 ULONGEST offset
, LONGEST len
);
1308 /* Handle qXfer:auxv:read. */
1311 handle_qxfer_auxv (const char *annex
,
1312 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1313 ULONGEST offset
, LONGEST len
)
1315 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
1318 if (annex
[0] != '\0' || current_thread
== NULL
)
1321 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
1324 /* Handle qXfer:exec-file:read. */
1327 handle_qxfer_exec_file (const char *const_annex
,
1328 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1329 ULONGEST offset
, LONGEST len
)
1335 if (the_target
->pid_to_exec_file
== NULL
|| writebuf
!= NULL
)
1338 if (const_annex
[0] == '\0')
1340 if (current_thread
== NULL
)
1343 pid
= pid_of (current_thread
);
1347 char *annex
= (char *) alloca (strlen (const_annex
) + 1);
1349 strcpy (annex
, const_annex
);
1350 annex
= unpack_varlen_hex (annex
, &pid
);
1352 if (annex
[0] != '\0')
1359 file
= (*the_target
->pid_to_exec_file
) (pid
);
1363 total_len
= strlen (file
);
1365 if (offset
> total_len
)
1368 if (offset
+ len
> total_len
)
1369 len
= total_len
- offset
;
1371 memcpy (readbuf
, file
+ offset
, len
);
1375 /* Handle qXfer:features:read. */
1378 handle_qxfer_features (const char *annex
,
1379 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1380 ULONGEST offset
, LONGEST len
)
1382 const char *document
;
1385 if (writebuf
!= NULL
)
1388 if (!target_running ())
1391 /* Grab the correct annex. */
1392 document
= get_features_xml (annex
);
1393 if (document
== NULL
)
1396 total_len
= strlen (document
);
1398 if (offset
> total_len
)
1401 if (offset
+ len
> total_len
)
1402 len
= total_len
- offset
;
1404 memcpy (readbuf
, document
+ offset
, len
);
1408 /* Worker routine for handle_qxfer_libraries.
1409 Add to the length pointed to by ARG a conservative estimate of the
1410 length needed to transmit the file name of INF. */
1413 accumulate_file_name_length (struct inferior_list_entry
*inf
, void *arg
)
1415 struct dll_info
*dll
= (struct dll_info
*) inf
;
1416 unsigned int *total_len
= (unsigned int *) arg
;
1418 /* Over-estimate the necessary memory. Assume that every character
1419 in the library name must be escaped. */
1420 *total_len
+= 128 + 6 * strlen (dll
->name
);
1423 /* Worker routine for handle_qxfer_libraries.
1424 Emit the XML to describe the library in INF. */
1427 emit_dll_description (struct inferior_list_entry
*inf
, void *arg
)
1429 struct dll_info
*dll
= (struct dll_info
*) inf
;
1430 char **p_ptr
= (char **) arg
;
1434 strcpy (p
, " <library name=\"");
1436 name
= xml_escape_text (dll
->name
);
1440 strcpy (p
, "\"><segment address=\"");
1442 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1444 strcpy (p
, "\"/></library>\n");
1450 /* Handle qXfer:libraries:read. */
1453 handle_qxfer_libraries (const char *annex
,
1454 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1455 ULONGEST offset
, LONGEST len
)
1457 unsigned int total_len
;
1460 if (writebuf
!= NULL
)
1463 if (annex
[0] != '\0' || current_thread
== NULL
)
1467 for_each_inferior_with_data (&all_dlls
, accumulate_file_name_length
,
1470 document
= (char *) malloc (total_len
);
1471 if (document
== NULL
)
1474 strcpy (document
, "<library-list version=\"1.0\">\n");
1475 p
= document
+ strlen (document
);
1477 for_each_inferior_with_data (&all_dlls
, emit_dll_description
, &p
);
1479 strcpy (p
, "</library-list>\n");
1481 total_len
= strlen (document
);
1483 if (offset
> total_len
)
1489 if (offset
+ len
> total_len
)
1490 len
= total_len
- offset
;
1492 memcpy (readbuf
, document
+ offset
, len
);
1497 /* Handle qXfer:libraries-svr4:read. */
1500 handle_qxfer_libraries_svr4 (const char *annex
,
1501 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1502 ULONGEST offset
, LONGEST len
)
1504 if (writebuf
!= NULL
)
1507 if (current_thread
== NULL
|| the_target
->qxfer_libraries_svr4
== NULL
)
1510 return the_target
->qxfer_libraries_svr4 (annex
, readbuf
, writebuf
, offset
, len
);
1513 /* Handle qXfer:osadata:read. */
1516 handle_qxfer_osdata (const char *annex
,
1517 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1518 ULONGEST offset
, LONGEST len
)
1520 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
1523 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
1526 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1529 handle_qxfer_siginfo (const char *annex
,
1530 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1531 ULONGEST offset
, LONGEST len
)
1533 if (the_target
->qxfer_siginfo
== NULL
)
1536 if (annex
[0] != '\0' || current_thread
== NULL
)
1539 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1542 /* Handle qXfer:spu:read and qXfer:spu:write. */
1545 handle_qxfer_spu (const char *annex
,
1546 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1547 ULONGEST offset
, LONGEST len
)
1549 if (the_target
->qxfer_spu
== NULL
)
1552 if (current_thread
== NULL
)
1555 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1558 /* Handle qXfer:statictrace:read. */
1561 handle_qxfer_statictrace (const char *annex
,
1562 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1563 ULONGEST offset
, LONGEST len
)
1567 if (writebuf
!= NULL
)
1570 if (annex
[0] != '\0' || current_thread
== NULL
|| current_traceframe
== -1)
1573 if (traceframe_read_sdata (current_traceframe
, offset
,
1574 readbuf
, len
, &nbytes
))
1579 /* Helper for handle_qxfer_threads_proper.
1580 Emit the XML to describe the thread of INF. */
1583 handle_qxfer_threads_worker (struct inferior_list_entry
*inf
, void *arg
)
1585 struct thread_info
*thread
= (struct thread_info
*) inf
;
1586 struct buffer
*buffer
= (struct buffer
*) arg
;
1587 ptid_t ptid
= thread_to_gdb_id (thread
);
1589 int core
= target_core_of_thread (ptid
);
1591 const char *name
= target_thread_name (ptid
);
1593 write_ptid (ptid_s
, ptid
);
1595 buffer_xml_printf (buffer
, "<thread id=\"%s\"", ptid_s
);
1599 sprintf (core_s
, "%d", core
);
1600 buffer_xml_printf (buffer
, " core=\"%s\"", core_s
);
1604 buffer_xml_printf (buffer
, " name=\"%s\"", name
);
1606 buffer_xml_printf (buffer
, "/>\n");
1609 /* Helper for handle_qxfer_threads. */
1612 handle_qxfer_threads_proper (struct buffer
*buffer
)
1614 buffer_grow_str (buffer
, "<threads>\n");
1616 for_each_inferior_with_data (&all_threads
, handle_qxfer_threads_worker
,
1619 buffer_grow_str0 (buffer
, "</threads>\n");
1622 /* Handle qXfer:threads:read. */
1625 handle_qxfer_threads (const char *annex
,
1626 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1627 ULONGEST offset
, LONGEST len
)
1629 static char *result
= 0;
1630 static unsigned int result_length
= 0;
1632 if (writebuf
!= NULL
)
1635 if (annex
[0] != '\0')
1640 struct buffer buffer
;
1641 /* When asked for data at offset 0, generate everything and store into
1642 'result'. Successive reads will be served off 'result'. */
1646 buffer_init (&buffer
);
1648 handle_qxfer_threads_proper (&buffer
);
1650 result
= buffer_finish (&buffer
);
1651 result_length
= strlen (result
);
1652 buffer_free (&buffer
);
1655 if (offset
>= result_length
)
1657 /* We're out of data. */
1664 if (len
> result_length
- offset
)
1665 len
= result_length
- offset
;
1667 memcpy (readbuf
, result
+ offset
, len
);
1672 /* Handle qXfer:traceframe-info:read. */
1675 handle_qxfer_traceframe_info (const char *annex
,
1676 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1677 ULONGEST offset
, LONGEST len
)
1679 static char *result
= 0;
1680 static unsigned int result_length
= 0;
1682 if (writebuf
!= NULL
)
1685 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1690 struct buffer buffer
;
1692 /* When asked for data at offset 0, generate everything and
1693 store into 'result'. Successive reads will be served off
1697 buffer_init (&buffer
);
1699 traceframe_read_info (current_traceframe
, &buffer
);
1701 result
= buffer_finish (&buffer
);
1702 result_length
= strlen (result
);
1703 buffer_free (&buffer
);
1706 if (offset
>= result_length
)
1708 /* We're out of data. */
1715 if (len
> result_length
- offset
)
1716 len
= result_length
- offset
;
1718 memcpy (readbuf
, result
+ offset
, len
);
1722 /* Handle qXfer:fdpic:read. */
1725 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1726 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1728 if (the_target
->read_loadmap
== NULL
)
1731 if (current_thread
== NULL
)
1734 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1737 /* Handle qXfer:btrace:read. */
1740 handle_qxfer_btrace (const char *annex
,
1741 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1742 ULONGEST offset
, LONGEST len
)
1744 static struct buffer cache
;
1745 struct thread_info
*thread
;
1746 enum btrace_read_type type
;
1749 if (the_target
->read_btrace
== NULL
|| writebuf
!= NULL
)
1752 if (ptid_equal (general_thread
, null_ptid
)
1753 || ptid_equal (general_thread
, minus_one_ptid
))
1755 strcpy (own_buf
, "E.Must select a single thread.");
1759 thread
= find_thread_ptid (general_thread
);
1762 strcpy (own_buf
, "E.No such thread.");
1766 if (thread
->btrace
== NULL
)
1768 strcpy (own_buf
, "E.Btrace not enabled.");
1772 if (strcmp (annex
, "all") == 0)
1773 type
= BTRACE_READ_ALL
;
1774 else if (strcmp (annex
, "new") == 0)
1775 type
= BTRACE_READ_NEW
;
1776 else if (strcmp (annex
, "delta") == 0)
1777 type
= BTRACE_READ_DELTA
;
1780 strcpy (own_buf
, "E.Bad annex.");
1786 buffer_free (&cache
);
1788 result
= target_read_btrace (thread
->btrace
, &cache
, type
);
1791 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1795 else if (offset
> cache
.used_size
)
1797 buffer_free (&cache
);
1801 if (len
> cache
.used_size
- offset
)
1802 len
= cache
.used_size
- offset
;
1804 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1809 /* Handle qXfer:btrace-conf:read. */
1812 handle_qxfer_btrace_conf (const char *annex
,
1813 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1814 ULONGEST offset
, LONGEST len
)
1816 static struct buffer cache
;
1817 struct thread_info
*thread
;
1820 if (the_target
->read_btrace_conf
== NULL
|| writebuf
!= NULL
)
1823 if (annex
[0] != '\0')
1826 if (ptid_equal (general_thread
, null_ptid
)
1827 || ptid_equal (general_thread
, minus_one_ptid
))
1829 strcpy (own_buf
, "E.Must select a single thread.");
1833 thread
= find_thread_ptid (general_thread
);
1836 strcpy (own_buf
, "E.No such thread.");
1840 if (thread
->btrace
== NULL
)
1842 strcpy (own_buf
, "E.Btrace not enabled.");
1848 buffer_free (&cache
);
1850 result
= target_read_btrace_conf (thread
->btrace
, &cache
);
1853 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1857 else if (offset
> cache
.used_size
)
1859 buffer_free (&cache
);
1863 if (len
> cache
.used_size
- offset
)
1864 len
= cache
.used_size
- offset
;
1866 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1871 static const struct qxfer qxfer_packets
[] =
1873 { "auxv", handle_qxfer_auxv
},
1874 { "btrace", handle_qxfer_btrace
},
1875 { "btrace-conf", handle_qxfer_btrace_conf
},
1876 { "exec-file", handle_qxfer_exec_file
},
1877 { "fdpic", handle_qxfer_fdpic
},
1878 { "features", handle_qxfer_features
},
1879 { "libraries", handle_qxfer_libraries
},
1880 { "libraries-svr4", handle_qxfer_libraries_svr4
},
1881 { "osdata", handle_qxfer_osdata
},
1882 { "siginfo", handle_qxfer_siginfo
},
1883 { "spu", handle_qxfer_spu
},
1884 { "statictrace", handle_qxfer_statictrace
},
1885 { "threads", handle_qxfer_threads
},
1886 { "traceframe-info", handle_qxfer_traceframe_info
},
1890 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1898 if (!startswith (own_buf
, "qXfer:"))
1901 /* Grab the object, r/w and annex. */
1902 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1904 write_enn (own_buf
);
1909 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1912 const struct qxfer
*q
= &qxfer_packets
[i
];
1914 if (strcmp (object
, q
->object
) == 0)
1916 if (strcmp (rw
, "read") == 0)
1918 unsigned char *data
;
1923 /* Grab the offset and length. */
1924 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1926 write_enn (own_buf
);
1930 /* Read one extra byte, as an indicator of whether there is
1932 if (len
> PBUFSIZ
- 2)
1934 data
= (unsigned char *) malloc (len
+ 1);
1937 write_enn (own_buf
);
1940 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1948 /* Preserve error message. */
1951 write_enn (own_buf
);
1953 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1955 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1960 else if (strcmp (rw
, "write") == 0)
1965 unsigned char *data
;
1967 strcpy (own_buf
, "E00");
1968 data
= (unsigned char *) malloc (packet_len
- (offset
- own_buf
));
1971 write_enn (own_buf
);
1974 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1975 &ofs
, &len
, data
) < 0)
1978 write_enn (own_buf
);
1982 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1990 /* Preserve error message. */
1993 write_enn (own_buf
);
1995 sprintf (own_buf
, "%x", n
);
2008 /* Compute 32 bit CRC from inferior memory.
2010 On success, return 32 bit CRC.
2011 On failure, return (unsigned long long) -1. */
2013 static unsigned long long
2014 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
2018 unsigned char byte
= 0;
2020 /* Return failure if memory read fails. */
2021 if (read_inferior_memory (base
, &byte
, 1) != 0)
2022 return (unsigned long long) -1;
2024 crc
= xcrc32 (&byte
, 1, crc
);
2027 return (unsigned long long) crc
;
2030 /* Add supported btrace packets to BUF. */
2033 supported_btrace_packets (char *buf
)
2035 int btrace_supported
= 0;
2037 if (target_supports_btrace (BTRACE_FORMAT_BTS
))
2039 strcat (buf
, ";Qbtrace:bts+");
2040 strcat (buf
, ";Qbtrace-conf:bts:size+");
2042 btrace_supported
= 1;
2045 if (target_supports_btrace (BTRACE_FORMAT_PT
))
2047 strcat (buf
, ";Qbtrace:pt+");
2048 strcat (buf
, ";Qbtrace-conf:pt:size+");
2050 btrace_supported
= 1;
2053 if (!btrace_supported
)
2056 strcat (buf
, ";Qbtrace:off+");
2057 strcat (buf
, ";qXfer:btrace:read+");
2058 strcat (buf
, ";qXfer:btrace-conf:read+");
2061 /* Handle all of the extended 'q' packets. */
2064 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
2066 static struct inferior_list_entry
*thread_ptr
;
2068 /* Reply the current thread id. */
2069 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
2072 require_running (own_buf
);
2074 if (!ptid_equal (general_thread
, null_ptid
)
2075 && !ptid_equal (general_thread
, minus_one_ptid
))
2076 gdb_id
= general_thread
;
2079 thread_ptr
= get_first_inferior (&all_threads
);
2080 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
2083 sprintf (own_buf
, "QC");
2085 write_ptid (own_buf
, gdb_id
);
2089 if (strcmp ("qSymbol::", own_buf
) == 0)
2091 struct thread_info
*save_thread
= current_thread
;
2093 /* For qSymbol, GDB only changes the current thread if the
2094 previous current thread was of a different process. So if
2095 the previous thread is gone, we need to pick another one of
2096 the same process. This can happen e.g., if we followed an
2097 exec in a non-leader thread. */
2098 if (current_thread
== NULL
)
2101 = find_any_thread_of_pid (ptid_get_pid (general_thread
));
2103 /* Just in case, if we didn't find a thread, then bail out
2104 instead of crashing. */
2105 if (current_thread
== NULL
)
2107 write_enn (own_buf
);
2108 current_thread
= save_thread
;
2113 /* GDB is suggesting new symbols have been loaded. This may
2114 mean a new shared library has been detected as loaded, so
2115 take the opportunity to check if breakpoints we think are
2116 inserted, still are. Note that it isn't guaranteed that
2117 we'll see this when a shared library is loaded, and nor will
2118 we see this for unloads (although breakpoints in unloaded
2119 libraries shouldn't trigger), as GDB may not find symbols for
2120 the library at all. We also re-validate breakpoints when we
2121 see a second GDB breakpoint for the same address, and or when
2122 we access breakpoint shadows. */
2123 validate_breakpoints ();
2125 if (target_supports_tracepoints ())
2126 tracepoint_look_up_symbols ();
2128 if (current_thread
!= NULL
&& the_target
->look_up_symbols
!= NULL
)
2129 (*the_target
->look_up_symbols
) ();
2131 current_thread
= save_thread
;
2133 strcpy (own_buf
, "OK");
2137 if (!disable_packet_qfThreadInfo
)
2139 if (strcmp ("qfThreadInfo", own_buf
) == 0)
2143 require_running (own_buf
);
2144 thread_ptr
= get_first_inferior (&all_threads
);
2147 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
2148 write_ptid (own_buf
, gdb_id
);
2149 thread_ptr
= thread_ptr
->next
;
2153 if (strcmp ("qsThreadInfo", own_buf
) == 0)
2157 require_running (own_buf
);
2158 if (thread_ptr
!= NULL
)
2161 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
2162 write_ptid (own_buf
, gdb_id
);
2163 thread_ptr
= thread_ptr
->next
;
2168 sprintf (own_buf
, "l");
2174 if (the_target
->read_offsets
!= NULL
2175 && strcmp ("qOffsets", own_buf
) == 0)
2177 CORE_ADDR text
, data
;
2179 require_running (own_buf
);
2180 if (the_target
->read_offsets (&text
, &data
))
2181 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
2182 (long)text
, (long)data
, (long)data
);
2184 write_enn (own_buf
);
2189 /* Protocol features query. */
2190 if (startswith (own_buf
, "qSupported")
2191 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
2193 char *p
= &own_buf
[10];
2194 int gdb_supports_qRelocInsn
= 0;
2196 /* Process each feature being provided by GDB. The first
2197 feature will follow a ':', and latter features will follow
2201 char **qsupported
= NULL
;
2206 /* Two passes, to avoid nested strtok calls in
2207 target_process_qsupported. */
2208 for (p
= strtok (p
+ 1, ";");
2210 p
= strtok (NULL
, ";"))
2213 qsupported
= XRESIZEVEC (char *, qsupported
, count
);
2214 qsupported
[count
- 1] = xstrdup (p
);
2217 for (i
= 0; i
< count
; i
++)
2220 if (strcmp (p
, "multiprocess+") == 0)
2222 /* GDB supports and wants multi-process support if
2224 if (target_supports_multi_process ())
2227 else if (strcmp (p
, "qRelocInsn+") == 0)
2229 /* GDB supports relocate instruction requests. */
2230 gdb_supports_qRelocInsn
= 1;
2232 else if (strcmp (p
, "swbreak+") == 0)
2234 /* GDB wants us to report whether a trap is caused
2235 by a software breakpoint and for us to handle PC
2236 adjustment if necessary on this target. */
2237 if (target_supports_stopped_by_sw_breakpoint ())
2238 swbreak_feature
= 1;
2240 else if (strcmp (p
, "hwbreak+") == 0)
2242 /* GDB wants us to report whether a trap is caused
2243 by a hardware breakpoint. */
2244 if (target_supports_stopped_by_hw_breakpoint ())
2245 hwbreak_feature
= 1;
2247 else if (strcmp (p
, "fork-events+") == 0)
2249 /* GDB supports and wants fork events if possible. */
2250 if (target_supports_fork_events ())
2251 report_fork_events
= 1;
2253 else if (strcmp (p
, "vfork-events+") == 0)
2255 /* GDB supports and wants vfork events if possible. */
2256 if (target_supports_vfork_events ())
2257 report_vfork_events
= 1;
2259 else if (strcmp (p
, "exec-events+") == 0)
2261 /* GDB supports and wants exec events if possible. */
2262 if (target_supports_exec_events ())
2263 report_exec_events
= 1;
2265 else if (strcmp (p
, "vContSupported+") == 0)
2266 vCont_supported
= 1;
2267 else if (strcmp (p
, "QThreadEvents+") == 0)
2269 else if (strcmp (p
, "no-resumed+") == 0)
2271 /* GDB supports and wants TARGET_WAITKIND_NO_RESUMED
2273 report_no_resumed
= 1;
2277 /* Move the unknown features all together. */
2278 qsupported
[i
] = NULL
;
2279 qsupported
[unknown
] = p
;
2284 /* Give the target backend a chance to process the unknown
2286 target_process_qsupported (qsupported
, unknown
);
2288 for (i
= 0; i
< count
; i
++)
2289 free (qsupported
[i
]);
2294 "PacketSize=%x;QPassSignals+;QProgramSignals+;"
2295 "QStartupWithShell+;QEnvironmentHexEncoded+;"
2296 "QEnvironmentReset+;QEnvironmentUnset+",
2299 if (target_supports_catch_syscall ())
2300 strcat (own_buf
, ";QCatchSyscalls+");
2302 if (the_target
->qxfer_libraries_svr4
!= NULL
)
2303 strcat (own_buf
, ";qXfer:libraries-svr4:read+"
2304 ";augmented-libraries-svr4-read+");
2307 /* We do not have any hook to indicate whether the non-SVR4 target
2308 backend supports qXfer:libraries:read, so always report it. */
2309 strcat (own_buf
, ";qXfer:libraries:read+");
2312 if (the_target
->read_auxv
!= NULL
)
2313 strcat (own_buf
, ";qXfer:auxv:read+");
2315 if (the_target
->qxfer_spu
!= NULL
)
2316 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
2318 if (the_target
->qxfer_siginfo
!= NULL
)
2319 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
2321 if (the_target
->read_loadmap
!= NULL
)
2322 strcat (own_buf
, ";qXfer:fdpic:read+");
2324 /* We always report qXfer:features:read, as targets may
2325 install XML files on a subsequent call to arch_setup.
2326 If we reported to GDB on startup that we don't support
2327 qXfer:feature:read at all, we will never be re-queried. */
2328 strcat (own_buf
, ";qXfer:features:read+");
2330 if (transport_is_reliable
)
2331 strcat (own_buf
, ";QStartNoAckMode+");
2333 if (the_target
->qxfer_osdata
!= NULL
)
2334 strcat (own_buf
, ";qXfer:osdata:read+");
2336 if (target_supports_multi_process ())
2337 strcat (own_buf
, ";multiprocess+");
2339 if (target_supports_fork_events ())
2340 strcat (own_buf
, ";fork-events+");
2342 if (target_supports_vfork_events ())
2343 strcat (own_buf
, ";vfork-events+");
2345 if (target_supports_exec_events ())
2346 strcat (own_buf
, ";exec-events+");
2348 if (target_supports_non_stop ())
2349 strcat (own_buf
, ";QNonStop+");
2351 if (target_supports_disable_randomization ())
2352 strcat (own_buf
, ";QDisableRandomization+");
2354 strcat (own_buf
, ";qXfer:threads:read+");
2356 if (target_supports_tracepoints ())
2358 strcat (own_buf
, ";ConditionalTracepoints+");
2359 strcat (own_buf
, ";TraceStateVariables+");
2360 strcat (own_buf
, ";TracepointSource+");
2361 strcat (own_buf
, ";DisconnectedTracing+");
2362 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
2363 strcat (own_buf
, ";FastTracepoints+");
2364 strcat (own_buf
, ";StaticTracepoints+");
2365 strcat (own_buf
, ";InstallInTrace+");
2366 strcat (own_buf
, ";qXfer:statictrace:read+");
2367 strcat (own_buf
, ";qXfer:traceframe-info:read+");
2368 strcat (own_buf
, ";EnableDisableTracepoints+");
2369 strcat (own_buf
, ";QTBuffer:size+");
2370 strcat (own_buf
, ";tracenz+");
2373 if (target_supports_hardware_single_step ()
2374 || target_supports_software_single_step () )
2376 strcat (own_buf
, ";ConditionalBreakpoints+");
2378 strcat (own_buf
, ";BreakpointCommands+");
2380 if (target_supports_agent ())
2381 strcat (own_buf
, ";QAgent+");
2383 supported_btrace_packets (own_buf
);
2385 if (target_supports_stopped_by_sw_breakpoint ())
2386 strcat (own_buf
, ";swbreak+");
2388 if (target_supports_stopped_by_hw_breakpoint ())
2389 strcat (own_buf
, ";hwbreak+");
2391 if (the_target
->pid_to_exec_file
!= NULL
)
2392 strcat (own_buf
, ";qXfer:exec-file:read+");
2394 strcat (own_buf
, ";vContSupported+");
2396 strcat (own_buf
, ";QThreadEvents+");
2398 strcat (own_buf
, ";no-resumed+");
2400 /* Reinitialize components as needed for the new connection. */
2401 hostio_handle_new_gdb_connection ();
2402 target_handle_new_gdb_connection ();
2407 /* Thread-local storage support. */
2408 if (the_target
->get_tls_address
!= NULL
2409 && startswith (own_buf
, "qGetTLSAddr:"))
2411 char *p
= own_buf
+ 12;
2412 CORE_ADDR parts
[2], address
= 0;
2414 ptid_t ptid
= null_ptid
;
2416 require_running (own_buf
);
2418 for (i
= 0; i
< 3; i
++)
2426 p2
= strchr (p
, ',');
2439 ptid
= read_ptid (p
, NULL
);
2441 decode_address (&parts
[i
- 1], p
, len
);
2445 if (p
!= NULL
|| i
< 3)
2449 struct thread_info
*thread
= find_thread_ptid (ptid
);
2454 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
2460 strcpy (own_buf
, paddress(address
));
2465 write_enn (own_buf
);
2469 /* Otherwise, pretend we do not understand this packet. */
2472 /* Windows OS Thread Information Block address support. */
2473 if (the_target
->get_tib_address
!= NULL
2474 && startswith (own_buf
, "qGetTIBAddr:"))
2479 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
2481 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
2484 strcpy (own_buf
, paddress(tlb
));
2489 write_enn (own_buf
);
2495 /* Handle "monitor" commands. */
2496 if (startswith (own_buf
, "qRcmd,"))
2498 char *mon
= (char *) malloc (PBUFSIZ
);
2499 int len
= strlen (own_buf
+ 6);
2503 write_enn (own_buf
);
2508 || hex2bin (own_buf
+ 6, (gdb_byte
*) mon
, len
/ 2) != len
/ 2)
2510 write_enn (own_buf
);
2514 mon
[len
/ 2] = '\0';
2518 if (the_target
->handle_monitor_command
== NULL
2519 || (*the_target
->handle_monitor_command
) (mon
) == 0)
2520 /* Default processing. */
2521 handle_monitor_command (mon
, own_buf
);
2527 if (startswith (own_buf
, "qSearch:memory:"))
2529 require_running (own_buf
);
2530 handle_search_memory (own_buf
, packet_len
);
2534 if (strcmp (own_buf
, "qAttached") == 0
2535 || startswith (own_buf
, "qAttached:"))
2537 struct process_info
*process
;
2539 if (own_buf
[sizeof ("qAttached") - 1])
2541 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
2542 process
= (struct process_info
*)
2543 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
2547 require_running (own_buf
);
2548 process
= current_process ();
2551 if (process
== NULL
)
2553 write_enn (own_buf
);
2557 strcpy (own_buf
, process
->attached
? "1" : "0");
2561 if (startswith (own_buf
, "qCRC:"))
2563 /* CRC check (compare-section). */
2567 unsigned long long crc
;
2569 require_running (own_buf
);
2570 comma
= unpack_varlen_hex (own_buf
+ 5, &base
);
2571 if (*comma
++ != ',')
2573 write_enn (own_buf
);
2576 len
= strtoul (comma
, NULL
, 16);
2577 crc
= crc32 (base
, len
, 0xffffffff);
2578 /* Check for memory failure. */
2579 if (crc
== (unsigned long long) -1)
2581 write_enn (own_buf
);
2584 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
2588 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
2591 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
2594 /* Otherwise we didn't know what packet it was. Say we didn't
2599 static void gdb_wants_all_threads_stopped (void);
2600 static void resume (struct thread_resume
*actions
, size_t n
);
2602 /* The callback that is passed to visit_actioned_threads. */
2603 typedef int (visit_actioned_threads_callback_ftype
)
2604 (const struct thread_resume
*, struct thread_info
*);
2606 /* Struct to pass data to visit_actioned_threads. */
2608 struct visit_actioned_threads_data
2610 const struct thread_resume
*actions
;
2612 visit_actioned_threads_callback_ftype
*callback
;
2615 /* Call CALLBACK for any thread to which ACTIONS applies to. Returns
2616 true if CALLBACK returns true. Returns false if no matching thread
2617 is found or CALLBACK results false.
2618 Note: This function is itself a callback for find_inferior. */
2621 visit_actioned_threads (struct inferior_list_entry
*entry
, void *datap
)
2623 struct visit_actioned_threads_data
*data
2624 = (struct visit_actioned_threads_data
*) datap
;
2625 const struct thread_resume
*actions
= data
->actions
;
2626 size_t num_actions
= data
->num_actions
;
2627 visit_actioned_threads_callback_ftype
*callback
= data
->callback
;
2630 for (i
= 0; i
< num_actions
; i
++)
2632 const struct thread_resume
*action
= &actions
[i
];
2634 if (ptid_equal (action
->thread
, minus_one_ptid
)
2635 || ptid_equal (action
->thread
, entry
->id
)
2636 || ((ptid_get_pid (action
->thread
)
2637 == ptid_get_pid (entry
->id
))
2638 && ptid_get_lwp (action
->thread
) == -1))
2640 struct thread_info
*thread
= (struct thread_info
*) entry
;
2642 if ((*callback
) (action
, thread
))
2650 /* Callback for visit_actioned_threads. If the thread has a pending
2651 status to report, report it now. */
2654 handle_pending_status (const struct thread_resume
*resumption
,
2655 struct thread_info
*thread
)
2657 if (thread
->status_pending_p
)
2659 thread
->status_pending_p
= 0;
2661 last_status
= thread
->last_status
;
2662 last_ptid
= thread
->entry
.id
;
2663 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2669 /* Parse vCont packets. */
2671 handle_v_cont (char *own_buf
)
2675 struct thread_resume
*resume_info
;
2676 struct thread_resume default_action
{ null_ptid
};
2678 /* Count the number of semicolons in the packet. There should be one
2679 for every action. */
2685 p
= strchr (p
, ';');
2688 resume_info
= (struct thread_resume
*) malloc (n
* sizeof (resume_info
[0]));
2689 if (resume_info
== NULL
)
2697 memset (&resume_info
[i
], 0, sizeof resume_info
[i
]);
2699 if (p
[0] == 's' || p
[0] == 'S')
2700 resume_info
[i
].kind
= resume_step
;
2701 else if (p
[0] == 'r')
2702 resume_info
[i
].kind
= resume_step
;
2703 else if (p
[0] == 'c' || p
[0] == 'C')
2704 resume_info
[i
].kind
= resume_continue
;
2705 else if (p
[0] == 't')
2706 resume_info
[i
].kind
= resume_stop
;
2710 if (p
[0] == 'S' || p
[0] == 'C')
2713 sig
= strtol (p
+ 1, &q
, 16);
2718 if (!gdb_signal_to_host_p ((enum gdb_signal
) sig
))
2720 resume_info
[i
].sig
= gdb_signal_to_host ((enum gdb_signal
) sig
);
2722 else if (p
[0] == 'r')
2726 p
= unpack_varlen_hex (p
+ 1, &addr
);
2727 resume_info
[i
].step_range_start
= addr
;
2732 p
= unpack_varlen_hex (p
+ 1, &addr
);
2733 resume_info
[i
].step_range_end
= addr
;
2742 resume_info
[i
].thread
= minus_one_ptid
;
2743 default_action
= resume_info
[i
];
2745 /* Note: we don't increment i here, we'll overwrite this entry
2746 the next time through. */
2748 else if (p
[0] == ':')
2750 ptid_t ptid
= read_ptid (p
+ 1, &q
);
2755 if (p
[0] != ';' && p
[0] != 0)
2758 resume_info
[i
].thread
= ptid
;
2765 resume_info
[i
] = default_action
;
2767 resume (resume_info
, n
);
2772 write_enn (own_buf
);
2777 /* Resume target with ACTIONS, an array of NUM_ACTIONS elements. */
2780 resume (struct thread_resume
*actions
, size_t num_actions
)
2784 /* Check if among the threads that GDB wants actioned, there's
2785 one with a pending status to report. If so, skip actually
2786 resuming/stopping and report the pending event
2788 struct visit_actioned_threads_data data
;
2790 data
.actions
= actions
;
2791 data
.num_actions
= num_actions
;
2792 data
.callback
= handle_pending_status
;
2793 if (find_inferior (&all_threads
, visit_actioned_threads
, &data
) != NULL
)
2799 (*the_target
->resume
) (actions
, num_actions
);
2805 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2807 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
2808 && !report_no_resumed
)
2810 /* The client does not support this stop reply. At least
2812 sprintf (own_buf
, "E.No unwaited-for children left.");
2813 disable_async_io ();
2817 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2818 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
2819 && last_status
.kind
!= TARGET_WAITKIND_NO_RESUMED
)
2820 current_thread
->last_status
= last_status
;
2822 /* From the client's perspective, all-stop mode always stops all
2823 threads implicitly (and the target backend has already done
2824 so by now). Tag all threads as "want-stopped", so we don't
2825 resume them implicitly without the client telling us to. */
2826 gdb_wants_all_threads_stopped ();
2827 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2828 disable_async_io ();
2830 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2831 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2832 target_mourn_inferior (last_ptid
);
2836 /* Attach to a new program. Return 1 if successful, 0 if failure. */
2838 handle_v_attach (char *own_buf
)
2842 pid
= strtol (own_buf
+ 8, NULL
, 16);
2843 if (pid
!= 0 && attach_inferior (pid
) == 0)
2845 /* Don't report shared library events after attaching, even if
2846 some libraries are preloaded. GDB will always poll the
2847 library list. Avoids the "stopped by shared library event"
2848 notice on the GDB side. */
2853 /* In non-stop, we don't send a resume reply. Stop events
2854 will follow up using the normal notification
2859 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2865 write_enn (own_buf
);
2870 /* Run a new program. Return 1 if successful, 0 if failure. */
2872 handle_v_run (char *own_buf
)
2875 std::vector
<char *> new_argv
;
2876 char *new_program_name
= NULL
;
2880 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
2886 for (i
= 0, p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
, ++i
)
2888 next_p
= strchr (p
, ';');
2890 next_p
= p
+ strlen (p
);
2892 if (i
== 0 && p
== next_p
)
2894 /* No program specified. */
2895 new_program_name
= NULL
;
2897 else if (p
== next_p
)
2899 /* Empty argument. */
2900 new_argv
.push_back (xstrdup ("''"));
2904 size_t len
= (next_p
- p
) / 2;
2905 /* ARG is the unquoted argument received via the RSP. */
2906 char *arg
= (char *) xmalloc (len
+ 1);
2907 /* FULL_ARGS will contain the quoted version of ARG. */
2908 char *full_arg
= (char *) xmalloc ((len
+ 1) * 2);
2909 /* These are pointers used to navigate the strings above. */
2910 char *tmp_arg
= arg
;
2911 char *tmp_full_arg
= full_arg
;
2914 hex2bin (p
, (gdb_byte
*) arg
, len
);
2917 while (*tmp_arg
!= '\0')
2923 *tmp_full_arg
= '\'';
2929 /* Quote single quote. */
2930 *tmp_full_arg
= '\\';
2938 *tmp_full_arg
= *tmp_arg
;
2944 *tmp_full_arg
++ = '\'';
2946 /* Finish FULL_ARG and push it into the vector containing
2948 *tmp_full_arg
= '\0';
2950 new_program_name
= full_arg
;
2952 new_argv
.push_back (full_arg
);
2958 new_argv
.push_back (NULL
);
2960 if (new_program_name
== NULL
)
2962 /* GDB didn't specify a program to run. Use the program from the
2963 last run with the new argument list. */
2964 if (program_name
== NULL
)
2966 write_enn (own_buf
);
2967 free_vector_argv (new_argv
);
2973 xfree (program_name
);
2974 program_name
= new_program_name
;
2977 /* Free the old argv and install the new one. */
2978 free_vector_argv (program_args
);
2979 program_args
= new_argv
;
2981 create_inferior (program_name
, program_args
);
2983 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
2985 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2987 /* In non-stop, sending a resume reply doesn't set the general
2988 thread, but GDB assumes a vRun sets it (this is so GDB can
2989 query which is the main thread of the new inferior. */
2991 general_thread
= last_ptid
;
2997 write_enn (own_buf
);
3002 /* Kill process. Return 1 if successful, 0 if failure. */
3004 handle_v_kill (char *own_buf
)
3007 char *p
= &own_buf
[6];
3009 pid
= strtol (p
, NULL
, 16);
3012 if (pid
!= 0 && kill_inferior (pid
) == 0)
3014 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
3015 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
3016 last_ptid
= pid_to_ptid (pid
);
3017 discard_queued_stop_replies (last_ptid
);
3023 write_enn (own_buf
);
3028 /* Handle all of the extended 'v' packets. */
3030 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
3032 if (!disable_packet_vCont
)
3034 if (strcmp (own_buf
, "vCtrlC") == 0)
3036 (*the_target
->request_interrupt
) ();
3041 if (startswith (own_buf
, "vCont;"))
3043 handle_v_cont (own_buf
);
3047 if (startswith (own_buf
, "vCont?"))
3049 strcpy (own_buf
, "vCont;c;C;t");
3051 if (target_supports_hardware_single_step ()
3052 || target_supports_software_single_step ()
3053 || !vCont_supported
)
3055 /* If target supports single step either by hardware or by
3056 software, add actions s and S to the list of supported
3057 actions. On the other hand, if GDB doesn't request the
3058 supported vCont actions in qSupported packet, add s and
3059 S to the list too. */
3060 own_buf
= own_buf
+ strlen (own_buf
);
3061 strcpy (own_buf
, ";s;S");
3064 if (target_supports_range_stepping ())
3066 own_buf
= own_buf
+ strlen (own_buf
);
3067 strcpy (own_buf
, ";r");
3073 if (startswith (own_buf
, "vFile:")
3074 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
3077 if (startswith (own_buf
, "vAttach;"))
3079 if ((!extended_protocol
|| !multi_process
) && target_running ())
3081 fprintf (stderr
, "Already debugging a process\n");
3082 write_enn (own_buf
);
3085 handle_v_attach (own_buf
);
3089 if (startswith (own_buf
, "vRun;"))
3091 if ((!extended_protocol
|| !multi_process
) && target_running ())
3093 fprintf (stderr
, "Already debugging a process\n");
3094 write_enn (own_buf
);
3097 handle_v_run (own_buf
);
3101 if (startswith (own_buf
, "vKill;"))
3103 if (!target_running ())
3105 fprintf (stderr
, "No process to kill\n");
3106 write_enn (own_buf
);
3109 handle_v_kill (own_buf
);
3113 if (handle_notif_ack (own_buf
, packet_len
))
3116 /* Otherwise we didn't know what packet it was. Say we didn't
3122 /* Resume thread and wait for another event. In non-stop mode,
3123 don't really wait here, but return immediatelly to the event
3126 myresume (char *own_buf
, int step
, int sig
)
3128 struct thread_resume resume_info
[2];
3130 int valid_cont_thread
;
3132 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
3133 && !ptid_equal (cont_thread
, minus_one_ptid
));
3135 if (step
|| sig
|| valid_cont_thread
)
3137 resume_info
[0].thread
= current_ptid
;
3139 resume_info
[0].kind
= resume_step
;
3141 resume_info
[0].kind
= resume_continue
;
3142 resume_info
[0].sig
= sig
;
3146 if (!valid_cont_thread
)
3148 resume_info
[n
].thread
= minus_one_ptid
;
3149 resume_info
[n
].kind
= resume_continue
;
3150 resume_info
[n
].sig
= 0;
3154 resume (resume_info
, n
);
3157 /* Callback for for_each_inferior. Make a new stop reply for each
3161 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
3163 struct thread_info
*thread
= (struct thread_info
*) entry
;
3165 /* For now, assume targets that don't have this callback also don't
3166 manage the thread's last_status field. */
3167 if (the_target
->thread_stopped
== NULL
)
3169 struct vstop_notif
*new_notif
= XNEW (struct vstop_notif
);
3171 new_notif
->ptid
= entry
->id
;
3172 new_notif
->status
= thread
->last_status
;
3173 /* Pass the last stop reply back to GDB, but don't notify
3175 notif_event_enque (¬if_stop
,
3176 (struct notif_event
*) new_notif
);
3180 if (thread_stopped (thread
))
3184 std::string status_string
3185 = target_waitstatus_to_string (&thread
->last_status
);
3187 debug_printf ("Reporting thread %s as already stopped with %s\n",
3188 target_pid_to_str (entry
->id
),
3189 status_string
.c_str ());
3192 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
3194 /* Pass the last stop reply back to GDB, but don't notify
3196 queue_stop_reply (entry
->id
, &thread
->last_status
);
3203 /* Set this inferior threads's state as "want-stopped". We won't
3204 resume this thread until the client gives us another action for
3208 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
3210 struct thread_info
*thread
= (struct thread_info
*) entry
;
3212 thread
->last_resume_kind
= resume_stop
;
3214 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
3216 /* Most threads are stopped implicitly (all-stop); tag that with
3218 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
3219 thread
->last_status
.value
.sig
= GDB_SIGNAL_0
;
3223 /* Set all threads' states as "want-stopped". */
3226 gdb_wants_all_threads_stopped (void)
3228 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
3231 /* Clear the gdb_detached flag of every process. */
3234 gdb_reattached_process (struct inferior_list_entry
*entry
)
3236 struct process_info
*process
= (struct process_info
*) entry
;
3238 process
->gdb_detached
= 0;
3241 /* Callback for for_each_inferior. Clear the thread's pending status
3245 clear_pending_status_callback (struct inferior_list_entry
*entry
)
3247 struct thread_info
*thread
= (struct thread_info
*) entry
;
3249 thread
->status_pending_p
= 0;
3252 /* Callback for for_each_inferior. If the thread is stopped with an
3253 interesting event, mark it as having a pending event. */
3256 set_pending_status_callback (struct inferior_list_entry
*entry
)
3258 struct thread_info
*thread
= (struct thread_info
*) entry
;
3260 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
3261 || (thread
->last_status
.value
.sig
!= GDB_SIGNAL_0
3262 /* A breakpoint, watchpoint or finished step from a previous
3263 GDB run isn't considered interesting for a new GDB run.
3264 If we left those pending, the new GDB could consider them
3265 random SIGTRAPs. This leaves out real async traps. We'd
3266 have to peek into the (target-specific) siginfo to
3267 distinguish those. */
3268 && thread
->last_status
.value
.sig
!= GDB_SIGNAL_TRAP
))
3269 thread
->status_pending_p
= 1;
3272 /* Callback for find_inferior. Return true if ENTRY (a thread) has a
3273 pending status to report to GDB. */
3276 find_status_pending_thread_callback (struct inferior_list_entry
*entry
, void *data
)
3278 struct thread_info
*thread
= (struct thread_info
*) entry
;
3280 return thread
->status_pending_p
;
3283 /* Status handler for the '?' packet. */
3286 handle_status (char *own_buf
)
3288 /* GDB is connected, don't forward events to the target anymore. */
3289 for_each_inferior (&all_processes
, gdb_reattached_process
);
3291 /* In non-stop mode, we must send a stop reply for each stopped
3292 thread. In all-stop mode, just send one for the first stopped
3297 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
3299 /* The first is sent immediatly. OK is sent if there is no
3300 stopped thread, which is the same handling of the vStopped
3301 packet (by design). */
3302 notif_write_event (¬if_stop
, own_buf
);
3306 struct inferior_list_entry
*thread
= NULL
;
3309 stabilize_threads ();
3310 gdb_wants_all_threads_stopped ();
3312 /* We can only report one status, but we might be coming out of
3313 non-stop -- if more than one thread is stopped with
3314 interesting events, leave events for the threads we're not
3315 reporting now pending. They'll be reported the next time the
3316 threads are resumed. Start by marking all interesting events
3318 for_each_inferior (&all_threads
, set_pending_status_callback
);
3320 /* Prefer the last thread that reported an event to GDB (even if
3321 that was a GDB_SIGNAL_TRAP). */
3322 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
3323 && last_status
.kind
!= TARGET_WAITKIND_EXITED
3324 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
3325 thread
= find_inferior_id (&all_threads
, last_ptid
);
3327 /* If the last event thread is not found for some reason, look
3328 for some other thread that might have an event to report. */
3330 thread
= find_inferior (&all_threads
,
3331 find_status_pending_thread_callback
, NULL
);
3333 /* If we're still out of luck, simply pick the first thread in
3336 thread
= get_first_inferior (&all_threads
);
3340 struct thread_info
*tp
= (struct thread_info
*) thread
;
3342 /* We're reporting this event, so it's no longer
3344 tp
->status_pending_p
= 0;
3346 /* GDB assumes the current thread is the thread we're
3347 reporting the status for. */
3348 general_thread
= thread
->id
;
3349 set_desired_thread (1);
3351 gdb_assert (tp
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
3352 prepare_resume_reply (own_buf
, tp
->entry
.id
, &tp
->last_status
);
3355 strcpy (own_buf
, "W00");
3360 gdbserver_version (void)
3362 printf ("GNU gdbserver %s%s\n"
3363 "Copyright (C) 2017 Free Software Foundation, Inc.\n"
3364 "gdbserver is free software, covered by the "
3365 "GNU General Public License.\n"
3366 "This gdbserver was configured as \"%s\"\n",
3367 PKGVERSION
, version
, host_name
);
3371 gdbserver_usage (FILE *stream
)
3373 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
3374 "\tgdbserver [OPTIONS] --attach COMM PID\n"
3375 "\tgdbserver [OPTIONS] --multi COMM\n"
3377 "COMM may either be a tty device (for serial debugging),\n"
3378 "HOST:PORT to listen for a TCP connection, or '-' or 'stdio' to use \n"
3379 "stdin/stdout of gdbserver.\n"
3380 "PROG is the executable program. ARGS are arguments passed to inferior.\n"
3381 "PID is the process ID to attach to, when --attach is specified.\n"
3383 "Operating modes:\n"
3385 " --attach Attach to running process PID.\n"
3386 " --multi Start server without a specific program, and\n"
3387 " only quit when explicitly commanded.\n"
3388 " --once Exit after the first connection has closed.\n"
3389 " --help Print this message and then exit.\n"
3390 " --version Display version information and exit.\n"
3394 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
3395 " --disable-randomization\n"
3396 " Run PROG with address space randomization disabled.\n"
3397 " --no-disable-randomization\n"
3398 " Don't disable address space randomization when\n"
3400 " --startup-with-shell\n"
3401 " Start PROG using a shell. I.e., execs a shell that\n"
3402 " then execs PROG. (default)\n"
3403 " --no-startup-with-shell\n"
3404 " Exec PROG directly instead of using a shell.\n"
3405 " Disables argument globbing and variable substitution\n"
3406 " on UNIX-like systems.\n"
3410 " --debug Enable general debugging output.\n"
3411 " --debug-format=opt1[,opt2,...]\n"
3412 " Specify extra content in debugging output.\n"
3417 " --remote-debug Enable remote protocol debugging output.\n"
3418 " --disable-packet=opt1[,opt2,...]\n"
3419 " Disable support for RSP packets or features.\n"
3421 " vCont, Tthread, qC, qfThreadInfo and \n"
3422 " threads (disable all threading packets).\n"
3424 "For more information, consult the GDB manual (available as on-line \n"
3425 "info or a printed manual).\n");
3426 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
3427 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
3431 gdbserver_show_disableable (FILE *stream
)
3433 fprintf (stream
, "Disableable packets:\n"
3434 " vCont \tAll vCont packets\n"
3435 " qC \tQuerying the current thread\n"
3436 " qfThreadInfo\tThread listing\n"
3437 " Tthread \tPassing the thread specifier in the "
3438 "T stop reply packet\n"
3439 " threads \tAll of the above\n");
3443 #undef require_running
3444 #define require_running(BUF) \
3445 if (!target_running ()) \
3452 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
3454 int pid
= * (int *) args
;
3456 if (ptid_get_pid (entry
->id
) == pid
)
3463 kill_inferior_callback (struct inferior_list_entry
*entry
)
3465 struct process_info
*process
= (struct process_info
*) entry
;
3466 int pid
= ptid_get_pid (process
->entry
.id
);
3468 kill_inferior (pid
);
3469 discard_queued_stop_replies (pid_to_ptid (pid
));
3472 /* Callback for for_each_inferior to detach or kill the inferior,
3473 depending on whether we attached to it or not.
3474 We inform the user whether we're detaching or killing the process
3475 as this is only called when gdbserver is about to exit. */
3478 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
3480 struct process_info
*process
= (struct process_info
*) entry
;
3481 int pid
= ptid_get_pid (process
->entry
.id
);
3483 if (process
->attached
)
3484 detach_inferior (pid
);
3486 kill_inferior (pid
);
3488 discard_queued_stop_replies (pid_to_ptid (pid
));
3491 /* for_each_inferior callback for detach_or_kill_for_exit to print
3492 the pids of started inferiors. */
3495 print_started_pid (struct inferior_list_entry
*entry
)
3497 struct process_info
*process
= (struct process_info
*) entry
;
3499 if (! process
->attached
)
3501 int pid
= ptid_get_pid (process
->entry
.id
);
3502 fprintf (stderr
, " %d", pid
);
3506 /* for_each_inferior callback for detach_or_kill_for_exit to print
3507 the pids of attached inferiors. */
3510 print_attached_pid (struct inferior_list_entry
*entry
)
3512 struct process_info
*process
= (struct process_info
*) entry
;
3514 if (process
->attached
)
3516 int pid
= ptid_get_pid (process
->entry
.id
);
3517 fprintf (stderr
, " %d", pid
);
3521 /* Call this when exiting gdbserver with possible inferiors that need
3522 to be killed or detached from. */
3525 detach_or_kill_for_exit (void)
3527 /* First print a list of the inferiors we will be killing/detaching.
3528 This is to assist the user, for example, in case the inferior unexpectedly
3529 dies after we exit: did we screw up or did the inferior exit on its own?
3530 Having this info will save some head-scratching. */
3532 if (have_started_inferiors_p ())
3534 fprintf (stderr
, "Killing process(es):");
3535 for_each_inferior (&all_processes
, print_started_pid
);
3536 fprintf (stderr
, "\n");
3538 if (have_attached_inferiors_p ())
3540 fprintf (stderr
, "Detaching process(es):");
3541 for_each_inferior (&all_processes
, print_attached_pid
);
3542 fprintf (stderr
, "\n");
3545 /* Now we can kill or detach the inferiors. */
3547 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
3550 /* Value that will be passed to exit(3) when gdbserver exits. */
3551 static int exit_code
;
3553 /* Cleanup version of detach_or_kill_for_exit. */
3556 detach_or_kill_for_exit_cleanup (void *ignore
)
3561 detach_or_kill_for_exit ();
3564 CATCH (exception
, RETURN_MASK_ALL
)
3567 fprintf (stderr
, "Detach or kill failed: %s\n", exception
.message
);
3573 /* Main function. This is called by the real "main" function,
3574 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
3576 static void ATTRIBUTE_NORETURN
3577 captured_main (int argc
, char *argv
[])
3582 const char *port
= NULL
;
3583 char **next_arg
= &argv
[1];
3584 volatile int multi_mode
= 0;
3585 volatile int attach
= 0;
3587 bool selftest
= false;
3589 while (*next_arg
!= NULL
&& **next_arg
== '-')
3591 if (strcmp (*next_arg
, "--version") == 0)
3593 gdbserver_version ();
3596 else if (strcmp (*next_arg
, "--help") == 0)
3598 gdbserver_usage (stdout
);
3601 else if (strcmp (*next_arg
, "--attach") == 0)
3603 else if (strcmp (*next_arg
, "--multi") == 0)
3605 else if (strcmp (*next_arg
, "--wrapper") == 0)
3612 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
3614 wrapper_argv
+= *next_arg
;
3615 wrapper_argv
+= ' ';
3619 if (!wrapper_argv
.empty ())
3621 /* Erase the last whitespace. */
3622 wrapper_argv
.erase (wrapper_argv
.end () - 1);
3625 if (next_arg
== tmp
|| *next_arg
== NULL
)
3627 gdbserver_usage (stderr
);
3631 /* Consume the "--". */
3634 else if (strcmp (*next_arg
, "--debug") == 0)
3636 else if (startswith (*next_arg
, "--debug-format="))
3639 = parse_debug_format_options ((*next_arg
)
3640 + sizeof ("--debug-format=") - 1, 0);
3642 if (error_msg
!= NULL
)
3644 fprintf (stderr
, "%s", error_msg
);
3648 else if (strcmp (*next_arg
, "--remote-debug") == 0)
3650 else if (strcmp (*next_arg
, "--disable-packet") == 0)
3652 gdbserver_show_disableable (stdout
);
3655 else if (startswith (*next_arg
, "--disable-packet="))
3657 char *packets
, *tok
;
3659 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
3660 for (tok
= strtok (packets
, ",");
3662 tok
= strtok (NULL
, ","))
3664 if (strcmp ("vCont", tok
) == 0)
3665 disable_packet_vCont
= 1;
3666 else if (strcmp ("Tthread", tok
) == 0)
3667 disable_packet_Tthread
= 1;
3668 else if (strcmp ("qC", tok
) == 0)
3669 disable_packet_qC
= 1;
3670 else if (strcmp ("qfThreadInfo", tok
) == 0)
3671 disable_packet_qfThreadInfo
= 1;
3672 else if (strcmp ("threads", tok
) == 0)
3674 disable_packet_vCont
= 1;
3675 disable_packet_Tthread
= 1;
3676 disable_packet_qC
= 1;
3677 disable_packet_qfThreadInfo
= 1;
3681 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
3683 gdbserver_show_disableable (stderr
);
3688 else if (strcmp (*next_arg
, "-") == 0)
3690 /* "-" specifies a stdio connection and is a form of port
3692 port
= STDIO_CONNECTION_NAME
;
3696 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
3697 disable_randomization
= 1;
3698 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
3699 disable_randomization
= 0;
3700 else if (strcmp (*next_arg
, "--startup-with-shell") == 0)
3701 startup_with_shell
= true;
3702 else if (strcmp (*next_arg
, "--no-startup-with-shell") == 0)
3703 startup_with_shell
= false;
3704 else if (strcmp (*next_arg
, "--once") == 0)
3706 else if (strcmp (*next_arg
, "--selftest") == 0)
3710 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
3723 if ((port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
3726 gdbserver_usage (stderr
);
3730 /* Remember stdio descriptors. LISTEN_DESC must not be listed, it will be
3731 opened by remote_prepare. */
3734 save_original_signals_state ();
3736 /* We need to know whether the remote connection is stdio before
3737 starting the inferior. Inferiors created in this scenario have
3738 stdin,stdout redirected. So do this here before we call
3741 remote_prepare (port
);
3746 /* --attach used to come after PORT, so allow it there for
3748 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
3755 && (*next_arg
== NULL
3756 || (*next_arg
)[0] == '\0'
3757 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
3759 || next_arg
[1] != NULL
))
3764 gdbserver_usage (stderr
);
3768 /* Gather information about the environment. */
3769 our_environ
= gdb_environ::from_host_environ ();
3771 initialize_async_io ();
3773 have_job_control ();
3774 initialize_event_loop ();
3775 if (target_supports_tracepoints ())
3776 initialize_tracepoint ();
3777 initialize_notif ();
3779 own_buf
= (char *) xmalloc (PBUFSIZ
+ 1);
3780 mem_buf
= (unsigned char *) xmalloc (PBUFSIZ
);
3784 selftests::run_tests ();
3785 throw_quit ("Quit");
3788 if (pid
== 0 && *next_arg
!= NULL
)
3792 n
= argc
- (next_arg
- argv
);
3793 program_name
= xstrdup (next_arg
[0]);
3794 for (i
= 1; i
< n
; i
++)
3795 program_args
.push_back (xstrdup (next_arg
[i
]));
3796 program_args
.push_back (NULL
);
3798 /* Wait till we are at first instruction in program. */
3799 create_inferior (program_name
, program_args
);
3801 /* We are now (hopefully) stopped at the first instruction of
3802 the target process. This assumes that the target process was
3803 successfully created. */
3807 if (attach_inferior (pid
) == -1)
3808 error ("Attaching not supported on this target");
3810 /* Otherwise succeeded. */
3814 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3815 last_status
.value
.integer
= 0;
3816 last_ptid
= minus_one_ptid
;
3818 make_cleanup (detach_or_kill_for_exit_cleanup
, NULL
);
3820 /* Don't report shared library events on the initial connection,
3821 even if some libraries are preloaded. Avoids the "stopped by
3822 shared library event" notice on gdb side. */
3825 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3826 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3831 if (!was_running
&& !multi_mode
)
3832 error ("No program to debug");
3839 report_fork_events
= 0;
3840 report_vfork_events
= 0;
3841 report_exec_events
= 0;
3842 /* Be sure we're out of tfind mode. */
3843 current_traceframe
= -1;
3844 cont_thread
= null_ptid
;
3845 swbreak_feature
= 0;
3846 hwbreak_feature
= 0;
3847 vCont_supported
= 0;
3853 /* Wait for events. This will return when all event sources
3854 are removed from the event loop. */
3855 start_event_loop ();
3857 /* If an exit was requested (using the "monitor exit"
3858 command), terminate now. */
3860 throw_quit ("Quit");
3862 /* The only other way to get here is for getpkt to fail:
3864 - If --once was specified, we're done.
3866 - If not in extended-remote mode, and we're no longer
3867 debugging anything, simply exit: GDB has disconnected
3868 after processing the last process exit.
3870 - Otherwise, close the connection and reopen it at the
3872 if (run_once
|| (!extended_protocol
&& !target_running ()))
3873 throw_quit ("Quit");
3876 "Remote side has terminated connection. "
3877 "GDBserver will reopen the connection.\n");
3879 /* Get rid of any pending statuses. An eventual reconnection
3880 (by the same GDB instance or another) will refresh all its
3881 state from scratch. */
3882 discard_queued_stop_replies (minus_one_ptid
);
3883 for_each_inferior (&all_threads
,
3884 clear_pending_status_callback
);
3888 if (disconnected_tracing
)
3890 /* Try to enable non-stop/async mode, so we we can
3891 both wait for an async socket accept, and handle
3892 async target events simultaneously. There's also
3893 no point either in having the target always stop
3894 all threads, when we're going to pass signals
3895 down without informing GDB. */
3898 if (start_non_stop (1))
3901 /* Detaching implicitly resumes all threads;
3902 simply disconnecting does not. */
3908 "Disconnected tracing disabled; "
3909 "stopping trace run.\n");
3914 CATCH (exception
, RETURN_MASK_ERROR
)
3917 fprintf (stderr
, "gdbserver: %s\n", exception
.message
);
3919 if (response_needed
)
3921 write_enn (own_buf
);
3926 throw_quit ("Quit");
3932 /* Main function. */
3935 main (int argc
, char *argv
[])
3940 captured_main (argc
, argv
);
3942 CATCH (exception
, RETURN_MASK_ALL
)
3944 if (exception
.reason
== RETURN_ERROR
)
3947 fprintf (stderr
, "%s\n", exception
.message
);
3948 fprintf (stderr
, "Exiting\n");
3956 gdb_assert_not_reached ("captured_main should never return");
3959 /* Process options coming from Z packets for a breakpoint. PACKET is
3960 the packet buffer. *PACKET is updated to point to the first char
3961 after the last processed option. */
3964 process_point_options (struct gdb_breakpoint
*bp
, char **packet
)
3966 char *dataptr
= *packet
;
3969 /* Check if data has the correct format. */
3970 if (*dataptr
!= ';')
3977 if (*dataptr
== ';')
3980 if (*dataptr
== 'X')
3982 /* Conditional expression. */
3984 debug_printf ("Found breakpoint condition.\n");
3985 if (!add_breakpoint_condition (bp
, &dataptr
))
3986 dataptr
= strchrnul (dataptr
, ';');
3988 else if (startswith (dataptr
, "cmds:"))
3990 dataptr
+= strlen ("cmds:");
3992 debug_printf ("Found breakpoint commands %s.\n", dataptr
);
3993 persist
= (*dataptr
== '1');
3995 if (add_breakpoint_commands (bp
, &dataptr
, persist
))
3996 dataptr
= strchrnul (dataptr
, ';');
4000 fprintf (stderr
, "Unknown token %c, ignoring.\n",
4002 /* Skip tokens until we find one that we recognize. */
4003 dataptr
= strchrnul (dataptr
, ';');
4009 /* Event loop callback that handles a serial event. The first byte in
4010 the serial buffer gets us here. We expect characters to arrive at
4011 a brisk pace, so we read the rest of the packet with a blocking
4015 process_serial_event (void)
4026 int new_packet_len
= -1;
4028 disable_async_io ();
4030 response_needed
= 0;
4031 packet_len
= getpkt (own_buf
);
4032 if (packet_len
<= 0)
4035 /* Force an event loop break. */
4038 response_needed
= 1;
4045 handle_query (own_buf
, packet_len
, &new_packet_len
);
4048 handle_general_set (own_buf
);
4051 require_running (own_buf
);
4056 pid
= strtol (&own_buf
[i
], NULL
, 16);
4059 pid
= ptid_get_pid (current_ptid
);
4061 if ((tracing
&& disconnected_tracing
) || any_persistent_commands ())
4063 struct process_info
*process
= find_process_pid (pid
);
4065 if (process
== NULL
)
4067 write_enn (own_buf
);
4071 if (tracing
&& disconnected_tracing
)
4073 "Disconnected tracing in effect, "
4074 "leaving gdbserver attached to the process\n");
4076 if (any_persistent_commands ())
4078 "Persistent commands are present, "
4079 "leaving gdbserver attached to the process\n");
4081 /* Make sure we're in non-stop/async mode, so we we can both
4082 wait for an async socket accept, and handle async target
4083 events simultaneously. There's also no point either in
4084 having the target stop all threads, when we're going to
4085 pass signals down without informing GDB. */
4089 debug_printf ("Forcing non-stop mode\n");
4095 process
->gdb_detached
= 1;
4097 /* Detaching implicitly resumes all threads. */
4098 target_continue_no_signal (minus_one_ptid
);
4101 break; /* from switch/case */
4104 fprintf (stderr
, "Detaching from process %d\n", pid
);
4106 if (detach_inferior (pid
) != 0)
4107 write_enn (own_buf
);
4110 discard_queued_stop_replies (pid_to_ptid (pid
));
4113 if (extended_protocol
|| target_running ())
4115 /* There is still at least one inferior remaining or
4116 we are in extended mode, so don't terminate gdbserver,
4117 and instead treat this like a normal program exit. */
4118 last_status
.kind
= TARGET_WAITKIND_EXITED
;
4119 last_status
.value
.integer
= 0;
4120 last_ptid
= pid_to_ptid (pid
);
4122 current_thread
= NULL
;
4129 /* If we are attached, then we can exit. Otherwise, we
4130 need to hang around doing nothing, until the child is
4132 join_inferior (pid
);
4138 extended_protocol
= 1;
4142 handle_status (own_buf
);
4145 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
4147 ptid_t gdb_id
, thread_id
;
4150 require_running (own_buf
);
4152 gdb_id
= read_ptid (&own_buf
[2], NULL
);
4154 pid
= ptid_get_pid (gdb_id
);
4156 if (ptid_equal (gdb_id
, null_ptid
)
4157 || ptid_equal (gdb_id
, minus_one_ptid
))
4158 thread_id
= null_ptid
;
4160 && ptid_equal (pid_to_ptid (pid
),
4163 struct thread_info
*thread
=
4164 (struct thread_info
*) find_inferior (&all_threads
,
4169 write_enn (own_buf
);
4173 thread_id
= thread
->entry
.id
;
4177 thread_id
= gdb_id_to_thread_id (gdb_id
);
4178 if (ptid_equal (thread_id
, null_ptid
))
4180 write_enn (own_buf
);
4185 if (own_buf
[1] == 'g')
4187 if (ptid_equal (thread_id
, null_ptid
))
4189 /* GDB is telling us to choose any thread. Check if
4190 the currently selected thread is still valid. If
4191 it is not, select the first available. */
4192 struct thread_info
*thread
=
4193 (struct thread_info
*) find_inferior_id (&all_threads
,
4196 thread
= get_first_thread ();
4197 thread_id
= thread
->entry
.id
;
4200 general_thread
= thread_id
;
4201 set_desired_thread (1);
4202 gdb_assert (current_thread
!= NULL
);
4204 else if (own_buf
[1] == 'c')
4205 cont_thread
= thread_id
;
4211 /* Silently ignore it so that gdb can extend the protocol
4212 without compatibility headaches. */
4217 require_running (own_buf
);
4218 if (current_traceframe
>= 0)
4220 struct regcache
*regcache
4221 = new_register_cache (current_target_desc ());
4223 if (fetch_traceframe_registers (current_traceframe
,
4225 registers_to_string (regcache
, own_buf
);
4227 write_enn (own_buf
);
4228 free_register_cache (regcache
);
4232 struct regcache
*regcache
;
4234 if (!set_desired_thread (1))
4235 write_enn (own_buf
);
4238 regcache
= get_thread_regcache (current_thread
, 1);
4239 registers_to_string (regcache
, own_buf
);
4244 require_running (own_buf
);
4245 if (current_traceframe
>= 0)
4246 write_enn (own_buf
);
4249 struct regcache
*regcache
;
4251 if (!set_desired_thread (1))
4252 write_enn (own_buf
);
4255 regcache
= get_thread_regcache (current_thread
, 1);
4256 registers_from_string (regcache
, &own_buf
[1]);
4262 require_running (own_buf
);
4263 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
4264 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
4266 write_enn (own_buf
);
4268 bin2hex (mem_buf
, own_buf
, res
);
4271 require_running (own_buf
);
4272 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
4273 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
4276 write_enn (own_buf
);
4279 require_running (own_buf
);
4280 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
4281 &mem_addr
, &len
, &mem_buf
) < 0
4282 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
4283 write_enn (own_buf
);
4288 require_running (own_buf
);
4289 hex2bin (own_buf
+ 1, &sig
, 1);
4290 if (gdb_signal_to_host_p ((enum gdb_signal
) sig
))
4291 signal
= gdb_signal_to_host ((enum gdb_signal
) sig
);
4294 myresume (own_buf
, 0, signal
);
4297 require_running (own_buf
);
4298 hex2bin (own_buf
+ 1, &sig
, 1);
4299 if (gdb_signal_to_host_p ((enum gdb_signal
) sig
))
4300 signal
= gdb_signal_to_host ((enum gdb_signal
) sig
);
4303 myresume (own_buf
, 1, signal
);
4306 require_running (own_buf
);
4308 myresume (own_buf
, 0, signal
);
4311 require_running (own_buf
);
4313 myresume (own_buf
, 1, signal
);
4315 case 'Z': /* insert_ ... */
4317 case 'z': /* remove_ ... */
4322 char type
= own_buf
[1];
4324 const int insert
= ch
== 'Z';
4325 char *p
= &own_buf
[3];
4327 p
= unpack_varlen_hex (p
, &addr
);
4328 kind
= strtol (p
+ 1, &dataptr
, 16);
4332 struct gdb_breakpoint
*bp
;
4334 bp
= set_gdb_breakpoint (type
, addr
, kind
, &res
);
4339 /* GDB may have sent us a list of *point parameters to
4340 be evaluated on the target's side. Read such list
4341 here. If we already have a list of parameters, GDB
4342 is telling us to drop that list and use this one
4344 clear_breakpoint_conditions_and_commands (bp
);
4345 process_point_options (bp
, &dataptr
);
4349 res
= delete_gdb_breakpoint (type
, addr
, kind
);
4357 write_enn (own_buf
);
4361 response_needed
= 0;
4362 if (!target_running ())
4363 /* The packet we received doesn't make sense - but we can't
4364 reply to it, either. */
4367 fprintf (stderr
, "Killing all inferiors\n");
4368 for_each_inferior (&all_processes
, kill_inferior_callback
);
4370 /* When using the extended protocol, we wait with no program
4371 running. The traditional protocol will exit instead. */
4372 if (extended_protocol
)
4374 last_status
.kind
= TARGET_WAITKIND_EXITED
;
4375 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
4383 ptid_t gdb_id
, thread_id
;
4385 require_running (own_buf
);
4387 gdb_id
= read_ptid (&own_buf
[1], NULL
);
4388 thread_id
= gdb_id_to_thread_id (gdb_id
);
4389 if (ptid_equal (thread_id
, null_ptid
))
4391 write_enn (own_buf
);
4395 if (mythread_alive (thread_id
))
4398 write_enn (own_buf
);
4402 response_needed
= 0;
4404 /* Restarting the inferior is only supported in the extended
4406 if (extended_protocol
)
4408 if (target_running ())
4409 for_each_inferior (&all_processes
,
4410 kill_inferior_callback
);
4411 fprintf (stderr
, "GDBserver restarting\n");
4413 /* Wait till we are at 1st instruction in prog. */
4414 if (program_name
!= NULL
)
4416 create_inferior (program_name
, program_args
);
4418 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
4420 /* Stopped at the first instruction of the target
4422 general_thread
= last_ptid
;
4426 /* Something went wrong. */
4427 general_thread
= null_ptid
;
4432 last_status
.kind
= TARGET_WAITKIND_EXITED
;
4433 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
4439 /* It is a request we don't understand. Respond with an
4440 empty packet so that gdb knows that we don't support this
4446 /* Extended (long) request. */
4447 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
4451 /* It is a request we don't understand. Respond with an empty
4452 packet so that gdb knows that we don't support this
4458 if (new_packet_len
!= -1)
4459 putpkt_binary (own_buf
, new_packet_len
);
4463 response_needed
= 0;
4471 /* Event-loop callback for serial events. */
4474 handle_serial_event (int err
, gdb_client_data client_data
)
4477 debug_printf ("handling possible serial event\n");
4479 /* Really handle it. */
4480 if (process_serial_event () < 0)
4483 /* Be sure to not change the selected thread behind GDB's back.
4484 Important in the non-stop mode asynchronous protocol. */
4485 set_desired_thread (1);
4490 /* Push a stop notification on the notification queue. */
4493 push_stop_notification (ptid_t ptid
, struct target_waitstatus
*status
)
4495 struct vstop_notif
*vstop_notif
= XNEW (struct vstop_notif
);
4497 vstop_notif
->status
= *status
;
4498 vstop_notif
->ptid
= ptid
;
4499 /* Push Stop notification. */
4500 notif_push (¬if_stop
, (struct notif_event
*) vstop_notif
);
4503 /* Event-loop callback for target events. */
4506 handle_target_event (int err
, gdb_client_data client_data
)
4509 debug_printf ("handling possible target event\n");
4511 last_ptid
= mywait (minus_one_ptid
, &last_status
,
4514 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
)
4516 if (gdb_connected () && report_no_resumed
)
4517 push_stop_notification (null_ptid
, &last_status
);
4519 else if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
4521 int pid
= ptid_get_pid (last_ptid
);
4522 struct process_info
*process
= find_process_pid (pid
);
4523 int forward_event
= !gdb_connected () || process
->gdb_detached
;
4525 if (last_status
.kind
== TARGET_WAITKIND_EXITED
4526 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
4528 mark_breakpoints_out (process
);
4529 target_mourn_inferior (last_ptid
);
4531 else if (last_status
.kind
== TARGET_WAITKIND_THREAD_EXITED
)
4535 /* We're reporting this thread as stopped. Update its
4536 "want-stopped" state to what the client wants, until it
4537 gets a new resume action. */
4538 current_thread
->last_resume_kind
= resume_stop
;
4539 current_thread
->last_status
= last_status
;
4544 if (!target_running ())
4546 /* The last process exited. We're done. */
4550 if (last_status
.kind
== TARGET_WAITKIND_EXITED
4551 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
4552 || last_status
.kind
== TARGET_WAITKIND_THREAD_EXITED
)
4556 /* A thread stopped with a signal, but gdb isn't
4557 connected to handle it. Pass it down to the
4558 inferior, as if it wasn't being traced. */
4559 enum gdb_signal signal
;
4562 debug_printf ("GDB not connected; forwarding event %d for"
4564 (int) last_status
.kind
,
4565 target_pid_to_str (last_ptid
));
4567 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
4568 signal
= last_status
.value
.sig
;
4570 signal
= GDB_SIGNAL_0
;
4571 target_continue (last_ptid
, signal
);
4575 push_stop_notification (last_ptid
, &last_status
);
4578 /* Be sure to not change the selected thread behind GDB's back.
4579 Important in the non-stop mode asynchronous protocol. */
4580 set_desired_thread (1);
4593 } // namespace selftests
4594 #endif /* GDB_SELF_TEST */