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