1 /* Main code for remote server for GDB.
2 Copyright 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002
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. */
32 int old_thread_from_wait
;
33 int extended_protocol
;
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. */
46 start_inferior (char *argv
[], char *statusptr
)
48 signal (SIGTTOU
, SIG_DFL
);
49 signal (SIGTTIN
, SIG_DFL
);
51 signal_pid
= create_inferior (argv
[0], argv
);
53 fprintf (stderr
, "Process %s created; pid = %d\n", argv
[0],
56 signal (SIGTTOU
, SIG_IGN
);
57 signal (SIGTTIN
, SIG_IGN
);
58 tcsetpgrp (fileno (stderr
), signal_pid
);
60 /* Wait till we are at 1st instruction in program, return signal number. */
61 return mywait (statusptr
, 0);
65 attach_inferior (int pid
, char *statusptr
, unsigned char *sigptr
)
67 /* myattach should return -1 if attaching is unsupported,
68 0 if it succeeded, and call error() otherwise. */
70 if (myattach (pid
) != 0)
73 /* FIXME - It may be that we should get the SIGNAL_PID from the
74 attach function, so that it can be the main thread instead of
75 whichever we were told to attach to. */
78 *sigptr
= mywait (statusptr
, 0);
83 extern int remote_debug
;
85 /* Handle all of the extended 'q' packets. */
87 handle_query (char *own_buf
)
89 static struct inferior_list_entry
*thread_ptr
;
91 if (strcmp ("qSymbol::", own_buf
) == 0)
93 if (the_target
->look_up_symbols
!= NULL
)
94 (*the_target
->look_up_symbols
) ();
96 strcpy (own_buf
, "OK");
100 if (strcmp ("qfThreadInfo", own_buf
) == 0)
102 thread_ptr
= all_threads
.head
;
103 sprintf (own_buf
, "m%x", thread_ptr
->id
);
104 thread_ptr
= thread_ptr
->next
;
108 if (strcmp ("qsThreadInfo", own_buf
) == 0)
110 if (thread_ptr
!= NULL
)
112 sprintf (own_buf
, "m%x", thread_ptr
->id
);
113 thread_ptr
= thread_ptr
->next
;
118 sprintf (own_buf
, "l");
123 /* Otherwise we didn't know what packet it was. Say we didn't
128 /* Parse vCont packets. */
130 handle_v_cont (char *own_buf
, char *status
, unsigned char *signal
)
134 struct thread_resume
*resume_info
, default_action
;
136 /* Count the number of semicolons in the packet. There should be one
145 /* Allocate room for one extra action, for the default remain-stopped
146 behavior; if no default action is in the list, we'll need the extra
148 resume_info
= malloc ((n
+ 1) * sizeof (resume_info
[0]));
150 default_action
.thread
= -1;
151 default_action
.leave_stopped
= 1;
152 default_action
.step
= 0;
153 default_action
.sig
= 0;
161 resume_info
[i
].leave_stopped
= 0;
163 if (p
[0] == 's' || p
[0] == 'S')
164 resume_info
[i
].step
= 1;
165 else if (p
[0] == 'c' || p
[0] == 'C')
166 resume_info
[i
].step
= 0;
170 if (p
[0] == 'S' || p
[0] == 'C')
173 sig
= strtol (p
+ 1, &q
, 16);
178 if (!target_signal_to_host_p (sig
))
180 resume_info
[i
].sig
= target_signal_to_host (sig
);
184 resume_info
[i
].sig
= 0;
190 resume_info
[i
].thread
= -1;
191 default_action
= resume_info
[i
];
193 /* Note: we don't increment i here, we'll overwrite this entry
194 the next time through. */
196 else if (p
[0] == ':')
198 resume_info
[i
].thread
= strtol (p
+ 1, &q
, 16);
202 if (p
[0] != ';' && p
[0] != 0)
209 resume_info
[i
] = default_action
;
211 /* Still used in occasional places in the backend. */
212 if (n
== 1 && resume_info
[0].thread
!= -1)
213 cont_thread
= resume_info
[0].thread
;
217 (*the_target
->resume
) (resume_info
);
221 *signal
= mywait (status
, 1);
222 prepare_resume_reply (own_buf
, *status
, *signal
);
226 /* No other way to report an error... */
227 strcpy (own_buf
, "");
232 /* Handle all of the extended 'v' packets. */
234 handle_v_requests (char *own_buf
, char *status
, unsigned char *signal
)
236 if (strncmp (own_buf
, "vCont;", 6) == 0)
238 handle_v_cont (own_buf
, status
, signal
);
242 if (strncmp (own_buf
, "vCont?", 6) == 0)
244 strcpy (own_buf
, "vCont;c;C;s;S");
248 /* Otherwise we didn't know what packet it was. Say we didn't
255 myresume (int step
, int sig
)
257 struct thread_resume resume_info
[2];
260 if (step
|| sig
|| cont_thread
> 0)
262 resume_info
[0].thread
263 = ((struct inferior_list_entry
*) current_inferior
)->id
;
264 resume_info
[0].step
= step
;
265 resume_info
[0].sig
= sig
;
266 resume_info
[0].leave_stopped
= 0;
269 resume_info
[n
].thread
= -1;
270 resume_info
[n
].step
= 0;
271 resume_info
[n
].sig
= 0;
272 resume_info
[n
].leave_stopped
= (cont_thread
> 0);
274 (*the_target
->resume
) (resume_info
);
280 gdbserver_usage (void)
282 error ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
283 "\tgdbserver COMM --attach PID\n"
285 "COMM may either be a tty device (for serial debugging), or \n"
286 "HOST:PORT to listen for a TCP connection.\n");
290 main (int argc
, char *argv
[])
292 char ch
, status
, *own_buf
, mem_buf
[2000];
294 unsigned char signal
;
301 if (setjmp (toplevel
))
303 fprintf (stderr
, "Exiting\n");
310 if (argc
>= 3 && strcmp (argv
[2], "--attach") == 0)
314 && (pid
= strtoul (argv
[3], &arg_end
, 10)) != 0
323 if (argc
< 3 || bad_attach
)
328 own_buf
= malloc (PBUFSIZ
);
332 /* Wait till we are at first instruction in program. */
333 signal
= start_inferior (&argv
[2], &status
);
335 /* We are now stopped at the first instruction of the target process */
339 switch (attach_inferior (pid
, &status
, &signal
))
342 error ("Attaching not supported on this target");
352 remote_open (argv
[1]);
356 while (getpkt (own_buf
) > 0)
364 handle_query (own_buf
);
367 remote_debug
= !remote_debug
;
370 fprintf (stderr
, "Detaching from inferior\n");
376 /* If we are attached, then we can exit. Otherwise, we need to
377 hang around doing nothing, until the child is gone. */
383 ret
= waitpid (signal_pid
, &status
, 0);
384 if (WIFEXITED (status
) || WIFSIGNALED (status
))
386 } while (ret
!= -1 || errno
!= ECHILD
);
394 extended_protocol
= 1;
395 prepare_resume_reply (own_buf
, status
, signal
);
399 /* We can not use the extended protocol if we are
400 attached, because we can not restart the running
401 program. So return unrecognized. */
406 prepare_resume_reply (own_buf
, status
, signal
);
412 general_thread
= strtol (&own_buf
[2], NULL
, 16);
414 set_desired_inferior (1);
417 cont_thread
= strtol (&own_buf
[2], NULL
, 16);
421 step_thread
= strtol (&own_buf
[2], NULL
, 16);
425 /* Silently ignore it so that gdb can extend the protocol
426 without compatibility headaches. */
432 set_desired_inferior (1);
433 registers_to_string (own_buf
);
436 set_desired_inferior (1);
437 registers_from_string (&own_buf
[1]);
441 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
442 read_inferior_memory (mem_addr
, mem_buf
, len
);
443 convert_int_to_ascii (mem_buf
, own_buf
, len
);
446 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
447 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
453 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
454 if (target_signal_to_host_p (sig
))
455 signal
= target_signal_to_host (sig
);
458 set_desired_inferior (0);
459 myresume (0, signal
);
460 signal
= mywait (&status
, 1);
461 prepare_resume_reply (own_buf
, status
, signal
);
464 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
465 if (target_signal_to_host_p (sig
))
466 signal
= target_signal_to_host (sig
);
469 set_desired_inferior (0);
470 myresume (1, signal
);
471 signal
= mywait (&status
, 1);
472 prepare_resume_reply (own_buf
, status
, signal
);
475 set_desired_inferior (0);
477 signal
= mywait (&status
, 1);
478 prepare_resume_reply (own_buf
, status
, signal
);
481 set_desired_inferior (0);
483 signal
= mywait (&status
, 1);
484 prepare_resume_reply (own_buf
, status
, signal
);
487 fprintf (stderr
, "Killing inferior\n");
489 /* When using the extended protocol, we start up a new
490 debugging session. The traditional protocol will
492 if (extended_protocol
)
495 fprintf (stderr
, "GDBserver restarting\n");
497 /* Wait till we are at 1st instruction in prog. */
498 signal
= start_inferior (&argv
[2], &status
);
508 if (mythread_alive (strtol (&own_buf
[1], NULL
, 16)))
514 /* Restarting the inferior is only supported in the
515 extended protocol. */
516 if (extended_protocol
)
520 fprintf (stderr
, "GDBserver restarting\n");
522 /* Wait till we are at 1st instruction in prog. */
523 signal
= start_inferior (&argv
[2], &status
);
529 /* It is a request we don't understand. Respond with an
530 empty packet so that gdb knows that we don't support this
536 /* Extended (long) request. */
537 handle_v_requests (own_buf
, &status
, &signal
);
540 /* It is a request we don't understand. Respond with an
541 empty packet so that gdb knows that we don't support this
551 "\nChild exited with status %d\n", sig
);
553 fprintf (stderr
, "\nChild terminated with signal = 0x%x\n", sig
);
554 if (status
== 'W' || status
== 'X')
556 if (extended_protocol
)
558 fprintf (stderr
, "Killing inferior\n");
561 fprintf (stderr
, "GDBserver restarting\n");
563 /* Wait till we are at 1st instruction in prog. */
564 signal
= start_inferior (&argv
[2], &status
);
570 fprintf (stderr
, "GDBserver exiting\n");
576 /* We come here when getpkt fails.
578 For the extended remote protocol we exit (and this is the only
579 way we gracefully exit!).
581 For the traditional remote protocol close the connection,
582 and re-open it at the top of the loop. */
583 if (extended_protocol
)
590 fprintf (stderr
, "Remote side has terminated connection. "
591 "GDBserver will reopen the connection.\n");