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