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