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