]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/server.c
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
;
33 start_inferior (char *argv
[], char *statusptr
)
35 inferior_pid
= create_inferior (argv
[0], argv
);
36 fprintf (stderr
, "Process %s created; pid = %d\n", argv
[0], inferior_pid
);
38 /* Wait till we are at 1st instruction in program, return signal number. */
39 return mywait (statusptr
);
43 attach_inferior (int pid
, char *statusptr
, unsigned char *sigptr
)
45 /* myattach should return -1 if attaching is unsupported,
46 0 if it succeeded, and call error() otherwise. */
47 if (myattach (pid
) != 0)
52 *sigptr
= mywait (statusptr
);
57 extern int remote_debug
;
60 main (int argc
, char *argv
[])
62 char ch
, status
, *own_buf
, mem_buf
[2000];
72 if (setjmp (toplevel
))
74 fprintf (stderr
, "Exiting\n");
78 if (argc
>= 3 && strcmp (argv
[2], "--attach") == 0)
82 && (pid
= strtoul (argv
[3], &arg_end
, 10)) != 0
91 if (argc
< 3 || bad_attach
)
92 error ("Usage:\tgdbserver tty prog [args ...]\n"
93 "\tgdbserver tty --attach pid");
97 own_buf
= malloc (PBUFSIZ
);
101 /* Wait till we are at first instruction in program. */
102 signal
= start_inferior (&argv
[2], &status
);
104 /* We are now stopped at the first instruction of the target process */
108 switch (attach_inferior (pid
, &status
, &signal
))
111 error ("Attaching not supported on this target");
121 remote_open (argv
[1]);
125 while (getpkt (own_buf
) > 0)
133 remote_debug
= !remote_debug
;
138 extended_protocol
= 1;
139 prepare_resume_reply (own_buf
, status
, signal
);
143 /* We can not use the extended protocol if we are
144 attached, because we can not restart the running
145 program. So return unrecognized. */
150 prepare_resume_reply (own_buf
, status
, signal
);
156 general_thread
= strtol (&own_buf
[2], NULL
, 16);
158 fetch_inferior_registers (0);
161 cont_thread
= strtol (&own_buf
[2], NULL
, 16);
165 /* Silently ignore it so that gdb can extend the protocol
166 without compatibility headaches. */
172 registers_to_string (own_buf
);
175 registers_from_string (&own_buf
[1]);
176 store_inferior_registers (-1);
180 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
181 read_inferior_memory (mem_addr
, mem_buf
, len
);
182 convert_int_to_ascii (mem_buf
, own_buf
, len
);
185 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
186 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
192 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
194 signal
= mywait (&status
);
195 prepare_resume_reply (own_buf
, status
, signal
);
198 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
200 signal
= mywait (&status
);
201 prepare_resume_reply (own_buf
, status
, signal
);
205 signal
= mywait (&status
);
206 prepare_resume_reply (own_buf
, status
, signal
);
210 signal
= mywait (&status
);
211 prepare_resume_reply (own_buf
, status
, signal
);
214 fprintf (stderr
, "Killing inferior\n");
216 /* When using the extended protocol, we start up a new
217 debugging session. The traditional protocol will
219 if (extended_protocol
)
222 fprintf (stderr
, "GDBserver restarting\n");
224 /* Wait till we are at 1st instruction in prog. */
225 signal
= start_inferior (&argv
[2], &status
);
235 if (mythread_alive (strtol (&own_buf
[1], NULL
, 16)))
241 /* Restarting the inferior is only supported in the
242 extended protocol. */
243 if (extended_protocol
)
247 fprintf (stderr
, "GDBserver restarting\n");
249 /* Wait till we are at 1st instruction in prog. */
250 signal
= start_inferior (&argv
[2], &status
);
256 /* It is a request we don't understand. Respond with an
257 empty packet so that gdb knows that we don't support this
263 /* It is a request we don't understand. Respond with an
264 empty packet so that gdb knows that we don't support this
274 "\nChild exited with status %d\n", sig
);
276 fprintf (stderr
, "\nChild terminated with signal = 0x%x\n", sig
);
277 if (status
== 'W' || status
== 'X')
279 if (extended_protocol
)
281 fprintf (stderr
, "Killing inferior\n");
284 fprintf (stderr
, "GDBserver restarting\n");
286 /* Wait till we are at 1st instruction in prog. */
287 signal
= start_inferior (&argv
[2], &status
);
293 fprintf (stderr
, "GDBserver exiting\n");
299 /* We come here when getpkt fails.
301 For the extended remote protocol we exit (and this is the only
302 way we gracefully exit!).
304 For the traditional remote protocol close the connection,
305 and re-open it at the top of the loop. */
306 if (extended_protocol
)
313 fprintf (stderr
, "Remote side has terminated connection. "
314 "GDBserver will reopen the connection.\n");