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
;
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 /* The PID of the originally created or attached inferior. Used to
55 send signals to the process when GDB sends us an asynchronous interrupt
56 (user hitting Control-C in the client), and to wait for the child to exit
57 when no longer debugging it. */
59 unsigned long signal_pid
;
62 /* A file descriptor for the controlling terminal. */
65 /* TERMINAL_FD's original foreground group. */
66 pid_t old_foreground_pgrp
;
68 /* Hand back terminal ownership to the original foreground group. */
71 restore_old_foreground_pgrp (void)
73 tcsetpgrp (terminal_fd
, old_foreground_pgrp
);
80 return all_threads
.head
!= NULL
;
84 start_inferior (char *argv
[], char *statusptr
)
89 signal (SIGTTOU
, SIG_DFL
);
90 signal (SIGTTIN
, SIG_DFL
);
93 signal_pid
= create_inferior (argv
[0], argv
);
95 /* FIXME: we don't actually know at this point that the create
96 actually succeeded. We won't know that until we wait. */
97 fprintf (stderr
, "Process %s created; pid = %ld\n", argv
[0],
102 signal (SIGTTOU
, SIG_IGN
);
103 signal (SIGTTIN
, SIG_IGN
);
104 terminal_fd
= fileno (stderr
);
105 old_foreground_pgrp
= tcgetpgrp (terminal_fd
);
106 tcsetpgrp (terminal_fd
, signal_pid
);
107 atexit (restore_old_foreground_pgrp
);
110 /* Wait till we are at 1st instruction in program, return signal
111 number (assuming success). */
112 return mywait (statusptr
, 0);
116 attach_inferior (int pid
, char *statusptr
, int *sigptr
)
118 /* myattach should return -1 if attaching is unsupported,
119 0 if it succeeded, and call error() otherwise. */
121 if (myattach (pid
) != 0)
126 fprintf (stderr
, "Attached; pid = %d\n", pid
);
129 /* FIXME - It may be that we should get the SIGNAL_PID from the
130 attach function, so that it can be the main thread instead of
131 whichever we were told to attach to. */
134 *sigptr
= mywait (statusptr
, 0);
136 /* GDB knows to ignore the first SIGSTOP after attaching to a running
137 process using the "attach" command, but this is different; it's
138 just using "target remote". Pretend it's just starting up. */
139 if (*statusptr
== 'T' && *sigptr
== TARGET_SIGNAL_STOP
)
140 *sigptr
= TARGET_SIGNAL_TRAP
;
145 extern int remote_debug
;
147 /* Decode a qXfer read request. Return 0 if everything looks OK,
151 decode_xfer_read (char *buf
, char **annex
, CORE_ADDR
*ofs
, unsigned int *len
)
153 /* Extract and NUL-terminate the annex. */
155 while (*buf
&& *buf
!= ':')
161 /* After the read marker and annex, qXfer looks like a
162 traditional 'm' packet. */
163 decode_m_packet (buf
, ofs
, len
);
168 /* Write the response to a successful qXfer read. Returns the
169 length of the (binary) data stored in BUF, corresponding
170 to as much of DATA/LEN as we could fit. IS_MORE controls
171 the first character of the response. */
173 write_qxfer_response (char *buf
, const void *data
, int len
, int is_more
)
182 return remote_escape_output (data
, len
, (unsigned char *) buf
+ 1, &out_len
,
186 /* Handle all of the extended 'Q' packets. */
188 handle_general_set (char *own_buf
)
190 if (strncmp ("QPassSignals:", own_buf
, strlen ("QPassSignals:")) == 0)
192 int numsigs
= (int) TARGET_SIGNAL_LAST
, i
;
193 const char *p
= own_buf
+ strlen ("QPassSignals:");
196 p
= decode_address_to_semicolon (&cursig
, p
);
197 for (i
= 0; i
< numsigs
; i
++)
203 /* Keep looping, to clear the remaining signals. */
206 p
= decode_address_to_semicolon (&cursig
, p
);
211 strcpy (own_buf
, "OK");
215 /* Otherwise we didn't know what packet it was. Say we didn't
221 get_features_xml (const char *annex
)
223 static int features_supported
= -1;
224 static char *document
;
227 extern const char *const xml_builtin
[][2];
230 /* Look for the annex. */
231 for (i
= 0; xml_builtin
[i
][0] != NULL
; i
++)
232 if (strcmp (annex
, xml_builtin
[i
][0]) == 0)
235 if (xml_builtin
[i
][0] != NULL
)
236 return xml_builtin
[i
][1];
239 if (strcmp (annex
, "target.xml") != 0)
242 if (features_supported
== -1)
244 const char *arch
= NULL
;
245 if (the_target
->arch_string
!= NULL
)
246 arch
= (*the_target
->arch_string
) ();
249 features_supported
= 0;
252 features_supported
= 1;
253 document
= malloc (64 + strlen (arch
));
254 snprintf (document
, 64 + strlen (arch
),
255 "<target><architecture>%s</architecture></target>",
264 monitor_show_help (void)
266 monitor_output ("The following monitor commands are supported:\n");
267 monitor_output (" set debug <0|1>\n");
268 monitor_output (" Enable general debugging messages\n");
269 monitor_output (" set remote-debug <0|1>\n");
270 monitor_output (" Enable remote protocol debugging messages\n");
273 #define require_running(BUF) \
274 if (!target_running ()) \
280 /* Handle all of the extended 'q' packets. */
282 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
284 static struct inferior_list_entry
*thread_ptr
;
286 /* Reply the current thread id. */
287 if (strcmp ("qC", own_buf
) == 0)
289 require_running (own_buf
);
290 thread_ptr
= all_threads
.head
;
291 sprintf (own_buf
, "QC%x",
292 thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
296 if (strcmp ("qSymbol::", own_buf
) == 0)
298 if (target_running () && the_target
->look_up_symbols
!= NULL
)
299 (*the_target
->look_up_symbols
) ();
301 strcpy (own_buf
, "OK");
305 if (strcmp ("qfThreadInfo", own_buf
) == 0)
307 require_running (own_buf
);
308 thread_ptr
= all_threads
.head
;
309 sprintf (own_buf
, "m%x", thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
310 thread_ptr
= thread_ptr
->next
;
314 if (strcmp ("qsThreadInfo", own_buf
) == 0)
316 require_running (own_buf
);
317 if (thread_ptr
!= NULL
)
319 sprintf (own_buf
, "m%x", thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
320 thread_ptr
= thread_ptr
->next
;
325 sprintf (own_buf
, "l");
330 if (the_target
->read_offsets
!= NULL
331 && strcmp ("qOffsets", own_buf
) == 0)
333 CORE_ADDR text
, data
;
335 require_running (own_buf
);
336 if (the_target
->read_offsets (&text
, &data
))
337 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
338 (long)text
, (long)data
, (long)data
);
345 if (the_target
->qxfer_spu
!= NULL
346 && strncmp ("qXfer:spu:read:", own_buf
, 15) == 0)
352 unsigned char *spu_buf
;
354 require_running (own_buf
);
355 strcpy (own_buf
, "E00");
356 if (decode_xfer_read (own_buf
+ 15, &annex
, &ofs
, &len
) < 0)
358 if (len
> PBUFSIZ
- 2)
360 spu_buf
= malloc (len
+ 1);
364 n
= (*the_target
->qxfer_spu
) (annex
, spu_buf
, NULL
, ofs
, len
+ 1);
368 *new_packet_len_p
= write_qxfer_response
369 (own_buf
, spu_buf
, len
, 1);
371 *new_packet_len_p
= write_qxfer_response
372 (own_buf
, spu_buf
, n
, 0);
378 if (the_target
->qxfer_spu
!= NULL
379 && strncmp ("qXfer:spu:write:", own_buf
, 16) == 0)
385 unsigned char *spu_buf
;
387 require_running (own_buf
);
388 strcpy (own_buf
, "E00");
389 spu_buf
= malloc (packet_len
- 15);
392 if (decode_xfer_write (own_buf
+ 16, packet_len
- 16, &annex
,
393 &ofs
, &len
, spu_buf
) < 0)
399 n
= (*the_target
->qxfer_spu
)
400 (annex
, NULL
, (unsigned const char *)spu_buf
, ofs
, len
);
404 sprintf (own_buf
, "%x", n
);
410 if (the_target
->read_auxv
!= NULL
411 && strncmp ("qXfer:auxv:read:", own_buf
, 16) == 0)
419 require_running (own_buf
);
421 /* Reject any annex; grab the offset and length. */
422 if (decode_xfer_read (own_buf
+ 16, &annex
, &ofs
, &len
) < 0
425 strcpy (own_buf
, "E00");
429 /* Read one extra byte, as an indicator of whether there is
431 if (len
> PBUFSIZ
- 2)
433 data
= malloc (len
+ 1);
434 n
= (*the_target
->read_auxv
) (ofs
, data
, len
+ 1);
438 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
440 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
447 if (strncmp ("qXfer:features:read:", own_buf
, 20) == 0)
450 unsigned int len
, total_len
;
451 const char *document
;
454 require_running (own_buf
);
456 /* Check for support. */
457 document
= get_features_xml ("target.xml");
458 if (document
== NULL
)
464 /* Grab the annex, offset, and length. */
465 if (decode_xfer_read (own_buf
+ 20, &annex
, &ofs
, &len
) < 0)
467 strcpy (own_buf
, "E00");
471 /* Now grab the correct annex. */
472 document
= get_features_xml (annex
);
473 if (document
== NULL
)
475 strcpy (own_buf
, "E00");
479 total_len
= strlen (document
);
480 if (len
> PBUFSIZ
- 2)
485 else if (len
< total_len
- ofs
)
486 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
489 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
495 if (strncmp ("qXfer:libraries:read:", own_buf
, 21) == 0)
498 unsigned int len
, total_len
;
500 struct inferior_list_entry
*dll_ptr
;
503 require_running (own_buf
);
505 /* Reject any annex; grab the offset and length. */
506 if (decode_xfer_read (own_buf
+ 21, &annex
, &ofs
, &len
) < 0
509 strcpy (own_buf
, "E00");
513 /* Over-estimate the necessary memory. Assume that every character
514 in the library name must be escaped. */
516 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
517 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
519 document
= malloc (total_len
);
520 strcpy (document
, "<library-list>\n");
521 p
= document
+ strlen (document
);
523 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
525 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
528 strcpy (p
, " <library name=\"");
530 name
= xml_escape_text (dll
->name
);
534 strcpy (p
, "\"><segment address=\"");
536 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
538 strcpy (p
, "\"/></library>\n");
542 strcpy (p
, "</library-list>\n");
544 total_len
= strlen (document
);
545 if (len
> PBUFSIZ
- 2)
550 else if (len
< total_len
- ofs
)
551 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
554 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
561 /* Protocol features query. */
562 if (strncmp ("qSupported", own_buf
, 10) == 0
563 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
565 sprintf (own_buf
, "PacketSize=%x;QPassSignals+", PBUFSIZ
- 1);
567 /* We do not have any hook to indicate whether the target backend
568 supports qXfer:libraries:read, so always report it. */
569 strcat (own_buf
, ";qXfer:libraries:read+");
571 if (the_target
->read_auxv
!= NULL
)
572 strcat (own_buf
, ";qXfer:auxv:read+");
574 if (the_target
->qxfer_spu
!= NULL
)
575 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
577 if (get_features_xml ("target.xml") != NULL
)
578 strcat (own_buf
, ";qXfer:features:read+");
583 /* Thread-local storage support. */
584 if (the_target
->get_tls_address
!= NULL
585 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
587 char *p
= own_buf
+ 12;
588 CORE_ADDR parts
[3], address
= 0;
591 require_running (own_buf
);
593 for (i
= 0; i
< 3; i
++)
601 p2
= strchr (p
, ',');
613 decode_address (&parts
[i
], p
, len
);
617 if (p
!= NULL
|| i
< 3)
621 struct thread_info
*thread
= gdb_id_to_thread (parts
[0]);
626 err
= the_target
->get_tls_address (thread
, parts
[1], parts
[2],
632 sprintf (own_buf
, "%llx", address
);
641 /* Otherwise, pretend we do not understand this packet. */
644 /* Handle "monitor" commands. */
645 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
647 char *mon
= malloc (PBUFSIZ
);
648 int len
= strlen (own_buf
+ 6);
650 if ((len
% 1) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
660 if (strcmp (mon
, "set debug 1") == 0)
663 monitor_output ("Debug output enabled.\n");
665 else if (strcmp (mon
, "set debug 0") == 0)
668 monitor_output ("Debug output disabled.\n");
670 else if (strcmp (mon
, "set remote-debug 1") == 0)
673 monitor_output ("Protocol debug output enabled.\n");
675 else if (strcmp (mon
, "set remote-debug 0") == 0)
678 monitor_output ("Protocol debug output disabled.\n");
680 else if (strcmp (mon
, "help") == 0)
681 monitor_show_help ();
682 else if (strcmp (mon
, "exit") == 0)
686 monitor_output ("Unknown monitor command.\n\n");
687 monitor_show_help ();
695 /* Otherwise we didn't know what packet it was. Say we didn't
700 /* Parse vCont packets. */
702 handle_v_cont (char *own_buf
, char *status
, int *signal
)
706 struct thread_resume
*resume_info
, default_action
;
708 /* Count the number of semicolons in the packet. There should be one
717 /* Allocate room for one extra action, for the default remain-stopped
718 behavior; if no default action is in the list, we'll need the extra
720 resume_info
= malloc ((n
+ 1) * sizeof (resume_info
[0]));
722 default_action
.thread
= -1;
723 default_action
.leave_stopped
= 1;
724 default_action
.step
= 0;
725 default_action
.sig
= 0;
733 resume_info
[i
].leave_stopped
= 0;
735 if (p
[0] == 's' || p
[0] == 'S')
736 resume_info
[i
].step
= 1;
737 else if (p
[0] == 'c' || p
[0] == 'C')
738 resume_info
[i
].step
= 0;
742 if (p
[0] == 'S' || p
[0] == 'C')
745 sig
= strtol (p
+ 1, &q
, 16);
750 if (!target_signal_to_host_p (sig
))
752 resume_info
[i
].sig
= target_signal_to_host (sig
);
756 resume_info
[i
].sig
= 0;
762 resume_info
[i
].thread
= -1;
763 default_action
= resume_info
[i
];
765 /* Note: we don't increment i here, we'll overwrite this entry
766 the next time through. */
768 else if (p
[0] == ':')
770 unsigned int gdb_id
= strtoul (p
+ 1, &q
, 16);
771 unsigned long thread_id
;
776 if (p
[0] != ';' && p
[0] != 0)
779 thread_id
= gdb_id_to_thread_id (gdb_id
);
781 resume_info
[i
].thread
= thread_id
;
789 resume_info
[i
] = default_action
;
791 /* Still used in occasional places in the backend. */
792 if (n
== 1 && resume_info
[0].thread
!= -1)
793 cont_thread
= resume_info
[0].thread
;
796 set_desired_inferior (0);
799 (*the_target
->resume
) (resume_info
);
803 *signal
= mywait (status
, 1);
804 prepare_resume_reply (own_buf
, *status
, *signal
);
814 /* Attach to a new program. Return 1 if successful, 0 if failure. */
816 handle_v_attach (char *own_buf
, char *status
, int *signal
)
820 pid
= strtol (own_buf
+ 8, NULL
, 16);
821 if (pid
!= 0 && attach_inferior (pid
, status
, signal
) == 0)
823 prepare_resume_reply (own_buf
, *status
, *signal
);
833 /* Run a new program. Return 1 if successful, 0 if failure. */
835 handle_v_run (char *own_buf
, char *status
, int *signal
)
837 char *p
, **pp
, *next_p
, **new_argv
;
841 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
847 new_argv
= malloc ((new_argc
+ 2) * sizeof (char *));
849 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
851 next_p
= strchr (p
, ';');
853 next_p
= p
+ strlen (p
);
855 if (i
== 0 && p
== next_p
)
859 new_argv
[i
] = malloc (1 + (next_p
- p
) / 2);
860 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
861 new_argv
[i
][(next_p
- p
) / 2] = '\0';
870 if (new_argv
[0] == NULL
)
872 if (program_argv
== NULL
)
878 new_argv
[0] = strdup (program_argv
[0]);
881 /* Free the old argv. */
884 for (pp
= program_argv
; *pp
!= NULL
; pp
++)
888 program_argv
= new_argv
;
890 *signal
= start_inferior (program_argv
, status
);
893 prepare_resume_reply (own_buf
, *status
, *signal
);
903 /* Handle all of the extended 'v' packets. */
905 handle_v_requests (char *own_buf
, char *status
, int *signal
,
906 int packet_len
, int *new_packet_len
)
908 if (strncmp (own_buf
, "vCont;", 6) == 0)
910 require_running (own_buf
);
911 handle_v_cont (own_buf
, status
, signal
);
915 if (strncmp (own_buf
, "vCont?", 6) == 0)
917 strcpy (own_buf
, "vCont;c;C;s;S");
921 if (strncmp (own_buf
, "vFile:", 6) == 0
922 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
925 if (strncmp (own_buf
, "vAttach;", 8) == 0)
927 if (target_running ())
929 fprintf (stderr
, "Killing inferior\n");
932 handle_v_attach (own_buf
, status
, signal
);
936 if (strncmp (own_buf
, "vRun;", 5) == 0)
938 if (target_running ())
940 fprintf (stderr
, "Killing inferior\n");
943 handle_v_run (own_buf
, status
, signal
);
947 /* Otherwise we didn't know what packet it was. Say we didn't
954 myresume (char *own_buf
, int step
, int *signalp
, char *statusp
)
956 struct thread_resume resume_info
[2];
960 set_desired_inferior (0);
962 if (step
|| sig
|| (cont_thread
!= 0 && cont_thread
!= -1))
964 resume_info
[0].thread
965 = ((struct inferior_list_entry
*) current_inferior
)->id
;
966 resume_info
[0].step
= step
;
967 resume_info
[0].sig
= sig
;
968 resume_info
[0].leave_stopped
= 0;
971 resume_info
[n
].thread
= -1;
972 resume_info
[n
].step
= 0;
973 resume_info
[n
].sig
= 0;
974 resume_info
[n
].leave_stopped
= (cont_thread
!= 0 && cont_thread
!= -1);
977 (*the_target
->resume
) (resume_info
);
978 *signalp
= mywait (statusp
, 1);
979 prepare_resume_reply (own_buf
, *statusp
, *signalp
);
984 gdbserver_version (void)
986 printf ("GNU gdbserver %s\n"
987 "Copyright (C) 2007 Free Software Foundation, Inc.\n"
988 "gdbserver is free software, covered by the GNU General Public License.\n"
989 "This gdbserver was configured as \"%s\"\n",
994 gdbserver_usage (void)
996 printf ("Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
997 "\tgdbserver [OPTIONS] --attach COMM PID\n"
998 "\tgdbserver [OPTIONS] --multi COMM\n"
1000 "COMM may either be a tty device (for serial debugging), or \n"
1001 "HOST:PORT to listen for a TCP connection.\n"
1004 " --debug\t\tEnable debugging output.\n");
1007 #undef require_running
1008 #define require_running(BUF) \
1009 if (!target_running ()) \
1016 main (int argc
, char *argv
[])
1018 char ch
, status
, *own_buf
;
1019 unsigned char *mem_buf
;
1026 char *arg_end
, *port
;
1027 char **next_arg
= &argv
[1];
1032 while (*next_arg
!= NULL
&& **next_arg
== '-')
1034 if (strcmp (*next_arg
, "--version") == 0)
1036 gdbserver_version ();
1039 else if (strcmp (*next_arg
, "--help") == 0)
1044 else if (strcmp (*next_arg
, "--attach") == 0)
1046 else if (strcmp (*next_arg
, "--multi") == 0)
1048 else if (strcmp (*next_arg
, "--debug") == 0)
1052 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
1060 if (setjmp (toplevel
))
1062 fprintf (stderr
, "Exiting\n");
1068 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
1077 /* --attach used to come after PORT, so allow it there for
1079 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
1086 && (*next_arg
== NULL
1087 || (*next_arg
)[0] == '\0'
1088 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
1090 || next_arg
[1] != NULL
))
1099 initialize_async_io ();
1102 own_buf
= malloc (PBUFSIZ
+ 1);
1103 mem_buf
= malloc (PBUFSIZ
);
1105 if (pid
== 0 && *next_arg
!= NULL
)
1109 n
= argc
- (next_arg
- argv
);
1110 program_argv
= malloc (sizeof (char *) * (n
+ 1));
1111 for (i
= 0; i
< n
; i
++)
1112 program_argv
[i
] = strdup (next_arg
[i
]);
1113 program_argv
[i
] = NULL
;
1115 /* Wait till we are at first instruction in program. */
1116 signal
= start_inferior (program_argv
, &status
);
1118 /* We are now (hopefully) stopped at the first instruction of
1119 the target process. This assumes that the target process was
1120 successfully created. */
1124 if (attach_inferior (pid
, &status
, &signal
) == -1)
1125 error ("Attaching not supported on this target");
1127 /* Otherwise succeeded. */
1135 /* Don't report shared library events on the initial connection,
1136 even if some libraries are preloaded. Avoids the "stopped by
1137 shared library event" notice on gdb side. */
1140 if (setjmp (toplevel
))
1142 fprintf (stderr
, "Killing inferior\n");
1147 if (status
== 'W' || status
== 'X')
1152 if (!was_running
&& !multi_mode
)
1154 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
1163 if (setjmp (toplevel
) != 0)
1165 /* An error occurred. */
1166 if (response_needed
)
1168 write_enn (own_buf
);
1173 disable_async_io ();
1174 while (!exit_requested
)
1178 int new_packet_len
= -1;
1180 response_needed
= 0;
1181 packet_len
= getpkt (own_buf
);
1182 if (packet_len
<= 0)
1184 response_needed
= 1;
1191 handle_query (own_buf
, packet_len
, &new_packet_len
);
1194 handle_general_set (own_buf
);
1197 require_running (own_buf
);
1198 fprintf (stderr
, "Detaching from inferior\n");
1199 if (detach_inferior () != 0)
1200 write_enn (own_buf
);
1205 if (extended_protocol
)
1207 /* Treat this like a normal program exit. */
1216 /* If we are attached, then we can exit. Otherwise, we
1217 need to hang around doing nothing, until the child
1227 extended_protocol
= 1;
1231 prepare_resume_reply (own_buf
, status
, signal
);
1234 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
1236 unsigned long gdb_id
, thread_id
;
1238 require_running (own_buf
);
1239 gdb_id
= strtoul (&own_buf
[2], NULL
, 16);
1240 if (gdb_id
== 0 || gdb_id
== -1)
1244 thread_id
= gdb_id_to_thread_id (gdb_id
);
1247 write_enn (own_buf
);
1252 if (own_buf
[1] == 'g')
1254 general_thread
= thread_id
;
1255 set_desired_inferior (1);
1257 else if (own_buf
[1] == 'c')
1258 cont_thread
= thread_id
;
1259 else if (own_buf
[1] == 's')
1260 step_thread
= thread_id
;
1266 /* Silently ignore it so that gdb can extend the protocol
1267 without compatibility headaches. */
1272 require_running (own_buf
);
1273 set_desired_inferior (1);
1274 registers_to_string (own_buf
);
1277 require_running (own_buf
);
1278 set_desired_inferior (1);
1279 registers_from_string (&own_buf
[1]);
1283 require_running (own_buf
);
1284 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
1285 if (read_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
1286 convert_int_to_ascii (mem_buf
, own_buf
, len
);
1288 write_enn (own_buf
);
1291 require_running (own_buf
);
1292 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
1293 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
1296 write_enn (own_buf
);
1299 require_running (own_buf
);
1300 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
1301 &mem_addr
, &len
, mem_buf
) < 0
1302 || write_inferior_memory (mem_addr
, mem_buf
, len
) != 0)
1303 write_enn (own_buf
);
1308 require_running (own_buf
);
1309 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
1310 if (target_signal_to_host_p (sig
))
1311 signal
= target_signal_to_host (sig
);
1314 myresume (own_buf
, 0, &signal
, &status
);
1317 require_running (own_buf
);
1318 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
1319 if (target_signal_to_host_p (sig
))
1320 signal
= target_signal_to_host (sig
);
1323 myresume (own_buf
, 1, &signal
, &status
);
1326 require_running (own_buf
);
1328 myresume (own_buf
, 0, &signal
, &status
);
1331 require_running (own_buf
);
1333 myresume (own_buf
, 1, &signal
, &status
);
1339 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
1340 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
1341 char type
= own_buf
[1];
1343 if (the_target
->insert_watchpoint
== NULL
1344 || (type
< '2' || type
> '4'))
1346 /* No watchpoint support or not a watchpoint command;
1347 unrecognized either way. */
1354 require_running (own_buf
);
1355 res
= (*the_target
->insert_watchpoint
) (type
, addr
, len
);
1362 write_enn (own_buf
);
1370 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
1371 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
1372 char type
= own_buf
[1];
1374 if (the_target
->remove_watchpoint
== NULL
1375 || (type
< '2' || type
> '4'))
1377 /* No watchpoint support or not a watchpoint command;
1378 unrecognized either way. */
1385 require_running (own_buf
);
1386 res
= (*the_target
->remove_watchpoint
) (type
, addr
, len
);
1393 write_enn (own_buf
);
1398 response_needed
= 0;
1399 if (!target_running ())
1400 /* The packet we received doesn't make sense - but we
1401 can't reply to it, either. */
1404 fprintf (stderr
, "Killing inferior\n");
1407 /* When using the extended protocol, we wait with no
1408 program running. The traditional protocol will exit
1410 if (extended_protocol
)
1413 signal
= TARGET_SIGNAL_KILL
;
1424 unsigned long gdb_id
, thread_id
;
1426 require_running (own_buf
);
1427 gdb_id
= strtoul (&own_buf
[1], NULL
, 16);
1428 thread_id
= gdb_id_to_thread_id (gdb_id
);
1431 write_enn (own_buf
);
1435 if (mythread_alive (thread_id
))
1438 write_enn (own_buf
);
1442 response_needed
= 0;
1444 /* Restarting the inferior is only supported in the
1445 extended protocol. */
1446 if (extended_protocol
)
1448 if (target_running ())
1450 fprintf (stderr
, "GDBserver restarting\n");
1452 /* Wait till we are at 1st instruction in prog. */
1453 if (program_argv
!= NULL
)
1454 signal
= start_inferior (program_argv
, &status
);
1458 signal
= TARGET_SIGNAL_KILL
;
1464 /* It is a request we don't understand. Respond with an
1465 empty packet so that gdb knows that we don't support this
1471 /* Extended (long) request. */
1472 handle_v_requests (own_buf
, &status
, &signal
,
1473 packet_len
, &new_packet_len
);
1477 /* It is a request we don't understand. Respond with an
1478 empty packet so that gdb knows that we don't support this
1484 if (new_packet_len
!= -1)
1485 putpkt_binary (own_buf
, new_packet_len
);
1489 response_needed
= 0;
1491 if (was_running
&& (status
== 'W' || status
== 'X'))
1497 "\nChild exited with status %d\n", signal
);
1499 fprintf (stderr
, "\nChild terminated with signal = 0x%x (%s)\n",
1500 target_signal_to_host (signal
),
1501 target_signal_to_name (signal
));
1503 if (extended_protocol
)
1507 fprintf (stderr
, "GDBserver exiting\n");
1512 if (status
!= 'W' && status
!= 'X')
1516 /* If an exit was requested (using the "monitor exit" command),
1517 terminate now. The only other way to get here is for
1518 getpkt to fail; close the connection and reopen it at the
1524 if (attached
&& target_running ())
1526 else if (target_running ())
1532 fprintf (stderr
, "Remote side has terminated connection. "
1533 "GDBserver will reopen the connection.\n");