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