* gnu-nat.c (proc_string): Use capital T for "Thread".
[deliverable/binutils-gdb.git] / gdb / gdbserver / server.c
CommitLineData
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
32unsigned long cont_thread;
33unsigned long general_thread;
34unsigned long step_thread;
35unsigned long thread_from_wait;
36unsigned long old_thread_from_wait;
0d62e5e8
DJ
37int server_waiting;
38
2d717e4f
DJ
39static int extended_protocol;
40static int attached;
41static int response_needed;
42static int exit_requested;
43
ccd213ac 44static 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. */
48int debug_threads;
49
89be2091
DJ
50int pass_signals[TARGET_SIGNAL_LAST];
51
c906108c 52jmp_buf toplevel;
c906108c 53
9b4b61c8
UW
54const 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 61unsigned long signal_pid;
a9fa9f7d 62
290fadea
RS
63#ifdef SIGTTOU
64/* A file descriptor for the controlling terminal. */
65int terminal_fd;
66
67/* TERMINAL_FD's original foreground group. */
68pid_t old_foreground_pgrp;
69
70/* Hand back terminal ownership to the original foreground group. */
71
72static void
73restore_old_foreground_pgrp (void)
74{
75 tcsetpgrp (terminal_fd, old_foreground_pgrp);
76}
77#endif
78
2d717e4f
DJ
79static int
80target_running (void)
81{
82 return all_threads.head != NULL;
83}
84
fc620387 85static int
ccd213ac 86start_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 162static int
fc620387 163attach_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 192extern int remote_debug;
ce3a066d 193
0876f84a
DJ
194/* Decode a qXfer read request. Return 0 if everything looks OK,
195 or -1 otherwise. */
196
197static int
198decode_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. */
219static int
23181151 220write_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. */
234void
235handle_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 267static const char *
fb1e4ffc 268get_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 278 if (gdbserver_xmltarget
221c031f 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
305void
306monitor_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
08388c79
DE
317/* Subroutine of handle_search_memory to simplify it. */
318
319static int
320handle_search_memory_1 (CORE_ADDR start_addr, CORE_ADDR search_space_len,
321 gdb_byte *pattern, unsigned pattern_len,
322 gdb_byte *search_buf,
323 unsigned chunk_size, unsigned search_buf_size,
324 CORE_ADDR *found_addrp)
325{
326 /* Prime the search buffer. */
327
328 if (read_inferior_memory (start_addr, search_buf, search_buf_size) != 0)
329 {
5e1471f5 330 warning ("Unable to access target memory at 0x%lx, halting search.",
08388c79
DE
331 (long) start_addr);
332 return -1;
333 }
334
335 /* Perform the search.
336
337 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
338 When we've scanned N bytes we copy the trailing bytes to the start and
339 read in another N bytes. */
340
341 while (search_space_len >= pattern_len)
342 {
343 gdb_byte *found_ptr;
344 unsigned nr_search_bytes = (search_space_len < search_buf_size
345 ? search_space_len
346 : search_buf_size);
347
348 found_ptr = memmem (search_buf, nr_search_bytes, pattern, pattern_len);
349
350 if (found_ptr != NULL)
351 {
352 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
353 *found_addrp = found_addr;
354 return 1;
355 }
356
357 /* Not found in this chunk, skip to next chunk. */
358
359 /* Don't let search_space_len wrap here, it's unsigned. */
360 if (search_space_len >= chunk_size)
361 search_space_len -= chunk_size;
362 else
363 search_space_len = 0;
364
365 if (search_space_len >= pattern_len)
366 {
367 unsigned keep_len = search_buf_size - chunk_size;
368 CORE_ADDR read_addr = start_addr + keep_len;
369 int nr_to_read;
370
371 /* Copy the trailing part of the previous iteration to the front
372 of the buffer for the next iteration. */
373 memcpy (search_buf, search_buf + chunk_size, keep_len);
374
375 nr_to_read = (search_space_len - keep_len < chunk_size
376 ? search_space_len - keep_len
377 : chunk_size);
378
379 if (read_inferior_memory (read_addr, search_buf + keep_len,
380 nr_to_read) != 0)
381 {
5e1471f5 382 warning ("Unable to access target memory at 0x%lx, halting search.",
08388c79
DE
383 (long) read_addr);
384 return -1;
385 }
386
387 start_addr += chunk_size;
388 }
389 }
390
391 /* Not found. */
392
393 return 0;
394}
395
396/* Handle qSearch:memory packets. */
397
398static void
399handle_search_memory (char *own_buf, int packet_len)
400{
401 CORE_ADDR start_addr;
402 CORE_ADDR search_space_len;
403 gdb_byte *pattern;
404 unsigned int pattern_len;
405 /* NOTE: also defined in find.c testcase. */
406#define SEARCH_CHUNK_SIZE 16000
407 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
408 /* Buffer to hold memory contents for searching. */
409 gdb_byte *search_buf;
410 unsigned search_buf_size;
411 int found;
412 CORE_ADDR found_addr;
413 int cmd_name_len = sizeof ("qSearch:memory:") - 1;
414
415 pattern = malloc (packet_len);
416 if (pattern == NULL)
417 {
5e1471f5 418 error ("Unable to allocate memory to perform the search");
08388c79
DE
419 strcpy (own_buf, "E00");
420 return;
421 }
422 if (decode_search_memory_packet (own_buf + cmd_name_len,
423 packet_len - cmd_name_len,
424 &start_addr, &search_space_len,
425 pattern, &pattern_len) < 0)
426 {
427 free (pattern);
5e1471f5 428 error ("Error in parsing qSearch:memory packet");
08388c79
DE
429 strcpy (own_buf, "E00");
430 return;
431 }
432
433 search_buf_size = chunk_size + pattern_len - 1;
434
435 /* No point in trying to allocate a buffer larger than the search space. */
436 if (search_space_len < search_buf_size)
437 search_buf_size = search_space_len;
438
439 search_buf = malloc (search_buf_size);
440 if (search_buf == NULL)
441 {
442 free (pattern);
5e1471f5 443 error ("Unable to allocate memory to perform the search");
08388c79
DE
444 strcpy (own_buf, "E00");
445 return;
446 }
447
448 found = handle_search_memory_1 (start_addr, search_space_len,
449 pattern, pattern_len,
450 search_buf, chunk_size, search_buf_size,
451 &found_addr);
452
453 if (found > 0)
454 sprintf (own_buf, "1,%lx", (long) found_addr);
455 else if (found == 0)
456 strcpy (own_buf, "0");
457 else
458 strcpy (own_buf, "E00");
459
460 free (search_buf);
461 free (pattern);
462}
463
2d717e4f
DJ
464#define require_running(BUF) \
465 if (!target_running ()) \
466 { \
467 write_enn (BUF); \
468 return; \
469 }
470
ce3a066d
DJ
471/* Handle all of the extended 'q' packets. */
472void
0e7f50da 473handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
ce3a066d 474{
0d62e5e8
DJ
475 static struct inferior_list_entry *thread_ptr;
476
bb63802a
UW
477 /* Reply the current thread id. */
478 if (strcmp ("qC", own_buf) == 0)
479 {
2d717e4f 480 require_running (own_buf);
bb63802a
UW
481 thread_ptr = all_threads.head;
482 sprintf (own_buf, "QC%x",
483 thread_to_gdb_id ((struct thread_info *)thread_ptr));
484 return;
485 }
486
ce3a066d
DJ
487 if (strcmp ("qSymbol::", own_buf) == 0)
488 {
2d717e4f 489 if (target_running () && the_target->look_up_symbols != NULL)
2f2893d9
DJ
490 (*the_target->look_up_symbols) ();
491
ce3a066d
DJ
492 strcpy (own_buf, "OK");
493 return;
494 }
495
0d62e5e8
DJ
496 if (strcmp ("qfThreadInfo", own_buf) == 0)
497 {
2d717e4f 498 require_running (own_buf);
0d62e5e8 499 thread_ptr = all_threads.head;
a06660f7 500 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
0d62e5e8
DJ
501 thread_ptr = thread_ptr->next;
502 return;
503 }
aa691b87 504
0d62e5e8
DJ
505 if (strcmp ("qsThreadInfo", own_buf) == 0)
506 {
2d717e4f 507 require_running (own_buf);
0d62e5e8
DJ
508 if (thread_ptr != NULL)
509 {
a06660f7 510 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
0d62e5e8
DJ
511 thread_ptr = thread_ptr->next;
512 return;
513 }
514 else
515 {
516 sprintf (own_buf, "l");
517 return;
518 }
519 }
aa691b87 520
52fb6437
NS
521 if (the_target->read_offsets != NULL
522 && strcmp ("qOffsets", own_buf) == 0)
523 {
524 CORE_ADDR text, data;
2d717e4f
DJ
525
526 require_running (own_buf);
52fb6437
NS
527 if (the_target->read_offsets (&text, &data))
528 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
529 (long)text, (long)data, (long)data);
530 else
531 write_enn (own_buf);
532
533 return;
534 }
535
0e7f50da
UW
536 if (the_target->qxfer_spu != NULL
537 && strncmp ("qXfer:spu:read:", own_buf, 15) == 0)
538 {
539 char *annex;
540 int n;
541 unsigned int len;
542 CORE_ADDR ofs;
543 unsigned char *spu_buf;
544
2d717e4f 545 require_running (own_buf);
0e7f50da
UW
546 strcpy (own_buf, "E00");
547 if (decode_xfer_read (own_buf + 15, &annex, &ofs, &len) < 0)
548 return;
549 if (len > PBUFSIZ - 2)
550 len = PBUFSIZ - 2;
551 spu_buf = malloc (len + 1);
552 if (!spu_buf)
553 return;
554
555 n = (*the_target->qxfer_spu) (annex, spu_buf, NULL, ofs, len + 1);
556 if (n < 0)
557 write_enn (own_buf);
558 else if (n > len)
559 *new_packet_len_p = write_qxfer_response
560 (own_buf, spu_buf, len, 1);
561 else
562 *new_packet_len_p = write_qxfer_response
563 (own_buf, spu_buf, n, 0);
564
565 free (spu_buf);
566 return;
567 }
568
569 if (the_target->qxfer_spu != NULL
570 && strncmp ("qXfer:spu:write:", own_buf, 16) == 0)
571 {
572 char *annex;
573 int n;
574 unsigned int len;
575 CORE_ADDR ofs;
576 unsigned char *spu_buf;
577
2d717e4f 578 require_running (own_buf);
0e7f50da
UW
579 strcpy (own_buf, "E00");
580 spu_buf = malloc (packet_len - 15);
581 if (!spu_buf)
582 return;
583 if (decode_xfer_write (own_buf + 16, packet_len - 16, &annex,
584 &ofs, &len, spu_buf) < 0)
585 {
586 free (spu_buf);
587 return;
588 }
589
590 n = (*the_target->qxfer_spu)
591 (annex, NULL, (unsigned const char *)spu_buf, ofs, len);
592 if (n < 0)
593 write_enn (own_buf);
594 else
595 sprintf (own_buf, "%x", n);
596
597 free (spu_buf);
598 return;
599 }
600
aa691b87 601 if (the_target->read_auxv != NULL
0876f84a 602 && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0)
aa691b87 603 {
0876f84a
DJ
604 unsigned char *data;
605 int n;
aa691b87
RM
606 CORE_ADDR ofs;
607 unsigned int len;
0876f84a
DJ
608 char *annex;
609
2d717e4f
DJ
610 require_running (own_buf);
611
0876f84a
DJ
612 /* Reject any annex; grab the offset and length. */
613 if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0
614 || annex[0] != '\0')
615 {
616 strcpy (own_buf, "E00");
617 return;
618 }
619
620 /* Read one extra byte, as an indicator of whether there is
621 more. */
622 if (len > PBUFSIZ - 2)
623 len = PBUFSIZ - 2;
624 data = malloc (len + 1);
625 n = (*the_target->read_auxv) (ofs, data, len + 1);
000ef4f0
DJ
626 if (n < 0)
627 write_enn (own_buf);
628 else if (n > len)
0876f84a 629 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
aa691b87 630 else
0876f84a
DJ
631 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
632
633 free (data);
634
aa691b87
RM
635 return;
636 }
637
23181151
DJ
638 if (strncmp ("qXfer:features:read:", own_buf, 20) == 0)
639 {
640 CORE_ADDR ofs;
641 unsigned int len, total_len;
642 const char *document;
643 char *annex;
644
2d717e4f
DJ
645 require_running (own_buf);
646
fb1e4ffc
DJ
647 /* Grab the annex, offset, and length. */
648 if (decode_xfer_read (own_buf + 20, &annex, &ofs, &len) < 0)
649 {
650 strcpy (own_buf, "E00");
651 return;
652 }
653
654 /* Now grab the correct annex. */
655 document = get_features_xml (annex);
656 if (document == NULL)
23181151
DJ
657 {
658 strcpy (own_buf, "E00");
659 return;
660 }
661
662 total_len = strlen (document);
663 if (len > PBUFSIZ - 2)
664 len = PBUFSIZ - 2;
665
666 if (ofs > total_len)
667 write_enn (own_buf);
668 else if (len < total_len - ofs)
669 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
670 len, 1);
671 else
672 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
673 total_len - ofs, 0);
674
675 return;
676 }
677
255e7678
DJ
678 if (strncmp ("qXfer:libraries:read:", own_buf, 21) == 0)
679 {
680 CORE_ADDR ofs;
681 unsigned int len, total_len;
682 char *document, *p;
683 struct inferior_list_entry *dll_ptr;
684 char *annex;
685
2d717e4f
DJ
686 require_running (own_buf);
687
255e7678
DJ
688 /* Reject any annex; grab the offset and length. */
689 if (decode_xfer_read (own_buf + 21, &annex, &ofs, &len) < 0
690 || annex[0] != '\0')
691 {
692 strcpy (own_buf, "E00");
693 return;
694 }
695
696 /* Over-estimate the necessary memory. Assume that every character
697 in the library name must be escaped. */
698 total_len = 64;
699 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
700 total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name);
701
702 document = malloc (total_len);
703 strcpy (document, "<library-list>\n");
704 p = document + strlen (document);
705
706 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
707 {
708 struct dll_info *dll = (struct dll_info *) dll_ptr;
709 char *name;
710
711 strcpy (p, " <library name=\"");
712 p = p + strlen (p);
713 name = xml_escape_text (dll->name);
714 strcpy (p, name);
715 free (name);
716 p = p + strlen (p);
717 strcpy (p, "\"><segment address=\"");
718 p = p + strlen (p);
719 sprintf (p, "0x%lx", (long) dll->base_addr);
720 p = p + strlen (p);
721 strcpy (p, "\"/></library>\n");
722 p = p + strlen (p);
723 }
724
725 strcpy (p, "</library-list>\n");
726
727 total_len = strlen (document);
728 if (len > PBUFSIZ - 2)
729 len = PBUFSIZ - 2;
730
731 if (ofs > total_len)
732 write_enn (own_buf);
733 else if (len < total_len - ofs)
734 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
735 len, 1);
736 else
737 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
738 total_len - ofs, 0);
739
740 free (document);
741 return;
742 }
743
be2a5f71
DJ
744 /* Protocol features query. */
745 if (strncmp ("qSupported", own_buf, 10) == 0
746 && (own_buf[10] == ':' || own_buf[10] == '\0'))
747 {
89be2091 748 sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1);
0876f84a 749
255e7678
DJ
750 /* We do not have any hook to indicate whether the target backend
751 supports qXfer:libraries:read, so always report it. */
752 strcat (own_buf, ";qXfer:libraries:read+");
753
0876f84a 754 if (the_target->read_auxv != NULL)
9f2e1e63 755 strcat (own_buf, ";qXfer:auxv:read+");
2d717e4f 756
0e7f50da
UW
757 if (the_target->qxfer_spu != NULL)
758 strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+");
0876f84a 759
221c031f
UW
760 /* We always report qXfer:features:read, as targets may
761 install XML files on a subsequent call to arch_setup.
762 If we reported to GDB on startup that we don't support
763 qXfer:feature:read at all, we will never be re-queried. */
764 strcat (own_buf, ";qXfer:features:read+");
23181151 765
be2a5f71
DJ
766 return;
767 }
768
dae5f5cf
DJ
769 /* Thread-local storage support. */
770 if (the_target->get_tls_address != NULL
771 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
772 {
773 char *p = own_buf + 12;
774 CORE_ADDR parts[3], address = 0;
775 int i, err;
776
2d717e4f
DJ
777 require_running (own_buf);
778
dae5f5cf
DJ
779 for (i = 0; i < 3; i++)
780 {
781 char *p2;
782 int len;
783
784 if (p == NULL)
785 break;
786
787 p2 = strchr (p, ',');
788 if (p2)
789 {
790 len = p2 - p;
791 p2++;
792 }
793 else
794 {
795 len = strlen (p);
796 p2 = NULL;
797 }
798
799 decode_address (&parts[i], p, len);
800 p = p2;
801 }
802
803 if (p != NULL || i < 3)
804 err = 1;
805 else
806 {
807 struct thread_info *thread = gdb_id_to_thread (parts[0]);
808
809 if (thread == NULL)
810 err = 2;
811 else
812 err = the_target->get_tls_address (thread, parts[1], parts[2],
813 &address);
814 }
815
816 if (err == 0)
817 {
818 sprintf (own_buf, "%llx", address);
819 return;
820 }
821 else if (err > 0)
822 {
823 write_enn (own_buf);
824 return;
825 }
826
827 /* Otherwise, pretend we do not understand this packet. */
828 }
829
c74d0ad8
DJ
830 /* Handle "monitor" commands. */
831 if (strncmp ("qRcmd,", own_buf, 6) == 0)
832 {
833 char *mon = malloc (PBUFSIZ);
834 int len = strlen (own_buf + 6);
835
d41b6bb4 836 if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
c74d0ad8
DJ
837 {
838 write_enn (own_buf);
839 free (mon);
840 return;
841 }
842 mon[len / 2] = '\0';
843
844 write_ok (own_buf);
845
846 if (strcmp (mon, "set debug 1") == 0)
847 {
848 debug_threads = 1;
849 monitor_output ("Debug output enabled.\n");
850 }
851 else if (strcmp (mon, "set debug 0") == 0)
852 {
853 debug_threads = 0;
854 monitor_output ("Debug output disabled.\n");
855 }
856 else if (strcmp (mon, "set remote-debug 1") == 0)
857 {
858 remote_debug = 1;
859 monitor_output ("Protocol debug output enabled.\n");
860 }
861 else if (strcmp (mon, "set remote-debug 0") == 0)
862 {
863 remote_debug = 0;
864 monitor_output ("Protocol debug output disabled.\n");
865 }
866 else if (strcmp (mon, "help") == 0)
867 monitor_show_help ();
2d717e4f
DJ
868 else if (strcmp (mon, "exit") == 0)
869 exit_requested = 1;
c74d0ad8
DJ
870 else
871 {
872 monitor_output ("Unknown monitor command.\n\n");
873 monitor_show_help ();
874 write_enn (own_buf);
875 }
876
877 free (mon);
878 return;
879 }
880
08388c79
DE
881 if (strncmp ("qSearch:memory:", own_buf, sizeof ("qSearch:memory:") - 1) == 0)
882 {
883 require_running (own_buf);
884 handle_search_memory (own_buf, packet_len);
885 return;
886 }
887
ce3a066d
DJ
888 /* Otherwise we didn't know what packet it was. Say we didn't
889 understand it. */
890 own_buf[0] = 0;
891}
892
64386c31
DJ
893/* Parse vCont packets. */
894void
fc620387 895handle_v_cont (char *own_buf, char *status, int *signal)
64386c31
DJ
896{
897 char *p, *q;
898 int n = 0, i = 0;
899 struct thread_resume *resume_info, default_action;
900
901 /* Count the number of semicolons in the packet. There should be one
902 for every action. */
903 p = &own_buf[5];
904 while (p)
905 {
906 n++;
907 p++;
908 p = strchr (p, ';');
909 }
910 /* Allocate room for one extra action, for the default remain-stopped
911 behavior; if no default action is in the list, we'll need the extra
912 slot. */
913 resume_info = malloc ((n + 1) * sizeof (resume_info[0]));
914
915 default_action.thread = -1;
916 default_action.leave_stopped = 1;
917 default_action.step = 0;
918 default_action.sig = 0;
919
920 p = &own_buf[5];
921 i = 0;
922 while (*p)
923 {
924 p++;
925
926 resume_info[i].leave_stopped = 0;
927
928 if (p[0] == 's' || p[0] == 'S')
929 resume_info[i].step = 1;
930 else if (p[0] == 'c' || p[0] == 'C')
931 resume_info[i].step = 0;
932 else
933 goto err;
934
935 if (p[0] == 'S' || p[0] == 'C')
936 {
937 int sig;
938 sig = strtol (p + 1, &q, 16);
939 if (p == q)
940 goto err;
941 p = q;
942
943 if (!target_signal_to_host_p (sig))
944 goto err;
945 resume_info[i].sig = target_signal_to_host (sig);
946 }
947 else
948 {
949 resume_info[i].sig = 0;
950 p = p + 1;
951 }
952
953 if (p[0] == 0)
954 {
955 resume_info[i].thread = -1;
956 default_action = resume_info[i];
957
958 /* Note: we don't increment i here, we'll overwrite this entry
959 the next time through. */
960 }
961 else if (p[0] == ':')
962 {
a06660f7
DJ
963 unsigned int gdb_id = strtoul (p + 1, &q, 16);
964 unsigned long thread_id;
965
64386c31
DJ
966 if (p == q)
967 goto err;
968 p = q;
969 if (p[0] != ';' && p[0] != 0)
970 goto err;
971
a06660f7
DJ
972 thread_id = gdb_id_to_thread_id (gdb_id);
973 if (thread_id)
974 resume_info[i].thread = thread_id;
975 else
976 goto err;
977
64386c31
DJ
978 i++;
979 }
980 }
981
982 resume_info[i] = default_action;
983
984 /* Still used in occasional places in the backend. */
985 if (n == 1 && resume_info[0].thread != -1)
986 cont_thread = resume_info[0].thread;
987 else
988 cont_thread = -1;
dc3f8883 989 set_desired_inferior (0);
64386c31 990
a20d5e98 991 enable_async_io ();
64386c31
DJ
992 (*the_target->resume) (resume_info);
993
994 free (resume_info);
995
996 *signal = mywait (status, 1);
997 prepare_resume_reply (own_buf, *status, *signal);
a20d5e98 998 disable_async_io ();
64386c31
DJ
999 return;
1000
1001err:
255e7678 1002 write_enn (own_buf);
64386c31
DJ
1003 free (resume_info);
1004 return;
1005}
1006
2d717e4f
DJ
1007/* Attach to a new program. Return 1 if successful, 0 if failure. */
1008int
1009handle_v_attach (char *own_buf, char *status, int *signal)
1010{
1011 int pid;
1012
1013 pid = strtol (own_buf + 8, NULL, 16);
1014 if (pid != 0 && attach_inferior (pid, status, signal) == 0)
1015 {
1016 prepare_resume_reply (own_buf, *status, *signal);
1017 return 1;
1018 }
1019 else
1020 {
1021 write_enn (own_buf);
1022 return 0;
1023 }
1024}
1025
1026/* Run a new program. Return 1 if successful, 0 if failure. */
1027static int
1028handle_v_run (char *own_buf, char *status, int *signal)
1029{
1030 char *p, **pp, *next_p, **new_argv;
1031 int i, new_argc;
1032
1033 new_argc = 0;
1034 for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';'))
1035 {
1036 p++;
1037 new_argc++;
1038 }
1039
1040 new_argv = malloc ((new_argc + 2) * sizeof (char *));
1041 i = 0;
1042 for (p = own_buf + strlen ("vRun;"); *p; p = next_p)
1043 {
1044 next_p = strchr (p, ';');
1045 if (next_p == NULL)
1046 next_p = p + strlen (p);
1047
1048 if (i == 0 && p == next_p)
1049 new_argv[i] = NULL;
1050 else
1051 {
1052 new_argv[i] = malloc (1 + (next_p - p) / 2);
1053 unhexify (new_argv[i], p, (next_p - p) / 2);
1054 new_argv[i][(next_p - p) / 2] = '\0';
1055 }
1056
1057 if (*next_p)
1058 next_p++;
1059 i++;
1060 }
1061 new_argv[i] = NULL;
1062
1063 if (new_argv[0] == NULL)
1064 {
1065 if (program_argv == NULL)
1066 {
1067 write_enn (own_buf);
1068 return 0;
1069 }
1070
1071 new_argv[0] = strdup (program_argv[0]);
1072 }
1073
1074 /* Free the old argv. */
1075 if (program_argv)
1076 {
1077 for (pp = program_argv; *pp != NULL; pp++)
1078 free (*pp);
1079 free (program_argv);
1080 }
1081 program_argv = new_argv;
1082
1083 *signal = start_inferior (program_argv, status);
1084 if (*status == 'T')
1085 {
1086 prepare_resume_reply (own_buf, *status, *signal);
1087 return 1;
1088 }
1089 else
1090 {
1091 write_enn (own_buf);
1092 return 0;
1093 }
1094}
1095
64386c31
DJ
1096/* Handle all of the extended 'v' packets. */
1097void
a6b151f1
DJ
1098handle_v_requests (char *own_buf, char *status, int *signal,
1099 int packet_len, int *new_packet_len)
64386c31
DJ
1100{
1101 if (strncmp (own_buf, "vCont;", 6) == 0)
1102 {
2d717e4f 1103 require_running (own_buf);
64386c31
DJ
1104 handle_v_cont (own_buf, status, signal);
1105 return;
1106 }
1107
1108 if (strncmp (own_buf, "vCont?", 6) == 0)
1109 {
1110 strcpy (own_buf, "vCont;c;C;s;S");
1111 return;
1112 }
1113
a6b151f1
DJ
1114 if (strncmp (own_buf, "vFile:", 6) == 0
1115 && handle_vFile (own_buf, packet_len, new_packet_len))
1116 return;
1117
2d717e4f
DJ
1118 if (strncmp (own_buf, "vAttach;", 8) == 0)
1119 {
1120 if (target_running ())
1121 {
fd96d250
PA
1122 fprintf (stderr, "Already debugging a process\n");
1123 write_enn (own_buf);
1124 return;
2d717e4f
DJ
1125 }
1126 handle_v_attach (own_buf, status, signal);
1127 return;
1128 }
1129
1130 if (strncmp (own_buf, "vRun;", 5) == 0)
1131 {
1132 if (target_running ())
1133 {
fd96d250
PA
1134 fprintf (stderr, "Already debugging a process\n");
1135 write_enn (own_buf);
1136 return;
2d717e4f
DJ
1137 }
1138 handle_v_run (own_buf, status, signal);
1139 return;
1140 }
1141
64386c31
DJ
1142 /* Otherwise we didn't know what packet it was. Say we didn't
1143 understand it. */
1144 own_buf[0] = 0;
1145 return;
1146}
1147
1148void
27524b67 1149myresume (char *own_buf, int step, int *signalp, char *statusp)
64386c31
DJ
1150{
1151 struct thread_resume resume_info[2];
1152 int n = 0;
a20d5e98
DJ
1153 int sig = *signalp;
1154
1155 set_desired_inferior (0);
64386c31 1156
d592fa2f 1157 if (step || sig || (cont_thread != 0 && cont_thread != -1))
64386c31
DJ
1158 {
1159 resume_info[0].thread
1160 = ((struct inferior_list_entry *) current_inferior)->id;
1161 resume_info[0].step = step;
1162 resume_info[0].sig = sig;
1163 resume_info[0].leave_stopped = 0;
1164 n++;
1165 }
1166 resume_info[n].thread = -1;
1167 resume_info[n].step = 0;
1168 resume_info[n].sig = 0;
d592fa2f 1169 resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1);
64386c31 1170
a20d5e98 1171 enable_async_io ();
64386c31 1172 (*the_target->resume) (resume_info);
a20d5e98
DJ
1173 *signalp = mywait (statusp, 1);
1174 prepare_resume_reply (own_buf, *statusp, *signalp);
1175 disable_async_io ();
64386c31
DJ
1176}
1177
dd24457d
DJ
1178static void
1179gdbserver_version (void)
1180{
c16158bc 1181 printf ("GNU gdbserver %s%s\n"
255e7678 1182 "Copyright (C) 2007 Free Software Foundation, Inc.\n"
dd24457d
DJ
1183 "gdbserver is free software, covered by the GNU General Public License.\n"
1184 "This gdbserver was configured as \"%s\"\n",
c16158bc 1185 PKGVERSION, version, host_name);
dd24457d
DJ
1186}
1187
0bc68c49 1188static void
c16158bc 1189gdbserver_usage (FILE *stream)
0bc68c49 1190{
c16158bc
JM
1191 fprintf (stream, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
1192 "\tgdbserver [OPTIONS] --attach COMM PID\n"
1193 "\tgdbserver [OPTIONS] --multi COMM\n"
1194 "\n"
1195 "COMM may either be a tty device (for serial debugging), or \n"
1196 "HOST:PORT to listen for a TCP connection.\n"
1197 "\n"
1198 "Options:\n"
1199 " --debug\t\tEnable debugging output.\n"
1200 " --wrapper WRAPPER --\tRun WRAPPER to start new programs.\n");
1201 if (REPORT_BUGS_TO[0] && stream == stdout)
1202 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
0bc68c49
DJ
1203}
1204
2d717e4f
DJ
1205#undef require_running
1206#define require_running(BUF) \
1207 if (!target_running ()) \
1208 { \
1209 write_enn (BUF); \
1210 break; \
1211 }
1212
c906108c 1213int
da85418c 1214main (int argc, char *argv[])
c906108c 1215{
f450004a 1216 char ch, status, *own_buf;
7fb85e41 1217 unsigned char *mem_buf;
c906108c 1218 int i = 0;
fc620387 1219 int signal;
c906108c
SS
1220 unsigned int len;
1221 CORE_ADDR mem_addr;
0729219d
DJ
1222 int bad_attach;
1223 int pid;
2d717e4f
DJ
1224 char *arg_end, *port;
1225 char **next_arg = &argv[1];
1226 int multi_mode = 0;
1227 int attach = 0;
1228 int was_running;
c906108c 1229
2d717e4f 1230 while (*next_arg != NULL && **next_arg == '-')
dd24457d 1231 {
2d717e4f
DJ
1232 if (strcmp (*next_arg, "--version") == 0)
1233 {
1234 gdbserver_version ();
1235 exit (0);
1236 }
1237 else if (strcmp (*next_arg, "--help") == 0)
1238 {
c16158bc 1239 gdbserver_usage (stdout);
2d717e4f
DJ
1240 exit (0);
1241 }
1242 else if (strcmp (*next_arg, "--attach") == 0)
1243 attach = 1;
1244 else if (strcmp (*next_arg, "--multi") == 0)
1245 multi_mode = 1;
ccd213ac
DJ
1246 else if (strcmp (*next_arg, "--wrapper") == 0)
1247 {
1248 next_arg++;
1249
1250 wrapper_argv = next_arg;
1251 while (*next_arg != NULL && strcmp (*next_arg, "--") != 0)
1252 next_arg++;
1253
1254 if (next_arg == wrapper_argv || *next_arg == NULL)
1255 {
c16158bc 1256 gdbserver_usage (stderr);
ccd213ac
DJ
1257 exit (1);
1258 }
1259
1260 /* Consume the "--". */
1261 *next_arg = NULL;
1262 }
2d717e4f
DJ
1263 else if (strcmp (*next_arg, "--debug") == 0)
1264 debug_threads = 1;
1265 else
1266 {
1267 fprintf (stderr, "Unknown argument: %s\n", *next_arg);
1268 exit (1);
1269 }
dd24457d 1270
2d717e4f
DJ
1271 next_arg++;
1272 continue;
dd24457d
DJ
1273 }
1274
c5aa993b 1275 if (setjmp (toplevel))
c906108c 1276 {
c5aa993b
JM
1277 fprintf (stderr, "Exiting\n");
1278 exit (1);
c906108c
SS
1279 }
1280
2d717e4f
DJ
1281 port = *next_arg;
1282 next_arg++;
1283 if (port == NULL || (!attach && !multi_mode && *next_arg == NULL))
1284 {
c16158bc 1285 gdbserver_usage (stderr);
2d717e4f
DJ
1286 exit (1);
1287 }
1288
0729219d
DJ
1289 bad_attach = 0;
1290 pid = 0;
2d717e4f
DJ
1291
1292 /* --attach used to come after PORT, so allow it there for
1293 compatibility. */
1294 if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0)
45b7b345 1295 {
2d717e4f
DJ
1296 attach = 1;
1297 next_arg++;
45b7b345
DJ
1298 }
1299
2d717e4f
DJ
1300 if (attach
1301 && (*next_arg == NULL
1302 || (*next_arg)[0] == '\0'
1303 || (pid = strtoul (*next_arg, &arg_end, 0)) == 0
1304 || *arg_end != '\0'
1305 || next_arg[1] != NULL))
1306 bad_attach = 1;
1307
1308 if (bad_attach)
dd24457d 1309 {
c16158bc 1310 gdbserver_usage (stderr);
dd24457d
DJ
1311 exit (1);
1312 }
c906108c 1313
a20d5e98 1314 initialize_async_io ();
4ce44c66
JM
1315 initialize_low ();
1316
255e7678 1317 own_buf = malloc (PBUFSIZ + 1);
7fb85e41 1318 mem_buf = malloc (PBUFSIZ);
0a30fbc4 1319
2d717e4f 1320 if (pid == 0 && *next_arg != NULL)
45b7b345 1321 {
2d717e4f
DJ
1322 int i, n;
1323
1324 n = argc - (next_arg - argv);
1325 program_argv = malloc (sizeof (char *) * (n + 1));
1326 for (i = 0; i < n; i++)
1327 program_argv[i] = strdup (next_arg[i]);
1328 program_argv[i] = NULL;
1329
45b7b345 1330 /* Wait till we are at first instruction in program. */
2d717e4f 1331 signal = start_inferior (program_argv, &status);
c906108c 1332
c588c53c
MS
1333 /* We are now (hopefully) stopped at the first instruction of
1334 the target process. This assumes that the target process was
1335 successfully created. */
45b7b345 1336 }
2d717e4f
DJ
1337 else if (pid != 0)
1338 {
1339 if (attach_inferior (pid, &status, &signal) == -1)
1340 error ("Attaching not supported on this target");
1341
1342 /* Otherwise succeeded. */
1343 }
45b7b345
DJ
1344 else
1345 {
2d717e4f
DJ
1346 status = 'W';
1347 signal = 0;
45b7b345 1348 }
c906108c 1349
311de423
PA
1350 /* Don't report shared library events on the initial connection,
1351 even if some libraries are preloaded. Avoids the "stopped by
1352 shared library event" notice on gdb side. */
1353 dlls_changed = 0;
1354
8264bb58
DJ
1355 if (setjmp (toplevel))
1356 {
1357 fprintf (stderr, "Killing inferior\n");
1358 kill_inferior ();
1359 exit (1);
1360 }
1361
c588c53c 1362 if (status == 'W' || status == 'X')
2d717e4f
DJ
1363 was_running = 0;
1364 else
1365 was_running = 1;
1366
1367 if (!was_running && !multi_mode)
c588c53c 1368 {
2d717e4f 1369 fprintf (stderr, "No program to debug. GDBserver exiting.\n");
c588c53c
MS
1370 exit (1);
1371 }
1372
c906108c
SS
1373 while (1)
1374 {
2d717e4f 1375 remote_open (port);
c906108c 1376
c5aa993b 1377 restart:
2d717e4f
DJ
1378 if (setjmp (toplevel) != 0)
1379 {
1380 /* An error occurred. */
1381 if (response_needed)
1382 {
1383 write_enn (own_buf);
1384 putpkt (own_buf);
1385 }
1386 }
1387
a20d5e98 1388 disable_async_io ();
2d717e4f 1389 while (!exit_requested)
c906108c
SS
1390 {
1391 unsigned char sig;
01f9e8fa
DJ
1392 int packet_len;
1393 int new_packet_len = -1;
1394
2d717e4f 1395 response_needed = 0;
01f9e8fa
DJ
1396 packet_len = getpkt (own_buf);
1397 if (packet_len <= 0)
1398 break;
2d717e4f 1399 response_needed = 1;
01f9e8fa 1400
c906108c
SS
1401 i = 0;
1402 ch = own_buf[i++];
1403 switch (ch)
1404 {
ce3a066d 1405 case 'q':
0e7f50da 1406 handle_query (own_buf, packet_len, &new_packet_len);
ce3a066d 1407 break;
89be2091
DJ
1408 case 'Q':
1409 handle_general_set (own_buf);
1410 break;
6ad8ae5c 1411 case 'D':
2d717e4f 1412 require_running (own_buf);
6ad8ae5c 1413 fprintf (stderr, "Detaching from inferior\n");
444d6139 1414 if (detach_inferior () != 0)
2d717e4f 1415 write_enn (own_buf);
444d6139
PA
1416 else
1417 {
1418 write_ok (own_buf);
6ad8ae5c 1419
2d717e4f
DJ
1420 if (extended_protocol)
1421 {
1422 /* Treat this like a normal program exit. */
1423 signal = 0;
1424 status = 'W';
1425 }
1426 else
1427 {
1428 putpkt (own_buf);
1429 remote_close ();
6ad8ae5c 1430
2d717e4f
DJ
1431 /* If we are attached, then we can exit. Otherwise, we
1432 need to hang around doing nothing, until the child
1433 is gone. */
1434 if (!attached)
1435 join_inferior ();
1436
1437 exit (0);
1438 }
444d6139 1439 }
2d717e4f 1440 break;
c906108c 1441 case '!':
2d717e4f
DJ
1442 extended_protocol = 1;
1443 write_ok (own_buf);
c906108c
SS
1444 break;
1445 case '?':
1446 prepare_resume_reply (own_buf, status, signal);
1447 break;
1448 case 'H':
a06660f7 1449 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
c906108c 1450 {
a06660f7
DJ
1451 unsigned long gdb_id, thread_id;
1452
2d717e4f 1453 require_running (own_buf);
a06660f7 1454 gdb_id = strtoul (&own_buf[2], NULL, 16);
2d717e4f
DJ
1455 if (gdb_id == 0 || gdb_id == -1)
1456 thread_id = gdb_id;
1457 else
a06660f7 1458 {
2d717e4f
DJ
1459 thread_id = gdb_id_to_thread_id (gdb_id);
1460 if (thread_id == 0)
1461 {
1462 write_enn (own_buf);
1463 break;
1464 }
a06660f7
DJ
1465 }
1466
1467 if (own_buf[1] == 'g')
1468 {
1469 general_thread = thread_id;
1470 set_desired_inferior (1);
1471 }
1472 else if (own_buf[1] == 'c')
1473 cont_thread = thread_id;
1474 else if (own_buf[1] == 's')
1475 step_thread = thread_id;
1476
0d62e5e8 1477 write_ok (own_buf);
a06660f7
DJ
1478 }
1479 else
1480 {
c906108c
SS
1481 /* Silently ignore it so that gdb can extend the protocol
1482 without compatibility headaches. */
1483 own_buf[0] = '\0';
c906108c
SS
1484 }
1485 break;
1486 case 'g':
2d717e4f 1487 require_running (own_buf);
0d62e5e8 1488 set_desired_inferior (1);
0a30fbc4 1489 registers_to_string (own_buf);
c906108c
SS
1490 break;
1491 case 'G':
2d717e4f 1492 require_running (own_buf);
0d62e5e8 1493 set_desired_inferior (1);
0a30fbc4 1494 registers_from_string (&own_buf[1]);
c906108c
SS
1495 write_ok (own_buf);
1496 break;
1497 case 'm':
2d717e4f 1498 require_running (own_buf);
c906108c 1499 decode_m_packet (&own_buf[1], &mem_addr, &len);
c3e735a6
DJ
1500 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
1501 convert_int_to_ascii (mem_buf, own_buf, len);
1502 else
1503 write_enn (own_buf);
c906108c
SS
1504 break;
1505 case 'M':
2d717e4f 1506 require_running (own_buf);
c906108c
SS
1507 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
1508 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
1509 write_ok (own_buf);
1510 else
1511 write_enn (own_buf);
1512 break;
01f9e8fa 1513 case 'X':
2d717e4f 1514 require_running (own_buf);
01f9e8fa
DJ
1515 if (decode_X_packet (&own_buf[1], packet_len - 1,
1516 &mem_addr, &len, mem_buf) < 0
1517 || write_inferior_memory (mem_addr, mem_buf, len) != 0)
1518 write_enn (own_buf);
1519 else
1520 write_ok (own_buf);
1521 break;
c906108c 1522 case 'C':
2d717e4f 1523 require_running (own_buf);
c906108c 1524 convert_ascii_to_int (own_buf + 1, &sig, 1);
0e98d0a7
DJ
1525 if (target_signal_to_host_p (sig))
1526 signal = target_signal_to_host (sig);
1527 else
1528 signal = 0;
27524b67 1529 myresume (own_buf, 0, &signal, &status);
c906108c
SS
1530 break;
1531 case 'S':
2d717e4f 1532 require_running (own_buf);
c906108c 1533 convert_ascii_to_int (own_buf + 1, &sig, 1);
0e98d0a7
DJ
1534 if (target_signal_to_host_p (sig))
1535 signal = target_signal_to_host (sig);
1536 else
1537 signal = 0;
27524b67 1538 myresume (own_buf, 1, &signal, &status);
c906108c
SS
1539 break;
1540 case 'c':
2d717e4f 1541 require_running (own_buf);
a20d5e98 1542 signal = 0;
27524b67 1543 myresume (own_buf, 0, &signal, &status);
c906108c
SS
1544 break;
1545 case 's':
2d717e4f 1546 require_running (own_buf);
a20d5e98 1547 signal = 0;
27524b67 1548 myresume (own_buf, 1, &signal, &status);
c906108c 1549 break;
e013ee27
OF
1550 case 'Z':
1551 {
1552 char *lenptr;
1553 char *dataptr;
1554 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1555 int len = strtol (lenptr + 1, &dataptr, 16);
1556 char type = own_buf[1];
1557
1558 if (the_target->insert_watchpoint == NULL
1559 || (type < '2' || type > '4'))
1560 {
1561 /* No watchpoint support or not a watchpoint command;
1562 unrecognized either way. */
1563 own_buf[0] = '\0';
1564 }
1565 else
1566 {
1567 int res;
1568
2d717e4f 1569 require_running (own_buf);
e013ee27
OF
1570 res = (*the_target->insert_watchpoint) (type, addr, len);
1571 if (res == 0)
1572 write_ok (own_buf);
1573 else if (res == 1)
1574 /* Unsupported. */
1575 own_buf[0] = '\0';
1576 else
1577 write_enn (own_buf);
1578 }
1579 break;
1580 }
1581 case 'z':
1582 {
1583 char *lenptr;
1584 char *dataptr;
1585 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1586 int len = strtol (lenptr + 1, &dataptr, 16);
1587 char type = own_buf[1];
1588
1589 if (the_target->remove_watchpoint == NULL
1590 || (type < '2' || type > '4'))
1591 {
1592 /* No watchpoint support or not a watchpoint command;
1593 unrecognized either way. */
1594 own_buf[0] = '\0';
1595 }
1596 else
1597 {
1598 int res;
1599
2d717e4f 1600 require_running (own_buf);
e013ee27
OF
1601 res = (*the_target->remove_watchpoint) (type, addr, len);
1602 if (res == 0)
1603 write_ok (own_buf);
1604 else if (res == 1)
1605 /* Unsupported. */
1606 own_buf[0] = '\0';
1607 else
1608 write_enn (own_buf);
1609 }
1610 break;
1611 }
c906108c 1612 case 'k':
2d717e4f
DJ
1613 response_needed = 0;
1614 if (!target_running ())
1615 /* The packet we received doesn't make sense - but we
1616 can't reply to it, either. */
1617 goto restart;
1618
c906108c
SS
1619 fprintf (stderr, "Killing inferior\n");
1620 kill_inferior ();
2d717e4f
DJ
1621
1622 /* When using the extended protocol, we wait with no
1623 program running. The traditional protocol will exit
1624 instead. */
c906108c
SS
1625 if (extended_protocol)
1626 {
2d717e4f
DJ
1627 status = 'X';
1628 signal = TARGET_SIGNAL_KILL;
1629 was_running = 0;
c906108c 1630 goto restart;
c906108c
SS
1631 }
1632 else
1633 {
1634 exit (0);
1635 break;
1636 }
1637 case 'T':
a06660f7
DJ
1638 {
1639 unsigned long gdb_id, thread_id;
1640
2d717e4f 1641 require_running (own_buf);
a06660f7
DJ
1642 gdb_id = strtoul (&own_buf[1], NULL, 16);
1643 thread_id = gdb_id_to_thread_id (gdb_id);
1644 if (thread_id == 0)
1645 {
1646 write_enn (own_buf);
1647 break;
1648 }
1649
1650 if (mythread_alive (thread_id))
1651 write_ok (own_buf);
1652 else
1653 write_enn (own_buf);
1654 }
c906108c
SS
1655 break;
1656 case 'R':
2d717e4f
DJ
1657 response_needed = 0;
1658
c906108c 1659 /* Restarting the inferior is only supported in the
c5aa993b 1660 extended protocol. */
c906108c
SS
1661 if (extended_protocol)
1662 {
2d717e4f
DJ
1663 if (target_running ())
1664 kill_inferior ();
c906108c
SS
1665 fprintf (stderr, "GDBserver restarting\n");
1666
1667 /* Wait till we are at 1st instruction in prog. */
2d717e4f
DJ
1668 if (program_argv != NULL)
1669 signal = start_inferior (program_argv, &status);
1670 else
1671 {
1672 status = 'X';
1673 signal = TARGET_SIGNAL_KILL;
1674 }
c906108c 1675 goto restart;
c906108c
SS
1676 }
1677 else
1678 {
1679 /* It is a request we don't understand. Respond with an
1680 empty packet so that gdb knows that we don't support this
1681 request. */
1682 own_buf[0] = '\0';
1683 break;
1684 }
64386c31
DJ
1685 case 'v':
1686 /* Extended (long) request. */
a6b151f1
DJ
1687 handle_v_requests (own_buf, &status, &signal,
1688 packet_len, &new_packet_len);
64386c31 1689 break;
a6b151f1 1690
c906108c
SS
1691 default:
1692 /* It is a request we don't understand. Respond with an
c5aa993b
JM
1693 empty packet so that gdb knows that we don't support this
1694 request. */
c906108c
SS
1695 own_buf[0] = '\0';
1696 break;
1697 }
1698
01f9e8fa
DJ
1699 if (new_packet_len != -1)
1700 putpkt_binary (own_buf, new_packet_len);
1701 else
1702 putpkt (own_buf);
c906108c 1703
2d717e4f
DJ
1704 response_needed = 0;
1705
1706 if (was_running && (status == 'W' || status == 'X'))
c906108c 1707 {
2d717e4f 1708 was_running = 0;
c906108c 1709
2d717e4f
DJ
1710 if (status == 'W')
1711 fprintf (stderr,
1712 "\nChild exited with status %d\n", signal);
1713 if (status == 'X')
1714 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
1715 target_signal_to_host (signal),
1716 target_signal_to_name (signal));
1717
1718 if (extended_protocol)
1719 goto restart;
c906108c
SS
1720 else
1721 {
1722 fprintf (stderr, "GDBserver exiting\n");
1723 exit (0);
1724 }
1725 }
c906108c 1726
2d717e4f
DJ
1727 if (status != 'W' && status != 'X')
1728 was_running = 1;
1729 }
c906108c 1730
2d717e4f
DJ
1731 /* If an exit was requested (using the "monitor exit" command),
1732 terminate now. The only other way to get here is for
1733 getpkt to fail; close the connection and reopen it at the
1734 top of the loop. */
c906108c 1735
2d717e4f 1736 if (exit_requested)
c906108c
SS
1737 {
1738 remote_close ();
2d717e4f
DJ
1739 if (attached && target_running ())
1740 detach_inferior ();
1741 else if (target_running ())
1742 kill_inferior ();
c906108c
SS
1743 exit (0);
1744 }
1745 else
1746 {
45b7b345
DJ
1747 fprintf (stderr, "Remote side has terminated connection. "
1748 "GDBserver will reopen the connection.\n");
c906108c
SS
1749 remote_close ();
1750 }
1751 }
1752}
This page took 0.676951 seconds and 4 git commands to generate.