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