Set proc->priv->new_inferior out of linux_add_process
[deliverable/binutils-gdb.git] / gdb / gdbserver / server.c
CommitLineData
c906108c 1/* Main code for remote server for GDB.
32d0add0 2 Copyright (C) 1989-2015 Free Software Foundation, Inc.
c906108c 3
c5aa993b 4 This file is part of GDB.
c906108c 5
c5aa993b
JM
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
a9762ec7 8 the Free Software Foundation; either version 3 of the License, or
c5aa993b 9 (at your option) any later version.
c906108c 10
c5aa993b
JM
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
c906108c 15
c5aa993b 16 You should have received a copy of the GNU General Public License
a9762ec7 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
18
19#include "server.h"
623b6bdf 20#include "gdbthread.h"
d1feda86 21#include "agent.h"
14a00470 22#include "notif.h"
3aee8918 23#include "tdesc.h"
9c3d6531 24#include "rsp-low.h"
c906108c 25
87ce2a04 26#include <ctype.h>
a9fa9f7d 27#include <unistd.h>
68070c10 28#if HAVE_SIGNAL_H
a9fa9f7d 29#include <signal.h>
68070c10 30#endif
87ce2a04 31#include "gdb_vecs.h"
8bdce1ff 32#include "gdb_wait.h"
9accd112 33#include "btrace-common.h"
602e3198 34#include "filestuff.h"
c144c7a0 35#include "tracepoint.h"
799cdc37 36#include "dll.h"
533b0600 37#include "hostio.h"
a9fa9f7d 38
122f36ef
PA
39/* The thread set with an `Hc' packet. `Hc' is deprecated in favor of
40 `vCont'. Note the multi-process extensions made `vCont' a
41 requirement, so `Hc pPID.TID' is pretty much undefined. So
42 CONT_THREAD can be null_ptid for no `Hc' thread, minus_one_ptid for
43 resuming all threads of the process (again, `Hc' isn't used for
40e91bc7 44 multi-process), or a specific thread ptid_t. */
95954743 45ptid_t cont_thread;
122f36ef
PA
46
47/* The thread set with an `Hg' packet. */
95954743 48ptid_t general_thread;
5b1c542e 49
0d62e5e8
DJ
50int server_waiting;
51
2d717e4f 52static int extended_protocol;
2d717e4f
DJ
53static int response_needed;
54static int exit_requested;
55
03f2bd59
JK
56/* --once: Exit after the first connection has closed. */
57int run_once;
58
95954743 59int multi_process;
89245bc0
DB
60int report_fork_events;
61int report_vfork_events;
bd99dc85 62int non_stop;
1ec68e26
PA
63int swbreak_feature;
64int hwbreak_feature;
bd99dc85 65
03583c20
UW
66/* Whether we should attempt to disable the operating system's address
67 space randomization feature before starting an inferior. */
68int disable_randomization = 1;
69
ccd213ac 70static char **program_argv, **wrapper_argv;
2d717e4f 71
a493e3e2
PA
72int pass_signals[GDB_SIGNAL_LAST];
73int program_signals[GDB_SIGNAL_LAST];
9b224c5e 74int program_signals_p;
89be2091 75
a9fa9f7d
DJ
76/* The PID of the originally created or attached inferior. Used to
77 send signals to the process when GDB sends us an asynchronous interrupt
78 (user hitting Control-C in the client), and to wait for the child to exit
79 when no longer debugging it. */
80
a1928bad 81unsigned long signal_pid;
a9fa9f7d 82
290fadea
RS
83#ifdef SIGTTOU
84/* A file descriptor for the controlling terminal. */
85int terminal_fd;
86
87/* TERMINAL_FD's original foreground group. */
88pid_t old_foreground_pgrp;
89
90/* Hand back terminal ownership to the original foreground group. */
91
92static void
93restore_old_foreground_pgrp (void)
94{
95 tcsetpgrp (terminal_fd, old_foreground_pgrp);
96}
97#endif
98
ec56be1b
PA
99/* Set if you want to disable optional thread related packets support
100 in gdbserver, for the sake of testing GDB against stubs that don't
101 support them. */
102int disable_packet_vCont;
103int disable_packet_Tthread;
104int disable_packet_qC;
105int disable_packet_qfThreadInfo;
106
5b1c542e
PA
107/* Last status reported to GDB. */
108static struct target_waitstatus last_status;
95954743 109static ptid_t last_ptid;
5b1c542e 110
bd99dc85
PA
111static char *own_buf;
112static unsigned char *mem_buf;
113
14a00470
YQ
114/* A sub-class of 'struct notif_event' for stop, holding information
115 relative to a single stop reply. We keep a queue of these to
116 push to GDB in non-stop mode. */
117
bd99dc85
PA
118struct vstop_notif
119{
14a00470 120 struct notif_event base;
bd99dc85
PA
121
122 /* Thread or process that got the event. */
95954743 123 ptid_t ptid;
bd99dc85
PA
124
125 /* Event info. */
126 struct target_waitstatus status;
127};
128
f4abbc16
MM
129/* The current btrace configuration. This is gdbserver's mirror of GDB's
130 btrace configuration. */
131static struct btrace_config current_btrace_conf;
132
14a00470 133DEFINE_QUEUE_P (notif_event_p);
bd99dc85
PA
134
135/* Put a stop reply to the stop reply queue. */
136
137static void
95954743 138queue_stop_reply (ptid_t ptid, struct target_waitstatus *status)
bd99dc85 139{
14a00470 140 struct vstop_notif *new_notif = xmalloc (sizeof (*new_notif));
bd99dc85 141
bd99dc85
PA
142 new_notif->ptid = ptid;
143 new_notif->status = *status;
144
14a00470 145 notif_event_enque (&notif_stop, (struct notif_event *) new_notif);
bd99dc85
PA
146}
147
14a00470
YQ
148static int
149remove_all_on_match_pid (QUEUE (notif_event_p) *q,
150 QUEUE_ITER (notif_event_p) *iter,
151 struct notif_event *event,
152 void *data)
bd99dc85 153{
14a00470 154 int *pid = data;
d20a8ad9 155
14a00470
YQ
156 if (*pid == -1
157 || ptid_get_pid (((struct vstop_notif *) event)->ptid) == *pid)
bd99dc85 158 {
14a00470
YQ
159 if (q->free_func != NULL)
160 q->free_func (event);
161
162 QUEUE_remove_elem (notif_event_p, q, iter);
bd99dc85 163 }
14a00470
YQ
164
165 return 1;
bd99dc85
PA
166}
167
95954743
PA
168/* Get rid of the currently pending stop replies for PID. If PID is
169 -1, then apply to all processes. */
bd99dc85
PA
170
171static void
95954743 172discard_queued_stop_replies (int pid)
bd99dc85 173{
14a00470
YQ
174 QUEUE_iterate (notif_event_p, notif_stop.queue,
175 remove_all_on_match_pid, &pid);
bd99dc85
PA
176}
177
bd99dc85 178static void
14a00470 179vstop_notif_reply (struct notif_event *event, char *own_buf)
bd99dc85 180{
14a00470
YQ
181 struct vstop_notif *vstop = (struct vstop_notif *) event;
182
183 prepare_resume_reply (own_buf, vstop->ptid, &vstop->status);
bd99dc85
PA
184}
185
14a00470
YQ
186struct notif_server notif_stop =
187{
188 "vStopped", "Stop", NULL, vstop_notif_reply,
189};
190
2d717e4f
DJ
191static int
192target_running (void)
193{
649ebbca 194 return get_first_thread () != NULL;
2d717e4f
DJ
195}
196
fc620387 197static int
5b1c542e 198start_inferior (char **argv)
c906108c 199{
ccd213ac 200 char **new_argv = argv;
2d717e4f 201
ccd213ac
DJ
202 if (wrapper_argv != NULL)
203 {
204 int i, count = 1;
205
206 for (i = 0; wrapper_argv[i] != NULL; i++)
207 count++;
208 for (i = 0; argv[i] != NULL; i++)
209 count++;
210 new_argv = alloca (sizeof (char *) * count);
211 count = 0;
212 for (i = 0; wrapper_argv[i] != NULL; i++)
213 new_argv[count++] = wrapper_argv[i];
214 for (i = 0; argv[i] != NULL; i++)
215 new_argv[count++] = argv[i];
216 new_argv[count] = NULL;
217 }
218
65730243
DE
219 if (debug_threads)
220 {
221 int i;
222 for (i = 0; new_argv[i]; ++i)
87ce2a04
DE
223 debug_printf ("new_argv[%d] = \"%s\"\n", i, new_argv[i]);
224 debug_flush ();
65730243
DE
225 }
226
b80864fb 227#ifdef SIGTTOU
a9fa9f7d
DJ
228 signal (SIGTTOU, SIG_DFL);
229 signal (SIGTTIN, SIG_DFL);
b80864fb 230#endif
a9fa9f7d 231
ccd213ac 232 signal_pid = create_inferior (new_argv[0], new_argv);
0d62e5e8 233
c588c53c
MS
234 /* FIXME: we don't actually know at this point that the create
235 actually succeeded. We won't know that until we wait. */
a1928bad 236 fprintf (stderr, "Process %s created; pid = %ld\n", argv[0],
a9fa9f7d 237 signal_pid);
b80864fb 238 fflush (stderr);
a9fa9f7d 239
b80864fb 240#ifdef SIGTTOU
a9fa9f7d
DJ
241 signal (SIGTTOU, SIG_IGN);
242 signal (SIGTTIN, SIG_IGN);
290fadea
RS
243 terminal_fd = fileno (stderr);
244 old_foreground_pgrp = tcgetpgrp (terminal_fd);
245 tcsetpgrp (terminal_fd, signal_pid);
246 atexit (restore_old_foreground_pgrp);
b80864fb 247#endif
c906108c 248
ccd213ac
DJ
249 if (wrapper_argv != NULL)
250 {
251 struct thread_resume resume_info;
ccd213ac 252
ee47b2f8 253 memset (&resume_info, 0, sizeof (resume_info));
95954743 254 resume_info.thread = pid_to_ptid (signal_pid);
bd99dc85 255 resume_info.kind = resume_continue;
ccd213ac 256 resume_info.sig = 0;
ccd213ac 257
06db92f0 258 last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
bd99dc85 259
eb97750b 260 if (last_status.kind == TARGET_WAITKIND_STOPPED)
ccd213ac 261 {
eb97750b
YQ
262 do
263 {
264 (*the_target->resume) (&resume_info, 1);
265
266 last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
267 if (last_status.kind != TARGET_WAITKIND_STOPPED)
268 break;
269
270 current_thread->last_resume_kind = resume_stop;
271 current_thread->last_status = last_status;
272 }
273 while (last_status.value.sig != GDB_SIGNAL_TRAP);
ccd213ac 274 }
5b1c542e 275 return signal_pid;
ccd213ac
DJ
276 }
277
5b1c542e
PA
278 /* Wait till we are at 1st instruction in program, return new pid
279 (assuming success). */
95954743 280 last_ptid = mywait (pid_to_ptid (signal_pid), &last_status, 0, 0);
5b1c542e 281
d20a8ad9
PA
282 if (last_status.kind != TARGET_WAITKIND_EXITED
283 && last_status.kind != TARGET_WAITKIND_SIGNALLED)
284 {
0bfdf32f
GB
285 current_thread->last_resume_kind = resume_stop;
286 current_thread->last_status = last_status;
d20a8ad9 287 }
80ad801e
PA
288 else
289 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid)));
d20a8ad9 290
5b1c542e 291 return signal_pid;
c906108c
SS
292}
293
45b7b345 294static int
5b1c542e 295attach_inferior (int pid)
45b7b345
DJ
296{
297 /* myattach should return -1 if attaching is unsupported,
298 0 if it succeeded, and call error() otherwise. */
a9fa9f7d 299
45b7b345
DJ
300 if (myattach (pid) != 0)
301 return -1;
302
6910d122 303 fprintf (stderr, "Attached; pid = %d\n", pid);
b80864fb 304 fflush (stderr);
6910d122 305
a9fa9f7d
DJ
306 /* FIXME - It may be that we should get the SIGNAL_PID from the
307 attach function, so that it can be the main thread instead of
308 whichever we were told to attach to. */
309 signal_pid = pid;
310
bd99dc85
PA
311 if (!non_stop)
312 {
95954743 313 last_ptid = mywait (pid_to_ptid (pid), &last_status, 0, 0);
bd99dc85
PA
314
315 /* GDB knows to ignore the first SIGSTOP after attaching to a running
316 process using the "attach" command, but this is different; it's
317 just using "target remote". Pretend it's just starting up. */
318 if (last_status.kind == TARGET_WAITKIND_STOPPED
a493e3e2
PA
319 && last_status.value.sig == GDB_SIGNAL_STOP)
320 last_status.value.sig = GDB_SIGNAL_TRAP;
d20a8ad9 321
0bfdf32f
GB
322 current_thread->last_resume_kind = resume_stop;
323 current_thread->last_status = last_status;
bd99dc85 324 }
9db87ebd 325
45b7b345
DJ
326 return 0;
327}
328
c906108c 329extern int remote_debug;
ce3a066d 330
0876f84a
DJ
331/* Decode a qXfer read request. Return 0 if everything looks OK,
332 or -1 otherwise. */
333
334static int
d08aafef 335decode_xfer_read (char *buf, CORE_ADDR *ofs, unsigned int *len)
0876f84a 336{
d08aafef
PA
337 /* After the read marker and annex, qXfer looks like a
338 traditional 'm' packet. */
339 decode_m_packet (buf, ofs, len);
340
341 return 0;
342}
343
344static int
345decode_xfer (char *buf, char **object, char **rw, char **annex, char **offset)
346{
347 /* Extract and NUL-terminate the object. */
348 *object = buf;
349 while (*buf && *buf != ':')
350 buf++;
351 if (*buf == '\0')
352 return -1;
353 *buf++ = 0;
354
355 /* Extract and NUL-terminate the read/write action. */
356 *rw = buf;
357 while (*buf && *buf != ':')
358 buf++;
359 if (*buf == '\0')
360 return -1;
361 *buf++ = 0;
362
0876f84a
DJ
363 /* Extract and NUL-terminate the annex. */
364 *annex = buf;
365 while (*buf && *buf != ':')
366 buf++;
367 if (*buf == '\0')
368 return -1;
369 *buf++ = 0;
370
d08aafef 371 *offset = buf;
0876f84a
DJ
372 return 0;
373}
374
375/* Write the response to a successful qXfer read. Returns the
376 length of the (binary) data stored in BUF, corresponding
377 to as much of DATA/LEN as we could fit. IS_MORE controls
378 the first character of the response. */
379static int
23181151 380write_qxfer_response (char *buf, const void *data, int len, int is_more)
0876f84a
DJ
381{
382 int out_len;
383
384 if (is_more)
385 buf[0] = 'm';
386 else
387 buf[0] = 'l';
388
124e13d9
SM
389 return remote_escape_output (data, len, 1, (unsigned char *) buf + 1,
390 &out_len, PBUFSIZ - 2) + 1;
0876f84a
DJ
391}
392
f4abbc16 393/* Handle btrace enabling in BTS format. */
9accd112
MM
394
395static const char *
f4abbc16 396handle_btrace_enable_bts (struct thread_info *thread)
9accd112
MM
397{
398 if (thread->btrace != NULL)
399 return "E.Btrace already enabled.";
400
f4abbc16
MM
401 current_btrace_conf.format = BTRACE_FORMAT_BTS;
402 thread->btrace = target_enable_btrace (thread->entry.id,
403 &current_btrace_conf);
9accd112
MM
404 if (thread->btrace == NULL)
405 return "E.Could not enable btrace.";
406
407 return NULL;
408}
409
b20a6524
MM
410/* Handle btrace enabling in Intel(R) Processor Trace format. */
411
412static const char *
413handle_btrace_enable_pt (struct thread_info *thread)
414{
415 if (thread->btrace != NULL)
416 return "E.Btrace already enabled.";
417
418 current_btrace_conf.format = BTRACE_FORMAT_PT;
419 thread->btrace = target_enable_btrace (thread->entry.id,
420 &current_btrace_conf);
421 if (thread->btrace == NULL)
422 return "E.Could not enable btrace.";
423
424 return NULL;
425}
426
9accd112
MM
427/* Handle btrace disabling. */
428
429static const char *
430handle_btrace_disable (struct thread_info *thread)
431{
432
433 if (thread->btrace == NULL)
434 return "E.Branch tracing not enabled.";
435
436 if (target_disable_btrace (thread->btrace) != 0)
437 return "E.Could not disable branch tracing.";
438
439 thread->btrace = NULL;
440 return NULL;
441}
442
443/* Handle the "Qbtrace" packet. */
444
445static int
446handle_btrace_general_set (char *own_buf)
447{
448 struct thread_info *thread;
449 const char *err;
450 char *op;
451
61012eef 452 if (!startswith (own_buf, "Qbtrace:"))
9accd112
MM
453 return 0;
454
455 op = own_buf + strlen ("Qbtrace:");
456
9accd112
MM
457 if (ptid_equal (general_thread, null_ptid)
458 || ptid_equal (general_thread, minus_one_ptid))
459 {
460 strcpy (own_buf, "E.Must select a single thread.");
461 return -1;
462 }
463
464 thread = find_thread_ptid (general_thread);
465 if (thread == NULL)
466 {
467 strcpy (own_buf, "E.No such thread.");
468 return -1;
469 }
470
471 err = NULL;
472
473 if (strcmp (op, "bts") == 0)
f4abbc16 474 err = handle_btrace_enable_bts (thread);
b20a6524
MM
475 else if (strcmp (op, "pt") == 0)
476 err = handle_btrace_enable_pt (thread);
9accd112
MM
477 else if (strcmp (op, "off") == 0)
478 err = handle_btrace_disable (thread);
479 else
b20a6524 480 err = "E.Bad Qbtrace operation. Use bts, pt, or off.";
9accd112
MM
481
482 if (err != 0)
483 strcpy (own_buf, err);
484 else
485 write_ok (own_buf);
486
487 return 1;
488}
489
d33501a5
MM
490/* Handle the "Qbtrace-conf" packet. */
491
492static int
493handle_btrace_conf_general_set (char *own_buf)
494{
495 struct thread_info *thread;
496 char *op;
497
61012eef 498 if (!startswith (own_buf, "Qbtrace-conf:"))
d33501a5
MM
499 return 0;
500
501 op = own_buf + strlen ("Qbtrace-conf:");
502
503 if (ptid_equal (general_thread, null_ptid)
504 || ptid_equal (general_thread, minus_one_ptid))
505 {
506 strcpy (own_buf, "E.Must select a single thread.");
507 return -1;
508 }
509
510 thread = find_thread_ptid (general_thread);
511 if (thread == NULL)
512 {
513 strcpy (own_buf, "E.No such thread.");
514 return -1;
515 }
516
61012eef 517 if (startswith (op, "bts:size="))
d33501a5
MM
518 {
519 unsigned long size;
520 char *endp = NULL;
521
522 errno = 0;
523 size = strtoul (op + strlen ("bts:size="), &endp, 16);
524 if (endp == NULL || *endp != 0 || errno != 0 || size > UINT_MAX)
525 {
526 strcpy (own_buf, "E.Bad size value.");
527 return -1;
528 }
529
530 current_btrace_conf.bts.size = (unsigned int) size;
531 }
b20a6524
MM
532 else if (strncmp (op, "pt:size=", strlen ("pt:size=")) == 0)
533 {
534 unsigned long size;
535 char *endp = NULL;
536
537 errno = 0;
538 size = strtoul (op + strlen ("pt:size="), &endp, 16);
539 if (endp == NULL || *endp != 0 || errno != 0 || size > UINT_MAX)
540 {
541 strcpy (own_buf, "E.Bad size value.");
542 return -1;
543 }
544
545 current_btrace_conf.pt.size = (unsigned int) size;
546 }
d33501a5
MM
547 else
548 {
549 strcpy (own_buf, "E.Bad Qbtrace configuration option.");
550 return -1;
551 }
552
553 write_ok (own_buf);
554 return 1;
555}
556
89be2091 557/* Handle all of the extended 'Q' packets. */
ae1ada35
DE
558
559static void
89be2091
DJ
560handle_general_set (char *own_buf)
561{
61012eef 562 if (startswith (own_buf, "QPassSignals:"))
89be2091 563 {
a493e3e2 564 int numsigs = (int) GDB_SIGNAL_LAST, i;
89be2091
DJ
565 const char *p = own_buf + strlen ("QPassSignals:");
566 CORE_ADDR cursig;
567
568 p = decode_address_to_semicolon (&cursig, p);
569 for (i = 0; i < numsigs; i++)
570 {
571 if (i == cursig)
572 {
573 pass_signals[i] = 1;
574 if (*p == '\0')
575 /* Keep looping, to clear the remaining signals. */
576 cursig = -1;
577 else
578 p = decode_address_to_semicolon (&cursig, p);
579 }
580 else
581 pass_signals[i] = 0;
582 }
583 strcpy (own_buf, "OK");
584 return;
585 }
586
61012eef 587 if (startswith (own_buf, "QProgramSignals:"))
9b224c5e 588 {
a493e3e2 589 int numsigs = (int) GDB_SIGNAL_LAST, i;
9b224c5e
PA
590 const char *p = own_buf + strlen ("QProgramSignals:");
591 CORE_ADDR cursig;
592
593 program_signals_p = 1;
594
595 p = decode_address_to_semicolon (&cursig, p);
596 for (i = 0; i < numsigs; i++)
597 {
598 if (i == cursig)
599 {
600 program_signals[i] = 1;
601 if (*p == '\0')
602 /* Keep looping, to clear the remaining signals. */
603 cursig = -1;
604 else
605 p = decode_address_to_semicolon (&cursig, p);
606 }
607 else
608 program_signals[i] = 0;
609 }
610 strcpy (own_buf, "OK");
611 return;
612 }
613
a6f3e723
SL
614 if (strcmp (own_buf, "QStartNoAckMode") == 0)
615 {
616 if (remote_debug)
617 {
618 fprintf (stderr, "[noack mode enabled]\n");
619 fflush (stderr);
620 }
621
622 noack_mode = 1;
623 write_ok (own_buf);
624 return;
625 }
626
61012eef 627 if (startswith (own_buf, "QNonStop:"))
bd99dc85
PA
628 {
629 char *mode = own_buf + 9;
630 int req = -1;
b2333d22 631 const char *req_str;
bd99dc85
PA
632
633 if (strcmp (mode, "0") == 0)
634 req = 0;
635 else if (strcmp (mode, "1") == 0)
636 req = 1;
637 else
638 {
639 /* We don't know what this mode is, so complain to
640 GDB. */
641 fprintf (stderr, "Unknown non-stop mode requested: %s\n",
642 own_buf);
643 write_enn (own_buf);
644 return;
645 }
646
647 req_str = req ? "non-stop" : "all-stop";
648 if (start_non_stop (req) != 0)
649 {
650 fprintf (stderr, "Setting %s mode failed\n", req_str);
651 write_enn (own_buf);
652 return;
653 }
654
655 non_stop = req;
656
657 if (remote_debug)
658 fprintf (stderr, "[%s mode enabled]\n", req_str);
659
660 write_ok (own_buf);
661 return;
662 }
663
61012eef 664 if (startswith (own_buf, "QDisableRandomization:"))
03583c20
UW
665 {
666 char *packet = own_buf + strlen ("QDisableRandomization:");
667 ULONGEST setting;
668
669 unpack_varlen_hex (packet, &setting);
670 disable_randomization = setting;
671
672 if (remote_debug)
673 {
674 if (disable_randomization)
675 fprintf (stderr, "[address space randomization disabled]\n");
676 else
677 fprintf (stderr, "[address space randomization enabled]\n");
678 }
679
680 write_ok (own_buf);
681 return;
682 }
683
219f2f23
PA
684 if (target_supports_tracepoints ()
685 && handle_tracepoint_general_set (own_buf))
686 return;
687
61012eef 688 if (startswith (own_buf, "QAgent:"))
d1feda86
YQ
689 {
690 char *mode = own_buf + strlen ("QAgent:");
691 int req = 0;
692
693 if (strcmp (mode, "0") == 0)
694 req = 0;
695 else if (strcmp (mode, "1") == 0)
696 req = 1;
697 else
698 {
699 /* We don't know what this value is, so complain to GDB. */
700 sprintf (own_buf, "E.Unknown QAgent value");
701 return;
702 }
703
704 /* Update the flag. */
705 use_agent = req;
706 if (remote_debug)
707 fprintf (stderr, "[%s agent]\n", req ? "Enable" : "Disable");
708 write_ok (own_buf);
709 return;
710 }
711
9accd112
MM
712 if (handle_btrace_general_set (own_buf))
713 return;
714
d33501a5
MM
715 if (handle_btrace_conf_general_set (own_buf))
716 return;
717
89be2091
DJ
718 /* Otherwise we didn't know what packet it was. Say we didn't
719 understand it. */
720 own_buf[0] = 0;
721}
722
23181151 723static const char *
fb1e4ffc 724get_features_xml (const char *annex)
23181151 725{
3aee8918
PA
726 const struct target_desc *desc = current_target_desc ();
727
728 /* `desc->xmltarget' defines what to return when looking for the
729 "target.xml" file. Its contents can either be verbatim XML code
730 (prefixed with a '@') or else the name of the actual XML file to
731 be used in place of "target.xml".
fb1e4ffc 732
9b4b61c8
UW
733 This variable is set up from the auto-generated
734 init_registers_... routine for the current target. */
fb1e4ffc 735
3aee8918 736 if (desc->xmltarget != NULL && strcmp (annex, "target.xml") == 0)
23181151 737 {
3aee8918
PA
738 if (*desc->xmltarget == '@')
739 return desc->xmltarget + 1;
23181151 740 else
3aee8918 741 annex = desc->xmltarget;
23181151
DJ
742 }
743
9b4b61c8
UW
744#ifdef USE_XML
745 {
746 extern const char *const xml_builtin[][2];
747 int i;
748
749 /* Look for the annex. */
750 for (i = 0; xml_builtin[i][0] != NULL; i++)
751 if (strcmp (annex, xml_builtin[i][0]) == 0)
752 break;
753
754 if (xml_builtin[i][0] != NULL)
755 return xml_builtin[i][1];
756 }
757#endif
758
759 return NULL;
23181151
DJ
760}
761
c74d0ad8
DJ
762void
763monitor_show_help (void)
764{
765 monitor_output ("The following monitor commands are supported:\n");
766 monitor_output (" set debug <0|1>\n");
1b3f6016 767 monitor_output (" Enable general debugging messages\n");
aa5ca48f
DE
768 monitor_output (" set debug-hw-points <0|1>\n");
769 monitor_output (" Enable h/w breakpoint/watchpoint debugging messages\n");
c74d0ad8
DJ
770 monitor_output (" set remote-debug <0|1>\n");
771 monitor_output (" Enable remote protocol debugging messages\n");
87ce2a04
DE
772 monitor_output (" set debug-format option1[,option2,...]\n");
773 monitor_output (" Add additional information to debugging messages\n");
774 monitor_output (" Options: all, none");
87ce2a04 775 monitor_output (", timestamp");
87ce2a04 776 monitor_output ("\n");
ecd7ecbc
DJ
777 monitor_output (" exit\n");
778 monitor_output (" Quit GDBserver\n");
c74d0ad8
DJ
779}
780
764880b7
PA
781/* Read trace frame or inferior memory. Returns the number of bytes
782 actually read, zero when no further transfer is possible, and -1 on
783 error. Return of a positive value smaller than LEN does not
784 indicate there's no more to be read, only the end of the transfer.
785 E.g., when GDB reads memory from a traceframe, a first request may
786 be served from a memory block that does not cover the whole request
787 length. A following request gets the rest served from either
788 another block (of the same traceframe) or from the read-only
789 regions. */
219f2f23
PA
790
791static int
90d74c30 792gdb_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
219f2f23 793{
764880b7 794 int res;
90d74c30 795
219f2f23
PA
796 if (current_traceframe >= 0)
797 {
798 ULONGEST nbytes;
799 ULONGEST length = len;
800
801 if (traceframe_read_mem (current_traceframe,
802 memaddr, myaddr, len, &nbytes))
9a13b2fa 803 return -1;
219f2f23 804 /* Data read from trace buffer, we're done. */
764880b7
PA
805 if (nbytes > 0)
806 return nbytes;
219f2f23 807 if (!in_readonly_region (memaddr, length))
764880b7 808 return -1;
219f2f23
PA
809 /* Otherwise we have a valid readonly case, fall through. */
810 /* (assume no half-trace half-real blocks for now) */
811 }
812
764880b7
PA
813 res = prepare_to_access_memory ();
814 if (res == 0)
90d74c30 815 {
764880b7 816 res = read_inferior_memory (memaddr, myaddr, len);
0146f85b 817 done_accessing_memory ();
90d74c30 818
764880b7
PA
819 return res == 0 ? len : -1;
820 }
821 else
822 return -1;
219f2f23
PA
823}
824
825/* Write trace frame or inferior memory. Actually, writing to trace
826 frames is forbidden. */
827
828static int
90d74c30 829gdb_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
219f2f23
PA
830{
831 if (current_traceframe >= 0)
832 return EIO;
833 else
90d74c30
PA
834 {
835 int ret;
836
837 ret = prepare_to_access_memory ();
838 if (ret == 0)
839 {
840 ret = write_inferior_memory (memaddr, myaddr, len);
0146f85b 841 done_accessing_memory ();
90d74c30
PA
842 }
843 return ret;
844 }
219f2f23
PA
845}
846
08388c79
DE
847/* Subroutine of handle_search_memory to simplify it. */
848
849static int
850handle_search_memory_1 (CORE_ADDR start_addr, CORE_ADDR search_space_len,
851 gdb_byte *pattern, unsigned pattern_len,
852 gdb_byte *search_buf,
853 unsigned chunk_size, unsigned search_buf_size,
854 CORE_ADDR *found_addrp)
855{
856 /* Prime the search buffer. */
857
764880b7
PA
858 if (gdb_read_memory (start_addr, search_buf, search_buf_size)
859 != search_buf_size)
08388c79 860 {
b3dc46ff
AB
861 warning ("Unable to access %ld bytes of target "
862 "memory at 0x%lx, halting search.",
863 (long) search_buf_size, (long) start_addr);
08388c79
DE
864 return -1;
865 }
866
867 /* Perform the search.
868
869 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
870 When we've scanned N bytes we copy the trailing bytes to the start and
871 read in another N bytes. */
872
873 while (search_space_len >= pattern_len)
874 {
875 gdb_byte *found_ptr;
876 unsigned nr_search_bytes = (search_space_len < search_buf_size
877 ? search_space_len
878 : search_buf_size);
879
880 found_ptr = memmem (search_buf, nr_search_bytes, pattern, pattern_len);
881
882 if (found_ptr != NULL)
883 {
884 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
885 *found_addrp = found_addr;
886 return 1;
887 }
888
889 /* Not found in this chunk, skip to next chunk. */
890
891 /* Don't let search_space_len wrap here, it's unsigned. */
892 if (search_space_len >= chunk_size)
893 search_space_len -= chunk_size;
894 else
895 search_space_len = 0;
896
897 if (search_space_len >= pattern_len)
898 {
899 unsigned keep_len = search_buf_size - chunk_size;
8a35fb51 900 CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
08388c79
DE
901 int nr_to_read;
902
903 /* Copy the trailing part of the previous iteration to the front
904 of the buffer for the next iteration. */
905 memcpy (search_buf, search_buf + chunk_size, keep_len);
906
907 nr_to_read = (search_space_len - keep_len < chunk_size
908 ? search_space_len - keep_len
909 : chunk_size);
910
90d74c30 911 if (gdb_read_memory (read_addr, search_buf + keep_len,
764880b7 912 nr_to_read) != search_buf_size)
08388c79 913 {
b3dc46ff 914 warning ("Unable to access %ld bytes of target memory "
493e2a69 915 "at 0x%lx, halting search.",
b3dc46ff 916 (long) nr_to_read, (long) read_addr);
08388c79
DE
917 return -1;
918 }
919
920 start_addr += chunk_size;
921 }
922 }
923
924 /* Not found. */
925
926 return 0;
927}
928
929/* Handle qSearch:memory packets. */
930
931static void
932handle_search_memory (char *own_buf, int packet_len)
933{
934 CORE_ADDR start_addr;
935 CORE_ADDR search_space_len;
936 gdb_byte *pattern;
937 unsigned int pattern_len;
938 /* NOTE: also defined in find.c testcase. */
939#define SEARCH_CHUNK_SIZE 16000
940 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
941 /* Buffer to hold memory contents for searching. */
942 gdb_byte *search_buf;
943 unsigned search_buf_size;
944 int found;
945 CORE_ADDR found_addr;
946 int cmd_name_len = sizeof ("qSearch:memory:") - 1;
947
aef93bd7 948 pattern = malloc (packet_len);
08388c79
DE
949 if (pattern == NULL)
950 {
5e1471f5 951 error ("Unable to allocate memory to perform the search");
08388c79
DE
952 strcpy (own_buf, "E00");
953 return;
954 }
955 if (decode_search_memory_packet (own_buf + cmd_name_len,
956 packet_len - cmd_name_len,
957 &start_addr, &search_space_len,
958 pattern, &pattern_len) < 0)
959 {
960 free (pattern);
5e1471f5 961 error ("Error in parsing qSearch:memory packet");
08388c79
DE
962 strcpy (own_buf, "E00");
963 return;
964 }
965
966 search_buf_size = chunk_size + pattern_len - 1;
967
968 /* No point in trying to allocate a buffer larger than the search space. */
969 if (search_space_len < search_buf_size)
970 search_buf_size = search_space_len;
971
aef93bd7 972 search_buf = malloc (search_buf_size);
08388c79
DE
973 if (search_buf == NULL)
974 {
975 free (pattern);
5e1471f5 976 error ("Unable to allocate memory to perform the search");
08388c79
DE
977 strcpy (own_buf, "E00");
978 return;
979 }
980
981 found = handle_search_memory_1 (start_addr, search_space_len,
982 pattern, pattern_len,
983 search_buf, chunk_size, search_buf_size,
984 &found_addr);
985
986 if (found > 0)
987 sprintf (own_buf, "1,%lx", (long) found_addr);
988 else if (found == 0)
989 strcpy (own_buf, "0");
990 else
991 strcpy (own_buf, "E00");
992
993 free (search_buf);
994 free (pattern);
995}
996
2d717e4f
DJ
997#define require_running(BUF) \
998 if (!target_running ()) \
999 { \
1000 write_enn (BUF); \
1001 return; \
1002 }
1003
87ce2a04
DE
1004/* Parse options to --debug-format= and "monitor set debug-format".
1005 ARG is the text after "--debug-format=" or "monitor set debug-format".
1006 IS_MONITOR is non-zero if we're invoked via "monitor set debug-format".
1007 This triggers calls to monitor_output.
1008 The result is NULL if all options were parsed ok, otherwise an error
1009 message which the caller must free.
1010
1011 N.B. These commands affect all debug format settings, they are not
1012 cumulative. If a format is not specified, it is turned off.
1013 However, we don't go to extra trouble with things like
1014 "monitor set debug-format all,none,timestamp".
1015 Instead we just parse them one at a time, in order.
1016
1017 The syntax for "monitor set debug" we support here is not identical
1018 to gdb's "set debug foo on|off" because we also use this function to
1019 parse "--debug-format=foo,bar". */
1020
1021static char *
1022parse_debug_format_options (const char *arg, int is_monitor)
1023{
1024 VEC (char_ptr) *options;
1025 int ix;
1026 char *option;
1027
1028 /* First turn all debug format options off. */
1029 debug_timestamp = 0;
1030
1031 /* First remove leading spaces, for "monitor set debug-format". */
1032 while (isspace (*arg))
1033 ++arg;
1034
1035 options = delim_string_to_char_ptr_vec (arg, ',');
1036
1037 for (ix = 0; VEC_iterate (char_ptr, options, ix, option); ++ix)
1038 {
1039 if (strcmp (option, "all") == 0)
1040 {
1041 debug_timestamp = 1;
1042 if (is_monitor)
1043 monitor_output ("All extra debug format options enabled.\n");
1044 }
1045 else if (strcmp (option, "none") == 0)
1046 {
1047 debug_timestamp = 0;
1048 if (is_monitor)
1049 monitor_output ("All extra debug format options disabled.\n");
1050 }
87ce2a04
DE
1051 else if (strcmp (option, "timestamp") == 0)
1052 {
1053 debug_timestamp = 1;
1054 if (is_monitor)
1055 monitor_output ("Timestamps will be added to debug output.\n");
1056 }
87ce2a04
DE
1057 else if (*option == '\0')
1058 {
1059 /* An empty option, e.g., "--debug-format=foo,,bar", is ignored. */
1060 continue;
1061 }
1062 else
1063 {
1064 char *msg = xstrprintf ("Unknown debug-format argument: \"%s\"\n",
1065 option);
1066
1067 free_char_ptr_vec (options);
1068 return msg;
1069 }
1070 }
1071
1072 free_char_ptr_vec (options);
1073 return NULL;
1074}
1075
cdbfd419
PP
1076/* Handle monitor commands not handled by target-specific handlers. */
1077
1078static void
d73f2619 1079handle_monitor_command (char *mon, char *own_buf)
cdbfd419
PP
1080{
1081 if (strcmp (mon, "set debug 1") == 0)
1082 {
1083 debug_threads = 1;
1084 monitor_output ("Debug output enabled.\n");
1085 }
1086 else if (strcmp (mon, "set debug 0") == 0)
1087 {
1088 debug_threads = 0;
1089 monitor_output ("Debug output disabled.\n");
1090 }
1091 else if (strcmp (mon, "set debug-hw-points 1") == 0)
1092 {
c5e92cca 1093 show_debug_regs = 1;
cdbfd419
PP
1094 monitor_output ("H/W point debugging output enabled.\n");
1095 }
1096 else if (strcmp (mon, "set debug-hw-points 0") == 0)
1097 {
c5e92cca 1098 show_debug_regs = 0;
cdbfd419
PP
1099 monitor_output ("H/W point debugging output disabled.\n");
1100 }
1101 else if (strcmp (mon, "set remote-debug 1") == 0)
1102 {
1103 remote_debug = 1;
1104 monitor_output ("Protocol debug output enabled.\n");
1105 }
1106 else if (strcmp (mon, "set remote-debug 0") == 0)
1107 {
1108 remote_debug = 0;
1109 monitor_output ("Protocol debug output disabled.\n");
1110 }
61012eef 1111 else if (startswith (mon, "set debug-format "))
87ce2a04
DE
1112 {
1113 char *error_msg
1114 = parse_debug_format_options (mon + sizeof ("set debug-format ") - 1,
1115 1);
1116
1117 if (error_msg != NULL)
1118 {
1119 monitor_output (error_msg);
1120 monitor_show_help ();
1121 write_enn (own_buf);
1122 xfree (error_msg);
1123 }
1124 }
cdbfd419
PP
1125 else if (strcmp (mon, "help") == 0)
1126 monitor_show_help ();
1127 else if (strcmp (mon, "exit") == 0)
1128 exit_requested = 1;
1129 else
1130 {
1131 monitor_output ("Unknown monitor command.\n\n");
1132 monitor_show_help ();
1133 write_enn (own_buf);
1134 }
1135}
1136
d08aafef
PA
1137/* Associates a callback with each supported qXfer'able object. */
1138
1139struct qxfer
1140{
1141 /* The object this handler handles. */
1142 const char *object;
1143
1144 /* Request that the target transfer up to LEN 8-bit bytes of the
1145 target's OBJECT. The OFFSET, for a seekable object, specifies
1146 the starting point. The ANNEX can be used to provide additional
1147 data-specific information to the target.
1148
1149 Return the number of bytes actually transfered, zero when no
5cc22e4c
MM
1150 further transfer is possible, -1 on error, -2 when the transfer
1151 is not supported, and -3 on a verbose error message that should
1152 be preserved. Return of a positive value smaller than LEN does
1153 not indicate the end of the object, only the end of the transfer.
d08aafef
PA
1154
1155 One, and only one, of readbuf or writebuf must be non-NULL. */
1156 int (*xfer) (const char *annex,
1157 gdb_byte *readbuf, const gdb_byte *writebuf,
1158 ULONGEST offset, LONGEST len);
1159};
1160
1161/* Handle qXfer:auxv:read. */
1162
1163static int
1164handle_qxfer_auxv (const char *annex,
1165 gdb_byte *readbuf, const gdb_byte *writebuf,
1166 ULONGEST offset, LONGEST len)
1167{
1168 if (the_target->read_auxv == NULL || writebuf != NULL)
1169 return -2;
1170
1171 if (annex[0] != '\0' || !target_running ())
1172 return -1;
1173
1174 return (*the_target->read_auxv) (offset, readbuf, len);
1175}
1176
e57f1de3
GB
1177/* Handle qXfer:exec-file:read. */
1178
1179static int
1180handle_qxfer_exec_file (const char *const_annex,
1181 gdb_byte *readbuf, const gdb_byte *writebuf,
1182 ULONGEST offset, LONGEST len)
1183{
835205d0 1184 char *file;
e57f1de3
GB
1185 ULONGEST pid;
1186 int total_len;
1187
1188 if (the_target->pid_to_exec_file == NULL || writebuf != NULL)
1189 return -2;
1190
835205d0
GB
1191 if (const_annex[0] == '\0')
1192 {
1193 if (current_thread == NULL)
1194 return -1;
1195
1196 pid = pid_of (current_thread);
1197 }
1198 else
1199 {
1200 char *annex = alloca (strlen (const_annex) + 1);
1201
1202 strcpy (annex, const_annex);
1203 annex = unpack_varlen_hex (annex, &pid);
1204
1205 if (annex[0] != '\0')
1206 return -1;
1207 }
1208
1209 if (pid <= 0)
e57f1de3
GB
1210 return -1;
1211
1212 file = (*the_target->pid_to_exec_file) (pid);
1213 if (file == NULL)
1214 return -1;
1215
1216 total_len = strlen (file);
1217
1218 if (offset > total_len)
1219 return -1;
1220
1221 if (offset + len > total_len)
1222 len = total_len - offset;
1223
1224 memcpy (readbuf, file + offset, len);
1225 return len;
1226}
1227
d08aafef
PA
1228/* Handle qXfer:features:read. */
1229
1230static int
1231handle_qxfer_features (const char *annex,
1232 gdb_byte *readbuf, const gdb_byte *writebuf,
1233 ULONGEST offset, LONGEST len)
1234{
1235 const char *document;
1236 size_t total_len;
1237
1238 if (writebuf != NULL)
1239 return -2;
1240
1241 if (!target_running ())
1242 return -1;
1243
1244 /* Grab the correct annex. */
1245 document = get_features_xml (annex);
1246 if (document == NULL)
1247 return -1;
1248
1249 total_len = strlen (document);
1250
1251 if (offset > total_len)
1252 return -1;
1253
1254 if (offset + len > total_len)
1255 len = total_len - offset;
1256
1257 memcpy (readbuf, document + offset, len);
1258 return len;
1259}
1260
649ebbca
DE
1261/* Worker routine for handle_qxfer_libraries.
1262 Add to the length pointed to by ARG a conservative estimate of the
1263 length needed to transmit the file name of INF. */
1264
1265static void
1266accumulate_file_name_length (struct inferior_list_entry *inf, void *arg)
1267{
1268 struct dll_info *dll = (struct dll_info *) inf;
1269 unsigned int *total_len = arg;
1270
1271 /* Over-estimate the necessary memory. Assume that every character
1272 in the library name must be escaped. */
1273 *total_len += 128 + 6 * strlen (dll->name);
1274}
1275
1276/* Worker routine for handle_qxfer_libraries.
1277 Emit the XML to describe the library in INF. */
1278
1279static void
1280emit_dll_description (struct inferior_list_entry *inf, void *arg)
1281{
1282 struct dll_info *dll = (struct dll_info *) inf;
1283 char **p_ptr = arg;
1284 char *p = *p_ptr;
1285 char *name;
1286
1287 strcpy (p, " <library name=\"");
1288 p = p + strlen (p);
1289 name = xml_escape_text (dll->name);
1290 strcpy (p, name);
1291 free (name);
1292 p = p + strlen (p);
1293 strcpy (p, "\"><segment address=\"");
1294 p = p + strlen (p);
1295 sprintf (p, "0x%lx", (long) dll->base_addr);
1296 p = p + strlen (p);
1297 strcpy (p, "\"/></library>\n");
1298 p = p + strlen (p);
1299
1300 *p_ptr = p;
1301}
1302
d08aafef
PA
1303/* Handle qXfer:libraries:read. */
1304
1305static int
1306handle_qxfer_libraries (const char *annex,
1307 gdb_byte *readbuf, const gdb_byte *writebuf,
1308 ULONGEST offset, LONGEST len)
1309{
1310 unsigned int total_len;
1311 char *document, *p;
d08aafef
PA
1312
1313 if (writebuf != NULL)
1314 return -2;
1315
1316 if (annex[0] != '\0' || !target_running ())
1317 return -1;
1318
d08aafef 1319 total_len = 64;
649ebbca
DE
1320 for_each_inferior_with_data (&all_dlls, accumulate_file_name_length,
1321 &total_len);
d08aafef
PA
1322
1323 document = malloc (total_len);
1324 if (document == NULL)
1325 return -1;
1326
24c05f46 1327 strcpy (document, "<library-list version=\"1.0\">\n");
d08aafef
PA
1328 p = document + strlen (document);
1329
649ebbca 1330 for_each_inferior_with_data (&all_dlls, emit_dll_description, &p);
d08aafef
PA
1331
1332 strcpy (p, "</library-list>\n");
1333
1334 total_len = strlen (document);
1335
1336 if (offset > total_len)
1337 {
1338 free (document);
1339 return -1;
1340 }
1341
1342 if (offset + len > total_len)
1343 len = total_len - offset;
1344
1345 memcpy (readbuf, document + offset, len);
1346 free (document);
1347 return len;
1348}
1349
2268b414
JK
1350/* Handle qXfer:libraries-svr4:read. */
1351
1352static int
1353handle_qxfer_libraries_svr4 (const char *annex,
1354 gdb_byte *readbuf, const gdb_byte *writebuf,
1355 ULONGEST offset, LONGEST len)
1356{
1357 if (writebuf != NULL)
1358 return -2;
1359
b1fbec62 1360 if (!target_running () || the_target->qxfer_libraries_svr4 == NULL)
2268b414
JK
1361 return -1;
1362
1363 return the_target->qxfer_libraries_svr4 (annex, readbuf, writebuf, offset, len);
1364}
1365
d08aafef
PA
1366/* Handle qXfer:osadata:read. */
1367
1368static int
1369handle_qxfer_osdata (const char *annex,
1370 gdb_byte *readbuf, const gdb_byte *writebuf,
1371 ULONGEST offset, LONGEST len)
1372{
1373 if (the_target->qxfer_osdata == NULL || writebuf != NULL)
1374 return -2;
1375
1376 return (*the_target->qxfer_osdata) (annex, readbuf, NULL, offset, len);
1377}
1378
1379/* Handle qXfer:siginfo:read and qXfer:siginfo:write. */
1380
1381static int
1382handle_qxfer_siginfo (const char *annex,
1383 gdb_byte *readbuf, const gdb_byte *writebuf,
1384 ULONGEST offset, LONGEST len)
1385{
1386 if (the_target->qxfer_siginfo == NULL)
1387 return -2;
1388
1389 if (annex[0] != '\0' || !target_running ())
1390 return -1;
1391
1392 return (*the_target->qxfer_siginfo) (annex, readbuf, writebuf, offset, len);
1393}
1394
1395/* Handle qXfer:spu:read and qXfer:spu:write. */
1396
1397static int
1398handle_qxfer_spu (const char *annex,
1399 gdb_byte *readbuf, const gdb_byte *writebuf,
1400 ULONGEST offset, LONGEST len)
1401{
1402 if (the_target->qxfer_spu == NULL)
1403 return -2;
1404
1405 if (!target_running ())
1406 return -1;
1407
1408 return (*the_target->qxfer_spu) (annex, readbuf, writebuf, offset, len);
1409}
1410
1411/* Handle qXfer:statictrace:read. */
1412
1413static int
1414handle_qxfer_statictrace (const char *annex,
1415 gdb_byte *readbuf, const gdb_byte *writebuf,
1416 ULONGEST offset, LONGEST len)
1417{
1418 ULONGEST nbytes;
1419
1420 if (writebuf != NULL)
1421 return -2;
1422
1423 if (annex[0] != '\0' || !target_running () || current_traceframe == -1)
1424 return -1;
1425
1426 if (traceframe_read_sdata (current_traceframe, offset,
1427 readbuf, len, &nbytes))
1428 return -1;
1429 return nbytes;
1430}
1431
649ebbca
DE
1432/* Helper for handle_qxfer_threads_proper.
1433 Emit the XML to describe the thread of INF. */
d08aafef 1434
dc146f7c 1435static void
649ebbca 1436handle_qxfer_threads_worker (struct inferior_list_entry *inf, void *arg)
dc146f7c 1437{
649ebbca
DE
1438 struct thread_info *thread = (struct thread_info *) inf;
1439 struct buffer *buffer = arg;
1440 ptid_t ptid = thread_to_gdb_id (thread);
1441 char ptid_s[100];
1442 int core = target_core_of_thread (ptid);
1443 char core_s[21];
dc146f7c 1444
649ebbca 1445 write_ptid (ptid_s, ptid);
dc146f7c 1446
649ebbca 1447 if (core != -1)
dc146f7c 1448 {
649ebbca
DE
1449 sprintf (core_s, "%d", core);
1450 buffer_xml_printf (buffer, "<thread id=\"%s\" core=\"%s\"/>\n",
1451 ptid_s, core_s);
1452 }
1453 else
1454 {
1455 buffer_xml_printf (buffer, "<thread id=\"%s\"/>\n",
1456 ptid_s);
1457 }
1458}
dc146f7c 1459
649ebbca 1460/* Helper for handle_qxfer_threads. */
dc146f7c 1461
649ebbca
DE
1462static void
1463handle_qxfer_threads_proper (struct buffer *buffer)
1464{
1465 buffer_grow_str (buffer, "<threads>\n");
1466
1467 for_each_inferior_with_data (&all_threads, handle_qxfer_threads_worker,
1468 buffer);
dc146f7c
VP
1469
1470 buffer_grow_str0 (buffer, "</threads>\n");
1471}
1472
d08aafef
PA
1473/* Handle qXfer:threads:read. */
1474
dc146f7c 1475static int
d08aafef
PA
1476handle_qxfer_threads (const char *annex,
1477 gdb_byte *readbuf, const gdb_byte *writebuf,
1478 ULONGEST offset, LONGEST len)
dc146f7c
VP
1479{
1480 static char *result = 0;
1481 static unsigned int result_length = 0;
1482
d08aafef
PA
1483 if (writebuf != NULL)
1484 return -2;
1485
1486 if (!target_running () || annex[0] != '\0')
1487 return -1;
dc146f7c
VP
1488
1489 if (offset == 0)
1490 {
1491 struct buffer buffer;
1492 /* When asked for data at offset 0, generate everything and store into
1493 'result'. Successive reads will be served off 'result'. */
1494 if (result)
1495 free (result);
1496
1497 buffer_init (&buffer);
1498
d08aafef 1499 handle_qxfer_threads_proper (&buffer);
dc146f7c
VP
1500
1501 result = buffer_finish (&buffer);
1502 result_length = strlen (result);
1503 buffer_free (&buffer);
1504 }
1505
1506 if (offset >= result_length)
1507 {
1508 /* We're out of data. */
1509 free (result);
1510 result = NULL;
1511 result_length = 0;
1512 return 0;
1513 }
1514
d08aafef
PA
1515 if (len > result_length - offset)
1516 len = result_length - offset;
1517
1518 memcpy (readbuf, result + offset, len);
1519
1520 return len;
1521}
1522
b3b9301e
PA
1523/* Handle qXfer:traceframe-info:read. */
1524
1525static int
1526handle_qxfer_traceframe_info (const char *annex,
1527 gdb_byte *readbuf, const gdb_byte *writebuf,
1528 ULONGEST offset, LONGEST len)
1529{
1530 static char *result = 0;
1531 static unsigned int result_length = 0;
1532
1533 if (writebuf != NULL)
1534 return -2;
1535
1536 if (!target_running () || annex[0] != '\0' || current_traceframe == -1)
1537 return -1;
1538
1539 if (offset == 0)
1540 {
1541 struct buffer buffer;
1542
1543 /* When asked for data at offset 0, generate everything and
1544 store into 'result'. Successive reads will be served off
1545 'result'. */
1546 free (result);
1547
1548 buffer_init (&buffer);
1549
1550 traceframe_read_info (current_traceframe, &buffer);
1551
1552 result = buffer_finish (&buffer);
1553 result_length = strlen (result);
1554 buffer_free (&buffer);
1555 }
1556
1557 if (offset >= result_length)
1558 {
1559 /* We're out of data. */
1560 free (result);
1561 result = NULL;
1562 result_length = 0;
1563 return 0;
1564 }
1565
1566 if (len > result_length - offset)
1567 len = result_length - offset;
1568
1569 memcpy (readbuf, result + offset, len);
1570 return len;
1571}
1572
78d85199
YQ
1573/* Handle qXfer:fdpic:read. */
1574
1575static int
1576handle_qxfer_fdpic (const char *annex, gdb_byte *readbuf,
1577 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1578{
1579 if (the_target->read_loadmap == NULL)
1580 return -2;
1581
1582 if (!target_running ())
1583 return -1;
1584
1585 return (*the_target->read_loadmap) (annex, offset, readbuf, len);
1586}
1587
9accd112
MM
1588/* Handle qXfer:btrace:read. */
1589
1590static int
1591handle_qxfer_btrace (const char *annex,
1592 gdb_byte *readbuf, const gdb_byte *writebuf,
1593 ULONGEST offset, LONGEST len)
1594{
1595 static struct buffer cache;
1596 struct thread_info *thread;
969c39fb 1597 int type, result;
9accd112
MM
1598
1599 if (the_target->read_btrace == NULL || writebuf != NULL)
1600 return -2;
1601
1602 if (!target_running ())
1603 return -1;
1604
1605 if (ptid_equal (general_thread, null_ptid)
1606 || ptid_equal (general_thread, minus_one_ptid))
1607 {
1608 strcpy (own_buf, "E.Must select a single thread.");
1609 return -3;
1610 }
1611
1612 thread = find_thread_ptid (general_thread);
1613 if (thread == NULL)
1614 {
1615 strcpy (own_buf, "E.No such thread.");
1616 return -3;
1617 }
1618
1619 if (thread->btrace == NULL)
1620 {
1621 strcpy (own_buf, "E.Btrace not enabled.");
1622 return -3;
1623 }
1624
1625 if (strcmp (annex, "all") == 0)
864089d2 1626 type = BTRACE_READ_ALL;
9accd112 1627 else if (strcmp (annex, "new") == 0)
864089d2 1628 type = BTRACE_READ_NEW;
969c39fb
MM
1629 else if (strcmp (annex, "delta") == 0)
1630 type = BTRACE_READ_DELTA;
9accd112
MM
1631 else
1632 {
1633 strcpy (own_buf, "E.Bad annex.");
1634 return -3;
1635 }
1636
1637 if (offset == 0)
1638 {
1639 buffer_free (&cache);
1640
969c39fb
MM
1641 result = target_read_btrace (thread->btrace, &cache, type);
1642 if (result != 0)
1643 {
1644 memcpy (own_buf, cache.buffer, cache.used_size);
1645 return -3;
1646 }
9accd112
MM
1647 }
1648 else if (offset > cache.used_size)
1649 {
1650 buffer_free (&cache);
1651 return -3;
1652 }
1653
1654 if (len > cache.used_size - offset)
1655 len = cache.used_size - offset;
1656
1657 memcpy (readbuf, cache.buffer + offset, len);
1658
1659 return len;
1660}
1661
f4abbc16
MM
1662/* Handle qXfer:btrace-conf:read. */
1663
1664static int
1665handle_qxfer_btrace_conf (const char *annex,
1666 gdb_byte *readbuf, const gdb_byte *writebuf,
1667 ULONGEST offset, LONGEST len)
1668{
1669 static struct buffer cache;
1670 struct thread_info *thread;
1671 int result;
1672
1673 if (the_target->read_btrace_conf == NULL || writebuf != NULL)
1674 return -2;
1675
1676 if (annex[0] != '\0' || !target_running ())
1677 return -1;
1678
1679 if (ptid_equal (general_thread, null_ptid)
1680 || ptid_equal (general_thread, minus_one_ptid))
1681 {
1682 strcpy (own_buf, "E.Must select a single thread.");
1683 return -3;
1684 }
1685
1686 thread = find_thread_ptid (general_thread);
1687 if (thread == NULL)
1688 {
1689 strcpy (own_buf, "E.No such thread.");
1690 return -3;
1691 }
1692
1693 if (thread->btrace == NULL)
1694 {
1695 strcpy (own_buf, "E.Btrace not enabled.");
1696 return -3;
1697 }
1698
1699 if (offset == 0)
1700 {
1701 buffer_free (&cache);
1702
1703 result = target_read_btrace_conf (thread->btrace, &cache);
1704 if (result != 0)
1705 {
1706 memcpy (own_buf, cache.buffer, cache.used_size);
1707 return -3;
1708 }
1709 }
1710 else if (offset > cache.used_size)
1711 {
1712 buffer_free (&cache);
1713 return -3;
1714 }
1715
1716 if (len > cache.used_size - offset)
1717 len = cache.used_size - offset;
1718
1719 memcpy (readbuf, cache.buffer + offset, len);
1720
1721 return len;
1722}
1723
d08aafef
PA
1724static const struct qxfer qxfer_packets[] =
1725 {
1726 { "auxv", handle_qxfer_auxv },
9accd112 1727 { "btrace", handle_qxfer_btrace },
f4abbc16 1728 { "btrace-conf", handle_qxfer_btrace_conf },
e57f1de3 1729 { "exec-file", handle_qxfer_exec_file},
78d85199 1730 { "fdpic", handle_qxfer_fdpic},
d08aafef
PA
1731 { "features", handle_qxfer_features },
1732 { "libraries", handle_qxfer_libraries },
2268b414 1733 { "libraries-svr4", handle_qxfer_libraries_svr4 },
d08aafef
PA
1734 { "osdata", handle_qxfer_osdata },
1735 { "siginfo", handle_qxfer_siginfo },
1736 { "spu", handle_qxfer_spu },
1737 { "statictrace", handle_qxfer_statictrace },
1738 { "threads", handle_qxfer_threads },
b3b9301e 1739 { "traceframe-info", handle_qxfer_traceframe_info },
d08aafef
PA
1740 };
1741
1742static int
1743handle_qxfer (char *own_buf, int packet_len, int *new_packet_len_p)
1744{
1745 int i;
1746 char *object;
1747 char *rw;
1748 char *annex;
1749 char *offset;
1750
61012eef 1751 if (!startswith (own_buf, "qXfer:"))
d08aafef
PA
1752 return 0;
1753
1754 /* Grab the object, r/w and annex. */
1755 if (decode_xfer (own_buf + 6, &object, &rw, &annex, &offset) < 0)
1756 {
1757 write_enn (own_buf);
1758 return 1;
1759 }
1760
1761 for (i = 0;
1762 i < sizeof (qxfer_packets) / sizeof (qxfer_packets[0]);
1763 i++)
1764 {
1765 const struct qxfer *q = &qxfer_packets[i];
1766
1767 if (strcmp (object, q->object) == 0)
1768 {
1769 if (strcmp (rw, "read") == 0)
1770 {
1771 unsigned char *data;
1772 int n;
1773 CORE_ADDR ofs;
1774 unsigned int len;
1775
1776 /* Grab the offset and length. */
1777 if (decode_xfer_read (offset, &ofs, &len) < 0)
1778 {
1779 write_enn (own_buf);
1780 return 1;
1781 }
1782
1783 /* Read one extra byte, as an indicator of whether there is
1784 more. */
1785 if (len > PBUFSIZ - 2)
1786 len = PBUFSIZ - 2;
1787 data = malloc (len + 1);
1788 if (data == NULL)
1789 {
1790 write_enn (own_buf);
1791 return 1;
1792 }
1793 n = (*q->xfer) (annex, data, NULL, ofs, len + 1);
1794 if (n == -2)
1795 {
1796 free (data);
1797 return 0;
1798 }
5cc22e4c
MM
1799 else if (n == -3)
1800 {
1801 /* Preserve error message. */
1802 }
d08aafef
PA
1803 else if (n < 0)
1804 write_enn (own_buf);
1805 else if (n > len)
1806 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
1807 else
1808 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
1809
1810 free (data);
1811 return 1;
1812 }
1813 else if (strcmp (rw, "write") == 0)
1814 {
1815 int n;
1816 unsigned int len;
1817 CORE_ADDR ofs;
1818 unsigned char *data;
1819
1820 strcpy (own_buf, "E00");
1821 data = malloc (packet_len - (offset - own_buf));
1822 if (data == NULL)
1823 {
1824 write_enn (own_buf);
1825 return 1;
1826 }
1827 if (decode_xfer_write (offset, packet_len - (offset - own_buf),
1828 &ofs, &len, data) < 0)
1829 {
1830 free (data);
1831 write_enn (own_buf);
1832 return 1;
1833 }
1834
1835 n = (*q->xfer) (annex, NULL, data, ofs, len);
1836 if (n == -2)
1837 {
1838 free (data);
1839 return 0;
1840 }
5cc22e4c
MM
1841 else if (n == -3)
1842 {
1843 /* Preserve error message. */
1844 }
d08aafef
PA
1845 else if (n < 0)
1846 write_enn (own_buf);
1847 else
1848 sprintf (own_buf, "%x", n);
dc146f7c 1849
d08aafef
PA
1850 free (data);
1851 return 1;
1852 }
dc146f7c 1853
d08aafef
PA
1854 return 0;
1855 }
1856 }
dc146f7c 1857
d08aafef 1858 return 0;
dc146f7c
VP
1859}
1860
30ba68cb
MS
1861/* Table used by the crc32 function to calcuate the checksum. */
1862
1863static unsigned int crc32_table[256] =
1864{0, 0};
1865
1866/* Compute 32 bit CRC from inferior memory.
1867
1868 On success, return 32 bit CRC.
1869 On failure, return (unsigned long long) -1. */
1870
1871static unsigned long long
1872crc32 (CORE_ADDR base, int len, unsigned int crc)
1873{
1874 if (!crc32_table[1])
1875 {
1876 /* Initialize the CRC table and the decoding table. */
1877 int i, j;
1878 unsigned int c;
1879
1880 for (i = 0; i < 256; i++)
1881 {
1882 for (c = i << 24, j = 8; j > 0; --j)
1883 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
1884 crc32_table[i] = c;
1885 }
1886 }
1887
1888 while (len--)
1889 {
1890 unsigned char byte = 0;
1891
1892 /* Return failure if memory read fails. */
1893 if (read_inferior_memory (base, &byte, 1) != 0)
1894 return (unsigned long long) -1;
1895
1896 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ byte) & 255];
1897 base++;
1898 }
1899 return (unsigned long long) crc;
1900}
1901
043c3577
MM
1902/* Add supported btrace packets to BUF. */
1903
1904static void
1905supported_btrace_packets (char *buf)
1906{
b20a6524
MM
1907 int btrace_supported = 0;
1908
043c3577 1909 if (target_supports_btrace (BTRACE_FORMAT_BTS))
d33501a5
MM
1910 {
1911 strcat (buf, ";Qbtrace:bts+");
1912 strcat (buf, ";Qbtrace-conf:bts:size+");
b20a6524
MM
1913
1914 btrace_supported = 1;
d33501a5 1915 }
b20a6524
MM
1916
1917 if (target_supports_btrace (BTRACE_FORMAT_PT))
1918 {
1919 strcat (buf, ";Qbtrace:pt+");
1920 strcat (buf, ";Qbtrace-conf:pt:size+");
1921
1922 btrace_supported = 1;
1923 }
1924
1925 if (!btrace_supported)
043c3577
MM
1926 return;
1927
1928 strcat (buf, ";Qbtrace:off+");
1929 strcat (buf, ";qXfer:btrace:read+");
f4abbc16 1930 strcat (buf, ";qXfer:btrace-conf:read+");
043c3577
MM
1931}
1932
ce3a066d 1933/* Handle all of the extended 'q' packets. */
d08aafef 1934
ce3a066d 1935void
0e7f50da 1936handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
ce3a066d 1937{
0d62e5e8
DJ
1938 static struct inferior_list_entry *thread_ptr;
1939
bb63802a 1940 /* Reply the current thread id. */
db42f210 1941 if (strcmp ("qC", own_buf) == 0 && !disable_packet_qC)
bb63802a 1942 {
95954743 1943 ptid_t gdb_id;
2d717e4f 1944 require_running (own_buf);
bd99dc85 1945
95954743
PA
1946 if (!ptid_equal (general_thread, null_ptid)
1947 && !ptid_equal (general_thread, minus_one_ptid))
bd99dc85
PA
1948 gdb_id = general_thread;
1949 else
1950 {
649ebbca 1951 thread_ptr = get_first_inferior (&all_threads);
bd99dc85
PA
1952 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
1953 }
1954
95954743
PA
1955 sprintf (own_buf, "QC");
1956 own_buf += 2;
4b812f4e 1957 write_ptid (own_buf, gdb_id);
bb63802a
UW
1958 return;
1959 }
1960
ce3a066d
DJ
1961 if (strcmp ("qSymbol::", own_buf) == 0)
1962 {
d3bbe7a0
PA
1963 /* GDB is suggesting new symbols have been loaded. This may
1964 mean a new shared library has been detected as loaded, so
1965 take the opportunity to check if breakpoints we think are
1966 inserted, still are. Note that it isn't guaranteed that
1967 we'll see this when a shared library is loaded, and nor will
1968 we see this for unloads (although breakpoints in unloaded
1969 libraries shouldn't trigger), as GDB may not find symbols for
1970 the library at all. We also re-validate breakpoints when we
1971 see a second GDB breakpoint for the same address, and or when
1972 we access breakpoint shadows. */
1973 validate_breakpoints ();
1974
fa593d66
PA
1975 if (target_supports_tracepoints ())
1976 tracepoint_look_up_symbols ();
1977
2d717e4f 1978 if (target_running () && the_target->look_up_symbols != NULL)
2f2893d9
DJ
1979 (*the_target->look_up_symbols) ();
1980
ce3a066d
DJ
1981 strcpy (own_buf, "OK");
1982 return;
1983 }
1984
db42f210 1985 if (!disable_packet_qfThreadInfo)
0d62e5e8 1986 {
db42f210 1987 if (strcmp ("qfThreadInfo", own_buf) == 0)
0d62e5e8 1988 {
95954743
PA
1989 ptid_t gdb_id;
1990
db42f210 1991 require_running (own_buf);
649ebbca 1992 thread_ptr = get_first_inferior (&all_threads);
95954743
PA
1993
1994 *own_buf++ = 'm';
1995 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
1996 write_ptid (own_buf, gdb_id);
0d62e5e8
DJ
1997 thread_ptr = thread_ptr->next;
1998 return;
1999 }
db42f210
PA
2000
2001 if (strcmp ("qsThreadInfo", own_buf) == 0)
0d62e5e8 2002 {
95954743
PA
2003 ptid_t gdb_id;
2004
db42f210
PA
2005 require_running (own_buf);
2006 if (thread_ptr != NULL)
2007 {
95954743
PA
2008 *own_buf++ = 'm';
2009 gdb_id = thread_to_gdb_id ((struct thread_info *)thread_ptr);
2010 write_ptid (own_buf, gdb_id);
db42f210
PA
2011 thread_ptr = thread_ptr->next;
2012 return;
2013 }
2014 else
2015 {
2016 sprintf (own_buf, "l");
2017 return;
2018 }
0d62e5e8
DJ
2019 }
2020 }
aa691b87 2021
52fb6437
NS
2022 if (the_target->read_offsets != NULL
2023 && strcmp ("qOffsets", own_buf) == 0)
2024 {
2025 CORE_ADDR text, data;
2d717e4f
DJ
2026
2027 require_running (own_buf);
52fb6437
NS
2028 if (the_target->read_offsets (&text, &data))
2029 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
2030 (long)text, (long)data, (long)data);
2031 else
2032 write_enn (own_buf);
1b3f6016 2033
52fb6437
NS
2034 return;
2035 }
2036
be2a5f71 2037 /* Protocol features query. */
61012eef 2038 if (startswith (own_buf, "qSupported")
be2a5f71
DJ
2039 && (own_buf[10] == ':' || own_buf[10] == '\0'))
2040 {
95954743 2041 char *p = &own_buf[10];
fa593d66 2042 int gdb_supports_qRelocInsn = 0;
95954743 2043
1570b33e
L
2044 /* Start processing qSupported packet. */
2045 target_process_qsupported (NULL);
2046
95954743
PA
2047 /* Process each feature being provided by GDB. The first
2048 feature will follow a ':', and latter features will follow
2049 ';'. */
2050 if (*p == ':')
d149dd1d
PA
2051 {
2052 char **qsupported = NULL;
2053 int count = 0;
2054 int i;
2055
2056 /* Two passes, to avoid nested strtok calls in
2057 target_process_qsupported. */
2058 for (p = strtok (p + 1, ";");
2059 p != NULL;
2060 p = strtok (NULL, ";"))
2061 {
2062 count++;
2063 qsupported = xrealloc (qsupported, count * sizeof (char *));
2064 qsupported[count - 1] = xstrdup (p);
2065 }
2066
2067 for (i = 0; i < count; i++)
2068 {
2069 p = qsupported[i];
2070 if (strcmp (p, "multiprocess+") == 0)
2071 {
2072 /* GDB supports and wants multi-process support if
2073 possible. */
2074 if (target_supports_multi_process ())
2075 multi_process = 1;
2076 }
fa593d66
PA
2077 else if (strcmp (p, "qRelocInsn+") == 0)
2078 {
2079 /* GDB supports relocate instruction requests. */
2080 gdb_supports_qRelocInsn = 1;
2081 }
1ec68e26
PA
2082 else if (strcmp (p, "swbreak+") == 0)
2083 {
2084 /* GDB wants us to report whether a trap is caused
2085 by a software breakpoint and for us to handle PC
2086 adjustment if necessary on this target. */
2087 if (target_supports_stopped_by_sw_breakpoint ())
2088 swbreak_feature = 1;
2089 }
2090 else if (strcmp (p, "hwbreak+") == 0)
2091 {
2092 /* GDB wants us to report whether a trap is caused
2093 by a hardware breakpoint. */
2094 if (target_supports_stopped_by_hw_breakpoint ())
2095 hwbreak_feature = 1;
2096 }
89245bc0
DB
2097 else if (strcmp (p, "fork-events+") == 0)
2098 {
2099 /* GDB supports and wants fork events if possible. */
2100 if (target_supports_fork_events ())
2101 report_fork_events = 1;
2102 }
2103 else if (strcmp (p, "vfork-events+") == 0)
2104 {
2105 /* GDB supports and wants vfork events if possible. */
2106 if (target_supports_vfork_events ())
2107 report_vfork_events = 1;
2108 }
d149dd1d
PA
2109 else
2110 target_process_qsupported (p);
2111
2112 free (p);
2113 }
2114
2115 free (qsupported);
2116 }
95954743 2117
9b224c5e
PA
2118 sprintf (own_buf,
2119 "PacketSize=%x;QPassSignals+;QProgramSignals+",
2120 PBUFSIZ - 1);
0876f84a 2121
2268b414 2122 if (the_target->qxfer_libraries_svr4 != NULL)
b1fbec62
GB
2123 strcat (own_buf, ";qXfer:libraries-svr4:read+"
2124 ";augmented-libraries-svr4-read+");
2268b414
JK
2125 else
2126 {
2127 /* We do not have any hook to indicate whether the non-SVR4 target
2128 backend supports qXfer:libraries:read, so always report it. */
2129 strcat (own_buf, ";qXfer:libraries:read+");
2130 }
255e7678 2131
0876f84a 2132 if (the_target->read_auxv != NULL)
9f2e1e63 2133 strcat (own_buf, ";qXfer:auxv:read+");
2d717e4f 2134
0e7f50da
UW
2135 if (the_target->qxfer_spu != NULL)
2136 strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+");
0876f84a 2137
4aa995e1
PA
2138 if (the_target->qxfer_siginfo != NULL)
2139 strcat (own_buf, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
2140
78d85199
YQ
2141 if (the_target->read_loadmap != NULL)
2142 strcat (own_buf, ";qXfer:fdpic:read+");
2143
221c031f
UW
2144 /* We always report qXfer:features:read, as targets may
2145 install XML files on a subsequent call to arch_setup.
2146 If we reported to GDB on startup that we don't support
2147 qXfer:feature:read at all, we will never be re-queried. */
2148 strcat (own_buf, ";qXfer:features:read+");
23181151 2149
a6f3e723
SL
2150 if (transport_is_reliable)
2151 strcat (own_buf, ";QStartNoAckMode+");
07e059b5
VP
2152
2153 if (the_target->qxfer_osdata != NULL)
1b3f6016 2154 strcat (own_buf, ";qXfer:osdata:read+");
07e059b5 2155
cf8fd78b
PA
2156 if (target_supports_multi_process ())
2157 strcat (own_buf, ";multiprocess+");
95954743 2158
89245bc0
DB
2159 if (target_supports_fork_events ())
2160 strcat (own_buf, ";fork-events+");
2161
2162 if (target_supports_vfork_events ())
2163 strcat (own_buf, ";vfork-events+");
2164
bd99dc85
PA
2165 if (target_supports_non_stop ())
2166 strcat (own_buf, ";QNonStop+");
2167
03583c20
UW
2168 if (target_supports_disable_randomization ())
2169 strcat (own_buf, ";QDisableRandomization+");
2170
dc146f7c
VP
2171 strcat (own_buf, ";qXfer:threads:read+");
2172
219f2f23
PA
2173 if (target_supports_tracepoints ())
2174 {
2175 strcat (own_buf, ";ConditionalTracepoints+");
2176 strcat (own_buf, ";TraceStateVariables+");
2177 strcat (own_buf, ";TracepointSource+");
8336d594 2178 strcat (own_buf, ";DisconnectedTracing+");
fa593d66
PA
2179 if (gdb_supports_qRelocInsn && target_supports_fast_tracepoints ())
2180 strcat (own_buf, ";FastTracepoints+");
0fb4aa4b 2181 strcat (own_buf, ";StaticTracepoints+");
1e4d1764 2182 strcat (own_buf, ";InstallInTrace+");
0fb4aa4b 2183 strcat (own_buf, ";qXfer:statictrace:read+");
b3b9301e 2184 strcat (own_buf, ";qXfer:traceframe-info:read+");
d248b706 2185 strcat (own_buf, ";EnableDisableTracepoints+");
f6f899bf 2186 strcat (own_buf, ";QTBuffer:size+");
3065dfb6 2187 strcat (own_buf, ";tracenz+");
219f2f23
PA
2188 }
2189
d3ce09f5 2190 /* Support target-side breakpoint conditions and commands. */
45614f15
YQ
2191 if (target_supports_conditional_breakpoints ())
2192 strcat (own_buf, ";ConditionalBreakpoints+");
d3ce09f5 2193 strcat (own_buf, ";BreakpointCommands+");
9f3a5c85 2194
d1feda86
YQ
2195 if (target_supports_agent ())
2196 strcat (own_buf, ";QAgent+");
2197
043c3577 2198 supported_btrace_packets (own_buf);
9accd112 2199
1ec68e26
PA
2200 if (target_supports_stopped_by_sw_breakpoint ())
2201 strcat (own_buf, ";swbreak+");
2202
2203 if (target_supports_stopped_by_hw_breakpoint ())
2204 strcat (own_buf, ";hwbreak+");
2205
e57f1de3
GB
2206 if (the_target->pid_to_exec_file != NULL)
2207 strcat (own_buf, ";qXfer:exec-file:read+");
2208
14d2069a
GB
2209 /* Reinitialize components as needed for the new connection. */
2210 hostio_handle_new_gdb_connection ();
de0d863e
DB
2211 target_handle_new_gdb_connection ();
2212
be2a5f71
DJ
2213 return;
2214 }
2215
dae5f5cf
DJ
2216 /* Thread-local storage support. */
2217 if (the_target->get_tls_address != NULL
61012eef 2218 && startswith (own_buf, "qGetTLSAddr:"))
dae5f5cf
DJ
2219 {
2220 char *p = own_buf + 12;
5b1c542e 2221 CORE_ADDR parts[2], address = 0;
dae5f5cf 2222 int i, err;
95954743 2223 ptid_t ptid = null_ptid;
dae5f5cf 2224
2d717e4f
DJ
2225 require_running (own_buf);
2226
dae5f5cf
DJ
2227 for (i = 0; i < 3; i++)
2228 {
2229 char *p2;
2230 int len;
2231
2232 if (p == NULL)
2233 break;
2234
2235 p2 = strchr (p, ',');
2236 if (p2)
2237 {
2238 len = p2 - p;
2239 p2++;
2240 }
2241 else
2242 {
2243 len = strlen (p);
2244 p2 = NULL;
2245 }
2246
5b1c542e 2247 if (i == 0)
95954743 2248 ptid = read_ptid (p, NULL);
5b1c542e
PA
2249 else
2250 decode_address (&parts[i - 1], p, len);
dae5f5cf
DJ
2251 p = p2;
2252 }
2253
2254 if (p != NULL || i < 3)
2255 err = 1;
2256 else
2257 {
e09875d4 2258 struct thread_info *thread = find_thread_ptid (ptid);
dae5f5cf
DJ
2259
2260 if (thread == NULL)
2261 err = 2;
2262 else
5b1c542e 2263 err = the_target->get_tls_address (thread, parts[0], parts[1],
dae5f5cf
DJ
2264 &address);
2265 }
2266
2267 if (err == 0)
2268 {
c6f46ca0 2269 strcpy (own_buf, paddress(address));
dae5f5cf
DJ
2270 return;
2271 }
2272 else if (err > 0)
2273 {
2274 write_enn (own_buf);
2275 return;
2276 }
2277
2278 /* Otherwise, pretend we do not understand this packet. */
2279 }
2280
711e434b
PM
2281 /* Windows OS Thread Information Block address support. */
2282 if (the_target->get_tib_address != NULL
61012eef 2283 && startswith (own_buf, "qGetTIBAddr:"))
711e434b
PM
2284 {
2285 char *annex;
2286 int n;
2287 CORE_ADDR tlb;
2288 ptid_t ptid = read_ptid (own_buf + 12, &annex);
2289
2290 n = (*the_target->get_tib_address) (ptid, &tlb);
2291 if (n == 1)
2292 {
c6f46ca0 2293 strcpy (own_buf, paddress(tlb));
711e434b
PM
2294 return;
2295 }
2296 else if (n == 0)
2297 {
2298 write_enn (own_buf);
2299 return;
2300 }
2301 return;
2302 }
2303
c74d0ad8 2304 /* Handle "monitor" commands. */
61012eef 2305 if (startswith (own_buf, "qRcmd,"))
c74d0ad8 2306 {
aef93bd7 2307 char *mon = malloc (PBUFSIZ);
c74d0ad8
DJ
2308 int len = strlen (own_buf + 6);
2309
aef93bd7
DE
2310 if (mon == NULL)
2311 {
2312 write_enn (own_buf);
2313 return;
2314 }
2315
ff0e980e
TT
2316 if ((len % 2) != 0
2317 || hex2bin (own_buf + 6, (gdb_byte *) mon, len / 2) != len / 2)
c74d0ad8
DJ
2318 {
2319 write_enn (own_buf);
2320 free (mon);
2321 return;
2322 }
2323 mon[len / 2] = '\0';
2324
2325 write_ok (own_buf);
2326
cdbfd419
PP
2327 if (the_target->handle_monitor_command == NULL
2328 || (*the_target->handle_monitor_command) (mon) == 0)
2329 /* Default processing. */
d73f2619 2330 handle_monitor_command (mon, own_buf);
c74d0ad8
DJ
2331
2332 free (mon);
2333 return;
2334 }
2335
61012eef 2336 if (startswith (own_buf, "qSearch:memory:"))
08388c79
DE
2337 {
2338 require_running (own_buf);
2339 handle_search_memory (own_buf, packet_len);
2340 return;
2341 }
2342
95954743 2343 if (strcmp (own_buf, "qAttached") == 0
61012eef 2344 || startswith (own_buf, "qAttached:"))
0b16c5cf 2345 {
95954743
PA
2346 struct process_info *process;
2347
2348 if (own_buf[sizeof ("qAttached") - 1])
2349 {
2350 int pid = strtoul (own_buf + sizeof ("qAttached:") - 1, NULL, 16);
2351 process = (struct process_info *)
2352 find_inferior_id (&all_processes, pid_to_ptid (pid));
2353 }
2354 else
2355 {
2356 require_running (own_buf);
2357 process = current_process ();
2358 }
2359
2360 if (process == NULL)
2361 {
2362 write_enn (own_buf);
2363 return;
2364 }
2365
2366 strcpy (own_buf, process->attached ? "1" : "0");
0b16c5cf
PA
2367 return;
2368 }
2369
61012eef 2370 if (startswith (own_buf, "qCRC:"))
30ba68cb
MS
2371 {
2372 /* CRC check (compare-section). */
2373 char *comma;
aca22551 2374 ULONGEST base;
30ba68cb
MS
2375 int len;
2376 unsigned long long crc;
2377
2378 require_running (own_buf);
aca22551 2379 comma = unpack_varlen_hex (own_buf + 5, &base);
30ba68cb
MS
2380 if (*comma++ != ',')
2381 {
2382 write_enn (own_buf);
2383 return;
2384 }
2385 len = strtoul (comma, NULL, 16);
2386 crc = crc32 (base, len, 0xffffffff);
2387 /* Check for memory failure. */
2388 if (crc == (unsigned long long) -1)
2389 {
2390 write_enn (own_buf);
2391 return;
2392 }
2393 sprintf (own_buf, "C%lx", (unsigned long) crc);
2394 return;
2395 }
2396
d08aafef
PA
2397 if (handle_qxfer (own_buf, packet_len, new_packet_len_p))
2398 return;
2399
219f2f23
PA
2400 if (target_supports_tracepoints () && handle_tracepoint_query (own_buf))
2401 return;
2402
ce3a066d
DJ
2403 /* Otherwise we didn't know what packet it was. Say we didn't
2404 understand it. */
2405 own_buf[0] = 0;
2406}
2407
ce1a5b52 2408static void gdb_wants_all_threads_stopped (void);
b7ea362b
PA
2409static void resume (struct thread_resume *actions, size_t n);
2410
649ebbca
DE
2411/* The callback that is passed to visit_actioned_threads. */
2412typedef int (visit_actioned_threads_callback_ftype)
2413 (const struct thread_resume *, struct thread_info *);
2414
2415/* Struct to pass data to visit_actioned_threads. */
2416
2417struct visit_actioned_threads_data
2418{
2419 const struct thread_resume *actions;
2420 size_t num_actions;
2421 visit_actioned_threads_callback_ftype *callback;
2422};
2423
b7ea362b
PA
2424/* Call CALLBACK for any thread to which ACTIONS applies to. Returns
2425 true if CALLBACK returns true. Returns false if no matching thread
649ebbca
DE
2426 is found or CALLBACK results false.
2427 Note: This function is itself a callback for find_inferior. */
b7ea362b
PA
2428
2429static int
649ebbca 2430visit_actioned_threads (struct inferior_list_entry *entry, void *datap)
b7ea362b 2431{
649ebbca
DE
2432 struct visit_actioned_threads_data *data = datap;
2433 const struct thread_resume *actions = data->actions;
2434 size_t num_actions = data->num_actions;
2435 visit_actioned_threads_callback_ftype *callback = data->callback;
2436 size_t i;
b7ea362b 2437
649ebbca 2438 for (i = 0; i < num_actions; i++)
b7ea362b 2439 {
649ebbca 2440 const struct thread_resume *action = &actions[i];
b7ea362b 2441
649ebbca
DE
2442 if (ptid_equal (action->thread, minus_one_ptid)
2443 || ptid_equal (action->thread, entry->id)
2444 || ((ptid_get_pid (action->thread)
2445 == ptid_get_pid (entry->id))
2446 && ptid_get_lwp (action->thread) == -1))
b7ea362b 2447 {
649ebbca 2448 struct thread_info *thread = (struct thread_info *) entry;
b7ea362b 2449
649ebbca
DE
2450 if ((*callback) (action, thread))
2451 return 1;
b7ea362b
PA
2452 }
2453 }
2454
2455 return 0;
2456}
2457
2458/* Callback for visit_actioned_threads. If the thread has a pending
2459 status to report, report it now. */
2460
2461static int
2462handle_pending_status (const struct thread_resume *resumption,
2463 struct thread_info *thread)
2464{
2465 if (thread->status_pending_p)
2466 {
2467 thread->status_pending_p = 0;
2468
2469 last_status = thread->last_status;
2470 last_ptid = thread->entry.id;
2471 prepare_resume_reply (own_buf, last_ptid, &last_status);
2472 return 1;
2473 }
2474 return 0;
2475}
ce1a5b52 2476
64386c31
DJ
2477/* Parse vCont packets. */
2478void
5b1c542e 2479handle_v_cont (char *own_buf)
64386c31
DJ
2480{
2481 char *p, *q;
2482 int n = 0, i = 0;
2bd7c093 2483 struct thread_resume *resume_info;
95954743 2484 struct thread_resume default_action = {{0}};
64386c31
DJ
2485
2486 /* Count the number of semicolons in the packet. There should be one
2487 for every action. */
2488 p = &own_buf[5];
2489 while (p)
2490 {
2491 n++;
2492 p++;
2493 p = strchr (p, ';');
2494 }
2bd7c093
PA
2495
2496 resume_info = malloc (n * sizeof (resume_info[0]));
aef93bd7
DE
2497 if (resume_info == NULL)
2498 goto err;
64386c31 2499
64386c31 2500 p = &own_buf[5];
64386c31
DJ
2501 while (*p)
2502 {
2503 p++;
2504
c2d6af84
PA
2505 memset (&resume_info[i], 0, sizeof resume_info[i]);
2506
64386c31 2507 if (p[0] == 's' || p[0] == 'S')
bd99dc85 2508 resume_info[i].kind = resume_step;
c2d6af84
PA
2509 else if (p[0] == 'r')
2510 resume_info[i].kind = resume_step;
64386c31 2511 else if (p[0] == 'c' || p[0] == 'C')
bd99dc85
PA
2512 resume_info[i].kind = resume_continue;
2513 else if (p[0] == 't')
2514 resume_info[i].kind = resume_stop;
64386c31
DJ
2515 else
2516 goto err;
2517
2518 if (p[0] == 'S' || p[0] == 'C')
2519 {
2520 int sig;
2521 sig = strtol (p + 1, &q, 16);
2522 if (p == q)
2523 goto err;
2524 p = q;
2525
2ea28649 2526 if (!gdb_signal_to_host_p (sig))
64386c31 2527 goto err;
2ea28649 2528 resume_info[i].sig = gdb_signal_to_host (sig);
64386c31 2529 }
c2d6af84
PA
2530 else if (p[0] == 'r')
2531 {
6740dc9c 2532 ULONGEST addr;
c2d6af84 2533
6740dc9c
PA
2534 p = unpack_varlen_hex (p + 1, &addr);
2535 resume_info[i].step_range_start = addr;
c2d6af84 2536
6740dc9c
PA
2537 if (*p != ',')
2538 goto err;
c2d6af84 2539
6740dc9c
PA
2540 p = unpack_varlen_hex (p + 1, &addr);
2541 resume_info[i].step_range_end = addr;
c2d6af84 2542 }
64386c31
DJ
2543 else
2544 {
64386c31
DJ
2545 p = p + 1;
2546 }
2547
2548 if (p[0] == 0)
2549 {
95954743 2550 resume_info[i].thread = minus_one_ptid;
64386c31
DJ
2551 default_action = resume_info[i];
2552
2553 /* Note: we don't increment i here, we'll overwrite this entry
2554 the next time through. */
2555 }
2556 else if (p[0] == ':')
2557 {
95954743 2558 ptid_t ptid = read_ptid (p + 1, &q);
a06660f7 2559
64386c31
DJ
2560 if (p == q)
2561 goto err;
2562 p = q;
2563 if (p[0] != ';' && p[0] != 0)
2564 goto err;
2565
95954743 2566 resume_info[i].thread = ptid;
a06660f7 2567
64386c31
DJ
2568 i++;
2569 }
2570 }
2571
2bd7c093
PA
2572 if (i < n)
2573 resume_info[i] = default_action;
64386c31 2574
0bfdf32f 2575 set_desired_thread (0);
64386c31 2576
b7ea362b
PA
2577 resume (resume_info, n);
2578 free (resume_info);
2579 return;
2580
2581err:
2582 write_enn (own_buf);
2583 free (resume_info);
2584 return;
2585}
2586
2587/* Resume target with ACTIONS, an array of NUM_ACTIONS elements. */
2588
2589static void
2590resume (struct thread_resume *actions, size_t num_actions)
2591{
bd99dc85 2592 if (!non_stop)
b7ea362b
PA
2593 {
2594 /* Check if among the threads that GDB wants actioned, there's
2595 one with a pending status to report. If so, skip actually
2596 resuming/stopping and report the pending event
2597 immediately. */
649ebbca
DE
2598 struct visit_actioned_threads_data data;
2599
2600 data.actions = actions;
2601 data.num_actions = num_actions;
2602 data.callback = handle_pending_status;
2603 if (find_inferior (&all_threads, visit_actioned_threads, &data) != NULL)
b7ea362b 2604 return;
bd99dc85 2605
b7ea362b
PA
2606 enable_async_io ();
2607 }
64386c31 2608
b7ea362b 2609 (*the_target->resume) (actions, num_actions);
64386c31 2610
bd99dc85
PA
2611 if (non_stop)
2612 write_ok (own_buf);
2613 else
2614 {
95954743 2615 last_ptid = mywait (minus_one_ptid, &last_status, 0, 1);
ce1a5b52 2616
fa96cb38
PA
2617 if (last_status.kind == TARGET_WAITKIND_NO_RESUMED)
2618 {
2619 /* No proper RSP support for this yet. At least return
2620 error. */
2621 sprintf (own_buf, "E.No unwaited-for children left.");
2622 disable_async_io ();
2623 return;
2624 }
2625
d20a8ad9 2626 if (last_status.kind != TARGET_WAITKIND_EXITED
fa96cb38
PA
2627 && last_status.kind != TARGET_WAITKIND_SIGNALLED
2628 && last_status.kind != TARGET_WAITKIND_NO_RESUMED)
0bfdf32f 2629 current_thread->last_status = last_status;
d20a8ad9 2630
ce1a5b52
PA
2631 /* From the client's perspective, all-stop mode always stops all
2632 threads implicitly (and the target backend has already done
2633 so by now). Tag all threads as "want-stopped", so we don't
2634 resume them implicitly without the client telling us to. */
2635 gdb_wants_all_threads_stopped ();
bd99dc85
PA
2636 prepare_resume_reply (own_buf, last_ptid, &last_status);
2637 disable_async_io ();
6bd31874
JB
2638
2639 if (last_status.kind == TARGET_WAITKIND_EXITED
2640 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
2641 mourn_inferior (find_process_pid (ptid_get_pid (last_ptid)));
bd99dc85 2642 }
64386c31
DJ
2643}
2644
2d717e4f
DJ
2645/* Attach to a new program. Return 1 if successful, 0 if failure. */
2646int
5b1c542e 2647handle_v_attach (char *own_buf)
2d717e4f
DJ
2648{
2649 int pid;
2650
2651 pid = strtol (own_buf + 8, NULL, 16);
5b1c542e 2652 if (pid != 0 && attach_inferior (pid) == 0)
2d717e4f 2653 {
aeba519e
PA
2654 /* Don't report shared library events after attaching, even if
2655 some libraries are preloaded. GDB will always poll the
2656 library list. Avoids the "stopped by shared library event"
2657 notice on the GDB side. */
2658 dlls_changed = 0;
bd99dc85
PA
2659
2660 if (non_stop)
2661 {
2662 /* In non-stop, we don't send a resume reply. Stop events
2663 will follow up using the normal notification
2664 mechanism. */
2665 write_ok (own_buf);
2666 }
2667 else
2668 prepare_resume_reply (own_buf, last_ptid, &last_status);
2669
2d717e4f
DJ
2670 return 1;
2671 }
2672 else
2673 {
2674 write_enn (own_buf);
2675 return 0;
2676 }
2677}
2678
2679/* Run a new program. Return 1 if successful, 0 if failure. */
2680static int
5b1c542e 2681handle_v_run (char *own_buf)
2d717e4f 2682{
aef93bd7 2683 char *p, *next_p, **new_argv;
2d717e4f
DJ
2684 int i, new_argc;
2685
2686 new_argc = 0;
2687 for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';'))
2688 {
2689 p++;
2690 new_argc++;
2691 }
2692
aef93bd7
DE
2693 new_argv = calloc (new_argc + 2, sizeof (char *));
2694 if (new_argv == NULL)
2695 {
2696 write_enn (own_buf);
2697 return 0;
2698 }
2699
2d717e4f
DJ
2700 i = 0;
2701 for (p = own_buf + strlen ("vRun;"); *p; p = next_p)
2702 {
2703 next_p = strchr (p, ';');
2704 if (next_p == NULL)
2705 next_p = p + strlen (p);
2706
2707 if (i == 0 && p == next_p)
2708 new_argv[i] = NULL;
2709 else
2710 {
aef93bd7 2711 /* FIXME: Fail request if out of memory instead of dying. */
bca929d3 2712 new_argv[i] = xmalloc (1 + (next_p - p) / 2);
ff0e980e 2713 hex2bin (p, (gdb_byte *) new_argv[i], (next_p - p) / 2);
2d717e4f
DJ
2714 new_argv[i][(next_p - p) / 2] = '\0';
2715 }
2716
2717 if (*next_p)
2718 next_p++;
2719 i++;
2720 }
2721 new_argv[i] = NULL;
2722
2723 if (new_argv[0] == NULL)
2724 {
f142445f
DJ
2725 /* GDB didn't specify a program to run. Use the program from the
2726 last run with the new argument list. */
9b710a42 2727
2d717e4f
DJ
2728 if (program_argv == NULL)
2729 {
2730 write_enn (own_buf);
b2c04452 2731 freeargv (new_argv);
2d717e4f
DJ
2732 return 0;
2733 }
2734
aef93bd7
DE
2735 new_argv[0] = strdup (program_argv[0]);
2736 if (new_argv[0] == NULL)
2737 {
aef93bd7 2738 write_enn (own_buf);
b2c04452 2739 freeargv (new_argv);
aef93bd7 2740 return 0;
1b3f6016 2741 }
2d717e4f 2742 }
f142445f 2743
aef93bd7
DE
2744 /* Free the old argv and install the new one. */
2745 freeargv (program_argv);
f142445f 2746 program_argv = new_argv;
2d717e4f 2747
5b1c542e
PA
2748 start_inferior (program_argv);
2749 if (last_status.kind == TARGET_WAITKIND_STOPPED)
2d717e4f 2750 {
5b1c542e 2751 prepare_resume_reply (own_buf, last_ptid, &last_status);
bd99dc85
PA
2752
2753 /* In non-stop, sending a resume reply doesn't set the general
2754 thread, but GDB assumes a vRun sets it (this is so GDB can
2755 query which is the main thread of the new inferior. */
2756 if (non_stop)
2757 general_thread = last_ptid;
2758
2d717e4f
DJ
2759 return 1;
2760 }
2761 else
2762 {
2763 write_enn (own_buf);
2764 return 0;
2765 }
2766}
2767
95954743
PA
2768/* Kill process. Return 1 if successful, 0 if failure. */
2769int
2770handle_v_kill (char *own_buf)
2771{
2772 int pid;
2773 char *p = &own_buf[6];
0f54c268
PM
2774 if (multi_process)
2775 pid = strtol (p, NULL, 16);
2776 else
2777 pid = signal_pid;
95954743
PA
2778 if (pid != 0 && kill_inferior (pid) == 0)
2779 {
2780 last_status.kind = TARGET_WAITKIND_SIGNALLED;
a493e3e2 2781 last_status.value.sig = GDB_SIGNAL_KILL;
95954743
PA
2782 last_ptid = pid_to_ptid (pid);
2783 discard_queued_stop_replies (pid);
2784 write_ok (own_buf);
2785 return 1;
2786 }
2787 else
2788 {
2789 write_enn (own_buf);
2790 return 0;
2791 }
2792}
2793
64386c31
DJ
2794/* Handle all of the extended 'v' packets. */
2795void
5b1c542e 2796handle_v_requests (char *own_buf, int packet_len, int *new_packet_len)
64386c31 2797{
db42f210 2798 if (!disable_packet_vCont)
64386c31 2799 {
61012eef 2800 if (startswith (own_buf, "vCont;"))
db42f210
PA
2801 {
2802 require_running (own_buf);
5b1c542e 2803 handle_v_cont (own_buf);
db42f210
PA
2804 return;
2805 }
64386c31 2806
61012eef 2807 if (startswith (own_buf, "vCont?"))
db42f210 2808 {
bd99dc85 2809 strcpy (own_buf, "vCont;c;C;s;S;t");
c2d6af84
PA
2810 if (target_supports_range_stepping ())
2811 {
2812 own_buf = own_buf + strlen (own_buf);
2813 strcpy (own_buf, ";r");
2814 }
db42f210
PA
2815 return;
2816 }
64386c31
DJ
2817 }
2818
61012eef 2819 if (startswith (own_buf, "vFile:")
a6b151f1
DJ
2820 && handle_vFile (own_buf, packet_len, new_packet_len))
2821 return;
2822
61012eef 2823 if (startswith (own_buf, "vAttach;"))
2d717e4f 2824 {
901f9912 2825 if ((!extended_protocol || !multi_process) && target_running ())
2d717e4f 2826 {
fd96d250
PA
2827 fprintf (stderr, "Already debugging a process\n");
2828 write_enn (own_buf);
2829 return;
2d717e4f 2830 }
5b1c542e 2831 handle_v_attach (own_buf);
2d717e4f
DJ
2832 return;
2833 }
2834
61012eef 2835 if (startswith (own_buf, "vRun;"))
2d717e4f 2836 {
901f9912 2837 if ((!extended_protocol || !multi_process) && target_running ())
2d717e4f 2838 {
fd96d250
PA
2839 fprintf (stderr, "Already debugging a process\n");
2840 write_enn (own_buf);
2841 return;
2d717e4f 2842 }
5b1c542e 2843 handle_v_run (own_buf);
2d717e4f
DJ
2844 return;
2845 }
2846
61012eef 2847 if (startswith (own_buf, "vKill;"))
95954743
PA
2848 {
2849 if (!target_running ())
2850 {
2851 fprintf (stderr, "No process to kill\n");
2852 write_enn (own_buf);
2853 return;
2854 }
2855 handle_v_kill (own_buf);
2856 return;
2857 }
2858
14a00470
YQ
2859 if (handle_notif_ack (own_buf, packet_len))
2860 return;
bd99dc85 2861
64386c31
DJ
2862 /* Otherwise we didn't know what packet it was. Say we didn't
2863 understand it. */
2864 own_buf[0] = 0;
2865 return;
2866}
2867
0bfdf32f 2868/* Resume thread and wait for another event. In non-stop mode,
bd99dc85
PA
2869 don't really wait here, but return immediatelly to the event
2870 loop. */
1fd7cdc2 2871static void
5b1c542e 2872myresume (char *own_buf, int step, int sig)
64386c31
DJ
2873{
2874 struct thread_resume resume_info[2];
2875 int n = 0;
2bd7c093 2876 int valid_cont_thread;
a20d5e98 2877
0bfdf32f 2878 set_desired_thread (0);
64386c31 2879
95954743
PA
2880 valid_cont_thread = (!ptid_equal (cont_thread, null_ptid)
2881 && !ptid_equal (cont_thread, minus_one_ptid));
2bd7c093
PA
2882
2883 if (step || sig || valid_cont_thread)
64386c31 2884 {
fbd5db48 2885 resume_info[0].thread = current_ptid;
bd99dc85
PA
2886 if (step)
2887 resume_info[0].kind = resume_step;
2888 else
2889 resume_info[0].kind = resume_continue;
64386c31 2890 resume_info[0].sig = sig;
64386c31
DJ
2891 n++;
2892 }
2bd7c093
PA
2893
2894 if (!valid_cont_thread)
2895 {
95954743 2896 resume_info[n].thread = minus_one_ptid;
bd99dc85 2897 resume_info[n].kind = resume_continue;
2bd7c093
PA
2898 resume_info[n].sig = 0;
2899 n++;
2900 }
64386c31 2901
b7ea362b 2902 resume (resume_info, n);
bd99dc85
PA
2903}
2904
2905/* Callback for for_each_inferior. Make a new stop reply for each
2906 stopped thread. */
2907
95954743
PA
2908static int
2909queue_stop_reply_callback (struct inferior_list_entry *entry, void *arg)
bd99dc85 2910{
8336d594 2911 struct thread_info *thread = (struct thread_info *) entry;
bd99dc85 2912
8336d594
PA
2913 /* For now, assume targets that don't have this callback also don't
2914 manage the thread's last_status field. */
2915 if (the_target->thread_stopped == NULL)
95954743 2916 {
14a00470
YQ
2917 struct vstop_notif *new_notif = xmalloc (sizeof (*new_notif));
2918
2919 new_notif->ptid = entry->id;
2920 new_notif->status = thread->last_status;
8336d594
PA
2921 /* Pass the last stop reply back to GDB, but don't notify
2922 yet. */
14a00470
YQ
2923 notif_event_enque (&notif_stop,
2924 (struct notif_event *) new_notif);
8336d594
PA
2925 }
2926 else
2927 {
2928 if (thread_stopped (thread))
2929 {
2930 if (debug_threads)
3360c0bf
LM
2931 {
2932 char *status_string
2933 = target_waitstatus_to_string (&thread->last_status);
2934
87ce2a04
DE
2935 debug_printf ("Reporting thread %s as already stopped with %s\n",
2936 target_pid_to_str (entry->id),
2937 status_string);
3360c0bf
LM
2938
2939 xfree (status_string);
2940 }
8336d594 2941
d20a8ad9
PA
2942 gdb_assert (thread->last_status.kind != TARGET_WAITKIND_IGNORE);
2943
8336d594
PA
2944 /* Pass the last stop reply back to GDB, but don't notify
2945 yet. */
2946 queue_stop_reply (entry->id, &thread->last_status);
2947 }
95954743
PA
2948 }
2949
2950 return 0;
64386c31
DJ
2951}
2952
ce1a5b52
PA
2953/* Set this inferior threads's state as "want-stopped". We won't
2954 resume this thread until the client gives us another action for
2955 it. */
8336d594
PA
2956
2957static void
2958gdb_wants_thread_stopped (struct inferior_list_entry *entry)
2959{
2960 struct thread_info *thread = (struct thread_info *) entry;
2961
2962 thread->last_resume_kind = resume_stop;
2963
2964 if (thread->last_status.kind == TARGET_WAITKIND_IGNORE)
2965 {
ce1a5b52
PA
2966 /* Most threads are stopped implicitly (all-stop); tag that with
2967 signal 0. */
8336d594 2968 thread->last_status.kind = TARGET_WAITKIND_STOPPED;
a493e3e2 2969 thread->last_status.value.sig = GDB_SIGNAL_0;
8336d594
PA
2970 }
2971}
2972
2973/* Set all threads' states as "want-stopped". */
2974
2975static void
2976gdb_wants_all_threads_stopped (void)
2977{
2978 for_each_inferior (&all_threads, gdb_wants_thread_stopped);
2979}
2980
2981/* Clear the gdb_detached flag of every process. */
2982
2983static void
2984gdb_reattached_process (struct inferior_list_entry *entry)
2985{
2986 struct process_info *process = (struct process_info *) entry;
2987
2988 process->gdb_detached = 0;
2989}
2990
b7ea362b
PA
2991/* Callback for for_each_inferior. Clear the thread's pending status
2992 flag. */
2993
2994static void
2995clear_pending_status_callback (struct inferior_list_entry *entry)
2996{
2997 struct thread_info *thread = (struct thread_info *) entry;
2998
2999 thread->status_pending_p = 0;
3000}
3001
3002/* Callback for for_each_inferior. If the thread is stopped with an
3003 interesting event, mark it as having a pending event. */
3004
3005static void
3006set_pending_status_callback (struct inferior_list_entry *entry)
3007{
3008 struct thread_info *thread = (struct thread_info *) entry;
3009
3010 if (thread->last_status.kind != TARGET_WAITKIND_STOPPED
3011 || (thread->last_status.value.sig != GDB_SIGNAL_0
3012 /* A breakpoint, watchpoint or finished step from a previous
3013 GDB run isn't considered interesting for a new GDB run.
3014 If we left those pending, the new GDB could consider them
3015 random SIGTRAPs. This leaves out real async traps. We'd
3016 have to peek into the (target-specific) siginfo to
3017 distinguish those. */
3018 && thread->last_status.value.sig != GDB_SIGNAL_TRAP))
3019 thread->status_pending_p = 1;
3020}
3021
3022/* Callback for find_inferior. Return true if ENTRY (a thread) has a
3023 pending status to report to GDB. */
3024
3025static int
3026find_status_pending_thread_callback (struct inferior_list_entry *entry, void *data)
3027{
3028 struct thread_info *thread = (struct thread_info *) entry;
3029
3030 return thread->status_pending_p;
3031}
3032
5b1c542e
PA
3033/* Status handler for the '?' packet. */
3034
3035static void
3036handle_status (char *own_buf)
3037{
8336d594
PA
3038 /* GDB is connected, don't forward events to the target anymore. */
3039 for_each_inferior (&all_processes, gdb_reattached_process);
bd99dc85
PA
3040
3041 /* In non-stop mode, we must send a stop reply for each stopped
3042 thread. In all-stop mode, just send one for the first stopped
3043 thread we find. */
3044
3045 if (non_stop)
3046 {
8336d594 3047 find_inferior (&all_threads, queue_stop_reply_callback, NULL);
bd99dc85
PA
3048
3049 /* The first is sent immediatly. OK is sent if there is no
3050 stopped thread, which is the same handling of the vStopped
3051 packet (by design). */
14a00470 3052 notif_write_event (&notif_stop, own_buf);
bd99dc85 3053 }
5b1c542e 3054 else
bd99dc85 3055 {
b7ea362b
PA
3056 struct inferior_list_entry *thread = NULL;
3057
7984d532 3058 pause_all (0);
fa593d66 3059 stabilize_threads ();
8336d594
PA
3060 gdb_wants_all_threads_stopped ();
3061
b7ea362b
PA
3062 /* We can only report one status, but we might be coming out of
3063 non-stop -- if more than one thread is stopped with
3064 interesting events, leave events for the threads we're not
3065 reporting now pending. They'll be reported the next time the
3066 threads are resumed. Start by marking all interesting events
3067 as pending. */
3068 for_each_inferior (&all_threads, set_pending_status_callback);
3069
3070 /* Prefer the last thread that reported an event to GDB (even if
3071 that was a GDB_SIGNAL_TRAP). */
3072 if (last_status.kind != TARGET_WAITKIND_IGNORE
3073 && last_status.kind != TARGET_WAITKIND_EXITED
3074 && last_status.kind != TARGET_WAITKIND_SIGNALLED)
3075 thread = find_inferior_id (&all_threads, last_ptid);
3076
3077 /* If the last event thread is not found for some reason, look
3078 for some other thread that might have an event to report. */
3079 if (thread == NULL)
3080 thread = find_inferior (&all_threads,
3081 find_status_pending_thread_callback, NULL);
3082
3083 /* If we're still out of luck, simply pick the first thread in
3084 the thread list. */
3085 if (thread == NULL)
649ebbca 3086 thread = get_first_inferior (&all_threads);
b7ea362b
PA
3087
3088 if (thread != NULL)
8336d594 3089 {
b7ea362b
PA
3090 struct thread_info *tp = (struct thread_info *) thread;
3091
3092 /* We're reporting this event, so it's no longer
3093 pending. */
3094 tp->status_pending_p = 0;
3095
3096 /* GDB assumes the current thread is the thread we're
3097 reporting the status for. */
3098 general_thread = thread->id;
0bfdf32f 3099 set_desired_thread (1);
8336d594 3100
b7ea362b
PA
3101 gdb_assert (tp->last_status.kind != TARGET_WAITKIND_IGNORE);
3102 prepare_resume_reply (own_buf, tp->entry.id, &tp->last_status);
8336d594 3103 }
bd99dc85
PA
3104 else
3105 strcpy (own_buf, "W00");
3106 }
5b1c542e
PA
3107}
3108
dd24457d
DJ
3109static void
3110gdbserver_version (void)
3111{
c16158bc 3112 printf ("GNU gdbserver %s%s\n"
76f2b779 3113 "Copyright (C) 2015 Free Software Foundation, Inc.\n"
493e2a69
MS
3114 "gdbserver is free software, covered by the "
3115 "GNU General Public License.\n"
dd24457d 3116 "This gdbserver was configured as \"%s\"\n",
c16158bc 3117 PKGVERSION, version, host_name);
dd24457d
DJ
3118}
3119
0bc68c49 3120static void
c16158bc 3121gdbserver_usage (FILE *stream)
0bc68c49 3122{
c16158bc
JM
3123 fprintf (stream, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
3124 "\tgdbserver [OPTIONS] --attach COMM PID\n"
3125 "\tgdbserver [OPTIONS] --multi COMM\n"
3126 "\n"
41f98f02
PA
3127 "COMM may either be a tty device (for serial debugging),\n"
3128 "HOST:PORT to listen for a TCP connection, or '-' or 'stdio' to use \n"
3129 "stdin/stdout of gdbserver.\n"
3130 "PROG is the executable program. ARGS are arguments passed to inferior.\n"
3131 "PID is the process ID to attach to, when --attach is specified.\n"
3132 "\n"
3133 "Operating modes:\n"
3134 "\n"
3135 " --attach Attach to running process PID.\n"
3136 " --multi Start server without a specific program, and\n"
3137 " only quit when explicitly commanded.\n"
3138 " --once Exit after the first connection has closed.\n"
3139 " --help Print this message and then exit.\n"
3140 " --version Display version information and exit.\n"
3141 "\n"
3142 "Other options:\n"
3143 "\n"
3144 " --wrapper WRAPPER -- Run WRAPPER to start new programs.\n"
3145 " --disable-randomization\n"
3146 " Run PROG with address space randomization disabled.\n"
3147 " --no-disable-randomization\n"
3148 " Don't disable address space randomization when\n"
3149 " starting PROG.\n"
3150 "\n"
3151 "Debug options:\n"
c16158bc 3152 "\n"
62709adf 3153 " --debug Enable general debugging output.\n"
87ce2a04
DE
3154 " --debug-format=opt1[,opt2,...]\n"
3155 " Specify extra content in debugging output.\n"
3156 " Options:\n"
3157 " all\n"
3158 " none\n"
87ce2a04 3159 " timestamp\n"
62709adf 3160 " --remote-debug Enable remote protocol debugging output.\n"
41f98f02
PA
3161 " --disable-packet=opt1[,opt2,...]\n"
3162 " Disable support for RSP packets or features.\n"
3163 " Options:\n"
3164 " vCont, Tthread, qC, qfThreadInfo and \n"
3165 " threads (disable all threading packets).\n"
3166 "\n"
3167 "For more information, consult the GDB manual (available as on-line \n"
3168 "info or a printed manual).\n");
c16158bc
JM
3169 if (REPORT_BUGS_TO[0] && stream == stdout)
3170 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
0bc68c49
DJ
3171}
3172
db42f210
PA
3173static void
3174gdbserver_show_disableable (FILE *stream)
3175{
3176 fprintf (stream, "Disableable packets:\n"
3177 " vCont \tAll vCont packets\n"
3178 " qC \tQuerying the current thread\n"
3179 " qfThreadInfo\tThread listing\n"
493e2a69
MS
3180 " Tthread \tPassing the thread specifier in the "
3181 "T stop reply packet\n"
db42f210
PA
3182 " threads \tAll of the above\n");
3183}
3184
3185
2d717e4f
DJ
3186#undef require_running
3187#define require_running(BUF) \
3188 if (!target_running ()) \
3189 { \
3190 write_enn (BUF); \
3191 break; \
3192 }
3193
95954743
PA
3194static int
3195first_thread_of (struct inferior_list_entry *entry, void *args)
3196{
3197 int pid = * (int *) args;
3198
3199 if (ptid_get_pid (entry->id) == pid)
3200 return 1;
3201
3202 return 0;
3203}
3204
3205static void
3206kill_inferior_callback (struct inferior_list_entry *entry)
3207{
3208 struct process_info *process = (struct process_info *) entry;
80894984 3209 int pid = ptid_get_pid (process->entry.id);
95954743
PA
3210
3211 kill_inferior (pid);
3212 discard_queued_stop_replies (pid);
3213}
3214
9f767825
DE
3215/* Callback for for_each_inferior to detach or kill the inferior,
3216 depending on whether we attached to it or not.
3217 We inform the user whether we're detaching or killing the process
3218 as this is only called when gdbserver is about to exit. */
3219
95954743
PA
3220static void
3221detach_or_kill_inferior_callback (struct inferior_list_entry *entry)
3222{
3223 struct process_info *process = (struct process_info *) entry;
80894984 3224 int pid = ptid_get_pid (process->entry.id);
95954743
PA
3225
3226 if (process->attached)
3227 detach_inferior (pid);
3228 else
3229 kill_inferior (pid);
3230
3231 discard_queued_stop_replies (pid);
3232}
3233
9f767825
DE
3234/* for_each_inferior callback for detach_or_kill_for_exit to print
3235 the pids of started inferiors. */
3236
3237static void
3238print_started_pid (struct inferior_list_entry *entry)
3239{
3240 struct process_info *process = (struct process_info *) entry;
3241
3242 if (! process->attached)
3243 {
80894984 3244 int pid = ptid_get_pid (process->entry.id);
9f767825
DE
3245 fprintf (stderr, " %d", pid);
3246 }
3247}
3248
3249/* for_each_inferior callback for detach_or_kill_for_exit to print
3250 the pids of attached inferiors. */
3251
3252static void
3253print_attached_pid (struct inferior_list_entry *entry)
3254{
3255 struct process_info *process = (struct process_info *) entry;
3256
3257 if (process->attached)
3258 {
80894984 3259 int pid = ptid_get_pid (process->entry.id);
9f767825
DE
3260 fprintf (stderr, " %d", pid);
3261 }
3262}
3263
3264/* Call this when exiting gdbserver with possible inferiors that need
3265 to be killed or detached from. */
3266
3267static void
3268detach_or_kill_for_exit (void)
3269{
3270 /* First print a list of the inferiors we will be killing/detaching.
3271 This is to assist the user, for example, in case the inferior unexpectedly
3272 dies after we exit: did we screw up or did the inferior exit on its own?
3273 Having this info will save some head-scratching. */
3274
3275 if (have_started_inferiors_p ())
3276 {
3277 fprintf (stderr, "Killing process(es):");
3278 for_each_inferior (&all_processes, print_started_pid);
3279 fprintf (stderr, "\n");
3280 }
3281 if (have_attached_inferiors_p ())
3282 {
3283 fprintf (stderr, "Detaching process(es):");
3284 for_each_inferior (&all_processes, print_attached_pid);
3285 fprintf (stderr, "\n");
3286 }
3287
3288 /* Now we can kill or detach the inferiors. */
3289
3290 for_each_inferior (&all_processes, detach_or_kill_inferior_callback);
3291}
3292
860789c7
GB
3293/* Value that will be passed to exit(3) when gdbserver exits. */
3294static int exit_code;
3295
3296/* Cleanup version of detach_or_kill_for_exit. */
3297
3298static void
3299detach_or_kill_for_exit_cleanup (void *ignore)
3300{
860789c7 3301
492d29ea 3302 TRY
860789c7
GB
3303 {
3304 detach_or_kill_for_exit ();
3305 }
3306
492d29ea 3307 CATCH (exception, RETURN_MASK_ALL)
860789c7
GB
3308 {
3309 fflush (stdout);
3310 fprintf (stderr, "Detach or kill failed: %s\n", exception.message);
3311 exit_code = 1;
3312 }
492d29ea 3313 END_CATCH
860789c7
GB
3314}
3315
3316/* Main function. This is called by the real "main" function,
3317 wrapped in a TRY_CATCH that handles any uncaught exceptions. */
3318
3319static void ATTRIBUTE_NORETURN
3320captured_main (int argc, char *argv[])
c906108c 3321{
0729219d
DJ
3322 int bad_attach;
3323 int pid;
2d717e4f
DJ
3324 char *arg_end, *port;
3325 char **next_arg = &argv[1];
89dc0afd
JK
3326 volatile int multi_mode = 0;
3327 volatile int attach = 0;
2d717e4f 3328 int was_running;
c906108c 3329
2d717e4f 3330 while (*next_arg != NULL && **next_arg == '-')
dd24457d 3331 {
2d717e4f
DJ
3332 if (strcmp (*next_arg, "--version") == 0)
3333 {
3334 gdbserver_version ();
3335 exit (0);
3336 }
3337 else if (strcmp (*next_arg, "--help") == 0)
3338 {
c16158bc 3339 gdbserver_usage (stdout);
2d717e4f
DJ
3340 exit (0);
3341 }
3342 else if (strcmp (*next_arg, "--attach") == 0)
3343 attach = 1;
3344 else if (strcmp (*next_arg, "--multi") == 0)
3345 multi_mode = 1;
ccd213ac
DJ
3346 else if (strcmp (*next_arg, "--wrapper") == 0)
3347 {
3348 next_arg++;
3349
3350 wrapper_argv = next_arg;
3351 while (*next_arg != NULL && strcmp (*next_arg, "--") != 0)
3352 next_arg++;
3353
3354 if (next_arg == wrapper_argv || *next_arg == NULL)
3355 {
c16158bc 3356 gdbserver_usage (stderr);
ccd213ac
DJ
3357 exit (1);
3358 }
3359
3360 /* Consume the "--". */
3361 *next_arg = NULL;
3362 }
2d717e4f
DJ
3363 else if (strcmp (*next_arg, "--debug") == 0)
3364 debug_threads = 1;
61012eef 3365 else if (startswith (*next_arg, "--debug-format="))
87ce2a04
DE
3366 {
3367 char *error_msg
3368 = parse_debug_format_options ((*next_arg)
3369 + sizeof ("--debug-format=") - 1, 0);
3370
3371 if (error_msg != NULL)
3372 {
3373 fprintf (stderr, "%s", error_msg);
3374 exit (1);
3375 }
3376 }
62709adf
PA
3377 else if (strcmp (*next_arg, "--remote-debug") == 0)
3378 remote_debug = 1;
db42f210
PA
3379 else if (strcmp (*next_arg, "--disable-packet") == 0)
3380 {
3381 gdbserver_show_disableable (stdout);
3382 exit (0);
3383 }
61012eef 3384 else if (startswith (*next_arg, "--disable-packet="))
db42f210
PA
3385 {
3386 char *packets, *tok;
3387
3388 packets = *next_arg += sizeof ("--disable-packet=") - 1;
3389 for (tok = strtok (packets, ",");
3390 tok != NULL;
3391 tok = strtok (NULL, ","))
3392 {
3393 if (strcmp ("vCont", tok) == 0)
3394 disable_packet_vCont = 1;
3395 else if (strcmp ("Tthread", tok) == 0)
3396 disable_packet_Tthread = 1;
3397 else if (strcmp ("qC", tok) == 0)
3398 disable_packet_qC = 1;
3399 else if (strcmp ("qfThreadInfo", tok) == 0)
3400 disable_packet_qfThreadInfo = 1;
3401 else if (strcmp ("threads", tok) == 0)
3402 {
3403 disable_packet_vCont = 1;
3404 disable_packet_Tthread = 1;
3405 disable_packet_qC = 1;
3406 disable_packet_qfThreadInfo = 1;
3407 }
3408 else
3409 {
3410 fprintf (stderr, "Don't know how to disable \"%s\".\n\n",
3411 tok);
3412 gdbserver_show_disableable (stderr);
3413 exit (1);
3414 }
3415 }
3416 }
e0f9f062
DE
3417 else if (strcmp (*next_arg, "-") == 0)
3418 {
3419 /* "-" specifies a stdio connection and is a form of port
3420 specification. */
3421 *next_arg = STDIO_CONNECTION_NAME;
3422 break;
3423 }
03583c20
UW
3424 else if (strcmp (*next_arg, "--disable-randomization") == 0)
3425 disable_randomization = 1;
3426 else if (strcmp (*next_arg, "--no-disable-randomization") == 0)
3427 disable_randomization = 0;
03f2bd59
JK
3428 else if (strcmp (*next_arg, "--once") == 0)
3429 run_once = 1;
2d717e4f
DJ
3430 else
3431 {
3432 fprintf (stderr, "Unknown argument: %s\n", *next_arg);
3433 exit (1);
3434 }
dd24457d 3435
2d717e4f
DJ
3436 next_arg++;
3437 continue;
dd24457d
DJ
3438 }
3439
2d717e4f
DJ
3440 port = *next_arg;
3441 next_arg++;
3442 if (port == NULL || (!attach && !multi_mode && *next_arg == NULL))
3443 {
c16158bc 3444 gdbserver_usage (stderr);
2d717e4f
DJ
3445 exit (1);
3446 }
3447
602e3198
JK
3448 /* Remember stdio descriptors. LISTEN_DESC must not be listed, it will be
3449 opened by remote_prepare. */
3450 notice_open_fds ();
3451
e0f9f062
DE
3452 /* We need to know whether the remote connection is stdio before
3453 starting the inferior. Inferiors created in this scenario have
3454 stdin,stdout redirected. So do this here before we call
3455 start_inferior. */
3456 remote_prepare (port);
3457
0729219d
DJ
3458 bad_attach = 0;
3459 pid = 0;
2d717e4f
DJ
3460
3461 /* --attach used to come after PORT, so allow it there for
3462 compatibility. */
3463 if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0)
45b7b345 3464 {
2d717e4f
DJ
3465 attach = 1;
3466 next_arg++;
45b7b345
DJ
3467 }
3468
2d717e4f
DJ
3469 if (attach
3470 && (*next_arg == NULL
3471 || (*next_arg)[0] == '\0'
3472 || (pid = strtoul (*next_arg, &arg_end, 0)) == 0
3473 || *arg_end != '\0'
3474 || next_arg[1] != NULL))
3475 bad_attach = 1;
3476
3477 if (bad_attach)
dd24457d 3478 {
c16158bc 3479 gdbserver_usage (stderr);
dd24457d
DJ
3480 exit (1);
3481 }
c906108c 3482
a20d5e98 3483 initialize_async_io ();
4ce44c66 3484 initialize_low ();
60f662b0 3485 initialize_event_loop ();
219f2f23
PA
3486 if (target_supports_tracepoints ())
3487 initialize_tracepoint ();
4ce44c66 3488
bca929d3
DE
3489 own_buf = xmalloc (PBUFSIZ + 1);
3490 mem_buf = xmalloc (PBUFSIZ);
0a30fbc4 3491
2d717e4f 3492 if (pid == 0 && *next_arg != NULL)
45b7b345 3493 {
2d717e4f
DJ
3494 int i, n;
3495
3496 n = argc - (next_arg - argv);
bca929d3 3497 program_argv = xmalloc (sizeof (char *) * (n + 1));
2d717e4f 3498 for (i = 0; i < n; i++)
bca929d3 3499 program_argv[i] = xstrdup (next_arg[i]);
2d717e4f
DJ
3500 program_argv[i] = NULL;
3501
45b7b345 3502 /* Wait till we are at first instruction in program. */
5b1c542e 3503 start_inferior (program_argv);
c906108c 3504
c588c53c
MS
3505 /* We are now (hopefully) stopped at the first instruction of
3506 the target process. This assumes that the target process was
3507 successfully created. */
45b7b345 3508 }
2d717e4f
DJ
3509 else if (pid != 0)
3510 {
5b1c542e 3511 if (attach_inferior (pid) == -1)
2d717e4f
DJ
3512 error ("Attaching not supported on this target");
3513
3514 /* Otherwise succeeded. */
3515 }
45b7b345
DJ
3516 else
3517 {
5b1c542e
PA
3518 last_status.kind = TARGET_WAITKIND_EXITED;
3519 last_status.value.integer = 0;
95954743 3520 last_ptid = minus_one_ptid;
45b7b345 3521 }
860789c7 3522 make_cleanup (detach_or_kill_for_exit_cleanup, NULL);
c906108c 3523
14a00470
YQ
3524 initialize_notif ();
3525
311de423
PA
3526 /* Don't report shared library events on the initial connection,
3527 even if some libraries are preloaded. Avoids the "stopped by
3528 shared library event" notice on gdb side. */
3529 dlls_changed = 0;
3530
5b1c542e
PA
3531 if (last_status.kind == TARGET_WAITKIND_EXITED
3532 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
2d717e4f
DJ
3533 was_running = 0;
3534 else
3535 was_running = 1;
3536
3537 if (!was_running && !multi_mode)
860789c7 3538 error ("No program to debug");
c588c53c 3539
c906108c
SS
3540 while (1)
3541 {
860789c7 3542
a6f3e723 3543 noack_mode = 0;
95954743 3544 multi_process = 0;
89245bc0
DB
3545 report_fork_events = 0;
3546 report_vfork_events = 0;
8336d594
PA
3547 /* Be sure we're out of tfind mode. */
3548 current_traceframe = -1;
40e91bc7 3549 cont_thread = null_ptid;
1ec68e26
PA
3550 swbreak_feature = 0;
3551 hwbreak_feature = 0;
bd99dc85 3552
2d717e4f 3553 remote_open (port);
c906108c 3554
492d29ea 3555 TRY
2d717e4f 3556 {
860789c7
GB
3557 /* Wait for events. This will return when all event sources
3558 are removed from the event loop. */
3559 start_event_loop ();
2d717e4f 3560
860789c7
GB
3561 /* If an exit was requested (using the "monitor exit"
3562 command), terminate now. The only other way to get
3563 here is for getpkt to fail; close the connection
3564 and reopen it at the top of the loop. */
bd99dc85 3565
860789c7
GB
3566 if (exit_requested || run_once)
3567 throw_quit ("Quit");
bd99dc85 3568
860789c7
GB
3569 fprintf (stderr,
3570 "Remote side has terminated connection. "
3571 "GDBserver will reopen the connection.\n");
8336d594 3572
860789c7
GB
3573 /* Get rid of any pending statuses. An eventual reconnection
3574 (by the same GDB instance or another) will refresh all its
3575 state from scratch. */
3576 discard_queued_stop_replies (-1);
3577 for_each_inferior (&all_threads,
3578 clear_pending_status_callback);
9939e131 3579
860789c7 3580 if (tracing)
8336d594 3581 {
860789c7 3582 if (disconnected_tracing)
8336d594 3583 {
860789c7
GB
3584 /* Try to enable non-stop/async mode, so we we can
3585 both wait for an async socket accept, and handle
3586 async target events simultaneously. There's also
3587 no point either in having the target always stop
3588 all threads, when we're going to pass signals
3589 down without informing GDB. */
3590 if (!non_stop)
3591 {
3592 if (start_non_stop (1))
3593 non_stop = 1;
3594
3595 /* Detaching implicitly resumes all threads;
3596 simply disconnecting does not. */
3597 }
3598 }
3599 else
3600 {
3601 fprintf (stderr,
3602 "Disconnected tracing disabled; "
3603 "stopping trace run.\n");
3604 stop_tracing ();
8336d594
PA
3605 }
3606 }
860789c7 3607 }
492d29ea 3608 CATCH (exception, RETURN_MASK_ERROR)
860789c7
GB
3609 {
3610 if (response_needed)
8336d594 3611 {
860789c7
GB
3612 write_enn (own_buf);
3613 putpkt (own_buf);
8336d594
PA
3614 }
3615 }
492d29ea 3616 END_CATCH
bd99dc85
PA
3617 }
3618}
01f9e8fa 3619
860789c7
GB
3620/* Main function. */
3621
3622int
3623main (int argc, char *argv[])
3624{
860789c7 3625
492d29ea 3626 TRY
860789c7
GB
3627 {
3628 captured_main (argc, argv);
3629 }
492d29ea 3630 CATCH (exception, RETURN_MASK_ALL)
860789c7 3631 {
492d29ea
PA
3632 if (exception.reason == RETURN_ERROR)
3633 {
3634 fflush (stdout);
3635 fprintf (stderr, "%s\n", exception.message);
3636 fprintf (stderr, "Exiting\n");
3637 exit_code = 1;
3638 }
3639
3640 exit (exit_code);
860789c7 3641 }
492d29ea 3642 END_CATCH
860789c7 3643
492d29ea 3644 gdb_assert_not_reached ("captured_main should never return");
860789c7
GB
3645}
3646
d708bcd1
PA
3647/* Skip PACKET until the next semi-colon (or end of string). */
3648
3649static void
3650skip_to_semicolon (char **packet)
3651{
3652 while (**packet != '\0' && **packet != ';')
3653 (*packet)++;
3654}
3655
802e8e6d
PA
3656/* Process options coming from Z packets for a breakpoint. PACKET is
3657 the packet buffer. *PACKET is updated to point to the first char
3658 after the last processed option. */
9f3a5c85
LM
3659
3660static void
802e8e6d 3661process_point_options (struct breakpoint *bp, char **packet)
9f3a5c85
LM
3662{
3663 char *dataptr = *packet;
d3ce09f5 3664 int persist;
9f3a5c85
LM
3665
3666 /* Check if data has the correct format. */
3667 if (*dataptr != ';')
3668 return;
3669
3670 dataptr++;
3671
3672 while (*dataptr)
3673 {
d3ce09f5
SS
3674 if (*dataptr == ';')
3675 ++dataptr;
3676
3677 if (*dataptr == 'X')
9f3a5c85 3678 {
d3ce09f5 3679 /* Conditional expression. */
d171ca78 3680 if (debug_threads)
87ce2a04 3681 debug_printf ("Found breakpoint condition.\n");
802e8e6d 3682 if (!add_breakpoint_condition (bp, &dataptr))
d708bcd1 3683 skip_to_semicolon (&dataptr);
d3ce09f5 3684 }
61012eef 3685 else if (startswith (dataptr, "cmds:"))
d3ce09f5
SS
3686 {
3687 dataptr += strlen ("cmds:");
3688 if (debug_threads)
87ce2a04 3689 debug_printf ("Found breakpoint commands %s.\n", dataptr);
d3ce09f5
SS
3690 persist = (*dataptr == '1');
3691 dataptr += 2;
802e8e6d 3692 if (add_breakpoint_commands (bp, &dataptr, persist))
d708bcd1 3693 skip_to_semicolon (&dataptr);
d3ce09f5
SS
3694 }
3695 else
3696 {
d3ce09f5
SS
3697 fprintf (stderr, "Unknown token %c, ignoring.\n",
3698 *dataptr);
78a99e91 3699 /* Skip tokens until we find one that we recognize. */
d708bcd1 3700 skip_to_semicolon (&dataptr);
9f3a5c85 3701 }
9f3a5c85
LM
3702 }
3703 *packet = dataptr;
3704}
3705
bd99dc85
PA
3706/* Event loop callback that handles a serial event. The first byte in
3707 the serial buffer gets us here. We expect characters to arrive at
3708 a brisk pace, so we read the rest of the packet with a blocking
3709 getpkt call. */
01f9e8fa 3710
8336d594 3711static int
bd99dc85
PA
3712process_serial_event (void)
3713{
3714 char ch;
3715 int i = 0;
3716 int signal;
3717 unsigned int len;
764880b7 3718 int res;
bd99dc85 3719 CORE_ADDR mem_addr;
95954743 3720 int pid;
bd99dc85
PA
3721 unsigned char sig;
3722 int packet_len;
3723 int new_packet_len = -1;
3724
3725 /* Used to decide when gdbserver should exit in
3726 multi-mode/remote. */
3727 static int have_ran = 0;
3728
3729 if (!have_ran)
3730 have_ran = target_running ();
3731
3732 disable_async_io ();
3733
3734 response_needed = 0;
3735 packet_len = getpkt (own_buf);
3736 if (packet_len <= 0)
3737 {
bd99dc85 3738 remote_close ();
8336d594
PA
3739 /* Force an event loop break. */
3740 return -1;
bd99dc85
PA
3741 }
3742 response_needed = 1;
3743
3744 i = 0;
3745 ch = own_buf[i++];
3746 switch (ch)
3747 {
3748 case 'q':
3749 handle_query (own_buf, packet_len, &new_packet_len);
3750 break;
3751 case 'Q':
3752 handle_general_set (own_buf);
3753 break;
3754 case 'D':
3755 require_running (own_buf);
95954743
PA
3756
3757 if (multi_process)
3758 {
3759 i++; /* skip ';' */
3760 pid = strtol (&own_buf[i], NULL, 16);
3761 }
3762 else
fbd5db48 3763 pid = ptid_get_pid (current_ptid);
95954743 3764
d3ce09f5 3765 if ((tracing && disconnected_tracing) || any_persistent_commands ())
8336d594
PA
3766 {
3767 struct thread_resume resume_info;
3768 struct process_info *process = find_process_pid (pid);
3769
3770 if (process == NULL)
3771 {
3772 write_enn (own_buf);
3773 break;
3774 }
3775
d3ce09f5
SS
3776 if (tracing && disconnected_tracing)
3777 fprintf (stderr,
3778 "Disconnected tracing in effect, "
3779 "leaving gdbserver attached to the process\n");
3780
3781 if (any_persistent_commands ())
3782 fprintf (stderr,
3783 "Persistent commands are present, "
3784 "leaving gdbserver attached to the process\n");
8336d594
PA
3785
3786 /* Make sure we're in non-stop/async mode, so we we can both
3787 wait for an async socket accept, and handle async target
3788 events simultaneously. There's also no point either in
3789 having the target stop all threads, when we're going to
3790 pass signals down without informing GDB. */
3791 if (!non_stop)
3792 {
3793 if (debug_threads)
87ce2a04 3794 debug_printf ("Forcing non-stop mode\n");
8336d594
PA
3795
3796 non_stop = 1;
3797 start_non_stop (1);
3798 }
3799
3800 process->gdb_detached = 1;
3801
3802 /* Detaching implicitly resumes all threads. */
3803 resume_info.thread = minus_one_ptid;
3804 resume_info.kind = resume_continue;
3805 resume_info.sig = 0;
3806 (*the_target->resume) (&resume_info, 1);
3807
3808 write_ok (own_buf);
3809 break; /* from switch/case */
3810 }
3811
95954743 3812 fprintf (stderr, "Detaching from process %d\n", pid);
8336d594 3813 stop_tracing ();
95954743 3814 if (detach_inferior (pid) != 0)
bd99dc85
PA
3815 write_enn (own_buf);
3816 else
3817 {
95954743 3818 discard_queued_stop_replies (pid);
bd99dc85
PA
3819 write_ok (own_buf);
3820
3821 if (extended_protocol)
c906108c 3822 {
bd99dc85
PA
3823 /* Treat this like a normal program exit. */
3824 last_status.kind = TARGET_WAITKIND_EXITED;
3825 last_status.value.integer = 0;
95954743 3826 last_ptid = pid_to_ptid (pid);
2d717e4f 3827
0bfdf32f 3828 current_thread = NULL;
bd99dc85
PA
3829 }
3830 else
3831 {
3832 putpkt (own_buf);
3833 remote_close ();
3834
3835 /* If we are attached, then we can exit. Otherwise, we
3836 need to hang around doing nothing, until the child is
3837 gone. */
71f55dd8 3838 join_inferior (pid);
bd99dc85
PA
3839 exit (0);
3840 }
3841 }
3842 break;
3843 case '!':
3844 extended_protocol = 1;
3845 write_ok (own_buf);
3846 break;
3847 case '?':
3848 handle_status (own_buf);
3849 break;
3850 case 'H':
3851 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
3852 {
95954743
PA
3853 ptid_t gdb_id, thread_id;
3854 int pid;
bd99dc85
PA
3855
3856 require_running (own_buf);
95954743
PA
3857
3858 gdb_id = read_ptid (&own_buf[2], NULL);
3859
3860 pid = ptid_get_pid (gdb_id);
3861
3862 if (ptid_equal (gdb_id, null_ptid)
3863 || ptid_equal (gdb_id, minus_one_ptid))
3864 thread_id = null_ptid;
3865 else if (pid != 0
3866 && ptid_equal (pid_to_ptid (pid),
3867 gdb_id))
3868 {
3869 struct thread_info *thread =
3870 (struct thread_info *) find_inferior (&all_threads,
3871 first_thread_of,
3872 &pid);
3873 if (!thread)
3874 {
3875 write_enn (own_buf);
3876 break;
3877 }
3878
80894984 3879 thread_id = thread->entry.id;
95954743 3880 }
bd99dc85
PA
3881 else
3882 {
3883 thread_id = gdb_id_to_thread_id (gdb_id);
95954743 3884 if (ptid_equal (thread_id, null_ptid))
c906108c 3885 {
a06660f7 3886 write_enn (own_buf);
c906108c
SS
3887 break;
3888 }
c906108c
SS
3889 }
3890
bd99dc85 3891 if (own_buf[1] == 'g')
c906108c 3892 {
95954743 3893 if (ptid_equal (thread_id, null_ptid))
c906108c 3894 {
bd99dc85
PA
3895 /* GDB is telling us to choose any thread. Check if
3896 the currently selected thread is still valid. If
3897 it is not, select the first available. */
3898 struct thread_info *thread =
3899 (struct thread_info *) find_inferior_id (&all_threads,
3900 general_thread);
3901 if (thread == NULL)
649ebbca
DE
3902 {
3903 thread = get_first_thread ();
3904 thread_id = thread->entry.id;
3905 }
c906108c 3906 }
bd99dc85
PA
3907
3908 general_thread = thread_id;
0bfdf32f 3909 set_desired_thread (1);
c906108c 3910 }
bd99dc85
PA
3911 else if (own_buf[1] == 'c')
3912 cont_thread = thread_id;
c906108c 3913
bd99dc85
PA
3914 write_ok (own_buf);
3915 }
3916 else
3917 {
3918 /* Silently ignore it so that gdb can extend the protocol
3919 without compatibility headaches. */
3920 own_buf[0] = '\0';
2d717e4f 3921 }
bd99dc85
PA
3922 break;
3923 case 'g':
219f2f23
PA
3924 require_running (own_buf);
3925 if (current_traceframe >= 0)
3926 {
3aee8918
PA
3927 struct regcache *regcache
3928 = new_register_cache (current_target_desc ());
219f2f23
PA
3929
3930 if (fetch_traceframe_registers (current_traceframe,
3931 regcache, -1) == 0)
3932 registers_to_string (regcache, own_buf);
3933 else
3934 write_enn (own_buf);
3935 free_register_cache (regcache);
3936 }
3937 else
3938 {
3939 struct regcache *regcache;
3940
0bfdf32f
GB
3941 set_desired_thread (1);
3942 regcache = get_thread_regcache (current_thread, 1);
219f2f23
PA
3943 registers_to_string (regcache, own_buf);
3944 }
bd99dc85
PA
3945 break;
3946 case 'G':
219f2f23
PA
3947 require_running (own_buf);
3948 if (current_traceframe >= 0)
3949 write_enn (own_buf);
3950 else
3951 {
442ea881
PA
3952 struct regcache *regcache;
3953
0bfdf32f
GB
3954 set_desired_thread (1);
3955 regcache = get_thread_regcache (current_thread, 1);
442ea881
PA
3956 registers_from_string (regcache, &own_buf[1]);
3957 write_ok (own_buf);
3958 }
bd99dc85
PA
3959 break;
3960 case 'm':
3961 require_running (own_buf);
3962 decode_m_packet (&own_buf[1], &mem_addr, &len);
764880b7
PA
3963 res = gdb_read_memory (mem_addr, mem_buf, len);
3964 if (res < 0)
bd99dc85 3965 write_enn (own_buf);
764880b7 3966 else
e9371aff 3967 bin2hex (mem_buf, own_buf, res);
bd99dc85
PA
3968 break;
3969 case 'M':
3970 require_running (own_buf);
fa593d66 3971 decode_M_packet (&own_buf[1], &mem_addr, &len, &mem_buf);
90d74c30 3972 if (gdb_write_memory (mem_addr, mem_buf, len) == 0)
bd99dc85
PA
3973 write_ok (own_buf);
3974 else
3975 write_enn (own_buf);
3976 break;
3977 case 'X':
3978 require_running (own_buf);
3979 if (decode_X_packet (&own_buf[1], packet_len - 1,
fa593d66 3980 &mem_addr, &len, &mem_buf) < 0
90d74c30 3981 || gdb_write_memory (mem_addr, mem_buf, len) != 0)
bd99dc85
PA
3982 write_enn (own_buf);
3983 else
3984 write_ok (own_buf);
3985 break;
3986 case 'C':
3987 require_running (own_buf);
a7191e8b 3988 hex2bin (own_buf + 1, &sig, 1);
2ea28649
PA
3989 if (gdb_signal_to_host_p (sig))
3990 signal = gdb_signal_to_host (sig);
bd99dc85
PA
3991 else
3992 signal = 0;
3993 myresume (own_buf, 0, signal);
3994 break;
3995 case 'S':
3996 require_running (own_buf);
a7191e8b 3997 hex2bin (own_buf + 1, &sig, 1);
2ea28649
PA
3998 if (gdb_signal_to_host_p (sig))
3999 signal = gdb_signal_to_host (sig);
bd99dc85
PA
4000 else
4001 signal = 0;
4002 myresume (own_buf, 1, signal);
4003 break;
4004 case 'c':
4005 require_running (own_buf);
4006 signal = 0;
4007 myresume (own_buf, 0, signal);
4008 break;
4009 case 's':
4010 require_running (own_buf);
4011 signal = 0;
4012 myresume (own_buf, 1, signal);
4013 break;
c6314022
AR
4014 case 'Z': /* insert_ ... */
4015 /* Fallthrough. */
4016 case 'z': /* remove_ ... */
bd99dc85 4017 {
bd99dc85 4018 char *dataptr;
aca22551
PA
4019 ULONGEST addr;
4020 int len;
bd99dc85 4021 char type = own_buf[1];
c6314022 4022 int res;
d993e290 4023 const int insert = ch == 'Z';
aca22551
PA
4024 char *p = &own_buf[3];
4025
4026 p = unpack_varlen_hex (p, &addr);
4027 len = strtol (p + 1, &dataptr, 16);
c6314022 4028
802e8e6d 4029 if (insert)
d993e290 4030 {
802e8e6d
PA
4031 struct breakpoint *bp;
4032
4033 bp = set_gdb_breakpoint (type, addr, len, &res);
4034 if (bp != NULL)
9f3a5c85 4035 {
802e8e6d
PA
4036 res = 0;
4037
4038 /* GDB may have sent us a list of *point parameters to
4039 be evaluated on the target's side. Read such list
4040 here. If we already have a list of parameters, GDB
4041 is telling us to drop that list and use this one
4042 instead. */
0a261ed8 4043 clear_breakpoint_conditions_and_commands (bp);
802e8e6d 4044 process_point_options (bp, &dataptr);
9f3a5c85 4045 }
d993e290 4046 }
802e8e6d
PA
4047 else
4048 res = delete_gdb_breakpoint (type, addr, len);
bd99dc85 4049
c6314022
AR
4050 if (res == 0)
4051 write_ok (own_buf);
4052 else if (res == 1)
4053 /* Unsupported. */
4054 own_buf[0] = '\0';
bd99dc85 4055 else
c6314022 4056 write_enn (own_buf);
bd99dc85
PA
4057 break;
4058 }
4059 case 'k':
4060 response_needed = 0;
4061 if (!target_running ())
95954743
PA
4062 /* The packet we received doesn't make sense - but we can't
4063 reply to it, either. */
8336d594 4064 return 0;
c906108c 4065
95954743
PA
4066 fprintf (stderr, "Killing all inferiors\n");
4067 for_each_inferior (&all_processes, kill_inferior_callback);
c906108c 4068
bd99dc85
PA
4069 /* When using the extended protocol, we wait with no program
4070 running. The traditional protocol will exit instead. */
4071 if (extended_protocol)
4072 {
4073 last_status.kind = TARGET_WAITKIND_EXITED;
a493e3e2 4074 last_status.value.sig = GDB_SIGNAL_KILL;
8336d594 4075 return 0;
bd99dc85
PA
4076 }
4077 else
8336d594
PA
4078 exit (0);
4079
bd99dc85
PA
4080 case 'T':
4081 {
95954743 4082 ptid_t gdb_id, thread_id;
bd99dc85
PA
4083
4084 require_running (own_buf);
95954743
PA
4085
4086 gdb_id = read_ptid (&own_buf[1], NULL);
bd99dc85 4087 thread_id = gdb_id_to_thread_id (gdb_id);
95954743 4088 if (ptid_equal (thread_id, null_ptid))
bd99dc85
PA
4089 {
4090 write_enn (own_buf);
4091 break;
4092 }
4093
4094 if (mythread_alive (thread_id))
4095 write_ok (own_buf);
4096 else
4097 write_enn (own_buf);
4098 }
4099 break;
4100 case 'R':
4101 response_needed = 0;
4102
4103 /* Restarting the inferior is only supported in the extended
4104 protocol. */
4105 if (extended_protocol)
4106 {
4107 if (target_running ())
95954743
PA
4108 for_each_inferior (&all_processes,
4109 kill_inferior_callback);
bd99dc85
PA
4110 fprintf (stderr, "GDBserver restarting\n");
4111
4112 /* Wait till we are at 1st instruction in prog. */
4113 if (program_argv != NULL)
51aee833
YQ
4114 {
4115 start_inferior (program_argv);
4116 if (last_status.kind == TARGET_WAITKIND_STOPPED)
4117 {
4118 /* Stopped at the first instruction of the target
4119 process. */
4120 general_thread = last_ptid;
4121 }
4122 else
4123 {
4124 /* Something went wrong. */
4125 general_thread = null_ptid;
4126 }
4127 }
bd99dc85
PA
4128 else
4129 {
4130 last_status.kind = TARGET_WAITKIND_EXITED;
a493e3e2 4131 last_status.value.sig = GDB_SIGNAL_KILL;
bd99dc85 4132 }
8336d594 4133 return 0;
c906108c
SS
4134 }
4135 else
4136 {
bd99dc85
PA
4137 /* It is a request we don't understand. Respond with an
4138 empty packet so that gdb knows that we don't support this
4139 request. */
4140 own_buf[0] = '\0';
4141 break;
4142 }
4143 case 'v':
4144 /* Extended (long) request. */
4145 handle_v_requests (own_buf, packet_len, &new_packet_len);
4146 break;
4147
4148 default:
4149 /* It is a request we don't understand. Respond with an empty
4150 packet so that gdb knows that we don't support this
4151 request. */
4152 own_buf[0] = '\0';
4153 break;
4154 }
4155
4156 if (new_packet_len != -1)
4157 putpkt_binary (own_buf, new_packet_len);
4158 else
4159 putpkt (own_buf);
4160
4161 response_needed = 0;
4162
4163 if (!extended_protocol && have_ran && !target_running ())
4164 {
4165 /* In non-stop, defer exiting until GDB had a chance to query
4166 the whole vStopped list (until it gets an OK). */
14a00470 4167 if (QUEUE_is_empty (notif_event_p, notif_stop.queue))
bd99dc85 4168 {
1a3d890b
PA
4169 /* Be transparent when GDB is connected through stdio -- no
4170 need to spam GDB's console. */
4171 if (!remote_connection_is_stdio ())
4172 fprintf (stderr, "GDBserver exiting\n");
c906108c 4173 remote_close ();
bd99dc85 4174 exit (0);
c906108c
SS
4175 }
4176 }
8336d594
PA
4177
4178 if (exit_requested)
4179 return -1;
4180
4181 return 0;
c906108c 4182}
bd99dc85
PA
4183
4184/* Event-loop callback for serial events. */
4185
8336d594 4186int
bd99dc85
PA
4187handle_serial_event (int err, gdb_client_data client_data)
4188{
4189 if (debug_threads)
87ce2a04 4190 debug_printf ("handling possible serial event\n");
bd99dc85
PA
4191
4192 /* Really handle it. */
8336d594
PA
4193 if (process_serial_event () < 0)
4194 return -1;
bd99dc85 4195
0bfdf32f 4196 /* Be sure to not change the selected thread behind GDB's back.
bd99dc85 4197 Important in the non-stop mode asynchronous protocol. */
0bfdf32f 4198 set_desired_thread (1);
8336d594
PA
4199
4200 return 0;
bd99dc85
PA
4201}
4202
4203/* Event-loop callback for target events. */
4204
8336d594 4205int
bd99dc85
PA
4206handle_target_event (int err, gdb_client_data client_data)
4207{
4208 if (debug_threads)
87ce2a04 4209 debug_printf ("handling possible target event\n");
bd99dc85 4210
95954743
PA
4211 last_ptid = mywait (minus_one_ptid, &last_status,
4212 TARGET_WNOHANG, 1);
bd99dc85 4213
fa96cb38
PA
4214 if (last_status.kind == TARGET_WAITKIND_NO_RESUMED)
4215 {
4216 /* No RSP support for this yet. */
4217 }
4218 else if (last_status.kind != TARGET_WAITKIND_IGNORE)
bd99dc85 4219 {
8336d594
PA
4220 int pid = ptid_get_pid (last_ptid);
4221 struct process_info *process = find_process_pid (pid);
4222 int forward_event = !gdb_connected () || process->gdb_detached;
4223
4224 if (last_status.kind == TARGET_WAITKIND_EXITED
4225 || last_status.kind == TARGET_WAITKIND_SIGNALLED)
f9e39928
PA
4226 {
4227 mark_breakpoints_out (process);
4228 mourn_inferior (process);
4229 }
ce1a5b52 4230 else
d20a8ad9
PA
4231 {
4232 /* We're reporting this thread as stopped. Update its
4233 "want-stopped" state to what the client wants, until it
4234 gets a new resume action. */
0bfdf32f
GB
4235 current_thread->last_resume_kind = resume_stop;
4236 current_thread->last_status = last_status;
d20a8ad9 4237 }
8336d594
PA
4238
4239 if (forward_event)
4240 {
4241 if (!target_running ())
4242 {
4243 /* The last process exited. We're done. */
4244 exit (0);
4245 }
4246
4247 if (last_status.kind == TARGET_WAITKIND_STOPPED)
4248 {
4249 /* A thread stopped with a signal, but gdb isn't
4250 connected to handle it. Pass it down to the
4251 inferior, as if it wasn't being traced. */
4252 struct thread_resume resume_info;
4253
4254 if (debug_threads)
87ce2a04
DE
4255 debug_printf ("GDB not connected; forwarding event %d for"
4256 " [%s]\n",
4257 (int) last_status.kind,
4258 target_pid_to_str (last_ptid));
8336d594
PA
4259
4260 resume_info.thread = last_ptid;
4261 resume_info.kind = resume_continue;
2ea28649 4262 resume_info.sig = gdb_signal_to_host (last_status.value.sig);
8336d594
PA
4263 (*the_target->resume) (&resume_info, 1);
4264 }
4265 else if (debug_threads)
87ce2a04
DE
4266 debug_printf ("GDB not connected; ignoring event %d for [%s]\n",
4267 (int) last_status.kind,
4268 target_pid_to_str (last_ptid));
8336d594
PA
4269 }
4270 else
4271 {
14a00470
YQ
4272 struct vstop_notif *vstop_notif
4273 = xmalloc (sizeof (struct vstop_notif));
4274
4275 vstop_notif->status = last_status;
4276 vstop_notif->ptid = last_ptid;
4277 /* Push Stop notification. */
4278 notif_push (&notif_stop,
4279 (struct notif_event *) vstop_notif);
8336d594 4280 }
bd99dc85
PA
4281 }
4282
0bfdf32f 4283 /* Be sure to not change the selected thread behind GDB's back.
bd99dc85 4284 Important in the non-stop mode asynchronous protocol. */
0bfdf32f 4285 set_desired_thread (1);
8336d594
PA
4286
4287 return 0;
bd99dc85 4288}
This page took 1.422578 seconds and 4 git commands to generate.