* acconfig.h: Remove.
[deliverable/binutils-gdb.git] / gdb / gdbserver / server.c
CommitLineData
c906108c 1/* Main code for remote server for GDB.
a1928bad
DJ
2 Copyright 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003, 2004,
3 2005
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
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, 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;
a1928bad 106 sprintf (own_buf, "m%lx", thread_ptr->id);
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 {
a1928bad 115 sprintf (own_buf, "m%lx", thread_ptr->id);
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 {
a1928bad 221 resume_info[i].thread = strtoul (p + 1, &q, 16);
64386c31
DJ
222 if (p == q)
223 goto err;
224 p = q;
225 if (p[0] != ';' && p[0] != 0)
226 goto err;
227
228 i++;
229 }
230 }
231
232 resume_info[i] = default_action;
233
234 /* Still used in occasional places in the backend. */
235 if (n == 1 && resume_info[0].thread != -1)
236 cont_thread = resume_info[0].thread;
237 else
238 cont_thread = -1;
dc3f8883 239 set_desired_inferior (0);
64386c31
DJ
240
241 (*the_target->resume) (resume_info);
242
243 free (resume_info);
244
245 *signal = mywait (status, 1);
246 prepare_resume_reply (own_buf, *status, *signal);
247 return;
248
249err:
250 /* No other way to report an error... */
251 strcpy (own_buf, "");
252 free (resume_info);
253 return;
254}
255
256/* Handle all of the extended 'v' packets. */
257void
fc620387 258handle_v_requests (char *own_buf, char *status, int *signal)
64386c31
DJ
259{
260 if (strncmp (own_buf, "vCont;", 6) == 0)
261 {
262 handle_v_cont (own_buf, status, signal);
263 return;
264 }
265
266 if (strncmp (own_buf, "vCont?", 6) == 0)
267 {
268 strcpy (own_buf, "vCont;c;C;s;S");
269 return;
270 }
271
272 /* Otherwise we didn't know what packet it was. Say we didn't
273 understand it. */
274 own_buf[0] = 0;
275 return;
276}
277
278void
279myresume (int step, int sig)
280{
281 struct thread_resume resume_info[2];
282 int n = 0;
283
284 if (step || sig || cont_thread > 0)
285 {
286 resume_info[0].thread
287 = ((struct inferior_list_entry *) current_inferior)->id;
288 resume_info[0].step = step;
289 resume_info[0].sig = sig;
290 resume_info[0].leave_stopped = 0;
291 n++;
292 }
293 resume_info[n].thread = -1;
294 resume_info[n].step = 0;
295 resume_info[n].sig = 0;
296 resume_info[n].leave_stopped = (cont_thread > 0);
297
298 (*the_target->resume) (resume_info);
299}
300
0729219d 301static int attached;
c906108c 302
0bc68c49
DJ
303static void
304gdbserver_usage (void)
305{
306 error ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
307 "\tgdbserver COMM --attach PID\n"
308 "\n"
309 "COMM may either be a tty device (for serial debugging), or \n"
310 "HOST:PORT to listen for a TCP connection.\n");
311}
312
c906108c 313int
da85418c 314main (int argc, char *argv[])
c906108c 315{
f450004a
DJ
316 char ch, status, *own_buf;
317 unsigned char mem_buf[2000];
c906108c 318 int i = 0;
fc620387 319 int signal;
c906108c
SS
320 unsigned int len;
321 CORE_ADDR mem_addr;
0729219d
DJ
322 int bad_attach;
323 int pid;
45b7b345 324 char *arg_end;
c906108c 325
c5aa993b 326 if (setjmp (toplevel))
c906108c 327 {
c5aa993b
JM
328 fprintf (stderr, "Exiting\n");
329 exit (1);
c906108c
SS
330 }
331
0729219d
DJ
332 bad_attach = 0;
333 pid = 0;
334 attached = 0;
45b7b345
DJ
335 if (argc >= 3 && strcmp (argv[2], "--attach") == 0)
336 {
337 if (argc == 4
338 && argv[3] != '\0'
339 && (pid = strtoul (argv[3], &arg_end, 10)) != 0
340 && *arg_end == '\0')
341 {
342 ;
343 }
344 else
345 bad_attach = 1;
346 }
347
348 if (argc < 3 || bad_attach)
0bc68c49 349 gdbserver_usage();
c906108c 350
4ce44c66
JM
351 initialize_low ();
352
0a30fbc4
DJ
353 own_buf = malloc (PBUFSIZ);
354
45b7b345
DJ
355 if (pid == 0)
356 {
357 /* Wait till we are at first instruction in program. */
358 signal = start_inferior (&argv[2], &status);
c906108c 359
45b7b345
DJ
360 /* We are now stopped at the first instruction of the target process */
361 }
362 else
363 {
364 switch (attach_inferior (pid, &status, &signal))
365 {
366 case -1:
367 error ("Attaching not supported on this target");
368 break;
369 default:
370 attached = 1;
371 break;
372 }
373 }
c906108c
SS
374
375 while (1)
376 {
377 remote_open (argv[1]);
378
c5aa993b
JM
379 restart:
380 setjmp (toplevel);
c906108c
SS
381 while (getpkt (own_buf) > 0)
382 {
383 unsigned char sig;
384 i = 0;
385 ch = own_buf[i++];
386 switch (ch)
387 {
ce3a066d
DJ
388 case 'q':
389 handle_query (own_buf);
390 break;
c906108c
SS
391 case 'd':
392 remote_debug = !remote_debug;
393 break;
6ad8ae5c
DJ
394 case 'D':
395 fprintf (stderr, "Detaching from inferior\n");
396 detach_inferior ();
397 write_ok (own_buf);
398 putpkt (own_buf);
aa691b87 399 remote_close ();
6ad8ae5c
DJ
400
401 /* If we are attached, then we can exit. Otherwise, we need to
402 hang around doing nothing, until the child is gone. */
403 if (!attached)
404 {
405 int status, ret;
406
407 do {
408 ret = waitpid (signal_pid, &status, 0);
409 if (WIFEXITED (status) || WIFSIGNALED (status))
410 break;
411 } while (ret != -1 || errno != ECHILD);
412 }
413
414 exit (0);
415
c906108c 416 case '!':
45b7b345
DJ
417 if (attached == 0)
418 {
419 extended_protocol = 1;
420 prepare_resume_reply (own_buf, status, signal);
421 }
422 else
423 {
424 /* We can not use the extended protocol if we are
425 attached, because we can not restart the running
426 program. So return unrecognized. */
427 own_buf[0] = '\0';
428 }
c906108c
SS
429 break;
430 case '?':
431 prepare_resume_reply (own_buf, status, signal);
432 break;
433 case 'H':
434 switch (own_buf[1])
435 {
436 case 'g':
a1928bad 437 general_thread = strtoul (&own_buf[2], NULL, 16);
c906108c 438 write_ok (own_buf);
0d62e5e8 439 set_desired_inferior (1);
c906108c
SS
440 break;
441 case 'c':
a1928bad 442 cont_thread = strtoul (&own_buf[2], NULL, 16);
c906108c
SS
443 write_ok (own_buf);
444 break;
0d62e5e8 445 case 's':
a1928bad 446 step_thread = strtoul (&own_buf[2], NULL, 16);
0d62e5e8
DJ
447 write_ok (own_buf);
448 break;
c906108c
SS
449 default:
450 /* Silently ignore it so that gdb can extend the protocol
451 without compatibility headaches. */
452 own_buf[0] = '\0';
453 break;
454 }
455 break;
456 case 'g':
0d62e5e8 457 set_desired_inferior (1);
0a30fbc4 458 registers_to_string (own_buf);
c906108c
SS
459 break;
460 case 'G':
0d62e5e8 461 set_desired_inferior (1);
0a30fbc4 462 registers_from_string (&own_buf[1]);
c906108c
SS
463 write_ok (own_buf);
464 break;
465 case 'm':
466 decode_m_packet (&own_buf[1], &mem_addr, &len);
c3e735a6
DJ
467 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
468 convert_int_to_ascii (mem_buf, own_buf, len);
469 else
470 write_enn (own_buf);
c906108c
SS
471 break;
472 case 'M':
473 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
474 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
475 write_ok (own_buf);
476 else
477 write_enn (own_buf);
478 break;
479 case 'C':
480 convert_ascii_to_int (own_buf + 1, &sig, 1);
0e98d0a7
DJ
481 if (target_signal_to_host_p (sig))
482 signal = target_signal_to_host (sig);
483 else
484 signal = 0;
0d62e5e8 485 set_desired_inferior (0);
0e98d0a7 486 myresume (0, signal);
0d62e5e8 487 signal = mywait (&status, 1);
c906108c
SS
488 prepare_resume_reply (own_buf, status, signal);
489 break;
490 case 'S':
491 convert_ascii_to_int (own_buf + 1, &sig, 1);
0e98d0a7
DJ
492 if (target_signal_to_host_p (sig))
493 signal = target_signal_to_host (sig);
494 else
495 signal = 0;
0d62e5e8 496 set_desired_inferior (0);
0e98d0a7 497 myresume (1, signal);
0d62e5e8 498 signal = mywait (&status, 1);
c906108c
SS
499 prepare_resume_reply (own_buf, status, signal);
500 break;
501 case 'c':
0d62e5e8 502 set_desired_inferior (0);
c906108c 503 myresume (0, 0);
0d62e5e8 504 signal = mywait (&status, 1);
c906108c
SS
505 prepare_resume_reply (own_buf, status, signal);
506 break;
507 case 's':
0d62e5e8 508 set_desired_inferior (0);
c906108c 509 myresume (1, 0);
0d62e5e8 510 signal = mywait (&status, 1);
c906108c
SS
511 prepare_resume_reply (own_buf, status, signal);
512 break;
e013ee27
OF
513 case 'Z':
514 {
515 char *lenptr;
516 char *dataptr;
517 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
518 int len = strtol (lenptr + 1, &dataptr, 16);
519 char type = own_buf[1];
520
521 if (the_target->insert_watchpoint == NULL
522 || (type < '2' || type > '4'))
523 {
524 /* No watchpoint support or not a watchpoint command;
525 unrecognized either way. */
526 own_buf[0] = '\0';
527 }
528 else
529 {
530 int res;
531
532 res = (*the_target->insert_watchpoint) (type, addr, len);
533 if (res == 0)
534 write_ok (own_buf);
535 else if (res == 1)
536 /* Unsupported. */
537 own_buf[0] = '\0';
538 else
539 write_enn (own_buf);
540 }
541 break;
542 }
543 case 'z':
544 {
545 char *lenptr;
546 char *dataptr;
547 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
548 int len = strtol (lenptr + 1, &dataptr, 16);
549 char type = own_buf[1];
550
551 if (the_target->remove_watchpoint == NULL
552 || (type < '2' || type > '4'))
553 {
554 /* No watchpoint support or not a watchpoint command;
555 unrecognized either way. */
556 own_buf[0] = '\0';
557 }
558 else
559 {
560 int res;
561
562 res = (*the_target->remove_watchpoint) (type, addr, len);
563 if (res == 0)
564 write_ok (own_buf);
565 else if (res == 1)
566 /* Unsupported. */
567 own_buf[0] = '\0';
568 else
569 write_enn (own_buf);
570 }
571 break;
572 }
c906108c
SS
573 case 'k':
574 fprintf (stderr, "Killing inferior\n");
575 kill_inferior ();
576 /* When using the extended protocol, we start up a new
c5aa993b 577 debugging session. The traditional protocol will
c906108c
SS
578 exit instead. */
579 if (extended_protocol)
580 {
581 write_ok (own_buf);
582 fprintf (stderr, "GDBserver restarting\n");
583
584 /* Wait till we are at 1st instruction in prog. */
585 signal = start_inferior (&argv[2], &status);
586 goto restart;
587 break;
588 }
589 else
590 {
591 exit (0);
592 break;
593 }
594 case 'T':
a1928bad 595 if (mythread_alive (strtoul (&own_buf[1], NULL, 16)))
c906108c
SS
596 write_ok (own_buf);
597 else
598 write_enn (own_buf);
599 break;
600 case 'R':
601 /* Restarting the inferior is only supported in the
c5aa993b 602 extended protocol. */
c906108c
SS
603 if (extended_protocol)
604 {
605 kill_inferior ();
606 write_ok (own_buf);
607 fprintf (stderr, "GDBserver restarting\n");
608
609 /* Wait till we are at 1st instruction in prog. */
610 signal = start_inferior (&argv[2], &status);
611 goto restart;
612 break;
613 }
614 else
615 {
616 /* It is a request we don't understand. Respond with an
617 empty packet so that gdb knows that we don't support this
618 request. */
619 own_buf[0] = '\0';
620 break;
621 }
64386c31
DJ
622 case 'v':
623 /* Extended (long) request. */
624 handle_v_requests (own_buf, &status, &signal);
625 break;
c906108c
SS
626 default:
627 /* It is a request we don't understand. Respond with an
c5aa993b
JM
628 empty packet so that gdb knows that we don't support this
629 request. */
c906108c
SS
630 own_buf[0] = '\0';
631 break;
632 }
633
634 putpkt (own_buf);
635
636 if (status == 'W')
637 fprintf (stderr,
3a7fb99b 638 "\nChild exited with status %d\n", signal);
c906108c 639 if (status == 'X')
3a7fb99b
DJ
640 fprintf (stderr, "\nChild terminated with signal = 0x%x\n",
641 signal);
c906108c
SS
642 if (status == 'W' || status == 'X')
643 {
644 if (extended_protocol)
645 {
646 fprintf (stderr, "Killing inferior\n");
647 kill_inferior ();
648 write_ok (own_buf);
649 fprintf (stderr, "GDBserver restarting\n");
650
651 /* Wait till we are at 1st instruction in prog. */
652 signal = start_inferior (&argv[2], &status);
653 goto restart;
654 break;
655 }
656 else
657 {
658 fprintf (stderr, "GDBserver exiting\n");
659 exit (0);
660 }
661 }
662 }
663
664 /* We come here when getpkt fails.
665
c5aa993b
JM
666 For the extended remote protocol we exit (and this is the only
667 way we gracefully exit!).
c906108c 668
c5aa993b
JM
669 For the traditional remote protocol close the connection,
670 and re-open it at the top of the loop. */
c906108c
SS
671 if (extended_protocol)
672 {
673 remote_close ();
674 exit (0);
675 }
676 else
677 {
45b7b345
DJ
678 fprintf (stderr, "Remote side has terminated connection. "
679 "GDBserver will reopen the connection.\n");
c906108c
SS
680 remote_close ();
681 }
682 }
683}
This page took 0.425479 seconds and 4 git commands to generate.