Fix whitespace problem in my most recent entry.
[deliverable/binutils-gdb.git] / gdb / gdbserver / server.c
CommitLineData
c906108c
SS
1/* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993 Free Software Foundation, Inc.
3
c5aa993b 4 This file is part of GDB.
c906108c 5
c5aa993b
JM
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.
c906108c 10
c5aa993b
JM
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.
c906108c 15
c5aa993b
JM
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. */
c906108c
SS
20
21#include "server.h"
22
23int cont_thread;
24int general_thread;
25int thread_from_wait;
26int old_thread_from_wait;
27int extended_protocol;
28jmp_buf toplevel;
29int inferior_pid;
30
31static unsigned char
da85418c 32start_inferior (char *argv[], char *statusptr)
c906108c
SS
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
41extern int remote_debug;
42
43int
da85418c 44main (int argc, char *argv[])
c906108c 45{
5c44784c 46 char ch, status, own_buf[PBUFSIZ], mem_buf[2000];
c906108c
SS
47 int i = 0;
48 unsigned char signal;
49 unsigned int len;
50 CORE_ADDR mem_addr;
51
c5aa993b 52 if (setjmp (toplevel))
c906108c 53 {
c5aa993b
JM
54 fprintf (stderr, "Exiting\n");
55 exit (1);
c906108c
SS
56 }
57
58 if (argc < 3)
c5aa993b 59 error ("Usage: gdbserver tty prog [args ...]");
c906108c 60
4ce44c66
JM
61 initialize_low ();
62
c906108c
SS
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
c5aa993b
JM
72 restart:
73 setjmp (toplevel);
c906108c
SS
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
c5aa993b 156 debugging session. The traditional protocol will
c906108c
SS
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
c5aa993b 181 extended protocol. */
c906108c
SS
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
c5aa993b
JM
203 empty packet so that gdb knows that we don't support this
204 request. */
c906108c
SS
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
c5aa993b
JM
240 For the extended remote protocol we exit (and this is the only
241 way we gracefully exit!).
c906108c 242
c5aa993b
JM
243 For the traditional remote protocol close the connection,
244 and re-open it at the top of the loop. */
c906108c
SS
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.102584 seconds and 4 git commands to generate.