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