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