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