2002-04-20 Daniel Jacobowitz <drow@mvista.com>
[deliverable/binutils-gdb.git] / gdb / gdbserver / server.c
CommitLineData
c906108c 1/* Main code for remote server for GDB.
db728ff7 2 Copyright 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002
b6ba6518 3 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b
JM
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. */
c906108c
SS
21
22#include "server.h"
23
24int cont_thread;
25int general_thread;
26int thread_from_wait;
27int old_thread_from_wait;
28int extended_protocol;
29jmp_buf toplevel;
c906108c
SS
30
31static unsigned char
da85418c 32start_inferior (char *argv[], char *statusptr)
c906108c 33{
ce3a066d
DJ
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);
c906108c
SS
38
39 /* Wait till we are at 1st instruction in program, return signal number. */
40 return mywait (statusptr);
41}
42
45b7b345
DJ
43static int
44attach_inferior (int pid, char *statusptr, unsigned char *sigptr)
45{
46 /* myattach should return -1 if attaching is unsupported,
47 0 if it succeeded, and call error() otherwise. */
48 if (myattach (pid) != 0)
49 return -1;
50
45b7b345
DJ
51 *sigptr = mywait (statusptr);
52
53 return 0;
54}
55
c906108c 56extern int remote_debug;
ce3a066d
DJ
57
58/* Handle all of the extended 'q' packets. */
59void
60handle_query (char *own_buf)
61{
62 if (strcmp ("qSymbol::", own_buf) == 0)
63 {
2f2893d9
DJ
64 if (the_target->look_up_symbols != NULL)
65 (*the_target->look_up_symbols) ();
66
ce3a066d
DJ
67 strcpy (own_buf, "OK");
68 return;
69 }
70
71 /* Otherwise we didn't know what packet it was. Say we didn't
72 understand it. */
73 own_buf[0] = 0;
74}
75
0729219d 76static int attached;
c906108c
SS
77
78int
da85418c 79main (int argc, char *argv[])
c906108c 80{
0a30fbc4 81 char ch, status, *own_buf, mem_buf[2000];
c906108c
SS
82 int i = 0;
83 unsigned char signal;
84 unsigned int len;
85 CORE_ADDR mem_addr;
0729219d
DJ
86 int bad_attach;
87 int pid;
45b7b345 88 char *arg_end;
c906108c 89
c5aa993b 90 if (setjmp (toplevel))
c906108c 91 {
c5aa993b
JM
92 fprintf (stderr, "Exiting\n");
93 exit (1);
c906108c
SS
94 }
95
0729219d
DJ
96 bad_attach = 0;
97 pid = 0;
98 attached = 0;
45b7b345
DJ
99 if (argc >= 3 && strcmp (argv[2], "--attach") == 0)
100 {
101 if (argc == 4
102 && argv[3] != '\0'
103 && (pid = strtoul (argv[3], &arg_end, 10)) != 0
104 && *arg_end == '\0')
105 {
106 ;
107 }
108 else
109 bad_attach = 1;
110 }
111
112 if (argc < 3 || bad_attach)
113 error ("Usage:\tgdbserver tty prog [args ...]\n"
114 "\tgdbserver tty --attach pid");
c906108c 115
4ce44c66
JM
116 initialize_low ();
117
0a30fbc4
DJ
118 own_buf = malloc (PBUFSIZ);
119
45b7b345
DJ
120 if (pid == 0)
121 {
122 /* Wait till we are at first instruction in program. */
123 signal = start_inferior (&argv[2], &status);
c906108c 124
45b7b345
DJ
125 /* We are now stopped at the first instruction of the target process */
126 }
127 else
128 {
129 switch (attach_inferior (pid, &status, &signal))
130 {
131 case -1:
132 error ("Attaching not supported on this target");
133 break;
134 default:
135 attached = 1;
136 break;
137 }
138 }
c906108c
SS
139
140 while (1)
141 {
142 remote_open (argv[1]);
143
c5aa993b
JM
144 restart:
145 setjmp (toplevel);
c906108c
SS
146 while (getpkt (own_buf) > 0)
147 {
148 unsigned char sig;
149 i = 0;
150 ch = own_buf[i++];
151 switch (ch)
152 {
ce3a066d
DJ
153 case 'q':
154 handle_query (own_buf);
155 break;
c906108c
SS
156 case 'd':
157 remote_debug = !remote_debug;
158 break;
159 case '!':
45b7b345
DJ
160 if (attached == 0)
161 {
162 extended_protocol = 1;
163 prepare_resume_reply (own_buf, status, signal);
164 }
165 else
166 {
167 /* We can not use the extended protocol if we are
168 attached, because we can not restart the running
169 program. So return unrecognized. */
170 own_buf[0] = '\0';
171 }
c906108c
SS
172 break;
173 case '?':
174 prepare_resume_reply (own_buf, status, signal);
175 break;
176 case 'H':
177 switch (own_buf[1])
178 {
179 case 'g':
180 general_thread = strtol (&own_buf[2], NULL, 16);
181 write_ok (own_buf);
182 fetch_inferior_registers (0);
183 break;
184 case 'c':
185 cont_thread = strtol (&own_buf[2], NULL, 16);
186 write_ok (own_buf);
187 break;
188 default:
189 /* Silently ignore it so that gdb can extend the protocol
190 without compatibility headaches. */
191 own_buf[0] = '\0';
192 break;
193 }
194 break;
195 case 'g':
0a30fbc4 196 registers_to_string (own_buf);
c906108c
SS
197 break;
198 case 'G':
0a30fbc4 199 registers_from_string (&own_buf[1]);
c906108c
SS
200 store_inferior_registers (-1);
201 write_ok (own_buf);
202 break;
203 case 'm':
204 decode_m_packet (&own_buf[1], &mem_addr, &len);
205 read_inferior_memory (mem_addr, mem_buf, len);
206 convert_int_to_ascii (mem_buf, own_buf, len);
207 break;
208 case 'M':
209 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
210 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
211 write_ok (own_buf);
212 else
213 write_enn (own_buf);
214 break;
215 case 'C':
216 convert_ascii_to_int (own_buf + 1, &sig, 1);
0e98d0a7
DJ
217 if (target_signal_to_host_p (sig))
218 signal = target_signal_to_host (sig);
219 else
220 signal = 0;
221 myresume (0, signal);
c906108c
SS
222 signal = mywait (&status);
223 prepare_resume_reply (own_buf, status, signal);
224 break;
225 case 'S':
226 convert_ascii_to_int (own_buf + 1, &sig, 1);
0e98d0a7
DJ
227 if (target_signal_to_host_p (sig))
228 signal = target_signal_to_host (sig);
229 else
230 signal = 0;
231 myresume (1, signal);
c906108c
SS
232 signal = mywait (&status);
233 prepare_resume_reply (own_buf, status, signal);
234 break;
235 case 'c':
236 myresume (0, 0);
237 signal = mywait (&status);
238 prepare_resume_reply (own_buf, status, signal);
239 break;
240 case 's':
241 myresume (1, 0);
242 signal = mywait (&status);
243 prepare_resume_reply (own_buf, status, signal);
244 break;
245 case 'k':
246 fprintf (stderr, "Killing inferior\n");
247 kill_inferior ();
248 /* When using the extended protocol, we start up a new
c5aa993b 249 debugging session. The traditional protocol will
c906108c
SS
250 exit instead. */
251 if (extended_protocol)
252 {
253 write_ok (own_buf);
254 fprintf (stderr, "GDBserver restarting\n");
255
256 /* Wait till we are at 1st instruction in prog. */
257 signal = start_inferior (&argv[2], &status);
258 goto restart;
259 break;
260 }
261 else
262 {
263 exit (0);
264 break;
265 }
266 case 'T':
267 if (mythread_alive (strtol (&own_buf[1], NULL, 16)))
268 write_ok (own_buf);
269 else
270 write_enn (own_buf);
271 break;
272 case 'R':
273 /* Restarting the inferior is only supported in the
c5aa993b 274 extended protocol. */
c906108c
SS
275 if (extended_protocol)
276 {
277 kill_inferior ();
278 write_ok (own_buf);
279 fprintf (stderr, "GDBserver restarting\n");
280
281 /* Wait till we are at 1st instruction in prog. */
282 signal = start_inferior (&argv[2], &status);
283 goto restart;
284 break;
285 }
286 else
287 {
288 /* It is a request we don't understand. Respond with an
289 empty packet so that gdb knows that we don't support this
290 request. */
291 own_buf[0] = '\0';
292 break;
293 }
294 default:
295 /* It is a request we don't understand. Respond with an
c5aa993b
JM
296 empty packet so that gdb knows that we don't support this
297 request. */
c906108c
SS
298 own_buf[0] = '\0';
299 break;
300 }
301
302 putpkt (own_buf);
303
304 if (status == 'W')
305 fprintf (stderr,
306 "\nChild exited with status %d\n", sig);
307 if (status == 'X')
308 fprintf (stderr, "\nChild terminated with signal = 0x%x\n", sig);
309 if (status == 'W' || status == 'X')
310 {
311 if (extended_protocol)
312 {
313 fprintf (stderr, "Killing inferior\n");
314 kill_inferior ();
315 write_ok (own_buf);
316 fprintf (stderr, "GDBserver restarting\n");
317
318 /* Wait till we are at 1st instruction in prog. */
319 signal = start_inferior (&argv[2], &status);
320 goto restart;
321 break;
322 }
323 else
324 {
325 fprintf (stderr, "GDBserver exiting\n");
326 exit (0);
327 }
328 }
329 }
330
331 /* We come here when getpkt fails.
332
c5aa993b
JM
333 For the extended remote protocol we exit (and this is the only
334 way we gracefully exit!).
c906108c 335
c5aa993b
JM
336 For the traditional remote protocol close the connection,
337 and re-open it at the top of the loop. */
c906108c
SS
338 if (extended_protocol)
339 {
340 remote_close ();
341 exit (0);
342 }
343 else
344 {
45b7b345
DJ
345 fprintf (stderr, "Remote side has terminated connection. "
346 "GDBserver will reopen the connection.\n");
c906108c
SS
347 remote_close ();
348 }
349 }
350}
This page took 0.195489 seconds and 4 git commands to generate.