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