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