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