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