]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbserver/server.c
* win32-nat.c (psapi_module_handle): Remove static.
[thirdparty/binutils-gdb.git] / gdb / gdbserver / server.c
CommitLineData
c906108c 1/* Main code for remote server for GDB.
6aba47ca
DJ
2 Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003,
3 2004, 2005, 2006, 2007 Free Software 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"
21
68070c10 22#if HAVE_UNISTD_H
a9fa9f7d 23#include <unistd.h>
68070c10
PA
24#endif
25#if HAVE_SIGNAL_H
a9fa9f7d 26#include <signal.h>
68070c10 27#endif
b80864fb 28#if HAVE_SYS_WAIT_H
a9fa9f7d 29#include <sys/wait.h>
b80864fb 30#endif
a9fa9f7d 31
a1928bad
DJ
32unsigned long cont_thread;
33unsigned long general_thread;
34unsigned long step_thread;
35unsigned long thread_from_wait;
36unsigned long old_thread_from_wait;
c906108c 37int extended_protocol;
0d62e5e8
DJ
38int server_waiting;
39
c74d0ad8
DJ
40/* Enable miscellaneous debugging output. The name is historical - it
41 was originally used to debug LinuxThreads support. */
42int debug_threads;
43
89be2091
DJ
44int pass_signals[TARGET_SIGNAL_LAST];
45
c906108c 46jmp_buf toplevel;
c906108c 47
a9fa9f7d
DJ
48/* The PID of the originally created or attached inferior. Used to
49 send signals to the process when GDB sends us an asynchronous interrupt
50 (user hitting Control-C in the client), and to wait for the child to exit
51 when no longer debugging it. */
52
a1928bad 53unsigned long signal_pid;
a9fa9f7d 54
290fadea
RS
55#ifdef SIGTTOU
56/* A file descriptor for the controlling terminal. */
57int terminal_fd;
58
59/* TERMINAL_FD's original foreground group. */
60pid_t old_foreground_pgrp;
61
62/* Hand back terminal ownership to the original foreground group. */
63
64static void
65restore_old_foreground_pgrp (void)
66{
67 tcsetpgrp (terminal_fd, old_foreground_pgrp);
68}
69#endif
70
fc620387 71static int
da85418c 72start_inferior (char *argv[], char *statusptr)
c906108c 73{
b80864fb 74#ifdef SIGTTOU
a9fa9f7d
DJ
75 signal (SIGTTOU, SIG_DFL);
76 signal (SIGTTIN, SIG_DFL);
b80864fb 77#endif
a9fa9f7d
DJ
78
79 signal_pid = create_inferior (argv[0], argv);
0d62e5e8 80
c588c53c
MS
81 /* FIXME: we don't actually know at this point that the create
82 actually succeeded. We won't know that until we wait. */
a1928bad 83 fprintf (stderr, "Process %s created; pid = %ld\n", argv[0],
a9fa9f7d 84 signal_pid);
b80864fb 85 fflush (stderr);
a9fa9f7d 86
b80864fb 87#ifdef SIGTTOU
a9fa9f7d
DJ
88 signal (SIGTTOU, SIG_IGN);
89 signal (SIGTTIN, SIG_IGN);
290fadea
RS
90 terminal_fd = fileno (stderr);
91 old_foreground_pgrp = tcgetpgrp (terminal_fd);
92 tcsetpgrp (terminal_fd, signal_pid);
93 atexit (restore_old_foreground_pgrp);
b80864fb 94#endif
c906108c 95
c588c53c
MS
96 /* Wait till we are at 1st instruction in program, return signal
97 number (assuming success). */
0d62e5e8 98 return mywait (statusptr, 0);
c906108c
SS
99}
100
45b7b345 101static int
fc620387 102attach_inferior (int pid, char *statusptr, int *sigptr)
45b7b345
DJ
103{
104 /* myattach should return -1 if attaching is unsupported,
105 0 if it succeeded, and call error() otherwise. */
a9fa9f7d 106
45b7b345
DJ
107 if (myattach (pid) != 0)
108 return -1;
109
6910d122 110 fprintf (stderr, "Attached; pid = %d\n", pid);
b80864fb 111 fflush (stderr);
6910d122 112
a9fa9f7d
DJ
113 /* FIXME - It may be that we should get the SIGNAL_PID from the
114 attach function, so that it can be the main thread instead of
115 whichever we were told to attach to. */
116 signal_pid = pid;
117
0d62e5e8 118 *sigptr = mywait (statusptr, 0);
45b7b345 119
9db87ebd
DJ
120 /* GDB knows to ignore the first SIGSTOP after attaching to a running
121 process using the "attach" command, but this is different; it's
122 just using "target remote". Pretend it's just starting up. */
b80864fb
DJ
123 if (*statusptr == 'T' && *sigptr == TARGET_SIGNAL_STOP)
124 *sigptr = TARGET_SIGNAL_TRAP;
9db87ebd 125
45b7b345
DJ
126 return 0;
127}
128
c906108c 129extern int remote_debug;
ce3a066d 130
0876f84a
DJ
131/* Decode a qXfer read request. Return 0 if everything looks OK,
132 or -1 otherwise. */
133
134static int
135decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len)
136{
137 /* Extract and NUL-terminate the annex. */
138 *annex = buf;
139 while (*buf && *buf != ':')
140 buf++;
141 if (*buf == '\0')
142 return -1;
143 *buf++ = 0;
144
0e7f50da 145 /* After the read marker and annex, qXfer looks like a
0876f84a
DJ
146 traditional 'm' packet. */
147 decode_m_packet (buf, ofs, len);
148
149 return 0;
150}
151
152/* Write the response to a successful qXfer read. Returns the
153 length of the (binary) data stored in BUF, corresponding
154 to as much of DATA/LEN as we could fit. IS_MORE controls
155 the first character of the response. */
156static int
23181151 157write_qxfer_response (char *buf, const void *data, int len, int is_more)
0876f84a
DJ
158{
159 int out_len;
160
161 if (is_more)
162 buf[0] = 'm';
163 else
164 buf[0] = 'l';
165
166 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
167 PBUFSIZ - 2) + 1;
168}
169
89be2091
DJ
170/* Handle all of the extended 'Q' packets. */
171void
172handle_general_set (char *own_buf)
173{
174 if (strncmp ("QPassSignals:", own_buf, strlen ("QPassSignals:")) == 0)
175 {
176 int numsigs = (int) TARGET_SIGNAL_LAST, i;
177 const char *p = own_buf + strlen ("QPassSignals:");
178 CORE_ADDR cursig;
179
180 p = decode_address_to_semicolon (&cursig, p);
181 for (i = 0; i < numsigs; i++)
182 {
183 if (i == cursig)
184 {
185 pass_signals[i] = 1;
186 if (*p == '\0')
187 /* Keep looping, to clear the remaining signals. */
188 cursig = -1;
189 else
190 p = decode_address_to_semicolon (&cursig, p);
191 }
192 else
193 pass_signals[i] = 0;
194 }
195 strcpy (own_buf, "OK");
196 return;
197 }
198
199 /* Otherwise we didn't know what packet it was. Say we didn't
200 understand it. */
201 own_buf[0] = 0;
202}
203
23181151 204static const char *
fb1e4ffc 205get_features_xml (const char *annex)
23181151
DJ
206{
207 static int features_supported = -1;
208 static char *document;
209
fb1e4ffc
DJ
210#ifdef USE_XML
211 extern const char *const xml_builtin[][2];
212 int i;
213
214 /* Look for the annex. */
215 for (i = 0; xml_builtin[i][0] != NULL; i++)
216 if (strcmp (annex, xml_builtin[i][0]) == 0)
217 break;
218
219 if (xml_builtin[i][0] != NULL)
220 return xml_builtin[i][1];
221#endif
222
223 if (strcmp (annex, "target.xml") != 0)
224 return NULL;
225
23181151
DJ
226 if (features_supported == -1)
227 {
820f2bda
PA
228 const char *arch = NULL;
229 if (the_target->arch_string != NULL)
230 arch = (*the_target->arch_string) ();
23181151
DJ
231
232 if (arch == NULL)
233 features_supported = 0;
234 else
235 {
236 features_supported = 1;
237 document = malloc (64 + strlen (arch));
238 snprintf (document, 64 + strlen (arch),
239 "<target><architecture>%s</architecture></target>",
240 arch);
241 }
242 }
243
244 return document;
245}
246
c74d0ad8
DJ
247void
248monitor_show_help (void)
249{
250 monitor_output ("The following monitor commands are supported:\n");
251 monitor_output (" set debug <0|1>\n");
252 monitor_output (" Enable general debugging messages\n");
253 monitor_output (" set remote-debug <0|1>\n");
254 monitor_output (" Enable remote protocol debugging messages\n");
255}
256
ce3a066d
DJ
257/* Handle all of the extended 'q' packets. */
258void
0e7f50da 259handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
ce3a066d 260{
0d62e5e8
DJ
261 static struct inferior_list_entry *thread_ptr;
262
bb63802a
UW
263 /* Reply the current thread id. */
264 if (strcmp ("qC", own_buf) == 0)
265 {
266 thread_ptr = all_threads.head;
267 sprintf (own_buf, "QC%x",
268 thread_to_gdb_id ((struct thread_info *)thread_ptr));
269 return;
270 }
271
ce3a066d
DJ
272 if (strcmp ("qSymbol::", own_buf) == 0)
273 {
2f2893d9
DJ
274 if (the_target->look_up_symbols != NULL)
275 (*the_target->look_up_symbols) ();
276
ce3a066d
DJ
277 strcpy (own_buf, "OK");
278 return;
279 }
280
0d62e5e8
DJ
281 if (strcmp ("qfThreadInfo", own_buf) == 0)
282 {
283 thread_ptr = all_threads.head;
a06660f7 284 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
0d62e5e8
DJ
285 thread_ptr = thread_ptr->next;
286 return;
287 }
aa691b87 288
0d62e5e8
DJ
289 if (strcmp ("qsThreadInfo", own_buf) == 0)
290 {
291 if (thread_ptr != NULL)
292 {
a06660f7 293 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
0d62e5e8
DJ
294 thread_ptr = thread_ptr->next;
295 return;
296 }
297 else
298 {
299 sprintf (own_buf, "l");
300 return;
301 }
302 }
aa691b87 303
52fb6437
NS
304 if (the_target->read_offsets != NULL
305 && strcmp ("qOffsets", own_buf) == 0)
306 {
307 CORE_ADDR text, data;
308
309 if (the_target->read_offsets (&text, &data))
310 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
311 (long)text, (long)data, (long)data);
312 else
313 write_enn (own_buf);
314
315 return;
316 }
317
0e7f50da
UW
318 if (the_target->qxfer_spu != NULL
319 && strncmp ("qXfer:spu:read:", own_buf, 15) == 0)
320 {
321 char *annex;
322 int n;
323 unsigned int len;
324 CORE_ADDR ofs;
325 unsigned char *spu_buf;
326
327 strcpy (own_buf, "E00");
328 if (decode_xfer_read (own_buf + 15, &annex, &ofs, &len) < 0)
329 return;
330 if (len > PBUFSIZ - 2)
331 len = PBUFSIZ - 2;
332 spu_buf = malloc (len + 1);
333 if (!spu_buf)
334 return;
335
336 n = (*the_target->qxfer_spu) (annex, spu_buf, NULL, ofs, len + 1);
337 if (n < 0)
338 write_enn (own_buf);
339 else if (n > len)
340 *new_packet_len_p = write_qxfer_response
341 (own_buf, spu_buf, len, 1);
342 else
343 *new_packet_len_p = write_qxfer_response
344 (own_buf, spu_buf, n, 0);
345
346 free (spu_buf);
347 return;
348 }
349
350 if (the_target->qxfer_spu != NULL
351 && strncmp ("qXfer:spu:write:", own_buf, 16) == 0)
352 {
353 char *annex;
354 int n;
355 unsigned int len;
356 CORE_ADDR ofs;
357 unsigned char *spu_buf;
358
359 strcpy (own_buf, "E00");
360 spu_buf = malloc (packet_len - 15);
361 if (!spu_buf)
362 return;
363 if (decode_xfer_write (own_buf + 16, packet_len - 16, &annex,
364 &ofs, &len, spu_buf) < 0)
365 {
366 free (spu_buf);
367 return;
368 }
369
370 n = (*the_target->qxfer_spu)
371 (annex, NULL, (unsigned const char *)spu_buf, ofs, len);
372 if (n < 0)
373 write_enn (own_buf);
374 else
375 sprintf (own_buf, "%x", n);
376
377 free (spu_buf);
378 return;
379 }
380
aa691b87 381 if (the_target->read_auxv != NULL
0876f84a 382 && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0)
aa691b87 383 {
0876f84a
DJ
384 unsigned char *data;
385 int n;
aa691b87
RM
386 CORE_ADDR ofs;
387 unsigned int len;
0876f84a
DJ
388 char *annex;
389
390 /* Reject any annex; grab the offset and length. */
391 if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0
392 || annex[0] != '\0')
393 {
394 strcpy (own_buf, "E00");
395 return;
396 }
397
398 /* Read one extra byte, as an indicator of whether there is
399 more. */
400 if (len > PBUFSIZ - 2)
401 len = PBUFSIZ - 2;
402 data = malloc (len + 1);
403 n = (*the_target->read_auxv) (ofs, data, len + 1);
000ef4f0
DJ
404 if (n < 0)
405 write_enn (own_buf);
406 else if (n > len)
0876f84a 407 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
aa691b87 408 else
0876f84a
DJ
409 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
410
411 free (data);
412
aa691b87
RM
413 return;
414 }
415
23181151
DJ
416 if (strncmp ("qXfer:features:read:", own_buf, 20) == 0)
417 {
418 CORE_ADDR ofs;
419 unsigned int len, total_len;
420 const char *document;
421 char *annex;
422
fb1e4ffc
DJ
423 /* Check for support. */
424 document = get_features_xml ("target.xml");
23181151
DJ
425 if (document == NULL)
426 {
427 own_buf[0] = '\0';
428 return;
429 }
430
fb1e4ffc
DJ
431 /* Grab the annex, offset, and length. */
432 if (decode_xfer_read (own_buf + 20, &annex, &ofs, &len) < 0)
433 {
434 strcpy (own_buf, "E00");
435 return;
436 }
437
438 /* Now grab the correct annex. */
439 document = get_features_xml (annex);
440 if (document == NULL)
23181151
DJ
441 {
442 strcpy (own_buf, "E00");
443 return;
444 }
445
446 total_len = strlen (document);
447 if (len > PBUFSIZ - 2)
448 len = PBUFSIZ - 2;
449
450 if (ofs > total_len)
451 write_enn (own_buf);
452 else if (len < total_len - ofs)
453 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
454 len, 1);
455 else
456 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
457 total_len - ofs, 0);
458
459 return;
460 }
461
255e7678
DJ
462 if (strncmp ("qXfer:libraries:read:", own_buf, 21) == 0)
463 {
464 CORE_ADDR ofs;
465 unsigned int len, total_len;
466 char *document, *p;
467 struct inferior_list_entry *dll_ptr;
468 char *annex;
469
470 /* Reject any annex; grab the offset and length. */
471 if (decode_xfer_read (own_buf + 21, &annex, &ofs, &len) < 0
472 || annex[0] != '\0')
473 {
474 strcpy (own_buf, "E00");
475 return;
476 }
477
478 /* Over-estimate the necessary memory. Assume that every character
479 in the library name must be escaped. */
480 total_len = 64;
481 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
482 total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name);
483
484 document = malloc (total_len);
485 strcpy (document, "<library-list>\n");
486 p = document + strlen (document);
487
488 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
489 {
490 struct dll_info *dll = (struct dll_info *) dll_ptr;
491 char *name;
492
493 strcpy (p, " <library name=\"");
494 p = p + strlen (p);
495 name = xml_escape_text (dll->name);
496 strcpy (p, name);
497 free (name);
498 p = p + strlen (p);
499 strcpy (p, "\"><segment address=\"");
500 p = p + strlen (p);
501 sprintf (p, "0x%lx", (long) dll->base_addr);
502 p = p + strlen (p);
503 strcpy (p, "\"/></library>\n");
504 p = p + strlen (p);
505 }
506
507 strcpy (p, "</library-list>\n");
508
509 total_len = strlen (document);
510 if (len > PBUFSIZ - 2)
511 len = PBUFSIZ - 2;
512
513 if (ofs > total_len)
514 write_enn (own_buf);
515 else if (len < total_len - ofs)
516 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
517 len, 1);
518 else
519 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
520 total_len - ofs, 0);
521
522 free (document);
523 return;
524 }
525
be2a5f71
DJ
526 /* Protocol features query. */
527 if (strncmp ("qSupported", own_buf, 10) == 0
528 && (own_buf[10] == ':' || own_buf[10] == '\0'))
529 {
89be2091 530 sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1);
0876f84a 531
255e7678
DJ
532 /* We do not have any hook to indicate whether the target backend
533 supports qXfer:libraries:read, so always report it. */
534 strcat (own_buf, ";qXfer:libraries:read+");
535
0876f84a 536 if (the_target->read_auxv != NULL)
9f2e1e63 537 strcat (own_buf, ";qXfer:auxv:read+");
0e7f50da
UW
538
539 if (the_target->qxfer_spu != NULL)
540 strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+");
0876f84a 541
fb1e4ffc 542 if (get_features_xml ("target.xml") != NULL)
23181151
DJ
543 strcat (own_buf, ";qXfer:features:read+");
544
be2a5f71
DJ
545 return;
546 }
547
dae5f5cf
DJ
548 /* Thread-local storage support. */
549 if (the_target->get_tls_address != NULL
550 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
551 {
552 char *p = own_buf + 12;
553 CORE_ADDR parts[3], address = 0;
554 int i, err;
555
556 for (i = 0; i < 3; i++)
557 {
558 char *p2;
559 int len;
560
561 if (p == NULL)
562 break;
563
564 p2 = strchr (p, ',');
565 if (p2)
566 {
567 len = p2 - p;
568 p2++;
569 }
570 else
571 {
572 len = strlen (p);
573 p2 = NULL;
574 }
575
576 decode_address (&parts[i], p, len);
577 p = p2;
578 }
579
580 if (p != NULL || i < 3)
581 err = 1;
582 else
583 {
584 struct thread_info *thread = gdb_id_to_thread (parts[0]);
585
586 if (thread == NULL)
587 err = 2;
588 else
589 err = the_target->get_tls_address (thread, parts[1], parts[2],
590 &address);
591 }
592
593 if (err == 0)
594 {
595 sprintf (own_buf, "%llx", address);
596 return;
597 }
598 else if (err > 0)
599 {
600 write_enn (own_buf);
601 return;
602 }
603
604 /* Otherwise, pretend we do not understand this packet. */
605 }
606
c74d0ad8
DJ
607 /* Handle "monitor" commands. */
608 if (strncmp ("qRcmd,", own_buf, 6) == 0)
609 {
610 char *mon = malloc (PBUFSIZ);
611 int len = strlen (own_buf + 6);
612
613 if ((len % 1) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
614 {
615 write_enn (own_buf);
616 free (mon);
617 return;
618 }
619 mon[len / 2] = '\0';
620
621 write_ok (own_buf);
622
623 if (strcmp (mon, "set debug 1") == 0)
624 {
625 debug_threads = 1;
626 monitor_output ("Debug output enabled.\n");
627 }
628 else if (strcmp (mon, "set debug 0") == 0)
629 {
630 debug_threads = 0;
631 monitor_output ("Debug output disabled.\n");
632 }
633 else if (strcmp (mon, "set remote-debug 1") == 0)
634 {
635 remote_debug = 1;
636 monitor_output ("Protocol debug output enabled.\n");
637 }
638 else if (strcmp (mon, "set remote-debug 0") == 0)
639 {
640 remote_debug = 0;
641 monitor_output ("Protocol debug output disabled.\n");
642 }
643 else if (strcmp (mon, "help") == 0)
644 monitor_show_help ();
645 else
646 {
647 monitor_output ("Unknown monitor command.\n\n");
648 monitor_show_help ();
649 write_enn (own_buf);
650 }
651
652 free (mon);
653 return;
654 }
655
ce3a066d
DJ
656 /* Otherwise we didn't know what packet it was. Say we didn't
657 understand it. */
658 own_buf[0] = 0;
659}
660
64386c31
DJ
661/* Parse vCont packets. */
662void
fc620387 663handle_v_cont (char *own_buf, char *status, int *signal)
64386c31
DJ
664{
665 char *p, *q;
666 int n = 0, i = 0;
667 struct thread_resume *resume_info, default_action;
668
669 /* Count the number of semicolons in the packet. There should be one
670 for every action. */
671 p = &own_buf[5];
672 while (p)
673 {
674 n++;
675 p++;
676 p = strchr (p, ';');
677 }
678 /* Allocate room for one extra action, for the default remain-stopped
679 behavior; if no default action is in the list, we'll need the extra
680 slot. */
681 resume_info = malloc ((n + 1) * sizeof (resume_info[0]));
682
683 default_action.thread = -1;
684 default_action.leave_stopped = 1;
685 default_action.step = 0;
686 default_action.sig = 0;
687
688 p = &own_buf[5];
689 i = 0;
690 while (*p)
691 {
692 p++;
693
694 resume_info[i].leave_stopped = 0;
695
696 if (p[0] == 's' || p[0] == 'S')
697 resume_info[i].step = 1;
698 else if (p[0] == 'c' || p[0] == 'C')
699 resume_info[i].step = 0;
700 else
701 goto err;
702
703 if (p[0] == 'S' || p[0] == 'C')
704 {
705 int sig;
706 sig = strtol (p + 1, &q, 16);
707 if (p == q)
708 goto err;
709 p = q;
710
711 if (!target_signal_to_host_p (sig))
712 goto err;
713 resume_info[i].sig = target_signal_to_host (sig);
714 }
715 else
716 {
717 resume_info[i].sig = 0;
718 p = p + 1;
719 }
720
721 if (p[0] == 0)
722 {
723 resume_info[i].thread = -1;
724 default_action = resume_info[i];
725
726 /* Note: we don't increment i here, we'll overwrite this entry
727 the next time through. */
728 }
729 else if (p[0] == ':')
730 {
a06660f7
DJ
731 unsigned int gdb_id = strtoul (p + 1, &q, 16);
732 unsigned long thread_id;
733
64386c31
DJ
734 if (p == q)
735 goto err;
736 p = q;
737 if (p[0] != ';' && p[0] != 0)
738 goto err;
739
a06660f7
DJ
740 thread_id = gdb_id_to_thread_id (gdb_id);
741 if (thread_id)
742 resume_info[i].thread = thread_id;
743 else
744 goto err;
745
64386c31
DJ
746 i++;
747 }
748 }
749
750 resume_info[i] = default_action;
751
752 /* Still used in occasional places in the backend. */
753 if (n == 1 && resume_info[0].thread != -1)
754 cont_thread = resume_info[0].thread;
755 else
756 cont_thread = -1;
dc3f8883 757 set_desired_inferior (0);
64386c31 758
a20d5e98 759 enable_async_io ();
64386c31
DJ
760 (*the_target->resume) (resume_info);
761
762 free (resume_info);
763
764 *signal = mywait (status, 1);
765 prepare_resume_reply (own_buf, *status, *signal);
a20d5e98 766 disable_async_io ();
64386c31
DJ
767 return;
768
769err:
255e7678 770 write_enn (own_buf);
64386c31
DJ
771 free (resume_info);
772 return;
773}
774
775/* Handle all of the extended 'v' packets. */
776void
a6b151f1
DJ
777handle_v_requests (char *own_buf, char *status, int *signal,
778 int packet_len, int *new_packet_len)
64386c31
DJ
779{
780 if (strncmp (own_buf, "vCont;", 6) == 0)
781 {
782 handle_v_cont (own_buf, status, signal);
783 return;
784 }
785
786 if (strncmp (own_buf, "vCont?", 6) == 0)
787 {
788 strcpy (own_buf, "vCont;c;C;s;S");
789 return;
790 }
791
a6b151f1
DJ
792 if (strncmp (own_buf, "vFile:", 6) == 0
793 && handle_vFile (own_buf, packet_len, new_packet_len))
794 return;
795
64386c31
DJ
796 /* Otherwise we didn't know what packet it was. Say we didn't
797 understand it. */
798 own_buf[0] = 0;
799 return;
800}
801
802void
27524b67 803myresume (char *own_buf, int step, int *signalp, char *statusp)
64386c31
DJ
804{
805 struct thread_resume resume_info[2];
806 int n = 0;
a20d5e98
DJ
807 int sig = *signalp;
808
809 set_desired_inferior (0);
64386c31 810
d592fa2f 811 if (step || sig || (cont_thread != 0 && cont_thread != -1))
64386c31
DJ
812 {
813 resume_info[0].thread
814 = ((struct inferior_list_entry *) current_inferior)->id;
815 resume_info[0].step = step;
816 resume_info[0].sig = sig;
817 resume_info[0].leave_stopped = 0;
818 n++;
819 }
820 resume_info[n].thread = -1;
821 resume_info[n].step = 0;
822 resume_info[n].sig = 0;
d592fa2f 823 resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1);
64386c31 824
a20d5e98 825 enable_async_io ();
64386c31 826 (*the_target->resume) (resume_info);
a20d5e98
DJ
827 *signalp = mywait (statusp, 1);
828 prepare_resume_reply (own_buf, *statusp, *signalp);
829 disable_async_io ();
64386c31
DJ
830}
831
0729219d 832static int attached;
c906108c 833
dd24457d
DJ
834static void
835gdbserver_version (void)
836{
837 printf ("GNU gdbserver %s\n"
255e7678 838 "Copyright (C) 2007 Free Software Foundation, Inc.\n"
dd24457d
DJ
839 "gdbserver is free software, covered by the GNU General Public License.\n"
840 "This gdbserver was configured as \"%s\"\n",
841 version, host_name);
842}
843
0bc68c49
DJ
844static void
845gdbserver_usage (void)
846{
dd24457d
DJ
847 printf ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
848 "\tgdbserver COMM --attach PID\n"
849 "\n"
850 "COMM may either be a tty device (for serial debugging), or \n"
851 "HOST:PORT to listen for a TCP connection.\n");
0bc68c49
DJ
852}
853
c906108c 854int
da85418c 855main (int argc, char *argv[])
c906108c 856{
f450004a 857 char ch, status, *own_buf;
7fb85e41 858 unsigned char *mem_buf;
c906108c 859 int i = 0;
fc620387 860 int signal;
c906108c
SS
861 unsigned int len;
862 CORE_ADDR mem_addr;
0729219d
DJ
863 int bad_attach;
864 int pid;
45b7b345 865 char *arg_end;
c906108c 866
dd24457d
DJ
867 if (argc >= 2 && strcmp (argv[1], "--version") == 0)
868 {
869 gdbserver_version ();
870 exit (0);
871 }
872
873 if (argc >= 2 && strcmp (argv[1], "--help") == 0)
874 {
875 gdbserver_usage ();
876 exit (0);
877 }
878
c5aa993b 879 if (setjmp (toplevel))
c906108c 880 {
c5aa993b
JM
881 fprintf (stderr, "Exiting\n");
882 exit (1);
c906108c
SS
883 }
884
0729219d
DJ
885 bad_attach = 0;
886 pid = 0;
887 attached = 0;
45b7b345
DJ
888 if (argc >= 3 && strcmp (argv[2], "--attach") == 0)
889 {
890 if (argc == 4
506c7aa0 891 && argv[3][0] != '\0'
6c2d16d2 892 && (pid = strtoul (argv[3], &arg_end, 0)) != 0
45b7b345
DJ
893 && *arg_end == '\0')
894 {
895 ;
896 }
897 else
898 bad_attach = 1;
899 }
900
901 if (argc < 3 || bad_attach)
dd24457d
DJ
902 {
903 gdbserver_usage ();
904 exit (1);
905 }
c906108c 906
a20d5e98 907 initialize_async_io ();
4ce44c66
JM
908 initialize_low ();
909
255e7678 910 own_buf = malloc (PBUFSIZ + 1);
7fb85e41 911 mem_buf = malloc (PBUFSIZ);
0a30fbc4 912
45b7b345
DJ
913 if (pid == 0)
914 {
915 /* Wait till we are at first instruction in program. */
916 signal = start_inferior (&argv[2], &status);
c906108c 917
c588c53c
MS
918 /* We are now (hopefully) stopped at the first instruction of
919 the target process. This assumes that the target process was
920 successfully created. */
45b7b345
DJ
921 }
922 else
923 {
924 switch (attach_inferior (pid, &status, &signal))
925 {
926 case -1:
927 error ("Attaching not supported on this target");
928 break;
929 default:
930 attached = 1;
931 break;
932 }
933 }
c906108c 934
311de423
PA
935 /* Don't report shared library events on the initial connection,
936 even if some libraries are preloaded. Avoids the "stopped by
937 shared library event" notice on gdb side. */
938 dlls_changed = 0;
939
8264bb58
DJ
940 if (setjmp (toplevel))
941 {
942 fprintf (stderr, "Killing inferior\n");
943 kill_inferior ();
944 exit (1);
945 }
946
c588c53c
MS
947 if (status == 'W' || status == 'X')
948 {
949 fprintf (stderr, "No inferior, GDBserver exiting.\n");
950 exit (1);
951 }
952
c906108c
SS
953 while (1)
954 {
955 remote_open (argv[1]);
956
c5aa993b
JM
957 restart:
958 setjmp (toplevel);
a20d5e98 959 disable_async_io ();
01f9e8fa 960 while (1)
c906108c
SS
961 {
962 unsigned char sig;
01f9e8fa
DJ
963 int packet_len;
964 int new_packet_len = -1;
965
966 packet_len = getpkt (own_buf);
967 if (packet_len <= 0)
968 break;
969
c906108c
SS
970 i = 0;
971 ch = own_buf[i++];
972 switch (ch)
973 {
ce3a066d 974 case 'q':
0e7f50da 975 handle_query (own_buf, packet_len, &new_packet_len);
ce3a066d 976 break;
89be2091
DJ
977 case 'Q':
978 handle_general_set (own_buf);
979 break;
6ad8ae5c
DJ
980 case 'D':
981 fprintf (stderr, "Detaching from inferior\n");
444d6139 982 if (detach_inferior () != 0)
6ad8ae5c 983 {
444d6139
PA
984 write_enn (own_buf);
985 putpkt (own_buf);
6ad8ae5c 986 }
444d6139
PA
987 else
988 {
989 write_ok (own_buf);
990 putpkt (own_buf);
991 remote_close ();
6ad8ae5c 992
444d6139
PA
993 /* If we are attached, then we can exit. Otherwise, we
994 need to hang around doing nothing, until the child
995 is gone. */
996 if (!attached)
997 join_inferior ();
6ad8ae5c 998
444d6139
PA
999 exit (0);
1000 }
c906108c 1001 case '!':
45b7b345
DJ
1002 if (attached == 0)
1003 {
1004 extended_protocol = 1;
1005 prepare_resume_reply (own_buf, status, signal);
1006 }
1007 else
1008 {
1009 /* We can not use the extended protocol if we are
1010 attached, because we can not restart the running
1011 program. So return unrecognized. */
1012 own_buf[0] = '\0';
1013 }
c906108c
SS
1014 break;
1015 case '?':
1016 prepare_resume_reply (own_buf, status, signal);
1017 break;
1018 case 'H':
a06660f7 1019 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
c906108c 1020 {
a06660f7
DJ
1021 unsigned long gdb_id, thread_id;
1022
1023 gdb_id = strtoul (&own_buf[2], NULL, 16);
1024 thread_id = gdb_id_to_thread_id (gdb_id);
1025 if (thread_id == 0)
1026 {
1027 write_enn (own_buf);
1028 break;
1029 }
1030
1031 if (own_buf[1] == 'g')
1032 {
1033 general_thread = thread_id;
1034 set_desired_inferior (1);
1035 }
1036 else if (own_buf[1] == 'c')
1037 cont_thread = thread_id;
1038 else if (own_buf[1] == 's')
1039 step_thread = thread_id;
1040
0d62e5e8 1041 write_ok (own_buf);
a06660f7
DJ
1042 }
1043 else
1044 {
c906108c
SS
1045 /* Silently ignore it so that gdb can extend the protocol
1046 without compatibility headaches. */
1047 own_buf[0] = '\0';
c906108c
SS
1048 }
1049 break;
1050 case 'g':
0d62e5e8 1051 set_desired_inferior (1);
0a30fbc4 1052 registers_to_string (own_buf);
c906108c
SS
1053 break;
1054 case 'G':
0d62e5e8 1055 set_desired_inferior (1);
0a30fbc4 1056 registers_from_string (&own_buf[1]);
c906108c
SS
1057 write_ok (own_buf);
1058 break;
1059 case 'm':
1060 decode_m_packet (&own_buf[1], &mem_addr, &len);
c3e735a6
DJ
1061 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
1062 convert_int_to_ascii (mem_buf, own_buf, len);
1063 else
1064 write_enn (own_buf);
c906108c
SS
1065 break;
1066 case 'M':
1067 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
1068 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
1069 write_ok (own_buf);
1070 else
1071 write_enn (own_buf);
1072 break;
01f9e8fa
DJ
1073 case 'X':
1074 if (decode_X_packet (&own_buf[1], packet_len - 1,
1075 &mem_addr, &len, mem_buf) < 0
1076 || write_inferior_memory (mem_addr, mem_buf, len) != 0)
1077 write_enn (own_buf);
1078 else
1079 write_ok (own_buf);
1080 break;
c906108c
SS
1081 case 'C':
1082 convert_ascii_to_int (own_buf + 1, &sig, 1);
0e98d0a7
DJ
1083 if (target_signal_to_host_p (sig))
1084 signal = target_signal_to_host (sig);
1085 else
1086 signal = 0;
27524b67 1087 myresume (own_buf, 0, &signal, &status);
c906108c
SS
1088 break;
1089 case 'S':
1090 convert_ascii_to_int (own_buf + 1, &sig, 1);
0e98d0a7
DJ
1091 if (target_signal_to_host_p (sig))
1092 signal = target_signal_to_host (sig);
1093 else
1094 signal = 0;
27524b67 1095 myresume (own_buf, 1, &signal, &status);
c906108c
SS
1096 break;
1097 case 'c':
a20d5e98 1098 signal = 0;
27524b67 1099 myresume (own_buf, 0, &signal, &status);
c906108c
SS
1100 break;
1101 case 's':
a20d5e98 1102 signal = 0;
27524b67 1103 myresume (own_buf, 1, &signal, &status);
c906108c 1104 break;
e013ee27
OF
1105 case 'Z':
1106 {
1107 char *lenptr;
1108 char *dataptr;
1109 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1110 int len = strtol (lenptr + 1, &dataptr, 16);
1111 char type = own_buf[1];
1112
1113 if (the_target->insert_watchpoint == NULL
1114 || (type < '2' || type > '4'))
1115 {
1116 /* No watchpoint support or not a watchpoint command;
1117 unrecognized either way. */
1118 own_buf[0] = '\0';
1119 }
1120 else
1121 {
1122 int res;
1123
1124 res = (*the_target->insert_watchpoint) (type, addr, len);
1125 if (res == 0)
1126 write_ok (own_buf);
1127 else if (res == 1)
1128 /* Unsupported. */
1129 own_buf[0] = '\0';
1130 else
1131 write_enn (own_buf);
1132 }
1133 break;
1134 }
1135 case 'z':
1136 {
1137 char *lenptr;
1138 char *dataptr;
1139 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1140 int len = strtol (lenptr + 1, &dataptr, 16);
1141 char type = own_buf[1];
1142
1143 if (the_target->remove_watchpoint == NULL
1144 || (type < '2' || type > '4'))
1145 {
1146 /* No watchpoint support or not a watchpoint command;
1147 unrecognized either way. */
1148 own_buf[0] = '\0';
1149 }
1150 else
1151 {
1152 int res;
1153
1154 res = (*the_target->remove_watchpoint) (type, addr, len);
1155 if (res == 0)
1156 write_ok (own_buf);
1157 else if (res == 1)
1158 /* Unsupported. */
1159 own_buf[0] = '\0';
1160 else
1161 write_enn (own_buf);
1162 }
1163 break;
1164 }
c906108c
SS
1165 case 'k':
1166 fprintf (stderr, "Killing inferior\n");
1167 kill_inferior ();
1168 /* When using the extended protocol, we start up a new
c5aa993b 1169 debugging session. The traditional protocol will
c906108c
SS
1170 exit instead. */
1171 if (extended_protocol)
1172 {
1173 write_ok (own_buf);
1174 fprintf (stderr, "GDBserver restarting\n");
1175
1176 /* Wait till we are at 1st instruction in prog. */
1177 signal = start_inferior (&argv[2], &status);
1178 goto restart;
1179 break;
1180 }
1181 else
1182 {
1183 exit (0);
1184 break;
1185 }
1186 case 'T':
a06660f7
DJ
1187 {
1188 unsigned long gdb_id, thread_id;
1189
1190 gdb_id = strtoul (&own_buf[1], NULL, 16);
1191 thread_id = gdb_id_to_thread_id (gdb_id);
1192 if (thread_id == 0)
1193 {
1194 write_enn (own_buf);
1195 break;
1196 }
1197
1198 if (mythread_alive (thread_id))
1199 write_ok (own_buf);
1200 else
1201 write_enn (own_buf);
1202 }
c906108c
SS
1203 break;
1204 case 'R':
1205 /* Restarting the inferior is only supported in the
c5aa993b 1206 extended protocol. */
c906108c
SS
1207 if (extended_protocol)
1208 {
1209 kill_inferior ();
1210 write_ok (own_buf);
1211 fprintf (stderr, "GDBserver restarting\n");
1212
1213 /* Wait till we are at 1st instruction in prog. */
1214 signal = start_inferior (&argv[2], &status);
1215 goto restart;
1216 break;
1217 }
1218 else
1219 {
1220 /* It is a request we don't understand. Respond with an
1221 empty packet so that gdb knows that we don't support this
1222 request. */
1223 own_buf[0] = '\0';
1224 break;
1225 }
64386c31
DJ
1226 case 'v':
1227 /* Extended (long) request. */
a6b151f1
DJ
1228 handle_v_requests (own_buf, &status, &signal,
1229 packet_len, &new_packet_len);
64386c31 1230 break;
a6b151f1 1231
c906108c
SS
1232 default:
1233 /* It is a request we don't understand. Respond with an
c5aa993b
JM
1234 empty packet so that gdb knows that we don't support this
1235 request. */
c906108c
SS
1236 own_buf[0] = '\0';
1237 break;
1238 }
1239
01f9e8fa
DJ
1240 if (new_packet_len != -1)
1241 putpkt_binary (own_buf, new_packet_len);
1242 else
1243 putpkt (own_buf);
c906108c
SS
1244
1245 if (status == 'W')
1246 fprintf (stderr,
3a7fb99b 1247 "\nChild exited with status %d\n", signal);
c906108c 1248 if (status == 'X')
b80864fb
DJ
1249 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
1250 target_signal_to_host (signal),
1251 target_signal_to_name (signal));
c906108c
SS
1252 if (status == 'W' || status == 'X')
1253 {
1254 if (extended_protocol)
1255 {
1256 fprintf (stderr, "Killing inferior\n");
1257 kill_inferior ();
1258 write_ok (own_buf);
1259 fprintf (stderr, "GDBserver restarting\n");
1260
1261 /* Wait till we are at 1st instruction in prog. */
1262 signal = start_inferior (&argv[2], &status);
1263 goto restart;
1264 break;
1265 }
1266 else
1267 {
1268 fprintf (stderr, "GDBserver exiting\n");
1269 exit (0);
1270 }
1271 }
1272 }
1273
1274 /* We come here when getpkt fails.
1275
c5aa993b
JM
1276 For the extended remote protocol we exit (and this is the only
1277 way we gracefully exit!).
c906108c 1278
c5aa993b
JM
1279 For the traditional remote protocol close the connection,
1280 and re-open it at the top of the loop. */
c906108c
SS
1281 if (extended_protocol)
1282 {
1283 remote_close ();
1284 exit (0);
1285 }
1286 else
1287 {
45b7b345
DJ
1288 fprintf (stderr, "Remote side has terminated connection. "
1289 "GDBserver will reopen the connection.\n");
c906108c
SS
1290 remote_close ();
1291 }
1292 }
1293}