2000-12-11 Fernando Nasser <fnasser@redhat.com>
[deliverable/binutils-gdb.git] / gdb / gdbserver / server.c
1 /* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include "server.h"
22
23 int cont_thread;
24 int general_thread;
25 int thread_from_wait;
26 int old_thread_from_wait;
27 int extended_protocol;
28 jmp_buf toplevel;
29 int inferior_pid;
30
31 static unsigned char
32 start_inferior (char *argv[], char *statusptr)
33 {
34 inferior_pid = create_inferior (argv[0], argv);
35 fprintf (stderr, "Process %s created; pid = %d\n", argv[0], inferior_pid);
36
37 /* Wait till we are at 1st instruction in program, return signal number. */
38 return mywait (statusptr);
39 }
40
41 extern int remote_debug;
42
43 int
44 main (int argc, char *argv[])
45 {
46 char ch, status, own_buf[PBUFSIZ], mem_buf[2000];
47 int i = 0;
48 unsigned char signal;
49 unsigned int len;
50 CORE_ADDR mem_addr;
51
52 if (setjmp (toplevel))
53 {
54 fprintf (stderr, "Exiting\n");
55 exit (1);
56 }
57
58 if (argc < 3)
59 error ("Usage: gdbserver tty prog [args ...]");
60
61 initialize_low ();
62
63 /* Wait till we are at first instruction in program. */
64 signal = start_inferior (&argv[2], &status);
65
66 /* We are now stopped at the first instruction of the target process */
67
68 while (1)
69 {
70 remote_open (argv[1]);
71
72 restart:
73 setjmp (toplevel);
74 while (getpkt (own_buf) > 0)
75 {
76 unsigned char sig;
77 i = 0;
78 ch = own_buf[i++];
79 switch (ch)
80 {
81 case 'd':
82 remote_debug = !remote_debug;
83 break;
84 case '!':
85 extended_protocol = 1;
86 prepare_resume_reply (own_buf, status, signal);
87 break;
88 case '?':
89 prepare_resume_reply (own_buf, status, signal);
90 break;
91 case 'H':
92 switch (own_buf[1])
93 {
94 case 'g':
95 general_thread = strtol (&own_buf[2], NULL, 16);
96 write_ok (own_buf);
97 fetch_inferior_registers (0);
98 break;
99 case 'c':
100 cont_thread = strtol (&own_buf[2], NULL, 16);
101 write_ok (own_buf);
102 break;
103 default:
104 /* Silently ignore it so that gdb can extend the protocol
105 without compatibility headaches. */
106 own_buf[0] = '\0';
107 break;
108 }
109 break;
110 case 'g':
111 convert_int_to_ascii (registers, own_buf, REGISTER_BYTES);
112 break;
113 case 'G':
114 convert_ascii_to_int (&own_buf[1], registers, REGISTER_BYTES);
115 store_inferior_registers (-1);
116 write_ok (own_buf);
117 break;
118 case 'm':
119 decode_m_packet (&own_buf[1], &mem_addr, &len);
120 read_inferior_memory (mem_addr, mem_buf, len);
121 convert_int_to_ascii (mem_buf, own_buf, len);
122 break;
123 case 'M':
124 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
125 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
126 write_ok (own_buf);
127 else
128 write_enn (own_buf);
129 break;
130 case 'C':
131 convert_ascii_to_int (own_buf + 1, &sig, 1);
132 myresume (0, sig);
133 signal = mywait (&status);
134 prepare_resume_reply (own_buf, status, signal);
135 break;
136 case 'S':
137 convert_ascii_to_int (own_buf + 1, &sig, 1);
138 myresume (1, sig);
139 signal = mywait (&status);
140 prepare_resume_reply (own_buf, status, signal);
141 break;
142 case 'c':
143 myresume (0, 0);
144 signal = mywait (&status);
145 prepare_resume_reply (own_buf, status, signal);
146 break;
147 case 's':
148 myresume (1, 0);
149 signal = mywait (&status);
150 prepare_resume_reply (own_buf, status, signal);
151 break;
152 case 'k':
153 fprintf (stderr, "Killing inferior\n");
154 kill_inferior ();
155 /* When using the extended protocol, we start up a new
156 debugging session. The traditional protocol will
157 exit instead. */
158 if (extended_protocol)
159 {
160 write_ok (own_buf);
161 fprintf (stderr, "GDBserver restarting\n");
162
163 /* Wait till we are at 1st instruction in prog. */
164 signal = start_inferior (&argv[2], &status);
165 goto restart;
166 break;
167 }
168 else
169 {
170 exit (0);
171 break;
172 }
173 case 'T':
174 if (mythread_alive (strtol (&own_buf[1], NULL, 16)))
175 write_ok (own_buf);
176 else
177 write_enn (own_buf);
178 break;
179 case 'R':
180 /* Restarting the inferior is only supported in the
181 extended protocol. */
182 if (extended_protocol)
183 {
184 kill_inferior ();
185 write_ok (own_buf);
186 fprintf (stderr, "GDBserver restarting\n");
187
188 /* Wait till we are at 1st instruction in prog. */
189 signal = start_inferior (&argv[2], &status);
190 goto restart;
191 break;
192 }
193 else
194 {
195 /* It is a request we don't understand. Respond with an
196 empty packet so that gdb knows that we don't support this
197 request. */
198 own_buf[0] = '\0';
199 break;
200 }
201 default:
202 /* It is a request we don't understand. Respond with an
203 empty packet so that gdb knows that we don't support this
204 request. */
205 own_buf[0] = '\0';
206 break;
207 }
208
209 putpkt (own_buf);
210
211 if (status == 'W')
212 fprintf (stderr,
213 "\nChild exited with status %d\n", sig);
214 if (status == 'X')
215 fprintf (stderr, "\nChild terminated with signal = 0x%x\n", sig);
216 if (status == 'W' || status == 'X')
217 {
218 if (extended_protocol)
219 {
220 fprintf (stderr, "Killing inferior\n");
221 kill_inferior ();
222 write_ok (own_buf);
223 fprintf (stderr, "GDBserver restarting\n");
224
225 /* Wait till we are at 1st instruction in prog. */
226 signal = start_inferior (&argv[2], &status);
227 goto restart;
228 break;
229 }
230 else
231 {
232 fprintf (stderr, "GDBserver exiting\n");
233 exit (0);
234 }
235 }
236 }
237
238 /* We come here when getpkt fails.
239
240 For the extended remote protocol we exit (and this is the only
241 way we gracefully exit!).
242
243 For the traditional remote protocol close the connection,
244 and re-open it at the top of the loop. */
245 if (extended_protocol)
246 {
247 remote_close ();
248 exit (0);
249 }
250 else
251 {
252 fprintf (stderr, "Remote side has terminated connection. GDBserver will reopen the connection.\n");
253
254 remote_close ();
255 }
256 }
257 }
This page took 0.035006 seconds and 4 git commands to generate.