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"
31 #include "filestuff.h"
33 /* The thread set with an `Hc' packet. `Hc' is deprecated in favor of
34 `vCont'. Note the multi-process extensions made `vCont' a
35 requirement, so `Hc pPID.TID' is pretty much undefined. So
36 CONT_THREAD can be null_ptid for no `Hc' thread, minus_one_ptid for
37 resuming all threads of the process (again, `Hc' isn't used for
38 multi-process), or a specific thread ptid_t.
40 We also set this when handling a single-thread `vCont' resume, as
41 some places in the backends check it to know when (and for which
42 thread) single-thread scheduler-locking is in effect. */
45 /* The thread set with an `Hg' packet. */
46 ptid_t general_thread
;
50 static int extended_protocol
;
51 static int response_needed
;
52 static int exit_requested
;
54 /* --once: Exit after the first connection has closed. */
60 /* Whether we should attempt to disable the operating system's address
61 space randomization feature before starting an inferior. */
62 int disable_randomization
= 1;
64 static char **program_argv
, **wrapper_argv
;
66 /* Enable miscellaneous debugging output. The name is historical - it
67 was originally used to debug LinuxThreads support. */
70 /* Enable debugging of h/w breakpoint/watchpoint support. */
73 int pass_signals
[GDB_SIGNAL_LAST
];
74 int program_signals
[GDB_SIGNAL_LAST
];
75 int program_signals_p
;
79 /* The PID of the originally created or attached inferior. Used to
80 send signals to the process when GDB sends us an asynchronous interrupt
81 (user hitting Control-C in the client), and to wait for the child to exit
82 when no longer debugging it. */
84 unsigned long signal_pid
;
87 /* A file descriptor for the controlling terminal. */
90 /* TERMINAL_FD's original foreground group. */
91 pid_t old_foreground_pgrp
;
93 /* Hand back terminal ownership to the original foreground group. */
96 restore_old_foreground_pgrp (void)
98 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
102 /* Set if you want to disable optional thread related packets support
103 in gdbserver, for the sake of testing GDB against stubs that don't
105 int disable_packet_vCont
;
106 int disable_packet_Tthread
;
107 int disable_packet_qC
;
108 int disable_packet_qfThreadInfo
;
110 /* Last status reported to GDB. */
111 static struct target_waitstatus last_status
;
112 static ptid_t last_ptid
;
114 static char *own_buf
;
115 static unsigned char *mem_buf
;
117 /* A sub-class of 'struct notif_event' for stop, holding information
118 relative to a single stop reply. We keep a queue of these to
119 push to GDB in non-stop mode. */
123 struct notif_event base
;
125 /* Thread or process that got the event. */
129 struct target_waitstatus status
;
132 DEFINE_QUEUE_P (notif_event_p
);
134 /* Put a stop reply to the stop reply queue. */
137 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
139 struct vstop_notif
*new_notif
= xmalloc (sizeof (*new_notif
));
141 new_notif
->ptid
= ptid
;
142 new_notif
->status
= *status
;
144 notif_event_enque (¬if_stop
, (struct notif_event
*) new_notif
);
148 remove_all_on_match_pid (QUEUE (notif_event_p
) *q
,
149 QUEUE_ITER (notif_event_p
) *iter
,
150 struct notif_event
*event
,
156 || ptid_get_pid (((struct vstop_notif
*) event
)->ptid
) == *pid
)
158 if (q
->free_func
!= NULL
)
159 q
->free_func (event
);
161 QUEUE_remove_elem (notif_event_p
, q
, iter
);
167 /* Get rid of the currently pending stop replies for PID. If PID is
168 -1, then apply to all processes. */
171 discard_queued_stop_replies (int pid
)
173 QUEUE_iterate (notif_event_p
, notif_stop
.queue
,
174 remove_all_on_match_pid
, &pid
);
178 vstop_notif_reply (struct notif_event
*event
, char *own_buf
)
180 struct vstop_notif
*vstop
= (struct vstop_notif
*) event
;
182 prepare_resume_reply (own_buf
, vstop
->ptid
, &vstop
->status
);
185 struct notif_server notif_stop
=
187 "vStopped", "Stop", NULL
, vstop_notif_reply
,
191 target_running (void)
193 return all_threads
.head
!= NULL
;
197 start_inferior (char **argv
)
199 char **new_argv
= argv
;
201 if (wrapper_argv
!= NULL
)
205 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
207 for (i
= 0; argv
[i
] != NULL
; i
++)
209 new_argv
= alloca (sizeof (char *) * count
);
211 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
212 new_argv
[count
++] = wrapper_argv
[i
];
213 for (i
= 0; argv
[i
] != NULL
; i
++)
214 new_argv
[count
++] = argv
[i
];
215 new_argv
[count
] = NULL
;
221 for (i
= 0; new_argv
[i
]; ++i
)
222 fprintf (stderr
, "new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
227 signal (SIGTTOU
, SIG_DFL
);
228 signal (SIGTTIN
, SIG_DFL
);
231 /* Clear this so the backend doesn't get confused, thinking
232 CONT_THREAD died, and it needs to resume all threads. */
233 cont_thread
= null_ptid
;
235 signal_pid
= create_inferior (new_argv
[0], new_argv
);
237 /* FIXME: we don't actually know at this point that the create
238 actually succeeded. We won't know that until we wait. */
239 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
244 signal (SIGTTOU
, SIG_IGN
);
245 signal (SIGTTIN
, SIG_IGN
);
246 terminal_fd
= fileno (stderr
);
247 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
248 tcsetpgrp (terminal_fd
, signal_pid
);
249 atexit (restore_old_foreground_pgrp
);
252 if (wrapper_argv
!= NULL
)
254 struct thread_resume resume_info
;
256 resume_info
.thread
= pid_to_ptid (signal_pid
);
257 resume_info
.kind
= resume_continue
;
260 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
262 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
267 (*the_target
->resume
) (&resume_info
, 1);
269 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
270 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
273 current_inferior
->last_resume_kind
= resume_stop
;
274 current_inferior
->last_status
= last_status
;
276 while (last_status
.value
.sig
!= GDB_SIGNAL_TRAP
);
281 /* Wait till we are at 1st instruction in program, return new pid
282 (assuming success). */
283 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
285 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
286 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
288 current_inferior
->last_resume_kind
= resume_stop
;
289 current_inferior
->last_status
= last_status
;
296 attach_inferior (int pid
)
298 /* myattach should return -1 if attaching is unsupported,
299 0 if it succeeded, and call error() otherwise. */
301 if (myattach (pid
) != 0)
304 fprintf (stderr
, "Attached; pid = %d\n", pid
);
307 /* FIXME - It may be that we should get the SIGNAL_PID from the
308 attach function, so that it can be the main thread instead of
309 whichever we were told to attach to. */
312 /* Clear this so the backend doesn't get confused, thinking
313 CONT_THREAD died, and it needs to resume all threads. */
314 cont_thread
= null_ptid
;
318 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
320 /* GDB knows to ignore the first SIGSTOP after attaching to a running
321 process using the "attach" command, but this is different; it's
322 just using "target remote". Pretend it's just starting up. */
323 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
324 && last_status
.value
.sig
== GDB_SIGNAL_STOP
)
325 last_status
.value
.sig
= GDB_SIGNAL_TRAP
;
327 current_inferior
->last_resume_kind
= resume_stop
;
328 current_inferior
->last_status
= last_status
;
334 extern int remote_debug
;
336 /* Decode a qXfer read request. Return 0 if everything looks OK,
340 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
342 /* After the read marker and annex, qXfer looks like a
343 traditional 'm' packet. */
344 decode_m_packet (buf
, ofs
, len
);
350 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
352 /* Extract and NUL-terminate the object. */
354 while (*buf
&& *buf
!= ':')
360 /* Extract and NUL-terminate the read/write action. */
362 while (*buf
&& *buf
!= ':')
368 /* Extract and NUL-terminate the annex. */
370 while (*buf
&& *buf
!= ':')
380 /* Write the response to a successful qXfer read. Returns the
381 length of the (binary) data stored in BUF, corresponding
382 to as much of DATA/LEN as we could fit. IS_MORE controls
383 the first character of the response. */
385 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
394 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
398 /* Handle btrace enabling. */
401 handle_btrace_enable (struct thread_info
*thread
)
403 if (thread
->btrace
!= NULL
)
404 return "E.Btrace already enabled.";
406 thread
->btrace
= target_enable_btrace (thread
->entry
.id
);
407 if (thread
->btrace
== NULL
)
408 return "E.Could not enable btrace.";
413 /* Handle btrace disabling. */
416 handle_btrace_disable (struct thread_info
*thread
)
419 if (thread
->btrace
== NULL
)
420 return "E.Branch tracing not enabled.";
422 if (target_disable_btrace (thread
->btrace
) != 0)
423 return "E.Could not disable branch tracing.";
425 thread
->btrace
= NULL
;
429 /* Handle the "Qbtrace" packet. */
432 handle_btrace_general_set (char *own_buf
)
434 struct thread_info
*thread
;
438 if (strncmp ("Qbtrace:", own_buf
, strlen ("Qbtrace:")) != 0)
441 op
= own_buf
+ strlen ("Qbtrace:");
443 if (!target_supports_btrace ())
445 strcpy (own_buf
, "E.Target does not support branch tracing.");
449 if (ptid_equal (general_thread
, null_ptid
)
450 || ptid_equal (general_thread
, minus_one_ptid
))
452 strcpy (own_buf
, "E.Must select a single thread.");
456 thread
= find_thread_ptid (general_thread
);
459 strcpy (own_buf
, "E.No such thread.");
465 if (strcmp (op
, "bts") == 0)
466 err
= handle_btrace_enable (thread
);
467 else if (strcmp (op
, "off") == 0)
468 err
= handle_btrace_disable (thread
);
470 err
= "E.Bad Qbtrace operation. Use bts or off.";
473 strcpy (own_buf
, err
);
480 /* Handle all of the extended 'Q' packets. */
483 handle_general_set (char *own_buf
)
485 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
487 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
488 const char *p
= own_buf
+ strlen ("QPassSignals:");
491 p
= decode_address_to_semicolon (&cursig
, p
);
492 for (i
= 0; i
< numsigs
; i
++)
498 /* Keep looping, to clear the remaining signals. */
501 p
= decode_address_to_semicolon (&cursig
, p
);
506 strcpy (own_buf
, "OK");
510 if (strncmp ("QProgramSignals:", own_buf
, strlen ("QProgramSignals:")) == 0)
512 int numsigs
= (int) GDB_SIGNAL_LAST
, i
;
513 const char *p
= own_buf
+ strlen ("QProgramSignals:");
516 program_signals_p
= 1;
518 p
= decode_address_to_semicolon (&cursig
, p
);
519 for (i
= 0; i
< numsigs
; i
++)
523 program_signals
[i
] = 1;
525 /* Keep looping, to clear the remaining signals. */
528 p
= decode_address_to_semicolon (&cursig
, p
);
531 program_signals
[i
] = 0;
533 strcpy (own_buf
, "OK");
537 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
541 fprintf (stderr
, "[noack mode enabled]\n");
550 if (strncmp (own_buf
, "QNonStop:", 9) == 0)
552 char *mode
= own_buf
+ 9;
556 if (strcmp (mode
, "0") == 0)
558 else if (strcmp (mode
, "1") == 0)
562 /* We don't know what this mode is, so complain to
564 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
570 req_str
= req
? "non-stop" : "all-stop";
571 if (start_non_stop (req
) != 0)
573 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
581 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
587 if (strncmp ("QDisableRandomization:", own_buf
,
588 strlen ("QDisableRandomization:")) == 0)
590 char *packet
= own_buf
+ strlen ("QDisableRandomization:");
593 unpack_varlen_hex (packet
, &setting
);
594 disable_randomization
= setting
;
598 if (disable_randomization
)
599 fprintf (stderr
, "[address space randomization disabled]\n");
601 fprintf (stderr
, "[address space randomization enabled]\n");
608 if (target_supports_tracepoints ()
609 && handle_tracepoint_general_set (own_buf
))
612 if (strncmp ("QAgent:", own_buf
, strlen ("QAgent:")) == 0)
614 char *mode
= own_buf
+ strlen ("QAgent:");
617 if (strcmp (mode
, "0") == 0)
619 else if (strcmp (mode
, "1") == 0)
623 /* We don't know what this value is, so complain to GDB. */
624 sprintf (own_buf
, "E.Unknown QAgent value");
628 /* Update the flag. */
631 fprintf (stderr
, "[%s agent]\n", req
? "Enable" : "Disable");
636 if (handle_btrace_general_set (own_buf
))
639 /* Otherwise we didn't know what packet it was. Say we didn't
645 get_features_xml (const char *annex
)
647 const struct target_desc
*desc
= current_target_desc ();
649 /* `desc->xmltarget' defines what to return when looking for the
650 "target.xml" file. Its contents can either be verbatim XML code
651 (prefixed with a '@') or else the name of the actual XML file to
652 be used in place of "target.xml".
654 This variable is set up from the auto-generated
655 init_registers_... routine for the current target. */
657 if (desc
->xmltarget
!= NULL
&& strcmp (annex
, "target.xml") == 0)
659 if (*desc
->xmltarget
== '@')
660 return desc
->xmltarget
+ 1;
662 annex
= desc
->xmltarget
;
667 extern const char *const xml_builtin
[][2];
670 /* Look for the annex. */
671 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
672 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
675 if (xml_builtin
[i
][0] != NULL
)
676 return xml_builtin
[i
][1];
684 monitor_show_help (void)
686 monitor_output ("The following monitor commands are supported:\n");
687 monitor_output (" set debug <0|1>\n");
688 monitor_output (" Enable general debugging messages\n");
689 monitor_output (" set debug-hw-points <0|1>\n");
690 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
691 monitor_output (" set remote-debug <0|1>\n");
692 monitor_output (" Enable remote protocol debugging messages\n");
693 monitor_output (" exit\n");
694 monitor_output (" Quit GDBserver\n");
697 /* Read trace frame or inferior memory. Returns the number of bytes
698 actually read, zero when no further transfer is possible, and -1 on
699 error. Return of a positive value smaller than LEN does not
700 indicate there's no more to be read, only the end of the transfer.
701 E.g., when GDB reads memory from a traceframe, a first request may
702 be served from a memory block that does not cover the whole request
703 length. A following request gets the rest served from either
704 another block (of the same traceframe) or from the read-only
708 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
712 if (current_traceframe
>= 0)
715 ULONGEST length
= len
;
717 if (traceframe_read_mem (current_traceframe
,
718 memaddr
, myaddr
, len
, &nbytes
))
720 /* Data read from trace buffer, we're done. */
723 if (!in_readonly_region (memaddr
, length
))
725 /* Otherwise we have a valid readonly case, fall through. */
726 /* (assume no half-trace half-real blocks for now) */
729 res
= prepare_to_access_memory ();
732 res
= read_inferior_memory (memaddr
, myaddr
, len
);
733 done_accessing_memory ();
735 return res
== 0 ? len
: -1;
741 /* Write trace frame or inferior memory. Actually, writing to trace
742 frames is forbidden. */
745 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
747 if (current_traceframe
>= 0)
753 ret
= prepare_to_access_memory ();
756 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
757 done_accessing_memory ();
763 /* Subroutine of handle_search_memory to simplify it. */
766 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
767 gdb_byte
*pattern
, unsigned pattern_len
,
768 gdb_byte
*search_buf
,
769 unsigned chunk_size
, unsigned search_buf_size
,
770 CORE_ADDR
*found_addrp
)
772 /* Prime the search buffer. */
774 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
777 warning ("Unable to access %ld bytes of target "
778 "memory at 0x%lx, halting search.",
779 (long) search_buf_size
, (long) start_addr
);
783 /* Perform the search.
785 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
786 When we've scanned N bytes we copy the trailing bytes to the start and
787 read in another N bytes. */
789 while (search_space_len
>= pattern_len
)
792 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
796 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
798 if (found_ptr
!= NULL
)
800 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
801 *found_addrp
= found_addr
;
805 /* Not found in this chunk, skip to next chunk. */
807 /* Don't let search_space_len wrap here, it's unsigned. */
808 if (search_space_len
>= chunk_size
)
809 search_space_len
-= chunk_size
;
811 search_space_len
= 0;
813 if (search_space_len
>= pattern_len
)
815 unsigned keep_len
= search_buf_size
- chunk_size
;
816 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
819 /* Copy the trailing part of the previous iteration to the front
820 of the buffer for the next iteration. */
821 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
823 nr_to_read
= (search_space_len
- keep_len
< chunk_size
824 ? search_space_len
- keep_len
827 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
828 nr_to_read
) != search_buf_size
)
830 warning ("Unable to access %ld bytes of target memory "
831 "at 0x%lx, halting search.",
832 (long) nr_to_read
, (long) read_addr
);
836 start_addr
+= chunk_size
;
845 /* Handle qSearch:memory packets. */
848 handle_search_memory (char *own_buf
, int packet_len
)
850 CORE_ADDR start_addr
;
851 CORE_ADDR search_space_len
;
853 unsigned int pattern_len
;
854 /* NOTE: also defined in find.c testcase. */
855 #define SEARCH_CHUNK_SIZE 16000
856 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
857 /* Buffer to hold memory contents for searching. */
858 gdb_byte
*search_buf
;
859 unsigned search_buf_size
;
861 CORE_ADDR found_addr
;
862 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
864 pattern
= malloc (packet_len
);
867 error ("Unable to allocate memory to perform the search");
868 strcpy (own_buf
, "E00");
871 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
872 packet_len
- cmd_name_len
,
873 &start_addr
, &search_space_len
,
874 pattern
, &pattern_len
) < 0)
877 error ("Error in parsing qSearch:memory packet");
878 strcpy (own_buf
, "E00");
882 search_buf_size
= chunk_size
+ pattern_len
- 1;
884 /* No point in trying to allocate a buffer larger than the search space. */
885 if (search_space_len
< search_buf_size
)
886 search_buf_size
= search_space_len
;
888 search_buf
= malloc (search_buf_size
);
889 if (search_buf
== NULL
)
892 error ("Unable to allocate memory to perform the search");
893 strcpy (own_buf
, "E00");
897 found
= handle_search_memory_1 (start_addr
, search_space_len
,
898 pattern
, pattern_len
,
899 search_buf
, chunk_size
, search_buf_size
,
903 sprintf (own_buf
, "1,%lx", (long) found_addr
);
905 strcpy (own_buf
, "0");
907 strcpy (own_buf
, "E00");
913 #define require_running(BUF) \
914 if (!target_running ()) \
920 /* Handle monitor commands not handled by target-specific handlers. */
923 handle_monitor_command (char *mon
, char *own_buf
)
925 if (strcmp (mon
, "set debug 1") == 0)
928 monitor_output ("Debug output enabled.\n");
930 else if (strcmp (mon
, "set debug 0") == 0)
933 monitor_output ("Debug output disabled.\n");
935 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
938 monitor_output ("H/W point debugging output enabled.\n");
940 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
943 monitor_output ("H/W point debugging output disabled.\n");
945 else if (strcmp (mon
, "set remote-debug 1") == 0)
948 monitor_output ("Protocol debug output enabled.\n");
950 else if (strcmp (mon
, "set remote-debug 0") == 0)
953 monitor_output ("Protocol debug output disabled.\n");
955 else if (strcmp (mon
, "help") == 0)
956 monitor_show_help ();
957 else if (strcmp (mon
, "exit") == 0)
961 monitor_output ("Unknown monitor command.\n\n");
962 monitor_show_help ();
967 /* Associates a callback with each supported qXfer'able object. */
971 /* The object this handler handles. */
974 /* Request that the target transfer up to LEN 8-bit bytes of the
975 target's OBJECT. The OFFSET, for a seekable object, specifies
976 the starting point. The ANNEX can be used to provide additional
977 data-specific information to the target.
979 Return the number of bytes actually transfered, zero when no
980 further transfer is possible, -1 on error, -2 when the transfer
981 is not supported, and -3 on a verbose error message that should
982 be preserved. Return of a positive value smaller than LEN does
983 not indicate the end of the object, only the end of the transfer.
985 One, and only one, of readbuf or writebuf must be non-NULL. */
986 int (*xfer
) (const char *annex
,
987 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
988 ULONGEST offset
, LONGEST len
);
991 /* Handle qXfer:auxv:read. */
994 handle_qxfer_auxv (const char *annex
,
995 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
996 ULONGEST offset
, LONGEST len
)
998 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
1001 if (annex
[0] != '\0' || !target_running ())
1004 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
1007 /* Handle qXfer:features:read. */
1010 handle_qxfer_features (const char *annex
,
1011 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1012 ULONGEST offset
, LONGEST len
)
1014 const char *document
;
1017 if (writebuf
!= NULL
)
1020 if (!target_running ())
1023 /* Grab the correct annex. */
1024 document
= get_features_xml (annex
);
1025 if (document
== NULL
)
1028 total_len
= strlen (document
);
1030 if (offset
> total_len
)
1033 if (offset
+ len
> total_len
)
1034 len
= total_len
- offset
;
1036 memcpy (readbuf
, document
+ offset
, len
);
1040 /* Handle qXfer:libraries:read. */
1043 handle_qxfer_libraries (const char *annex
,
1044 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1045 ULONGEST offset
, LONGEST len
)
1047 unsigned int total_len
;
1049 struct inferior_list_entry
*dll_ptr
;
1051 if (writebuf
!= NULL
)
1054 if (annex
[0] != '\0' || !target_running ())
1057 /* Over-estimate the necessary memory. Assume that every character
1058 in the library name must be escaped. */
1060 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
1061 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
1063 document
= malloc (total_len
);
1064 if (document
== NULL
)
1067 strcpy (document
, "<library-list>\n");
1068 p
= document
+ strlen (document
);
1070 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
1072 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
1075 strcpy (p
, " <library name=\"");
1077 name
= xml_escape_text (dll
->name
);
1081 strcpy (p
, "\"><segment address=\"");
1083 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1085 strcpy (p
, "\"/></library>\n");
1089 strcpy (p
, "</library-list>\n");
1091 total_len
= strlen (document
);
1093 if (offset
> total_len
)
1099 if (offset
+ len
> total_len
)
1100 len
= total_len
- offset
;
1102 memcpy (readbuf
, document
+ offset
, len
);
1107 /* Handle qXfer:libraries-svr4:read. */
1110 handle_qxfer_libraries_svr4 (const char *annex
,
1111 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1112 ULONGEST offset
, LONGEST len
)
1114 if (writebuf
!= NULL
)
1117 if (!target_running () || the_target
->qxfer_libraries_svr4
== NULL
)
1120 return the_target
->qxfer_libraries_svr4 (annex
, readbuf
, writebuf
, offset
, len
);
1123 /* Handle qXfer:osadata:read. */
1126 handle_qxfer_osdata (const char *annex
,
1127 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1128 ULONGEST offset
, LONGEST len
)
1130 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
1133 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
1136 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1139 handle_qxfer_siginfo (const char *annex
,
1140 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1141 ULONGEST offset
, LONGEST len
)
1143 if (the_target
->qxfer_siginfo
== NULL
)
1146 if (annex
[0] != '\0' || !target_running ())
1149 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1152 /* Handle qXfer:spu:read and qXfer:spu:write. */
1155 handle_qxfer_spu (const char *annex
,
1156 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1157 ULONGEST offset
, LONGEST len
)
1159 if (the_target
->qxfer_spu
== NULL
)
1162 if (!target_running ())
1165 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1168 /* Handle qXfer:statictrace:read. */
1171 handle_qxfer_statictrace (const char *annex
,
1172 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1173 ULONGEST offset
, LONGEST len
)
1177 if (writebuf
!= NULL
)
1180 if (annex
[0] != '\0' || !target_running () || current_traceframe
== -1)
1183 if (traceframe_read_sdata (current_traceframe
, offset
,
1184 readbuf
, len
, &nbytes
))
1189 /* Helper for handle_qxfer_threads. */
1192 handle_qxfer_threads_proper (struct buffer
*buffer
)
1194 struct inferior_list_entry
*thread
;
1196 buffer_grow_str (buffer
, "<threads>\n");
1198 for (thread
= all_threads
.head
; thread
; thread
= thread
->next
)
1200 ptid_t ptid
= thread_to_gdb_id ((struct thread_info
*)thread
);
1202 int core
= target_core_of_thread (ptid
);
1205 write_ptid (ptid_s
, ptid
);
1209 sprintf (core_s
, "%d", core
);
1210 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1215 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1220 buffer_grow_str0 (buffer
, "</threads>\n");
1223 /* Handle qXfer:threads:read. */
1226 handle_qxfer_threads (const char *annex
,
1227 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1228 ULONGEST offset
, LONGEST len
)
1230 static char *result
= 0;
1231 static unsigned int result_length
= 0;
1233 if (writebuf
!= NULL
)
1236 if (!target_running () || annex
[0] != '\0')
1241 struct buffer buffer
;
1242 /* When asked for data at offset 0, generate everything and store into
1243 'result'. Successive reads will be served off 'result'. */
1247 buffer_init (&buffer
);
1249 handle_qxfer_threads_proper (&buffer
);
1251 result
= buffer_finish (&buffer
);
1252 result_length
= strlen (result
);
1253 buffer_free (&buffer
);
1256 if (offset
>= result_length
)
1258 /* We're out of data. */
1265 if (len
> result_length
- offset
)
1266 len
= result_length
- offset
;
1268 memcpy (readbuf
, result
+ offset
, len
);
1273 /* Handle qXfer:traceframe-info:read. */
1276 handle_qxfer_traceframe_info (const char *annex
,
1277 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1278 ULONGEST offset
, LONGEST len
)
1280 static char *result
= 0;
1281 static unsigned int result_length
= 0;
1283 if (writebuf
!= NULL
)
1286 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1291 struct buffer buffer
;
1293 /* When asked for data at offset 0, generate everything and
1294 store into 'result'. Successive reads will be served off
1298 buffer_init (&buffer
);
1300 traceframe_read_info (current_traceframe
, &buffer
);
1302 result
= buffer_finish (&buffer
);
1303 result_length
= strlen (result
);
1304 buffer_free (&buffer
);
1307 if (offset
>= result_length
)
1309 /* We're out of data. */
1316 if (len
> result_length
- offset
)
1317 len
= result_length
- offset
;
1319 memcpy (readbuf
, result
+ offset
, len
);
1323 /* Handle qXfer:fdpic:read. */
1326 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1327 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1329 if (the_target
->read_loadmap
== NULL
)
1332 if (!target_running ())
1335 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1338 /* Handle qXfer:btrace:read. */
1341 handle_qxfer_btrace (const char *annex
,
1342 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1343 ULONGEST offset
, LONGEST len
)
1345 static struct buffer cache
;
1346 struct thread_info
*thread
;
1349 if (the_target
->read_btrace
== NULL
|| writebuf
!= NULL
)
1352 if (!target_running ())
1355 if (ptid_equal (general_thread
, null_ptid
)
1356 || ptid_equal (general_thread
, minus_one_ptid
))
1358 strcpy (own_buf
, "E.Must select a single thread.");
1362 thread
= find_thread_ptid (general_thread
);
1365 strcpy (own_buf
, "E.No such thread.");
1369 if (thread
->btrace
== NULL
)
1371 strcpy (own_buf
, "E.Btrace not enabled.");
1375 if (strcmp (annex
, "all") == 0)
1376 type
= btrace_read_all
;
1377 else if (strcmp (annex
, "new") == 0)
1378 type
= btrace_read_new
;
1381 strcpy (own_buf
, "E.Bad annex.");
1387 buffer_free (&cache
);
1389 target_read_btrace (thread
->btrace
, &cache
, type
);
1391 else if (offset
> cache
.used_size
)
1393 buffer_free (&cache
);
1397 if (len
> cache
.used_size
- offset
)
1398 len
= cache
.used_size
- offset
;
1400 memcpy (readbuf
, cache
.buffer
+ offset
, len
);
1405 static const struct qxfer qxfer_packets
[] =
1407 { "auxv", handle_qxfer_auxv
},
1408 { "btrace", handle_qxfer_btrace
},
1409 { "fdpic", handle_qxfer_fdpic
},
1410 { "features", handle_qxfer_features
},
1411 { "libraries", handle_qxfer_libraries
},
1412 { "libraries-svr4", handle_qxfer_libraries_svr4
},
1413 { "osdata", handle_qxfer_osdata
},
1414 { "siginfo", handle_qxfer_siginfo
},
1415 { "spu", handle_qxfer_spu
},
1416 { "statictrace", handle_qxfer_statictrace
},
1417 { "threads", handle_qxfer_threads
},
1418 { "traceframe-info", handle_qxfer_traceframe_info
},
1422 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1430 if (strncmp (own_buf
, "qXfer:", 6) != 0)
1433 /* Grab the object, r/w and annex. */
1434 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1436 write_enn (own_buf
);
1441 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1444 const struct qxfer
*q
= &qxfer_packets
[i
];
1446 if (strcmp (object
, q
->object
) == 0)
1448 if (strcmp (rw
, "read") == 0)
1450 unsigned char *data
;
1455 /* Grab the offset and length. */
1456 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1458 write_enn (own_buf
);
1462 /* Read one extra byte, as an indicator of whether there is
1464 if (len
> PBUFSIZ
- 2)
1466 data
= malloc (len
+ 1);
1469 write_enn (own_buf
);
1472 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1480 /* Preserve error message. */
1483 write_enn (own_buf
);
1485 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1487 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1492 else if (strcmp (rw
, "write") == 0)
1497 unsigned char *data
;
1499 strcpy (own_buf
, "E00");
1500 data
= malloc (packet_len
- (offset
- own_buf
));
1503 write_enn (own_buf
);
1506 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1507 &ofs
, &len
, data
) < 0)
1510 write_enn (own_buf
);
1514 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1522 /* Preserve error message. */
1525 write_enn (own_buf
);
1527 sprintf (own_buf
, "%x", n
);
1540 /* Table used by the crc32 function to calcuate the checksum. */
1542 static unsigned int crc32_table
[256] =
1545 /* Compute 32 bit CRC from inferior memory.
1547 On success, return 32 bit CRC.
1548 On failure, return (unsigned long long) -1. */
1550 static unsigned long long
1551 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1553 if (!crc32_table
[1])
1555 /* Initialize the CRC table and the decoding table. */
1559 for (i
= 0; i
< 256; i
++)
1561 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1562 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1569 unsigned char byte
= 0;
1571 /* Return failure if memory read fails. */
1572 if (read_inferior_memory (base
, &byte
, 1) != 0)
1573 return (unsigned long long) -1;
1575 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1578 return (unsigned long long) crc
;
1581 /* Handle all of the extended 'q' packets. */
1584 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1586 static struct inferior_list_entry
*thread_ptr
;
1588 /* Reply the current thread id. */
1589 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1592 require_running (own_buf
);
1594 if (!ptid_equal (general_thread
, null_ptid
)
1595 && !ptid_equal (general_thread
, minus_one_ptid
))
1596 gdb_id
= general_thread
;
1599 thread_ptr
= all_threads
.head
;
1600 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1603 sprintf (own_buf
, "QC");
1605 write_ptid (own_buf
, gdb_id
);
1609 if (strcmp ("qSymbol::", own_buf
) == 0)
1611 /* GDB is suggesting new symbols have been loaded. This may
1612 mean a new shared library has been detected as loaded, so
1613 take the opportunity to check if breakpoints we think are
1614 inserted, still are. Note that it isn't guaranteed that
1615 we'll see this when a shared library is loaded, and nor will
1616 we see this for unloads (although breakpoints in unloaded
1617 libraries shouldn't trigger), as GDB may not find symbols for
1618 the library at all. We also re-validate breakpoints when we
1619 see a second GDB breakpoint for the same address, and or when
1620 we access breakpoint shadows. */
1621 validate_breakpoints ();
1623 if (target_supports_tracepoints ())
1624 tracepoint_look_up_symbols ();
1626 if (target_running () && the_target
->look_up_symbols
!= NULL
)
1627 (*the_target
->look_up_symbols
) ();
1629 strcpy (own_buf
, "OK");
1633 if (!disable_packet_qfThreadInfo
)
1635 if (strcmp ("qfThreadInfo", own_buf
) == 0)
1639 require_running (own_buf
);
1640 thread_ptr
= all_threads
.head
;
1643 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1644 write_ptid (own_buf
, gdb_id
);
1645 thread_ptr
= thread_ptr
->next
;
1649 if (strcmp ("qsThreadInfo", own_buf
) == 0)
1653 require_running (own_buf
);
1654 if (thread_ptr
!= NULL
)
1657 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1658 write_ptid (own_buf
, gdb_id
);
1659 thread_ptr
= thread_ptr
->next
;
1664 sprintf (own_buf
, "l");
1670 if (the_target
->read_offsets
!= NULL
1671 && strcmp ("qOffsets", own_buf
) == 0)
1673 CORE_ADDR text
, data
;
1675 require_running (own_buf
);
1676 if (the_target
->read_offsets (&text
, &data
))
1677 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
1678 (long)text
, (long)data
, (long)data
);
1680 write_enn (own_buf
);
1685 /* Protocol features query. */
1686 if (strncmp ("qSupported", own_buf
, 10) == 0
1687 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1689 char *p
= &own_buf
[10];
1690 int gdb_supports_qRelocInsn
= 0;
1692 /* Start processing qSupported packet. */
1693 target_process_qsupported (NULL
);
1695 /* Process each feature being provided by GDB. The first
1696 feature will follow a ':', and latter features will follow
1700 char **qsupported
= NULL
;
1704 /* Two passes, to avoid nested strtok calls in
1705 target_process_qsupported. */
1706 for (p
= strtok (p
+ 1, ";");
1708 p
= strtok (NULL
, ";"))
1711 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
1712 qsupported
[count
- 1] = xstrdup (p
);
1715 for (i
= 0; i
< count
; i
++)
1718 if (strcmp (p
, "multiprocess+") == 0)
1720 /* GDB supports and wants multi-process support if
1722 if (target_supports_multi_process ())
1725 else if (strcmp (p
, "qRelocInsn+") == 0)
1727 /* GDB supports relocate instruction requests. */
1728 gdb_supports_qRelocInsn
= 1;
1731 target_process_qsupported (p
);
1740 "PacketSize=%x;QPassSignals+;QProgramSignals+",
1743 if (the_target
->qxfer_libraries_svr4
!= NULL
)
1744 strcat (own_buf
, ";qXfer:libraries-svr4:read+"
1745 ";augmented-libraries-svr4-read+");
1748 /* We do not have any hook to indicate whether the non-SVR4 target
1749 backend supports qXfer:libraries:read, so always report it. */
1750 strcat (own_buf
, ";qXfer:libraries:read+");
1753 if (the_target
->read_auxv
!= NULL
)
1754 strcat (own_buf
, ";qXfer:auxv:read+");
1756 if (the_target
->qxfer_spu
!= NULL
)
1757 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
1759 if (the_target
->qxfer_siginfo
!= NULL
)
1760 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1762 if (the_target
->read_loadmap
!= NULL
)
1763 strcat (own_buf
, ";qXfer:fdpic:read+");
1765 /* We always report qXfer:features:read, as targets may
1766 install XML files on a subsequent call to arch_setup.
1767 If we reported to GDB on startup that we don't support
1768 qXfer:feature:read at all, we will never be re-queried. */
1769 strcat (own_buf
, ";qXfer:features:read+");
1771 if (transport_is_reliable
)
1772 strcat (own_buf
, ";QStartNoAckMode+");
1774 if (the_target
->qxfer_osdata
!= NULL
)
1775 strcat (own_buf
, ";qXfer:osdata:read+");
1777 if (target_supports_multi_process ())
1778 strcat (own_buf
, ";multiprocess+");
1780 if (target_supports_non_stop ())
1781 strcat (own_buf
, ";QNonStop+");
1783 if (target_supports_disable_randomization ())
1784 strcat (own_buf
, ";QDisableRandomization+");
1786 strcat (own_buf
, ";qXfer:threads:read+");
1788 if (target_supports_tracepoints ())
1790 strcat (own_buf
, ";ConditionalTracepoints+");
1791 strcat (own_buf
, ";TraceStateVariables+");
1792 strcat (own_buf
, ";TracepointSource+");
1793 strcat (own_buf
, ";DisconnectedTracing+");
1794 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
1795 strcat (own_buf
, ";FastTracepoints+");
1796 strcat (own_buf
, ";StaticTracepoints+");
1797 strcat (own_buf
, ";InstallInTrace+");
1798 strcat (own_buf
, ";qXfer:statictrace:read+");
1799 strcat (own_buf
, ";qXfer:traceframe-info:read+");
1800 strcat (own_buf
, ";EnableDisableTracepoints+");
1801 strcat (own_buf
, ";QTBuffer:size+");
1802 strcat (own_buf
, ";tracenz+");
1805 /* Support target-side breakpoint conditions and commands. */
1806 strcat (own_buf
, ";ConditionalBreakpoints+");
1807 strcat (own_buf
, ";BreakpointCommands+");
1809 if (target_supports_agent ())
1810 strcat (own_buf
, ";QAgent+");
1812 if (target_supports_btrace ())
1814 strcat (own_buf
, ";Qbtrace:bts+");
1815 strcat (own_buf
, ";Qbtrace:off+");
1816 strcat (own_buf
, ";qXfer:btrace:read+");
1822 /* Thread-local storage support. */
1823 if (the_target
->get_tls_address
!= NULL
1824 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
1826 char *p
= own_buf
+ 12;
1827 CORE_ADDR parts
[2], address
= 0;
1829 ptid_t ptid
= null_ptid
;
1831 require_running (own_buf
);
1833 for (i
= 0; i
< 3; i
++)
1841 p2
= strchr (p
, ',');
1854 ptid
= read_ptid (p
, NULL
);
1856 decode_address (&parts
[i
- 1], p
, len
);
1860 if (p
!= NULL
|| i
< 3)
1864 struct thread_info
*thread
= find_thread_ptid (ptid
);
1869 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
1875 strcpy (own_buf
, paddress(address
));
1880 write_enn (own_buf
);
1884 /* Otherwise, pretend we do not understand this packet. */
1887 /* Windows OS Thread Information Block address support. */
1888 if (the_target
->get_tib_address
!= NULL
1889 && strncmp ("qGetTIBAddr:", own_buf
, 12) == 0)
1894 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
1896 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
1899 strcpy (own_buf
, paddress(tlb
));
1904 write_enn (own_buf
);
1910 /* Handle "monitor" commands. */
1911 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
1913 char *mon
= malloc (PBUFSIZ
);
1914 int len
= strlen (own_buf
+ 6);
1918 write_enn (own_buf
);
1922 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
1924 write_enn (own_buf
);
1928 mon
[len
/ 2] = '\0';
1932 if (the_target
->handle_monitor_command
== NULL
1933 || (*the_target
->handle_monitor_command
) (mon
) == 0)
1934 /* Default processing. */
1935 handle_monitor_command (mon
, own_buf
);
1941 if (strncmp ("qSearch:memory:", own_buf
,
1942 sizeof ("qSearch:memory:") - 1) == 0)
1944 require_running (own_buf
);
1945 handle_search_memory (own_buf
, packet_len
);
1949 if (strcmp (own_buf
, "qAttached") == 0
1950 || strncmp (own_buf
, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1952 struct process_info
*process
;
1954 if (own_buf
[sizeof ("qAttached") - 1])
1956 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
1957 process
= (struct process_info
*)
1958 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
1962 require_running (own_buf
);
1963 process
= current_process ();
1966 if (process
== NULL
)
1968 write_enn (own_buf
);
1972 strcpy (own_buf
, process
->attached
? "1" : "0");
1976 if (strncmp ("qCRC:", own_buf
, 5) == 0)
1978 /* CRC check (compare-section). */
1982 unsigned long long crc
;
1984 require_running (own_buf
);
1985 comma
= unpack_varlen_hex (own_buf
+ 5, &base
);
1986 if (*comma
++ != ',')
1988 write_enn (own_buf
);
1991 len
= strtoul (comma
, NULL
, 16);
1992 crc
= crc32 (base
, len
, 0xffffffff);
1993 /* Check for memory failure. */
1994 if (crc
== (unsigned long long) -1)
1996 write_enn (own_buf
);
1999 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
2003 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
2006 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
2009 /* Otherwise we didn't know what packet it was. Say we didn't
2014 static void gdb_wants_all_threads_stopped (void);
2016 /* Parse vCont packets. */
2018 handle_v_cont (char *own_buf
)
2022 struct thread_resume
*resume_info
;
2023 struct thread_resume default_action
= {{0}};
2025 /* Count the number of semicolons in the packet. There should be one
2026 for every action. */
2032 p
= strchr (p
, ';');
2035 resume_info
= malloc (n
* sizeof (resume_info
[0]));
2036 if (resume_info
== NULL
)
2044 memset (&resume_info
[i
], 0, sizeof resume_info
[i
]);
2046 if (p
[0] == 's' || p
[0] == 'S')
2047 resume_info
[i
].kind
= resume_step
;
2048 else if (p
[0] == 'r')
2049 resume_info
[i
].kind
= resume_step
;
2050 else if (p
[0] == 'c' || p
[0] == 'C')
2051 resume_info
[i
].kind
= resume_continue
;
2052 else if (p
[0] == 't')
2053 resume_info
[i
].kind
= resume_stop
;
2057 if (p
[0] == 'S' || p
[0] == 'C')
2060 sig
= strtol (p
+ 1, &q
, 16);
2065 if (!gdb_signal_to_host_p (sig
))
2067 resume_info
[i
].sig
= gdb_signal_to_host (sig
);
2069 else if (p
[0] == 'r')
2073 p
= unpack_varlen_hex (p
+ 1, &addr
);
2074 resume_info
[i
].step_range_start
= addr
;
2079 p
= unpack_varlen_hex (p
+ 1, &addr
);
2080 resume_info
[i
].step_range_end
= addr
;
2089 resume_info
[i
].thread
= minus_one_ptid
;
2090 default_action
= resume_info
[i
];
2092 /* Note: we don't increment i here, we'll overwrite this entry
2093 the next time through. */
2095 else if (p
[0] == ':')
2097 ptid_t ptid
= read_ptid (p
+ 1, &q
);
2102 if (p
[0] != ';' && p
[0] != 0)
2105 resume_info
[i
].thread
= ptid
;
2112 resume_info
[i
] = default_action
;
2114 /* `cont_thread' is still used in occasional places in the backend,
2115 to implement single-thread scheduler-locking. Doesn't make sense
2116 to set it if we see a stop request, or a wildcard action (one
2117 with '-1' (all threads), or 'pPID.-1' (all threads of PID)). */
2119 && !(ptid_equal (resume_info
[0].thread
, minus_one_ptid
)
2120 || ptid_get_lwp (resume_info
[0].thread
) == -1)
2121 && resume_info
[0].kind
!= resume_stop
)
2122 cont_thread
= resume_info
[0].thread
;
2124 cont_thread
= minus_one_ptid
;
2125 set_desired_inferior (0);
2130 (*the_target
->resume
) (resume_info
, n
);
2138 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2140 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2141 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2142 current_inferior
->last_status
= last_status
;
2144 /* From the client's perspective, all-stop mode always stops all
2145 threads implicitly (and the target backend has already done
2146 so by now). Tag all threads as "want-stopped", so we don't
2147 resume them implicitly without the client telling us to. */
2148 gdb_wants_all_threads_stopped ();
2149 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2150 disable_async_io ();
2152 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2153 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2154 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2159 write_enn (own_buf
);
2164 /* Attach to a new program. Return 1 if successful, 0 if failure. */
2166 handle_v_attach (char *own_buf
)
2170 pid
= strtol (own_buf
+ 8, NULL
, 16);
2171 if (pid
!= 0 && attach_inferior (pid
) == 0)
2173 /* Don't report shared library events after attaching, even if
2174 some libraries are preloaded. GDB will always poll the
2175 library list. Avoids the "stopped by shared library event"
2176 notice on the GDB side. */
2181 /* In non-stop, we don't send a resume reply. Stop events
2182 will follow up using the normal notification
2187 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2193 write_enn (own_buf
);
2198 /* Run a new program. Return 1 if successful, 0 if failure. */
2200 handle_v_run (char *own_buf
)
2202 char *p
, *next_p
, **new_argv
;
2206 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
2212 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
2213 if (new_argv
== NULL
)
2215 write_enn (own_buf
);
2220 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
2222 next_p
= strchr (p
, ';');
2224 next_p
= p
+ strlen (p
);
2226 if (i
== 0 && p
== next_p
)
2230 /* FIXME: Fail request if out of memory instead of dying. */
2231 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
2232 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
2233 new_argv
[i
][(next_p
- p
) / 2] = '\0';
2242 if (new_argv
[0] == NULL
)
2244 /* GDB didn't specify a program to run. Use the program from the
2245 last run with the new argument list. */
2247 if (program_argv
== NULL
)
2249 write_enn (own_buf
);
2250 freeargv (new_argv
);
2254 new_argv
[0] = strdup (program_argv
[0]);
2255 if (new_argv
[0] == NULL
)
2257 write_enn (own_buf
);
2258 freeargv (new_argv
);
2263 /* Free the old argv and install the new one. */
2264 freeargv (program_argv
);
2265 program_argv
= new_argv
;
2267 start_inferior (program_argv
);
2268 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
2270 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2272 /* In non-stop, sending a resume reply doesn't set the general
2273 thread, but GDB assumes a vRun sets it (this is so GDB can
2274 query which is the main thread of the new inferior. */
2276 general_thread
= last_ptid
;
2282 write_enn (own_buf
);
2287 /* Kill process. Return 1 if successful, 0 if failure. */
2289 handle_v_kill (char *own_buf
)
2292 char *p
= &own_buf
[6];
2294 pid
= strtol (p
, NULL
, 16);
2297 if (pid
!= 0 && kill_inferior (pid
) == 0)
2299 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2300 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
2301 last_ptid
= pid_to_ptid (pid
);
2302 discard_queued_stop_replies (pid
);
2308 write_enn (own_buf
);
2313 /* Handle all of the extended 'v' packets. */
2315 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2317 if (!disable_packet_vCont
)
2319 if (strncmp (own_buf
, "vCont;", 6) == 0)
2321 require_running (own_buf
);
2322 handle_v_cont (own_buf
);
2326 if (strncmp (own_buf
, "vCont?", 6) == 0)
2328 strcpy (own_buf
, "vCont;c;C;s;S;t");
2329 if (target_supports_range_stepping ())
2331 own_buf
= own_buf
+ strlen (own_buf
);
2332 strcpy (own_buf
, ";r");
2338 if (strncmp (own_buf
, "vFile:", 6) == 0
2339 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2342 if (strncmp (own_buf
, "vAttach;", 8) == 0)
2344 if ((!extended_protocol
|| !multi_process
) && target_running ())
2346 fprintf (stderr
, "Already debugging a process\n");
2347 write_enn (own_buf
);
2350 handle_v_attach (own_buf
);
2354 if (strncmp (own_buf
, "vRun;", 5) == 0)
2356 if ((!extended_protocol
|| !multi_process
) && target_running ())
2358 fprintf (stderr
, "Already debugging a process\n");
2359 write_enn (own_buf
);
2362 handle_v_run (own_buf
);
2366 if (strncmp (own_buf
, "vKill;", 6) == 0)
2368 if (!target_running ())
2370 fprintf (stderr
, "No process to kill\n");
2371 write_enn (own_buf
);
2374 handle_v_kill (own_buf
);
2378 if (handle_notif_ack (own_buf
, packet_len
))
2381 /* Otherwise we didn't know what packet it was. Say we didn't
2387 /* Resume inferior and wait for another event. In non-stop mode,
2388 don't really wait here, but return immediatelly to the event
2391 myresume (char *own_buf
, int step
, int sig
)
2393 struct thread_resume resume_info
[2];
2395 int valid_cont_thread
;
2397 set_desired_inferior (0);
2399 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2400 && !ptid_equal (cont_thread
, minus_one_ptid
));
2402 if (step
|| sig
|| valid_cont_thread
)
2404 resume_info
[0].thread
= current_ptid
;
2406 resume_info
[0].kind
= resume_step
;
2408 resume_info
[0].kind
= resume_continue
;
2409 resume_info
[0].sig
= sig
;
2413 if (!valid_cont_thread
)
2415 resume_info
[n
].thread
= minus_one_ptid
;
2416 resume_info
[n
].kind
= resume_continue
;
2417 resume_info
[n
].sig
= 0;
2424 (*the_target
->resume
) (resume_info
, n
);
2430 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2432 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2433 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2435 current_inferior
->last_resume_kind
= resume_stop
;
2436 current_inferior
->last_status
= last_status
;
2439 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2440 disable_async_io ();
2442 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2443 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2444 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2448 /* Callback for for_each_inferior. Make a new stop reply for each
2452 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2454 struct thread_info
*thread
= (struct thread_info
*) entry
;
2456 /* For now, assume targets that don't have this callback also don't
2457 manage the thread's last_status field. */
2458 if (the_target
->thread_stopped
== NULL
)
2460 struct vstop_notif
*new_notif
= xmalloc (sizeof (*new_notif
));
2462 new_notif
->ptid
= entry
->id
;
2463 new_notif
->status
= thread
->last_status
;
2464 /* Pass the last stop reply back to GDB, but don't notify
2466 notif_event_enque (¬if_stop
,
2467 (struct notif_event
*) new_notif
);
2471 if (thread_stopped (thread
))
2476 = target_waitstatus_to_string (&thread
->last_status
);
2479 "Reporting thread %s as already stopped with %s\n",
2480 target_pid_to_str (entry
->id
),
2483 xfree (status_string
);
2486 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2488 /* Pass the last stop reply back to GDB, but don't notify
2490 queue_stop_reply (entry
->id
, &thread
->last_status
);
2497 /* Set this inferior threads's state as "want-stopped". We won't
2498 resume this thread until the client gives us another action for
2502 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2504 struct thread_info
*thread
= (struct thread_info
*) entry
;
2506 thread
->last_resume_kind
= resume_stop
;
2508 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2510 /* Most threads are stopped implicitly (all-stop); tag that with
2512 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2513 thread
->last_status
.value
.sig
= GDB_SIGNAL_0
;
2517 /* Set all threads' states as "want-stopped". */
2520 gdb_wants_all_threads_stopped (void)
2522 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2525 /* Clear the gdb_detached flag of every process. */
2528 gdb_reattached_process (struct inferior_list_entry
*entry
)
2530 struct process_info
*process
= (struct process_info
*) entry
;
2532 process
->gdb_detached
= 0;
2535 /* Status handler for the '?' packet. */
2538 handle_status (char *own_buf
)
2540 /* GDB is connected, don't forward events to the target anymore. */
2541 for_each_inferior (&all_processes
, gdb_reattached_process
);
2543 /* In non-stop mode, we must send a stop reply for each stopped
2544 thread. In all-stop mode, just send one for the first stopped
2549 discard_queued_stop_replies (-1);
2550 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2552 /* The first is sent immediatly. OK is sent if there is no
2553 stopped thread, which is the same handling of the vStopped
2554 packet (by design). */
2555 notif_write_event (¬if_stop
, own_buf
);
2560 stabilize_threads ();
2561 gdb_wants_all_threads_stopped ();
2563 if (all_threads
.head
)
2565 struct target_waitstatus status
;
2567 status
.kind
= TARGET_WAITKIND_STOPPED
;
2568 status
.value
.sig
= GDB_SIGNAL_TRAP
;
2569 prepare_resume_reply (own_buf
,
2570 all_threads
.head
->id
, &status
);
2573 strcpy (own_buf
, "W00");
2578 gdbserver_version (void)
2580 printf ("GNU gdbserver %s%s\n"
2581 "Copyright (C) 2013 Free Software Foundation, Inc.\n"
2582 "gdbserver is free software, covered by the "
2583 "GNU General Public License.\n"
2584 "This gdbserver was configured as \"%s\"\n",
2585 PKGVERSION
, version
, host_name
);
2589 gdbserver_usage (FILE *stream
)
2591 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2592 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2593 "\tgdbserver [OPTIONS] --multi COMM\n"
2595 "COMM may either be a tty device (for serial debugging), or \n"
2596 "HOST:PORT to listen for a TCP connection.\n"
2599 " --debug Enable general debugging output.\n"
2600 " --remote-debug Enable remote protocol debugging output.\n"
2601 " --version Display version information and exit.\n"
2602 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
2603 " --once Exit after the first connection has "
2605 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
2606 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
2610 gdbserver_show_disableable (FILE *stream
)
2612 fprintf (stream
, "Disableable packets:\n"
2613 " vCont \tAll vCont packets\n"
2614 " qC \tQuerying the current thread\n"
2615 " qfThreadInfo\tThread listing\n"
2616 " Tthread \tPassing the thread specifier in the "
2617 "T stop reply packet\n"
2618 " threads \tAll of the above\n");
2622 #undef require_running
2623 #define require_running(BUF) \
2624 if (!target_running ()) \
2631 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
2633 int pid
= * (int *) args
;
2635 if (ptid_get_pid (entry
->id
) == pid
)
2642 kill_inferior_callback (struct inferior_list_entry
*entry
)
2644 struct process_info
*process
= (struct process_info
*) entry
;
2645 int pid
= ptid_get_pid (process
->head
.id
);
2647 kill_inferior (pid
);
2648 discard_queued_stop_replies (pid
);
2651 /* Callback for for_each_inferior to detach or kill the inferior,
2652 depending on whether we attached to it or not.
2653 We inform the user whether we're detaching or killing the process
2654 as this is only called when gdbserver is about to exit. */
2657 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
2659 struct process_info
*process
= (struct process_info
*) entry
;
2660 int pid
= ptid_get_pid (process
->head
.id
);
2662 if (process
->attached
)
2663 detach_inferior (pid
);
2665 kill_inferior (pid
);
2667 discard_queued_stop_replies (pid
);
2670 /* for_each_inferior callback for detach_or_kill_for_exit to print
2671 the pids of started inferiors. */
2674 print_started_pid (struct inferior_list_entry
*entry
)
2676 struct process_info
*process
= (struct process_info
*) entry
;
2678 if (! process
->attached
)
2680 int pid
= ptid_get_pid (process
->head
.id
);
2681 fprintf (stderr
, " %d", pid
);
2685 /* for_each_inferior callback for detach_or_kill_for_exit to print
2686 the pids of attached inferiors. */
2689 print_attached_pid (struct inferior_list_entry
*entry
)
2691 struct process_info
*process
= (struct process_info
*) entry
;
2693 if (process
->attached
)
2695 int pid
= ptid_get_pid (process
->head
.id
);
2696 fprintf (stderr
, " %d", pid
);
2700 /* Call this when exiting gdbserver with possible inferiors that need
2701 to be killed or detached from. */
2704 detach_or_kill_for_exit (void)
2706 /* First print a list of the inferiors we will be killing/detaching.
2707 This is to assist the user, for example, in case the inferior unexpectedly
2708 dies after we exit: did we screw up or did the inferior exit on its own?
2709 Having this info will save some head-scratching. */
2711 if (have_started_inferiors_p ())
2713 fprintf (stderr
, "Killing process(es):");
2714 for_each_inferior (&all_processes
, print_started_pid
);
2715 fprintf (stderr
, "\n");
2717 if (have_attached_inferiors_p ())
2719 fprintf (stderr
, "Detaching process(es):");
2720 for_each_inferior (&all_processes
, print_attached_pid
);
2721 fprintf (stderr
, "\n");
2724 /* Now we can kill or detach the inferiors. */
2726 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
2730 main (int argc
, char *argv
[])
2734 char *arg_end
, *port
;
2735 char **next_arg
= &argv
[1];
2736 volatile int multi_mode
= 0;
2737 volatile int attach
= 0;
2740 while (*next_arg
!= NULL
&& **next_arg
== '-')
2742 if (strcmp (*next_arg
, "--version") == 0)
2744 gdbserver_version ();
2747 else if (strcmp (*next_arg
, "--help") == 0)
2749 gdbserver_usage (stdout
);
2752 else if (strcmp (*next_arg
, "--attach") == 0)
2754 else if (strcmp (*next_arg
, "--multi") == 0)
2756 else if (strcmp (*next_arg
, "--wrapper") == 0)
2760 wrapper_argv
= next_arg
;
2761 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
2764 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
2766 gdbserver_usage (stderr
);
2770 /* Consume the "--". */
2773 else if (strcmp (*next_arg
, "--debug") == 0)
2775 else if (strcmp (*next_arg
, "--remote-debug") == 0)
2777 else if (strcmp (*next_arg
, "--disable-packet") == 0)
2779 gdbserver_show_disableable (stdout
);
2782 else if (strncmp (*next_arg
,
2783 "--disable-packet=",
2784 sizeof ("--disable-packet=") - 1) == 0)
2786 char *packets
, *tok
;
2788 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
2789 for (tok
= strtok (packets
, ",");
2791 tok
= strtok (NULL
, ","))
2793 if (strcmp ("vCont", tok
) == 0)
2794 disable_packet_vCont
= 1;
2795 else if (strcmp ("Tthread", tok
) == 0)
2796 disable_packet_Tthread
= 1;
2797 else if (strcmp ("qC", tok
) == 0)
2798 disable_packet_qC
= 1;
2799 else if (strcmp ("qfThreadInfo", tok
) == 0)
2800 disable_packet_qfThreadInfo
= 1;
2801 else if (strcmp ("threads", tok
) == 0)
2803 disable_packet_vCont
= 1;
2804 disable_packet_Tthread
= 1;
2805 disable_packet_qC
= 1;
2806 disable_packet_qfThreadInfo
= 1;
2810 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
2812 gdbserver_show_disableable (stderr
);
2817 else if (strcmp (*next_arg
, "-") == 0)
2819 /* "-" specifies a stdio connection and is a form of port
2821 *next_arg
= STDIO_CONNECTION_NAME
;
2824 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
2825 disable_randomization
= 1;
2826 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
2827 disable_randomization
= 0;
2828 else if (strcmp (*next_arg
, "--once") == 0)
2832 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
2840 if (setjmp (toplevel
))
2842 fprintf (stderr
, "Exiting\n");
2848 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
2850 gdbserver_usage (stderr
);
2854 /* Remember stdio descriptors. LISTEN_DESC must not be listed, it will be
2855 opened by remote_prepare. */
2858 /* We need to know whether the remote connection is stdio before
2859 starting the inferior. Inferiors created in this scenario have
2860 stdin,stdout redirected. So do this here before we call
2862 remote_prepare (port
);
2867 /* --attach used to come after PORT, so allow it there for
2869 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
2876 && (*next_arg
== NULL
2877 || (*next_arg
)[0] == '\0'
2878 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
2880 || next_arg
[1] != NULL
))
2885 gdbserver_usage (stderr
);
2889 initialize_async_io ();
2891 initialize_event_loop ();
2892 if (target_supports_tracepoints ())
2893 initialize_tracepoint ();
2895 own_buf
= xmalloc (PBUFSIZ
+ 1);
2896 mem_buf
= xmalloc (PBUFSIZ
);
2898 if (pid
== 0 && *next_arg
!= NULL
)
2902 n
= argc
- (next_arg
- argv
);
2903 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
2904 for (i
= 0; i
< n
; i
++)
2905 program_argv
[i
] = xstrdup (next_arg
[i
]);
2906 program_argv
[i
] = NULL
;
2908 /* Wait till we are at first instruction in program. */
2909 start_inferior (program_argv
);
2911 /* We are now (hopefully) stopped at the first instruction of
2912 the target process. This assumes that the target process was
2913 successfully created. */
2917 if (attach_inferior (pid
) == -1)
2918 error ("Attaching not supported on this target");
2920 /* Otherwise succeeded. */
2924 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2925 last_status
.value
.integer
= 0;
2926 last_ptid
= minus_one_ptid
;
2929 initialize_notif ();
2931 /* Don't report shared library events on the initial connection,
2932 even if some libraries are preloaded. Avoids the "stopped by
2933 shared library event" notice on gdb side. */
2936 if (setjmp (toplevel
))
2938 /* If something fails and longjmps while detaching or killing
2939 inferiors, we'd end up here again, stuck in an infinite loop
2940 trap. Be sure that if that happens, we exit immediately
2942 if (setjmp (toplevel
) == 0)
2943 detach_or_kill_for_exit ();
2945 fprintf (stderr
, "Detach or kill failed. Exiting\n");
2949 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2950 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2955 if (!was_running
&& !multi_mode
)
2957 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
2965 /* Be sure we're out of tfind mode. */
2966 current_traceframe
= -1;
2970 if (setjmp (toplevel
) != 0)
2972 /* An error occurred. */
2973 if (response_needed
)
2975 write_enn (own_buf
);
2980 /* Wait for events. This will return when all event sources are
2981 removed from the event loop. */
2982 start_event_loop ();
2984 /* If an exit was requested (using the "monitor exit" command),
2985 terminate now. The only other way to get here is for
2986 getpkt to fail; close the connection and reopen it at the
2989 if (exit_requested
|| run_once
)
2991 /* If something fails and longjmps while detaching or
2992 killing inferiors, we'd end up here again, stuck in an
2993 infinite loop trap. Be sure that if that happens, we
2994 exit immediately instead. */
2995 if (setjmp (toplevel
) == 0)
2997 detach_or_kill_for_exit ();
3002 fprintf (stderr
, "Detach or kill failed. Exiting\n");
3008 "Remote side has terminated connection. "
3009 "GDBserver will reopen the connection.\n");
3013 if (disconnected_tracing
)
3015 /* Try to enable non-stop/async mode, so we we can both
3016 wait for an async socket accept, and handle async
3017 target events simultaneously. There's also no point
3018 either in having the target always stop all threads,
3019 when we're going to pass signals down without
3023 if (start_non_stop (1))
3026 /* Detaching implicitly resumes all threads; simply
3027 disconnecting does not. */
3033 "Disconnected tracing disabled; stopping trace run.\n");
3040 /* Process options coming from Z packets for *point at address
3041 POINT_ADDR. PACKET is the packet buffer. *PACKET is updated
3042 to point to the first char after the last processed option. */
3045 process_point_options (CORE_ADDR point_addr
, char **packet
)
3047 char *dataptr
= *packet
;
3050 /* Check if data has the correct format. */
3051 if (*dataptr
!= ';')
3058 if (*dataptr
== ';')
3061 if (*dataptr
== 'X')
3063 /* Conditional expression. */
3065 fprintf (stderr
, "Found breakpoint condition.\n");
3066 add_breakpoint_condition (point_addr
, &dataptr
);
3068 else if (strncmp (dataptr
, "cmds:", strlen ("cmds:")) == 0)
3070 dataptr
+= strlen ("cmds:");
3072 fprintf (stderr
, "Found breakpoint commands %s.\n", dataptr
);
3073 persist
= (*dataptr
== '1');
3075 add_breakpoint_commands (point_addr
, &dataptr
, persist
);
3079 fprintf (stderr
, "Unknown token %c, ignoring.\n",
3081 /* Skip tokens until we find one that we recognize. */
3082 while (*dataptr
&& *dataptr
!= ';')
3089 /* Event loop callback that handles a serial event. The first byte in
3090 the serial buffer gets us here. We expect characters to arrive at
3091 a brisk pace, so we read the rest of the packet with a blocking
3095 process_serial_event (void)
3106 int new_packet_len
= -1;
3108 /* Used to decide when gdbserver should exit in
3109 multi-mode/remote. */
3110 static int have_ran
= 0;
3113 have_ran
= target_running ();
3115 disable_async_io ();
3117 response_needed
= 0;
3118 packet_len
= getpkt (own_buf
);
3119 if (packet_len
<= 0)
3122 /* Force an event loop break. */
3125 response_needed
= 1;
3132 handle_query (own_buf
, packet_len
, &new_packet_len
);
3135 handle_general_set (own_buf
);
3138 require_running (own_buf
);
3143 pid
= strtol (&own_buf
[i
], NULL
, 16);
3146 pid
= ptid_get_pid (current_ptid
);
3148 if ((tracing
&& disconnected_tracing
) || any_persistent_commands ())
3150 struct thread_resume resume_info
;
3151 struct process_info
*process
= find_process_pid (pid
);
3153 if (process
== NULL
)
3155 write_enn (own_buf
);
3159 if (tracing
&& disconnected_tracing
)
3161 "Disconnected tracing in effect, "
3162 "leaving gdbserver attached to the process\n");
3164 if (any_persistent_commands ())
3166 "Persistent commands are present, "
3167 "leaving gdbserver attached to the process\n");
3169 /* Make sure we're in non-stop/async mode, so we we can both
3170 wait for an async socket accept, and handle async target
3171 events simultaneously. There's also no point either in
3172 having the target stop all threads, when we're going to
3173 pass signals down without informing GDB. */
3177 fprintf (stderr
, "Forcing non-stop mode\n");
3183 process
->gdb_detached
= 1;
3185 /* Detaching implicitly resumes all threads. */
3186 resume_info
.thread
= minus_one_ptid
;
3187 resume_info
.kind
= resume_continue
;
3188 resume_info
.sig
= 0;
3189 (*the_target
->resume
) (&resume_info
, 1);
3192 break; /* from switch/case */
3195 fprintf (stderr
, "Detaching from process %d\n", pid
);
3197 if (detach_inferior (pid
) != 0)
3198 write_enn (own_buf
);
3201 discard_queued_stop_replies (pid
);
3204 if (extended_protocol
)
3206 /* Treat this like a normal program exit. */
3207 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3208 last_status
.value
.integer
= 0;
3209 last_ptid
= pid_to_ptid (pid
);
3211 current_inferior
= NULL
;
3218 /* If we are attached, then we can exit. Otherwise, we
3219 need to hang around doing nothing, until the child is
3221 join_inferior (pid
);
3227 extended_protocol
= 1;
3231 handle_status (own_buf
);
3234 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
3236 ptid_t gdb_id
, thread_id
;
3239 require_running (own_buf
);
3241 gdb_id
= read_ptid (&own_buf
[2], NULL
);
3243 pid
= ptid_get_pid (gdb_id
);
3245 if (ptid_equal (gdb_id
, null_ptid
)
3246 || ptid_equal (gdb_id
, minus_one_ptid
))
3247 thread_id
= null_ptid
;
3249 && ptid_equal (pid_to_ptid (pid
),
3252 struct thread_info
*thread
=
3253 (struct thread_info
*) find_inferior (&all_threads
,
3258 write_enn (own_buf
);
3262 thread_id
= ((struct inferior_list_entry
*)thread
)->id
;
3266 thread_id
= gdb_id_to_thread_id (gdb_id
);
3267 if (ptid_equal (thread_id
, null_ptid
))
3269 write_enn (own_buf
);
3274 if (own_buf
[1] == 'g')
3276 if (ptid_equal (thread_id
, null_ptid
))
3278 /* GDB is telling us to choose any thread. Check if
3279 the currently selected thread is still valid. If
3280 it is not, select the first available. */
3281 struct thread_info
*thread
=
3282 (struct thread_info
*) find_inferior_id (&all_threads
,
3285 thread_id
= all_threads
.head
->id
;
3288 general_thread
= thread_id
;
3289 set_desired_inferior (1);
3291 else if (own_buf
[1] == 'c')
3292 cont_thread
= thread_id
;
3298 /* Silently ignore it so that gdb can extend the protocol
3299 without compatibility headaches. */
3304 require_running (own_buf
);
3305 if (current_traceframe
>= 0)
3307 struct regcache
*regcache
3308 = new_register_cache (current_target_desc ());
3310 if (fetch_traceframe_registers (current_traceframe
,
3312 registers_to_string (regcache
, own_buf
);
3314 write_enn (own_buf
);
3315 free_register_cache (regcache
);
3319 struct regcache
*regcache
;
3321 set_desired_inferior (1);
3322 regcache
= get_thread_regcache (current_inferior
, 1);
3323 registers_to_string (regcache
, own_buf
);
3327 require_running (own_buf
);
3328 if (current_traceframe
>= 0)
3329 write_enn (own_buf
);
3332 struct regcache
*regcache
;
3334 set_desired_inferior (1);
3335 regcache
= get_thread_regcache (current_inferior
, 1);
3336 registers_from_string (regcache
, &own_buf
[1]);
3341 require_running (own_buf
);
3342 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
3343 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
3345 write_enn (own_buf
);
3347 convert_int_to_ascii (mem_buf
, own_buf
, res
);
3350 require_running (own_buf
);
3351 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
3352 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
3355 write_enn (own_buf
);
3358 require_running (own_buf
);
3359 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
3360 &mem_addr
, &len
, &mem_buf
) < 0
3361 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
3362 write_enn (own_buf
);
3367 require_running (own_buf
);
3368 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3369 if (gdb_signal_to_host_p (sig
))
3370 signal
= gdb_signal_to_host (sig
);
3373 myresume (own_buf
, 0, signal
);
3376 require_running (own_buf
);
3377 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3378 if (gdb_signal_to_host_p (sig
))
3379 signal
= gdb_signal_to_host (sig
);
3382 myresume (own_buf
, 1, signal
);
3385 require_running (own_buf
);
3387 myresume (own_buf
, 0, signal
);
3390 require_running (own_buf
);
3392 myresume (own_buf
, 1, signal
);
3394 case 'Z': /* insert_ ... */
3396 case 'z': /* remove_ ... */
3401 char type
= own_buf
[1];
3403 const int insert
= ch
== 'Z';
3404 char *p
= &own_buf
[3];
3406 p
= unpack_varlen_hex (p
, &addr
);
3407 len
= strtol (p
+ 1, &dataptr
, 16);
3409 /* Default to unrecognized/unsupported. */
3413 case '0': /* software-breakpoint */
3414 case '1': /* hardware-breakpoint */
3415 case '2': /* write watchpoint */
3416 case '3': /* read watchpoint */
3417 case '4': /* access watchpoint */
3418 require_running (own_buf
);
3419 if (insert
&& the_target
->insert_point
!= NULL
)
3421 /* Insert the breakpoint. If it is already inserted, nothing
3423 res
= (*the_target
->insert_point
) (type
, addr
, len
);
3425 /* GDB may have sent us a list of *point parameters to be
3426 evaluated on the target's side. Read such list here. If we
3427 already have a list of parameters, GDB is telling us to drop
3428 that list and use this one instead. */
3429 if (!res
&& (type
== '0' || type
== '1'))
3431 /* Remove previous conditions. */
3432 clear_gdb_breakpoint_conditions (addr
);
3433 process_point_options (addr
, &dataptr
);
3436 else if (!insert
&& the_target
->remove_point
!= NULL
)
3437 res
= (*the_target
->remove_point
) (type
, addr
, len
);
3449 write_enn (own_buf
);
3453 response_needed
= 0;
3454 if (!target_running ())
3455 /* The packet we received doesn't make sense - but we can't
3456 reply to it, either. */
3459 fprintf (stderr
, "Killing all inferiors\n");
3460 for_each_inferior (&all_processes
, kill_inferior_callback
);
3462 /* When using the extended protocol, we wait with no program
3463 running. The traditional protocol will exit instead. */
3464 if (extended_protocol
)
3466 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3467 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
3475 ptid_t gdb_id
, thread_id
;
3477 require_running (own_buf
);
3479 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3480 thread_id
= gdb_id_to_thread_id (gdb_id
);
3481 if (ptid_equal (thread_id
, null_ptid
))
3483 write_enn (own_buf
);
3487 if (mythread_alive (thread_id
))
3490 write_enn (own_buf
);
3494 response_needed
= 0;
3496 /* Restarting the inferior is only supported in the extended
3498 if (extended_protocol
)
3500 if (target_running ())
3501 for_each_inferior (&all_processes
,
3502 kill_inferior_callback
);
3503 fprintf (stderr
, "GDBserver restarting\n");
3505 /* Wait till we are at 1st instruction in prog. */
3506 if (program_argv
!= NULL
)
3507 start_inferior (program_argv
);
3510 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3511 last_status
.value
.sig
= GDB_SIGNAL_KILL
;
3517 /* It is a request we don't understand. Respond with an
3518 empty packet so that gdb knows that we don't support this
3524 /* Extended (long) request. */
3525 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
3529 /* It is a request we don't understand. Respond with an empty
3530 packet so that gdb knows that we don't support this
3536 if (new_packet_len
!= -1)
3537 putpkt_binary (own_buf
, new_packet_len
);
3541 response_needed
= 0;
3543 if (!extended_protocol
&& have_ran
&& !target_running ())
3545 /* In non-stop, defer exiting until GDB had a chance to query
3546 the whole vStopped list (until it gets an OK). */
3547 if (QUEUE_is_empty (notif_event_p
, notif_stop
.queue
))
3549 fprintf (stderr
, "GDBserver exiting\n");
3561 /* Event-loop callback for serial events. */
3564 handle_serial_event (int err
, gdb_client_data client_data
)
3567 fprintf (stderr
, "handling possible serial event\n");
3569 /* Really handle it. */
3570 if (process_serial_event () < 0)
3573 /* Be sure to not change the selected inferior behind GDB's back.
3574 Important in the non-stop mode asynchronous protocol. */
3575 set_desired_inferior (1);
3580 /* Event-loop callback for target events. */
3583 handle_target_event (int err
, gdb_client_data client_data
)
3586 fprintf (stderr
, "handling possible target event\n");
3588 last_ptid
= mywait (minus_one_ptid
, &last_status
,
3591 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3593 int pid
= ptid_get_pid (last_ptid
);
3594 struct process_info
*process
= find_process_pid (pid
);
3595 int forward_event
= !gdb_connected () || process
->gdb_detached
;
3597 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3598 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3600 mark_breakpoints_out (process
);
3601 mourn_inferior (process
);
3605 /* We're reporting this thread as stopped. Update its
3606 "want-stopped" state to what the client wants, until it
3607 gets a new resume action. */
3608 current_inferior
->last_resume_kind
= resume_stop
;
3609 current_inferior
->last_status
= last_status
;
3614 if (!target_running ())
3616 /* The last process exited. We're done. */
3620 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
3622 /* A thread stopped with a signal, but gdb isn't
3623 connected to handle it. Pass it down to the
3624 inferior, as if it wasn't being traced. */
3625 struct thread_resume resume_info
;
3629 "GDB not connected; forwarding event %d for [%s]\n",
3630 (int) last_status
.kind
,
3631 target_pid_to_str (last_ptid
));
3633 resume_info
.thread
= last_ptid
;
3634 resume_info
.kind
= resume_continue
;
3635 resume_info
.sig
= gdb_signal_to_host (last_status
.value
.sig
);
3636 (*the_target
->resume
) (&resume_info
, 1);
3638 else if (debug_threads
)
3639 fprintf (stderr
, "GDB not connected; ignoring event %d for [%s]\n",
3640 (int) last_status
.kind
,
3641 target_pid_to_str (last_ptid
));
3645 struct vstop_notif
*vstop_notif
3646 = xmalloc (sizeof (struct vstop_notif
));
3648 vstop_notif
->status
= last_status
;
3649 vstop_notif
->ptid
= last_ptid
;
3650 /* Push Stop notification. */
3651 notif_push (¬if_stop
,
3652 (struct notif_event
*) vstop_notif
);
3656 /* Be sure to not change the selected inferior behind GDB's back.
3657 Important in the non-stop mode asynchronous protocol. */
3658 set_desired_inferior (1);