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