]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbserver/server.c
Don't check assertions until symbols are finalized. Create an output
[thirdparty/binutils-gdb.git] / gdb / gdbserver / server.c
CommitLineData
c906108c 1/* Main code for remote server for GDB.
6aba47ca 2 Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003,
9b254dd1 3 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "server.h"
21
68070c10 22#if HAVE_UNISTD_H
a9fa9f7d 23#include <unistd.h>
68070c10
PA
24#endif
25#if HAVE_SIGNAL_H
a9fa9f7d 26#include <signal.h>
68070c10 27#endif
b80864fb 28#if HAVE_SYS_WAIT_H
a9fa9f7d 29#include <sys/wait.h>
b80864fb 30#endif
a9fa9f7d 31
a1928bad
DJ
32unsigned long cont_thread;
33unsigned long general_thread;
34unsigned long step_thread;
35unsigned long thread_from_wait;
36unsigned long old_thread_from_wait;
0d62e5e8
DJ
37int server_waiting;
38
2d717e4f
DJ
39static int extended_protocol;
40static int attached;
41static int response_needed;
42static int exit_requested;
43
44static char **program_argv;
45
c74d0ad8
DJ
46/* Enable miscellaneous debugging output. The name is historical - it
47 was originally used to debug LinuxThreads support. */
48int debug_threads;
49
89be2091
DJ
50int pass_signals[TARGET_SIGNAL_LAST];
51
c906108c 52jmp_buf toplevel;
c906108c 53
a9fa9f7d
DJ
54/* The PID of the originally created or attached inferior. Used to
55 send signals to the process when GDB sends us an asynchronous interrupt
56 (user hitting Control-C in the client), and to wait for the child to exit
57 when no longer debugging it. */
58
a1928bad 59unsigned long signal_pid;
a9fa9f7d 60
290fadea
RS
61#ifdef SIGTTOU
62/* A file descriptor for the controlling terminal. */
63int terminal_fd;
64
65/* TERMINAL_FD's original foreground group. */
66pid_t old_foreground_pgrp;
67
68/* Hand back terminal ownership to the original foreground group. */
69
70static void
71restore_old_foreground_pgrp (void)
72{
73 tcsetpgrp (terminal_fd, old_foreground_pgrp);
74}
75#endif
76
2d717e4f
DJ
77static int
78target_running (void)
79{
80 return all_threads.head != NULL;
81}
82
fc620387 83static int
da85418c 84start_inferior (char *argv[], char *statusptr)
c906108c 85{
2d717e4f
DJ
86 attached = 0;
87
b80864fb 88#ifdef SIGTTOU
a9fa9f7d
DJ
89 signal (SIGTTOU, SIG_DFL);
90 signal (SIGTTIN, SIG_DFL);
b80864fb 91#endif
a9fa9f7d
DJ
92
93 signal_pid = create_inferior (argv[0], argv);
0d62e5e8 94
c588c53c
MS
95 /* FIXME: we don't actually know at this point that the create
96 actually succeeded. We won't know that until we wait. */
a1928bad 97 fprintf (stderr, "Process %s created; pid = %ld\n", argv[0],
a9fa9f7d 98 signal_pid);
b80864fb 99 fflush (stderr);
a9fa9f7d 100
b80864fb 101#ifdef SIGTTOU
a9fa9f7d
DJ
102 signal (SIGTTOU, SIG_IGN);
103 signal (SIGTTIN, SIG_IGN);
290fadea
RS
104 terminal_fd = fileno (stderr);
105 old_foreground_pgrp = tcgetpgrp (terminal_fd);
106 tcsetpgrp (terminal_fd, signal_pid);
107 atexit (restore_old_foreground_pgrp);
b80864fb 108#endif
c906108c 109
c588c53c
MS
110 /* Wait till we are at 1st instruction in program, return signal
111 number (assuming success). */
0d62e5e8 112 return mywait (statusptr, 0);
c906108c
SS
113}
114
45b7b345 115static int
fc620387 116attach_inferior (int pid, char *statusptr, int *sigptr)
45b7b345
DJ
117{
118 /* myattach should return -1 if attaching is unsupported,
119 0 if it succeeded, and call error() otherwise. */
a9fa9f7d 120
45b7b345
DJ
121 if (myattach (pid) != 0)
122 return -1;
123
2d717e4f
DJ
124 attached = 1;
125
6910d122 126 fprintf (stderr, "Attached; pid = %d\n", pid);
b80864fb 127 fflush (stderr);
6910d122 128
a9fa9f7d
DJ
129 /* FIXME - It may be that we should get the SIGNAL_PID from the
130 attach function, so that it can be the main thread instead of
131 whichever we were told to attach to. */
132 signal_pid = pid;
133
0d62e5e8 134 *sigptr = mywait (statusptr, 0);
45b7b345 135
9db87ebd
DJ
136 /* GDB knows to ignore the first SIGSTOP after attaching to a running
137 process using the "attach" command, but this is different; it's
138 just using "target remote". Pretend it's just starting up. */
b80864fb
DJ
139 if (*statusptr == 'T' && *sigptr == TARGET_SIGNAL_STOP)
140 *sigptr = TARGET_SIGNAL_TRAP;
9db87ebd 141
45b7b345
DJ
142 return 0;
143}
144
c906108c 145extern int remote_debug;
ce3a066d 146
0876f84a
DJ
147/* Decode a qXfer read request. Return 0 if everything looks OK,
148 or -1 otherwise. */
149
150static int
151decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len)
152{
153 /* Extract and NUL-terminate the annex. */
154 *annex = buf;
155 while (*buf && *buf != ':')
156 buf++;
157 if (*buf == '\0')
158 return -1;
159 *buf++ = 0;
160
0e7f50da 161 /* After the read marker and annex, qXfer looks like a
0876f84a
DJ
162 traditional 'm' packet. */
163 decode_m_packet (buf, ofs, len);
164
165 return 0;
166}
167
168/* Write the response to a successful qXfer read. Returns the
169 length of the (binary) data stored in BUF, corresponding
170 to as much of DATA/LEN as we could fit. IS_MORE controls
171 the first character of the response. */
172static int
23181151 173write_qxfer_response (char *buf, const void *data, int len, int is_more)
0876f84a
DJ
174{
175 int out_len;
176
177 if (is_more)
178 buf[0] = 'm';
179 else
180 buf[0] = 'l';
181
182 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
183 PBUFSIZ - 2) + 1;
184}
185
89be2091
DJ
186/* Handle all of the extended 'Q' packets. */
187void
188handle_general_set (char *own_buf)
189{
190 if (strncmp ("QPassSignals:", own_buf, strlen ("QPassSignals:")) == 0)
191 {
192 int numsigs = (int) TARGET_SIGNAL_LAST, i;
193 const char *p = own_buf + strlen ("QPassSignals:");
194 CORE_ADDR cursig;
195
196 p = decode_address_to_semicolon (&cursig, p);
197 for (i = 0; i < numsigs; i++)
198 {
199 if (i == cursig)
200 {
201 pass_signals[i] = 1;
202 if (*p == '\0')
203 /* Keep looping, to clear the remaining signals. */
204 cursig = -1;
205 else
206 p = decode_address_to_semicolon (&cursig, p);
207 }
208 else
209 pass_signals[i] = 0;
210 }
211 strcpy (own_buf, "OK");
212 return;
213 }
214
215 /* Otherwise we didn't know what packet it was. Say we didn't
216 understand it. */
217 own_buf[0] = 0;
218}
219
23181151 220static const char *
fb1e4ffc 221get_features_xml (const char *annex)
23181151
DJ
222{
223 static int features_supported = -1;
224 static char *document;
225
fb1e4ffc
DJ
226#ifdef USE_XML
227 extern const char *const xml_builtin[][2];
228 int i;
229
230 /* Look for the annex. */
231 for (i = 0; xml_builtin[i][0] != NULL; i++)
232 if (strcmp (annex, xml_builtin[i][0]) == 0)
233 break;
234
235 if (xml_builtin[i][0] != NULL)
236 return xml_builtin[i][1];
237#endif
238
239 if (strcmp (annex, "target.xml") != 0)
240 return NULL;
241
23181151
DJ
242 if (features_supported == -1)
243 {
820f2bda
PA
244 const char *arch = NULL;
245 if (the_target->arch_string != NULL)
246 arch = (*the_target->arch_string) ();
23181151
DJ
247
248 if (arch == NULL)
249 features_supported = 0;
250 else
251 {
252 features_supported = 1;
253 document = malloc (64 + strlen (arch));
254 snprintf (document, 64 + strlen (arch),
255 "<target><architecture>%s</architecture></target>",
256 arch);
257 }
258 }
259
260 return document;
261}
262
c74d0ad8
DJ
263void
264monitor_show_help (void)
265{
266 monitor_output ("The following monitor commands are supported:\n");
267 monitor_output (" set debug <0|1>\n");
268 monitor_output (" Enable general debugging messages\n");
269 monitor_output (" set remote-debug <0|1>\n");
270 monitor_output (" Enable remote protocol debugging messages\n");
ecd7ecbc
DJ
271 monitor_output (" exit\n");
272 monitor_output (" Quit GDBserver\n");
c74d0ad8
DJ
273}
274
2d717e4f
DJ
275#define require_running(BUF) \
276 if (!target_running ()) \
277 { \
278 write_enn (BUF); \
279 return; \
280 }
281
ce3a066d
DJ
282/* Handle all of the extended 'q' packets. */
283void
0e7f50da 284handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
ce3a066d 285{
0d62e5e8
DJ
286 static struct inferior_list_entry *thread_ptr;
287
bb63802a
UW
288 /* Reply the current thread id. */
289 if (strcmp ("qC", own_buf) == 0)
290 {
2d717e4f 291 require_running (own_buf);
bb63802a
UW
292 thread_ptr = all_threads.head;
293 sprintf (own_buf, "QC%x",
294 thread_to_gdb_id ((struct thread_info *)thread_ptr));
295 return;
296 }
297
ce3a066d
DJ
298 if (strcmp ("qSymbol::", own_buf) == 0)
299 {
2d717e4f 300 if (target_running () && the_target->look_up_symbols != NULL)
2f2893d9
DJ
301 (*the_target->look_up_symbols) ();
302
ce3a066d
DJ
303 strcpy (own_buf, "OK");
304 return;
305 }
306
0d62e5e8
DJ
307 if (strcmp ("qfThreadInfo", own_buf) == 0)
308 {
2d717e4f 309 require_running (own_buf);
0d62e5e8 310 thread_ptr = all_threads.head;
a06660f7 311 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
0d62e5e8
DJ
312 thread_ptr = thread_ptr->next;
313 return;
314 }
aa691b87 315
0d62e5e8
DJ
316 if (strcmp ("qsThreadInfo", own_buf) == 0)
317 {
2d717e4f 318 require_running (own_buf);
0d62e5e8
DJ
319 if (thread_ptr != NULL)
320 {
a06660f7 321 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
0d62e5e8
DJ
322 thread_ptr = thread_ptr->next;
323 return;
324 }
325 else
326 {
327 sprintf (own_buf, "l");
328 return;
329 }
330 }
aa691b87 331
52fb6437
NS
332 if (the_target->read_offsets != NULL
333 && strcmp ("qOffsets", own_buf) == 0)
334 {
335 CORE_ADDR text, data;
2d717e4f
DJ
336
337 require_running (own_buf);
52fb6437
NS
338 if (the_target->read_offsets (&text, &data))
339 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
340 (long)text, (long)data, (long)data);
341 else
342 write_enn (own_buf);
343
344 return;
345 }
346
0e7f50da
UW
347 if (the_target->qxfer_spu != NULL
348 && strncmp ("qXfer:spu:read:", own_buf, 15) == 0)
349 {
350 char *annex;
351 int n;
352 unsigned int len;
353 CORE_ADDR ofs;
354 unsigned char *spu_buf;
355
2d717e4f 356 require_running (own_buf);
0e7f50da
UW
357 strcpy (own_buf, "E00");
358 if (decode_xfer_read (own_buf + 15, &annex, &ofs, &len) < 0)
359 return;
360 if (len > PBUFSIZ - 2)
361 len = PBUFSIZ - 2;
362 spu_buf = malloc (len + 1);
363 if (!spu_buf)
364 return;
365
366 n = (*the_target->qxfer_spu) (annex, spu_buf, NULL, ofs, len + 1);
367 if (n < 0)
368 write_enn (own_buf);
369 else if (n > len)
370 *new_packet_len_p = write_qxfer_response
371 (own_buf, spu_buf, len, 1);
372 else
373 *new_packet_len_p = write_qxfer_response
374 (own_buf, spu_buf, n, 0);
375
376 free (spu_buf);
377 return;
378 }
379
380 if (the_target->qxfer_spu != NULL
381 && strncmp ("qXfer:spu:write:", own_buf, 16) == 0)
382 {
383 char *annex;
384 int n;
385 unsigned int len;
386 CORE_ADDR ofs;
387 unsigned char *spu_buf;
388
2d717e4f 389 require_running (own_buf);
0e7f50da
UW
390 strcpy (own_buf, "E00");
391 spu_buf = malloc (packet_len - 15);
392 if (!spu_buf)
393 return;
394 if (decode_xfer_write (own_buf + 16, packet_len - 16, &annex,
395 &ofs, &len, spu_buf) < 0)
396 {
397 free (spu_buf);
398 return;
399 }
400
401 n = (*the_target->qxfer_spu)
402 (annex, NULL, (unsigned const char *)spu_buf, ofs, len);
403 if (n < 0)
404 write_enn (own_buf);
405 else
406 sprintf (own_buf, "%x", n);
407
408 free (spu_buf);
409 return;
410 }
411
aa691b87 412 if (the_target->read_auxv != NULL
0876f84a 413 && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0)
aa691b87 414 {
0876f84a
DJ
415 unsigned char *data;
416 int n;
aa691b87
RM
417 CORE_ADDR ofs;
418 unsigned int len;
0876f84a
DJ
419 char *annex;
420
2d717e4f
DJ
421 require_running (own_buf);
422
0876f84a
DJ
423 /* Reject any annex; grab the offset and length. */
424 if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0
425 || annex[0] != '\0')
426 {
427 strcpy (own_buf, "E00");
428 return;
429 }
430
431 /* Read one extra byte, as an indicator of whether there is
432 more. */
433 if (len > PBUFSIZ - 2)
434 len = PBUFSIZ - 2;
435 data = malloc (len + 1);
436 n = (*the_target->read_auxv) (ofs, data, len + 1);
000ef4f0
DJ
437 if (n < 0)
438 write_enn (own_buf);
439 else if (n > len)
0876f84a 440 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
aa691b87 441 else
0876f84a
DJ
442 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
443
444 free (data);
445
aa691b87
RM
446 return;
447 }
448
23181151
DJ
449 if (strncmp ("qXfer:features:read:", own_buf, 20) == 0)
450 {
451 CORE_ADDR ofs;
452 unsigned int len, total_len;
453 const char *document;
454 char *annex;
455
2d717e4f
DJ
456 require_running (own_buf);
457
fb1e4ffc
DJ
458 /* Check for support. */
459 document = get_features_xml ("target.xml");
23181151
DJ
460 if (document == NULL)
461 {
462 own_buf[0] = '\0';
463 return;
464 }
465
fb1e4ffc
DJ
466 /* Grab the annex, offset, and length. */
467 if (decode_xfer_read (own_buf + 20, &annex, &ofs, &len) < 0)
468 {
469 strcpy (own_buf, "E00");
470 return;
471 }
472
473 /* Now grab the correct annex. */
474 document = get_features_xml (annex);
475 if (document == NULL)
23181151
DJ
476 {
477 strcpy (own_buf, "E00");
478 return;
479 }
480
481 total_len = strlen (document);
482 if (len > PBUFSIZ - 2)
483 len = PBUFSIZ - 2;
484
485 if (ofs > total_len)
486 write_enn (own_buf);
487 else if (len < total_len - ofs)
488 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
489 len, 1);
490 else
491 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
492 total_len - ofs, 0);
493
494 return;
495 }
496
255e7678
DJ
497 if (strncmp ("qXfer:libraries:read:", own_buf, 21) == 0)
498 {
499 CORE_ADDR ofs;
500 unsigned int len, total_len;
501 char *document, *p;
502 struct inferior_list_entry *dll_ptr;
503 char *annex;
504
2d717e4f
DJ
505 require_running (own_buf);
506
255e7678
DJ
507 /* Reject any annex; grab the offset and length. */
508 if (decode_xfer_read (own_buf + 21, &annex, &ofs, &len) < 0
509 || annex[0] != '\0')
510 {
511 strcpy (own_buf, "E00");
512 return;
513 }
514
515 /* Over-estimate the necessary memory. Assume that every character
516 in the library name must be escaped. */
517 total_len = 64;
518 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
519 total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name);
520
521 document = malloc (total_len);
522 strcpy (document, "<library-list>\n");
523 p = document + strlen (document);
524
525 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
526 {
527 struct dll_info *dll = (struct dll_info *) dll_ptr;
528 char *name;
529
530 strcpy (p, " <library name=\"");
531 p = p + strlen (p);
532 name = xml_escape_text (dll->name);
533 strcpy (p, name);
534 free (name);
535 p = p + strlen (p);
536 strcpy (p, "\"><segment address=\"");
537 p = p + strlen (p);
538 sprintf (p, "0x%lx", (long) dll->base_addr);
539 p = p + strlen (p);
540 strcpy (p, "\"/></library>\n");
541 p = p + strlen (p);
542 }
543
544 strcpy (p, "</library-list>\n");
545
546 total_len = strlen (document);
547 if (len > PBUFSIZ - 2)
548 len = PBUFSIZ - 2;
549
550 if (ofs > total_len)
551 write_enn (own_buf);
552 else if (len < total_len - ofs)
553 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
554 len, 1);
555 else
556 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
557 total_len - ofs, 0);
558
559 free (document);
560 return;
561 }
562
be2a5f71
DJ
563 /* Protocol features query. */
564 if (strncmp ("qSupported", own_buf, 10) == 0
565 && (own_buf[10] == ':' || own_buf[10] == '\0'))
566 {
89be2091 567 sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1);
0876f84a 568
255e7678
DJ
569 /* We do not have any hook to indicate whether the target backend
570 supports qXfer:libraries:read, so always report it. */
571 strcat (own_buf, ";qXfer:libraries:read+");
572
0876f84a 573 if (the_target->read_auxv != NULL)
9f2e1e63 574 strcat (own_buf, ";qXfer:auxv:read+");
2d717e4f 575
0e7f50da
UW
576 if (the_target->qxfer_spu != NULL)
577 strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+");
0876f84a 578
fb1e4ffc 579 if (get_features_xml ("target.xml") != NULL)
23181151
DJ
580 strcat (own_buf, ";qXfer:features:read+");
581
be2a5f71
DJ
582 return;
583 }
584
dae5f5cf
DJ
585 /* Thread-local storage support. */
586 if (the_target->get_tls_address != NULL
587 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
588 {
589 char *p = own_buf + 12;
590 CORE_ADDR parts[3], address = 0;
591 int i, err;
592
2d717e4f
DJ
593 require_running (own_buf);
594
dae5f5cf
DJ
595 for (i = 0; i < 3; i++)
596 {
597 char *p2;
598 int len;
599
600 if (p == NULL)
601 break;
602
603 p2 = strchr (p, ',');
604 if (p2)
605 {
606 len = p2 - p;
607 p2++;
608 }
609 else
610 {
611 len = strlen (p);
612 p2 = NULL;
613 }
614
615 decode_address (&parts[i], p, len);
616 p = p2;
617 }
618
619 if (p != NULL || i < 3)
620 err = 1;
621 else
622 {
623 struct thread_info *thread = gdb_id_to_thread (parts[0]);
624
625 if (thread == NULL)
626 err = 2;
627 else
628 err = the_target->get_tls_address (thread, parts[1], parts[2],
629 &address);
630 }
631
632 if (err == 0)
633 {
634 sprintf (own_buf, "%llx", address);
635 return;
636 }
637 else if (err > 0)
638 {
639 write_enn (own_buf);
640 return;
641 }
642
643 /* Otherwise, pretend we do not understand this packet. */
644 }
645
c74d0ad8
DJ
646 /* Handle "monitor" commands. */
647 if (strncmp ("qRcmd,", own_buf, 6) == 0)
648 {
649 char *mon = malloc (PBUFSIZ);
650 int len = strlen (own_buf + 6);
651
d41b6bb4 652 if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
c74d0ad8
DJ
653 {
654 write_enn (own_buf);
655 free (mon);
656 return;
657 }
658 mon[len / 2] = '\0';
659
660 write_ok (own_buf);
661
662 if (strcmp (mon, "set debug 1") == 0)
663 {
664 debug_threads = 1;
665 monitor_output ("Debug output enabled.\n");
666 }
667 else if (strcmp (mon, "set debug 0") == 0)
668 {
669 debug_threads = 0;
670 monitor_output ("Debug output disabled.\n");
671 }
672 else if (strcmp (mon, "set remote-debug 1") == 0)
673 {
674 remote_debug = 1;
675 monitor_output ("Protocol debug output enabled.\n");
676 }
677 else if (strcmp (mon, "set remote-debug 0") == 0)
678 {
679 remote_debug = 0;
680 monitor_output ("Protocol debug output disabled.\n");
681 }
682 else if (strcmp (mon, "help") == 0)
683 monitor_show_help ();
2d717e4f
DJ
684 else if (strcmp (mon, "exit") == 0)
685 exit_requested = 1;
c74d0ad8
DJ
686 else
687 {
688 monitor_output ("Unknown monitor command.\n\n");
689 monitor_show_help ();
690 write_enn (own_buf);
691 }
692
693 free (mon);
694 return;
695 }
696
ce3a066d
DJ
697 /* Otherwise we didn't know what packet it was. Say we didn't
698 understand it. */
699 own_buf[0] = 0;
700}
701
64386c31
DJ
702/* Parse vCont packets. */
703void
fc620387 704handle_v_cont (char *own_buf, char *status, int *signal)
64386c31
DJ
705{
706 char *p, *q;
707 int n = 0, i = 0;
708 struct thread_resume *resume_info, default_action;
709
710 /* Count the number of semicolons in the packet. There should be one
711 for every action. */
712 p = &own_buf[5];
713 while (p)
714 {
715 n++;
716 p++;
717 p = strchr (p, ';');
718 }
719 /* Allocate room for one extra action, for the default remain-stopped
720 behavior; if no default action is in the list, we'll need the extra
721 slot. */
722 resume_info = malloc ((n + 1) * sizeof (resume_info[0]));
723
724 default_action.thread = -1;
725 default_action.leave_stopped = 1;
726 default_action.step = 0;
727 default_action.sig = 0;
728
729 p = &own_buf[5];
730 i = 0;
731 while (*p)
732 {
733 p++;
734
735 resume_info[i].leave_stopped = 0;
736
737 if (p[0] == 's' || p[0] == 'S')
738 resume_info[i].step = 1;
739 else if (p[0] == 'c' || p[0] == 'C')
740 resume_info[i].step = 0;
741 else
742 goto err;
743
744 if (p[0] == 'S' || p[0] == 'C')
745 {
746 int sig;
747 sig = strtol (p + 1, &q, 16);
748 if (p == q)
749 goto err;
750 p = q;
751
752 if (!target_signal_to_host_p (sig))
753 goto err;
754 resume_info[i].sig = target_signal_to_host (sig);
755 }
756 else
757 {
758 resume_info[i].sig = 0;
759 p = p + 1;
760 }
761
762 if (p[0] == 0)
763 {
764 resume_info[i].thread = -1;
765 default_action = resume_info[i];
766
767 /* Note: we don't increment i here, we'll overwrite this entry
768 the next time through. */
769 }
770 else if (p[0] == ':')
771 {
a06660f7
DJ
772 unsigned int gdb_id = strtoul (p + 1, &q, 16);
773 unsigned long thread_id;
774
64386c31
DJ
775 if (p == q)
776 goto err;
777 p = q;
778 if (p[0] != ';' && p[0] != 0)
779 goto err;
780
a06660f7
DJ
781 thread_id = gdb_id_to_thread_id (gdb_id);
782 if (thread_id)
783 resume_info[i].thread = thread_id;
784 else
785 goto err;
786
64386c31
DJ
787 i++;
788 }
789 }
790
791 resume_info[i] = default_action;
792
793 /* Still used in occasional places in the backend. */
794 if (n == 1 && resume_info[0].thread != -1)
795 cont_thread = resume_info[0].thread;
796 else
797 cont_thread = -1;
dc3f8883 798 set_desired_inferior (0);
64386c31 799
a20d5e98 800 enable_async_io ();
64386c31
DJ
801 (*the_target->resume) (resume_info);
802
803 free (resume_info);
804
805 *signal = mywait (status, 1);
806 prepare_resume_reply (own_buf, *status, *signal);
a20d5e98 807 disable_async_io ();
64386c31
DJ
808 return;
809
810err:
255e7678 811 write_enn (own_buf);
64386c31
DJ
812 free (resume_info);
813 return;
814}
815
2d717e4f
DJ
816/* Attach to a new program. Return 1 if successful, 0 if failure. */
817int
818handle_v_attach (char *own_buf, char *status, int *signal)
819{
820 int pid;
821
822 pid = strtol (own_buf + 8, NULL, 16);
823 if (pid != 0 && attach_inferior (pid, status, signal) == 0)
824 {
825 prepare_resume_reply (own_buf, *status, *signal);
826 return 1;
827 }
828 else
829 {
830 write_enn (own_buf);
831 return 0;
832 }
833}
834
835/* Run a new program. Return 1 if successful, 0 if failure. */
836static int
837handle_v_run (char *own_buf, char *status, int *signal)
838{
839 char *p, **pp, *next_p, **new_argv;
840 int i, new_argc;
841
842 new_argc = 0;
843 for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';'))
844 {
845 p++;
846 new_argc++;
847 }
848
849 new_argv = malloc ((new_argc + 2) * sizeof (char *));
850 i = 0;
851 for (p = own_buf + strlen ("vRun;"); *p; p = next_p)
852 {
853 next_p = strchr (p, ';');
854 if (next_p == NULL)
855 next_p = p + strlen (p);
856
857 if (i == 0 && p == next_p)
858 new_argv[i] = NULL;
859 else
860 {
861 new_argv[i] = malloc (1 + (next_p - p) / 2);
862 unhexify (new_argv[i], p, (next_p - p) / 2);
863 new_argv[i][(next_p - p) / 2] = '\0';
864 }
865
866 if (*next_p)
867 next_p++;
868 i++;
869 }
870 new_argv[i] = NULL;
871
872 if (new_argv[0] == NULL)
873 {
874 if (program_argv == NULL)
875 {
876 write_enn (own_buf);
877 return 0;
878 }
879
880 new_argv[0] = strdup (program_argv[0]);
881 }
882
883 /* Free the old argv. */
884 if (program_argv)
885 {
886 for (pp = program_argv; *pp != NULL; pp++)
887 free (*pp);
888 free (program_argv);
889 }
890 program_argv = new_argv;
891
892 *signal = start_inferior (program_argv, status);
893 if (*status == 'T')
894 {
895 prepare_resume_reply (own_buf, *status, *signal);
896 return 1;
897 }
898 else
899 {
900 write_enn (own_buf);
901 return 0;
902 }
903}
904
64386c31
DJ
905/* Handle all of the extended 'v' packets. */
906void
a6b151f1
DJ
907handle_v_requests (char *own_buf, char *status, int *signal,
908 int packet_len, int *new_packet_len)
64386c31
DJ
909{
910 if (strncmp (own_buf, "vCont;", 6) == 0)
911 {
2d717e4f 912 require_running (own_buf);
64386c31
DJ
913 handle_v_cont (own_buf, status, signal);
914 return;
915 }
916
917 if (strncmp (own_buf, "vCont?", 6) == 0)
918 {
919 strcpy (own_buf, "vCont;c;C;s;S");
920 return;
921 }
922
a6b151f1
DJ
923 if (strncmp (own_buf, "vFile:", 6) == 0
924 && handle_vFile (own_buf, packet_len, new_packet_len))
925 return;
926
2d717e4f
DJ
927 if (strncmp (own_buf, "vAttach;", 8) == 0)
928 {
929 if (target_running ())
930 {
fd96d250
PA
931 fprintf (stderr, "Already debugging a process\n");
932 write_enn (own_buf);
933 return;
2d717e4f
DJ
934 }
935 handle_v_attach (own_buf, status, signal);
936 return;
937 }
938
939 if (strncmp (own_buf, "vRun;", 5) == 0)
940 {
941 if (target_running ())
942 {
fd96d250
PA
943 fprintf (stderr, "Already debugging a process\n");
944 write_enn (own_buf);
945 return;
2d717e4f
DJ
946 }
947 handle_v_run (own_buf, status, signal);
948 return;
949 }
950
64386c31
DJ
951 /* Otherwise we didn't know what packet it was. Say we didn't
952 understand it. */
953 own_buf[0] = 0;
954 return;
955}
956
957void
27524b67 958myresume (char *own_buf, int step, int *signalp, char *statusp)
64386c31
DJ
959{
960 struct thread_resume resume_info[2];
961 int n = 0;
a20d5e98
DJ
962 int sig = *signalp;
963
964 set_desired_inferior (0);
64386c31 965
d592fa2f 966 if (step || sig || (cont_thread != 0 && cont_thread != -1))
64386c31
DJ
967 {
968 resume_info[0].thread
969 = ((struct inferior_list_entry *) current_inferior)->id;
970 resume_info[0].step = step;
971 resume_info[0].sig = sig;
972 resume_info[0].leave_stopped = 0;
973 n++;
974 }
975 resume_info[n].thread = -1;
976 resume_info[n].step = 0;
977 resume_info[n].sig = 0;
d592fa2f 978 resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1);
64386c31 979
a20d5e98 980 enable_async_io ();
64386c31 981 (*the_target->resume) (resume_info);
a20d5e98
DJ
982 *signalp = mywait (statusp, 1);
983 prepare_resume_reply (own_buf, *statusp, *signalp);
984 disable_async_io ();
64386c31
DJ
985}
986
dd24457d
DJ
987static void
988gdbserver_version (void)
989{
990 printf ("GNU gdbserver %s\n"
255e7678 991 "Copyright (C) 2007 Free Software Foundation, Inc.\n"
dd24457d
DJ
992 "gdbserver is free software, covered by the GNU General Public License.\n"
993 "This gdbserver was configured as \"%s\"\n",
994 version, host_name);
995}
996
0bc68c49
DJ
997static void
998gdbserver_usage (void)
999{
2d717e4f
DJ
1000 printf ("Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
1001 "\tgdbserver [OPTIONS] --attach COMM PID\n"
1002 "\tgdbserver [OPTIONS] --multi COMM\n"
dd24457d
DJ
1003 "\n"
1004 "COMM may either be a tty device (for serial debugging), or \n"
2d717e4f
DJ
1005 "HOST:PORT to listen for a TCP connection.\n"
1006 "\n"
1007 "Options:\n"
1008 " --debug\t\tEnable debugging output.\n");
0bc68c49
DJ
1009}
1010
2d717e4f
DJ
1011#undef require_running
1012#define require_running(BUF) \
1013 if (!target_running ()) \
1014 { \
1015 write_enn (BUF); \
1016 break; \
1017 }
1018
c906108c 1019int
da85418c 1020main (int argc, char *argv[])
c906108c 1021{
f450004a 1022 char ch, status, *own_buf;
7fb85e41 1023 unsigned char *mem_buf;
c906108c 1024 int i = 0;
fc620387 1025 int signal;
c906108c
SS
1026 unsigned int len;
1027 CORE_ADDR mem_addr;
0729219d
DJ
1028 int bad_attach;
1029 int pid;
2d717e4f
DJ
1030 char *arg_end, *port;
1031 char **next_arg = &argv[1];
1032 int multi_mode = 0;
1033 int attach = 0;
1034 int was_running;
c906108c 1035
2d717e4f 1036 while (*next_arg != NULL && **next_arg == '-')
dd24457d 1037 {
2d717e4f
DJ
1038 if (strcmp (*next_arg, "--version") == 0)
1039 {
1040 gdbserver_version ();
1041 exit (0);
1042 }
1043 else if (strcmp (*next_arg, "--help") == 0)
1044 {
1045 gdbserver_usage ();
1046 exit (0);
1047 }
1048 else if (strcmp (*next_arg, "--attach") == 0)
1049 attach = 1;
1050 else if (strcmp (*next_arg, "--multi") == 0)
1051 multi_mode = 1;
1052 else if (strcmp (*next_arg, "--debug") == 0)
1053 debug_threads = 1;
1054 else
1055 {
1056 fprintf (stderr, "Unknown argument: %s\n", *next_arg);
1057 exit (1);
1058 }
dd24457d 1059
2d717e4f
DJ
1060 next_arg++;
1061 continue;
dd24457d
DJ
1062 }
1063
c5aa993b 1064 if (setjmp (toplevel))
c906108c 1065 {
c5aa993b
JM
1066 fprintf (stderr, "Exiting\n");
1067 exit (1);
c906108c
SS
1068 }
1069
2d717e4f
DJ
1070 port = *next_arg;
1071 next_arg++;
1072 if (port == NULL || (!attach && !multi_mode && *next_arg == NULL))
1073 {
1074 gdbserver_usage ();
1075 exit (1);
1076 }
1077
0729219d
DJ
1078 bad_attach = 0;
1079 pid = 0;
2d717e4f
DJ
1080
1081 /* --attach used to come after PORT, so allow it there for
1082 compatibility. */
1083 if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0)
45b7b345 1084 {
2d717e4f
DJ
1085 attach = 1;
1086 next_arg++;
45b7b345
DJ
1087 }
1088
2d717e4f
DJ
1089 if (attach
1090 && (*next_arg == NULL
1091 || (*next_arg)[0] == '\0'
1092 || (pid = strtoul (*next_arg, &arg_end, 0)) == 0
1093 || *arg_end != '\0'
1094 || next_arg[1] != NULL))
1095 bad_attach = 1;
1096
1097 if (bad_attach)
dd24457d
DJ
1098 {
1099 gdbserver_usage ();
1100 exit (1);
1101 }
c906108c 1102
a20d5e98 1103 initialize_async_io ();
4ce44c66
JM
1104 initialize_low ();
1105
255e7678 1106 own_buf = malloc (PBUFSIZ + 1);
7fb85e41 1107 mem_buf = malloc (PBUFSIZ);
0a30fbc4 1108
2d717e4f 1109 if (pid == 0 && *next_arg != NULL)
45b7b345 1110 {
2d717e4f
DJ
1111 int i, n;
1112
1113 n = argc - (next_arg - argv);
1114 program_argv = malloc (sizeof (char *) * (n + 1));
1115 for (i = 0; i < n; i++)
1116 program_argv[i] = strdup (next_arg[i]);
1117 program_argv[i] = NULL;
1118
45b7b345 1119 /* Wait till we are at first instruction in program. */
2d717e4f 1120 signal = start_inferior (program_argv, &status);
c906108c 1121
c588c53c
MS
1122 /* We are now (hopefully) stopped at the first instruction of
1123 the target process. This assumes that the target process was
1124 successfully created. */
45b7b345 1125 }
2d717e4f
DJ
1126 else if (pid != 0)
1127 {
1128 if (attach_inferior (pid, &status, &signal) == -1)
1129 error ("Attaching not supported on this target");
1130
1131 /* Otherwise succeeded. */
1132 }
45b7b345
DJ
1133 else
1134 {
2d717e4f
DJ
1135 status = 'W';
1136 signal = 0;
45b7b345 1137 }
c906108c 1138
311de423
PA
1139 /* Don't report shared library events on the initial connection,
1140 even if some libraries are preloaded. Avoids the "stopped by
1141 shared library event" notice on gdb side. */
1142 dlls_changed = 0;
1143
8264bb58
DJ
1144 if (setjmp (toplevel))
1145 {
1146 fprintf (stderr, "Killing inferior\n");
1147 kill_inferior ();
1148 exit (1);
1149 }
1150
c588c53c 1151 if (status == 'W' || status == 'X')
2d717e4f
DJ
1152 was_running = 0;
1153 else
1154 was_running = 1;
1155
1156 if (!was_running && !multi_mode)
c588c53c 1157 {
2d717e4f 1158 fprintf (stderr, "No program to debug. GDBserver exiting.\n");
c588c53c
MS
1159 exit (1);
1160 }
1161
c906108c
SS
1162 while (1)
1163 {
2d717e4f 1164 remote_open (port);
c906108c 1165
c5aa993b 1166 restart:
2d717e4f
DJ
1167 if (setjmp (toplevel) != 0)
1168 {
1169 /* An error occurred. */
1170 if (response_needed)
1171 {
1172 write_enn (own_buf);
1173 putpkt (own_buf);
1174 }
1175 }
1176
a20d5e98 1177 disable_async_io ();
2d717e4f 1178 while (!exit_requested)
c906108c
SS
1179 {
1180 unsigned char sig;
01f9e8fa
DJ
1181 int packet_len;
1182 int new_packet_len = -1;
1183
2d717e4f 1184 response_needed = 0;
01f9e8fa
DJ
1185 packet_len = getpkt (own_buf);
1186 if (packet_len <= 0)
1187 break;
2d717e4f 1188 response_needed = 1;
01f9e8fa 1189
c906108c
SS
1190 i = 0;
1191 ch = own_buf[i++];
1192 switch (ch)
1193 {
ce3a066d 1194 case 'q':
0e7f50da 1195 handle_query (own_buf, packet_len, &new_packet_len);
ce3a066d 1196 break;
89be2091
DJ
1197 case 'Q':
1198 handle_general_set (own_buf);
1199 break;
6ad8ae5c 1200 case 'D':
2d717e4f 1201 require_running (own_buf);
6ad8ae5c 1202 fprintf (stderr, "Detaching from inferior\n");
444d6139 1203 if (detach_inferior () != 0)
2d717e4f 1204 write_enn (own_buf);
444d6139
PA
1205 else
1206 {
1207 write_ok (own_buf);
6ad8ae5c 1208
2d717e4f
DJ
1209 if (extended_protocol)
1210 {
1211 /* Treat this like a normal program exit. */
1212 signal = 0;
1213 status = 'W';
1214 }
1215 else
1216 {
1217 putpkt (own_buf);
1218 remote_close ();
6ad8ae5c 1219
2d717e4f
DJ
1220 /* If we are attached, then we can exit. Otherwise, we
1221 need to hang around doing nothing, until the child
1222 is gone. */
1223 if (!attached)
1224 join_inferior ();
1225
1226 exit (0);
1227 }
444d6139 1228 }
2d717e4f 1229 break;
c906108c 1230 case '!':
2d717e4f
DJ
1231 extended_protocol = 1;
1232 write_ok (own_buf);
c906108c
SS
1233 break;
1234 case '?':
1235 prepare_resume_reply (own_buf, status, signal);
1236 break;
1237 case 'H':
a06660f7 1238 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
c906108c 1239 {
a06660f7
DJ
1240 unsigned long gdb_id, thread_id;
1241
2d717e4f 1242 require_running (own_buf);
a06660f7 1243 gdb_id = strtoul (&own_buf[2], NULL, 16);
2d717e4f
DJ
1244 if (gdb_id == 0 || gdb_id == -1)
1245 thread_id = gdb_id;
1246 else
a06660f7 1247 {
2d717e4f
DJ
1248 thread_id = gdb_id_to_thread_id (gdb_id);
1249 if (thread_id == 0)
1250 {
1251 write_enn (own_buf);
1252 break;
1253 }
a06660f7
DJ
1254 }
1255
1256 if (own_buf[1] == 'g')
1257 {
1258 general_thread = thread_id;
1259 set_desired_inferior (1);
1260 }
1261 else if (own_buf[1] == 'c')
1262 cont_thread = thread_id;
1263 else if (own_buf[1] == 's')
1264 step_thread = thread_id;
1265
0d62e5e8 1266 write_ok (own_buf);
a06660f7
DJ
1267 }
1268 else
1269 {
c906108c
SS
1270 /* Silently ignore it so that gdb can extend the protocol
1271 without compatibility headaches. */
1272 own_buf[0] = '\0';
c906108c
SS
1273 }
1274 break;
1275 case 'g':
2d717e4f 1276 require_running (own_buf);
0d62e5e8 1277 set_desired_inferior (1);
0a30fbc4 1278 registers_to_string (own_buf);
c906108c
SS
1279 break;
1280 case 'G':
2d717e4f 1281 require_running (own_buf);
0d62e5e8 1282 set_desired_inferior (1);
0a30fbc4 1283 registers_from_string (&own_buf[1]);
c906108c
SS
1284 write_ok (own_buf);
1285 break;
1286 case 'm':
2d717e4f 1287 require_running (own_buf);
c906108c 1288 decode_m_packet (&own_buf[1], &mem_addr, &len);
c3e735a6
DJ
1289 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
1290 convert_int_to_ascii (mem_buf, own_buf, len);
1291 else
1292 write_enn (own_buf);
c906108c
SS
1293 break;
1294 case 'M':
2d717e4f 1295 require_running (own_buf);
c906108c
SS
1296 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
1297 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
1298 write_ok (own_buf);
1299 else
1300 write_enn (own_buf);
1301 break;
01f9e8fa 1302 case 'X':
2d717e4f 1303 require_running (own_buf);
01f9e8fa
DJ
1304 if (decode_X_packet (&own_buf[1], packet_len - 1,
1305 &mem_addr, &len, mem_buf) < 0
1306 || write_inferior_memory (mem_addr, mem_buf, len) != 0)
1307 write_enn (own_buf);
1308 else
1309 write_ok (own_buf);
1310 break;
c906108c 1311 case 'C':
2d717e4f 1312 require_running (own_buf);
c906108c 1313 convert_ascii_to_int (own_buf + 1, &sig, 1);
0e98d0a7
DJ
1314 if (target_signal_to_host_p (sig))
1315 signal = target_signal_to_host (sig);
1316 else
1317 signal = 0;
27524b67 1318 myresume (own_buf, 0, &signal, &status);
c906108c
SS
1319 break;
1320 case 'S':
2d717e4f 1321 require_running (own_buf);
c906108c 1322 convert_ascii_to_int (own_buf + 1, &sig, 1);
0e98d0a7
DJ
1323 if (target_signal_to_host_p (sig))
1324 signal = target_signal_to_host (sig);
1325 else
1326 signal = 0;
27524b67 1327 myresume (own_buf, 1, &signal, &status);
c906108c
SS
1328 break;
1329 case 'c':
2d717e4f 1330 require_running (own_buf);
a20d5e98 1331 signal = 0;
27524b67 1332 myresume (own_buf, 0, &signal, &status);
c906108c
SS
1333 break;
1334 case 's':
2d717e4f 1335 require_running (own_buf);
a20d5e98 1336 signal = 0;
27524b67 1337 myresume (own_buf, 1, &signal, &status);
c906108c 1338 break;
e013ee27
OF
1339 case 'Z':
1340 {
1341 char *lenptr;
1342 char *dataptr;
1343 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1344 int len = strtol (lenptr + 1, &dataptr, 16);
1345 char type = own_buf[1];
1346
1347 if (the_target->insert_watchpoint == NULL
1348 || (type < '2' || type > '4'))
1349 {
1350 /* No watchpoint support or not a watchpoint command;
1351 unrecognized either way. */
1352 own_buf[0] = '\0';
1353 }
1354 else
1355 {
1356 int res;
1357
2d717e4f 1358 require_running (own_buf);
e013ee27
OF
1359 res = (*the_target->insert_watchpoint) (type, addr, len);
1360 if (res == 0)
1361 write_ok (own_buf);
1362 else if (res == 1)
1363 /* Unsupported. */
1364 own_buf[0] = '\0';
1365 else
1366 write_enn (own_buf);
1367 }
1368 break;
1369 }
1370 case 'z':
1371 {
1372 char *lenptr;
1373 char *dataptr;
1374 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1375 int len = strtol (lenptr + 1, &dataptr, 16);
1376 char type = own_buf[1];
1377
1378 if (the_target->remove_watchpoint == NULL
1379 || (type < '2' || type > '4'))
1380 {
1381 /* No watchpoint support or not a watchpoint command;
1382 unrecognized either way. */
1383 own_buf[0] = '\0';
1384 }
1385 else
1386 {
1387 int res;
1388
2d717e4f 1389 require_running (own_buf);
e013ee27
OF
1390 res = (*the_target->remove_watchpoint) (type, addr, len);
1391 if (res == 0)
1392 write_ok (own_buf);
1393 else if (res == 1)
1394 /* Unsupported. */
1395 own_buf[0] = '\0';
1396 else
1397 write_enn (own_buf);
1398 }
1399 break;
1400 }
c906108c 1401 case 'k':
2d717e4f
DJ
1402 response_needed = 0;
1403 if (!target_running ())
1404 /* The packet we received doesn't make sense - but we
1405 can't reply to it, either. */
1406 goto restart;
1407
c906108c
SS
1408 fprintf (stderr, "Killing inferior\n");
1409 kill_inferior ();
2d717e4f
DJ
1410
1411 /* When using the extended protocol, we wait with no
1412 program running. The traditional protocol will exit
1413 instead. */
c906108c
SS
1414 if (extended_protocol)
1415 {
2d717e4f
DJ
1416 status = 'X';
1417 signal = TARGET_SIGNAL_KILL;
1418 was_running = 0;
c906108c 1419 goto restart;
c906108c
SS
1420 }
1421 else
1422 {
1423 exit (0);
1424 break;
1425 }
1426 case 'T':
a06660f7
DJ
1427 {
1428 unsigned long gdb_id, thread_id;
1429
2d717e4f 1430 require_running (own_buf);
a06660f7
DJ
1431 gdb_id = strtoul (&own_buf[1], NULL, 16);
1432 thread_id = gdb_id_to_thread_id (gdb_id);
1433 if (thread_id == 0)
1434 {
1435 write_enn (own_buf);
1436 break;
1437 }
1438
1439 if (mythread_alive (thread_id))
1440 write_ok (own_buf);
1441 else
1442 write_enn (own_buf);
1443 }
c906108c
SS
1444 break;
1445 case 'R':
2d717e4f
DJ
1446 response_needed = 0;
1447
c906108c 1448 /* Restarting the inferior is only supported in the
c5aa993b 1449 extended protocol. */
c906108c
SS
1450 if (extended_protocol)
1451 {
2d717e4f
DJ
1452 if (target_running ())
1453 kill_inferior ();
c906108c
SS
1454 fprintf (stderr, "GDBserver restarting\n");
1455
1456 /* Wait till we are at 1st instruction in prog. */
2d717e4f
DJ
1457 if (program_argv != NULL)
1458 signal = start_inferior (program_argv, &status);
1459 else
1460 {
1461 status = 'X';
1462 signal = TARGET_SIGNAL_KILL;
1463 }
c906108c 1464 goto restart;
c906108c
SS
1465 }
1466 else
1467 {
1468 /* It is a request we don't understand. Respond with an
1469 empty packet so that gdb knows that we don't support this
1470 request. */
1471 own_buf[0] = '\0';
1472 break;
1473 }
64386c31
DJ
1474 case 'v':
1475 /* Extended (long) request. */
a6b151f1
DJ
1476 handle_v_requests (own_buf, &status, &signal,
1477 packet_len, &new_packet_len);
64386c31 1478 break;
a6b151f1 1479
c906108c
SS
1480 default:
1481 /* It is a request we don't understand. Respond with an
c5aa993b
JM
1482 empty packet so that gdb knows that we don't support this
1483 request. */
c906108c
SS
1484 own_buf[0] = '\0';
1485 break;
1486 }
1487
01f9e8fa
DJ
1488 if (new_packet_len != -1)
1489 putpkt_binary (own_buf, new_packet_len);
1490 else
1491 putpkt (own_buf);
c906108c 1492
2d717e4f
DJ
1493 response_needed = 0;
1494
1495 if (was_running && (status == 'W' || status == 'X'))
c906108c 1496 {
2d717e4f 1497 was_running = 0;
c906108c 1498
2d717e4f
DJ
1499 if (status == 'W')
1500 fprintf (stderr,
1501 "\nChild exited with status %d\n", signal);
1502 if (status == 'X')
1503 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
1504 target_signal_to_host (signal),
1505 target_signal_to_name (signal));
1506
1507 if (extended_protocol)
1508 goto restart;
c906108c
SS
1509 else
1510 {
1511 fprintf (stderr, "GDBserver exiting\n");
1512 exit (0);
1513 }
1514 }
c906108c 1515
2d717e4f
DJ
1516 if (status != 'W' && status != 'X')
1517 was_running = 1;
1518 }
c906108c 1519
2d717e4f
DJ
1520 /* If an exit was requested (using the "monitor exit" command),
1521 terminate now. The only other way to get here is for
1522 getpkt to fail; close the connection and reopen it at the
1523 top of the loop. */
c906108c 1524
2d717e4f 1525 if (exit_requested)
c906108c
SS
1526 {
1527 remote_close ();
2d717e4f
DJ
1528 if (attached && target_running ())
1529 detach_inferior ();
1530 else if (target_running ())
1531 kill_inferior ();
c906108c
SS
1532 exit (0);
1533 }
1534 else
1535 {
45b7b345
DJ
1536 fprintf (stderr, "Remote side has terminated connection. "
1537 "GDBserver will reopen the connection.\n");
c906108c
SS
1538 remote_close ();
1539 }
1540 }
1541}