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