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