2002-08-01 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / gdbserver / server.c
CommitLineData
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
24int cont_thread;
25int general_thread;
0d62e5e8 26int step_thread;
c906108c
SS
27int thread_from_wait;
28int old_thread_from_wait;
29int extended_protocol;
0d62e5e8
DJ
30int server_waiting;
31
c906108c 32jmp_buf toplevel;
c906108c
SS
33
34static unsigned char
da85418c 35start_inferior (char *argv[], char *statusptr)
c906108c 36{
ce3a066d
DJ
37 /* FIXME Check error? Or turn to void. */
38 create_inferior (argv[0], argv);
0d62e5e8
DJ
39
40 fprintf (stderr, "Process %s created; pid = %d\n", argv[0],
41 all_threads.head->id);
c906108c
SS
42
43 /* Wait till we are at 1st instruction in program, return signal number. */
0d62e5e8 44 return mywait (statusptr, 0);
c906108c
SS
45}
46
45b7b345
DJ
47static int
48attach_inferior (int pid, char *statusptr, unsigned char *sigptr)
49{
50 /* myattach should return -1 if attaching is unsupported,
51 0 if it succeeded, and call error() otherwise. */
52 if (myattach (pid) != 0)
53 return -1;
54
0d62e5e8 55 *sigptr = mywait (statusptr, 0);
45b7b345
DJ
56
57 return 0;
58}
59
c906108c 60extern int remote_debug;
ce3a066d
DJ
61
62/* Handle all of the extended 'q' packets. */
63void
64handle_query (char *own_buf)
65{
0d62e5e8
DJ
66 static struct inferior_list_entry *thread_ptr;
67
ce3a066d
DJ
68 if (strcmp ("qSymbol::", own_buf) == 0)
69 {
2f2893d9
DJ
70 if (the_target->look_up_symbols != NULL)
71 (*the_target->look_up_symbols) ();
72
ce3a066d
DJ
73 strcpy (own_buf, "OK");
74 return;
75 }
76
0d62e5e8
DJ
77 if (strcmp ("qfThreadInfo", own_buf) == 0)
78 {
79 thread_ptr = all_threads.head;
80 sprintf (own_buf, "m%x", thread_ptr->id);
81 thread_ptr = thread_ptr->next;
82 return;
83 }
84
85 if (strcmp ("qsThreadInfo", own_buf) == 0)
86 {
87 if (thread_ptr != NULL)
88 {
89 sprintf (own_buf, "m%x", thread_ptr->id);
90 thread_ptr = thread_ptr->next;
91 return;
92 }
93 else
94 {
95 sprintf (own_buf, "l");
96 return;
97 }
98 }
99
ce3a066d
DJ
100 /* Otherwise we didn't know what packet it was. Say we didn't
101 understand it. */
102 own_buf[0] = 0;
103}
104
0729219d 105static int attached;
c906108c 106
0bc68c49
DJ
107static void
108gdbserver_usage (void)
109{
110 error ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
111 "\tgdbserver COMM --attach PID\n"
112 "\n"
113 "COMM may either be a tty device (for serial debugging), or \n"
114 "HOST:PORT to listen for a TCP connection.\n");
115}
116
c906108c 117int
da85418c 118main (int argc, char *argv[])
c906108c 119{
0a30fbc4 120 char ch, status, *own_buf, mem_buf[2000];
c906108c
SS
121 int i = 0;
122 unsigned char signal;
123 unsigned int len;
124 CORE_ADDR mem_addr;
0729219d
DJ
125 int bad_attach;
126 int pid;
45b7b345 127 char *arg_end;
c906108c 128
c5aa993b 129 if (setjmp (toplevel))
c906108c 130 {
c5aa993b
JM
131 fprintf (stderr, "Exiting\n");
132 exit (1);
c906108c
SS
133 }
134
0729219d
DJ
135 bad_attach = 0;
136 pid = 0;
137 attached = 0;
45b7b345
DJ
138 if (argc >= 3 && strcmp (argv[2], "--attach") == 0)
139 {
140 if (argc == 4
141 && argv[3] != '\0'
142 && (pid = strtoul (argv[3], &arg_end, 10)) != 0
143 && *arg_end == '\0')
144 {
145 ;
146 }
147 else
148 bad_attach = 1;
149 }
150
151 if (argc < 3 || bad_attach)
0bc68c49 152 gdbserver_usage();
c906108c 153
4ce44c66
JM
154 initialize_low ();
155
0a30fbc4
DJ
156 own_buf = malloc (PBUFSIZ);
157
45b7b345
DJ
158 if (pid == 0)
159 {
160 /* Wait till we are at first instruction in program. */
161 signal = start_inferior (&argv[2], &status);
c906108c 162
45b7b345
DJ
163 /* We are now stopped at the first instruction of the target process */
164 }
165 else
166 {
167 switch (attach_inferior (pid, &status, &signal))
168 {
169 case -1:
170 error ("Attaching not supported on this target");
171 break;
172 default:
173 attached = 1;
174 break;
175 }
176 }
c906108c
SS
177
178 while (1)
179 {
180 remote_open (argv[1]);
181
c5aa993b
JM
182 restart:
183 setjmp (toplevel);
c906108c
SS
184 while (getpkt (own_buf) > 0)
185 {
186 unsigned char sig;
187 i = 0;
188 ch = own_buf[i++];
189 switch (ch)
190 {
ce3a066d
DJ
191 case 'q':
192 handle_query (own_buf);
193 break;
c906108c
SS
194 case 'd':
195 remote_debug = !remote_debug;
196 break;
197 case '!':
45b7b345
DJ
198 if (attached == 0)
199 {
200 extended_protocol = 1;
201 prepare_resume_reply (own_buf, status, signal);
202 }
203 else
204 {
205 /* We can not use the extended protocol if we are
206 attached, because we can not restart the running
207 program. So return unrecognized. */
208 own_buf[0] = '\0';
209 }
c906108c
SS
210 break;
211 case '?':
212 prepare_resume_reply (own_buf, status, signal);
213 break;
214 case 'H':
215 switch (own_buf[1])
216 {
217 case 'g':
218 general_thread = strtol (&own_buf[2], NULL, 16);
219 write_ok (own_buf);
0d62e5e8 220 set_desired_inferior (1);
c906108c
SS
221 break;
222 case 'c':
223 cont_thread = strtol (&own_buf[2], NULL, 16);
224 write_ok (own_buf);
225 break;
0d62e5e8
DJ
226 case 's':
227 step_thread = strtol (&own_buf[2], NULL, 16);
228 write_ok (own_buf);
229 break;
c906108c
SS
230 default:
231 /* Silently ignore it so that gdb can extend the protocol
232 without compatibility headaches. */
233 own_buf[0] = '\0';
234 break;
235 }
236 break;
237 case 'g':
0d62e5e8 238 set_desired_inferior (1);
0a30fbc4 239 registers_to_string (own_buf);
c906108c
SS
240 break;
241 case 'G':
0d62e5e8 242 set_desired_inferior (1);
0a30fbc4 243 registers_from_string (&own_buf[1]);
c906108c
SS
244 write_ok (own_buf);
245 break;
246 case 'm':
247 decode_m_packet (&own_buf[1], &mem_addr, &len);
248 read_inferior_memory (mem_addr, mem_buf, len);
249 convert_int_to_ascii (mem_buf, own_buf, len);
250 break;
251 case 'M':
252 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
253 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
254 write_ok (own_buf);
255 else
256 write_enn (own_buf);
257 break;
258 case 'C':
259 convert_ascii_to_int (own_buf + 1, &sig, 1);
0e98d0a7
DJ
260 if (target_signal_to_host_p (sig))
261 signal = target_signal_to_host (sig);
262 else
263 signal = 0;
0d62e5e8 264 set_desired_inferior (0);
0e98d0a7 265 myresume (0, signal);
0d62e5e8 266 signal = mywait (&status, 1);
c906108c
SS
267 prepare_resume_reply (own_buf, status, signal);
268 break;
269 case 'S':
270 convert_ascii_to_int (own_buf + 1, &sig, 1);
0e98d0a7
DJ
271 if (target_signal_to_host_p (sig))
272 signal = target_signal_to_host (sig);
273 else
274 signal = 0;
0d62e5e8 275 set_desired_inferior (0);
0e98d0a7 276 myresume (1, signal);
0d62e5e8 277 signal = mywait (&status, 1);
c906108c
SS
278 prepare_resume_reply (own_buf, status, signal);
279 break;
280 case 'c':
0d62e5e8 281 set_desired_inferior (0);
c906108c 282 myresume (0, 0);
0d62e5e8 283 signal = mywait (&status, 1);
c906108c
SS
284 prepare_resume_reply (own_buf, status, signal);
285 break;
286 case 's':
0d62e5e8 287 set_desired_inferior (0);
c906108c 288 myresume (1, 0);
0d62e5e8 289 signal = mywait (&status, 1);
c906108c
SS
290 prepare_resume_reply (own_buf, status, signal);
291 break;
292 case 'k':
293 fprintf (stderr, "Killing inferior\n");
294 kill_inferior ();
295 /* When using the extended protocol, we start up a new
c5aa993b 296 debugging session. The traditional protocol will
c906108c
SS
297 exit instead. */
298 if (extended_protocol)
299 {
300 write_ok (own_buf);
301 fprintf (stderr, "GDBserver restarting\n");
302
303 /* Wait till we are at 1st instruction in prog. */
304 signal = start_inferior (&argv[2], &status);
305 goto restart;
306 break;
307 }
308 else
309 {
310 exit (0);
311 break;
312 }
313 case 'T':
314 if (mythread_alive (strtol (&own_buf[1], NULL, 16)))
315 write_ok (own_buf);
316 else
317 write_enn (own_buf);
318 break;
319 case 'R':
320 /* Restarting the inferior is only supported in the
c5aa993b 321 extended protocol. */
c906108c
SS
322 if (extended_protocol)
323 {
324 kill_inferior ();
325 write_ok (own_buf);
326 fprintf (stderr, "GDBserver restarting\n");
327
328 /* Wait till we are at 1st instruction in prog. */
329 signal = start_inferior (&argv[2], &status);
330 goto restart;
331 break;
332 }
333 else
334 {
335 /* It is a request we don't understand. Respond with an
336 empty packet so that gdb knows that we don't support this
337 request. */
338 own_buf[0] = '\0';
339 break;
340 }
341 default:
342 /* It is a request we don't understand. Respond with an
c5aa993b
JM
343 empty packet so that gdb knows that we don't support this
344 request. */
c906108c
SS
345 own_buf[0] = '\0';
346 break;
347 }
348
349 putpkt (own_buf);
350
351 if (status == 'W')
352 fprintf (stderr,
353 "\nChild exited with status %d\n", sig);
354 if (status == 'X')
355 fprintf (stderr, "\nChild terminated with signal = 0x%x\n", sig);
356 if (status == 'W' || status == 'X')
357 {
358 if (extended_protocol)
359 {
360 fprintf (stderr, "Killing inferior\n");
361 kill_inferior ();
362 write_ok (own_buf);
363 fprintf (stderr, "GDBserver restarting\n");
364
365 /* Wait till we are at 1st instruction in prog. */
366 signal = start_inferior (&argv[2], &status);
367 goto restart;
368 break;
369 }
370 else
371 {
372 fprintf (stderr, "GDBserver exiting\n");
373 exit (0);
374 }
375 }
376 }
377
378 /* We come here when getpkt fails.
379
c5aa993b
JM
380 For the extended remote protocol we exit (and this is the only
381 way we gracefully exit!).
c906108c 382
c5aa993b
JM
383 For the traditional remote protocol close the connection,
384 and re-open it at the top of the loop. */
c906108c
SS
385 if (extended_protocol)
386 {
387 remote_close ();
388 exit (0);
389 }
390 else
391 {
45b7b345
DJ
392 fprintf (stderr, "Remote side has terminated connection. "
393 "GDBserver will reopen the connection.\n");
c906108c
SS
394 remote_close ();
395 }
396 }
397}
This page took 0.206871 seconds and 4 git commands to generate.