1 /* Main code for remote server for GDB.
2 Copyright 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., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, 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);
86 extern int remote_debug
;
88 /* Handle all of the extended 'q' packets. */
90 handle_query (char *own_buf
)
92 static struct inferior_list_entry
*thread_ptr
;
94 if (strcmp ("qSymbol::", own_buf
) == 0)
96 if (the_target
->look_up_symbols
!= NULL
)
97 (*the_target
->look_up_symbols
) ();
99 strcpy (own_buf
, "OK");
103 if (strcmp ("qfThreadInfo", own_buf
) == 0)
105 thread_ptr
= all_threads
.head
;
106 sprintf (own_buf
, "m%x", thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
107 thread_ptr
= thread_ptr
->next
;
111 if (strcmp ("qsThreadInfo", own_buf
) == 0)
113 if (thread_ptr
!= NULL
)
115 sprintf (own_buf
, "m%x", thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
116 thread_ptr
= thread_ptr
->next
;
121 sprintf (own_buf
, "l");
126 if (the_target
->read_auxv
!= NULL
127 && strncmp ("qPart:auxv:read::", own_buf
, 17) == 0)
129 unsigned char data
[(PBUFSIZ
- 1) / 2];
133 decode_m_packet (&own_buf
[17], &ofs
, &len
); /* "OFS,LEN" */
134 if (len
> sizeof data
)
136 n
= (*the_target
->read_auxv
) (ofs
, data
, len
);
142 convert_int_to_ascii (data
, own_buf
, n
);
146 /* Otherwise we didn't know what packet it was. Say we didn't
151 /* Parse vCont packets. */
153 handle_v_cont (char *own_buf
, char *status
, int *signal
)
157 struct thread_resume
*resume_info
, default_action
;
159 /* Count the number of semicolons in the packet. There should be one
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
171 resume_info
= malloc ((n
+ 1) * sizeof (resume_info
[0]));
173 default_action
.thread
= -1;
174 default_action
.leave_stopped
= 1;
175 default_action
.step
= 0;
176 default_action
.sig
= 0;
184 resume_info
[i
].leave_stopped
= 0;
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;
193 if (p
[0] == 'S' || p
[0] == 'C')
196 sig
= strtol (p
+ 1, &q
, 16);
201 if (!target_signal_to_host_p (sig
))
203 resume_info
[i
].sig
= target_signal_to_host (sig
);
207 resume_info
[i
].sig
= 0;
213 resume_info
[i
].thread
= -1;
214 default_action
= resume_info
[i
];
216 /* Note: we don't increment i here, we'll overwrite this entry
217 the next time through. */
219 else if (p
[0] == ':')
221 unsigned int gdb_id
= strtoul (p
+ 1, &q
, 16);
222 unsigned long thread_id
;
227 if (p
[0] != ';' && p
[0] != 0)
230 thread_id
= gdb_id_to_thread_id (gdb_id
);
232 resume_info
[i
].thread
= thread_id
;
240 resume_info
[i
] = default_action
;
242 /* Still used in occasional places in the backend. */
243 if (n
== 1 && resume_info
[0].thread
!= -1)
244 cont_thread
= resume_info
[0].thread
;
247 set_desired_inferior (0);
249 (*the_target
->resume
) (resume_info
);
253 *signal
= mywait (status
, 1);
254 prepare_resume_reply (own_buf
, *status
, *signal
);
258 /* No other way to report an error... */
259 strcpy (own_buf
, "");
264 /* Handle all of the extended 'v' packets. */
266 handle_v_requests (char *own_buf
, char *status
, int *signal
)
268 if (strncmp (own_buf
, "vCont;", 6) == 0)
270 handle_v_cont (own_buf
, status
, signal
);
274 if (strncmp (own_buf
, "vCont?", 6) == 0)
276 strcpy (own_buf
, "vCont;c;C;s;S");
280 /* Otherwise we didn't know what packet it was. Say we didn't
287 myresume (int step
, int sig
)
289 struct thread_resume resume_info
[2];
292 if (step
|| sig
|| (cont_thread
!= 0 && cont_thread
!= -1))
294 resume_info
[0].thread
295 = ((struct inferior_list_entry
*) current_inferior
)->id
;
296 resume_info
[0].step
= step
;
297 resume_info
[0].sig
= sig
;
298 resume_info
[0].leave_stopped
= 0;
301 resume_info
[n
].thread
= -1;
302 resume_info
[n
].step
= 0;
303 resume_info
[n
].sig
= 0;
304 resume_info
[n
].leave_stopped
= (cont_thread
!= 0 && cont_thread
!= -1);
306 (*the_target
->resume
) (resume_info
);
312 gdbserver_usage (void)
314 error ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
315 "\tgdbserver COMM --attach PID\n"
317 "COMM may either be a tty device (for serial debugging), or \n"
318 "HOST:PORT to listen for a TCP connection.\n");
322 main (int argc
, char *argv
[])
324 char ch
, status
, *own_buf
;
325 unsigned char mem_buf
[2000];
334 if (setjmp (toplevel
))
336 fprintf (stderr
, "Exiting\n");
343 if (argc
>= 3 && strcmp (argv
[2], "--attach") == 0)
347 && (pid
= strtoul (argv
[3], &arg_end
, 10)) != 0
356 if (argc
< 3 || bad_attach
)
361 own_buf
= malloc (PBUFSIZ
);
365 /* Wait till we are at first instruction in program. */
366 signal
= start_inferior (&argv
[2], &status
);
368 /* We are now stopped at the first instruction of the target process */
372 switch (attach_inferior (pid
, &status
, &signal
))
375 error ("Attaching not supported on this target");
385 remote_open (argv
[1]);
389 while (getpkt (own_buf
) > 0)
397 handle_query (own_buf
);
400 remote_debug
= !remote_debug
;
403 fprintf (stderr
, "Detaching from inferior\n");
409 /* If we are attached, then we can exit. Otherwise, we need to
410 hang around doing nothing, until the child is gone. */
416 ret
= waitpid (signal_pid
, &status
, 0);
417 if (WIFEXITED (status
) || WIFSIGNALED (status
))
419 } while (ret
!= -1 || errno
!= ECHILD
);
427 extended_protocol
= 1;
428 prepare_resume_reply (own_buf
, status
, signal
);
432 /* We can not use the extended protocol if we are
433 attached, because we can not restart the running
434 program. So return unrecognized. */
439 prepare_resume_reply (own_buf
, status
, signal
);
442 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
444 unsigned long gdb_id
, thread_id
;
446 gdb_id
= strtoul (&own_buf
[2], NULL
, 16);
447 thread_id
= gdb_id_to_thread_id (gdb_id
);
454 if (own_buf
[1] == 'g')
456 general_thread
= thread_id
;
457 set_desired_inferior (1);
459 else if (own_buf
[1] == 'c')
460 cont_thread
= thread_id
;
461 else if (own_buf
[1] == 's')
462 step_thread
= thread_id
;
468 /* Silently ignore it so that gdb can extend the protocol
469 without compatibility headaches. */
474 set_desired_inferior (1);
475 registers_to_string (own_buf
);
478 set_desired_inferior (1);
479 registers_from_string (&own_buf
[1]);
483 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
484 if (read_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
485 convert_int_to_ascii (mem_buf
, own_buf
, len
);
490 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
491 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
497 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
498 if (target_signal_to_host_p (sig
))
499 signal
= target_signal_to_host (sig
);
502 set_desired_inferior (0);
503 myresume (0, signal
);
504 signal
= mywait (&status
, 1);
505 prepare_resume_reply (own_buf
, status
, signal
);
508 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
509 if (target_signal_to_host_p (sig
))
510 signal
= target_signal_to_host (sig
);
513 set_desired_inferior (0);
514 myresume (1, signal
);
515 signal
= mywait (&status
, 1);
516 prepare_resume_reply (own_buf
, status
, signal
);
519 set_desired_inferior (0);
521 signal
= mywait (&status
, 1);
522 prepare_resume_reply (own_buf
, status
, signal
);
525 set_desired_inferior (0);
527 signal
= mywait (&status
, 1);
528 prepare_resume_reply (own_buf
, status
, signal
);
534 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
535 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
536 char type
= own_buf
[1];
538 if (the_target
->insert_watchpoint
== NULL
539 || (type
< '2' || type
> '4'))
541 /* No watchpoint support or not a watchpoint command;
542 unrecognized either way. */
549 res
= (*the_target
->insert_watchpoint
) (type
, addr
, len
);
564 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
565 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
566 char type
= own_buf
[1];
568 if (the_target
->remove_watchpoint
== NULL
569 || (type
< '2' || type
> '4'))
571 /* No watchpoint support or not a watchpoint command;
572 unrecognized either way. */
579 res
= (*the_target
->remove_watchpoint
) (type
, addr
, len
);
591 fprintf (stderr
, "Killing inferior\n");
593 /* When using the extended protocol, we start up a new
594 debugging session. The traditional protocol will
596 if (extended_protocol
)
599 fprintf (stderr
, "GDBserver restarting\n");
601 /* Wait till we are at 1st instruction in prog. */
602 signal
= start_inferior (&argv
[2], &status
);
613 unsigned long gdb_id
, thread_id
;
615 gdb_id
= strtoul (&own_buf
[1], NULL
, 16);
616 thread_id
= gdb_id_to_thread_id (gdb_id
);
623 if (mythread_alive (thread_id
))
630 /* Restarting the inferior is only supported in the
631 extended protocol. */
632 if (extended_protocol
)
636 fprintf (stderr
, "GDBserver restarting\n");
638 /* Wait till we are at 1st instruction in prog. */
639 signal
= start_inferior (&argv
[2], &status
);
645 /* It is a request we don't understand. Respond with an
646 empty packet so that gdb knows that we don't support this
652 /* Extended (long) request. */
653 handle_v_requests (own_buf
, &status
, &signal
);
656 /* It is a request we don't understand. Respond with an
657 empty packet so that gdb knows that we don't support this
667 "\nChild exited with status %d\n", signal
);
669 fprintf (stderr
, "\nChild terminated with signal = 0x%x\n",
671 if (status
== 'W' || status
== 'X')
673 if (extended_protocol
)
675 fprintf (stderr
, "Killing inferior\n");
678 fprintf (stderr
, "GDBserver restarting\n");
680 /* Wait till we are at 1st instruction in prog. */
681 signal
= start_inferior (&argv
[2], &status
);
687 fprintf (stderr
, "GDBserver exiting\n");
693 /* We come here when getpkt fails.
695 For the extended remote protocol we exit (and this is the only
696 way we gracefully exit!).
698 For the traditional remote protocol close the connection,
699 and re-open it at the top of the loop. */
700 if (extended_protocol
)
707 fprintf (stderr
, "Remote side has terminated connection. "
708 "GDBserver will reopen the connection.\n");