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