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