]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/server.c
2002-02-14 Daniel Jacobowitz <drow@mvista.com>
[thirdparty/binutils-gdb.git] / gdb / gdbserver / server.c
1 /* Main code for remote server for GDB.
2 Copyright 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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. */
21
22 #include "server.h"
23
24 int cont_thread;
25 int general_thread;
26 int thread_from_wait;
27 int old_thread_from_wait;
28 int extended_protocol;
29 jmp_buf toplevel;
30 int inferior_pid;
31
32 static unsigned char
33 start_inferior (char *argv[], char *statusptr)
34 {
35 inferior_pid = create_inferior (argv[0], argv);
36 fprintf (stderr, "Process %s created; pid = %d\n", argv[0], inferior_pid);
37
38 /* Wait till we are at 1st instruction in program, return signal number. */
39 return mywait (statusptr);
40 }
41
42 static int
43 attach_inferior (int pid, char *statusptr, unsigned char *sigptr)
44 {
45 /* myattach should return -1 if attaching is unsupported,
46 0 if it succeeded, and call error() otherwise. */
47 if (myattach (pid) != 0)
48 return -1;
49
50 inferior_pid = pid;
51
52 *sigptr = mywait (statusptr);
53
54 return 0;
55 }
56
57 extern int remote_debug;
58
59 int
60 main (int argc, char *argv[])
61 {
62 char ch, status, *own_buf, mem_buf[2000];
63 int i = 0;
64 unsigned char signal;
65 unsigned int len;
66 CORE_ADDR mem_addr;
67 int bad_attach = 0;
68 int pid = 0;
69 int attached = 0;
70 char *arg_end;
71
72 if (setjmp (toplevel))
73 {
74 fprintf (stderr, "Exiting\n");
75 exit (1);
76 }
77
78 if (argc >= 3 && strcmp (argv[2], "--attach") == 0)
79 {
80 if (argc == 4
81 && argv[3] != '\0'
82 && (pid = strtoul (argv[3], &arg_end, 10)) != 0
83 && *arg_end == '\0')
84 {
85 ;
86 }
87 else
88 bad_attach = 1;
89 }
90
91 if (argc < 3 || bad_attach)
92 error ("Usage:\tgdbserver tty prog [args ...]\n"
93 "\tgdbserver tty --attach pid");
94
95 initialize_low ();
96
97 own_buf = malloc (PBUFSIZ);
98
99 if (pid == 0)
100 {
101 /* Wait till we are at first instruction in program. */
102 signal = start_inferior (&argv[2], &status);
103
104 /* We are now stopped at the first instruction of the target process */
105 }
106 else
107 {
108 switch (attach_inferior (pid, &status, &signal))
109 {
110 case -1:
111 error ("Attaching not supported on this target");
112 break;
113 default:
114 attached = 1;
115 break;
116 }
117 }
118
119 while (1)
120 {
121 remote_open (argv[1]);
122
123 restart:
124 setjmp (toplevel);
125 while (getpkt (own_buf) > 0)
126 {
127 unsigned char sig;
128 i = 0;
129 ch = own_buf[i++];
130 switch (ch)
131 {
132 case 'd':
133 remote_debug = !remote_debug;
134 break;
135 case '!':
136 if (attached == 0)
137 {
138 extended_protocol = 1;
139 prepare_resume_reply (own_buf, status, signal);
140 }
141 else
142 {
143 /* We can not use the extended protocol if we are
144 attached, because we can not restart the running
145 program. So return unrecognized. */
146 own_buf[0] = '\0';
147 }
148 break;
149 case '?':
150 prepare_resume_reply (own_buf, status, signal);
151 break;
152 case 'H':
153 switch (own_buf[1])
154 {
155 case 'g':
156 general_thread = strtol (&own_buf[2], NULL, 16);
157 write_ok (own_buf);
158 fetch_inferior_registers (0);
159 break;
160 case 'c':
161 cont_thread = strtol (&own_buf[2], NULL, 16);
162 write_ok (own_buf);
163 break;
164 default:
165 /* Silently ignore it so that gdb can extend the protocol
166 without compatibility headaches. */
167 own_buf[0] = '\0';
168 break;
169 }
170 break;
171 case 'g':
172 registers_to_string (own_buf);
173 break;
174 case 'G':
175 registers_from_string (&own_buf[1]);
176 store_inferior_registers (-1);
177 write_ok (own_buf);
178 break;
179 case 'm':
180 decode_m_packet (&own_buf[1], &mem_addr, &len);
181 read_inferior_memory (mem_addr, mem_buf, len);
182 convert_int_to_ascii (mem_buf, own_buf, len);
183 break;
184 case 'M':
185 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
186 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
187 write_ok (own_buf);
188 else
189 write_enn (own_buf);
190 break;
191 case 'C':
192 convert_ascii_to_int (own_buf + 1, &sig, 1);
193 myresume (0, sig);
194 signal = mywait (&status);
195 prepare_resume_reply (own_buf, status, signal);
196 break;
197 case 'S':
198 convert_ascii_to_int (own_buf + 1, &sig, 1);
199 myresume (1, sig);
200 signal = mywait (&status);
201 prepare_resume_reply (own_buf, status, signal);
202 break;
203 case 'c':
204 myresume (0, 0);
205 signal = mywait (&status);
206 prepare_resume_reply (own_buf, status, signal);
207 break;
208 case 's':
209 myresume (1, 0);
210 signal = mywait (&status);
211 prepare_resume_reply (own_buf, status, signal);
212 break;
213 case 'k':
214 fprintf (stderr, "Killing inferior\n");
215 kill_inferior ();
216 /* When using the extended protocol, we start up a new
217 debugging session. The traditional protocol will
218 exit instead. */
219 if (extended_protocol)
220 {
221 write_ok (own_buf);
222 fprintf (stderr, "GDBserver restarting\n");
223
224 /* Wait till we are at 1st instruction in prog. */
225 signal = start_inferior (&argv[2], &status);
226 goto restart;
227 break;
228 }
229 else
230 {
231 exit (0);
232 break;
233 }
234 case 'T':
235 if (mythread_alive (strtol (&own_buf[1], NULL, 16)))
236 write_ok (own_buf);
237 else
238 write_enn (own_buf);
239 break;
240 case 'R':
241 /* Restarting the inferior is only supported in the
242 extended protocol. */
243 if (extended_protocol)
244 {
245 kill_inferior ();
246 write_ok (own_buf);
247 fprintf (stderr, "GDBserver restarting\n");
248
249 /* Wait till we are at 1st instruction in prog. */
250 signal = start_inferior (&argv[2], &status);
251 goto restart;
252 break;
253 }
254 else
255 {
256 /* It is a request we don't understand. Respond with an
257 empty packet so that gdb knows that we don't support this
258 request. */
259 own_buf[0] = '\0';
260 break;
261 }
262 default:
263 /* It is a request we don't understand. Respond with an
264 empty packet so that gdb knows that we don't support this
265 request. */
266 own_buf[0] = '\0';
267 break;
268 }
269
270 putpkt (own_buf);
271
272 if (status == 'W')
273 fprintf (stderr,
274 "\nChild exited with status %d\n", sig);
275 if (status == 'X')
276 fprintf (stderr, "\nChild terminated with signal = 0x%x\n", sig);
277 if (status == 'W' || status == 'X')
278 {
279 if (extended_protocol)
280 {
281 fprintf (stderr, "Killing inferior\n");
282 kill_inferior ();
283 write_ok (own_buf);
284 fprintf (stderr, "GDBserver restarting\n");
285
286 /* Wait till we are at 1st instruction in prog. */
287 signal = start_inferior (&argv[2], &status);
288 goto restart;
289 break;
290 }
291 else
292 {
293 fprintf (stderr, "GDBserver exiting\n");
294 exit (0);
295 }
296 }
297 }
298
299 /* We come here when getpkt fails.
300
301 For the extended remote protocol we exit (and this is the only
302 way we gracefully exit!).
303
304 For the traditional remote protocol close the connection,
305 and re-open it at the top of the loop. */
306 if (extended_protocol)
307 {
308 remote_close ();
309 exit (0);
310 }
311 else
312 {
313 fprintf (stderr, "Remote side has terminated connection. "
314 "GDBserver will reopen the connection.\n");
315 remote_close ();
316 }
317 }
318 }