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