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