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