* linux-low.c (linux_wait_1): Avoid setting need_step_over is
[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,
4c38e0a4 3 2004, 2005, 2006, 2007, 2008, 2009, 2010 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
95954743
PA
35ptid_t cont_thread;
36ptid_t general_thread;
37ptid_t step_thread;
5b1c542e 38
0d62e5e8
DJ
39int server_waiting;
40
2d717e4f 41static int extended_protocol;
2d717e4f
DJ
42static int response_needed;
43static int exit_requested;
44
95954743 45int multi_process;
bd99dc85
PA
46int non_stop;
47
ccd213ac 48static char **program_argv, **wrapper_argv;
2d717e4f 49
c74d0ad8
DJ
50/* Enable miscellaneous debugging output. The name is historical - it
51 was originally used to debug LinuxThreads support. */
52int debug_threads;
53
aa5ca48f
DE
54/* Enable debugging of h/w breakpoint/watchpoint support. */
55int debug_hw_points;
56
89be2091
DJ
57int pass_signals[TARGET_SIGNAL_LAST];
58
c906108c 59jmp_buf toplevel;
c906108c 60
9b4b61c8
UW
61const char *gdbserver_xmltarget;
62
a9fa9f7d
DJ
63/* The PID of the originally created or attached inferior. Used to
64 send signals to the process when GDB sends us an asynchronous interrupt
65 (user hitting Control-C in the client), and to wait for the child to exit
66 when no longer debugging it. */
67
a1928bad 68unsigned long signal_pid;
a9fa9f7d 69
290fadea
RS
70#ifdef SIGTTOU
71/* A file descriptor for the controlling terminal. */
72int terminal_fd;
73
74/* TERMINAL_FD's original foreground group. */
75pid_t old_foreground_pgrp;
76
77/* Hand back terminal ownership to the original foreground group. */
78
79static void
80restore_old_foreground_pgrp (void)
81{
82 tcsetpgrp (terminal_fd, old_foreground_pgrp);
83}
84#endif
85
ec56be1b
PA
86/* Set if you want to disable optional thread related packets support
87 in gdbserver, for the sake of testing GDB against stubs that don't
88 support them. */
89int disable_packet_vCont;
90int disable_packet_Tthread;
91int disable_packet_qC;
92int disable_packet_qfThreadInfo;
93
5b1c542e
PA
94/* Last status reported to GDB. */
95static struct target_waitstatus last_status;
95954743 96static ptid_t last_ptid;
5b1c542e 97
bd99dc85
PA
98static char *own_buf;
99static unsigned char *mem_buf;
100
101/* Structure holding information relative to a single stop reply. We
102 keep a queue of these (really a singly-linked list) to push to GDB
103 in non-stop mode. */
104struct vstop_notif
105{
106 /* Pointer to next in list. */
107 struct vstop_notif *next;
108
109 /* Thread or process that got the event. */
95954743 110 ptid_t ptid;
bd99dc85
PA
111
112 /* Event info. */
113 struct target_waitstatus status;
114};
115
116/* The pending stop replies list head. */
117static struct vstop_notif *notif_queue = NULL;
118
119/* Put a stop reply to the stop reply queue. */
120
121static void
95954743 122queue_stop_reply (ptid_t ptid, struct target_waitstatus *status)
bd99dc85
PA
123{
124 struct vstop_notif *new_notif;
125
126 new_notif = malloc (sizeof (*new_notif));
127 new_notif->next = NULL;
128 new_notif->ptid = ptid;
129 new_notif->status = *status;
130
131 if (notif_queue)
132 {
133 struct vstop_notif *tail;
134 for (tail = notif_queue;
135 tail && tail->next;
136 tail = tail->next)
137 ;
138 tail->next = new_notif;
139 }
140 else
141 notif_queue = new_notif;
142
143 if (remote_debug)
144 {
145 int i = 0;
146 struct vstop_notif *n;
147
148 for (n = notif_queue; n; n = n->next)
149 i++;
150
151 fprintf (stderr, "pending stop replies: %d\n", i);
152 }
153}
154
155/* Place an event in the stop reply queue, and push a notification if
156 we aren't sending one yet. */
157
158void
95954743 159push_event (ptid_t ptid, struct target_waitstatus *status)
bd99dc85
PA
160{
161 queue_stop_reply (ptid, status);
162
163 /* If this is the first stop reply in the queue, then inform GDB
164 about it, by sending a Stop notification. */
165 if (notif_queue->next == NULL)
166 {
167 char *p = own_buf;
168 strcpy (p, "Stop:");
169 p += strlen (p);
170 prepare_resume_reply (p,
171 notif_queue->ptid, &notif_queue->status);
172 putpkt_notif (own_buf);
173 }
174}
175
95954743
PA
176/* Get rid of the currently pending stop replies for PID. If PID is
177 -1, then apply to all processes. */
bd99dc85
PA
178
179static void
95954743 180discard_queued_stop_replies (int pid)
bd99dc85 181{
95954743 182 struct vstop_notif *prev = NULL, *reply, *next;
bd99dc85 183
95954743 184 for (reply = notif_queue; reply; reply = next)
bd99dc85 185 {
95954743
PA
186 next = reply->next;
187
188 if (pid == -1
189 || ptid_get_pid (reply->ptid) == pid)
190 {
191 if (reply == notif_queue)
192 notif_queue = next;
193 else
194 prev->next = reply->next;
bd99dc85 195
95954743
PA
196 free (reply);
197 }
198 else
199 prev = reply;
bd99dc85
PA
200 }
201}
202
203/* If there are more stop replies to push, push one now. */
204
205static void
206send_next_stop_reply (char *own_buf)
207{
208 if (notif_queue)
209 prepare_resume_reply (own_buf,
210 notif_queue->ptid,
211 &notif_queue->status);
212 else
213 write_ok (own_buf);
214}
215
2d717e4f
DJ
216static int
217target_running (void)
218{
219 return all_threads.head != NULL;
220}
221
fc620387 222static int
5b1c542e 223start_inferior (char **argv)
c906108c 224{
ccd213ac 225 char **new_argv = argv;
2d717e4f 226
ccd213ac
DJ
227 if (wrapper_argv != NULL)
228 {
229 int i, count = 1;
230
231 for (i = 0; wrapper_argv[i] != NULL; i++)
232 count++;
233 for (i = 0; argv[i] != NULL; i++)
234 count++;
235 new_argv = alloca (sizeof (char *) * count);
236 count = 0;
237 for (i = 0; wrapper_argv[i] != NULL; i++)
238 new_argv[count++] = wrapper_argv[i];
239 for (i = 0; argv[i] != NULL; i++)
240 new_argv[count++] = argv[i];
241 new_argv[count] = NULL;
242 }
243
b80864fb 244#ifdef SIGTTOU
a9fa9f7d
DJ
245 signal (SIGTTOU, SIG_DFL);
246 signal (SIGTTIN, SIG_DFL);
b80864fb 247#endif
a9fa9f7d 248
ccd213ac 249 signal_pid = create_inferior (new_argv[0], new_argv);
0d62e5e8 250
c588c53c
MS
251 /* FIXME: we don't actually know at this point that the create
252 actually succeeded. We won't know that until we wait. */
a1928bad 253 fprintf (stderr, "Process %s created; pid = %ld\n", argv[0],
a9fa9f7d 254 signal_pid);
b80864fb 255 fflush (stderr);
a9fa9f7d 256
b80864fb 257#ifdef SIGTTOU
a9fa9f7d
DJ
258 signal (SIGTTOU, SIG_IGN);
259 signal (SIGTTIN, SIG_IGN);
290fadea
RS
260 terminal_fd = fileno (stderr);
261 old_foreground_pgrp = tcgetpgrp (terminal_fd);
262 tcsetpgrp (terminal_fd, signal_pid);
263 atexit (restore_old_foreground_pgrp);
b80864fb 264#endif
c906108c 265
ccd213ac
DJ
266 if (wrapper_argv != NULL)
267 {
268 struct thread_resume resume_info;
95954743 269 ptid_t ptid;
ccd213ac 270
95954743 271 resume_info.thread = pid_to_ptid (signal_pid);
bd99dc85 272 resume_info.kind = resume_continue;
ccd213ac 273 resume_info.sig = 0;
ccd213ac 274
95954743 275 ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
bd99dc85 276
5b1c542e
PA
277 if (last_status.kind != TARGET_WAITKIND_STOPPED)
278 return signal_pid;
ccd213ac
DJ
279
280 do
281 {
2bd7c093 282 (*the_target->resume) (&resume_info, 1);
ccd213ac 283
95954743 284 mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
5b1c542e
PA
285 if (last_status.kind != TARGET_WAITKIND_STOPPED)
286 return signal_pid;
ccd213ac 287 }
5b1c542e 288 while (last_status.value.sig != TARGET_SIGNAL_TRAP);
ccd213ac 289
5b1c542e 290 return signal_pid;
ccd213ac
DJ
291 }
292
5b1c542e
PA
293 /* Wait till we are at 1st instruction in program, return new pid
294 (assuming success). */
95954743 295 last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
5b1c542e
PA
296
297 return signal_pid;
c906108c
SS
298}
299
45b7b345 300static int
5b1c542e 301attach_inferior (int pid)
45b7b345
DJ
302{
303 /* myattach should return -1 if attaching is unsupported,
304 0 if it succeeded, and call error() otherwise. */
a9fa9f7d 305
45b7b345
DJ
306 if (myattach (pid) != 0)
307 return -1;
308
6910d122 309 fprintf (stderr, "Attached; pid = %d\n", pid);
b80864fb 310 fflush (stderr);
6910d122 311
a9fa9f7d
DJ
312 /* FIXME - It may be that we should get the SIGNAL_PID from the
313 attach function, so that it can be the main thread instead of
314 whichever we were told to attach to. */
315 signal_pid = pid;
316
bd99dc85
PA
317 if (!non_stop)
318 {
95954743 319 last_ptid = mywait (pid_to_ptid (pid), &last_status, 0, 0);
bd99dc85
PA
320
321 /* GDB knows to ignore the first SIGSTOP after attaching to a running
322 process using the "attach" command, but this is different; it's
323 just using "target remote". Pretend it's just starting up. */
324 if (last_status.kind == TARGET_WAITKIND_STOPPED
325 && last_status.value.sig == TARGET_SIGNAL_STOP)
326 last_status.value.sig = TARGET_SIGNAL_TRAP;
327 }
9db87ebd 328
45b7b345
DJ
329 return 0;
330}
331
c906108c 332extern int remote_debug;
ce3a066d 333
0876f84a
DJ
334/* Decode a qXfer read request. Return 0 if everything looks OK,
335 or -1 otherwise. */
336
337static int
338decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len)
339{
340 /* Extract and NUL-terminate the annex. */
341 *annex = buf;
342 while (*buf && *buf != ':')
343 buf++;
344 if (*buf == '\0')
345 return -1;
346 *buf++ = 0;
347
0e7f50da 348 /* After the read marker and annex, qXfer looks like a
0876f84a
DJ
349 traditional 'm' packet. */
350 decode_m_packet (buf, ofs, len);
351
352 return 0;
353}
354
355/* Write the response to a successful qXfer read. Returns the
356 length of the (binary) data stored in BUF, corresponding
357 to as much of DATA/LEN as we could fit. IS_MORE controls
358 the first character of the response. */
359static int
23181151 360write_qxfer_response (char *buf, const void *data, int len, int is_more)
0876f84a
DJ
361{
362 int out_len;
363
364 if (is_more)
365 buf[0] = 'm';
366 else
367 buf[0] = 'l';
368
369 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
370 PBUFSIZ - 2) + 1;
371}
372
89be2091
DJ
373/* Handle all of the extended 'Q' packets. */
374void
375handle_general_set (char *own_buf)
376{
377 if (strncmp ("QPassSignals:", own_buf, strlen ("QPassSignals:")) == 0)
378 {
379 int numsigs = (int) TARGET_SIGNAL_LAST, i;
380 const char *p = own_buf + strlen ("QPassSignals:");
381 CORE_ADDR cursig;
382
383 p = decode_address_to_semicolon (&cursig, p);
384 for (i = 0; i < numsigs; i++)
385 {
386 if (i == cursig)
387 {
388 pass_signals[i] = 1;
389 if (*p == '\0')
390 /* Keep looping, to clear the remaining signals. */
391 cursig = -1;
392 else
393 p = decode_address_to_semicolon (&cursig, p);
394 }
395 else
396 pass_signals[i] = 0;
397 }
398 strcpy (own_buf, "OK");
399 return;
400 }
401
a6f3e723
SL
402 if (strcmp (own_buf, "QStartNoAckMode") == 0)
403 {
404 if (remote_debug)
405 {
406 fprintf (stderr, "[noack mode enabled]\n");
407 fflush (stderr);
408 }
409
410 noack_mode = 1;
411 write_ok (own_buf);
412 return;
413 }
414
bd99dc85
PA
415 if (strncmp (own_buf, "QNonStop:", 9) == 0)
416 {
417 char *mode = own_buf + 9;
418 int req = -1;
419 char *req_str;
420
421 if (strcmp (mode, "0") == 0)
422 req = 0;
423 else if (strcmp (mode, "1") == 0)
424 req = 1;
425 else
426 {
427 /* We don't know what this mode is, so complain to
428 GDB. */
429 fprintf (stderr, "Unknown non-stop mode requested: %s\n",
430 own_buf);
431 write_enn (own_buf);
432 return;
433 }
434
435 req_str = req ? "non-stop" : "all-stop";
436 if (start_non_stop (req) != 0)
437 {
438 fprintf (stderr, "Setting %s mode failed\n", req_str);
439 write_enn (own_buf);
440 return;
441 }
442
443 non_stop = req;
444
445 if (remote_debug)
446 fprintf (stderr, "[%s mode enabled]\n", req_str);
447
448 write_ok (own_buf);
449 return;
450 }
451
89be2091
DJ
452 /* Otherwise we didn't know what packet it was. Say we didn't
453 understand it. */
454 own_buf[0] = 0;
455}
456
23181151 457static const char *
fb1e4ffc 458get_features_xml (const char *annex)
23181151 459{
9b4b61c8
UW
460 /* gdbserver_xmltarget defines what to return when looking
461 for the "target.xml" file. Its contents can either be
462 verbatim XML code (prefixed with a '@') or else the name
463 of the actual XML file to be used in place of "target.xml".
fb1e4ffc 464
9b4b61c8
UW
465 This variable is set up from the auto-generated
466 init_registers_... routine for the current target. */
fb1e4ffc 467
9b4b61c8 468 if (gdbserver_xmltarget
221c031f 469 && strcmp (annex, "target.xml") == 0)
23181151 470 {
9b4b61c8
UW
471 if (*gdbserver_xmltarget == '@')
472 return gdbserver_xmltarget + 1;
23181151 473 else
9b4b61c8 474 annex = gdbserver_xmltarget;
23181151
DJ
475 }
476
9b4b61c8
UW
477#ifdef USE_XML
478 {
479 extern const char *const xml_builtin[][2];
480 int i;
481
482 /* Look for the annex. */
483 for (i = 0; xml_builtin[i][0] != NULL; i++)
484 if (strcmp (annex, xml_builtin[i][0]) == 0)
485 break;
486
487 if (xml_builtin[i][0] != NULL)
488 return xml_builtin[i][1];
489 }
490#endif
491
492 return NULL;
23181151
DJ
493}
494
c74d0ad8
DJ
495void
496monitor_show_help (void)
497{
498 monitor_output ("The following monitor commands are supported:\n");
499 monitor_output (" set debug <0|1>\n");
1b3f6016 500 monitor_output (" Enable general debugging messages\n");
aa5ca48f
DE
501 monitor_output (" set debug-hw-points <0|1>\n");
502 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
c74d0ad8
DJ
503 monitor_output (" set remote-debug <0|1>\n");
504 monitor_output (" Enable remote protocol debugging messages\n");
ecd7ecbc
DJ
505 monitor_output (" exit\n");
506 monitor_output (" Quit GDBserver\n");
c74d0ad8
DJ
507}
508
08388c79
DE
509/* Subroutine of handle_search_memory to simplify it. */
510
511static int
512handle_search_memory_1 (CORE_ADDR start_addr, CORE_ADDR search_space_len,
513 gdb_byte *pattern, unsigned pattern_len,
514 gdb_byte *search_buf,
515 unsigned chunk_size, unsigned search_buf_size,
516 CORE_ADDR *found_addrp)
517{
518 /* Prime the search buffer. */
519
520 if (read_inferior_memory (start_addr, search_buf, search_buf_size) != 0)
521 {
5e1471f5 522 warning ("Unable to access target memory at 0x%lx, halting search.",
08388c79
DE
523 (long) start_addr);
524 return -1;
525 }
526
527 /* Perform the search.
528
529 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
530 When we've scanned N bytes we copy the trailing bytes to the start and
531 read in another N bytes. */
532
533 while (search_space_len >= pattern_len)
534 {
535 gdb_byte *found_ptr;
536 unsigned nr_search_bytes = (search_space_len < search_buf_size
537 ? search_space_len
538 : search_buf_size);
539
540 found_ptr = memmem (search_buf, nr_search_bytes, pattern, pattern_len);
541
542 if (found_ptr != NULL)
543 {
544 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
545 *found_addrp = found_addr;
546 return 1;
547 }
548
549 /* Not found in this chunk, skip to next chunk. */
550
551 /* Don't let search_space_len wrap here, it's unsigned. */
552 if (search_space_len >= chunk_size)
553 search_space_len -= chunk_size;
554 else
555 search_space_len = 0;
556
557 if (search_space_len >= pattern_len)
558 {
559 unsigned keep_len = search_buf_size - chunk_size;
8a35fb51 560 CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
08388c79
DE
561 int nr_to_read;
562
563 /* Copy the trailing part of the previous iteration to the front
564 of the buffer for the next iteration. */
565 memcpy (search_buf, search_buf + chunk_size, keep_len);
566
567 nr_to_read = (search_space_len - keep_len < chunk_size
568 ? search_space_len - keep_len
569 : chunk_size);
570
571 if (read_inferior_memory (read_addr, search_buf + keep_len,
572 nr_to_read) != 0)
573 {
5e1471f5 574 warning ("Unable to access target memory at 0x%lx, halting search.",
08388c79
DE
575 (long) read_addr);
576 return -1;
577 }
578
579 start_addr += chunk_size;
580 }
581 }
582
583 /* Not found. */
584
585 return 0;
586}
587
588/* Handle qSearch:memory packets. */
589
590static void
591handle_search_memory (char *own_buf, int packet_len)
592{
593 CORE_ADDR start_addr;
594 CORE_ADDR search_space_len;
595 gdb_byte *pattern;
596 unsigned int pattern_len;
597 /* NOTE: also defined in find.c testcase. */
598#define SEARCH_CHUNK_SIZE 16000
599 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
600 /* Buffer to hold memory contents for searching. */
601 gdb_byte *search_buf;
602 unsigned search_buf_size;
603 int found;
604 CORE_ADDR found_addr;
605 int cmd_name_len = sizeof ("qSearch:memory:") - 1;
606
aef93bd7 607 pattern = malloc (packet_len);
08388c79
DE
608 if (pattern == NULL)
609 {
5e1471f5 610 error ("Unable to allocate memory to perform the search");
08388c79
DE
611 strcpy (own_buf, "E00");
612 return;
613 }
614 if (decode_search_memory_packet (own_buf + cmd_name_len,
615 packet_len - cmd_name_len,
616 &start_addr, &search_space_len,
617 pattern, &pattern_len) < 0)
618 {
619 free (pattern);
5e1471f5 620 error ("Error in parsing qSearch:memory packet");
08388c79
DE
621 strcpy (own_buf, "E00");
622 return;
623 }
624
625 search_buf_size = chunk_size + pattern_len - 1;
626
627 /* No point in trying to allocate a buffer larger than the search space. */
628 if (search_space_len < search_buf_size)
629 search_buf_size = search_space_len;
630
aef93bd7 631 search_buf = malloc (search_buf_size);
08388c79
DE
632 if (search_buf == NULL)
633 {
634 free (pattern);
5e1471f5 635 error ("Unable to allocate memory to perform the search");
08388c79
DE
636 strcpy (own_buf, "E00");
637 return;
638 }
639
640 found = handle_search_memory_1 (start_addr, search_space_len,
641 pattern, pattern_len,
642 search_buf, chunk_size, search_buf_size,
643 &found_addr);
644
645 if (found > 0)
646 sprintf (own_buf, "1,%lx", (long) found_addr);
647 else if (found == 0)
648 strcpy (own_buf, "0");
649 else
650 strcpy (own_buf, "E00");
651
652 free (search_buf);
653 free (pattern);
654}
655
2d717e4f
DJ
656#define require_running(BUF) \
657 if (!target_running ()) \
658 { \
659 write_enn (BUF); \
660 return; \
661 }
662
cdbfd419
PP
663/* Handle monitor commands not handled by target-specific handlers. */
664
665static void
666handle_monitor_command (char *mon)
667{
668 if (strcmp (mon, "set debug 1") == 0)
669 {
670 debug_threads = 1;
671 monitor_output ("Debug output enabled.\n");
672 }
673 else if (strcmp (mon, "set debug 0") == 0)
674 {
675 debug_threads = 0;
676 monitor_output ("Debug output disabled.\n");
677 }
678 else if (strcmp (mon, "set debug-hw-points 1") == 0)
679 {
680 debug_hw_points = 1;
681 monitor_output ("H/W point debugging output enabled.\n");
682 }
683 else if (strcmp (mon, "set debug-hw-points 0") == 0)
684 {
685 debug_hw_points = 0;
686 monitor_output ("H/W point debugging output disabled.\n");
687 }
688 else if (strcmp (mon, "set remote-debug 1") == 0)
689 {
690 remote_debug = 1;
691 monitor_output ("Protocol debug output enabled.\n");
692 }
693 else if (strcmp (mon, "set remote-debug 0") == 0)
694 {
695 remote_debug = 0;
696 monitor_output ("Protocol debug output disabled.\n");
697 }
698 else if (strcmp (mon, "help") == 0)
699 monitor_show_help ();
700 else if (strcmp (mon, "exit") == 0)
701 exit_requested = 1;
702 else
703 {
704 monitor_output ("Unknown monitor command.\n\n");
705 monitor_show_help ();
706 write_enn (own_buf);
707 }
708}
709
dc146f7c
VP
710static void
711handle_threads_qxfer_proper (struct buffer *buffer)
712{
713 struct inferior_list_entry *thread;
714
715 buffer_grow_str (buffer, "<threads>\n");
716
717 for (thread = all_threads.head; thread; thread = thread->next)
718 {
719 ptid_t ptid = thread_to_gdb_id ((struct thread_info *)thread);
720 char ptid_s[100];
721 int core = -1;
722 char core_s[21];
723
724 write_ptid (ptid_s, ptid);
725
726 if (the_target->core_of_thread)
727 core = (*the_target->core_of_thread) (ptid);
728
729 if (core != -1)
730 {
731 sprintf (core_s, "%d", core);
732 buffer_xml_printf (buffer, "<thread id=\"%s\" core=\"%s\"/>\n",
733 ptid_s, core_s);
734 }
735 else
736 {
737 buffer_xml_printf (buffer, "<thread id=\"%s\"/>\n",
738 ptid_s);
739 }
740 }
741
742 buffer_grow_str0 (buffer, "</threads>\n");
743}
744
745static int
746handle_threads_qxfer (const char *annex,
747 unsigned char *readbuf,
748 CORE_ADDR offset, int length)
749{
750 static char *result = 0;
751 static unsigned int result_length = 0;
752
753 if (annex && strcmp (annex, "") != 0)
754 return 0;
755
756 if (offset == 0)
757 {
758 struct buffer buffer;
759 /* When asked for data at offset 0, generate everything and store into
760 'result'. Successive reads will be served off 'result'. */
761 if (result)
762 free (result);
763
764 buffer_init (&buffer);
765
766 handle_threads_qxfer_proper (&buffer);
767
768 result = buffer_finish (&buffer);
769 result_length = strlen (result);
770 buffer_free (&buffer);
771 }
772
773 if (offset >= result_length)
774 {
775 /* We're out of data. */
776 free (result);
777 result = NULL;
778 result_length = 0;
779 return 0;
780 }
781
782 if (length > result_length - offset)
783 length = result_length - offset;
784
785 memcpy (readbuf, result + offset, length);
786
787 return length;
788
789}
790
30ba68cb
MS
791/* Table used by the crc32 function to calcuate the checksum. */
792
793static unsigned int crc32_table[256] =
794{0, 0};
795
796/* Compute 32 bit CRC from inferior memory.
797
798 On success, return 32 bit CRC.
799 On failure, return (unsigned long long) -1. */
800
801static unsigned long long
802crc32 (CORE_ADDR base, int len, unsigned int crc)
803{
804 if (!crc32_table[1])
805 {
806 /* Initialize the CRC table and the decoding table. */
807 int i, j;
808 unsigned int c;
809
810 for (i = 0; i < 256; i++)
811 {
812 for (c = i << 24, j = 8; j > 0; --j)
813 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
814 crc32_table[i] = c;
815 }
816 }
817
818 while (len--)
819 {
820 unsigned char byte = 0;
821
822 /* Return failure if memory read fails. */
823 if (read_inferior_memory (base, &byte, 1) != 0)
824 return (unsigned long long) -1;
825
826 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ byte) & 255];
827 base++;
828 }
829 return (unsigned long long) crc;
830}
831
ce3a066d
DJ
832/* Handle all of the extended 'q' packets. */
833void
0e7f50da 834handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
ce3a066d 835{
0d62e5e8
DJ
836 static struct inferior_list_entry *thread_ptr;
837
bb63802a 838 /* Reply the current thread id. */
db42f210 839 if (strcmp ("qC", own_buf) == 0 && !disable_packet_qC)
bb63802a 840 {
95954743 841 ptid_t gdb_id;
2d717e4f 842 require_running (own_buf);
bd99dc85 843
95954743
PA
844 if (!ptid_equal (general_thread, null_ptid)
845 && !ptid_equal (general_thread, minus_one_ptid))
bd99dc85
PA
846 gdb_id = general_thread;
847 else
848 {
849 thread_ptr = all_threads.head;
850 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
851 }
852
95954743
PA
853 sprintf (own_buf, "QC");
854 own_buf += 2;
855 own_buf = write_ptid (own_buf, gdb_id);
bb63802a
UW
856 return;
857 }
858
ce3a066d
DJ
859 if (strcmp ("qSymbol::", own_buf) == 0)
860 {
2d717e4f 861 if (target_running () && the_target->look_up_symbols != NULL)
2f2893d9
DJ
862 (*the_target->look_up_symbols) ();
863
ce3a066d
DJ
864 strcpy (own_buf, "OK");
865 return;
866 }
867
db42f210 868 if (!disable_packet_qfThreadInfo)
0d62e5e8 869 {
db42f210 870 if (strcmp ("qfThreadInfo", own_buf) == 0)
0d62e5e8 871 {
95954743
PA
872 ptid_t gdb_id;
873
db42f210
PA
874 require_running (own_buf);
875 thread_ptr = all_threads.head;
95954743
PA
876
877 *own_buf++ = 'm';
878 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
879 write_ptid (own_buf, gdb_id);
0d62e5e8
DJ
880 thread_ptr = thread_ptr->next;
881 return;
882 }
db42f210
PA
883
884 if (strcmp ("qsThreadInfo", own_buf) == 0)
0d62e5e8 885 {
95954743
PA
886 ptid_t gdb_id;
887
db42f210
PA
888 require_running (own_buf);
889 if (thread_ptr != NULL)
890 {
95954743
PA
891 *own_buf++ = 'm';
892 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
893 write_ptid (own_buf, gdb_id);
db42f210
PA
894 thread_ptr = thread_ptr->next;
895 return;
896 }
897 else
898 {
899 sprintf (own_buf, "l");
900 return;
901 }
0d62e5e8
DJ
902 }
903 }
aa691b87 904
52fb6437
NS
905 if (the_target->read_offsets != NULL
906 && strcmp ("qOffsets", own_buf) == 0)
907 {
908 CORE_ADDR text, data;
2d717e4f
DJ
909
910 require_running (own_buf);
52fb6437
NS
911 if (the_target->read_offsets (&text, &data))
912 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
913 (long)text, (long)data, (long)data);
914 else
915 write_enn (own_buf);
1b3f6016 916
52fb6437
NS
917 return;
918 }
919
0e7f50da
UW
920 if (the_target->qxfer_spu != NULL
921 && strncmp ("qXfer:spu:read:", own_buf, 15) == 0)
922 {
923 char *annex;
924 int n;
925 unsigned int len;
926 CORE_ADDR ofs;
927 unsigned char *spu_buf;
928
2d717e4f 929 require_running (own_buf);
0e7f50da
UW
930 strcpy (own_buf, "E00");
931 if (decode_xfer_read (own_buf + 15, &annex, &ofs, &len) < 0)
78e5cee6 932 return;
0e7f50da
UW
933 if (len > PBUFSIZ - 2)
934 len = PBUFSIZ - 2;
aef93bd7 935 spu_buf = malloc (len + 1);
0e7f50da 936 if (!spu_buf)
1b3f6016 937 return;
0e7f50da
UW
938
939 n = (*the_target->qxfer_spu) (annex, spu_buf, NULL, ofs, len + 1);
1b3f6016 940 if (n < 0)
0e7f50da
UW
941 write_enn (own_buf);
942 else if (n > len)
78e5cee6 943 *new_packet_len_p = write_qxfer_response (own_buf, spu_buf, len, 1);
1b3f6016 944 else
78e5cee6 945 *new_packet_len_p = write_qxfer_response (own_buf, spu_buf, n, 0);
0e7f50da
UW
946
947 free (spu_buf);
948 return;
949 }
950
951 if (the_target->qxfer_spu != NULL
952 && strncmp ("qXfer:spu:write:", own_buf, 16) == 0)
953 {
954 char *annex;
955 int n;
956 unsigned int len;
957 CORE_ADDR ofs;
958 unsigned char *spu_buf;
959
2d717e4f 960 require_running (own_buf);
0e7f50da 961 strcpy (own_buf, "E00");
aef93bd7 962 spu_buf = malloc (packet_len - 15);
0e7f50da 963 if (!spu_buf)
1b3f6016 964 return;
0e7f50da
UW
965 if (decode_xfer_write (own_buf + 16, packet_len - 16, &annex,
966 &ofs, &len, spu_buf) < 0)
967 {
968 free (spu_buf);
969 return;
970 }
971
1b3f6016 972 n = (*the_target->qxfer_spu)
0e7f50da
UW
973 (annex, NULL, (unsigned const char *)spu_buf, ofs, len);
974 if (n < 0)
975 write_enn (own_buf);
976 else
977 sprintf (own_buf, "%x", n);
978
979 free (spu_buf);
980 return;
981 }
982
aa691b87 983 if (the_target->read_auxv != NULL
0876f84a 984 && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0)
aa691b87 985 {
0876f84a
DJ
986 unsigned char *data;
987 int n;
aa691b87
RM
988 CORE_ADDR ofs;
989 unsigned int len;
0876f84a
DJ
990 char *annex;
991
2d717e4f
DJ
992 require_running (own_buf);
993
0876f84a
DJ
994 /* Reject any annex; grab the offset and length. */
995 if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0
996 || annex[0] != '\0')
997 {
998 strcpy (own_buf, "E00");
999 return;
1000 }
1001
1002 /* Read one extra byte, as an indicator of whether there is
1003 more. */
1004 if (len > PBUFSIZ - 2)
1005 len = PBUFSIZ - 2;
aef93bd7
DE
1006 data = malloc (len + 1);
1007 if (data == NULL)
1008 {
1009 write_enn (own_buf);
1010 return;
1011 }
0876f84a 1012 n = (*the_target->read_auxv) (ofs, data, len + 1);
000ef4f0
DJ
1013 if (n < 0)
1014 write_enn (own_buf);
1015 else if (n > len)
0876f84a 1016 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
aa691b87 1017 else
0876f84a
DJ
1018 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
1019
1020 free (data);
1021
aa691b87
RM
1022 return;
1023 }
1024
23181151
DJ
1025 if (strncmp ("qXfer:features:read:", own_buf, 20) == 0)
1026 {
1027 CORE_ADDR ofs;
1028 unsigned int len, total_len;
1029 const char *document;
1030 char *annex;
1031
2d717e4f
DJ
1032 require_running (own_buf);
1033
fb1e4ffc
DJ
1034 /* Grab the annex, offset, and length. */
1035 if (decode_xfer_read (own_buf + 20, &annex, &ofs, &len) < 0)
1036 {
1037 strcpy (own_buf, "E00");
1038 return;
1039 }
1040
1041 /* Now grab the correct annex. */
1042 document = get_features_xml (annex);
1043 if (document == NULL)
23181151
DJ
1044 {
1045 strcpy (own_buf, "E00");
1046 return;
1047 }
1048
1049 total_len = strlen (document);
1050 if (len > PBUFSIZ - 2)
1051 len = PBUFSIZ - 2;
1052
1053 if (ofs > total_len)
1054 write_enn (own_buf);
1055 else if (len < total_len - ofs)
1056 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
1057 len, 1);
1058 else
1059 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
1060 total_len - ofs, 0);
1061
1062 return;
1063 }
1064
255e7678
DJ
1065 if (strncmp ("qXfer:libraries:read:", own_buf, 21) == 0)
1066 {
1067 CORE_ADDR ofs;
1068 unsigned int len, total_len;
1069 char *document, *p;
1070 struct inferior_list_entry *dll_ptr;
1071 char *annex;
1072
2d717e4f
DJ
1073 require_running (own_buf);
1074
255e7678
DJ
1075 /* Reject any annex; grab the offset and length. */
1076 if (decode_xfer_read (own_buf + 21, &annex, &ofs, &len) < 0
1077 || annex[0] != '\0')
1078 {
1079 strcpy (own_buf, "E00");
1080 return;
1081 }
1082
1083 /* Over-estimate the necessary memory. Assume that every character
1084 in the library name must be escaped. */
1085 total_len = 64;
1086 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
1087 total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name);
1088
aef93bd7
DE
1089 document = malloc (total_len);
1090 if (document == NULL)
1091 {
1092 write_enn (own_buf);
1093 return;
1094 }
255e7678
DJ
1095 strcpy (document, "<library-list>\n");
1096 p = document + strlen (document);
1097
1098 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
1099 {
1100 struct dll_info *dll = (struct dll_info *) dll_ptr;
1101 char *name;
1102
1103 strcpy (p, " <library name=\"");
1104 p = p + strlen (p);
1105 name = xml_escape_text (dll->name);
1106 strcpy (p, name);
1107 free (name);
1108 p = p + strlen (p);
1109 strcpy (p, "\"><segment address=\"");
1110 p = p + strlen (p);
1111 sprintf (p, "0x%lx", (long) dll->base_addr);
1112 p = p + strlen (p);
1113 strcpy (p, "\"/></library>\n");
1114 p = p + strlen (p);
1115 }
1116
1117 strcpy (p, "</library-list>\n");
1118
1119 total_len = strlen (document);
1120 if (len > PBUFSIZ - 2)
1121 len = PBUFSIZ - 2;
1122
1123 if (ofs > total_len)
1124 write_enn (own_buf);
1125 else if (len < total_len - ofs)
1126 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
1127 len, 1);
1128 else
1129 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
1130 total_len - ofs, 0);
1131
1132 free (document);
1133 return;
1134 }
1135
07e059b5
VP
1136 if (the_target->qxfer_osdata != NULL
1137 && strncmp ("qXfer:osdata:read:", own_buf, 18) == 0)
1138 {
1139 char *annex;
1140 int n;
1141 unsigned int len;
1142 CORE_ADDR ofs;
1143 unsigned char *workbuf;
1144
1145 strcpy (own_buf, "E00");
1146 if (decode_xfer_read (own_buf + 18, &annex, &ofs, &len) < 0)
78e5cee6 1147 return;
07e059b5 1148 if (len > PBUFSIZ - 2)
78e5cee6 1149 len = PBUFSIZ - 2;
aef93bd7 1150 workbuf = malloc (len + 1);
07e059b5 1151 if (!workbuf)
1b3f6016 1152 return;
07e059b5
VP
1153
1154 n = (*the_target->qxfer_osdata) (annex, workbuf, NULL, ofs, len + 1);
1155 if (n < 0)
78e5cee6 1156 write_enn (own_buf);
07e059b5 1157 else if (n > len)
78e5cee6 1158 *new_packet_len_p = write_qxfer_response (own_buf, workbuf, len, 1);
07e059b5 1159 else
78e5cee6 1160 *new_packet_len_p = write_qxfer_response (own_buf, workbuf, n, 0);
07e059b5
VP
1161
1162 free (workbuf);
1163 return;
1164 }
1165
4aa995e1
PA
1166 if (the_target->qxfer_siginfo != NULL
1167 && strncmp ("qXfer:siginfo:read:", own_buf, 19) == 0)
1168 {
1169 unsigned char *data;
1170 int n;
1171 CORE_ADDR ofs;
1172 unsigned int len;
1173 char *annex;
1174
1175 require_running (own_buf);
1176
1177 /* Reject any annex; grab the offset and length. */
1178 if (decode_xfer_read (own_buf + 19, &annex, &ofs, &len) < 0
1179 || annex[0] != '\0')
1180 {
1181 strcpy (own_buf, "E00");
1182 return;
1183 }
1184
1185 /* Read one extra byte, as an indicator of whether there is
1186 more. */
1187 if (len > PBUFSIZ - 2)
1188 len = PBUFSIZ - 2;
1189 data = malloc (len + 1);
1190 if (!data)
1b3f6016 1191 return;
4aa995e1
PA
1192 n = (*the_target->qxfer_siginfo) (annex, data, NULL, ofs, len + 1);
1193 if (n < 0)
1194 write_enn (own_buf);
1195 else if (n > len)
1196 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
1197 else
1198 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
1199
1200 free (data);
1201 return;
1202 }
1203
1204 if (the_target->qxfer_siginfo != NULL
1205 && strncmp ("qXfer:siginfo:write:", own_buf, 20) == 0)
1206 {
1207 char *annex;
1208 int n;
1209 unsigned int len;
1210 CORE_ADDR ofs;
1211 unsigned char *data;
1212
1213 require_running (own_buf);
1214
1215 strcpy (own_buf, "E00");
1216 data = malloc (packet_len - 19);
1217 if (!data)
1b3f6016 1218 return;
4aa995e1
PA
1219 if (decode_xfer_write (own_buf + 20, packet_len - 20, &annex,
1220 &ofs, &len, data) < 0)
1221 {
1222 free (data);
1223 return;
1224 }
1225
1226 n = (*the_target->qxfer_siginfo)
1227 (annex, NULL, (unsigned const char *)data, ofs, len);
1228 if (n < 0)
1229 write_enn (own_buf);
1230 else
1231 sprintf (own_buf, "%x", n);
1232
1233 free (data);
1234 return;
1235 }
1236
dc146f7c
VP
1237 if (strncmp ("qXfer:threads:read:", own_buf, 19) == 0)
1238 {
1239 unsigned char *data;
1240 int n;
1241 CORE_ADDR ofs;
1242 unsigned int len;
1243 char *annex;
1244
1245 require_running (own_buf);
1246
1247 /* Reject any annex; grab the offset and length. */
1248 if (decode_xfer_read (own_buf + 19, &annex, &ofs, &len) < 0
1249 || annex[0] != '\0')
1250 {
1251 strcpy (own_buf, "E00");
1252 return;
1253 }
1254
1255 /* Read one extra byte, as an indicator of whether there is
1256 more. */
1257 if (len > PBUFSIZ - 2)
1258 len = PBUFSIZ - 2;
1259 data = malloc (len + 1);
1260 if (!data)
1261 return;
1262 n = handle_threads_qxfer (annex, data, ofs, len + 1);
1263 if (n < 0)
1264 write_enn (own_buf);
1265 else if (n > len)
1266 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
1267 else
1268 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
1269
1270 free (data);
1271 return;
1272 }
1273
be2a5f71
DJ
1274 /* Protocol features query. */
1275 if (strncmp ("qSupported", own_buf, 10) == 0
1276 && (own_buf[10] == ':' || own_buf[10] == '\0'))
1277 {
95954743
PA
1278 char *p = &own_buf[10];
1279
1280 /* Process each feature being provided by GDB. The first
1281 feature will follow a ':', and latter features will follow
1282 ';'. */
1283 if (*p == ':')
1284 for (p = strtok (p + 1, ";");
1285 p != NULL;
1286 p = strtok (NULL, ";"))
1287 {
95954743 1288 if (strcmp (p, "multiprocess+") == 0)
cf8fd78b
PA
1289 {
1290 /* GDB supports and wants multi-process support if
1291 possible. */
1292 if (target_supports_multi_process ())
1293 multi_process = 1;
1294 }
95954743
PA
1295 }
1296
89be2091 1297 sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1);
0876f84a 1298
255e7678
DJ
1299 /* We do not have any hook to indicate whether the target backend
1300 supports qXfer:libraries:read, so always report it. */
1301 strcat (own_buf, ";qXfer:libraries:read+");
1302
0876f84a 1303 if (the_target->read_auxv != NULL)
9f2e1e63 1304 strcat (own_buf, ";qXfer:auxv:read+");
2d717e4f 1305
0e7f50da
UW
1306 if (the_target->qxfer_spu != NULL)
1307 strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+");
0876f84a 1308
4aa995e1
PA
1309 if (the_target->qxfer_siginfo != NULL)
1310 strcat (own_buf, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1311
221c031f
UW
1312 /* We always report qXfer:features:read, as targets may
1313 install XML files on a subsequent call to arch_setup.
1314 If we reported to GDB on startup that we don't support
1315 qXfer:feature:read at all, we will never be re-queried. */
1316 strcat (own_buf, ";qXfer:features:read+");
23181151 1317
a6f3e723
SL
1318 if (transport_is_reliable)
1319 strcat (own_buf, ";QStartNoAckMode+");
07e059b5
VP
1320
1321 if (the_target->qxfer_osdata != NULL)
1b3f6016 1322 strcat (own_buf, ";qXfer:osdata:read+");
07e059b5 1323
cf8fd78b
PA
1324 if (target_supports_multi_process ())
1325 strcat (own_buf, ";multiprocess+");
95954743 1326
bd99dc85
PA
1327 if (target_supports_non_stop ())
1328 strcat (own_buf, ";QNonStop+");
1329
dc146f7c
VP
1330 strcat (own_buf, ";qXfer:threads:read+");
1331
be2a5f71
DJ
1332 return;
1333 }
1334
dae5f5cf
DJ
1335 /* Thread-local storage support. */
1336 if (the_target->get_tls_address != NULL
1337 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
1338 {
1339 char *p = own_buf + 12;
5b1c542e 1340 CORE_ADDR parts[2], address = 0;
dae5f5cf 1341 int i, err;
95954743 1342 ptid_t ptid = null_ptid;
dae5f5cf 1343
2d717e4f
DJ
1344 require_running (own_buf);
1345
dae5f5cf
DJ
1346 for (i = 0; i < 3; i++)
1347 {
1348 char *p2;
1349 int len;
1350
1351 if (p == NULL)
1352 break;
1353
1354 p2 = strchr (p, ',');
1355 if (p2)
1356 {
1357 len = p2 - p;
1358 p2++;
1359 }
1360 else
1361 {
1362 len = strlen (p);
1363 p2 = NULL;
1364 }
1365
5b1c542e 1366 if (i == 0)
95954743 1367 ptid = read_ptid (p, NULL);
5b1c542e
PA
1368 else
1369 decode_address (&parts[i - 1], p, len);
dae5f5cf
DJ
1370 p = p2;
1371 }
1372
1373 if (p != NULL || i < 3)
1374 err = 1;
1375 else
1376 {
e09875d4 1377 struct thread_info *thread = find_thread_ptid (ptid);
dae5f5cf
DJ
1378
1379 if (thread == NULL)
1380 err = 2;
1381 else
5b1c542e 1382 err = the_target->get_tls_address (thread, parts[0], parts[1],
dae5f5cf
DJ
1383 &address);
1384 }
1385
1386 if (err == 0)
1387 {
1388 sprintf (own_buf, "%llx", address);
1389 return;
1390 }
1391 else if (err > 0)
1392 {
1393 write_enn (own_buf);
1394 return;
1395 }
1396
1397 /* Otherwise, pretend we do not understand this packet. */
1398 }
1399
c74d0ad8
DJ
1400 /* Handle "monitor" commands. */
1401 if (strncmp ("qRcmd,", own_buf, 6) == 0)
1402 {
aef93bd7 1403 char *mon = malloc (PBUFSIZ);
c74d0ad8
DJ
1404 int len = strlen (own_buf + 6);
1405
aef93bd7
DE
1406 if (mon == NULL)
1407 {
1408 write_enn (own_buf);
1409 return;
1410 }
1411
d41b6bb4 1412 if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
c74d0ad8
DJ
1413 {
1414 write_enn (own_buf);
1415 free (mon);
1416 return;
1417 }
1418 mon[len / 2] = '\0';
1419
1420 write_ok (own_buf);
1421
cdbfd419
PP
1422 if (the_target->handle_monitor_command == NULL
1423 || (*the_target->handle_monitor_command) (mon) == 0)
1424 /* Default processing. */
1425 handle_monitor_command (mon);
c74d0ad8
DJ
1426
1427 free (mon);
1428 return;
1429 }
1430
08388c79
DE
1431 if (strncmp ("qSearch:memory:", own_buf, sizeof ("qSearch:memory:") - 1) == 0)
1432 {
1433 require_running (own_buf);
1434 handle_search_memory (own_buf, packet_len);
1435 return;
1436 }
1437
95954743
PA
1438 if (strcmp (own_buf, "qAttached") == 0
1439 || strncmp (own_buf, "qAttached:", sizeof ("qAttached:") - 1) == 0)
0b16c5cf 1440 {
95954743
PA
1441 struct process_info *process;
1442
1443 if (own_buf[sizeof ("qAttached") - 1])
1444 {
1445 int pid = strtoul (own_buf + sizeof ("qAttached:") - 1, NULL, 16);
1446 process = (struct process_info *)
1447 find_inferior_id (&all_processes, pid_to_ptid (pid));
1448 }
1449 else
1450 {
1451 require_running (own_buf);
1452 process = current_process ();
1453 }
1454
1455 if (process == NULL)
1456 {
1457 write_enn (own_buf);
1458 return;
1459 }
1460
1461 strcpy (own_buf, process->attached ? "1" : "0");
0b16c5cf
PA
1462 return;
1463 }
1464
30ba68cb
MS
1465 if (strncmp ("qCRC:", own_buf, 5) == 0)
1466 {
1467 /* CRC check (compare-section). */
1468 char *comma;
1469 CORE_ADDR base;
1470 int len;
1471 unsigned long long crc;
1472
1473 require_running (own_buf);
2280c721 1474 base = strtoul (own_buf + 5, &comma, 16);
30ba68cb
MS
1475 if (*comma++ != ',')
1476 {
1477 write_enn (own_buf);
1478 return;
1479 }
1480 len = strtoul (comma, NULL, 16);
1481 crc = crc32 (base, len, 0xffffffff);
1482 /* Check for memory failure. */
1483 if (crc == (unsigned long long) -1)
1484 {
1485 write_enn (own_buf);
1486 return;
1487 }
1488 sprintf (own_buf, "C%lx", (unsigned long) crc);
1489 return;
1490 }
1491
ce3a066d
DJ
1492 /* Otherwise we didn't know what packet it was. Say we didn't
1493 understand it. */
1494 own_buf[0] = 0;
1495}
1496
64386c31
DJ
1497/* Parse vCont packets. */
1498void
5b1c542e 1499handle_v_cont (char *own_buf)
64386c31
DJ
1500{
1501 char *p, *q;
1502 int n = 0, i = 0;
2bd7c093 1503 struct thread_resume *resume_info;
95954743 1504 struct thread_resume default_action = {{0}};
64386c31
DJ
1505
1506 /* Count the number of semicolons in the packet. There should be one
1507 for every action. */
1508 p = &own_buf[5];
1509 while (p)
1510 {
1511 n++;
1512 p++;
1513 p = strchr (p, ';');
1514 }
2bd7c093
PA
1515
1516 resume_info = malloc (n * sizeof (resume_info[0]));
aef93bd7
DE
1517 if (resume_info == NULL)
1518 goto err;
64386c31 1519
64386c31 1520 p = &own_buf[5];
64386c31
DJ
1521 while (*p)
1522 {
1523 p++;
1524
64386c31 1525 if (p[0] == 's' || p[0] == 'S')
bd99dc85 1526 resume_info[i].kind = resume_step;
64386c31 1527 else if (p[0] == 'c' || p[0] == 'C')
bd99dc85
PA
1528 resume_info[i].kind = resume_continue;
1529 else if (p[0] == 't')
1530 resume_info[i].kind = resume_stop;
64386c31
DJ
1531 else
1532 goto err;
1533
1534 if (p[0] == 'S' || p[0] == 'C')
1535 {
1536 int sig;
1537 sig = strtol (p + 1, &q, 16);
1538 if (p == q)
1539 goto err;
1540 p = q;
1541
1542 if (!target_signal_to_host_p (sig))
1543 goto err;
1544 resume_info[i].sig = target_signal_to_host (sig);
1545 }
1546 else
1547 {
1548 resume_info[i].sig = 0;
1549 p = p + 1;
1550 }
1551
1552 if (p[0] == 0)
1553 {
95954743 1554 resume_info[i].thread = minus_one_ptid;
64386c31
DJ
1555 default_action = resume_info[i];
1556
1557 /* Note: we don't increment i here, we'll overwrite this entry
1558 the next time through. */
1559 }
1560 else if (p[0] == ':')
1561 {
95954743 1562 ptid_t ptid = read_ptid (p + 1, &q);
a06660f7 1563
64386c31
DJ
1564 if (p == q)
1565 goto err;
1566 p = q;
1567 if (p[0] != ';' && p[0] != 0)
1568 goto err;
1569
95954743 1570 resume_info[i].thread = ptid;
a06660f7 1571
64386c31
DJ
1572 i++;
1573 }
1574 }
1575
2bd7c093
PA
1576 if (i < n)
1577 resume_info[i] = default_action;
64386c31
DJ
1578
1579 /* Still used in occasional places in the backend. */
bd99dc85 1580 if (n == 1
95954743 1581 && !ptid_equal (resume_info[0].thread, minus_one_ptid)
bd99dc85 1582 && resume_info[0].kind != resume_stop)
64386c31
DJ
1583 cont_thread = resume_info[0].thread;
1584 else
95954743 1585 cont_thread = minus_one_ptid;
dc3f8883 1586 set_desired_inferior (0);
64386c31 1587
bd99dc85
PA
1588 if (!non_stop)
1589 enable_async_io ();
1590
2bd7c093 1591 (*the_target->resume) (resume_info, n);
64386c31
DJ
1592
1593 free (resume_info);
1594
bd99dc85
PA
1595 if (non_stop)
1596 write_ok (own_buf);
1597 else
1598 {
95954743 1599 last_ptid = mywait (minus_one_ptid, &last_status, 0, 1);
bd99dc85
PA
1600 prepare_resume_reply (own_buf, last_ptid, &last_status);
1601 disable_async_io ();
1602 }
64386c31
DJ
1603 return;
1604
1605err:
255e7678 1606 write_enn (own_buf);
64386c31
DJ
1607 free (resume_info);
1608 return;
1609}
1610
2d717e4f
DJ
1611/* Attach to a new program. Return 1 if successful, 0 if failure. */
1612int
5b1c542e 1613handle_v_attach (char *own_buf)
2d717e4f
DJ
1614{
1615 int pid;
1616
1617 pid = strtol (own_buf + 8, NULL, 16);
5b1c542e 1618 if (pid != 0 && attach_inferior (pid) == 0)
2d717e4f 1619 {
aeba519e
PA
1620 /* Don't report shared library events after attaching, even if
1621 some libraries are preloaded. GDB will always poll the
1622 library list. Avoids the "stopped by shared library event"
1623 notice on the GDB side. */
1624 dlls_changed = 0;
bd99dc85
PA
1625
1626 if (non_stop)
1627 {
1628 /* In non-stop, we don't send a resume reply. Stop events
1629 will follow up using the normal notification
1630 mechanism. */
1631 write_ok (own_buf);
1632 }
1633 else
1634 prepare_resume_reply (own_buf, last_ptid, &last_status);
1635
2d717e4f
DJ
1636 return 1;
1637 }
1638 else
1639 {
1640 write_enn (own_buf);
1641 return 0;
1642 }
1643}
1644
1645/* Run a new program. Return 1 if successful, 0 if failure. */
1646static int
5b1c542e 1647handle_v_run (char *own_buf)
2d717e4f 1648{
aef93bd7 1649 char *p, *next_p, **new_argv;
2d717e4f
DJ
1650 int i, new_argc;
1651
1652 new_argc = 0;
1653 for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';'))
1654 {
1655 p++;
1656 new_argc++;
1657 }
1658
aef93bd7
DE
1659 new_argv = calloc (new_argc + 2, sizeof (char *));
1660 if (new_argv == NULL)
1661 {
1662 write_enn (own_buf);
1663 return 0;
1664 }
1665
2d717e4f
DJ
1666 i = 0;
1667 for (p = own_buf + strlen ("vRun;"); *p; p = next_p)
1668 {
1669 next_p = strchr (p, ';');
1670 if (next_p == NULL)
1671 next_p = p + strlen (p);
1672
1673 if (i == 0 && p == next_p)
1674 new_argv[i] = NULL;
1675 else
1676 {
aef93bd7 1677 /* FIXME: Fail request if out of memory instead of dying. */
bca929d3 1678 new_argv[i] = xmalloc (1 + (next_p - p) / 2);
2d717e4f
DJ
1679 unhexify (new_argv[i], p, (next_p - p) / 2);
1680 new_argv[i][(next_p - p) / 2] = '\0';
1681 }
1682
1683 if (*next_p)
1684 next_p++;
1685 i++;
1686 }
1687 new_argv[i] = NULL;
1688
1689 if (new_argv[0] == NULL)
1690 {
f142445f
DJ
1691 /* GDB didn't specify a program to run. Use the program from the
1692 last run with the new argument list. */
9b710a42 1693
2d717e4f
DJ
1694 if (program_argv == NULL)
1695 {
aef93bd7 1696 /* FIXME: new_argv memory leak */
2d717e4f
DJ
1697 write_enn (own_buf);
1698 return 0;
1699 }
1700
aef93bd7
DE
1701 new_argv[0] = strdup (program_argv[0]);
1702 if (new_argv[0] == NULL)
1703 {
1704 /* FIXME: new_argv memory leak */
1705 write_enn (own_buf);
1706 return 0;
1b3f6016 1707 }
2d717e4f 1708 }
f142445f 1709
aef93bd7
DE
1710 /* Free the old argv and install the new one. */
1711 freeargv (program_argv);
f142445f 1712 program_argv = new_argv;
2d717e4f 1713
5b1c542e
PA
1714 start_inferior (program_argv);
1715 if (last_status.kind == TARGET_WAITKIND_STOPPED)
2d717e4f 1716 {
5b1c542e 1717 prepare_resume_reply (own_buf, last_ptid, &last_status);
bd99dc85
PA
1718
1719 /* In non-stop, sending a resume reply doesn't set the general
1720 thread, but GDB assumes a vRun sets it (this is so GDB can
1721 query which is the main thread of the new inferior. */
1722 if (non_stop)
1723 general_thread = last_ptid;
1724
2d717e4f
DJ
1725 return 1;
1726 }
1727 else
1728 {
1729 write_enn (own_buf);
1730 return 0;
1731 }
1732}
1733
95954743
PA
1734/* Kill process. Return 1 if successful, 0 if failure. */
1735int
1736handle_v_kill (char *own_buf)
1737{
1738 int pid;
1739 char *p = &own_buf[6];
0f54c268
PM
1740 if (multi_process)
1741 pid = strtol (p, NULL, 16);
1742 else
1743 pid = signal_pid;
95954743
PA
1744 if (pid != 0 && kill_inferior (pid) == 0)
1745 {
1746 last_status.kind = TARGET_WAITKIND_SIGNALLED;
1747 last_status.value.sig = TARGET_SIGNAL_KILL;
1748 last_ptid = pid_to_ptid (pid);
1749 discard_queued_stop_replies (pid);
1750 write_ok (own_buf);
1751 return 1;
1752 }
1753 else
1754 {
1755 write_enn (own_buf);
1756 return 0;
1757 }
1758}
1759
bd99dc85
PA
1760/* Handle a 'vStopped' packet. */
1761static void
1762handle_v_stopped (char *own_buf)
1763{
1764 /* If we're waiting for GDB to acknowledge a pending stop reply,
1765 consider that done. */
1766 if (notif_queue)
1767 {
1768 struct vstop_notif *head;
1769
1770 if (remote_debug)
95954743
PA
1771 fprintf (stderr, "vStopped: acking %s\n",
1772 target_pid_to_str (notif_queue->ptid));
bd99dc85
PA
1773
1774 head = notif_queue;
1775 notif_queue = notif_queue->next;
1776 free (head);
1777 }
1778
1779 /* Push another stop reply, or if there are no more left, an OK. */
1780 send_next_stop_reply (own_buf);
1781}
1782
64386c31
DJ
1783/* Handle all of the extended 'v' packets. */
1784void
5b1c542e 1785handle_v_requests (char *own_buf, int packet_len, int *new_packet_len)
64386c31 1786{
db42f210 1787 if (!disable_packet_vCont)
64386c31 1788 {
db42f210
PA
1789 if (strncmp (own_buf, "vCont;", 6) == 0)
1790 {
1791 require_running (own_buf);
5b1c542e 1792 handle_v_cont (own_buf);
db42f210
PA
1793 return;
1794 }
64386c31 1795
db42f210
PA
1796 if (strncmp (own_buf, "vCont?", 6) == 0)
1797 {
bd99dc85 1798 strcpy (own_buf, "vCont;c;C;s;S;t");
db42f210
PA
1799 return;
1800 }
64386c31
DJ
1801 }
1802
a6b151f1
DJ
1803 if (strncmp (own_buf, "vFile:", 6) == 0
1804 && handle_vFile (own_buf, packet_len, new_packet_len))
1805 return;
1806
2d717e4f
DJ
1807 if (strncmp (own_buf, "vAttach;", 8) == 0)
1808 {
95954743 1809 if (!multi_process && target_running ())
2d717e4f 1810 {
fd96d250
PA
1811 fprintf (stderr, "Already debugging a process\n");
1812 write_enn (own_buf);
1813 return;
2d717e4f 1814 }
5b1c542e 1815 handle_v_attach (own_buf);
2d717e4f
DJ
1816 return;
1817 }
1818
1819 if (strncmp (own_buf, "vRun;", 5) == 0)
1820 {
95954743 1821 if (!multi_process && target_running ())
2d717e4f 1822 {
fd96d250
PA
1823 fprintf (stderr, "Already debugging a process\n");
1824 write_enn (own_buf);
1825 return;
2d717e4f 1826 }
5b1c542e 1827 handle_v_run (own_buf);
2d717e4f
DJ
1828 return;
1829 }
1830
95954743
PA
1831 if (strncmp (own_buf, "vKill;", 6) == 0)
1832 {
1833 if (!target_running ())
1834 {
1835 fprintf (stderr, "No process to kill\n");
1836 write_enn (own_buf);
1837 return;
1838 }
1839 handle_v_kill (own_buf);
1840 return;
1841 }
1842
bd99dc85
PA
1843 if (strncmp (own_buf, "vStopped", 8) == 0)
1844 {
1845 handle_v_stopped (own_buf);
1846 return;
1847 }
1848
64386c31
DJ
1849 /* Otherwise we didn't know what packet it was. Say we didn't
1850 understand it. */
1851 own_buf[0] = 0;
1852 return;
1853}
1854
bd99dc85
PA
1855/* Resume inferior and wait for another event. In non-stop mode,
1856 don't really wait here, but return immediatelly to the event
1857 loop. */
64386c31 1858void
5b1c542e 1859myresume (char *own_buf, int step, int sig)
64386c31
DJ
1860{
1861 struct thread_resume resume_info[2];
1862 int n = 0;
2bd7c093 1863 int valid_cont_thread;
a20d5e98
DJ
1864
1865 set_desired_inferior (0);
64386c31 1866
95954743
PA
1867 valid_cont_thread = (!ptid_equal (cont_thread, null_ptid)
1868 && !ptid_equal (cont_thread, minus_one_ptid));
2bd7c093
PA
1869
1870 if (step || sig || valid_cont_thread)
64386c31
DJ
1871 {
1872 resume_info[0].thread
1873 = ((struct inferior_list_entry *) current_inferior)->id;
bd99dc85
PA
1874 if (step)
1875 resume_info[0].kind = resume_step;
1876 else
1877 resume_info[0].kind = resume_continue;
64386c31 1878 resume_info[0].sig = sig;
64386c31
DJ
1879 n++;
1880 }
2bd7c093
PA
1881
1882 if (!valid_cont_thread)
1883 {
95954743 1884 resume_info[n].thread = minus_one_ptid;
bd99dc85 1885 resume_info[n].kind = resume_continue;
2bd7c093
PA
1886 resume_info[n].sig = 0;
1887 n++;
1888 }
64386c31 1889
bd99dc85
PA
1890 if (!non_stop)
1891 enable_async_io ();
1892
2bd7c093 1893 (*the_target->resume) (resume_info, n);
bd99dc85
PA
1894
1895 if (non_stop)
1896 write_ok (own_buf);
1897 else
1898 {
95954743 1899 last_ptid = mywait (minus_one_ptid, &last_status, 0, 1);
bd99dc85
PA
1900 prepare_resume_reply (own_buf, last_ptid, &last_status);
1901 disable_async_io ();
1902 }
1903}
1904
1905/* Callback for for_each_inferior. Make a new stop reply for each
1906 stopped thread. */
1907
95954743
PA
1908static int
1909queue_stop_reply_callback (struct inferior_list_entry *entry, void *arg)
bd99dc85 1910{
95954743 1911 int pid = * (int *) arg;
bd99dc85 1912
95954743
PA
1913 if (pid == -1
1914 || ptid_get_pid (entry->id) == pid)
1915 {
1916 struct target_waitstatus status;
1917
1918 status.kind = TARGET_WAITKIND_STOPPED;
1919 status.value.sig = TARGET_SIGNAL_TRAP;
bd99dc85 1920
95954743
PA
1921 /* Pass the last stop reply back to GDB, but don't notify. */
1922 queue_stop_reply (entry->id, &status);
1923 }
1924
1925 return 0;
64386c31
DJ
1926}
1927
5b1c542e
PA
1928/* Status handler for the '?' packet. */
1929
1930static void
1931handle_status (char *own_buf)
1932{
bd99dc85
PA
1933 struct target_waitstatus status;
1934 status.kind = TARGET_WAITKIND_STOPPED;
1935 status.value.sig = TARGET_SIGNAL_TRAP;
1936
1937 /* In non-stop mode, we must send a stop reply for each stopped
1938 thread. In all-stop mode, just send one for the first stopped
1939 thread we find. */
1940
1941 if (non_stop)
1942 {
95954743
PA
1943 int pid = -1;
1944 discard_queued_stop_replies (pid);
1945 find_inferior (&all_threads, queue_stop_reply_callback, &pid);
bd99dc85
PA
1946
1947 /* The first is sent immediatly. OK is sent if there is no
1948 stopped thread, which is the same handling of the vStopped
1949 packet (by design). */
1950 send_next_stop_reply (own_buf);
1951 }
5b1c542e 1952 else
bd99dc85
PA
1953 {
1954 if (all_threads.head)
1955 prepare_resume_reply (own_buf,
1956 all_threads.head->id, &status);
1957 else
1958 strcpy (own_buf, "W00");
1959 }
5b1c542e
PA
1960}
1961
dd24457d
DJ
1962static void
1963gdbserver_version (void)
1964{
c16158bc 1965 printf ("GNU gdbserver %s%s\n"
6e7ffa39 1966 "Copyright (C) 2010 Free Software Foundation, Inc.\n"
dd24457d
DJ
1967 "gdbserver is free software, covered by the GNU General Public License.\n"
1968 "This gdbserver was configured as \"%s\"\n",
c16158bc 1969 PKGVERSION, version, host_name);
dd24457d
DJ
1970}
1971
0bc68c49 1972static void
c16158bc 1973gdbserver_usage (FILE *stream)
0bc68c49 1974{
c16158bc
JM
1975 fprintf (stream, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
1976 "\tgdbserver [OPTIONS] --attach COMM PID\n"
1977 "\tgdbserver [OPTIONS] --multi COMM\n"
1978 "\n"
1979 "COMM may either be a tty device (for serial debugging), or \n"
1980 "HOST:PORT to listen for a TCP connection.\n"
1981 "\n"
1982 "Options:\n"
62709adf
PA
1983 " --debug Enable general debugging output.\n"
1984 " --remote-debug Enable remote protocol debugging output.\n"
1985 " --version Display version information and exit.\n"
1986 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n");
c16158bc
JM
1987 if (REPORT_BUGS_TO[0] && stream == stdout)
1988 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
0bc68c49
DJ
1989}
1990
db42f210
PA
1991static void
1992gdbserver_show_disableable (FILE *stream)
1993{
1994 fprintf (stream, "Disableable packets:\n"
1995 " vCont \tAll vCont packets\n"
1996 " qC \tQuerying the current thread\n"
1997 " qfThreadInfo\tThread listing\n"
1998 " Tthread \tPassing the thread specifier in the T stop reply packet\n"
1999 " threads \tAll of the above\n");
2000}
2001
2002
2d717e4f
DJ
2003#undef require_running
2004#define require_running(BUF) \
2005 if (!target_running ()) \
2006 { \
2007 write_enn (BUF); \
2008 break; \
2009 }
2010
95954743
PA
2011static int
2012first_thread_of (struct inferior_list_entry *entry, void *args)
2013{
2014 int pid = * (int *) args;
2015
2016 if (ptid_get_pid (entry->id) == pid)
2017 return 1;
2018
2019 return 0;
2020}
2021
2022static void
2023kill_inferior_callback (struct inferior_list_entry *entry)
2024{
2025 struct process_info *process = (struct process_info *) entry;
2026 int pid = ptid_get_pid (process->head.id);
2027
2028 kill_inferior (pid);
2029 discard_queued_stop_replies (pid);
2030}
2031
9f767825
DE
2032/* Callback for for_each_inferior to detach or kill the inferior,
2033 depending on whether we attached to it or not.
2034 We inform the user whether we're detaching or killing the process
2035 as this is only called when gdbserver is about to exit. */
2036
95954743
PA
2037static void
2038detach_or_kill_inferior_callback (struct inferior_list_entry *entry)
2039{
2040 struct process_info *process = (struct process_info *) entry;
2041 int pid = ptid_get_pid (process->head.id);
2042
2043 if (process->attached)
2044 detach_inferior (pid);
2045 else
2046 kill_inferior (pid);
2047
2048 discard_queued_stop_replies (pid);
2049}
2050
9f767825
DE
2051/* for_each_inferior callback for detach_or_kill_for_exit to print
2052 the pids of started inferiors. */
2053
2054static void
2055print_started_pid (struct inferior_list_entry *entry)
2056{
2057 struct process_info *process = (struct process_info *) entry;
2058
2059 if (! process->attached)
2060 {
2061 int pid = ptid_get_pid (process->head.id);
2062 fprintf (stderr, " %d", pid);
2063 }
2064}
2065
2066/* for_each_inferior callback for detach_or_kill_for_exit to print
2067 the pids of attached inferiors. */
2068
2069static void
2070print_attached_pid (struct inferior_list_entry *entry)
2071{
2072 struct process_info *process = (struct process_info *) entry;
2073
2074 if (process->attached)
2075 {
2076 int pid = ptid_get_pid (process->head.id);
2077 fprintf (stderr, " %d", pid);
2078 }
2079}
2080
2081/* Call this when exiting gdbserver with possible inferiors that need
2082 to be killed or detached from. */
2083
2084static void
2085detach_or_kill_for_exit (void)
2086{
2087 /* First print a list of the inferiors we will be killing/detaching.
2088 This is to assist the user, for example, in case the inferior unexpectedly
2089 dies after we exit: did we screw up or did the inferior exit on its own?
2090 Having this info will save some head-scratching. */
2091
2092 if (have_started_inferiors_p ())
2093 {
2094 fprintf (stderr, "Killing process(es):");
2095 for_each_inferior (&all_processes, print_started_pid);
2096 fprintf (stderr, "\n");
2097 }
2098 if (have_attached_inferiors_p ())
2099 {
2100 fprintf (stderr, "Detaching process(es):");
2101 for_each_inferior (&all_processes, print_attached_pid);
2102 fprintf (stderr, "\n");
2103 }
2104
2105 /* Now we can kill or detach the inferiors. */
2106
2107 for_each_inferior (&all_processes, detach_or_kill_inferior_callback);
2108}
2109
95954743
PA
2110static void
2111join_inferiors_callback (struct inferior_list_entry *entry)
2112{
2113 struct process_info *process = (struct process_info *) entry;
2114
2115 /* If we are attached, then we can exit. Otherwise, we need to hang
2116 around doing nothing, until the child is gone. */
2117 if (!process->attached)
2118 join_inferior (ptid_get_pid (process->head.id));
2119}
2120
c906108c 2121int
da85418c 2122main (int argc, char *argv[])
c906108c 2123{
0729219d
DJ
2124 int bad_attach;
2125 int pid;
2d717e4f
DJ
2126 char *arg_end, *port;
2127 char **next_arg = &argv[1];
2128 int multi_mode = 0;
2129 int attach = 0;
2130 int was_running;
c906108c 2131
2d717e4f 2132 while (*next_arg != NULL && **next_arg == '-')
dd24457d 2133 {
2d717e4f
DJ
2134 if (strcmp (*next_arg, "--version") == 0)
2135 {
2136 gdbserver_version ();
2137 exit (0);
2138 }
2139 else if (strcmp (*next_arg, "--help") == 0)
2140 {
c16158bc 2141 gdbserver_usage (stdout);
2d717e4f
DJ
2142 exit (0);
2143 }
2144 else if (strcmp (*next_arg, "--attach") == 0)
2145 attach = 1;
2146 else if (strcmp (*next_arg, "--multi") == 0)
2147 multi_mode = 1;
ccd213ac
DJ
2148 else if (strcmp (*next_arg, "--wrapper") == 0)
2149 {
2150 next_arg++;
2151
2152 wrapper_argv = next_arg;
2153 while (*next_arg != NULL && strcmp (*next_arg, "--") != 0)
2154 next_arg++;
2155
2156 if (next_arg == wrapper_argv || *next_arg == NULL)
2157 {
c16158bc 2158 gdbserver_usage (stderr);
ccd213ac
DJ
2159 exit (1);
2160 }
2161
2162 /* Consume the "--". */
2163 *next_arg = NULL;
2164 }
2d717e4f
DJ
2165 else if (strcmp (*next_arg, "--debug") == 0)
2166 debug_threads = 1;
62709adf
PA
2167 else if (strcmp (*next_arg, "--remote-debug") == 0)
2168 remote_debug = 1;
db42f210
PA
2169 else if (strcmp (*next_arg, "--disable-packet") == 0)
2170 {
2171 gdbserver_show_disableable (stdout);
2172 exit (0);
2173 }
2174 else if (strncmp (*next_arg,
2175 "--disable-packet=",
2176 sizeof ("--disable-packet=") - 1) == 0)
2177 {
2178 char *packets, *tok;
2179
2180 packets = *next_arg += sizeof ("--disable-packet=") - 1;
2181 for (tok = strtok (packets, ",");
2182 tok != NULL;
2183 tok = strtok (NULL, ","))
2184 {
2185 if (strcmp ("vCont", tok) == 0)
2186 disable_packet_vCont = 1;
2187 else if (strcmp ("Tthread", tok) == 0)
2188 disable_packet_Tthread = 1;
2189 else if (strcmp ("qC", tok) == 0)
2190 disable_packet_qC = 1;
2191 else if (strcmp ("qfThreadInfo", tok) == 0)
2192 disable_packet_qfThreadInfo = 1;
2193 else if (strcmp ("threads", tok) == 0)
2194 {
2195 disable_packet_vCont = 1;
2196 disable_packet_Tthread = 1;
2197 disable_packet_qC = 1;
2198 disable_packet_qfThreadInfo = 1;
2199 }
2200 else
2201 {
2202 fprintf (stderr, "Don't know how to disable \"%s\".\n\n",
2203 tok);
2204 gdbserver_show_disableable (stderr);
2205 exit (1);
2206 }
2207 }
2208 }
2d717e4f
DJ
2209 else
2210 {
2211 fprintf (stderr, "Unknown argument: %s\n", *next_arg);
2212 exit (1);
2213 }
dd24457d 2214
2d717e4f
DJ
2215 next_arg++;
2216 continue;
dd24457d
DJ
2217 }
2218
c5aa993b 2219 if (setjmp (toplevel))
c906108c 2220 {
c5aa993b
JM
2221 fprintf (stderr, "Exiting\n");
2222 exit (1);
c906108c
SS
2223 }
2224
2d717e4f
DJ
2225 port = *next_arg;
2226 next_arg++;
2227 if (port == NULL || (!attach && !multi_mode && *next_arg == NULL))
2228 {
c16158bc 2229 gdbserver_usage (stderr);
2d717e4f
DJ
2230 exit (1);
2231 }
2232
0729219d
DJ
2233 bad_attach = 0;
2234 pid = 0;
2d717e4f
DJ
2235
2236 /* --attach used to come after PORT, so allow it there for
2237 compatibility. */
2238 if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0)
45b7b345 2239 {
2d717e4f
DJ
2240 attach = 1;
2241 next_arg++;
45b7b345
DJ
2242 }
2243
2d717e4f
DJ
2244 if (attach
2245 && (*next_arg == NULL
2246 || (*next_arg)[0] == '\0'
2247 || (pid = strtoul (*next_arg, &arg_end, 0)) == 0
2248 || *arg_end != '\0'
2249 || next_arg[1] != NULL))
2250 bad_attach = 1;
2251
2252 if (bad_attach)
dd24457d 2253 {
c16158bc 2254 gdbserver_usage (stderr);
dd24457d
DJ
2255 exit (1);
2256 }
c906108c 2257
95954743 2258 initialize_inferiors ();
a20d5e98 2259 initialize_async_io ();
4ce44c66
JM
2260 initialize_low ();
2261
bca929d3
DE
2262 own_buf = xmalloc (PBUFSIZ + 1);
2263 mem_buf = xmalloc (PBUFSIZ);
0a30fbc4 2264
2d717e4f 2265 if (pid == 0 && *next_arg != NULL)
45b7b345 2266 {
2d717e4f
DJ
2267 int i, n;
2268
2269 n = argc - (next_arg - argv);
bca929d3 2270 program_argv = xmalloc (sizeof (char *) * (n + 1));
2d717e4f 2271 for (i = 0; i < n; i++)
bca929d3 2272 program_argv[i] = xstrdup (next_arg[i]);
2d717e4f
DJ
2273 program_argv[i] = NULL;
2274
45b7b345 2275 /* Wait till we are at first instruction in program. */
5b1c542e 2276 start_inferior (program_argv);
c906108c 2277
c588c53c
MS
2278 /* We are now (hopefully) stopped at the first instruction of
2279 the target process. This assumes that the target process was
2280 successfully created. */
45b7b345 2281 }
2d717e4f
DJ
2282 else if (pid != 0)
2283 {
5b1c542e 2284 if (attach_inferior (pid) == -1)
2d717e4f
DJ
2285 error ("Attaching not supported on this target");
2286
2287 /* Otherwise succeeded. */
2288 }
45b7b345
DJ
2289 else
2290 {
5b1c542e
PA
2291 last_status.kind = TARGET_WAITKIND_EXITED;
2292 last_status.value.integer = 0;
95954743 2293 last_ptid = minus_one_ptid;
45b7b345 2294 }
c906108c 2295
311de423
PA
2296 /* Don't report shared library events on the initial connection,
2297 even if some libraries are preloaded. Avoids the "stopped by
2298 shared library event" notice on gdb side. */
2299 dlls_changed = 0;
2300
8264bb58
DJ
2301 if (setjmp (toplevel))
2302 {
9f767825 2303 detach_or_kill_for_exit ();
8264bb58
DJ
2304 exit (1);
2305 }
2306
5b1c542e
PA
2307 if (last_status.kind == TARGET_WAITKIND_EXITED
2308 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
2d717e4f
DJ
2309 was_running = 0;
2310 else
2311 was_running = 1;
2312
2313 if (!was_running && !multi_mode)
c588c53c 2314 {
2d717e4f 2315 fprintf (stderr, "No program to debug. GDBserver exiting.\n");
c588c53c
MS
2316 exit (1);
2317 }
2318
c906108c
SS
2319 while (1)
2320 {
a6f3e723 2321 noack_mode = 0;
95954743 2322 multi_process = 0;
bd99dc85
PA
2323 non_stop = 0;
2324
2d717e4f 2325 remote_open (port);
c906108c 2326
2d717e4f
DJ
2327 if (setjmp (toplevel) != 0)
2328 {
2329 /* An error occurred. */
2330 if (response_needed)
2331 {
2332 write_enn (own_buf);
2333 putpkt (own_buf);
2334 }
2335 }
2336
bd99dc85
PA
2337 /* Wait for events. This will return when all event sources are
2338 removed from the event loop. */
2339 start_event_loop ();
2340
2341 /* If an exit was requested (using the "monitor exit" command),
2342 terminate now. The only other way to get here is for
2343 getpkt to fail; close the connection and reopen it at the
2344 top of the loop. */
2345
2346 if (exit_requested)
c906108c 2347 {
9f767825 2348 detach_or_kill_for_exit ();
bd99dc85
PA
2349 exit (0);
2350 }
2351 else
2352 fprintf (stderr, "Remote side has terminated connection. "
2353 "GDBserver will reopen the connection.\n");
2354 }
2355}
01f9e8fa 2356
bd99dc85
PA
2357/* Event loop callback that handles a serial event. The first byte in
2358 the serial buffer gets us here. We expect characters to arrive at
2359 a brisk pace, so we read the rest of the packet with a blocking
2360 getpkt call. */
01f9e8fa 2361
bd99dc85
PA
2362static void
2363process_serial_event (void)
2364{
2365 char ch;
2366 int i = 0;
2367 int signal;
2368 unsigned int len;
2369 CORE_ADDR mem_addr;
95954743 2370 int pid;
bd99dc85
PA
2371 unsigned char sig;
2372 int packet_len;
2373 int new_packet_len = -1;
2374
2375 /* Used to decide when gdbserver should exit in
2376 multi-mode/remote. */
2377 static int have_ran = 0;
2378
2379 if (!have_ran)
2380 have_ran = target_running ();
2381
2382 disable_async_io ();
2383
2384 response_needed = 0;
2385 packet_len = getpkt (own_buf);
2386 if (packet_len <= 0)
2387 {
2388 target_async (0);
2389 remote_close ();
2390 return;
2391 }
2392 response_needed = 1;
2393
2394 i = 0;
2395 ch = own_buf[i++];
2396 switch (ch)
2397 {
2398 case 'q':
2399 handle_query (own_buf, packet_len, &new_packet_len);
2400 break;
2401 case 'Q':
2402 handle_general_set (own_buf);
2403 break;
2404 case 'D':
2405 require_running (own_buf);
95954743
PA
2406
2407 if (multi_process)
2408 {
2409 i++; /* skip ';' */
2410 pid = strtol (&own_buf[i], NULL, 16);
2411 }
2412 else
2413 pid =
2414 ptid_get_pid (((struct inferior_list_entry *) current_inferior)->id);
2415
2416 fprintf (stderr, "Detaching from process %d\n", pid);
2417 if (detach_inferior (pid) != 0)
bd99dc85
PA
2418 write_enn (own_buf);
2419 else
2420 {
95954743 2421 discard_queued_stop_replies (pid);
bd99dc85
PA
2422 write_ok (own_buf);
2423
2424 if (extended_protocol)
c906108c 2425 {
bd99dc85
PA
2426 /* Treat this like a normal program exit. */
2427 last_status.kind = TARGET_WAITKIND_EXITED;
2428 last_status.value.integer = 0;
95954743 2429 last_ptid = pid_to_ptid (pid);
2d717e4f 2430
bd99dc85
PA
2431 current_inferior = NULL;
2432 }
2433 else
2434 {
2435 putpkt (own_buf);
2436 remote_close ();
2437
2438 /* If we are attached, then we can exit. Otherwise, we
2439 need to hang around doing nothing, until the child is
2440 gone. */
95954743
PA
2441 for_each_inferior (&all_processes,
2442 join_inferiors_callback);
bd99dc85
PA
2443 exit (0);
2444 }
2445 }
2446 break;
2447 case '!':
2448 extended_protocol = 1;
2449 write_ok (own_buf);
2450 break;
2451 case '?':
2452 handle_status (own_buf);
2453 break;
2454 case 'H':
2455 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
2456 {
95954743
PA
2457 ptid_t gdb_id, thread_id;
2458 int pid;
bd99dc85
PA
2459
2460 require_running (own_buf);
95954743
PA
2461
2462 gdb_id = read_ptid (&own_buf[2], NULL);
2463
2464 pid = ptid_get_pid (gdb_id);
2465
2466 if (ptid_equal (gdb_id, null_ptid)
2467 || ptid_equal (gdb_id, minus_one_ptid))
2468 thread_id = null_ptid;
2469 else if (pid != 0
2470 && ptid_equal (pid_to_ptid (pid),
2471 gdb_id))
2472 {
2473 struct thread_info *thread =
2474 (struct thread_info *) find_inferior (&all_threads,
2475 first_thread_of,
2476 &pid);
2477 if (!thread)
2478 {
2479 write_enn (own_buf);
2480 break;
2481 }
2482
2483 thread_id = ((struct inferior_list_entry *)thread)->id;
2484 }
bd99dc85
PA
2485 else
2486 {
2487 thread_id = gdb_id_to_thread_id (gdb_id);
95954743 2488 if (ptid_equal (thread_id, null_ptid))
c906108c 2489 {
a06660f7 2490 write_enn (own_buf);
c906108c
SS
2491 break;
2492 }
c906108c
SS
2493 }
2494
bd99dc85 2495 if (own_buf[1] == 'g')
c906108c 2496 {
95954743 2497 if (ptid_equal (thread_id, null_ptid))
c906108c 2498 {
bd99dc85
PA
2499 /* GDB is telling us to choose any thread. Check if
2500 the currently selected thread is still valid. If
2501 it is not, select the first available. */
2502 struct thread_info *thread =
2503 (struct thread_info *) find_inferior_id (&all_threads,
2504 general_thread);
2505 if (thread == NULL)
2506 thread_id = all_threads.head->id;
c906108c 2507 }
bd99dc85
PA
2508
2509 general_thread = thread_id;
2510 set_desired_inferior (1);
c906108c 2511 }
bd99dc85
PA
2512 else if (own_buf[1] == 'c')
2513 cont_thread = thread_id;
2514 else if (own_buf[1] == 's')
2515 step_thread = thread_id;
c906108c 2516
bd99dc85
PA
2517 write_ok (own_buf);
2518 }
2519 else
2520 {
2521 /* Silently ignore it so that gdb can extend the protocol
2522 without compatibility headaches. */
2523 own_buf[0] = '\0';
2d717e4f 2524 }
bd99dc85
PA
2525 break;
2526 case 'g':
442ea881
PA
2527 {
2528 struct regcache *regcache;
2529
2530 require_running (own_buf);
2531 set_desired_inferior (1);
2532 regcache = get_thread_regcache (current_inferior, 1);
2533 registers_to_string (regcache, own_buf);
2534 }
bd99dc85
PA
2535 break;
2536 case 'G':
442ea881
PA
2537 {
2538 struct regcache *regcache;
2539
2540 require_running (own_buf);
2541 set_desired_inferior (1);
2542 regcache = get_thread_regcache (current_inferior, 1);
2543 registers_from_string (regcache, &own_buf[1]);
2544 write_ok (own_buf);
2545 }
bd99dc85
PA
2546 break;
2547 case 'm':
2548 require_running (own_buf);
2549 decode_m_packet (&own_buf[1], &mem_addr, &len);
2550 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
2551 convert_int_to_ascii (mem_buf, own_buf, len);
2552 else
2553 write_enn (own_buf);
2554 break;
2555 case 'M':
2556 require_running (own_buf);
2557 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
2558 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
2559 write_ok (own_buf);
2560 else
2561 write_enn (own_buf);
2562 break;
2563 case 'X':
2564 require_running (own_buf);
2565 if (decode_X_packet (&own_buf[1], packet_len - 1,
2566 &mem_addr, &len, mem_buf) < 0
2567 || write_inferior_memory (mem_addr, mem_buf, len) != 0)
2568 write_enn (own_buf);
2569 else
2570 write_ok (own_buf);
2571 break;
2572 case 'C':
2573 require_running (own_buf);
2574 convert_ascii_to_int (own_buf + 1, &sig, 1);
2575 if (target_signal_to_host_p (sig))
2576 signal = target_signal_to_host (sig);
2577 else
2578 signal = 0;
2579 myresume (own_buf, 0, signal);
2580 break;
2581 case 'S':
2582 require_running (own_buf);
2583 convert_ascii_to_int (own_buf + 1, &sig, 1);
2584 if (target_signal_to_host_p (sig))
2585 signal = target_signal_to_host (sig);
2586 else
2587 signal = 0;
2588 myresume (own_buf, 1, signal);
2589 break;
2590 case 'c':
2591 require_running (own_buf);
2592 signal = 0;
2593 myresume (own_buf, 0, signal);
2594 break;
2595 case 's':
2596 require_running (own_buf);
2597 signal = 0;
2598 myresume (own_buf, 1, signal);
2599 break;
c6314022
AR
2600 case 'Z': /* insert_ ... */
2601 /* Fallthrough. */
2602 case 'z': /* remove_ ... */
bd99dc85
PA
2603 {
2604 char *lenptr;
2605 char *dataptr;
2606 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
2607 int len = strtol (lenptr + 1, &dataptr, 16);
2608 char type = own_buf[1];
c6314022 2609 int res;
d993e290 2610 const int insert = ch == 'Z';
c6314022 2611
d993e290
PA
2612 /* Default to unrecognized/unsupported. */
2613 res = 1;
2614 switch (type)
2615 {
2616 case '0': /* software-breakpoint */
2617 case '1': /* hardware-breakpoint */
2618 case '2': /* write watchpoint */
2619 case '3': /* read watchpoint */
2620 case '4': /* access watchpoint */
2621 require_running (own_buf);
2622 if (insert && the_target->insert_point != NULL)
2623 res = (*the_target->insert_point) (type, addr, len);
2624 else if (!insert && the_target->remove_point != NULL)
2625 res = (*the_target->remove_point) (type, addr, len);
2626 break;
2627 default:
2628 break;
2629 }
bd99dc85 2630
c6314022
AR
2631 if (res == 0)
2632 write_ok (own_buf);
2633 else if (res == 1)
2634 /* Unsupported. */
2635 own_buf[0] = '\0';
bd99dc85 2636 else
c6314022 2637 write_enn (own_buf);
bd99dc85
PA
2638 break;
2639 }
2640 case 'k':
2641 response_needed = 0;
2642 if (!target_running ())
95954743
PA
2643 /* The packet we received doesn't make sense - but we can't
2644 reply to it, either. */
bd99dc85 2645 return;
c906108c 2646
95954743
PA
2647 fprintf (stderr, "Killing all inferiors\n");
2648 for_each_inferior (&all_processes, kill_inferior_callback);
c906108c 2649
bd99dc85
PA
2650 /* When using the extended protocol, we wait with no program
2651 running. The traditional protocol will exit instead. */
2652 if (extended_protocol)
2653 {
2654 last_status.kind = TARGET_WAITKIND_EXITED;
2655 last_status.value.sig = TARGET_SIGNAL_KILL;
2656 return;
2657 }
2658 else
c906108c 2659 {
c906108c 2660 exit (0);
bd99dc85
PA
2661 break;
2662 }
2663 case 'T':
2664 {
95954743 2665 ptid_t gdb_id, thread_id;
bd99dc85
PA
2666
2667 require_running (own_buf);
95954743
PA
2668
2669 gdb_id = read_ptid (&own_buf[1], NULL);
bd99dc85 2670 thread_id = gdb_id_to_thread_id (gdb_id);
95954743 2671 if (ptid_equal (thread_id, null_ptid))
bd99dc85
PA
2672 {
2673 write_enn (own_buf);
2674 break;
2675 }
2676
2677 if (mythread_alive (thread_id))
2678 write_ok (own_buf);
2679 else
2680 write_enn (own_buf);
2681 }
2682 break;
2683 case 'R':
2684 response_needed = 0;
2685
2686 /* Restarting the inferior is only supported in the extended
2687 protocol. */
2688 if (extended_protocol)
2689 {
2690 if (target_running ())
95954743
PA
2691 for_each_inferior (&all_processes,
2692 kill_inferior_callback);
bd99dc85
PA
2693 fprintf (stderr, "GDBserver restarting\n");
2694
2695 /* Wait till we are at 1st instruction in prog. */
2696 if (program_argv != NULL)
2697 start_inferior (program_argv);
2698 else
2699 {
2700 last_status.kind = TARGET_WAITKIND_EXITED;
2701 last_status.value.sig = TARGET_SIGNAL_KILL;
2702 }
2703 return;
c906108c
SS
2704 }
2705 else
2706 {
bd99dc85
PA
2707 /* It is a request we don't understand. Respond with an
2708 empty packet so that gdb knows that we don't support this
2709 request. */
2710 own_buf[0] = '\0';
2711 break;
2712 }
2713 case 'v':
2714 /* Extended (long) request. */
2715 handle_v_requests (own_buf, packet_len, &new_packet_len);
2716 break;
2717
2718 default:
2719 /* It is a request we don't understand. Respond with an empty
2720 packet so that gdb knows that we don't support this
2721 request. */
2722 own_buf[0] = '\0';
2723 break;
2724 }
2725
2726 if (new_packet_len != -1)
2727 putpkt_binary (own_buf, new_packet_len);
2728 else
2729 putpkt (own_buf);
2730
2731 response_needed = 0;
2732
2733 if (!extended_protocol && have_ran && !target_running ())
2734 {
2735 /* In non-stop, defer exiting until GDB had a chance to query
2736 the whole vStopped list (until it gets an OK). */
2737 if (!notif_queue)
2738 {
2739 fprintf (stderr, "GDBserver exiting\n");
c906108c 2740 remote_close ();
bd99dc85 2741 exit (0);
c906108c
SS
2742 }
2743 }
2744}
bd99dc85
PA
2745
2746/* Event-loop callback for serial events. */
2747
2748void
2749handle_serial_event (int err, gdb_client_data client_data)
2750{
2751 if (debug_threads)
2752 fprintf (stderr, "handling possible serial event\n");
2753
2754 /* Really handle it. */
2755 process_serial_event ();
2756
2757 /* Be sure to not change the selected inferior behind GDB's back.
2758 Important in the non-stop mode asynchronous protocol. */
2759 set_desired_inferior (1);
2760}
2761
2762/* Event-loop callback for target events. */
2763
2764void
2765handle_target_event (int err, gdb_client_data client_data)
2766{
2767 if (debug_threads)
2768 fprintf (stderr, "handling possible target event\n");
2769
95954743
PA
2770 last_ptid = mywait (minus_one_ptid, &last_status,
2771 TARGET_WNOHANG, 1);
bd99dc85
PA
2772
2773 if (last_status.kind != TARGET_WAITKIND_IGNORE)
2774 {
2775 /* Something interesting. Tell GDB about it. */
2776 push_event (last_ptid, &last_status);
2777 }
2778
2779 /* Be sure to not change the selected inferior behind GDB's back.
2780 Important in the non-stop mode asynchronous protocol. */
2781 set_desired_inferior (1);
2782}
This page took 1.157789 seconds and 4 git commands to generate.