b674ed0070ffcba1984bdf1d5d0c05bef0ec96e4
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. */
28 int old_thread_from_wait
;
29 int extended_protocol
;
35 start_inferior (char *argv
[], char *statusptr
)
37 /* FIXME Check error? Or turn to void. */
38 create_inferior (argv
[0], argv
);
40 fprintf (stderr
, "Process %s created; pid = %d\n", argv
[0],
41 all_threads
.head
->id
);
43 /* Wait till we are at 1st instruction in program, return signal number. */
44 return mywait (statusptr
, 0);
48 attach_inferior (int pid
, char *statusptr
, unsigned char *sigptr
)
50 /* myattach should return -1 if attaching is unsupported,
51 0 if it succeeded, and call error() otherwise. */
52 if (myattach (pid
) != 0)
55 *sigptr
= mywait (statusptr
, 0);
60 extern int remote_debug
;
62 /* Handle all of the extended 'q' packets. */
64 handle_query (char *own_buf
)
66 static struct inferior_list_entry
*thread_ptr
;
68 if (strcmp ("qSymbol::", own_buf
) == 0)
70 if (the_target
->look_up_symbols
!= NULL
)
71 (*the_target
->look_up_symbols
) ();
73 strcpy (own_buf
, "OK");
77 if (strcmp ("qfThreadInfo", own_buf
) == 0)
79 thread_ptr
= all_threads
.head
;
80 sprintf (own_buf
, "m%x", thread_ptr
->id
);
81 thread_ptr
= thread_ptr
->next
;
85 if (strcmp ("qsThreadInfo", own_buf
) == 0)
87 if (thread_ptr
!= NULL
)
89 sprintf (own_buf
, "m%x", thread_ptr
->id
);
90 thread_ptr
= thread_ptr
->next
;
95 sprintf (own_buf
, "l");
100 /* Otherwise we didn't know what packet it was. Say we didn't
108 gdbserver_usage (void)
110 error ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
111 "\tgdbserver COMM --attach PID\n"
113 "COMM may either be a tty device (for serial debugging), or \n"
114 "HOST:PORT to listen for a TCP connection.\n");
118 main (int argc
, char *argv
[])
120 char ch
, status
, *own_buf
, mem_buf
[2000];
122 unsigned char signal
;
129 if (setjmp (toplevel
))
131 fprintf (stderr
, "Exiting\n");
138 if (argc
>= 3 && strcmp (argv
[2], "--attach") == 0)
142 && (pid
= strtoul (argv
[3], &arg_end
, 10)) != 0
151 if (argc
< 3 || bad_attach
)
156 own_buf
= malloc (PBUFSIZ
);
160 /* Wait till we are at first instruction in program. */
161 signal
= start_inferior (&argv
[2], &status
);
163 /* We are now stopped at the first instruction of the target process */
167 switch (attach_inferior (pid
, &status
, &signal
))
170 error ("Attaching not supported on this target");
180 remote_open (argv
[1]);
184 while (getpkt (own_buf
) > 0)
192 handle_query (own_buf
);
195 remote_debug
= !remote_debug
;
200 extended_protocol
= 1;
201 prepare_resume_reply (own_buf
, status
, signal
);
205 /* We can not use the extended protocol if we are
206 attached, because we can not restart the running
207 program. So return unrecognized. */
212 prepare_resume_reply (own_buf
, status
, signal
);
218 general_thread
= strtol (&own_buf
[2], NULL
, 16);
220 set_desired_inferior (1);
223 cont_thread
= strtol (&own_buf
[2], NULL
, 16);
227 step_thread
= strtol (&own_buf
[2], NULL
, 16);
231 /* Silently ignore it so that gdb can extend the protocol
232 without compatibility headaches. */
238 set_desired_inferior (1);
239 registers_to_string (own_buf
);
242 set_desired_inferior (1);
243 registers_from_string (&own_buf
[1]);
247 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
248 read_inferior_memory (mem_addr
, mem_buf
, len
);
249 convert_int_to_ascii (mem_buf
, own_buf
, len
);
252 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
253 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
259 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
260 if (target_signal_to_host_p (sig
))
261 signal
= target_signal_to_host (sig
);
264 set_desired_inferior (0);
265 myresume (0, signal
);
266 signal
= mywait (&status
, 1);
267 prepare_resume_reply (own_buf
, status
, signal
);
270 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
271 if (target_signal_to_host_p (sig
))
272 signal
= target_signal_to_host (sig
);
275 set_desired_inferior (0);
276 myresume (1, signal
);
277 signal
= mywait (&status
, 1);
278 prepare_resume_reply (own_buf
, status
, signal
);
281 set_desired_inferior (0);
283 signal
= mywait (&status
, 1);
284 prepare_resume_reply (own_buf
, status
, signal
);
287 set_desired_inferior (0);
289 signal
= mywait (&status
, 1);
290 prepare_resume_reply (own_buf
, status
, signal
);
293 fprintf (stderr
, "Killing inferior\n");
295 /* When using the extended protocol, we start up a new
296 debugging session. The traditional protocol will
298 if (extended_protocol
)
301 fprintf (stderr
, "GDBserver restarting\n");
303 /* Wait till we are at 1st instruction in prog. */
304 signal
= start_inferior (&argv
[2], &status
);
314 if (mythread_alive (strtol (&own_buf
[1], NULL
, 16)))
320 /* Restarting the inferior is only supported in the
321 extended protocol. */
322 if (extended_protocol
)
326 fprintf (stderr
, "GDBserver restarting\n");
328 /* Wait till we are at 1st instruction in prog. */
329 signal
= start_inferior (&argv
[2], &status
);
335 /* It is a request we don't understand. Respond with an
336 empty packet so that gdb knows that we don't support this
342 /* It is a request we don't understand. Respond with an
343 empty packet so that gdb knows that we don't support this
353 "\nChild exited with status %d\n", sig
);
355 fprintf (stderr
, "\nChild terminated with signal = 0x%x\n", sig
);
356 if (status
== 'W' || status
== 'X')
358 if (extended_protocol
)
360 fprintf (stderr
, "Killing inferior\n");
363 fprintf (stderr
, "GDBserver restarting\n");
365 /* Wait till we are at 1st instruction in prog. */
366 signal
= start_inferior (&argv
[2], &status
);
372 fprintf (stderr
, "GDBserver exiting\n");
378 /* We come here when getpkt fails.
380 For the extended remote protocol we exit (and this is the only
381 way we gracefully exit!).
383 For the traditional remote protocol close the connection,
384 and re-open it at the top of the loop. */
385 if (extended_protocol
)
392 fprintf (stderr
, "Remote side has terminated connection. "
393 "GDBserver will reopen the connection.\n");