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