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");
271 monitor_output (" exit\n");
272 monitor_output (" Quit GDBserver\n");
275 #define require_running(BUF) \
276 if (!target_running ()) \
282 /* Handle all of the extended 'q' packets. */
284 handle_query (char *own_buf
, int packet_len
, int *new_packet_len_p
)
286 static struct inferior_list_entry
*thread_ptr
;
288 /* Reply the current thread id. */
289 if (strcmp ("qC", own_buf
) == 0)
291 require_running (own_buf
);
292 thread_ptr
= all_threads
.head
;
293 sprintf (own_buf
, "QC%x",
294 thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
298 if (strcmp ("qSymbol::", own_buf
) == 0)
300 if (target_running () && the_target
->look_up_symbols
!= NULL
)
301 (*the_target
->look_up_symbols
) ();
303 strcpy (own_buf
, "OK");
307 if (strcmp ("qfThreadInfo", own_buf
) == 0)
309 require_running (own_buf
);
310 thread_ptr
= all_threads
.head
;
311 sprintf (own_buf
, "m%x", thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
312 thread_ptr
= thread_ptr
->next
;
316 if (strcmp ("qsThreadInfo", own_buf
) == 0)
318 require_running (own_buf
);
319 if (thread_ptr
!= NULL
)
321 sprintf (own_buf
, "m%x", thread_to_gdb_id ((struct thread_info
*)thread_ptr
));
322 thread_ptr
= thread_ptr
->next
;
327 sprintf (own_buf
, "l");
332 if (the_target
->read_offsets
!= NULL
333 && strcmp ("qOffsets", own_buf
) == 0)
335 CORE_ADDR text
, data
;
337 require_running (own_buf
);
338 if (the_target
->read_offsets (&text
, &data
))
339 sprintf (own_buf
, "Text=%lX;Data=%lX;Bss=%lX",
340 (long)text
, (long)data
, (long)data
);
347 if (the_target
->qxfer_spu
!= NULL
348 && strncmp ("qXfer:spu:read:", own_buf
, 15) == 0)
354 unsigned char *spu_buf
;
356 require_running (own_buf
);
357 strcpy (own_buf
, "E00");
358 if (decode_xfer_read (own_buf
+ 15, &annex
, &ofs
, &len
) < 0)
360 if (len
> PBUFSIZ
- 2)
362 spu_buf
= malloc (len
+ 1);
366 n
= (*the_target
->qxfer_spu
) (annex
, spu_buf
, NULL
, ofs
, len
+ 1);
370 *new_packet_len_p
= write_qxfer_response
371 (own_buf
, spu_buf
, len
, 1);
373 *new_packet_len_p
= write_qxfer_response
374 (own_buf
, spu_buf
, n
, 0);
380 if (the_target
->qxfer_spu
!= NULL
381 && strncmp ("qXfer:spu:write:", own_buf
, 16) == 0)
387 unsigned char *spu_buf
;
389 require_running (own_buf
);
390 strcpy (own_buf
, "E00");
391 spu_buf
= malloc (packet_len
- 15);
394 if (decode_xfer_write (own_buf
+ 16, packet_len
- 16, &annex
,
395 &ofs
, &len
, spu_buf
) < 0)
401 n
= (*the_target
->qxfer_spu
)
402 (annex
, NULL
, (unsigned const char *)spu_buf
, ofs
, len
);
406 sprintf (own_buf
, "%x", n
);
412 if (the_target
->read_auxv
!= NULL
413 && strncmp ("qXfer:auxv:read:", own_buf
, 16) == 0)
421 require_running (own_buf
);
423 /* Reject any annex; grab the offset and length. */
424 if (decode_xfer_read (own_buf
+ 16, &annex
, &ofs
, &len
) < 0
427 strcpy (own_buf
, "E00");
431 /* Read one extra byte, as an indicator of whether there is
433 if (len
> PBUFSIZ
- 2)
435 data
= malloc (len
+ 1);
436 n
= (*the_target
->read_auxv
) (ofs
, data
, len
+ 1);
440 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, len
, 1);
442 *new_packet_len_p
= write_qxfer_response (own_buf
, data
, n
, 0);
449 if (strncmp ("qXfer:features:read:", own_buf
, 20) == 0)
452 unsigned int len
, total_len
;
453 const char *document
;
456 require_running (own_buf
);
458 /* Check for support. */
459 document
= get_features_xml ("target.xml");
460 if (document
== NULL
)
466 /* Grab the annex, offset, and length. */
467 if (decode_xfer_read (own_buf
+ 20, &annex
, &ofs
, &len
) < 0)
469 strcpy (own_buf
, "E00");
473 /* Now grab the correct annex. */
474 document
= get_features_xml (annex
);
475 if (document
== NULL
)
477 strcpy (own_buf
, "E00");
481 total_len
= strlen (document
);
482 if (len
> PBUFSIZ
- 2)
487 else if (len
< total_len
- ofs
)
488 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
491 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
497 if (strncmp ("qXfer:libraries:read:", own_buf
, 21) == 0)
500 unsigned int len
, total_len
;
502 struct inferior_list_entry
*dll_ptr
;
505 require_running (own_buf
);
507 /* Reject any annex; grab the offset and length. */
508 if (decode_xfer_read (own_buf
+ 21, &annex
, &ofs
, &len
) < 0
511 strcpy (own_buf
, "E00");
515 /* Over-estimate the necessary memory. Assume that every character
516 in the library name must be escaped. */
518 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
519 total_len
+= 128 + 6 * strlen (((struct dll_info
*) dll_ptr
)->name
);
521 document
= malloc (total_len
);
522 strcpy (document
, "<library-list>\n");
523 p
= document
+ strlen (document
);
525 for (dll_ptr
= all_dlls
.head
; dll_ptr
!= NULL
; dll_ptr
= dll_ptr
->next
)
527 struct dll_info
*dll
= (struct dll_info
*) dll_ptr
;
530 strcpy (p
, " <library name=\"");
532 name
= xml_escape_text (dll
->name
);
536 strcpy (p
, "\"><segment address=\"");
538 sprintf (p
, "0x%lx", (long) dll
->base_addr
);
540 strcpy (p
, "\"/></library>\n");
544 strcpy (p
, "</library-list>\n");
546 total_len
= strlen (document
);
547 if (len
> PBUFSIZ
- 2)
552 else if (len
< total_len
- ofs
)
553 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
556 *new_packet_len_p
= write_qxfer_response (own_buf
, document
+ ofs
,
563 /* Protocol features query. */
564 if (strncmp ("qSupported", own_buf
, 10) == 0
565 && (own_buf
[10] == ':' || own_buf
[10] == '\0'))
567 sprintf (own_buf
, "PacketSize=%x;QPassSignals+", PBUFSIZ
- 1);
569 /* We do not have any hook to indicate whether the target backend
570 supports qXfer:libraries:read, so always report it. */
571 strcat (own_buf
, ";qXfer:libraries:read+");
573 if (the_target
->read_auxv
!= NULL
)
574 strcat (own_buf
, ";qXfer:auxv:read+");
576 if (the_target
->qxfer_spu
!= NULL
)
577 strcat (own_buf
, ";qXfer:spu:read+;qXfer:spu:write+");
579 if (get_features_xml ("target.xml") != NULL
)
580 strcat (own_buf
, ";qXfer:features:read+");
585 /* Thread-local storage support. */
586 if (the_target
->get_tls_address
!= NULL
587 && strncmp ("qGetTLSAddr:", own_buf
, 12) == 0)
589 char *p
= own_buf
+ 12;
590 CORE_ADDR parts
[3], address
= 0;
593 require_running (own_buf
);
595 for (i
= 0; i
< 3; i
++)
603 p2
= strchr (p
, ',');
615 decode_address (&parts
[i
], p
, len
);
619 if (p
!= NULL
|| i
< 3)
623 struct thread_info
*thread
= gdb_id_to_thread (parts
[0]);
628 err
= the_target
->get_tls_address (thread
, parts
[1], parts
[2],
634 sprintf (own_buf
, "%llx", address
);
643 /* Otherwise, pretend we do not understand this packet. */
646 /* Handle "monitor" commands. */
647 if (strncmp ("qRcmd,", own_buf
, 6) == 0)
649 char *mon
= malloc (PBUFSIZ
);
650 int len
= strlen (own_buf
+ 6);
652 if ((len
% 2) != 0 || unhexify (mon
, own_buf
+ 6, len
/ 2) != len
/ 2)
662 if (strcmp (mon
, "set debug 1") == 0)
665 monitor_output ("Debug output enabled.\n");
667 else if (strcmp (mon
, "set debug 0") == 0)
670 monitor_output ("Debug output disabled.\n");
672 else if (strcmp (mon
, "set remote-debug 1") == 0)
675 monitor_output ("Protocol debug output enabled.\n");
677 else if (strcmp (mon
, "set remote-debug 0") == 0)
680 monitor_output ("Protocol debug output disabled.\n");
682 else if (strcmp (mon
, "help") == 0)
683 monitor_show_help ();
684 else if (strcmp (mon
, "exit") == 0)
688 monitor_output ("Unknown monitor command.\n\n");
689 monitor_show_help ();
697 /* Otherwise we didn't know what packet it was. Say we didn't
702 /* Parse vCont packets. */
704 handle_v_cont (char *own_buf
, char *status
, int *signal
)
708 struct thread_resume
*resume_info
, default_action
;
710 /* Count the number of semicolons in the packet. There should be one
719 /* Allocate room for one extra action, for the default remain-stopped
720 behavior; if no default action is in the list, we'll need the extra
722 resume_info
= malloc ((n
+ 1) * sizeof (resume_info
[0]));
724 default_action
.thread
= -1;
725 default_action
.leave_stopped
= 1;
726 default_action
.step
= 0;
727 default_action
.sig
= 0;
735 resume_info
[i
].leave_stopped
= 0;
737 if (p
[0] == 's' || p
[0] == 'S')
738 resume_info
[i
].step
= 1;
739 else if (p
[0] == 'c' || p
[0] == 'C')
740 resume_info
[i
].step
= 0;
744 if (p
[0] == 'S' || p
[0] == 'C')
747 sig
= strtol (p
+ 1, &q
, 16);
752 if (!target_signal_to_host_p (sig
))
754 resume_info
[i
].sig
= target_signal_to_host (sig
);
758 resume_info
[i
].sig
= 0;
764 resume_info
[i
].thread
= -1;
765 default_action
= resume_info
[i
];
767 /* Note: we don't increment i here, we'll overwrite this entry
768 the next time through. */
770 else if (p
[0] == ':')
772 unsigned int gdb_id
= strtoul (p
+ 1, &q
, 16);
773 unsigned long thread_id
;
778 if (p
[0] != ';' && p
[0] != 0)
781 thread_id
= gdb_id_to_thread_id (gdb_id
);
783 resume_info
[i
].thread
= thread_id
;
791 resume_info
[i
] = default_action
;
793 /* Still used in occasional places in the backend. */
794 if (n
== 1 && resume_info
[0].thread
!= -1)
795 cont_thread
= resume_info
[0].thread
;
798 set_desired_inferior (0);
801 (*the_target
->resume
) (resume_info
);
805 *signal
= mywait (status
, 1);
806 prepare_resume_reply (own_buf
, *status
, *signal
);
816 /* Attach to a new program. Return 1 if successful, 0 if failure. */
818 handle_v_attach (char *own_buf
, char *status
, int *signal
)
822 pid
= strtol (own_buf
+ 8, NULL
, 16);
823 if (pid
!= 0 && attach_inferior (pid
, status
, signal
) == 0)
825 prepare_resume_reply (own_buf
, *status
, *signal
);
835 /* Run a new program. Return 1 if successful, 0 if failure. */
837 handle_v_run (char *own_buf
, char *status
, int *signal
)
839 char *p
, **pp
, *next_p
, **new_argv
;
843 for (p
= own_buf
+ strlen ("vRun;"); p
&& *p
; p
= strchr (p
, ';'))
849 new_argv
= malloc ((new_argc
+ 2) * sizeof (char *));
851 for (p
= own_buf
+ strlen ("vRun;"); *p
; p
= next_p
)
853 next_p
= strchr (p
, ';');
855 next_p
= p
+ strlen (p
);
857 if (i
== 0 && p
== next_p
)
861 new_argv
[i
] = malloc (1 + (next_p
- p
) / 2);
862 unhexify (new_argv
[i
], p
, (next_p
- p
) / 2);
863 new_argv
[i
][(next_p
- p
) / 2] = '\0';
872 if (new_argv
[0] == NULL
)
874 if (program_argv
== NULL
)
880 new_argv
[0] = strdup (program_argv
[0]);
883 /* Free the old argv. */
886 for (pp
= program_argv
; *pp
!= NULL
; pp
++)
890 program_argv
= new_argv
;
892 *signal
= start_inferior (program_argv
, status
);
895 prepare_resume_reply (own_buf
, *status
, *signal
);
905 /* Handle all of the extended 'v' packets. */
907 handle_v_requests (char *own_buf
, char *status
, int *signal
,
908 int packet_len
, int *new_packet_len
)
910 if (strncmp (own_buf
, "vCont;", 6) == 0)
912 require_running (own_buf
);
913 handle_v_cont (own_buf
, status
, signal
);
917 if (strncmp (own_buf
, "vCont?", 6) == 0)
919 strcpy (own_buf
, "vCont;c;C;s;S");
923 if (strncmp (own_buf
, "vFile:", 6) == 0
924 && handle_vFile (own_buf
, packet_len
, new_packet_len
))
927 if (strncmp (own_buf
, "vAttach;", 8) == 0)
929 if (target_running ())
931 fprintf (stderr
, "Killing inferior\n");
934 handle_v_attach (own_buf
, status
, signal
);
938 if (strncmp (own_buf
, "vRun;", 5) == 0)
940 if (target_running ())
942 fprintf (stderr
, "Killing inferior\n");
945 handle_v_run (own_buf
, status
, signal
);
949 /* Otherwise we didn't know what packet it was. Say we didn't
956 myresume (char *own_buf
, int step
, int *signalp
, char *statusp
)
958 struct thread_resume resume_info
[2];
962 set_desired_inferior (0);
964 if (step
|| sig
|| (cont_thread
!= 0 && cont_thread
!= -1))
966 resume_info
[0].thread
967 = ((struct inferior_list_entry
*) current_inferior
)->id
;
968 resume_info
[0].step
= step
;
969 resume_info
[0].sig
= sig
;
970 resume_info
[0].leave_stopped
= 0;
973 resume_info
[n
].thread
= -1;
974 resume_info
[n
].step
= 0;
975 resume_info
[n
].sig
= 0;
976 resume_info
[n
].leave_stopped
= (cont_thread
!= 0 && cont_thread
!= -1);
979 (*the_target
->resume
) (resume_info
);
980 *signalp
= mywait (statusp
, 1);
981 prepare_resume_reply (own_buf
, *statusp
, *signalp
);
986 gdbserver_version (void)
988 printf ("GNU gdbserver %s\n"
989 "Copyright (C) 2007 Free Software Foundation, Inc.\n"
990 "gdbserver is free software, covered by the GNU General Public License.\n"
991 "This gdbserver was configured as \"%s\"\n",
996 gdbserver_usage (void)
998 printf ("Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
999 "\tgdbserver [OPTIONS] --attach COMM PID\n"
1000 "\tgdbserver [OPTIONS] --multi COMM\n"
1002 "COMM may either be a tty device (for serial debugging), or \n"
1003 "HOST:PORT to listen for a TCP connection.\n"
1006 " --debug\t\tEnable debugging output.\n");
1009 #undef require_running
1010 #define require_running(BUF) \
1011 if (!target_running ()) \
1018 main (int argc
, char *argv
[])
1020 char ch
, status
, *own_buf
;
1021 unsigned char *mem_buf
;
1028 char *arg_end
, *port
;
1029 char **next_arg
= &argv
[1];
1034 while (*next_arg
!= NULL
&& **next_arg
== '-')
1036 if (strcmp (*next_arg
, "--version") == 0)
1038 gdbserver_version ();
1041 else if (strcmp (*next_arg
, "--help") == 0)
1046 else if (strcmp (*next_arg
, "--attach") == 0)
1048 else if (strcmp (*next_arg
, "--multi") == 0)
1050 else if (strcmp (*next_arg
, "--debug") == 0)
1054 fprintf (stderr
, "Unknown argument: %s\n", *next_arg
);
1062 if (setjmp (toplevel
))
1064 fprintf (stderr
, "Exiting\n");
1070 if (port
== NULL
|| (!attach
&& !multi_mode
&& *next_arg
== NULL
))
1079 /* --attach used to come after PORT, so allow it there for
1081 if (*next_arg
!= NULL
&& strcmp (*next_arg
, "--attach") == 0)
1088 && (*next_arg
== NULL
1089 || (*next_arg
)[0] == '\0'
1090 || (pid
= strtoul (*next_arg
, &arg_end
, 0)) == 0
1092 || next_arg
[1] != NULL
))
1101 initialize_async_io ();
1104 own_buf
= malloc (PBUFSIZ
+ 1);
1105 mem_buf
= malloc (PBUFSIZ
);
1107 if (pid
== 0 && *next_arg
!= NULL
)
1111 n
= argc
- (next_arg
- argv
);
1112 program_argv
= malloc (sizeof (char *) * (n
+ 1));
1113 for (i
= 0; i
< n
; i
++)
1114 program_argv
[i
] = strdup (next_arg
[i
]);
1115 program_argv
[i
] = NULL
;
1117 /* Wait till we are at first instruction in program. */
1118 signal
= start_inferior (program_argv
, &status
);
1120 /* We are now (hopefully) stopped at the first instruction of
1121 the target process. This assumes that the target process was
1122 successfully created. */
1126 if (attach_inferior (pid
, &status
, &signal
) == -1)
1127 error ("Attaching not supported on this target");
1129 /* Otherwise succeeded. */
1137 /* Don't report shared library events on the initial connection,
1138 even if some libraries are preloaded. Avoids the "stopped by
1139 shared library event" notice on gdb side. */
1142 if (setjmp (toplevel
))
1144 fprintf (stderr
, "Killing inferior\n");
1149 if (status
== 'W' || status
== 'X')
1154 if (!was_running
&& !multi_mode
)
1156 fprintf (stderr
, "No program to debug. GDBserver exiting.\n");
1165 if (setjmp (toplevel
) != 0)
1167 /* An error occurred. */
1168 if (response_needed
)
1170 write_enn (own_buf
);
1175 disable_async_io ();
1176 while (!exit_requested
)
1180 int new_packet_len
= -1;
1182 response_needed
= 0;
1183 packet_len
= getpkt (own_buf
);
1184 if (packet_len
<= 0)
1186 response_needed
= 1;
1193 handle_query (own_buf
, packet_len
, &new_packet_len
);
1196 handle_general_set (own_buf
);
1199 require_running (own_buf
);
1200 fprintf (stderr
, "Detaching from inferior\n");
1201 if (detach_inferior () != 0)
1202 write_enn (own_buf
);
1207 if (extended_protocol
)
1209 /* Treat this like a normal program exit. */
1218 /* If we are attached, then we can exit. Otherwise, we
1219 need to hang around doing nothing, until the child
1229 extended_protocol
= 1;
1233 prepare_resume_reply (own_buf
, status
, signal
);
1236 if (own_buf
[1] == 'c' || own_buf
[1] == 'g' || own_buf
[1] == 's')
1238 unsigned long gdb_id
, thread_id
;
1240 require_running (own_buf
);
1241 gdb_id
= strtoul (&own_buf
[2], NULL
, 16);
1242 if (gdb_id
== 0 || gdb_id
== -1)
1246 thread_id
= gdb_id_to_thread_id (gdb_id
);
1249 write_enn (own_buf
);
1254 if (own_buf
[1] == 'g')
1256 general_thread
= thread_id
;
1257 set_desired_inferior (1);
1259 else if (own_buf
[1] == 'c')
1260 cont_thread
= thread_id
;
1261 else if (own_buf
[1] == 's')
1262 step_thread
= thread_id
;
1268 /* Silently ignore it so that gdb can extend the protocol
1269 without compatibility headaches. */
1274 require_running (own_buf
);
1275 set_desired_inferior (1);
1276 registers_to_string (own_buf
);
1279 require_running (own_buf
);
1280 set_desired_inferior (1);
1281 registers_from_string (&own_buf
[1]);
1285 require_running (own_buf
);
1286 decode_m_packet (&own_buf
[1], &mem_addr
, &len
);
1287 if (read_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
1288 convert_int_to_ascii (mem_buf
, own_buf
, len
);
1290 write_enn (own_buf
);
1293 require_running (own_buf
);
1294 decode_M_packet (&own_buf
[1], &mem_addr
, &len
, mem_buf
);
1295 if (write_inferior_memory (mem_addr
, mem_buf
, len
) == 0)
1298 write_enn (own_buf
);
1301 require_running (own_buf
);
1302 if (decode_X_packet (&own_buf
[1], packet_len
- 1,
1303 &mem_addr
, &len
, mem_buf
) < 0
1304 || write_inferior_memory (mem_addr
, mem_buf
, len
) != 0)
1305 write_enn (own_buf
);
1310 require_running (own_buf
);
1311 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
1312 if (target_signal_to_host_p (sig
))
1313 signal
= target_signal_to_host (sig
);
1316 myresume (own_buf
, 0, &signal
, &status
);
1319 require_running (own_buf
);
1320 convert_ascii_to_int (own_buf
+ 1, &sig
, 1);
1321 if (target_signal_to_host_p (sig
))
1322 signal
= target_signal_to_host (sig
);
1325 myresume (own_buf
, 1, &signal
, &status
);
1328 require_running (own_buf
);
1330 myresume (own_buf
, 0, &signal
, &status
);
1333 require_running (own_buf
);
1335 myresume (own_buf
, 1, &signal
, &status
);
1341 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
1342 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
1343 char type
= own_buf
[1];
1345 if (the_target
->insert_watchpoint
== NULL
1346 || (type
< '2' || type
> '4'))
1348 /* No watchpoint support or not a watchpoint command;
1349 unrecognized either way. */
1356 require_running (own_buf
);
1357 res
= (*the_target
->insert_watchpoint
) (type
, addr
, len
);
1364 write_enn (own_buf
);
1372 CORE_ADDR addr
= strtoul (&own_buf
[3], &lenptr
, 16);
1373 int len
= strtol (lenptr
+ 1, &dataptr
, 16);
1374 char type
= own_buf
[1];
1376 if (the_target
->remove_watchpoint
== NULL
1377 || (type
< '2' || type
> '4'))
1379 /* No watchpoint support or not a watchpoint command;
1380 unrecognized either way. */
1387 require_running (own_buf
);
1388 res
= (*the_target
->remove_watchpoint
) (type
, addr
, len
);
1395 write_enn (own_buf
);
1400 response_needed
= 0;
1401 if (!target_running ())
1402 /* The packet we received doesn't make sense - but we
1403 can't reply to it, either. */
1406 fprintf (stderr
, "Killing inferior\n");
1409 /* When using the extended protocol, we wait with no
1410 program running. The traditional protocol will exit
1412 if (extended_protocol
)
1415 signal
= TARGET_SIGNAL_KILL
;
1426 unsigned long gdb_id
, thread_id
;
1428 require_running (own_buf
);
1429 gdb_id
= strtoul (&own_buf
[1], NULL
, 16);
1430 thread_id
= gdb_id_to_thread_id (gdb_id
);
1433 write_enn (own_buf
);
1437 if (mythread_alive (thread_id
))
1440 write_enn (own_buf
);
1444 response_needed
= 0;
1446 /* Restarting the inferior is only supported in the
1447 extended protocol. */
1448 if (extended_protocol
)
1450 if (target_running ())
1452 fprintf (stderr
, "GDBserver restarting\n");
1454 /* Wait till we are at 1st instruction in prog. */
1455 if (program_argv
!= NULL
)
1456 signal
= start_inferior (program_argv
, &status
);
1460 signal
= TARGET_SIGNAL_KILL
;
1466 /* It is a request we don't understand. Respond with an
1467 empty packet so that gdb knows that we don't support this
1473 /* Extended (long) request. */
1474 handle_v_requests (own_buf
, &status
, &signal
,
1475 packet_len
, &new_packet_len
);
1479 /* It is a request we don't understand. Respond with an
1480 empty packet so that gdb knows that we don't support this
1486 if (new_packet_len
!= -1)
1487 putpkt_binary (own_buf
, new_packet_len
);
1491 response_needed
= 0;
1493 if (was_running
&& (status
== 'W' || status
== 'X'))
1499 "\nChild exited with status %d\n", signal
);
1501 fprintf (stderr
, "\nChild terminated with signal = 0x%x (%s)\n",
1502 target_signal_to_host (signal
),
1503 target_signal_to_name (signal
));
1505 if (extended_protocol
)
1509 fprintf (stderr
, "GDBserver exiting\n");
1514 if (status
!= 'W' && status
!= 'X')
1518 /* If an exit was requested (using the "monitor exit" command),
1519 terminate now. The only other way to get here is for
1520 getpkt to fail; close the connection and reopen it at the
1526 if (attached
&& target_running ())
1528 else if (target_running ())
1534 fprintf (stderr
, "Remote side has terminated connection. "
1535 "GDBserver will reopen the connection.\n");