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