2002-01-17 Daniel Jacobowitz <drow@mvista.com>
[deliverable/binutils-gdb.git] / gdb / gdbserver / server.c
CommitLineData
c906108c 1/* Main code for remote server for GDB.
b6ba6518
KB
2 Copyright 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000
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;
26int thread_from_wait;
27int old_thread_from_wait;
28int extended_protocol;
29jmp_buf toplevel;
30int inferior_pid;
31
32static unsigned char
da85418c 33start_inferior (char *argv[], char *statusptr)
c906108c
SS
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
45b7b345
DJ
42static int
43attach_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
c906108c
SS
57extern int remote_debug;
58
59int
da85418c 60main (int argc, char *argv[])
c906108c 61{
5c44784c 62 char ch, status, own_buf[PBUFSIZ], mem_buf[2000];
c906108c
SS
63 int i = 0;
64 unsigned char signal;
65 unsigned int len;
66 CORE_ADDR mem_addr;
45b7b345
DJ
67 int bad_attach = 0;
68 int pid = 0;
69 int attached = 0;
70 char *arg_end;
c906108c 71
c5aa993b 72 if (setjmp (toplevel))
c906108c 73 {
c5aa993b
JM
74 fprintf (stderr, "Exiting\n");
75 exit (1);
c906108c
SS
76 }
77
45b7b345
DJ
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");
c906108c 94
4ce44c66
JM
95 initialize_low ();
96
45b7b345
DJ
97 if (pid == 0)
98 {
99 /* Wait till we are at first instruction in program. */
100 signal = start_inferior (&argv[2], &status);
c906108c 101
45b7b345
DJ
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 }
c906108c
SS
116
117 while (1)
118 {
119 remote_open (argv[1]);
120
c5aa993b
JM
121 restart:
122 setjmp (toplevel);
c906108c
SS
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 '!':
45b7b345
DJ
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 }
c906108c
SS
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
c5aa993b 215 debugging session. The traditional protocol will
c906108c
SS
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
c5aa993b 240 extended protocol. */
c906108c
SS
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
c5aa993b
JM
262 empty packet so that gdb knows that we don't support this
263 request. */
c906108c
SS
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
c5aa993b
JM
299 For the extended remote protocol we exit (and this is the only
300 way we gracefully exit!).
c906108c 301
c5aa993b
JM
302 For the traditional remote protocol close the connection,
303 and re-open it at the top of the loop. */
c906108c
SS
304 if (extended_protocol)
305 {
306 remote_close ();
307 exit (0);
308 }
309 else
310 {
45b7b345
DJ
311 fprintf (stderr, "Remote side has terminated connection. "
312 "GDBserver will reopen the connection.\n");
c906108c
SS
313 remote_close ();
314 }
315 }
316}
This page took 0.153044 seconds and 4 git commands to generate.