]>
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
;
61 main (int argc
, char *argv
[])
63 char ch
, status
, *own_buf
, mem_buf
[2000];
72 if (setjmp (toplevel
))
74 fprintf (stderr
, "Exiting\n");
81 if (argc
>= 3 && strcmp (argv
[2], "--attach") == 0)
85 && (pid
= strtoul (argv
[3], &arg_end
, 10)) != 0
94 if (argc
< 3 || bad_attach
)
95 error ("Usage:\tgdbserver tty prog [args ...]\n"
96 "\tgdbserver tty --attach pid");
100 own_buf
= malloc (PBUFSIZ
);
104 /* Wait till we are at first instruction in program. */
105 signal
= start_inferior (&argv
[2], &status
);
107 /* We are now stopped at the first instruction of the target process */
111 switch (attach_inferior (pid
, &status
, &signal
))
114 error ("Attaching not supported on this target");
124 remote_open (argv
[1]);
128 while (getpkt (own_buf
) > 0)
136 remote_debug
= !remote_debug
;
141 extended_protocol
= 1;
142 prepare_resume_reply (own_buf
, status
, signal
);
146 /* We can not use the extended protocol if we are
147 attached, because we can not restart the running
148 program. So return unrecognized. */
153 prepare_resume_reply (own_buf
, status
, signal
);
159 general_thread
= strtol (&own_buf
[2], NULL
, 16);
161 fetch_inferior_registers (0);
164 cont_thread
= strtol (&own_buf
[2], NULL
, 16);
168 /* Silently ignore it so that gdb can extend the protocol
169 without compatibility headaches. */
175 registers_to_string (own_buf
);
178 registers_from_string (&own_buf
[1]);
179 store_inferior_registers (-1);
183 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
184 read_inferior_memory (mem_addr
, mem_buf
, len
);
185 convert_int_to_ascii (mem_buf
, own_buf
, len
);
188 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
189 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
195 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
196 if (target_signal_to_host_p (sig
))
197 signal
= target_signal_to_host (sig
);
200 myresume (0, signal
);
201 signal
= mywait (&status
);
202 prepare_resume_reply (own_buf
, status
, signal
);
205 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
206 if (target_signal_to_host_p (sig
))
207 signal
= target_signal_to_host (sig
);
210 myresume (1, signal
);
211 signal
= mywait (&status
);
212 prepare_resume_reply (own_buf
, status
, signal
);
216 signal
= mywait (&status
);
217 prepare_resume_reply (own_buf
, status
, signal
);
221 signal
= mywait (&status
);
222 prepare_resume_reply (own_buf
, status
, signal
);
225 fprintf (stderr
, "Killing inferior\n");
227 /* When using the extended protocol, we start up a new
228 debugging session. The traditional protocol will
230 if (extended_protocol
)
233 fprintf (stderr
, "GDBserver restarting\n");
235 /* Wait till we are at 1st instruction in prog. */
236 signal
= start_inferior (&argv
[2], &status
);
246 if (mythread_alive (strtol (&own_buf
[1], NULL
, 16)))
252 /* Restarting the inferior is only supported in the
253 extended protocol. */
254 if (extended_protocol
)
258 fprintf (stderr
, "GDBserver restarting\n");
260 /* Wait till we are at 1st instruction in prog. */
261 signal
= start_inferior (&argv
[2], &status
);
267 /* It is a request we don't understand. Respond with an
268 empty packet so that gdb knows that we don't support this
274 /* It is a request we don't understand. Respond with an
275 empty packet so that gdb knows that we don't support this
285 "\nChild exited with status %d\n", sig
);
287 fprintf (stderr
, "\nChild terminated with signal = 0x%x\n", sig
);
288 if (status
== 'W' || status
== 'X')
290 if (extended_protocol
)
292 fprintf (stderr
, "Killing inferior\n");
295 fprintf (stderr
, "GDBserver restarting\n");
297 /* Wait till we are at 1st instruction in prog. */
298 signal
= start_inferior (&argv
[2], &status
);
304 fprintf (stderr
, "GDBserver exiting\n");
310 /* We come here when getpkt fails.
312 For the extended remote protocol we exit (and this is the only
313 way we gracefully exit!).
315 For the traditional remote protocol close the connection,
316 and re-open it at the top of the loop. */
317 if (extended_protocol
)
324 fprintf (stderr
, "Remote side has terminated connection. "
325 "GDBserver will reopen the connection.\n");