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 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/>. */
32 unsigned long cont_thread
;
33 unsigned long general_thread
;
34 unsigned long step_thread
;
35 unsigned long thread_from_wait
;
36 unsigned long old_thread_from_wait
;
39 static int extended_protocol
;
41 static int response_needed
;
42 static int exit_requested
;
44 static char **program_argv
;
46 /* Enable miscellaneous debugging output. The name is historical - it
47 was originally used to debug LinuxThreads support. */
50 int pass_signals
[TARGET_SIGNAL_LAST
];
54 const char *gdbserver_xmltarget
;
56 /* The PID of the originally created or attached inferior. Used to
57 send signals to the process when GDB sends us an asynchronous interrupt
58 (user hitting Control-C in the client), and to wait for the child to exit
59 when no longer debugging it. */
61 unsigned long signal_pid
;
64 /* A file descriptor for the controlling terminal. */
67 /* TERMINAL_FD's original foreground group. */
68 pid_t old_foreground_pgrp
;
70 /* Hand back terminal ownership to the original foreground group. */
73 restore_old_foreground_pgrp (void)
75 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
82 return all_threads
.head
!= NULL
;
86 start_inferior (char *argv
[], char *statusptr
)
91 signal (SIGTTOU
, SIG_DFL
);
92 signal (SIGTTIN
, SIG_DFL
);
95 signal_pid
= create_inferior (argv
[0], argv
);
97 /* FIXME: we don't actually know at this point that the create
98 actually succeeded. We won't know that until we wait. */
99 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
104 signal (SIGTTOU
, SIG_IGN
);
105 signal (SIGTTIN
, SIG_IGN
);
106 terminal_fd
= fileno (stderr
);
107 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
108 tcsetpgrp (terminal_fd
, signal_pid
);
109 atexit (restore_old_foreground_pgrp
);
112 /* Wait till we are at 1st instruction in program, return signal
113 number (assuming success). */
114 return mywait (statusptr
, 0);
118 attach_inferior (int pid
, char *statusptr
, int *sigptr
)
120 /* myattach should return -1 if attaching is unsupported,
121 0 if it succeeded, and call error() otherwise. */
123 if (myattach (pid
) != 0)
128 fprintf (stderr
, "Attached; pid = %d\n", pid
);
131 /* FIXME - It may be that we should get the SIGNAL_PID from the
132 attach function, so that it can be the main thread instead of
133 whichever we were told to attach to. */
136 *sigptr
= mywait (statusptr
, 0);
138 /* GDB knows to ignore the first SIGSTOP after attaching to a running
139 process using the "attach" command, but this is different; it's
140 just using "target remote". Pretend it's just starting up. */
141 if (*statusptr
== 'T' && *sigptr
== TARGET_SIGNAL_STOP
)
142 *sigptr
= TARGET_SIGNAL_TRAP
;
147 extern int remote_debug
;
149 /* Decode a qXfer read request. Return 0 if everything looks OK,
153 decode_xfer_read (char *buf
, char **annex
, CORE_ADDR
*ofs
, unsigned int *len
)
155 /* Extract and NUL-terminate the annex. */
157 while (*buf
&& *buf
!= ':')
163 /* After the read marker and annex, qXfer looks like a
164 traditional 'm' packet. */
165 decode_m_packet (buf
, ofs
, len
);
170 /* Write the response to a successful qXfer read. Returns the
171 length of the (binary) data stored in BUF, corresponding
172 to as much of DATA/LEN as we could fit. IS_MORE controls
173 the first character of the response. */
175 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
184 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
188 /* Handle all of the extended 'Q' packets. */
190 handle_general_set (char *own_buf
)
192 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
194 int numsigs
= (int) TARGET_SIGNAL_LAST
, i
;
195 const char *p
= own_buf
+ strlen ("QPassSignals:");
198 p
= decode_address_to_semicolon (&cursig
, p
);
199 for (i
= 0; i
< numsigs
; i
++)
205 /* Keep looping, to clear the remaining signals. */
208 p
= decode_address_to_semicolon (&cursig
, p
);
213 strcpy (own_buf
, "OK");
217 /* Otherwise we didn't know what packet it was. Say we didn't
223 get_features_xml (const char *annex
)
225 /* gdbserver_xmltarget defines what to return when looking
226 for the "target.xml" file. Its contents can either be
227 verbatim XML code (prefixed with a '@') or else the name
228 of the actual XML file to be used in place of "target.xml".
230 This variable is set up from the auto-generated
231 init_registers_... routine for the current target. */
233 if (gdbserver_xmltarget
234 && strcmp (annex
, "target.xml") != 0)
236 if (*gdbserver_xmltarget
== '@')
237 return gdbserver_xmltarget
+ 1;
239 annex
= gdbserver_xmltarget
;
244 extern const char *const xml_builtin
[][2];
247 /* Look for the annex. */
248 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
249 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
252 if (xml_builtin
[i
][0] != NULL
)
253 return xml_builtin
[i
][1];
261 monitor_show_help (void)
263 monitor_output ("The following monitor commands are supported:\n");
264 monitor_output (" set debug <0|1>\n");
265 monitor_output (" Enable general debugging messages\n");
266 monitor_output (" set remote-debug <0|1>\n");
267 monitor_output (" Enable remote protocol debugging messages\n");
268 monitor_output (" exit\n");
269 monitor_output (" Quit GDBserver\n");
272 #define require_running(BUF) \
273 if (!target_running ()) \
279 /* Handle all of the extended 'q' packets. */
281 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
283 static struct inferior_list_entry
*thread_ptr
;
285 /* Reply the current thread id. */
286 if (strcmp ("qC", own_buf
) == 0)
288 require_running (own_buf
);
289 thread_ptr
= all_threads
.head
;
290 sprintf (own_buf
, "QC%x",
291 thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
295 if (strcmp ("qSymbol::", own_buf
) == 0)
297 if (target_running () && the_target
->look_up_symbols
!= NULL
)
298 (*the_target
->look_up_symbols
) ();
300 strcpy (own_buf
, "OK");
304 if (strcmp ("qfThreadInfo", own_buf
) == 0)
306 require_running (own_buf
);
307 thread_ptr
= all_threads
.head
;
308 sprintf (own_buf
, "m%x", thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
309 thread_ptr
= thread_ptr
->next
;
313 if (strcmp ("qsThreadInfo", own_buf
) == 0)
315 require_running (own_buf
);
316 if (thread_ptr
!= NULL
)
318 sprintf (own_buf
, "m%x", thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
319 thread_ptr
= thread_ptr
->next
;
324 sprintf (own_buf
, "l");
329 if (the_target
->read_offsets
!= NULL
330 && strcmp ("qOffsets", own_buf
) == 0)
332 CORE_ADDR text
, data
;
334 require_running (own_buf
);
335 if (the_target
->read_offsets (&text
, &data
))
336 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
337 (long)text
, (long)data
, (long)data
);
344 if (the_target
->qxfer_spu
!= NULL
345 && strncmp ("qXfer:spu:read:", own_buf
, 15) == 0)
351 unsigned char *spu_buf
;
353 require_running (own_buf
);
354 strcpy (own_buf
, "E00");
355 if (decode_xfer_read (own_buf
+ 15, &annex
, &ofs
, &len
) < 0)
357 if (len
> PBUFSIZ
- 2)
359 spu_buf
= malloc (len
+ 1);
363 n
= (*the_target
->qxfer_spu
) (annex
, spu_buf
, NULL
, ofs
, len
+ 1);
367 *new_packet_len_p
= write_qxfer_response
368 (own_buf
, spu_buf
, len
, 1);
370 *new_packet_len_p
= write_qxfer_response
371 (own_buf
, spu_buf
, n
, 0);
377 if (the_target
->qxfer_spu
!= NULL
378 && strncmp ("qXfer:spu:write:", own_buf
, 16) == 0)
384 unsigned char *spu_buf
;
386 require_running (own_buf
);
387 strcpy (own_buf
, "E00");
388 spu_buf
= malloc (packet_len
- 15);
391 if (decode_xfer_write (own_buf
+ 16, packet_len
- 16, &annex
,
392 &ofs
, &len
, spu_buf
) < 0)
398 n
= (*the_target
->qxfer_spu
)
399 (annex
, NULL
, (unsigned const char *)spu_buf
, ofs
, len
);
403 sprintf (own_buf
, "%x", n
);
409 if (the_target
->read_auxv
!= NULL
410 && strncmp ("qXfer:auxv:read:", own_buf
, 16) == 0)
418 require_running (own_buf
);
420 /* Reject any annex; grab the offset and length. */
421 if (decode_xfer_read (own_buf
+ 16, &annex
, &ofs
, &len
) < 0
424 strcpy (own_buf
, "E00");
428 /* Read one extra byte, as an indicator of whether there is
430 if (len
> PBUFSIZ
- 2)
432 data
= malloc (len
+ 1);
433 n
= (*the_target
->read_auxv
) (ofs
, data
, len
+ 1);
437 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
439 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
446 if (strncmp ("qXfer:features:read:", own_buf
, 20) == 0)
449 unsigned int len
, total_len
;
450 const char *document
;
453 require_running (own_buf
);
455 /* Check for support. */
456 document
= get_features_xml ("target.xml");
457 if (document
== NULL
)
463 /* Grab the annex, offset, and length. */
464 if (decode_xfer_read (own_buf
+ 20, &annex
, &ofs
, &len
) < 0)
466 strcpy (own_buf
, "E00");
470 /* Now grab the correct annex. */
471 document
= get_features_xml (annex
);
472 if (document
== NULL
)
474 strcpy (own_buf
, "E00");
478 total_len
= strlen (document
);
479 if (len
> PBUFSIZ
- 2)
484 else if (len
< total_len
- ofs
)
485 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
488 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
494 if (strncmp ("qXfer:libraries:read:", own_buf
, 21) == 0)
497 unsigned int len
, total_len
;
499 struct inferior_list_entry
*dll_ptr
;
502 require_running (own_buf
);
504 /* Reject any annex; grab the offset and length. */
505 if (decode_xfer_read (own_buf
+ 21, &annex
, &ofs
, &len
) < 0
508 strcpy (own_buf
, "E00");
512 /* Over-estimate the necessary memory. Assume that every character
513 in the library name must be escaped. */
515 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
516 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
518 document
= malloc (total_len
);
519 strcpy (document
, "<library-list>\n");
520 p
= document
+ strlen (document
);
522 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
524 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
527 strcpy (p
, " <library name=\"");
529 name
= xml_escape_text (dll
->name
);
533 strcpy (p
, "\"><segment address=\"");
535 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
537 strcpy (p
, "\"/></library>\n");
541 strcpy (p
, "</library-list>\n");
543 total_len
= strlen (document
);
544 if (len
> PBUFSIZ
- 2)
549 else if (len
< total_len
- ofs
)
550 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
553 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
560 /* Protocol features query. */
561 if (strncmp ("qSupported", own_buf
, 10) == 0
562 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
564 sprintf (own_buf
, "PacketSize=%x;QPassSignals+", PBUFSIZ
- 1);
566 /* We do not have any hook to indicate whether the target backend
567 supports qXfer:libraries:read, so always report it. */
568 strcat (own_buf
, ";qXfer:libraries:read+");
570 if (the_target
->read_auxv
!= NULL
)
571 strcat (own_buf
, ";qXfer:auxv:read+");
573 if (the_target
->qxfer_spu
!= NULL
)
574 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
576 if (get_features_xml ("target.xml") != NULL
)
577 strcat (own_buf
, ";qXfer:features:read+");
582 /* Thread-local storage support. */
583 if (the_target
->get_tls_address
!= NULL
584 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
586 char *p
= own_buf
+ 12;
587 CORE_ADDR parts
[3], address
= 0;
590 require_running (own_buf
);
592 for (i
= 0; i
< 3; i
++)
600 p2
= strchr (p
, ',');
612 decode_address (&parts
[i
], p
, len
);
616 if (p
!= NULL
|| i
< 3)
620 struct thread_info
*thread
= gdb_id_to_thread (parts
[0]);
625 err
= the_target
->get_tls_address (thread
, parts
[1], parts
[2],
631 sprintf (own_buf
, "%llx", address
);
640 /* Otherwise, pretend we do not understand this packet. */
643 /* Handle "monitor" commands. */
644 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
646 char *mon
= malloc (PBUFSIZ
);
647 int len
= strlen (own_buf
+ 6);
649 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
659 if (strcmp (mon
, "set debug 1") == 0)
662 monitor_output ("Debug output enabled.\n");
664 else if (strcmp (mon
, "set debug 0") == 0)
667 monitor_output ("Debug output disabled.\n");
669 else if (strcmp (mon
, "set remote-debug 1") == 0)
672 monitor_output ("Protocol debug output enabled.\n");
674 else if (strcmp (mon
, "set remote-debug 0") == 0)
677 monitor_output ("Protocol debug output disabled.\n");
679 else if (strcmp (mon
, "help") == 0)
680 monitor_show_help ();
681 else if (strcmp (mon
, "exit") == 0)
685 monitor_output ("Unknown monitor command.\n\n");
686 monitor_show_help ();
694 /* Otherwise we didn't know what packet it was. Say we didn't
699 /* Parse vCont packets. */
701 handle_v_cont (char *own_buf
, char *status
, int *signal
)
705 struct thread_resume
*resume_info
, default_action
;
707 /* Count the number of semicolons in the packet. There should be one
716 /* Allocate room for one extra action, for the default remain-stopped
717 behavior; if no default action is in the list, we'll need the extra
719 resume_info
= malloc ((n
+ 1) * sizeof (resume_info
[0]));
721 default_action
.thread
= -1;
722 default_action
.leave_stopped
= 1;
723 default_action
.step
= 0;
724 default_action
.sig
= 0;
732 resume_info
[i
].leave_stopped
= 0;
734 if (p
[0] == 's' || p
[0] == 'S')
735 resume_info
[i
].step
= 1;
736 else if (p
[0] == 'c' || p
[0] == 'C')
737 resume_info
[i
].step
= 0;
741 if (p
[0] == 'S' || p
[0] == 'C')
744 sig
= strtol (p
+ 1, &q
, 16);
749 if (!target_signal_to_host_p (sig
))
751 resume_info
[i
].sig
= target_signal_to_host (sig
);
755 resume_info
[i
].sig
= 0;
761 resume_info
[i
].thread
= -1;
762 default_action
= resume_info
[i
];
764 /* Note: we don't increment i here, we'll overwrite this entry
765 the next time through. */
767 else if (p
[0] == ':')
769 unsigned int gdb_id
= strtoul (p
+ 1, &q
, 16);
770 unsigned long thread_id
;
775 if (p
[0] != ';' && p
[0] != 0)
778 thread_id
= gdb_id_to_thread_id (gdb_id
);
780 resume_info
[i
].thread
= thread_id
;
788 resume_info
[i
] = default_action
;
790 /* Still used in occasional places in the backend. */
791 if (n
== 1 && resume_info
[0].thread
!= -1)
792 cont_thread
= resume_info
[0].thread
;
795 set_desired_inferior (0);
798 (*the_target
->resume
) (resume_info
);
802 *signal
= mywait (status
, 1);
803 prepare_resume_reply (own_buf
, *status
, *signal
);
813 /* Attach to a new program. Return 1 if successful, 0 if failure. */
815 handle_v_attach (char *own_buf
, char *status
, int *signal
)
819 pid
= strtol (own_buf
+ 8, NULL
, 16);
820 if (pid
!= 0 && attach_inferior (pid
, status
, signal
) == 0)
822 prepare_resume_reply (own_buf
, *status
, *signal
);
832 /* Run a new program. Return 1 if successful, 0 if failure. */
834 handle_v_run (char *own_buf
, char *status
, int *signal
)
836 char *p
, **pp
, *next_p
, **new_argv
;
840 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
846 new_argv
= malloc ((new_argc
+ 2) * sizeof (char *));
848 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
850 next_p
= strchr (p
, ';');
852 next_p
= p
+ strlen (p
);
854 if (i
== 0 && p
== next_p
)
858 new_argv
[i
] = malloc (1 + (next_p
- p
) / 2);
859 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
860 new_argv
[i
][(next_p
- p
) / 2] = '\0';
869 if (new_argv
[0] == NULL
)
871 if (program_argv
== NULL
)
877 new_argv
[0] = strdup (program_argv
[0]);
880 /* Free the old argv. */
883 for (pp
= program_argv
; *pp
!= NULL
; pp
++)
887 program_argv
= new_argv
;
889 *signal
= start_inferior (program_argv
, status
);
892 prepare_resume_reply (own_buf
, *status
, *signal
);
902 /* Handle all of the extended 'v' packets. */
904 handle_v_requests (char *own_buf
, char *status
, int *signal
,
905 int packet_len
, int *new_packet_len
)
907 if (strncmp (own_buf
, "vCont;", 6) == 0)
909 require_running (own_buf
);
910 handle_v_cont (own_buf
, status
, signal
);
914 if (strncmp (own_buf
, "vCont?", 6) == 0)
916 strcpy (own_buf
, "vCont;c;C;s;S");
920 if (strncmp (own_buf
, "vFile:", 6) == 0
921 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
924 if (strncmp (own_buf
, "vAttach;", 8) == 0)
926 if (target_running ())
928 fprintf (stderr
, "Already debugging a process\n");
932 handle_v_attach (own_buf
, status
, signal
);
936 if (strncmp (own_buf
, "vRun;", 5) == 0)
938 if (target_running ())
940 fprintf (stderr
, "Already debugging a process\n");
944 handle_v_run (own_buf
, status
, signal
);
948 /* Otherwise we didn't know what packet it was. Say we didn't
955 myresume (char *own_buf
, int step
, int *signalp
, char *statusp
)
957 struct thread_resume resume_info
[2];
961 set_desired_inferior (0);
963 if (step
|| sig
|| (cont_thread
!= 0 && cont_thread
!= -1))
965 resume_info
[0].thread
966 = ((struct inferior_list_entry
*) current_inferior
)->id
;
967 resume_info
[0].step
= step
;
968 resume_info
[0].sig
= sig
;
969 resume_info
[0].leave_stopped
= 0;
972 resume_info
[n
].thread
= -1;
973 resume_info
[n
].step
= 0;
974 resume_info
[n
].sig
= 0;
975 resume_info
[n
].leave_stopped
= (cont_thread
!= 0 && cont_thread
!= -1);
978 (*the_target
->resume
) (resume_info
);
979 *signalp
= mywait (statusp
, 1);
980 prepare_resume_reply (own_buf
, *statusp
, *signalp
);
985 gdbserver_version (void)
987 printf ("GNU gdbserver %s\n"
988 "Copyright (C) 2007 Free Software Foundation, Inc.\n"
989 "gdbserver is free software, covered by the GNU General Public License.\n"
990 "This gdbserver was configured as \"%s\"\n",
995 gdbserver_usage (void)
997 printf ("Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
998 "\tgdbserver [OPTIONS] --attach COMM PID\n"
999 "\tgdbserver [OPTIONS] --multi COMM\n"
1001 "COMM may either be a tty device (for serial debugging), or \n"
1002 "HOST:PORT to listen for a TCP connection.\n"
1005 " --debug\t\tEnable debugging output.\n");
1008 #undef require_running
1009 #define require_running(BUF) \
1010 if (!target_running ()) \
1017 main (int argc
, char *argv
[])
1019 char ch
, status
, *own_buf
;
1020 unsigned char *mem_buf
;
1027 char *arg_end
, *port
;
1028 char **next_arg
= &argv
[1];
1033 while (*next_arg
!= NULL
&& **next_arg
== '-')
1035 if (strcmp (*next_arg
, "--version") == 0)
1037 gdbserver_version ();
1040 else if (strcmp (*next_arg
, "--help") == 0)
1045 else if (strcmp (*next_arg
, "--attach") == 0)
1047 else if (strcmp (*next_arg
, "--multi") == 0)
1049 else if (strcmp (*next_arg
, "--debug") == 0)
1053 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
1061 if (setjmp (toplevel
))
1063 fprintf (stderr
, "Exiting\n");
1069 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
1078 /* --attach used to come after PORT, so allow it there for
1080 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
1087 && (*next_arg
== NULL
1088 || (*next_arg
)[0] == '\0'
1089 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
1091 || next_arg
[1] != NULL
))
1100 initialize_async_io ();
1103 own_buf
= malloc (PBUFSIZ
+ 1);
1104 mem_buf
= malloc (PBUFSIZ
);
1106 if (pid
== 0 && *next_arg
!= NULL
)
1110 n
= argc
- (next_arg
- argv
);
1111 program_argv
= malloc (sizeof (char *) * (n
+ 1));
1112 for (i
= 0; i
< n
; i
++)
1113 program_argv
[i
] = strdup (next_arg
[i
]);
1114 program_argv
[i
] = NULL
;
1116 /* Wait till we are at first instruction in program. */
1117 signal
= start_inferior (program_argv
, &status
);
1119 /* We are now (hopefully) stopped at the first instruction of
1120 the target process. This assumes that the target process was
1121 successfully created. */
1125 if (attach_inferior (pid
, &status
, &signal
) == -1)
1126 error ("Attaching not supported on this target");
1128 /* Otherwise succeeded. */
1136 /* Don't report shared library events on the initial connection,
1137 even if some libraries are preloaded. Avoids the "stopped by
1138 shared library event" notice on gdb side. */
1141 if (setjmp (toplevel
))
1143 fprintf (stderr
, "Killing inferior\n");
1148 if (status
== 'W' || status
== 'X')
1153 if (!was_running
&& !multi_mode
)
1155 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
1164 if (setjmp (toplevel
) != 0)
1166 /* An error occurred. */
1167 if (response_needed
)
1169 write_enn (own_buf
);
1174 disable_async_io ();
1175 while (!exit_requested
)
1179 int new_packet_len
= -1;
1181 response_needed
= 0;
1182 packet_len
= getpkt (own_buf
);
1183 if (packet_len
<= 0)
1185 response_needed
= 1;
1192 handle_query (own_buf
, packet_len
, &new_packet_len
);
1195 handle_general_set (own_buf
);
1198 require_running (own_buf
);
1199 fprintf (stderr
, "Detaching from inferior\n");
1200 if (detach_inferior () != 0)
1201 write_enn (own_buf
);
1206 if (extended_protocol
)
1208 /* Treat this like a normal program exit. */
1217 /* If we are attached, then we can exit. Otherwise, we
1218 need to hang around doing nothing, until the child
1228 extended_protocol
= 1;
1232 prepare_resume_reply (own_buf
, status
, signal
);
1235 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
1237 unsigned long gdb_id
, thread_id
;
1239 require_running (own_buf
);
1240 gdb_id
= strtoul (&own_buf
[2], NULL
, 16);
1241 if (gdb_id
== 0 || gdb_id
== -1)
1245 thread_id
= gdb_id_to_thread_id (gdb_id
);
1248 write_enn (own_buf
);
1253 if (own_buf
[1] == 'g')
1255 general_thread
= thread_id
;
1256 set_desired_inferior (1);
1258 else if (own_buf
[1] == 'c')
1259 cont_thread
= thread_id
;
1260 else if (own_buf
[1] == 's')
1261 step_thread
= thread_id
;
1267 /* Silently ignore it so that gdb can extend the protocol
1268 without compatibility headaches. */
1273 require_running (own_buf
);
1274 set_desired_inferior (1);
1275 registers_to_string (own_buf
);
1278 require_running (own_buf
);
1279 set_desired_inferior (1);
1280 registers_from_string (&own_buf
[1]);
1284 require_running (own_buf
);
1285 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
1286 if (read_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
1287 convert_int_to_ascii (mem_buf
, own_buf
, len
);
1289 write_enn (own_buf
);
1292 require_running (own_buf
);
1293 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
1294 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
1297 write_enn (own_buf
);
1300 require_running (own_buf
);
1301 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
1302 &mem_addr
, &len
, mem_buf
) < 0
1303 || write_inferior_memory (mem_addr
, mem_buf
, len
) != 0)
1304 write_enn (own_buf
);
1309 require_running (own_buf
);
1310 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
1311 if (target_signal_to_host_p (sig
))
1312 signal
= target_signal_to_host (sig
);
1315 myresume (own_buf
, 0, &signal
, &status
);
1318 require_running (own_buf
);
1319 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
1320 if (target_signal_to_host_p (sig
))
1321 signal
= target_signal_to_host (sig
);
1324 myresume (own_buf
, 1, &signal
, &status
);
1327 require_running (own_buf
);
1329 myresume (own_buf
, 0, &signal
, &status
);
1332 require_running (own_buf
);
1334 myresume (own_buf
, 1, &signal
, &status
);
1340 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
1341 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
1342 char type
= own_buf
[1];
1344 if (the_target
->insert_watchpoint
== NULL
1345 || (type
< '2' || type
> '4'))
1347 /* No watchpoint support or not a watchpoint command;
1348 unrecognized either way. */
1355 require_running (own_buf
);
1356 res
= (*the_target
->insert_watchpoint
) (type
, addr
, len
);
1363 write_enn (own_buf
);
1371 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
1372 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
1373 char type
= own_buf
[1];
1375 if (the_target
->remove_watchpoint
== NULL
1376 || (type
< '2' || type
> '4'))
1378 /* No watchpoint support or not a watchpoint command;
1379 unrecognized either way. */
1386 require_running (own_buf
);
1387 res
= (*the_target
->remove_watchpoint
) (type
, addr
, len
);
1394 write_enn (own_buf
);
1399 response_needed
= 0;
1400 if (!target_running ())
1401 /* The packet we received doesn't make sense - but we
1402 can't reply to it, either. */
1405 fprintf (stderr
, "Killing inferior\n");
1408 /* When using the extended protocol, we wait with no
1409 program running. The traditional protocol will exit
1411 if (extended_protocol
)
1414 signal
= TARGET_SIGNAL_KILL
;
1425 unsigned long gdb_id
, thread_id
;
1427 require_running (own_buf
);
1428 gdb_id
= strtoul (&own_buf
[1], NULL
, 16);
1429 thread_id
= gdb_id_to_thread_id (gdb_id
);
1432 write_enn (own_buf
);
1436 if (mythread_alive (thread_id
))
1439 write_enn (own_buf
);
1443 response_needed
= 0;
1445 /* Restarting the inferior is only supported in the
1446 extended protocol. */
1447 if (extended_protocol
)
1449 if (target_running ())
1451 fprintf (stderr
, "GDBserver restarting\n");
1453 /* Wait till we are at 1st instruction in prog. */
1454 if (program_argv
!= NULL
)
1455 signal
= start_inferior (program_argv
, &status
);
1459 signal
= TARGET_SIGNAL_KILL
;
1465 /* It is a request we don't understand. Respond with an
1466 empty packet so that gdb knows that we don't support this
1472 /* Extended (long) request. */
1473 handle_v_requests (own_buf
, &status
, &signal
,
1474 packet_len
, &new_packet_len
);
1478 /* It is a request we don't understand. Respond with an
1479 empty packet so that gdb knows that we don't support this
1485 if (new_packet_len
!= -1)
1486 putpkt_binary (own_buf
, new_packet_len
);
1490 response_needed
= 0;
1492 if (was_running
&& (status
== 'W' || status
== 'X'))
1498 "\nChild exited with status %d\n", signal
);
1500 fprintf (stderr
, "\nChild terminated with signal = 0x%x (%s)\n",
1501 target_signal_to_host (signal
),
1502 target_signal_to_name (signal
));
1504 if (extended_protocol
)
1508 fprintf (stderr
, "GDBserver exiting\n");
1513 if (status
!= 'W' && status
!= 'X')
1517 /* If an exit was requested (using the "monitor exit" command),
1518 terminate now. The only other way to get here is for
1519 getpkt to fail; close the connection and reopen it at the
1525 if (attached
&& target_running ())
1527 else if (target_running ())
1533 fprintf (stderr
, "Remote side has terminated connection. "
1534 "GDBserver will reopen the connection.\n");