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