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