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