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