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