1 /* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 This file is part of GDB.
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
32 unsigned long cont_thread
;
33 unsigned long general_thread
;
34 unsigned long step_thread
;
35 unsigned long thread_from_wait
;
36 unsigned long old_thread_from_wait
;
39 static int extended_protocol
;
41 static int response_needed
;
42 static int exit_requested
;
44 static char **program_argv
, **wrapper_argv
;
46 /* Enable miscellaneous debugging output. The name is historical - it
47 was originally used to debug LinuxThreads support. */
50 int pass_signals
[TARGET_SIGNAL_LAST
];
54 const char *gdbserver_xmltarget
;
56 /* The PID of the originally created or attached inferior. Used to
57 send signals to the process when GDB sends us an asynchronous interrupt
58 (user hitting Control-C in the client), and to wait for the child to exit
59 when no longer debugging it. */
61 unsigned long signal_pid
;
64 /* A file descriptor for the controlling terminal. */
67 /* TERMINAL_FD's original foreground group. */
68 pid_t old_foreground_pgrp
;
70 /* Hand back terminal ownership to the original foreground group. */
73 restore_old_foreground_pgrp (void)
75 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
82 return all_threads
.head
!= NULL
;
86 start_inferior (char **argv
, char *statusptr
)
88 char **new_argv
= argv
;
91 if (wrapper_argv
!= NULL
)
95 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
97 for (i
= 0; argv
[i
] != NULL
; i
++)
99 new_argv
= alloca (sizeof (char *) * count
);
101 for (i
= 0; wrapper_argv
[i
] != NULL
; i
++)
102 new_argv
[count
++] = wrapper_argv
[i
];
103 for (i
= 0; argv
[i
] != NULL
; i
++)
104 new_argv
[count
++] = argv
[i
];
105 new_argv
[count
] = NULL
;
109 signal (SIGTTOU
, SIG_DFL
);
110 signal (SIGTTIN
, SIG_DFL
);
113 signal_pid
= create_inferior (new_argv
[0], new_argv
);
115 /* FIXME: we don't actually know at this point that the create
116 actually succeeded. We won't know that until we wait. */
117 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
122 signal (SIGTTOU
, SIG_IGN
);
123 signal (SIGTTIN
, SIG_IGN
);
124 terminal_fd
= fileno (stderr
);
125 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
126 tcsetpgrp (terminal_fd
, signal_pid
);
127 atexit (restore_old_foreground_pgrp
);
130 if (wrapper_argv
!= NULL
)
132 struct thread_resume resume_info
;
135 resume_info
.thread
= -1;
136 resume_info
.step
= 0;
138 resume_info
.leave_stopped
= 0;
140 sig
= mywait (statusptr
, 0);
141 if (*statusptr
!= 'T')
146 (*the_target
->resume
) (&resume_info
);
148 sig
= mywait (statusptr
, 0);
149 if (*statusptr
!= 'T')
152 while (sig
!= TARGET_SIGNAL_TRAP
);
157 /* Wait till we are at 1st instruction in program, return signal
158 number (assuming success). */
159 return mywait (statusptr
, 0);
163 attach_inferior (int pid
, char *statusptr
, int *sigptr
)
165 /* myattach should return -1 if attaching is unsupported,
166 0 if it succeeded, and call error() otherwise. */
168 if (myattach (pid
) != 0)
173 fprintf (stderr
, "Attached; pid = %d\n", pid
);
176 /* FIXME - It may be that we should get the SIGNAL_PID from the
177 attach function, so that it can be the main thread instead of
178 whichever we were told to attach to. */
181 *sigptr
= mywait (statusptr
, 0);
183 /* GDB knows to ignore the first SIGSTOP after attaching to a running
184 process using the "attach" command, but this is different; it's
185 just using "target remote". Pretend it's just starting up. */
186 if (*statusptr
== 'T' && *sigptr
== TARGET_SIGNAL_STOP
)
187 *sigptr
= TARGET_SIGNAL_TRAP
;
192 extern int remote_debug
;
194 /* Decode a qXfer read request. Return 0 if everything looks OK,
198 decode_xfer_read (char *buf
, char **annex
, CORE_ADDR
*ofs
, unsigned int *len
)
200 /* Extract and NUL-terminate the annex. */
202 while (*buf
&& *buf
!= ':')
208 /* After the read marker and annex, qXfer looks like a
209 traditional 'm' packet. */
210 decode_m_packet (buf
, ofs
, len
);
215 /* Write the response to a successful qXfer read. Returns the
216 length of the (binary) data stored in BUF, corresponding
217 to as much of DATA/LEN as we could fit. IS_MORE controls
218 the first character of the response. */
220 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
229 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
233 /* Handle all of the extended 'Q' packets. */
235 handle_general_set (char *own_buf
)
237 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
239 int numsigs
= (int) TARGET_SIGNAL_LAST
, i
;
240 const char *p
= own_buf
+ strlen ("QPassSignals:");
243 p
= decode_address_to_semicolon (&cursig
, p
);
244 for (i
= 0; i
< numsigs
; i
++)
250 /* Keep looping, to clear the remaining signals. */
253 p
= decode_address_to_semicolon (&cursig
, p
);
258 strcpy (own_buf
, "OK");
262 /* Otherwise we didn't know what packet it was. Say we didn't
268 get_features_xml (const char *annex
)
270 /* gdbserver_xmltarget defines what to return when looking
271 for the "target.xml" file. Its contents can either be
272 verbatim XML code (prefixed with a '@') or else the name
273 of the actual XML file to be used in place of "target.xml".
275 This variable is set up from the auto-generated
276 init_registers_... routine for the current target. */
278 if (gdbserver_xmltarget
279 && strcmp (annex
, "target.xml") == 0)
281 if (*gdbserver_xmltarget
== '@')
282 return gdbserver_xmltarget
+ 1;
284 annex
= gdbserver_xmltarget
;
289 extern const char *const xml_builtin
[][2];
292 /* Look for the annex. */
293 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
294 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
297 if (xml_builtin
[i
][0] != NULL
)
298 return xml_builtin
[i
][1];
306 monitor_show_help (void)
308 monitor_output ("The following monitor commands are supported:\n");
309 monitor_output (" set debug <0|1>\n");
310 monitor_output (" Enable general debugging messages\n");
311 monitor_output (" set remote-debug <0|1>\n");
312 monitor_output (" Enable remote protocol debugging messages\n");
313 monitor_output (" exit\n");
314 monitor_output (" Quit GDBserver\n");
317 #define require_running(BUF) \
318 if (!target_running ()) \
324 /* Handle all of the extended 'q' packets. */
326 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
328 static struct inferior_list_entry
*thread_ptr
;
330 /* Reply the current thread id. */
331 if (strcmp ("qC", own_buf
) == 0)
333 require_running (own_buf
);
334 thread_ptr
= all_threads
.head
;
335 sprintf (own_buf
, "QC%x",
336 thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
340 if (strcmp ("qSymbol::", own_buf
) == 0)
342 if (target_running () && the_target
->look_up_symbols
!= NULL
)
343 (*the_target
->look_up_symbols
) ();
345 strcpy (own_buf
, "OK");
349 if (strcmp ("qfThreadInfo", own_buf
) == 0)
351 require_running (own_buf
);
352 thread_ptr
= all_threads
.head
;
353 sprintf (own_buf
, "m%x", thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
354 thread_ptr
= thread_ptr
->next
;
358 if (strcmp ("qsThreadInfo", own_buf
) == 0)
360 require_running (own_buf
);
361 if (thread_ptr
!= NULL
)
363 sprintf (own_buf
, "m%x", thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
364 thread_ptr
= thread_ptr
->next
;
369 sprintf (own_buf
, "l");
374 if (the_target
->read_offsets
!= NULL
375 && strcmp ("qOffsets", own_buf
) == 0)
377 CORE_ADDR text
, data
;
379 require_running (own_buf
);
380 if (the_target
->read_offsets (&text
, &data
))
381 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
382 (long)text
, (long)data
, (long)data
);
389 if (the_target
->qxfer_spu
!= NULL
390 && strncmp ("qXfer:spu:read:", own_buf
, 15) == 0)
396 unsigned char *spu_buf
;
398 require_running (own_buf
);
399 strcpy (own_buf
, "E00");
400 if (decode_xfer_read (own_buf
+ 15, &annex
, &ofs
, &len
) < 0)
402 if (len
> PBUFSIZ
- 2)
404 spu_buf
= malloc (len
+ 1);
408 n
= (*the_target
->qxfer_spu
) (annex
, spu_buf
, NULL
, ofs
, len
+ 1);
412 *new_packet_len_p
= write_qxfer_response
413 (own_buf
, spu_buf
, len
, 1);
415 *new_packet_len_p
= write_qxfer_response
416 (own_buf
, spu_buf
, n
, 0);
422 if (the_target
->qxfer_spu
!= NULL
423 && strncmp ("qXfer:spu:write:", own_buf
, 16) == 0)
429 unsigned char *spu_buf
;
431 require_running (own_buf
);
432 strcpy (own_buf
, "E00");
433 spu_buf
= malloc (packet_len
- 15);
436 if (decode_xfer_write (own_buf
+ 16, packet_len
- 16, &annex
,
437 &ofs
, &len
, spu_buf
) < 0)
443 n
= (*the_target
->qxfer_spu
)
444 (annex
, NULL
, (unsigned const char *)spu_buf
, ofs
, len
);
448 sprintf (own_buf
, "%x", n
);
454 if (the_target
->read_auxv
!= NULL
455 && strncmp ("qXfer:auxv:read:", own_buf
, 16) == 0)
463 require_running (own_buf
);
465 /* Reject any annex; grab the offset and length. */
466 if (decode_xfer_read (own_buf
+ 16, &annex
, &ofs
, &len
) < 0
469 strcpy (own_buf
, "E00");
473 /* Read one extra byte, as an indicator of whether there is
475 if (len
> PBUFSIZ
- 2)
477 data
= malloc (len
+ 1);
478 n
= (*the_target
->read_auxv
) (ofs
, data
, len
+ 1);
482 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
484 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
491 if (strncmp ("qXfer:features:read:", own_buf
, 20) == 0)
494 unsigned int len
, total_len
;
495 const char *document
;
498 require_running (own_buf
);
500 /* Grab the annex, offset, and length. */
501 if (decode_xfer_read (own_buf
+ 20, &annex
, &ofs
, &len
) < 0)
503 strcpy (own_buf
, "E00");
507 /* Now grab the correct annex. */
508 document
= get_features_xml (annex
);
509 if (document
== NULL
)
511 strcpy (own_buf
, "E00");
515 total_len
= strlen (document
);
516 if (len
> PBUFSIZ
- 2)
521 else if (len
< total_len
- ofs
)
522 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
525 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
531 if (strncmp ("qXfer:libraries:read:", own_buf
, 21) == 0)
534 unsigned int len
, total_len
;
536 struct inferior_list_entry
*dll_ptr
;
539 require_running (own_buf
);
541 /* Reject any annex; grab the offset and length. */
542 if (decode_xfer_read (own_buf
+ 21, &annex
, &ofs
, &len
) < 0
545 strcpy (own_buf
, "E00");
549 /* Over-estimate the necessary memory. Assume that every character
550 in the library name must be escaped. */
552 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
553 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
555 document
= malloc (total_len
);
556 strcpy (document
, "<library-list>\n");
557 p
= document
+ strlen (document
);
559 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
561 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
564 strcpy (p
, " <library name=\"");
566 name
= xml_escape_text (dll
->name
);
570 strcpy (p
, "\"><segment address=\"");
572 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
574 strcpy (p
, "\"/></library>\n");
578 strcpy (p
, "</library-list>\n");
580 total_len
= strlen (document
);
581 if (len
> PBUFSIZ
- 2)
586 else if (len
< total_len
- ofs
)
587 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
590 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
597 /* Protocol features query. */
598 if (strncmp ("qSupported", own_buf
, 10) == 0
599 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
601 sprintf (own_buf
, "PacketSize=%x;QPassSignals+", PBUFSIZ
- 1);
603 /* We do not have any hook to indicate whether the target backend
604 supports qXfer:libraries:read, so always report it. */
605 strcat (own_buf
, ";qXfer:libraries:read+");
607 if (the_target
->read_auxv
!= NULL
)
608 strcat (own_buf
, ";qXfer:auxv:read+");
610 if (the_target
->qxfer_spu
!= NULL
)
611 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
613 /* We always report qXfer:features:read, as targets may
614 install XML files on a subsequent call to arch_setup.
615 If we reported to GDB on startup that we don't support
616 qXfer:feature:read at all, we will never be re-queried. */
617 strcat (own_buf
, ";qXfer:features:read+");
622 /* Thread-local storage support. */
623 if (the_target
->get_tls_address
!= NULL
624 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
626 char *p
= own_buf
+ 12;
627 CORE_ADDR parts
[3], address
= 0;
630 require_running (own_buf
);
632 for (i
= 0; i
< 3; i
++)
640 p2
= strchr (p
, ',');
652 decode_address (&parts
[i
], p
, len
);
656 if (p
!= NULL
|| i
< 3)
660 struct thread_info
*thread
= gdb_id_to_thread (parts
[0]);
665 err
= the_target
->get_tls_address (thread
, parts
[1], parts
[2],
671 sprintf (own_buf
, "%llx", address
);
680 /* Otherwise, pretend we do not understand this packet. */
683 /* Handle "monitor" commands. */
684 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
686 char *mon
= malloc (PBUFSIZ
);
687 int len
= strlen (own_buf
+ 6);
689 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
699 if (strcmp (mon
, "set debug 1") == 0)
702 monitor_output ("Debug output enabled.\n");
704 else if (strcmp (mon
, "set debug 0") == 0)
707 monitor_output ("Debug output disabled.\n");
709 else if (strcmp (mon
, "set remote-debug 1") == 0)
712 monitor_output ("Protocol debug output enabled.\n");
714 else if (strcmp (mon
, "set remote-debug 0") == 0)
717 monitor_output ("Protocol debug output disabled.\n");
719 else if (strcmp (mon
, "help") == 0)
720 monitor_show_help ();
721 else if (strcmp (mon
, "exit") == 0)
725 monitor_output ("Unknown monitor command.\n\n");
726 monitor_show_help ();
734 /* Otherwise we didn't know what packet it was. Say we didn't
739 /* Parse vCont packets. */
741 handle_v_cont (char *own_buf
, char *status
, int *signal
)
745 struct thread_resume
*resume_info
, default_action
;
747 /* Count the number of semicolons in the packet. There should be one
756 /* Allocate room for one extra action, for the default remain-stopped
757 behavior; if no default action is in the list, we'll need the extra
759 resume_info
= malloc ((n
+ 1) * sizeof (resume_info
[0]));
761 default_action
.thread
= -1;
762 default_action
.leave_stopped
= 1;
763 default_action
.step
= 0;
764 default_action
.sig
= 0;
772 resume_info
[i
].leave_stopped
= 0;
774 if (p
[0] == 's' || p
[0] == 'S')
775 resume_info
[i
].step
= 1;
776 else if (p
[0] == 'c' || p
[0] == 'C')
777 resume_info
[i
].step
= 0;
781 if (p
[0] == 'S' || p
[0] == 'C')
784 sig
= strtol (p
+ 1, &q
, 16);
789 if (!target_signal_to_host_p (sig
))
791 resume_info
[i
].sig
= target_signal_to_host (sig
);
795 resume_info
[i
].sig
= 0;
801 resume_info
[i
].thread
= -1;
802 default_action
= resume_info
[i
];
804 /* Note: we don't increment i here, we'll overwrite this entry
805 the next time through. */
807 else if (p
[0] == ':')
809 unsigned int gdb_id
= strtoul (p
+ 1, &q
, 16);
810 unsigned long thread_id
;
815 if (p
[0] != ';' && p
[0] != 0)
818 thread_id
= gdb_id_to_thread_id (gdb_id
);
820 resume_info
[i
].thread
= thread_id
;
828 resume_info
[i
] = default_action
;
830 /* Still used in occasional places in the backend. */
831 if (n
== 1 && resume_info
[0].thread
!= -1)
832 cont_thread
= resume_info
[0].thread
;
835 set_desired_inferior (0);
838 (*the_target
->resume
) (resume_info
);
842 *signal
= mywait (status
, 1);
843 prepare_resume_reply (own_buf
, *status
, *signal
);
853 /* Attach to a new program. Return 1 if successful, 0 if failure. */
855 handle_v_attach (char *own_buf
, char *status
, int *signal
)
859 pid
= strtol (own_buf
+ 8, NULL
, 16);
860 if (pid
!= 0 && attach_inferior (pid
, status
, signal
) == 0)
862 prepare_resume_reply (own_buf
, *status
, *signal
);
872 /* Run a new program. Return 1 if successful, 0 if failure. */
874 handle_v_run (char *own_buf
, char *status
, int *signal
)
876 char *p
, **pp
, *next_p
, **new_argv
;
880 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
886 new_argv
= malloc ((new_argc
+ 2) * sizeof (char *));
888 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
890 next_p
= strchr (p
, ';');
892 next_p
= p
+ strlen (p
);
894 if (i
== 0 && p
== next_p
)
898 new_argv
[i
] = malloc (1 + (next_p
- p
) / 2);
899 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
900 new_argv
[i
][(next_p
- p
) / 2] = '\0';
909 if (new_argv
[0] == NULL
)
911 if (program_argv
== NULL
)
917 new_argv
[0] = strdup (program_argv
[0]);
920 /* Free the old argv. */
923 for (pp
= program_argv
; *pp
!= NULL
; pp
++)
927 program_argv
= new_argv
;
929 *signal
= start_inferior (program_argv
, status
);
932 prepare_resume_reply (own_buf
, *status
, *signal
);
942 /* Handle all of the extended 'v' packets. */
944 handle_v_requests (char *own_buf
, char *status
, int *signal
,
945 int packet_len
, int *new_packet_len
)
947 if (strncmp (own_buf
, "vCont;", 6) == 0)
949 require_running (own_buf
);
950 handle_v_cont (own_buf
, status
, signal
);
954 if (strncmp (own_buf
, "vCont?", 6) == 0)
956 strcpy (own_buf
, "vCont;c;C;s;S");
960 if (strncmp (own_buf
, "vFile:", 6) == 0
961 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
964 if (strncmp (own_buf
, "vAttach;", 8) == 0)
966 if (target_running ())
968 fprintf (stderr
, "Already debugging a process\n");
972 handle_v_attach (own_buf
, status
, signal
);
976 if (strncmp (own_buf
, "vRun;", 5) == 0)
978 if (target_running ())
980 fprintf (stderr
, "Already debugging a process\n");
984 handle_v_run (own_buf
, status
, signal
);
988 /* Otherwise we didn't know what packet it was. Say we didn't
995 myresume (char *own_buf
, int step
, int *signalp
, char *statusp
)
997 struct thread_resume resume_info
[2];
1001 set_desired_inferior (0);
1003 if (step
|| sig
|| (cont_thread
!= 0 && cont_thread
!= -1))
1005 resume_info
[0].thread
1006 = ((struct inferior_list_entry
*) current_inferior
)->id
;
1007 resume_info
[0].step
= step
;
1008 resume_info
[0].sig
= sig
;
1009 resume_info
[0].leave_stopped
= 0;
1012 resume_info
[n
].thread
= -1;
1013 resume_info
[n
].step
= 0;
1014 resume_info
[n
].sig
= 0;
1015 resume_info
[n
].leave_stopped
= (cont_thread
!= 0 && cont_thread
!= -1);
1018 (*the_target
->resume
) (resume_info
);
1019 *signalp
= mywait (statusp
, 1);
1020 prepare_resume_reply (own_buf
, *statusp
, *signalp
);
1021 disable_async_io ();
1025 gdbserver_version (void)
1027 printf ("GNU gdbserver %s\n"
1028 "Copyright (C) 2007 Free Software Foundation, Inc.\n"
1029 "gdbserver is free software, covered by the GNU General Public License.\n"
1030 "This gdbserver was configured as \"%s\"\n",
1031 version
, host_name
);
1035 gdbserver_usage (void)
1037 printf ("Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
1038 "\tgdbserver [OPTIONS] --attach COMM PID\n"
1039 "\tgdbserver [OPTIONS] --multi COMM\n"
1041 "COMM may either be a tty device (for serial debugging), or \n"
1042 "HOST:PORT to listen for a TCP connection.\n"
1045 " --debug\t\tEnable debugging output.\n"
1046 " --wrapper WRAPPER --\tRun WRAPPER to start new programs.\n");
1049 #undef require_running
1050 #define require_running(BUF) \
1051 if (!target_running ()) \
1058 main (int argc
, char *argv
[])
1060 char ch
, status
, *own_buf
;
1061 unsigned char *mem_buf
;
1068 char *arg_end
, *port
;
1069 char **next_arg
= &argv
[1];
1074 while (*next_arg
!= NULL
&& **next_arg
== '-')
1076 if (strcmp (*next_arg
, "--version") == 0)
1078 gdbserver_version ();
1081 else if (strcmp (*next_arg
, "--help") == 0)
1086 else if (strcmp (*next_arg
, "--attach") == 0)
1088 else if (strcmp (*next_arg
, "--multi") == 0)
1090 else if (strcmp (*next_arg
, "--wrapper") == 0)
1094 wrapper_argv
= next_arg
;
1095 while (*next_arg
!= NULL
&& strcmp (*next_arg
, "--") != 0)
1098 if (next_arg
== wrapper_argv
|| *next_arg
== NULL
)
1104 /* Consume the "--". */
1107 else if (strcmp (*next_arg
, "--debug") == 0)
1111 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
1119 if (setjmp (toplevel
))
1121 fprintf (stderr
, "Exiting\n");
1127 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
1136 /* --attach used to come after PORT, so allow it there for
1138 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
1145 && (*next_arg
== NULL
1146 || (*next_arg
)[0] == '\0'
1147 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
1149 || next_arg
[1] != NULL
))
1158 initialize_async_io ();
1161 own_buf
= malloc (PBUFSIZ
+ 1);
1162 mem_buf
= malloc (PBUFSIZ
);
1164 if (pid
== 0 && *next_arg
!= NULL
)
1168 n
= argc
- (next_arg
- argv
);
1169 program_argv
= malloc (sizeof (char *) * (n
+ 1));
1170 for (i
= 0; i
< n
; i
++)
1171 program_argv
[i
] = strdup (next_arg
[i
]);
1172 program_argv
[i
] = NULL
;
1174 /* Wait till we are at first instruction in program. */
1175 signal
= start_inferior (program_argv
, &status
);
1177 /* We are now (hopefully) stopped at the first instruction of
1178 the target process. This assumes that the target process was
1179 successfully created. */
1183 if (attach_inferior (pid
, &status
, &signal
) == -1)
1184 error ("Attaching not supported on this target");
1186 /* Otherwise succeeded. */
1194 /* Don't report shared library events on the initial connection,
1195 even if some libraries are preloaded. Avoids the "stopped by
1196 shared library event" notice on gdb side. */
1199 if (setjmp (toplevel
))
1201 fprintf (stderr
, "Killing inferior\n");
1206 if (status
== 'W' || status
== 'X')
1211 if (!was_running
&& !multi_mode
)
1213 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
1222 if (setjmp (toplevel
) != 0)
1224 /* An error occurred. */
1225 if (response_needed
)
1227 write_enn (own_buf
);
1232 disable_async_io ();
1233 while (!exit_requested
)
1237 int new_packet_len
= -1;
1239 response_needed
= 0;
1240 packet_len
= getpkt (own_buf
);
1241 if (packet_len
<= 0)
1243 response_needed
= 1;
1250 handle_query (own_buf
, packet_len
, &new_packet_len
);
1253 handle_general_set (own_buf
);
1256 require_running (own_buf
);
1257 fprintf (stderr
, "Detaching from inferior\n");
1258 if (detach_inferior () != 0)
1259 write_enn (own_buf
);
1264 if (extended_protocol
)
1266 /* Treat this like a normal program exit. */
1275 /* If we are attached, then we can exit. Otherwise, we
1276 need to hang around doing nothing, until the child
1286 extended_protocol
= 1;
1290 prepare_resume_reply (own_buf
, status
, signal
);
1293 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
1295 unsigned long gdb_id
, thread_id
;
1297 require_running (own_buf
);
1298 gdb_id
= strtoul (&own_buf
[2], NULL
, 16);
1299 if (gdb_id
== 0 || gdb_id
== -1)
1303 thread_id
= gdb_id_to_thread_id (gdb_id
);
1306 write_enn (own_buf
);
1311 if (own_buf
[1] == 'g')
1313 general_thread
= thread_id
;
1314 set_desired_inferior (1);
1316 else if (own_buf
[1] == 'c')
1317 cont_thread
= thread_id
;
1318 else if (own_buf
[1] == 's')
1319 step_thread
= thread_id
;
1325 /* Silently ignore it so that gdb can extend the protocol
1326 without compatibility headaches. */
1331 require_running (own_buf
);
1332 set_desired_inferior (1);
1333 registers_to_string (own_buf
);
1336 require_running (own_buf
);
1337 set_desired_inferior (1);
1338 registers_from_string (&own_buf
[1]);
1342 require_running (own_buf
);
1343 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
1344 if (read_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
1345 convert_int_to_ascii (mem_buf
, own_buf
, len
);
1347 write_enn (own_buf
);
1350 require_running (own_buf
);
1351 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
1352 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
1355 write_enn (own_buf
);
1358 require_running (own_buf
);
1359 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
1360 &mem_addr
, &len
, mem_buf
) < 0
1361 || write_inferior_memory (mem_addr
, mem_buf
, len
) != 0)
1362 write_enn (own_buf
);
1367 require_running (own_buf
);
1368 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
1369 if (target_signal_to_host_p (sig
))
1370 signal
= target_signal_to_host (sig
);
1373 myresume (own_buf
, 0, &signal
, &status
);
1376 require_running (own_buf
);
1377 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
1378 if (target_signal_to_host_p (sig
))
1379 signal
= target_signal_to_host (sig
);
1382 myresume (own_buf
, 1, &signal
, &status
);
1385 require_running (own_buf
);
1387 myresume (own_buf
, 0, &signal
, &status
);
1390 require_running (own_buf
);
1392 myresume (own_buf
, 1, &signal
, &status
);
1398 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
1399 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
1400 char type
= own_buf
[1];
1402 if (the_target
->insert_watchpoint
== NULL
1403 || (type
< '2' || type
> '4'))
1405 /* No watchpoint support or not a watchpoint command;
1406 unrecognized either way. */
1413 require_running (own_buf
);
1414 res
= (*the_target
->insert_watchpoint
) (type
, addr
, len
);
1421 write_enn (own_buf
);
1429 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
1430 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
1431 char type
= own_buf
[1];
1433 if (the_target
->remove_watchpoint
== NULL
1434 || (type
< '2' || type
> '4'))
1436 /* No watchpoint support or not a watchpoint command;
1437 unrecognized either way. */
1444 require_running (own_buf
);
1445 res
= (*the_target
->remove_watchpoint
) (type
, addr
, len
);
1452 write_enn (own_buf
);
1457 response_needed
= 0;
1458 if (!target_running ())
1459 /* The packet we received doesn't make sense - but we
1460 can't reply to it, either. */
1463 fprintf (stderr
, "Killing inferior\n");
1466 /* When using the extended protocol, we wait with no
1467 program running. The traditional protocol will exit
1469 if (extended_protocol
)
1472 signal
= TARGET_SIGNAL_KILL
;
1483 unsigned long gdb_id
, thread_id
;
1485 require_running (own_buf
);
1486 gdb_id
= strtoul (&own_buf
[1], NULL
, 16);
1487 thread_id
= gdb_id_to_thread_id (gdb_id
);
1490 write_enn (own_buf
);
1494 if (mythread_alive (thread_id
))
1497 write_enn (own_buf
);
1501 response_needed
= 0;
1503 /* Restarting the inferior is only supported in the
1504 extended protocol. */
1505 if (extended_protocol
)
1507 if (target_running ())
1509 fprintf (stderr
, "GDBserver restarting\n");
1511 /* Wait till we are at 1st instruction in prog. */
1512 if (program_argv
!= NULL
)
1513 signal
= start_inferior (program_argv
, &status
);
1517 signal
= TARGET_SIGNAL_KILL
;
1523 /* It is a request we don't understand. Respond with an
1524 empty packet so that gdb knows that we don't support this
1530 /* Extended (long) request. */
1531 handle_v_requests (own_buf
, &status
, &signal
,
1532 packet_len
, &new_packet_len
);
1536 /* It is a request we don't understand. Respond with an
1537 empty packet so that gdb knows that we don't support this
1543 if (new_packet_len
!= -1)
1544 putpkt_binary (own_buf
, new_packet_len
);
1548 response_needed
= 0;
1550 if (was_running
&& (status
== 'W' || status
== 'X'))
1556 "\nChild exited with status %d\n", signal
);
1558 fprintf (stderr
, "\nChild terminated with signal = 0x%x (%s)\n",
1559 target_signal_to_host (signal
),
1560 target_signal_to_name (signal
));
1562 if (extended_protocol
)
1566 fprintf (stderr
, "GDBserver exiting\n");
1571 if (status
!= 'W' && status
!= 'X')
1575 /* If an exit was requested (using the "monitor exit" command),
1576 terminate now. The only other way to get here is for
1577 getpkt to fail; close the connection and reopen it at the
1583 if (attached
&& target_running ())
1585 else if (target_running ())
1591 fprintf (stderr
, "Remote side has terminated connection. "
1592 "GDBserver will reopen the connection.\n");