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