]>
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
[PBUFSIZ
], 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");
99 /* Wait till we are at first instruction in program. */
100 signal
= start_inferior (&argv
[2], &status
);
102 /* We are now stopped at the first instruction of the target process */
106 switch (attach_inferior (pid
, &status
, &signal
))
109 error ("Attaching not supported on this target");
119 remote_open (argv
[1]);
123 while (getpkt (own_buf
) > 0)
131 remote_debug
= !remote_debug
;
136 extended_protocol
= 1;
137 prepare_resume_reply (own_buf
, status
, signal
);
141 /* We can not use the extended protocol if we are
142 attached, because we can not restart the running
143 program. So return unrecognized. */
148 prepare_resume_reply (own_buf
, status
, signal
);
154 general_thread
= strtol (&own_buf
[2], NULL
, 16);
156 fetch_inferior_registers (0);
159 cont_thread
= strtol (&own_buf
[2], NULL
, 16);
163 /* Silently ignore it so that gdb can extend the protocol
164 without compatibility headaches. */
170 convert_int_to_ascii (registers
, own_buf
, REGISTER_BYTES
);
173 convert_ascii_to_int (&own_buf
[1], registers
, REGISTER_BYTES
);
174 store_inferior_registers (-1);
178 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
179 read_inferior_memory (mem_addr
, mem_buf
, len
);
180 convert_int_to_ascii (mem_buf
, own_buf
, len
);
183 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
184 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
190 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
192 signal
= mywait (&status
);
193 prepare_resume_reply (own_buf
, status
, signal
);
196 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
198 signal
= mywait (&status
);
199 prepare_resume_reply (own_buf
, status
, signal
);
203 signal
= mywait (&status
);
204 prepare_resume_reply (own_buf
, status
, signal
);
208 signal
= mywait (&status
);
209 prepare_resume_reply (own_buf
, status
, signal
);
212 fprintf (stderr
, "Killing inferior\n");
214 /* When using the extended protocol, we start up a new
215 debugging session. The traditional protocol will
217 if (extended_protocol
)
220 fprintf (stderr
, "GDBserver restarting\n");
222 /* Wait till we are at 1st instruction in prog. */
223 signal
= start_inferior (&argv
[2], &status
);
233 if (mythread_alive (strtol (&own_buf
[1], NULL
, 16)))
239 /* Restarting the inferior is only supported in the
240 extended protocol. */
241 if (extended_protocol
)
245 fprintf (stderr
, "GDBserver restarting\n");
247 /* Wait till we are at 1st instruction in prog. */
248 signal
= start_inferior (&argv
[2], &status
);
254 /* It is a request we don't understand. Respond with an
255 empty packet so that gdb knows that we don't support this
261 /* It is a request we don't understand. Respond with an
262 empty packet so that gdb knows that we don't support this
272 "\nChild exited with status %d\n", sig
);
274 fprintf (stderr
, "\nChild terminated with signal = 0x%x\n", sig
);
275 if (status
== 'W' || status
== 'X')
277 if (extended_protocol
)
279 fprintf (stderr
, "Killing inferior\n");
282 fprintf (stderr
, "GDBserver restarting\n");
284 /* Wait till we are at 1st instruction in prog. */
285 signal
= start_inferior (&argv
[2], &status
);
291 fprintf (stderr
, "GDBserver exiting\n");
297 /* We come here when getpkt fails.
299 For the extended remote protocol we exit (and this is the only
300 way we gracefully exit!).
302 For the traditional remote protocol close the connection,
303 and re-open it at the top of the loop. */
304 if (extended_protocol
)
311 fprintf (stderr
, "Remote side has terminated connection. "
312 "GDBserver will reopen the connection.\n");