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