1 /* Main code for remote server for GDB.
2 Copyright (C) 1989-2015 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "gdbthread.h"
33 #include "btrace-common.h"
34 #include "filestuff.h"
35 #include "tracepoint.h"
39 /* The thread set with an `Hc' packet. `Hc' is deprecated in favor of
40 `vCont'. Note the multi-process extensions made `vCont' a
41 requirement, so `Hc pPID.TID' is pretty much undefined. So
42 CONT_THREAD can be null_ptid for no `Hc' thread, minus_one_ptid for
43 resuming all threads of the process (again, `Hc' isn't used for
44 multi-process), or a specific thread ptid_t. */
47 /* The thread set with an `Hg' packet. */
48 ptid_t general_thread
;
52 static int extended_protocol
;
53 static int response_needed
;
54 static int exit_requested
;
56 /* --once: Exit after the first connection has closed. */
60 int report_fork_events
;
61 int report_vfork_events
;
66 /* Whether we should attempt to disable the operating system's address
67 space randomization feature before starting an inferior. */
68 int disable_randomization
= 1;
70 static char **program_argv
, **wrapper_argv
;
72 int pass_signals
[GDB_SIGNAL_LAST
];
73 int program_signals
[GDB_SIGNAL_LAST
];
74 int program_signals_p
;
76 /* The PID of the originally created or attached inferior. Used to
77 send signals to the process when GDB sends us an asynchronous interrupt
78 (user hitting Control-C in the client), and to wait for the child to exit
79 when no longer debugging it. */
81 unsigned long signal_pid
;
84 /* A file descriptor for the controlling terminal. */
87 /* TERMINAL_FD's original foreground group. */
88 pid_t old_foreground_pgrp
;
90 /* Hand back terminal ownership to the original foreground group. */
93 restore_old_foreground_pgrp (void)
95 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
99 /* Set if you want to disable optional thread related packets support
100 in gdbserver, for the sake of testing GDB against stubs that don't
102 int disable_packet_vCont
;
103 int disable_packet_Tthread
;
104 int disable_packet_qC
;
105 int disable_packet_qfThreadInfo
;
107 /* Last status reported to GDB. */
108 static struct target_waitstatus last_status
;
109 static ptid_t last_ptid
;
111 static char *own_buf
;
112 static unsigned char *mem_buf
;
114 /* A sub-class of 'struct notif_event' for stop, holding information
115 relative to a single stop reply. We keep a queue of these to
116 push to GDB in non-stop mode. */
120 struct notif_event base
;
122 /* Thread or process that got the event. */
126 struct target_waitstatus status
;
129 /* The current btrace configuration. This is gdbserver's mirror of GDB's
130 btrace configuration. */
131 static struct btrace_config current_btrace_conf
;
133 DEFINE_QUEUE_P (notif_event_p
);
135 /* Put a stop reply to the stop reply queue. */
138 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
140 struct vstop_notif
*new_notif
= xmalloc (sizeof (*new_notif
));
142 new_notif
->ptid
= ptid
;
143 new_notif
->status
= *status
;
145 notif_event_enque (¬if_stop
, (struct notif_event
*) new_notif
);
149 remove_all_on_match_pid (QUEUE (notif_event_p
) *q
,
150 QUEUE_ITER (notif_event_p
) *iter
,
151 struct notif_event
*event
,
157 || ptid_get_pid (((struct vstop_notif
*) event
)->ptid
) == *pid
)
159 if (q
->free_func
!= NULL
)
160 q
->free_func (event
);
162 QUEUE_remove_elem (notif_event_p
, q
, iter
);
168 /* Get rid of the currently pending stop replies for PID. If PID is
169 -1, then apply to all processes. */
172 discard_queued_stop_replies (int pid
)
174 QUEUE_iterate (notif_event_p
, notif_stop
.queue
,
175 remove_all_on_match_pid
, &pid
);
179 vstop_notif_reply (struct notif_event
*event
, char *own_buf
)
181 struct vstop_notif
*vstop
= (struct vstop_notif
*) event
;
183 prepare_resume_reply (own_buf
, vstop
->ptid
, &vstop
->status
);
186 struct notif_server notif_stop
=
188 "vStopped", "Stop", NULL
, vstop_notif_reply
,
192 target_running (void)
194 return get_first_thread () != NULL
;
198 start_inferior (char **argv
)
200 char **new_argv
= argv
;
202 if (wrapper_argv
!= NULL
)
206 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
208 for (i
= 0; argv
[i
] != NULL
; i
++)
210 new_argv
= alloca (sizeof (char *) * count
);
212 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
213 new_argv
[count
++] = wrapper_argv
[i
];
214 for (i
= 0; argv
[i
] != NULL
; i
++)
215 new_argv
[count
++] = argv
[i
];
216 new_argv
[count
] = NULL
;
222 for (i
= 0; new_argv
[i
]; ++i
)
223 debug_printf ("new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
228 signal (SIGTTOU
, SIG_DFL
);
229 signal (SIGTTIN
, SIG_DFL
);
232 signal_pid
= create_inferior (new_argv
[0], new_argv
);
234 /* FIXME: we don't actually know at this point that the create
235 actually succeeded. We won't know that until we wait. */
236 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
241 signal (SIGTTOU
, SIG_IGN
);
242 signal (SIGTTIN
, SIG_IGN
);
243 terminal_fd
= fileno (stderr
);
244 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
245 tcsetpgrp (terminal_fd
, signal_pid
);
246 atexit (restore_old_foreground_pgrp
);
249 if (wrapper_argv
!= NULL
)
251 struct thread_resume resume_info
;
253 memset (&resume_info
, 0, sizeof (resume_info
));
254 resume_info
.thread
= pid_to_ptid (signal_pid
);
255 resume_info
.kind
= resume_continue
;
258 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
260 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
264 (*the_target
->resume
) (&resume_info
, 1);
266 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
267 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
270 current_thread
->last_resume_kind
= resume_stop
;
271 current_thread
->last_status
= last_status
;
273 while (last_status
.value
.sig
!= GDB_SIGNAL_TRAP
);
278 /* Wait till we are at 1st instruction in program, return new pid
279 (assuming success). */
280 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
282 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
283 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
285 current_thread
->last_resume_kind
= resume_stop
;
286 current_thread
->last_status
= last_status
;
289 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
295 attach_inferior (int pid
)
297 /* myattach should return -1 if attaching is unsupported,
298 0 if it succeeded, and call error() otherwise. */
300 if (myattach (pid
) != 0)
303 fprintf (stderr
, "Attached; pid = %d\n", pid
);
306 /* FIXME - It may be that we should get the SIGNAL_PID from the
307 attach function, so that it can be the main thread instead of
308 whichever we were told to attach to. */
313 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
315 /* GDB knows to ignore the first SIGSTOP after attaching to a running
316 process using the "attach" command, but this is different; it's
317 just using "target remote". Pretend it's just starting up. */
318 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
319 && last_status
.value
.sig
== GDB_SIGNAL_STOP
)
320 last_status
.value
.sig
= GDB_SIGNAL_TRAP
;
322 current_thread
->last_resume_kind
= resume_stop
;
323 current_thread
->last_status
= last_status
;
329 extern int remote_debug
;
331 /* Decode a qXfer read request. Return 0 if everything looks OK,
335 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
337 /* After the read marker and annex, qXfer looks like a
338 traditional 'm' packet. */
339 decode_m_packet (buf
, ofs
, len
);
345 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
347 /* Extract and NUL-terminate the object. */
349 while (*buf
&& *buf
!= ':')
355 /* Extract and NUL-terminate the read/write action. */
357 while (*buf
&& *buf
!= ':')
363 /* Extract and NUL-terminate the annex. */
365 while (*buf
&& *buf
!= ':')
375 /* Write the response to a successful qXfer read. Returns the
376 length of the (binary) data stored in BUF, corresponding
377 to as much of DATA/LEN as we could fit. IS_MORE controls
378 the first character of the response. */
380 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
389 return remote_escape_output (data
, len
, 1, (unsigned char *) buf
+ 1,
390 &out_len
, PBUFSIZ
- 2) + 1;
393 /* Handle btrace enabling in BTS format. */
396 handle_btrace_enable_bts (struct thread_info
*thread
)
398 if (thread
->btrace
!= NULL
)
399 return "E.Btrace already enabled.";
401 current_btrace_conf
.format
= BTRACE_FORMAT_BTS
;
402 thread
->btrace
= target_enable_btrace (thread
->entry
.id
,
403 ¤t_btrace_conf
);
404 if (thread
->btrace
== NULL
)
405 return "E.Could not enable btrace.";
410 /* Handle btrace enabling in Intel(R) Processor Trace format. */
413 handle_btrace_enable_pt (struct thread_info
*thread
)
415 if (thread
->btrace
!= NULL
)
416 return "E.Btrace already enabled.";
418 current_btrace_conf
.format
= BTRACE_FORMAT_PT
;
419 thread
->btrace
= target_enable_btrace (thread
->entry
.id
,
420 ¤t_btrace_conf
);
421 if (thread
->btrace
== NULL
)
422 return "E.Could not enable btrace.";
427 /* Handle btrace disabling. */
430 handle_btrace_disable (struct thread_info
*thread
)
433 if (thread
->btrace
== NULL
)
434 return "E.Branch tracing not enabled.";
436 if (target_disable_btrace (thread
->btrace
) != 0)
437 return "E.Could not disable branch tracing.";
439 thread
->btrace
= NULL
;
443 /* Handle the "Qbtrace" packet. */
446 handle_btrace_general_set (char *own_buf
)
448 struct thread_info
*thread
;
452 if (!startswith (own_buf
, "Qbtrace:"))
455 op
= own_buf
+ strlen ("Qbtrace:");
457 if (ptid_equal (general_thread
, null_ptid
)
458 || ptid_equal (general_thread
, minus_one_ptid
))
460 strcpy (own_buf
, "E.Must select a single thread.");
464 thread
= find_thread_ptid (general_thread
);
467 strcpy (own_buf
, "E.No such thread.");
473 if (strcmp (op
, "bts") == 0)
474 err
= handle_btrace_enable_bts (thread
);
475 else if (strcmp (op
, "pt") == 0)
476 err
= handle_btrace_enable_pt (thread
);
477 else if (strcmp (op
, "off") == 0)
478 err
= handle_btrace_disable (thread
);
480 err
= "E.Bad Qbtrace operation. Use bts, pt, or off.";
483 strcpy (own_buf
, err
);
490 /* Handle the "Qbtrace-conf" packet. */
493 handle_btrace_conf_general_set (char *own_buf
)
495 struct thread_info
*thread
;
498 if (!startswith (own_buf
, "Qbtrace-conf:"))
501 op
= own_buf
+ strlen ("Qbtrace-conf:");
503 if (ptid_equal (general_thread
, null_ptid
)
504 || ptid_equal (general_thread
, minus_one_ptid
))
506 strcpy (own_buf
, "E.Must select a single thread.");
510 thread
= find_thread_ptid (general_thread
);
513 strcpy (own_buf
, "E.No such thread.");
517 if (startswith (op
, "bts:size="))
523 size
= strtoul (op
+ strlen ("bts:size="), &endp
, 16);
524 if (endp
== NULL
|| *endp
!= 0 || errno
!= 0 || size
> UINT_MAX
)
526 strcpy (own_buf
, "E.Bad size value.");
530 current_btrace_conf
.bts
.size
= (unsigned int) size
;
532 else if (strncmp (op
, "pt:size=", strlen ("pt:size=")) == 0)
538 size
= strtoul (op
+ strlen ("pt:size="), &endp
, 16);
539 if (endp
== NULL
|| *endp
!= 0 || errno
!= 0 || size
> UINT_MAX
)
541 strcpy (own_buf
, "E.Bad size value.");
545 current_btrace_conf
.pt
.size
= (unsigned int) size
;
549 strcpy (own_buf
, "E.Bad Qbtrace configuration option.");
557 /* Handle all of the extended 'Q' packets. */
560 handle_general_set (char *own_buf
)
562 if (startswith (own_buf
, "QPassSignals:"))
564 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
565 const char *p
= own_buf
+ strlen ("QPassSignals:");
568 p
= decode_address_to_semicolon (&cursig
, p
);
569 for (i
= 0; i
< numsigs
; i
++)
575 /* Keep looping, to clear the remaining signals. */
578 p
= decode_address_to_semicolon (&cursig
, p
);
583 strcpy (own_buf
, "OK");
587 if (startswith (own_buf
, "QProgramSignals:"))
589 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
590 const char *p
= own_buf
+ strlen ("QProgramSignals:");
593 program_signals_p
= 1;
595 p
= decode_address_to_semicolon (&cursig
, p
);
596 for (i
= 0; i
< numsigs
; i
++)
600 program_signals
[i
] = 1;
602 /* Keep looping, to clear the remaining signals. */
605 p
= decode_address_to_semicolon (&cursig
, p
);
608 program_signals
[i
] = 0;
610 strcpy (own_buf
, "OK");
614 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
618 fprintf (stderr
, "[noack mode enabled]\n");
627 if (startswith (own_buf
, "QNonStop:"))
629 char *mode
= own_buf
+ 9;
633 if (strcmp (mode
, "0") == 0)
635 else if (strcmp (mode
, "1") == 0)
639 /* We don't know what this mode is, so complain to
641 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
647 req_str
= req
? "non-stop" : "all-stop";
648 if (start_non_stop (req
) != 0)
650 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
658 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
664 if (startswith (own_buf
, "QDisableRandomization:"))
666 char *packet
= own_buf
+ strlen ("QDisableRandomization:");
669 unpack_varlen_hex (packet
, &setting
);
670 disable_randomization
= setting
;
674 if (disable_randomization
)
675 fprintf (stderr
, "[address space randomization disabled]\n");
677 fprintf (stderr
, "[address space randomization enabled]\n");
684 if (target_supports_tracepoints ()
685 && handle_tracepoint_general_set (own_buf
))
688 if (startswith (own_buf
, "QAgent:"))
690 char *mode
= own_buf
+ strlen ("QAgent:");
693 if (strcmp (mode
, "0") == 0)
695 else if (strcmp (mode
, "1") == 0)
699 /* We don't know what this value is, so complain to GDB. */
700 sprintf (own_buf
, "E.Unknown QAgent value");
704 /* Update the flag. */
707 fprintf (stderr
, "[%s agent]\n", req
? "Enable" : "Disable");
712 if (handle_btrace_general_set (own_buf
))
715 if (handle_btrace_conf_general_set (own_buf
))
718 /* Otherwise we didn't know what packet it was. Say we didn't
724 get_features_xml (const char *annex
)
726 const struct target_desc
*desc
= current_target_desc ();
728 /* `desc->xmltarget' defines what to return when looking for the
729 "target.xml" file. Its contents can either be verbatim XML code
730 (prefixed with a '@') or else the name of the actual XML file to
731 be used in place of "target.xml".
733 This variable is set up from the auto-generated
734 init_registers_... routine for the current target. */
736 if (desc
->xmltarget
!= NULL
&& strcmp (annex
, "target.xml") == 0)
738 if (*desc
->xmltarget
== '@')
739 return desc
->xmltarget
+ 1;
741 annex
= desc
->xmltarget
;
746 extern const char *const xml_builtin
[][2];
749 /* Look for the annex. */
750 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
751 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
754 if (xml_builtin
[i
][0] != NULL
)
755 return xml_builtin
[i
][1];
763 monitor_show_help (void)
765 monitor_output ("The following monitor commands are supported:\n");
766 monitor_output (" set debug <0|1>\n");
767 monitor_output (" Enable general debugging messages\n");
768 monitor_output (" set debug-hw-points <0|1>\n");
769 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
770 monitor_output (" set remote-debug <0|1>\n");
771 monitor_output (" Enable remote protocol debugging messages\n");
772 monitor_output (" set debug-format option1[,option2,...]\n");
773 monitor_output (" Add additional information to debugging messages\n");
774 monitor_output (" Options: all, none");
775 monitor_output (", timestamp");
776 monitor_output ("\n");
777 monitor_output (" exit\n");
778 monitor_output (" Quit GDBserver\n");
781 /* Read trace frame or inferior memory. Returns the number of bytes
782 actually read, zero when no further transfer is possible, and -1 on
783 error. Return of a positive value smaller than LEN does not
784 indicate there's no more to be read, only the end of the transfer.
785 E.g., when GDB reads memory from a traceframe, a first request may
786 be served from a memory block that does not cover the whole request
787 length. A following request gets the rest served from either
788 another block (of the same traceframe) or from the read-only
792 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
796 if (current_traceframe
>= 0)
799 ULONGEST length
= len
;
801 if (traceframe_read_mem (current_traceframe
,
802 memaddr
, myaddr
, len
, &nbytes
))
804 /* Data read from trace buffer, we're done. */
807 if (!in_readonly_region (memaddr
, length
))
809 /* Otherwise we have a valid readonly case, fall through. */
810 /* (assume no half-trace half-real blocks for now) */
813 res
= prepare_to_access_memory ();
816 res
= read_inferior_memory (memaddr
, myaddr
, len
);
817 done_accessing_memory ();
819 return res
== 0 ? len
: -1;
825 /* Write trace frame or inferior memory. Actually, writing to trace
826 frames is forbidden. */
829 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
831 if (current_traceframe
>= 0)
837 ret
= prepare_to_access_memory ();
840 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
841 done_accessing_memory ();
847 /* Subroutine of handle_search_memory to simplify it. */
850 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
851 gdb_byte
*pattern
, unsigned pattern_len
,
852 gdb_byte
*search_buf
,
853 unsigned chunk_size
, unsigned search_buf_size
,
854 CORE_ADDR
*found_addrp
)
856 /* Prime the search buffer. */
858 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
861 warning ("Unable to access %ld bytes of target "
862 "memory at 0x%lx, halting search.",
863 (long) search_buf_size
, (long) start_addr
);
867 /* Perform the search.
869 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
870 When we've scanned N bytes we copy the trailing bytes to the start and
871 read in another N bytes. */
873 while (search_space_len
>= pattern_len
)
876 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
880 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
882 if (found_ptr
!= NULL
)
884 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
885 *found_addrp
= found_addr
;
889 /* Not found in this chunk, skip to next chunk. */
891 /* Don't let search_space_len wrap here, it's unsigned. */
892 if (search_space_len
>= chunk_size
)
893 search_space_len
-= chunk_size
;
895 search_space_len
= 0;
897 if (search_space_len
>= pattern_len
)
899 unsigned keep_len
= search_buf_size
- chunk_size
;
900 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
903 /* Copy the trailing part of the previous iteration to the front
904 of the buffer for the next iteration. */
905 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
907 nr_to_read
= (search_space_len
- keep_len
< chunk_size
908 ? search_space_len
- keep_len
911 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
912 nr_to_read
) != search_buf_size
)
914 warning ("Unable to access %ld bytes of target memory "
915 "at 0x%lx, halting search.",
916 (long) nr_to_read
, (long) read_addr
);
920 start_addr
+= chunk_size
;
929 /* Handle qSearch:memory packets. */
932 handle_search_memory (char *own_buf
, int packet_len
)
934 CORE_ADDR start_addr
;
935 CORE_ADDR search_space_len
;
937 unsigned int pattern_len
;
938 /* NOTE: also defined in find.c testcase. */
939 #define SEARCH_CHUNK_SIZE 16000
940 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
941 /* Buffer to hold memory contents for searching. */
942 gdb_byte
*search_buf
;
943 unsigned search_buf_size
;
945 CORE_ADDR found_addr
;
946 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
948 pattern
= malloc (packet_len
);
951 error ("Unable to allocate memory to perform the search");
952 strcpy (own_buf
, "E00");
955 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
956 packet_len
- cmd_name_len
,
957 &start_addr
, &search_space_len
,
958 pattern
, &pattern_len
) < 0)
961 error ("Error in parsing qSearch:memory packet");
962 strcpy (own_buf
, "E00");
966 search_buf_size
= chunk_size
+ pattern_len
- 1;
968 /* No point in trying to allocate a buffer larger than the search space. */
969 if (search_space_len
< search_buf_size
)
970 search_buf_size
= search_space_len
;
972 search_buf
= malloc (search_buf_size
);
973 if (search_buf
== NULL
)
976 error ("Unable to allocate memory to perform the search");
977 strcpy (own_buf
, "E00");
981 found
= handle_search_memory_1 (start_addr
, search_space_len
,
982 pattern
, pattern_len
,
983 search_buf
, chunk_size
, search_buf_size
,
987 sprintf (own_buf
, "1,%lx", (long) found_addr
);
989 strcpy (own_buf
, "0");
991 strcpy (own_buf
, "E00");
997 #define require_running(BUF) \
998 if (!target_running ()) \
1004 /* Parse options to --debug-format= and "monitor set debug-format".
1005 ARG is the text after "--debug-format=" or "monitor set debug-format".
1006 IS_MONITOR is non-zero if we're invoked via "monitor set debug-format".
1007 This triggers calls to monitor_output.
1008 The result is NULL if all options were parsed ok, otherwise an error
1009 message which the caller must free.
1011 N.B. These commands affect all debug format settings, they are not
1012 cumulative. If a format is not specified, it is turned off.
1013 However, we don't go to extra trouble with things like
1014 "monitor set debug-format all,none,timestamp".
1015 Instead we just parse them one at a time, in order.
1017 The syntax for "monitor set debug" we support here is not identical
1018 to gdb's "set debug foo on|off" because we also use this function to
1019 parse "--debug-format=foo,bar". */
1022 parse_debug_format_options (const char *arg
, int is_monitor
)
1024 VEC (char_ptr
) *options
;
1028 /* First turn all debug format options off. */
1029 debug_timestamp
= 0;
1031 /* First remove leading spaces, for "monitor set debug-format". */
1032 while (isspace (*arg
))
1035 options
= delim_string_to_char_ptr_vec (arg
, ',');
1037 for (ix
= 0; VEC_iterate (char_ptr
, options
, ix
, option
); ++ix
)
1039 if (strcmp (option
, "all") == 0)
1041 debug_timestamp
= 1;
1043 monitor_output ("All extra debug format options enabled.\n");
1045 else if (strcmp (option
, "none") == 0)
1047 debug_timestamp
= 0;
1049 monitor_output ("All extra debug format options disabled.\n");
1051 else if (strcmp (option
, "timestamp") == 0)
1053 debug_timestamp
= 1;
1055 monitor_output ("Timestamps will be added to debug output.\n");
1057 else if (*option
== '\0')
1059 /* An empty option, e.g., "--debug-format=foo,,bar", is ignored. */
1064 char *msg
= xstrprintf ("Unknown debug-format argument: \"%s\"\n",
1067 free_char_ptr_vec (options
);
1072 free_char_ptr_vec (options
);
1076 /* Handle monitor commands not handled by target-specific handlers. */
1079 handle_monitor_command (char *mon
, char *own_buf
)
1081 if (strcmp (mon
, "set debug 1") == 0)
1084 monitor_output ("Debug output enabled.\n");
1086 else if (strcmp (mon
, "set debug 0") == 0)
1089 monitor_output ("Debug output disabled.\n");
1091 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
1093 show_debug_regs
= 1;
1094 monitor_output ("H/W point debugging output enabled.\n");
1096 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
1098 show_debug_regs
= 0;
1099 monitor_output ("H/W point debugging output disabled.\n");
1101 else if (strcmp (mon
, "set remote-debug 1") == 0)
1104 monitor_output ("Protocol debug output enabled.\n");
1106 else if (strcmp (mon
, "set remote-debug 0") == 0)
1109 monitor_output ("Protocol debug output disabled.\n");
1111 else if (startswith (mon
, "set debug-format "))
1114 = parse_debug_format_options (mon
+ sizeof ("set debug-format ") - 1,
1117 if (error_msg
!= NULL
)
1119 monitor_output (error_msg
);
1120 monitor_show_help ();
1121 write_enn (own_buf
);
1125 else if (strcmp (mon
, "help") == 0)
1126 monitor_show_help ();
1127 else if (strcmp (mon
, "exit") == 0)
1131 monitor_output ("Unknown monitor command.\n\n");
1132 monitor_show_help ();
1133 write_enn (own_buf
);
1137 /* Associates a callback with each supported qXfer'able object. */
1141 /* The object this handler handles. */
1144 /* Request that the target transfer up to LEN 8-bit bytes of the
1145 target's OBJECT. The OFFSET, for a seekable object, specifies
1146 the starting point. The ANNEX can be used to provide additional
1147 data-specific information to the target.
1149 Return the number of bytes actually transfered, zero when no
1150 further transfer is possible, -1 on error, -2 when the transfer
1151 is not supported, and -3 on a verbose error message that should
1152 be preserved. Return of a positive value smaller than LEN does
1153 not indicate the end of the object, only the end of the transfer.
1155 One, and only one, of readbuf or writebuf must be non-NULL. */
1156 int (*xfer
) (const char *annex
,
1157 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1158 ULONGEST offset
, LONGEST len
);
1161 /* Handle qXfer:auxv:read. */
1164 handle_qxfer_auxv (const char *annex
,
1165 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1166 ULONGEST offset
, LONGEST len
)
1168 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
1171 if (annex
[0] != '\0' || !target_running ())
1174 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
1177 /* Handle qXfer:exec-file:read. */
1180 handle_qxfer_exec_file (const char *const_annex
,
1181 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1182 ULONGEST offset
, LONGEST len
)
1188 if (the_target
->pid_to_exec_file
== NULL
|| writebuf
!= NULL
)
1191 if (const_annex
[0] == '\0')
1193 if (current_thread
== NULL
)
1196 pid
= pid_of (current_thread
);
1200 char *annex
= alloca (strlen (const_annex
) + 1);
1202 strcpy (annex
, const_annex
);
1203 annex
= unpack_varlen_hex (annex
, &pid
);
1205 if (annex
[0] != '\0')
1212 file
= (*the_target
->pid_to_exec_file
) (pid
);
1216 total_len
= strlen (file
);
1218 if (offset
> total_len
)
1221 if (offset
+ len
> total_len
)
1222 len
= total_len
- offset
;
1224 memcpy (readbuf
, file
+ offset
, len
);
1228 /* Handle qXfer:features:read. */
1231 handle_qxfer_features (const char *annex
,
1232 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1233 ULONGEST offset
, LONGEST len
)
1235 const char *document
;
1238 if (writebuf
!= NULL
)
1241 if (!target_running ())
1244 /* Grab the correct annex. */
1245 document
= get_features_xml (annex
);
1246 if (document
== NULL
)
1249 total_len
= strlen (document
);
1251 if (offset
> total_len
)
1254 if (offset
+ len
> total_len
)
1255 len
= total_len
- offset
;
1257 memcpy (readbuf
, document
+ offset
, len
);
1261 /* Worker routine for handle_qxfer_libraries.
1262 Add to the length pointed to by ARG a conservative estimate of the
1263 length needed to transmit the file name of INF. */
1266 accumulate_file_name_length (struct inferior_list_entry
*inf
, void *arg
)
1268 struct dll_info
*dll
= (struct dll_info
*) inf
;
1269 unsigned int *total_len
= arg
;
1271 /* Over-estimate the necessary memory. Assume that every character
1272 in the library name must be escaped. */
1273 *total_len
+= 128 + 6 * strlen (dll
->name
);
1276 /* Worker routine for handle_qxfer_libraries.
1277 Emit the XML to describe the library in INF. */
1280 emit_dll_description (struct inferior_list_entry
*inf
, void *arg
)
1282 struct dll_info
*dll
= (struct dll_info
*) inf
;
1287 strcpy (p
, " <library name=\"");
1289 name
= xml_escape_text (dll
->name
);
1293 strcpy (p
, "\"><segment address=\"");
1295 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1297 strcpy (p
, "\"/></library>\n");
1303 /* Handle qXfer:libraries:read. */
1306 handle_qxfer_libraries (const char *annex
,
1307 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1308 ULONGEST offset
, LONGEST len
)
1310 unsigned int total_len
;
1313 if (writebuf
!= NULL
)
1316 if (annex
[0] != '\0' || !target_running ())
1320 for_each_inferior_with_data (&all_dlls
, accumulate_file_name_length
,
1323 document
= malloc (total_len
);
1324 if (document
== NULL
)
1327 strcpy (document
, "<library-list version=\"1.0\">\n");
1328 p
= document
+ strlen (document
);
1330 for_each_inferior_with_data (&all_dlls
, emit_dll_description
, &p
);
1332 strcpy (p
, "</library-list>\n");
1334 total_len
= strlen (document
);
1336 if (offset
> total_len
)
1342 if (offset
+ len
> total_len
)
1343 len
= total_len
- offset
;
1345 memcpy (readbuf
, document
+ offset
, len
);
1350 /* Handle qXfer:libraries-svr4:read. */
1353 handle_qxfer_libraries_svr4 (const char *annex
,
1354 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1355 ULONGEST offset
, LONGEST len
)
1357 if (writebuf
!= NULL
)
1360 if (!target_running () || the_target
->qxfer_libraries_svr4
== NULL
)
1363 return the_target
->qxfer_libraries_svr4 (annex
, readbuf
, writebuf
, offset
, len
);
1366 /* Handle qXfer:osadata:read. */
1369 handle_qxfer_osdata (const char *annex
,
1370 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1371 ULONGEST offset
, LONGEST len
)
1373 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
1376 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
1379 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1382 handle_qxfer_siginfo (const char *annex
,
1383 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1384 ULONGEST offset
, LONGEST len
)
1386 if (the_target
->qxfer_siginfo
== NULL
)
1389 if (annex
[0] != '\0' || !target_running ())
1392 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1395 /* Handle qXfer:spu:read and qXfer:spu:write. */
1398 handle_qxfer_spu (const char *annex
,
1399 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1400 ULONGEST offset
, LONGEST len
)
1402 if (the_target
->qxfer_spu
== NULL
)
1405 if (!target_running ())
1408 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1411 /* Handle qXfer:statictrace:read. */
1414 handle_qxfer_statictrace (const char *annex
,
1415 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1416 ULONGEST offset
, LONGEST len
)
1420 if (writebuf
!= NULL
)
1423 if (annex
[0] != '\0' || !target_running () || current_traceframe
== -1)
1426 if (traceframe_read_sdata (current_traceframe
, offset
,
1427 readbuf
, len
, &nbytes
))
1432 /* Helper for handle_qxfer_threads_proper.
1433 Emit the XML to describe the thread of INF. */
1436 handle_qxfer_threads_worker (struct inferior_list_entry
*inf
, void *arg
)
1438 struct thread_info
*thread
= (struct thread_info
*) inf
;
1439 struct buffer
*buffer
= arg
;
1440 ptid_t ptid
= thread_to_gdb_id (thread
);
1442 int core
= target_core_of_thread (ptid
);
1445 write_ptid (ptid_s
, ptid
);
1449 sprintf (core_s
, "%d", core
);
1450 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1455 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1460 /* Helper for handle_qxfer_threads. */
1463 handle_qxfer_threads_proper (struct buffer
*buffer
)
1465 buffer_grow_str (buffer
, "<threads>\n");
1467 for_each_inferior_with_data (&all_threads
, handle_qxfer_threads_worker
,
1470 buffer_grow_str0 (buffer
, "</threads>\n");
1473 /* Handle qXfer:threads:read. */
1476 handle_qxfer_threads (const char *annex
,
1477 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1478 ULONGEST offset
, LONGEST len
)
1480 static char *result
= 0;
1481 static unsigned int result_length
= 0;
1483 if (writebuf
!= NULL
)
1486 if (!target_running () || annex
[0] != '\0')
1491 struct buffer buffer
;
1492 /* When asked for data at offset 0, generate everything and store into
1493 'result'. Successive reads will be served off 'result'. */
1497 buffer_init (&buffer
);
1499 handle_qxfer_threads_proper (&buffer
);
1501 result
= buffer_finish (&buffer
);
1502 result_length
= strlen (result
);
1503 buffer_free (&buffer
);
1506 if (offset
>= result_length
)
1508 /* We're out of data. */
1515 if (len
> result_length
- offset
)
1516 len
= result_length
- offset
;
1518 memcpy (readbuf
, result
+ offset
, len
);
1523 /* Handle qXfer:traceframe-info:read. */
1526 handle_qxfer_traceframe_info (const char *annex
,
1527 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1528 ULONGEST offset
, LONGEST len
)
1530 static char *result
= 0;
1531 static unsigned int result_length
= 0;
1533 if (writebuf
!= NULL
)
1536 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1541 struct buffer buffer
;
1543 /* When asked for data at offset 0, generate everything and
1544 store into 'result'. Successive reads will be served off
1548 buffer_init (&buffer
);
1550 traceframe_read_info (current_traceframe
, &buffer
);
1552 result
= buffer_finish (&buffer
);
1553 result_length
= strlen (result
);
1554 buffer_free (&buffer
);
1557 if (offset
>= result_length
)
1559 /* We're out of data. */
1566 if (len
> result_length
- offset
)
1567 len
= result_length
- offset
;
1569 memcpy (readbuf
, result
+ offset
, len
);
1573 /* Handle qXfer:fdpic:read. */
1576 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1577 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1579 if (the_target
->read_loadmap
== NULL
)
1582 if (!target_running ())
1585 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1588 /* Handle qXfer:btrace:read. */
1591 handle_qxfer_btrace (const char *annex
,
1592 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1593 ULONGEST offset
, LONGEST len
)
1595 static struct buffer cache
;
1596 struct thread_info
*thread
;
1599 if (the_target
->read_btrace
== NULL
|| writebuf
!= NULL
)
1602 if (!target_running ())
1605 if (ptid_equal (general_thread
, null_ptid
)
1606 || ptid_equal (general_thread
, minus_one_ptid
))
1608 strcpy (own_buf
, "E.Must select a single thread.");
1612 thread
= find_thread_ptid (general_thread
);
1615 strcpy (own_buf
, "E.No such thread.");
1619 if (thread
->btrace
== NULL
)
1621 strcpy (own_buf
, "E.Btrace not enabled.");
1625 if (strcmp (annex
, "all") == 0)
1626 type
= BTRACE_READ_ALL
;
1627 else if (strcmp (annex
, "new") == 0)
1628 type
= BTRACE_READ_NEW
;
1629 else if (strcmp (annex
, "delta") == 0)
1630 type
= BTRACE_READ_DELTA
;
1633 strcpy (own_buf
, "E.Bad annex.");
1639 buffer_free (&cache
);
1641 result
= target_read_btrace (thread
->btrace
, &cache
, type
);
1644 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1648 else if (offset
> cache
.used_size
)
1650 buffer_free (&cache
);
1654 if (len
> cache
.used_size
- offset
)
1655 len
= cache
.used_size
- offset
;
1657 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1662 /* Handle qXfer:btrace-conf:read. */
1665 handle_qxfer_btrace_conf (const char *annex
,
1666 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1667 ULONGEST offset
, LONGEST len
)
1669 static struct buffer cache
;
1670 struct thread_info
*thread
;
1673 if (the_target
->read_btrace_conf
== NULL
|| writebuf
!= NULL
)
1676 if (annex
[0] != '\0' || !target_running ())
1679 if (ptid_equal (general_thread
, null_ptid
)
1680 || ptid_equal (general_thread
, minus_one_ptid
))
1682 strcpy (own_buf
, "E.Must select a single thread.");
1686 thread
= find_thread_ptid (general_thread
);
1689 strcpy (own_buf
, "E.No such thread.");
1693 if (thread
->btrace
== NULL
)
1695 strcpy (own_buf
, "E.Btrace not enabled.");
1701 buffer_free (&cache
);
1703 result
= target_read_btrace_conf (thread
->btrace
, &cache
);
1706 memcpy (own_buf
, cache
.buffer
, cache
.used_size
);
1710 else if (offset
> cache
.used_size
)
1712 buffer_free (&cache
);
1716 if (len
> cache
.used_size
- offset
)
1717 len
= cache
.used_size
- offset
;
1719 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1724 static const struct qxfer qxfer_packets
[] =
1726 { "auxv", handle_qxfer_auxv
},
1727 { "btrace", handle_qxfer_btrace
},
1728 { "btrace-conf", handle_qxfer_btrace_conf
},
1729 { "exec-file", handle_qxfer_exec_file
},
1730 { "fdpic", handle_qxfer_fdpic
},
1731 { "features", handle_qxfer_features
},
1732 { "libraries", handle_qxfer_libraries
},
1733 { "libraries-svr4", handle_qxfer_libraries_svr4
},
1734 { "osdata", handle_qxfer_osdata
},
1735 { "siginfo", handle_qxfer_siginfo
},
1736 { "spu", handle_qxfer_spu
},
1737 { "statictrace", handle_qxfer_statictrace
},
1738 { "threads", handle_qxfer_threads
},
1739 { "traceframe-info", handle_qxfer_traceframe_info
},
1743 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1751 if (!startswith (own_buf
, "qXfer:"))
1754 /* Grab the object, r/w and annex. */
1755 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1757 write_enn (own_buf
);
1762 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1765 const struct qxfer
*q
= &qxfer_packets
[i
];
1767 if (strcmp (object
, q
->object
) == 0)
1769 if (strcmp (rw
, "read") == 0)
1771 unsigned char *data
;
1776 /* Grab the offset and length. */
1777 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1779 write_enn (own_buf
);
1783 /* Read one extra byte, as an indicator of whether there is
1785 if (len
> PBUFSIZ
- 2)
1787 data
= malloc (len
+ 1);
1790 write_enn (own_buf
);
1793 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1801 /* Preserve error message. */
1804 write_enn (own_buf
);
1806 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1808 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1813 else if (strcmp (rw
, "write") == 0)
1818 unsigned char *data
;
1820 strcpy (own_buf
, "E00");
1821 data
= malloc (packet_len
- (offset
- own_buf
));
1824 write_enn (own_buf
);
1827 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1828 &ofs
, &len
, data
) < 0)
1831 write_enn (own_buf
);
1835 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1843 /* Preserve error message. */
1846 write_enn (own_buf
);
1848 sprintf (own_buf
, "%x", n
);
1861 /* Table used by the crc32 function to calcuate the checksum. */
1863 static unsigned int crc32_table
[256] =
1866 /* Compute 32 bit CRC from inferior memory.
1868 On success, return 32 bit CRC.
1869 On failure, return (unsigned long long) -1. */
1871 static unsigned long long
1872 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1874 if (!crc32_table
[1])
1876 /* Initialize the CRC table and the decoding table. */
1880 for (i
= 0; i
< 256; i
++)
1882 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1883 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1890 unsigned char byte
= 0;
1892 /* Return failure if memory read fails. */
1893 if (read_inferior_memory (base
, &byte
, 1) != 0)
1894 return (unsigned long long) -1;
1896 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1899 return (unsigned long long) crc
;
1902 /* Add supported btrace packets to BUF. */
1905 supported_btrace_packets (char *buf
)
1907 int btrace_supported
= 0;
1909 if (target_supports_btrace (BTRACE_FORMAT_BTS
))
1911 strcat (buf
, ";Qbtrace:bts+");
1912 strcat (buf
, ";Qbtrace-conf:bts:size+");
1914 btrace_supported
= 1;
1917 if (target_supports_btrace (BTRACE_FORMAT_PT
))
1919 strcat (buf
, ";Qbtrace:pt+");
1920 strcat (buf
, ";Qbtrace-conf:pt:size+");
1922 btrace_supported
= 1;
1925 if (!btrace_supported
)
1928 strcat (buf
, ";Qbtrace:off+");
1929 strcat (buf
, ";qXfer:btrace:read+");
1930 strcat (buf
, ";qXfer:btrace-conf:read+");
1933 /* Handle all of the extended 'q' packets. */
1936 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1938 static struct inferior_list_entry
*thread_ptr
;
1940 /* Reply the current thread id. */
1941 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1944 require_running (own_buf
);
1946 if (!ptid_equal (general_thread
, null_ptid
)
1947 && !ptid_equal (general_thread
, minus_one_ptid
))
1948 gdb_id
= general_thread
;
1951 thread_ptr
= get_first_inferior (&all_threads
);
1952 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1955 sprintf (own_buf
, "QC");
1957 write_ptid (own_buf
, gdb_id
);
1961 if (strcmp ("qSymbol::", own_buf
) == 0)
1963 /* GDB is suggesting new symbols have been loaded. This may
1964 mean a new shared library has been detected as loaded, so
1965 take the opportunity to check if breakpoints we think are
1966 inserted, still are. Note that it isn't guaranteed that
1967 we'll see this when a shared library is loaded, and nor will
1968 we see this for unloads (although breakpoints in unloaded
1969 libraries shouldn't trigger), as GDB may not find symbols for
1970 the library at all. We also re-validate breakpoints when we
1971 see a second GDB breakpoint for the same address, and or when
1972 we access breakpoint shadows. */
1973 validate_breakpoints ();
1975 if (target_supports_tracepoints ())
1976 tracepoint_look_up_symbols ();
1978 if (target_running () && the_target
->look_up_symbols
!= NULL
)
1979 (*the_target
->look_up_symbols
) ();
1981 strcpy (own_buf
, "OK");
1985 if (!disable_packet_qfThreadInfo
)
1987 if (strcmp ("qfThreadInfo", own_buf
) == 0)
1991 require_running (own_buf
);
1992 thread_ptr
= get_first_inferior (&all_threads
);
1995 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1996 write_ptid (own_buf
, gdb_id
);
1997 thread_ptr
= thread_ptr
->next
;
2001 if (strcmp ("qsThreadInfo", own_buf
) == 0)
2005 require_running (own_buf
);
2006 if (thread_ptr
!= NULL
)
2009 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
2010 write_ptid (own_buf
, gdb_id
);
2011 thread_ptr
= thread_ptr
->next
;
2016 sprintf (own_buf
, "l");
2022 if (the_target
->read_offsets
!= NULL
2023 && strcmp ("qOffsets", own_buf
) == 0)
2025 CORE_ADDR text
, data
;
2027 require_running (own_buf
);
2028 if (the_target
->read_offsets (&text
, &data
))
2029 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
2030 (long)text
, (long)data
, (long)data
);
2032 write_enn (own_buf
);
2037 /* Protocol features query. */
2038 if (startswith (own_buf
, "qSupported")
2039 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
2041 char *p
= &own_buf
[10];
2042 int gdb_supports_qRelocInsn
= 0;
2044 /* Start processing qSupported packet. */
2045 target_process_qsupported (NULL
);
2047 /* Process each feature being provided by GDB. The first
2048 feature will follow a ':', and latter features will follow
2052 char **qsupported
= NULL
;
2056 /* Two passes, to avoid nested strtok calls in
2057 target_process_qsupported. */
2058 for (p
= strtok (p
+ 1, ";");
2060 p
= strtok (NULL
, ";"))
2063 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
2064 qsupported
[count
- 1] = xstrdup (p
);
2067 for (i
= 0; i
< count
; i
++)
2070 if (strcmp (p
, "multiprocess+") == 0)
2072 /* GDB supports and wants multi-process support if
2074 if (target_supports_multi_process ())
2077 else if (strcmp (p
, "qRelocInsn+") == 0)
2079 /* GDB supports relocate instruction requests. */
2080 gdb_supports_qRelocInsn
= 1;
2082 else if (strcmp (p
, "swbreak+") == 0)
2084 /* GDB wants us to report whether a trap is caused
2085 by a software breakpoint and for us to handle PC
2086 adjustment if necessary on this target. */
2087 if (target_supports_stopped_by_sw_breakpoint ())
2088 swbreak_feature
= 1;
2090 else if (strcmp (p
, "hwbreak+") == 0)
2092 /* GDB wants us to report whether a trap is caused
2093 by a hardware breakpoint. */
2094 if (target_supports_stopped_by_hw_breakpoint ())
2095 hwbreak_feature
= 1;
2097 else if (strcmp (p
, "fork-events+") == 0)
2099 /* GDB supports and wants fork events if possible. */
2100 if (target_supports_fork_events ())
2101 report_fork_events
= 1;
2103 else if (strcmp (p
, "vfork-events+") == 0)
2105 /* GDB supports and wants vfork events if possible. */
2106 if (target_supports_vfork_events ())
2107 report_vfork_events
= 1;
2110 target_process_qsupported (p
);
2119 "PacketSize=%x;QPassSignals+;QProgramSignals+",
2122 if (the_target
->qxfer_libraries_svr4
!= NULL
)
2123 strcat (own_buf
, ";qXfer:libraries-svr4:read+"
2124 ";augmented-libraries-svr4-read+");
2127 /* We do not have any hook to indicate whether the non-SVR4 target
2128 backend supports qXfer:libraries:read, so always report it. */
2129 strcat (own_buf
, ";qXfer:libraries:read+");
2132 if (the_target
->read_auxv
!= NULL
)
2133 strcat (own_buf
, ";qXfer:auxv:read+");
2135 if (the_target
->qxfer_spu
!= NULL
)
2136 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
2138 if (the_target
->qxfer_siginfo
!= NULL
)
2139 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
2141 if (the_target
->read_loadmap
!= NULL
)
2142 strcat (own_buf
, ";qXfer:fdpic:read+");
2144 /* We always report qXfer:features:read, as targets may
2145 install XML files on a subsequent call to arch_setup.
2146 If we reported to GDB on startup that we don't support
2147 qXfer:feature:read at all, we will never be re-queried. */
2148 strcat (own_buf
, ";qXfer:features:read+");
2150 if (transport_is_reliable
)
2151 strcat (own_buf
, ";QStartNoAckMode+");
2153 if (the_target
->qxfer_osdata
!= NULL
)
2154 strcat (own_buf
, ";qXfer:osdata:read+");
2156 if (target_supports_multi_process ())
2157 strcat (own_buf
, ";multiprocess+");
2159 if (target_supports_fork_events ())
2160 strcat (own_buf
, ";fork-events+");
2162 if (target_supports_vfork_events ())
2163 strcat (own_buf
, ";vfork-events+");
2165 if (target_supports_non_stop ())
2166 strcat (own_buf
, ";QNonStop+");
2168 if (target_supports_disable_randomization ())
2169 strcat (own_buf
, ";QDisableRandomization+");
2171 strcat (own_buf
, ";qXfer:threads:read+");
2173 if (target_supports_tracepoints ())
2175 strcat (own_buf
, ";ConditionalTracepoints+");
2176 strcat (own_buf
, ";TraceStateVariables+");
2177 strcat (own_buf
, ";TracepointSource+");
2178 strcat (own_buf
, ";DisconnectedTracing+");
2179 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
2180 strcat (own_buf
, ";FastTracepoints+");
2181 strcat (own_buf
, ";StaticTracepoints+");
2182 strcat (own_buf
, ";InstallInTrace+");
2183 strcat (own_buf
, ";qXfer:statictrace:read+");
2184 strcat (own_buf
, ";qXfer:traceframe-info:read+");
2185 strcat (own_buf
, ";EnableDisableTracepoints+");
2186 strcat (own_buf
, ";QTBuffer:size+");
2187 strcat (own_buf
, ";tracenz+");
2190 /* Support target-side breakpoint conditions and commands. */
2191 if (target_supports_conditional_breakpoints ())
2192 strcat (own_buf
, ";ConditionalBreakpoints+");
2193 strcat (own_buf
, ";BreakpointCommands+");
2195 if (target_supports_agent ())
2196 strcat (own_buf
, ";QAgent+");
2198 supported_btrace_packets (own_buf
);
2200 if (target_supports_stopped_by_sw_breakpoint ())
2201 strcat (own_buf
, ";swbreak+");
2203 if (target_supports_stopped_by_hw_breakpoint ())
2204 strcat (own_buf
, ";hwbreak+");
2206 if (the_target
->pid_to_exec_file
!= NULL
)
2207 strcat (own_buf
, ";qXfer:exec-file:read+");
2209 /* Reinitialize components as needed for the new connection. */
2210 hostio_handle_new_gdb_connection ();
2211 target_handle_new_gdb_connection ();
2216 /* Thread-local storage support. */
2217 if (the_target
->get_tls_address
!= NULL
2218 && startswith (own_buf
, "qGetTLSAddr:"))
2220 char *p
= own_buf
+ 12;
2221 CORE_ADDR parts
[2], address
= 0;
2223 ptid_t ptid
= null_ptid
;
2225 require_running (own_buf
);
2227 for (i
= 0; i
< 3; i
++)
2235 p2
= strchr (p
, ',');
2248 ptid
= read_ptid (p
, NULL
);
2250 decode_address (&parts
[i
- 1], p
, len
);
2254 if (p
!= NULL
|| i
< 3)
2258 struct thread_info
*thread
= find_thread_ptid (ptid
);
2263 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
2269 strcpy (own_buf
, paddress(address
));
2274 write_enn (own_buf
);
2278 /* Otherwise, pretend we do not understand this packet. */
2281 /* Windows OS Thread Information Block address support. */
2282 if (the_target
->get_tib_address
!= NULL
2283 && startswith (own_buf
, "qGetTIBAddr:"))
2288 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
2290 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
2293 strcpy (own_buf
, paddress(tlb
));
2298 write_enn (own_buf
);
2304 /* Handle "monitor" commands. */
2305 if (startswith (own_buf
, "qRcmd,"))
2307 char *mon
= malloc (PBUFSIZ
);
2308 int len
= strlen (own_buf
+ 6);
2312 write_enn (own_buf
);
2317 || hex2bin (own_buf
+ 6, (gdb_byte
*) mon
, len
/ 2) != len
/ 2)
2319 write_enn (own_buf
);
2323 mon
[len
/ 2] = '\0';
2327 if (the_target
->handle_monitor_command
== NULL
2328 || (*the_target
->handle_monitor_command
) (mon
) == 0)
2329 /* Default processing. */
2330 handle_monitor_command (mon
, own_buf
);
2336 if (startswith (own_buf
, "qSearch:memory:"))
2338 require_running (own_buf
);
2339 handle_search_memory (own_buf
, packet_len
);
2343 if (strcmp (own_buf
, "qAttached") == 0
2344 || startswith (own_buf
, "qAttached:"))
2346 struct process_info
*process
;
2348 if (own_buf
[sizeof ("qAttached") - 1])
2350 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
2351 process
= (struct process_info
*)
2352 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
2356 require_running (own_buf
);
2357 process
= current_process ();
2360 if (process
== NULL
)
2362 write_enn (own_buf
);
2366 strcpy (own_buf
, process
->attached
? "1" : "0");
2370 if (startswith (own_buf
, "qCRC:"))
2372 /* CRC check (compare-section). */
2376 unsigned long long crc
;
2378 require_running (own_buf
);
2379 comma
= unpack_varlen_hex (own_buf
+ 5, &base
);
2380 if (*comma
++ != ',')
2382 write_enn (own_buf
);
2385 len
= strtoul (comma
, NULL
, 16);
2386 crc
= crc32 (base
, len
, 0xffffffff);
2387 /* Check for memory failure. */
2388 if (crc
== (unsigned long long) -1)
2390 write_enn (own_buf
);
2393 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
2397 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
2400 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
2403 /* Otherwise we didn't know what packet it was. Say we didn't
2408 static void gdb_wants_all_threads_stopped (void);
2409 static void resume (struct thread_resume
*actions
, size_t n
);
2411 /* The callback that is passed to visit_actioned_threads. */
2412 typedef int (visit_actioned_threads_callback_ftype
)
2413 (const struct thread_resume
*, struct thread_info
*);
2415 /* Struct to pass data to visit_actioned_threads. */
2417 struct visit_actioned_threads_data
2419 const struct thread_resume
*actions
;
2421 visit_actioned_threads_callback_ftype
*callback
;
2424 /* Call CALLBACK for any thread to which ACTIONS applies to. Returns
2425 true if CALLBACK returns true. Returns false if no matching thread
2426 is found or CALLBACK results false.
2427 Note: This function is itself a callback for find_inferior. */
2430 visit_actioned_threads (struct inferior_list_entry
*entry
, void *datap
)
2432 struct visit_actioned_threads_data
*data
= datap
;
2433 const struct thread_resume
*actions
= data
->actions
;
2434 size_t num_actions
= data
->num_actions
;
2435 visit_actioned_threads_callback_ftype
*callback
= data
->callback
;
2438 for (i
= 0; i
< num_actions
; i
++)
2440 const struct thread_resume
*action
= &actions
[i
];
2442 if (ptid_equal (action
->thread
, minus_one_ptid
)
2443 || ptid_equal (action
->thread
, entry
->id
)
2444 || ((ptid_get_pid (action
->thread
)
2445 == ptid_get_pid (entry
->id
))
2446 && ptid_get_lwp (action
->thread
) == -1))
2448 struct thread_info
*thread
= (struct thread_info
*) entry
;
2450 if ((*callback
) (action
, thread
))
2458 /* Callback for visit_actioned_threads. If the thread has a pending
2459 status to report, report it now. */
2462 handle_pending_status (const struct thread_resume
*resumption
,
2463 struct thread_info
*thread
)
2465 if (thread
->status_pending_p
)
2467 thread
->status_pending_p
= 0;
2469 last_status
= thread
->last_status
;
2470 last_ptid
= thread
->entry
.id
;
2471 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2477 /* Parse vCont packets. */
2479 handle_v_cont (char *own_buf
)
2483 struct thread_resume
*resume_info
;
2484 struct thread_resume default_action
= {{0}};
2486 /* Count the number of semicolons in the packet. There should be one
2487 for every action. */
2493 p
= strchr (p
, ';');
2496 resume_info
= malloc (n
* sizeof (resume_info
[0]));
2497 if (resume_info
== NULL
)
2505 memset (&resume_info
[i
], 0, sizeof resume_info
[i
]);
2507 if (p
[0] == 's' || p
[0] == 'S')
2508 resume_info
[i
].kind
= resume_step
;
2509 else if (p
[0] == 'r')
2510 resume_info
[i
].kind
= resume_step
;
2511 else if (p
[0] == 'c' || p
[0] == 'C')
2512 resume_info
[i
].kind
= resume_continue
;
2513 else if (p
[0] == 't')
2514 resume_info
[i
].kind
= resume_stop
;
2518 if (p
[0] == 'S' || p
[0] == 'C')
2521 sig
= strtol (p
+ 1, &q
, 16);
2526 if (!gdb_signal_to_host_p (sig
))
2528 resume_info
[i
].sig
= gdb_signal_to_host (sig
);
2530 else if (p
[0] == 'r')
2534 p
= unpack_varlen_hex (p
+ 1, &addr
);
2535 resume_info
[i
].step_range_start
= addr
;
2540 p
= unpack_varlen_hex (p
+ 1, &addr
);
2541 resume_info
[i
].step_range_end
= addr
;
2550 resume_info
[i
].thread
= minus_one_ptid
;
2551 default_action
= resume_info
[i
];
2553 /* Note: we don't increment i here, we'll overwrite this entry
2554 the next time through. */
2556 else if (p
[0] == ':')
2558 ptid_t ptid
= read_ptid (p
+ 1, &q
);
2563 if (p
[0] != ';' && p
[0] != 0)
2566 resume_info
[i
].thread
= ptid
;
2573 resume_info
[i
] = default_action
;
2575 set_desired_thread (0);
2577 resume (resume_info
, n
);
2582 write_enn (own_buf
);
2587 /* Resume target with ACTIONS, an array of NUM_ACTIONS elements. */
2590 resume (struct thread_resume
*actions
, size_t num_actions
)
2594 /* Check if among the threads that GDB wants actioned, there's
2595 one with a pending status to report. If so, skip actually
2596 resuming/stopping and report the pending event
2598 struct visit_actioned_threads_data data
;
2600 data
.actions
= actions
;
2601 data
.num_actions
= num_actions
;
2602 data
.callback
= handle_pending_status
;
2603 if (find_inferior (&all_threads
, visit_actioned_threads
, &data
) != NULL
)
2609 (*the_target
->resume
) (actions
, num_actions
);
2615 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2617 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
)
2619 /* No proper RSP support for this yet. At least return
2621 sprintf (own_buf
, "E.No unwaited-for children left.");
2622 disable_async_io ();
2626 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2627 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
2628 && last_status
.kind
!= TARGET_WAITKIND_NO_RESUMED
)
2629 current_thread
->last_status
= last_status
;
2631 /* From the client's perspective, all-stop mode always stops all
2632 threads implicitly (and the target backend has already done
2633 so by now). Tag all threads as "want-stopped", so we don't
2634 resume them implicitly without the client telling us to. */
2635 gdb_wants_all_threads_stopped ();
2636 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2637 disable_async_io ();
2639 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2640 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2641 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2645 /* Attach to a new program. Return 1 if successful, 0 if failure. */
2647 handle_v_attach (char *own_buf
)
2651 pid
= strtol (own_buf
+ 8, NULL
, 16);
2652 if (pid
!= 0 && attach_inferior (pid
) == 0)
2654 /* Don't report shared library events after attaching, even if
2655 some libraries are preloaded. GDB will always poll the
2656 library list. Avoids the "stopped by shared library event"
2657 notice on the GDB side. */
2662 /* In non-stop, we don't send a resume reply. Stop events
2663 will follow up using the normal notification
2668 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2674 write_enn (own_buf
);
2679 /* Run a new program. Return 1 if successful, 0 if failure. */
2681 handle_v_run (char *own_buf
)
2683 char *p
, *next_p
, **new_argv
;
2687 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
2693 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
2694 if (new_argv
== NULL
)
2696 write_enn (own_buf
);
2701 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
2703 next_p
= strchr (p
, ';');
2705 next_p
= p
+ strlen (p
);
2707 if (i
== 0 && p
== next_p
)
2711 /* FIXME: Fail request if out of memory instead of dying. */
2712 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
2713 hex2bin (p
, (gdb_byte
*) new_argv
[i
], (next_p
- p
) / 2);
2714 new_argv
[i
][(next_p
- p
) / 2] = '\0';
2723 if (new_argv
[0] == NULL
)
2725 /* GDB didn't specify a program to run. Use the program from the
2726 last run with the new argument list. */
2728 if (program_argv
== NULL
)
2730 write_enn (own_buf
);
2731 freeargv (new_argv
);
2735 new_argv
[0] = strdup (program_argv
[0]);
2736 if (new_argv
[0] == NULL
)
2738 write_enn (own_buf
);
2739 freeargv (new_argv
);
2744 /* Free the old argv and install the new one. */
2745 freeargv (program_argv
);
2746 program_argv
= new_argv
;
2748 start_inferior (program_argv
);
2749 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
2751 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2753 /* In non-stop, sending a resume reply doesn't set the general
2754 thread, but GDB assumes a vRun sets it (this is so GDB can
2755 query which is the main thread of the new inferior. */
2757 general_thread
= last_ptid
;
2763 write_enn (own_buf
);
2768 /* Kill process. Return 1 if successful, 0 if failure. */
2770 handle_v_kill (char *own_buf
)
2773 char *p
= &own_buf
[6];
2775 pid
= strtol (p
, NULL
, 16);
2778 if (pid
!= 0 && kill_inferior (pid
) == 0)
2780 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2781 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
2782 last_ptid
= pid_to_ptid (pid
);
2783 discard_queued_stop_replies (pid
);
2789 write_enn (own_buf
);
2794 /* Handle all of the extended 'v' packets. */
2796 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2798 if (!disable_packet_vCont
)
2800 if (startswith (own_buf
, "vCont;"))
2802 require_running (own_buf
);
2803 handle_v_cont (own_buf
);
2807 if (startswith (own_buf
, "vCont?"))
2809 strcpy (own_buf
, "vCont;c;C;s;S;t");
2810 if (target_supports_range_stepping ())
2812 own_buf
= own_buf
+ strlen (own_buf
);
2813 strcpy (own_buf
, ";r");
2819 if (startswith (own_buf
, "vFile:")
2820 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2823 if (startswith (own_buf
, "vAttach;"))
2825 if ((!extended_protocol
|| !multi_process
) && target_running ())
2827 fprintf (stderr
, "Already debugging a process\n");
2828 write_enn (own_buf
);
2831 handle_v_attach (own_buf
);
2835 if (startswith (own_buf
, "vRun;"))
2837 if ((!extended_protocol
|| !multi_process
) && target_running ())
2839 fprintf (stderr
, "Already debugging a process\n");
2840 write_enn (own_buf
);
2843 handle_v_run (own_buf
);
2847 if (startswith (own_buf
, "vKill;"))
2849 if (!target_running ())
2851 fprintf (stderr
, "No process to kill\n");
2852 write_enn (own_buf
);
2855 handle_v_kill (own_buf
);
2859 if (handle_notif_ack (own_buf
, packet_len
))
2862 /* Otherwise we didn't know what packet it was. Say we didn't
2868 /* Resume thread and wait for another event. In non-stop mode,
2869 don't really wait here, but return immediatelly to the event
2872 myresume (char *own_buf
, int step
, int sig
)
2874 struct thread_resume resume_info
[2];
2876 int valid_cont_thread
;
2878 set_desired_thread (0);
2880 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2881 && !ptid_equal (cont_thread
, minus_one_ptid
));
2883 if (step
|| sig
|| valid_cont_thread
)
2885 resume_info
[0].thread
= current_ptid
;
2887 resume_info
[0].kind
= resume_step
;
2889 resume_info
[0].kind
= resume_continue
;
2890 resume_info
[0].sig
= sig
;
2894 if (!valid_cont_thread
)
2896 resume_info
[n
].thread
= minus_one_ptid
;
2897 resume_info
[n
].kind
= resume_continue
;
2898 resume_info
[n
].sig
= 0;
2902 resume (resume_info
, n
);
2905 /* Callback for for_each_inferior. Make a new stop reply for each
2909 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2911 struct thread_info
*thread
= (struct thread_info
*) entry
;
2913 /* For now, assume targets that don't have this callback also don't
2914 manage the thread's last_status field. */
2915 if (the_target
->thread_stopped
== NULL
)
2917 struct vstop_notif
*new_notif
= xmalloc (sizeof (*new_notif
));
2919 new_notif
->ptid
= entry
->id
;
2920 new_notif
->status
= thread
->last_status
;
2921 /* Pass the last stop reply back to GDB, but don't notify
2923 notif_event_enque (¬if_stop
,
2924 (struct notif_event
*) new_notif
);
2928 if (thread_stopped (thread
))
2933 = target_waitstatus_to_string (&thread
->last_status
);
2935 debug_printf ("Reporting thread %s as already stopped with %s\n",
2936 target_pid_to_str (entry
->id
),
2939 xfree (status_string
);
2942 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2944 /* Pass the last stop reply back to GDB, but don't notify
2946 queue_stop_reply (entry
->id
, &thread
->last_status
);
2953 /* Set this inferior threads's state as "want-stopped". We won't
2954 resume this thread until the client gives us another action for
2958 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2960 struct thread_info
*thread
= (struct thread_info
*) entry
;
2962 thread
->last_resume_kind
= resume_stop
;
2964 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2966 /* Most threads are stopped implicitly (all-stop); tag that with
2968 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2969 thread
->last_status
.value
.sig
= GDB_SIGNAL_0
;
2973 /* Set all threads' states as "want-stopped". */
2976 gdb_wants_all_threads_stopped (void)
2978 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2981 /* Clear the gdb_detached flag of every process. */
2984 gdb_reattached_process (struct inferior_list_entry
*entry
)
2986 struct process_info
*process
= (struct process_info
*) entry
;
2988 process
->gdb_detached
= 0;
2991 /* Callback for for_each_inferior. Clear the thread's pending status
2995 clear_pending_status_callback (struct inferior_list_entry
*entry
)
2997 struct thread_info
*thread
= (struct thread_info
*) entry
;
2999 thread
->status_pending_p
= 0;
3002 /* Callback for for_each_inferior. If the thread is stopped with an
3003 interesting event, mark it as having a pending event. */
3006 set_pending_status_callback (struct inferior_list_entry
*entry
)
3008 struct thread_info
*thread
= (struct thread_info
*) entry
;
3010 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
3011 || (thread
->last_status
.value
.sig
!= GDB_SIGNAL_0
3012 /* A breakpoint, watchpoint or finished step from a previous
3013 GDB run isn't considered interesting for a new GDB run.
3014 If we left those pending, the new GDB could consider them
3015 random SIGTRAPs. This leaves out real async traps. We'd
3016 have to peek into the (target-specific) siginfo to
3017 distinguish those. */
3018 && thread
->last_status
.value
.sig
!= GDB_SIGNAL_TRAP
))
3019 thread
->status_pending_p
= 1;
3022 /* Callback for find_inferior. Return true if ENTRY (a thread) has a
3023 pending status to report to GDB. */
3026 find_status_pending_thread_callback (struct inferior_list_entry
*entry
, void *data
)
3028 struct thread_info
*thread
= (struct thread_info
*) entry
;
3030 return thread
->status_pending_p
;
3033 /* Status handler for the '?' packet. */
3036 handle_status (char *own_buf
)
3038 /* GDB is connected, don't forward events to the target anymore. */
3039 for_each_inferior (&all_processes
, gdb_reattached_process
);
3041 /* In non-stop mode, we must send a stop reply for each stopped
3042 thread. In all-stop mode, just send one for the first stopped
3047 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
3049 /* The first is sent immediatly. OK is sent if there is no
3050 stopped thread, which is the same handling of the vStopped
3051 packet (by design). */
3052 notif_write_event (¬if_stop
, own_buf
);
3056 struct inferior_list_entry
*thread
= NULL
;
3059 stabilize_threads ();
3060 gdb_wants_all_threads_stopped ();
3062 /* We can only report one status, but we might be coming out of
3063 non-stop -- if more than one thread is stopped with
3064 interesting events, leave events for the threads we're not
3065 reporting now pending. They'll be reported the next time the
3066 threads are resumed. Start by marking all interesting events
3068 for_each_inferior (&all_threads
, set_pending_status_callback
);
3070 /* Prefer the last thread that reported an event to GDB (even if
3071 that was a GDB_SIGNAL_TRAP). */
3072 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
3073 && last_status
.kind
!= TARGET_WAITKIND_EXITED
3074 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
3075 thread
= find_inferior_id (&all_threads
, last_ptid
);
3077 /* If the last event thread is not found for some reason, look
3078 for some other thread that might have an event to report. */
3080 thread
= find_inferior (&all_threads
,
3081 find_status_pending_thread_callback
, NULL
);
3083 /* If we're still out of luck, simply pick the first thread in
3086 thread
= get_first_inferior (&all_threads
);
3090 struct thread_info
*tp
= (struct thread_info
*) thread
;
3092 /* We're reporting this event, so it's no longer
3094 tp
->status_pending_p
= 0;
3096 /* GDB assumes the current thread is the thread we're
3097 reporting the status for. */
3098 general_thread
= thread
->id
;
3099 set_desired_thread (1);
3101 gdb_assert (tp
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
3102 prepare_resume_reply (own_buf
, tp
->entry
.id
, &tp
->last_status
);
3105 strcpy (own_buf
, "W00");
3110 gdbserver_version (void)
3112 printf ("GNU gdbserver %s%s\n"
3113 "Copyright (C) 2015 Free Software Foundation, Inc.\n"
3114 "gdbserver is free software, covered by the "
3115 "GNU General Public License.\n"
3116 "This gdbserver was configured as \"%s\"\n",
3117 PKGVERSION
, version
, host_name
);
3121 gdbserver_usage (FILE *stream
)
3123 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
3124 "\tgdbserver [OPTIONS] --attach COMM PID\n"
3125 "\tgdbserver [OPTIONS] --multi COMM\n"
3127 "COMM may either be a tty device (for serial debugging),\n"
3128 "HOST:PORT to listen for a TCP connection, or '-' or 'stdio' to use \n"
3129 "stdin/stdout of gdbserver.\n"
3130 "PROG is the executable program. ARGS are arguments passed to inferior.\n"
3131 "PID is the process ID to attach to, when --attach is specified.\n"
3133 "Operating modes:\n"
3135 " --attach Attach to running process PID.\n"
3136 " --multi Start server without a specific program, and\n"
3137 " only quit when explicitly commanded.\n"
3138 " --once Exit after the first connection has closed.\n"
3139 " --help Print this message and then exit.\n"
3140 " --version Display version information and exit.\n"
3144 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
3145 " --disable-randomization\n"
3146 " Run PROG with address space randomization disabled.\n"
3147 " --no-disable-randomization\n"
3148 " Don't disable address space randomization when\n"
3153 " --debug Enable general debugging output.\n"
3154 " --debug-format=opt1[,opt2,...]\n"
3155 " Specify extra content in debugging output.\n"
3160 " --remote-debug Enable remote protocol debugging output.\n"
3161 " --disable-packet=opt1[,opt2,...]\n"
3162 " Disable support for RSP packets or features.\n"
3164 " vCont, Tthread, qC, qfThreadInfo and \n"
3165 " threads (disable all threading packets).\n"
3167 "For more information, consult the GDB manual (available as on-line \n"
3168 "info or a printed manual).\n");
3169 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
3170 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
3174 gdbserver_show_disableable (FILE *stream
)
3176 fprintf (stream
, "Disableable packets:\n"
3177 " vCont \tAll vCont packets\n"
3178 " qC \tQuerying the current thread\n"
3179 " qfThreadInfo\tThread listing\n"
3180 " Tthread \tPassing the thread specifier in the "
3181 "T stop reply packet\n"
3182 " threads \tAll of the above\n");
3186 #undef require_running
3187 #define require_running(BUF) \
3188 if (!target_running ()) \
3195 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
3197 int pid
= * (int *) args
;
3199 if (ptid_get_pid (entry
->id
) == pid
)
3206 kill_inferior_callback (struct inferior_list_entry
*entry
)
3208 struct process_info
*process
= (struct process_info
*) entry
;
3209 int pid
= ptid_get_pid (process
->entry
.id
);
3211 kill_inferior (pid
);
3212 discard_queued_stop_replies (pid
);
3215 /* Callback for for_each_inferior to detach or kill the inferior,
3216 depending on whether we attached to it or not.
3217 We inform the user whether we're detaching or killing the process
3218 as this is only called when gdbserver is about to exit. */
3221 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
3223 struct process_info
*process
= (struct process_info
*) entry
;
3224 int pid
= ptid_get_pid (process
->entry
.id
);
3226 if (process
->attached
)
3227 detach_inferior (pid
);
3229 kill_inferior (pid
);
3231 discard_queued_stop_replies (pid
);
3234 /* for_each_inferior callback for detach_or_kill_for_exit to print
3235 the pids of started inferiors. */
3238 print_started_pid (struct inferior_list_entry
*entry
)
3240 struct process_info
*process
= (struct process_info
*) entry
;
3242 if (! process
->attached
)
3244 int pid
= ptid_get_pid (process
->entry
.id
);
3245 fprintf (stderr
, " %d", pid
);
3249 /* for_each_inferior callback for detach_or_kill_for_exit to print
3250 the pids of attached inferiors. */
3253 print_attached_pid (struct inferior_list_entry
*entry
)
3255 struct process_info
*process
= (struct process_info
*) entry
;
3257 if (process
->attached
)
3259 int pid
= ptid_get_pid (process
->entry
.id
);
3260 fprintf (stderr
, " %d", pid
);
3264 /* Call this when exiting gdbserver with possible inferiors that need
3265 to be killed or detached from. */
3268 detach_or_kill_for_exit (void)
3270 /* First print a list of the inferiors we will be killing/detaching.
3271 This is to assist the user, for example, in case the inferior unexpectedly
3272 dies after we exit: did we screw up or did the inferior exit on its own?
3273 Having this info will save some head-scratching. */
3275 if (have_started_inferiors_p ())
3277 fprintf (stderr
, "Killing process(es):");
3278 for_each_inferior (&all_processes
, print_started_pid
);
3279 fprintf (stderr
, "\n");
3281 if (have_attached_inferiors_p ())
3283 fprintf (stderr
, "Detaching process(es):");
3284 for_each_inferior (&all_processes
, print_attached_pid
);
3285 fprintf (stderr
, "\n");
3288 /* Now we can kill or detach the inferiors. */
3290 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
3293 /* Value that will be passed to exit(3) when gdbserver exits. */
3294 static int exit_code
;
3296 /* Cleanup version of detach_or_kill_for_exit. */
3299 detach_or_kill_for_exit_cleanup (void *ignore
)
3304 detach_or_kill_for_exit ();
3307 CATCH (exception
, RETURN_MASK_ALL
)
3310 fprintf (stderr
, "Detach or kill failed: %s\n", exception
.message
);
3316 /* Main function. This is called by the real "main" function,
3317 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
3319 static void ATTRIBUTE_NORETURN
3320 captured_main (int argc
, char *argv
[])
3324 char *arg_end
, *port
;
3325 char **next_arg
= &argv
[1];
3326 volatile int multi_mode
= 0;
3327 volatile int attach
= 0;
3330 while (*next_arg
!= NULL
&& **next_arg
== '-')
3332 if (strcmp (*next_arg
, "--version") == 0)
3334 gdbserver_version ();
3337 else if (strcmp (*next_arg
, "--help") == 0)
3339 gdbserver_usage (stdout
);
3342 else if (strcmp (*next_arg
, "--attach") == 0)
3344 else if (strcmp (*next_arg
, "--multi") == 0)
3346 else if (strcmp (*next_arg
, "--wrapper") == 0)
3350 wrapper_argv
= next_arg
;
3351 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
3354 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
3356 gdbserver_usage (stderr
);
3360 /* Consume the "--". */
3363 else if (strcmp (*next_arg
, "--debug") == 0)
3365 else if (startswith (*next_arg
, "--debug-format="))
3368 = parse_debug_format_options ((*next_arg
)
3369 + sizeof ("--debug-format=") - 1, 0);
3371 if (error_msg
!= NULL
)
3373 fprintf (stderr
, "%s", error_msg
);
3377 else if (strcmp (*next_arg
, "--remote-debug") == 0)
3379 else if (strcmp (*next_arg
, "--disable-packet") == 0)
3381 gdbserver_show_disableable (stdout
);
3384 else if (startswith (*next_arg
, "--disable-packet="))
3386 char *packets
, *tok
;
3388 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
3389 for (tok
= strtok (packets
, ",");
3391 tok
= strtok (NULL
, ","))
3393 if (strcmp ("vCont", tok
) == 0)
3394 disable_packet_vCont
= 1;
3395 else if (strcmp ("Tthread", tok
) == 0)
3396 disable_packet_Tthread
= 1;
3397 else if (strcmp ("qC", tok
) == 0)
3398 disable_packet_qC
= 1;
3399 else if (strcmp ("qfThreadInfo", tok
) == 0)
3400 disable_packet_qfThreadInfo
= 1;
3401 else if (strcmp ("threads", tok
) == 0)
3403 disable_packet_vCont
= 1;
3404 disable_packet_Tthread
= 1;
3405 disable_packet_qC
= 1;
3406 disable_packet_qfThreadInfo
= 1;
3410 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
3412 gdbserver_show_disableable (stderr
);
3417 else if (strcmp (*next_arg
, "-") == 0)
3419 /* "-" specifies a stdio connection and is a form of port
3421 *next_arg
= STDIO_CONNECTION_NAME
;
3424 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
3425 disable_randomization
= 1;
3426 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
3427 disable_randomization
= 0;
3428 else if (strcmp (*next_arg
, "--once") == 0)
3432 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
3442 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
3444 gdbserver_usage (stderr
);
3448 /* Remember stdio descriptors. LISTEN_DESC must not be listed, it will be
3449 opened by remote_prepare. */
3452 /* We need to know whether the remote connection is stdio before
3453 starting the inferior. Inferiors created in this scenario have
3454 stdin,stdout redirected. So do this here before we call
3456 remote_prepare (port
);
3461 /* --attach used to come after PORT, so allow it there for
3463 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
3470 && (*next_arg
== NULL
3471 || (*next_arg
)[0] == '\0'
3472 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
3474 || next_arg
[1] != NULL
))
3479 gdbserver_usage (stderr
);
3483 initialize_async_io ();
3485 initialize_event_loop ();
3486 if (target_supports_tracepoints ())
3487 initialize_tracepoint ();
3489 own_buf
= xmalloc (PBUFSIZ
+ 1);
3490 mem_buf
= xmalloc (PBUFSIZ
);
3492 if (pid
== 0 && *next_arg
!= NULL
)
3496 n
= argc
- (next_arg
- argv
);
3497 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
3498 for (i
= 0; i
< n
; i
++)
3499 program_argv
[i
] = xstrdup (next_arg
[i
]);
3500 program_argv
[i
] = NULL
;
3502 /* Wait till we are at first instruction in program. */
3503 start_inferior (program_argv
);
3505 /* We are now (hopefully) stopped at the first instruction of
3506 the target process. This assumes that the target process was
3507 successfully created. */
3511 if (attach_inferior (pid
) == -1)
3512 error ("Attaching not supported on this target");
3514 /* Otherwise succeeded. */
3518 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3519 last_status
.value
.integer
= 0;
3520 last_ptid
= minus_one_ptid
;
3522 make_cleanup (detach_or_kill_for_exit_cleanup
, NULL
);
3524 initialize_notif ();
3526 /* Don't report shared library events on the initial connection,
3527 even if some libraries are preloaded. Avoids the "stopped by
3528 shared library event" notice on gdb side. */
3531 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3532 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3537 if (!was_running
&& !multi_mode
)
3538 error ("No program to debug");
3545 report_fork_events
= 0;
3546 report_vfork_events
= 0;
3547 /* Be sure we're out of tfind mode. */
3548 current_traceframe
= -1;
3549 cont_thread
= null_ptid
;
3550 swbreak_feature
= 0;
3551 hwbreak_feature
= 0;
3557 /* Wait for events. This will return when all event sources
3558 are removed from the event loop. */
3559 start_event_loop ();
3561 /* If an exit was requested (using the "monitor exit"
3562 command), terminate now. The only other way to get
3563 here is for getpkt to fail; close the connection
3564 and reopen it at the top of the loop. */
3566 if (exit_requested
|| run_once
)
3567 throw_quit ("Quit");
3570 "Remote side has terminated connection. "
3571 "GDBserver will reopen the connection.\n");
3573 /* Get rid of any pending statuses. An eventual reconnection
3574 (by the same GDB instance or another) will refresh all its
3575 state from scratch. */
3576 discard_queued_stop_replies (-1);
3577 for_each_inferior (&all_threads
,
3578 clear_pending_status_callback
);
3582 if (disconnected_tracing
)
3584 /* Try to enable non-stop/async mode, so we we can
3585 both wait for an async socket accept, and handle
3586 async target events simultaneously. There's also
3587 no point either in having the target always stop
3588 all threads, when we're going to pass signals
3589 down without informing GDB. */
3592 if (start_non_stop (1))
3595 /* Detaching implicitly resumes all threads;
3596 simply disconnecting does not. */
3602 "Disconnected tracing disabled; "
3603 "stopping trace run.\n");
3608 CATCH (exception
, RETURN_MASK_ERROR
)
3610 if (response_needed
)
3612 write_enn (own_buf
);
3620 /* Main function. */
3623 main (int argc
, char *argv
[])
3628 captured_main (argc
, argv
);
3630 CATCH (exception
, RETURN_MASK_ALL
)
3632 if (exception
.reason
== RETURN_ERROR
)
3635 fprintf (stderr
, "%s\n", exception
.message
);
3636 fprintf (stderr
, "Exiting\n");
3644 gdb_assert_not_reached ("captured_main should never return");
3647 /* Skip PACKET until the next semi-colon (or end of string). */
3650 skip_to_semicolon (char **packet
)
3652 while (**packet
!= '\0' && **packet
!= ';')
3656 /* Process options coming from Z packets for a breakpoint. PACKET is
3657 the packet buffer. *PACKET is updated to point to the first char
3658 after the last processed option. */
3661 process_point_options (struct breakpoint
*bp
, char **packet
)
3663 char *dataptr
= *packet
;
3666 /* Check if data has the correct format. */
3667 if (*dataptr
!= ';')
3674 if (*dataptr
== ';')
3677 if (*dataptr
== 'X')
3679 /* Conditional expression. */
3681 debug_printf ("Found breakpoint condition.\n");
3682 if (!add_breakpoint_condition (bp
, &dataptr
))
3683 skip_to_semicolon (&dataptr
);
3685 else if (startswith (dataptr
, "cmds:"))
3687 dataptr
+= strlen ("cmds:");
3689 debug_printf ("Found breakpoint commands %s.\n", dataptr
);
3690 persist
= (*dataptr
== '1');
3692 if (add_breakpoint_commands (bp
, &dataptr
, persist
))
3693 skip_to_semicolon (&dataptr
);
3697 fprintf (stderr
, "Unknown token %c, ignoring.\n",
3699 /* Skip tokens until we find one that we recognize. */
3700 skip_to_semicolon (&dataptr
);
3706 /* Event loop callback that handles a serial event. The first byte in
3707 the serial buffer gets us here. We expect characters to arrive at
3708 a brisk pace, so we read the rest of the packet with a blocking
3712 process_serial_event (void)
3723 int new_packet_len
= -1;
3725 /* Used to decide when gdbserver should exit in
3726 multi-mode/remote. */
3727 static int have_ran
= 0;
3730 have_ran
= target_running ();
3732 disable_async_io ();
3734 response_needed
= 0;
3735 packet_len
= getpkt (own_buf
);
3736 if (packet_len
<= 0)
3739 /* Force an event loop break. */
3742 response_needed
= 1;
3749 handle_query (own_buf
, packet_len
, &new_packet_len
);
3752 handle_general_set (own_buf
);
3755 require_running (own_buf
);
3760 pid
= strtol (&own_buf
[i
], NULL
, 16);
3763 pid
= ptid_get_pid (current_ptid
);
3765 if ((tracing
&& disconnected_tracing
) || any_persistent_commands ())
3767 struct thread_resume resume_info
;
3768 struct process_info
*process
= find_process_pid (pid
);
3770 if (process
== NULL
)
3772 write_enn (own_buf
);
3776 if (tracing
&& disconnected_tracing
)
3778 "Disconnected tracing in effect, "
3779 "leaving gdbserver attached to the process\n");
3781 if (any_persistent_commands ())
3783 "Persistent commands are present, "
3784 "leaving gdbserver attached to the process\n");
3786 /* Make sure we're in non-stop/async mode, so we we can both
3787 wait for an async socket accept, and handle async target
3788 events simultaneously. There's also no point either in
3789 having the target stop all threads, when we're going to
3790 pass signals down without informing GDB. */
3794 debug_printf ("Forcing non-stop mode\n");
3800 process
->gdb_detached
= 1;
3802 /* Detaching implicitly resumes all threads. */
3803 resume_info
.thread
= minus_one_ptid
;
3804 resume_info
.kind
= resume_continue
;
3805 resume_info
.sig
= 0;
3806 (*the_target
->resume
) (&resume_info
, 1);
3809 break; /* from switch/case */
3812 fprintf (stderr
, "Detaching from process %d\n", pid
);
3814 if (detach_inferior (pid
) != 0)
3815 write_enn (own_buf
);
3818 discard_queued_stop_replies (pid
);
3821 if (extended_protocol
)
3823 /* Treat this like a normal program exit. */
3824 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3825 last_status
.value
.integer
= 0;
3826 last_ptid
= pid_to_ptid (pid
);
3828 current_thread
= NULL
;
3835 /* If we are attached, then we can exit. Otherwise, we
3836 need to hang around doing nothing, until the child is
3838 join_inferior (pid
);
3844 extended_protocol
= 1;
3848 handle_status (own_buf
);
3851 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
3853 ptid_t gdb_id
, thread_id
;
3856 require_running (own_buf
);
3858 gdb_id
= read_ptid (&own_buf
[2], NULL
);
3860 pid
= ptid_get_pid (gdb_id
);
3862 if (ptid_equal (gdb_id
, null_ptid
)
3863 || ptid_equal (gdb_id
, minus_one_ptid
))
3864 thread_id
= null_ptid
;
3866 && ptid_equal (pid_to_ptid (pid
),
3869 struct thread_info
*thread
=
3870 (struct thread_info
*) find_inferior (&all_threads
,
3875 write_enn (own_buf
);
3879 thread_id
= thread
->entry
.id
;
3883 thread_id
= gdb_id_to_thread_id (gdb_id
);
3884 if (ptid_equal (thread_id
, null_ptid
))
3886 write_enn (own_buf
);
3891 if (own_buf
[1] == 'g')
3893 if (ptid_equal (thread_id
, null_ptid
))
3895 /* GDB is telling us to choose any thread. Check if
3896 the currently selected thread is still valid. If
3897 it is not, select the first available. */
3898 struct thread_info
*thread
=
3899 (struct thread_info
*) find_inferior_id (&all_threads
,
3903 thread
= get_first_thread ();
3904 thread_id
= thread
->entry
.id
;
3908 general_thread
= thread_id
;
3909 set_desired_thread (1);
3911 else if (own_buf
[1] == 'c')
3912 cont_thread
= thread_id
;
3918 /* Silently ignore it so that gdb can extend the protocol
3919 without compatibility headaches. */
3924 require_running (own_buf
);
3925 if (current_traceframe
>= 0)
3927 struct regcache
*regcache
3928 = new_register_cache (current_target_desc ());
3930 if (fetch_traceframe_registers (current_traceframe
,
3932 registers_to_string (regcache
, own_buf
);
3934 write_enn (own_buf
);
3935 free_register_cache (regcache
);
3939 struct regcache
*regcache
;
3941 set_desired_thread (1);
3942 regcache
= get_thread_regcache (current_thread
, 1);
3943 registers_to_string (regcache
, own_buf
);
3947 require_running (own_buf
);
3948 if (current_traceframe
>= 0)
3949 write_enn (own_buf
);
3952 struct regcache
*regcache
;
3954 set_desired_thread (1);
3955 regcache
= get_thread_regcache (current_thread
, 1);
3956 registers_from_string (regcache
, &own_buf
[1]);
3961 require_running (own_buf
);
3962 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
3963 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
3965 write_enn (own_buf
);
3967 bin2hex (mem_buf
, own_buf
, res
);
3970 require_running (own_buf
);
3971 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
3972 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
3975 write_enn (own_buf
);
3978 require_running (own_buf
);
3979 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
3980 &mem_addr
, &len
, &mem_buf
) < 0
3981 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
3982 write_enn (own_buf
);
3987 require_running (own_buf
);
3988 hex2bin (own_buf
+ 1, &sig
, 1);
3989 if (gdb_signal_to_host_p (sig
))
3990 signal
= gdb_signal_to_host (sig
);
3993 myresume (own_buf
, 0, signal
);
3996 require_running (own_buf
);
3997 hex2bin (own_buf
+ 1, &sig
, 1);
3998 if (gdb_signal_to_host_p (sig
))
3999 signal
= gdb_signal_to_host (sig
);
4002 myresume (own_buf
, 1, signal
);
4005 require_running (own_buf
);
4007 myresume (own_buf
, 0, signal
);
4010 require_running (own_buf
);
4012 myresume (own_buf
, 1, signal
);
4014 case 'Z': /* insert_ ... */
4016 case 'z': /* remove_ ... */
4021 char type
= own_buf
[1];
4023 const int insert
= ch
== 'Z';
4024 char *p
= &own_buf
[3];
4026 p
= unpack_varlen_hex (p
, &addr
);
4027 len
= strtol (p
+ 1, &dataptr
, 16);
4031 struct breakpoint
*bp
;
4033 bp
= set_gdb_breakpoint (type
, addr
, len
, &res
);
4038 /* GDB may have sent us a list of *point parameters to
4039 be evaluated on the target's side. Read such list
4040 here. If we already have a list of parameters, GDB
4041 is telling us to drop that list and use this one
4043 clear_breakpoint_conditions_and_commands (bp
);
4044 process_point_options (bp
, &dataptr
);
4048 res
= delete_gdb_breakpoint (type
, addr
, len
);
4056 write_enn (own_buf
);
4060 response_needed
= 0;
4061 if (!target_running ())
4062 /* The packet we received doesn't make sense - but we can't
4063 reply to it, either. */
4066 fprintf (stderr
, "Killing all inferiors\n");
4067 for_each_inferior (&all_processes
, kill_inferior_callback
);
4069 /* When using the extended protocol, we wait with no program
4070 running. The traditional protocol will exit instead. */
4071 if (extended_protocol
)
4073 last_status
.kind
= TARGET_WAITKIND_EXITED
;
4074 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
4082 ptid_t gdb_id
, thread_id
;
4084 require_running (own_buf
);
4086 gdb_id
= read_ptid (&own_buf
[1], NULL
);
4087 thread_id
= gdb_id_to_thread_id (gdb_id
);
4088 if (ptid_equal (thread_id
, null_ptid
))
4090 write_enn (own_buf
);
4094 if (mythread_alive (thread_id
))
4097 write_enn (own_buf
);
4101 response_needed
= 0;
4103 /* Restarting the inferior is only supported in the extended
4105 if (extended_protocol
)
4107 if (target_running ())
4108 for_each_inferior (&all_processes
,
4109 kill_inferior_callback
);
4110 fprintf (stderr
, "GDBserver restarting\n");
4112 /* Wait till we are at 1st instruction in prog. */
4113 if (program_argv
!= NULL
)
4115 start_inferior (program_argv
);
4116 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
4118 /* Stopped at the first instruction of the target
4120 general_thread
= last_ptid
;
4124 /* Something went wrong. */
4125 general_thread
= null_ptid
;
4130 last_status
.kind
= TARGET_WAITKIND_EXITED
;
4131 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
4137 /* It is a request we don't understand. Respond with an
4138 empty packet so that gdb knows that we don't support this
4144 /* Extended (long) request. */
4145 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
4149 /* It is a request we don't understand. Respond with an empty
4150 packet so that gdb knows that we don't support this
4156 if (new_packet_len
!= -1)
4157 putpkt_binary (own_buf
, new_packet_len
);
4161 response_needed
= 0;
4163 if (!extended_protocol
&& have_ran
&& !target_running ())
4165 /* In non-stop, defer exiting until GDB had a chance to query
4166 the whole vStopped list (until it gets an OK). */
4167 if (QUEUE_is_empty (notif_event_p
, notif_stop
.queue
))
4169 /* Be transparent when GDB is connected through stdio -- no
4170 need to spam GDB's console. */
4171 if (!remote_connection_is_stdio ())
4172 fprintf (stderr
, "GDBserver exiting\n");
4184 /* Event-loop callback for serial events. */
4187 handle_serial_event (int err
, gdb_client_data client_data
)
4190 debug_printf ("handling possible serial event\n");
4192 /* Really handle it. */
4193 if (process_serial_event () < 0)
4196 /* Be sure to not change the selected thread behind GDB's back.
4197 Important in the non-stop mode asynchronous protocol. */
4198 set_desired_thread (1);
4203 /* Event-loop callback for target events. */
4206 handle_target_event (int err
, gdb_client_data client_data
)
4209 debug_printf ("handling possible target event\n");
4211 last_ptid
= mywait (minus_one_ptid
, &last_status
,
4214 if (last_status
.kind
== TARGET_WAITKIND_NO_RESUMED
)
4216 /* No RSP support for this yet. */
4218 else if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
4220 int pid
= ptid_get_pid (last_ptid
);
4221 struct process_info
*process
= find_process_pid (pid
);
4222 int forward_event
= !gdb_connected () || process
->gdb_detached
;
4224 if (last_status
.kind
== TARGET_WAITKIND_EXITED
4225 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
4227 mark_breakpoints_out (process
);
4228 mourn_inferior (process
);
4232 /* We're reporting this thread as stopped. Update its
4233 "want-stopped" state to what the client wants, until it
4234 gets a new resume action. */
4235 current_thread
->last_resume_kind
= resume_stop
;
4236 current_thread
->last_status
= last_status
;
4241 if (!target_running ())
4243 /* The last process exited. We're done. */
4247 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
4249 /* A thread stopped with a signal, but gdb isn't
4250 connected to handle it. Pass it down to the
4251 inferior, as if it wasn't being traced. */
4252 struct thread_resume resume_info
;
4255 debug_printf ("GDB not connected; forwarding event %d for"
4257 (int) last_status
.kind
,
4258 target_pid_to_str (last_ptid
));
4260 resume_info
.thread
= last_ptid
;
4261 resume_info
.kind
= resume_continue
;
4262 resume_info
.sig
= gdb_signal_to_host (last_status
.value
.sig
);
4263 (*the_target
->resume
) (&resume_info
, 1);
4265 else if (debug_threads
)
4266 debug_printf ("GDB not connected; ignoring event %d for [%s]\n",
4267 (int) last_status
.kind
,
4268 target_pid_to_str (last_ptid
));
4272 struct vstop_notif
*vstop_notif
4273 = xmalloc (sizeof (struct vstop_notif
));
4275 vstop_notif
->status
= last_status
;
4276 vstop_notif
->ptid
= last_ptid
;
4277 /* Push Stop notification. */
4278 notif_push (¬if_stop
,
4279 (struct notif_event
*) vstop_notif
);
4283 /* Be sure to not change the selected thread behind GDB's back.
4284 Important in the non-stop mode asynchronous protocol. */
4285 set_desired_thread (1);