ia64/xen-unstable

view tools/debugger/gdb/gdb-6.2.1-xen-sparse/gdb/gdbserver/server.c @ 9780:83ba928544c4

The Xen Hypervisor currently operates a bit differently when the
guest is being debugged. The differences are handling of int3 exception
and missed pit timer injections. The Xen hypervisor should get back to
the normal mode when the gdb connection is closed. With the attached
patch gdbserver properly detaches from the guest when the gdb detaches
or quits.

Signed-Off-By: Nitin A Kamble <nitin.a.kamble@intel.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed Apr 19 22:19:41 2006 +0100 (2006-04-19)
parents e08dcff87bb2
children
line source
1 /* Main code for remote server for GDB.
2 Copyright 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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. */
22 #include "server.h"
23 #include "linux-low.h"
24 #include <unistd.h>
25 #include <signal.h>
26 #include <sys/wait.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
31 int cont_thread;
32 int general_thread;
33 int step_thread;
34 int thread_from_wait;
35 int old_thread_from_wait;
36 int extended_protocol;
37 int server_waiting;
38 int isfile = 0;
40 jmp_buf toplevel;
42 /* The PID of the originally created or attached inferior. Used to
43 send signals to the process when GDB sends us an asynchronous interrupt
44 (user hitting Control-C in the client), and to wait for the child to exit
45 when no longer debugging it. */
47 int signal_pid;
49 static unsigned char
50 start_inferior (char *argv[], char *statusptr)
51 {
52 signal (SIGTTOU, SIG_DFL);
53 signal (SIGTTIN, SIG_DFL);
55 signal_pid = create_inferior (argv[0], argv);
57 fprintf (stderr, "Process %s created; pid = %d\n", argv[0],
58 signal_pid);
60 signal (SIGTTOU, SIG_IGN);
61 signal (SIGTTIN, SIG_IGN);
62 tcsetpgrp (fileno (stderr), signal_pid);
64 /* Wait till we are at 1st instruction in program, return signal number. */
65 return mywait (statusptr, 0);
66 }
68 static int
69 attach_inferior (int pid, char *statusptr, unsigned char *sigptr)
70 {
71 /* myattach should return -1 if attaching is unsupported,
72 0 if it succeeded, and call error() otherwise. */
74 if (myattach (pid) != 0)
75 return -1;
77 fprintf (stderr, "Attached; pid = %d\n", pid);
79 /* FIXME - It may be that we should get the SIGNAL_PID from the
80 attach function, so that it can be the main thread instead of
81 whichever we were told to attach to. */
82 signal_pid = pid;
84 *sigptr = mywait (statusptr, 0);
86 return 0;
87 }
89 extern int remote_debug;
91 /* Handle all of the extended 'q' packets. */
92 void
93 handle_query (char *own_buf)
94 {
95 static struct inferior_list_entry *thread_ptr;
97 if (strcmp ("qSymbol::", own_buf) == 0)
98 {
99 if (the_target->look_up_symbols != NULL)
100 (*the_target->look_up_symbols) ();
102 strcpy (own_buf, "OK");
103 return;
104 }
105 if (strcmp ("qC", own_buf) == 0)
106 {
107 struct process_info *process;
108 if (current_inferior == NULL)
109 return;
110 process = get_thread_process(current_inferior);
111 sprintf(own_buf, "QC%x", process->thread_known ? process->tid : 0);
113 }
114 if (strcmp ("qfThreadInfo", own_buf) == 0)
115 {
116 thread_ptr = all_threads.head;
117 sprintf (own_buf, "m%x", thread_ptr->id);
118 thread_ptr = thread_ptr->next;
119 return;
120 }
122 if (strcmp ("qsThreadInfo", own_buf) == 0)
123 {
124 if (thread_ptr != NULL)
125 {
126 sprintf (own_buf, "m%x", thread_ptr->id);
127 thread_ptr = thread_ptr->next;
128 return;
129 }
130 else
131 {
132 sprintf (own_buf, "l");
133 return;
134 }
135 }
137 if (the_target->read_auxv != NULL
138 && strncmp ("qPart:auxv:read::", own_buf, 17) == 0)
139 {
140 char data[(PBUFSIZ - 1) / 2];
141 CORE_ADDR ofs;
142 unsigned int len;
143 int n;
144 decode_m_packet (&own_buf[17], &ofs, &len); /* "OFS,LEN" */
145 if (len > sizeof data)
146 len = sizeof data;
147 n = (*the_target->read_auxv) (ofs, data, len);
148 if (n == 0)
149 write_ok (own_buf);
150 else if (n < 0)
151 write_enn (own_buf);
152 else
153 convert_int_to_ascii (data, own_buf, n);
154 return;
155 }
157 /* Otherwise we didn't know what packet it was. Say we didn't
158 understand it. */
159 own_buf[0] = 0;
160 }
162 /* Parse vCont packets. */
163 void
164 handle_v_cont (char *own_buf, char *status, unsigned char *signal)
165 {
166 char *p, *q;
167 int n = 0, i = 0;
168 struct thread_resume *resume_info, default_action;
170 /* Count the number of semicolons in the packet. There should be one
171 for every action. */
172 p = &own_buf[5];
173 while (p)
174 {
175 n++;
176 p++;
177 p = strchr (p, ';');
178 }
179 /* Allocate room for one extra action, for the default remain-stopped
180 behavior; if no default action is in the list, we'll need the extra
181 slot. */
182 resume_info = malloc ((n + 1) * sizeof (resume_info[0]));
184 default_action.thread = -1;
185 default_action.leave_stopped = 1;
186 default_action.step = 0;
187 default_action.sig = 0;
189 p = &own_buf[5];
190 i = 0;
191 while (*p)
192 {
193 p++;
195 resume_info[i].leave_stopped = 0;
197 if (p[0] == 's' || p[0] == 'S')
198 resume_info[i].step = 1;
199 else if (p[0] == 'c' || p[0] == 'C')
200 resume_info[i].step = 0;
201 else
202 goto err;
204 if (p[0] == 'S' || p[0] == 'C')
205 {
206 int sig;
207 sig = strtol (p + 1, &q, 16);
208 if (p == q)
209 goto err;
210 p = q;
212 if (!target_signal_to_host_p (sig))
213 goto err;
214 resume_info[i].sig = target_signal_to_host (sig);
215 }
216 else
217 {
218 resume_info[i].sig = 0;
219 p = p + 1;
220 }
222 if (p[0] == 0)
223 {
224 resume_info[i].thread = -1;
225 default_action = resume_info[i];
227 /* Note: we don't increment i here, we'll overwrite this entry
228 the next time through. */
229 }
230 else if (p[0] == ':')
231 {
232 resume_info[i].thread = strtol (p + 1, &q, 16);
233 if (p == q)
234 goto err;
235 p = q;
236 if (p[0] != ';' && p[0] != 0)
237 goto err;
239 i++;
240 }
241 }
243 resume_info[i] = default_action;
245 /* Still used in occasional places in the backend. */
246 if (n == 1 && resume_info[0].thread != -1)
247 cont_thread = resume_info[0].thread;
248 else
249 cont_thread = -1;
250 set_desired_inferior (0);
252 (*the_target->resume) (resume_info);
254 free (resume_info);
256 *signal = mywait (status, 1);
257 prepare_resume_reply (own_buf, *status, *signal);
258 return;
260 err:
261 /* No other way to report an error... */
262 strcpy (own_buf, "");
263 free (resume_info);
264 return;
265 }
267 /* Handle all of the extended 'v' packets. */
268 void
269 handle_v_requests (char *own_buf, char *status, unsigned char *signal)
270 {
271 if (strncmp (own_buf, "vCont;", 6) == 0)
272 {
273 handle_v_cont (own_buf, status, signal);
274 return;
275 }
277 if (strncmp (own_buf, "vCont?", 6) == 0)
278 {
279 strcpy (own_buf, "vCont;c;C;s;S");
280 return;
281 }
283 /* Otherwise we didn't know what packet it was. Say we didn't
284 understand it. */
285 own_buf[0] = 0;
286 return;
287 }
289 void
290 handle_breakpoint_requests (char *own_buf, char *status, unsigned char *signal)
291 {
292 /* Currently we only support software breakpoints */
293 switch (own_buf[1]) {
294 case '0': /* software breakpoint, int3 based */
295 own_buf[0] = '\0';
296 break;
297 case '1': /* hardware breakpoint */
298 default:
299 write_enn (own_buf);
300 break;
301 }
302 }
304 void
305 myresume (int step, int sig)
306 {
307 struct thread_resume resume_info[2];
308 int n = 0;
310 if (step || sig || cont_thread > 0)
311 {
312 resume_info[0].thread
313 = ((struct inferior_list_entry *) current_inferior)->id;
314 resume_info[0].step = step;
315 resume_info[0].sig = sig;
316 resume_info[0].leave_stopped = 0;
317 n++;
318 }
319 resume_info[n].thread = -1;
320 resume_info[n].step = 0;
321 resume_info[n].sig = 0;
322 resume_info[n].leave_stopped = (cont_thread > 0);
324 (*the_target->resume) (resume_info);
325 }
327 static int attached;
329 static void
330 gdbserver_usage (void)
331 {
332 error ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
333 "\tgdbserver COMM --attach PID\n"
334 "\tgdbserver COMM --file COREFILE\n"
335 "\n"
336 "COMM may either be a tty device (for serial debugging), or \n"
337 "HOST:PORT to listen for a TCP connection.\n");
338 }
340 extern control_c_pressed_flag;
341 #include <signal.h>
343 void ctrl_c_handler(int signo)
344 {
345 printf("Ctrl-C pressed: Quit from the attached gdb first\n");
346 control_c_pressed_flag = 1;
347 }
349 struct sigaction ctrl_c_sigaction = { .sa_handler = ctrl_c_handler };
350 struct sigaction old_sigaction;
352 int
353 main (int argc, char *argv[])
354 {
355 char ch, status, *own_buf, mem_buf[2000];
356 int i = 0;
357 unsigned char signal;
358 unsigned int len;
359 CORE_ADDR mem_addr;
360 int bad_attach;
361 int pid;
362 char *arg_end;
364 if (setjmp (toplevel))
365 {
366 fprintf (stderr, "Exiting\n");
367 exit (1);
368 }
370 bad_attach = 0;
371 pid = 0;
372 attached = 0;
373 if (argc >= 3 && strcmp (argv[2], "--attach") == 0)
374 {
375 if (argc == 4
376 && argv[3] != '\0'
377 && (pid = strtoul (argv[3], &arg_end, 10)) != 0
378 && *arg_end == '\0')
379 {
380 ;
381 }
382 else
383 bad_attach = 1;
384 }
385 else if (argc >= 3 && strcmp (argv[2], "--file") == 0)
386 {
387 if (argc == 4
388 && argv[3] != '\0')
389 {
390 if ((pid = open(argv[3], O_RDONLY)) <= 0)
391 bad_attach = 1;
392 else
393 isfile = 1;
394 }
395 else
396 bad_attach = 1;
397 }
399 if (argc < 3 || bad_attach)
400 gdbserver_usage();
402 initialize_low ();
404 own_buf = malloc (PBUFSIZ);
406 if (pid == 0)
407 {
408 /* Wait till we are at first instruction in program. */
409 signal = start_inferior (&argv[2], &status);
411 /* We are now stopped at the first instruction of the target process */
412 }
413 else
414 {
415 switch (attach_inferior (pid, &status, &signal))
416 {
417 case -1:
418 error ("Attaching not supported on this target");
419 break;
420 default:
421 attached = 1;
422 break;
423 }
424 }
427 while (1)
428 {
429 remote_open (argv[1]);
430 sigaction(SIGINT, &ctrl_c_sigaction, &old_sigaction);
432 restart:
433 setjmp (toplevel);
434 while (getpkt (own_buf) > 0)
435 {
436 unsigned char sig;
437 i = 0;
438 ch = own_buf[i++];
439 switch (ch)
440 {
441 case 'q':
442 handle_query (own_buf);
443 break;
444 case 'd':
445 remote_debug = !remote_debug;
446 break;
447 case 'D':
448 fprintf (stderr, "Detaching from inferior\n");
449 detach_inferior ();
450 write_ok (own_buf);
451 putpkt (own_buf);
452 remote_close ();
454 /* If we are attached, then we can exit. Otherwise, we need to
455 hang around doing nothing, until the child is gone. */
456 if (!attached)
457 {
458 int status, ret;
460 do {
461 ret = waitpid (signal_pid, &status, 0);
462 if (WIFEXITED (status) || WIFSIGNALED (status))
463 break;
464 } while (ret != -1 || errno != ECHILD);
465 }
467 exit (0);
469 case '!':
470 if (attached == 0)
471 {
472 extended_protocol = 1;
473 prepare_resume_reply (own_buf, status, signal);
474 }
475 else
476 {
477 /* We can not use the extended protocol if we are
478 attached, because we can not restart the running
479 program. So return unrecognized. */
480 own_buf[0] = '\0';
481 }
482 break;
483 case '?':
484 prepare_resume_reply (own_buf, status, signal);
485 break;
486 case 'H':
487 switch (own_buf[1])
488 {
489 case 'g':
490 general_thread = strtol (&own_buf[2], NULL, 16);
491 write_ok (own_buf);
492 set_desired_inferior (1);
493 break;
494 case 'c':
495 cont_thread = strtol (&own_buf[2], NULL, 16);
496 write_ok (own_buf);
497 break;
498 case 's':
499 step_thread = strtol (&own_buf[2], NULL, 16);
500 write_ok (own_buf);
501 break;
502 default:
503 /* Silently ignore it so that gdb can extend the protocol
504 without compatibility headaches. */
505 own_buf[0] = '\0';
506 break;
507 }
508 break;
509 case 'g':
510 set_desired_inferior (1);
511 registers_to_string (own_buf);
512 break;
513 case 'G':
514 set_desired_inferior (1);
515 registers_from_string (&own_buf[1]);
516 write_ok (own_buf);
517 break;
518 case 'm':
519 decode_m_packet (&own_buf[1], &mem_addr, &len);
520 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
521 convert_int_to_ascii (mem_buf, own_buf, len);
522 else
523 write_enn (own_buf);
524 break;
525 case 'M':
526 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
527 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
528 write_ok (own_buf);
529 else
530 write_enn (own_buf);
531 break;
532 case 'C':
533 convert_ascii_to_int (own_buf + 1, &sig, 1);
534 if (target_signal_to_host_p (sig))
535 signal = target_signal_to_host (sig);
536 else
537 signal = 0;
538 set_desired_inferior (0);
539 myresume (0, signal);
540 signal = mywait (&status, 1);
541 prepare_resume_reply (own_buf, status, signal);
542 break;
543 case 'S':
544 convert_ascii_to_int (own_buf + 1, &sig, 1);
545 if (target_signal_to_host_p (sig))
546 signal = target_signal_to_host (sig);
547 else
548 signal = 0;
549 set_desired_inferior (0);
550 myresume (1, signal);
551 signal = mywait (&status, 1);
552 prepare_resume_reply (own_buf, status, signal);
553 break;
554 case 'c':
555 set_desired_inferior (0);
556 myresume (0, 0);
557 signal = mywait (&status, 1);
558 prepare_resume_reply (own_buf, status, signal);
559 break;
560 case 's':
561 set_desired_inferior (0);
562 myresume (1, 0);
563 signal = mywait (&status, 1);
564 prepare_resume_reply (own_buf, status, signal);
565 break;
566 case 'k':
567 fprintf (stderr, "Killing inferior\n");
568 kill_inferior ();
569 /* When using the extended protocol, we start up a new
570 debugging session. The traditional protocol will
571 exit instead. */
572 if (extended_protocol)
573 {
574 write_ok (own_buf);
575 fprintf (stderr, "GDBserver restarting\n");
577 /* Wait till we are at 1st instruction in prog. */
578 signal = start_inferior (&argv[2], &status);
579 goto restart;
580 break;
581 }
582 else
583 {
584 exit (0);
585 break;
586 }
587 case 'T':
588 if (mythread_alive (strtol (&own_buf[1], NULL, 16)))
589 write_ok (own_buf);
590 else
591 write_enn (own_buf);
592 break;
593 case 'R':
594 /* Restarting the inferior is only supported in the
595 extended protocol. */
596 if (extended_protocol)
597 {
598 kill_inferior ();
599 write_ok (own_buf);
600 fprintf (stderr, "GDBserver restarting\n");
602 /* Wait till we are at 1st instruction in prog. */
603 signal = start_inferior (&argv[2], &status);
604 goto restart;
605 break;
606 }
607 else
608 {
609 /* It is a request we don't understand. Respond with an
610 empty packet so that gdb knows that we don't support this
611 request. */
612 own_buf[0] = '\0';
613 break;
614 }
615 case 'v':
616 /* Extended (long) request. */
617 handle_v_requests (own_buf, &status, &signal);
618 break;
619 case 'Z':
620 handle_breakpoint_requests (own_buf, &status, &signal);
621 break;
622 default:
623 /* It is a request we don't understand. Respond with an
624 empty packet so that gdb knows that we don't support this
625 request. */
626 own_buf[0] = '\0';
627 break;
628 }
630 putpkt (own_buf);
632 if (status == 'W')
633 fprintf (stderr,
634 "\nChild exited with status %d\n", signal);
635 if (status == 'X')
636 fprintf (stderr, "\nChild terminated with signal = 0x%x\n",
637 signal);
638 if (status == 'W' || status == 'X')
639 {
640 if (extended_protocol)
641 {
642 fprintf (stderr, "Killing inferior\n");
643 kill_inferior ();
644 write_ok (own_buf);
645 fprintf (stderr, "GDBserver restarting\n");
647 /* Wait till we are at 1st instruction in prog. */
648 signal = start_inferior (&argv[2], &status);
649 goto restart;
650 break;
651 }
652 else
653 {
654 fprintf (stderr, "GDBserver exiting\n");
655 exit (0);
656 }
657 }
658 }
660 /* We come here when getpkt fails.
662 For the extended remote protocol we exit (and this is the only
663 way we gracefully exit!).
665 For the traditional remote protocol close the connection,
666 and re-open it at the top of the loop. */
667 detach_inferior ();
668 remote_close ();
669 if (extended_protocol)
670 exit (0);
671 else
672 fprintf (stderr, "Remote side has terminated connection. "
673 "GDBserver will reopen the connection.\n");
674 sigaction(SIGINT, &old_sigaction, NULL);
675 }
676 }