]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbserver/server.c
import gdb-1999-11-16 snapshot
[thirdparty/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
32start_inferior (argv, statusptr)
33 char *argv[];
34 char *statusptr;
35{
36 inferior_pid = create_inferior (argv[0], argv);
37 fprintf (stderr, "Process %s created; pid = %d\n", argv[0], inferior_pid);
38
39 /* Wait till we are at 1st instruction in program, return signal number. */
40 return mywait (statusptr);
41}
42
43extern int remote_debug;
44
45int
46main (argc, argv)
47 int argc;
48 char *argv[];
49{
5c44784c 50 char ch, status, own_buf[PBUFSIZ], mem_buf[2000];
c906108c
SS
51 int i = 0;
52 unsigned char signal;
53 unsigned int len;
54 CORE_ADDR mem_addr;
55
c5aa993b 56 if (setjmp (toplevel))
c906108c 57 {
c5aa993b
JM
58 fprintf (stderr, "Exiting\n");
59 exit (1);
c906108c
SS
60 }
61
62 if (argc < 3)
c5aa993b 63 error ("Usage: gdbserver tty prog [args ...]");
c906108c 64
4ce44c66
JM
65 initialize_low ();
66
c906108c
SS
67 /* Wait till we are at first instruction in program. */
68 signal = start_inferior (&argv[2], &status);
69
70 /* We are now stopped at the first instruction of the target process */
71
72 while (1)
73 {
74 remote_open (argv[1]);
75
c5aa993b
JM
76 restart:
77 setjmp (toplevel);
c906108c
SS
78 while (getpkt (own_buf) > 0)
79 {
80 unsigned char sig;
81 i = 0;
82 ch = own_buf[i++];
83 switch (ch)
84 {
85 case 'd':
86 remote_debug = !remote_debug;
87 break;
88 case '!':
89 extended_protocol = 1;
90 prepare_resume_reply (own_buf, status, signal);
91 break;
92 case '?':
93 prepare_resume_reply (own_buf, status, signal);
94 break;
95 case 'H':
96 switch (own_buf[1])
97 {
98 case 'g':
99 general_thread = strtol (&own_buf[2], NULL, 16);
100 write_ok (own_buf);
101 fetch_inferior_registers (0);
102 break;
103 case 'c':
104 cont_thread = strtol (&own_buf[2], NULL, 16);
105 write_ok (own_buf);
106 break;
107 default:
108 /* Silently ignore it so that gdb can extend the protocol
109 without compatibility headaches. */
110 own_buf[0] = '\0';
111 break;
112 }
113 break;
114 case 'g':
115 convert_int_to_ascii (registers, own_buf, REGISTER_BYTES);
116 break;
117 case 'G':
118 convert_ascii_to_int (&own_buf[1], registers, REGISTER_BYTES);
119 store_inferior_registers (-1);
120 write_ok (own_buf);
121 break;
122 case 'm':
123 decode_m_packet (&own_buf[1], &mem_addr, &len);
124 read_inferior_memory (mem_addr, mem_buf, len);
125 convert_int_to_ascii (mem_buf, own_buf, len);
126 break;
127 case 'M':
128 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
129 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
130 write_ok (own_buf);
131 else
132 write_enn (own_buf);
133 break;
134 case 'C':
135 convert_ascii_to_int (own_buf + 1, &sig, 1);
136 myresume (0, sig);
137 signal = mywait (&status);
138 prepare_resume_reply (own_buf, status, signal);
139 break;
140 case 'S':
141 convert_ascii_to_int (own_buf + 1, &sig, 1);
142 myresume (1, sig);
143 signal = mywait (&status);
144 prepare_resume_reply (own_buf, status, signal);
145 break;
146 case 'c':
147 myresume (0, 0);
148 signal = mywait (&status);
149 prepare_resume_reply (own_buf, status, signal);
150 break;
151 case 's':
152 myresume (1, 0);
153 signal = mywait (&status);
154 prepare_resume_reply (own_buf, status, signal);
155 break;
156 case 'k':
157 fprintf (stderr, "Killing inferior\n");
158 kill_inferior ();
159 /* When using the extended protocol, we start up a new
c5aa993b 160 debugging session. The traditional protocol will
c906108c
SS
161 exit instead. */
162 if (extended_protocol)
163 {
164 write_ok (own_buf);
165 fprintf (stderr, "GDBserver restarting\n");
166
167 /* Wait till we are at 1st instruction in prog. */
168 signal = start_inferior (&argv[2], &status);
169 goto restart;
170 break;
171 }
172 else
173 {
174 exit (0);
175 break;
176 }
177 case 'T':
178 if (mythread_alive (strtol (&own_buf[1], NULL, 16)))
179 write_ok (own_buf);
180 else
181 write_enn (own_buf);
182 break;
183 case 'R':
184 /* Restarting the inferior is only supported in the
c5aa993b 185 extended protocol. */
c906108c
SS
186 if (extended_protocol)
187 {
188 kill_inferior ();
189 write_ok (own_buf);
190 fprintf (stderr, "GDBserver restarting\n");
191
192 /* Wait till we are at 1st instruction in prog. */
193 signal = start_inferior (&argv[2], &status);
194 goto restart;
195 break;
196 }
197 else
198 {
199 /* It is a request we don't understand. Respond with an
200 empty packet so that gdb knows that we don't support this
201 request. */
202 own_buf[0] = '\0';
203 break;
204 }
205 default:
206 /* It is a request we don't understand. Respond with an
c5aa993b
JM
207 empty packet so that gdb knows that we don't support this
208 request. */
c906108c
SS
209 own_buf[0] = '\0';
210 break;
211 }
212
213 putpkt (own_buf);
214
215 if (status == 'W')
216 fprintf (stderr,
217 "\nChild exited with status %d\n", sig);
218 if (status == 'X')
219 fprintf (stderr, "\nChild terminated with signal = 0x%x\n", sig);
220 if (status == 'W' || status == 'X')
221 {
222 if (extended_protocol)
223 {
224 fprintf (stderr, "Killing inferior\n");
225 kill_inferior ();
226 write_ok (own_buf);
227 fprintf (stderr, "GDBserver restarting\n");
228
229 /* Wait till we are at 1st instruction in prog. */
230 signal = start_inferior (&argv[2], &status);
231 goto restart;
232 break;
233 }
234 else
235 {
236 fprintf (stderr, "GDBserver exiting\n");
237 exit (0);
238 }
239 }
240 }
241
242 /* We come here when getpkt fails.
243
c5aa993b
JM
244 For the extended remote protocol we exit (and this is the only
245 way we gracefully exit!).
c906108c 246
c5aa993b
JM
247 For the traditional remote protocol close the connection,
248 and re-open it at the top of the loop. */
c906108c
SS
249 if (extended_protocol)
250 {
251 remote_close ();
252 exit (0);
253 }
254 else
255 {
256 fprintf (stderr, "Remote side has terminated connection. GDBserver will reopen the connection.\n");
257
258 remote_close ();
259 }
260 }
261}