3afab2b1d1117dc96e3338e696eb661690fc8e4d
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. */
27 int old_thread_from_wait
;
28 int extended_protocol
;
32 start_inferior (char *argv
[], char *statusptr
)
34 /* FIXME Check error? Or turn to void. */
35 create_inferior (argv
[0], argv
);
36 /* FIXME Print pid properly. */
37 fprintf (stderr
, "Process %s created; pid = %d\n", argv
[0], signal_pid
);
39 /* Wait till we are at 1st instruction in program, return signal number. */
40 return mywait (statusptr
);
44 attach_inferior (int pid
, char *statusptr
, unsigned char *sigptr
)
46 /* myattach should return -1 if attaching is unsupported,
47 0 if it succeeded, and call error() otherwise. */
48 if (myattach (pid
) != 0)
53 *sigptr
= mywait (statusptr
);
58 extern int remote_debug
;
60 /* Handle all of the extended 'q' packets. */
62 handle_query (char *own_buf
)
64 if (strcmp ("qSymbol::", own_buf
) == 0)
66 if (the_target
->look_up_symbols
!= NULL
)
67 (*the_target
->look_up_symbols
) ();
69 strcpy (own_buf
, "OK");
73 /* Otherwise we didn't know what packet it was. Say we didn't
81 main (int argc
, char *argv
[])
83 char ch
, status
, *own_buf
, mem_buf
[2000];
92 if (setjmp (toplevel
))
94 fprintf (stderr
, "Exiting\n");
101 if (argc
>= 3 && strcmp (argv
[2], "--attach") == 0)
105 && (pid
= strtoul (argv
[3], &arg_end
, 10)) != 0
114 if (argc
< 3 || bad_attach
)
115 error ("Usage:\tgdbserver tty prog [args ...]\n"
116 "\tgdbserver tty --attach pid");
120 own_buf
= malloc (PBUFSIZ
);
124 /* Wait till we are at first instruction in program. */
125 signal
= start_inferior (&argv
[2], &status
);
127 /* We are now stopped at the first instruction of the target process */
131 switch (attach_inferior (pid
, &status
, &signal
))
134 error ("Attaching not supported on this target");
144 remote_open (argv
[1]);
148 while (getpkt (own_buf
) > 0)
156 handle_query (own_buf
);
159 remote_debug
= !remote_debug
;
164 extended_protocol
= 1;
165 prepare_resume_reply (own_buf
, status
, signal
);
169 /* We can not use the extended protocol if we are
170 attached, because we can not restart the running
171 program. So return unrecognized. */
176 prepare_resume_reply (own_buf
, status
, signal
);
182 general_thread
= strtol (&own_buf
[2], NULL
, 16);
184 fetch_inferior_registers (0);
187 cont_thread
= strtol (&own_buf
[2], NULL
, 16);
191 /* Silently ignore it so that gdb can extend the protocol
192 without compatibility headaches. */
198 registers_to_string (own_buf
);
201 registers_from_string (&own_buf
[1]);
202 store_inferior_registers (-1);
206 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
207 read_inferior_memory (mem_addr
, mem_buf
, len
);
208 convert_int_to_ascii (mem_buf
, own_buf
, len
);
211 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
212 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
218 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
219 if (target_signal_to_host_p (sig
))
220 signal
= target_signal_to_host (sig
);
223 myresume (0, signal
);
224 signal
= mywait (&status
);
225 prepare_resume_reply (own_buf
, status
, signal
);
228 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
229 if (target_signal_to_host_p (sig
))
230 signal
= target_signal_to_host (sig
);
233 myresume (1, signal
);
234 signal
= mywait (&status
);
235 prepare_resume_reply (own_buf
, status
, signal
);
239 signal
= mywait (&status
);
240 prepare_resume_reply (own_buf
, status
, signal
);
244 signal
= mywait (&status
);
245 prepare_resume_reply (own_buf
, status
, signal
);
248 fprintf (stderr
, "Killing inferior\n");
250 /* When using the extended protocol, we start up a new
251 debugging session. The traditional protocol will
253 if (extended_protocol
)
256 fprintf (stderr
, "GDBserver restarting\n");
258 /* Wait till we are at 1st instruction in prog. */
259 signal
= start_inferior (&argv
[2], &status
);
269 if (mythread_alive (strtol (&own_buf
[1], NULL
, 16)))
275 /* Restarting the inferior is only supported in the
276 extended protocol. */
277 if (extended_protocol
)
281 fprintf (stderr
, "GDBserver restarting\n");
283 /* Wait till we are at 1st instruction in prog. */
284 signal
= start_inferior (&argv
[2], &status
);
290 /* It is a request we don't understand. Respond with an
291 empty packet so that gdb knows that we don't support this
297 /* It is a request we don't understand. Respond with an
298 empty packet so that gdb knows that we don't support this
308 "\nChild exited with status %d\n", sig
);
310 fprintf (stderr
, "\nChild terminated with signal = 0x%x\n", sig
);
311 if (status
== 'W' || status
== 'X')
313 if (extended_protocol
)
315 fprintf (stderr
, "Killing inferior\n");
318 fprintf (stderr
, "GDBserver restarting\n");
320 /* Wait till we are at 1st instruction in prog. */
321 signal
= start_inferior (&argv
[2], &status
);
327 fprintf (stderr
, "GDBserver exiting\n");
333 /* We come here when getpkt fails.
335 For the extended remote protocol we exit (and this is the only
336 way we gracefully exit!).
338 For the traditional remote protocol close the connection,
339 and re-open it at the top of the loop. */
340 if (extended_protocol
)
347 fprintf (stderr
, "Remote side has terminated connection. "
348 "GDBserver will reopen the connection.\n");
This page took 0.076999 seconds and 4 git commands to generate.