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