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