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