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