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