* gdb.texinfo (GDB/MI Tracepoint Commands): Document MI tracepoint
[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
89be2091
DJ
452 /* Otherwise we didn't know what packet it was. Say we didn't
453 understand it. */
454 own_buf[0] = 0;
455}
456
23181151 457static const char *
fb1e4ffc 458get_features_xml (const char *annex)
23181151 459{
9b4b61c8
UW
460 /* gdbserver_xmltarget defines what to return when looking
461 for the "target.xml" file. Its contents can either be
462 verbatim XML code (prefixed with a '@') or else the name
463 of the actual XML file to be used in place of "target.xml".
fb1e4ffc 464
9b4b61c8
UW
465 This variable is set up from the auto-generated
466 init_registers_... routine for the current target. */
fb1e4ffc 467
9b4b61c8 468 if (gdbserver_xmltarget
221c031f 469 && strcmp (annex, "target.xml") == 0)
23181151 470 {
9b4b61c8
UW
471 if (*gdbserver_xmltarget == '@')
472 return gdbserver_xmltarget + 1;
23181151 473 else
9b4b61c8 474 annex = gdbserver_xmltarget;
23181151
DJ
475 }
476
9b4b61c8
UW
477#ifdef USE_XML
478 {
479 extern const char *const xml_builtin[][2];
480 int i;
481
482 /* Look for the annex. */
483 for (i = 0; xml_builtin[i][0] != NULL; i++)
484 if (strcmp (annex, xml_builtin[i][0]) == 0)
485 break;
486
487 if (xml_builtin[i][0] != NULL)
488 return xml_builtin[i][1];
489 }
490#endif
491
492 return NULL;
23181151
DJ
493}
494
c74d0ad8
DJ
495void
496monitor_show_help (void)
497{
498 monitor_output ("The following monitor commands are supported:\n");
499 monitor_output (" set debug <0|1>\n");
1b3f6016 500 monitor_output (" Enable general debugging messages\n");
aa5ca48f
DE
501 monitor_output (" set debug-hw-points <0|1>\n");
502 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
c74d0ad8
DJ
503 monitor_output (" set remote-debug <0|1>\n");
504 monitor_output (" Enable remote protocol debugging messages\n");
ecd7ecbc
DJ
505 monitor_output (" exit\n");
506 monitor_output (" Quit GDBserver\n");
c74d0ad8
DJ
507}
508
08388c79
DE
509/* Subroutine of handle_search_memory to simplify it. */
510
511static int
512handle_search_memory_1 (CORE_ADDR start_addr, CORE_ADDR search_space_len,
513 gdb_byte *pattern, unsigned pattern_len,
514 gdb_byte *search_buf,
515 unsigned chunk_size, unsigned search_buf_size,
516 CORE_ADDR *found_addrp)
517{
518 /* Prime the search buffer. */
519
520 if (read_inferior_memory (start_addr, search_buf, search_buf_size) != 0)
521 {
5e1471f5 522 warning ("Unable to access target memory at 0x%lx, halting search.",
08388c79
DE
523 (long) start_addr);
524 return -1;
525 }
526
527 /* Perform the search.
528
529 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
530 When we've scanned N bytes we copy the trailing bytes to the start and
531 read in another N bytes. */
532
533 while (search_space_len >= pattern_len)
534 {
535 gdb_byte *found_ptr;
536 unsigned nr_search_bytes = (search_space_len < search_buf_size
537 ? search_space_len
538 : search_buf_size);
539
540 found_ptr = memmem (search_buf, nr_search_bytes, pattern, pattern_len);
541
542 if (found_ptr != NULL)
543 {
544 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
545 *found_addrp = found_addr;
546 return 1;
547 }
548
549 /* Not found in this chunk, skip to next chunk. */
550
551 /* Don't let search_space_len wrap here, it's unsigned. */
552 if (search_space_len >= chunk_size)
553 search_space_len -= chunk_size;
554 else
555 search_space_len = 0;
556
557 if (search_space_len >= pattern_len)
558 {
559 unsigned keep_len = search_buf_size - chunk_size;
8a35fb51 560 CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
08388c79
DE
561 int nr_to_read;
562
563 /* Copy the trailing part of the previous iteration to the front
564 of the buffer for the next iteration. */
565 memcpy (search_buf, search_buf + chunk_size, keep_len);
566
567 nr_to_read = (search_space_len - keep_len < chunk_size
568 ? search_space_len - keep_len
569 : chunk_size);
570
571 if (read_inferior_memory (read_addr, search_buf + keep_len,
572 nr_to_read) != 0)
573 {
5e1471f5 574 warning ("Unable to access target memory at 0x%lx, halting search.",
08388c79
DE
575 (long) read_addr);
576 return -1;
577 }
578
579 start_addr += chunk_size;
580 }
581 }
582
583 /* Not found. */
584
585 return 0;
586}
587
588/* Handle qSearch:memory packets. */
589
590static void
591handle_search_memory (char *own_buf, int packet_len)
592{
593 CORE_ADDR start_addr;
594 CORE_ADDR search_space_len;
595 gdb_byte *pattern;
596 unsigned int pattern_len;
597 /* NOTE: also defined in find.c testcase. */
598#define SEARCH_CHUNK_SIZE 16000
599 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
600 /* Buffer to hold memory contents for searching. */
601 gdb_byte *search_buf;
602 unsigned search_buf_size;
603 int found;
604 CORE_ADDR found_addr;
605 int cmd_name_len = sizeof ("qSearch:memory:") - 1;
606
aef93bd7 607 pattern = malloc (packet_len);
08388c79
DE
608 if (pattern == NULL)
609 {
5e1471f5 610 error ("Unable to allocate memory to perform the search");
08388c79
DE
611 strcpy (own_buf, "E00");
612 return;
613 }
614 if (decode_search_memory_packet (own_buf + cmd_name_len,
615 packet_len - cmd_name_len,
616 &start_addr, &search_space_len,
617 pattern, &pattern_len) < 0)
618 {
619 free (pattern);
5e1471f5 620 error ("Error in parsing qSearch:memory packet");
08388c79
DE
621 strcpy (own_buf, "E00");
622 return;
623 }
624
625 search_buf_size = chunk_size + pattern_len - 1;
626
627 /* No point in trying to allocate a buffer larger than the search space. */
628 if (search_space_len < search_buf_size)
629 search_buf_size = search_space_len;
630
aef93bd7 631 search_buf = malloc (search_buf_size);
08388c79
DE
632 if (search_buf == NULL)
633 {
634 free (pattern);
5e1471f5 635 error ("Unable to allocate memory to perform the search");
08388c79
DE
636 strcpy (own_buf, "E00");
637 return;
638 }
639
640 found = handle_search_memory_1 (start_addr, search_space_len,
641 pattern, pattern_len,
642 search_buf, chunk_size, search_buf_size,
643 &found_addr);
644
645 if (found > 0)
646 sprintf (own_buf, "1,%lx", (long) found_addr);
647 else if (found == 0)
648 strcpy (own_buf, "0");
649 else
650 strcpy (own_buf, "E00");
651
652 free (search_buf);
653 free (pattern);
654}
655
2d717e4f
DJ
656#define require_running(BUF) \
657 if (!target_running ()) \
658 { \
659 write_enn (BUF); \
660 return; \
661 }
662
cdbfd419
PP
663/* Handle monitor commands not handled by target-specific handlers. */
664
665static void
666handle_monitor_command (char *mon)
667{
668 if (strcmp (mon, "set debug 1") == 0)
669 {
670 debug_threads = 1;
671 monitor_output ("Debug output enabled.\n");
672 }
673 else if (strcmp (mon, "set debug 0") == 0)
674 {
675 debug_threads = 0;
676 monitor_output ("Debug output disabled.\n");
677 }
678 else if (strcmp (mon, "set debug-hw-points 1") == 0)
679 {
680 debug_hw_points = 1;
681 monitor_output ("H/W point debugging output enabled.\n");
682 }
683 else if (strcmp (mon, "set debug-hw-points 0") == 0)
684 {
685 debug_hw_points = 0;
686 monitor_output ("H/W point debugging output disabled.\n");
687 }
688 else if (strcmp (mon, "set remote-debug 1") == 0)
689 {
690 remote_debug = 1;
691 monitor_output ("Protocol debug output enabled.\n");
692 }
693 else if (strcmp (mon, "set remote-debug 0") == 0)
694 {
695 remote_debug = 0;
696 monitor_output ("Protocol debug output disabled.\n");
697 }
698 else if (strcmp (mon, "help") == 0)
699 monitor_show_help ();
700 else if (strcmp (mon, "exit") == 0)
701 exit_requested = 1;
702 else
703 {
704 monitor_output ("Unknown monitor command.\n\n");
705 monitor_show_help ();
706 write_enn (own_buf);
707 }
708}
709
dc146f7c
VP
710static void
711handle_threads_qxfer_proper (struct buffer *buffer)
712{
713 struct inferior_list_entry *thread;
714
715 buffer_grow_str (buffer, "<threads>\n");
716
717 for (thread = all_threads.head; thread; thread = thread->next)
718 {
719 ptid_t ptid = thread_to_gdb_id ((struct thread_info *)thread);
720 char ptid_s[100];
721 int core = -1;
722 char core_s[21];
723
724 write_ptid (ptid_s, ptid);
725
726 if (the_target->core_of_thread)
727 core = (*the_target->core_of_thread) (ptid);
728
729 if (core != -1)
730 {
731 sprintf (core_s, "%d", core);
732 buffer_xml_printf (buffer, "<thread id=\"%s\" core=\"%s\"/>\n",
733 ptid_s, core_s);
734 }
735 else
736 {
737 buffer_xml_printf (buffer, "<thread id=\"%s\"/>\n",
738 ptid_s);
739 }
740 }
741
742 buffer_grow_str0 (buffer, "</threads>\n");
743}
744
745static int
746handle_threads_qxfer (const char *annex,
747 unsigned char *readbuf,
748 CORE_ADDR offset, int length)
749{
750 static char *result = 0;
751 static unsigned int result_length = 0;
752
753 if (annex && strcmp (annex, "") != 0)
754 return 0;
755
756 if (offset == 0)
757 {
758 struct buffer buffer;
759 /* When asked for data at offset 0, generate everything and store into
760 'result'. Successive reads will be served off 'result'. */
761 if (result)
762 free (result);
763
764 buffer_init (&buffer);
765
766 handle_threads_qxfer_proper (&buffer);
767
768 result = buffer_finish (&buffer);
769 result_length = strlen (result);
770 buffer_free (&buffer);
771 }
772
773 if (offset >= result_length)
774 {
775 /* We're out of data. */
776 free (result);
777 result = NULL;
778 result_length = 0;
779 return 0;
780 }
781
782 if (length > result_length - offset)
783 length = result_length - offset;
784
785 memcpy (readbuf, result + offset, length);
786
787 return length;
788
789}
790
ce3a066d
DJ
791/* Handle all of the extended 'q' packets. */
792void
0e7f50da 793handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
ce3a066d 794{
0d62e5e8
DJ
795 static struct inferior_list_entry *thread_ptr;
796
bb63802a 797 /* Reply the current thread id. */
db42f210 798 if (strcmp ("qC", own_buf) == 0 && !disable_packet_qC)
bb63802a 799 {
95954743 800 ptid_t gdb_id;
2d717e4f 801 require_running (own_buf);
bd99dc85 802
95954743
PA
803 if (!ptid_equal (general_thread, null_ptid)
804 && !ptid_equal (general_thread, minus_one_ptid))
bd99dc85
PA
805 gdb_id = general_thread;
806 else
807 {
808 thread_ptr = all_threads.head;
809 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
810 }
811
95954743
PA
812 sprintf (own_buf, "QC");
813 own_buf += 2;
814 own_buf = write_ptid (own_buf, gdb_id);
bb63802a
UW
815 return;
816 }
817
ce3a066d
DJ
818 if (strcmp ("qSymbol::", own_buf) == 0)
819 {
2d717e4f 820 if (target_running () && the_target->look_up_symbols != NULL)
2f2893d9
DJ
821 (*the_target->look_up_symbols) ();
822
ce3a066d
DJ
823 strcpy (own_buf, "OK");
824 return;
825 }
826
db42f210 827 if (!disable_packet_qfThreadInfo)
0d62e5e8 828 {
db42f210 829 if (strcmp ("qfThreadInfo", own_buf) == 0)
0d62e5e8 830 {
95954743
PA
831 ptid_t gdb_id;
832
db42f210
PA
833 require_running (own_buf);
834 thread_ptr = all_threads.head;
95954743
PA
835
836 *own_buf++ = 'm';
837 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
838 write_ptid (own_buf, gdb_id);
0d62e5e8
DJ
839 thread_ptr = thread_ptr->next;
840 return;
841 }
db42f210
PA
842
843 if (strcmp ("qsThreadInfo", own_buf) == 0)
0d62e5e8 844 {
95954743
PA
845 ptid_t gdb_id;
846
db42f210
PA
847 require_running (own_buf);
848 if (thread_ptr != NULL)
849 {
95954743
PA
850 *own_buf++ = 'm';
851 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
852 write_ptid (own_buf, gdb_id);
db42f210
PA
853 thread_ptr = thread_ptr->next;
854 return;
855 }
856 else
857 {
858 sprintf (own_buf, "l");
859 return;
860 }
0d62e5e8
DJ
861 }
862 }
aa691b87 863
52fb6437
NS
864 if (the_target->read_offsets != NULL
865 && strcmp ("qOffsets", own_buf) == 0)
866 {
867 CORE_ADDR text, data;
2d717e4f
DJ
868
869 require_running (own_buf);
52fb6437
NS
870 if (the_target->read_offsets (&text, &data))
871 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
872 (long)text, (long)data, (long)data);
873 else
874 write_enn (own_buf);
1b3f6016 875
52fb6437
NS
876 return;
877 }
878
0e7f50da
UW
879 if (the_target->qxfer_spu != NULL
880 && strncmp ("qXfer:spu:read:", own_buf, 15) == 0)
881 {
882 char *annex;
883 int n;
884 unsigned int len;
885 CORE_ADDR ofs;
886 unsigned char *spu_buf;
887
2d717e4f 888 require_running (own_buf);
0e7f50da
UW
889 strcpy (own_buf, "E00");
890 if (decode_xfer_read (own_buf + 15, &annex, &ofs, &len) < 0)
78e5cee6 891 return;
0e7f50da
UW
892 if (len > PBUFSIZ - 2)
893 len = PBUFSIZ - 2;
aef93bd7 894 spu_buf = malloc (len + 1);
0e7f50da 895 if (!spu_buf)
1b3f6016 896 return;
0e7f50da
UW
897
898 n = (*the_target->qxfer_spu) (annex, spu_buf, NULL, ofs, len + 1);
1b3f6016 899 if (n < 0)
0e7f50da
UW
900 write_enn (own_buf);
901 else if (n > len)
78e5cee6 902 *new_packet_len_p = write_qxfer_response (own_buf, spu_buf, len, 1);
1b3f6016 903 else
78e5cee6 904 *new_packet_len_p = write_qxfer_response (own_buf, spu_buf, n, 0);
0e7f50da
UW
905
906 free (spu_buf);
907 return;
908 }
909
910 if (the_target->qxfer_spu != NULL
911 && strncmp ("qXfer:spu:write:", own_buf, 16) == 0)
912 {
913 char *annex;
914 int n;
915 unsigned int len;
916 CORE_ADDR ofs;
917 unsigned char *spu_buf;
918
2d717e4f 919 require_running (own_buf);
0e7f50da 920 strcpy (own_buf, "E00");
aef93bd7 921 spu_buf = malloc (packet_len - 15);
0e7f50da 922 if (!spu_buf)
1b3f6016 923 return;
0e7f50da
UW
924 if (decode_xfer_write (own_buf + 16, packet_len - 16, &annex,
925 &ofs, &len, spu_buf) < 0)
926 {
927 free (spu_buf);
928 return;
929 }
930
1b3f6016 931 n = (*the_target->qxfer_spu)
0e7f50da
UW
932 (annex, NULL, (unsigned const char *)spu_buf, ofs, len);
933 if (n < 0)
934 write_enn (own_buf);
935 else
936 sprintf (own_buf, "%x", n);
937
938 free (spu_buf);
939 return;
940 }
941
aa691b87 942 if (the_target->read_auxv != NULL
0876f84a 943 && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0)
aa691b87 944 {
0876f84a
DJ
945 unsigned char *data;
946 int n;
aa691b87
RM
947 CORE_ADDR ofs;
948 unsigned int len;
0876f84a
DJ
949 char *annex;
950
2d717e4f
DJ
951 require_running (own_buf);
952
0876f84a
DJ
953 /* Reject any annex; grab the offset and length. */
954 if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0
955 || annex[0] != '\0')
956 {
957 strcpy (own_buf, "E00");
958 return;
959 }
960
961 /* Read one extra byte, as an indicator of whether there is
962 more. */
963 if (len > PBUFSIZ - 2)
964 len = PBUFSIZ - 2;
aef93bd7
DE
965 data = malloc (len + 1);
966 if (data == NULL)
967 {
968 write_enn (own_buf);
969 return;
970 }
0876f84a 971 n = (*the_target->read_auxv) (ofs, data, len + 1);
000ef4f0
DJ
972 if (n < 0)
973 write_enn (own_buf);
974 else if (n > len)
0876f84a 975 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
aa691b87 976 else
0876f84a
DJ
977 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
978
979 free (data);
980
aa691b87
RM
981 return;
982 }
983
23181151
DJ
984 if (strncmp ("qXfer:features:read:", own_buf, 20) == 0)
985 {
986 CORE_ADDR ofs;
987 unsigned int len, total_len;
988 const char *document;
989 char *annex;
990
2d717e4f
DJ
991 require_running (own_buf);
992
fb1e4ffc
DJ
993 /* Grab the annex, offset, and length. */
994 if (decode_xfer_read (own_buf + 20, &annex, &ofs, &len) < 0)
995 {
996 strcpy (own_buf, "E00");
997 return;
998 }
999
1000 /* Now grab the correct annex. */
1001 document = get_features_xml (annex);
1002 if (document == NULL)
23181151
DJ
1003 {
1004 strcpy (own_buf, "E00");
1005 return;
1006 }
1007
1008 total_len = strlen (document);
1009 if (len > PBUFSIZ - 2)
1010 len = PBUFSIZ - 2;
1011
1012 if (ofs > total_len)
1013 write_enn (own_buf);
1014 else if (len < total_len - ofs)
1015 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
1016 len, 1);
1017 else
1018 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
1019 total_len - ofs, 0);
1020
1021 return;
1022 }
1023
255e7678
DJ
1024 if (strncmp ("qXfer:libraries:read:", own_buf, 21) == 0)
1025 {
1026 CORE_ADDR ofs;
1027 unsigned int len, total_len;
1028 char *document, *p;
1029 struct inferior_list_entry *dll_ptr;
1030 char *annex;
1031
2d717e4f
DJ
1032 require_running (own_buf);
1033
255e7678
DJ
1034 /* Reject any annex; grab the offset and length. */
1035 if (decode_xfer_read (own_buf + 21, &annex, &ofs, &len) < 0
1036 || annex[0] != '\0')
1037 {
1038 strcpy (own_buf, "E00");
1039 return;
1040 }
1041
1042 /* Over-estimate the necessary memory. Assume that every character
1043 in the library name must be escaped. */
1044 total_len = 64;
1045 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
1046 total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name);
1047
aef93bd7
DE
1048 document = malloc (total_len);
1049 if (document == NULL)
1050 {
1051 write_enn (own_buf);
1052 return;
1053 }
255e7678
DJ
1054 strcpy (document, "<library-list>\n");
1055 p = document + strlen (document);
1056
1057 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
1058 {
1059 struct dll_info *dll = (struct dll_info *) dll_ptr;
1060 char *name;
1061
1062 strcpy (p, " <library name=\"");
1063 p = p + strlen (p);
1064 name = xml_escape_text (dll->name);
1065 strcpy (p, name);
1066 free (name);
1067 p = p + strlen (p);
1068 strcpy (p, "\"><segment address=\"");
1069 p = p + strlen (p);
1070 sprintf (p, "0x%lx", (long) dll->base_addr);
1071 p = p + strlen (p);
1072 strcpy (p, "\"/></library>\n");
1073 p = p + strlen (p);
1074 }
1075
1076 strcpy (p, "</library-list>\n");
1077
1078 total_len = strlen (document);
1079 if (len > PBUFSIZ - 2)
1080 len = PBUFSIZ - 2;
1081
1082 if (ofs > total_len)
1083 write_enn (own_buf);
1084 else if (len < total_len - ofs)
1085 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
1086 len, 1);
1087 else
1088 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
1089 total_len - ofs, 0);
1090
1091 free (document);
1092 return;
1093 }
1094
07e059b5
VP
1095 if (the_target->qxfer_osdata != NULL
1096 && strncmp ("qXfer:osdata:read:", own_buf, 18) == 0)
1097 {
1098 char *annex;
1099 int n;
1100 unsigned int len;
1101 CORE_ADDR ofs;
1102 unsigned char *workbuf;
1103
1104 strcpy (own_buf, "E00");
1105 if (decode_xfer_read (own_buf + 18, &annex, &ofs, &len) < 0)
78e5cee6 1106 return;
07e059b5 1107 if (len > PBUFSIZ - 2)
78e5cee6 1108 len = PBUFSIZ - 2;
aef93bd7 1109 workbuf = malloc (len + 1);
07e059b5 1110 if (!workbuf)
1b3f6016 1111 return;
07e059b5
VP
1112
1113 n = (*the_target->qxfer_osdata) (annex, workbuf, NULL, ofs, len + 1);
1114 if (n < 0)
78e5cee6 1115 write_enn (own_buf);
07e059b5 1116 else if (n > len)
78e5cee6 1117 *new_packet_len_p = write_qxfer_response (own_buf, workbuf, len, 1);
07e059b5 1118 else
78e5cee6 1119 *new_packet_len_p = write_qxfer_response (own_buf, workbuf, n, 0);
07e059b5
VP
1120
1121 free (workbuf);
1122 return;
1123 }
1124
4aa995e1
PA
1125 if (the_target->qxfer_siginfo != NULL
1126 && strncmp ("qXfer:siginfo:read:", own_buf, 19) == 0)
1127 {
1128 unsigned char *data;
1129 int n;
1130 CORE_ADDR ofs;
1131 unsigned int len;
1132 char *annex;
1133
1134 require_running (own_buf);
1135
1136 /* Reject any annex; grab the offset and length. */
1137 if (decode_xfer_read (own_buf + 19, &annex, &ofs, &len) < 0
1138 || annex[0] != '\0')
1139 {
1140 strcpy (own_buf, "E00");
1141 return;
1142 }
1143
1144 /* Read one extra byte, as an indicator of whether there is
1145 more. */
1146 if (len > PBUFSIZ - 2)
1147 len = PBUFSIZ - 2;
1148 data = malloc (len + 1);
1149 if (!data)
1b3f6016 1150 return;
4aa995e1
PA
1151 n = (*the_target->qxfer_siginfo) (annex, data, NULL, ofs, len + 1);
1152 if (n < 0)
1153 write_enn (own_buf);
1154 else if (n > len)
1155 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
1156 else
1157 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
1158
1159 free (data);
1160 return;
1161 }
1162
1163 if (the_target->qxfer_siginfo != NULL
1164 && strncmp ("qXfer:siginfo:write:", own_buf, 20) == 0)
1165 {
1166 char *annex;
1167 int n;
1168 unsigned int len;
1169 CORE_ADDR ofs;
1170 unsigned char *data;
1171
1172 require_running (own_buf);
1173
1174 strcpy (own_buf, "E00");
1175 data = malloc (packet_len - 19);
1176 if (!data)
1b3f6016 1177 return;
4aa995e1
PA
1178 if (decode_xfer_write (own_buf + 20, packet_len - 20, &annex,
1179 &ofs, &len, data) < 0)
1180 {
1181 free (data);
1182 return;
1183 }
1184
1185 n = (*the_target->qxfer_siginfo)
1186 (annex, NULL, (unsigned const char *)data, ofs, len);
1187 if (n < 0)
1188 write_enn (own_buf);
1189 else
1190 sprintf (own_buf, "%x", n);
1191
1192 free (data);
1193 return;
1194 }
1195
dc146f7c
VP
1196 if (strncmp ("qXfer:threads:read:", own_buf, 19) == 0)
1197 {
1198 unsigned char *data;
1199 int n;
1200 CORE_ADDR ofs;
1201 unsigned int len;
1202 char *annex;
1203
1204 require_running (own_buf);
1205
1206 /* Reject any annex; grab the offset and length. */
1207 if (decode_xfer_read (own_buf + 19, &annex, &ofs, &len) < 0
1208 || annex[0] != '\0')
1209 {
1210 strcpy (own_buf, "E00");
1211 return;
1212 }
1213
1214 /* Read one extra byte, as an indicator of whether there is
1215 more. */
1216 if (len > PBUFSIZ - 2)
1217 len = PBUFSIZ - 2;
1218 data = malloc (len + 1);
1219 if (!data)
1220 return;
1221 n = handle_threads_qxfer (annex, data, ofs, len + 1);
1222 if (n < 0)
1223 write_enn (own_buf);
1224 else if (n > len)
1225 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
1226 else
1227 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
1228
1229 free (data);
1230 return;
1231 }
1232
be2a5f71
DJ
1233 /* Protocol features query. */
1234 if (strncmp ("qSupported", own_buf, 10) == 0
1235 && (own_buf[10] == ':' || own_buf[10] == '\0'))
1236 {
95954743
PA
1237 char *p = &own_buf[10];
1238
1239 /* Process each feature being provided by GDB. The first
1240 feature will follow a ':', and latter features will follow
1241 ';'. */
1242 if (*p == ':')
1243 for (p = strtok (p + 1, ";");
1244 p != NULL;
1245 p = strtok (NULL, ";"))
1246 {
95954743 1247 if (strcmp (p, "multiprocess+") == 0)
cf8fd78b
PA
1248 {
1249 /* GDB supports and wants multi-process support if
1250 possible. */
1251 if (target_supports_multi_process ())
1252 multi_process = 1;
1253 }
95954743
PA
1254 }
1255
89be2091 1256 sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1);
0876f84a 1257
255e7678
DJ
1258 /* We do not have any hook to indicate whether the target backend
1259 supports qXfer:libraries:read, so always report it. */
1260 strcat (own_buf, ";qXfer:libraries:read+");
1261
0876f84a 1262 if (the_target->read_auxv != NULL)
9f2e1e63 1263 strcat (own_buf, ";qXfer:auxv:read+");
2d717e4f 1264
0e7f50da
UW
1265 if (the_target->qxfer_spu != NULL)
1266 strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+");
0876f84a 1267
4aa995e1
PA
1268 if (the_target->qxfer_siginfo != NULL)
1269 strcat (own_buf, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
1270
221c031f
UW
1271 /* We always report qXfer:features:read, as targets may
1272 install XML files on a subsequent call to arch_setup.
1273 If we reported to GDB on startup that we don't support
1274 qXfer:feature:read at all, we will never be re-queried. */
1275 strcat (own_buf, ";qXfer:features:read+");
23181151 1276
a6f3e723
SL
1277 if (transport_is_reliable)
1278 strcat (own_buf, ";QStartNoAckMode+");
07e059b5
VP
1279
1280 if (the_target->qxfer_osdata != NULL)
1b3f6016 1281 strcat (own_buf, ";qXfer:osdata:read+");
07e059b5 1282
cf8fd78b
PA
1283 if (target_supports_multi_process ())
1284 strcat (own_buf, ";multiprocess+");
95954743 1285
bd99dc85
PA
1286 if (target_supports_non_stop ())
1287 strcat (own_buf, ";QNonStop+");
1288
dc146f7c
VP
1289 strcat (own_buf, ";qXfer:threads:read+");
1290
be2a5f71
DJ
1291 return;
1292 }
1293
dae5f5cf
DJ
1294 /* Thread-local storage support. */
1295 if (the_target->get_tls_address != NULL
1296 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
1297 {
1298 char *p = own_buf + 12;
5b1c542e 1299 CORE_ADDR parts[2], address = 0;
dae5f5cf 1300 int i, err;
95954743 1301 ptid_t ptid = null_ptid;
dae5f5cf 1302
2d717e4f
DJ
1303 require_running (own_buf);
1304
dae5f5cf
DJ
1305 for (i = 0; i < 3; i++)
1306 {
1307 char *p2;
1308 int len;
1309
1310 if (p == NULL)
1311 break;
1312
1313 p2 = strchr (p, ',');
1314 if (p2)
1315 {
1316 len = p2 - p;
1317 p2++;
1318 }
1319 else
1320 {
1321 len = strlen (p);
1322 p2 = NULL;
1323 }
1324
5b1c542e 1325 if (i == 0)
95954743 1326 ptid = read_ptid (p, NULL);
5b1c542e
PA
1327 else
1328 decode_address (&parts[i - 1], p, len);
dae5f5cf
DJ
1329 p = p2;
1330 }
1331
1332 if (p != NULL || i < 3)
1333 err = 1;
1334 else
1335 {
e09875d4 1336 struct thread_info *thread = find_thread_ptid (ptid);
dae5f5cf
DJ
1337
1338 if (thread == NULL)
1339 err = 2;
1340 else
5b1c542e 1341 err = the_target->get_tls_address (thread, parts[0], parts[1],
dae5f5cf
DJ
1342 &address);
1343 }
1344
1345 if (err == 0)
1346 {
1347 sprintf (own_buf, "%llx", address);
1348 return;
1349 }
1350 else if (err > 0)
1351 {
1352 write_enn (own_buf);
1353 return;
1354 }
1355
1356 /* Otherwise, pretend we do not understand this packet. */
1357 }
1358
c74d0ad8
DJ
1359 /* Handle "monitor" commands. */
1360 if (strncmp ("qRcmd,", own_buf, 6) == 0)
1361 {
aef93bd7 1362 char *mon = malloc (PBUFSIZ);
c74d0ad8
DJ
1363 int len = strlen (own_buf + 6);
1364
aef93bd7
DE
1365 if (mon == NULL)
1366 {
1367 write_enn (own_buf);
1368 return;
1369 }
1370
d41b6bb4 1371 if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
c74d0ad8
DJ
1372 {
1373 write_enn (own_buf);
1374 free (mon);
1375 return;
1376 }
1377 mon[len / 2] = '\0';
1378
1379 write_ok (own_buf);
1380
cdbfd419
PP
1381 if (the_target->handle_monitor_command == NULL
1382 || (*the_target->handle_monitor_command) (mon) == 0)
1383 /* Default processing. */
1384 handle_monitor_command (mon);
c74d0ad8
DJ
1385
1386 free (mon);
1387 return;
1388 }
1389
08388c79
DE
1390 if (strncmp ("qSearch:memory:", own_buf, sizeof ("qSearch:memory:") - 1) == 0)
1391 {
1392 require_running (own_buf);
1393 handle_search_memory (own_buf, packet_len);
1394 return;
1395 }
1396
95954743
PA
1397 if (strcmp (own_buf, "qAttached") == 0
1398 || strncmp (own_buf, "qAttached:", sizeof ("qAttached:") - 1) == 0)
0b16c5cf 1399 {
95954743
PA
1400 struct process_info *process;
1401
1402 if (own_buf[sizeof ("qAttached") - 1])
1403 {
1404 int pid = strtoul (own_buf + sizeof ("qAttached:") - 1, NULL, 16);
1405 process = (struct process_info *)
1406 find_inferior_id (&all_processes, pid_to_ptid (pid));
1407 }
1408 else
1409 {
1410 require_running (own_buf);
1411 process = current_process ();
1412 }
1413
1414 if (process == NULL)
1415 {
1416 write_enn (own_buf);
1417 return;
1418 }
1419
1420 strcpy (own_buf, process->attached ? "1" : "0");
0b16c5cf
PA
1421 return;
1422 }
1423
ce3a066d
DJ
1424 /* Otherwise we didn't know what packet it was. Say we didn't
1425 understand it. */
1426 own_buf[0] = 0;
1427}
1428
64386c31
DJ
1429/* Parse vCont packets. */
1430void
5b1c542e 1431handle_v_cont (char *own_buf)
64386c31
DJ
1432{
1433 char *p, *q;
1434 int n = 0, i = 0;
2bd7c093 1435 struct thread_resume *resume_info;
95954743 1436 struct thread_resume default_action = {{0}};
64386c31
DJ
1437
1438 /* Count the number of semicolons in the packet. There should be one
1439 for every action. */
1440 p = &own_buf[5];
1441 while (p)
1442 {
1443 n++;
1444 p++;
1445 p = strchr (p, ';');
1446 }
2bd7c093
PA
1447
1448 resume_info = malloc (n * sizeof (resume_info[0]));
aef93bd7
DE
1449 if (resume_info == NULL)
1450 goto err;
64386c31 1451
64386c31 1452 p = &own_buf[5];
64386c31
DJ
1453 while (*p)
1454 {
1455 p++;
1456
64386c31 1457 if (p[0] == 's' || p[0] == 'S')
bd99dc85 1458 resume_info[i].kind = resume_step;
64386c31 1459 else if (p[0] == 'c' || p[0] == 'C')
bd99dc85
PA
1460 resume_info[i].kind = resume_continue;
1461 else if (p[0] == 't')
1462 resume_info[i].kind = resume_stop;
64386c31
DJ
1463 else
1464 goto err;
1465
1466 if (p[0] == 'S' || p[0] == 'C')
1467 {
1468 int sig;
1469 sig = strtol (p + 1, &q, 16);
1470 if (p == q)
1471 goto err;
1472 p = q;
1473
1474 if (!target_signal_to_host_p (sig))
1475 goto err;
1476 resume_info[i].sig = target_signal_to_host (sig);
1477 }
1478 else
1479 {
1480 resume_info[i].sig = 0;
1481 p = p + 1;
1482 }
1483
1484 if (p[0] == 0)
1485 {
95954743 1486 resume_info[i].thread = minus_one_ptid;
64386c31
DJ
1487 default_action = resume_info[i];
1488
1489 /* Note: we don't increment i here, we'll overwrite this entry
1490 the next time through. */
1491 }
1492 else if (p[0] == ':')
1493 {
95954743 1494 ptid_t ptid = read_ptid (p + 1, &q);
a06660f7 1495
64386c31
DJ
1496 if (p == q)
1497 goto err;
1498 p = q;
1499 if (p[0] != ';' && p[0] != 0)
1500 goto err;
1501
95954743 1502 resume_info[i].thread = ptid;
a06660f7 1503
64386c31
DJ
1504 i++;
1505 }
1506 }
1507
2bd7c093
PA
1508 if (i < n)
1509 resume_info[i] = default_action;
64386c31
DJ
1510
1511 /* Still used in occasional places in the backend. */
bd99dc85 1512 if (n == 1
95954743 1513 && !ptid_equal (resume_info[0].thread, minus_one_ptid)
bd99dc85 1514 && resume_info[0].kind != resume_stop)
64386c31
DJ
1515 cont_thread = resume_info[0].thread;
1516 else
95954743 1517 cont_thread = minus_one_ptid;
dc3f8883 1518 set_desired_inferior (0);
64386c31 1519
bd99dc85
PA
1520 if (!non_stop)
1521 enable_async_io ();
1522
2bd7c093 1523 (*the_target->resume) (resume_info, n);
64386c31
DJ
1524
1525 free (resume_info);
1526
bd99dc85
PA
1527 if (non_stop)
1528 write_ok (own_buf);
1529 else
1530 {
95954743 1531 last_ptid = mywait (minus_one_ptid, &last_status, 0, 1);
bd99dc85
PA
1532 prepare_resume_reply (own_buf, last_ptid, &last_status);
1533 disable_async_io ();
1534 }
64386c31
DJ
1535 return;
1536
1537err:
255e7678 1538 write_enn (own_buf);
64386c31
DJ
1539 free (resume_info);
1540 return;
1541}
1542
2d717e4f
DJ
1543/* Attach to a new program. Return 1 if successful, 0 if failure. */
1544int
5b1c542e 1545handle_v_attach (char *own_buf)
2d717e4f
DJ
1546{
1547 int pid;
1548
1549 pid = strtol (own_buf + 8, NULL, 16);
5b1c542e 1550 if (pid != 0 && attach_inferior (pid) == 0)
2d717e4f 1551 {
aeba519e
PA
1552 /* Don't report shared library events after attaching, even if
1553 some libraries are preloaded. GDB will always poll the
1554 library list. Avoids the "stopped by shared library event"
1555 notice on the GDB side. */
1556 dlls_changed = 0;
bd99dc85
PA
1557
1558 if (non_stop)
1559 {
1560 /* In non-stop, we don't send a resume reply. Stop events
1561 will follow up using the normal notification
1562 mechanism. */
1563 write_ok (own_buf);
1564 }
1565 else
1566 prepare_resume_reply (own_buf, last_ptid, &last_status);
1567
2d717e4f
DJ
1568 return 1;
1569 }
1570 else
1571 {
1572 write_enn (own_buf);
1573 return 0;
1574 }
1575}
1576
1577/* Run a new program. Return 1 if successful, 0 if failure. */
1578static int
5b1c542e 1579handle_v_run (char *own_buf)
2d717e4f 1580{
aef93bd7 1581 char *p, *next_p, **new_argv;
2d717e4f
DJ
1582 int i, new_argc;
1583
1584 new_argc = 0;
1585 for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';'))
1586 {
1587 p++;
1588 new_argc++;
1589 }
1590
aef93bd7
DE
1591 new_argv = calloc (new_argc + 2, sizeof (char *));
1592 if (new_argv == NULL)
1593 {
1594 write_enn (own_buf);
1595 return 0;
1596 }
1597
2d717e4f
DJ
1598 i = 0;
1599 for (p = own_buf + strlen ("vRun;"); *p; p = next_p)
1600 {
1601 next_p = strchr (p, ';');
1602 if (next_p == NULL)
1603 next_p = p + strlen (p);
1604
1605 if (i == 0 && p == next_p)
1606 new_argv[i] = NULL;
1607 else
1608 {
aef93bd7 1609 /* FIXME: Fail request if out of memory instead of dying. */
bca929d3 1610 new_argv[i] = xmalloc (1 + (next_p - p) / 2);
2d717e4f
DJ
1611 unhexify (new_argv[i], p, (next_p - p) / 2);
1612 new_argv[i][(next_p - p) / 2] = '\0';
1613 }
1614
1615 if (*next_p)
1616 next_p++;
1617 i++;
1618 }
1619 new_argv[i] = NULL;
1620
1621 if (new_argv[0] == NULL)
1622 {
f142445f
DJ
1623 /* GDB didn't specify a program to run. Use the program from the
1624 last run with the new argument list. */
9b710a42 1625
2d717e4f
DJ
1626 if (program_argv == NULL)
1627 {
aef93bd7 1628 /* FIXME: new_argv memory leak */
2d717e4f
DJ
1629 write_enn (own_buf);
1630 return 0;
1631 }
1632
aef93bd7
DE
1633 new_argv[0] = strdup (program_argv[0]);
1634 if (new_argv[0] == NULL)
1635 {
1636 /* FIXME: new_argv memory leak */
1637 write_enn (own_buf);
1638 return 0;
1b3f6016 1639 }
2d717e4f 1640 }
f142445f 1641
aef93bd7
DE
1642 /* Free the old argv and install the new one. */
1643 freeargv (program_argv);
f142445f 1644 program_argv = new_argv;
2d717e4f 1645
5b1c542e
PA
1646 start_inferior (program_argv);
1647 if (last_status.kind == TARGET_WAITKIND_STOPPED)
2d717e4f 1648 {
5b1c542e 1649 prepare_resume_reply (own_buf, last_ptid, &last_status);
bd99dc85
PA
1650
1651 /* In non-stop, sending a resume reply doesn't set the general
1652 thread, but GDB assumes a vRun sets it (this is so GDB can
1653 query which is the main thread of the new inferior. */
1654 if (non_stop)
1655 general_thread = last_ptid;
1656
2d717e4f
DJ
1657 return 1;
1658 }
1659 else
1660 {
1661 write_enn (own_buf);
1662 return 0;
1663 }
1664}
1665
95954743
PA
1666/* Kill process. Return 1 if successful, 0 if failure. */
1667int
1668handle_v_kill (char *own_buf)
1669{
1670 int pid;
1671 char *p = &own_buf[6];
0f54c268
PM
1672 if (multi_process)
1673 pid = strtol (p, NULL, 16);
1674 else
1675 pid = signal_pid;
95954743
PA
1676 if (pid != 0 && kill_inferior (pid) == 0)
1677 {
1678 last_status.kind = TARGET_WAITKIND_SIGNALLED;
1679 last_status.value.sig = TARGET_SIGNAL_KILL;
1680 last_ptid = pid_to_ptid (pid);
1681 discard_queued_stop_replies (pid);
1682 write_ok (own_buf);
1683 return 1;
1684 }
1685 else
1686 {
1687 write_enn (own_buf);
1688 return 0;
1689 }
1690}
1691
bd99dc85
PA
1692/* Handle a 'vStopped' packet. */
1693static void
1694handle_v_stopped (char *own_buf)
1695{
1696 /* If we're waiting for GDB to acknowledge a pending stop reply,
1697 consider that done. */
1698 if (notif_queue)
1699 {
1700 struct vstop_notif *head;
1701
1702 if (remote_debug)
95954743
PA
1703 fprintf (stderr, "vStopped: acking %s\n",
1704 target_pid_to_str (notif_queue->ptid));
bd99dc85
PA
1705
1706 head = notif_queue;
1707 notif_queue = notif_queue->next;
1708 free (head);
1709 }
1710
1711 /* Push another stop reply, or if there are no more left, an OK. */
1712 send_next_stop_reply (own_buf);
1713}
1714
64386c31
DJ
1715/* Handle all of the extended 'v' packets. */
1716void
5b1c542e 1717handle_v_requests (char *own_buf, int packet_len, int *new_packet_len)
64386c31 1718{
db42f210 1719 if (!disable_packet_vCont)
64386c31 1720 {
db42f210
PA
1721 if (strncmp (own_buf, "vCont;", 6) == 0)
1722 {
1723 require_running (own_buf);
5b1c542e 1724 handle_v_cont (own_buf);
db42f210
PA
1725 return;
1726 }
64386c31 1727
db42f210
PA
1728 if (strncmp (own_buf, "vCont?", 6) == 0)
1729 {
bd99dc85 1730 strcpy (own_buf, "vCont;c;C;s;S;t");
db42f210
PA
1731 return;
1732 }
64386c31
DJ
1733 }
1734
a6b151f1
DJ
1735 if (strncmp (own_buf, "vFile:", 6) == 0
1736 && handle_vFile (own_buf, packet_len, new_packet_len))
1737 return;
1738
2d717e4f
DJ
1739 if (strncmp (own_buf, "vAttach;", 8) == 0)
1740 {
95954743 1741 if (!multi_process && target_running ())
2d717e4f 1742 {
fd96d250
PA
1743 fprintf (stderr, "Already debugging a process\n");
1744 write_enn (own_buf);
1745 return;
2d717e4f 1746 }
5b1c542e 1747 handle_v_attach (own_buf);
2d717e4f
DJ
1748 return;
1749 }
1750
1751 if (strncmp (own_buf, "vRun;", 5) == 0)
1752 {
95954743 1753 if (!multi_process && target_running ())
2d717e4f 1754 {
fd96d250
PA
1755 fprintf (stderr, "Already debugging a process\n");
1756 write_enn (own_buf);
1757 return;
2d717e4f 1758 }
5b1c542e 1759 handle_v_run (own_buf);
2d717e4f
DJ
1760 return;
1761 }
1762
95954743
PA
1763 if (strncmp (own_buf, "vKill;", 6) == 0)
1764 {
1765 if (!target_running ())
1766 {
1767 fprintf (stderr, "No process to kill\n");
1768 write_enn (own_buf);
1769 return;
1770 }
1771 handle_v_kill (own_buf);
1772 return;
1773 }
1774
bd99dc85
PA
1775 if (strncmp (own_buf, "vStopped", 8) == 0)
1776 {
1777 handle_v_stopped (own_buf);
1778 return;
1779 }
1780
64386c31
DJ
1781 /* Otherwise we didn't know what packet it was. Say we didn't
1782 understand it. */
1783 own_buf[0] = 0;
1784 return;
1785}
1786
bd99dc85
PA
1787/* Resume inferior and wait for another event. In non-stop mode,
1788 don't really wait here, but return immediatelly to the event
1789 loop. */
64386c31 1790void
5b1c542e 1791myresume (char *own_buf, int step, int sig)
64386c31
DJ
1792{
1793 struct thread_resume resume_info[2];
1794 int n = 0;
2bd7c093 1795 int valid_cont_thread;
a20d5e98
DJ
1796
1797 set_desired_inferior (0);
64386c31 1798
95954743
PA
1799 valid_cont_thread = (!ptid_equal (cont_thread, null_ptid)
1800 && !ptid_equal (cont_thread, minus_one_ptid));
2bd7c093
PA
1801
1802 if (step || sig || valid_cont_thread)
64386c31
DJ
1803 {
1804 resume_info[0].thread
1805 = ((struct inferior_list_entry *) current_inferior)->id;
bd99dc85
PA
1806 if (step)
1807 resume_info[0].kind = resume_step;
1808 else
1809 resume_info[0].kind = resume_continue;
64386c31 1810 resume_info[0].sig = sig;
64386c31
DJ
1811 n++;
1812 }
2bd7c093
PA
1813
1814 if (!valid_cont_thread)
1815 {
95954743 1816 resume_info[n].thread = minus_one_ptid;
bd99dc85 1817 resume_info[n].kind = resume_continue;
2bd7c093
PA
1818 resume_info[n].sig = 0;
1819 n++;
1820 }
64386c31 1821
bd99dc85
PA
1822 if (!non_stop)
1823 enable_async_io ();
1824
2bd7c093 1825 (*the_target->resume) (resume_info, n);
bd99dc85
PA
1826
1827 if (non_stop)
1828 write_ok (own_buf);
1829 else
1830 {
95954743 1831 last_ptid = mywait (minus_one_ptid, &last_status, 0, 1);
bd99dc85
PA
1832 prepare_resume_reply (own_buf, last_ptid, &last_status);
1833 disable_async_io ();
1834 }
1835}
1836
1837/* Callback for for_each_inferior. Make a new stop reply for each
1838 stopped thread. */
1839
95954743
PA
1840static int
1841queue_stop_reply_callback (struct inferior_list_entry *entry, void *arg)
bd99dc85 1842{
95954743 1843 int pid = * (int *) arg;
bd99dc85 1844
95954743
PA
1845 if (pid == -1
1846 || ptid_get_pid (entry->id) == pid)
1847 {
1848 struct target_waitstatus status;
1849
1850 status.kind = TARGET_WAITKIND_STOPPED;
1851 status.value.sig = TARGET_SIGNAL_TRAP;
bd99dc85 1852
95954743
PA
1853 /* Pass the last stop reply back to GDB, but don't notify. */
1854 queue_stop_reply (entry->id, &status);
1855 }
1856
1857 return 0;
64386c31
DJ
1858}
1859
5b1c542e
PA
1860/* Status handler for the '?' packet. */
1861
1862static void
1863handle_status (char *own_buf)
1864{
bd99dc85
PA
1865 struct target_waitstatus status;
1866 status.kind = TARGET_WAITKIND_STOPPED;
1867 status.value.sig = TARGET_SIGNAL_TRAP;
1868
1869 /* In non-stop mode, we must send a stop reply for each stopped
1870 thread. In all-stop mode, just send one for the first stopped
1871 thread we find. */
1872
1873 if (non_stop)
1874 {
95954743
PA
1875 int pid = -1;
1876 discard_queued_stop_replies (pid);
1877 find_inferior (&all_threads, queue_stop_reply_callback, &pid);
bd99dc85
PA
1878
1879 /* The first is sent immediatly. OK is sent if there is no
1880 stopped thread, which is the same handling of the vStopped
1881 packet (by design). */
1882 send_next_stop_reply (own_buf);
1883 }
5b1c542e 1884 else
bd99dc85
PA
1885 {
1886 if (all_threads.head)
1887 prepare_resume_reply (own_buf,
1888 all_threads.head->id, &status);
1889 else
1890 strcpy (own_buf, "W00");
1891 }
5b1c542e
PA
1892}
1893
dd24457d
DJ
1894static void
1895gdbserver_version (void)
1896{
c16158bc 1897 printf ("GNU gdbserver %s%s\n"
6e7ffa39 1898 "Copyright (C) 2010 Free Software Foundation, Inc.\n"
dd24457d
DJ
1899 "gdbserver is free software, covered by the GNU General Public License.\n"
1900 "This gdbserver was configured as \"%s\"\n",
c16158bc 1901 PKGVERSION, version, host_name);
dd24457d
DJ
1902}
1903
0bc68c49 1904static void
c16158bc 1905gdbserver_usage (FILE *stream)
0bc68c49 1906{
c16158bc
JM
1907 fprintf (stream, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
1908 "\tgdbserver [OPTIONS] --attach COMM PID\n"
1909 "\tgdbserver [OPTIONS] --multi COMM\n"
1910 "\n"
1911 "COMM may either be a tty device (for serial debugging), or \n"
1912 "HOST:PORT to listen for a TCP connection.\n"
1913 "\n"
1914 "Options:\n"
62709adf
PA
1915 " --debug Enable general debugging output.\n"
1916 " --remote-debug Enable remote protocol debugging output.\n"
1917 " --version Display version information and exit.\n"
1918 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n");
c16158bc
JM
1919 if (REPORT_BUGS_TO[0] && stream == stdout)
1920 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
0bc68c49
DJ
1921}
1922
db42f210
PA
1923static void
1924gdbserver_show_disableable (FILE *stream)
1925{
1926 fprintf (stream, "Disableable packets:\n"
1927 " vCont \tAll vCont packets\n"
1928 " qC \tQuerying the current thread\n"
1929 " qfThreadInfo\tThread listing\n"
1930 " Tthread \tPassing the thread specifier in the T stop reply packet\n"
1931 " threads \tAll of the above\n");
1932}
1933
1934
2d717e4f
DJ
1935#undef require_running
1936#define require_running(BUF) \
1937 if (!target_running ()) \
1938 { \
1939 write_enn (BUF); \
1940 break; \
1941 }
1942
95954743
PA
1943static int
1944first_thread_of (struct inferior_list_entry *entry, void *args)
1945{
1946 int pid = * (int *) args;
1947
1948 if (ptid_get_pid (entry->id) == pid)
1949 return 1;
1950
1951 return 0;
1952}
1953
1954static void
1955kill_inferior_callback (struct inferior_list_entry *entry)
1956{
1957 struct process_info *process = (struct process_info *) entry;
1958 int pid = ptid_get_pid (process->head.id);
1959
1960 kill_inferior (pid);
1961 discard_queued_stop_replies (pid);
1962}
1963
9f767825
DE
1964/* Callback for for_each_inferior to detach or kill the inferior,
1965 depending on whether we attached to it or not.
1966 We inform the user whether we're detaching or killing the process
1967 as this is only called when gdbserver is about to exit. */
1968
95954743
PA
1969static void
1970detach_or_kill_inferior_callback (struct inferior_list_entry *entry)
1971{
1972 struct process_info *process = (struct process_info *) entry;
1973 int pid = ptid_get_pid (process->head.id);
1974
1975 if (process->attached)
1976 detach_inferior (pid);
1977 else
1978 kill_inferior (pid);
1979
1980 discard_queued_stop_replies (pid);
1981}
1982
9f767825
DE
1983/* for_each_inferior callback for detach_or_kill_for_exit to print
1984 the pids of started inferiors. */
1985
1986static void
1987print_started_pid (struct inferior_list_entry *entry)
1988{
1989 struct process_info *process = (struct process_info *) entry;
1990
1991 if (! process->attached)
1992 {
1993 int pid = ptid_get_pid (process->head.id);
1994 fprintf (stderr, " %d", pid);
1995 }
1996}
1997
1998/* for_each_inferior callback for detach_or_kill_for_exit to print
1999 the pids of attached inferiors. */
2000
2001static void
2002print_attached_pid (struct inferior_list_entry *entry)
2003{
2004 struct process_info *process = (struct process_info *) entry;
2005
2006 if (process->attached)
2007 {
2008 int pid = ptid_get_pid (process->head.id);
2009 fprintf (stderr, " %d", pid);
2010 }
2011}
2012
2013/* Call this when exiting gdbserver with possible inferiors that need
2014 to be killed or detached from. */
2015
2016static void
2017detach_or_kill_for_exit (void)
2018{
2019 /* First print a list of the inferiors we will be killing/detaching.
2020 This is to assist the user, for example, in case the inferior unexpectedly
2021 dies after we exit: did we screw up or did the inferior exit on its own?
2022 Having this info will save some head-scratching. */
2023
2024 if (have_started_inferiors_p ())
2025 {
2026 fprintf (stderr, "Killing process(es):");
2027 for_each_inferior (&all_processes, print_started_pid);
2028 fprintf (stderr, "\n");
2029 }
2030 if (have_attached_inferiors_p ())
2031 {
2032 fprintf (stderr, "Detaching process(es):");
2033 for_each_inferior (&all_processes, print_attached_pid);
2034 fprintf (stderr, "\n");
2035 }
2036
2037 /* Now we can kill or detach the inferiors. */
2038
2039 for_each_inferior (&all_processes, detach_or_kill_inferior_callback);
2040}
2041
95954743
PA
2042static void
2043join_inferiors_callback (struct inferior_list_entry *entry)
2044{
2045 struct process_info *process = (struct process_info *) entry;
2046
2047 /* If we are attached, then we can exit. Otherwise, we need to hang
2048 around doing nothing, until the child is gone. */
2049 if (!process->attached)
2050 join_inferior (ptid_get_pid (process->head.id));
2051}
2052
c906108c 2053int
da85418c 2054main (int argc, char *argv[])
c906108c 2055{
0729219d
DJ
2056 int bad_attach;
2057 int pid;
2d717e4f
DJ
2058 char *arg_end, *port;
2059 char **next_arg = &argv[1];
2060 int multi_mode = 0;
2061 int attach = 0;
2062 int was_running;
c906108c 2063
2d717e4f 2064 while (*next_arg != NULL && **next_arg == '-')
dd24457d 2065 {
2d717e4f
DJ
2066 if (strcmp (*next_arg, "--version") == 0)
2067 {
2068 gdbserver_version ();
2069 exit (0);
2070 }
2071 else if (strcmp (*next_arg, "--help") == 0)
2072 {
c16158bc 2073 gdbserver_usage (stdout);
2d717e4f
DJ
2074 exit (0);
2075 }
2076 else if (strcmp (*next_arg, "--attach") == 0)
2077 attach = 1;
2078 else if (strcmp (*next_arg, "--multi") == 0)
2079 multi_mode = 1;
ccd213ac
DJ
2080 else if (strcmp (*next_arg, "--wrapper") == 0)
2081 {
2082 next_arg++;
2083
2084 wrapper_argv = next_arg;
2085 while (*next_arg != NULL && strcmp (*next_arg, "--") != 0)
2086 next_arg++;
2087
2088 if (next_arg == wrapper_argv || *next_arg == NULL)
2089 {
c16158bc 2090 gdbserver_usage (stderr);
ccd213ac
DJ
2091 exit (1);
2092 }
2093
2094 /* Consume the "--". */
2095 *next_arg = NULL;
2096 }
2d717e4f
DJ
2097 else if (strcmp (*next_arg, "--debug") == 0)
2098 debug_threads = 1;
62709adf
PA
2099 else if (strcmp (*next_arg, "--remote-debug") == 0)
2100 remote_debug = 1;
db42f210
PA
2101 else if (strcmp (*next_arg, "--disable-packet") == 0)
2102 {
2103 gdbserver_show_disableable (stdout);
2104 exit (0);
2105 }
2106 else if (strncmp (*next_arg,
2107 "--disable-packet=",
2108 sizeof ("--disable-packet=") - 1) == 0)
2109 {
2110 char *packets, *tok;
2111
2112 packets = *next_arg += sizeof ("--disable-packet=") - 1;
2113 for (tok = strtok (packets, ",");
2114 tok != NULL;
2115 tok = strtok (NULL, ","))
2116 {
2117 if (strcmp ("vCont", tok) == 0)
2118 disable_packet_vCont = 1;
2119 else if (strcmp ("Tthread", tok) == 0)
2120 disable_packet_Tthread = 1;
2121 else if (strcmp ("qC", tok) == 0)
2122 disable_packet_qC = 1;
2123 else if (strcmp ("qfThreadInfo", tok) == 0)
2124 disable_packet_qfThreadInfo = 1;
2125 else if (strcmp ("threads", tok) == 0)
2126 {
2127 disable_packet_vCont = 1;
2128 disable_packet_Tthread = 1;
2129 disable_packet_qC = 1;
2130 disable_packet_qfThreadInfo = 1;
2131 }
2132 else
2133 {
2134 fprintf (stderr, "Don't know how to disable \"%s\".\n\n",
2135 tok);
2136 gdbserver_show_disableable (stderr);
2137 exit (1);
2138 }
2139 }
2140 }
2d717e4f
DJ
2141 else
2142 {
2143 fprintf (stderr, "Unknown argument: %s\n", *next_arg);
2144 exit (1);
2145 }
dd24457d 2146
2d717e4f
DJ
2147 next_arg++;
2148 continue;
dd24457d
DJ
2149 }
2150
c5aa993b 2151 if (setjmp (toplevel))
c906108c 2152 {
c5aa993b
JM
2153 fprintf (stderr, "Exiting\n");
2154 exit (1);
c906108c
SS
2155 }
2156
2d717e4f
DJ
2157 port = *next_arg;
2158 next_arg++;
2159 if (port == NULL || (!attach && !multi_mode && *next_arg == NULL))
2160 {
c16158bc 2161 gdbserver_usage (stderr);
2d717e4f
DJ
2162 exit (1);
2163 }
2164
0729219d
DJ
2165 bad_attach = 0;
2166 pid = 0;
2d717e4f
DJ
2167
2168 /* --attach used to come after PORT, so allow it there for
2169 compatibility. */
2170 if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0)
45b7b345 2171 {
2d717e4f
DJ
2172 attach = 1;
2173 next_arg++;
45b7b345
DJ
2174 }
2175
2d717e4f
DJ
2176 if (attach
2177 && (*next_arg == NULL
2178 || (*next_arg)[0] == '\0'
2179 || (pid = strtoul (*next_arg, &arg_end, 0)) == 0
2180 || *arg_end != '\0'
2181 || next_arg[1] != NULL))
2182 bad_attach = 1;
2183
2184 if (bad_attach)
dd24457d 2185 {
c16158bc 2186 gdbserver_usage (stderr);
dd24457d
DJ
2187 exit (1);
2188 }
c906108c 2189
95954743 2190 initialize_inferiors ();
a20d5e98 2191 initialize_async_io ();
4ce44c66
JM
2192 initialize_low ();
2193
bca929d3
DE
2194 own_buf = xmalloc (PBUFSIZ + 1);
2195 mem_buf = xmalloc (PBUFSIZ);
0a30fbc4 2196
2d717e4f 2197 if (pid == 0 && *next_arg != NULL)
45b7b345 2198 {
2d717e4f
DJ
2199 int i, n;
2200
2201 n = argc - (next_arg - argv);
bca929d3 2202 program_argv = xmalloc (sizeof (char *) * (n + 1));
2d717e4f 2203 for (i = 0; i < n; i++)
bca929d3 2204 program_argv[i] = xstrdup (next_arg[i]);
2d717e4f
DJ
2205 program_argv[i] = NULL;
2206
45b7b345 2207 /* Wait till we are at first instruction in program. */
5b1c542e 2208 start_inferior (program_argv);
c906108c 2209
c588c53c
MS
2210 /* We are now (hopefully) stopped at the first instruction of
2211 the target process. This assumes that the target process was
2212 successfully created. */
45b7b345 2213 }
2d717e4f
DJ
2214 else if (pid != 0)
2215 {
5b1c542e 2216 if (attach_inferior (pid) == -1)
2d717e4f
DJ
2217 error ("Attaching not supported on this target");
2218
2219 /* Otherwise succeeded. */
2220 }
45b7b345
DJ
2221 else
2222 {
5b1c542e
PA
2223 last_status.kind = TARGET_WAITKIND_EXITED;
2224 last_status.value.integer = 0;
95954743 2225 last_ptid = minus_one_ptid;
45b7b345 2226 }
c906108c 2227
311de423
PA
2228 /* Don't report shared library events on the initial connection,
2229 even if some libraries are preloaded. Avoids the "stopped by
2230 shared library event" notice on gdb side. */
2231 dlls_changed = 0;
2232
8264bb58
DJ
2233 if (setjmp (toplevel))
2234 {
9f767825 2235 detach_or_kill_for_exit ();
8264bb58
DJ
2236 exit (1);
2237 }
2238
5b1c542e
PA
2239 if (last_status.kind == TARGET_WAITKIND_EXITED
2240 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
2d717e4f
DJ
2241 was_running = 0;
2242 else
2243 was_running = 1;
2244
2245 if (!was_running && !multi_mode)
c588c53c 2246 {
2d717e4f 2247 fprintf (stderr, "No program to debug. GDBserver exiting.\n");
c588c53c
MS
2248 exit (1);
2249 }
2250
c906108c
SS
2251 while (1)
2252 {
a6f3e723 2253 noack_mode = 0;
95954743 2254 multi_process = 0;
bd99dc85
PA
2255 non_stop = 0;
2256
2d717e4f 2257 remote_open (port);
c906108c 2258
2d717e4f
DJ
2259 if (setjmp (toplevel) != 0)
2260 {
2261 /* An error occurred. */
2262 if (response_needed)
2263 {
2264 write_enn (own_buf);
2265 putpkt (own_buf);
2266 }
2267 }
2268
bd99dc85
PA
2269 /* Wait for events. This will return when all event sources are
2270 removed from the event loop. */
2271 start_event_loop ();
2272
2273 /* If an exit was requested (using the "monitor exit" command),
2274 terminate now. The only other way to get here is for
2275 getpkt to fail; close the connection and reopen it at the
2276 top of the loop. */
2277
2278 if (exit_requested)
c906108c 2279 {
9f767825 2280 detach_or_kill_for_exit ();
bd99dc85
PA
2281 exit (0);
2282 }
2283 else
2284 fprintf (stderr, "Remote side has terminated connection. "
2285 "GDBserver will reopen the connection.\n");
2286 }
2287}
01f9e8fa 2288
bd99dc85
PA
2289/* Event loop callback that handles a serial event. The first byte in
2290 the serial buffer gets us here. We expect characters to arrive at
2291 a brisk pace, so we read the rest of the packet with a blocking
2292 getpkt call. */
01f9e8fa 2293
bd99dc85
PA
2294static void
2295process_serial_event (void)
2296{
2297 char ch;
2298 int i = 0;
2299 int signal;
2300 unsigned int len;
2301 CORE_ADDR mem_addr;
95954743 2302 int pid;
bd99dc85
PA
2303 unsigned char sig;
2304 int packet_len;
2305 int new_packet_len = -1;
2306
2307 /* Used to decide when gdbserver should exit in
2308 multi-mode/remote. */
2309 static int have_ran = 0;
2310
2311 if (!have_ran)
2312 have_ran = target_running ();
2313
2314 disable_async_io ();
2315
2316 response_needed = 0;
2317 packet_len = getpkt (own_buf);
2318 if (packet_len <= 0)
2319 {
2320 target_async (0);
2321 remote_close ();
2322 return;
2323 }
2324 response_needed = 1;
2325
2326 i = 0;
2327 ch = own_buf[i++];
2328 switch (ch)
2329 {
2330 case 'q':
2331 handle_query (own_buf, packet_len, &new_packet_len);
2332 break;
2333 case 'Q':
2334 handle_general_set (own_buf);
2335 break;
2336 case 'D':
2337 require_running (own_buf);
95954743
PA
2338
2339 if (multi_process)
2340 {
2341 i++; /* skip ';' */
2342 pid = strtol (&own_buf[i], NULL, 16);
2343 }
2344 else
2345 pid =
2346 ptid_get_pid (((struct inferior_list_entry *) current_inferior)->id);
2347
2348 fprintf (stderr, "Detaching from process %d\n", pid);
2349 if (detach_inferior (pid) != 0)
bd99dc85
PA
2350 write_enn (own_buf);
2351 else
2352 {
95954743 2353 discard_queued_stop_replies (pid);
bd99dc85
PA
2354 write_ok (own_buf);
2355
2356 if (extended_protocol)
c906108c 2357 {
bd99dc85
PA
2358 /* Treat this like a normal program exit. */
2359 last_status.kind = TARGET_WAITKIND_EXITED;
2360 last_status.value.integer = 0;
95954743 2361 last_ptid = pid_to_ptid (pid);
2d717e4f 2362
bd99dc85
PA
2363 current_inferior = NULL;
2364 }
2365 else
2366 {
2367 putpkt (own_buf);
2368 remote_close ();
2369
2370 /* If we are attached, then we can exit. Otherwise, we
2371 need to hang around doing nothing, until the child is
2372 gone. */
95954743
PA
2373 for_each_inferior (&all_processes,
2374 join_inferiors_callback);
bd99dc85
PA
2375 exit (0);
2376 }
2377 }
2378 break;
2379 case '!':
2380 extended_protocol = 1;
2381 write_ok (own_buf);
2382 break;
2383 case '?':
2384 handle_status (own_buf);
2385 break;
2386 case 'H':
2387 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
2388 {
95954743
PA
2389 ptid_t gdb_id, thread_id;
2390 int pid;
bd99dc85
PA
2391
2392 require_running (own_buf);
95954743
PA
2393
2394 gdb_id = read_ptid (&own_buf[2], NULL);
2395
2396 pid = ptid_get_pid (gdb_id);
2397
2398 if (ptid_equal (gdb_id, null_ptid)
2399 || ptid_equal (gdb_id, minus_one_ptid))
2400 thread_id = null_ptid;
2401 else if (pid != 0
2402 && ptid_equal (pid_to_ptid (pid),
2403 gdb_id))
2404 {
2405 struct thread_info *thread =
2406 (struct thread_info *) find_inferior (&all_threads,
2407 first_thread_of,
2408 &pid);
2409 if (!thread)
2410 {
2411 write_enn (own_buf);
2412 break;
2413 }
2414
2415 thread_id = ((struct inferior_list_entry *)thread)->id;
2416 }
bd99dc85
PA
2417 else
2418 {
2419 thread_id = gdb_id_to_thread_id (gdb_id);
95954743 2420 if (ptid_equal (thread_id, null_ptid))
c906108c 2421 {
a06660f7 2422 write_enn (own_buf);
c906108c
SS
2423 break;
2424 }
c906108c
SS
2425 }
2426
bd99dc85 2427 if (own_buf[1] == 'g')
c906108c 2428 {
95954743 2429 if (ptid_equal (thread_id, null_ptid))
c906108c 2430 {
bd99dc85
PA
2431 /* GDB is telling us to choose any thread. Check if
2432 the currently selected thread is still valid. If
2433 it is not, select the first available. */
2434 struct thread_info *thread =
2435 (struct thread_info *) find_inferior_id (&all_threads,
2436 general_thread);
2437 if (thread == NULL)
2438 thread_id = all_threads.head->id;
c906108c 2439 }
bd99dc85
PA
2440
2441 general_thread = thread_id;
2442 set_desired_inferior (1);
c906108c 2443 }
bd99dc85
PA
2444 else if (own_buf[1] == 'c')
2445 cont_thread = thread_id;
2446 else if (own_buf[1] == 's')
2447 step_thread = thread_id;
c906108c 2448
bd99dc85
PA
2449 write_ok (own_buf);
2450 }
2451 else
2452 {
2453 /* Silently ignore it so that gdb can extend the protocol
2454 without compatibility headaches. */
2455 own_buf[0] = '\0';
2d717e4f 2456 }
bd99dc85
PA
2457 break;
2458 case 'g':
442ea881
PA
2459 {
2460 struct regcache *regcache;
2461
2462 require_running (own_buf);
2463 set_desired_inferior (1);
2464 regcache = get_thread_regcache (current_inferior, 1);
2465 registers_to_string (regcache, own_buf);
2466 }
bd99dc85
PA
2467 break;
2468 case 'G':
442ea881
PA
2469 {
2470 struct regcache *regcache;
2471
2472 require_running (own_buf);
2473 set_desired_inferior (1);
2474 regcache = get_thread_regcache (current_inferior, 1);
2475 registers_from_string (regcache, &own_buf[1]);
2476 write_ok (own_buf);
2477 }
bd99dc85
PA
2478 break;
2479 case 'm':
2480 require_running (own_buf);
2481 decode_m_packet (&own_buf[1], &mem_addr, &len);
2482 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
2483 convert_int_to_ascii (mem_buf, own_buf, len);
2484 else
2485 write_enn (own_buf);
2486 break;
2487 case 'M':
2488 require_running (own_buf);
2489 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
2490 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
2491 write_ok (own_buf);
2492 else
2493 write_enn (own_buf);
2494 break;
2495 case 'X':
2496 require_running (own_buf);
2497 if (decode_X_packet (&own_buf[1], packet_len - 1,
2498 &mem_addr, &len, mem_buf) < 0
2499 || write_inferior_memory (mem_addr, mem_buf, len) != 0)
2500 write_enn (own_buf);
2501 else
2502 write_ok (own_buf);
2503 break;
2504 case 'C':
2505 require_running (own_buf);
2506 convert_ascii_to_int (own_buf + 1, &sig, 1);
2507 if (target_signal_to_host_p (sig))
2508 signal = target_signal_to_host (sig);
2509 else
2510 signal = 0;
2511 myresume (own_buf, 0, signal);
2512 break;
2513 case 'S':
2514 require_running (own_buf);
2515 convert_ascii_to_int (own_buf + 1, &sig, 1);
2516 if (target_signal_to_host_p (sig))
2517 signal = target_signal_to_host (sig);
2518 else
2519 signal = 0;
2520 myresume (own_buf, 1, signal);
2521 break;
2522 case 'c':
2523 require_running (own_buf);
2524 signal = 0;
2525 myresume (own_buf, 0, signal);
2526 break;
2527 case 's':
2528 require_running (own_buf);
2529 signal = 0;
2530 myresume (own_buf, 1, signal);
2531 break;
c6314022
AR
2532 case 'Z': /* insert_ ... */
2533 /* Fallthrough. */
2534 case 'z': /* remove_ ... */
bd99dc85
PA
2535 {
2536 char *lenptr;
2537 char *dataptr;
2538 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
2539 int len = strtol (lenptr + 1, &dataptr, 16);
2540 char type = own_buf[1];
c6314022 2541 int res;
d993e290 2542 const int insert = ch == 'Z';
c6314022 2543
d993e290
PA
2544 /* Default to unrecognized/unsupported. */
2545 res = 1;
2546 switch (type)
2547 {
2548 case '0': /* software-breakpoint */
2549 case '1': /* hardware-breakpoint */
2550 case '2': /* write watchpoint */
2551 case '3': /* read watchpoint */
2552 case '4': /* access watchpoint */
2553 require_running (own_buf);
2554 if (insert && the_target->insert_point != NULL)
2555 res = (*the_target->insert_point) (type, addr, len);
2556 else if (!insert && the_target->remove_point != NULL)
2557 res = (*the_target->remove_point) (type, addr, len);
2558 break;
2559 default:
2560 break;
2561 }
bd99dc85 2562
c6314022
AR
2563 if (res == 0)
2564 write_ok (own_buf);
2565 else if (res == 1)
2566 /* Unsupported. */
2567 own_buf[0] = '\0';
bd99dc85 2568 else
c6314022 2569 write_enn (own_buf);
bd99dc85
PA
2570 break;
2571 }
2572 case 'k':
2573 response_needed = 0;
2574 if (!target_running ())
95954743
PA
2575 /* The packet we received doesn't make sense - but we can't
2576 reply to it, either. */
bd99dc85 2577 return;
c906108c 2578
95954743
PA
2579 fprintf (stderr, "Killing all inferiors\n");
2580 for_each_inferior (&all_processes, kill_inferior_callback);
c906108c 2581
bd99dc85
PA
2582 /* When using the extended protocol, we wait with no program
2583 running. The traditional protocol will exit instead. */
2584 if (extended_protocol)
2585 {
2586 last_status.kind = TARGET_WAITKIND_EXITED;
2587 last_status.value.sig = TARGET_SIGNAL_KILL;
2588 return;
2589 }
2590 else
c906108c 2591 {
c906108c 2592 exit (0);
bd99dc85
PA
2593 break;
2594 }
2595 case 'T':
2596 {
95954743 2597 ptid_t gdb_id, thread_id;
bd99dc85
PA
2598
2599 require_running (own_buf);
95954743
PA
2600
2601 gdb_id = read_ptid (&own_buf[1], NULL);
bd99dc85 2602 thread_id = gdb_id_to_thread_id (gdb_id);
95954743 2603 if (ptid_equal (thread_id, null_ptid))
bd99dc85
PA
2604 {
2605 write_enn (own_buf);
2606 break;
2607 }
2608
2609 if (mythread_alive (thread_id))
2610 write_ok (own_buf);
2611 else
2612 write_enn (own_buf);
2613 }
2614 break;
2615 case 'R':
2616 response_needed = 0;
2617
2618 /* Restarting the inferior is only supported in the extended
2619 protocol. */
2620 if (extended_protocol)
2621 {
2622 if (target_running ())
95954743
PA
2623 for_each_inferior (&all_processes,
2624 kill_inferior_callback);
bd99dc85
PA
2625 fprintf (stderr, "GDBserver restarting\n");
2626
2627 /* Wait till we are at 1st instruction in prog. */
2628 if (program_argv != NULL)
2629 start_inferior (program_argv);
2630 else
2631 {
2632 last_status.kind = TARGET_WAITKIND_EXITED;
2633 last_status.value.sig = TARGET_SIGNAL_KILL;
2634 }
2635 return;
c906108c
SS
2636 }
2637 else
2638 {
bd99dc85
PA
2639 /* It is a request we don't understand. Respond with an
2640 empty packet so that gdb knows that we don't support this
2641 request. */
2642 own_buf[0] = '\0';
2643 break;
2644 }
2645 case 'v':
2646 /* Extended (long) request. */
2647 handle_v_requests (own_buf, packet_len, &new_packet_len);
2648 break;
2649
2650 default:
2651 /* It is a request we don't understand. Respond with an empty
2652 packet so that gdb knows that we don't support this
2653 request. */
2654 own_buf[0] = '\0';
2655 break;
2656 }
2657
2658 if (new_packet_len != -1)
2659 putpkt_binary (own_buf, new_packet_len);
2660 else
2661 putpkt (own_buf);
2662
2663 response_needed = 0;
2664
2665 if (!extended_protocol && have_ran && !target_running ())
2666 {
2667 /* In non-stop, defer exiting until GDB had a chance to query
2668 the whole vStopped list (until it gets an OK). */
2669 if (!notif_queue)
2670 {
2671 fprintf (stderr, "GDBserver exiting\n");
c906108c 2672 remote_close ();
bd99dc85 2673 exit (0);
c906108c
SS
2674 }
2675 }
2676}
bd99dc85
PA
2677
2678/* Event-loop callback for serial events. */
2679
2680void
2681handle_serial_event (int err, gdb_client_data client_data)
2682{
2683 if (debug_threads)
2684 fprintf (stderr, "handling possible serial event\n");
2685
2686 /* Really handle it. */
2687 process_serial_event ();
2688
2689 /* Be sure to not change the selected inferior behind GDB's back.
2690 Important in the non-stop mode asynchronous protocol. */
2691 set_desired_inferior (1);
2692}
2693
2694/* Event-loop callback for target events. */
2695
2696void
2697handle_target_event (int err, gdb_client_data client_data)
2698{
2699 if (debug_threads)
2700 fprintf (stderr, "handling possible target event\n");
2701
95954743
PA
2702 last_ptid = mywait (minus_one_ptid, &last_status,
2703 TARGET_WNOHANG, 1);
bd99dc85
PA
2704
2705 if (last_status.kind != TARGET_WAITKIND_IGNORE)
2706 {
2707 /* Something interesting. Tell GDB about it. */
2708 push_event (last_ptid, &last_status);
2709 }
2710
2711 /* Be sure to not change the selected inferior behind GDB's back.
2712 Important in the non-stop mode asynchronous protocol. */
2713 set_desired_inferior (1);
2714}
This page took 1.161309 seconds and 4 git commands to generate.