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