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