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