Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Main code for remote server for GDB. |
6aba47ca | 2 | Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003, |
9b254dd1 | 3 | 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 10 | (at your option) any later version. |
c906108c | 11 | |
c5aa993b JM |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
c906108c | 16 | |
c5aa993b | 17 | You should have received a copy of the GNU General Public License |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
19 | |
20 | #include "server.h" | |
21 | ||
68070c10 | 22 | #if HAVE_UNISTD_H |
a9fa9f7d | 23 | #include <unistd.h> |
68070c10 PA |
24 | #endif |
25 | #if HAVE_SIGNAL_H | |
a9fa9f7d | 26 | #include <signal.h> |
68070c10 | 27 | #endif |
b80864fb | 28 | #if HAVE_SYS_WAIT_H |
a9fa9f7d | 29 | #include <sys/wait.h> |
b80864fb | 30 | #endif |
a9fa9f7d | 31 | |
a1928bad DJ |
32 | unsigned long cont_thread; |
33 | unsigned long general_thread; | |
34 | unsigned long step_thread; | |
35 | unsigned long thread_from_wait; | |
36 | unsigned long old_thread_from_wait; | |
0d62e5e8 DJ |
37 | int server_waiting; |
38 | ||
2d717e4f DJ |
39 | static int extended_protocol; |
40 | static int attached; | |
41 | static int response_needed; | |
42 | static int exit_requested; | |
43 | ||
ccd213ac | 44 | static char **program_argv, **wrapper_argv; |
2d717e4f | 45 | |
c74d0ad8 DJ |
46 | /* Enable miscellaneous debugging output. The name is historical - it |
47 | was originally used to debug LinuxThreads support. */ | |
48 | int debug_threads; | |
49 | ||
89be2091 DJ |
50 | int pass_signals[TARGET_SIGNAL_LAST]; |
51 | ||
c906108c | 52 | jmp_buf toplevel; |
c906108c | 53 | |
9b4b61c8 UW |
54 | const char *gdbserver_xmltarget; |
55 | ||
a9fa9f7d DJ |
56 | /* The PID of the originally created or attached inferior. Used to |
57 | send signals to the process when GDB sends us an asynchronous interrupt | |
58 | (user hitting Control-C in the client), and to wait for the child to exit | |
59 | when no longer debugging it. */ | |
60 | ||
a1928bad | 61 | unsigned long signal_pid; |
a9fa9f7d | 62 | |
290fadea RS |
63 | #ifdef SIGTTOU |
64 | /* A file descriptor for the controlling terminal. */ | |
65 | int terminal_fd; | |
66 | ||
67 | /* TERMINAL_FD's original foreground group. */ | |
68 | pid_t old_foreground_pgrp; | |
69 | ||
70 | /* Hand back terminal ownership to the original foreground group. */ | |
71 | ||
72 | static void | |
73 | restore_old_foreground_pgrp (void) | |
74 | { | |
75 | tcsetpgrp (terminal_fd, old_foreground_pgrp); | |
76 | } | |
77 | #endif | |
78 | ||
2d717e4f DJ |
79 | static int |
80 | target_running (void) | |
81 | { | |
82 | return all_threads.head != NULL; | |
83 | } | |
84 | ||
fc620387 | 85 | static int |
ccd213ac | 86 | start_inferior (char **argv, char *statusptr) |
c906108c | 87 | { |
ccd213ac | 88 | char **new_argv = argv; |
2d717e4f DJ |
89 | attached = 0; |
90 | ||
ccd213ac DJ |
91 | if (wrapper_argv != NULL) |
92 | { | |
93 | int i, count = 1; | |
94 | ||
95 | for (i = 0; wrapper_argv[i] != NULL; i++) | |
96 | count++; | |
97 | for (i = 0; argv[i] != NULL; i++) | |
98 | count++; | |
99 | new_argv = alloca (sizeof (char *) * count); | |
100 | count = 0; | |
101 | for (i = 0; wrapper_argv[i] != NULL; i++) | |
102 | new_argv[count++] = wrapper_argv[i]; | |
103 | for (i = 0; argv[i] != NULL; i++) | |
104 | new_argv[count++] = argv[i]; | |
105 | new_argv[count] = NULL; | |
106 | } | |
107 | ||
b80864fb | 108 | #ifdef SIGTTOU |
a9fa9f7d DJ |
109 | signal (SIGTTOU, SIG_DFL); |
110 | signal (SIGTTIN, SIG_DFL); | |
b80864fb | 111 | #endif |
a9fa9f7d | 112 | |
ccd213ac | 113 | signal_pid = create_inferior (new_argv[0], new_argv); |
0d62e5e8 | 114 | |
c588c53c MS |
115 | /* FIXME: we don't actually know at this point that the create |
116 | actually succeeded. We won't know that until we wait. */ | |
a1928bad | 117 | fprintf (stderr, "Process %s created; pid = %ld\n", argv[0], |
a9fa9f7d | 118 | signal_pid); |
b80864fb | 119 | fflush (stderr); |
a9fa9f7d | 120 | |
b80864fb | 121 | #ifdef SIGTTOU |
a9fa9f7d DJ |
122 | signal (SIGTTOU, SIG_IGN); |
123 | signal (SIGTTIN, SIG_IGN); | |
290fadea RS |
124 | terminal_fd = fileno (stderr); |
125 | old_foreground_pgrp = tcgetpgrp (terminal_fd); | |
126 | tcsetpgrp (terminal_fd, signal_pid); | |
127 | atexit (restore_old_foreground_pgrp); | |
b80864fb | 128 | #endif |
c906108c | 129 | |
ccd213ac DJ |
130 | if (wrapper_argv != NULL) |
131 | { | |
132 | struct thread_resume resume_info; | |
133 | int sig; | |
134 | ||
135 | resume_info.thread = -1; | |
136 | resume_info.step = 0; | |
137 | resume_info.sig = 0; | |
138 | resume_info.leave_stopped = 0; | |
139 | ||
140 | sig = mywait (statusptr, 0); | |
141 | if (*statusptr != 'T') | |
142 | return sig; | |
143 | ||
144 | do | |
145 | { | |
146 | (*the_target->resume) (&resume_info); | |
147 | ||
148 | sig = mywait (statusptr, 0); | |
149 | if (*statusptr != 'T') | |
150 | return sig; | |
151 | } | |
152 | while (sig != TARGET_SIGNAL_TRAP); | |
153 | ||
154 | return sig; | |
155 | } | |
156 | ||
c588c53c MS |
157 | /* Wait till we are at 1st instruction in program, return signal |
158 | number (assuming success). */ | |
0d62e5e8 | 159 | return mywait (statusptr, 0); |
c906108c SS |
160 | } |
161 | ||
45b7b345 | 162 | static int |
fc620387 | 163 | attach_inferior (int pid, char *statusptr, int *sigptr) |
45b7b345 DJ |
164 | { |
165 | /* myattach should return -1 if attaching is unsupported, | |
166 | 0 if it succeeded, and call error() otherwise. */ | |
a9fa9f7d | 167 | |
45b7b345 DJ |
168 | if (myattach (pid) != 0) |
169 | return -1; | |
170 | ||
2d717e4f DJ |
171 | attached = 1; |
172 | ||
6910d122 | 173 | fprintf (stderr, "Attached; pid = %d\n", pid); |
b80864fb | 174 | fflush (stderr); |
6910d122 | 175 | |
a9fa9f7d DJ |
176 | /* FIXME - It may be that we should get the SIGNAL_PID from the |
177 | attach function, so that it can be the main thread instead of | |
178 | whichever we were told to attach to. */ | |
179 | signal_pid = pid; | |
180 | ||
0d62e5e8 | 181 | *sigptr = mywait (statusptr, 0); |
45b7b345 | 182 | |
9db87ebd DJ |
183 | /* GDB knows to ignore the first SIGSTOP after attaching to a running |
184 | process using the "attach" command, but this is different; it's | |
185 | just using "target remote". Pretend it's just starting up. */ | |
b80864fb DJ |
186 | if (*statusptr == 'T' && *sigptr == TARGET_SIGNAL_STOP) |
187 | *sigptr = TARGET_SIGNAL_TRAP; | |
9db87ebd | 188 | |
45b7b345 DJ |
189 | return 0; |
190 | } | |
191 | ||
c906108c | 192 | extern int remote_debug; |
ce3a066d | 193 | |
0876f84a DJ |
194 | /* Decode a qXfer read request. Return 0 if everything looks OK, |
195 | or -1 otherwise. */ | |
196 | ||
197 | static int | |
198 | decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len) | |
199 | { | |
200 | /* Extract and NUL-terminate the annex. */ | |
201 | *annex = buf; | |
202 | while (*buf && *buf != ':') | |
203 | buf++; | |
204 | if (*buf == '\0') | |
205 | return -1; | |
206 | *buf++ = 0; | |
207 | ||
0e7f50da | 208 | /* After the read marker and annex, qXfer looks like a |
0876f84a DJ |
209 | traditional 'm' packet. */ |
210 | decode_m_packet (buf, ofs, len); | |
211 | ||
212 | return 0; | |
213 | } | |
214 | ||
215 | /* Write the response to a successful qXfer read. Returns the | |
216 | length of the (binary) data stored in BUF, corresponding | |
217 | to as much of DATA/LEN as we could fit. IS_MORE controls | |
218 | the first character of the response. */ | |
219 | static int | |
23181151 | 220 | write_qxfer_response (char *buf, const void *data, int len, int is_more) |
0876f84a DJ |
221 | { |
222 | int out_len; | |
223 | ||
224 | if (is_more) | |
225 | buf[0] = 'm'; | |
226 | else | |
227 | buf[0] = 'l'; | |
228 | ||
229 | return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len, | |
230 | PBUFSIZ - 2) + 1; | |
231 | } | |
232 | ||
89be2091 DJ |
233 | /* Handle all of the extended 'Q' packets. */ |
234 | void | |
235 | handle_general_set (char *own_buf) | |
236 | { | |
237 | if (strncmp ("QPassSignals:", own_buf, strlen ("QPassSignals:")) == 0) | |
238 | { | |
239 | int numsigs = (int) TARGET_SIGNAL_LAST, i; | |
240 | const char *p = own_buf + strlen ("QPassSignals:"); | |
241 | CORE_ADDR cursig; | |
242 | ||
243 | p = decode_address_to_semicolon (&cursig, p); | |
244 | for (i = 0; i < numsigs; i++) | |
245 | { | |
246 | if (i == cursig) | |
247 | { | |
248 | pass_signals[i] = 1; | |
249 | if (*p == '\0') | |
250 | /* Keep looping, to clear the remaining signals. */ | |
251 | cursig = -1; | |
252 | else | |
253 | p = decode_address_to_semicolon (&cursig, p); | |
254 | } | |
255 | else | |
256 | pass_signals[i] = 0; | |
257 | } | |
258 | strcpy (own_buf, "OK"); | |
259 | return; | |
260 | } | |
261 | ||
262 | /* Otherwise we didn't know what packet it was. Say we didn't | |
263 | understand it. */ | |
264 | own_buf[0] = 0; | |
265 | } | |
266 | ||
23181151 | 267 | static const char * |
fb1e4ffc | 268 | get_features_xml (const char *annex) |
23181151 | 269 | { |
9b4b61c8 UW |
270 | /* gdbserver_xmltarget defines what to return when looking |
271 | for the "target.xml" file. Its contents can either be | |
272 | verbatim XML code (prefixed with a '@') or else the name | |
273 | of the actual XML file to be used in place of "target.xml". | |
fb1e4ffc | 274 | |
9b4b61c8 UW |
275 | This variable is set up from the auto-generated |
276 | init_registers_... routine for the current target. */ | |
fb1e4ffc | 277 | |
9b4b61c8 UW |
278 | if (gdbserver_xmltarget |
279 | && strcmp (annex, "target.xml") != 0) | |
23181151 | 280 | { |
9b4b61c8 UW |
281 | if (*gdbserver_xmltarget == '@') |
282 | return gdbserver_xmltarget + 1; | |
23181151 | 283 | else |
9b4b61c8 | 284 | annex = gdbserver_xmltarget; |
23181151 DJ |
285 | } |
286 | ||
9b4b61c8 UW |
287 | #ifdef USE_XML |
288 | { | |
289 | extern const char *const xml_builtin[][2]; | |
290 | int i; | |
291 | ||
292 | /* Look for the annex. */ | |
293 | for (i = 0; xml_builtin[i][0] != NULL; i++) | |
294 | if (strcmp (annex, xml_builtin[i][0]) == 0) | |
295 | break; | |
296 | ||
297 | if (xml_builtin[i][0] != NULL) | |
298 | return xml_builtin[i][1]; | |
299 | } | |
300 | #endif | |
301 | ||
302 | return NULL; | |
23181151 DJ |
303 | } |
304 | ||
c74d0ad8 DJ |
305 | void |
306 | monitor_show_help (void) | |
307 | { | |
308 | monitor_output ("The following monitor commands are supported:\n"); | |
309 | monitor_output (" set debug <0|1>\n"); | |
310 | monitor_output (" Enable general debugging messages\n"); | |
311 | monitor_output (" set remote-debug <0|1>\n"); | |
312 | monitor_output (" Enable remote protocol debugging messages\n"); | |
ecd7ecbc DJ |
313 | monitor_output (" exit\n"); |
314 | monitor_output (" Quit GDBserver\n"); | |
c74d0ad8 DJ |
315 | } |
316 | ||
2d717e4f DJ |
317 | #define require_running(BUF) \ |
318 | if (!target_running ()) \ | |
319 | { \ | |
320 | write_enn (BUF); \ | |
321 | return; \ | |
322 | } | |
323 | ||
ce3a066d DJ |
324 | /* Handle all of the extended 'q' packets. */ |
325 | void | |
0e7f50da | 326 | handle_query (char *own_buf, int packet_len, int *new_packet_len_p) |
ce3a066d | 327 | { |
0d62e5e8 DJ |
328 | static struct inferior_list_entry *thread_ptr; |
329 | ||
bb63802a UW |
330 | /* Reply the current thread id. */ |
331 | if (strcmp ("qC", own_buf) == 0) | |
332 | { | |
2d717e4f | 333 | require_running (own_buf); |
bb63802a UW |
334 | thread_ptr = all_threads.head; |
335 | sprintf (own_buf, "QC%x", | |
336 | thread_to_gdb_id ((struct thread_info *)thread_ptr)); | |
337 | return; | |
338 | } | |
339 | ||
ce3a066d DJ |
340 | if (strcmp ("qSymbol::", own_buf) == 0) |
341 | { | |
2d717e4f | 342 | if (target_running () && the_target->look_up_symbols != NULL) |
2f2893d9 DJ |
343 | (*the_target->look_up_symbols) (); |
344 | ||
ce3a066d DJ |
345 | strcpy (own_buf, "OK"); |
346 | return; | |
347 | } | |
348 | ||
0d62e5e8 DJ |
349 | if (strcmp ("qfThreadInfo", own_buf) == 0) |
350 | { | |
2d717e4f | 351 | require_running (own_buf); |
0d62e5e8 | 352 | thread_ptr = all_threads.head; |
a06660f7 | 353 | sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr)); |
0d62e5e8 DJ |
354 | thread_ptr = thread_ptr->next; |
355 | return; | |
356 | } | |
aa691b87 | 357 | |
0d62e5e8 DJ |
358 | if (strcmp ("qsThreadInfo", own_buf) == 0) |
359 | { | |
2d717e4f | 360 | require_running (own_buf); |
0d62e5e8 DJ |
361 | if (thread_ptr != NULL) |
362 | { | |
a06660f7 | 363 | sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr)); |
0d62e5e8 DJ |
364 | thread_ptr = thread_ptr->next; |
365 | return; | |
366 | } | |
367 | else | |
368 | { | |
369 | sprintf (own_buf, "l"); | |
370 | return; | |
371 | } | |
372 | } | |
aa691b87 | 373 | |
52fb6437 NS |
374 | if (the_target->read_offsets != NULL |
375 | && strcmp ("qOffsets", own_buf) == 0) | |
376 | { | |
377 | CORE_ADDR text, data; | |
2d717e4f DJ |
378 | |
379 | require_running (own_buf); | |
52fb6437 NS |
380 | if (the_target->read_offsets (&text, &data)) |
381 | sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX", | |
382 | (long)text, (long)data, (long)data); | |
383 | else | |
384 | write_enn (own_buf); | |
385 | ||
386 | return; | |
387 | } | |
388 | ||
0e7f50da UW |
389 | if (the_target->qxfer_spu != NULL |
390 | && strncmp ("qXfer:spu:read:", own_buf, 15) == 0) | |
391 | { | |
392 | char *annex; | |
393 | int n; | |
394 | unsigned int len; | |
395 | CORE_ADDR ofs; | |
396 | unsigned char *spu_buf; | |
397 | ||
2d717e4f | 398 | require_running (own_buf); |
0e7f50da UW |
399 | strcpy (own_buf, "E00"); |
400 | if (decode_xfer_read (own_buf + 15, &annex, &ofs, &len) < 0) | |
401 | return; | |
402 | if (len > PBUFSIZ - 2) | |
403 | len = PBUFSIZ - 2; | |
404 | spu_buf = malloc (len + 1); | |
405 | if (!spu_buf) | |
406 | return; | |
407 | ||
408 | n = (*the_target->qxfer_spu) (annex, spu_buf, NULL, ofs, len + 1); | |
409 | if (n < 0) | |
410 | write_enn (own_buf); | |
411 | else if (n > len) | |
412 | *new_packet_len_p = write_qxfer_response | |
413 | (own_buf, spu_buf, len, 1); | |
414 | else | |
415 | *new_packet_len_p = write_qxfer_response | |
416 | (own_buf, spu_buf, n, 0); | |
417 | ||
418 | free (spu_buf); | |
419 | return; | |
420 | } | |
421 | ||
422 | if (the_target->qxfer_spu != NULL | |
423 | && strncmp ("qXfer:spu:write:", own_buf, 16) == 0) | |
424 | { | |
425 | char *annex; | |
426 | int n; | |
427 | unsigned int len; | |
428 | CORE_ADDR ofs; | |
429 | unsigned char *spu_buf; | |
430 | ||
2d717e4f | 431 | require_running (own_buf); |
0e7f50da UW |
432 | strcpy (own_buf, "E00"); |
433 | spu_buf = malloc (packet_len - 15); | |
434 | if (!spu_buf) | |
435 | return; | |
436 | if (decode_xfer_write (own_buf + 16, packet_len - 16, &annex, | |
437 | &ofs, &len, spu_buf) < 0) | |
438 | { | |
439 | free (spu_buf); | |
440 | return; | |
441 | } | |
442 | ||
443 | n = (*the_target->qxfer_spu) | |
444 | (annex, NULL, (unsigned const char *)spu_buf, ofs, len); | |
445 | if (n < 0) | |
446 | write_enn (own_buf); | |
447 | else | |
448 | sprintf (own_buf, "%x", n); | |
449 | ||
450 | free (spu_buf); | |
451 | return; | |
452 | } | |
453 | ||
aa691b87 | 454 | if (the_target->read_auxv != NULL |
0876f84a | 455 | && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0) |
aa691b87 | 456 | { |
0876f84a DJ |
457 | unsigned char *data; |
458 | int n; | |
aa691b87 RM |
459 | CORE_ADDR ofs; |
460 | unsigned int len; | |
0876f84a DJ |
461 | char *annex; |
462 | ||
2d717e4f DJ |
463 | require_running (own_buf); |
464 | ||
0876f84a DJ |
465 | /* Reject any annex; grab the offset and length. */ |
466 | if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0 | |
467 | || annex[0] != '\0') | |
468 | { | |
469 | strcpy (own_buf, "E00"); | |
470 | return; | |
471 | } | |
472 | ||
473 | /* Read one extra byte, as an indicator of whether there is | |
474 | more. */ | |
475 | if (len > PBUFSIZ - 2) | |
476 | len = PBUFSIZ - 2; | |
477 | data = malloc (len + 1); | |
478 | n = (*the_target->read_auxv) (ofs, data, len + 1); | |
000ef4f0 DJ |
479 | if (n < 0) |
480 | write_enn (own_buf); | |
481 | else if (n > len) | |
0876f84a | 482 | *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1); |
aa691b87 | 483 | else |
0876f84a DJ |
484 | *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0); |
485 | ||
486 | free (data); | |
487 | ||
aa691b87 RM |
488 | return; |
489 | } | |
490 | ||
23181151 DJ |
491 | if (strncmp ("qXfer:features:read:", own_buf, 20) == 0) |
492 | { | |
493 | CORE_ADDR ofs; | |
494 | unsigned int len, total_len; | |
495 | const char *document; | |
496 | char *annex; | |
497 | ||
2d717e4f DJ |
498 | require_running (own_buf); |
499 | ||
fb1e4ffc DJ |
500 | /* Check for support. */ |
501 | document = get_features_xml ("target.xml"); | |
23181151 DJ |
502 | if (document == NULL) |
503 | { | |
504 | own_buf[0] = '\0'; | |
505 | return; | |
506 | } | |
507 | ||
fb1e4ffc DJ |
508 | /* Grab the annex, offset, and length. */ |
509 | if (decode_xfer_read (own_buf + 20, &annex, &ofs, &len) < 0) | |
510 | { | |
511 | strcpy (own_buf, "E00"); | |
512 | return; | |
513 | } | |
514 | ||
515 | /* Now grab the correct annex. */ | |
516 | document = get_features_xml (annex); | |
517 | if (document == NULL) | |
23181151 DJ |
518 | { |
519 | strcpy (own_buf, "E00"); | |
520 | return; | |
521 | } | |
522 | ||
523 | total_len = strlen (document); | |
524 | if (len > PBUFSIZ - 2) | |
525 | len = PBUFSIZ - 2; | |
526 | ||
527 | if (ofs > total_len) | |
528 | write_enn (own_buf); | |
529 | else if (len < total_len - ofs) | |
530 | *new_packet_len_p = write_qxfer_response (own_buf, document + ofs, | |
531 | len, 1); | |
532 | else | |
533 | *new_packet_len_p = write_qxfer_response (own_buf, document + ofs, | |
534 | total_len - ofs, 0); | |
535 | ||
536 | return; | |
537 | } | |
538 | ||
255e7678 DJ |
539 | if (strncmp ("qXfer:libraries:read:", own_buf, 21) == 0) |
540 | { | |
541 | CORE_ADDR ofs; | |
542 | unsigned int len, total_len; | |
543 | char *document, *p; | |
544 | struct inferior_list_entry *dll_ptr; | |
545 | char *annex; | |
546 | ||
2d717e4f DJ |
547 | require_running (own_buf); |
548 | ||
255e7678 DJ |
549 | /* Reject any annex; grab the offset and length. */ |
550 | if (decode_xfer_read (own_buf + 21, &annex, &ofs, &len) < 0 | |
551 | || annex[0] != '\0') | |
552 | { | |
553 | strcpy (own_buf, "E00"); | |
554 | return; | |
555 | } | |
556 | ||
557 | /* Over-estimate the necessary memory. Assume that every character | |
558 | in the library name must be escaped. */ | |
559 | total_len = 64; | |
560 | for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next) | |
561 | total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name); | |
562 | ||
563 | document = malloc (total_len); | |
564 | strcpy (document, "<library-list>\n"); | |
565 | p = document + strlen (document); | |
566 | ||
567 | for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next) | |
568 | { | |
569 | struct dll_info *dll = (struct dll_info *) dll_ptr; | |
570 | char *name; | |
571 | ||
572 | strcpy (p, " <library name=\""); | |
573 | p = p + strlen (p); | |
574 | name = xml_escape_text (dll->name); | |
575 | strcpy (p, name); | |
576 | free (name); | |
577 | p = p + strlen (p); | |
578 | strcpy (p, "\"><segment address=\""); | |
579 | p = p + strlen (p); | |
580 | sprintf (p, "0x%lx", (long) dll->base_addr); | |
581 | p = p + strlen (p); | |
582 | strcpy (p, "\"/></library>\n"); | |
583 | p = p + strlen (p); | |
584 | } | |
585 | ||
586 | strcpy (p, "</library-list>\n"); | |
587 | ||
588 | total_len = strlen (document); | |
589 | if (len > PBUFSIZ - 2) | |
590 | len = PBUFSIZ - 2; | |
591 | ||
592 | if (ofs > total_len) | |
593 | write_enn (own_buf); | |
594 | else if (len < total_len - ofs) | |
595 | *new_packet_len_p = write_qxfer_response (own_buf, document + ofs, | |
596 | len, 1); | |
597 | else | |
598 | *new_packet_len_p = write_qxfer_response (own_buf, document + ofs, | |
599 | total_len - ofs, 0); | |
600 | ||
601 | free (document); | |
602 | return; | |
603 | } | |
604 | ||
be2a5f71 DJ |
605 | /* Protocol features query. */ |
606 | if (strncmp ("qSupported", own_buf, 10) == 0 | |
607 | && (own_buf[10] == ':' || own_buf[10] == '\0')) | |
608 | { | |
89be2091 | 609 | sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1); |
0876f84a | 610 | |
255e7678 DJ |
611 | /* We do not have any hook to indicate whether the target backend |
612 | supports qXfer:libraries:read, so always report it. */ | |
613 | strcat (own_buf, ";qXfer:libraries:read+"); | |
614 | ||
0876f84a | 615 | if (the_target->read_auxv != NULL) |
9f2e1e63 | 616 | strcat (own_buf, ";qXfer:auxv:read+"); |
2d717e4f | 617 | |
0e7f50da UW |
618 | if (the_target->qxfer_spu != NULL) |
619 | strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+"); | |
0876f84a | 620 | |
fb1e4ffc | 621 | if (get_features_xml ("target.xml") != NULL) |
23181151 DJ |
622 | strcat (own_buf, ";qXfer:features:read+"); |
623 | ||
be2a5f71 DJ |
624 | return; |
625 | } | |
626 | ||
dae5f5cf DJ |
627 | /* Thread-local storage support. */ |
628 | if (the_target->get_tls_address != NULL | |
629 | && strncmp ("qGetTLSAddr:", own_buf, 12) == 0) | |
630 | { | |
631 | char *p = own_buf + 12; | |
632 | CORE_ADDR parts[3], address = 0; | |
633 | int i, err; | |
634 | ||
2d717e4f DJ |
635 | require_running (own_buf); |
636 | ||
dae5f5cf DJ |
637 | for (i = 0; i < 3; i++) |
638 | { | |
639 | char *p2; | |
640 | int len; | |
641 | ||
642 | if (p == NULL) | |
643 | break; | |
644 | ||
645 | p2 = strchr (p, ','); | |
646 | if (p2) | |
647 | { | |
648 | len = p2 - p; | |
649 | p2++; | |
650 | } | |
651 | else | |
652 | { | |
653 | len = strlen (p); | |
654 | p2 = NULL; | |
655 | } | |
656 | ||
657 | decode_address (&parts[i], p, len); | |
658 | p = p2; | |
659 | } | |
660 | ||
661 | if (p != NULL || i < 3) | |
662 | err = 1; | |
663 | else | |
664 | { | |
665 | struct thread_info *thread = gdb_id_to_thread (parts[0]); | |
666 | ||
667 | if (thread == NULL) | |
668 | err = 2; | |
669 | else | |
670 | err = the_target->get_tls_address (thread, parts[1], parts[2], | |
671 | &address); | |
672 | } | |
673 | ||
674 | if (err == 0) | |
675 | { | |
676 | sprintf (own_buf, "%llx", address); | |
677 | return; | |
678 | } | |
679 | else if (err > 0) | |
680 | { | |
681 | write_enn (own_buf); | |
682 | return; | |
683 | } | |
684 | ||
685 | /* Otherwise, pretend we do not understand this packet. */ | |
686 | } | |
687 | ||
c74d0ad8 DJ |
688 | /* Handle "monitor" commands. */ |
689 | if (strncmp ("qRcmd,", own_buf, 6) == 0) | |
690 | { | |
691 | char *mon = malloc (PBUFSIZ); | |
692 | int len = strlen (own_buf + 6); | |
693 | ||
d41b6bb4 | 694 | if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2) |
c74d0ad8 DJ |
695 | { |
696 | write_enn (own_buf); | |
697 | free (mon); | |
698 | return; | |
699 | } | |
700 | mon[len / 2] = '\0'; | |
701 | ||
702 | write_ok (own_buf); | |
703 | ||
704 | if (strcmp (mon, "set debug 1") == 0) | |
705 | { | |
706 | debug_threads = 1; | |
707 | monitor_output ("Debug output enabled.\n"); | |
708 | } | |
709 | else if (strcmp (mon, "set debug 0") == 0) | |
710 | { | |
711 | debug_threads = 0; | |
712 | monitor_output ("Debug output disabled.\n"); | |
713 | } | |
714 | else if (strcmp (mon, "set remote-debug 1") == 0) | |
715 | { | |
716 | remote_debug = 1; | |
717 | monitor_output ("Protocol debug output enabled.\n"); | |
718 | } | |
719 | else if (strcmp (mon, "set remote-debug 0") == 0) | |
720 | { | |
721 | remote_debug = 0; | |
722 | monitor_output ("Protocol debug output disabled.\n"); | |
723 | } | |
724 | else if (strcmp (mon, "help") == 0) | |
725 | monitor_show_help (); | |
2d717e4f DJ |
726 | else if (strcmp (mon, "exit") == 0) |
727 | exit_requested = 1; | |
c74d0ad8 DJ |
728 | else |
729 | { | |
730 | monitor_output ("Unknown monitor command.\n\n"); | |
731 | monitor_show_help (); | |
732 | write_enn (own_buf); | |
733 | } | |
734 | ||
735 | free (mon); | |
736 | return; | |
737 | } | |
738 | ||
ce3a066d DJ |
739 | /* Otherwise we didn't know what packet it was. Say we didn't |
740 | understand it. */ | |
741 | own_buf[0] = 0; | |
742 | } | |
743 | ||
64386c31 DJ |
744 | /* Parse vCont packets. */ |
745 | void | |
fc620387 | 746 | handle_v_cont (char *own_buf, char *status, int *signal) |
64386c31 DJ |
747 | { |
748 | char *p, *q; | |
749 | int n = 0, i = 0; | |
750 | struct thread_resume *resume_info, default_action; | |
751 | ||
752 | /* Count the number of semicolons in the packet. There should be one | |
753 | for every action. */ | |
754 | p = &own_buf[5]; | |
755 | while (p) | |
756 | { | |
757 | n++; | |
758 | p++; | |
759 | p = strchr (p, ';'); | |
760 | } | |
761 | /* Allocate room for one extra action, for the default remain-stopped | |
762 | behavior; if no default action is in the list, we'll need the extra | |
763 | slot. */ | |
764 | resume_info = malloc ((n + 1) * sizeof (resume_info[0])); | |
765 | ||
766 | default_action.thread = -1; | |
767 | default_action.leave_stopped = 1; | |
768 | default_action.step = 0; | |
769 | default_action.sig = 0; | |
770 | ||
771 | p = &own_buf[5]; | |
772 | i = 0; | |
773 | while (*p) | |
774 | { | |
775 | p++; | |
776 | ||
777 | resume_info[i].leave_stopped = 0; | |
778 | ||
779 | if (p[0] == 's' || p[0] == 'S') | |
780 | resume_info[i].step = 1; | |
781 | else if (p[0] == 'c' || p[0] == 'C') | |
782 | resume_info[i].step = 0; | |
783 | else | |
784 | goto err; | |
785 | ||
786 | if (p[0] == 'S' || p[0] == 'C') | |
787 | { | |
788 | int sig; | |
789 | sig = strtol (p + 1, &q, 16); | |
790 | if (p == q) | |
791 | goto err; | |
792 | p = q; | |
793 | ||
794 | if (!target_signal_to_host_p (sig)) | |
795 | goto err; | |
796 | resume_info[i].sig = target_signal_to_host (sig); | |
797 | } | |
798 | else | |
799 | { | |
800 | resume_info[i].sig = 0; | |
801 | p = p + 1; | |
802 | } | |
803 | ||
804 | if (p[0] == 0) | |
805 | { | |
806 | resume_info[i].thread = -1; | |
807 | default_action = resume_info[i]; | |
808 | ||
809 | /* Note: we don't increment i here, we'll overwrite this entry | |
810 | the next time through. */ | |
811 | } | |
812 | else if (p[0] == ':') | |
813 | { | |
a06660f7 DJ |
814 | unsigned int gdb_id = strtoul (p + 1, &q, 16); |
815 | unsigned long thread_id; | |
816 | ||
64386c31 DJ |
817 | if (p == q) |
818 | goto err; | |
819 | p = q; | |
820 | if (p[0] != ';' && p[0] != 0) | |
821 | goto err; | |
822 | ||
a06660f7 DJ |
823 | thread_id = gdb_id_to_thread_id (gdb_id); |
824 | if (thread_id) | |
825 | resume_info[i].thread = thread_id; | |
826 | else | |
827 | goto err; | |
828 | ||
64386c31 DJ |
829 | i++; |
830 | } | |
831 | } | |
832 | ||
833 | resume_info[i] = default_action; | |
834 | ||
835 | /* Still used in occasional places in the backend. */ | |
836 | if (n == 1 && resume_info[0].thread != -1) | |
837 | cont_thread = resume_info[0].thread; | |
838 | else | |
839 | cont_thread = -1; | |
dc3f8883 | 840 | set_desired_inferior (0); |
64386c31 | 841 | |
a20d5e98 | 842 | enable_async_io (); |
64386c31 DJ |
843 | (*the_target->resume) (resume_info); |
844 | ||
845 | free (resume_info); | |
846 | ||
847 | *signal = mywait (status, 1); | |
848 | prepare_resume_reply (own_buf, *status, *signal); | |
a20d5e98 | 849 | disable_async_io (); |
64386c31 DJ |
850 | return; |
851 | ||
852 | err: | |
255e7678 | 853 | write_enn (own_buf); |
64386c31 DJ |
854 | free (resume_info); |
855 | return; | |
856 | } | |
857 | ||
2d717e4f DJ |
858 | /* Attach to a new program. Return 1 if successful, 0 if failure. */ |
859 | int | |
860 | handle_v_attach (char *own_buf, char *status, int *signal) | |
861 | { | |
862 | int pid; | |
863 | ||
864 | pid = strtol (own_buf + 8, NULL, 16); | |
865 | if (pid != 0 && attach_inferior (pid, status, signal) == 0) | |
866 | { | |
867 | prepare_resume_reply (own_buf, *status, *signal); | |
868 | return 1; | |
869 | } | |
870 | else | |
871 | { | |
872 | write_enn (own_buf); | |
873 | return 0; | |
874 | } | |
875 | } | |
876 | ||
877 | /* Run a new program. Return 1 if successful, 0 if failure. */ | |
878 | static int | |
879 | handle_v_run (char *own_buf, char *status, int *signal) | |
880 | { | |
881 | char *p, **pp, *next_p, **new_argv; | |
882 | int i, new_argc; | |
883 | ||
884 | new_argc = 0; | |
885 | for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';')) | |
886 | { | |
887 | p++; | |
888 | new_argc++; | |
889 | } | |
890 | ||
891 | new_argv = malloc ((new_argc + 2) * sizeof (char *)); | |
892 | i = 0; | |
893 | for (p = own_buf + strlen ("vRun;"); *p; p = next_p) | |
894 | { | |
895 | next_p = strchr (p, ';'); | |
896 | if (next_p == NULL) | |
897 | next_p = p + strlen (p); | |
898 | ||
899 | if (i == 0 && p == next_p) | |
900 | new_argv[i] = NULL; | |
901 | else | |
902 | { | |
903 | new_argv[i] = malloc (1 + (next_p - p) / 2); | |
904 | unhexify (new_argv[i], p, (next_p - p) / 2); | |
905 | new_argv[i][(next_p - p) / 2] = '\0'; | |
906 | } | |
907 | ||
908 | if (*next_p) | |
909 | next_p++; | |
910 | i++; | |
911 | } | |
912 | new_argv[i] = NULL; | |
913 | ||
914 | if (new_argv[0] == NULL) | |
915 | { | |
916 | if (program_argv == NULL) | |
917 | { | |
918 | write_enn (own_buf); | |
919 | return 0; | |
920 | } | |
921 | ||
922 | new_argv[0] = strdup (program_argv[0]); | |
923 | } | |
924 | ||
925 | /* Free the old argv. */ | |
926 | if (program_argv) | |
927 | { | |
928 | for (pp = program_argv; *pp != NULL; pp++) | |
929 | free (*pp); | |
930 | free (program_argv); | |
931 | } | |
932 | program_argv = new_argv; | |
933 | ||
934 | *signal = start_inferior (program_argv, status); | |
935 | if (*status == 'T') | |
936 | { | |
937 | prepare_resume_reply (own_buf, *status, *signal); | |
938 | return 1; | |
939 | } | |
940 | else | |
941 | { | |
942 | write_enn (own_buf); | |
943 | return 0; | |
944 | } | |
945 | } | |
946 | ||
64386c31 DJ |
947 | /* Handle all of the extended 'v' packets. */ |
948 | void | |
a6b151f1 DJ |
949 | handle_v_requests (char *own_buf, char *status, int *signal, |
950 | int packet_len, int *new_packet_len) | |
64386c31 DJ |
951 | { |
952 | if (strncmp (own_buf, "vCont;", 6) == 0) | |
953 | { | |
2d717e4f | 954 | require_running (own_buf); |
64386c31 DJ |
955 | handle_v_cont (own_buf, status, signal); |
956 | return; | |
957 | } | |
958 | ||
959 | if (strncmp (own_buf, "vCont?", 6) == 0) | |
960 | { | |
961 | strcpy (own_buf, "vCont;c;C;s;S"); | |
962 | return; | |
963 | } | |
964 | ||
a6b151f1 DJ |
965 | if (strncmp (own_buf, "vFile:", 6) == 0 |
966 | && handle_vFile (own_buf, packet_len, new_packet_len)) | |
967 | return; | |
968 | ||
2d717e4f DJ |
969 | if (strncmp (own_buf, "vAttach;", 8) == 0) |
970 | { | |
971 | if (target_running ()) | |
972 | { | |
fd96d250 PA |
973 | fprintf (stderr, "Already debugging a process\n"); |
974 | write_enn (own_buf); | |
975 | return; | |
2d717e4f DJ |
976 | } |
977 | handle_v_attach (own_buf, status, signal); | |
978 | return; | |
979 | } | |
980 | ||
981 | if (strncmp (own_buf, "vRun;", 5) == 0) | |
982 | { | |
983 | if (target_running ()) | |
984 | { | |
fd96d250 PA |
985 | fprintf (stderr, "Already debugging a process\n"); |
986 | write_enn (own_buf); | |
987 | return; | |
2d717e4f DJ |
988 | } |
989 | handle_v_run (own_buf, status, signal); | |
990 | return; | |
991 | } | |
992 | ||
64386c31 DJ |
993 | /* Otherwise we didn't know what packet it was. Say we didn't |
994 | understand it. */ | |
995 | own_buf[0] = 0; | |
996 | return; | |
997 | } | |
998 | ||
999 | void | |
27524b67 | 1000 | myresume (char *own_buf, int step, int *signalp, char *statusp) |
64386c31 DJ |
1001 | { |
1002 | struct thread_resume resume_info[2]; | |
1003 | int n = 0; | |
a20d5e98 DJ |
1004 | int sig = *signalp; |
1005 | ||
1006 | set_desired_inferior (0); | |
64386c31 | 1007 | |
d592fa2f | 1008 | if (step || sig || (cont_thread != 0 && cont_thread != -1)) |
64386c31 DJ |
1009 | { |
1010 | resume_info[0].thread | |
1011 | = ((struct inferior_list_entry *) current_inferior)->id; | |
1012 | resume_info[0].step = step; | |
1013 | resume_info[0].sig = sig; | |
1014 | resume_info[0].leave_stopped = 0; | |
1015 | n++; | |
1016 | } | |
1017 | resume_info[n].thread = -1; | |
1018 | resume_info[n].step = 0; | |
1019 | resume_info[n].sig = 0; | |
d592fa2f | 1020 | resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1); |
64386c31 | 1021 | |
a20d5e98 | 1022 | enable_async_io (); |
64386c31 | 1023 | (*the_target->resume) (resume_info); |
a20d5e98 DJ |
1024 | *signalp = mywait (statusp, 1); |
1025 | prepare_resume_reply (own_buf, *statusp, *signalp); | |
1026 | disable_async_io (); | |
64386c31 DJ |
1027 | } |
1028 | ||
dd24457d DJ |
1029 | static void |
1030 | gdbserver_version (void) | |
1031 | { | |
1032 | printf ("GNU gdbserver %s\n" | |
255e7678 | 1033 | "Copyright (C) 2007 Free Software Foundation, Inc.\n" |
dd24457d DJ |
1034 | "gdbserver is free software, covered by the GNU General Public License.\n" |
1035 | "This gdbserver was configured as \"%s\"\n", | |
1036 | version, host_name); | |
1037 | } | |
1038 | ||
0bc68c49 DJ |
1039 | static void |
1040 | gdbserver_usage (void) | |
1041 | { | |
2d717e4f DJ |
1042 | printf ("Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n" |
1043 | "\tgdbserver [OPTIONS] --attach COMM PID\n" | |
1044 | "\tgdbserver [OPTIONS] --multi COMM\n" | |
dd24457d DJ |
1045 | "\n" |
1046 | "COMM may either be a tty device (for serial debugging), or \n" | |
2d717e4f DJ |
1047 | "HOST:PORT to listen for a TCP connection.\n" |
1048 | "\n" | |
1049 | "Options:\n" | |
ccd213ac DJ |
1050 | " --debug\t\tEnable debugging output.\n" |
1051 | " --wrapper WRAPPER --\tRun WRAPPER to start new programs.\n"); | |
0bc68c49 DJ |
1052 | } |
1053 | ||
2d717e4f DJ |
1054 | #undef require_running |
1055 | #define require_running(BUF) \ | |
1056 | if (!target_running ()) \ | |
1057 | { \ | |
1058 | write_enn (BUF); \ | |
1059 | break; \ | |
1060 | } | |
1061 | ||
c906108c | 1062 | int |
da85418c | 1063 | main (int argc, char *argv[]) |
c906108c | 1064 | { |
f450004a | 1065 | char ch, status, *own_buf; |
7fb85e41 | 1066 | unsigned char *mem_buf; |
c906108c | 1067 | int i = 0; |
fc620387 | 1068 | int signal; |
c906108c SS |
1069 | unsigned int len; |
1070 | CORE_ADDR mem_addr; | |
0729219d DJ |
1071 | int bad_attach; |
1072 | int pid; | |
2d717e4f DJ |
1073 | char *arg_end, *port; |
1074 | char **next_arg = &argv[1]; | |
1075 | int multi_mode = 0; | |
1076 | int attach = 0; | |
1077 | int was_running; | |
c906108c | 1078 | |
2d717e4f | 1079 | while (*next_arg != NULL && **next_arg == '-') |
dd24457d | 1080 | { |
2d717e4f DJ |
1081 | if (strcmp (*next_arg, "--version") == 0) |
1082 | { | |
1083 | gdbserver_version (); | |
1084 | exit (0); | |
1085 | } | |
1086 | else if (strcmp (*next_arg, "--help") == 0) | |
1087 | { | |
1088 | gdbserver_usage (); | |
1089 | exit (0); | |
1090 | } | |
1091 | else if (strcmp (*next_arg, "--attach") == 0) | |
1092 | attach = 1; | |
1093 | else if (strcmp (*next_arg, "--multi") == 0) | |
1094 | multi_mode = 1; | |
ccd213ac DJ |
1095 | else if (strcmp (*next_arg, "--wrapper") == 0) |
1096 | { | |
1097 | next_arg++; | |
1098 | ||
1099 | wrapper_argv = next_arg; | |
1100 | while (*next_arg != NULL && strcmp (*next_arg, "--") != 0) | |
1101 | next_arg++; | |
1102 | ||
1103 | if (next_arg == wrapper_argv || *next_arg == NULL) | |
1104 | { | |
1105 | gdbserver_usage (); | |
1106 | exit (1); | |
1107 | } | |
1108 | ||
1109 | /* Consume the "--". */ | |
1110 | *next_arg = NULL; | |
1111 | } | |
2d717e4f DJ |
1112 | else if (strcmp (*next_arg, "--debug") == 0) |
1113 | debug_threads = 1; | |
1114 | else | |
1115 | { | |
1116 | fprintf (stderr, "Unknown argument: %s\n", *next_arg); | |
1117 | exit (1); | |
1118 | } | |
dd24457d | 1119 | |
2d717e4f DJ |
1120 | next_arg++; |
1121 | continue; | |
dd24457d DJ |
1122 | } |
1123 | ||
c5aa993b | 1124 | if (setjmp (toplevel)) |
c906108c | 1125 | { |
c5aa993b JM |
1126 | fprintf (stderr, "Exiting\n"); |
1127 | exit (1); | |
c906108c SS |
1128 | } |
1129 | ||
2d717e4f DJ |
1130 | port = *next_arg; |
1131 | next_arg++; | |
1132 | if (port == NULL || (!attach && !multi_mode && *next_arg == NULL)) | |
1133 | { | |
1134 | gdbserver_usage (); | |
1135 | exit (1); | |
1136 | } | |
1137 | ||
0729219d DJ |
1138 | bad_attach = 0; |
1139 | pid = 0; | |
2d717e4f DJ |
1140 | |
1141 | /* --attach used to come after PORT, so allow it there for | |
1142 | compatibility. */ | |
1143 | if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0) | |
45b7b345 | 1144 | { |
2d717e4f DJ |
1145 | attach = 1; |
1146 | next_arg++; | |
45b7b345 DJ |
1147 | } |
1148 | ||
2d717e4f DJ |
1149 | if (attach |
1150 | && (*next_arg == NULL | |
1151 | || (*next_arg)[0] == '\0' | |
1152 | || (pid = strtoul (*next_arg, &arg_end, 0)) == 0 | |
1153 | || *arg_end != '\0' | |
1154 | || next_arg[1] != NULL)) | |
1155 | bad_attach = 1; | |
1156 | ||
1157 | if (bad_attach) | |
dd24457d DJ |
1158 | { |
1159 | gdbserver_usage (); | |
1160 | exit (1); | |
1161 | } | |
c906108c | 1162 | |
a20d5e98 | 1163 | initialize_async_io (); |
4ce44c66 JM |
1164 | initialize_low (); |
1165 | ||
255e7678 | 1166 | own_buf = malloc (PBUFSIZ + 1); |
7fb85e41 | 1167 | mem_buf = malloc (PBUFSIZ); |
0a30fbc4 | 1168 | |
2d717e4f | 1169 | if (pid == 0 && *next_arg != NULL) |
45b7b345 | 1170 | { |
2d717e4f DJ |
1171 | int i, n; |
1172 | ||
1173 | n = argc - (next_arg - argv); | |
1174 | program_argv = malloc (sizeof (char *) * (n + 1)); | |
1175 | for (i = 0; i < n; i++) | |
1176 | program_argv[i] = strdup (next_arg[i]); | |
1177 | program_argv[i] = NULL; | |
1178 | ||
45b7b345 | 1179 | /* Wait till we are at first instruction in program. */ |
2d717e4f | 1180 | signal = start_inferior (program_argv, &status); |
c906108c | 1181 | |
c588c53c MS |
1182 | /* We are now (hopefully) stopped at the first instruction of |
1183 | the target process. This assumes that the target process was | |
1184 | successfully created. */ | |
45b7b345 | 1185 | } |
2d717e4f DJ |
1186 | else if (pid != 0) |
1187 | { | |
1188 | if (attach_inferior (pid, &status, &signal) == -1) | |
1189 | error ("Attaching not supported on this target"); | |
1190 | ||
1191 | /* Otherwise succeeded. */ | |
1192 | } | |
45b7b345 DJ |
1193 | else |
1194 | { | |
2d717e4f DJ |
1195 | status = 'W'; |
1196 | signal = 0; | |
45b7b345 | 1197 | } |
c906108c | 1198 | |
311de423 PA |
1199 | /* Don't report shared library events on the initial connection, |
1200 | even if some libraries are preloaded. Avoids the "stopped by | |
1201 | shared library event" notice on gdb side. */ | |
1202 | dlls_changed = 0; | |
1203 | ||
8264bb58 DJ |
1204 | if (setjmp (toplevel)) |
1205 | { | |
1206 | fprintf (stderr, "Killing inferior\n"); | |
1207 | kill_inferior (); | |
1208 | exit (1); | |
1209 | } | |
1210 | ||
c588c53c | 1211 | if (status == 'W' || status == 'X') |
2d717e4f DJ |
1212 | was_running = 0; |
1213 | else | |
1214 | was_running = 1; | |
1215 | ||
1216 | if (!was_running && !multi_mode) | |
c588c53c | 1217 | { |
2d717e4f | 1218 | fprintf (stderr, "No program to debug. GDBserver exiting.\n"); |
c588c53c MS |
1219 | exit (1); |
1220 | } | |
1221 | ||
c906108c SS |
1222 | while (1) |
1223 | { | |
2d717e4f | 1224 | remote_open (port); |
c906108c | 1225 | |
c5aa993b | 1226 | restart: |
2d717e4f DJ |
1227 | if (setjmp (toplevel) != 0) |
1228 | { | |
1229 | /* An error occurred. */ | |
1230 | if (response_needed) | |
1231 | { | |
1232 | write_enn (own_buf); | |
1233 | putpkt (own_buf); | |
1234 | } | |
1235 | } | |
1236 | ||
a20d5e98 | 1237 | disable_async_io (); |
2d717e4f | 1238 | while (!exit_requested) |
c906108c SS |
1239 | { |
1240 | unsigned char sig; | |
01f9e8fa DJ |
1241 | int packet_len; |
1242 | int new_packet_len = -1; | |
1243 | ||
2d717e4f | 1244 | response_needed = 0; |
01f9e8fa DJ |
1245 | packet_len = getpkt (own_buf); |
1246 | if (packet_len <= 0) | |
1247 | break; | |
2d717e4f | 1248 | response_needed = 1; |
01f9e8fa | 1249 | |
c906108c SS |
1250 | i = 0; |
1251 | ch = own_buf[i++]; | |
1252 | switch (ch) | |
1253 | { | |
ce3a066d | 1254 | case 'q': |
0e7f50da | 1255 | handle_query (own_buf, packet_len, &new_packet_len); |
ce3a066d | 1256 | break; |
89be2091 DJ |
1257 | case 'Q': |
1258 | handle_general_set (own_buf); | |
1259 | break; | |
6ad8ae5c | 1260 | case 'D': |
2d717e4f | 1261 | require_running (own_buf); |
6ad8ae5c | 1262 | fprintf (stderr, "Detaching from inferior\n"); |
444d6139 | 1263 | if (detach_inferior () != 0) |
2d717e4f | 1264 | write_enn (own_buf); |
444d6139 PA |
1265 | else |
1266 | { | |
1267 | write_ok (own_buf); | |
6ad8ae5c | 1268 | |
2d717e4f DJ |
1269 | if (extended_protocol) |
1270 | { | |
1271 | /* Treat this like a normal program exit. */ | |
1272 | signal = 0; | |
1273 | status = 'W'; | |
1274 | } | |
1275 | else | |
1276 | { | |
1277 | putpkt (own_buf); | |
1278 | remote_close (); | |
6ad8ae5c | 1279 | |
2d717e4f DJ |
1280 | /* If we are attached, then we can exit. Otherwise, we |
1281 | need to hang around doing nothing, until the child | |
1282 | is gone. */ | |
1283 | if (!attached) | |
1284 | join_inferior (); | |
1285 | ||
1286 | exit (0); | |
1287 | } | |
444d6139 | 1288 | } |
2d717e4f | 1289 | break; |
c906108c | 1290 | case '!': |
2d717e4f DJ |
1291 | extended_protocol = 1; |
1292 | write_ok (own_buf); | |
c906108c SS |
1293 | break; |
1294 | case '?': | |
1295 | prepare_resume_reply (own_buf, status, signal); | |
1296 | break; | |
1297 | case 'H': | |
a06660f7 | 1298 | if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') |
c906108c | 1299 | { |
a06660f7 DJ |
1300 | unsigned long gdb_id, thread_id; |
1301 | ||
2d717e4f | 1302 | require_running (own_buf); |
a06660f7 | 1303 | gdb_id = strtoul (&own_buf[2], NULL, 16); |
2d717e4f DJ |
1304 | if (gdb_id == 0 || gdb_id == -1) |
1305 | thread_id = gdb_id; | |
1306 | else | |
a06660f7 | 1307 | { |
2d717e4f DJ |
1308 | thread_id = gdb_id_to_thread_id (gdb_id); |
1309 | if (thread_id == 0) | |
1310 | { | |
1311 | write_enn (own_buf); | |
1312 | break; | |
1313 | } | |
a06660f7 DJ |
1314 | } |
1315 | ||
1316 | if (own_buf[1] == 'g') | |
1317 | { | |
1318 | general_thread = thread_id; | |
1319 | set_desired_inferior (1); | |
1320 | } | |
1321 | else if (own_buf[1] == 'c') | |
1322 | cont_thread = thread_id; | |
1323 | else if (own_buf[1] == 's') | |
1324 | step_thread = thread_id; | |
1325 | ||
0d62e5e8 | 1326 | write_ok (own_buf); |
a06660f7 DJ |
1327 | } |
1328 | else | |
1329 | { | |
c906108c SS |
1330 | /* Silently ignore it so that gdb can extend the protocol |
1331 | without compatibility headaches. */ | |
1332 | own_buf[0] = '\0'; | |
c906108c SS |
1333 | } |
1334 | break; | |
1335 | case 'g': | |
2d717e4f | 1336 | require_running (own_buf); |
0d62e5e8 | 1337 | set_desired_inferior (1); |
0a30fbc4 | 1338 | registers_to_string (own_buf); |
c906108c SS |
1339 | break; |
1340 | case 'G': | |
2d717e4f | 1341 | require_running (own_buf); |
0d62e5e8 | 1342 | set_desired_inferior (1); |
0a30fbc4 | 1343 | registers_from_string (&own_buf[1]); |
c906108c SS |
1344 | write_ok (own_buf); |
1345 | break; | |
1346 | case 'm': | |
2d717e4f | 1347 | require_running (own_buf); |
c906108c | 1348 | decode_m_packet (&own_buf[1], &mem_addr, &len); |
c3e735a6 DJ |
1349 | if (read_inferior_memory (mem_addr, mem_buf, len) == 0) |
1350 | convert_int_to_ascii (mem_buf, own_buf, len); | |
1351 | else | |
1352 | write_enn (own_buf); | |
c906108c SS |
1353 | break; |
1354 | case 'M': | |
2d717e4f | 1355 | require_running (own_buf); |
c906108c SS |
1356 | decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf); |
1357 | if (write_inferior_memory (mem_addr, mem_buf, len) == 0) | |
1358 | write_ok (own_buf); | |
1359 | else | |
1360 | write_enn (own_buf); | |
1361 | break; | |
01f9e8fa | 1362 | case 'X': |
2d717e4f | 1363 | require_running (own_buf); |
01f9e8fa DJ |
1364 | if (decode_X_packet (&own_buf[1], packet_len - 1, |
1365 | &mem_addr, &len, mem_buf) < 0 | |
1366 | || write_inferior_memory (mem_addr, mem_buf, len) != 0) | |
1367 | write_enn (own_buf); | |
1368 | else | |
1369 | write_ok (own_buf); | |
1370 | break; | |
c906108c | 1371 | case 'C': |
2d717e4f | 1372 | require_running (own_buf); |
c906108c | 1373 | convert_ascii_to_int (own_buf + 1, &sig, 1); |
0e98d0a7 DJ |
1374 | if (target_signal_to_host_p (sig)) |
1375 | signal = target_signal_to_host (sig); | |
1376 | else | |
1377 | signal = 0; | |
27524b67 | 1378 | myresume (own_buf, 0, &signal, &status); |
c906108c SS |
1379 | break; |
1380 | case 'S': | |
2d717e4f | 1381 | require_running (own_buf); |
c906108c | 1382 | convert_ascii_to_int (own_buf + 1, &sig, 1); |
0e98d0a7 DJ |
1383 | if (target_signal_to_host_p (sig)) |
1384 | signal = target_signal_to_host (sig); | |
1385 | else | |
1386 | signal = 0; | |
27524b67 | 1387 | myresume (own_buf, 1, &signal, &status); |
c906108c SS |
1388 | break; |
1389 | case 'c': | |
2d717e4f | 1390 | require_running (own_buf); |
a20d5e98 | 1391 | signal = 0; |
27524b67 | 1392 | myresume (own_buf, 0, &signal, &status); |
c906108c SS |
1393 | break; |
1394 | case 's': | |
2d717e4f | 1395 | require_running (own_buf); |
a20d5e98 | 1396 | signal = 0; |
27524b67 | 1397 | myresume (own_buf, 1, &signal, &status); |
c906108c | 1398 | break; |
e013ee27 OF |
1399 | case 'Z': |
1400 | { | |
1401 | char *lenptr; | |
1402 | char *dataptr; | |
1403 | CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); | |
1404 | int len = strtol (lenptr + 1, &dataptr, 16); | |
1405 | char type = own_buf[1]; | |
1406 | ||
1407 | if (the_target->insert_watchpoint == NULL | |
1408 | || (type < '2' || type > '4')) | |
1409 | { | |
1410 | /* No watchpoint support or not a watchpoint command; | |
1411 | unrecognized either way. */ | |
1412 | own_buf[0] = '\0'; | |
1413 | } | |
1414 | else | |
1415 | { | |
1416 | int res; | |
1417 | ||
2d717e4f | 1418 | require_running (own_buf); |
e013ee27 OF |
1419 | res = (*the_target->insert_watchpoint) (type, addr, len); |
1420 | if (res == 0) | |
1421 | write_ok (own_buf); | |
1422 | else if (res == 1) | |
1423 | /* Unsupported. */ | |
1424 | own_buf[0] = '\0'; | |
1425 | else | |
1426 | write_enn (own_buf); | |
1427 | } | |
1428 | break; | |
1429 | } | |
1430 | case 'z': | |
1431 | { | |
1432 | char *lenptr; | |
1433 | char *dataptr; | |
1434 | CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); | |
1435 | int len = strtol (lenptr + 1, &dataptr, 16); | |
1436 | char type = own_buf[1]; | |
1437 | ||
1438 | if (the_target->remove_watchpoint == NULL | |
1439 | || (type < '2' || type > '4')) | |
1440 | { | |
1441 | /* No watchpoint support or not a watchpoint command; | |
1442 | unrecognized either way. */ | |
1443 | own_buf[0] = '\0'; | |
1444 | } | |
1445 | else | |
1446 | { | |
1447 | int res; | |
1448 | ||
2d717e4f | 1449 | require_running (own_buf); |
e013ee27 OF |
1450 | res = (*the_target->remove_watchpoint) (type, addr, len); |
1451 | if (res == 0) | |
1452 | write_ok (own_buf); | |
1453 | else if (res == 1) | |
1454 | /* Unsupported. */ | |
1455 | own_buf[0] = '\0'; | |
1456 | else | |
1457 | write_enn (own_buf); | |
1458 | } | |
1459 | break; | |
1460 | } | |
c906108c | 1461 | case 'k': |
2d717e4f DJ |
1462 | response_needed = 0; |
1463 | if (!target_running ()) | |
1464 | /* The packet we received doesn't make sense - but we | |
1465 | can't reply to it, either. */ | |
1466 | goto restart; | |
1467 | ||
c906108c SS |
1468 | fprintf (stderr, "Killing inferior\n"); |
1469 | kill_inferior (); | |
2d717e4f DJ |
1470 | |
1471 | /* When using the extended protocol, we wait with no | |
1472 | program running. The traditional protocol will exit | |
1473 | instead. */ | |
c906108c SS |
1474 | if (extended_protocol) |
1475 | { | |
2d717e4f DJ |
1476 | status = 'X'; |
1477 | signal = TARGET_SIGNAL_KILL; | |
1478 | was_running = 0; | |
c906108c | 1479 | goto restart; |
c906108c SS |
1480 | } |
1481 | else | |
1482 | { | |
1483 | exit (0); | |
1484 | break; | |
1485 | } | |
1486 | case 'T': | |
a06660f7 DJ |
1487 | { |
1488 | unsigned long gdb_id, thread_id; | |
1489 | ||
2d717e4f | 1490 | require_running (own_buf); |
a06660f7 DJ |
1491 | gdb_id = strtoul (&own_buf[1], NULL, 16); |
1492 | thread_id = gdb_id_to_thread_id (gdb_id); | |
1493 | if (thread_id == 0) | |
1494 | { | |
1495 | write_enn (own_buf); | |
1496 | break; | |
1497 | } | |
1498 | ||
1499 | if (mythread_alive (thread_id)) | |
1500 | write_ok (own_buf); | |
1501 | else | |
1502 | write_enn (own_buf); | |
1503 | } | |
c906108c SS |
1504 | break; |
1505 | case 'R': | |
2d717e4f DJ |
1506 | response_needed = 0; |
1507 | ||
c906108c | 1508 | /* Restarting the inferior is only supported in the |
c5aa993b | 1509 | extended protocol. */ |
c906108c SS |
1510 | if (extended_protocol) |
1511 | { | |
2d717e4f DJ |
1512 | if (target_running ()) |
1513 | kill_inferior (); | |
c906108c SS |
1514 | fprintf (stderr, "GDBserver restarting\n"); |
1515 | ||
1516 | /* Wait till we are at 1st instruction in prog. */ | |
2d717e4f DJ |
1517 | if (program_argv != NULL) |
1518 | signal = start_inferior (program_argv, &status); | |
1519 | else | |
1520 | { | |
1521 | status = 'X'; | |
1522 | signal = TARGET_SIGNAL_KILL; | |
1523 | } | |
c906108c | 1524 | goto restart; |
c906108c SS |
1525 | } |
1526 | else | |
1527 | { | |
1528 | /* It is a request we don't understand. Respond with an | |
1529 | empty packet so that gdb knows that we don't support this | |
1530 | request. */ | |
1531 | own_buf[0] = '\0'; | |
1532 | break; | |
1533 | } | |
64386c31 DJ |
1534 | case 'v': |
1535 | /* Extended (long) request. */ | |
a6b151f1 DJ |
1536 | handle_v_requests (own_buf, &status, &signal, |
1537 | packet_len, &new_packet_len); | |
64386c31 | 1538 | break; |
a6b151f1 | 1539 | |
c906108c SS |
1540 | default: |
1541 | /* It is a request we don't understand. Respond with an | |
c5aa993b JM |
1542 | empty packet so that gdb knows that we don't support this |
1543 | request. */ | |
c906108c SS |
1544 | own_buf[0] = '\0'; |
1545 | break; | |
1546 | } | |
1547 | ||
01f9e8fa DJ |
1548 | if (new_packet_len != -1) |
1549 | putpkt_binary (own_buf, new_packet_len); | |
1550 | else | |
1551 | putpkt (own_buf); | |
c906108c | 1552 | |
2d717e4f DJ |
1553 | response_needed = 0; |
1554 | ||
1555 | if (was_running && (status == 'W' || status == 'X')) | |
c906108c | 1556 | { |
2d717e4f | 1557 | was_running = 0; |
c906108c | 1558 | |
2d717e4f DJ |
1559 | if (status == 'W') |
1560 | fprintf (stderr, | |
1561 | "\nChild exited with status %d\n", signal); | |
1562 | if (status == 'X') | |
1563 | fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n", | |
1564 | target_signal_to_host (signal), | |
1565 | target_signal_to_name (signal)); | |
1566 | ||
1567 | if (extended_protocol) | |
1568 | goto restart; | |
c906108c SS |
1569 | else |
1570 | { | |
1571 | fprintf (stderr, "GDBserver exiting\n"); | |
1572 | exit (0); | |
1573 | } | |
1574 | } | |
c906108c | 1575 | |
2d717e4f DJ |
1576 | if (status != 'W' && status != 'X') |
1577 | was_running = 1; | |
1578 | } | |
c906108c | 1579 | |
2d717e4f DJ |
1580 | /* If an exit was requested (using the "monitor exit" command), |
1581 | terminate now. The only other way to get here is for | |
1582 | getpkt to fail; close the connection and reopen it at the | |
1583 | top of the loop. */ | |
c906108c | 1584 | |
2d717e4f | 1585 | if (exit_requested) |
c906108c SS |
1586 | { |
1587 | remote_close (); | |
2d717e4f DJ |
1588 | if (attached && target_running ()) |
1589 | detach_inferior (); | |
1590 | else if (target_running ()) | |
1591 | kill_inferior (); | |
c906108c SS |
1592 | exit (0); |
1593 | } | |
1594 | else | |
1595 | { | |
45b7b345 DJ |
1596 | fprintf (stderr, "Remote side has terminated connection. " |
1597 | "GDBserver will reopen the connection.\n"); | |
c906108c SS |
1598 | remote_close (); |
1599 | } | |
1600 | } | |
1601 | } |