]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/server.c
2002-01-17 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[PBUFSIZ], 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 if (pid == 0)
98 {
99 /* Wait till we are at first instruction in program. */
100 signal = start_inferior (&argv[2], &status);
101
102 /* We are now stopped at the first instruction of the target process */
103 }
104 else
105 {
106 switch (attach_inferior (pid, &status, &signal))
107 {
108 case -1:
109 error ("Attaching not supported on this target");
110 break;
111 default:
112 attached = 1;
113 break;
114 }
115 }
116
117 while (1)
118 {
119 remote_open (argv[1]);
120
121 restart:
122 setjmp (toplevel);
123 while (getpkt (own_buf) > 0)
124 {
125 unsigned char sig;
126 i = 0;
127 ch = own_buf[i++];
128 switch (ch)
129 {
130 case 'd':
131 remote_debug = !remote_debug;
132 break;
133 case '!':
134 if (attached == 0)
135 {
136 extended_protocol = 1;
137 prepare_resume_reply (own_buf, status, signal);
138 }
139 else
140 {
141 /* We can not use the extended protocol if we are
142 attached, because we can not restart the running
143 program. So return unrecognized. */
144 own_buf[0] = '\0';
145 }
146 break;
147 case '?':
148 prepare_resume_reply (own_buf, status, signal);
149 break;
150 case 'H':
151 switch (own_buf[1])
152 {
153 case 'g':
154 general_thread = strtol (&own_buf[2], NULL, 16);
155 write_ok (own_buf);
156 fetch_inferior_registers (0);
157 break;
158 case 'c':
159 cont_thread = strtol (&own_buf[2], NULL, 16);
160 write_ok (own_buf);
161 break;
162 default:
163 /* Silently ignore it so that gdb can extend the protocol
164 without compatibility headaches. */
165 own_buf[0] = '\0';
166 break;
167 }
168 break;
169 case 'g':
170 convert_int_to_ascii (registers, own_buf, REGISTER_BYTES);
171 break;
172 case 'G':
173 convert_ascii_to_int (&own_buf[1], registers, REGISTER_BYTES);
174 store_inferior_registers (-1);
175 write_ok (own_buf);
176 break;
177 case 'm':
178 decode_m_packet (&own_buf[1], &mem_addr, &len);
179 read_inferior_memory (mem_addr, mem_buf, len);
180 convert_int_to_ascii (mem_buf, own_buf, len);
181 break;
182 case 'M':
183 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
184 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
185 write_ok (own_buf);
186 else
187 write_enn (own_buf);
188 break;
189 case 'C':
190 convert_ascii_to_int (own_buf + 1, &sig, 1);
191 myresume (0, sig);
192 signal = mywait (&status);
193 prepare_resume_reply (own_buf, status, signal);
194 break;
195 case 'S':
196 convert_ascii_to_int (own_buf + 1, &sig, 1);
197 myresume (1, sig);
198 signal = mywait (&status);
199 prepare_resume_reply (own_buf, status, signal);
200 break;
201 case 'c':
202 myresume (0, 0);
203 signal = mywait (&status);
204 prepare_resume_reply (own_buf, status, signal);
205 break;
206 case 's':
207 myresume (1, 0);
208 signal = mywait (&status);
209 prepare_resume_reply (own_buf, status, signal);
210 break;
211 case 'k':
212 fprintf (stderr, "Killing inferior\n");
213 kill_inferior ();
214 /* When using the extended protocol, we start up a new
215 debugging session. The traditional protocol will
216 exit instead. */
217 if (extended_protocol)
218 {
219 write_ok (own_buf);
220 fprintf (stderr, "GDBserver restarting\n");
221
222 /* Wait till we are at 1st instruction in prog. */
223 signal = start_inferior (&argv[2], &status);
224 goto restart;
225 break;
226 }
227 else
228 {
229 exit (0);
230 break;
231 }
232 case 'T':
233 if (mythread_alive (strtol (&own_buf[1], NULL, 16)))
234 write_ok (own_buf);
235 else
236 write_enn (own_buf);
237 break;
238 case 'R':
239 /* Restarting the inferior is only supported in the
240 extended protocol. */
241 if (extended_protocol)
242 {
243 kill_inferior ();
244 write_ok (own_buf);
245 fprintf (stderr, "GDBserver restarting\n");
246
247 /* Wait till we are at 1st instruction in prog. */
248 signal = start_inferior (&argv[2], &status);
249 goto restart;
250 break;
251 }
252 else
253 {
254 /* It is a request we don't understand. Respond with an
255 empty packet so that gdb knows that we don't support this
256 request. */
257 own_buf[0] = '\0';
258 break;
259 }
260 default:
261 /* It is a request we don't understand. Respond with an
262 empty packet so that gdb knows that we don't support this
263 request. */
264 own_buf[0] = '\0';
265 break;
266 }
267
268 putpkt (own_buf);
269
270 if (status == 'W')
271 fprintf (stderr,
272 "\nChild exited with status %d\n", sig);
273 if (status == 'X')
274 fprintf (stderr, "\nChild terminated with signal = 0x%x\n", sig);
275 if (status == 'W' || status == 'X')
276 {
277 if (extended_protocol)
278 {
279 fprintf (stderr, "Killing inferior\n");
280 kill_inferior ();
281 write_ok (own_buf);
282 fprintf (stderr, "GDBserver restarting\n");
283
284 /* Wait till we are at 1st instruction in prog. */
285 signal = start_inferior (&argv[2], &status);
286 goto restart;
287 break;
288 }
289 else
290 {
291 fprintf (stderr, "GDBserver exiting\n");
292 exit (0);
293 }
294 }
295 }
296
297 /* We come here when getpkt fails.
298
299 For the extended remote protocol we exit (and this is the only
300 way we gracefully exit!).
301
302 For the traditional remote protocol close the connection,
303 and re-open it at the top of the loop. */
304 if (extended_protocol)
305 {
306 remote_close ();
307 exit (0);
308 }
309 else
310 {
311 fprintf (stderr, "Remote side has terminated connection. "
312 "GDBserver will reopen the connection.\n");
313 remote_close ();
314 }
315 }
316 }