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