Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Main code for remote server for GDB. |
6f0f660e | 2 | Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003, 2004, |
dd24457d | 3 | 2005, 2006 |
b6ba6518 | 4 | Free Software Foundation, Inc. |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
c906108c | 12 | |
c5aa993b JM |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c906108c | 17 | |
c5aa993b JM |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software | |
6f0f660e EZ |
20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | Boston, MA 02110-1301, USA. */ | |
c906108c SS |
22 | |
23 | #include "server.h" | |
24 | ||
a9fa9f7d DJ |
25 | #include <unistd.h> |
26 | #include <signal.h> | |
27 | #include <sys/wait.h> | |
28 | ||
a1928bad DJ |
29 | unsigned long cont_thread; |
30 | unsigned long general_thread; | |
31 | unsigned long step_thread; | |
32 | unsigned long thread_from_wait; | |
33 | unsigned long old_thread_from_wait; | |
c906108c | 34 | int extended_protocol; |
0d62e5e8 DJ |
35 | int server_waiting; |
36 | ||
c906108c | 37 | jmp_buf toplevel; |
c906108c | 38 | |
a9fa9f7d DJ |
39 | /* The PID of the originally created or attached inferior. Used to |
40 | send signals to the process when GDB sends us an asynchronous interrupt | |
41 | (user hitting Control-C in the client), and to wait for the child to exit | |
42 | when no longer debugging it. */ | |
43 | ||
a1928bad | 44 | unsigned long signal_pid; |
a9fa9f7d | 45 | |
fc620387 | 46 | static int |
da85418c | 47 | start_inferior (char *argv[], char *statusptr) |
c906108c | 48 | { |
a9fa9f7d DJ |
49 | signal (SIGTTOU, SIG_DFL); |
50 | signal (SIGTTIN, SIG_DFL); | |
51 | ||
52 | signal_pid = create_inferior (argv[0], argv); | |
0d62e5e8 | 53 | |
a1928bad | 54 | fprintf (stderr, "Process %s created; pid = %ld\n", argv[0], |
a9fa9f7d DJ |
55 | signal_pid); |
56 | ||
57 | signal (SIGTTOU, SIG_IGN); | |
58 | signal (SIGTTIN, SIG_IGN); | |
59 | tcsetpgrp (fileno (stderr), signal_pid); | |
c906108c SS |
60 | |
61 | /* Wait till we are at 1st instruction in program, return signal number. */ | |
0d62e5e8 | 62 | return mywait (statusptr, 0); |
c906108c SS |
63 | } |
64 | ||
45b7b345 | 65 | static int |
fc620387 | 66 | attach_inferior (int pid, char *statusptr, int *sigptr) |
45b7b345 DJ |
67 | { |
68 | /* myattach should return -1 if attaching is unsupported, | |
69 | 0 if it succeeded, and call error() otherwise. */ | |
a9fa9f7d | 70 | |
45b7b345 DJ |
71 | if (myattach (pid) != 0) |
72 | return -1; | |
73 | ||
6910d122 DJ |
74 | fprintf (stderr, "Attached; pid = %d\n", pid); |
75 | ||
a9fa9f7d DJ |
76 | /* FIXME - It may be that we should get the SIGNAL_PID from the |
77 | attach function, so that it can be the main thread instead of | |
78 | whichever we were told to attach to. */ | |
79 | signal_pid = pid; | |
80 | ||
0d62e5e8 | 81 | *sigptr = mywait (statusptr, 0); |
45b7b345 | 82 | |
9db87ebd DJ |
83 | /* GDB knows to ignore the first SIGSTOP after attaching to a running |
84 | process using the "attach" command, but this is different; it's | |
85 | just using "target remote". Pretend it's just starting up. */ | |
86 | if (*statusptr == 'T' && *sigptr == SIGSTOP) | |
87 | *sigptr = SIGTRAP; | |
88 | ||
45b7b345 DJ |
89 | return 0; |
90 | } | |
91 | ||
c906108c | 92 | extern int remote_debug; |
ce3a066d DJ |
93 | |
94 | /* Handle all of the extended 'q' packets. */ | |
95 | void | |
96 | handle_query (char *own_buf) | |
97 | { | |
0d62e5e8 DJ |
98 | static struct inferior_list_entry *thread_ptr; |
99 | ||
ce3a066d DJ |
100 | if (strcmp ("qSymbol::", own_buf) == 0) |
101 | { | |
2f2893d9 DJ |
102 | if (the_target->look_up_symbols != NULL) |
103 | (*the_target->look_up_symbols) (); | |
104 | ||
ce3a066d DJ |
105 | strcpy (own_buf, "OK"); |
106 | return; | |
107 | } | |
108 | ||
0d62e5e8 DJ |
109 | if (strcmp ("qfThreadInfo", own_buf) == 0) |
110 | { | |
111 | thread_ptr = all_threads.head; | |
a06660f7 | 112 | sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr)); |
0d62e5e8 DJ |
113 | thread_ptr = thread_ptr->next; |
114 | return; | |
115 | } | |
aa691b87 | 116 | |
0d62e5e8 DJ |
117 | if (strcmp ("qsThreadInfo", own_buf) == 0) |
118 | { | |
119 | if (thread_ptr != NULL) | |
120 | { | |
a06660f7 | 121 | sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr)); |
0d62e5e8 DJ |
122 | thread_ptr = thread_ptr->next; |
123 | return; | |
124 | } | |
125 | else | |
126 | { | |
127 | sprintf (own_buf, "l"); | |
128 | return; | |
129 | } | |
130 | } | |
aa691b87 | 131 | |
52fb6437 NS |
132 | if (the_target->read_offsets != NULL |
133 | && strcmp ("qOffsets", own_buf) == 0) | |
134 | { | |
135 | CORE_ADDR text, data; | |
136 | ||
137 | if (the_target->read_offsets (&text, &data)) | |
138 | sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX", | |
139 | (long)text, (long)data, (long)data); | |
140 | else | |
141 | write_enn (own_buf); | |
142 | ||
143 | return; | |
144 | } | |
145 | ||
aa691b87 RM |
146 | if (the_target->read_auxv != NULL |
147 | && strncmp ("qPart:auxv:read::", own_buf, 17) == 0) | |
148 | { | |
f450004a | 149 | unsigned char data[(PBUFSIZ - 1) / 2]; |
aa691b87 RM |
150 | CORE_ADDR ofs; |
151 | unsigned int len; | |
152 | int n; | |
153 | decode_m_packet (&own_buf[17], &ofs, &len); /* "OFS,LEN" */ | |
154 | if (len > sizeof data) | |
155 | len = sizeof data; | |
156 | n = (*the_target->read_auxv) (ofs, data, len); | |
157 | if (n == 0) | |
158 | write_ok (own_buf); | |
159 | else if (n < 0) | |
160 | write_enn (own_buf); | |
161 | else | |
162 | convert_int_to_ascii (data, own_buf, n); | |
163 | return; | |
164 | } | |
165 | ||
be2a5f71 DJ |
166 | /* Protocol features query. */ |
167 | if (strncmp ("qSupported", own_buf, 10) == 0 | |
168 | && (own_buf[10] == ':' || own_buf[10] == '\0')) | |
169 | { | |
170 | sprintf (own_buf, "PacketSize=%x", PBUFSIZ - 1); | |
171 | return; | |
172 | } | |
173 | ||
ce3a066d DJ |
174 | /* Otherwise we didn't know what packet it was. Say we didn't |
175 | understand it. */ | |
176 | own_buf[0] = 0; | |
177 | } | |
178 | ||
64386c31 DJ |
179 | /* Parse vCont packets. */ |
180 | void | |
fc620387 | 181 | handle_v_cont (char *own_buf, char *status, int *signal) |
64386c31 DJ |
182 | { |
183 | char *p, *q; | |
184 | int n = 0, i = 0; | |
185 | struct thread_resume *resume_info, default_action; | |
186 | ||
187 | /* Count the number of semicolons in the packet. There should be one | |
188 | for every action. */ | |
189 | p = &own_buf[5]; | |
190 | while (p) | |
191 | { | |
192 | n++; | |
193 | p++; | |
194 | p = strchr (p, ';'); | |
195 | } | |
196 | /* Allocate room for one extra action, for the default remain-stopped | |
197 | behavior; if no default action is in the list, we'll need the extra | |
198 | slot. */ | |
199 | resume_info = malloc ((n + 1) * sizeof (resume_info[0])); | |
200 | ||
201 | default_action.thread = -1; | |
202 | default_action.leave_stopped = 1; | |
203 | default_action.step = 0; | |
204 | default_action.sig = 0; | |
205 | ||
206 | p = &own_buf[5]; | |
207 | i = 0; | |
208 | while (*p) | |
209 | { | |
210 | p++; | |
211 | ||
212 | resume_info[i].leave_stopped = 0; | |
213 | ||
214 | if (p[0] == 's' || p[0] == 'S') | |
215 | resume_info[i].step = 1; | |
216 | else if (p[0] == 'c' || p[0] == 'C') | |
217 | resume_info[i].step = 0; | |
218 | else | |
219 | goto err; | |
220 | ||
221 | if (p[0] == 'S' || p[0] == 'C') | |
222 | { | |
223 | int sig; | |
224 | sig = strtol (p + 1, &q, 16); | |
225 | if (p == q) | |
226 | goto err; | |
227 | p = q; | |
228 | ||
229 | if (!target_signal_to_host_p (sig)) | |
230 | goto err; | |
231 | resume_info[i].sig = target_signal_to_host (sig); | |
232 | } | |
233 | else | |
234 | { | |
235 | resume_info[i].sig = 0; | |
236 | p = p + 1; | |
237 | } | |
238 | ||
239 | if (p[0] == 0) | |
240 | { | |
241 | resume_info[i].thread = -1; | |
242 | default_action = resume_info[i]; | |
243 | ||
244 | /* Note: we don't increment i here, we'll overwrite this entry | |
245 | the next time through. */ | |
246 | } | |
247 | else if (p[0] == ':') | |
248 | { | |
a06660f7 DJ |
249 | unsigned int gdb_id = strtoul (p + 1, &q, 16); |
250 | unsigned long thread_id; | |
251 | ||
64386c31 DJ |
252 | if (p == q) |
253 | goto err; | |
254 | p = q; | |
255 | if (p[0] != ';' && p[0] != 0) | |
256 | goto err; | |
257 | ||
a06660f7 DJ |
258 | thread_id = gdb_id_to_thread_id (gdb_id); |
259 | if (thread_id) | |
260 | resume_info[i].thread = thread_id; | |
261 | else | |
262 | goto err; | |
263 | ||
64386c31 DJ |
264 | i++; |
265 | } | |
266 | } | |
267 | ||
268 | resume_info[i] = default_action; | |
269 | ||
270 | /* Still used in occasional places in the backend. */ | |
271 | if (n == 1 && resume_info[0].thread != -1) | |
272 | cont_thread = resume_info[0].thread; | |
273 | else | |
274 | cont_thread = -1; | |
dc3f8883 | 275 | set_desired_inferior (0); |
64386c31 DJ |
276 | |
277 | (*the_target->resume) (resume_info); | |
278 | ||
279 | free (resume_info); | |
280 | ||
281 | *signal = mywait (status, 1); | |
282 | prepare_resume_reply (own_buf, *status, *signal); | |
283 | return; | |
284 | ||
285 | err: | |
286 | /* No other way to report an error... */ | |
287 | strcpy (own_buf, ""); | |
288 | free (resume_info); | |
289 | return; | |
290 | } | |
291 | ||
292 | /* Handle all of the extended 'v' packets. */ | |
293 | void | |
fc620387 | 294 | handle_v_requests (char *own_buf, char *status, int *signal) |
64386c31 DJ |
295 | { |
296 | if (strncmp (own_buf, "vCont;", 6) == 0) | |
297 | { | |
298 | handle_v_cont (own_buf, status, signal); | |
299 | return; | |
300 | } | |
301 | ||
302 | if (strncmp (own_buf, "vCont?", 6) == 0) | |
303 | { | |
304 | strcpy (own_buf, "vCont;c;C;s;S"); | |
305 | return; | |
306 | } | |
307 | ||
308 | /* Otherwise we didn't know what packet it was. Say we didn't | |
309 | understand it. */ | |
310 | own_buf[0] = 0; | |
311 | return; | |
312 | } | |
313 | ||
314 | void | |
315 | myresume (int step, int sig) | |
316 | { | |
317 | struct thread_resume resume_info[2]; | |
318 | int n = 0; | |
319 | ||
d592fa2f | 320 | if (step || sig || (cont_thread != 0 && cont_thread != -1)) |
64386c31 DJ |
321 | { |
322 | resume_info[0].thread | |
323 | = ((struct inferior_list_entry *) current_inferior)->id; | |
324 | resume_info[0].step = step; | |
325 | resume_info[0].sig = sig; | |
326 | resume_info[0].leave_stopped = 0; | |
327 | n++; | |
328 | } | |
329 | resume_info[n].thread = -1; | |
330 | resume_info[n].step = 0; | |
331 | resume_info[n].sig = 0; | |
d592fa2f | 332 | resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1); |
64386c31 DJ |
333 | |
334 | (*the_target->resume) (resume_info); | |
335 | } | |
336 | ||
0729219d | 337 | static int attached; |
c906108c | 338 | |
dd24457d DJ |
339 | static void |
340 | gdbserver_version (void) | |
341 | { | |
342 | printf ("GNU gdbserver %s\n" | |
343 | "Copyright (C) 2006 Free Software Foundation, Inc.\n" | |
344 | "gdbserver is free software, covered by the GNU General Public License.\n" | |
345 | "This gdbserver was configured as \"%s\"\n", | |
346 | version, host_name); | |
347 | } | |
348 | ||
0bc68c49 DJ |
349 | static void |
350 | gdbserver_usage (void) | |
351 | { | |
dd24457d DJ |
352 | printf ("Usage:\tgdbserver COMM PROG [ARGS ...]\n" |
353 | "\tgdbserver COMM --attach PID\n" | |
354 | "\n" | |
355 | "COMM may either be a tty device (for serial debugging), or \n" | |
356 | "HOST:PORT to listen for a TCP connection.\n"); | |
0bc68c49 DJ |
357 | } |
358 | ||
c906108c | 359 | int |
da85418c | 360 | main (int argc, char *argv[]) |
c906108c | 361 | { |
f450004a | 362 | char ch, status, *own_buf; |
7fb85e41 | 363 | unsigned char *mem_buf; |
c906108c | 364 | int i = 0; |
fc620387 | 365 | int signal; |
c906108c SS |
366 | unsigned int len; |
367 | CORE_ADDR mem_addr; | |
0729219d DJ |
368 | int bad_attach; |
369 | int pid; | |
45b7b345 | 370 | char *arg_end; |
c906108c | 371 | |
dd24457d DJ |
372 | if (argc >= 2 && strcmp (argv[1], "--version") == 0) |
373 | { | |
374 | gdbserver_version (); | |
375 | exit (0); | |
376 | } | |
377 | ||
378 | if (argc >= 2 && strcmp (argv[1], "--help") == 0) | |
379 | { | |
380 | gdbserver_usage (); | |
381 | exit (0); | |
382 | } | |
383 | ||
c5aa993b | 384 | if (setjmp (toplevel)) |
c906108c | 385 | { |
c5aa993b JM |
386 | fprintf (stderr, "Exiting\n"); |
387 | exit (1); | |
c906108c SS |
388 | } |
389 | ||
0729219d DJ |
390 | bad_attach = 0; |
391 | pid = 0; | |
392 | attached = 0; | |
45b7b345 DJ |
393 | if (argc >= 3 && strcmp (argv[2], "--attach") == 0) |
394 | { | |
395 | if (argc == 4 | |
396 | && argv[3] != '\0' | |
397 | && (pid = strtoul (argv[3], &arg_end, 10)) != 0 | |
398 | && *arg_end == '\0') | |
399 | { | |
400 | ; | |
401 | } | |
402 | else | |
403 | bad_attach = 1; | |
404 | } | |
405 | ||
406 | if (argc < 3 || bad_attach) | |
dd24457d DJ |
407 | { |
408 | gdbserver_usage (); | |
409 | exit (1); | |
410 | } | |
c906108c | 411 | |
4ce44c66 JM |
412 | initialize_low (); |
413 | ||
0a30fbc4 | 414 | own_buf = malloc (PBUFSIZ); |
7fb85e41 | 415 | mem_buf = malloc (PBUFSIZ); |
0a30fbc4 | 416 | |
45b7b345 DJ |
417 | if (pid == 0) |
418 | { | |
419 | /* Wait till we are at first instruction in program. */ | |
420 | signal = start_inferior (&argv[2], &status); | |
c906108c | 421 | |
45b7b345 DJ |
422 | /* We are now stopped at the first instruction of the target process */ |
423 | } | |
424 | else | |
425 | { | |
426 | switch (attach_inferior (pid, &status, &signal)) | |
427 | { | |
428 | case -1: | |
429 | error ("Attaching not supported on this target"); | |
430 | break; | |
431 | default: | |
432 | attached = 1; | |
433 | break; | |
434 | } | |
435 | } | |
c906108c SS |
436 | |
437 | while (1) | |
438 | { | |
439 | remote_open (argv[1]); | |
440 | ||
c5aa993b JM |
441 | restart: |
442 | setjmp (toplevel); | |
c906108c SS |
443 | while (getpkt (own_buf) > 0) |
444 | { | |
445 | unsigned char sig; | |
446 | i = 0; | |
447 | ch = own_buf[i++]; | |
448 | switch (ch) | |
449 | { | |
ce3a066d DJ |
450 | case 'q': |
451 | handle_query (own_buf); | |
452 | break; | |
c906108c SS |
453 | case 'd': |
454 | remote_debug = !remote_debug; | |
455 | break; | |
6ad8ae5c DJ |
456 | case 'D': |
457 | fprintf (stderr, "Detaching from inferior\n"); | |
458 | detach_inferior (); | |
459 | write_ok (own_buf); | |
460 | putpkt (own_buf); | |
aa691b87 | 461 | remote_close (); |
6ad8ae5c DJ |
462 | |
463 | /* If we are attached, then we can exit. Otherwise, we need to | |
464 | hang around doing nothing, until the child is gone. */ | |
465 | if (!attached) | |
466 | { | |
467 | int status, ret; | |
468 | ||
469 | do { | |
470 | ret = waitpid (signal_pid, &status, 0); | |
471 | if (WIFEXITED (status) || WIFSIGNALED (status)) | |
472 | break; | |
473 | } while (ret != -1 || errno != ECHILD); | |
474 | } | |
475 | ||
476 | exit (0); | |
477 | ||
c906108c | 478 | case '!': |
45b7b345 DJ |
479 | if (attached == 0) |
480 | { | |
481 | extended_protocol = 1; | |
482 | prepare_resume_reply (own_buf, status, signal); | |
483 | } | |
484 | else | |
485 | { | |
486 | /* We can not use the extended protocol if we are | |
487 | attached, because we can not restart the running | |
488 | program. So return unrecognized. */ | |
489 | own_buf[0] = '\0'; | |
490 | } | |
c906108c SS |
491 | break; |
492 | case '?': | |
493 | prepare_resume_reply (own_buf, status, signal); | |
494 | break; | |
495 | case 'H': | |
a06660f7 | 496 | if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') |
c906108c | 497 | { |
a06660f7 DJ |
498 | unsigned long gdb_id, thread_id; |
499 | ||
500 | gdb_id = strtoul (&own_buf[2], NULL, 16); | |
501 | thread_id = gdb_id_to_thread_id (gdb_id); | |
502 | if (thread_id == 0) | |
503 | { | |
504 | write_enn (own_buf); | |
505 | break; | |
506 | } | |
507 | ||
508 | if (own_buf[1] == 'g') | |
509 | { | |
510 | general_thread = thread_id; | |
511 | set_desired_inferior (1); | |
512 | } | |
513 | else if (own_buf[1] == 'c') | |
514 | cont_thread = thread_id; | |
515 | else if (own_buf[1] == 's') | |
516 | step_thread = thread_id; | |
517 | ||
0d62e5e8 | 518 | write_ok (own_buf); |
a06660f7 DJ |
519 | } |
520 | else | |
521 | { | |
c906108c SS |
522 | /* Silently ignore it so that gdb can extend the protocol |
523 | without compatibility headaches. */ | |
524 | own_buf[0] = '\0'; | |
c906108c SS |
525 | } |
526 | break; | |
527 | case 'g': | |
0d62e5e8 | 528 | set_desired_inferior (1); |
0a30fbc4 | 529 | registers_to_string (own_buf); |
c906108c SS |
530 | break; |
531 | case 'G': | |
0d62e5e8 | 532 | set_desired_inferior (1); |
0a30fbc4 | 533 | registers_from_string (&own_buf[1]); |
c906108c SS |
534 | write_ok (own_buf); |
535 | break; | |
536 | case 'm': | |
537 | decode_m_packet (&own_buf[1], &mem_addr, &len); | |
c3e735a6 DJ |
538 | if (read_inferior_memory (mem_addr, mem_buf, len) == 0) |
539 | convert_int_to_ascii (mem_buf, own_buf, len); | |
540 | else | |
541 | write_enn (own_buf); | |
c906108c SS |
542 | break; |
543 | case 'M': | |
544 | decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf); | |
545 | if (write_inferior_memory (mem_addr, mem_buf, len) == 0) | |
546 | write_ok (own_buf); | |
547 | else | |
548 | write_enn (own_buf); | |
549 | break; | |
550 | case 'C': | |
551 | convert_ascii_to_int (own_buf + 1, &sig, 1); | |
0e98d0a7 DJ |
552 | if (target_signal_to_host_p (sig)) |
553 | signal = target_signal_to_host (sig); | |
554 | else | |
555 | signal = 0; | |
0d62e5e8 | 556 | set_desired_inferior (0); |
0e98d0a7 | 557 | myresume (0, signal); |
0d62e5e8 | 558 | signal = mywait (&status, 1); |
c906108c SS |
559 | prepare_resume_reply (own_buf, status, signal); |
560 | break; | |
561 | case 'S': | |
562 | convert_ascii_to_int (own_buf + 1, &sig, 1); | |
0e98d0a7 DJ |
563 | if (target_signal_to_host_p (sig)) |
564 | signal = target_signal_to_host (sig); | |
565 | else | |
566 | signal = 0; | |
0d62e5e8 | 567 | set_desired_inferior (0); |
0e98d0a7 | 568 | myresume (1, signal); |
0d62e5e8 | 569 | signal = mywait (&status, 1); |
c906108c SS |
570 | prepare_resume_reply (own_buf, status, signal); |
571 | break; | |
572 | case 'c': | |
0d62e5e8 | 573 | set_desired_inferior (0); |
c906108c | 574 | myresume (0, 0); |
0d62e5e8 | 575 | signal = mywait (&status, 1); |
c906108c SS |
576 | prepare_resume_reply (own_buf, status, signal); |
577 | break; | |
578 | case 's': | |
0d62e5e8 | 579 | set_desired_inferior (0); |
c906108c | 580 | myresume (1, 0); |
0d62e5e8 | 581 | signal = mywait (&status, 1); |
c906108c SS |
582 | prepare_resume_reply (own_buf, status, signal); |
583 | break; | |
e013ee27 OF |
584 | case 'Z': |
585 | { | |
586 | char *lenptr; | |
587 | char *dataptr; | |
588 | CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); | |
589 | int len = strtol (lenptr + 1, &dataptr, 16); | |
590 | char type = own_buf[1]; | |
591 | ||
592 | if (the_target->insert_watchpoint == NULL | |
593 | || (type < '2' || type > '4')) | |
594 | { | |
595 | /* No watchpoint support or not a watchpoint command; | |
596 | unrecognized either way. */ | |
597 | own_buf[0] = '\0'; | |
598 | } | |
599 | else | |
600 | { | |
601 | int res; | |
602 | ||
603 | res = (*the_target->insert_watchpoint) (type, addr, len); | |
604 | if (res == 0) | |
605 | write_ok (own_buf); | |
606 | else if (res == 1) | |
607 | /* Unsupported. */ | |
608 | own_buf[0] = '\0'; | |
609 | else | |
610 | write_enn (own_buf); | |
611 | } | |
612 | break; | |
613 | } | |
614 | case 'z': | |
615 | { | |
616 | char *lenptr; | |
617 | char *dataptr; | |
618 | CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); | |
619 | int len = strtol (lenptr + 1, &dataptr, 16); | |
620 | char type = own_buf[1]; | |
621 | ||
622 | if (the_target->remove_watchpoint == NULL | |
623 | || (type < '2' || type > '4')) | |
624 | { | |
625 | /* No watchpoint support or not a watchpoint command; | |
626 | unrecognized either way. */ | |
627 | own_buf[0] = '\0'; | |
628 | } | |
629 | else | |
630 | { | |
631 | int res; | |
632 | ||
633 | res = (*the_target->remove_watchpoint) (type, addr, len); | |
634 | if (res == 0) | |
635 | write_ok (own_buf); | |
636 | else if (res == 1) | |
637 | /* Unsupported. */ | |
638 | own_buf[0] = '\0'; | |
639 | else | |
640 | write_enn (own_buf); | |
641 | } | |
642 | break; | |
643 | } | |
c906108c SS |
644 | case 'k': |
645 | fprintf (stderr, "Killing inferior\n"); | |
646 | kill_inferior (); | |
647 | /* When using the extended protocol, we start up a new | |
c5aa993b | 648 | debugging session. The traditional protocol will |
c906108c SS |
649 | exit instead. */ |
650 | if (extended_protocol) | |
651 | { | |
652 | write_ok (own_buf); | |
653 | fprintf (stderr, "GDBserver restarting\n"); | |
654 | ||
655 | /* Wait till we are at 1st instruction in prog. */ | |
656 | signal = start_inferior (&argv[2], &status); | |
657 | goto restart; | |
658 | break; | |
659 | } | |
660 | else | |
661 | { | |
662 | exit (0); | |
663 | break; | |
664 | } | |
665 | case 'T': | |
a06660f7 DJ |
666 | { |
667 | unsigned long gdb_id, thread_id; | |
668 | ||
669 | gdb_id = strtoul (&own_buf[1], NULL, 16); | |
670 | thread_id = gdb_id_to_thread_id (gdb_id); | |
671 | if (thread_id == 0) | |
672 | { | |
673 | write_enn (own_buf); | |
674 | break; | |
675 | } | |
676 | ||
677 | if (mythread_alive (thread_id)) | |
678 | write_ok (own_buf); | |
679 | else | |
680 | write_enn (own_buf); | |
681 | } | |
c906108c SS |
682 | break; |
683 | case 'R': | |
684 | /* Restarting the inferior is only supported in the | |
c5aa993b | 685 | extended protocol. */ |
c906108c SS |
686 | if (extended_protocol) |
687 | { | |
688 | kill_inferior (); | |
689 | write_ok (own_buf); | |
690 | fprintf (stderr, "GDBserver restarting\n"); | |
691 | ||
692 | /* Wait till we are at 1st instruction in prog. */ | |
693 | signal = start_inferior (&argv[2], &status); | |
694 | goto restart; | |
695 | break; | |
696 | } | |
697 | else | |
698 | { | |
699 | /* It is a request we don't understand. Respond with an | |
700 | empty packet so that gdb knows that we don't support this | |
701 | request. */ | |
702 | own_buf[0] = '\0'; | |
703 | break; | |
704 | } | |
64386c31 DJ |
705 | case 'v': |
706 | /* Extended (long) request. */ | |
707 | handle_v_requests (own_buf, &status, &signal); | |
708 | break; | |
c906108c SS |
709 | default: |
710 | /* It is a request we don't understand. Respond with an | |
c5aa993b JM |
711 | empty packet so that gdb knows that we don't support this |
712 | request. */ | |
c906108c SS |
713 | own_buf[0] = '\0'; |
714 | break; | |
715 | } | |
716 | ||
717 | putpkt (own_buf); | |
718 | ||
719 | if (status == 'W') | |
720 | fprintf (stderr, | |
3a7fb99b | 721 | "\nChild exited with status %d\n", signal); |
c906108c | 722 | if (status == 'X') |
3a7fb99b DJ |
723 | fprintf (stderr, "\nChild terminated with signal = 0x%x\n", |
724 | signal); | |
c906108c SS |
725 | if (status == 'W' || status == 'X') |
726 | { | |
727 | if (extended_protocol) | |
728 | { | |
729 | fprintf (stderr, "Killing inferior\n"); | |
730 | kill_inferior (); | |
731 | write_ok (own_buf); | |
732 | fprintf (stderr, "GDBserver restarting\n"); | |
733 | ||
734 | /* Wait till we are at 1st instruction in prog. */ | |
735 | signal = start_inferior (&argv[2], &status); | |
736 | goto restart; | |
737 | break; | |
738 | } | |
739 | else | |
740 | { | |
741 | fprintf (stderr, "GDBserver exiting\n"); | |
742 | exit (0); | |
743 | } | |
744 | } | |
745 | } | |
746 | ||
747 | /* We come here when getpkt fails. | |
748 | ||
c5aa993b JM |
749 | For the extended remote protocol we exit (and this is the only |
750 | way we gracefully exit!). | |
c906108c | 751 | |
c5aa993b JM |
752 | For the traditional remote protocol close the connection, |
753 | and re-open it at the top of the loop. */ | |
c906108c SS |
754 | if (extended_protocol) |
755 | { | |
756 | remote_close (); | |
757 | exit (0); | |
758 | } | |
759 | else | |
760 | { | |
45b7b345 DJ |
761 | fprintf (stderr, "Remote side has terminated connection. " |
762 | "GDBserver will reopen the connection.\n"); | |
c906108c SS |
763 | remote_close (); |
764 | } | |
765 | } | |
766 | } |