]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbserver/server.c
Updated copyright notices for most files.
[thirdparty/binutils-gdb.git] / gdb / gdbserver / server.c
CommitLineData
c906108c 1/* Main code for remote server for GDB.
6aba47ca 2 Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003,
0fb0cc75 3 2004, 2005, 2006, 2007, 2008, 2009 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
ec56be1b
PA
31#if HAVE_MALLOC_H
32#include <malloc.h>
33#endif
a9fa9f7d 34
a1928bad
DJ
35unsigned long cont_thread;
36unsigned long general_thread;
37unsigned long step_thread;
38unsigned long thread_from_wait;
39unsigned long old_thread_from_wait;
0d62e5e8
DJ
40int server_waiting;
41
2d717e4f
DJ
42static int extended_protocol;
43static int attached;
44static int response_needed;
45static int exit_requested;
46
ccd213ac 47static char **program_argv, **wrapper_argv;
2d717e4f 48
c74d0ad8
DJ
49/* Enable miscellaneous debugging output. The name is historical - it
50 was originally used to debug LinuxThreads support. */
51int debug_threads;
52
89be2091
DJ
53int pass_signals[TARGET_SIGNAL_LAST];
54
c906108c 55jmp_buf toplevel;
c906108c 56
9b4b61c8
UW
57const char *gdbserver_xmltarget;
58
a9fa9f7d
DJ
59/* The PID of the originally created or attached inferior. Used to
60 send signals to the process when GDB sends us an asynchronous interrupt
61 (user hitting Control-C in the client), and to wait for the child to exit
62 when no longer debugging it. */
63
a1928bad 64unsigned long signal_pid;
a9fa9f7d 65
290fadea
RS
66#ifdef SIGTTOU
67/* A file descriptor for the controlling terminal. */
68int terminal_fd;
69
70/* TERMINAL_FD's original foreground group. */
71pid_t old_foreground_pgrp;
72
73/* Hand back terminal ownership to the original foreground group. */
74
75static void
76restore_old_foreground_pgrp (void)
77{
78 tcsetpgrp (terminal_fd, old_foreground_pgrp);
79}
80#endif
81
ec56be1b
PA
82/* Set if you want to disable optional thread related packets support
83 in gdbserver, for the sake of testing GDB against stubs that don't
84 support them. */
85int disable_packet_vCont;
86int disable_packet_Tthread;
87int disable_packet_qC;
88int disable_packet_qfThreadInfo;
89
2d717e4f
DJ
90static int
91target_running (void)
92{
93 return all_threads.head != NULL;
94}
95
fc620387 96static int
ccd213ac 97start_inferior (char **argv, char *statusptr)
c906108c 98{
ccd213ac 99 char **new_argv = argv;
2d717e4f
DJ
100 attached = 0;
101
ccd213ac
DJ
102 if (wrapper_argv != NULL)
103 {
104 int i, count = 1;
105
106 for (i = 0; wrapper_argv[i] != NULL; i++)
107 count++;
108 for (i = 0; argv[i] != NULL; i++)
109 count++;
110 new_argv = alloca (sizeof (char *) * count);
111 count = 0;
112 for (i = 0; wrapper_argv[i] != NULL; i++)
113 new_argv[count++] = wrapper_argv[i];
114 for (i = 0; argv[i] != NULL; i++)
115 new_argv[count++] = argv[i];
116 new_argv[count] = NULL;
117 }
118
b80864fb 119#ifdef SIGTTOU
a9fa9f7d
DJ
120 signal (SIGTTOU, SIG_DFL);
121 signal (SIGTTIN, SIG_DFL);
b80864fb 122#endif
a9fa9f7d 123
ccd213ac 124 signal_pid = create_inferior (new_argv[0], new_argv);
0d62e5e8 125
c588c53c
MS
126 /* FIXME: we don't actually know at this point that the create
127 actually succeeded. We won't know that until we wait. */
a1928bad 128 fprintf (stderr, "Process %s created; pid = %ld\n", argv[0],
a9fa9f7d 129 signal_pid);
b80864fb 130 fflush (stderr);
a9fa9f7d 131
b80864fb 132#ifdef SIGTTOU
a9fa9f7d
DJ
133 signal (SIGTTOU, SIG_IGN);
134 signal (SIGTTIN, SIG_IGN);
290fadea
RS
135 terminal_fd = fileno (stderr);
136 old_foreground_pgrp = tcgetpgrp (terminal_fd);
137 tcsetpgrp (terminal_fd, signal_pid);
138 atexit (restore_old_foreground_pgrp);
b80864fb 139#endif
c906108c 140
ccd213ac
DJ
141 if (wrapper_argv != NULL)
142 {
143 struct thread_resume resume_info;
144 int sig;
145
146 resume_info.thread = -1;
147 resume_info.step = 0;
148 resume_info.sig = 0;
149 resume_info.leave_stopped = 0;
150
151 sig = mywait (statusptr, 0);
152 if (*statusptr != 'T')
153 return sig;
154
155 do
156 {
157 (*the_target->resume) (&resume_info);
158
159 sig = mywait (statusptr, 0);
160 if (*statusptr != 'T')
161 return sig;
162 }
163 while (sig != TARGET_SIGNAL_TRAP);
164
165 return sig;
166 }
167
c588c53c
MS
168 /* Wait till we are at 1st instruction in program, return signal
169 number (assuming success). */
0d62e5e8 170 return mywait (statusptr, 0);
c906108c
SS
171}
172
45b7b345 173static int
fc620387 174attach_inferior (int pid, char *statusptr, int *sigptr)
45b7b345
DJ
175{
176 /* myattach should return -1 if attaching is unsupported,
177 0 if it succeeded, and call error() otherwise. */
a9fa9f7d 178
45b7b345
DJ
179 if (myattach (pid) != 0)
180 return -1;
181
2d717e4f
DJ
182 attached = 1;
183
6910d122 184 fprintf (stderr, "Attached; pid = %d\n", pid);
b80864fb 185 fflush (stderr);
6910d122 186
a9fa9f7d
DJ
187 /* FIXME - It may be that we should get the SIGNAL_PID from the
188 attach function, so that it can be the main thread instead of
189 whichever we were told to attach to. */
190 signal_pid = pid;
191
0d62e5e8 192 *sigptr = mywait (statusptr, 0);
45b7b345 193
9db87ebd
DJ
194 /* GDB knows to ignore the first SIGSTOP after attaching to a running
195 process using the "attach" command, but this is different; it's
196 just using "target remote". Pretend it's just starting up. */
b80864fb
DJ
197 if (*statusptr == 'T' && *sigptr == TARGET_SIGNAL_STOP)
198 *sigptr = TARGET_SIGNAL_TRAP;
9db87ebd 199
45b7b345
DJ
200 return 0;
201}
202
c906108c 203extern int remote_debug;
ce3a066d 204
0876f84a
DJ
205/* Decode a qXfer read request. Return 0 if everything looks OK,
206 or -1 otherwise. */
207
208static int
209decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len)
210{
211 /* Extract and NUL-terminate the annex. */
212 *annex = buf;
213 while (*buf && *buf != ':')
214 buf++;
215 if (*buf == '\0')
216 return -1;
217 *buf++ = 0;
218
0e7f50da 219 /* After the read marker and annex, qXfer looks like a
0876f84a
DJ
220 traditional 'm' packet. */
221 decode_m_packet (buf, ofs, len);
222
223 return 0;
224}
225
226/* Write the response to a successful qXfer read. Returns the
227 length of the (binary) data stored in BUF, corresponding
228 to as much of DATA/LEN as we could fit. IS_MORE controls
229 the first character of the response. */
230static int
23181151 231write_qxfer_response (char *buf, const void *data, int len, int is_more)
0876f84a
DJ
232{
233 int out_len;
234
235 if (is_more)
236 buf[0] = 'm';
237 else
238 buf[0] = 'l';
239
240 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
241 PBUFSIZ - 2) + 1;
242}
243
89be2091
DJ
244/* Handle all of the extended 'Q' packets. */
245void
246handle_general_set (char *own_buf)
247{
248 if (strncmp ("QPassSignals:", own_buf, strlen ("QPassSignals:")) == 0)
249 {
250 int numsigs = (int) TARGET_SIGNAL_LAST, i;
251 const char *p = own_buf + strlen ("QPassSignals:");
252 CORE_ADDR cursig;
253
254 p = decode_address_to_semicolon (&cursig, p);
255 for (i = 0; i < numsigs; i++)
256 {
257 if (i == cursig)
258 {
259 pass_signals[i] = 1;
260 if (*p == '\0')
261 /* Keep looping, to clear the remaining signals. */
262 cursig = -1;
263 else
264 p = decode_address_to_semicolon (&cursig, p);
265 }
266 else
267 pass_signals[i] = 0;
268 }
269 strcpy (own_buf, "OK");
270 return;
271 }
272
a6f3e723
SL
273 if (strcmp (own_buf, "QStartNoAckMode") == 0)
274 {
275 if (remote_debug)
276 {
277 fprintf (stderr, "[noack mode enabled]\n");
278 fflush (stderr);
279 }
280
281 noack_mode = 1;
282 write_ok (own_buf);
283 return;
284 }
285
89be2091
DJ
286 /* Otherwise we didn't know what packet it was. Say we didn't
287 understand it. */
288 own_buf[0] = 0;
289}
290
23181151 291static const char *
fb1e4ffc 292get_features_xml (const char *annex)
23181151 293{
9b4b61c8
UW
294 /* gdbserver_xmltarget defines what to return when looking
295 for the "target.xml" file. Its contents can either be
296 verbatim XML code (prefixed with a '@') or else the name
297 of the actual XML file to be used in place of "target.xml".
fb1e4ffc 298
9b4b61c8
UW
299 This variable is set up from the auto-generated
300 init_registers_... routine for the current target. */
fb1e4ffc 301
9b4b61c8 302 if (gdbserver_xmltarget
221c031f 303 && strcmp (annex, "target.xml") == 0)
23181151 304 {
9b4b61c8
UW
305 if (*gdbserver_xmltarget == '@')
306 return gdbserver_xmltarget + 1;
23181151 307 else
9b4b61c8 308 annex = gdbserver_xmltarget;
23181151
DJ
309 }
310
9b4b61c8
UW
311#ifdef USE_XML
312 {
313 extern const char *const xml_builtin[][2];
314 int i;
315
316 /* Look for the annex. */
317 for (i = 0; xml_builtin[i][0] != NULL; i++)
318 if (strcmp (annex, xml_builtin[i][0]) == 0)
319 break;
320
321 if (xml_builtin[i][0] != NULL)
322 return xml_builtin[i][1];
323 }
324#endif
325
326 return NULL;
23181151
DJ
327}
328
c74d0ad8
DJ
329void
330monitor_show_help (void)
331{
332 monitor_output ("The following monitor commands are supported:\n");
333 monitor_output (" set debug <0|1>\n");
334 monitor_output (" Enable general debugging messages\n");
335 monitor_output (" set remote-debug <0|1>\n");
336 monitor_output (" Enable remote protocol debugging messages\n");
ecd7ecbc
DJ
337 monitor_output (" exit\n");
338 monitor_output (" Quit GDBserver\n");
c74d0ad8
DJ
339}
340
08388c79
DE
341/* Subroutine of handle_search_memory to simplify it. */
342
343static int
344handle_search_memory_1 (CORE_ADDR start_addr, CORE_ADDR search_space_len,
345 gdb_byte *pattern, unsigned pattern_len,
346 gdb_byte *search_buf,
347 unsigned chunk_size, unsigned search_buf_size,
348 CORE_ADDR *found_addrp)
349{
350 /* Prime the search buffer. */
351
352 if (read_inferior_memory (start_addr, search_buf, search_buf_size) != 0)
353 {
5e1471f5 354 warning ("Unable to access target memory at 0x%lx, halting search.",
08388c79
DE
355 (long) start_addr);
356 return -1;
357 }
358
359 /* Perform the search.
360
361 The loop is kept simple by allocating [N + pattern-length - 1] bytes.
362 When we've scanned N bytes we copy the trailing bytes to the start and
363 read in another N bytes. */
364
365 while (search_space_len >= pattern_len)
366 {
367 gdb_byte *found_ptr;
368 unsigned nr_search_bytes = (search_space_len < search_buf_size
369 ? search_space_len
370 : search_buf_size);
371
372 found_ptr = memmem (search_buf, nr_search_bytes, pattern, pattern_len);
373
374 if (found_ptr != NULL)
375 {
376 CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
377 *found_addrp = found_addr;
378 return 1;
379 }
380
381 /* Not found in this chunk, skip to next chunk. */
382
383 /* Don't let search_space_len wrap here, it's unsigned. */
384 if (search_space_len >= chunk_size)
385 search_space_len -= chunk_size;
386 else
387 search_space_len = 0;
388
389 if (search_space_len >= pattern_len)
390 {
391 unsigned keep_len = search_buf_size - chunk_size;
392 CORE_ADDR read_addr = start_addr + keep_len;
393 int nr_to_read;
394
395 /* Copy the trailing part of the previous iteration to the front
396 of the buffer for the next iteration. */
397 memcpy (search_buf, search_buf + chunk_size, keep_len);
398
399 nr_to_read = (search_space_len - keep_len < chunk_size
400 ? search_space_len - keep_len
401 : chunk_size);
402
403 if (read_inferior_memory (read_addr, search_buf + keep_len,
404 nr_to_read) != 0)
405 {
5e1471f5 406 warning ("Unable to access target memory at 0x%lx, halting search.",
08388c79
DE
407 (long) read_addr);
408 return -1;
409 }
410
411 start_addr += chunk_size;
412 }
413 }
414
415 /* Not found. */
416
417 return 0;
418}
419
420/* Handle qSearch:memory packets. */
421
422static void
423handle_search_memory (char *own_buf, int packet_len)
424{
425 CORE_ADDR start_addr;
426 CORE_ADDR search_space_len;
427 gdb_byte *pattern;
428 unsigned int pattern_len;
429 /* NOTE: also defined in find.c testcase. */
430#define SEARCH_CHUNK_SIZE 16000
431 const unsigned chunk_size = SEARCH_CHUNK_SIZE;
432 /* Buffer to hold memory contents for searching. */
433 gdb_byte *search_buf;
434 unsigned search_buf_size;
435 int found;
436 CORE_ADDR found_addr;
437 int cmd_name_len = sizeof ("qSearch:memory:") - 1;
438
bca929d3 439 pattern = xmalloc (packet_len);
08388c79
DE
440 if (pattern == NULL)
441 {
5e1471f5 442 error ("Unable to allocate memory to perform the search");
08388c79
DE
443 strcpy (own_buf, "E00");
444 return;
445 }
446 if (decode_search_memory_packet (own_buf + cmd_name_len,
447 packet_len - cmd_name_len,
448 &start_addr, &search_space_len,
449 pattern, &pattern_len) < 0)
450 {
451 free (pattern);
5e1471f5 452 error ("Error in parsing qSearch:memory packet");
08388c79
DE
453 strcpy (own_buf, "E00");
454 return;
455 }
456
457 search_buf_size = chunk_size + pattern_len - 1;
458
459 /* No point in trying to allocate a buffer larger than the search space. */
460 if (search_space_len < search_buf_size)
461 search_buf_size = search_space_len;
462
bca929d3 463 search_buf = xmalloc (search_buf_size);
08388c79
DE
464 if (search_buf == NULL)
465 {
466 free (pattern);
5e1471f5 467 error ("Unable to allocate memory to perform the search");
08388c79
DE
468 strcpy (own_buf, "E00");
469 return;
470 }
471
472 found = handle_search_memory_1 (start_addr, search_space_len,
473 pattern, pattern_len,
474 search_buf, chunk_size, search_buf_size,
475 &found_addr);
476
477 if (found > 0)
478 sprintf (own_buf, "1,%lx", (long) found_addr);
479 else if (found == 0)
480 strcpy (own_buf, "0");
481 else
482 strcpy (own_buf, "E00");
483
484 free (search_buf);
485 free (pattern);
486}
487
2d717e4f
DJ
488#define require_running(BUF) \
489 if (!target_running ()) \
490 { \
491 write_enn (BUF); \
492 return; \
493 }
494
ce3a066d
DJ
495/* Handle all of the extended 'q' packets. */
496void
0e7f50da 497handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
ce3a066d 498{
0d62e5e8
DJ
499 static struct inferior_list_entry *thread_ptr;
500
bb63802a 501 /* Reply the current thread id. */
db42f210 502 if (strcmp ("qC", own_buf) == 0 && !disable_packet_qC)
bb63802a 503 {
2d717e4f 504 require_running (own_buf);
bb63802a
UW
505 thread_ptr = all_threads.head;
506 sprintf (own_buf, "QC%x",
db42f210 507 thread_to_gdb_id ((struct thread_info *)thread_ptr));
bb63802a
UW
508 return;
509 }
510
ce3a066d
DJ
511 if (strcmp ("qSymbol::", own_buf) == 0)
512 {
2d717e4f 513 if (target_running () && the_target->look_up_symbols != NULL)
2f2893d9
DJ
514 (*the_target->look_up_symbols) ();
515
ce3a066d
DJ
516 strcpy (own_buf, "OK");
517 return;
518 }
519
db42f210 520 if (!disable_packet_qfThreadInfo)
0d62e5e8 521 {
db42f210 522 if (strcmp ("qfThreadInfo", own_buf) == 0)
0d62e5e8 523 {
db42f210
PA
524 require_running (own_buf);
525 thread_ptr = all_threads.head;
a06660f7 526 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
0d62e5e8
DJ
527 thread_ptr = thread_ptr->next;
528 return;
529 }
db42f210
PA
530
531 if (strcmp ("qsThreadInfo", own_buf) == 0)
0d62e5e8 532 {
db42f210
PA
533 require_running (own_buf);
534 if (thread_ptr != NULL)
535 {
536 sprintf (own_buf, "m%x", thread_to_gdb_id ((struct thread_info *)thread_ptr));
537 thread_ptr = thread_ptr->next;
538 return;
539 }
540 else
541 {
542 sprintf (own_buf, "l");
543 return;
544 }
0d62e5e8
DJ
545 }
546 }
aa691b87 547
52fb6437
NS
548 if (the_target->read_offsets != NULL
549 && strcmp ("qOffsets", own_buf) == 0)
550 {
551 CORE_ADDR text, data;
2d717e4f
DJ
552
553 require_running (own_buf);
52fb6437
NS
554 if (the_target->read_offsets (&text, &data))
555 sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
556 (long)text, (long)data, (long)data);
557 else
558 write_enn (own_buf);
559
560 return;
561 }
562
0e7f50da
UW
563 if (the_target->qxfer_spu != NULL
564 && strncmp ("qXfer:spu:read:", own_buf, 15) == 0)
565 {
566 char *annex;
567 int n;
568 unsigned int len;
569 CORE_ADDR ofs;
570 unsigned char *spu_buf;
571
2d717e4f 572 require_running (own_buf);
0e7f50da
UW
573 strcpy (own_buf, "E00");
574 if (decode_xfer_read (own_buf + 15, &annex, &ofs, &len) < 0)
575 return;
576 if (len > PBUFSIZ - 2)
577 len = PBUFSIZ - 2;
bca929d3 578 spu_buf = xmalloc (len + 1);
0e7f50da
UW
579 if (!spu_buf)
580 return;
581
582 n = (*the_target->qxfer_spu) (annex, spu_buf, NULL, ofs, len + 1);
583 if (n < 0)
584 write_enn (own_buf);
585 else if (n > len)
586 *new_packet_len_p = write_qxfer_response
587 (own_buf, spu_buf, len, 1);
588 else
589 *new_packet_len_p = write_qxfer_response
590 (own_buf, spu_buf, n, 0);
591
592 free (spu_buf);
593 return;
594 }
595
596 if (the_target->qxfer_spu != NULL
597 && strncmp ("qXfer:spu:write:", own_buf, 16) == 0)
598 {
599 char *annex;
600 int n;
601 unsigned int len;
602 CORE_ADDR ofs;
603 unsigned char *spu_buf;
604
2d717e4f 605 require_running (own_buf);
0e7f50da 606 strcpy (own_buf, "E00");
bca929d3 607 spu_buf = xmalloc (packet_len - 15);
0e7f50da
UW
608 if (!spu_buf)
609 return;
610 if (decode_xfer_write (own_buf + 16, packet_len - 16, &annex,
611 &ofs, &len, spu_buf) < 0)
612 {
613 free (spu_buf);
614 return;
615 }
616
617 n = (*the_target->qxfer_spu)
618 (annex, NULL, (unsigned const char *)spu_buf, ofs, len);
619 if (n < 0)
620 write_enn (own_buf);
621 else
622 sprintf (own_buf, "%x", n);
623
624 free (spu_buf);
625 return;
626 }
627
aa691b87 628 if (the_target->read_auxv != NULL
0876f84a 629 && strncmp ("qXfer:auxv:read:", own_buf, 16) == 0)
aa691b87 630 {
0876f84a
DJ
631 unsigned char *data;
632 int n;
aa691b87
RM
633 CORE_ADDR ofs;
634 unsigned int len;
0876f84a
DJ
635 char *annex;
636
2d717e4f
DJ
637 require_running (own_buf);
638
0876f84a
DJ
639 /* Reject any annex; grab the offset and length. */
640 if (decode_xfer_read (own_buf + 16, &annex, &ofs, &len) < 0
641 || annex[0] != '\0')
642 {
643 strcpy (own_buf, "E00");
644 return;
645 }
646
647 /* Read one extra byte, as an indicator of whether there is
648 more. */
649 if (len > PBUFSIZ - 2)
650 len = PBUFSIZ - 2;
bca929d3 651 data = xmalloc (len + 1);
0876f84a 652 n = (*the_target->read_auxv) (ofs, data, len + 1);
000ef4f0
DJ
653 if (n < 0)
654 write_enn (own_buf);
655 else if (n > len)
0876f84a 656 *new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
aa691b87 657 else
0876f84a
DJ
658 *new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
659
660 free (data);
661
aa691b87
RM
662 return;
663 }
664
23181151
DJ
665 if (strncmp ("qXfer:features:read:", own_buf, 20) == 0)
666 {
667 CORE_ADDR ofs;
668 unsigned int len, total_len;
669 const char *document;
670 char *annex;
671
2d717e4f
DJ
672 require_running (own_buf);
673
fb1e4ffc
DJ
674 /* Grab the annex, offset, and length. */
675 if (decode_xfer_read (own_buf + 20, &annex, &ofs, &len) < 0)
676 {
677 strcpy (own_buf, "E00");
678 return;
679 }
680
681 /* Now grab the correct annex. */
682 document = get_features_xml (annex);
683 if (document == NULL)
23181151
DJ
684 {
685 strcpy (own_buf, "E00");
686 return;
687 }
688
689 total_len = strlen (document);
690 if (len > PBUFSIZ - 2)
691 len = PBUFSIZ - 2;
692
693 if (ofs > total_len)
694 write_enn (own_buf);
695 else if (len < total_len - ofs)
696 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
697 len, 1);
698 else
699 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
700 total_len - ofs, 0);
701
702 return;
703 }
704
255e7678
DJ
705 if (strncmp ("qXfer:libraries:read:", own_buf, 21) == 0)
706 {
707 CORE_ADDR ofs;
708 unsigned int len, total_len;
709 char *document, *p;
710 struct inferior_list_entry *dll_ptr;
711 char *annex;
712
2d717e4f
DJ
713 require_running (own_buf);
714
255e7678
DJ
715 /* Reject any annex; grab the offset and length. */
716 if (decode_xfer_read (own_buf + 21, &annex, &ofs, &len) < 0
717 || annex[0] != '\0')
718 {
719 strcpy (own_buf, "E00");
720 return;
721 }
722
723 /* Over-estimate the necessary memory. Assume that every character
724 in the library name must be escaped. */
725 total_len = 64;
726 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
727 total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name);
728
bca929d3 729 document = xmalloc (total_len);
255e7678
DJ
730 strcpy (document, "<library-list>\n");
731 p = document + strlen (document);
732
733 for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next)
734 {
735 struct dll_info *dll = (struct dll_info *) dll_ptr;
736 char *name;
737
738 strcpy (p, " <library name=\"");
739 p = p + strlen (p);
740 name = xml_escape_text (dll->name);
741 strcpy (p, name);
742 free (name);
743 p = p + strlen (p);
744 strcpy (p, "\"><segment address=\"");
745 p = p + strlen (p);
746 sprintf (p, "0x%lx", (long) dll->base_addr);
747 p = p + strlen (p);
748 strcpy (p, "\"/></library>\n");
749 p = p + strlen (p);
750 }
751
752 strcpy (p, "</library-list>\n");
753
754 total_len = strlen (document);
755 if (len > PBUFSIZ - 2)
756 len = PBUFSIZ - 2;
757
758 if (ofs > total_len)
759 write_enn (own_buf);
760 else if (len < total_len - ofs)
761 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
762 len, 1);
763 else
764 *new_packet_len_p = write_qxfer_response (own_buf, document + ofs,
765 total_len - ofs, 0);
766
767 free (document);
768 return;
769 }
770
07e059b5
VP
771 if (the_target->qxfer_osdata != NULL
772 && strncmp ("qXfer:osdata:read:", own_buf, 18) == 0)
773 {
774 char *annex;
775 int n;
776 unsigned int len;
777 CORE_ADDR ofs;
778 unsigned char *workbuf;
779
780 strcpy (own_buf, "E00");
781 if (decode_xfer_read (own_buf + 18, &annex, &ofs, &len) < 0)
782 return;
783 if (len > PBUFSIZ - 2)
784 len = PBUFSIZ - 2;
bca929d3 785 workbuf = xmalloc (len + 1);
07e059b5
VP
786 if (!workbuf)
787 return;
788
789 n = (*the_target->qxfer_osdata) (annex, workbuf, NULL, ofs, len + 1);
790 if (n < 0)
791 write_enn (own_buf);
792 else if (n > len)
793 *new_packet_len_p = write_qxfer_response
794 (own_buf, workbuf, len, 1);
795 else
796 *new_packet_len_p = write_qxfer_response
797 (own_buf, workbuf, n, 0);
798
799 free (workbuf);
800 return;
801 }
802
be2a5f71
DJ
803 /* Protocol features query. */
804 if (strncmp ("qSupported", own_buf, 10) == 0
805 && (own_buf[10] == ':' || own_buf[10] == '\0'))
806 {
89be2091 807 sprintf (own_buf, "PacketSize=%x;QPassSignals+", PBUFSIZ - 1);
0876f84a 808
255e7678
DJ
809 /* We do not have any hook to indicate whether the target backend
810 supports qXfer:libraries:read, so always report it. */
811 strcat (own_buf, ";qXfer:libraries:read+");
812
0876f84a 813 if (the_target->read_auxv != NULL)
9f2e1e63 814 strcat (own_buf, ";qXfer:auxv:read+");
2d717e4f 815
0e7f50da
UW
816 if (the_target->qxfer_spu != NULL)
817 strcat (own_buf, ";qXfer:spu:read+;qXfer:spu:write+");
0876f84a 818
221c031f
UW
819 /* We always report qXfer:features:read, as targets may
820 install XML files on a subsequent call to arch_setup.
821 If we reported to GDB on startup that we don't support
822 qXfer:feature:read at all, we will never be re-queried. */
823 strcat (own_buf, ";qXfer:features:read+");
23181151 824
a6f3e723
SL
825 if (transport_is_reliable)
826 strcat (own_buf, ";QStartNoAckMode+");
07e059b5
VP
827
828 if (the_target->qxfer_osdata != NULL)
829 strcat (own_buf, ";qXfer:osdata:read+");
830
be2a5f71
DJ
831 return;
832 }
833
dae5f5cf
DJ
834 /* Thread-local storage support. */
835 if (the_target->get_tls_address != NULL
836 && strncmp ("qGetTLSAddr:", own_buf, 12) == 0)
837 {
838 char *p = own_buf + 12;
839 CORE_ADDR parts[3], address = 0;
840 int i, err;
841
2d717e4f
DJ
842 require_running (own_buf);
843
dae5f5cf
DJ
844 for (i = 0; i < 3; i++)
845 {
846 char *p2;
847 int len;
848
849 if (p == NULL)
850 break;
851
852 p2 = strchr (p, ',');
853 if (p2)
854 {
855 len = p2 - p;
856 p2++;
857 }
858 else
859 {
860 len = strlen (p);
861 p2 = NULL;
862 }
863
864 decode_address (&parts[i], p, len);
865 p = p2;
866 }
867
868 if (p != NULL || i < 3)
869 err = 1;
870 else
871 {
872 struct thread_info *thread = gdb_id_to_thread (parts[0]);
873
874 if (thread == NULL)
875 err = 2;
876 else
877 err = the_target->get_tls_address (thread, parts[1], parts[2],
878 &address);
879 }
880
881 if (err == 0)
882 {
883 sprintf (own_buf, "%llx", address);
884 return;
885 }
886 else if (err > 0)
887 {
888 write_enn (own_buf);
889 return;
890 }
891
892 /* Otherwise, pretend we do not understand this packet. */
893 }
894
c74d0ad8
DJ
895 /* Handle "monitor" commands. */
896 if (strncmp ("qRcmd,", own_buf, 6) == 0)
897 {
bca929d3 898 char *mon = xmalloc (PBUFSIZ);
c74d0ad8
DJ
899 int len = strlen (own_buf + 6);
900
d41b6bb4 901 if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
c74d0ad8
DJ
902 {
903 write_enn (own_buf);
904 free (mon);
905 return;
906 }
907 mon[len / 2] = '\0';
908
909 write_ok (own_buf);
910
911 if (strcmp (mon, "set debug 1") == 0)
912 {
913 debug_threads = 1;
914 monitor_output ("Debug output enabled.\n");
915 }
916 else if (strcmp (mon, "set debug 0") == 0)
917 {
918 debug_threads = 0;
919 monitor_output ("Debug output disabled.\n");
920 }
921 else if (strcmp (mon, "set remote-debug 1") == 0)
922 {
923 remote_debug = 1;
924 monitor_output ("Protocol debug output enabled.\n");
925 }
926 else if (strcmp (mon, "set remote-debug 0") == 0)
927 {
928 remote_debug = 0;
929 monitor_output ("Protocol debug output disabled.\n");
930 }
931 else if (strcmp (mon, "help") == 0)
932 monitor_show_help ();
2d717e4f
DJ
933 else if (strcmp (mon, "exit") == 0)
934 exit_requested = 1;
c74d0ad8
DJ
935 else
936 {
937 monitor_output ("Unknown monitor command.\n\n");
938 monitor_show_help ();
939 write_enn (own_buf);
940 }
941
942 free (mon);
943 return;
944 }
945
08388c79
DE
946 if (strncmp ("qSearch:memory:", own_buf, sizeof ("qSearch:memory:") - 1) == 0)
947 {
948 require_running (own_buf);
949 handle_search_memory (own_buf, packet_len);
950 return;
951 }
952
ce3a066d
DJ
953 /* Otherwise we didn't know what packet it was. Say we didn't
954 understand it. */
955 own_buf[0] = 0;
956}
957
64386c31
DJ
958/* Parse vCont packets. */
959void
fc620387 960handle_v_cont (char *own_buf, char *status, int *signal)
64386c31
DJ
961{
962 char *p, *q;
963 int n = 0, i = 0;
964 struct thread_resume *resume_info, default_action;
965
966 /* Count the number of semicolons in the packet. There should be one
967 for every action. */
968 p = &own_buf[5];
969 while (p)
970 {
971 n++;
972 p++;
973 p = strchr (p, ';');
974 }
975 /* Allocate room for one extra action, for the default remain-stopped
976 behavior; if no default action is in the list, we'll need the extra
977 slot. */
bca929d3 978 resume_info = xmalloc ((n + 1) * sizeof (resume_info[0]));
64386c31
DJ
979
980 default_action.thread = -1;
981 default_action.leave_stopped = 1;
982 default_action.step = 0;
983 default_action.sig = 0;
984
985 p = &own_buf[5];
986 i = 0;
987 while (*p)
988 {
989 p++;
990
991 resume_info[i].leave_stopped = 0;
992
993 if (p[0] == 's' || p[0] == 'S')
994 resume_info[i].step = 1;
995 else if (p[0] == 'c' || p[0] == 'C')
996 resume_info[i].step = 0;
997 else
998 goto err;
999
1000 if (p[0] == 'S' || p[0] == 'C')
1001 {
1002 int sig;
1003 sig = strtol (p + 1, &q, 16);
1004 if (p == q)
1005 goto err;
1006 p = q;
1007
1008 if (!target_signal_to_host_p (sig))
1009 goto err;
1010 resume_info[i].sig = target_signal_to_host (sig);
1011 }
1012 else
1013 {
1014 resume_info[i].sig = 0;
1015 p = p + 1;
1016 }
1017
1018 if (p[0] == 0)
1019 {
1020 resume_info[i].thread = -1;
1021 default_action = resume_info[i];
1022
1023 /* Note: we don't increment i here, we'll overwrite this entry
1024 the next time through. */
1025 }
1026 else if (p[0] == ':')
1027 {
a06660f7
DJ
1028 unsigned int gdb_id = strtoul (p + 1, &q, 16);
1029 unsigned long thread_id;
1030
64386c31
DJ
1031 if (p == q)
1032 goto err;
1033 p = q;
1034 if (p[0] != ';' && p[0] != 0)
1035 goto err;
1036
a06660f7
DJ
1037 thread_id = gdb_id_to_thread_id (gdb_id);
1038 if (thread_id)
1039 resume_info[i].thread = thread_id;
1040 else
1041 goto err;
1042
64386c31
DJ
1043 i++;
1044 }
1045 }
1046
1047 resume_info[i] = default_action;
1048
1049 /* Still used in occasional places in the backend. */
1050 if (n == 1 && resume_info[0].thread != -1)
1051 cont_thread = resume_info[0].thread;
1052 else
1053 cont_thread = -1;
dc3f8883 1054 set_desired_inferior (0);
64386c31 1055
a20d5e98 1056 enable_async_io ();
64386c31
DJ
1057 (*the_target->resume) (resume_info);
1058
1059 free (resume_info);
1060
1061 *signal = mywait (status, 1);
1062 prepare_resume_reply (own_buf, *status, *signal);
a20d5e98 1063 disable_async_io ();
64386c31
DJ
1064 return;
1065
1066err:
255e7678 1067 write_enn (own_buf);
64386c31
DJ
1068 free (resume_info);
1069 return;
1070}
1071
2d717e4f
DJ
1072/* Attach to a new program. Return 1 if successful, 0 if failure. */
1073int
1074handle_v_attach (char *own_buf, char *status, int *signal)
1075{
1076 int pid;
1077
1078 pid = strtol (own_buf + 8, NULL, 16);
1079 if (pid != 0 && attach_inferior (pid, status, signal) == 0)
1080 {
aeba519e
PA
1081 /* Don't report shared library events after attaching, even if
1082 some libraries are preloaded. GDB will always poll the
1083 library list. Avoids the "stopped by shared library event"
1084 notice on the GDB side. */
1085 dlls_changed = 0;
2d717e4f
DJ
1086 prepare_resume_reply (own_buf, *status, *signal);
1087 return 1;
1088 }
1089 else
1090 {
1091 write_enn (own_buf);
1092 return 0;
1093 }
1094}
1095
1096/* Run a new program. Return 1 if successful, 0 if failure. */
1097static int
1098handle_v_run (char *own_buf, char *status, int *signal)
1099{
1100 char *p, **pp, *next_p, **new_argv;
1101 int i, new_argc;
1102
1103 new_argc = 0;
1104 for (p = own_buf + strlen ("vRun;"); p && *p; p = strchr (p, ';'))
1105 {
1106 p++;
1107 new_argc++;
1108 }
1109
bca929d3 1110 new_argv = xcalloc (new_argc + 2, sizeof (char *));
2d717e4f
DJ
1111 i = 0;
1112 for (p = own_buf + strlen ("vRun;"); *p; p = next_p)
1113 {
1114 next_p = strchr (p, ';');
1115 if (next_p == NULL)
1116 next_p = p + strlen (p);
1117
1118 if (i == 0 && p == next_p)
1119 new_argv[i] = NULL;
1120 else
1121 {
bca929d3 1122 new_argv[i] = xmalloc (1 + (next_p - p) / 2);
2d717e4f
DJ
1123 unhexify (new_argv[i], p, (next_p - p) / 2);
1124 new_argv[i][(next_p - p) / 2] = '\0';
1125 }
1126
1127 if (*next_p)
1128 next_p++;
1129 i++;
1130 }
1131 new_argv[i] = NULL;
1132
1133 if (new_argv[0] == NULL)
1134 {
f142445f
DJ
1135 /* GDB didn't specify a program to run. Use the program from the
1136 last run with the new argument list. */
9b710a42 1137
2d717e4f
DJ
1138 if (program_argv == NULL)
1139 {
1140 write_enn (own_buf);
1141 return 0;
1142 }
1143
bca929d3 1144 new_argv[0] = xstrdup (program_argv[0]);
2d717e4f 1145 }
f142445f
DJ
1146
1147 /* Free the old argv. */
1148 if (program_argv)
2d717e4f 1149 {
f142445f
DJ
1150 for (pp = program_argv; *pp != NULL; pp++)
1151 free (*pp);
1152 free (program_argv);
2d717e4f 1153 }
f142445f 1154 program_argv = new_argv;
2d717e4f
DJ
1155
1156 *signal = start_inferior (program_argv, status);
1157 if (*status == 'T')
1158 {
1159 prepare_resume_reply (own_buf, *status, *signal);
1160 return 1;
1161 }
1162 else
1163 {
1164 write_enn (own_buf);
1165 return 0;
1166 }
1167}
1168
64386c31
DJ
1169/* Handle all of the extended 'v' packets. */
1170void
a6b151f1
DJ
1171handle_v_requests (char *own_buf, char *status, int *signal,
1172 int packet_len, int *new_packet_len)
64386c31 1173{
db42f210 1174 if (!disable_packet_vCont)
64386c31 1175 {
db42f210
PA
1176 if (strncmp (own_buf, "vCont;", 6) == 0)
1177 {
1178 require_running (own_buf);
1179 handle_v_cont (own_buf, status, signal);
1180 return;
1181 }
64386c31 1182
db42f210
PA
1183 if (strncmp (own_buf, "vCont?", 6) == 0)
1184 {
1185 strcpy (own_buf, "vCont;c;C;s;S");
1186 return;
1187 }
64386c31
DJ
1188 }
1189
a6b151f1
DJ
1190 if (strncmp (own_buf, "vFile:", 6) == 0
1191 && handle_vFile (own_buf, packet_len, new_packet_len))
1192 return;
1193
2d717e4f
DJ
1194 if (strncmp (own_buf, "vAttach;", 8) == 0)
1195 {
1196 if (target_running ())
1197 {
fd96d250
PA
1198 fprintf (stderr, "Already debugging a process\n");
1199 write_enn (own_buf);
1200 return;
2d717e4f
DJ
1201 }
1202 handle_v_attach (own_buf, status, signal);
1203 return;
1204 }
1205
1206 if (strncmp (own_buf, "vRun;", 5) == 0)
1207 {
1208 if (target_running ())
1209 {
fd96d250
PA
1210 fprintf (stderr, "Already debugging a process\n");
1211 write_enn (own_buf);
1212 return;
2d717e4f
DJ
1213 }
1214 handle_v_run (own_buf, status, signal);
1215 return;
1216 }
1217
64386c31
DJ
1218 /* Otherwise we didn't know what packet it was. Say we didn't
1219 understand it. */
1220 own_buf[0] = 0;
1221 return;
1222}
1223
1224void
27524b67 1225myresume (char *own_buf, int step, int *signalp, char *statusp)
64386c31
DJ
1226{
1227 struct thread_resume resume_info[2];
1228 int n = 0;
a20d5e98
DJ
1229 int sig = *signalp;
1230
1231 set_desired_inferior (0);
64386c31 1232
d592fa2f 1233 if (step || sig || (cont_thread != 0 && cont_thread != -1))
64386c31
DJ
1234 {
1235 resume_info[0].thread
1236 = ((struct inferior_list_entry *) current_inferior)->id;
1237 resume_info[0].step = step;
1238 resume_info[0].sig = sig;
1239 resume_info[0].leave_stopped = 0;
1240 n++;
1241 }
1242 resume_info[n].thread = -1;
1243 resume_info[n].step = 0;
1244 resume_info[n].sig = 0;
d592fa2f 1245 resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1);
64386c31 1246
a20d5e98 1247 enable_async_io ();
64386c31 1248 (*the_target->resume) (resume_info);
a20d5e98
DJ
1249 *signalp = mywait (statusp, 1);
1250 prepare_resume_reply (own_buf, *statusp, *signalp);
1251 disable_async_io ();
64386c31
DJ
1252}
1253
dd24457d
DJ
1254static void
1255gdbserver_version (void)
1256{
c16158bc 1257 printf ("GNU gdbserver %s%s\n"
255e7678 1258 "Copyright (C) 2007 Free Software Foundation, Inc.\n"
dd24457d
DJ
1259 "gdbserver is free software, covered by the GNU General Public License.\n"
1260 "This gdbserver was configured as \"%s\"\n",
c16158bc 1261 PKGVERSION, version, host_name);
dd24457d
DJ
1262}
1263
0bc68c49 1264static void
c16158bc 1265gdbserver_usage (FILE *stream)
0bc68c49 1266{
c16158bc
JM
1267 fprintf (stream, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
1268 "\tgdbserver [OPTIONS] --attach COMM PID\n"
1269 "\tgdbserver [OPTIONS] --multi COMM\n"
1270 "\n"
1271 "COMM may either be a tty device (for serial debugging), or \n"
1272 "HOST:PORT to listen for a TCP connection.\n"
1273 "\n"
1274 "Options:\n"
1275 " --debug\t\tEnable debugging output.\n"
8e4c5421 1276 " --version\t\tDisplay version information and exit.\n"
c16158bc
JM
1277 " --wrapper WRAPPER --\tRun WRAPPER to start new programs.\n");
1278 if (REPORT_BUGS_TO[0] && stream == stdout)
1279 fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
0bc68c49
DJ
1280}
1281
db42f210
PA
1282static void
1283gdbserver_show_disableable (FILE *stream)
1284{
1285 fprintf (stream, "Disableable packets:\n"
1286 " vCont \tAll vCont packets\n"
1287 " qC \tQuerying the current thread\n"
1288 " qfThreadInfo\tThread listing\n"
1289 " Tthread \tPassing the thread specifier in the T stop reply packet\n"
1290 " threads \tAll of the above\n");
1291}
1292
1293
2d717e4f
DJ
1294#undef require_running
1295#define require_running(BUF) \
1296 if (!target_running ()) \
1297 { \
1298 write_enn (BUF); \
1299 break; \
1300 }
1301
c906108c 1302int
da85418c 1303main (int argc, char *argv[])
c906108c 1304{
f450004a 1305 char ch, status, *own_buf;
7fb85e41 1306 unsigned char *mem_buf;
c906108c 1307 int i = 0;
fc620387 1308 int signal;
c906108c
SS
1309 unsigned int len;
1310 CORE_ADDR mem_addr;
0729219d
DJ
1311 int bad_attach;
1312 int pid;
2d717e4f
DJ
1313 char *arg_end, *port;
1314 char **next_arg = &argv[1];
1315 int multi_mode = 0;
1316 int attach = 0;
1317 int was_running;
c906108c 1318
2d717e4f 1319 while (*next_arg != NULL && **next_arg == '-')
dd24457d 1320 {
2d717e4f
DJ
1321 if (strcmp (*next_arg, "--version") == 0)
1322 {
1323 gdbserver_version ();
1324 exit (0);
1325 }
1326 else if (strcmp (*next_arg, "--help") == 0)
1327 {
c16158bc 1328 gdbserver_usage (stdout);
2d717e4f
DJ
1329 exit (0);
1330 }
1331 else if (strcmp (*next_arg, "--attach") == 0)
1332 attach = 1;
1333 else if (strcmp (*next_arg, "--multi") == 0)
1334 multi_mode = 1;
ccd213ac
DJ
1335 else if (strcmp (*next_arg, "--wrapper") == 0)
1336 {
1337 next_arg++;
1338
1339 wrapper_argv = next_arg;
1340 while (*next_arg != NULL && strcmp (*next_arg, "--") != 0)
1341 next_arg++;
1342
1343 if (next_arg == wrapper_argv || *next_arg == NULL)
1344 {
c16158bc 1345 gdbserver_usage (stderr);
ccd213ac
DJ
1346 exit (1);
1347 }
1348
1349 /* Consume the "--". */
1350 *next_arg = NULL;
1351 }
2d717e4f
DJ
1352 else if (strcmp (*next_arg, "--debug") == 0)
1353 debug_threads = 1;
db42f210
PA
1354 else if (strcmp (*next_arg, "--disable-packet") == 0)
1355 {
1356 gdbserver_show_disableable (stdout);
1357 exit (0);
1358 }
1359 else if (strncmp (*next_arg,
1360 "--disable-packet=",
1361 sizeof ("--disable-packet=") - 1) == 0)
1362 {
1363 char *packets, *tok;
1364
1365 packets = *next_arg += sizeof ("--disable-packet=") - 1;
1366 for (tok = strtok (packets, ",");
1367 tok != NULL;
1368 tok = strtok (NULL, ","))
1369 {
1370 if (strcmp ("vCont", tok) == 0)
1371 disable_packet_vCont = 1;
1372 else if (strcmp ("Tthread", tok) == 0)
1373 disable_packet_Tthread = 1;
1374 else if (strcmp ("qC", tok) == 0)
1375 disable_packet_qC = 1;
1376 else if (strcmp ("qfThreadInfo", tok) == 0)
1377 disable_packet_qfThreadInfo = 1;
1378 else if (strcmp ("threads", tok) == 0)
1379 {
1380 disable_packet_vCont = 1;
1381 disable_packet_Tthread = 1;
1382 disable_packet_qC = 1;
1383 disable_packet_qfThreadInfo = 1;
1384 }
1385 else
1386 {
1387 fprintf (stderr, "Don't know how to disable \"%s\".\n\n",
1388 tok);
1389 gdbserver_show_disableable (stderr);
1390 exit (1);
1391 }
1392 }
1393 }
2d717e4f
DJ
1394 else
1395 {
1396 fprintf (stderr, "Unknown argument: %s\n", *next_arg);
1397 exit (1);
1398 }
dd24457d 1399
2d717e4f
DJ
1400 next_arg++;
1401 continue;
dd24457d
DJ
1402 }
1403
c5aa993b 1404 if (setjmp (toplevel))
c906108c 1405 {
c5aa993b
JM
1406 fprintf (stderr, "Exiting\n");
1407 exit (1);
c906108c
SS
1408 }
1409
2d717e4f
DJ
1410 port = *next_arg;
1411 next_arg++;
1412 if (port == NULL || (!attach && !multi_mode && *next_arg == NULL))
1413 {
c16158bc 1414 gdbserver_usage (stderr);
2d717e4f
DJ
1415 exit (1);
1416 }
1417
0729219d
DJ
1418 bad_attach = 0;
1419 pid = 0;
2d717e4f
DJ
1420
1421 /* --attach used to come after PORT, so allow it there for
1422 compatibility. */
1423 if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0)
45b7b345 1424 {
2d717e4f
DJ
1425 attach = 1;
1426 next_arg++;
45b7b345
DJ
1427 }
1428
2d717e4f
DJ
1429 if (attach
1430 && (*next_arg == NULL
1431 || (*next_arg)[0] == '\0'
1432 || (pid = strtoul (*next_arg, &arg_end, 0)) == 0
1433 || *arg_end != '\0'
1434 || next_arg[1] != NULL))
1435 bad_attach = 1;
1436
1437 if (bad_attach)
dd24457d 1438 {
c16158bc 1439 gdbserver_usage (stderr);
dd24457d
DJ
1440 exit (1);
1441 }
c906108c 1442
a20d5e98 1443 initialize_async_io ();
4ce44c66
JM
1444 initialize_low ();
1445
bca929d3
DE
1446 own_buf = xmalloc (PBUFSIZ + 1);
1447 mem_buf = xmalloc (PBUFSIZ);
0a30fbc4 1448
2d717e4f 1449 if (pid == 0 && *next_arg != NULL)
45b7b345 1450 {
2d717e4f
DJ
1451 int i, n;
1452
1453 n = argc - (next_arg - argv);
bca929d3 1454 program_argv = xmalloc (sizeof (char *) * (n + 1));
2d717e4f 1455 for (i = 0; i < n; i++)
bca929d3 1456 program_argv[i] = xstrdup (next_arg[i]);
2d717e4f
DJ
1457 program_argv[i] = NULL;
1458
45b7b345 1459 /* Wait till we are at first instruction in program. */
2d717e4f 1460 signal = start_inferior (program_argv, &status);
c906108c 1461
c588c53c
MS
1462 /* We are now (hopefully) stopped at the first instruction of
1463 the target process. This assumes that the target process was
1464 successfully created. */
45b7b345 1465 }
2d717e4f
DJ
1466 else if (pid != 0)
1467 {
1468 if (attach_inferior (pid, &status, &signal) == -1)
1469 error ("Attaching not supported on this target");
1470
1471 /* Otherwise succeeded. */
1472 }
45b7b345
DJ
1473 else
1474 {
2d717e4f
DJ
1475 status = 'W';
1476 signal = 0;
45b7b345 1477 }
c906108c 1478
311de423
PA
1479 /* Don't report shared library events on the initial connection,
1480 even if some libraries are preloaded. Avoids the "stopped by
1481 shared library event" notice on gdb side. */
1482 dlls_changed = 0;
1483
8264bb58
DJ
1484 if (setjmp (toplevel))
1485 {
1486 fprintf (stderr, "Killing inferior\n");
1487 kill_inferior ();
1488 exit (1);
1489 }
1490
c588c53c 1491 if (status == 'W' || status == 'X')
2d717e4f
DJ
1492 was_running = 0;
1493 else
1494 was_running = 1;
1495
1496 if (!was_running && !multi_mode)
c588c53c 1497 {
2d717e4f 1498 fprintf (stderr, "No program to debug. GDBserver exiting.\n");
c588c53c
MS
1499 exit (1);
1500 }
1501
c906108c
SS
1502 while (1)
1503 {
a6f3e723 1504 noack_mode = 0;
2d717e4f 1505 remote_open (port);
c906108c 1506
c5aa993b 1507 restart:
2d717e4f
DJ
1508 if (setjmp (toplevel) != 0)
1509 {
1510 /* An error occurred. */
1511 if (response_needed)
1512 {
1513 write_enn (own_buf);
1514 putpkt (own_buf);
1515 }
1516 }
1517
a20d5e98 1518 disable_async_io ();
2d717e4f 1519 while (!exit_requested)
c906108c
SS
1520 {
1521 unsigned char sig;
01f9e8fa
DJ
1522 int packet_len;
1523 int new_packet_len = -1;
1524
2d717e4f 1525 response_needed = 0;
01f9e8fa
DJ
1526 packet_len = getpkt (own_buf);
1527 if (packet_len <= 0)
1528 break;
2d717e4f 1529 response_needed = 1;
01f9e8fa 1530
c906108c
SS
1531 i = 0;
1532 ch = own_buf[i++];
1533 switch (ch)
1534 {
ce3a066d 1535 case 'q':
0e7f50da 1536 handle_query (own_buf, packet_len, &new_packet_len);
ce3a066d 1537 break;
89be2091
DJ
1538 case 'Q':
1539 handle_general_set (own_buf);
1540 break;
6ad8ae5c 1541 case 'D':
2d717e4f 1542 require_running (own_buf);
6ad8ae5c 1543 fprintf (stderr, "Detaching from inferior\n");
444d6139 1544 if (detach_inferior () != 0)
2d717e4f 1545 write_enn (own_buf);
444d6139
PA
1546 else
1547 {
1548 write_ok (own_buf);
6ad8ae5c 1549
2d717e4f
DJ
1550 if (extended_protocol)
1551 {
1552 /* Treat this like a normal program exit. */
1553 signal = 0;
1554 status = 'W';
1555 }
1556 else
1557 {
1558 putpkt (own_buf);
1559 remote_close ();
6ad8ae5c 1560
2d717e4f
DJ
1561 /* If we are attached, then we can exit. Otherwise, we
1562 need to hang around doing nothing, until the child
1563 is gone. */
1564 if (!attached)
1565 join_inferior ();
1566
1567 exit (0);
1568 }
444d6139 1569 }
2d717e4f 1570 break;
c906108c 1571 case '!':
2d717e4f
DJ
1572 extended_protocol = 1;
1573 write_ok (own_buf);
c906108c
SS
1574 break;
1575 case '?':
1576 prepare_resume_reply (own_buf, status, signal);
1577 break;
1578 case 'H':
a06660f7 1579 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's')
c906108c 1580 {
a06660f7
DJ
1581 unsigned long gdb_id, thread_id;
1582
2d717e4f 1583 require_running (own_buf);
a06660f7 1584 gdb_id = strtoul (&own_buf[2], NULL, 16);
2d717e4f
DJ
1585 if (gdb_id == 0 || gdb_id == -1)
1586 thread_id = gdb_id;
1587 else
a06660f7 1588 {
2d717e4f
DJ
1589 thread_id = gdb_id_to_thread_id (gdb_id);
1590 if (thread_id == 0)
1591 {
1592 write_enn (own_buf);
1593 break;
1594 }
a06660f7
DJ
1595 }
1596
1597 if (own_buf[1] == 'g')
1598 {
1599 general_thread = thread_id;
1600 set_desired_inferior (1);
1601 }
1602 else if (own_buf[1] == 'c')
1603 cont_thread = thread_id;
1604 else if (own_buf[1] == 's')
1605 step_thread = thread_id;
1606
0d62e5e8 1607 write_ok (own_buf);
a06660f7
DJ
1608 }
1609 else
1610 {
c906108c
SS
1611 /* Silently ignore it so that gdb can extend the protocol
1612 without compatibility headaches. */
1613 own_buf[0] = '\0';
c906108c
SS
1614 }
1615 break;
1616 case 'g':
2d717e4f 1617 require_running (own_buf);
0d62e5e8 1618 set_desired_inferior (1);
0a30fbc4 1619 registers_to_string (own_buf);
c906108c
SS
1620 break;
1621 case 'G':
2d717e4f 1622 require_running (own_buf);
0d62e5e8 1623 set_desired_inferior (1);
0a30fbc4 1624 registers_from_string (&own_buf[1]);
c906108c
SS
1625 write_ok (own_buf);
1626 break;
1627 case 'm':
2d717e4f 1628 require_running (own_buf);
c906108c 1629 decode_m_packet (&own_buf[1], &mem_addr, &len);
c3e735a6
DJ
1630 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
1631 convert_int_to_ascii (mem_buf, own_buf, len);
1632 else
1633 write_enn (own_buf);
c906108c
SS
1634 break;
1635 case 'M':
2d717e4f 1636 require_running (own_buf);
c906108c
SS
1637 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
1638 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
1639 write_ok (own_buf);
1640 else
1641 write_enn (own_buf);
1642 break;
01f9e8fa 1643 case 'X':
2d717e4f 1644 require_running (own_buf);
01f9e8fa
DJ
1645 if (decode_X_packet (&own_buf[1], packet_len - 1,
1646 &mem_addr, &len, mem_buf) < 0
1647 || write_inferior_memory (mem_addr, mem_buf, len) != 0)
1648 write_enn (own_buf);
1649 else
1650 write_ok (own_buf);
1651 break;
c906108c 1652 case 'C':
2d717e4f 1653 require_running (own_buf);
c906108c 1654 convert_ascii_to_int (own_buf + 1, &sig, 1);
0e98d0a7
DJ
1655 if (target_signal_to_host_p (sig))
1656 signal = target_signal_to_host (sig);
1657 else
1658 signal = 0;
27524b67 1659 myresume (own_buf, 0, &signal, &status);
c906108c
SS
1660 break;
1661 case 'S':
2d717e4f 1662 require_running (own_buf);
c906108c 1663 convert_ascii_to_int (own_buf + 1, &sig, 1);
0e98d0a7
DJ
1664 if (target_signal_to_host_p (sig))
1665 signal = target_signal_to_host (sig);
1666 else
1667 signal = 0;
27524b67 1668 myresume (own_buf, 1, &signal, &status);
c906108c
SS
1669 break;
1670 case 'c':
2d717e4f 1671 require_running (own_buf);
a20d5e98 1672 signal = 0;
27524b67 1673 myresume (own_buf, 0, &signal, &status);
c906108c
SS
1674 break;
1675 case 's':
2d717e4f 1676 require_running (own_buf);
a20d5e98 1677 signal = 0;
27524b67 1678 myresume (own_buf, 1, &signal, &status);
c906108c 1679 break;
e013ee27
OF
1680 case 'Z':
1681 {
1682 char *lenptr;
1683 char *dataptr;
1684 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1685 int len = strtol (lenptr + 1, &dataptr, 16);
1686 char type = own_buf[1];
1687
1688 if (the_target->insert_watchpoint == NULL
1689 || (type < '2' || type > '4'))
1690 {
1691 /* No watchpoint support or not a watchpoint command;
1692 unrecognized either way. */
1693 own_buf[0] = '\0';
1694 }
1695 else
1696 {
1697 int res;
1698
2d717e4f 1699 require_running (own_buf);
e013ee27
OF
1700 res = (*the_target->insert_watchpoint) (type, addr, len);
1701 if (res == 0)
1702 write_ok (own_buf);
1703 else if (res == 1)
1704 /* Unsupported. */
1705 own_buf[0] = '\0';
1706 else
1707 write_enn (own_buf);
1708 }
1709 break;
1710 }
1711 case 'z':
1712 {
1713 char *lenptr;
1714 char *dataptr;
1715 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1716 int len = strtol (lenptr + 1, &dataptr, 16);
1717 char type = own_buf[1];
1718
1719 if (the_target->remove_watchpoint == NULL
1720 || (type < '2' || type > '4'))
1721 {
1722 /* No watchpoint support or not a watchpoint command;
1723 unrecognized either way. */
1724 own_buf[0] = '\0';
1725 }
1726 else
1727 {
1728 int res;
1729
2d717e4f 1730 require_running (own_buf);
e013ee27
OF
1731 res = (*the_target->remove_watchpoint) (type, addr, len);
1732 if (res == 0)
1733 write_ok (own_buf);
1734 else if (res == 1)
1735 /* Unsupported. */
1736 own_buf[0] = '\0';
1737 else
1738 write_enn (own_buf);
1739 }
1740 break;
1741 }
c906108c 1742 case 'k':
2d717e4f
DJ
1743 response_needed = 0;
1744 if (!target_running ())
1745 /* The packet we received doesn't make sense - but we
1746 can't reply to it, either. */
1747 goto restart;
1748
c906108c
SS
1749 fprintf (stderr, "Killing inferior\n");
1750 kill_inferior ();
2d717e4f
DJ
1751
1752 /* When using the extended protocol, we wait with no
1753 program running. The traditional protocol will exit
1754 instead. */
c906108c
SS
1755 if (extended_protocol)
1756 {
2d717e4f
DJ
1757 status = 'X';
1758 signal = TARGET_SIGNAL_KILL;
1759 was_running = 0;
c906108c 1760 goto restart;
c906108c
SS
1761 }
1762 else
1763 {
1764 exit (0);
1765 break;
1766 }
1767 case 'T':
a06660f7
DJ
1768 {
1769 unsigned long gdb_id, thread_id;
1770
2d717e4f 1771 require_running (own_buf);
a06660f7
DJ
1772 gdb_id = strtoul (&own_buf[1], NULL, 16);
1773 thread_id = gdb_id_to_thread_id (gdb_id);
1774 if (thread_id == 0)
1775 {
1776 write_enn (own_buf);
1777 break;
1778 }
1779
1780 if (mythread_alive (thread_id))
1781 write_ok (own_buf);
1782 else
1783 write_enn (own_buf);
1784 }
c906108c
SS
1785 break;
1786 case 'R':
2d717e4f
DJ
1787 response_needed = 0;
1788
c906108c 1789 /* Restarting the inferior is only supported in the
c5aa993b 1790 extended protocol. */
c906108c
SS
1791 if (extended_protocol)
1792 {
2d717e4f
DJ
1793 if (target_running ())
1794 kill_inferior ();
c906108c
SS
1795 fprintf (stderr, "GDBserver restarting\n");
1796
1797 /* Wait till we are at 1st instruction in prog. */
2d717e4f
DJ
1798 if (program_argv != NULL)
1799 signal = start_inferior (program_argv, &status);
1800 else
1801 {
1802 status = 'X';
1803 signal = TARGET_SIGNAL_KILL;
1804 }
c906108c 1805 goto restart;
c906108c
SS
1806 }
1807 else
1808 {
1809 /* It is a request we don't understand. Respond with an
1810 empty packet so that gdb knows that we don't support this
1811 request. */
1812 own_buf[0] = '\0';
1813 break;
1814 }
64386c31
DJ
1815 case 'v':
1816 /* Extended (long) request. */
a6b151f1
DJ
1817 handle_v_requests (own_buf, &status, &signal,
1818 packet_len, &new_packet_len);
64386c31 1819 break;
a6b151f1 1820
c906108c
SS
1821 default:
1822 /* It is a request we don't understand. Respond with an
c5aa993b
JM
1823 empty packet so that gdb knows that we don't support this
1824 request. */
c906108c
SS
1825 own_buf[0] = '\0';
1826 break;
1827 }
1828
01f9e8fa
DJ
1829 if (new_packet_len != -1)
1830 putpkt_binary (own_buf, new_packet_len);
1831 else
1832 putpkt (own_buf);
c906108c 1833
2d717e4f
DJ
1834 response_needed = 0;
1835
1836 if (was_running && (status == 'W' || status == 'X'))
c906108c 1837 {
2d717e4f 1838 was_running = 0;
c906108c 1839
2d717e4f
DJ
1840 if (status == 'W')
1841 fprintf (stderr,
1842 "\nChild exited with status %d\n", signal);
1843 if (status == 'X')
1844 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
1845 target_signal_to_host (signal),
1846 target_signal_to_name (signal));
1847
1848 if (extended_protocol)
1849 goto restart;
c906108c
SS
1850 else
1851 {
1852 fprintf (stderr, "GDBserver exiting\n");
1853 exit (0);
1854 }
1855 }
c906108c 1856
2d717e4f
DJ
1857 if (status != 'W' && status != 'X')
1858 was_running = 1;
1859 }
c906108c 1860
2d717e4f
DJ
1861 /* If an exit was requested (using the "monitor exit" command),
1862 terminate now. The only other way to get here is for
1863 getpkt to fail; close the connection and reopen it at the
1864 top of the loop. */
c906108c 1865
2d717e4f 1866 if (exit_requested)
c906108c
SS
1867 {
1868 remote_close ();
2d717e4f
DJ
1869 if (attached && target_running ())
1870 detach_inferior ();
1871 else if (target_running ())
1872 kill_inferior ();
c906108c
SS
1873 exit (0);
1874 }
1875 else
1876 {
45b7b345
DJ
1877 fprintf (stderr, "Remote side has terminated connection. "
1878 "GDBserver will reopen the connection.\n");
c906108c
SS
1879 remote_close ();
1880 }
1881 }
1882}