Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Main code for remote server for GDB. |
db728ff7 | 2 | Copyright 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002 |
b6ba6518 | 3 | Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
c906108c | 11 | |
c5aa993b JM |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
c906108c | 16 | |
c5aa993b JM |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
21 | |
22 | #include "server.h" | |
23 | ||
a9fa9f7d DJ |
24 | #include <unistd.h> |
25 | #include <signal.h> | |
26 | #include <sys/wait.h> | |
27 | ||
c906108c SS |
28 | int cont_thread; |
29 | int general_thread; | |
0d62e5e8 | 30 | int step_thread; |
c906108c SS |
31 | int thread_from_wait; |
32 | int old_thread_from_wait; | |
33 | int extended_protocol; | |
0d62e5e8 DJ |
34 | int server_waiting; |
35 | ||
c906108c | 36 | jmp_buf toplevel; |
c906108c | 37 | |
a9fa9f7d DJ |
38 | /* The PID of the originally created or attached inferior. Used to |
39 | send signals to the process when GDB sends us an asynchronous interrupt | |
40 | (user hitting Control-C in the client), and to wait for the child to exit | |
41 | when no longer debugging it. */ | |
42 | ||
43 | int signal_pid; | |
44 | ||
c906108c | 45 | static unsigned char |
da85418c | 46 | start_inferior (char *argv[], char *statusptr) |
c906108c | 47 | { |
a9fa9f7d DJ |
48 | signal (SIGTTOU, SIG_DFL); |
49 | signal (SIGTTIN, SIG_DFL); | |
50 | ||
51 | signal_pid = create_inferior (argv[0], argv); | |
0d62e5e8 DJ |
52 | |
53 | fprintf (stderr, "Process %s created; pid = %d\n", argv[0], | |
a9fa9f7d DJ |
54 | signal_pid); |
55 | ||
56 | signal (SIGTTOU, SIG_IGN); | |
57 | signal (SIGTTIN, SIG_IGN); | |
58 | tcsetpgrp (fileno (stderr), signal_pid); | |
c906108c SS |
59 | |
60 | /* Wait till we are at 1st instruction in program, return signal number. */ | |
0d62e5e8 | 61 | return mywait (statusptr, 0); |
c906108c SS |
62 | } |
63 | ||
45b7b345 DJ |
64 | static int |
65 | attach_inferior (int pid, char *statusptr, unsigned char *sigptr) | |
66 | { | |
67 | /* myattach should return -1 if attaching is unsupported, | |
68 | 0 if it succeeded, and call error() otherwise. */ | |
a9fa9f7d | 69 | |
45b7b345 DJ |
70 | if (myattach (pid) != 0) |
71 | return -1; | |
72 | ||
a9fa9f7d DJ |
73 | /* FIXME - It may be that we should get the SIGNAL_PID from the |
74 | attach function, so that it can be the main thread instead of | |
75 | whichever we were told to attach to. */ | |
76 | signal_pid = pid; | |
77 | ||
0d62e5e8 | 78 | *sigptr = mywait (statusptr, 0); |
45b7b345 DJ |
79 | |
80 | return 0; | |
81 | } | |
82 | ||
c906108c | 83 | extern int remote_debug; |
ce3a066d DJ |
84 | |
85 | /* Handle all of the extended 'q' packets. */ | |
86 | void | |
87 | handle_query (char *own_buf) | |
88 | { | |
0d62e5e8 DJ |
89 | static struct inferior_list_entry *thread_ptr; |
90 | ||
ce3a066d DJ |
91 | if (strcmp ("qSymbol::", own_buf) == 0) |
92 | { | |
2f2893d9 DJ |
93 | if (the_target->look_up_symbols != NULL) |
94 | (*the_target->look_up_symbols) (); | |
95 | ||
ce3a066d DJ |
96 | strcpy (own_buf, "OK"); |
97 | return; | |
98 | } | |
99 | ||
0d62e5e8 DJ |
100 | if (strcmp ("qfThreadInfo", own_buf) == 0) |
101 | { | |
102 | thread_ptr = all_threads.head; | |
103 | sprintf (own_buf, "m%x", thread_ptr->id); | |
104 | thread_ptr = thread_ptr->next; | |
105 | return; | |
106 | } | |
107 | ||
108 | if (strcmp ("qsThreadInfo", own_buf) == 0) | |
109 | { | |
110 | if (thread_ptr != NULL) | |
111 | { | |
112 | sprintf (own_buf, "m%x", thread_ptr->id); | |
113 | thread_ptr = thread_ptr->next; | |
114 | return; | |
115 | } | |
116 | else | |
117 | { | |
118 | sprintf (own_buf, "l"); | |
119 | return; | |
120 | } | |
121 | } | |
122 | ||
ce3a066d DJ |
123 | /* Otherwise we didn't know what packet it was. Say we didn't |
124 | understand it. */ | |
125 | own_buf[0] = 0; | |
126 | } | |
127 | ||
0729219d | 128 | static int attached; |
c906108c | 129 | |
0bc68c49 DJ |
130 | static void |
131 | gdbserver_usage (void) | |
132 | { | |
133 | error ("Usage:\tgdbserver COMM PROG [ARGS ...]\n" | |
134 | "\tgdbserver COMM --attach PID\n" | |
135 | "\n" | |
136 | "COMM may either be a tty device (for serial debugging), or \n" | |
137 | "HOST:PORT to listen for a TCP connection.\n"); | |
138 | } | |
139 | ||
c906108c | 140 | int |
da85418c | 141 | main (int argc, char *argv[]) |
c906108c | 142 | { |
0a30fbc4 | 143 | char ch, status, *own_buf, mem_buf[2000]; |
c906108c SS |
144 | int i = 0; |
145 | unsigned char signal; | |
146 | unsigned int len; | |
147 | CORE_ADDR mem_addr; | |
0729219d DJ |
148 | int bad_attach; |
149 | int pid; | |
45b7b345 | 150 | char *arg_end; |
c906108c | 151 | |
c5aa993b | 152 | if (setjmp (toplevel)) |
c906108c | 153 | { |
c5aa993b JM |
154 | fprintf (stderr, "Exiting\n"); |
155 | exit (1); | |
c906108c SS |
156 | } |
157 | ||
0729219d DJ |
158 | bad_attach = 0; |
159 | pid = 0; | |
160 | attached = 0; | |
45b7b345 DJ |
161 | if (argc >= 3 && strcmp (argv[2], "--attach") == 0) |
162 | { | |
163 | if (argc == 4 | |
164 | && argv[3] != '\0' | |
165 | && (pid = strtoul (argv[3], &arg_end, 10)) != 0 | |
166 | && *arg_end == '\0') | |
167 | { | |
168 | ; | |
169 | } | |
170 | else | |
171 | bad_attach = 1; | |
172 | } | |
173 | ||
174 | if (argc < 3 || bad_attach) | |
0bc68c49 | 175 | gdbserver_usage(); |
c906108c | 176 | |
4ce44c66 JM |
177 | initialize_low (); |
178 | ||
0a30fbc4 DJ |
179 | own_buf = malloc (PBUFSIZ); |
180 | ||
45b7b345 DJ |
181 | if (pid == 0) |
182 | { | |
183 | /* Wait till we are at first instruction in program. */ | |
184 | signal = start_inferior (&argv[2], &status); | |
c906108c | 185 | |
45b7b345 DJ |
186 | /* We are now stopped at the first instruction of the target process */ |
187 | } | |
188 | else | |
189 | { | |
190 | switch (attach_inferior (pid, &status, &signal)) | |
191 | { | |
192 | case -1: | |
193 | error ("Attaching not supported on this target"); | |
194 | break; | |
195 | default: | |
196 | attached = 1; | |
197 | break; | |
198 | } | |
199 | } | |
c906108c SS |
200 | |
201 | while (1) | |
202 | { | |
203 | remote_open (argv[1]); | |
204 | ||
c5aa993b JM |
205 | restart: |
206 | setjmp (toplevel); | |
c906108c SS |
207 | while (getpkt (own_buf) > 0) |
208 | { | |
209 | unsigned char sig; | |
210 | i = 0; | |
211 | ch = own_buf[i++]; | |
212 | switch (ch) | |
213 | { | |
ce3a066d DJ |
214 | case 'q': |
215 | handle_query (own_buf); | |
216 | break; | |
c906108c SS |
217 | case 'd': |
218 | remote_debug = !remote_debug; | |
219 | break; | |
220 | case '!': | |
45b7b345 DJ |
221 | if (attached == 0) |
222 | { | |
223 | extended_protocol = 1; | |
224 | prepare_resume_reply (own_buf, status, signal); | |
225 | } | |
226 | else | |
227 | { | |
228 | /* We can not use the extended protocol if we are | |
229 | attached, because we can not restart the running | |
230 | program. So return unrecognized. */ | |
231 | own_buf[0] = '\0'; | |
232 | } | |
c906108c SS |
233 | break; |
234 | case '?': | |
235 | prepare_resume_reply (own_buf, status, signal); | |
236 | break; | |
237 | case 'H': | |
238 | switch (own_buf[1]) | |
239 | { | |
240 | case 'g': | |
241 | general_thread = strtol (&own_buf[2], NULL, 16); | |
242 | write_ok (own_buf); | |
0d62e5e8 | 243 | set_desired_inferior (1); |
c906108c SS |
244 | break; |
245 | case 'c': | |
246 | cont_thread = strtol (&own_buf[2], NULL, 16); | |
247 | write_ok (own_buf); | |
248 | break; | |
0d62e5e8 DJ |
249 | case 's': |
250 | step_thread = strtol (&own_buf[2], NULL, 16); | |
251 | write_ok (own_buf); | |
252 | break; | |
c906108c SS |
253 | default: |
254 | /* Silently ignore it so that gdb can extend the protocol | |
255 | without compatibility headaches. */ | |
256 | own_buf[0] = '\0'; | |
257 | break; | |
258 | } | |
259 | break; | |
260 | case 'g': | |
0d62e5e8 | 261 | set_desired_inferior (1); |
0a30fbc4 | 262 | registers_to_string (own_buf); |
c906108c SS |
263 | break; |
264 | case 'G': | |
0d62e5e8 | 265 | set_desired_inferior (1); |
0a30fbc4 | 266 | registers_from_string (&own_buf[1]); |
c906108c SS |
267 | write_ok (own_buf); |
268 | break; | |
269 | case 'm': | |
270 | decode_m_packet (&own_buf[1], &mem_addr, &len); | |
271 | read_inferior_memory (mem_addr, mem_buf, len); | |
272 | convert_int_to_ascii (mem_buf, own_buf, len); | |
273 | break; | |
274 | case 'M': | |
275 | decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf); | |
276 | if (write_inferior_memory (mem_addr, mem_buf, len) == 0) | |
277 | write_ok (own_buf); | |
278 | else | |
279 | write_enn (own_buf); | |
280 | break; | |
281 | case 'C': | |
282 | convert_ascii_to_int (own_buf + 1, &sig, 1); | |
0e98d0a7 DJ |
283 | if (target_signal_to_host_p (sig)) |
284 | signal = target_signal_to_host (sig); | |
285 | else | |
286 | signal = 0; | |
0d62e5e8 | 287 | set_desired_inferior (0); |
0e98d0a7 | 288 | myresume (0, signal); |
0d62e5e8 | 289 | signal = mywait (&status, 1); |
c906108c SS |
290 | prepare_resume_reply (own_buf, status, signal); |
291 | break; | |
292 | case 'S': | |
293 | convert_ascii_to_int (own_buf + 1, &sig, 1); | |
0e98d0a7 DJ |
294 | if (target_signal_to_host_p (sig)) |
295 | signal = target_signal_to_host (sig); | |
296 | else | |
297 | signal = 0; | |
0d62e5e8 | 298 | set_desired_inferior (0); |
0e98d0a7 | 299 | myresume (1, signal); |
0d62e5e8 | 300 | signal = mywait (&status, 1); |
c906108c SS |
301 | prepare_resume_reply (own_buf, status, signal); |
302 | break; | |
303 | case 'c': | |
0d62e5e8 | 304 | set_desired_inferior (0); |
c906108c | 305 | myresume (0, 0); |
0d62e5e8 | 306 | signal = mywait (&status, 1); |
c906108c SS |
307 | prepare_resume_reply (own_buf, status, signal); |
308 | break; | |
309 | case 's': | |
0d62e5e8 | 310 | set_desired_inferior (0); |
c906108c | 311 | myresume (1, 0); |
0d62e5e8 | 312 | signal = mywait (&status, 1); |
c906108c SS |
313 | prepare_resume_reply (own_buf, status, signal); |
314 | break; | |
315 | case 'k': | |
316 | fprintf (stderr, "Killing inferior\n"); | |
317 | kill_inferior (); | |
318 | /* When using the extended protocol, we start up a new | |
c5aa993b | 319 | debugging session. The traditional protocol will |
c906108c SS |
320 | exit instead. */ |
321 | if (extended_protocol) | |
322 | { | |
323 | write_ok (own_buf); | |
324 | fprintf (stderr, "GDBserver restarting\n"); | |
325 | ||
326 | /* Wait till we are at 1st instruction in prog. */ | |
327 | signal = start_inferior (&argv[2], &status); | |
328 | goto restart; | |
329 | break; | |
330 | } | |
331 | else | |
332 | { | |
333 | exit (0); | |
334 | break; | |
335 | } | |
336 | case 'T': | |
337 | if (mythread_alive (strtol (&own_buf[1], NULL, 16))) | |
338 | write_ok (own_buf); | |
339 | else | |
340 | write_enn (own_buf); | |
341 | break; | |
342 | case 'R': | |
343 | /* Restarting the inferior is only supported in the | |
c5aa993b | 344 | extended protocol. */ |
c906108c SS |
345 | if (extended_protocol) |
346 | { | |
347 | kill_inferior (); | |
348 | write_ok (own_buf); | |
349 | fprintf (stderr, "GDBserver restarting\n"); | |
350 | ||
351 | /* Wait till we are at 1st instruction in prog. */ | |
352 | signal = start_inferior (&argv[2], &status); | |
353 | goto restart; | |
354 | break; | |
355 | } | |
356 | else | |
357 | { | |
358 | /* It is a request we don't understand. Respond with an | |
359 | empty packet so that gdb knows that we don't support this | |
360 | request. */ | |
361 | own_buf[0] = '\0'; | |
362 | break; | |
363 | } | |
364 | default: | |
365 | /* It is a request we don't understand. Respond with an | |
c5aa993b JM |
366 | empty packet so that gdb knows that we don't support this |
367 | request. */ | |
c906108c SS |
368 | own_buf[0] = '\0'; |
369 | break; | |
370 | } | |
371 | ||
372 | putpkt (own_buf); | |
373 | ||
374 | if (status == 'W') | |
375 | fprintf (stderr, | |
376 | "\nChild exited with status %d\n", sig); | |
377 | if (status == 'X') | |
378 | fprintf (stderr, "\nChild terminated with signal = 0x%x\n", sig); | |
379 | if (status == 'W' || status == 'X') | |
380 | { | |
381 | if (extended_protocol) | |
382 | { | |
383 | fprintf (stderr, "Killing inferior\n"); | |
384 | kill_inferior (); | |
385 | write_ok (own_buf); | |
386 | fprintf (stderr, "GDBserver restarting\n"); | |
387 | ||
388 | /* Wait till we are at 1st instruction in prog. */ | |
389 | signal = start_inferior (&argv[2], &status); | |
390 | goto restart; | |
391 | break; | |
392 | } | |
393 | else | |
394 | { | |
395 | fprintf (stderr, "GDBserver exiting\n"); | |
396 | exit (0); | |
397 | } | |
398 | } | |
399 | } | |
400 | ||
401 | /* We come here when getpkt fails. | |
402 | ||
c5aa993b JM |
403 | For the extended remote protocol we exit (and this is the only |
404 | way we gracefully exit!). | |
c906108c | 405 | |
c5aa993b JM |
406 | For the traditional remote protocol close the connection, |
407 | and re-open it at the top of the loop. */ | |
c906108c SS |
408 | if (extended_protocol) |
409 | { | |
410 | remote_close (); | |
411 | exit (0); | |
412 | } | |
413 | else | |
414 | { | |
45b7b345 DJ |
415 | fprintf (stderr, "Remote side has terminated connection. " |
416 | "GDBserver will reopen the connection.\n"); | |
c906108c SS |
417 | remote_close (); |
418 | } | |
419 | } | |
420 | } |