from Mat Hostetter <mat@lcs.mit.edu>
[deliverable/binutils-gdb.git] / gdb / gdbserver / server.c
CommitLineData
c906108c 1/* Main code for remote server for GDB.
6aba47ca
DJ
2 Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003,
3 2004, 2005, 2006, 2007 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
9 the Free Software Foundation; either version 2 of the License, or
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
JM
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
6f0f660e
EZ
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
c906108c
SS
21
22#include "server.h"
23
a9fa9f7d
DJ
24#include <unistd.h>
25#include <signal.h>
b80864fb 26#if HAVE_SYS_WAIT_H
a9fa9f7d 27#include <sys/wait.h>
b80864fb 28#endif
a9fa9f7d 29
a1928bad
DJ
30unsigned long cont_thread;
31unsigned long general_thread;
32unsigned long step_thread;
33unsigned long thread_from_wait;
34unsigned long old_thread_from_wait;
c906108c 35int extended_protocol;
0d62e5e8
DJ
36int server_waiting;
37
c74d0ad8
DJ
38/* Enable miscellaneous debugging output. The name is historical - it
39 was originally used to debug LinuxThreads support. */
40int debug_threads;
41
89be2091
DJ
42int pass_signals[TARGET_SIGNAL_LAST];
43
c906108c 44jmp_buf toplevel;
c906108c 45
a9fa9f7d
DJ
46/* The PID of the originally created or attached inferior. Used to
47 send signals to the process when GDB sends us an asynchronous interrupt
48 (user hitting Control-C in the client), and to wait for the child to exit
49 when no longer debugging it. */
50
a1928bad 51unsigned long signal_pid;
a9fa9f7d 52
290fadea
RS
53#ifdef SIGTTOU
54/* A file descriptor for the controlling terminal. */
55int terminal_fd;
56
57/* TERMINAL_FD's original foreground group. */
58pid_t old_foreground_pgrp;
59
60/* Hand back terminal ownership to the original foreground group. */
61
62static void
63restore_old_foreground_pgrp (void)
64{
65 tcsetpgrp (terminal_fd, old_foreground_pgrp);
66}
67#endif
68
fc620387 69static int
da85418c 70start_inferior (char *argv[], char *statusptr)
c906108c 71{
b80864fb 72#ifdef SIGTTOU
a9fa9f7d
DJ
73 signal (SIGTTOU, SIG_DFL);
74 signal (SIGTTIN, SIG_DFL);
b80864fb 75#endif
a9fa9f7d
DJ
76
77 signal_pid = create_inferior (argv[0], argv);
0d62e5e8 78
a1928bad 79 fprintf (stderr, "Process %s created; pid = %ld\n", argv[0],
a9fa9f7d 80 signal_pid);
b80864fb 81 fflush (stderr);
a9fa9f7d 82
b80864fb 83#ifdef SIGTTOU
a9fa9f7d
DJ
84 signal (SIGTTOU, SIG_IGN);
85 signal (SIGTTIN, SIG_IGN);
290fadea
RS
86 terminal_fd = fileno (stderr);
87 old_foreground_pgrp = tcgetpgrp (terminal_fd);
88 tcsetpgrp (terminal_fd, signal_pid);
89 atexit (restore_old_foreground_pgrp);
b80864fb 90#endif
c906108c
SS
91
92 /* Wait till we are at 1st instruction in program, return signal number. */
0d62e5e8 93 return mywait (statusptr, 0);
c906108c
SS
94}
95
45b7b345 96static int
fc620387 97attach_inferior (int pid, char *statusptr, int *sigptr)
45b7b345
DJ
98{
99 /* myattach should return -1 if attaching is unsupported,
100 0 if it succeeded, and call error() otherwise. */
a9fa9f7d 101
45b7b345
DJ
102 if (myattach (pid) != 0)
103 return -1;
104
6910d122 105 fprintf (stderr, "Attached; pid = %d\n", pid);
b80864fb 106 fflush (stderr);
6910d122 107
a9fa9f7d
DJ
108 /* FIXME - It may be that we should get the SIGNAL_PID from the
109 attach function, so that it can be the main thread instead of
110 whichever we were told to attach to. */
111 signal_pid = pid;
112
0d62e5e8 113 *sigptr = mywait (statusptr, 0);
45b7b345 114
9db87ebd
DJ
115 /* GDB knows to ignore the first SIGSTOP after attaching to a running
116 process using the "attach" command, but this is different; it's
117 just using "target remote". Pretend it's just starting up. */
b80864fb
DJ
118 if (*statusptr == 'T' && *sigptr == TARGET_SIGNAL_STOP)
119 *sigptr = TARGET_SIGNAL_TRAP;
9db87ebd 120
45b7b345
DJ
121 return 0;
122}
123
c906108c 124extern int remote_debug;
ce3a066d 125
0876f84a
DJ
126/* Decode a qXfer read request. Return 0 if everything looks OK,
127 or -1 otherwise. */
128
129static int
130decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len)
131{
132 /* Extract and NUL-terminate the annex. */
133 *annex = buf;
134 while (*buf && *buf != ':')
135 buf++;
136 if (*buf == '\0')
137 return -1;
138 *buf++ = 0;
139
140 /* After the read/write marker and annex, qXfer looks like a
141 traditional 'm' packet. */
142 decode_m_packet (buf, ofs, len);
143
144 return 0;
145}
146
147/* Write the response to a successful qXfer read. Returns the
148 length of the (binary) data stored in BUF, corresponding
149 to as much of DATA/LEN as we could fit. IS_MORE controls
150 the first character of the response. */
151static int
23181151 152write_qxfer_response (char *buf, const void *data, int len, int is_more)
0876f84a
DJ
153{
154 int out_len;
155
156 if (is_more)
157 buf[0] = 'm';
158 else
159 buf[0] = 'l';
160
161 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
162 PBUFSIZ - 2) + 1;
163}
164
89be2091
DJ
165/* Handle all of the extended 'Q' packets. */
166void
167handle_general_set (char *own_buf)
168{
169 if (strncmp ("QPassSignals:", own_buf, strlen ("QPassSignals:")) == 0)
170 {
171 int numsigs = (int) TARGET_SIGNAL_LAST, i;
172 const char *p = own_buf + strlen ("QPassSignals:");
173 CORE_ADDR cursig;
174
175 p = decode_address_to_semicolon (&cursig, p);
176 for (i = 0; i < numsigs; i++)
177 {
178 if (i == cursig)
179 {
180 pass_signals[i] = 1;
181 if (*p == '\0')
182 /* Keep looping, to clear the remaining signals. */
183 cursig = -1;
184 else
185 p = decode_address_to_semicolon (&cursig, p);
186 }
187 else
188 pass_signals[i] = 0;
189 }
190 strcpy (own_buf, "OK");
191 return;
192 }
193
194 /* Otherwise we didn't know what packet it was. Say we didn't
195 understand it. */
196 own_buf[0] = 0;
197}
198
23181151 199static const char *
fb1e4ffc 200get_features_xml (const char *annex)
23181151
DJ
201{
202 static int features_supported = -1;
203 static char *document;
204
fb1e4ffc
DJ
205#ifdef USE_XML
206 extern const char *const xml_builtin[][2];
207 int i;
208
209 /* Look for the annex. */
210 for (i = 0; xml_builtin[i][0] != NULL; i++)
211 if (strcmp (annex, xml_builtin[i][0]) == 0)
212 break;
213
214 if (xml_builtin[i][0] != NULL)
215 return xml_builtin[i][1];
216#endif
217
218 if (strcmp (annex, "target.xml") != 0)
219 return NULL;
220
23181151
DJ
221 if (features_supported == -1)
222 {
820f2bda
PA
223 const char *arch = NULL;
224 if (the_target->arch_string != NULL)
225 arch = (*the_target->arch_string) ();
23181151
DJ
226
227 if (arch == NULL)
228 features_supported = 0;
229 else
230 {
231 features_supported = 1;
232 document = malloc (64 + strlen (arch));
233 snprintf (document, 64 + strlen (arch),
234 "<target><architecture>%s</architecture></target>",
235 arch);
236 }
237 }
238
239 return document;
240}
241
c74d0ad8
DJ
242void
243monitor_show_help (void)
244{
245 monitor_output ("The following monitor commands are supported:\n");
246 monitor_output (" set debug <0|1>\n");
247 monitor_output (" Enable general debugging messages\n");
248 monitor_output (" set remote-debug <0|1>\n");
249 monitor_output (" Enable remote protocol debugging messages\n");
250}
251
ce3a066d
DJ
252/* Handle all of the extended 'q' packets. */
253void
0876f84a 254handle_query (char *own_buf, int *new_packet_len_p)
ce3a066d 255{
0d62e5e8
DJ
256 static struct inferior_list_entry *thread_ptr;
257
ce3a066d
DJ
258 if (strcmp ("qSymbol::", own_buf) == 0)
259 {
2f2893d9
DJ
260 if (the_target->look_up_symbols != NULL)
261 (*the_target->look_up_symbols) ();
262
ce3a066d
DJ
263 strcpy (own_buf, "OK");
264 return;
265 }
266
0d62e5e8
DJ
267 if (strcmp ("qfThreadInfo", own_buf) == 0)
268 {
269 thread_ptr = all_threads.head;
a06660f7 270 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
0d62e5e8
DJ
271 thread_ptr = thread_ptr->next;
272 return;
273 }
aa691b87 274
0d62e5e8
DJ
275 if (strcmp ("qsThreadInfo", own_buf) == 0)
276 {
277 if (thread_ptr != NULL)
278 {
a06660f7 279 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
0d62e5e8
DJ
280 thread_ptr = thread_ptr->next;
281 return;
282 }
283 else
284 {
285 sprintf (own_buf, "l");
286 return;
287 }
288 }
aa691b87 289
52fb6437
NS
290 if (the_target->read_offsets != NULL
291 && strcmp ("qOffsets", own_buf) == 0)
292 {
293 CORE_ADDR text, data;
294
295 if (the_target->read_offsets (&text, &data))
296 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
297 (long)text, (long)data, (long)data);
298 else
299 write_enn (own_buf);
300
301 return;
302 }
303
aa691b87 304 if (the_target->read_auxv != NULL
0876f84a 305 && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0)
aa691b87 306 {
0876f84a
DJ
307 unsigned char *data;
308 int n;
aa691b87
RM
309 CORE_ADDR ofs;
310 unsigned int len;
0876f84a
DJ
311 char *annex;
312
313 /* Reject any annex; grab the offset and length. */
314 if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0
315 || annex[0] != '\0')
316 {
317 strcpy (own_buf, "E00");
318 return;
319 }
320
321 /* Read one extra byte, as an indicator of whether there is
322 more. */
323 if (len > PBUFSIZ - 2)
324 len = PBUFSIZ - 2;
325 data = malloc (len + 1);
326 n = (*the_target->read_auxv) (ofs, data, len + 1);
000ef4f0
DJ
327 if (n < 0)
328 write_enn (own_buf);
329 else if (n > len)
0876f84a 330 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
aa691b87 331 else
0876f84a
DJ
332 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
333
334 free (data);
335
aa691b87
RM
336 return;
337 }
338
23181151
DJ
339 if (strncmp ("qXfer:features:read:", own_buf, 20) == 0)
340 {
341 CORE_ADDR ofs;
342 unsigned int len, total_len;
343 const char *document;
344 char *annex;
345
fb1e4ffc
DJ
346 /* Check for support. */
347 document = get_features_xml ("target.xml");
23181151
DJ
348 if (document == NULL)
349 {
350 own_buf[0] = '\0';
351 return;
352 }
353
fb1e4ffc
DJ
354 /* Grab the annex, offset, and length. */
355 if (decode_xfer_read (own_buf + 20, &annex, &ofs, &len) < 0)
356 {
357 strcpy (own_buf, "E00");
358 return;
359 }
360
361 /* Now grab the correct annex. */
362 document = get_features_xml (annex);
363 if (document == NULL)
23181151
DJ
364 {
365 strcpy (own_buf, "E00");
366 return;
367 }
368
369 total_len = strlen (document);
370 if (len > PBUFSIZ - 2)
371 len = PBUFSIZ - 2;
372
373 if (ofs > total_len)
374 write_enn (own_buf);
375 else if (len < total_len - ofs)
376 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
377 len, 1);
378 else
379 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
380 total_len - ofs, 0);
381
382 return;
383 }
384
be2a5f71
DJ
385 /* Protocol features query. */
386 if (strncmp ("qSupported", own_buf, 10) == 0
387 && (own_buf[10] == ':' || own_buf[10] == '\0'))
388 {
89be2091 389 sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1);
0876f84a
DJ
390
391 if (the_target->read_auxv != NULL)
9f2e1e63 392 strcat (own_buf, ";qXfer:auxv:read+");
0876f84a 393
fb1e4ffc 394 if (get_features_xml ("target.xml") != NULL)
23181151
DJ
395 strcat (own_buf, ";qXfer:features:read+");
396
be2a5f71
DJ
397 return;
398 }
399
dae5f5cf
DJ
400 /* Thread-local storage support. */
401 if (the_target->get_tls_address != NULL
402 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
403 {
404 char *p = own_buf + 12;
405 CORE_ADDR parts[3], address = 0;
406 int i, err;
407
408 for (i = 0; i < 3; i++)
409 {
410 char *p2;
411 int len;
412
413 if (p == NULL)
414 break;
415
416 p2 = strchr (p, ',');
417 if (p2)
418 {
419 len = p2 - p;
420 p2++;
421 }
422 else
423 {
424 len = strlen (p);
425 p2 = NULL;
426 }
427
428 decode_address (&parts[i], p, len);
429 p = p2;
430 }
431
432 if (p != NULL || i < 3)
433 err = 1;
434 else
435 {
436 struct thread_info *thread = gdb_id_to_thread (parts[0]);
437
438 if (thread == NULL)
439 err = 2;
440 else
441 err = the_target->get_tls_address (thread, parts[1], parts[2],
442 &address);
443 }
444
445 if (err == 0)
446 {
447 sprintf (own_buf, "%llx", address);
448 return;
449 }
450 else if (err > 0)
451 {
452 write_enn (own_buf);
453 return;
454 }
455
456 /* Otherwise, pretend we do not understand this packet. */
457 }
458
c74d0ad8
DJ
459 /* Handle "monitor" commands. */
460 if (strncmp ("qRcmd,", own_buf, 6) == 0)
461 {
462 char *mon = malloc (PBUFSIZ);
463 int len = strlen (own_buf + 6);
464
465 if ((len % 1) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
466 {
467 write_enn (own_buf);
468 free (mon);
469 return;
470 }
471 mon[len / 2] = '\0';
472
473 write_ok (own_buf);
474
475 if (strcmp (mon, "set debug 1") == 0)
476 {
477 debug_threads = 1;
478 monitor_output ("Debug output enabled.\n");
479 }
480 else if (strcmp (mon, "set debug 0") == 0)
481 {
482 debug_threads = 0;
483 monitor_output ("Debug output disabled.\n");
484 }
485 else if (strcmp (mon, "set remote-debug 1") == 0)
486 {
487 remote_debug = 1;
488 monitor_output ("Protocol debug output enabled.\n");
489 }
490 else if (strcmp (mon, "set remote-debug 0") == 0)
491 {
492 remote_debug = 0;
493 monitor_output ("Protocol debug output disabled.\n");
494 }
495 else if (strcmp (mon, "help") == 0)
496 monitor_show_help ();
497 else
498 {
499 monitor_output ("Unknown monitor command.\n\n");
500 monitor_show_help ();
501 write_enn (own_buf);
502 }
503
504 free (mon);
505 return;
506 }
507
ce3a066d
DJ
508 /* Otherwise we didn't know what packet it was. Say we didn't
509 understand it. */
510 own_buf[0] = 0;
511}
512
64386c31
DJ
513/* Parse vCont packets. */
514void
fc620387 515handle_v_cont (char *own_buf, char *status, int *signal)
64386c31
DJ
516{
517 char *p, *q;
518 int n = 0, i = 0;
519 struct thread_resume *resume_info, default_action;
520
521 /* Count the number of semicolons in the packet. There should be one
522 for every action. */
523 p = &own_buf[5];
524 while (p)
525 {
526 n++;
527 p++;
528 p = strchr (p, ';');
529 }
530 /* Allocate room for one extra action, for the default remain-stopped
531 behavior; if no default action is in the list, we'll need the extra
532 slot. */
533 resume_info = malloc ((n + 1) * sizeof (resume_info[0]));
534
535 default_action.thread = -1;
536 default_action.leave_stopped = 1;
537 default_action.step = 0;
538 default_action.sig = 0;
539
540 p = &own_buf[5];
541 i = 0;
542 while (*p)
543 {
544 p++;
545
546 resume_info[i].leave_stopped = 0;
547
548 if (p[0] == 's' || p[0] == 'S')
549 resume_info[i].step = 1;
550 else if (p[0] == 'c' || p[0] == 'C')
551 resume_info[i].step = 0;
552 else
553 goto err;
554
555 if (p[0] == 'S' || p[0] == 'C')
556 {
557 int sig;
558 sig = strtol (p + 1, &q, 16);
559 if (p == q)
560 goto err;
561 p = q;
562
563 if (!target_signal_to_host_p (sig))
564 goto err;
565 resume_info[i].sig = target_signal_to_host (sig);
566 }
567 else
568 {
569 resume_info[i].sig = 0;
570 p = p + 1;
571 }
572
573 if (p[0] == 0)
574 {
575 resume_info[i].thread = -1;
576 default_action = resume_info[i];
577
578 /* Note: we don't increment i here, we'll overwrite this entry
579 the next time through. */
580 }
581 else if (p[0] == ':')
582 {
a06660f7
DJ
583 unsigned int gdb_id = strtoul (p + 1, &q, 16);
584 unsigned long thread_id;
585
64386c31
DJ
586 if (p == q)
587 goto err;
588 p = q;
589 if (p[0] != ';' && p[0] != 0)
590 goto err;
591
a06660f7
DJ
592 thread_id = gdb_id_to_thread_id (gdb_id);
593 if (thread_id)
594 resume_info[i].thread = thread_id;
595 else
596 goto err;
597
64386c31
DJ
598 i++;
599 }
600 }
601
602 resume_info[i] = default_action;
603
604 /* Still used in occasional places in the backend. */
605 if (n == 1 && resume_info[0].thread != -1)
606 cont_thread = resume_info[0].thread;
607 else
608 cont_thread = -1;
dc3f8883 609 set_desired_inferior (0);
64386c31
DJ
610
611 (*the_target->resume) (resume_info);
612
613 free (resume_info);
614
615 *signal = mywait (status, 1);
616 prepare_resume_reply (own_buf, *status, *signal);
617 return;
618
619err:
620 /* No other way to report an error... */
621 strcpy (own_buf, "");
622 free (resume_info);
623 return;
624}
625
626/* Handle all of the extended 'v' packets. */
627void
fc620387 628handle_v_requests (char *own_buf, char *status, int *signal)
64386c31
DJ
629{
630 if (strncmp (own_buf, "vCont;", 6) == 0)
631 {
632 handle_v_cont (own_buf, status, signal);
633 return;
634 }
635
636 if (strncmp (own_buf, "vCont?", 6) == 0)
637 {
638 strcpy (own_buf, "vCont;c;C;s;S");
639 return;
640 }
641
642 /* Otherwise we didn't know what packet it was. Say we didn't
643 understand it. */
644 own_buf[0] = 0;
645 return;
646}
647
648void
649myresume (int step, int sig)
650{
651 struct thread_resume resume_info[2];
652 int n = 0;
653
d592fa2f 654 if (step || sig || (cont_thread != 0 && cont_thread != -1))
64386c31
DJ
655 {
656 resume_info[0].thread
657 = ((struct inferior_list_entry *) current_inferior)->id;
658 resume_info[0].step = step;
659 resume_info[0].sig = sig;
660 resume_info[0].leave_stopped = 0;
661 n++;
662 }
663 resume_info[n].thread = -1;
664 resume_info[n].step = 0;
665 resume_info[n].sig = 0;
d592fa2f 666 resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1);
64386c31
DJ
667
668 (*the_target->resume) (resume_info);
669}
670
0729219d 671static int attached;
c906108c 672
dd24457d
DJ
673static void
674gdbserver_version (void)
675{
676 printf ("GNU gdbserver %s\n"
677 "Copyright (C) 2006 Free Software Foundation, Inc.\n"
678 "gdbserver is free software, covered by the GNU General Public License.\n"
679 "This gdbserver was configured as \"%s\"\n",
680 version, host_name);
681}
682
0bc68c49
DJ
683static void
684gdbserver_usage (void)
685{
dd24457d
DJ
686 printf ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
687 "\tgdbserver COMM --attach PID\n"
688 "\n"
689 "COMM may either be a tty device (for serial debugging), or \n"
690 "HOST:PORT to listen for a TCP connection.\n");
0bc68c49
DJ
691}
692
c906108c 693int
da85418c 694main (int argc, char *argv[])
c906108c 695{
f450004a 696 char ch, status, *own_buf;
7fb85e41 697 unsigned char *mem_buf;
c906108c 698 int i = 0;
fc620387 699 int signal;
c906108c
SS
700 unsigned int len;
701 CORE_ADDR mem_addr;
0729219d
DJ
702 int bad_attach;
703 int pid;
45b7b345 704 char *arg_end;
c906108c 705
dd24457d
DJ
706 if (argc >= 2 && strcmp (argv[1], "--version") == 0)
707 {
708 gdbserver_version ();
709 exit (0);
710 }
711
712 if (argc >= 2 && strcmp (argv[1], "--help") == 0)
713 {
714 gdbserver_usage ();
715 exit (0);
716 }
717
c5aa993b 718 if (setjmp (toplevel))
c906108c 719 {
c5aa993b
JM
720 fprintf (stderr, "Exiting\n");
721 exit (1);
c906108c
SS
722 }
723
0729219d
DJ
724 bad_attach = 0;
725 pid = 0;
726 attached = 0;
45b7b345
DJ
727 if (argc >= 3 && strcmp (argv[2], "--attach") == 0)
728 {
729 if (argc == 4
730 && argv[3] != '\0'
731 && (pid = strtoul (argv[3], &arg_end, 10)) != 0
732 && *arg_end == '\0')
733 {
734 ;
735 }
736 else
737 bad_attach = 1;
738 }
739
740 if (argc < 3 || bad_attach)
dd24457d
DJ
741 {
742 gdbserver_usage ();
743 exit (1);
744 }
c906108c 745
4ce44c66
JM
746 initialize_low ();
747
0a30fbc4 748 own_buf = malloc (PBUFSIZ);
7fb85e41 749 mem_buf = malloc (PBUFSIZ);
0a30fbc4 750
45b7b345
DJ
751 if (pid == 0)
752 {
753 /* Wait till we are at first instruction in program. */
754 signal = start_inferior (&argv[2], &status);
c906108c 755
45b7b345
DJ
756 /* We are now stopped at the first instruction of the target process */
757 }
758 else
759 {
760 switch (attach_inferior (pid, &status, &signal))
761 {
762 case -1:
763 error ("Attaching not supported on this target");
764 break;
765 default:
766 attached = 1;
767 break;
768 }
769 }
c906108c 770
8264bb58
DJ
771 if (setjmp (toplevel))
772 {
773 fprintf (stderr, "Killing inferior\n");
774 kill_inferior ();
775 exit (1);
776 }
777
c906108c
SS
778 while (1)
779 {
780 remote_open (argv[1]);
781
c5aa993b
JM
782 restart:
783 setjmp (toplevel);
01f9e8fa 784 while (1)
c906108c
SS
785 {
786 unsigned char sig;
01f9e8fa
DJ
787 int packet_len;
788 int new_packet_len = -1;
789
790 packet_len = getpkt (own_buf);
791 if (packet_len <= 0)
792 break;
793
c906108c
SS
794 i = 0;
795 ch = own_buf[i++];
796 switch (ch)
797 {
ce3a066d 798 case 'q':
0876f84a 799 handle_query (own_buf, &new_packet_len);
ce3a066d 800 break;
89be2091
DJ
801 case 'Q':
802 handle_general_set (own_buf);
803 break;
b80864fb
DJ
804#ifndef USE_WIN32API
805 /* Skip "detach" support on mingw32, since we don't have
806 waitpid. */
6ad8ae5c
DJ
807 case 'D':
808 fprintf (stderr, "Detaching from inferior\n");
809 detach_inferior ();
810 write_ok (own_buf);
811 putpkt (own_buf);
aa691b87 812 remote_close ();
6ad8ae5c
DJ
813
814 /* If we are attached, then we can exit. Otherwise, we need to
815 hang around doing nothing, until the child is gone. */
816 if (!attached)
817 {
818 int status, ret;
819
820 do {
821 ret = waitpid (signal_pid, &status, 0);
822 if (WIFEXITED (status) || WIFSIGNALED (status))
823 break;
824 } while (ret != -1 || errno != ECHILD);
825 }
826
827 exit (0);
b80864fb 828#endif
6ad8ae5c 829
c906108c 830 case '!':
45b7b345
DJ
831 if (attached == 0)
832 {
833 extended_protocol = 1;
834 prepare_resume_reply (own_buf, status, signal);
835 }
836 else
837 {
838 /* We can not use the extended protocol if we are
839 attached, because we can not restart the running
840 program. So return unrecognized. */
841 own_buf[0] = '\0';
842 }
c906108c
SS
843 break;
844 case '?':
845 prepare_resume_reply (own_buf, status, signal);
846 break;
847 case 'H':
a06660f7 848 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
c906108c 849 {
a06660f7
DJ
850 unsigned long gdb_id, thread_id;
851
852 gdb_id = strtoul (&own_buf[2], NULL, 16);
853 thread_id = gdb_id_to_thread_id (gdb_id);
854 if (thread_id == 0)
855 {
856 write_enn (own_buf);
857 break;
858 }
859
860 if (own_buf[1] == 'g')
861 {
862 general_thread = thread_id;
863 set_desired_inferior (1);
864 }
865 else if (own_buf[1] == 'c')
866 cont_thread = thread_id;
867 else if (own_buf[1] == 's')
868 step_thread = thread_id;
869
0d62e5e8 870 write_ok (own_buf);
a06660f7
DJ
871 }
872 else
873 {
c906108c
SS
874 /* Silently ignore it so that gdb can extend the protocol
875 without compatibility headaches. */
876 own_buf[0] = '\0';
c906108c
SS
877 }
878 break;
879 case 'g':
0d62e5e8 880 set_desired_inferior (1);
0a30fbc4 881 registers_to_string (own_buf);
c906108c
SS
882 break;
883 case 'G':
0d62e5e8 884 set_desired_inferior (1);
0a30fbc4 885 registers_from_string (&own_buf[1]);
c906108c
SS
886 write_ok (own_buf);
887 break;
888 case 'm':
889 decode_m_packet (&own_buf[1], &mem_addr, &len);
c3e735a6
DJ
890 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
891 convert_int_to_ascii (mem_buf, own_buf, len);
892 else
893 write_enn (own_buf);
c906108c
SS
894 break;
895 case 'M':
896 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
897 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
898 write_ok (own_buf);
899 else
900 write_enn (own_buf);
901 break;
01f9e8fa
DJ
902 case 'X':
903 if (decode_X_packet (&own_buf[1], packet_len - 1,
904 &mem_addr, &len, mem_buf) < 0
905 || write_inferior_memory (mem_addr, mem_buf, len) != 0)
906 write_enn (own_buf);
907 else
908 write_ok (own_buf);
909 break;
c906108c
SS
910 case 'C':
911 convert_ascii_to_int (own_buf + 1, &sig, 1);
0e98d0a7
DJ
912 if (target_signal_to_host_p (sig))
913 signal = target_signal_to_host (sig);
914 else
915 signal = 0;
0d62e5e8 916 set_desired_inferior (0);
0e98d0a7 917 myresume (0, signal);
0d62e5e8 918 signal = mywait (&status, 1);
c906108c
SS
919 prepare_resume_reply (own_buf, status, signal);
920 break;
921 case 'S':
922 convert_ascii_to_int (own_buf + 1, &sig, 1);
0e98d0a7
DJ
923 if (target_signal_to_host_p (sig))
924 signal = target_signal_to_host (sig);
925 else
926 signal = 0;
0d62e5e8 927 set_desired_inferior (0);
0e98d0a7 928 myresume (1, signal);
0d62e5e8 929 signal = mywait (&status, 1);
c906108c
SS
930 prepare_resume_reply (own_buf, status, signal);
931 break;
932 case 'c':
0d62e5e8 933 set_desired_inferior (0);
c906108c 934 myresume (0, 0);
0d62e5e8 935 signal = mywait (&status, 1);
c906108c
SS
936 prepare_resume_reply (own_buf, status, signal);
937 break;
938 case 's':
0d62e5e8 939 set_desired_inferior (0);
c906108c 940 myresume (1, 0);
0d62e5e8 941 signal = mywait (&status, 1);
c906108c
SS
942 prepare_resume_reply (own_buf, status, signal);
943 break;
e013ee27
OF
944 case 'Z':
945 {
946 char *lenptr;
947 char *dataptr;
948 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
949 int len = strtol (lenptr + 1, &dataptr, 16);
950 char type = own_buf[1];
951
952 if (the_target->insert_watchpoint == NULL
953 || (type < '2' || type > '4'))
954 {
955 /* No watchpoint support or not a watchpoint command;
956 unrecognized either way. */
957 own_buf[0] = '\0';
958 }
959 else
960 {
961 int res;
962
963 res = (*the_target->insert_watchpoint) (type, addr, len);
964 if (res == 0)
965 write_ok (own_buf);
966 else if (res == 1)
967 /* Unsupported. */
968 own_buf[0] = '\0';
969 else
970 write_enn (own_buf);
971 }
972 break;
973 }
974 case 'z':
975 {
976 char *lenptr;
977 char *dataptr;
978 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
979 int len = strtol (lenptr + 1, &dataptr, 16);
980 char type = own_buf[1];
981
982 if (the_target->remove_watchpoint == NULL
983 || (type < '2' || type > '4'))
984 {
985 /* No watchpoint support or not a watchpoint command;
986 unrecognized either way. */
987 own_buf[0] = '\0';
988 }
989 else
990 {
991 int res;
992
993 res = (*the_target->remove_watchpoint) (type, addr, len);
994 if (res == 0)
995 write_ok (own_buf);
996 else if (res == 1)
997 /* Unsupported. */
998 own_buf[0] = '\0';
999 else
1000 write_enn (own_buf);
1001 }
1002 break;
1003 }
c906108c
SS
1004 case 'k':
1005 fprintf (stderr, "Killing inferior\n");
1006 kill_inferior ();
1007 /* When using the extended protocol, we start up a new
c5aa993b 1008 debugging session. The traditional protocol will
c906108c
SS
1009 exit instead. */
1010 if (extended_protocol)
1011 {
1012 write_ok (own_buf);
1013 fprintf (stderr, "GDBserver restarting\n");
1014
1015 /* Wait till we are at 1st instruction in prog. */
1016 signal = start_inferior (&argv[2], &status);
1017 goto restart;
1018 break;
1019 }
1020 else
1021 {
1022 exit (0);
1023 break;
1024 }
1025 case 'T':
a06660f7
DJ
1026 {
1027 unsigned long gdb_id, thread_id;
1028
1029 gdb_id = strtoul (&own_buf[1], NULL, 16);
1030 thread_id = gdb_id_to_thread_id (gdb_id);
1031 if (thread_id == 0)
1032 {
1033 write_enn (own_buf);
1034 break;
1035 }
1036
1037 if (mythread_alive (thread_id))
1038 write_ok (own_buf);
1039 else
1040 write_enn (own_buf);
1041 }
c906108c
SS
1042 break;
1043 case 'R':
1044 /* Restarting the inferior is only supported in the
c5aa993b 1045 extended protocol. */
c906108c
SS
1046 if (extended_protocol)
1047 {
1048 kill_inferior ();
1049 write_ok (own_buf);
1050 fprintf (stderr, "GDBserver restarting\n");
1051
1052 /* Wait till we are at 1st instruction in prog. */
1053 signal = start_inferior (&argv[2], &status);
1054 goto restart;
1055 break;
1056 }
1057 else
1058 {
1059 /* It is a request we don't understand. Respond with an
1060 empty packet so that gdb knows that we don't support this
1061 request. */
1062 own_buf[0] = '\0';
1063 break;
1064 }
64386c31
DJ
1065 case 'v':
1066 /* Extended (long) request. */
1067 handle_v_requests (own_buf, &status, &signal);
1068 break;
c906108c
SS
1069 default:
1070 /* It is a request we don't understand. Respond with an
c5aa993b
JM
1071 empty packet so that gdb knows that we don't support this
1072 request. */
c906108c
SS
1073 own_buf[0] = '\0';
1074 break;
1075 }
1076
01f9e8fa
DJ
1077 if (new_packet_len != -1)
1078 putpkt_binary (own_buf, new_packet_len);
1079 else
1080 putpkt (own_buf);
c906108c
SS
1081
1082 if (status == 'W')
1083 fprintf (stderr,
3a7fb99b 1084 "\nChild exited with status %d\n", signal);
c906108c 1085 if (status == 'X')
b80864fb
DJ
1086 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
1087 target_signal_to_host (signal),
1088 target_signal_to_name (signal));
c906108c
SS
1089 if (status == 'W' || status == 'X')
1090 {
1091 if (extended_protocol)
1092 {
1093 fprintf (stderr, "Killing inferior\n");
1094 kill_inferior ();
1095 write_ok (own_buf);
1096 fprintf (stderr, "GDBserver restarting\n");
1097
1098 /* Wait till we are at 1st instruction in prog. */
1099 signal = start_inferior (&argv[2], &status);
1100 goto restart;
1101 break;
1102 }
1103 else
1104 {
1105 fprintf (stderr, "GDBserver exiting\n");
1106 exit (0);
1107 }
1108 }
1109 }
1110
1111 /* We come here when getpkt fails.
1112
c5aa993b
JM
1113 For the extended remote protocol we exit (and this is the only
1114 way we gracefully exit!).
c906108c 1115
c5aa993b
JM
1116 For the traditional remote protocol close the connection,
1117 and re-open it at the top of the loop. */
c906108c
SS
1118 if (extended_protocol)
1119 {
1120 remote_close ();
1121 exit (0);
1122 }
1123 else
1124 {
45b7b345
DJ
1125 fprintf (stderr, "Remote side has terminated connection. "
1126 "GDBserver will reopen the connection.\n");
c906108c
SS
1127 remote_close ();
1128 }
1129 }
1130}
This page took 0.696126 seconds and 4 git commands to generate.