1 /* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
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/>. */
35 unsigned long cont_thread
;
36 unsigned long general_thread
;
37 unsigned long step_thread
;
38 unsigned long thread_from_wait
;
39 unsigned long old_thread_from_wait
;
42 static int extended_protocol
;
44 static int response_needed
;
45 static int exit_requested
;
47 static char **program_argv
, **wrapper_argv
;
49 /* Enable miscellaneous debugging output. The name is historical - it
50 was originally used to debug LinuxThreads support. */
53 int pass_signals
[TARGET_SIGNAL_LAST
];
57 const char *gdbserver_xmltarget
;
59 /* The PID of the originally created or attached inferior. Used to
60 send signals to the process when GDB sends us an asynchronous interrupt
61 (user hitting Control-C in the client), and to wait for the child to exit
62 when no longer debugging it. */
64 unsigned long signal_pid
;
67 /* A file descriptor for the controlling terminal. */
70 /* TERMINAL_FD's original foreground group. */
71 pid_t old_foreground_pgrp
;
73 /* Hand back terminal ownership to the original foreground group. */
76 restore_old_foreground_pgrp (void)
78 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
82 /* Set if you want to disable optional thread related packets support
83 in gdbserver, for the sake of testing GDB against stubs that don't
85 int disable_packet_vCont
;
86 int disable_packet_Tthread
;
87 int disable_packet_qC
;
88 int disable_packet_qfThreadInfo
;
93 return all_threads
.head
!= NULL
;
97 start_inferior (char **argv
, char *statusptr
)
99 char **new_argv
= argv
;
102 if (wrapper_argv
!= NULL
)
106 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
108 for (i
= 0; argv
[i
] != NULL
; i
++)
110 new_argv
= alloca (sizeof (char *) * count
);
112 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
113 new_argv
[count
++] = wrapper_argv
[i
];
114 for (i
= 0; argv
[i
] != NULL
; i
++)
115 new_argv
[count
++] = argv
[i
];
116 new_argv
[count
] = NULL
;
120 signal (SIGTTOU
, SIG_DFL
);
121 signal (SIGTTIN
, SIG_DFL
);
124 signal_pid
= create_inferior (new_argv
[0], new_argv
);
126 /* FIXME: we don't actually know at this point that the create
127 actually succeeded. We won't know that until we wait. */
128 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
133 signal (SIGTTOU
, SIG_IGN
);
134 signal (SIGTTIN
, SIG_IGN
);
135 terminal_fd
= fileno (stderr
);
136 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
137 tcsetpgrp (terminal_fd
, signal_pid
);
138 atexit (restore_old_foreground_pgrp
);
141 if (wrapper_argv
!= NULL
)
143 struct thread_resume resume_info
;
146 resume_info
.thread
= -1;
147 resume_info
.step
= 0;
149 resume_info
.leave_stopped
= 0;
151 sig
= mywait (statusptr
, 0);
152 if (*statusptr
!= 'T')
157 (*the_target
->resume
) (&resume_info
);
159 sig
= mywait (statusptr
, 0);
160 if (*statusptr
!= 'T')
163 while (sig
!= TARGET_SIGNAL_TRAP
);
168 /* Wait till we are at 1st instruction in program, return signal
169 number (assuming success). */
170 return mywait (statusptr
, 0);
174 attach_inferior (int pid
, char *statusptr
, int *sigptr
)
176 /* myattach should return -1 if attaching is unsupported,
177 0 if it succeeded, and call error() otherwise. */
179 if (myattach (pid
) != 0)
184 fprintf (stderr
, "Attached; pid = %d\n", pid
);
187 /* FIXME - It may be that we should get the SIGNAL_PID from the
188 attach function, so that it can be the main thread instead of
189 whichever we were told to attach to. */
192 *sigptr
= mywait (statusptr
, 0);
194 /* GDB knows to ignore the first SIGSTOP after attaching to a running
195 process using the "attach" command, but this is different; it's
196 just using "target remote". Pretend it's just starting up. */
197 if (*statusptr
== 'T' && *sigptr
== TARGET_SIGNAL_STOP
)
198 *sigptr
= TARGET_SIGNAL_TRAP
;
203 extern int remote_debug
;
205 /* Decode a qXfer read request. Return 0 if everything looks OK,
209 decode_xfer_read (char *buf
, char **annex
, CORE_ADDR
*ofs
, unsigned int *len
)
211 /* Extract and NUL-terminate the annex. */
213 while (*buf
&& *buf
!= ':')
219 /* After the read marker and annex, qXfer looks like a
220 traditional 'm' packet. */
221 decode_m_packet (buf
, ofs
, len
);
226 /* Write the response to a successful qXfer read. Returns the
227 length of the (binary) data stored in BUF, corresponding
228 to as much of DATA/LEN as we could fit. IS_MORE controls
229 the first character of the response. */
231 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
240 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
244 /* Handle all of the extended 'Q' packets. */
246 handle_general_set (char *own_buf
)
248 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
250 int numsigs
= (int) TARGET_SIGNAL_LAST
, i
;
251 const char *p
= own_buf
+ strlen ("QPassSignals:");
254 p
= decode_address_to_semicolon (&cursig
, p
);
255 for (i
= 0; i
< numsigs
; i
++)
261 /* Keep looping, to clear the remaining signals. */
264 p
= decode_address_to_semicolon (&cursig
, p
);
269 strcpy (own_buf
, "OK");
273 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
277 fprintf (stderr
, "[noack mode enabled]\n");
286 /* Otherwise we didn't know what packet it was. Say we didn't
292 get_features_xml (const char *annex
)
294 /* gdbserver_xmltarget defines what to return when looking
295 for the "target.xml" file. Its contents can either be
296 verbatim XML code (prefixed with a '@') or else the name
297 of the actual XML file to be used in place of "target.xml".
299 This variable is set up from the auto-generated
300 init_registers_... routine for the current target. */
302 if (gdbserver_xmltarget
303 && strcmp (annex
, "target.xml") == 0)
305 if (*gdbserver_xmltarget
== '@')
306 return gdbserver_xmltarget
+ 1;
308 annex
= gdbserver_xmltarget
;
313 extern const char *const xml_builtin
[][2];
316 /* Look for the annex. */
317 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
318 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
321 if (xml_builtin
[i
][0] != NULL
)
322 return xml_builtin
[i
][1];
330 monitor_show_help (void)
332 monitor_output ("The following monitor commands are supported:\n");
333 monitor_output (" set debug <0|1>\n");
334 monitor_output (" Enable general debugging messages\n");
335 monitor_output (" set remote-debug <0|1>\n");
336 monitor_output (" Enable remote protocol debugging messages\n");
337 monitor_output (" exit\n");
338 monitor_output (" Quit GDBserver\n");
341 /* Subroutine of handle_search_memory to simplify it. */
344 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
345 gdb_byte
*pattern
, unsigned pattern_len
,
346 gdb_byte
*search_buf
,
347 unsigned chunk_size
, unsigned search_buf_size
,
348 CORE_ADDR
*found_addrp
)
350 /* Prime the search buffer. */
352 if (read_inferior_memory (start_addr
, search_buf
, search_buf_size
) != 0)
354 warning ("Unable to access target memory at 0x%lx, halting search.",
359 /* Perform the search.
361 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
362 When we've scanned N bytes we copy the trailing bytes to the start and
363 read in another N bytes. */
365 while (search_space_len
>= pattern_len
)
368 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
372 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
374 if (found_ptr
!= NULL
)
376 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
377 *found_addrp
= found_addr
;
381 /* Not found in this chunk, skip to next chunk. */
383 /* Don't let search_space_len wrap here, it's unsigned. */
384 if (search_space_len
>= chunk_size
)
385 search_space_len
-= chunk_size
;
387 search_space_len
= 0;
389 if (search_space_len
>= pattern_len
)
391 unsigned keep_len
= search_buf_size
- chunk_size
;
392 CORE_ADDR read_addr
= start_addr
+ keep_len
;
395 /* Copy the trailing part of the previous iteration to the front
396 of the buffer for the next iteration. */
397 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
399 nr_to_read
= (search_space_len
- keep_len
< chunk_size
400 ? search_space_len
- keep_len
403 if (read_inferior_memory (read_addr
, search_buf
+ keep_len
,
406 warning ("Unable to access target memory at 0x%lx, halting search.",
411 start_addr
+= chunk_size
;
420 /* Handle qSearch:memory packets. */
423 handle_search_memory (char *own_buf
, int packet_len
)
425 CORE_ADDR start_addr
;
426 CORE_ADDR search_space_len
;
428 unsigned int pattern_len
;
429 /* NOTE: also defined in find.c testcase. */
430 #define SEARCH_CHUNK_SIZE 16000
431 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
432 /* Buffer to hold memory contents for searching. */
433 gdb_byte
*search_buf
;
434 unsigned search_buf_size
;
436 CORE_ADDR found_addr
;
437 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
439 pattern
= malloc (packet_len
);
442 error ("Unable to allocate memory to perform the search");
443 strcpy (own_buf
, "E00");
446 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
447 packet_len
- cmd_name_len
,
448 &start_addr
, &search_space_len
,
449 pattern
, &pattern_len
) < 0)
452 error ("Error in parsing qSearch:memory packet");
453 strcpy (own_buf
, "E00");
457 search_buf_size
= chunk_size
+ pattern_len
- 1;
459 /* No point in trying to allocate a buffer larger than the search space. */
460 if (search_space_len
< search_buf_size
)
461 search_buf_size
= search_space_len
;
463 search_buf
= malloc (search_buf_size
);
464 if (search_buf
== NULL
)
467 error ("Unable to allocate memory to perform the search");
468 strcpy (own_buf
, "E00");
472 found
= handle_search_memory_1 (start_addr
, search_space_len
,
473 pattern
, pattern_len
,
474 search_buf
, chunk_size
, search_buf_size
,
478 sprintf (own_buf
, "1,%lx", (long) found_addr
);
480 strcpy (own_buf
, "0");
482 strcpy (own_buf
, "E00");
488 #define require_running(BUF) \
489 if (!target_running ()) \
495 /* Handle all of the extended 'q' packets. */
497 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
499 static struct inferior_list_entry
*thread_ptr
;
501 /* Reply the current thread id. */
502 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
504 require_running (own_buf
);
505 thread_ptr
= all_threads
.head
;
506 sprintf (own_buf
, "QC%x",
507 thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
511 if (strcmp ("qSymbol::", own_buf
) == 0)
513 if (target_running () && the_target
->look_up_symbols
!= NULL
)
514 (*the_target
->look_up_symbols
) ();
516 strcpy (own_buf
, "OK");
520 if (!disable_packet_qfThreadInfo
)
522 if (strcmp ("qfThreadInfo", own_buf
) == 0)
524 require_running (own_buf
);
525 thread_ptr
= all_threads
.head
;
526 sprintf (own_buf
, "m%x", thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
527 thread_ptr
= thread_ptr
->next
;
531 if (strcmp ("qsThreadInfo", own_buf
) == 0)
533 require_running (own_buf
);
534 if (thread_ptr
!= NULL
)
536 sprintf (own_buf
, "m%x", thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
537 thread_ptr
= thread_ptr
->next
;
542 sprintf (own_buf
, "l");
548 if (the_target
->read_offsets
!= NULL
549 && strcmp ("qOffsets", own_buf
) == 0)
551 CORE_ADDR text
, data
;
553 require_running (own_buf
);
554 if (the_target
->read_offsets (&text
, &data
))
555 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
556 (long)text
, (long)data
, (long)data
);
563 if (the_target
->qxfer_spu
!= NULL
564 && strncmp ("qXfer:spu:read:", own_buf
, 15) == 0)
570 unsigned char *spu_buf
;
572 require_running (own_buf
);
573 strcpy (own_buf
, "E00");
574 if (decode_xfer_read (own_buf
+ 15, &annex
, &ofs
, &len
) < 0)
576 if (len
> PBUFSIZ
- 2)
578 spu_buf
= malloc (len
+ 1);
582 n
= (*the_target
->qxfer_spu
) (annex
, spu_buf
, NULL
, ofs
, len
+ 1);
586 *new_packet_len_p
= write_qxfer_response
587 (own_buf
, spu_buf
, len
, 1);
589 *new_packet_len_p
= write_qxfer_response
590 (own_buf
, spu_buf
, n
, 0);
596 if (the_target
->qxfer_spu
!= NULL
597 && strncmp ("qXfer:spu:write:", own_buf
, 16) == 0)
603 unsigned char *spu_buf
;
605 require_running (own_buf
);
606 strcpy (own_buf
, "E00");
607 spu_buf
= malloc (packet_len
- 15);
610 if (decode_xfer_write (own_buf
+ 16, packet_len
- 16, &annex
,
611 &ofs
, &len
, spu_buf
) < 0)
617 n
= (*the_target
->qxfer_spu
)
618 (annex
, NULL
, (unsigned const char *)spu_buf
, ofs
, len
);
622 sprintf (own_buf
, "%x", n
);
628 if (the_target
->read_auxv
!= NULL
629 && strncmp ("qXfer:auxv:read:", own_buf
, 16) == 0)
637 require_running (own_buf
);
639 /* Reject any annex; grab the offset and length. */
640 if (decode_xfer_read (own_buf
+ 16, &annex
, &ofs
, &len
) < 0
643 strcpy (own_buf
, "E00");
647 /* Read one extra byte, as an indicator of whether there is
649 if (len
> PBUFSIZ
- 2)
651 data
= malloc (len
+ 1);
657 n
= (*the_target
->read_auxv
) (ofs
, data
, len
+ 1);
661 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
663 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
670 if (strncmp ("qXfer:features:read:", own_buf
, 20) == 0)
673 unsigned int len
, total_len
;
674 const char *document
;
677 require_running (own_buf
);
679 /* Grab the annex, offset, and length. */
680 if (decode_xfer_read (own_buf
+ 20, &annex
, &ofs
, &len
) < 0)
682 strcpy (own_buf
, "E00");
686 /* Now grab the correct annex. */
687 document
= get_features_xml (annex
);
688 if (document
== NULL
)
690 strcpy (own_buf
, "E00");
694 total_len
= strlen (document
);
695 if (len
> PBUFSIZ
- 2)
700 else if (len
< total_len
- ofs
)
701 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
704 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
710 if (strncmp ("qXfer:libraries:read:", own_buf
, 21) == 0)
713 unsigned int len
, total_len
;
715 struct inferior_list_entry
*dll_ptr
;
718 require_running (own_buf
);
720 /* Reject any annex; grab the offset and length. */
721 if (decode_xfer_read (own_buf
+ 21, &annex
, &ofs
, &len
) < 0
724 strcpy (own_buf
, "E00");
728 /* Over-estimate the necessary memory. Assume that every character
729 in the library name must be escaped. */
731 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
732 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
734 document
= malloc (total_len
);
735 if (document
== NULL
)
740 strcpy (document
, "<library-list>\n");
741 p
= document
+ strlen (document
);
743 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
745 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
748 strcpy (p
, " <library name=\"");
750 name
= xml_escape_text (dll
->name
);
754 strcpy (p
, "\"><segment address=\"");
756 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
758 strcpy (p
, "\"/></library>\n");
762 strcpy (p
, "</library-list>\n");
764 total_len
= strlen (document
);
765 if (len
> PBUFSIZ
- 2)
770 else if (len
< total_len
- ofs
)
771 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
774 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
781 if (the_target
->qxfer_osdata
!= NULL
782 && strncmp ("qXfer:osdata:read:", own_buf
, 18) == 0)
788 unsigned char *workbuf
;
790 strcpy (own_buf
, "E00");
791 if (decode_xfer_read (own_buf
+ 18, &annex
, &ofs
, &len
) < 0)
793 if (len
> PBUFSIZ
- 2)
795 workbuf
= malloc (len
+ 1);
799 n
= (*the_target
->qxfer_osdata
) (annex
, workbuf
, NULL
, ofs
, len
+ 1);
803 *new_packet_len_p
= write_qxfer_response
804 (own_buf
, workbuf
, len
, 1);
806 *new_packet_len_p
= write_qxfer_response
807 (own_buf
, workbuf
, n
, 0);
813 if (the_target
->qxfer_siginfo
!= NULL
814 && strncmp ("qXfer:siginfo:read:", own_buf
, 19) == 0)
822 require_running (own_buf
);
824 /* Reject any annex; grab the offset and length. */
825 if (decode_xfer_read (own_buf
+ 19, &annex
, &ofs
, &len
) < 0
828 strcpy (own_buf
, "E00");
832 /* Read one extra byte, as an indicator of whether there is
834 if (len
> PBUFSIZ
- 2)
836 data
= malloc (len
+ 1);
839 n
= (*the_target
->qxfer_siginfo
) (annex
, data
, NULL
, ofs
, len
+ 1);
843 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
845 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
851 if (the_target
->qxfer_siginfo
!= NULL
852 && strncmp ("qXfer:siginfo:write:", own_buf
, 20) == 0)
860 require_running (own_buf
);
862 strcpy (own_buf
, "E00");
863 data
= malloc (packet_len
- 19);
866 if (decode_xfer_write (own_buf
+ 20, packet_len
- 20, &annex
,
867 &ofs
, &len
, data
) < 0)
873 n
= (*the_target
->qxfer_siginfo
)
874 (annex
, NULL
, (unsigned const char *)data
, ofs
, len
);
878 sprintf (own_buf
, "%x", n
);
884 /* Protocol features query. */
885 if (strncmp ("qSupported", own_buf
, 10) == 0
886 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
888 sprintf (own_buf
, "PacketSize=%x;QPassSignals+", PBUFSIZ
- 1);
890 /* We do not have any hook to indicate whether the target backend
891 supports qXfer:libraries:read, so always report it. */
892 strcat (own_buf
, ";qXfer:libraries:read+");
894 if (the_target
->read_auxv
!= NULL
)
895 strcat (own_buf
, ";qXfer:auxv:read+");
897 if (the_target
->qxfer_spu
!= NULL
)
898 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
900 if (the_target
->qxfer_siginfo
!= NULL
)
901 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
903 /* We always report qXfer:features:read, as targets may
904 install XML files on a subsequent call to arch_setup.
905 If we reported to GDB on startup that we don't support
906 qXfer:feature:read at all, we will never be re-queried. */
907 strcat (own_buf
, ";qXfer:features:read+");
909 if (transport_is_reliable
)
910 strcat (own_buf
, ";QStartNoAckMode+");
912 if (the_target
->qxfer_osdata
!= NULL
)
913 strcat (own_buf
, ";qXfer:osdata:read+");
918 /* Thread-local storage support. */
919 if (the_target
->get_tls_address
!= NULL
920 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
922 char *p
= own_buf
+ 12;
923 CORE_ADDR parts
[3], address
= 0;
926 require_running (own_buf
);
928 for (i
= 0; i
< 3; i
++)
936 p2
= strchr (p
, ',');
948 decode_address (&parts
[i
], p
, len
);
952 if (p
!= NULL
|| i
< 3)
956 struct thread_info
*thread
= gdb_id_to_thread (parts
[0]);
961 err
= the_target
->get_tls_address (thread
, parts
[1], parts
[2],
967 sprintf (own_buf
, "%llx", address
);
976 /* Otherwise, pretend we do not understand this packet. */
979 /* Handle "monitor" commands. */
980 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
982 char *mon
= malloc (PBUFSIZ
);
983 int len
= strlen (own_buf
+ 6);
991 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
1001 if (strcmp (mon
, "set debug 1") == 0)
1004 monitor_output ("Debug output enabled.\n");
1006 else if (strcmp (mon
, "set debug 0") == 0)
1009 monitor_output ("Debug output disabled.\n");
1011 else if (strcmp (mon
, "set remote-debug 1") == 0)
1014 monitor_output ("Protocol debug output enabled.\n");
1016 else if (strcmp (mon
, "set remote-debug 0") == 0)
1019 monitor_output ("Protocol debug output disabled.\n");
1021 else if (strcmp (mon
, "help") == 0)
1022 monitor_show_help ();
1023 else if (strcmp (mon
, "exit") == 0)
1027 monitor_output ("Unknown monitor command.\n\n");
1028 monitor_show_help ();
1029 write_enn (own_buf
);
1036 if (strncmp ("qSearch:memory:", own_buf
, sizeof ("qSearch:memory:") - 1) == 0)
1038 require_running (own_buf
);
1039 handle_search_memory (own_buf
, packet_len
);
1043 /* Otherwise we didn't know what packet it was. Say we didn't
1048 /* Parse vCont packets. */
1050 handle_v_cont (char *own_buf
, char *status
, int *signal
)
1054 struct thread_resume
*resume_info
, default_action
;
1056 /* Count the number of semicolons in the packet. There should be one
1057 for every action. */
1063 p
= strchr (p
, ';');
1065 /* Allocate room for one extra action, for the default remain-stopped
1066 behavior; if no default action is in the list, we'll need the extra
1068 resume_info
= malloc ((n
+ 1) * sizeof (resume_info
[0]));
1069 if (resume_info
== NULL
)
1072 default_action
.thread
= -1;
1073 default_action
.leave_stopped
= 1;
1074 default_action
.step
= 0;
1075 default_action
.sig
= 0;
1083 resume_info
[i
].leave_stopped
= 0;
1085 if (p
[0] == 's' || p
[0] == 'S')
1086 resume_info
[i
].step
= 1;
1087 else if (p
[0] == 'c' || p
[0] == 'C')
1088 resume_info
[i
].step
= 0;
1092 if (p
[0] == 'S' || p
[0] == 'C')
1095 sig
= strtol (p
+ 1, &q
, 16);
1100 if (!target_signal_to_host_p (sig
))
1102 resume_info
[i
].sig
= target_signal_to_host (sig
);
1106 resume_info
[i
].sig
= 0;
1112 resume_info
[i
].thread
= -1;
1113 default_action
= resume_info
[i
];
1115 /* Note: we don't increment i here, we'll overwrite this entry
1116 the next time through. */
1118 else if (p
[0] == ':')
1120 unsigned int gdb_id
= strtoul (p
+ 1, &q
, 16);
1121 unsigned long thread_id
;
1126 if (p
[0] != ';' && p
[0] != 0)
1129 thread_id
= gdb_id_to_thread_id (gdb_id
);
1131 resume_info
[i
].thread
= thread_id
;
1139 resume_info
[i
] = default_action
;
1141 /* Still used in occasional places in the backend. */
1142 if (n
== 1 && resume_info
[0].thread
!= -1)
1143 cont_thread
= resume_info
[0].thread
;
1146 set_desired_inferior (0);
1149 (*the_target
->resume
) (resume_info
);
1153 *signal
= mywait (status
, 1);
1154 prepare_resume_reply (own_buf
, *status
, *signal
);
1155 disable_async_io ();
1159 write_enn (own_buf
);
1164 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1166 handle_v_attach (char *own_buf
, char *status
, int *signal
)
1170 pid
= strtol (own_buf
+ 8, NULL
, 16);
1171 if (pid
!= 0 && attach_inferior (pid
, status
, signal
) == 0)
1173 /* Don't report shared library events after attaching, even if
1174 some libraries are preloaded. GDB will always poll the
1175 library list. Avoids the "stopped by shared library event"
1176 notice on the GDB side. */
1178 prepare_resume_reply (own_buf
, *status
, *signal
);
1183 write_enn (own_buf
);
1188 /* Run a new program. Return 1 if successful, 0 if failure. */
1190 handle_v_run (char *own_buf
, char *status
, int *signal
)
1192 char *p
, *next_p
, **new_argv
;
1196 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
1202 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
1203 if (new_argv
== NULL
)
1205 write_enn (own_buf
);
1210 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
1212 next_p
= strchr (p
, ';');
1214 next_p
= p
+ strlen (p
);
1216 if (i
== 0 && p
== next_p
)
1220 /* FIXME: Fail request if out of memory instead of dying. */
1221 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
1222 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
1223 new_argv
[i
][(next_p
- p
) / 2] = '\0';
1232 if (new_argv
[0] == NULL
)
1234 /* GDB didn't specify a program to run. Use the program from the
1235 last run with the new argument list. */
1237 if (program_argv
== NULL
)
1239 /* FIXME: new_argv memory leak */
1240 write_enn (own_buf
);
1244 new_argv
[0] = strdup (program_argv
[0]);
1245 if (new_argv
[0] == NULL
)
1247 /* FIXME: new_argv memory leak */
1248 write_enn (own_buf
);
1253 /* Free the old argv and install the new one. */
1254 freeargv (program_argv
);
1255 program_argv
= new_argv
;
1257 *signal
= start_inferior (program_argv
, status
);
1260 prepare_resume_reply (own_buf
, *status
, *signal
);
1265 write_enn (own_buf
);
1270 /* Handle all of the extended 'v' packets. */
1272 handle_v_requests (char *own_buf
, char *status
, int *signal
,
1273 int packet_len
, int *new_packet_len
)
1275 if (!disable_packet_vCont
)
1277 if (strncmp (own_buf
, "vCont;", 6) == 0)
1279 require_running (own_buf
);
1280 handle_v_cont (own_buf
, status
, signal
);
1284 if (strncmp (own_buf
, "vCont?", 6) == 0)
1286 strcpy (own_buf
, "vCont;c;C;s;S");
1291 if (strncmp (own_buf
, "vFile:", 6) == 0
1292 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
1295 if (strncmp (own_buf
, "vAttach;", 8) == 0)
1297 if (target_running ())
1299 fprintf (stderr
, "Already debugging a process\n");
1300 write_enn (own_buf
);
1303 handle_v_attach (own_buf
, status
, signal
);
1307 if (strncmp (own_buf
, "vRun;", 5) == 0)
1309 if (target_running ())
1311 fprintf (stderr
, "Already debugging a process\n");
1312 write_enn (own_buf
);
1315 handle_v_run (own_buf
, status
, signal
);
1319 /* Otherwise we didn't know what packet it was. Say we didn't
1326 myresume (char *own_buf
, int step
, int *signalp
, char *statusp
)
1328 struct thread_resume resume_info
[2];
1332 set_desired_inferior (0);
1334 if (step
|| sig
|| (cont_thread
!= 0 && cont_thread
!= -1))
1336 resume_info
[0].thread
1337 = ((struct inferior_list_entry
*) current_inferior
)->id
;
1338 resume_info
[0].step
= step
;
1339 resume_info
[0].sig
= sig
;
1340 resume_info
[0].leave_stopped
= 0;
1343 resume_info
[n
].thread
= -1;
1344 resume_info
[n
].step
= 0;
1345 resume_info
[n
].sig
= 0;
1346 resume_info
[n
].leave_stopped
= (cont_thread
!= 0 && cont_thread
!= -1);
1349 (*the_target
->resume
) (resume_info
);
1350 *signalp
= mywait (statusp
, 1);
1351 prepare_resume_reply (own_buf
, *statusp
, *signalp
);
1352 disable_async_io ();
1356 gdbserver_version (void)
1358 printf ("GNU gdbserver %s%s\n"
1359 "Copyright (C) 2009 Free Software Foundation, Inc.\n"
1360 "gdbserver is free software, covered by the GNU General Public License.\n"
1361 "This gdbserver was configured as \"%s\"\n",
1362 PKGVERSION
, version
, host_name
);
1366 gdbserver_usage (FILE *stream
)
1368 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
1369 "\tgdbserver [OPTIONS] --attach COMM PID\n"
1370 "\tgdbserver [OPTIONS] --multi COMM\n"
1372 "COMM may either be a tty device (for serial debugging), or \n"
1373 "HOST:PORT to listen for a TCP connection.\n"
1376 " --debug Enable general debugging output.\n"
1377 " --remote-debug Enable remote protocol debugging output.\n"
1378 " --version Display version information and exit.\n"
1379 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n");
1380 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
1381 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
1385 gdbserver_show_disableable (FILE *stream
)
1387 fprintf (stream
, "Disableable packets:\n"
1388 " vCont \tAll vCont packets\n"
1389 " qC \tQuerying the current thread\n"
1390 " qfThreadInfo\tThread listing\n"
1391 " Tthread \tPassing the thread specifier in the T stop reply packet\n"
1392 " threads \tAll of the above\n");
1396 #undef require_running
1397 #define require_running(BUF) \
1398 if (!target_running ()) \
1405 main (int argc
, char *argv
[])
1407 char ch
, status
, *own_buf
;
1408 unsigned char *mem_buf
;
1415 char *arg_end
, *port
;
1416 char **next_arg
= &argv
[1];
1421 while (*next_arg
!= NULL
&& **next_arg
== '-')
1423 if (strcmp (*next_arg
, "--version") == 0)
1425 gdbserver_version ();
1428 else if (strcmp (*next_arg
, "--help") == 0)
1430 gdbserver_usage (stdout
);
1433 else if (strcmp (*next_arg
, "--attach") == 0)
1435 else if (strcmp (*next_arg
, "--multi") == 0)
1437 else if (strcmp (*next_arg
, "--wrapper") == 0)
1441 wrapper_argv
= next_arg
;
1442 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
1445 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
1447 gdbserver_usage (stderr
);
1451 /* Consume the "--". */
1454 else if (strcmp (*next_arg
, "--debug") == 0)
1456 else if (strcmp (*next_arg
, "--remote-debug") == 0)
1458 else if (strcmp (*next_arg
, "--disable-packet") == 0)
1460 gdbserver_show_disableable (stdout
);
1463 else if (strncmp (*next_arg
,
1464 "--disable-packet=",
1465 sizeof ("--disable-packet=") - 1) == 0)
1467 char *packets
, *tok
;
1469 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
1470 for (tok
= strtok (packets
, ",");
1472 tok
= strtok (NULL
, ","))
1474 if (strcmp ("vCont", tok
) == 0)
1475 disable_packet_vCont
= 1;
1476 else if (strcmp ("Tthread", tok
) == 0)
1477 disable_packet_Tthread
= 1;
1478 else if (strcmp ("qC", tok
) == 0)
1479 disable_packet_qC
= 1;
1480 else if (strcmp ("qfThreadInfo", tok
) == 0)
1481 disable_packet_qfThreadInfo
= 1;
1482 else if (strcmp ("threads", tok
) == 0)
1484 disable_packet_vCont
= 1;
1485 disable_packet_Tthread
= 1;
1486 disable_packet_qC
= 1;
1487 disable_packet_qfThreadInfo
= 1;
1491 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
1493 gdbserver_show_disableable (stderr
);
1500 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
1508 if (setjmp (toplevel
))
1510 fprintf (stderr
, "Exiting\n");
1516 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
1518 gdbserver_usage (stderr
);
1525 /* --attach used to come after PORT, so allow it there for
1527 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
1534 && (*next_arg
== NULL
1535 || (*next_arg
)[0] == '\0'
1536 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
1538 || next_arg
[1] != NULL
))
1543 gdbserver_usage (stderr
);
1547 initialize_async_io ();
1550 own_buf
= xmalloc (PBUFSIZ
+ 1);
1551 mem_buf
= xmalloc (PBUFSIZ
);
1553 if (pid
== 0 && *next_arg
!= NULL
)
1557 n
= argc
- (next_arg
- argv
);
1558 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
1559 for (i
= 0; i
< n
; i
++)
1560 program_argv
[i
] = xstrdup (next_arg
[i
]);
1561 program_argv
[i
] = NULL
;
1563 /* Wait till we are at first instruction in program. */
1564 signal
= start_inferior (program_argv
, &status
);
1566 /* We are now (hopefully) stopped at the first instruction of
1567 the target process. This assumes that the target process was
1568 successfully created. */
1572 if (attach_inferior (pid
, &status
, &signal
) == -1)
1573 error ("Attaching not supported on this target");
1575 /* Otherwise succeeded. */
1583 /* Don't report shared library events on the initial connection,
1584 even if some libraries are preloaded. Avoids the "stopped by
1585 shared library event" notice on gdb side. */
1588 if (setjmp (toplevel
))
1590 fprintf (stderr
, "Killing inferior\n");
1595 if (status
== 'W' || status
== 'X')
1600 if (!was_running
&& !multi_mode
)
1602 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
1612 if (setjmp (toplevel
) != 0)
1614 /* An error occurred. */
1615 if (response_needed
)
1617 write_enn (own_buf
);
1622 disable_async_io ();
1623 while (!exit_requested
)
1627 int new_packet_len
= -1;
1629 response_needed
= 0;
1630 packet_len
= getpkt (own_buf
);
1631 if (packet_len
<= 0)
1633 response_needed
= 1;
1640 handle_query (own_buf
, packet_len
, &new_packet_len
);
1643 handle_general_set (own_buf
);
1646 require_running (own_buf
);
1647 fprintf (stderr
, "Detaching from inferior\n");
1648 if (detach_inferior () != 0)
1649 write_enn (own_buf
);
1654 if (extended_protocol
)
1656 /* Treat this like a normal program exit. */
1665 /* If we are attached, then we can exit. Otherwise, we
1666 need to hang around doing nothing, until the child
1676 extended_protocol
= 1;
1680 prepare_resume_reply (own_buf
, status
, signal
);
1683 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
1685 unsigned long gdb_id
, thread_id
;
1687 require_running (own_buf
);
1688 gdb_id
= strtoul (&own_buf
[2], NULL
, 16);
1689 if (gdb_id
== 0 || gdb_id
== -1)
1693 thread_id
= gdb_id_to_thread_id (gdb_id
);
1696 write_enn (own_buf
);
1701 if (own_buf
[1] == 'g')
1703 general_thread
= thread_id
;
1704 set_desired_inferior (1);
1706 else if (own_buf
[1] == 'c')
1707 cont_thread
= thread_id
;
1708 else if (own_buf
[1] == 's')
1709 step_thread
= thread_id
;
1715 /* Silently ignore it so that gdb can extend the protocol
1716 without compatibility headaches. */
1721 require_running (own_buf
);
1722 set_desired_inferior (1);
1723 registers_to_string (own_buf
);
1726 require_running (own_buf
);
1727 set_desired_inferior (1);
1728 registers_from_string (&own_buf
[1]);
1732 require_running (own_buf
);
1733 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
1734 if (read_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
1735 convert_int_to_ascii (mem_buf
, own_buf
, len
);
1737 write_enn (own_buf
);
1740 require_running (own_buf
);
1741 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
1742 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
1745 write_enn (own_buf
);
1748 require_running (own_buf
);
1749 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
1750 &mem_addr
, &len
, mem_buf
) < 0
1751 || write_inferior_memory (mem_addr
, mem_buf
, len
) != 0)
1752 write_enn (own_buf
);
1757 require_running (own_buf
);
1758 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
1759 if (target_signal_to_host_p (sig
))
1760 signal
= target_signal_to_host (sig
);
1763 myresume (own_buf
, 0, &signal
, &status
);
1766 require_running (own_buf
);
1767 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
1768 if (target_signal_to_host_p (sig
))
1769 signal
= target_signal_to_host (sig
);
1772 myresume (own_buf
, 1, &signal
, &status
);
1775 require_running (own_buf
);
1777 myresume (own_buf
, 0, &signal
, &status
);
1780 require_running (own_buf
);
1782 myresume (own_buf
, 1, &signal
, &status
);
1788 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
1789 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
1790 char type
= own_buf
[1];
1792 if (the_target
->insert_watchpoint
== NULL
1793 || (type
< '2' || type
> '4'))
1795 /* No watchpoint support or not a watchpoint command;
1796 unrecognized either way. */
1803 require_running (own_buf
);
1804 res
= (*the_target
->insert_watchpoint
) (type
, addr
, len
);
1811 write_enn (own_buf
);
1819 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
1820 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
1821 char type
= own_buf
[1];
1823 if (the_target
->remove_watchpoint
== NULL
1824 || (type
< '2' || type
> '4'))
1826 /* No watchpoint support or not a watchpoint command;
1827 unrecognized either way. */
1834 require_running (own_buf
);
1835 res
= (*the_target
->remove_watchpoint
) (type
, addr
, len
);
1842 write_enn (own_buf
);
1847 response_needed
= 0;
1848 if (!target_running ())
1849 /* The packet we received doesn't make sense - but we
1850 can't reply to it, either. */
1853 fprintf (stderr
, "Killing inferior\n");
1856 /* When using the extended protocol, we wait with no
1857 program running. The traditional protocol will exit
1859 if (extended_protocol
)
1862 signal
= TARGET_SIGNAL_KILL
;
1873 unsigned long gdb_id
, thread_id
;
1875 require_running (own_buf
);
1876 gdb_id
= strtoul (&own_buf
[1], NULL
, 16);
1877 thread_id
= gdb_id_to_thread_id (gdb_id
);
1880 write_enn (own_buf
);
1884 if (mythread_alive (thread_id
))
1887 write_enn (own_buf
);
1891 response_needed
= 0;
1893 /* Restarting the inferior is only supported in the
1894 extended protocol. */
1895 if (extended_protocol
)
1897 if (target_running ())
1899 fprintf (stderr
, "GDBserver restarting\n");
1901 /* Wait till we are at 1st instruction in prog. */
1902 if (program_argv
!= NULL
)
1903 signal
= start_inferior (program_argv
, &status
);
1907 signal
= TARGET_SIGNAL_KILL
;
1913 /* It is a request we don't understand. Respond with an
1914 empty packet so that gdb knows that we don't support this
1920 /* Extended (long) request. */
1921 handle_v_requests (own_buf
, &status
, &signal
,
1922 packet_len
, &new_packet_len
);
1926 /* It is a request we don't understand. Respond with an
1927 empty packet so that gdb knows that we don't support this
1933 if (new_packet_len
!= -1)
1934 putpkt_binary (own_buf
, new_packet_len
);
1938 response_needed
= 0;
1940 if (was_running
&& (status
== 'W' || status
== 'X'))
1946 "\nChild exited with status %d\n", signal
);
1948 fprintf (stderr
, "\nChild terminated with signal = 0x%x (%s)\n",
1949 target_signal_to_host (signal
),
1950 target_signal_to_name (signal
));
1952 if (extended_protocol
)
1956 fprintf (stderr
, "GDBserver exiting\n");
1961 if (status
!= 'W' && status
!= 'X')
1965 /* If an exit was requested (using the "monitor exit" command),
1966 terminate now. The only other way to get here is for
1967 getpkt to fail; close the connection and reopen it at the
1973 if (attached
&& target_running ())
1975 else if (target_running ())
1981 fprintf (stderr
, "Remote side has terminated connection. "
1982 "GDBserver will reopen the connection.\n");