* server.c (monitor_show_help): Add "exit".
[deliverable/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
652 if ((len % 1) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
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 {
931 fprintf (stderr, "Killing inferior\n");
932 kill_inferior ();
933 }
934 handle_v_attach (own_buf, status, signal);
935 return;
936 }
937
938 if (strncmp (own_buf, "vRun;", 5) == 0)
939 {
940 if (target_running ())
941 {
942 fprintf (stderr, "Killing inferior\n");
943 kill_inferior ();
944 }
945 handle_v_run (own_buf, status, signal);
946 return;
947 }
948
64386c31
DJ
949 /* Otherwise we didn't know what packet it was. Say we didn't
950 understand it. */
951 own_buf[0] = 0;
952 return;
953}
954
955void
27524b67 956myresume (char *own_buf, int step, int *signalp, char *statusp)
64386c31
DJ
957{
958 struct thread_resume resume_info[2];
959 int n = 0;
a20d5e98
DJ
960 int sig = *signalp;
961
962 set_desired_inferior (0);
64386c31 963
d592fa2f 964 if (step || sig || (cont_thread != 0 && cont_thread != -1))
64386c31
DJ
965 {
966 resume_info[0].thread
967 = ((struct inferior_list_entry *) current_inferior)->id;
968 resume_info[0].step = step;
969 resume_info[0].sig = sig;
970 resume_info[0].leave_stopped = 0;
971 n++;
972 }
973 resume_info[n].thread = -1;
974 resume_info[n].step = 0;
975 resume_info[n].sig = 0;
d592fa2f 976 resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1);
64386c31 977
a20d5e98 978 enable_async_io ();
64386c31 979 (*the_target->resume) (resume_info);
a20d5e98
DJ
980 *signalp = mywait (statusp, 1);
981 prepare_resume_reply (own_buf, *statusp, *signalp);
982 disable_async_io ();
64386c31
DJ
983}
984
dd24457d
DJ
985static void
986gdbserver_version (void)
987{
988 printf ("GNU gdbserver %s\n"
255e7678 989 "Copyright (C) 2007 Free Software Foundation, Inc.\n"
dd24457d
DJ
990 "gdbserver is free software, covered by the GNU General Public License.\n"
991 "This gdbserver was configured as \"%s\"\n",
992 version, host_name);
993}
994
0bc68c49
DJ
995static void
996gdbserver_usage (void)
997{
2d717e4f
DJ
998 printf ("Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
999 "\tgdbserver [OPTIONS] --attach COMM PID\n"
1000 "\tgdbserver [OPTIONS] --multi COMM\n"
dd24457d
DJ
1001 "\n"
1002 "COMM may either be a tty device (for serial debugging), or \n"
2d717e4f
DJ
1003 "HOST:PORT to listen for a TCP connection.\n"
1004 "\n"
1005 "Options:\n"
1006 " --debug\t\tEnable debugging output.\n");
0bc68c49
DJ
1007}
1008
2d717e4f
DJ
1009#undef require_running
1010#define require_running(BUF) \
1011 if (!target_running ()) \
1012 { \
1013 write_enn (BUF); \
1014 break; \
1015 }
1016
c906108c 1017int
da85418c 1018main (int argc, char *argv[])
c906108c 1019{
f450004a 1020 char ch, status, *own_buf;
7fb85e41 1021 unsigned char *mem_buf;
c906108c 1022 int i = 0;
fc620387 1023 int signal;
c906108c
SS
1024 unsigned int len;
1025 CORE_ADDR mem_addr;
0729219d
DJ
1026 int bad_attach;
1027 int pid;
2d717e4f
DJ
1028 char *arg_end, *port;
1029 char **next_arg = &argv[1];
1030 int multi_mode = 0;
1031 int attach = 0;
1032 int was_running;
c906108c 1033
2d717e4f 1034 while (*next_arg != NULL && **next_arg == '-')
dd24457d 1035 {
2d717e4f
DJ
1036 if (strcmp (*next_arg, "--version") == 0)
1037 {
1038 gdbserver_version ();
1039 exit (0);
1040 }
1041 else if (strcmp (*next_arg, "--help") == 0)
1042 {
1043 gdbserver_usage ();
1044 exit (0);
1045 }
1046 else if (strcmp (*next_arg, "--attach") == 0)
1047 attach = 1;
1048 else if (strcmp (*next_arg, "--multi") == 0)
1049 multi_mode = 1;
1050 else if (strcmp (*next_arg, "--debug") == 0)
1051 debug_threads = 1;
1052 else
1053 {
1054 fprintf (stderr, "Unknown argument: %s\n", *next_arg);
1055 exit (1);
1056 }
dd24457d 1057
2d717e4f
DJ
1058 next_arg++;
1059 continue;
dd24457d
DJ
1060 }
1061
c5aa993b 1062 if (setjmp (toplevel))
c906108c 1063 {
c5aa993b
JM
1064 fprintf (stderr, "Exiting\n");
1065 exit (1);
c906108c
SS
1066 }
1067
2d717e4f
DJ
1068 port = *next_arg;
1069 next_arg++;
1070 if (port == NULL || (!attach && !multi_mode && *next_arg == NULL))
1071 {
1072 gdbserver_usage ();
1073 exit (1);
1074 }
1075
0729219d
DJ
1076 bad_attach = 0;
1077 pid = 0;
2d717e4f
DJ
1078
1079 /* --attach used to come after PORT, so allow it there for
1080 compatibility. */
1081 if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0)
45b7b345 1082 {
2d717e4f
DJ
1083 attach = 1;
1084 next_arg++;
45b7b345
DJ
1085 }
1086
2d717e4f
DJ
1087 if (attach
1088 && (*next_arg == NULL
1089 || (*next_arg)[0] == '\0'
1090 || (pid = strtoul (*next_arg, &arg_end, 0)) == 0
1091 || *arg_end != '\0'
1092 || next_arg[1] != NULL))
1093 bad_attach = 1;
1094
1095 if (bad_attach)
dd24457d
DJ
1096 {
1097 gdbserver_usage ();
1098 exit (1);
1099 }
c906108c 1100
a20d5e98 1101 initialize_async_io ();
4ce44c66
JM
1102 initialize_low ();
1103
255e7678 1104 own_buf = malloc (PBUFSIZ + 1);
7fb85e41 1105 mem_buf = malloc (PBUFSIZ);
0a30fbc4 1106
2d717e4f 1107 if (pid == 0 && *next_arg != NULL)
45b7b345 1108 {
2d717e4f
DJ
1109 int i, n;
1110
1111 n = argc - (next_arg - argv);
1112 program_argv = malloc (sizeof (char *) * (n + 1));
1113 for (i = 0; i < n; i++)
1114 program_argv[i] = strdup (next_arg[i]);
1115 program_argv[i] = NULL;
1116
45b7b345 1117 /* Wait till we are at first instruction in program. */
2d717e4f 1118 signal = start_inferior (program_argv, &status);
c906108c 1119
c588c53c
MS
1120 /* We are now (hopefully) stopped at the first instruction of
1121 the target process. This assumes that the target process was
1122 successfully created. */
45b7b345 1123 }
2d717e4f
DJ
1124 else if (pid != 0)
1125 {
1126 if (attach_inferior (pid, &status, &signal) == -1)
1127 error ("Attaching not supported on this target");
1128
1129 /* Otherwise succeeded. */
1130 }
45b7b345
DJ
1131 else
1132 {
2d717e4f
DJ
1133 status = 'W';
1134 signal = 0;
45b7b345 1135 }
c906108c 1136
311de423
PA
1137 /* Don't report shared library events on the initial connection,
1138 even if some libraries are preloaded. Avoids the "stopped by
1139 shared library event" notice on gdb side. */
1140 dlls_changed = 0;
1141
8264bb58
DJ
1142 if (setjmp (toplevel))
1143 {
1144 fprintf (stderr, "Killing inferior\n");
1145 kill_inferior ();
1146 exit (1);
1147 }
1148
c588c53c 1149 if (status == 'W' || status == 'X')
2d717e4f
DJ
1150 was_running = 0;
1151 else
1152 was_running = 1;
1153
1154 if (!was_running && !multi_mode)
c588c53c 1155 {
2d717e4f 1156 fprintf (stderr, "No program to debug. GDBserver exiting.\n");
c588c53c
MS
1157 exit (1);
1158 }
1159
c906108c
SS
1160 while (1)
1161 {
2d717e4f 1162 remote_open (port);
c906108c 1163
c5aa993b 1164 restart:
2d717e4f
DJ
1165 if (setjmp (toplevel) != 0)
1166 {
1167 /* An error occurred. */
1168 if (response_needed)
1169 {
1170 write_enn (own_buf);
1171 putpkt (own_buf);
1172 }
1173 }
1174
a20d5e98 1175 disable_async_io ();
2d717e4f 1176 while (!exit_requested)
c906108c
SS
1177 {
1178 unsigned char sig;
01f9e8fa
DJ
1179 int packet_len;
1180 int new_packet_len = -1;
1181
2d717e4f 1182 response_needed = 0;
01f9e8fa
DJ
1183 packet_len = getpkt (own_buf);
1184 if (packet_len <= 0)
1185 break;
2d717e4f 1186 response_needed = 1;
01f9e8fa 1187
c906108c
SS
1188 i = 0;
1189 ch = own_buf[i++];
1190 switch (ch)
1191 {
ce3a066d 1192 case 'q':
0e7f50da 1193 handle_query (own_buf, packet_len, &new_packet_len);
ce3a066d 1194 break;
89be2091
DJ
1195 case 'Q':
1196 handle_general_set (own_buf);
1197 break;
6ad8ae5c 1198 case 'D':
2d717e4f 1199 require_running (own_buf);
6ad8ae5c 1200 fprintf (stderr, "Detaching from inferior\n");
444d6139 1201 if (detach_inferior () != 0)
2d717e4f 1202 write_enn (own_buf);
444d6139
PA
1203 else
1204 {
1205 write_ok (own_buf);
6ad8ae5c 1206
2d717e4f
DJ
1207 if (extended_protocol)
1208 {
1209 /* Treat this like a normal program exit. */
1210 signal = 0;
1211 status = 'W';
1212 }
1213 else
1214 {
1215 putpkt (own_buf);
1216 remote_close ();
6ad8ae5c 1217
2d717e4f
DJ
1218 /* If we are attached, then we can exit. Otherwise, we
1219 need to hang around doing nothing, until the child
1220 is gone. */
1221 if (!attached)
1222 join_inferior ();
1223
1224 exit (0);
1225 }
444d6139 1226 }
2d717e4f 1227 break;
c906108c 1228 case '!':
2d717e4f
DJ
1229 extended_protocol = 1;
1230 write_ok (own_buf);
c906108c
SS
1231 break;
1232 case '?':
1233 prepare_resume_reply (own_buf, status, signal);
1234 break;
1235 case 'H':
a06660f7 1236 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
c906108c 1237 {
a06660f7
DJ
1238 unsigned long gdb_id, thread_id;
1239
2d717e4f 1240 require_running (own_buf);
a06660f7 1241 gdb_id = strtoul (&own_buf[2], NULL, 16);
2d717e4f
DJ
1242 if (gdb_id == 0 || gdb_id == -1)
1243 thread_id = gdb_id;
1244 else
a06660f7 1245 {
2d717e4f
DJ
1246 thread_id = gdb_id_to_thread_id (gdb_id);
1247 if (thread_id == 0)
1248 {
1249 write_enn (own_buf);
1250 break;
1251 }
a06660f7
DJ
1252 }
1253
1254 if (own_buf[1] == 'g')
1255 {
1256 general_thread = thread_id;
1257 set_desired_inferior (1);
1258 }
1259 else if (own_buf[1] == 'c')
1260 cont_thread = thread_id;
1261 else if (own_buf[1] == 's')
1262 step_thread = thread_id;
1263
0d62e5e8 1264 write_ok (own_buf);
a06660f7
DJ
1265 }
1266 else
1267 {
c906108c
SS
1268 /* Silently ignore it so that gdb can extend the protocol
1269 without compatibility headaches. */
1270 own_buf[0] = '\0';
c906108c
SS
1271 }
1272 break;
1273 case 'g':
2d717e4f 1274 require_running (own_buf);
0d62e5e8 1275 set_desired_inferior (1);
0a30fbc4 1276 registers_to_string (own_buf);
c906108c
SS
1277 break;
1278 case 'G':
2d717e4f 1279 require_running (own_buf);
0d62e5e8 1280 set_desired_inferior (1);
0a30fbc4 1281 registers_from_string (&own_buf[1]);
c906108c
SS
1282 write_ok (own_buf);
1283 break;
1284 case 'm':
2d717e4f 1285 require_running (own_buf);
c906108c 1286 decode_m_packet (&own_buf[1], &mem_addr, &len);
c3e735a6
DJ
1287 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
1288 convert_int_to_ascii (mem_buf, own_buf, len);
1289 else
1290 write_enn (own_buf);
c906108c
SS
1291 break;
1292 case 'M':
2d717e4f 1293 require_running (own_buf);
c906108c
SS
1294 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
1295 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
1296 write_ok (own_buf);
1297 else
1298 write_enn (own_buf);
1299 break;
01f9e8fa 1300 case 'X':
2d717e4f 1301 require_running (own_buf);
01f9e8fa
DJ
1302 if (decode_X_packet (&own_buf[1], packet_len - 1,
1303 &mem_addr, &len, mem_buf) < 0
1304 || write_inferior_memory (mem_addr, mem_buf, len) != 0)
1305 write_enn (own_buf);
1306 else
1307 write_ok (own_buf);
1308 break;
c906108c 1309 case 'C':
2d717e4f 1310 require_running (own_buf);
c906108c 1311 convert_ascii_to_int (own_buf + 1, &sig, 1);
0e98d0a7
DJ
1312 if (target_signal_to_host_p (sig))
1313 signal = target_signal_to_host (sig);
1314 else
1315 signal = 0;
27524b67 1316 myresume (own_buf, 0, &signal, &status);
c906108c
SS
1317 break;
1318 case 'S':
2d717e4f 1319 require_running (own_buf);
c906108c 1320 convert_ascii_to_int (own_buf + 1, &sig, 1);
0e98d0a7
DJ
1321 if (target_signal_to_host_p (sig))
1322 signal = target_signal_to_host (sig);
1323 else
1324 signal = 0;
27524b67 1325 myresume (own_buf, 1, &signal, &status);
c906108c
SS
1326 break;
1327 case 'c':
2d717e4f 1328 require_running (own_buf);
a20d5e98 1329 signal = 0;
27524b67 1330 myresume (own_buf, 0, &signal, &status);
c906108c
SS
1331 break;
1332 case 's':
2d717e4f 1333 require_running (own_buf);
a20d5e98 1334 signal = 0;
27524b67 1335 myresume (own_buf, 1, &signal, &status);
c906108c 1336 break;
e013ee27
OF
1337 case 'Z':
1338 {
1339 char *lenptr;
1340 char *dataptr;
1341 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1342 int len = strtol (lenptr + 1, &dataptr, 16);
1343 char type = own_buf[1];
1344
1345 if (the_target->insert_watchpoint == NULL
1346 || (type < '2' || type > '4'))
1347 {
1348 /* No watchpoint support or not a watchpoint command;
1349 unrecognized either way. */
1350 own_buf[0] = '\0';
1351 }
1352 else
1353 {
1354 int res;
1355
2d717e4f 1356 require_running (own_buf);
e013ee27
OF
1357 res = (*the_target->insert_watchpoint) (type, addr, len);
1358 if (res == 0)
1359 write_ok (own_buf);
1360 else if (res == 1)
1361 /* Unsupported. */
1362 own_buf[0] = '\0';
1363 else
1364 write_enn (own_buf);
1365 }
1366 break;
1367 }
1368 case 'z':
1369 {
1370 char *lenptr;
1371 char *dataptr;
1372 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1373 int len = strtol (lenptr + 1, &dataptr, 16);
1374 char type = own_buf[1];
1375
1376 if (the_target->remove_watchpoint == NULL
1377 || (type < '2' || type > '4'))
1378 {
1379 /* No watchpoint support or not a watchpoint command;
1380 unrecognized either way. */
1381 own_buf[0] = '\0';
1382 }
1383 else
1384 {
1385 int res;
1386
2d717e4f 1387 require_running (own_buf);
e013ee27
OF
1388 res = (*the_target->remove_watchpoint) (type, addr, len);
1389 if (res == 0)
1390 write_ok (own_buf);
1391 else if (res == 1)
1392 /* Unsupported. */
1393 own_buf[0] = '\0';
1394 else
1395 write_enn (own_buf);
1396 }
1397 break;
1398 }
c906108c 1399 case 'k':
2d717e4f
DJ
1400 response_needed = 0;
1401 if (!target_running ())
1402 /* The packet we received doesn't make sense - but we
1403 can't reply to it, either. */
1404 goto restart;
1405
c906108c
SS
1406 fprintf (stderr, "Killing inferior\n");
1407 kill_inferior ();
2d717e4f
DJ
1408
1409 /* When using the extended protocol, we wait with no
1410 program running. The traditional protocol will exit
1411 instead. */
c906108c
SS
1412 if (extended_protocol)
1413 {
2d717e4f
DJ
1414 status = 'X';
1415 signal = TARGET_SIGNAL_KILL;
1416 was_running = 0;
c906108c 1417 goto restart;
c906108c
SS
1418 }
1419 else
1420 {
1421 exit (0);
1422 break;
1423 }
1424 case 'T':
a06660f7
DJ
1425 {
1426 unsigned long gdb_id, thread_id;
1427
2d717e4f 1428 require_running (own_buf);
a06660f7
DJ
1429 gdb_id = strtoul (&own_buf[1], NULL, 16);
1430 thread_id = gdb_id_to_thread_id (gdb_id);
1431 if (thread_id == 0)
1432 {
1433 write_enn (own_buf);
1434 break;
1435 }
1436
1437 if (mythread_alive (thread_id))
1438 write_ok (own_buf);
1439 else
1440 write_enn (own_buf);
1441 }
c906108c
SS
1442 break;
1443 case 'R':
2d717e4f
DJ
1444 response_needed = 0;
1445
c906108c 1446 /* Restarting the inferior is only supported in the
c5aa993b 1447 extended protocol. */
c906108c
SS
1448 if (extended_protocol)
1449 {
2d717e4f
DJ
1450 if (target_running ())
1451 kill_inferior ();
c906108c
SS
1452 fprintf (stderr, "GDBserver restarting\n");
1453
1454 /* Wait till we are at 1st instruction in prog. */
2d717e4f
DJ
1455 if (program_argv != NULL)
1456 signal = start_inferior (program_argv, &status);
1457 else
1458 {
1459 status = 'X';
1460 signal = TARGET_SIGNAL_KILL;
1461 }
c906108c 1462 goto restart;
c906108c
SS
1463 }
1464 else
1465 {
1466 /* It is a request we don't understand. Respond with an
1467 empty packet so that gdb knows that we don't support this
1468 request. */
1469 own_buf[0] = '\0';
1470 break;
1471 }
64386c31
DJ
1472 case 'v':
1473 /* Extended (long) request. */
a6b151f1
DJ
1474 handle_v_requests (own_buf, &status, &signal,
1475 packet_len, &new_packet_len);
64386c31 1476 break;
a6b151f1 1477
c906108c
SS
1478 default:
1479 /* It is a request we don't understand. Respond with an
c5aa993b
JM
1480 empty packet so that gdb knows that we don't support this
1481 request. */
c906108c
SS
1482 own_buf[0] = '\0';
1483 break;
1484 }
1485
01f9e8fa
DJ
1486 if (new_packet_len != -1)
1487 putpkt_binary (own_buf, new_packet_len);
1488 else
1489 putpkt (own_buf);
c906108c 1490
2d717e4f
DJ
1491 response_needed = 0;
1492
1493 if (was_running && (status == 'W' || status == 'X'))
c906108c 1494 {
2d717e4f 1495 was_running = 0;
c906108c 1496
2d717e4f
DJ
1497 if (status == 'W')
1498 fprintf (stderr,
1499 "\nChild exited with status %d\n", signal);
1500 if (status == 'X')
1501 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
1502 target_signal_to_host (signal),
1503 target_signal_to_name (signal));
1504
1505 if (extended_protocol)
1506 goto restart;
c906108c
SS
1507 else
1508 {
1509 fprintf (stderr, "GDBserver exiting\n");
1510 exit (0);
1511 }
1512 }
c906108c 1513
2d717e4f
DJ
1514 if (status != 'W' && status != 'X')
1515 was_running = 1;
1516 }
c906108c 1517
2d717e4f
DJ
1518 /* If an exit was requested (using the "monitor exit" command),
1519 terminate now. The only other way to get here is for
1520 getpkt to fail; close the connection and reopen it at the
1521 top of the loop. */
c906108c 1522
2d717e4f 1523 if (exit_requested)
c906108c
SS
1524 {
1525 remote_close ();
2d717e4f
DJ
1526 if (attached && target_running ())
1527 detach_inferior ();
1528 else if (target_running ())
1529 kill_inferior ();
c906108c
SS
1530 exit (0);
1531 }
1532 else
1533 {
45b7b345
DJ
1534 fprintf (stderr, "Remote side has terminated connection. "
1535 "GDBserver will reopen the connection.\n");
c906108c
SS
1536 remote_close ();
1537 }
1538 }
1539}
This page took 0.9807 seconds and 4 git commands to generate.