1 /* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993-1995, 1997-2000, 2002-2012 Free Software
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "gdbthread.h"
34 /* The thread set with an `Hc' packet. `Hc' is deprecated in favor of
35 `vCont'. Note the multi-process extensions made `vCont' a
36 requirement, so `Hc pPID.TID' is pretty much undefined. So
37 CONT_THREAD can be null_ptid for no `Hc' thread, minus_one_ptid for
38 resuming all threads of the process (again, `Hc' isn't used for
39 multi-process), or a specific thread ptid_t.
41 We also set this when handling a single-thread `vCont' resume, as
42 some places in the backends check it to know when (and for which
43 thread) single-thread scheduler-locking is in effect. */
46 /* The thread set with an `Hg' packet. */
47 ptid_t general_thread
;
51 static int extended_protocol
;
52 static int response_needed
;
53 static int exit_requested
;
55 /* --once: Exit after the first connection has closed. */
61 /* Whether we should attempt to disable the operating system's address
62 space randomization feature before starting an inferior. */
63 int disable_randomization
= 1;
65 static char **program_argv
, **wrapper_argv
;
67 /* Enable miscellaneous debugging output. The name is historical - it
68 was originally used to debug LinuxThreads support. */
71 /* Enable debugging of h/w breakpoint/watchpoint support. */
74 int pass_signals
[TARGET_SIGNAL_LAST
];
75 int program_signals
[TARGET_SIGNAL_LAST
];
76 int program_signals_p
;
80 const char *gdbserver_xmltarget
;
82 /* The PID of the originally created or attached inferior. Used to
83 send signals to the process when GDB sends us an asynchronous interrupt
84 (user hitting Control-C in the client), and to wait for the child to exit
85 when no longer debugging it. */
87 unsigned long signal_pid
;
90 /* A file descriptor for the controlling terminal. */
93 /* TERMINAL_FD's original foreground group. */
94 pid_t old_foreground_pgrp
;
96 /* Hand back terminal ownership to the original foreground group. */
99 restore_old_foreground_pgrp (void)
101 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
105 /* Set if you want to disable optional thread related packets support
106 in gdbserver, for the sake of testing GDB against stubs that don't
108 int disable_packet_vCont
;
109 int disable_packet_Tthread
;
110 int disable_packet_qC
;
111 int disable_packet_qfThreadInfo
;
113 /* Last status reported to GDB. */
114 static struct target_waitstatus last_status
;
115 static ptid_t last_ptid
;
117 static char *own_buf
;
118 static unsigned char *mem_buf
;
120 /* Structure holding information relative to a single stop reply. We
121 keep a queue of these (really a singly-linked list) to push to GDB
125 /* Pointer to next in list. */
126 struct vstop_notif
*next
;
128 /* Thread or process that got the event. */
132 struct target_waitstatus status
;
135 /* The pending stop replies list head. */
136 static struct vstop_notif
*notif_queue
= NULL
;
138 /* Put a stop reply to the stop reply queue. */
141 queue_stop_reply (ptid_t ptid
, struct target_waitstatus
*status
)
143 struct vstop_notif
*new_notif
;
145 new_notif
= xmalloc (sizeof (*new_notif
));
146 new_notif
->next
= NULL
;
147 new_notif
->ptid
= ptid
;
148 new_notif
->status
= *status
;
152 struct vstop_notif
*tail
;
153 for (tail
= notif_queue
;
157 tail
->next
= new_notif
;
160 notif_queue
= new_notif
;
165 struct vstop_notif
*n
;
167 for (n
= notif_queue
; n
; n
= n
->next
)
170 fprintf (stderr
, "pending stop replies: %d\n", i
);
174 /* Place an event in the stop reply queue, and push a notification if
175 we aren't sending one yet. */
178 push_event (ptid_t ptid
, struct target_waitstatus
*status
)
180 gdb_assert (status
->kind
!= TARGET_WAITKIND_IGNORE
);
182 queue_stop_reply (ptid
, status
);
184 /* If this is the first stop reply in the queue, then inform GDB
185 about it, by sending a Stop notification. */
186 if (notif_queue
->next
== NULL
)
191 prepare_resume_reply (p
,
192 notif_queue
->ptid
, ¬if_queue
->status
);
193 putpkt_notif (own_buf
);
197 /* Get rid of the currently pending stop replies for PID. If PID is
198 -1, then apply to all processes. */
201 discard_queued_stop_replies (int pid
)
203 struct vstop_notif
*prev
= NULL
, *reply
, *next
;
205 for (reply
= notif_queue
; reply
; reply
= next
)
210 || ptid_get_pid (reply
->ptid
) == pid
)
212 if (reply
== notif_queue
)
215 prev
->next
= reply
->next
;
224 /* If there are more stop replies to push, push one now. */
227 send_next_stop_reply (char *own_buf
)
230 prepare_resume_reply (own_buf
,
232 ¬if_queue
->status
);
238 target_running (void)
240 return all_threads
.head
!= NULL
;
244 start_inferior (char **argv
)
246 char **new_argv
= argv
;
248 if (wrapper_argv
!= NULL
)
252 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
254 for (i
= 0; argv
[i
] != NULL
; i
++)
256 new_argv
= alloca (sizeof (char *) * count
);
258 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
259 new_argv
[count
++] = wrapper_argv
[i
];
260 for (i
= 0; argv
[i
] != NULL
; i
++)
261 new_argv
[count
++] = argv
[i
];
262 new_argv
[count
] = NULL
;
268 for (i
= 0; new_argv
[i
]; ++i
)
269 fprintf (stderr
, "new_argv[%d] = \"%s\"\n", i
, new_argv
[i
]);
274 signal (SIGTTOU
, SIG_DFL
);
275 signal (SIGTTIN
, SIG_DFL
);
278 /* Clear this so the backend doesn't get confused, thinking
279 CONT_THREAD died, and it needs to resume all threads. */
280 cont_thread
= null_ptid
;
282 signal_pid
= create_inferior (new_argv
[0], new_argv
);
284 /* FIXME: we don't actually know at this point that the create
285 actually succeeded. We won't know that until we wait. */
286 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
291 signal (SIGTTOU
, SIG_IGN
);
292 signal (SIGTTIN
, SIG_IGN
);
293 terminal_fd
= fileno (stderr
);
294 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
295 tcsetpgrp (terminal_fd
, signal_pid
);
296 atexit (restore_old_foreground_pgrp
);
299 if (wrapper_argv
!= NULL
)
301 struct thread_resume resume_info
;
303 resume_info
.thread
= pid_to_ptid (signal_pid
);
304 resume_info
.kind
= resume_continue
;
307 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
309 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
314 (*the_target
->resume
) (&resume_info
, 1);
316 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
317 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
320 current_inferior
->last_resume_kind
= resume_stop
;
321 current_inferior
->last_status
= last_status
;
323 while (last_status
.value
.sig
!= TARGET_SIGNAL_TRAP
);
325 current_inferior
->last_resume_kind
= resume_stop
;
326 current_inferior
->last_status
= last_status
;
330 /* Wait till we are at 1st instruction in program, return new pid
331 (assuming success). */
332 last_ptid
= mywait (pid_to_ptid (signal_pid
), &last_status
, 0, 0);
334 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
335 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
337 current_inferior
->last_resume_kind
= resume_stop
;
338 current_inferior
->last_status
= last_status
;
345 attach_inferior (int pid
)
347 /* myattach should return -1 if attaching is unsupported,
348 0 if it succeeded, and call error() otherwise. */
350 if (myattach (pid
) != 0)
353 fprintf (stderr
, "Attached; pid = %d\n", pid
);
356 /* FIXME - It may be that we should get the SIGNAL_PID from the
357 attach function, so that it can be the main thread instead of
358 whichever we were told to attach to. */
361 /* Clear this so the backend doesn't get confused, thinking
362 CONT_THREAD died, and it needs to resume all threads. */
363 cont_thread
= null_ptid
;
367 last_ptid
= mywait (pid_to_ptid (pid
), &last_status
, 0, 0);
369 /* GDB knows to ignore the first SIGSTOP after attaching to a running
370 process using the "attach" command, but this is different; it's
371 just using "target remote". Pretend it's just starting up. */
372 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
373 && last_status
.value
.sig
== TARGET_SIGNAL_STOP
)
374 last_status
.value
.sig
= TARGET_SIGNAL_TRAP
;
376 current_inferior
->last_resume_kind
= resume_stop
;
377 current_inferior
->last_status
= last_status
;
383 extern int remote_debug
;
385 /* Decode a qXfer read request. Return 0 if everything looks OK,
389 decode_xfer_read (char *buf
, CORE_ADDR
*ofs
, unsigned int *len
)
391 /* After the read marker and annex, qXfer looks like a
392 traditional 'm' packet. */
393 decode_m_packet (buf
, ofs
, len
);
399 decode_xfer (char *buf
, char **object
, char **rw
, char **annex
, char **offset
)
401 /* Extract and NUL-terminate the object. */
403 while (*buf
&& *buf
!= ':')
409 /* Extract and NUL-terminate the read/write action. */
411 while (*buf
&& *buf
!= ':')
417 /* Extract and NUL-terminate the annex. */
419 while (*buf
&& *buf
!= ':')
429 /* Write the response to a successful qXfer read. Returns the
430 length of the (binary) data stored in BUF, corresponding
431 to as much of DATA/LEN as we could fit. IS_MORE controls
432 the first character of the response. */
434 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
443 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
447 /* Handle all of the extended 'Q' packets. */
450 handle_general_set (char *own_buf
)
452 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
454 int numsigs
= (int) TARGET_SIGNAL_LAST
, i
;
455 const char *p
= own_buf
+ strlen ("QPassSignals:");
458 p
= decode_address_to_semicolon (&cursig
, p
);
459 for (i
= 0; i
< numsigs
; i
++)
465 /* Keep looping, to clear the remaining signals. */
468 p
= decode_address_to_semicolon (&cursig
, p
);
473 strcpy (own_buf
, "OK");
477 if (strncmp ("QProgramSignals:", own_buf
, strlen ("QProgramSignals:")) == 0)
479 int numsigs
= (int) TARGET_SIGNAL_LAST
, i
;
480 const char *p
= own_buf
+ strlen ("QProgramSignals:");
483 program_signals_p
= 1;
485 p
= decode_address_to_semicolon (&cursig
, p
);
486 for (i
= 0; i
< numsigs
; i
++)
490 program_signals
[i
] = 1;
492 /* Keep looping, to clear the remaining signals. */
495 p
= decode_address_to_semicolon (&cursig
, p
);
498 program_signals
[i
] = 0;
500 strcpy (own_buf
, "OK");
504 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
508 fprintf (stderr
, "[noack mode enabled]\n");
517 if (strncmp (own_buf
, "QNonStop:", 9) == 0)
519 char *mode
= own_buf
+ 9;
523 if (strcmp (mode
, "0") == 0)
525 else if (strcmp (mode
, "1") == 0)
529 /* We don't know what this mode is, so complain to
531 fprintf (stderr
, "Unknown non-stop mode requested: %s\n",
537 req_str
= req
? "non-stop" : "all-stop";
538 if (start_non_stop (req
) != 0)
540 fprintf (stderr
, "Setting %s mode failed\n", req_str
);
548 fprintf (stderr
, "[%s mode enabled]\n", req_str
);
554 if (strncmp ("QDisableRandomization:", own_buf
,
555 strlen ("QDisableRandomization:")) == 0)
557 char *packet
= own_buf
+ strlen ("QDisableRandomization:");
560 unpack_varlen_hex (packet
, &setting
);
561 disable_randomization
= setting
;
565 if (disable_randomization
)
566 fprintf (stderr
, "[address space randomization disabled]\n");
568 fprintf (stderr
, "[address space randomization enabled]\n");
575 if (target_supports_tracepoints ()
576 && handle_tracepoint_general_set (own_buf
))
579 if (strncmp ("QAgent:", own_buf
, strlen ("QAgent:")) == 0)
581 char *mode
= own_buf
+ strlen ("QAgent:");
584 if (strcmp (mode
, "0") == 0)
586 else if (strcmp (mode
, "1") == 0)
590 /* We don't know what this value is, so complain to GDB. */
591 sprintf (own_buf
, "E.Unknown QAgent value");
595 /* Update the flag. */
598 fprintf (stderr
, "[%s agent]\n", req
? "Enable" : "Disable");
603 /* Otherwise we didn't know what packet it was. Say we didn't
609 get_features_xml (const char *annex
)
611 /* gdbserver_xmltarget defines what to return when looking
612 for the "target.xml" file. Its contents can either be
613 verbatim XML code (prefixed with a '@') or else the name
614 of the actual XML file to be used in place of "target.xml".
616 This variable is set up from the auto-generated
617 init_registers_... routine for the current target. */
619 if (gdbserver_xmltarget
620 && strcmp (annex
, "target.xml") == 0)
622 if (*gdbserver_xmltarget
== '@')
623 return gdbserver_xmltarget
+ 1;
625 annex
= gdbserver_xmltarget
;
630 extern const char *const xml_builtin
[][2];
633 /* Look for the annex. */
634 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
635 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
638 if (xml_builtin
[i
][0] != NULL
)
639 return xml_builtin
[i
][1];
647 monitor_show_help (void)
649 monitor_output ("The following monitor commands are supported:\n");
650 monitor_output (" set debug <0|1>\n");
651 monitor_output (" Enable general debugging messages\n");
652 monitor_output (" set debug-hw-points <0|1>\n");
653 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
654 monitor_output (" set remote-debug <0|1>\n");
655 monitor_output (" Enable remote protocol debugging messages\n");
656 monitor_output (" exit\n");
657 monitor_output (" Quit GDBserver\n");
660 /* Read trace frame or inferior memory. Returns the number of bytes
661 actually read, zero when no further transfer is possible, and -1 on
662 error. Return of a positive value smaller than LEN does not
663 indicate there's no more to be read, only the end of the transfer.
664 E.g., when GDB reads memory from a traceframe, a first request may
665 be served from a memory block that does not cover the whole request
666 length. A following request gets the rest served from either
667 another block (of the same traceframe) or from the read-only
671 gdb_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
675 if (current_traceframe
>= 0)
678 ULONGEST length
= len
;
680 if (traceframe_read_mem (current_traceframe
,
681 memaddr
, myaddr
, len
, &nbytes
))
683 /* Data read from trace buffer, we're done. */
686 if (!in_readonly_region (memaddr
, length
))
688 /* Otherwise we have a valid readonly case, fall through. */
689 /* (assume no half-trace half-real blocks for now) */
692 res
= prepare_to_access_memory ();
695 res
= read_inferior_memory (memaddr
, myaddr
, len
);
696 done_accessing_memory ();
698 return res
== 0 ? len
: -1;
704 /* Write trace frame or inferior memory. Actually, writing to trace
705 frames is forbidden. */
708 gdb_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
710 if (current_traceframe
>= 0)
716 ret
= prepare_to_access_memory ();
719 ret
= write_inferior_memory (memaddr
, myaddr
, len
);
720 done_accessing_memory ();
726 /* Subroutine of handle_search_memory to simplify it. */
729 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
730 gdb_byte
*pattern
, unsigned pattern_len
,
731 gdb_byte
*search_buf
,
732 unsigned chunk_size
, unsigned search_buf_size
,
733 CORE_ADDR
*found_addrp
)
735 /* Prime the search buffer. */
737 if (gdb_read_memory (start_addr
, search_buf
, search_buf_size
)
740 warning ("Unable to access target memory at 0x%lx, halting search.",
745 /* Perform the search.
747 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
748 When we've scanned N bytes we copy the trailing bytes to the start and
749 read in another N bytes. */
751 while (search_space_len
>= pattern_len
)
754 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
758 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
760 if (found_ptr
!= NULL
)
762 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
763 *found_addrp
= found_addr
;
767 /* Not found in this chunk, skip to next chunk. */
769 /* Don't let search_space_len wrap here, it's unsigned. */
770 if (search_space_len
>= chunk_size
)
771 search_space_len
-= chunk_size
;
773 search_space_len
= 0;
775 if (search_space_len
>= pattern_len
)
777 unsigned keep_len
= search_buf_size
- chunk_size
;
778 CORE_ADDR read_addr
= start_addr
+ chunk_size
+ keep_len
;
781 /* Copy the trailing part of the previous iteration to the front
782 of the buffer for the next iteration. */
783 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
785 nr_to_read
= (search_space_len
- keep_len
< chunk_size
786 ? search_space_len
- keep_len
789 if (gdb_read_memory (read_addr
, search_buf
+ keep_len
,
790 nr_to_read
) != search_buf_size
)
792 warning ("Unable to access target memory "
793 "at 0x%lx, halting search.",
798 start_addr
+= chunk_size
;
807 /* Handle qSearch:memory packets. */
810 handle_search_memory (char *own_buf
, int packet_len
)
812 CORE_ADDR start_addr
;
813 CORE_ADDR search_space_len
;
815 unsigned int pattern_len
;
816 /* NOTE: also defined in find.c testcase. */
817 #define SEARCH_CHUNK_SIZE 16000
818 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
819 /* Buffer to hold memory contents for searching. */
820 gdb_byte
*search_buf
;
821 unsigned search_buf_size
;
823 CORE_ADDR found_addr
;
824 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
826 pattern
= malloc (packet_len
);
829 error ("Unable to allocate memory to perform the search");
830 strcpy (own_buf
, "E00");
833 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
834 packet_len
- cmd_name_len
,
835 &start_addr
, &search_space_len
,
836 pattern
, &pattern_len
) < 0)
839 error ("Error in parsing qSearch:memory packet");
840 strcpy (own_buf
, "E00");
844 search_buf_size
= chunk_size
+ pattern_len
- 1;
846 /* No point in trying to allocate a buffer larger than the search space. */
847 if (search_space_len
< search_buf_size
)
848 search_buf_size
= search_space_len
;
850 search_buf
= malloc (search_buf_size
);
851 if (search_buf
== NULL
)
854 error ("Unable to allocate memory to perform the search");
855 strcpy (own_buf
, "E00");
859 found
= handle_search_memory_1 (start_addr
, search_space_len
,
860 pattern
, pattern_len
,
861 search_buf
, chunk_size
, search_buf_size
,
865 sprintf (own_buf
, "1,%lx", (long) found_addr
);
867 strcpy (own_buf
, "0");
869 strcpy (own_buf
, "E00");
875 #define require_running(BUF) \
876 if (!target_running ()) \
882 /* Handle monitor commands not handled by target-specific handlers. */
885 handle_monitor_command (char *mon
, char *own_buf
)
887 if (strcmp (mon
, "set debug 1") == 0)
890 monitor_output ("Debug output enabled.\n");
892 else if (strcmp (mon
, "set debug 0") == 0)
895 monitor_output ("Debug output disabled.\n");
897 else if (strcmp (mon
, "set debug-hw-points 1") == 0)
900 monitor_output ("H/W point debugging output enabled.\n");
902 else if (strcmp (mon
, "set debug-hw-points 0") == 0)
905 monitor_output ("H/W point debugging output disabled.\n");
907 else if (strcmp (mon
, "set remote-debug 1") == 0)
910 monitor_output ("Protocol debug output enabled.\n");
912 else if (strcmp (mon
, "set remote-debug 0") == 0)
915 monitor_output ("Protocol debug output disabled.\n");
917 else if (strcmp (mon
, "help") == 0)
918 monitor_show_help ();
919 else if (strcmp (mon
, "exit") == 0)
923 monitor_output ("Unknown monitor command.\n\n");
924 monitor_show_help ();
929 /* Associates a callback with each supported qXfer'able object. */
933 /* The object this handler handles. */
936 /* Request that the target transfer up to LEN 8-bit bytes of the
937 target's OBJECT. The OFFSET, for a seekable object, specifies
938 the starting point. The ANNEX can be used to provide additional
939 data-specific information to the target.
941 Return the number of bytes actually transfered, zero when no
942 further transfer is possible, -1 on error, and -2 when the
943 transfer is not supported. Return of a positive value smaller
944 than LEN does not indicate the end of the object, only the end of
947 One, and only one, of readbuf or writebuf must be non-NULL. */
948 int (*xfer
) (const char *annex
,
949 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
950 ULONGEST offset
, LONGEST len
);
953 /* Handle qXfer:auxv:read. */
956 handle_qxfer_auxv (const char *annex
,
957 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
958 ULONGEST offset
, LONGEST len
)
960 if (the_target
->read_auxv
== NULL
|| writebuf
!= NULL
)
963 if (annex
[0] != '\0' || !target_running ())
966 return (*the_target
->read_auxv
) (offset
, readbuf
, len
);
969 /* Handle qXfer:features:read. */
972 handle_qxfer_features (const char *annex
,
973 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
974 ULONGEST offset
, LONGEST len
)
976 const char *document
;
979 if (writebuf
!= NULL
)
982 if (!target_running ())
985 /* Grab the correct annex. */
986 document
= get_features_xml (annex
);
987 if (document
== NULL
)
990 total_len
= strlen (document
);
992 if (offset
> total_len
)
995 if (offset
+ len
> total_len
)
996 len
= total_len
- offset
;
998 memcpy (readbuf
, document
+ offset
, len
);
1002 /* Handle qXfer:libraries:read. */
1005 handle_qxfer_libraries (const char *annex
,
1006 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1007 ULONGEST offset
, LONGEST len
)
1009 unsigned int total_len
;
1011 struct inferior_list_entry
*dll_ptr
;
1013 if (writebuf
!= NULL
)
1016 if (annex
[0] != '\0' || !target_running ())
1019 /* Over-estimate the necessary memory. Assume that every character
1020 in the library name must be escaped. */
1022 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
1023 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
1025 document
= malloc (total_len
);
1026 if (document
== NULL
)
1029 strcpy (document
, "<library-list>\n");
1030 p
= document
+ strlen (document
);
1032 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
1034 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
1037 strcpy (p
, " <library name=\"");
1039 name
= xml_escape_text (dll
->name
);
1043 strcpy (p
, "\"><segment address=\"");
1045 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
1047 strcpy (p
, "\"/></library>\n");
1051 strcpy (p
, "</library-list>\n");
1053 total_len
= strlen (document
);
1055 if (offset
> total_len
)
1061 if (offset
+ len
> total_len
)
1062 len
= total_len
- offset
;
1064 memcpy (readbuf
, document
+ offset
, len
);
1069 /* Handle qXfer:libraries-svr4:read. */
1072 handle_qxfer_libraries_svr4 (const char *annex
,
1073 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1074 ULONGEST offset
, LONGEST len
)
1076 if (writebuf
!= NULL
)
1079 if (annex
[0] != '\0' || !target_running ()
1080 || the_target
->qxfer_libraries_svr4
== NULL
)
1083 return the_target
->qxfer_libraries_svr4 (annex
, readbuf
, writebuf
, offset
, len
);
1086 /* Handle qXfer:osadata:read. */
1089 handle_qxfer_osdata (const char *annex
,
1090 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1091 ULONGEST offset
, LONGEST len
)
1093 if (the_target
->qxfer_osdata
== NULL
|| writebuf
!= NULL
)
1096 return (*the_target
->qxfer_osdata
) (annex
, readbuf
, NULL
, offset
, len
);
1099 /* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1102 handle_qxfer_siginfo (const char *annex
,
1103 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1104 ULONGEST offset
, LONGEST len
)
1106 if (the_target
->qxfer_siginfo
== NULL
)
1109 if (annex
[0] != '\0' || !target_running ())
1112 return (*the_target
->qxfer_siginfo
) (annex
, readbuf
, writebuf
, offset
, len
);
1115 /* Handle qXfer:spu:read and qXfer:spu:write. */
1118 handle_qxfer_spu (const char *annex
,
1119 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1120 ULONGEST offset
, LONGEST len
)
1122 if (the_target
->qxfer_spu
== NULL
)
1125 if (!target_running ())
1128 return (*the_target
->qxfer_spu
) (annex
, readbuf
, writebuf
, offset
, len
);
1131 /* Handle qXfer:statictrace:read. */
1134 handle_qxfer_statictrace (const char *annex
,
1135 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1136 ULONGEST offset
, LONGEST len
)
1140 if (writebuf
!= NULL
)
1143 if (annex
[0] != '\0' || !target_running () || current_traceframe
== -1)
1146 if (traceframe_read_sdata (current_traceframe
, offset
,
1147 readbuf
, len
, &nbytes
))
1152 /* Helper for handle_qxfer_threads. */
1155 handle_qxfer_threads_proper (struct buffer
*buffer
)
1157 struct inferior_list_entry
*thread
;
1159 buffer_grow_str (buffer
, "<threads>\n");
1161 for (thread
= all_threads
.head
; thread
; thread
= thread
->next
)
1163 ptid_t ptid
= thread_to_gdb_id ((struct thread_info
*)thread
);
1165 int core
= target_core_of_thread (ptid
);
1168 write_ptid (ptid_s
, ptid
);
1172 sprintf (core_s
, "%d", core
);
1173 buffer_xml_printf (buffer
, "<thread id=\"%s\" core=\"%s\"/>\n",
1178 buffer_xml_printf (buffer
, "<thread id=\"%s\"/>\n",
1183 buffer_grow_str0 (buffer
, "</threads>\n");
1186 /* Handle qXfer:threads:read. */
1189 handle_qxfer_threads (const char *annex
,
1190 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1191 ULONGEST offset
, LONGEST len
)
1193 static char *result
= 0;
1194 static unsigned int result_length
= 0;
1196 if (writebuf
!= NULL
)
1199 if (!target_running () || annex
[0] != '\0')
1204 struct buffer buffer
;
1205 /* When asked for data at offset 0, generate everything and store into
1206 'result'. Successive reads will be served off 'result'. */
1210 buffer_init (&buffer
);
1212 handle_qxfer_threads_proper (&buffer
);
1214 result
= buffer_finish (&buffer
);
1215 result_length
= strlen (result
);
1216 buffer_free (&buffer
);
1219 if (offset
>= result_length
)
1221 /* We're out of data. */
1228 if (len
> result_length
- offset
)
1229 len
= result_length
- offset
;
1231 memcpy (readbuf
, result
+ offset
, len
);
1236 /* Handle qXfer:traceframe-info:read. */
1239 handle_qxfer_traceframe_info (const char *annex
,
1240 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1241 ULONGEST offset
, LONGEST len
)
1243 static char *result
= 0;
1244 static unsigned int result_length
= 0;
1246 if (writebuf
!= NULL
)
1249 if (!target_running () || annex
[0] != '\0' || current_traceframe
== -1)
1254 struct buffer buffer
;
1256 /* When asked for data at offset 0, generate everything and
1257 store into 'result'. Successive reads will be served off
1261 buffer_init (&buffer
);
1263 traceframe_read_info (current_traceframe
, &buffer
);
1265 result
= buffer_finish (&buffer
);
1266 result_length
= strlen (result
);
1267 buffer_free (&buffer
);
1270 if (offset
>= result_length
)
1272 /* We're out of data. */
1279 if (len
> result_length
- offset
)
1280 len
= result_length
- offset
;
1282 memcpy (readbuf
, result
+ offset
, len
);
1286 /* Handle qXfer:fdpic:read. */
1289 handle_qxfer_fdpic (const char *annex
, gdb_byte
*readbuf
,
1290 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1292 if (the_target
->read_loadmap
== NULL
)
1295 if (!target_running ())
1298 return (*the_target
->read_loadmap
) (annex
, offset
, readbuf
, len
);
1301 static const struct qxfer qxfer_packets
[] =
1303 { "auxv", handle_qxfer_auxv
},
1304 { "fdpic", handle_qxfer_fdpic
},
1305 { "features", handle_qxfer_features
},
1306 { "libraries", handle_qxfer_libraries
},
1307 { "libraries-svr4", handle_qxfer_libraries_svr4
},
1308 { "osdata", handle_qxfer_osdata
},
1309 { "siginfo", handle_qxfer_siginfo
},
1310 { "spu", handle_qxfer_spu
},
1311 { "statictrace", handle_qxfer_statictrace
},
1312 { "threads", handle_qxfer_threads
},
1313 { "traceframe-info", handle_qxfer_traceframe_info
},
1317 handle_qxfer (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1325 if (strncmp (own_buf
, "qXfer:", 6) != 0)
1328 /* Grab the object, r/w and annex. */
1329 if (decode_xfer (own_buf
+ 6, &object
, &rw
, &annex
, &offset
) < 0)
1331 write_enn (own_buf
);
1336 i
< sizeof (qxfer_packets
) / sizeof (qxfer_packets
[0]);
1339 const struct qxfer
*q
= &qxfer_packets
[i
];
1341 if (strcmp (object
, q
->object
) == 0)
1343 if (strcmp (rw
, "read") == 0)
1345 unsigned char *data
;
1350 /* Grab the offset and length. */
1351 if (decode_xfer_read (offset
, &ofs
, &len
) < 0)
1353 write_enn (own_buf
);
1357 /* Read one extra byte, as an indicator of whether there is
1359 if (len
> PBUFSIZ
- 2)
1361 data
= malloc (len
+ 1);
1364 write_enn (own_buf
);
1367 n
= (*q
->xfer
) (annex
, data
, NULL
, ofs
, len
+ 1);
1374 write_enn (own_buf
);
1376 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
1378 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
1383 else if (strcmp (rw
, "write") == 0)
1388 unsigned char *data
;
1390 strcpy (own_buf
, "E00");
1391 data
= malloc (packet_len
- (offset
- own_buf
));
1394 write_enn (own_buf
);
1397 if (decode_xfer_write (offset
, packet_len
- (offset
- own_buf
),
1398 &ofs
, &len
, data
) < 0)
1401 write_enn (own_buf
);
1405 n
= (*q
->xfer
) (annex
, NULL
, data
, ofs
, len
);
1412 write_enn (own_buf
);
1414 sprintf (own_buf
, "%x", n
);
1427 /* Table used by the crc32 function to calcuate the checksum. */
1429 static unsigned int crc32_table
[256] =
1432 /* Compute 32 bit CRC from inferior memory.
1434 On success, return 32 bit CRC.
1435 On failure, return (unsigned long long) -1. */
1437 static unsigned long long
1438 crc32 (CORE_ADDR base
, int len
, unsigned int crc
)
1440 if (!crc32_table
[1])
1442 /* Initialize the CRC table and the decoding table. */
1446 for (i
= 0; i
< 256; i
++)
1448 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
1449 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
1456 unsigned char byte
= 0;
1458 /* Return failure if memory read fails. */
1459 if (read_inferior_memory (base
, &byte
, 1) != 0)
1460 return (unsigned long long) -1;
1462 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ byte
) & 255];
1465 return (unsigned long long) crc
;
1468 /* Handle all of the extended 'q' packets. */
1471 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
1473 static struct inferior_list_entry
*thread_ptr
;
1475 /* Reply the current thread id. */
1476 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
1479 require_running (own_buf
);
1481 if (!ptid_equal (general_thread
, null_ptid
)
1482 && !ptid_equal (general_thread
, minus_one_ptid
))
1483 gdb_id
= general_thread
;
1486 thread_ptr
= all_threads
.head
;
1487 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1490 sprintf (own_buf
, "QC");
1492 write_ptid (own_buf
, gdb_id
);
1496 if (strcmp ("qSymbol::", own_buf
) == 0)
1498 /* GDB is suggesting new symbols have been loaded. This may
1499 mean a new shared library has been detected as loaded, so
1500 take the opportunity to check if breakpoints we think are
1501 inserted, still are. Note that it isn't guaranteed that
1502 we'll see this when a shared library is loaded, and nor will
1503 we see this for unloads (although breakpoints in unloaded
1504 libraries shouldn't trigger), as GDB may not find symbols for
1505 the library at all. We also re-validate breakpoints when we
1506 see a second GDB breakpoint for the same address, and or when
1507 we access breakpoint shadows. */
1508 validate_breakpoints ();
1510 if (target_supports_tracepoints ())
1511 tracepoint_look_up_symbols ();
1513 if (target_running () && the_target
->look_up_symbols
!= NULL
)
1514 (*the_target
->look_up_symbols
) ();
1516 strcpy (own_buf
, "OK");
1520 if (!disable_packet_qfThreadInfo
)
1522 if (strcmp ("qfThreadInfo", own_buf
) == 0)
1526 require_running (own_buf
);
1527 thread_ptr
= all_threads
.head
;
1530 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1531 write_ptid (own_buf
, gdb_id
);
1532 thread_ptr
= thread_ptr
->next
;
1536 if (strcmp ("qsThreadInfo", own_buf
) == 0)
1540 require_running (own_buf
);
1541 if (thread_ptr
!= NULL
)
1544 gdb_id
= thread_to_gdb_id ((struct thread_info
*)thread_ptr
);
1545 write_ptid (own_buf
, gdb_id
);
1546 thread_ptr
= thread_ptr
->next
;
1551 sprintf (own_buf
, "l");
1557 if (the_target
->read_offsets
!= NULL
1558 && strcmp ("qOffsets", own_buf
) == 0)
1560 CORE_ADDR text
, data
;
1562 require_running (own_buf
);
1563 if (the_target
->read_offsets (&text
, &data
))
1564 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
1565 (long)text
, (long)data
, (long)data
);
1567 write_enn (own_buf
);
1572 /* Protocol features query. */
1573 if (strncmp ("qSupported", own_buf
, 10) == 0
1574 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
1576 char *p
= &own_buf
[10];
1577 int gdb_supports_qRelocInsn
= 0;
1579 /* Start processing qSupported packet. */
1580 target_process_qsupported (NULL
);
1582 /* Process each feature being provided by GDB. The first
1583 feature will follow a ':', and latter features will follow
1587 char **qsupported
= NULL
;
1591 /* Two passes, to avoid nested strtok calls in
1592 target_process_qsupported. */
1593 for (p
= strtok (p
+ 1, ";");
1595 p
= strtok (NULL
, ";"))
1598 qsupported
= xrealloc (qsupported
, count
* sizeof (char *));
1599 qsupported
[count
- 1] = xstrdup (p
);
1602 for (i
= 0; i
< count
; i
++)
1605 if (strcmp (p
, "multiprocess+") == 0)
1607 /* GDB supports and wants multi-process support if
1609 if (target_supports_multi_process ())
1612 else if (strcmp (p
, "qRelocInsn+") == 0)
1614 /* GDB supports relocate instruction requests. */
1615 gdb_supports_qRelocInsn
= 1;
1618 target_process_qsupported (p
);
1627 "PacketSize=%x;QPassSignals+;QProgramSignals+",
1630 if (the_target
->qxfer_libraries_svr4
!= NULL
)
1631 strcat (own_buf
, ";qXfer:libraries-svr4:read+");
1634 /* We do not have any hook to indicate whether the non-SVR4 target
1635 backend supports qXfer:libraries:read, so always report it. */
1636 strcat (own_buf
, ";qXfer:libraries:read+");
1639 if (the_target
->read_auxv
!= NULL
)
1640 strcat (own_buf
, ";qXfer:auxv:read+");
1642 if (the_target
->qxfer_spu
!= NULL
)
1643 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
1645 if (the_target
->qxfer_siginfo
!= NULL
)
1646 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1648 if (the_target
->read_loadmap
!= NULL
)
1649 strcat (own_buf
, ";qXfer:fdpic:read+");
1651 /* We always report qXfer:features:read, as targets may
1652 install XML files on a subsequent call to arch_setup.
1653 If we reported to GDB on startup that we don't support
1654 qXfer:feature:read at all, we will never be re-queried. */
1655 strcat (own_buf
, ";qXfer:features:read+");
1657 if (transport_is_reliable
)
1658 strcat (own_buf
, ";QStartNoAckMode+");
1660 if (the_target
->qxfer_osdata
!= NULL
)
1661 strcat (own_buf
, ";qXfer:osdata:read+");
1663 if (target_supports_multi_process ())
1664 strcat (own_buf
, ";multiprocess+");
1666 if (target_supports_non_stop ())
1667 strcat (own_buf
, ";QNonStop+");
1669 if (target_supports_disable_randomization ())
1670 strcat (own_buf
, ";QDisableRandomization+");
1672 strcat (own_buf
, ";qXfer:threads:read+");
1674 if (target_supports_tracepoints ())
1676 strcat (own_buf
, ";ConditionalTracepoints+");
1677 strcat (own_buf
, ";TraceStateVariables+");
1678 strcat (own_buf
, ";TracepointSource+");
1679 strcat (own_buf
, ";DisconnectedTracing+");
1680 if (gdb_supports_qRelocInsn
&& target_supports_fast_tracepoints ())
1681 strcat (own_buf
, ";FastTracepoints+");
1682 strcat (own_buf
, ";StaticTracepoints+");
1683 strcat (own_buf
, ";InstallInTrace+");
1684 strcat (own_buf
, ";qXfer:statictrace:read+");
1685 strcat (own_buf
, ";qXfer:traceframe-info:read+");
1686 strcat (own_buf
, ";EnableDisableTracepoints+");
1687 strcat (own_buf
, ";tracenz+");
1690 /* Support target-side breakpoint conditions. */
1691 strcat (own_buf
, ";ConditionalBreakpoints+");
1693 if (target_supports_agent ())
1694 strcat (own_buf
, ";QAgent+");
1699 /* Thread-local storage support. */
1700 if (the_target
->get_tls_address
!= NULL
1701 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
1703 char *p
= own_buf
+ 12;
1704 CORE_ADDR parts
[2], address
= 0;
1706 ptid_t ptid
= null_ptid
;
1708 require_running (own_buf
);
1710 for (i
= 0; i
< 3; i
++)
1718 p2
= strchr (p
, ',');
1731 ptid
= read_ptid (p
, NULL
);
1733 decode_address (&parts
[i
- 1], p
, len
);
1737 if (p
!= NULL
|| i
< 3)
1741 struct thread_info
*thread
= find_thread_ptid (ptid
);
1746 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
1752 strcpy (own_buf
, paddress(address
));
1757 write_enn (own_buf
);
1761 /* Otherwise, pretend we do not understand this packet. */
1764 /* Windows OS Thread Information Block address support. */
1765 if (the_target
->get_tib_address
!= NULL
1766 && strncmp ("qGetTIBAddr:", own_buf
, 12) == 0)
1771 ptid_t ptid
= read_ptid (own_buf
+ 12, &annex
);
1773 n
= (*the_target
->get_tib_address
) (ptid
, &tlb
);
1776 strcpy (own_buf
, paddress(tlb
));
1781 write_enn (own_buf
);
1787 /* Handle "monitor" commands. */
1788 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
1790 char *mon
= malloc (PBUFSIZ
);
1791 int len
= strlen (own_buf
+ 6);
1795 write_enn (own_buf
);
1799 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
1801 write_enn (own_buf
);
1805 mon
[len
/ 2] = '\0';
1809 if (the_target
->handle_monitor_command
== NULL
1810 || (*the_target
->handle_monitor_command
) (mon
) == 0)
1811 /* Default processing. */
1812 handle_monitor_command (mon
, own_buf
);
1818 if (strncmp ("qSearch:memory:", own_buf
,
1819 sizeof ("qSearch:memory:") - 1) == 0)
1821 require_running (own_buf
);
1822 handle_search_memory (own_buf
, packet_len
);
1826 if (strcmp (own_buf
, "qAttached") == 0
1827 || strncmp (own_buf
, "qAttached:", sizeof ("qAttached:") - 1) == 0)
1829 struct process_info
*process
;
1831 if (own_buf
[sizeof ("qAttached") - 1])
1833 int pid
= strtoul (own_buf
+ sizeof ("qAttached:") - 1, NULL
, 16);
1834 process
= (struct process_info
*)
1835 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
1839 require_running (own_buf
);
1840 process
= current_process ();
1843 if (process
== NULL
)
1845 write_enn (own_buf
);
1849 strcpy (own_buf
, process
->attached
? "1" : "0");
1853 if (strncmp ("qCRC:", own_buf
, 5) == 0)
1855 /* CRC check (compare-section). */
1859 unsigned long long crc
;
1861 require_running (own_buf
);
1862 base
= strtoul (own_buf
+ 5, &comma
, 16);
1863 if (*comma
++ != ',')
1865 write_enn (own_buf
);
1868 len
= strtoul (comma
, NULL
, 16);
1869 crc
= crc32 (base
, len
, 0xffffffff);
1870 /* Check for memory failure. */
1871 if (crc
== (unsigned long long) -1)
1873 write_enn (own_buf
);
1876 sprintf (own_buf
, "C%lx", (unsigned long) crc
);
1880 if (handle_qxfer (own_buf
, packet_len
, new_packet_len_p
))
1883 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf
))
1886 /* Otherwise we didn't know what packet it was. Say we didn't
1891 static void gdb_wants_all_threads_stopped (void);
1893 /* Parse vCont packets. */
1895 handle_v_cont (char *own_buf
)
1899 struct thread_resume
*resume_info
;
1900 struct thread_resume default_action
= {{0}};
1902 /* Count the number of semicolons in the packet. There should be one
1903 for every action. */
1909 p
= strchr (p
, ';');
1912 resume_info
= malloc (n
* sizeof (resume_info
[0]));
1913 if (resume_info
== NULL
)
1921 if (p
[0] == 's' || p
[0] == 'S')
1922 resume_info
[i
].kind
= resume_step
;
1923 else if (p
[0] == 'c' || p
[0] == 'C')
1924 resume_info
[i
].kind
= resume_continue
;
1925 else if (p
[0] == 't')
1926 resume_info
[i
].kind
= resume_stop
;
1930 if (p
[0] == 'S' || p
[0] == 'C')
1933 sig
= strtol (p
+ 1, &q
, 16);
1938 if (!target_signal_to_host_p (sig
))
1940 resume_info
[i
].sig
= target_signal_to_host (sig
);
1944 resume_info
[i
].sig
= 0;
1950 resume_info
[i
].thread
= minus_one_ptid
;
1951 default_action
= resume_info
[i
];
1953 /* Note: we don't increment i here, we'll overwrite this entry
1954 the next time through. */
1956 else if (p
[0] == ':')
1958 ptid_t ptid
= read_ptid (p
+ 1, &q
);
1963 if (p
[0] != ';' && p
[0] != 0)
1966 resume_info
[i
].thread
= ptid
;
1973 resume_info
[i
] = default_action
;
1975 /* `cont_thread' is still used in occasional places in the backend,
1976 to implement single-thread scheduler-locking. Doesn't make sense
1977 to set it if we see a stop request, or any form of wildcard
1980 && !(ptid_equal (resume_info
[0].thread
, minus_one_ptid
)
1981 || ptid_get_lwp (resume_info
[0].thread
) == -1)
1982 && resume_info
[0].kind
!= resume_stop
)
1983 cont_thread
= resume_info
[0].thread
;
1985 cont_thread
= minus_one_ptid
;
1986 set_desired_inferior (0);
1991 (*the_target
->resume
) (resume_info
, n
);
1999 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2001 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2002 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2003 current_inferior
->last_status
= last_status
;
2005 /* From the client's perspective, all-stop mode always stops all
2006 threads implicitly (and the target backend has already done
2007 so by now). Tag all threads as "want-stopped", so we don't
2008 resume them implicitly without the client telling us to. */
2009 gdb_wants_all_threads_stopped ();
2010 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2011 disable_async_io ();
2013 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2014 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2015 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2020 write_enn (own_buf
);
2025 /* Attach to a new program. Return 1 if successful, 0 if failure. */
2027 handle_v_attach (char *own_buf
)
2031 pid
= strtol (own_buf
+ 8, NULL
, 16);
2032 if (pid
!= 0 && attach_inferior (pid
) == 0)
2034 /* Don't report shared library events after attaching, even if
2035 some libraries are preloaded. GDB will always poll the
2036 library list. Avoids the "stopped by shared library event"
2037 notice on the GDB side. */
2042 /* In non-stop, we don't send a resume reply. Stop events
2043 will follow up using the normal notification
2048 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2054 write_enn (own_buf
);
2059 /* Run a new program. Return 1 if successful, 0 if failure. */
2061 handle_v_run (char *own_buf
)
2063 char *p
, *next_p
, **new_argv
;
2067 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
2073 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
2074 if (new_argv
== NULL
)
2076 write_enn (own_buf
);
2081 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
2083 next_p
= strchr (p
, ';');
2085 next_p
= p
+ strlen (p
);
2087 if (i
== 0 && p
== next_p
)
2091 /* FIXME: Fail request if out of memory instead of dying. */
2092 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
2093 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
2094 new_argv
[i
][(next_p
- p
) / 2] = '\0';
2103 if (new_argv
[0] == NULL
)
2105 /* GDB didn't specify a program to run. Use the program from the
2106 last run with the new argument list. */
2108 if (program_argv
== NULL
)
2110 write_enn (own_buf
);
2111 freeargv (new_argv
);
2115 new_argv
[0] = strdup (program_argv
[0]);
2116 if (new_argv
[0] == NULL
)
2118 write_enn (own_buf
);
2119 freeargv (new_argv
);
2124 /* Free the old argv and install the new one. */
2125 freeargv (program_argv
);
2126 program_argv
= new_argv
;
2128 start_inferior (program_argv
);
2129 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
2131 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2133 /* In non-stop, sending a resume reply doesn't set the general
2134 thread, but GDB assumes a vRun sets it (this is so GDB can
2135 query which is the main thread of the new inferior. */
2137 general_thread
= last_ptid
;
2143 write_enn (own_buf
);
2148 /* Kill process. Return 1 if successful, 0 if failure. */
2150 handle_v_kill (char *own_buf
)
2153 char *p
= &own_buf
[6];
2155 pid
= strtol (p
, NULL
, 16);
2158 if (pid
!= 0 && kill_inferior (pid
) == 0)
2160 last_status
.kind
= TARGET_WAITKIND_SIGNALLED
;
2161 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
2162 last_ptid
= pid_to_ptid (pid
);
2163 discard_queued_stop_replies (pid
);
2169 write_enn (own_buf
);
2174 /* Handle a 'vStopped' packet. */
2176 handle_v_stopped (char *own_buf
)
2178 /* If we're waiting for GDB to acknowledge a pending stop reply,
2179 consider that done. */
2182 struct vstop_notif
*head
;
2185 fprintf (stderr
, "vStopped: acking %s\n",
2186 target_pid_to_str (notif_queue
->ptid
));
2189 notif_queue
= notif_queue
->next
;
2193 /* Push another stop reply, or if there are no more left, an OK. */
2194 send_next_stop_reply (own_buf
);
2197 /* Handle all of the extended 'v' packets. */
2199 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
2201 if (!disable_packet_vCont
)
2203 if (strncmp (own_buf
, "vCont;", 6) == 0)
2205 require_running (own_buf
);
2206 handle_v_cont (own_buf
);
2210 if (strncmp (own_buf
, "vCont?", 6) == 0)
2212 strcpy (own_buf
, "vCont;c;C;s;S;t");
2217 if (strncmp (own_buf
, "vFile:", 6) == 0
2218 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
2221 if (strncmp (own_buf
, "vAttach;", 8) == 0)
2223 if ((!extended_protocol
|| !multi_process
) && target_running ())
2225 fprintf (stderr
, "Already debugging a process\n");
2226 write_enn (own_buf
);
2229 handle_v_attach (own_buf
);
2233 if (strncmp (own_buf
, "vRun;", 5) == 0)
2235 if ((!extended_protocol
|| !multi_process
) && target_running ())
2237 fprintf (stderr
, "Already debugging a process\n");
2238 write_enn (own_buf
);
2241 handle_v_run (own_buf
);
2245 if (strncmp (own_buf
, "vKill;", 6) == 0)
2247 if (!target_running ())
2249 fprintf (stderr
, "No process to kill\n");
2250 write_enn (own_buf
);
2253 handle_v_kill (own_buf
);
2257 if (strncmp (own_buf
, "vStopped", 8) == 0)
2259 handle_v_stopped (own_buf
);
2263 /* Otherwise we didn't know what packet it was. Say we didn't
2269 /* Resume inferior and wait for another event. In non-stop mode,
2270 don't really wait here, but return immediatelly to the event
2273 myresume (char *own_buf
, int step
, int sig
)
2275 struct thread_resume resume_info
[2];
2277 int valid_cont_thread
;
2279 set_desired_inferior (0);
2281 valid_cont_thread
= (!ptid_equal (cont_thread
, null_ptid
)
2282 && !ptid_equal (cont_thread
, minus_one_ptid
));
2284 if (step
|| sig
|| valid_cont_thread
)
2286 resume_info
[0].thread
2287 = ((struct inferior_list_entry
*) current_inferior
)->id
;
2289 resume_info
[0].kind
= resume_step
;
2291 resume_info
[0].kind
= resume_continue
;
2292 resume_info
[0].sig
= sig
;
2296 if (!valid_cont_thread
)
2298 resume_info
[n
].thread
= minus_one_ptid
;
2299 resume_info
[n
].kind
= resume_continue
;
2300 resume_info
[n
].sig
= 0;
2307 (*the_target
->resume
) (resume_info
, n
);
2313 last_ptid
= mywait (minus_one_ptid
, &last_status
, 0, 1);
2315 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
2316 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
2318 current_inferior
->last_resume_kind
= resume_stop
;
2319 current_inferior
->last_status
= last_status
;
2322 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
2323 disable_async_io ();
2325 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2326 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2327 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid
)));
2331 /* Callback for for_each_inferior. Make a new stop reply for each
2335 queue_stop_reply_callback (struct inferior_list_entry
*entry
, void *arg
)
2337 struct thread_info
*thread
= (struct thread_info
*) entry
;
2339 /* For now, assume targets that don't have this callback also don't
2340 manage the thread's last_status field. */
2341 if (the_target
->thread_stopped
== NULL
)
2343 /* Pass the last stop reply back to GDB, but don't notify
2345 queue_stop_reply (entry
->id
, &thread
->last_status
);
2349 if (thread_stopped (thread
))
2353 "Reporting thread %s as already stopped with %s\n",
2354 target_pid_to_str (entry
->id
),
2355 target_waitstatus_to_string (&thread
->last_status
));
2357 gdb_assert (thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
);
2359 /* Pass the last stop reply back to GDB, but don't notify
2361 queue_stop_reply (entry
->id
, &thread
->last_status
);
2368 /* Set this inferior threads's state as "want-stopped". We won't
2369 resume this thread until the client gives us another action for
2373 gdb_wants_thread_stopped (struct inferior_list_entry
*entry
)
2375 struct thread_info
*thread
= (struct thread_info
*) entry
;
2377 thread
->last_resume_kind
= resume_stop
;
2379 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2381 /* Most threads are stopped implicitly (all-stop); tag that with
2383 thread
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
2384 thread
->last_status
.value
.sig
= TARGET_SIGNAL_0
;
2388 /* Set all threads' states as "want-stopped". */
2391 gdb_wants_all_threads_stopped (void)
2393 for_each_inferior (&all_threads
, gdb_wants_thread_stopped
);
2396 /* Clear the gdb_detached flag of every process. */
2399 gdb_reattached_process (struct inferior_list_entry
*entry
)
2401 struct process_info
*process
= (struct process_info
*) entry
;
2403 process
->gdb_detached
= 0;
2406 /* Status handler for the '?' packet. */
2409 handle_status (char *own_buf
)
2411 /* GDB is connected, don't forward events to the target anymore. */
2412 for_each_inferior (&all_processes
, gdb_reattached_process
);
2414 /* In non-stop mode, we must send a stop reply for each stopped
2415 thread. In all-stop mode, just send one for the first stopped
2420 discard_queued_stop_replies (-1);
2421 find_inferior (&all_threads
, queue_stop_reply_callback
, NULL
);
2423 /* The first is sent immediatly. OK is sent if there is no
2424 stopped thread, which is the same handling of the vStopped
2425 packet (by design). */
2426 send_next_stop_reply (own_buf
);
2431 stabilize_threads ();
2432 gdb_wants_all_threads_stopped ();
2434 if (all_threads
.head
)
2436 struct target_waitstatus status
;
2438 status
.kind
= TARGET_WAITKIND_STOPPED
;
2439 status
.value
.sig
= TARGET_SIGNAL_TRAP
;
2440 prepare_resume_reply (own_buf
,
2441 all_threads
.head
->id
, &status
);
2444 strcpy (own_buf
, "W00");
2449 gdbserver_version (void)
2451 printf ("GNU gdbserver %s%s\n"
2452 "Copyright (C) 2012 Free Software Foundation, Inc.\n"
2453 "gdbserver is free software, covered by the "
2454 "GNU General Public License.\n"
2455 "This gdbserver was configured as \"%s\"\n",
2456 PKGVERSION
, version
, host_name
);
2460 gdbserver_usage (FILE *stream
)
2462 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
2463 "\tgdbserver [OPTIONS] --attach COMM PID\n"
2464 "\tgdbserver [OPTIONS] --multi COMM\n"
2466 "COMM may either be a tty device (for serial debugging), or \n"
2467 "HOST:PORT to listen for a TCP connection.\n"
2470 " --debug Enable general debugging output.\n"
2471 " --remote-debug Enable remote protocol debugging output.\n"
2472 " --version Display version information and exit.\n"
2473 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
2474 " --once Exit after the first connection has "
2476 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
2477 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
2481 gdbserver_show_disableable (FILE *stream
)
2483 fprintf (stream
, "Disableable packets:\n"
2484 " vCont \tAll vCont packets\n"
2485 " qC \tQuerying the current thread\n"
2486 " qfThreadInfo\tThread listing\n"
2487 " Tthread \tPassing the thread specifier in the "
2488 "T stop reply packet\n"
2489 " threads \tAll of the above\n");
2493 #undef require_running
2494 #define require_running(BUF) \
2495 if (!target_running ()) \
2502 first_thread_of (struct inferior_list_entry
*entry
, void *args
)
2504 int pid
= * (int *) args
;
2506 if (ptid_get_pid (entry
->id
) == pid
)
2513 kill_inferior_callback (struct inferior_list_entry
*entry
)
2515 struct process_info
*process
= (struct process_info
*) entry
;
2516 int pid
= ptid_get_pid (process
->head
.id
);
2518 kill_inferior (pid
);
2519 discard_queued_stop_replies (pid
);
2522 /* Callback for for_each_inferior to detach or kill the inferior,
2523 depending on whether we attached to it or not.
2524 We inform the user whether we're detaching or killing the process
2525 as this is only called when gdbserver is about to exit. */
2528 detach_or_kill_inferior_callback (struct inferior_list_entry
*entry
)
2530 struct process_info
*process
= (struct process_info
*) entry
;
2531 int pid
= ptid_get_pid (process
->head
.id
);
2533 if (process
->attached
)
2534 detach_inferior (pid
);
2536 kill_inferior (pid
);
2538 discard_queued_stop_replies (pid
);
2541 /* for_each_inferior callback for detach_or_kill_for_exit to print
2542 the pids of started inferiors. */
2545 print_started_pid (struct inferior_list_entry
*entry
)
2547 struct process_info
*process
= (struct process_info
*) entry
;
2549 if (! process
->attached
)
2551 int pid
= ptid_get_pid (process
->head
.id
);
2552 fprintf (stderr
, " %d", pid
);
2556 /* for_each_inferior callback for detach_or_kill_for_exit to print
2557 the pids of attached inferiors. */
2560 print_attached_pid (struct inferior_list_entry
*entry
)
2562 struct process_info
*process
= (struct process_info
*) entry
;
2564 if (process
->attached
)
2566 int pid
= ptid_get_pid (process
->head
.id
);
2567 fprintf (stderr
, " %d", pid
);
2571 /* Call this when exiting gdbserver with possible inferiors that need
2572 to be killed or detached from. */
2575 detach_or_kill_for_exit (void)
2577 /* First print a list of the inferiors we will be killing/detaching.
2578 This is to assist the user, for example, in case the inferior unexpectedly
2579 dies after we exit: did we screw up or did the inferior exit on its own?
2580 Having this info will save some head-scratching. */
2582 if (have_started_inferiors_p ())
2584 fprintf (stderr
, "Killing process(es):");
2585 for_each_inferior (&all_processes
, print_started_pid
);
2586 fprintf (stderr
, "\n");
2588 if (have_attached_inferiors_p ())
2590 fprintf (stderr
, "Detaching process(es):");
2591 for_each_inferior (&all_processes
, print_attached_pid
);
2592 fprintf (stderr
, "\n");
2595 /* Now we can kill or detach the inferiors. */
2597 for_each_inferior (&all_processes
, detach_or_kill_inferior_callback
);
2601 main (int argc
, char *argv
[])
2605 char *arg_end
, *port
;
2606 char **next_arg
= &argv
[1];
2611 while (*next_arg
!= NULL
&& **next_arg
== '-')
2613 if (strcmp (*next_arg
, "--version") == 0)
2615 gdbserver_version ();
2618 else if (strcmp (*next_arg
, "--help") == 0)
2620 gdbserver_usage (stdout
);
2623 else if (strcmp (*next_arg
, "--attach") == 0)
2625 else if (strcmp (*next_arg
, "--multi") == 0)
2627 else if (strcmp (*next_arg
, "--wrapper") == 0)
2631 wrapper_argv
= next_arg
;
2632 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
2635 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
2637 gdbserver_usage (stderr
);
2641 /* Consume the "--". */
2644 else if (strcmp (*next_arg
, "--debug") == 0)
2646 else if (strcmp (*next_arg
, "--remote-debug") == 0)
2648 else if (strcmp (*next_arg
, "--disable-packet") == 0)
2650 gdbserver_show_disableable (stdout
);
2653 else if (strncmp (*next_arg
,
2654 "--disable-packet=",
2655 sizeof ("--disable-packet=") - 1) == 0)
2657 char *packets
, *tok
;
2659 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
2660 for (tok
= strtok (packets
, ",");
2662 tok
= strtok (NULL
, ","))
2664 if (strcmp ("vCont", tok
) == 0)
2665 disable_packet_vCont
= 1;
2666 else if (strcmp ("Tthread", tok
) == 0)
2667 disable_packet_Tthread
= 1;
2668 else if (strcmp ("qC", tok
) == 0)
2669 disable_packet_qC
= 1;
2670 else if (strcmp ("qfThreadInfo", tok
) == 0)
2671 disable_packet_qfThreadInfo
= 1;
2672 else if (strcmp ("threads", tok
) == 0)
2674 disable_packet_vCont
= 1;
2675 disable_packet_Tthread
= 1;
2676 disable_packet_qC
= 1;
2677 disable_packet_qfThreadInfo
= 1;
2681 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
2683 gdbserver_show_disableable (stderr
);
2688 else if (strcmp (*next_arg
, "-") == 0)
2690 /* "-" specifies a stdio connection and is a form of port
2692 *next_arg
= STDIO_CONNECTION_NAME
;
2695 else if (strcmp (*next_arg
, "--disable-randomization") == 0)
2696 disable_randomization
= 1;
2697 else if (strcmp (*next_arg
, "--no-disable-randomization") == 0)
2698 disable_randomization
= 0;
2699 else if (strcmp (*next_arg
, "--once") == 0)
2703 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
2711 if (setjmp (toplevel
))
2713 fprintf (stderr
, "Exiting\n");
2719 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
2721 gdbserver_usage (stderr
);
2725 /* We need to know whether the remote connection is stdio before
2726 starting the inferior. Inferiors created in this scenario have
2727 stdin,stdout redirected. So do this here before we call
2729 remote_prepare (port
);
2734 /* --attach used to come after PORT, so allow it there for
2736 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
2743 && (*next_arg
== NULL
2744 || (*next_arg
)[0] == '\0'
2745 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
2747 || next_arg
[1] != NULL
))
2752 gdbserver_usage (stderr
);
2756 initialize_async_io ();
2758 if (target_supports_tracepoints ())
2759 initialize_tracepoint ();
2761 own_buf
= xmalloc (PBUFSIZ
+ 1);
2762 mem_buf
= xmalloc (PBUFSIZ
);
2764 if (pid
== 0 && *next_arg
!= NULL
)
2768 n
= argc
- (next_arg
- argv
);
2769 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
2770 for (i
= 0; i
< n
; i
++)
2771 program_argv
[i
] = xstrdup (next_arg
[i
]);
2772 program_argv
[i
] = NULL
;
2774 /* Wait till we are at first instruction in program. */
2775 start_inferior (program_argv
);
2777 /* We are now (hopefully) stopped at the first instruction of
2778 the target process. This assumes that the target process was
2779 successfully created. */
2783 if (attach_inferior (pid
) == -1)
2784 error ("Attaching not supported on this target");
2786 /* Otherwise succeeded. */
2790 last_status
.kind
= TARGET_WAITKIND_EXITED
;
2791 last_status
.value
.integer
= 0;
2792 last_ptid
= minus_one_ptid
;
2795 /* Don't report shared library events on the initial connection,
2796 even if some libraries are preloaded. Avoids the "stopped by
2797 shared library event" notice on gdb side. */
2800 if (setjmp (toplevel
))
2802 /* If something fails and longjmps while detaching or killing
2803 inferiors, we'd end up here again, stuck in an infinite loop
2804 trap. Be sure that if that happens, we exit immediately
2806 if (setjmp (toplevel
) == 0)
2807 detach_or_kill_for_exit ();
2809 fprintf (stderr
, "Detach or kill failed. Exiting\n");
2813 if (last_status
.kind
== TARGET_WAITKIND_EXITED
2814 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
2819 if (!was_running
&& !multi_mode
)
2821 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
2829 /* Be sure we're out of tfind mode. */
2830 current_traceframe
= -1;
2834 if (setjmp (toplevel
) != 0)
2836 /* An error occurred. */
2837 if (response_needed
)
2839 write_enn (own_buf
);
2844 /* Wait for events. This will return when all event sources are
2845 removed from the event loop. */
2846 start_event_loop ();
2848 /* If an exit was requested (using the "monitor exit" command),
2849 terminate now. The only other way to get here is for
2850 getpkt to fail; close the connection and reopen it at the
2853 if (exit_requested
|| run_once
)
2855 /* If something fails and longjmps while detaching or
2856 killing inferiors, we'd end up here again, stuck in an
2857 infinite loop trap. Be sure that if that happens, we
2858 exit immediately instead. */
2859 if (setjmp (toplevel
) == 0)
2861 detach_or_kill_for_exit ();
2866 fprintf (stderr
, "Detach or kill failed. Exiting\n");
2872 "Remote side has terminated connection. "
2873 "GDBserver will reopen the connection.\n");
2877 if (disconnected_tracing
)
2879 /* Try to enable non-stop/async mode, so we we can both
2880 wait for an async socket accept, and handle async
2881 target events simultaneously. There's also no point
2882 either in having the target always stop all threads,
2883 when we're going to pass signals down without
2887 if (start_non_stop (1))
2890 /* Detaching implicitly resumes all threads; simply
2891 disconnecting does not. */
2897 "Disconnected tracing disabled; stopping trace run.\n");
2904 /* Process options coming from Z packets for *point at address
2905 POINT_ADDR. PACKET is the packet buffer. *PACKET is updated
2906 to point to the first char after the last processed option. */
2909 process_point_options (CORE_ADDR point_addr
, char **packet
)
2911 char *dataptr
= *packet
;
2913 /* Check if data has the correct format. */
2914 if (*dataptr
!= ';')
2924 /* Conditional expression. */
2926 fprintf (stderr
, "Found breakpoint condition.\n");
2927 add_breakpoint_condition (point_addr
, &dataptr
);
2930 /* Unrecognized token, just skip it. */
2931 fprintf (stderr
, "Unknown token %c, ignoring.\n",
2935 /* Skip tokens until we find one that we recognize. */
2936 while (*dataptr
&& *dataptr
!= 'X' && *dataptr
!= ';')
2942 /* Event loop callback that handles a serial event. The first byte in
2943 the serial buffer gets us here. We expect characters to arrive at
2944 a brisk pace, so we read the rest of the packet with a blocking
2948 process_serial_event (void)
2959 int new_packet_len
= -1;
2961 /* Used to decide when gdbserver should exit in
2962 multi-mode/remote. */
2963 static int have_ran
= 0;
2966 have_ran
= target_running ();
2968 disable_async_io ();
2970 response_needed
= 0;
2971 packet_len
= getpkt (own_buf
);
2972 if (packet_len
<= 0)
2975 /* Force an event loop break. */
2978 response_needed
= 1;
2985 handle_query (own_buf
, packet_len
, &new_packet_len
);
2988 handle_general_set (own_buf
);
2991 require_running (own_buf
);
2996 pid
= strtol (&own_buf
[i
], NULL
, 16);
3000 ptid_get_pid (((struct inferior_list_entry
*) current_inferior
)->id
);
3002 if (tracing
&& disconnected_tracing
)
3004 struct thread_resume resume_info
;
3005 struct process_info
*process
= find_process_pid (pid
);
3007 if (process
== NULL
)
3009 write_enn (own_buf
);
3014 "Disconnected tracing in effect, "
3015 "leaving gdbserver attached to the process\n");
3017 /* Make sure we're in non-stop/async mode, so we we can both
3018 wait for an async socket accept, and handle async target
3019 events simultaneously. There's also no point either in
3020 having the target stop all threads, when we're going to
3021 pass signals down without informing GDB. */
3025 fprintf (stderr
, "Forcing non-stop mode\n");
3031 process
->gdb_detached
= 1;
3033 /* Detaching implicitly resumes all threads. */
3034 resume_info
.thread
= minus_one_ptid
;
3035 resume_info
.kind
= resume_continue
;
3036 resume_info
.sig
= 0;
3037 (*the_target
->resume
) (&resume_info
, 1);
3040 break; /* from switch/case */
3043 fprintf (stderr
, "Detaching from process %d\n", pid
);
3045 if (detach_inferior (pid
) != 0)
3046 write_enn (own_buf
);
3049 discard_queued_stop_replies (pid
);
3052 if (extended_protocol
)
3054 /* Treat this like a normal program exit. */
3055 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3056 last_status
.value
.integer
= 0;
3057 last_ptid
= pid_to_ptid (pid
);
3059 current_inferior
= NULL
;
3066 /* If we are attached, then we can exit. Otherwise, we
3067 need to hang around doing nothing, until the child is
3069 join_inferior (pid
);
3075 extended_protocol
= 1;
3079 handle_status (own_buf
);
3082 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
3084 ptid_t gdb_id
, thread_id
;
3087 require_running (own_buf
);
3089 gdb_id
= read_ptid (&own_buf
[2], NULL
);
3091 pid
= ptid_get_pid (gdb_id
);
3093 if (ptid_equal (gdb_id
, null_ptid
)
3094 || ptid_equal (gdb_id
, minus_one_ptid
))
3095 thread_id
= null_ptid
;
3097 && ptid_equal (pid_to_ptid (pid
),
3100 struct thread_info
*thread
=
3101 (struct thread_info
*) find_inferior (&all_threads
,
3106 write_enn (own_buf
);
3110 thread_id
= ((struct inferior_list_entry
*)thread
)->id
;
3114 thread_id
= gdb_id_to_thread_id (gdb_id
);
3115 if (ptid_equal (thread_id
, null_ptid
))
3117 write_enn (own_buf
);
3122 if (own_buf
[1] == 'g')
3124 if (ptid_equal (thread_id
, null_ptid
))
3126 /* GDB is telling us to choose any thread. Check if
3127 the currently selected thread is still valid. If
3128 it is not, select the first available. */
3129 struct thread_info
*thread
=
3130 (struct thread_info
*) find_inferior_id (&all_threads
,
3133 thread_id
= all_threads
.head
->id
;
3136 general_thread
= thread_id
;
3137 set_desired_inferior (1);
3139 else if (own_buf
[1] == 'c')
3140 cont_thread
= thread_id
;
3146 /* Silently ignore it so that gdb can extend the protocol
3147 without compatibility headaches. */
3152 require_running (own_buf
);
3153 if (current_traceframe
>= 0)
3155 struct regcache
*regcache
= new_register_cache ();
3157 if (fetch_traceframe_registers (current_traceframe
,
3159 registers_to_string (regcache
, own_buf
);
3161 write_enn (own_buf
);
3162 free_register_cache (regcache
);
3166 struct regcache
*regcache
;
3168 set_desired_inferior (1);
3169 regcache
= get_thread_regcache (current_inferior
, 1);
3170 registers_to_string (regcache
, own_buf
);
3174 require_running (own_buf
);
3175 if (current_traceframe
>= 0)
3176 write_enn (own_buf
);
3179 struct regcache
*regcache
;
3181 set_desired_inferior (1);
3182 regcache
= get_thread_regcache (current_inferior
, 1);
3183 registers_from_string (regcache
, &own_buf
[1]);
3188 require_running (own_buf
);
3189 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
3190 res
= gdb_read_memory (mem_addr
, mem_buf
, len
);
3192 write_enn (own_buf
);
3194 convert_int_to_ascii (mem_buf
, own_buf
, res
);
3197 require_running (own_buf
);
3198 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, &mem_buf
);
3199 if (gdb_write_memory (mem_addr
, mem_buf
, len
) == 0)
3202 write_enn (own_buf
);
3205 require_running (own_buf
);
3206 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
3207 &mem_addr
, &len
, &mem_buf
) < 0
3208 || gdb_write_memory (mem_addr
, mem_buf
, len
) != 0)
3209 write_enn (own_buf
);
3214 require_running (own_buf
);
3215 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3216 if (target_signal_to_host_p (sig
))
3217 signal
= target_signal_to_host (sig
);
3220 myresume (own_buf
, 0, signal
);
3223 require_running (own_buf
);
3224 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
3225 if (target_signal_to_host_p (sig
))
3226 signal
= target_signal_to_host (sig
);
3229 myresume (own_buf
, 1, signal
);
3232 require_running (own_buf
);
3234 myresume (own_buf
, 0, signal
);
3237 require_running (own_buf
);
3239 myresume (own_buf
, 1, signal
);
3241 case 'Z': /* insert_ ... */
3243 case 'z': /* remove_ ... */
3247 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
3248 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
3249 char type
= own_buf
[1];
3251 const int insert
= ch
== 'Z';
3253 /* Default to unrecognized/unsupported. */
3257 case '0': /* software-breakpoint */
3258 case '1': /* hardware-breakpoint */
3259 case '2': /* write watchpoint */
3260 case '3': /* read watchpoint */
3261 case '4': /* access watchpoint */
3262 require_running (own_buf
);
3263 if (insert
&& the_target
->insert_point
!= NULL
)
3265 /* Insert the breakpoint. If it is already inserted, nothing
3267 res
= (*the_target
->insert_point
) (type
, addr
, len
);
3269 /* GDB may have sent us a list of *point parameters to be
3270 evaluated on the target's side. Read such list here. If we
3271 already have a list of parameters, GDB is telling us to drop
3272 that list and use this one instead. */
3273 if (!res
&& (type
== '0' || type
== '1'))
3275 /* Remove previous conditions. */
3276 clear_gdb_breakpoint_conditions (addr
);
3277 process_point_options (addr
, &dataptr
);
3280 else if (!insert
&& the_target
->remove_point
!= NULL
)
3281 res
= (*the_target
->remove_point
) (type
, addr
, len
);
3293 write_enn (own_buf
);
3297 response_needed
= 0;
3298 if (!target_running ())
3299 /* The packet we received doesn't make sense - but we can't
3300 reply to it, either. */
3303 fprintf (stderr
, "Killing all inferiors\n");
3304 for_each_inferior (&all_processes
, kill_inferior_callback
);
3306 /* When using the extended protocol, we wait with no program
3307 running. The traditional protocol will exit instead. */
3308 if (extended_protocol
)
3310 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3311 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3319 ptid_t gdb_id
, thread_id
;
3321 require_running (own_buf
);
3323 gdb_id
= read_ptid (&own_buf
[1], NULL
);
3324 thread_id
= gdb_id_to_thread_id (gdb_id
);
3325 if (ptid_equal (thread_id
, null_ptid
))
3327 write_enn (own_buf
);
3331 if (mythread_alive (thread_id
))
3334 write_enn (own_buf
);
3338 response_needed
= 0;
3340 /* Restarting the inferior is only supported in the extended
3342 if (extended_protocol
)
3344 if (target_running ())
3345 for_each_inferior (&all_processes
,
3346 kill_inferior_callback
);
3347 fprintf (stderr
, "GDBserver restarting\n");
3349 /* Wait till we are at 1st instruction in prog. */
3350 if (program_argv
!= NULL
)
3351 start_inferior (program_argv
);
3354 last_status
.kind
= TARGET_WAITKIND_EXITED
;
3355 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
3361 /* It is a request we don't understand. Respond with an
3362 empty packet so that gdb knows that we don't support this
3368 /* Extended (long) request. */
3369 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
3373 /* It is a request we don't understand. Respond with an empty
3374 packet so that gdb knows that we don't support this
3380 if (new_packet_len
!= -1)
3381 putpkt_binary (own_buf
, new_packet_len
);
3385 response_needed
= 0;
3387 if (!extended_protocol
&& have_ran
&& !target_running ())
3389 /* In non-stop, defer exiting until GDB had a chance to query
3390 the whole vStopped list (until it gets an OK). */
3393 fprintf (stderr
, "GDBserver exiting\n");
3405 /* Event-loop callback for serial events. */
3408 handle_serial_event (int err
, gdb_client_data client_data
)
3411 fprintf (stderr
, "handling possible serial event\n");
3413 /* Really handle it. */
3414 if (process_serial_event () < 0)
3417 /* Be sure to not change the selected inferior behind GDB's back.
3418 Important in the non-stop mode asynchronous protocol. */
3419 set_desired_inferior (1);
3424 /* Event-loop callback for target events. */
3427 handle_target_event (int err
, gdb_client_data client_data
)
3430 fprintf (stderr
, "handling possible target event\n");
3432 last_ptid
= mywait (minus_one_ptid
, &last_status
,
3435 if (last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3437 int pid
= ptid_get_pid (last_ptid
);
3438 struct process_info
*process
= find_process_pid (pid
);
3439 int forward_event
= !gdb_connected () || process
->gdb_detached
;
3441 if (last_status
.kind
== TARGET_WAITKIND_EXITED
3442 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
3444 mark_breakpoints_out (process
);
3445 mourn_inferior (process
);
3449 /* We're reporting this thread as stopped. Update its
3450 "want-stopped" state to what the client wants, until it
3451 gets a new resume action. */
3452 current_inferior
->last_resume_kind
= resume_stop
;
3453 current_inferior
->last_status
= last_status
;
3458 if (!target_running ())
3460 /* The last process exited. We're done. */
3464 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
3466 /* A thread stopped with a signal, but gdb isn't
3467 connected to handle it. Pass it down to the
3468 inferior, as if it wasn't being traced. */
3469 struct thread_resume resume_info
;
3473 "GDB not connected; forwarding event %d for [%s]\n",
3474 (int) last_status
.kind
,
3475 target_pid_to_str (last_ptid
));
3477 resume_info
.thread
= last_ptid
;
3478 resume_info
.kind
= resume_continue
;
3479 resume_info
.sig
= target_signal_to_host (last_status
.value
.sig
);
3480 (*the_target
->resume
) (&resume_info
, 1);
3482 else if (debug_threads
)
3483 fprintf (stderr
, "GDB not connected; ignoring event %d for [%s]\n",
3484 (int) last_status
.kind
,
3485 target_pid_to_str (last_ptid
));
3489 /* Something interesting. Tell GDB about it. */
3490 push_event (last_ptid
, &last_status
);
3494 /* Be sure to not change the selected inferior behind GDB's back.
3495 Important in the non-stop mode asynchronous protocol. */
3496 set_desired_inferior (1);