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
))
2475 = target_waitstatus_to_string (&thread
->last_status
);
2478 "Reporting thread %s as already stopped with %s\n",
2479 target_pid_to_str (entry
->id
),
2482 xfree (status_string
);
2485 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2487 /* Pass the last stop reply back to GDB, but don't notify
2489 queue_stop_reply (entry
->id
, &thread
->last_status
);
2496 /* Set this inferior threads's state as "want-stopped". We won't
2497 resume this thread until the client gives us another action for
2501 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2503 struct thread_info
*thread
= (struct thread_info
*) entry
;
2505 thread
->last_resume_kind
= resume_stop
;
2507 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2509 /* Most threads are stopped implicitly (all-stop); tag that with
2511 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2512 thread
->last_status
.value
.sig
= GDB_SIGNAL_0
;
2516 /* Set all threads' states as "want-stopped". */
2519 gdb_wants_all_threads_stopped (void)
2521 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2524 /* Clear the gdb_detached flag of every process. */
2527 gdb_reattached_process (struct inferior_list_entry
*entry
)
2529 struct process_info
*process
= (struct process_info
*) entry
;
2531 process
->gdb_detached
= 0;
2534 /* Status handler for the '?' packet. */
2537 handle_status (char *own_buf
)
2539 /* GDB is connected, don't forward events to the target anymore. */
2540 for_each_inferior (&all_processes
, gdb_reattached_process
);
2542 /* In non-stop mode, we must send a stop reply for each stopped
2543 thread. In all-stop mode, just send one for the first stopped
2548 discard_queued_stop_replies (-1);
2549 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2551 /* The first is sent immediatly. OK is sent if there is no
2552 stopped thread, which is the same handling of the vStopped
2553 packet (by design). */
2554 notif_write_event (¬if_stop
, own_buf
);
2559 stabilize_threads ();
2560 gdb_wants_all_threads_stopped ();
2562 if (all_threads
.head
)
2564 struct target_waitstatus status
;
2566 status
.kind
= TARGET_WAITKIND_STOPPED
;
2567 status
.value
.sig
= GDB_SIGNAL_TRAP
;
2568 prepare_resume_reply (own_buf
,
2569 all_threads
.head
->id
, &status
);
2572 strcpy (own_buf
, "W00");
2577 gdbserver_version (void)
2579 printf ("GNU gdbserver %s%s\n"
2580 "Copyright (C) 2013 Free Software Foundation, Inc.\n"
2581 "gdbserver is free software, covered by the "
2582 "GNU General Public License.\n"
2583 "This gdbserver was configured as \"%s\"\n",
2584 PKGVERSION
, version
, host_name
);
2588 gdbserver_usage (FILE *stream
)
2590 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2591 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2592 "\tgdbserver [OPTIONS] --multi COMM\n"
2594 "COMM may either be a tty device (for serial debugging), or \n"
2595 "HOST:PORT to listen for a TCP connection.\n"
2598 " --debug Enable general debugging output.\n"
2599 " --remote-debug Enable remote protocol debugging output.\n"
2600 " --version Display version information and exit.\n"
2601 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
2602 " --once Exit after the first connection has "
2604 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
2605 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
2609 gdbserver_show_disableable (FILE *stream
)
2611 fprintf (stream
, "Disableable packets:\n"
2612 " vCont \tAll vCont packets\n"
2613 " qC \tQuerying the current thread\n"
2614 " qfThreadInfo\tThread listing\n"
2615 " Tthread \tPassing the thread specifier in the "
2616 "T stop reply packet\n"
2617 " threads \tAll of the above\n");
2621 #undef require_running
2622 #define require_running(BUF) \
2623 if (!target_running ()) \
2630 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
2632 int pid
= * (int *) args
;
2634 if (ptid_get_pid (entry
->id
) == pid
)
2641 kill_inferior_callback (struct inferior_list_entry
*entry
)
2643 struct process_info
*process
= (struct process_info
*) entry
;
2644 int pid
= ptid_get_pid (process
->head
.id
);
2646 kill_inferior (pid
);
2647 discard_queued_stop_replies (pid
);
2650 /* Callback for for_each_inferior to detach or kill the inferior,
2651 depending on whether we attached to it or not.
2652 We inform the user whether we're detaching or killing the process
2653 as this is only called when gdbserver is about to exit. */
2656 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
2658 struct process_info
*process
= (struct process_info
*) entry
;
2659 int pid
= ptid_get_pid (process
->head
.id
);
2661 if (process
->attached
)
2662 detach_inferior (pid
);
2664 kill_inferior (pid
);
2666 discard_queued_stop_replies (pid
);
2669 /* for_each_inferior callback for detach_or_kill_for_exit to print
2670 the pids of started inferiors. */
2673 print_started_pid (struct inferior_list_entry
*entry
)
2675 struct process_info
*process
= (struct process_info
*) entry
;
2677 if (! process
->attached
)
2679 int pid
= ptid_get_pid (process
->head
.id
);
2680 fprintf (stderr
, " %d", pid
);
2684 /* for_each_inferior callback for detach_or_kill_for_exit to print
2685 the pids of attached inferiors. */
2688 print_attached_pid (struct inferior_list_entry
*entry
)
2690 struct process_info
*process
= (struct process_info
*) entry
;
2692 if (process
->attached
)
2694 int pid
= ptid_get_pid (process
->head
.id
);
2695 fprintf (stderr
, " %d", pid
);
2699 /* Call this when exiting gdbserver with possible inferiors that need
2700 to be killed or detached from. */
2703 detach_or_kill_for_exit (void)
2705 /* First print a list of the inferiors we will be killing/detaching.
2706 This is to assist the user, for example, in case the inferior unexpectedly
2707 dies after we exit: did we screw up or did the inferior exit on its own?
2708 Having this info will save some head-scratching. */
2710 if (have_started_inferiors_p ())
2712 fprintf (stderr
, "Killing process(es):");
2713 for_each_inferior (&all_processes
, print_started_pid
);
2714 fprintf (stderr
, "\n");
2716 if (have_attached_inferiors_p ())
2718 fprintf (stderr
, "Detaching process(es):");
2719 for_each_inferior (&all_processes
, print_attached_pid
);
2720 fprintf (stderr
, "\n");
2723 /* Now we can kill or detach the inferiors. */
2725 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
2729 main (int argc
, char *argv
[])
2733 char *arg_end
, *port
;
2734 char **next_arg
= &argv
[1];
2735 volatile int multi_mode
= 0;
2736 volatile int attach
= 0;
2739 while (*next_arg
!= NULL
&& **next_arg
== '-')
2741 if (strcmp (*next_arg
, "--version") == 0)
2743 gdbserver_version ();
2746 else if (strcmp (*next_arg
, "--help") == 0)
2748 gdbserver_usage (stdout
);
2751 else if (strcmp (*next_arg
, "--attach") == 0)
2753 else if (strcmp (*next_arg
, "--multi") == 0)
2755 else if (strcmp (*next_arg
, "--wrapper") == 0)
2759 wrapper_argv
= next_arg
;
2760 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
2763 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
2765 gdbserver_usage (stderr
);
2769 /* Consume the "--". */
2772 else if (strcmp (*next_arg
, "--debug") == 0)
2774 else if (strcmp (*next_arg
, "--remote-debug") == 0)
2776 else if (strcmp (*next_arg
, "--disable-packet") == 0)
2778 gdbserver_show_disableable (stdout
);
2781 else if (strncmp (*next_arg
,
2782 "--disable-packet=",
2783 sizeof ("--disable-packet=") - 1) == 0)
2785 char *packets
, *tok
;
2787 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
2788 for (tok
= strtok (packets
, ",");
2790 tok
= strtok (NULL
, ","))
2792 if (strcmp ("vCont", tok
) == 0)
2793 disable_packet_vCont
= 1;
2794 else if (strcmp ("Tthread", tok
) == 0)
2795 disable_packet_Tthread
= 1;
2796 else if (strcmp ("qC", tok
) == 0)
2797 disable_packet_qC
= 1;
2798 else if (strcmp ("qfThreadInfo", tok
) == 0)
2799 disable_packet_qfThreadInfo
= 1;
2800 else if (strcmp ("threads", tok
) == 0)
2802 disable_packet_vCont
= 1;
2803 disable_packet_Tthread
= 1;
2804 disable_packet_qC
= 1;
2805 disable_packet_qfThreadInfo
= 1;
2809 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
2811 gdbserver_show_disableable (stderr
);
2816 else if (strcmp (*next_arg
, "-") == 0)
2818 /* "-" specifies a stdio connection and is a form of port
2820 *next_arg
= STDIO_CONNECTION_NAME
;
2823 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
2824 disable_randomization
= 1;
2825 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
2826 disable_randomization
= 0;
2827 else if (strcmp (*next_arg
, "--once") == 0)
2831 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
2839 if (setjmp (toplevel
))
2841 fprintf (stderr
, "Exiting\n");
2847 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
2849 gdbserver_usage (stderr
);
2853 /* We need to know whether the remote connection is stdio before
2854 starting the inferior. Inferiors created in this scenario have
2855 stdin,stdout redirected. So do this here before we call
2857 remote_prepare (port
);
2862 /* --attach used to come after PORT, so allow it there for
2864 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
2871 && (*next_arg
== NULL
2872 || (*next_arg
)[0] == '\0'
2873 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
2875 || next_arg
[1] != NULL
))
2880 gdbserver_usage (stderr
);
2884 initialize_async_io ();
2886 initialize_event_loop ();
2887 if (target_supports_tracepoints ())
2888 initialize_tracepoint ();
2890 own_buf
= xmalloc (PBUFSIZ
+ 1);
2891 mem_buf
= xmalloc (PBUFSIZ
);
2893 if (pid
== 0 && *next_arg
!= NULL
)
2897 n
= argc
- (next_arg
- argv
);
2898 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
2899 for (i
= 0; i
< n
; i
++)
2900 program_argv
[i
] = xstrdup (next_arg
[i
]);
2901 program_argv
[i
] = NULL
;
2903 /* Wait till we are at first instruction in program. */
2904 start_inferior (program_argv
);
2906 /* We are now (hopefully) stopped at the first instruction of
2907 the target process. This assumes that the target process was
2908 successfully created. */
2912 if (attach_inferior (pid
) == -1)
2913 error ("Attaching not supported on this target");
2915 /* Otherwise succeeded. */
2919 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2920 last_status
.value
.integer
= 0;
2921 last_ptid
= minus_one_ptid
;
2924 initialize_notif ();
2926 /* Don't report shared library events on the initial connection,
2927 even if some libraries are preloaded. Avoids the "stopped by
2928 shared library event" notice on gdb side. */
2931 if (setjmp (toplevel
))
2933 /* If something fails and longjmps while detaching or killing
2934 inferiors, we'd end up here again, stuck in an infinite loop
2935 trap. Be sure that if that happens, we exit immediately
2937 if (setjmp (toplevel
) == 0)
2938 detach_or_kill_for_exit ();
2940 fprintf (stderr
, "Detach or kill failed. Exiting\n");
2944 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2945 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2950 if (!was_running
&& !multi_mode
)
2952 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
2960 /* Be sure we're out of tfind mode. */
2961 current_traceframe
= -1;
2965 if (setjmp (toplevel
) != 0)
2967 /* An error occurred. */
2968 if (response_needed
)
2970 write_enn (own_buf
);
2975 /* Wait for events. This will return when all event sources are
2976 removed from the event loop. */
2977 start_event_loop ();
2979 /* If an exit was requested (using the "monitor exit" command),
2980 terminate now. The only other way to get here is for
2981 getpkt to fail; close the connection and reopen it at the
2984 if (exit_requested
|| run_once
)
2986 /* If something fails and longjmps while detaching or
2987 killing inferiors, we'd end up here again, stuck in an
2988 infinite loop trap. Be sure that if that happens, we
2989 exit immediately instead. */
2990 if (setjmp (toplevel
) == 0)
2992 detach_or_kill_for_exit ();
2997 fprintf (stderr
, "Detach or kill failed. Exiting\n");
3003 "Remote side has terminated connection. "
3004 "GDBserver will reopen the connection.\n");
3008 if (disconnected_tracing
)
3010 /* Try to enable non-stop/async mode, so we we can both
3011 wait for an async socket accept, and handle async
3012 target events simultaneously. There's also no point
3013 either in having the target always stop all threads,
3014 when we're going to pass signals down without
3018 if (start_non_stop (1))
3021 /* Detaching implicitly resumes all threads; simply
3022 disconnecting does not. */
3028 "Disconnected tracing disabled; stopping trace run.\n");
3035 /* Process options coming from Z packets for *point at address
3036 POINT_ADDR. PACKET is the packet buffer. *PACKET is updated
3037 to point to the first char after the last processed option. */
3040 process_point_options (CORE_ADDR point_addr
, char **packet
)
3042 char *dataptr
= *packet
;
3045 /* Check if data has the correct format. */
3046 if (*dataptr
!= ';')
3053 if (*dataptr
== ';')
3056 if (*dataptr
== 'X')
3058 /* Conditional expression. */
3060 fprintf (stderr
, "Found breakpoint condition.\n");
3061 add_breakpoint_condition (point_addr
, &dataptr
);
3063 else if (strncmp (dataptr
, "cmds:", strlen ("cmds:")) == 0)
3065 dataptr
+= strlen ("cmds:");
3067 fprintf (stderr
, "Found breakpoint commands %s.\n", dataptr
);
3068 persist
= (*dataptr
== '1');
3070 add_breakpoint_commands (point_addr
, &dataptr
, persist
);
3074 fprintf (stderr
, "Unknown token %c, ignoring.\n",
3076 /* Skip tokens until we find one that we recognize. */
3077 while (*dataptr
&& *dataptr
!= ';')
3084 /* Event loop callback that handles a serial event. The first byte in
3085 the serial buffer gets us here. We expect characters to arrive at
3086 a brisk pace, so we read the rest of the packet with a blocking
3090 process_serial_event (void)
3101 int new_packet_len
= -1;
3103 /* Used to decide when gdbserver should exit in
3104 multi-mode/remote. */
3105 static int have_ran
= 0;
3108 have_ran
= target_running ();
3110 disable_async_io ();
3112 response_needed
= 0;
3113 packet_len
= getpkt (own_buf
);
3114 if (packet_len
<= 0)
3117 /* Force an event loop break. */
3120 response_needed
= 1;
3127 handle_query (own_buf
, packet_len
, &new_packet_len
);
3130 handle_general_set (own_buf
);
3133 require_running (own_buf
);
3138 pid
= strtol (&own_buf
[i
], NULL
, 16);
3141 pid
= ptid_get_pid (current_ptid
);
3143 if ((tracing
&& disconnected_tracing
) || any_persistent_commands ())
3145 struct thread_resume resume_info
;
3146 struct process_info
*process
= find_process_pid (pid
);
3148 if (process
== NULL
)
3150 write_enn (own_buf
);
3154 if (tracing
&& disconnected_tracing
)
3156 "Disconnected tracing in effect, "
3157 "leaving gdbserver attached to the process\n");
3159 if (any_persistent_commands ())
3161 "Persistent commands are present, "
3162 "leaving gdbserver attached to the process\n");
3164 /* Make sure we're in non-stop/async mode, so we we can both
3165 wait for an async socket accept, and handle async target
3166 events simultaneously. There's also no point either in
3167 having the target stop all threads, when we're going to
3168 pass signals down without informing GDB. */
3172 fprintf (stderr
, "Forcing non-stop mode\n");
3178 process
->gdb_detached
= 1;
3180 /* Detaching implicitly resumes all threads. */
3181 resume_info
.thread
= minus_one_ptid
;
3182 resume_info
.kind
= resume_continue
;
3183 resume_info
.sig
= 0;
3184 (*the_target
->resume
) (&resume_info
, 1);
3187 break; /* from switch/case */
3190 fprintf (stderr
, "Detaching from process %d\n", pid
);
3192 if (detach_inferior (pid
) != 0)
3193 write_enn (own_buf
);
3196 discard_queued_stop_replies (pid
);
3199 if (extended_protocol
)
3201 /* Treat this like a normal program exit. */
3202 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3203 last_status
.value
.integer
= 0;
3204 last_ptid
= pid_to_ptid (pid
);
3206 current_inferior
= NULL
;
3213 /* If we are attached, then we can exit. Otherwise, we
3214 need to hang around doing nothing, until the child is
3216 join_inferior (pid
);
3222 extended_protocol
= 1;
3226 handle_status (own_buf
);
3229 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
3231 ptid_t gdb_id
, thread_id
;
3234 require_running (own_buf
);
3236 gdb_id
= read_ptid (&own_buf
[2], NULL
);
3238 pid
= ptid_get_pid (gdb_id
);
3240 if (ptid_equal (gdb_id
, null_ptid
)
3241 || ptid_equal (gdb_id
, minus_one_ptid
))
3242 thread_id
= null_ptid
;
3244 && ptid_equal (pid_to_ptid (pid
),
3247 struct thread_info
*thread
=
3248 (struct thread_info
*) find_inferior (&all_threads
,
3253 write_enn (own_buf
);
3257 thread_id
= ((struct inferior_list_entry
*)thread
)->id
;
3261 thread_id
= gdb_id_to_thread_id (gdb_id
);
3262 if (ptid_equal (thread_id
, null_ptid
))
3264 write_enn (own_buf
);
3269 if (own_buf
[1] == 'g')
3271 if (ptid_equal (thread_id
, null_ptid
))
3273 /* GDB is telling us to choose any thread. Check if
3274 the currently selected thread is still valid. If
3275 it is not, select the first available. */
3276 struct thread_info
*thread
=
3277 (struct thread_info
*) find_inferior_id (&all_threads
,
3280 thread_id
= all_threads
.head
->id
;
3283 general_thread
= thread_id
;
3284 set_desired_inferior (1);
3286 else if (own_buf
[1] == 'c')
3287 cont_thread
= thread_id
;
3293 /* Silently ignore it so that gdb can extend the protocol
3294 without compatibility headaches. */
3299 require_running (own_buf
);
3300 if (current_traceframe
>= 0)
3302 struct regcache
*regcache
3303 = new_register_cache (current_target_desc ());
3305 if (fetch_traceframe_registers (current_traceframe
,
3307 registers_to_string (regcache
, own_buf
);
3309 write_enn (own_buf
);
3310 free_register_cache (regcache
);
3314 struct regcache
*regcache
;
3316 set_desired_inferior (1);
3317 regcache
= get_thread_regcache (current_inferior
, 1);
3318 registers_to_string (regcache
, own_buf
);
3322 require_running (own_buf
);
3323 if (current_traceframe
>= 0)
3324 write_enn (own_buf
);
3327 struct regcache
*regcache
;
3329 set_desired_inferior (1);
3330 regcache
= get_thread_regcache (current_inferior
, 1);
3331 registers_from_string (regcache
, &own_buf
[1]);
3336 require_running (own_buf
);
3337 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
3338 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
3340 write_enn (own_buf
);
3342 convert_int_to_ascii (mem_buf
, own_buf
, res
);
3345 require_running (own_buf
);
3346 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
3347 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
3350 write_enn (own_buf
);
3353 require_running (own_buf
);
3354 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
3355 &mem_addr
, &len
, &mem_buf
) < 0
3356 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
3357 write_enn (own_buf
);
3362 require_running (own_buf
);
3363 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3364 if (gdb_signal_to_host_p (sig
))
3365 signal
= gdb_signal_to_host (sig
);
3368 myresume (own_buf
, 0, signal
);
3371 require_running (own_buf
);
3372 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3373 if (gdb_signal_to_host_p (sig
))
3374 signal
= gdb_signal_to_host (sig
);
3377 myresume (own_buf
, 1, signal
);
3380 require_running (own_buf
);
3382 myresume (own_buf
, 0, signal
);
3385 require_running (own_buf
);
3387 myresume (own_buf
, 1, signal
);
3389 case 'Z': /* insert_ ... */
3391 case 'z': /* remove_ ... */
3396 char type
= own_buf
[1];
3398 const int insert
= ch
== 'Z';
3399 char *p
= &own_buf
[3];
3401 p
= unpack_varlen_hex (p
, &addr
);
3402 len
= strtol (p
+ 1, &dataptr
, 16);
3404 /* Default to unrecognized/unsupported. */
3408 case '0': /* software-breakpoint */
3409 case '1': /* hardware-breakpoint */
3410 case '2': /* write watchpoint */
3411 case '3': /* read watchpoint */
3412 case '4': /* access watchpoint */
3413 require_running (own_buf
);
3414 if (insert
&& the_target
->insert_point
!= NULL
)
3416 /* Insert the breakpoint. If it is already inserted, nothing
3418 res
= (*the_target
->insert_point
) (type
, addr
, len
);
3420 /* GDB may have sent us a list of *point parameters to be
3421 evaluated on the target's side. Read such list here. If we
3422 already have a list of parameters, GDB is telling us to drop
3423 that list and use this one instead. */
3424 if (!res
&& (type
== '0' || type
== '1'))
3426 /* Remove previous conditions. */
3427 clear_gdb_breakpoint_conditions (addr
);
3428 process_point_options (addr
, &dataptr
);
3431 else if (!insert
&& the_target
->remove_point
!= NULL
)
3432 res
= (*the_target
->remove_point
) (type
, addr
, len
);
3444 write_enn (own_buf
);
3448 response_needed
= 0;
3449 if (!target_running ())
3450 /* The packet we received doesn't make sense - but we can't
3451 reply to it, either. */
3454 fprintf (stderr
, "Killing all inferiors\n");
3455 for_each_inferior (&all_processes
, kill_inferior_callback
);
3457 /* When using the extended protocol, we wait with no program
3458 running. The traditional protocol will exit instead. */
3459 if (extended_protocol
)
3461 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3462 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
3470 ptid_t gdb_id
, thread_id
;
3472 require_running (own_buf
);
3474 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3475 thread_id
= gdb_id_to_thread_id (gdb_id
);
3476 if (ptid_equal (thread_id
, null_ptid
))
3478 write_enn (own_buf
);
3482 if (mythread_alive (thread_id
))
3485 write_enn (own_buf
);
3489 response_needed
= 0;
3491 /* Restarting the inferior is only supported in the extended
3493 if (extended_protocol
)
3495 if (target_running ())
3496 for_each_inferior (&all_processes
,
3497 kill_inferior_callback
);
3498 fprintf (stderr
, "GDBserver restarting\n");
3500 /* Wait till we are at 1st instruction in prog. */
3501 if (program_argv
!= NULL
)
3502 start_inferior (program_argv
);
3505 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3506 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
3512 /* It is a request we don't understand. Respond with an
3513 empty packet so that gdb knows that we don't support this
3519 /* Extended (long) request. */
3520 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
3524 /* It is a request we don't understand. Respond with an empty
3525 packet so that gdb knows that we don't support this
3531 if (new_packet_len
!= -1)
3532 putpkt_binary (own_buf
, new_packet_len
);
3536 response_needed
= 0;
3538 if (!extended_protocol
&& have_ran
&& !target_running ())
3540 /* In non-stop, defer exiting until GDB had a chance to query
3541 the whole vStopped list (until it gets an OK). */
3542 if (QUEUE_is_empty (notif_event_p
, notif_stop
.queue
))
3544 fprintf (stderr
, "GDBserver exiting\n");
3556 /* Event-loop callback for serial events. */
3559 handle_serial_event (int err
, gdb_client_data client_data
)
3562 fprintf (stderr
, "handling possible serial event\n");
3564 /* Really handle it. */
3565 if (process_serial_event () < 0)
3568 /* Be sure to not change the selected inferior behind GDB's back.
3569 Important in the non-stop mode asynchronous protocol. */
3570 set_desired_inferior (1);
3575 /* Event-loop callback for target events. */
3578 handle_target_event (int err
, gdb_client_data client_data
)
3581 fprintf (stderr
, "handling possible target event\n");
3583 last_ptid
= mywait (minus_one_ptid
, &last_status
,
3586 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3588 int pid
= ptid_get_pid (last_ptid
);
3589 struct process_info
*process
= find_process_pid (pid
);
3590 int forward_event
= !gdb_connected () || process
->gdb_detached
;
3592 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3593 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3595 mark_breakpoints_out (process
);
3596 mourn_inferior (process
);
3600 /* We're reporting this thread as stopped. Update its
3601 "want-stopped" state to what the client wants, until it
3602 gets a new resume action. */
3603 current_inferior
->last_resume_kind
= resume_stop
;
3604 current_inferior
->last_status
= last_status
;
3609 if (!target_running ())
3611 /* The last process exited. We're done. */
3615 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
3617 /* A thread stopped with a signal, but gdb isn't
3618 connected to handle it. Pass it down to the
3619 inferior, as if it wasn't being traced. */
3620 struct thread_resume resume_info
;
3624 "GDB not connected; forwarding event %d for [%s]\n",
3625 (int) last_status
.kind
,
3626 target_pid_to_str (last_ptid
));
3628 resume_info
.thread
= last_ptid
;
3629 resume_info
.kind
= resume_continue
;
3630 resume_info
.sig
= gdb_signal_to_host (last_status
.value
.sig
);
3631 (*the_target
->resume
) (&resume_info
, 1);
3633 else if (debug_threads
)
3634 fprintf (stderr
, "GDB not connected; ignoring event %d for [%s]\n",
3635 (int) last_status
.kind
,
3636 target_pid_to_str (last_ptid
));
3640 struct vstop_notif
*vstop_notif
3641 = xmalloc (sizeof (struct vstop_notif
));
3643 vstop_notif
->status
= last_status
;
3644 vstop_notif
->ptid
= last_ptid
;
3645 /* Push Stop notification. */
3646 notif_push (¬if_stop
,
3647 (struct notif_event
*) vstop_notif
);
3651 /* Be sure to not change the selected inferior behind GDB's back.
3652 Important in the non-stop mode asynchronous protocol. */
3653 set_desired_inferior (1);