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