1 /* Main code for remote server for GDB.
2 Copyright (C) 1989-2013 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"
30 #include "btrace-common.h"
32 /* The thread set with an `Hc' packet. `Hc' is deprecated in favor of
33 `vCont'. Note the multi-process extensions made `vCont' a
34 requirement, so `Hc pPID.TID' is pretty much undefined. So
35 CONT_THREAD can be null_ptid for no `Hc' thread, minus_one_ptid for
36 resuming all threads of the process (again, `Hc' isn't used for
37 multi-process), or a specific thread ptid_t.
39 We also set this when handling a single-thread `vCont' resume, as
40 some places in the backends check it to know when (and for which
41 thread) single-thread scheduler-locking is in effect. */
44 /* The thread set with an `Hg' packet. */
45 ptid_t general_thread
;
49 static int extended_protocol
;
50 static int response_needed
;
51 static int exit_requested
;
53 /* --once: Exit after the first connection has closed. */
59 /* Whether we should attempt to disable the operating system's address
60 space randomization feature before starting an inferior. */
61 int disable_randomization
= 1;
63 static char **program_argv
, **wrapper_argv
;
65 /* Enable miscellaneous debugging output. The name is historical - it
66 was originally used to debug LinuxThreads support. */
69 /* Enable debugging of h/w breakpoint/watchpoint support. */
72 int pass_signals
[GDB_SIGNAL_LAST
];
73 int program_signals
[GDB_SIGNAL_LAST
];
74 int program_signals_p
;
78 /* The PID of the originally created or attached inferior. Used to
79 send signals to the process when GDB sends us an asynchronous interrupt
80 (user hitting Control-C in the client), and to wait for the child to exit
81 when no longer debugging it. */
83 unsigned long signal_pid
;
86 /* A file descriptor for the controlling terminal. */
89 /* TERMINAL_FD's original foreground group. */
90 pid_t old_foreground_pgrp
;
92 /* Hand back terminal ownership to the original foreground group. */
95 restore_old_foreground_pgrp (void)
97 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
101 /* Set if you want to disable optional thread related packets support
102 in gdbserver, for the sake of testing GDB against stubs that don't
104 int disable_packet_vCont
;
105 int disable_packet_Tthread
;
106 int disable_packet_qC
;
107 int disable_packet_qfThreadInfo
;
109 /* Last status reported to GDB. */
110 static struct target_waitstatus last_status
;
111 static ptid_t last_ptid
;
113 static char *own_buf
;
114 static unsigned char *mem_buf
;
116 /* A sub-class of 'struct notif_event' for stop, holding information
117 relative to a single stop reply. We keep a queue of these to
118 push to GDB in non-stop mode. */
122 struct notif_event base
;
124 /* Thread or process that got the event. */
128 struct target_waitstatus status
;
131 DEFINE_QUEUE_P (notif_event_p
);
133 /* Put a stop reply to the stop reply queue. */
136 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
138 struct vstop_notif
*new_notif
= xmalloc (sizeof (*new_notif
));
140 new_notif
->ptid
= ptid
;
141 new_notif
->status
= *status
;
143 notif_event_enque (¬if_stop
, (struct notif_event
*) new_notif
);
147 remove_all_on_match_pid (QUEUE (notif_event_p
) *q
,
148 QUEUE_ITER (notif_event_p
) *iter
,
149 struct notif_event
*event
,
155 || ptid_get_pid (((struct vstop_notif
*) event
)->ptid
) == *pid
)
157 if (q
->free_func
!= NULL
)
158 q
->free_func (event
);
160 QUEUE_remove_elem (notif_event_p
, q
, iter
);
166 /* Get rid of the currently pending stop replies for PID. If PID is
167 -1, then apply to all processes. */
170 discard_queued_stop_replies (int pid
)
172 QUEUE_iterate (notif_event_p
, notif_stop
.queue
,
173 remove_all_on_match_pid
, &pid
);
177 vstop_notif_reply (struct notif_event
*event
, char *own_buf
)
179 struct vstop_notif
*vstop
= (struct vstop_notif
*) event
;
181 prepare_resume_reply (own_buf
, vstop
->ptid
, &vstop
->status
);
184 struct notif_server notif_stop
=
186 "vStopped", "Stop", NULL
, vstop_notif_reply
,
190 target_running (void)
192 return all_threads
.head
!= NULL
;
196 start_inferior (char **argv
)
198 char **new_argv
= argv
;
200 if (wrapper_argv
!= NULL
)
204 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
206 for (i
= 0; argv
[i
] != NULL
; i
++)
208 new_argv
= alloca (sizeof (char *) * count
);
210 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
211 new_argv
[count
++] = wrapper_argv
[i
];
212 for (i
= 0; argv
[i
] != NULL
; i
++)
213 new_argv
[count
++] = argv
[i
];
214 new_argv
[count
] = NULL
;
220 for (i
= 0; new_argv
[i
]; ++i
)
221 fprintf (stderr
, "new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
226 signal (SIGTTOU
, SIG_DFL
);
227 signal (SIGTTIN
, SIG_DFL
);
230 /* Clear this so the backend doesn't get confused, thinking
231 CONT_THREAD died, and it needs to resume all threads. */
232 cont_thread
= null_ptid
;
234 signal_pid
= create_inferior (new_argv
[0], new_argv
);
236 /* FIXME: we don't actually know at this point that the create
237 actually succeeded. We won't know that until we wait. */
238 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
243 signal (SIGTTOU
, SIG_IGN
);
244 signal (SIGTTIN
, SIG_IGN
);
245 terminal_fd
= fileno (stderr
);
246 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
247 tcsetpgrp (terminal_fd
, signal_pid
);
248 atexit (restore_old_foreground_pgrp
);
251 if (wrapper_argv
!= NULL
)
253 struct thread_resume resume_info
;
255 resume_info
.thread
= pid_to_ptid (signal_pid
);
256 resume_info
.kind
= resume_continue
;
259 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
261 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
266 (*the_target
->resume
) (&resume_info
, 1);
268 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
269 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
272 current_inferior
->last_resume_kind
= resume_stop
;
273 current_inferior
->last_status
= last_status
;
275 while (last_status
.value
.sig
!= GDB_SIGNAL_TRAP
);
280 /* Wait till we are at 1st instruction in program, return new pid
281 (assuming success). */
282 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
284 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
285 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
287 current_inferior
->last_resume_kind
= resume_stop
;
288 current_inferior
->last_status
= last_status
;
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. */
311 /* Clear this so the backend doesn't get confused, thinking
312 CONT_THREAD died, and it needs to resume all threads. */
313 cont_thread
= null_ptid
;
317 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
319 /* GDB knows to ignore the first SIGSTOP after attaching to a running
320 process using the "attach" command, but this is different; it's
321 just using "target remote". Pretend it's just starting up. */
322 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
323 && last_status
.value
.sig
== GDB_SIGNAL_STOP
)
324 last_status
.value
.sig
= GDB_SIGNAL_TRAP
;
326 current_inferior
->last_resume_kind
= resume_stop
;
327 current_inferior
->last_status
= last_status
;
333 extern int remote_debug
;
335 /* Decode a qXfer read request. Return 0 if everything looks OK,
339 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
341 /* After the read marker and annex, qXfer looks like a
342 traditional 'm' packet. */
343 decode_m_packet (buf
, ofs
, len
);
349 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
351 /* Extract and NUL-terminate the object. */
353 while (*buf
&& *buf
!= ':')
359 /* Extract and NUL-terminate the read/write action. */
361 while (*buf
&& *buf
!= ':')
367 /* Extract and NUL-terminate the annex. */
369 while (*buf
&& *buf
!= ':')
379 /* Write the response to a successful qXfer read. Returns the
380 length of the (binary) data stored in BUF, corresponding
381 to as much of DATA/LEN as we could fit. IS_MORE controls
382 the first character of the response. */
384 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
393 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
397 /* Handle btrace enabling. */
400 handle_btrace_enable (struct thread_info
*thread
)
402 if (thread
->btrace
!= NULL
)
403 return "E.Btrace already enabled.";
405 thread
->btrace
= target_enable_btrace (thread
->entry
.id
);
406 if (thread
->btrace
== NULL
)
407 return "E.Could not enable btrace.";
412 /* Handle btrace disabling. */
415 handle_btrace_disable (struct thread_info
*thread
)
418 if (thread
->btrace
== NULL
)
419 return "E.Branch tracing not enabled.";
421 if (target_disable_btrace (thread
->btrace
) != 0)
422 return "E.Could not disable branch tracing.";
424 thread
->btrace
= NULL
;
428 /* Handle the "Qbtrace" packet. */
431 handle_btrace_general_set (char *own_buf
)
433 struct thread_info
*thread
;
437 if (strncmp ("Qbtrace:", own_buf
, strlen ("Qbtrace:")) != 0)
440 op
= own_buf
+ strlen ("Qbtrace:");
442 if (!target_supports_btrace ())
444 strcpy (own_buf
, "E.Target does not support branch tracing.");
448 if (ptid_equal (general_thread
, null_ptid
)
449 || ptid_equal (general_thread
, minus_one_ptid
))
451 strcpy (own_buf
, "E.Must select a single thread.");
455 thread
= find_thread_ptid (general_thread
);
458 strcpy (own_buf
, "E.No such thread.");
464 if (strcmp (op
, "bts") == 0)
465 err
= handle_btrace_enable (thread
);
466 else if (strcmp (op
, "off") == 0)
467 err
= handle_btrace_disable (thread
);
469 err
= "E.Bad Qbtrace operation. Use bts or off.";
472 strcpy (own_buf
, err
);
479 /* Handle all of the extended 'Q' packets. */
482 handle_general_set (char *own_buf
)
484 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
486 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
487 const char *p
= own_buf
+ strlen ("QPassSignals:");
490 p
= decode_address_to_semicolon (&cursig
, p
);
491 for (i
= 0; i
< numsigs
; i
++)
497 /* Keep looping, to clear the remaining signals. */
500 p
= decode_address_to_semicolon (&cursig
, p
);
505 strcpy (own_buf
, "OK");
509 if (strncmp ("QProgramSignals:", own_buf
, strlen ("QProgramSignals:")) == 0)
511 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
512 const char *p
= own_buf
+ strlen ("QProgramSignals:");
515 program_signals_p
= 1;
517 p
= decode_address_to_semicolon (&cursig
, p
);
518 for (i
= 0; i
< numsigs
; i
++)
522 program_signals
[i
] = 1;
524 /* Keep looping, to clear the remaining signals. */
527 p
= decode_address_to_semicolon (&cursig
, p
);
530 program_signals
[i
] = 0;
532 strcpy (own_buf
, "OK");
536 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
540 fprintf (stderr
, "[noack mode enabled]\n");
549 if (strncmp (own_buf
, "QNonStop:", 9) == 0)
551 char *mode
= own_buf
+ 9;
555 if (strcmp (mode
, "0") == 0)
557 else if (strcmp (mode
, "1") == 0)
561 /* We don't know what this mode is, so complain to
563 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
569 req_str
= req
? "non-stop" : "all-stop";
570 if (start_non_stop (req
) != 0)
572 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
580 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
586 if (strncmp ("QDisableRandomization:", own_buf
,
587 strlen ("QDisableRandomization:")) == 0)
589 char *packet
= own_buf
+ strlen ("QDisableRandomization:");
592 unpack_varlen_hex (packet
, &setting
);
593 disable_randomization
= setting
;
597 if (disable_randomization
)
598 fprintf (stderr
, "[address space randomization disabled]\n");
600 fprintf (stderr
, "[address space randomization enabled]\n");
607 if (target_supports_tracepoints ()
608 && handle_tracepoint_general_set (own_buf
))
611 if (strncmp ("QAgent:", own_buf
, strlen ("QAgent:")) == 0)
613 char *mode
= own_buf
+ strlen ("QAgent:");
616 if (strcmp (mode
, "0") == 0)
618 else if (strcmp (mode
, "1") == 0)
622 /* We don't know what this value is, so complain to GDB. */
623 sprintf (own_buf
, "E.Unknown QAgent value");
627 /* Update the flag. */
630 fprintf (stderr
, "[%s agent]\n", req
? "Enable" : "Disable");
635 if (handle_btrace_general_set (own_buf
))
638 /* Otherwise we didn't know what packet it was. Say we didn't
644 get_features_xml (const char *annex
)
646 const struct target_desc
*desc
= current_target_desc ();
648 /* `desc->xmltarget' defines what to return when looking for the
649 "target.xml" file. Its contents can either be verbatim XML code
650 (prefixed with a '@') or else the name of the actual XML file to
651 be used in place of "target.xml".
653 This variable is set up from the auto-generated
654 init_registers_... routine for the current target. */
656 if (desc
->xmltarget
!= NULL
&& strcmp (annex
, "target.xml") == 0)
658 if (*desc
->xmltarget
== '@')
659 return desc
->xmltarget
+ 1;
661 annex
= desc
->xmltarget
;
666 extern const char *const xml_builtin
[][2];
669 /* Look for the annex. */
670 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
671 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
674 if (xml_builtin
[i
][0] != NULL
)
675 return xml_builtin
[i
][1];
683 monitor_show_help (void)
685 monitor_output ("The following monitor commands are supported:\n");
686 monitor_output (" set debug <0|1>\n");
687 monitor_output (" Enable general debugging messages\n");
688 monitor_output (" set debug-hw-points <0|1>\n");
689 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
690 monitor_output (" set remote-debug <0|1>\n");
691 monitor_output (" Enable remote protocol debugging messages\n");
692 monitor_output (" exit\n");
693 monitor_output (" Quit GDBserver\n");
696 /* Read trace frame or inferior memory. Returns the number of bytes
697 actually read, zero when no further transfer is possible, and -1 on
698 error. Return of a positive value smaller than LEN does not
699 indicate there's no more to be read, only the end of the transfer.
700 E.g., when GDB reads memory from a traceframe, a first request may
701 be served from a memory block that does not cover the whole request
702 length. A following request gets the rest served from either
703 another block (of the same traceframe) or from the read-only
707 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
711 if (current_traceframe
>= 0)
714 ULONGEST length
= len
;
716 if (traceframe_read_mem (current_traceframe
,
717 memaddr
, myaddr
, len
, &nbytes
))
719 /* Data read from trace buffer, we're done. */
722 if (!in_readonly_region (memaddr
, length
))
724 /* Otherwise we have a valid readonly case, fall through. */
725 /* (assume no half-trace half-real blocks for now) */
728 res
= prepare_to_access_memory ();
731 res
= read_inferior_memory (memaddr
, myaddr
, len
);
732 done_accessing_memory ();
734 return res
== 0 ? len
: -1;
740 /* Write trace frame or inferior memory. Actually, writing to trace
741 frames is forbidden. */
744 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
746 if (current_traceframe
>= 0)
752 ret
= prepare_to_access_memory ();
755 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
756 done_accessing_memory ();
762 /* Subroutine of handle_search_memory to simplify it. */
765 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
766 gdb_byte
*pattern
, unsigned pattern_len
,
767 gdb_byte
*search_buf
,
768 unsigned chunk_size
, unsigned search_buf_size
,
769 CORE_ADDR
*found_addrp
)
771 /* Prime the search buffer. */
773 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
776 warning ("Unable to access %ld bytes of target "
777 "memory at 0x%lx, halting search.",
778 (long) search_buf_size
, (long) start_addr
);
782 /* Perform the search.
784 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
785 When we've scanned N bytes we copy the trailing bytes to the start and
786 read in another N bytes. */
788 while (search_space_len
>= pattern_len
)
791 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
795 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
797 if (found_ptr
!= NULL
)
799 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
800 *found_addrp
= found_addr
;
804 /* Not found in this chunk, skip to next chunk. */
806 /* Don't let search_space_len wrap here, it's unsigned. */
807 if (search_space_len
>= chunk_size
)
808 search_space_len
-= chunk_size
;
810 search_space_len
= 0;
812 if (search_space_len
>= pattern_len
)
814 unsigned keep_len
= search_buf_size
- chunk_size
;
815 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
818 /* Copy the trailing part of the previous iteration to the front
819 of the buffer for the next iteration. */
820 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
822 nr_to_read
= (search_space_len
- keep_len
< chunk_size
823 ? search_space_len
- keep_len
826 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
827 nr_to_read
) != search_buf_size
)
829 warning ("Unable to access %ld bytes of target memory "
830 "at 0x%lx, halting search.",
831 (long) nr_to_read
, (long) read_addr
);
835 start_addr
+= chunk_size
;
844 /* Handle qSearch:memory packets. */
847 handle_search_memory (char *own_buf
, int packet_len
)
849 CORE_ADDR start_addr
;
850 CORE_ADDR search_space_len
;
852 unsigned int pattern_len
;
853 /* NOTE: also defined in find.c testcase. */
854 #define SEARCH_CHUNK_SIZE 16000
855 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
856 /* Buffer to hold memory contents for searching. */
857 gdb_byte
*search_buf
;
858 unsigned search_buf_size
;
860 CORE_ADDR found_addr
;
861 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
863 pattern
= malloc (packet_len
);
866 error ("Unable to allocate memory to perform the search");
867 strcpy (own_buf
, "E00");
870 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
871 packet_len
- cmd_name_len
,
872 &start_addr
, &search_space_len
,
873 pattern
, &pattern_len
) < 0)
876 error ("Error in parsing qSearch:memory packet");
877 strcpy (own_buf
, "E00");
881 search_buf_size
= chunk_size
+ pattern_len
- 1;
883 /* No point in trying to allocate a buffer larger than the search space. */
884 if (search_space_len
< search_buf_size
)
885 search_buf_size
= search_space_len
;
887 search_buf
= malloc (search_buf_size
);
888 if (search_buf
== NULL
)
891 error ("Unable to allocate memory to perform the search");
892 strcpy (own_buf
, "E00");
896 found
= handle_search_memory_1 (start_addr
, search_space_len
,
897 pattern
, pattern_len
,
898 search_buf
, chunk_size
, search_buf_size
,
902 sprintf (own_buf
, "1,%lx", (long) found_addr
);
904 strcpy (own_buf
, "0");
906 strcpy (own_buf
, "E00");
912 #define require_running(BUF) \
913 if (!target_running ()) \
919 /* Handle monitor commands not handled by target-specific handlers. */
922 handle_monitor_command (char *mon
, char *own_buf
)
924 if (strcmp (mon
, "set debug 1") == 0)
927 monitor_output ("Debug output enabled.\n");
929 else if (strcmp (mon
, "set debug 0") == 0)
932 monitor_output ("Debug output disabled.\n");
934 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
937 monitor_output ("H/W point debugging output enabled.\n");
939 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
942 monitor_output ("H/W point debugging output disabled.\n");
944 else if (strcmp (mon
, "set remote-debug 1") == 0)
947 monitor_output ("Protocol debug output enabled.\n");
949 else if (strcmp (mon
, "set remote-debug 0") == 0)
952 monitor_output ("Protocol debug output disabled.\n");
954 else if (strcmp (mon
, "help") == 0)
955 monitor_show_help ();
956 else if (strcmp (mon
, "exit") == 0)
960 monitor_output ("Unknown monitor command.\n\n");
961 monitor_show_help ();
966 /* Associates a callback with each supported qXfer'able object. */
970 /* The object this handler handles. */
973 /* Request that the target transfer up to LEN 8-bit bytes of the
974 target's OBJECT. The OFFSET, for a seekable object, specifies
975 the starting point. The ANNEX can be used to provide additional
976 data-specific information to the target.
978 Return the number of bytes actually transfered, zero when no
979 further transfer is possible, -1 on error, -2 when the transfer
980 is not supported, and -3 on a verbose error message that should
981 be preserved. Return of a positive value smaller than LEN does
982 not indicate the end of the object, only the end of the transfer.
984 One, and only one, of readbuf or writebuf must be non-NULL. */
985 int (*xfer
) (const char *annex
,
986 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
987 ULONGEST offset
, LONGEST len
);
990 /* Handle qXfer:auxv:read. */
993 handle_qxfer_auxv (const char *annex
,
994 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
995 ULONGEST offset
, LONGEST len
)
997 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
1000 if (annex
[0] != '\0' || !target_running ())
1003 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
1006 /* Handle qXfer:features:read. */
1009 handle_qxfer_features (const char *annex
,
1010 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1011 ULONGEST offset
, LONGEST len
)
1013 const char *document
;
1016 if (writebuf
!= NULL
)
1019 if (!target_running ())
1022 /* Grab the correct annex. */
1023 document
= get_features_xml (annex
);
1024 if (document
== NULL
)
1027 total_len
= strlen (document
);
1029 if (offset
> total_len
)
1032 if (offset
+ len
> total_len
)
1033 len
= total_len
- offset
;
1035 memcpy (readbuf
, document
+ offset
, len
);
1039 /* Handle qXfer:libraries:read. */
1042 handle_qxfer_libraries (const char *annex
,
1043 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1044 ULONGEST offset
, LONGEST len
)
1046 unsigned int total_len
;
1048 struct inferior_list_entry
*dll_ptr
;
1050 if (writebuf
!= NULL
)
1053 if (annex
[0] != '\0' || !target_running ())
1056 /* Over-estimate the necessary memory. Assume that every character
1057 in the library name must be escaped. */
1059 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
1060 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
1062 document
= malloc (total_len
);
1063 if (document
== NULL
)
1066 strcpy (document
, "<library-list>\n");
1067 p
= document
+ strlen (document
);
1069 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
1071 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
1074 strcpy (p
, " <library name=\"");
1076 name
= xml_escape_text (dll
->name
);
1080 strcpy (p
, "\"><segment address=\"");
1082 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1084 strcpy (p
, "\"/></library>\n");
1088 strcpy (p
, "</library-list>\n");
1090 total_len
= strlen (document
);
1092 if (offset
> total_len
)
1098 if (offset
+ len
> total_len
)
1099 len
= total_len
- offset
;
1101 memcpy (readbuf
, document
+ offset
, len
);
1106 /* Handle qXfer:libraries-svr4:read. */
1109 handle_qxfer_libraries_svr4 (const char *annex
,
1110 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1111 ULONGEST offset
, LONGEST len
)
1113 if (writebuf
!= NULL
)
1116 if (!target_running () || the_target
->qxfer_libraries_svr4
== NULL
)
1119 return the_target
->qxfer_libraries_svr4 (annex
, readbuf
, writebuf
, offset
, len
);
1122 /* Handle qXfer:osadata:read. */
1125 handle_qxfer_osdata (const char *annex
,
1126 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1127 ULONGEST offset
, LONGEST len
)
1129 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
1132 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
1135 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1138 handle_qxfer_siginfo (const char *annex
,
1139 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1140 ULONGEST offset
, LONGEST len
)
1142 if (the_target
->qxfer_siginfo
== NULL
)
1145 if (annex
[0] != '\0' || !target_running ())
1148 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1151 /* Handle qXfer:spu:read and qXfer:spu:write. */
1154 handle_qxfer_spu (const char *annex
,
1155 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1156 ULONGEST offset
, LONGEST len
)
1158 if (the_target
->qxfer_spu
== NULL
)
1161 if (!target_running ())
1164 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1167 /* Handle qXfer:statictrace:read. */
1170 handle_qxfer_statictrace (const char *annex
,
1171 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1172 ULONGEST offset
, LONGEST len
)
1176 if (writebuf
!= NULL
)
1179 if (annex
[0] != '\0' || !target_running () || current_traceframe
== -1)
1182 if (traceframe_read_sdata (current_traceframe
, offset
,
1183 readbuf
, len
, &nbytes
))
1188 /* Helper for handle_qxfer_threads. */
1191 handle_qxfer_threads_proper (struct buffer
*buffer
)
1193 struct inferior_list_entry
*thread
;
1195 buffer_grow_str (buffer
, "<threads>\n");
1197 for (thread
= all_threads
.head
; thread
; thread
= thread
->next
)
1199 ptid_t ptid
= thread_to_gdb_id ((struct thread_info
*)thread
);
1201 int core
= target_core_of_thread (ptid
);
1204 write_ptid (ptid_s
, ptid
);
1208 sprintf (core_s
, "%d", core
);
1209 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1214 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1219 buffer_grow_str0 (buffer
, "</threads>\n");
1222 /* Handle qXfer:threads:read. */
1225 handle_qxfer_threads (const char *annex
,
1226 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1227 ULONGEST offset
, LONGEST len
)
1229 static char *result
= 0;
1230 static unsigned int result_length
= 0;
1232 if (writebuf
!= NULL
)
1235 if (!target_running () || annex
[0] != '\0')
1240 struct buffer buffer
;
1241 /* When asked for data at offset 0, generate everything and store into
1242 'result'. Successive reads will be served off 'result'. */
1246 buffer_init (&buffer
);
1248 handle_qxfer_threads_proper (&buffer
);
1250 result
= buffer_finish (&buffer
);
1251 result_length
= strlen (result
);
1252 buffer_free (&buffer
);
1255 if (offset
>= result_length
)
1257 /* We're out of data. */
1264 if (len
> result_length
- offset
)
1265 len
= result_length
- offset
;
1267 memcpy (readbuf
, result
+ offset
, len
);
1272 /* Handle qXfer:traceframe-info:read. */
1275 handle_qxfer_traceframe_info (const char *annex
,
1276 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1277 ULONGEST offset
, LONGEST len
)
1279 static char *result
= 0;
1280 static unsigned int result_length
= 0;
1282 if (writebuf
!= NULL
)
1285 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1290 struct buffer buffer
;
1292 /* When asked for data at offset 0, generate everything and
1293 store into 'result'. Successive reads will be served off
1297 buffer_init (&buffer
);
1299 traceframe_read_info (current_traceframe
, &buffer
);
1301 result
= buffer_finish (&buffer
);
1302 result_length
= strlen (result
);
1303 buffer_free (&buffer
);
1306 if (offset
>= result_length
)
1308 /* We're out of data. */
1315 if (len
> result_length
- offset
)
1316 len
= result_length
- offset
;
1318 memcpy (readbuf
, result
+ offset
, len
);
1322 /* Handle qXfer:fdpic:read. */
1325 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1326 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1328 if (the_target
->read_loadmap
== NULL
)
1331 if (!target_running ())
1334 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1337 /* Handle qXfer:btrace:read. */
1340 handle_qxfer_btrace (const char *annex
,
1341 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1342 ULONGEST offset
, LONGEST len
)
1344 static struct buffer cache
;
1345 struct thread_info
*thread
;
1348 if (the_target
->read_btrace
== NULL
|| writebuf
!= NULL
)
1351 if (!target_running ())
1354 if (ptid_equal (general_thread
, null_ptid
)
1355 || ptid_equal (general_thread
, minus_one_ptid
))
1357 strcpy (own_buf
, "E.Must select a single thread.");
1361 thread
= find_thread_ptid (general_thread
);
1364 strcpy (own_buf
, "E.No such thread.");
1368 if (thread
->btrace
== NULL
)
1370 strcpy (own_buf
, "E.Btrace not enabled.");
1374 if (strcmp (annex
, "all") == 0)
1375 type
= btrace_read_all
;
1376 else if (strcmp (annex
, "new") == 0)
1377 type
= btrace_read_new
;
1380 strcpy (own_buf
, "E.Bad annex.");
1386 buffer_free (&cache
);
1388 target_read_btrace (thread
->btrace
, &cache
, type
);
1390 else if (offset
> cache
.used_size
)
1392 buffer_free (&cache
);
1396 if (len
> cache
.used_size
- offset
)
1397 len
= cache
.used_size
- offset
;
1399 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1404 static const struct qxfer qxfer_packets
[] =
1406 { "auxv", handle_qxfer_auxv
},
1407 { "btrace", handle_qxfer_btrace
},
1408 { "fdpic", handle_qxfer_fdpic
},
1409 { "features", handle_qxfer_features
},
1410 { "libraries", handle_qxfer_libraries
},
1411 { "libraries-svr4", handle_qxfer_libraries_svr4
},
1412 { "osdata", handle_qxfer_osdata
},
1413 { "siginfo", handle_qxfer_siginfo
},
1414 { "spu", handle_qxfer_spu
},
1415 { "statictrace", handle_qxfer_statictrace
},
1416 { "threads", handle_qxfer_threads
},
1417 { "traceframe-info", handle_qxfer_traceframe_info
},
1421 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1429 if (strncmp (own_buf
, "qXfer:", 6) != 0)
1432 /* Grab the object, r/w and annex. */
1433 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1435 write_enn (own_buf
);
1440 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1443 const struct qxfer
*q
= &qxfer_packets
[i
];
1445 if (strcmp (object
, q
->object
) == 0)
1447 if (strcmp (rw
, "read") == 0)
1449 unsigned char *data
;
1454 /* Grab the offset and length. */
1455 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1457 write_enn (own_buf
);
1461 /* Read one extra byte, as an indicator of whether there is
1463 if (len
> PBUFSIZ
- 2)
1465 data
= malloc (len
+ 1);
1468 write_enn (own_buf
);
1471 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1479 /* Preserve error message. */
1482 write_enn (own_buf
);
1484 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1486 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1491 else if (strcmp (rw
, "write") == 0)
1496 unsigned char *data
;
1498 strcpy (own_buf
, "E00");
1499 data
= malloc (packet_len
- (offset
- own_buf
));
1502 write_enn (own_buf
);
1505 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1506 &ofs
, &len
, data
) < 0)
1509 write_enn (own_buf
);
1513 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1521 /* Preserve error message. */
1524 write_enn (own_buf
);
1526 sprintf (own_buf
, "%x", n
);
1539 /* Table used by the crc32 function to calcuate the checksum. */
1541 static unsigned int crc32_table
[256] =
1544 /* Compute 32 bit CRC from inferior memory.
1546 On success, return 32 bit CRC.
1547 On failure, return (unsigned long long) -1. */
1549 static unsigned long long
1550 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1552 if (!crc32_table
[1])
1554 /* Initialize the CRC table and the decoding table. */
1558 for (i
= 0; i
< 256; i
++)
1560 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1561 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1568 unsigned char byte
= 0;
1570 /* Return failure if memory read fails. */
1571 if (read_inferior_memory (base
, &byte
, 1) != 0)
1572 return (unsigned long long) -1;
1574 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1577 return (unsigned long long) crc
;
1580 /* Handle all of the extended 'q' packets. */
1583 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1585 static struct inferior_list_entry
*thread_ptr
;
1587 /* Reply the current thread id. */
1588 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1591 require_running (own_buf
);
1593 if (!ptid_equal (general_thread
, null_ptid
)
1594 && !ptid_equal (general_thread
, minus_one_ptid
))
1595 gdb_id
= general_thread
;
1598 thread_ptr
= all_threads
.head
;
1599 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1602 sprintf (own_buf
, "QC");
1604 write_ptid (own_buf
, gdb_id
);
1608 if (strcmp ("qSymbol::", own_buf
) == 0)
1610 /* GDB is suggesting new symbols have been loaded. This may
1611 mean a new shared library has been detected as loaded, so
1612 take the opportunity to check if breakpoints we think are
1613 inserted, still are. Note that it isn't guaranteed that
1614 we'll see this when a shared library is loaded, and nor will
1615 we see this for unloads (although breakpoints in unloaded
1616 libraries shouldn't trigger), as GDB may not find symbols for
1617 the library at all. We also re-validate breakpoints when we
1618 see a second GDB breakpoint for the same address, and or when
1619 we access breakpoint shadows. */
1620 validate_breakpoints ();
1622 if (target_supports_tracepoints ())
1623 tracepoint_look_up_symbols ();
1625 if (target_running () && the_target
->look_up_symbols
!= NULL
)
1626 (*the_target
->look_up_symbols
) ();
1628 strcpy (own_buf
, "OK");
1632 if (!disable_packet_qfThreadInfo
)
1634 if (strcmp ("qfThreadInfo", own_buf
) == 0)
1638 require_running (own_buf
);
1639 thread_ptr
= all_threads
.head
;
1642 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1643 write_ptid (own_buf
, gdb_id
);
1644 thread_ptr
= thread_ptr
->next
;
1648 if (strcmp ("qsThreadInfo", own_buf
) == 0)
1652 require_running (own_buf
);
1653 if (thread_ptr
!= NULL
)
1656 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1657 write_ptid (own_buf
, gdb_id
);
1658 thread_ptr
= thread_ptr
->next
;
1663 sprintf (own_buf
, "l");
1669 if (the_target
->read_offsets
!= NULL
1670 && strcmp ("qOffsets", own_buf
) == 0)
1672 CORE_ADDR text
, data
;
1674 require_running (own_buf
);
1675 if (the_target
->read_offsets (&text
, &data
))
1676 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
1677 (long)text
, (long)data
, (long)data
);
1679 write_enn (own_buf
);
1684 /* Protocol features query. */
1685 if (strncmp ("qSupported", own_buf
, 10) == 0
1686 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1688 char *p
= &own_buf
[10];
1689 int gdb_supports_qRelocInsn
= 0;
1691 /* Start processing qSupported packet. */
1692 target_process_qsupported (NULL
);
1694 /* Process each feature being provided by GDB. The first
1695 feature will follow a ':', and latter features will follow
1699 char **qsupported
= NULL
;
1703 /* Two passes, to avoid nested strtok calls in
1704 target_process_qsupported. */
1705 for (p
= strtok (p
+ 1, ";");
1707 p
= strtok (NULL
, ";"))
1710 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
1711 qsupported
[count
- 1] = xstrdup (p
);
1714 for (i
= 0; i
< count
; i
++)
1717 if (strcmp (p
, "multiprocess+") == 0)
1719 /* GDB supports and wants multi-process support if
1721 if (target_supports_multi_process ())
1724 else if (strcmp (p
, "qRelocInsn+") == 0)
1726 /* GDB supports relocate instruction requests. */
1727 gdb_supports_qRelocInsn
= 1;
1730 target_process_qsupported (p
);
1739 "PacketSize=%x;QPassSignals+;QProgramSignals+",
1742 if (the_target
->qxfer_libraries_svr4
!= NULL
)
1743 strcat (own_buf
, ";qXfer:libraries-svr4:read+"
1744 ";augmented-libraries-svr4-read+");
1747 /* We do not have any hook to indicate whether the non-SVR4 target
1748 backend supports qXfer:libraries:read, so always report it. */
1749 strcat (own_buf
, ";qXfer:libraries:read+");
1752 if (the_target
->read_auxv
!= NULL
)
1753 strcat (own_buf
, ";qXfer:auxv:read+");
1755 if (the_target
->qxfer_spu
!= NULL
)
1756 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
1758 if (the_target
->qxfer_siginfo
!= NULL
)
1759 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1761 if (the_target
->read_loadmap
!= NULL
)
1762 strcat (own_buf
, ";qXfer:fdpic:read+");
1764 /* We always report qXfer:features:read, as targets may
1765 install XML files on a subsequent call to arch_setup.
1766 If we reported to GDB on startup that we don't support
1767 qXfer:feature:read at all, we will never be re-queried. */
1768 strcat (own_buf
, ";qXfer:features:read+");
1770 if (transport_is_reliable
)
1771 strcat (own_buf
, ";QStartNoAckMode+");
1773 if (the_target
->qxfer_osdata
!= NULL
)
1774 strcat (own_buf
, ";qXfer:osdata:read+");
1776 if (target_supports_multi_process ())
1777 strcat (own_buf
, ";multiprocess+");
1779 if (target_supports_non_stop ())
1780 strcat (own_buf
, ";QNonStop+");
1782 if (target_supports_disable_randomization ())
1783 strcat (own_buf
, ";QDisableRandomization+");
1785 strcat (own_buf
, ";qXfer:threads:read+");
1787 if (target_supports_tracepoints ())
1789 strcat (own_buf
, ";ConditionalTracepoints+");
1790 strcat (own_buf
, ";TraceStateVariables+");
1791 strcat (own_buf
, ";TracepointSource+");
1792 strcat (own_buf
, ";DisconnectedTracing+");
1793 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
1794 strcat (own_buf
, ";FastTracepoints+");
1795 strcat (own_buf
, ";StaticTracepoints+");
1796 strcat (own_buf
, ";InstallInTrace+");
1797 strcat (own_buf
, ";qXfer:statictrace:read+");
1798 strcat (own_buf
, ";qXfer:traceframe-info:read+");
1799 strcat (own_buf
, ";EnableDisableTracepoints+");
1800 strcat (own_buf
, ";QTBuffer:size+");
1801 strcat (own_buf
, ";tracenz+");
1804 /* Support target-side breakpoint conditions and commands. */
1805 strcat (own_buf
, ";ConditionalBreakpoints+");
1806 strcat (own_buf
, ";BreakpointCommands+");
1808 if (target_supports_agent ())
1809 strcat (own_buf
, ";QAgent+");
1811 if (target_supports_btrace ())
1813 strcat (own_buf
, ";Qbtrace:bts+");
1814 strcat (own_buf
, ";Qbtrace:off+");
1815 strcat (own_buf
, ";qXfer:btrace:read+");
1821 /* Thread-local storage support. */
1822 if (the_target
->get_tls_address
!= NULL
1823 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
1825 char *p
= own_buf
+ 12;
1826 CORE_ADDR parts
[2], address
= 0;
1828 ptid_t ptid
= null_ptid
;
1830 require_running (own_buf
);
1832 for (i
= 0; i
< 3; i
++)
1840 p2
= strchr (p
, ',');
1853 ptid
= read_ptid (p
, NULL
);
1855 decode_address (&parts
[i
- 1], p
, len
);
1859 if (p
!= NULL
|| i
< 3)
1863 struct thread_info
*thread
= find_thread_ptid (ptid
);
1868 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
1874 strcpy (own_buf
, paddress(address
));
1879 write_enn (own_buf
);
1883 /* Otherwise, pretend we do not understand this packet. */
1886 /* Windows OS Thread Information Block address support. */
1887 if (the_target
->get_tib_address
!= NULL
1888 && strncmp ("qGetTIBAddr:", own_buf
, 12) == 0)
1893 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
1895 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
1898 strcpy (own_buf
, paddress(tlb
));
1903 write_enn (own_buf
);
1909 /* Handle "monitor" commands. */
1910 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
1912 char *mon
= malloc (PBUFSIZ
);
1913 int len
= strlen (own_buf
+ 6);
1917 write_enn (own_buf
);
1921 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
1923 write_enn (own_buf
);
1927 mon
[len
/ 2] = '\0';
1931 if (the_target
->handle_monitor_command
== NULL
1932 || (*the_target
->handle_monitor_command
) (mon
) == 0)
1933 /* Default processing. */
1934 handle_monitor_command (mon
, own_buf
);
1940 if (strncmp ("qSearch:memory:", own_buf
,
1941 sizeof ("qSearch:memory:") - 1) == 0)
1943 require_running (own_buf
);
1944 handle_search_memory (own_buf
, packet_len
);
1948 if (strcmp (own_buf
, "qAttached") == 0
1949 || strncmp (own_buf
, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1951 struct process_info
*process
;
1953 if (own_buf
[sizeof ("qAttached") - 1])
1955 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
1956 process
= (struct process_info
*)
1957 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
1961 require_running (own_buf
);
1962 process
= current_process ();
1965 if (process
== NULL
)
1967 write_enn (own_buf
);
1971 strcpy (own_buf
, process
->attached
? "1" : "0");
1975 if (strncmp ("qCRC:", own_buf
, 5) == 0)
1977 /* CRC check (compare-section). */
1981 unsigned long long crc
;
1983 require_running (own_buf
);
1984 comma
= unpack_varlen_hex (own_buf
+ 5, &base
);
1985 if (*comma
++ != ',')
1987 write_enn (own_buf
);
1990 len
= strtoul (comma
, NULL
, 16);
1991 crc
= crc32 (base
, len
, 0xffffffff);
1992 /* Check for memory failure. */
1993 if (crc
== (unsigned long long) -1)
1995 write_enn (own_buf
);
1998 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
2002 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
2005 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
2008 /* Otherwise we didn't know what packet it was. Say we didn't
2013 static void gdb_wants_all_threads_stopped (void);
2015 /* Parse vCont packets. */
2017 handle_v_cont (char *own_buf
)
2021 struct thread_resume
*resume_info
;
2022 struct thread_resume default_action
= {{0}};
2024 /* Count the number of semicolons in the packet. There should be one
2025 for every action. */
2031 p
= strchr (p
, ';');
2034 resume_info
= malloc (n
* sizeof (resume_info
[0]));
2035 if (resume_info
== NULL
)
2043 memset (&resume_info
[i
], 0, sizeof resume_info
[i
]);
2045 if (p
[0] == 's' || p
[0] == 'S')
2046 resume_info
[i
].kind
= resume_step
;
2047 else if (p
[0] == 'r')
2048 resume_info
[i
].kind
= resume_step
;
2049 else if (p
[0] == 'c' || p
[0] == 'C')
2050 resume_info
[i
].kind
= resume_continue
;
2051 else if (p
[0] == 't')
2052 resume_info
[i
].kind
= resume_stop
;
2056 if (p
[0] == 'S' || p
[0] == 'C')
2059 sig
= strtol (p
+ 1, &q
, 16);
2064 if (!gdb_signal_to_host_p (sig
))
2066 resume_info
[i
].sig
= gdb_signal_to_host (sig
);
2068 else if (p
[0] == 'r')
2072 p
= unpack_varlen_hex (p
+ 1, &addr
);
2073 resume_info
[i
].step_range_start
= addr
;
2078 p
= unpack_varlen_hex (p
+ 1, &addr
);
2079 resume_info
[i
].step_range_end
= addr
;
2088 resume_info
[i
].thread
= minus_one_ptid
;
2089 default_action
= resume_info
[i
];
2091 /* Note: we don't increment i here, we'll overwrite this entry
2092 the next time through. */
2094 else if (p
[0] == ':')
2096 ptid_t ptid
= read_ptid (p
+ 1, &q
);
2101 if (p
[0] != ';' && p
[0] != 0)
2104 resume_info
[i
].thread
= ptid
;
2111 resume_info
[i
] = default_action
;
2113 /* `cont_thread' is still used in occasional places in the backend,
2114 to implement single-thread scheduler-locking. Doesn't make sense
2115 to set it if we see a stop request, or a wildcard action (one
2116 with '-1' (all threads), or 'pPID.-1' (all threads of PID)). */
2118 && !(ptid_equal (resume_info
[0].thread
, minus_one_ptid
)
2119 || ptid_get_lwp (resume_info
[0].thread
) == -1)
2120 && resume_info
[0].kind
!= resume_stop
)
2121 cont_thread
= resume_info
[0].thread
;
2123 cont_thread
= minus_one_ptid
;
2124 set_desired_inferior (0);
2129 (*the_target
->resume
) (resume_info
, n
);
2137 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2139 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2140 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2141 current_inferior
->last_status
= last_status
;
2143 /* From the client's perspective, all-stop mode always stops all
2144 threads implicitly (and the target backend has already done
2145 so by now). Tag all threads as "want-stopped", so we don't
2146 resume them implicitly without the client telling us to. */
2147 gdb_wants_all_threads_stopped ();
2148 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2149 disable_async_io ();
2151 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2152 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2153 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2158 write_enn (own_buf
);
2163 /* Attach to a new program. Return 1 if successful, 0 if failure. */
2165 handle_v_attach (char *own_buf
)
2169 pid
= strtol (own_buf
+ 8, NULL
, 16);
2170 if (pid
!= 0 && attach_inferior (pid
) == 0)
2172 /* Don't report shared library events after attaching, even if
2173 some libraries are preloaded. GDB will always poll the
2174 library list. Avoids the "stopped by shared library event"
2175 notice on the GDB side. */
2180 /* In non-stop, we don't send a resume reply. Stop events
2181 will follow up using the normal notification
2186 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2192 write_enn (own_buf
);
2197 /* Run a new program. Return 1 if successful, 0 if failure. */
2199 handle_v_run (char *own_buf
)
2201 char *p
, *next_p
, **new_argv
;
2205 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
2211 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
2212 if (new_argv
== NULL
)
2214 write_enn (own_buf
);
2219 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
2221 next_p
= strchr (p
, ';');
2223 next_p
= p
+ strlen (p
);
2225 if (i
== 0 && p
== next_p
)
2229 /* FIXME: Fail request if out of memory instead of dying. */
2230 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
2231 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
2232 new_argv
[i
][(next_p
- p
) / 2] = '\0';
2241 if (new_argv
[0] == NULL
)
2243 /* GDB didn't specify a program to run. Use the program from the
2244 last run with the new argument list. */
2246 if (program_argv
== NULL
)
2248 write_enn (own_buf
);
2249 freeargv (new_argv
);
2253 new_argv
[0] = strdup (program_argv
[0]);
2254 if (new_argv
[0] == NULL
)
2256 write_enn (own_buf
);
2257 freeargv (new_argv
);
2262 /* Free the old argv and install the new one. */
2263 freeargv (program_argv
);
2264 program_argv
= new_argv
;
2266 start_inferior (program_argv
);
2267 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
2269 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2271 /* In non-stop, sending a resume reply doesn't set the general
2272 thread, but GDB assumes a vRun sets it (this is so GDB can
2273 query which is the main thread of the new inferior. */
2275 general_thread
= last_ptid
;
2281 write_enn (own_buf
);
2286 /* Kill process. Return 1 if successful, 0 if failure. */
2288 handle_v_kill (char *own_buf
)
2291 char *p
= &own_buf
[6];
2293 pid
= strtol (p
, NULL
, 16);
2296 if (pid
!= 0 && kill_inferior (pid
) == 0)
2298 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2299 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
2300 last_ptid
= pid_to_ptid (pid
);
2301 discard_queued_stop_replies (pid
);
2307 write_enn (own_buf
);
2312 /* Handle all of the extended 'v' packets. */
2314 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2316 if (!disable_packet_vCont
)
2318 if (strncmp (own_buf
, "vCont;", 6) == 0)
2320 require_running (own_buf
);
2321 handle_v_cont (own_buf
);
2325 if (strncmp (own_buf
, "vCont?", 6) == 0)
2327 strcpy (own_buf
, "vCont;c;C;s;S;t");
2328 if (target_supports_range_stepping ())
2330 own_buf
= own_buf
+ strlen (own_buf
);
2331 strcpy (own_buf
, ";r");
2337 if (strncmp (own_buf
, "vFile:", 6) == 0
2338 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2341 if (strncmp (own_buf
, "vAttach;", 8) == 0)
2343 if ((!extended_protocol
|| !multi_process
) && target_running ())
2345 fprintf (stderr
, "Already debugging a process\n");
2346 write_enn (own_buf
);
2349 handle_v_attach (own_buf
);
2353 if (strncmp (own_buf
, "vRun;", 5) == 0)
2355 if ((!extended_protocol
|| !multi_process
) && target_running ())
2357 fprintf (stderr
, "Already debugging a process\n");
2358 write_enn (own_buf
);
2361 handle_v_run (own_buf
);
2365 if (strncmp (own_buf
, "vKill;", 6) == 0)
2367 if (!target_running ())
2369 fprintf (stderr
, "No process to kill\n");
2370 write_enn (own_buf
);
2373 handle_v_kill (own_buf
);
2377 if (handle_notif_ack (own_buf
, packet_len
))
2380 /* Otherwise we didn't know what packet it was. Say we didn't
2386 /* Resume inferior and wait for another event. In non-stop mode,
2387 don't really wait here, but return immediatelly to the event
2390 myresume (char *own_buf
, int step
, int sig
)
2392 struct thread_resume resume_info
[2];
2394 int valid_cont_thread
;
2396 set_desired_inferior (0);
2398 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2399 && !ptid_equal (cont_thread
, minus_one_ptid
));
2401 if (step
|| sig
|| valid_cont_thread
)
2403 resume_info
[0].thread
= current_ptid
;
2405 resume_info
[0].kind
= resume_step
;
2407 resume_info
[0].kind
= resume_continue
;
2408 resume_info
[0].sig
= sig
;
2412 if (!valid_cont_thread
)
2414 resume_info
[n
].thread
= minus_one_ptid
;
2415 resume_info
[n
].kind
= resume_continue
;
2416 resume_info
[n
].sig
= 0;
2423 (*the_target
->resume
) (resume_info
, n
);
2429 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2431 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2432 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2434 current_inferior
->last_resume_kind
= resume_stop
;
2435 current_inferior
->last_status
= last_status
;
2438 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2439 disable_async_io ();
2441 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2442 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2443 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2447 /* Callback for for_each_inferior. Make a new stop reply for each
2451 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2453 struct thread_info
*thread
= (struct thread_info
*) entry
;
2455 /* For now, assume targets that don't have this callback also don't
2456 manage the thread's last_status field. */
2457 if (the_target
->thread_stopped
== NULL
)
2459 struct vstop_notif
*new_notif
= xmalloc (sizeof (*new_notif
));
2461 new_notif
->ptid
= entry
->id
;
2462 new_notif
->status
= thread
->last_status
;
2463 /* Pass the last stop reply back to GDB, but don't notify
2465 notif_event_enque (¬if_stop
,
2466 (struct notif_event
*) new_notif
);
2470 if (thread_stopped (thread
))
2474 "Reporting thread %s as already stopped with %s\n",
2475 target_pid_to_str (entry
->id
),
2476 target_waitstatus_to_string (&thread
->last_status
));
2478 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2480 /* Pass the last stop reply back to GDB, but don't notify
2482 queue_stop_reply (entry
->id
, &thread
->last_status
);
2489 /* Set this inferior threads's state as "want-stopped". We won't
2490 resume this thread until the client gives us another action for
2494 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2496 struct thread_info
*thread
= (struct thread_info
*) entry
;
2498 thread
->last_resume_kind
= resume_stop
;
2500 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2502 /* Most threads are stopped implicitly (all-stop); tag that with
2504 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2505 thread
->last_status
.value
.sig
= GDB_SIGNAL_0
;
2509 /* Set all threads' states as "want-stopped". */
2512 gdb_wants_all_threads_stopped (void)
2514 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2517 /* Clear the gdb_detached flag of every process. */
2520 gdb_reattached_process (struct inferior_list_entry
*entry
)
2522 struct process_info
*process
= (struct process_info
*) entry
;
2524 process
->gdb_detached
= 0;
2527 /* Status handler for the '?' packet. */
2530 handle_status (char *own_buf
)
2532 /* GDB is connected, don't forward events to the target anymore. */
2533 for_each_inferior (&all_processes
, gdb_reattached_process
);
2535 /* In non-stop mode, we must send a stop reply for each stopped
2536 thread. In all-stop mode, just send one for the first stopped
2541 discard_queued_stop_replies (-1);
2542 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2544 /* The first is sent immediatly. OK is sent if there is no
2545 stopped thread, which is the same handling of the vStopped
2546 packet (by design). */
2547 notif_write_event (¬if_stop
, own_buf
);
2552 stabilize_threads ();
2553 gdb_wants_all_threads_stopped ();
2555 if (all_threads
.head
)
2557 struct target_waitstatus status
;
2559 status
.kind
= TARGET_WAITKIND_STOPPED
;
2560 status
.value
.sig
= GDB_SIGNAL_TRAP
;
2561 prepare_resume_reply (own_buf
,
2562 all_threads
.head
->id
, &status
);
2565 strcpy (own_buf
, "W00");
2570 gdbserver_version (void)
2572 printf ("GNU gdbserver %s%s\n"
2573 "Copyright (C) 2013 Free Software Foundation, Inc.\n"
2574 "gdbserver is free software, covered by the "
2575 "GNU General Public License.\n"
2576 "This gdbserver was configured as \"%s\"\n",
2577 PKGVERSION
, version
, host_name
);
2581 gdbserver_usage (FILE *stream
)
2583 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2584 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2585 "\tgdbserver [OPTIONS] --multi COMM\n"
2587 "COMM may either be a tty device (for serial debugging), or \n"
2588 "HOST:PORT to listen for a TCP connection.\n"
2591 " --debug Enable general debugging output.\n"
2592 " --remote-debug Enable remote protocol debugging output.\n"
2593 " --version Display version information and exit.\n"
2594 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
2595 " --once Exit after the first connection has "
2597 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
2598 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
2602 gdbserver_show_disableable (FILE *stream
)
2604 fprintf (stream
, "Disableable packets:\n"
2605 " vCont \tAll vCont packets\n"
2606 " qC \tQuerying the current thread\n"
2607 " qfThreadInfo\tThread listing\n"
2608 " Tthread \tPassing the thread specifier in the "
2609 "T stop reply packet\n"
2610 " threads \tAll of the above\n");
2614 #undef require_running
2615 #define require_running(BUF) \
2616 if (!target_running ()) \
2623 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
2625 int pid
= * (int *) args
;
2627 if (ptid_get_pid (entry
->id
) == pid
)
2634 kill_inferior_callback (struct inferior_list_entry
*entry
)
2636 struct process_info
*process
= (struct process_info
*) entry
;
2637 int pid
= ptid_get_pid (process
->head
.id
);
2639 kill_inferior (pid
);
2640 discard_queued_stop_replies (pid
);
2643 /* Callback for for_each_inferior to detach or kill the inferior,
2644 depending on whether we attached to it or not.
2645 We inform the user whether we're detaching or killing the process
2646 as this is only called when gdbserver is about to exit. */
2649 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
2651 struct process_info
*process
= (struct process_info
*) entry
;
2652 int pid
= ptid_get_pid (process
->head
.id
);
2654 if (process
->attached
)
2655 detach_inferior (pid
);
2657 kill_inferior (pid
);
2659 discard_queued_stop_replies (pid
);
2662 /* for_each_inferior callback for detach_or_kill_for_exit to print
2663 the pids of started inferiors. */
2666 print_started_pid (struct inferior_list_entry
*entry
)
2668 struct process_info
*process
= (struct process_info
*) entry
;
2670 if (! process
->attached
)
2672 int pid
= ptid_get_pid (process
->head
.id
);
2673 fprintf (stderr
, " %d", pid
);
2677 /* for_each_inferior callback for detach_or_kill_for_exit to print
2678 the pids of attached inferiors. */
2681 print_attached_pid (struct inferior_list_entry
*entry
)
2683 struct process_info
*process
= (struct process_info
*) entry
;
2685 if (process
->attached
)
2687 int pid
= ptid_get_pid (process
->head
.id
);
2688 fprintf (stderr
, " %d", pid
);
2692 /* Call this when exiting gdbserver with possible inferiors that need
2693 to be killed or detached from. */
2696 detach_or_kill_for_exit (void)
2698 /* First print a list of the inferiors we will be killing/detaching.
2699 This is to assist the user, for example, in case the inferior unexpectedly
2700 dies after we exit: did we screw up or did the inferior exit on its own?
2701 Having this info will save some head-scratching. */
2703 if (have_started_inferiors_p ())
2705 fprintf (stderr
, "Killing process(es):");
2706 for_each_inferior (&all_processes
, print_started_pid
);
2707 fprintf (stderr
, "\n");
2709 if (have_attached_inferiors_p ())
2711 fprintf (stderr
, "Detaching process(es):");
2712 for_each_inferior (&all_processes
, print_attached_pid
);
2713 fprintf (stderr
, "\n");
2716 /* Now we can kill or detach the inferiors. */
2718 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
2722 main (int argc
, char *argv
[])
2726 char *arg_end
, *port
;
2727 char **next_arg
= &argv
[1];
2728 volatile int multi_mode
= 0;
2729 volatile int attach
= 0;
2732 while (*next_arg
!= NULL
&& **next_arg
== '-')
2734 if (strcmp (*next_arg
, "--version") == 0)
2736 gdbserver_version ();
2739 else if (strcmp (*next_arg
, "--help") == 0)
2741 gdbserver_usage (stdout
);
2744 else if (strcmp (*next_arg
, "--attach") == 0)
2746 else if (strcmp (*next_arg
, "--multi") == 0)
2748 else if (strcmp (*next_arg
, "--wrapper") == 0)
2752 wrapper_argv
= next_arg
;
2753 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
2756 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
2758 gdbserver_usage (stderr
);
2762 /* Consume the "--". */
2765 else if (strcmp (*next_arg
, "--debug") == 0)
2767 else if (strcmp (*next_arg
, "--remote-debug") == 0)
2769 else if (strcmp (*next_arg
, "--disable-packet") == 0)
2771 gdbserver_show_disableable (stdout
);
2774 else if (strncmp (*next_arg
,
2775 "--disable-packet=",
2776 sizeof ("--disable-packet=") - 1) == 0)
2778 char *packets
, *tok
;
2780 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
2781 for (tok
= strtok (packets
, ",");
2783 tok
= strtok (NULL
, ","))
2785 if (strcmp ("vCont", tok
) == 0)
2786 disable_packet_vCont
= 1;
2787 else if (strcmp ("Tthread", tok
) == 0)
2788 disable_packet_Tthread
= 1;
2789 else if (strcmp ("qC", tok
) == 0)
2790 disable_packet_qC
= 1;
2791 else if (strcmp ("qfThreadInfo", tok
) == 0)
2792 disable_packet_qfThreadInfo
= 1;
2793 else if (strcmp ("threads", tok
) == 0)
2795 disable_packet_vCont
= 1;
2796 disable_packet_Tthread
= 1;
2797 disable_packet_qC
= 1;
2798 disable_packet_qfThreadInfo
= 1;
2802 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
2804 gdbserver_show_disableable (stderr
);
2809 else if (strcmp (*next_arg
, "-") == 0)
2811 /* "-" specifies a stdio connection and is a form of port
2813 *next_arg
= STDIO_CONNECTION_NAME
;
2816 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
2817 disable_randomization
= 1;
2818 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
2819 disable_randomization
= 0;
2820 else if (strcmp (*next_arg
, "--once") == 0)
2824 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
2832 if (setjmp (toplevel
))
2834 fprintf (stderr
, "Exiting\n");
2840 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
2842 gdbserver_usage (stderr
);
2846 /* We need to know whether the remote connection is stdio before
2847 starting the inferior. Inferiors created in this scenario have
2848 stdin,stdout redirected. So do this here before we call
2850 remote_prepare (port
);
2855 /* --attach used to come after PORT, so allow it there for
2857 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
2864 && (*next_arg
== NULL
2865 || (*next_arg
)[0] == '\0'
2866 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
2868 || next_arg
[1] != NULL
))
2873 gdbserver_usage (stderr
);
2877 initialize_async_io ();
2879 initialize_event_loop ();
2880 if (target_supports_tracepoints ())
2881 initialize_tracepoint ();
2883 own_buf
= xmalloc (PBUFSIZ
+ 1);
2884 mem_buf
= xmalloc (PBUFSIZ
);
2886 if (pid
== 0 && *next_arg
!= NULL
)
2890 n
= argc
- (next_arg
- argv
);
2891 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
2892 for (i
= 0; i
< n
; i
++)
2893 program_argv
[i
] = xstrdup (next_arg
[i
]);
2894 program_argv
[i
] = NULL
;
2896 /* Wait till we are at first instruction in program. */
2897 start_inferior (program_argv
);
2899 /* We are now (hopefully) stopped at the first instruction of
2900 the target process. This assumes that the target process was
2901 successfully created. */
2905 if (attach_inferior (pid
) == -1)
2906 error ("Attaching not supported on this target");
2908 /* Otherwise succeeded. */
2912 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2913 last_status
.value
.integer
= 0;
2914 last_ptid
= minus_one_ptid
;
2917 initialize_notif ();
2919 /* Don't report shared library events on the initial connection,
2920 even if some libraries are preloaded. Avoids the "stopped by
2921 shared library event" notice on gdb side. */
2924 if (setjmp (toplevel
))
2926 /* If something fails and longjmps while detaching or killing
2927 inferiors, we'd end up here again, stuck in an infinite loop
2928 trap. Be sure that if that happens, we exit immediately
2930 if (setjmp (toplevel
) == 0)
2931 detach_or_kill_for_exit ();
2933 fprintf (stderr
, "Detach or kill failed. Exiting\n");
2937 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2938 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2943 if (!was_running
&& !multi_mode
)
2945 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
2953 /* Be sure we're out of tfind mode. */
2954 current_traceframe
= -1;
2958 if (setjmp (toplevel
) != 0)
2960 /* An error occurred. */
2961 if (response_needed
)
2963 write_enn (own_buf
);
2968 /* Wait for events. This will return when all event sources are
2969 removed from the event loop. */
2970 start_event_loop ();
2972 /* If an exit was requested (using the "monitor exit" command),
2973 terminate now. The only other way to get here is for
2974 getpkt to fail; close the connection and reopen it at the
2977 if (exit_requested
|| run_once
)
2979 /* If something fails and longjmps while detaching or
2980 killing inferiors, we'd end up here again, stuck in an
2981 infinite loop trap. Be sure that if that happens, we
2982 exit immediately instead. */
2983 if (setjmp (toplevel
) == 0)
2985 detach_or_kill_for_exit ();
2990 fprintf (stderr
, "Detach or kill failed. Exiting\n");
2996 "Remote side has terminated connection. "
2997 "GDBserver will reopen the connection.\n");
3001 if (disconnected_tracing
)
3003 /* Try to enable non-stop/async mode, so we we can both
3004 wait for an async socket accept, and handle async
3005 target events simultaneously. There's also no point
3006 either in having the target always stop all threads,
3007 when we're going to pass signals down without
3011 if (start_non_stop (1))
3014 /* Detaching implicitly resumes all threads; simply
3015 disconnecting does not. */
3021 "Disconnected tracing disabled; stopping trace run.\n");
3028 /* Process options coming from Z packets for *point at address
3029 POINT_ADDR. PACKET is the packet buffer. *PACKET is updated
3030 to point to the first char after the last processed option. */
3033 process_point_options (CORE_ADDR point_addr
, char **packet
)
3035 char *dataptr
= *packet
;
3038 /* Check if data has the correct format. */
3039 if (*dataptr
!= ';')
3046 if (*dataptr
== ';')
3049 if (*dataptr
== 'X')
3051 /* Conditional expression. */
3053 fprintf (stderr
, "Found breakpoint condition.\n");
3054 add_breakpoint_condition (point_addr
, &dataptr
);
3056 else if (strncmp (dataptr
, "cmds:", strlen ("cmds:")) == 0)
3058 dataptr
+= strlen ("cmds:");
3060 fprintf (stderr
, "Found breakpoint commands %s.\n", dataptr
);
3061 persist
= (*dataptr
== '1');
3063 add_breakpoint_commands (point_addr
, &dataptr
, persist
);
3067 fprintf (stderr
, "Unknown token %c, ignoring.\n",
3069 /* Skip tokens until we find one that we recognize. */
3070 while (*dataptr
&& *dataptr
!= ';')
3077 /* Event loop callback that handles a serial event. The first byte in
3078 the serial buffer gets us here. We expect characters to arrive at
3079 a brisk pace, so we read the rest of the packet with a blocking
3083 process_serial_event (void)
3094 int new_packet_len
= -1;
3096 /* Used to decide when gdbserver should exit in
3097 multi-mode/remote. */
3098 static int have_ran
= 0;
3101 have_ran
= target_running ();
3103 disable_async_io ();
3105 response_needed
= 0;
3106 packet_len
= getpkt (own_buf
);
3107 if (packet_len
<= 0)
3110 /* Force an event loop break. */
3113 response_needed
= 1;
3120 handle_query (own_buf
, packet_len
, &new_packet_len
);
3123 handle_general_set (own_buf
);
3126 require_running (own_buf
);
3131 pid
= strtol (&own_buf
[i
], NULL
, 16);
3134 pid
= ptid_get_pid (current_ptid
);
3136 if ((tracing
&& disconnected_tracing
) || any_persistent_commands ())
3138 struct thread_resume resume_info
;
3139 struct process_info
*process
= find_process_pid (pid
);
3141 if (process
== NULL
)
3143 write_enn (own_buf
);
3147 if (tracing
&& disconnected_tracing
)
3149 "Disconnected tracing in effect, "
3150 "leaving gdbserver attached to the process\n");
3152 if (any_persistent_commands ())
3154 "Persistent commands are present, "
3155 "leaving gdbserver attached to the process\n");
3157 /* Make sure we're in non-stop/async mode, so we we can both
3158 wait for an async socket accept, and handle async target
3159 events simultaneously. There's also no point either in
3160 having the target stop all threads, when we're going to
3161 pass signals down without informing GDB. */
3165 fprintf (stderr
, "Forcing non-stop mode\n");
3171 process
->gdb_detached
= 1;
3173 /* Detaching implicitly resumes all threads. */
3174 resume_info
.thread
= minus_one_ptid
;
3175 resume_info
.kind
= resume_continue
;
3176 resume_info
.sig
= 0;
3177 (*the_target
->resume
) (&resume_info
, 1);
3180 break; /* from switch/case */
3183 fprintf (stderr
, "Detaching from process %d\n", pid
);
3185 if (detach_inferior (pid
) != 0)
3186 write_enn (own_buf
);
3189 discard_queued_stop_replies (pid
);
3192 if (extended_protocol
)
3194 /* Treat this like a normal program exit. */
3195 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3196 last_status
.value
.integer
= 0;
3197 last_ptid
= pid_to_ptid (pid
);
3199 current_inferior
= NULL
;
3206 /* If we are attached, then we can exit. Otherwise, we
3207 need to hang around doing nothing, until the child is
3209 join_inferior (pid
);
3215 extended_protocol
= 1;
3219 handle_status (own_buf
);
3222 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
3224 ptid_t gdb_id
, thread_id
;
3227 require_running (own_buf
);
3229 gdb_id
= read_ptid (&own_buf
[2], NULL
);
3231 pid
= ptid_get_pid (gdb_id
);
3233 if (ptid_equal (gdb_id
, null_ptid
)
3234 || ptid_equal (gdb_id
, minus_one_ptid
))
3235 thread_id
= null_ptid
;
3237 && ptid_equal (pid_to_ptid (pid
),
3240 struct thread_info
*thread
=
3241 (struct thread_info
*) find_inferior (&all_threads
,
3246 write_enn (own_buf
);
3250 thread_id
= ((struct inferior_list_entry
*)thread
)->id
;
3254 thread_id
= gdb_id_to_thread_id (gdb_id
);
3255 if (ptid_equal (thread_id
, null_ptid
))
3257 write_enn (own_buf
);
3262 if (own_buf
[1] == 'g')
3264 if (ptid_equal (thread_id
, null_ptid
))
3266 /* GDB is telling us to choose any thread. Check if
3267 the currently selected thread is still valid. If
3268 it is not, select the first available. */
3269 struct thread_info
*thread
=
3270 (struct thread_info
*) find_inferior_id (&all_threads
,
3273 thread_id
= all_threads
.head
->id
;
3276 general_thread
= thread_id
;
3277 set_desired_inferior (1);
3279 else if (own_buf
[1] == 'c')
3280 cont_thread
= thread_id
;
3286 /* Silently ignore it so that gdb can extend the protocol
3287 without compatibility headaches. */
3292 require_running (own_buf
);
3293 if (current_traceframe
>= 0)
3295 struct regcache
*regcache
3296 = new_register_cache (current_target_desc ());
3298 if (fetch_traceframe_registers (current_traceframe
,
3300 registers_to_string (regcache
, own_buf
);
3302 write_enn (own_buf
);
3303 free_register_cache (regcache
);
3307 struct regcache
*regcache
;
3309 set_desired_inferior (1);
3310 regcache
= get_thread_regcache (current_inferior
, 1);
3311 registers_to_string (regcache
, own_buf
);
3315 require_running (own_buf
);
3316 if (current_traceframe
>= 0)
3317 write_enn (own_buf
);
3320 struct regcache
*regcache
;
3322 set_desired_inferior (1);
3323 regcache
= get_thread_regcache (current_inferior
, 1);
3324 registers_from_string (regcache
, &own_buf
[1]);
3329 require_running (own_buf
);
3330 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
3331 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
3333 write_enn (own_buf
);
3335 convert_int_to_ascii (mem_buf
, own_buf
, res
);
3338 require_running (own_buf
);
3339 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
3340 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
3343 write_enn (own_buf
);
3346 require_running (own_buf
);
3347 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
3348 &mem_addr
, &len
, &mem_buf
) < 0
3349 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
3350 write_enn (own_buf
);
3355 require_running (own_buf
);
3356 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3357 if (gdb_signal_to_host_p (sig
))
3358 signal
= gdb_signal_to_host (sig
);
3361 myresume (own_buf
, 0, signal
);
3364 require_running (own_buf
);
3365 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3366 if (gdb_signal_to_host_p (sig
))
3367 signal
= gdb_signal_to_host (sig
);
3370 myresume (own_buf
, 1, signal
);
3373 require_running (own_buf
);
3375 myresume (own_buf
, 0, signal
);
3378 require_running (own_buf
);
3380 myresume (own_buf
, 1, signal
);
3382 case 'Z': /* insert_ ... */
3384 case 'z': /* remove_ ... */
3389 char type
= own_buf
[1];
3391 const int insert
= ch
== 'Z';
3392 char *p
= &own_buf
[3];
3394 p
= unpack_varlen_hex (p
, &addr
);
3395 len
= strtol (p
+ 1, &dataptr
, 16);
3397 /* Default to unrecognized/unsupported. */
3401 case '0': /* software-breakpoint */
3402 case '1': /* hardware-breakpoint */
3403 case '2': /* write watchpoint */
3404 case '3': /* read watchpoint */
3405 case '4': /* access watchpoint */
3406 require_running (own_buf
);
3407 if (insert
&& the_target
->insert_point
!= NULL
)
3409 /* Insert the breakpoint. If it is already inserted, nothing
3411 res
= (*the_target
->insert_point
) (type
, addr
, len
);
3413 /* GDB may have sent us a list of *point parameters to be
3414 evaluated on the target's side. Read such list here. If we
3415 already have a list of parameters, GDB is telling us to drop
3416 that list and use this one instead. */
3417 if (!res
&& (type
== '0' || type
== '1'))
3419 /* Remove previous conditions. */
3420 clear_gdb_breakpoint_conditions (addr
);
3421 process_point_options (addr
, &dataptr
);
3424 else if (!insert
&& the_target
->remove_point
!= NULL
)
3425 res
= (*the_target
->remove_point
) (type
, addr
, len
);
3437 write_enn (own_buf
);
3441 response_needed
= 0;
3442 if (!target_running ())
3443 /* The packet we received doesn't make sense - but we can't
3444 reply to it, either. */
3447 fprintf (stderr
, "Killing all inferiors\n");
3448 for_each_inferior (&all_processes
, kill_inferior_callback
);
3450 /* When using the extended protocol, we wait with no program
3451 running. The traditional protocol will exit instead. */
3452 if (extended_protocol
)
3454 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3455 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
3463 ptid_t gdb_id
, thread_id
;
3465 require_running (own_buf
);
3467 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3468 thread_id
= gdb_id_to_thread_id (gdb_id
);
3469 if (ptid_equal (thread_id
, null_ptid
))
3471 write_enn (own_buf
);
3475 if (mythread_alive (thread_id
))
3478 write_enn (own_buf
);
3482 response_needed
= 0;
3484 /* Restarting the inferior is only supported in the extended
3486 if (extended_protocol
)
3488 if (target_running ())
3489 for_each_inferior (&all_processes
,
3490 kill_inferior_callback
);
3491 fprintf (stderr
, "GDBserver restarting\n");
3493 /* Wait till we are at 1st instruction in prog. */
3494 if (program_argv
!= NULL
)
3495 start_inferior (program_argv
);
3498 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3499 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
3505 /* It is a request we don't understand. Respond with an
3506 empty packet so that gdb knows that we don't support this
3512 /* Extended (long) request. */
3513 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
3517 /* It is a request we don't understand. Respond with an empty
3518 packet so that gdb knows that we don't support this
3524 if (new_packet_len
!= -1)
3525 putpkt_binary (own_buf
, new_packet_len
);
3529 response_needed
= 0;
3531 if (!extended_protocol
&& have_ran
&& !target_running ())
3533 /* In non-stop, defer exiting until GDB had a chance to query
3534 the whole vStopped list (until it gets an OK). */
3535 if (QUEUE_is_empty (notif_event_p
, notif_stop
.queue
))
3537 fprintf (stderr
, "GDBserver exiting\n");
3549 /* Event-loop callback for serial events. */
3552 handle_serial_event (int err
, gdb_client_data client_data
)
3555 fprintf (stderr
, "handling possible serial event\n");
3557 /* Really handle it. */
3558 if (process_serial_event () < 0)
3561 /* Be sure to not change the selected inferior behind GDB's back.
3562 Important in the non-stop mode asynchronous protocol. */
3563 set_desired_inferior (1);
3568 /* Event-loop callback for target events. */
3571 handle_target_event (int err
, gdb_client_data client_data
)
3574 fprintf (stderr
, "handling possible target event\n");
3576 last_ptid
= mywait (minus_one_ptid
, &last_status
,
3579 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3581 int pid
= ptid_get_pid (last_ptid
);
3582 struct process_info
*process
= find_process_pid (pid
);
3583 int forward_event
= !gdb_connected () || process
->gdb_detached
;
3585 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3586 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3588 mark_breakpoints_out (process
);
3589 mourn_inferior (process
);
3593 /* We're reporting this thread as stopped. Update its
3594 "want-stopped" state to what the client wants, until it
3595 gets a new resume action. */
3596 current_inferior
->last_resume_kind
= resume_stop
;
3597 current_inferior
->last_status
= last_status
;
3602 if (!target_running ())
3604 /* The last process exited. We're done. */
3608 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
3610 /* A thread stopped with a signal, but gdb isn't
3611 connected to handle it. Pass it down to the
3612 inferior, as if it wasn't being traced. */
3613 struct thread_resume resume_info
;
3617 "GDB not connected; forwarding event %d for [%s]\n",
3618 (int) last_status
.kind
,
3619 target_pid_to_str (last_ptid
));
3621 resume_info
.thread
= last_ptid
;
3622 resume_info
.kind
= resume_continue
;
3623 resume_info
.sig
= gdb_signal_to_host (last_status
.value
.sig
);
3624 (*the_target
->resume
) (&resume_info
, 1);
3626 else if (debug_threads
)
3627 fprintf (stderr
, "GDB not connected; ignoring event %d for [%s]\n",
3628 (int) last_status
.kind
,
3629 target_pid_to_str (last_ptid
));
3633 struct vstop_notif
*vstop_notif
3634 = xmalloc (sizeof (struct vstop_notif
));
3636 vstop_notif
->status
= last_status
;
3637 vstop_notif
->ptid
= last_ptid
;
3638 /* Push Stop notification. */
3639 notif_push (¬if_stop
,
3640 (struct notif_event
*) vstop_notif
);
3644 /* Be sure to not change the selected inferior behind GDB's back.
3645 Important in the non-stop mode asynchronous protocol. */
3646 set_desired_inferior (1);