1 /* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003, 2004,
4 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
29 unsigned long cont_thread
;
30 unsigned long general_thread
;
31 unsigned long step_thread
;
32 unsigned long thread_from_wait
;
33 unsigned long old_thread_from_wait
;
34 int extended_protocol
;
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. */
44 unsigned long signal_pid
;
47 start_inferior (char *argv
[], char *statusptr
)
49 signal (SIGTTOU
, SIG_DFL
);
50 signal (SIGTTIN
, SIG_DFL
);
52 signal_pid
= create_inferior (argv
[0], argv
);
54 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
57 signal (SIGTTOU
, SIG_IGN
);
58 signal (SIGTTIN
, SIG_IGN
);
59 tcsetpgrp (fileno (stderr
), signal_pid
);
61 /* Wait till we are at 1st instruction in program, return signal number. */
62 return mywait (statusptr
, 0);
66 attach_inferior (int pid
, char *statusptr
, int *sigptr
)
68 /* myattach should return -1 if attaching is unsupported,
69 0 if it succeeded, and call error() otherwise. */
71 if (myattach (pid
) != 0)
74 fprintf (stderr
, "Attached; pid = %d\n", pid
);
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. */
81 *sigptr
= mywait (statusptr
, 0);
83 /* GDB knows to ignore the first SIGSTOP after attaching to a running
84 process using the "attach" command, but this is different; it's
85 just using "target remote". Pretend it's just starting up. */
86 if (*statusptr
== 'T' && *sigptr
== SIGSTOP
)
92 extern int remote_debug
;
94 /* Handle all of the extended 'q' packets. */
96 handle_query (char *own_buf
)
98 static struct inferior_list_entry
*thread_ptr
;
100 if (strcmp ("qSymbol::", own_buf
) == 0)
102 if (the_target
->look_up_symbols
!= NULL
)
103 (*the_target
->look_up_symbols
) ();
105 strcpy (own_buf
, "OK");
109 if (strcmp ("qfThreadInfo", own_buf
) == 0)
111 thread_ptr
= all_threads
.head
;
112 sprintf (own_buf
, "m%x", thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
113 thread_ptr
= thread_ptr
->next
;
117 if (strcmp ("qsThreadInfo", own_buf
) == 0)
119 if (thread_ptr
!= NULL
)
121 sprintf (own_buf
, "m%x", thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
122 thread_ptr
= thread_ptr
->next
;
127 sprintf (own_buf
, "l");
132 if (the_target
->read_offsets
!= NULL
133 && strcmp ("qOffsets", own_buf
) == 0)
135 CORE_ADDR text
, data
;
137 if (the_target
->read_offsets (&text
, &data
))
138 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
139 (long)text
, (long)data
, (long)data
);
146 if (the_target
->read_auxv
!= NULL
147 && strncmp ("qPart:auxv:read::", own_buf
, 17) == 0)
149 unsigned char data
[(PBUFSIZ
- 1) / 2];
153 decode_m_packet (&own_buf
[17], &ofs
, &len
); /* "OFS,LEN" */
154 if (len
> sizeof data
)
156 n
= (*the_target
->read_auxv
) (ofs
, data
, len
);
162 convert_int_to_ascii (data
, own_buf
, n
);
166 /* Otherwise we didn't know what packet it was. Say we didn't
171 /* Parse vCont packets. */
173 handle_v_cont (char *own_buf
, char *status
, int *signal
)
177 struct thread_resume
*resume_info
, default_action
;
179 /* Count the number of semicolons in the packet. There should be one
188 /* Allocate room for one extra action, for the default remain-stopped
189 behavior; if no default action is in the list, we'll need the extra
191 resume_info
= malloc ((n
+ 1) * sizeof (resume_info
[0]));
193 default_action
.thread
= -1;
194 default_action
.leave_stopped
= 1;
195 default_action
.step
= 0;
196 default_action
.sig
= 0;
204 resume_info
[i
].leave_stopped
= 0;
206 if (p
[0] == 's' || p
[0] == 'S')
207 resume_info
[i
].step
= 1;
208 else if (p
[0] == 'c' || p
[0] == 'C')
209 resume_info
[i
].step
= 0;
213 if (p
[0] == 'S' || p
[0] == 'C')
216 sig
= strtol (p
+ 1, &q
, 16);
221 if (!target_signal_to_host_p (sig
))
223 resume_info
[i
].sig
= target_signal_to_host (sig
);
227 resume_info
[i
].sig
= 0;
233 resume_info
[i
].thread
= -1;
234 default_action
= resume_info
[i
];
236 /* Note: we don't increment i here, we'll overwrite this entry
237 the next time through. */
239 else if (p
[0] == ':')
241 unsigned int gdb_id
= strtoul (p
+ 1, &q
, 16);
242 unsigned long thread_id
;
247 if (p
[0] != ';' && p
[0] != 0)
250 thread_id
= gdb_id_to_thread_id (gdb_id
);
252 resume_info
[i
].thread
= thread_id
;
260 resume_info
[i
] = default_action
;
262 /* Still used in occasional places in the backend. */
263 if (n
== 1 && resume_info
[0].thread
!= -1)
264 cont_thread
= resume_info
[0].thread
;
267 set_desired_inferior (0);
269 (*the_target
->resume
) (resume_info
);
273 *signal
= mywait (status
, 1);
274 prepare_resume_reply (own_buf
, *status
, *signal
);
278 /* No other way to report an error... */
279 strcpy (own_buf
, "");
284 /* Handle all of the extended 'v' packets. */
286 handle_v_requests (char *own_buf
, char *status
, int *signal
)
288 if (strncmp (own_buf
, "vCont;", 6) == 0)
290 handle_v_cont (own_buf
, status
, signal
);
294 if (strncmp (own_buf
, "vCont?", 6) == 0)
296 strcpy (own_buf
, "vCont;c;C;s;S");
300 /* Otherwise we didn't know what packet it was. Say we didn't
307 myresume (int step
, int sig
)
309 struct thread_resume resume_info
[2];
312 if (step
|| sig
|| (cont_thread
!= 0 && cont_thread
!= -1))
314 resume_info
[0].thread
315 = ((struct inferior_list_entry
*) current_inferior
)->id
;
316 resume_info
[0].step
= step
;
317 resume_info
[0].sig
= sig
;
318 resume_info
[0].leave_stopped
= 0;
321 resume_info
[n
].thread
= -1;
322 resume_info
[n
].step
= 0;
323 resume_info
[n
].sig
= 0;
324 resume_info
[n
].leave_stopped
= (cont_thread
!= 0 && cont_thread
!= -1);
326 (*the_target
->resume
) (resume_info
);
332 gdbserver_version (void)
334 printf ("GNU gdbserver %s\n"
335 "Copyright (C) 2006 Free Software Foundation, Inc.\n"
336 "gdbserver is free software, covered by the GNU General Public License.\n"
337 "This gdbserver was configured as \"%s\"\n",
342 gdbserver_usage (void)
344 printf ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
345 "\tgdbserver COMM --attach PID\n"
347 "COMM may either be a tty device (for serial debugging), or \n"
348 "HOST:PORT to listen for a TCP connection.\n");
352 main (int argc
, char *argv
[])
354 char ch
, status
, *own_buf
;
355 unsigned char *mem_buf
;
364 if (argc
>= 2 && strcmp (argv
[1], "--version") == 0)
366 gdbserver_version ();
370 if (argc
>= 2 && strcmp (argv
[1], "--help") == 0)
376 if (setjmp (toplevel
))
378 fprintf (stderr
, "Exiting\n");
385 if (argc
>= 3 && strcmp (argv
[2], "--attach") == 0)
389 && (pid
= strtoul (argv
[3], &arg_end
, 10)) != 0
398 if (argc
< 3 || bad_attach
)
406 own_buf
= malloc (PBUFSIZ
);
407 mem_buf
= malloc (PBUFSIZ
);
411 /* Wait till we are at first instruction in program. */
412 signal
= start_inferior (&argv
[2], &status
);
414 /* We are now stopped at the first instruction of the target process */
418 switch (attach_inferior (pid
, &status
, &signal
))
421 error ("Attaching not supported on this target");
431 remote_open (argv
[1]);
435 while (getpkt (own_buf
) > 0)
443 handle_query (own_buf
);
446 remote_debug
= !remote_debug
;
449 fprintf (stderr
, "Detaching from inferior\n");
455 /* If we are attached, then we can exit. Otherwise, we need to
456 hang around doing nothing, until the child is gone. */
462 ret
= waitpid (signal_pid
, &status
, 0);
463 if (WIFEXITED (status
) || WIFSIGNALED (status
))
465 } while (ret
!= -1 || errno
!= ECHILD
);
473 extended_protocol
= 1;
474 prepare_resume_reply (own_buf
, status
, signal
);
478 /* We can not use the extended protocol if we are
479 attached, because we can not restart the running
480 program. So return unrecognized. */
485 prepare_resume_reply (own_buf
, status
, signal
);
488 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
490 unsigned long gdb_id
, thread_id
;
492 gdb_id
= strtoul (&own_buf
[2], NULL
, 16);
493 thread_id
= gdb_id_to_thread_id (gdb_id
);
500 if (own_buf
[1] == 'g')
502 general_thread
= thread_id
;
503 set_desired_inferior (1);
505 else if (own_buf
[1] == 'c')
506 cont_thread
= thread_id
;
507 else if (own_buf
[1] == 's')
508 step_thread
= thread_id
;
514 /* Silently ignore it so that gdb can extend the protocol
515 without compatibility headaches. */
520 set_desired_inferior (1);
521 registers_to_string (own_buf
);
524 set_desired_inferior (1);
525 registers_from_string (&own_buf
[1]);
529 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
530 if (read_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
531 convert_int_to_ascii (mem_buf
, own_buf
, len
);
536 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
537 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
543 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
544 if (target_signal_to_host_p (sig
))
545 signal
= target_signal_to_host (sig
);
548 set_desired_inferior (0);
549 myresume (0, signal
);
550 signal
= mywait (&status
, 1);
551 prepare_resume_reply (own_buf
, status
, signal
);
554 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
555 if (target_signal_to_host_p (sig
))
556 signal
= target_signal_to_host (sig
);
559 set_desired_inferior (0);
560 myresume (1, signal
);
561 signal
= mywait (&status
, 1);
562 prepare_resume_reply (own_buf
, status
, signal
);
565 set_desired_inferior (0);
567 signal
= mywait (&status
, 1);
568 prepare_resume_reply (own_buf
, status
, signal
);
571 set_desired_inferior (0);
573 signal
= mywait (&status
, 1);
574 prepare_resume_reply (own_buf
, status
, signal
);
580 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
581 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
582 char type
= own_buf
[1];
584 if (the_target
->insert_watchpoint
== NULL
585 || (type
< '2' || type
> '4'))
587 /* No watchpoint support or not a watchpoint command;
588 unrecognized either way. */
595 res
= (*the_target
->insert_watchpoint
) (type
, addr
, len
);
610 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
611 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
612 char type
= own_buf
[1];
614 if (the_target
->remove_watchpoint
== NULL
615 || (type
< '2' || type
> '4'))
617 /* No watchpoint support or not a watchpoint command;
618 unrecognized either way. */
625 res
= (*the_target
->remove_watchpoint
) (type
, addr
, len
);
637 fprintf (stderr
, "Killing inferior\n");
639 /* When using the extended protocol, we start up a new
640 debugging session. The traditional protocol will
642 if (extended_protocol
)
645 fprintf (stderr
, "GDBserver restarting\n");
647 /* Wait till we are at 1st instruction in prog. */
648 signal
= start_inferior (&argv
[2], &status
);
659 unsigned long gdb_id
, thread_id
;
661 gdb_id
= strtoul (&own_buf
[1], NULL
, 16);
662 thread_id
= gdb_id_to_thread_id (gdb_id
);
669 if (mythread_alive (thread_id
))
676 /* Restarting the inferior is only supported in the
677 extended protocol. */
678 if (extended_protocol
)
682 fprintf (stderr
, "GDBserver restarting\n");
684 /* Wait till we are at 1st instruction in prog. */
685 signal
= start_inferior (&argv
[2], &status
);
691 /* It is a request we don't understand. Respond with an
692 empty packet so that gdb knows that we don't support this
698 /* Extended (long) request. */
699 handle_v_requests (own_buf
, &status
, &signal
);
702 /* It is a request we don't understand. Respond with an
703 empty packet so that gdb knows that we don't support this
713 "\nChild exited with status %d\n", signal
);
715 fprintf (stderr
, "\nChild terminated with signal = 0x%x\n",
717 if (status
== 'W' || status
== 'X')
719 if (extended_protocol
)
721 fprintf (stderr
, "Killing inferior\n");
724 fprintf (stderr
, "GDBserver restarting\n");
726 /* Wait till we are at 1st instruction in prog. */
727 signal
= start_inferior (&argv
[2], &status
);
733 fprintf (stderr
, "GDBserver exiting\n");
739 /* We come here when getpkt fails.
741 For the extended remote protocol we exit (and this is the only
742 way we gracefully exit!).
744 For the traditional remote protocol close the connection,
745 and re-open it at the top of the loop. */
746 if (extended_protocol
)
753 fprintf (stderr
, "Remote side has terminated connection. "
754 "GDBserver will reopen the connection.\n");