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