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