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