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 | 93 | |
0876f84a DJ |
94 | /* Decode a qXfer read request. Return 0 if everything looks OK, |
95 | or -1 otherwise. */ | |
96 | ||
97 | static int | |
98 | decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len) | |
99 | { | |
100 | /* Extract and NUL-terminate the annex. */ | |
101 | *annex = buf; | |
102 | while (*buf && *buf != ':') | |
103 | buf++; | |
104 | if (*buf == '\0') | |
105 | return -1; | |
106 | *buf++ = 0; | |
107 | ||
108 | /* After the read/write marker and annex, qXfer looks like a | |
109 | traditional 'm' packet. */ | |
110 | decode_m_packet (buf, ofs, len); | |
111 | ||
112 | return 0; | |
113 | } | |
114 | ||
115 | /* Write the response to a successful qXfer read. Returns the | |
116 | length of the (binary) data stored in BUF, corresponding | |
117 | to as much of DATA/LEN as we could fit. IS_MORE controls | |
118 | the first character of the response. */ | |
119 | static int | |
120 | write_qxfer_response (char *buf, unsigned char *data, int len, int is_more) | |
121 | { | |
122 | int out_len; | |
123 | ||
124 | if (is_more) | |
125 | buf[0] = 'm'; | |
126 | else | |
127 | buf[0] = 'l'; | |
128 | ||
129 | return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len, | |
130 | PBUFSIZ - 2) + 1; | |
131 | } | |
132 | ||
ce3a066d DJ |
133 | /* Handle all of the extended 'q' packets. */ |
134 | void | |
0876f84a | 135 | handle_query (char *own_buf, int *new_packet_len_p) |
ce3a066d | 136 | { |
0d62e5e8 DJ |
137 | static struct inferior_list_entry *thread_ptr; |
138 | ||
ce3a066d DJ |
139 | if (strcmp ("qSymbol::", own_buf) == 0) |
140 | { | |
2f2893d9 DJ |
141 | if (the_target->look_up_symbols != NULL) |
142 | (*the_target->look_up_symbols) (); | |
143 | ||
ce3a066d DJ |
144 | strcpy (own_buf, "OK"); |
145 | return; | |
146 | } | |
147 | ||
0d62e5e8 DJ |
148 | if (strcmp ("qfThreadInfo", own_buf) == 0) |
149 | { | |
150 | thread_ptr = all_threads.head; | |
a06660f7 | 151 | sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr)); |
0d62e5e8 DJ |
152 | thread_ptr = thread_ptr->next; |
153 | return; | |
154 | } | |
aa691b87 | 155 | |
0d62e5e8 DJ |
156 | if (strcmp ("qsThreadInfo", own_buf) == 0) |
157 | { | |
158 | if (thread_ptr != NULL) | |
159 | { | |
a06660f7 | 160 | sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr)); |
0d62e5e8 DJ |
161 | thread_ptr = thread_ptr->next; |
162 | return; | |
163 | } | |
164 | else | |
165 | { | |
166 | sprintf (own_buf, "l"); | |
167 | return; | |
168 | } | |
169 | } | |
aa691b87 | 170 | |
52fb6437 NS |
171 | if (the_target->read_offsets != NULL |
172 | && strcmp ("qOffsets", own_buf) == 0) | |
173 | { | |
174 | CORE_ADDR text, data; | |
175 | ||
176 | if (the_target->read_offsets (&text, &data)) | |
177 | sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX", | |
178 | (long)text, (long)data, (long)data); | |
179 | else | |
180 | write_enn (own_buf); | |
181 | ||
182 | return; | |
183 | } | |
184 | ||
aa691b87 | 185 | if (the_target->read_auxv != NULL |
0876f84a | 186 | && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0) |
aa691b87 | 187 | { |
0876f84a DJ |
188 | unsigned char *data; |
189 | int n; | |
aa691b87 RM |
190 | CORE_ADDR ofs; |
191 | unsigned int len; | |
0876f84a DJ |
192 | char *annex; |
193 | ||
194 | /* Reject any annex; grab the offset and length. */ | |
195 | if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0 | |
196 | || annex[0] != '\0') | |
197 | { | |
198 | strcpy (own_buf, "E00"); | |
199 | return; | |
200 | } | |
201 | ||
202 | /* Read one extra byte, as an indicator of whether there is | |
203 | more. */ | |
204 | if (len > PBUFSIZ - 2) | |
205 | len = PBUFSIZ - 2; | |
206 | data = malloc (len + 1); | |
207 | n = (*the_target->read_auxv) (ofs, data, len + 1); | |
208 | if (n > len) | |
209 | *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1); | |
aa691b87 | 210 | else |
0876f84a DJ |
211 | *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0); |
212 | ||
213 | free (data); | |
214 | ||
aa691b87 RM |
215 | return; |
216 | } | |
217 | ||
be2a5f71 DJ |
218 | /* Protocol features query. */ |
219 | if (strncmp ("qSupported", own_buf, 10) == 0 | |
220 | && (own_buf[10] == ':' || own_buf[10] == '\0')) | |
221 | { | |
222 | sprintf (own_buf, "PacketSize=%x", PBUFSIZ - 1); | |
0876f84a DJ |
223 | |
224 | if (the_target->read_auxv != NULL) | |
225 | strcat (own_buf, ";qPart:auxv:read+"); | |
226 | ||
be2a5f71 DJ |
227 | return; |
228 | } | |
229 | ||
ce3a066d DJ |
230 | /* Otherwise we didn't know what packet it was. Say we didn't |
231 | understand it. */ | |
232 | own_buf[0] = 0; | |
233 | } | |
234 | ||
64386c31 DJ |
235 | /* Parse vCont packets. */ |
236 | void | |
fc620387 | 237 | handle_v_cont (char *own_buf, char *status, int *signal) |
64386c31 DJ |
238 | { |
239 | char *p, *q; | |
240 | int n = 0, i = 0; | |
241 | struct thread_resume *resume_info, default_action; | |
242 | ||
243 | /* Count the number of semicolons in the packet. There should be one | |
244 | for every action. */ | |
245 | p = &own_buf[5]; | |
246 | while (p) | |
247 | { | |
248 | n++; | |
249 | p++; | |
250 | p = strchr (p, ';'); | |
251 | } | |
252 | /* Allocate room for one extra action, for the default remain-stopped | |
253 | behavior; if no default action is in the list, we'll need the extra | |
254 | slot. */ | |
255 | resume_info = malloc ((n + 1) * sizeof (resume_info[0])); | |
256 | ||
257 | default_action.thread = -1; | |
258 | default_action.leave_stopped = 1; | |
259 | default_action.step = 0; | |
260 | default_action.sig = 0; | |
261 | ||
262 | p = &own_buf[5]; | |
263 | i = 0; | |
264 | while (*p) | |
265 | { | |
266 | p++; | |
267 | ||
268 | resume_info[i].leave_stopped = 0; | |
269 | ||
270 | if (p[0] == 's' || p[0] == 'S') | |
271 | resume_info[i].step = 1; | |
272 | else if (p[0] == 'c' || p[0] == 'C') | |
273 | resume_info[i].step = 0; | |
274 | else | |
275 | goto err; | |
276 | ||
277 | if (p[0] == 'S' || p[0] == 'C') | |
278 | { | |
279 | int sig; | |
280 | sig = strtol (p + 1, &q, 16); | |
281 | if (p == q) | |
282 | goto err; | |
283 | p = q; | |
284 | ||
285 | if (!target_signal_to_host_p (sig)) | |
286 | goto err; | |
287 | resume_info[i].sig = target_signal_to_host (sig); | |
288 | } | |
289 | else | |
290 | { | |
291 | resume_info[i].sig = 0; | |
292 | p = p + 1; | |
293 | } | |
294 | ||
295 | if (p[0] == 0) | |
296 | { | |
297 | resume_info[i].thread = -1; | |
298 | default_action = resume_info[i]; | |
299 | ||
300 | /* Note: we don't increment i here, we'll overwrite this entry | |
301 | the next time through. */ | |
302 | } | |
303 | else if (p[0] == ':') | |
304 | { | |
a06660f7 DJ |
305 | unsigned int gdb_id = strtoul (p + 1, &q, 16); |
306 | unsigned long thread_id; | |
307 | ||
64386c31 DJ |
308 | if (p == q) |
309 | goto err; | |
310 | p = q; | |
311 | if (p[0] != ';' && p[0] != 0) | |
312 | goto err; | |
313 | ||
a06660f7 DJ |
314 | thread_id = gdb_id_to_thread_id (gdb_id); |
315 | if (thread_id) | |
316 | resume_info[i].thread = thread_id; | |
317 | else | |
318 | goto err; | |
319 | ||
64386c31 DJ |
320 | i++; |
321 | } | |
322 | } | |
323 | ||
324 | resume_info[i] = default_action; | |
325 | ||
326 | /* Still used in occasional places in the backend. */ | |
327 | if (n == 1 && resume_info[0].thread != -1) | |
328 | cont_thread = resume_info[0].thread; | |
329 | else | |
330 | cont_thread = -1; | |
dc3f8883 | 331 | set_desired_inferior (0); |
64386c31 DJ |
332 | |
333 | (*the_target->resume) (resume_info); | |
334 | ||
335 | free (resume_info); | |
336 | ||
337 | *signal = mywait (status, 1); | |
338 | prepare_resume_reply (own_buf, *status, *signal); | |
339 | return; | |
340 | ||
341 | err: | |
342 | /* No other way to report an error... */ | |
343 | strcpy (own_buf, ""); | |
344 | free (resume_info); | |
345 | return; | |
346 | } | |
347 | ||
348 | /* Handle all of the extended 'v' packets. */ | |
349 | void | |
fc620387 | 350 | handle_v_requests (char *own_buf, char *status, int *signal) |
64386c31 DJ |
351 | { |
352 | if (strncmp (own_buf, "vCont;", 6) == 0) | |
353 | { | |
354 | handle_v_cont (own_buf, status, signal); | |
355 | return; | |
356 | } | |
357 | ||
358 | if (strncmp (own_buf, "vCont?", 6) == 0) | |
359 | { | |
360 | strcpy (own_buf, "vCont;c;C;s;S"); | |
361 | return; | |
362 | } | |
363 | ||
364 | /* Otherwise we didn't know what packet it was. Say we didn't | |
365 | understand it. */ | |
366 | own_buf[0] = 0; | |
367 | return; | |
368 | } | |
369 | ||
370 | void | |
371 | myresume (int step, int sig) | |
372 | { | |
373 | struct thread_resume resume_info[2]; | |
374 | int n = 0; | |
375 | ||
d592fa2f | 376 | if (step || sig || (cont_thread != 0 && cont_thread != -1)) |
64386c31 DJ |
377 | { |
378 | resume_info[0].thread | |
379 | = ((struct inferior_list_entry *) current_inferior)->id; | |
380 | resume_info[0].step = step; | |
381 | resume_info[0].sig = sig; | |
382 | resume_info[0].leave_stopped = 0; | |
383 | n++; | |
384 | } | |
385 | resume_info[n].thread = -1; | |
386 | resume_info[n].step = 0; | |
387 | resume_info[n].sig = 0; | |
d592fa2f | 388 | resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1); |
64386c31 DJ |
389 | |
390 | (*the_target->resume) (resume_info); | |
391 | } | |
392 | ||
0729219d | 393 | static int attached; |
c906108c | 394 | |
dd24457d DJ |
395 | static void |
396 | gdbserver_version (void) | |
397 | { | |
398 | printf ("GNU gdbserver %s\n" | |
399 | "Copyright (C) 2006 Free Software Foundation, Inc.\n" | |
400 | "gdbserver is free software, covered by the GNU General Public License.\n" | |
401 | "This gdbserver was configured as \"%s\"\n", | |
402 | version, host_name); | |
403 | } | |
404 | ||
0bc68c49 DJ |
405 | static void |
406 | gdbserver_usage (void) | |
407 | { | |
dd24457d DJ |
408 | printf ("Usage:\tgdbserver COMM PROG [ARGS ...]\n" |
409 | "\tgdbserver COMM --attach PID\n" | |
410 | "\n" | |
411 | "COMM may either be a tty device (for serial debugging), or \n" | |
412 | "HOST:PORT to listen for a TCP connection.\n"); | |
0bc68c49 DJ |
413 | } |
414 | ||
c906108c | 415 | int |
da85418c | 416 | main (int argc, char *argv[]) |
c906108c | 417 | { |
f450004a | 418 | char ch, status, *own_buf; |
7fb85e41 | 419 | unsigned char *mem_buf; |
c906108c | 420 | int i = 0; |
fc620387 | 421 | int signal; |
c906108c SS |
422 | unsigned int len; |
423 | CORE_ADDR mem_addr; | |
0729219d DJ |
424 | int bad_attach; |
425 | int pid; | |
45b7b345 | 426 | char *arg_end; |
c906108c | 427 | |
dd24457d DJ |
428 | if (argc >= 2 && strcmp (argv[1], "--version") == 0) |
429 | { | |
430 | gdbserver_version (); | |
431 | exit (0); | |
432 | } | |
433 | ||
434 | if (argc >= 2 && strcmp (argv[1], "--help") == 0) | |
435 | { | |
436 | gdbserver_usage (); | |
437 | exit (0); | |
438 | } | |
439 | ||
c5aa993b | 440 | if (setjmp (toplevel)) |
c906108c | 441 | { |
c5aa993b JM |
442 | fprintf (stderr, "Exiting\n"); |
443 | exit (1); | |
c906108c SS |
444 | } |
445 | ||
0729219d DJ |
446 | bad_attach = 0; |
447 | pid = 0; | |
448 | attached = 0; | |
45b7b345 DJ |
449 | if (argc >= 3 && strcmp (argv[2], "--attach") == 0) |
450 | { | |
451 | if (argc == 4 | |
452 | && argv[3] != '\0' | |
453 | && (pid = strtoul (argv[3], &arg_end, 10)) != 0 | |
454 | && *arg_end == '\0') | |
455 | { | |
456 | ; | |
457 | } | |
458 | else | |
459 | bad_attach = 1; | |
460 | } | |
461 | ||
462 | if (argc < 3 || bad_attach) | |
dd24457d DJ |
463 | { |
464 | gdbserver_usage (); | |
465 | exit (1); | |
466 | } | |
c906108c | 467 | |
4ce44c66 JM |
468 | initialize_low (); |
469 | ||
0a30fbc4 | 470 | own_buf = malloc (PBUFSIZ); |
7fb85e41 | 471 | mem_buf = malloc (PBUFSIZ); |
0a30fbc4 | 472 | |
45b7b345 DJ |
473 | if (pid == 0) |
474 | { | |
475 | /* Wait till we are at first instruction in program. */ | |
476 | signal = start_inferior (&argv[2], &status); | |
c906108c | 477 | |
45b7b345 DJ |
478 | /* We are now stopped at the first instruction of the target process */ |
479 | } | |
480 | else | |
481 | { | |
482 | switch (attach_inferior (pid, &status, &signal)) | |
483 | { | |
484 | case -1: | |
485 | error ("Attaching not supported on this target"); | |
486 | break; | |
487 | default: | |
488 | attached = 1; | |
489 | break; | |
490 | } | |
491 | } | |
c906108c SS |
492 | |
493 | while (1) | |
494 | { | |
495 | remote_open (argv[1]); | |
496 | ||
c5aa993b JM |
497 | restart: |
498 | setjmp (toplevel); | |
01f9e8fa | 499 | while (1) |
c906108c SS |
500 | { |
501 | unsigned char sig; | |
01f9e8fa DJ |
502 | int packet_len; |
503 | int new_packet_len = -1; | |
504 | ||
505 | packet_len = getpkt (own_buf); | |
506 | if (packet_len <= 0) | |
507 | break; | |
508 | ||
c906108c SS |
509 | i = 0; |
510 | ch = own_buf[i++]; | |
511 | switch (ch) | |
512 | { | |
ce3a066d | 513 | case 'q': |
0876f84a | 514 | handle_query (own_buf, &new_packet_len); |
ce3a066d | 515 | break; |
c906108c SS |
516 | case 'd': |
517 | remote_debug = !remote_debug; | |
518 | break; | |
6ad8ae5c DJ |
519 | case 'D': |
520 | fprintf (stderr, "Detaching from inferior\n"); | |
521 | detach_inferior (); | |
522 | write_ok (own_buf); | |
523 | putpkt (own_buf); | |
aa691b87 | 524 | remote_close (); |
6ad8ae5c DJ |
525 | |
526 | /* If we are attached, then we can exit. Otherwise, we need to | |
527 | hang around doing nothing, until the child is gone. */ | |
528 | if (!attached) | |
529 | { | |
530 | int status, ret; | |
531 | ||
532 | do { | |
533 | ret = waitpid (signal_pid, &status, 0); | |
534 | if (WIFEXITED (status) || WIFSIGNALED (status)) | |
535 | break; | |
536 | } while (ret != -1 || errno != ECHILD); | |
537 | } | |
538 | ||
539 | exit (0); | |
540 | ||
c906108c | 541 | case '!': |
45b7b345 DJ |
542 | if (attached == 0) |
543 | { | |
544 | extended_protocol = 1; | |
545 | prepare_resume_reply (own_buf, status, signal); | |
546 | } | |
547 | else | |
548 | { | |
549 | /* We can not use the extended protocol if we are | |
550 | attached, because we can not restart the running | |
551 | program. So return unrecognized. */ | |
552 | own_buf[0] = '\0'; | |
553 | } | |
c906108c SS |
554 | break; |
555 | case '?': | |
556 | prepare_resume_reply (own_buf, status, signal); | |
557 | break; | |
558 | case 'H': | |
a06660f7 | 559 | if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') |
c906108c | 560 | { |
a06660f7 DJ |
561 | unsigned long gdb_id, thread_id; |
562 | ||
563 | gdb_id = strtoul (&own_buf[2], NULL, 16); | |
564 | thread_id = gdb_id_to_thread_id (gdb_id); | |
565 | if (thread_id == 0) | |
566 | { | |
567 | write_enn (own_buf); | |
568 | break; | |
569 | } | |
570 | ||
571 | if (own_buf[1] == 'g') | |
572 | { | |
573 | general_thread = thread_id; | |
574 | set_desired_inferior (1); | |
575 | } | |
576 | else if (own_buf[1] == 'c') | |
577 | cont_thread = thread_id; | |
578 | else if (own_buf[1] == 's') | |
579 | step_thread = thread_id; | |
580 | ||
0d62e5e8 | 581 | write_ok (own_buf); |
a06660f7 DJ |
582 | } |
583 | else | |
584 | { | |
c906108c SS |
585 | /* Silently ignore it so that gdb can extend the protocol |
586 | without compatibility headaches. */ | |
587 | own_buf[0] = '\0'; | |
c906108c SS |
588 | } |
589 | break; | |
590 | case 'g': | |
0d62e5e8 | 591 | set_desired_inferior (1); |
0a30fbc4 | 592 | registers_to_string (own_buf); |
c906108c SS |
593 | break; |
594 | case 'G': | |
0d62e5e8 | 595 | set_desired_inferior (1); |
0a30fbc4 | 596 | registers_from_string (&own_buf[1]); |
c906108c SS |
597 | write_ok (own_buf); |
598 | break; | |
599 | case 'm': | |
600 | decode_m_packet (&own_buf[1], &mem_addr, &len); | |
c3e735a6 DJ |
601 | if (read_inferior_memory (mem_addr, mem_buf, len) == 0) |
602 | convert_int_to_ascii (mem_buf, own_buf, len); | |
603 | else | |
604 | write_enn (own_buf); | |
c906108c SS |
605 | break; |
606 | case 'M': | |
607 | decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf); | |
608 | if (write_inferior_memory (mem_addr, mem_buf, len) == 0) | |
609 | write_ok (own_buf); | |
610 | else | |
611 | write_enn (own_buf); | |
612 | break; | |
01f9e8fa DJ |
613 | case 'X': |
614 | if (decode_X_packet (&own_buf[1], packet_len - 1, | |
615 | &mem_addr, &len, mem_buf) < 0 | |
616 | || write_inferior_memory (mem_addr, mem_buf, len) != 0) | |
617 | write_enn (own_buf); | |
618 | else | |
619 | write_ok (own_buf); | |
620 | break; | |
c906108c SS |
621 | case 'C': |
622 | convert_ascii_to_int (own_buf + 1, &sig, 1); | |
0e98d0a7 DJ |
623 | if (target_signal_to_host_p (sig)) |
624 | signal = target_signal_to_host (sig); | |
625 | else | |
626 | signal = 0; | |
0d62e5e8 | 627 | set_desired_inferior (0); |
0e98d0a7 | 628 | myresume (0, signal); |
0d62e5e8 | 629 | signal = mywait (&status, 1); |
c906108c SS |
630 | prepare_resume_reply (own_buf, status, signal); |
631 | break; | |
632 | case 'S': | |
633 | convert_ascii_to_int (own_buf + 1, &sig, 1); | |
0e98d0a7 DJ |
634 | if (target_signal_to_host_p (sig)) |
635 | signal = target_signal_to_host (sig); | |
636 | else | |
637 | signal = 0; | |
0d62e5e8 | 638 | set_desired_inferior (0); |
0e98d0a7 | 639 | myresume (1, signal); |
0d62e5e8 | 640 | signal = mywait (&status, 1); |
c906108c SS |
641 | prepare_resume_reply (own_buf, status, signal); |
642 | break; | |
643 | case 'c': | |
0d62e5e8 | 644 | set_desired_inferior (0); |
c906108c | 645 | myresume (0, 0); |
0d62e5e8 | 646 | signal = mywait (&status, 1); |
c906108c SS |
647 | prepare_resume_reply (own_buf, status, signal); |
648 | break; | |
649 | case 's': | |
0d62e5e8 | 650 | set_desired_inferior (0); |
c906108c | 651 | myresume (1, 0); |
0d62e5e8 | 652 | signal = mywait (&status, 1); |
c906108c SS |
653 | prepare_resume_reply (own_buf, status, signal); |
654 | break; | |
e013ee27 OF |
655 | case 'Z': |
656 | { | |
657 | char *lenptr; | |
658 | char *dataptr; | |
659 | CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); | |
660 | int len = strtol (lenptr + 1, &dataptr, 16); | |
661 | char type = own_buf[1]; | |
662 | ||
663 | if (the_target->insert_watchpoint == NULL | |
664 | || (type < '2' || type > '4')) | |
665 | { | |
666 | /* No watchpoint support or not a watchpoint command; | |
667 | unrecognized either way. */ | |
668 | own_buf[0] = '\0'; | |
669 | } | |
670 | else | |
671 | { | |
672 | int res; | |
673 | ||
674 | res = (*the_target->insert_watchpoint) (type, addr, len); | |
675 | if (res == 0) | |
676 | write_ok (own_buf); | |
677 | else if (res == 1) | |
678 | /* Unsupported. */ | |
679 | own_buf[0] = '\0'; | |
680 | else | |
681 | write_enn (own_buf); | |
682 | } | |
683 | break; | |
684 | } | |
685 | case 'z': | |
686 | { | |
687 | char *lenptr; | |
688 | char *dataptr; | |
689 | CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); | |
690 | int len = strtol (lenptr + 1, &dataptr, 16); | |
691 | char type = own_buf[1]; | |
692 | ||
693 | if (the_target->remove_watchpoint == NULL | |
694 | || (type < '2' || type > '4')) | |
695 | { | |
696 | /* No watchpoint support or not a watchpoint command; | |
697 | unrecognized either way. */ | |
698 | own_buf[0] = '\0'; | |
699 | } | |
700 | else | |
701 | { | |
702 | int res; | |
703 | ||
704 | res = (*the_target->remove_watchpoint) (type, addr, len); | |
705 | if (res == 0) | |
706 | write_ok (own_buf); | |
707 | else if (res == 1) | |
708 | /* Unsupported. */ | |
709 | own_buf[0] = '\0'; | |
710 | else | |
711 | write_enn (own_buf); | |
712 | } | |
713 | break; | |
714 | } | |
c906108c SS |
715 | case 'k': |
716 | fprintf (stderr, "Killing inferior\n"); | |
717 | kill_inferior (); | |
718 | /* When using the extended protocol, we start up a new | |
c5aa993b | 719 | debugging session. The traditional protocol will |
c906108c SS |
720 | exit instead. */ |
721 | if (extended_protocol) | |
722 | { | |
723 | write_ok (own_buf); | |
724 | fprintf (stderr, "GDBserver restarting\n"); | |
725 | ||
726 | /* Wait till we are at 1st instruction in prog. */ | |
727 | signal = start_inferior (&argv[2], &status); | |
728 | goto restart; | |
729 | break; | |
730 | } | |
731 | else | |
732 | { | |
733 | exit (0); | |
734 | break; | |
735 | } | |
736 | case 'T': | |
a06660f7 DJ |
737 | { |
738 | unsigned long gdb_id, thread_id; | |
739 | ||
740 | gdb_id = strtoul (&own_buf[1], NULL, 16); | |
741 | thread_id = gdb_id_to_thread_id (gdb_id); | |
742 | if (thread_id == 0) | |
743 | { | |
744 | write_enn (own_buf); | |
745 | break; | |
746 | } | |
747 | ||
748 | if (mythread_alive (thread_id)) | |
749 | write_ok (own_buf); | |
750 | else | |
751 | write_enn (own_buf); | |
752 | } | |
c906108c SS |
753 | break; |
754 | case 'R': | |
755 | /* Restarting the inferior is only supported in the | |
c5aa993b | 756 | extended protocol. */ |
c906108c SS |
757 | if (extended_protocol) |
758 | { | |
759 | kill_inferior (); | |
760 | write_ok (own_buf); | |
761 | fprintf (stderr, "GDBserver restarting\n"); | |
762 | ||
763 | /* Wait till we are at 1st instruction in prog. */ | |
764 | signal = start_inferior (&argv[2], &status); | |
765 | goto restart; | |
766 | break; | |
767 | } | |
768 | else | |
769 | { | |
770 | /* It is a request we don't understand. Respond with an | |
771 | empty packet so that gdb knows that we don't support this | |
772 | request. */ | |
773 | own_buf[0] = '\0'; | |
774 | break; | |
775 | } | |
64386c31 DJ |
776 | case 'v': |
777 | /* Extended (long) request. */ | |
778 | handle_v_requests (own_buf, &status, &signal); | |
779 | break; | |
c906108c SS |
780 | default: |
781 | /* It is a request we don't understand. Respond with an | |
c5aa993b JM |
782 | empty packet so that gdb knows that we don't support this |
783 | request. */ | |
c906108c SS |
784 | own_buf[0] = '\0'; |
785 | break; | |
786 | } | |
787 | ||
01f9e8fa DJ |
788 | if (new_packet_len != -1) |
789 | putpkt_binary (own_buf, new_packet_len); | |
790 | else | |
791 | putpkt (own_buf); | |
c906108c SS |
792 | |
793 | if (status == 'W') | |
794 | fprintf (stderr, | |
3a7fb99b | 795 | "\nChild exited with status %d\n", signal); |
c906108c | 796 | if (status == 'X') |
3a7fb99b DJ |
797 | fprintf (stderr, "\nChild terminated with signal = 0x%x\n", |
798 | signal); | |
c906108c SS |
799 | if (status == 'W' || status == 'X') |
800 | { | |
801 | if (extended_protocol) | |
802 | { | |
803 | fprintf (stderr, "Killing inferior\n"); | |
804 | kill_inferior (); | |
805 | write_ok (own_buf); | |
806 | fprintf (stderr, "GDBserver restarting\n"); | |
807 | ||
808 | /* Wait till we are at 1st instruction in prog. */ | |
809 | signal = start_inferior (&argv[2], &status); | |
810 | goto restart; | |
811 | break; | |
812 | } | |
813 | else | |
814 | { | |
815 | fprintf (stderr, "GDBserver exiting\n"); | |
816 | exit (0); | |
817 | } | |
818 | } | |
819 | } | |
820 | ||
821 | /* We come here when getpkt fails. | |
822 | ||
c5aa993b JM |
823 | For the extended remote protocol we exit (and this is the only |
824 | way we gracefully exit!). | |
c906108c | 825 | |
c5aa993b JM |
826 | For the traditional remote protocol close the connection, |
827 | and re-open it at the top of the loop. */ | |
c906108c SS |
828 | if (extended_protocol) |
829 | { | |
830 | remote_close (); | |
831 | exit (0); | |
832 | } | |
833 | else | |
834 | { | |
45b7b345 DJ |
835 | fprintf (stderr, "Remote side has terminated connection. " |
836 | "GDBserver will reopen the connection.\n"); | |
c906108c SS |
837 | remote_close (); |
838 | } | |
839 | } | |
840 | } |