]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/server.c
1 /* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
26 int old_thread_from_wait
;
27 int extended_protocol
;
32 start_inferior (argv
, statusptr
)
36 inferior_pid
= create_inferior (argv
[0], argv
);
37 fprintf (stderr
, "Process %s created; pid = %d\n", argv
[0], inferior_pid
);
39 /* Wait till we are at 1st instruction in program, return signal number. */
40 return mywait (statusptr
);
43 extern int remote_debug
;
50 char ch
, status
, own_buf
[PBUFSIZ
], mem_buf
[2000];
56 if (setjmp (toplevel
))
58 fprintf (stderr
, "Exiting\n");
63 error ("Usage: gdbserver tty prog [args ...]");
67 /* Wait till we are at first instruction in program. */
68 signal
= start_inferior (&argv
[2], &status
);
70 /* We are now stopped at the first instruction of the target process */
74 remote_open (argv
[1]);
78 while (getpkt (own_buf
) > 0)
86 remote_debug
= !remote_debug
;
89 extended_protocol
= 1;
90 prepare_resume_reply (own_buf
, status
, signal
);
93 prepare_resume_reply (own_buf
, status
, signal
);
99 general_thread
= strtol (&own_buf
[2], NULL
, 16);
101 fetch_inferior_registers (0);
104 cont_thread
= strtol (&own_buf
[2], NULL
, 16);
108 /* Silently ignore it so that gdb can extend the protocol
109 without compatibility headaches. */
115 convert_int_to_ascii (registers
, own_buf
, REGISTER_BYTES
);
118 convert_ascii_to_int (&own_buf
[1], registers
, REGISTER_BYTES
);
119 store_inferior_registers (-1);
123 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
124 read_inferior_memory (mem_addr
, mem_buf
, len
);
125 convert_int_to_ascii (mem_buf
, own_buf
, len
);
128 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
129 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
135 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
137 signal
= mywait (&status
);
138 prepare_resume_reply (own_buf
, status
, signal
);
141 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
143 signal
= mywait (&status
);
144 prepare_resume_reply (own_buf
, status
, signal
);
148 signal
= mywait (&status
);
149 prepare_resume_reply (own_buf
, status
, signal
);
153 signal
= mywait (&status
);
154 prepare_resume_reply (own_buf
, status
, signal
);
157 fprintf (stderr
, "Killing inferior\n");
159 /* When using the extended protocol, we start up a new
160 debugging session. The traditional protocol will
162 if (extended_protocol
)
165 fprintf (stderr
, "GDBserver restarting\n");
167 /* Wait till we are at 1st instruction in prog. */
168 signal
= start_inferior (&argv
[2], &status
);
178 if (mythread_alive (strtol (&own_buf
[1], NULL
, 16)))
184 /* Restarting the inferior is only supported in the
185 extended protocol. */
186 if (extended_protocol
)
190 fprintf (stderr
, "GDBserver restarting\n");
192 /* Wait till we are at 1st instruction in prog. */
193 signal
= start_inferior (&argv
[2], &status
);
199 /* It is a request we don't understand. Respond with an
200 empty packet so that gdb knows that we don't support this
206 /* It is a request we don't understand. Respond with an
207 empty packet so that gdb knows that we don't support this
217 "\nChild exited with status %d\n", sig
);
219 fprintf (stderr
, "\nChild terminated with signal = 0x%x\n", sig
);
220 if (status
== 'W' || status
== 'X')
222 if (extended_protocol
)
224 fprintf (stderr
, "Killing inferior\n");
227 fprintf (stderr
, "GDBserver restarting\n");
229 /* Wait till we are at 1st instruction in prog. */
230 signal
= start_inferior (&argv
[2], &status
);
236 fprintf (stderr
, "GDBserver exiting\n");
242 /* We come here when getpkt fails.
244 For the extended remote protocol we exit (and this is the only
245 way we gracefully exit!).
247 For the traditional remote protocol close the connection,
248 and re-open it at the top of the loop. */
249 if (extended_protocol
)
256 fprintf (stderr
, "Remote side has terminated connection. GDBserver will reopen the connection.\n");