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
;
41 static int extended_protocol
;
43 static int response_needed
;
44 static int exit_requested
;
46 static char **program_argv
, **wrapper_argv
;
48 /* Enable miscellaneous debugging output. The name is historical - it
49 was originally used to debug LinuxThreads support. */
52 int pass_signals
[TARGET_SIGNAL_LAST
];
56 const char *gdbserver_xmltarget
;
58 /* The PID of the originally created or attached inferior. Used to
59 send signals to the process when GDB sends us an asynchronous interrupt
60 (user hitting Control-C in the client), and to wait for the child to exit
61 when no longer debugging it. */
63 unsigned long signal_pid
;
66 /* A file descriptor for the controlling terminal. */
69 /* TERMINAL_FD's original foreground group. */
70 pid_t old_foreground_pgrp
;
72 /* Hand back terminal ownership to the original foreground group. */
75 restore_old_foreground_pgrp (void)
77 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
81 /* Set if you want to disable optional thread related packets support
82 in gdbserver, for the sake of testing GDB against stubs that don't
84 int disable_packet_vCont
;
85 int disable_packet_Tthread
;
86 int disable_packet_qC
;
87 int disable_packet_qfThreadInfo
;
89 /* Last status reported to GDB. */
90 static struct target_waitstatus last_status
;
91 static unsigned long last_ptid
;
96 return all_threads
.head
!= NULL
;
100 start_inferior (char **argv
)
102 char **new_argv
= argv
;
105 if (wrapper_argv
!= NULL
)
109 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
111 for (i
= 0; argv
[i
] != NULL
; i
++)
113 new_argv
= alloca (sizeof (char *) * count
);
115 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
116 new_argv
[count
++] = wrapper_argv
[i
];
117 for (i
= 0; argv
[i
] != NULL
; i
++)
118 new_argv
[count
++] = argv
[i
];
119 new_argv
[count
] = NULL
;
123 signal (SIGTTOU
, SIG_DFL
);
124 signal (SIGTTIN
, SIG_DFL
);
127 signal_pid
= create_inferior (new_argv
[0], new_argv
);
129 /* FIXME: we don't actually know at this point that the create
130 actually succeeded. We won't know that until we wait. */
131 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
136 signal (SIGTTOU
, SIG_IGN
);
137 signal (SIGTTIN
, SIG_IGN
);
138 terminal_fd
= fileno (stderr
);
139 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
140 tcsetpgrp (terminal_fd
, signal_pid
);
141 atexit (restore_old_foreground_pgrp
);
144 if (wrapper_argv
!= NULL
)
146 struct thread_resume resume_info
;
149 resume_info
.thread
= -1;
150 resume_info
.step
= 0;
153 ptid
= mywait (&last_status
, 0);
154 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
159 (*the_target
->resume
) (&resume_info
, 1);
161 mywait (&last_status
, 0);
162 if (last_status
.kind
!= TARGET_WAITKIND_STOPPED
)
165 while (last_status
.value
.sig
!= TARGET_SIGNAL_TRAP
);
170 /* Wait till we are at 1st instruction in program, return new pid
171 (assuming success). */
172 last_ptid
= mywait (&last_status
, 0);
178 attach_inferior (int pid
)
180 /* myattach should return -1 if attaching is unsupported,
181 0 if it succeeded, and call error() otherwise. */
183 if (myattach (pid
) != 0)
188 fprintf (stderr
, "Attached; pid = %d\n", pid
);
191 /* FIXME - It may be that we should get the SIGNAL_PID from the
192 attach function, so that it can be the main thread instead of
193 whichever we were told to attach to. */
196 last_ptid
= mywait (&last_status
, 0);
198 /* GDB knows to ignore the first SIGSTOP after attaching to a running
199 process using the "attach" command, but this is different; it's
200 just using "target remote". Pretend it's just starting up. */
201 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
202 && last_status
.value
.sig
== TARGET_SIGNAL_STOP
)
203 last_status
.value
.sig
= TARGET_SIGNAL_TRAP
;
208 extern int remote_debug
;
210 /* Decode a qXfer read request. Return 0 if everything looks OK,
214 decode_xfer_read (char *buf
, char **annex
, CORE_ADDR
*ofs
, unsigned int *len
)
216 /* Extract and NUL-terminate the annex. */
218 while (*buf
&& *buf
!= ':')
224 /* After the read marker and annex, qXfer looks like a
225 traditional 'm' packet. */
226 decode_m_packet (buf
, ofs
, len
);
231 /* Write the response to a successful qXfer read. Returns the
232 length of the (binary) data stored in BUF, corresponding
233 to as much of DATA/LEN as we could fit. IS_MORE controls
234 the first character of the response. */
236 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
245 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
249 /* Handle all of the extended 'Q' packets. */
251 handle_general_set (char *own_buf
)
253 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
255 int numsigs
= (int) TARGET_SIGNAL_LAST
, i
;
256 const char *p
= own_buf
+ strlen ("QPassSignals:");
259 p
= decode_address_to_semicolon (&cursig
, p
);
260 for (i
= 0; i
< numsigs
; i
++)
266 /* Keep looping, to clear the remaining signals. */
269 p
= decode_address_to_semicolon (&cursig
, p
);
274 strcpy (own_buf
, "OK");
278 if (strcmp (own_buf
, "QStartNoAckMode") == 0)
282 fprintf (stderr
, "[noack mode enabled]\n");
291 /* Otherwise we didn't know what packet it was. Say we didn't
297 get_features_xml (const char *annex
)
299 /* gdbserver_xmltarget defines what to return when looking
300 for the "target.xml" file. Its contents can either be
301 verbatim XML code (prefixed with a '@') or else the name
302 of the actual XML file to be used in place of "target.xml".
304 This variable is set up from the auto-generated
305 init_registers_... routine for the current target. */
307 if (gdbserver_xmltarget
308 && strcmp (annex
, "target.xml") == 0)
310 if (*gdbserver_xmltarget
== '@')
311 return gdbserver_xmltarget
+ 1;
313 annex
= gdbserver_xmltarget
;
318 extern const char *const xml_builtin
[][2];
321 /* Look for the annex. */
322 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
323 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
326 if (xml_builtin
[i
][0] != NULL
)
327 return xml_builtin
[i
][1];
335 monitor_show_help (void)
337 monitor_output ("The following monitor commands are supported:\n");
338 monitor_output (" set debug <0|1>\n");
339 monitor_output (" Enable general debugging messages\n");
340 monitor_output (" set remote-debug <0|1>\n");
341 monitor_output (" Enable remote protocol debugging messages\n");
342 monitor_output (" exit\n");
343 monitor_output (" Quit GDBserver\n");
346 /* Subroutine of handle_search_memory to simplify it. */
349 handle_search_memory_1 (CORE_ADDR start_addr
, CORE_ADDR search_space_len
,
350 gdb_byte
*pattern
, unsigned pattern_len
,
351 gdb_byte
*search_buf
,
352 unsigned chunk_size
, unsigned search_buf_size
,
353 CORE_ADDR
*found_addrp
)
355 /* Prime the search buffer. */
357 if (read_inferior_memory (start_addr
, search_buf
, search_buf_size
) != 0)
359 warning ("Unable to access target memory at 0x%lx, halting search.",
364 /* Perform the search.
366 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
367 When we've scanned N bytes we copy the trailing bytes to the start and
368 read in another N bytes. */
370 while (search_space_len
>= pattern_len
)
373 unsigned nr_search_bytes
= (search_space_len
< search_buf_size
377 found_ptr
= memmem (search_buf
, nr_search_bytes
, pattern
, pattern_len
);
379 if (found_ptr
!= NULL
)
381 CORE_ADDR found_addr
= start_addr
+ (found_ptr
- search_buf
);
382 *found_addrp
= found_addr
;
386 /* Not found in this chunk, skip to next chunk. */
388 /* Don't let search_space_len wrap here, it's unsigned. */
389 if (search_space_len
>= chunk_size
)
390 search_space_len
-= chunk_size
;
392 search_space_len
= 0;
394 if (search_space_len
>= pattern_len
)
396 unsigned keep_len
= search_buf_size
- chunk_size
;
397 CORE_ADDR read_addr
= start_addr
+ keep_len
;
400 /* Copy the trailing part of the previous iteration to the front
401 of the buffer for the next iteration. */
402 memcpy (search_buf
, search_buf
+ chunk_size
, keep_len
);
404 nr_to_read
= (search_space_len
- keep_len
< chunk_size
405 ? search_space_len
- keep_len
408 if (read_inferior_memory (read_addr
, search_buf
+ keep_len
,
411 warning ("Unable to access target memory at 0x%lx, halting search.",
416 start_addr
+= chunk_size
;
425 /* Handle qSearch:memory packets. */
428 handle_search_memory (char *own_buf
, int packet_len
)
430 CORE_ADDR start_addr
;
431 CORE_ADDR search_space_len
;
433 unsigned int pattern_len
;
434 /* NOTE: also defined in find.c testcase. */
435 #define SEARCH_CHUNK_SIZE 16000
436 const unsigned chunk_size
= SEARCH_CHUNK_SIZE
;
437 /* Buffer to hold memory contents for searching. */
438 gdb_byte
*search_buf
;
439 unsigned search_buf_size
;
441 CORE_ADDR found_addr
;
442 int cmd_name_len
= sizeof ("qSearch:memory:") - 1;
444 pattern
= malloc (packet_len
);
447 error ("Unable to allocate memory to perform the search");
448 strcpy (own_buf
, "E00");
451 if (decode_search_memory_packet (own_buf
+ cmd_name_len
,
452 packet_len
- cmd_name_len
,
453 &start_addr
, &search_space_len
,
454 pattern
, &pattern_len
) < 0)
457 error ("Error in parsing qSearch:memory packet");
458 strcpy (own_buf
, "E00");
462 search_buf_size
= chunk_size
+ pattern_len
- 1;
464 /* No point in trying to allocate a buffer larger than the search space. */
465 if (search_space_len
< search_buf_size
)
466 search_buf_size
= search_space_len
;
468 search_buf
= malloc (search_buf_size
);
469 if (search_buf
== NULL
)
472 error ("Unable to allocate memory to perform the search");
473 strcpy (own_buf
, "E00");
477 found
= handle_search_memory_1 (start_addr
, search_space_len
,
478 pattern
, pattern_len
,
479 search_buf
, chunk_size
, search_buf_size
,
483 sprintf (own_buf
, "1,%lx", (long) found_addr
);
485 strcpy (own_buf
, "0");
487 strcpy (own_buf
, "E00");
493 #define require_running(BUF) \
494 if (!target_running ()) \
500 /* Handle all of the extended 'q' packets. */
502 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
504 static struct inferior_list_entry
*thread_ptr
;
506 /* Reply the current thread id. */
507 if (strcmp ("qC", own_buf
) == 0 && !disable_packet_qC
)
509 require_running (own_buf
);
510 thread_ptr
= all_threads
.head
;
511 sprintf (own_buf
, "QC%x",
512 thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
516 if (strcmp ("qSymbol::", own_buf
) == 0)
518 if (target_running () && the_target
->look_up_symbols
!= NULL
)
519 (*the_target
->look_up_symbols
) ();
521 strcpy (own_buf
, "OK");
525 if (!disable_packet_qfThreadInfo
)
527 if (strcmp ("qfThreadInfo", own_buf
) == 0)
529 require_running (own_buf
);
530 thread_ptr
= all_threads
.head
;
531 sprintf (own_buf
, "m%x",
532 thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
533 thread_ptr
= thread_ptr
->next
;
537 if (strcmp ("qsThreadInfo", own_buf
) == 0)
539 require_running (own_buf
);
540 if (thread_ptr
!= NULL
)
542 sprintf (own_buf
, "m%x",
543 thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
544 thread_ptr
= thread_ptr
->next
;
549 sprintf (own_buf
, "l");
555 if (the_target
->read_offsets
!= NULL
556 && strcmp ("qOffsets", own_buf
) == 0)
558 CORE_ADDR text
, data
;
560 require_running (own_buf
);
561 if (the_target
->read_offsets (&text
, &data
))
562 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
563 (long)text
, (long)data
, (long)data
);
570 if (the_target
->qxfer_spu
!= NULL
571 && strncmp ("qXfer:spu:read:", own_buf
, 15) == 0)
577 unsigned char *spu_buf
;
579 require_running (own_buf
);
580 strcpy (own_buf
, "E00");
581 if (decode_xfer_read (own_buf
+ 15, &annex
, &ofs
, &len
) < 0)
583 if (len
> PBUFSIZ
- 2)
585 spu_buf
= malloc (len
+ 1);
589 n
= (*the_target
->qxfer_spu
) (annex
, spu_buf
, NULL
, ofs
, len
+ 1);
593 *new_packet_len_p
= write_qxfer_response (own_buf
, spu_buf
, len
, 1);
595 *new_packet_len_p
= write_qxfer_response (own_buf
, spu_buf
, n
, 0);
601 if (the_target
->qxfer_spu
!= NULL
602 && strncmp ("qXfer:spu:write:", own_buf
, 16) == 0)
608 unsigned char *spu_buf
;
610 require_running (own_buf
);
611 strcpy (own_buf
, "E00");
612 spu_buf
= malloc (packet_len
- 15);
615 if (decode_xfer_write (own_buf
+ 16, packet_len
- 16, &annex
,
616 &ofs
, &len
, spu_buf
) < 0)
622 n
= (*the_target
->qxfer_spu
)
623 (annex
, NULL
, (unsigned const char *)spu_buf
, ofs
, len
);
627 sprintf (own_buf
, "%x", n
);
633 if (the_target
->read_auxv
!= NULL
634 && strncmp ("qXfer:auxv:read:", own_buf
, 16) == 0)
642 require_running (own_buf
);
644 /* Reject any annex; grab the offset and length. */
645 if (decode_xfer_read (own_buf
+ 16, &annex
, &ofs
, &len
) < 0
648 strcpy (own_buf
, "E00");
652 /* Read one extra byte, as an indicator of whether there is
654 if (len
> PBUFSIZ
- 2)
656 data
= malloc (len
+ 1);
662 n
= (*the_target
->read_auxv
) (ofs
, data
, len
+ 1);
666 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
668 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
675 if (strncmp ("qXfer:features:read:", own_buf
, 20) == 0)
678 unsigned int len
, total_len
;
679 const char *document
;
682 require_running (own_buf
);
684 /* Grab the annex, offset, and length. */
685 if (decode_xfer_read (own_buf
+ 20, &annex
, &ofs
, &len
) < 0)
687 strcpy (own_buf
, "E00");
691 /* Now grab the correct annex. */
692 document
= get_features_xml (annex
);
693 if (document
== NULL
)
695 strcpy (own_buf
, "E00");
699 total_len
= strlen (document
);
700 if (len
> PBUFSIZ
- 2)
705 else if (len
< total_len
- ofs
)
706 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
709 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
715 if (strncmp ("qXfer:libraries:read:", own_buf
, 21) == 0)
718 unsigned int len
, total_len
;
720 struct inferior_list_entry
*dll_ptr
;
723 require_running (own_buf
);
725 /* Reject any annex; grab the offset and length. */
726 if (decode_xfer_read (own_buf
+ 21, &annex
, &ofs
, &len
) < 0
729 strcpy (own_buf
, "E00");
733 /* Over-estimate the necessary memory. Assume that every character
734 in the library name must be escaped. */
736 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
737 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
739 document
= malloc (total_len
);
740 if (document
== NULL
)
745 strcpy (document
, "<library-list>\n");
746 p
= document
+ strlen (document
);
748 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
750 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
753 strcpy (p
, " <library name=\"");
755 name
= xml_escape_text (dll
->name
);
759 strcpy (p
, "\"><segment address=\"");
761 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
763 strcpy (p
, "\"/></library>\n");
767 strcpy (p
, "</library-list>\n");
769 total_len
= strlen (document
);
770 if (len
> PBUFSIZ
- 2)
775 else if (len
< total_len
- ofs
)
776 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
779 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
786 if (the_target
->qxfer_osdata
!= NULL
787 && strncmp ("qXfer:osdata:read:", own_buf
, 18) == 0)
793 unsigned char *workbuf
;
795 strcpy (own_buf
, "E00");
796 if (decode_xfer_read (own_buf
+ 18, &annex
, &ofs
, &len
) < 0)
798 if (len
> PBUFSIZ
- 2)
800 workbuf
= malloc (len
+ 1);
804 n
= (*the_target
->qxfer_osdata
) (annex
, workbuf
, NULL
, ofs
, len
+ 1);
808 *new_packet_len_p
= write_qxfer_response (own_buf
, workbuf
, len
, 1);
810 *new_packet_len_p
= write_qxfer_response (own_buf
, workbuf
, n
, 0);
816 if (the_target
->qxfer_siginfo
!= NULL
817 && strncmp ("qXfer:siginfo:read:", own_buf
, 19) == 0)
825 require_running (own_buf
);
827 /* Reject any annex; grab the offset and length. */
828 if (decode_xfer_read (own_buf
+ 19, &annex
, &ofs
, &len
) < 0
831 strcpy (own_buf
, "E00");
835 /* Read one extra byte, as an indicator of whether there is
837 if (len
> PBUFSIZ
- 2)
839 data
= malloc (len
+ 1);
842 n
= (*the_target
->qxfer_siginfo
) (annex
, data
, NULL
, ofs
, len
+ 1);
846 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
848 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
854 if (the_target
->qxfer_siginfo
!= NULL
855 && strncmp ("qXfer:siginfo:write:", own_buf
, 20) == 0)
863 require_running (own_buf
);
865 strcpy (own_buf
, "E00");
866 data
= malloc (packet_len
- 19);
869 if (decode_xfer_write (own_buf
+ 20, packet_len
- 20, &annex
,
870 &ofs
, &len
, data
) < 0)
876 n
= (*the_target
->qxfer_siginfo
)
877 (annex
, NULL
, (unsigned const char *)data
, ofs
, len
);
881 sprintf (own_buf
, "%x", n
);
887 /* Protocol features query. */
888 if (strncmp ("qSupported", own_buf
, 10) == 0
889 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
891 sprintf (own_buf
, "PacketSize=%x;QPassSignals+", PBUFSIZ
- 1);
893 /* We do not have any hook to indicate whether the target backend
894 supports qXfer:libraries:read, so always report it. */
895 strcat (own_buf
, ";qXfer:libraries:read+");
897 if (the_target
->read_auxv
!= NULL
)
898 strcat (own_buf
, ";qXfer:auxv:read+");
900 if (the_target
->qxfer_spu
!= NULL
)
901 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
903 if (the_target
->qxfer_siginfo
!= NULL
)
904 strcat (own_buf
, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
906 /* We always report qXfer:features:read, as targets may
907 install XML files on a subsequent call to arch_setup.
908 If we reported to GDB on startup that we don't support
909 qXfer:feature:read at all, we will never be re-queried. */
910 strcat (own_buf
, ";qXfer:features:read+");
912 if (transport_is_reliable
)
913 strcat (own_buf
, ";QStartNoAckMode+");
915 if (the_target
->qxfer_osdata
!= NULL
)
916 strcat (own_buf
, ";qXfer:osdata:read+");
921 /* Thread-local storage support. */
922 if (the_target
->get_tls_address
!= NULL
923 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
925 char *p
= own_buf
+ 12;
926 CORE_ADDR parts
[2], address
= 0;
930 require_running (own_buf
);
932 for (i
= 0; i
< 3; i
++)
940 p2
= strchr (p
, ',');
953 ptid
= strtoul (p
, NULL
, 16);
955 decode_address (&parts
[i
- 1], p
, len
);
959 if (p
!= NULL
|| i
< 3)
963 struct thread_info
*thread
= gdb_id_to_thread (ptid
);
968 err
= the_target
->get_tls_address (thread
, parts
[0], parts
[1],
974 sprintf (own_buf
, "%llx", address
);
983 /* Otherwise, pretend we do not understand this packet. */
986 /* Handle "monitor" commands. */
987 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
989 char *mon
= malloc (PBUFSIZ
);
990 int len
= strlen (own_buf
+ 6);
998 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
1000 write_enn (own_buf
);
1004 mon
[len
/ 2] = '\0';
1008 if (strcmp (mon
, "set debug 1") == 0)
1011 monitor_output ("Debug output enabled.\n");
1013 else if (strcmp (mon
, "set debug 0") == 0)
1016 monitor_output ("Debug output disabled.\n");
1018 else if (strcmp (mon
, "set remote-debug 1") == 0)
1021 monitor_output ("Protocol debug output enabled.\n");
1023 else if (strcmp (mon
, "set remote-debug 0") == 0)
1026 monitor_output ("Protocol debug output disabled.\n");
1028 else if (strcmp (mon
, "help") == 0)
1029 monitor_show_help ();
1030 else if (strcmp (mon
, "exit") == 0)
1034 monitor_output ("Unknown monitor command.\n\n");
1035 monitor_show_help ();
1036 write_enn (own_buf
);
1043 if (strncmp ("qSearch:memory:", own_buf
, sizeof ("qSearch:memory:") - 1) == 0)
1045 require_running (own_buf
);
1046 handle_search_memory (own_buf
, packet_len
);
1050 if (strcmp (own_buf
, "qAttached") == 0)
1052 require_running (own_buf
);
1053 strcpy (own_buf
, attached
? "1" : "0");
1057 /* Otherwise we didn't know what packet it was. Say we didn't
1062 /* Parse vCont packets. */
1064 handle_v_cont (char *own_buf
)
1068 struct thread_resume
*resume_info
;
1069 struct thread_resume default_action
= {0};
1071 /* Count the number of semicolons in the packet. There should be one
1072 for every action. */
1078 p
= strchr (p
, ';');
1081 resume_info
= malloc (n
* sizeof (resume_info
[0]));
1082 if (resume_info
== NULL
)
1090 if (p
[0] == 's' || p
[0] == 'S')
1091 resume_info
[i
].step
= 1;
1092 else if (p
[0] == 'c' || p
[0] == 'C')
1093 resume_info
[i
].step
= 0;
1097 if (p
[0] == 'S' || p
[0] == 'C')
1100 sig
= strtol (p
+ 1, &q
, 16);
1105 if (!target_signal_to_host_p (sig
))
1107 resume_info
[i
].sig
= target_signal_to_host (sig
);
1111 resume_info
[i
].sig
= 0;
1117 resume_info
[i
].thread
= -1;
1118 default_action
= resume_info
[i
];
1120 /* Note: we don't increment i here, we'll overwrite this entry
1121 the next time through. */
1123 else if (p
[0] == ':')
1125 unsigned int gdb_id
= strtoul (p
+ 1, &q
, 16);
1126 unsigned long thread_id
;
1131 if (p
[0] != ';' && p
[0] != 0)
1134 thread_id
= gdb_id_to_thread_id (gdb_id
);
1136 resume_info
[i
].thread
= thread_id
;
1145 resume_info
[i
] = default_action
;
1147 /* Still used in occasional places in the backend. */
1148 if (n
== 1 && resume_info
[0].thread
!= -1)
1149 cont_thread
= resume_info
[0].thread
;
1152 set_desired_inferior (0);
1155 (*the_target
->resume
) (resume_info
, n
);
1159 last_ptid
= mywait (&last_status
, 1);
1160 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1161 disable_async_io ();
1165 write_enn (own_buf
);
1170 /* Attach to a new program. Return 1 if successful, 0 if failure. */
1172 handle_v_attach (char *own_buf
)
1176 pid
= strtol (own_buf
+ 8, NULL
, 16);
1177 if (pid
!= 0 && attach_inferior (pid
) == 0)
1179 /* Don't report shared library events after attaching, even if
1180 some libraries are preloaded. GDB will always poll the
1181 library list. Avoids the "stopped by shared library event"
1182 notice on the GDB side. */
1184 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1189 write_enn (own_buf
);
1194 /* Run a new program. Return 1 if successful, 0 if failure. */
1196 handle_v_run (char *own_buf
)
1198 char *p
, *next_p
, **new_argv
;
1202 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
1208 new_argv
= calloc (new_argc
+ 2, sizeof (char *));
1209 if (new_argv
== NULL
)
1211 write_enn (own_buf
);
1216 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
1218 next_p
= strchr (p
, ';');
1220 next_p
= p
+ strlen (p
);
1222 if (i
== 0 && p
== next_p
)
1226 /* FIXME: Fail request if out of memory instead of dying. */
1227 new_argv
[i
] = xmalloc (1 + (next_p
- p
) / 2);
1228 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
1229 new_argv
[i
][(next_p
- p
) / 2] = '\0';
1238 if (new_argv
[0] == NULL
)
1240 /* GDB didn't specify a program to run. Use the program from the
1241 last run with the new argument list. */
1243 if (program_argv
== NULL
)
1245 /* FIXME: new_argv memory leak */
1246 write_enn (own_buf
);
1250 new_argv
[0] = strdup (program_argv
[0]);
1251 if (new_argv
[0] == NULL
)
1253 /* FIXME: new_argv memory leak */
1254 write_enn (own_buf
);
1259 /* Free the old argv and install the new one. */
1260 freeargv (program_argv
);
1261 program_argv
= new_argv
;
1263 start_inferior (program_argv
);
1264 if (last_status
.kind
== TARGET_WAITKIND_STOPPED
)
1266 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1271 write_enn (own_buf
);
1276 /* Handle all of the extended 'v' packets. */
1278 handle_v_requests (char *own_buf
, int packet_len
, int *new_packet_len
)
1280 if (!disable_packet_vCont
)
1282 if (strncmp (own_buf
, "vCont;", 6) == 0)
1284 require_running (own_buf
);
1285 handle_v_cont (own_buf
);
1289 if (strncmp (own_buf
, "vCont?", 6) == 0)
1291 strcpy (own_buf
, "vCont;c;C;s;S");
1296 if (strncmp (own_buf
, "vFile:", 6) == 0
1297 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
1300 if (strncmp (own_buf
, "vAttach;", 8) == 0)
1302 if (target_running ())
1304 fprintf (stderr
, "Already debugging a process\n");
1305 write_enn (own_buf
);
1308 handle_v_attach (own_buf
);
1312 if (strncmp (own_buf
, "vRun;", 5) == 0)
1314 if (target_running ())
1316 fprintf (stderr
, "Already debugging a process\n");
1317 write_enn (own_buf
);
1320 handle_v_run (own_buf
);
1324 /* Otherwise we didn't know what packet it was. Say we didn't
1331 myresume (char *own_buf
, int step
, int sig
)
1333 struct thread_resume resume_info
[2];
1335 int valid_cont_thread
;
1337 set_desired_inferior (0);
1339 valid_cont_thread
= (cont_thread
!= 0 && cont_thread
!= -1);
1341 if (step
|| sig
|| valid_cont_thread
)
1343 resume_info
[0].thread
1344 = ((struct inferior_list_entry
*) current_inferior
)->id
;
1345 resume_info
[0].step
= step
;
1346 resume_info
[0].sig
= sig
;
1350 if (!valid_cont_thread
)
1352 resume_info
[n
].thread
= -1;
1353 resume_info
[n
].step
= 0;
1354 resume_info
[n
].sig
= 0;
1359 (*the_target
->resume
) (resume_info
, n
);
1360 last_ptid
= mywait (&last_status
, 1);
1361 prepare_resume_reply (own_buf
, last_ptid
, &last_status
);
1362 disable_async_io ();
1365 /* Status handler for the '?' packet. */
1368 handle_status (char *own_buf
)
1370 if (all_threads
.head
)
1371 prepare_resume_reply (own_buf
,
1372 all_threads
.head
->id
, &last_status
);
1374 strcpy (own_buf
, "W00");
1378 gdbserver_version (void)
1380 printf ("GNU gdbserver %s%s\n"
1381 "Copyright (C) 2009 Free Software Foundation, Inc.\n"
1382 "gdbserver is free software, covered by the GNU General Public License.\n"
1383 "This gdbserver was configured as \"%s\"\n",
1384 PKGVERSION
, version
, host_name
);
1388 gdbserver_usage (FILE *stream
)
1390 fprintf (stream
, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
1391 "\tgdbserver [OPTIONS] --attach COMM PID\n"
1392 "\tgdbserver [OPTIONS] --multi COMM\n"
1394 "COMM may either be a tty device (for serial debugging), or \n"
1395 "HOST:PORT to listen for a TCP connection.\n"
1398 " --debug Enable general debugging output.\n"
1399 " --remote-debug Enable remote protocol debugging output.\n"
1400 " --version Display version information and exit.\n"
1401 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n");
1402 if (REPORT_BUGS_TO
[0] && stream
== stdout
)
1403 fprintf (stream
, "Report bugs to \"%s\".\n", REPORT_BUGS_TO
);
1407 gdbserver_show_disableable (FILE *stream
)
1409 fprintf (stream
, "Disableable packets:\n"
1410 " vCont \tAll vCont packets\n"
1411 " qC \tQuerying the current thread\n"
1412 " qfThreadInfo\tThread listing\n"
1413 " Tthread \tPassing the thread specifier in the T stop reply packet\n"
1414 " threads \tAll of the above\n");
1418 #undef require_running
1419 #define require_running(BUF) \
1420 if (!target_running ()) \
1427 main (int argc
, char *argv
[])
1430 unsigned char *mem_buf
;
1437 char *arg_end
, *port
;
1438 char **next_arg
= &argv
[1];
1443 while (*next_arg
!= NULL
&& **next_arg
== '-')
1445 if (strcmp (*next_arg
, "--version") == 0)
1447 gdbserver_version ();
1450 else if (strcmp (*next_arg
, "--help") == 0)
1452 gdbserver_usage (stdout
);
1455 else if (strcmp (*next_arg
, "--attach") == 0)
1457 else if (strcmp (*next_arg
, "--multi") == 0)
1459 else if (strcmp (*next_arg
, "--wrapper") == 0)
1463 wrapper_argv
= next_arg
;
1464 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
1467 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
1469 gdbserver_usage (stderr
);
1473 /* Consume the "--". */
1476 else if (strcmp (*next_arg
, "--debug") == 0)
1478 else if (strcmp (*next_arg
, "--remote-debug") == 0)
1480 else if (strcmp (*next_arg
, "--disable-packet") == 0)
1482 gdbserver_show_disableable (stdout
);
1485 else if (strncmp (*next_arg
,
1486 "--disable-packet=",
1487 sizeof ("--disable-packet=") - 1) == 0)
1489 char *packets
, *tok
;
1491 packets
= *next_arg
+= sizeof ("--disable-packet=") - 1;
1492 for (tok
= strtok (packets
, ",");
1494 tok
= strtok (NULL
, ","))
1496 if (strcmp ("vCont", tok
) == 0)
1497 disable_packet_vCont
= 1;
1498 else if (strcmp ("Tthread", tok
) == 0)
1499 disable_packet_Tthread
= 1;
1500 else if (strcmp ("qC", tok
) == 0)
1501 disable_packet_qC
= 1;
1502 else if (strcmp ("qfThreadInfo", tok
) == 0)
1503 disable_packet_qfThreadInfo
= 1;
1504 else if (strcmp ("threads", tok
) == 0)
1506 disable_packet_vCont
= 1;
1507 disable_packet_Tthread
= 1;
1508 disable_packet_qC
= 1;
1509 disable_packet_qfThreadInfo
= 1;
1513 fprintf (stderr
, "Don't know how to disable \"%s\".\n\n",
1515 gdbserver_show_disableable (stderr
);
1522 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
1530 if (setjmp (toplevel
))
1532 fprintf (stderr
, "Exiting\n");
1538 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
1540 gdbserver_usage (stderr
);
1547 /* --attach used to come after PORT, so allow it there for
1549 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
1556 && (*next_arg
== NULL
1557 || (*next_arg
)[0] == '\0'
1558 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
1560 || next_arg
[1] != NULL
))
1565 gdbserver_usage (stderr
);
1569 initialize_async_io ();
1572 own_buf
= xmalloc (PBUFSIZ
+ 1);
1573 mem_buf
= xmalloc (PBUFSIZ
);
1575 if (pid
== 0 && *next_arg
!= NULL
)
1579 n
= argc
- (next_arg
- argv
);
1580 program_argv
= xmalloc (sizeof (char *) * (n
+ 1));
1581 for (i
= 0; i
< n
; i
++)
1582 program_argv
[i
] = xstrdup (next_arg
[i
]);
1583 program_argv
[i
] = NULL
;
1585 /* Wait till we are at first instruction in program. */
1586 start_inferior (program_argv
);
1588 /* We are now (hopefully) stopped at the first instruction of
1589 the target process. This assumes that the target process was
1590 successfully created. */
1594 if (attach_inferior (pid
) == -1)
1595 error ("Attaching not supported on this target");
1597 /* Otherwise succeeded. */
1601 last_status
.kind
= TARGET_WAITKIND_EXITED
;
1602 last_status
.value
.integer
= 0;
1606 /* Don't report shared library events on the initial connection,
1607 even if some libraries are preloaded. Avoids the "stopped by
1608 shared library event" notice on gdb side. */
1611 if (setjmp (toplevel
))
1613 fprintf (stderr
, "Killing inferior\n");
1618 if (last_status
.kind
== TARGET_WAITKIND_EXITED
1619 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
1624 if (!was_running
&& !multi_mode
)
1626 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
1636 if (setjmp (toplevel
) != 0)
1638 /* An error occurred. */
1639 if (response_needed
)
1641 write_enn (own_buf
);
1646 disable_async_io ();
1647 while (!exit_requested
)
1651 int new_packet_len
= -1;
1653 response_needed
= 0;
1654 packet_len
= getpkt (own_buf
);
1655 if (packet_len
<= 0)
1657 response_needed
= 1;
1664 handle_query (own_buf
, packet_len
, &new_packet_len
);
1667 handle_general_set (own_buf
);
1670 require_running (own_buf
);
1671 fprintf (stderr
, "Detaching from inferior\n");
1672 if (detach_inferior () != 0)
1673 write_enn (own_buf
);
1678 if (extended_protocol
)
1680 /* Treat this like a normal program exit. */
1681 last_status
.kind
= TARGET_WAITKIND_EXITED
;
1682 last_status
.value
.integer
= 0;
1683 last_ptid
= signal_pid
;
1690 /* If we are attached, then we can exit. Otherwise, we
1691 need to hang around doing nothing, until the child
1701 extended_protocol
= 1;
1705 handle_status (own_buf
);
1708 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
1710 unsigned long gdb_id
, thread_id
;
1712 require_running (own_buf
);
1713 gdb_id
= strtoul (&own_buf
[2], NULL
, 16);
1714 if (gdb_id
== 0 || gdb_id
== -1)
1718 thread_id
= gdb_id_to_thread_id (gdb_id
);
1721 write_enn (own_buf
);
1726 if (own_buf
[1] == 'g')
1728 general_thread
= thread_id
;
1729 set_desired_inferior (1);
1731 else if (own_buf
[1] == 'c')
1732 cont_thread
= thread_id
;
1733 else if (own_buf
[1] == 's')
1734 step_thread
= thread_id
;
1740 /* Silently ignore it so that gdb can extend the protocol
1741 without compatibility headaches. */
1746 require_running (own_buf
);
1747 set_desired_inferior (1);
1748 registers_to_string (own_buf
);
1751 require_running (own_buf
);
1752 set_desired_inferior (1);
1753 registers_from_string (&own_buf
[1]);
1757 require_running (own_buf
);
1758 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
1759 if (read_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
1760 convert_int_to_ascii (mem_buf
, own_buf
, len
);
1762 write_enn (own_buf
);
1765 require_running (own_buf
);
1766 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
1767 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
1770 write_enn (own_buf
);
1773 require_running (own_buf
);
1774 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
1775 &mem_addr
, &len
, mem_buf
) < 0
1776 || write_inferior_memory (mem_addr
, mem_buf
, len
) != 0)
1777 write_enn (own_buf
);
1782 require_running (own_buf
);
1783 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
1784 if (target_signal_to_host_p (sig
))
1785 signal
= target_signal_to_host (sig
);
1788 myresume (own_buf
, 0, signal
);
1791 require_running (own_buf
);
1792 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
1793 if (target_signal_to_host_p (sig
))
1794 signal
= target_signal_to_host (sig
);
1797 myresume (own_buf
, 1, signal
);
1800 require_running (own_buf
);
1802 myresume (own_buf
, 0, signal
);
1805 require_running (own_buf
);
1807 myresume (own_buf
, 1, signal
);
1813 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
1814 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
1815 char type
= own_buf
[1];
1817 if (the_target
->insert_watchpoint
== NULL
1818 || (type
< '2' || type
> '4'))
1820 /* No watchpoint support or not a watchpoint command;
1821 unrecognized either way. */
1828 require_running (own_buf
);
1829 res
= (*the_target
->insert_watchpoint
) (type
, addr
, len
);
1836 write_enn (own_buf
);
1844 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
1845 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
1846 char type
= own_buf
[1];
1848 if (the_target
->remove_watchpoint
== NULL
1849 || (type
< '2' || type
> '4'))
1851 /* No watchpoint support or not a watchpoint command;
1852 unrecognized either way. */
1859 require_running (own_buf
);
1860 res
= (*the_target
->remove_watchpoint
) (type
, addr
, len
);
1867 write_enn (own_buf
);
1872 response_needed
= 0;
1873 if (!target_running ())
1874 /* The packet we received doesn't make sense - but we
1875 can't reply to it, either. */
1878 fprintf (stderr
, "Killing inferior\n");
1881 /* When using the extended protocol, we wait with no
1882 program running. The traditional protocol will exit
1884 if (extended_protocol
)
1886 last_status
.kind
= TARGET_WAITKIND_EXITED
;
1887 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
1898 unsigned long gdb_id
, thread_id
;
1900 require_running (own_buf
);
1901 gdb_id
= strtoul (&own_buf
[1], NULL
, 16);
1902 thread_id
= gdb_id_to_thread_id (gdb_id
);
1905 write_enn (own_buf
);
1909 if (mythread_alive (thread_id
))
1912 write_enn (own_buf
);
1916 response_needed
= 0;
1918 /* Restarting the inferior is only supported in the
1919 extended protocol. */
1920 if (extended_protocol
)
1922 if (target_running ())
1924 fprintf (stderr
, "GDBserver restarting\n");
1926 /* Wait till we are at 1st instruction in prog. */
1927 if (program_argv
!= NULL
)
1928 start_inferior (program_argv
);
1931 last_status
.kind
= TARGET_WAITKIND_EXITED
;
1932 last_status
.value
.sig
= TARGET_SIGNAL_KILL
;
1938 /* It is a request we don't understand. Respond with an
1939 empty packet so that gdb knows that we don't support this
1945 /* Extended (long) request. */
1946 handle_v_requests (own_buf
, packet_len
, &new_packet_len
);
1950 /* It is a request we don't understand. Respond with an
1951 empty packet so that gdb knows that we don't support this
1957 if (new_packet_len
!= -1)
1958 putpkt_binary (own_buf
, new_packet_len
);
1962 response_needed
= 0;
1965 && (last_status
.kind
== TARGET_WAITKIND_EXITED
1966 || last_status
.kind
== TARGET_WAITKIND_SIGNALLED
))
1970 if (last_status
.kind
== TARGET_WAITKIND_EXITED
)
1972 "\nChild exited with status %d\n",
1973 last_status
.value
.integer
);
1974 else if (last_status
.kind
== TARGET_WAITKIND_SIGNALLED
)
1975 fprintf (stderr
, "\nChild terminated with signal = 0x%x (%s)\n",
1976 target_signal_to_host (last_status
.value
.sig
),
1977 target_signal_to_name (last_status
.value
.sig
));
1979 if (extended_protocol
)
1983 fprintf (stderr
, "GDBserver exiting\n");
1989 if (last_status
.kind
!= TARGET_WAITKIND_EXITED
1990 && last_status
.kind
!= TARGET_WAITKIND_SIGNALLED
)
1994 /* If an exit was requested (using the "monitor exit" command),
1995 terminate now. The only other way to get here is for
1996 getpkt to fail; close the connection and reopen it at the
2002 if (attached
&& target_running ())
2004 else if (target_running ())
2010 fprintf (stderr
, "Remote side has terminated connection. "
2011 "GDBserver will reopen the connection.\n");