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