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