2002-02-14 Daniel Jacobowitz <drow@mvista.com>
[deliverable/binutils-gdb.git] / gdb / gdbserver / linux-low.c
1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright 1995, 1996, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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. */
21
22 #include "server.h"
23 #include <sys/wait.h>
24
25 #include <stdio.h>
26 #include <sys/param.h>
27 #include <sys/dir.h>
28 #include <sys/ptrace.h>
29 #include <sys/user.h>
30 #include <signal.h>
31 #include <sys/ioctl.h>
32 #include <fcntl.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36
37 #define PTRACE_ARG3_TYPE long
38 #define PTRACE_XFER_TYPE int
39
40 extern int errno;
41 extern int num_regs;
42 extern int regmap[];
43
44 /* Start an inferior process and returns its pid.
45 ALLARGS is a vector of program-name and args. */
46
47 int
48 create_inferior (char *program, char **allargs)
49 {
50 int pid;
51
52 pid = fork ();
53 if (pid < 0)
54 perror_with_name ("fork");
55
56 if (pid == 0)
57 {
58 ptrace (PTRACE_TRACEME, 0, 0, 0);
59
60 execv (program, allargs);
61
62 fprintf (stderr, "Cannot exec %s: %s.\n", program,
63 strerror (errno));
64 fflush (stderr);
65 _exit (0177);
66 }
67
68 return pid;
69 }
70
71 /* Attach to an inferior process. */
72
73 int
74 myattach (int pid)
75 {
76 if (ptrace (PTRACE_ATTACH, pid, 0, 0) != 0)
77 {
78 fprintf (stderr, "Cannot attach to process %d: %s (%d)\n", pid,
79 errno < sys_nerr ? sys_errlist[errno] : "unknown error",
80 errno);
81 fflush (stderr);
82 _exit (0177);
83 }
84
85 return 0;
86 }
87
88 /* Kill the inferior process. Make us have no inferior. */
89
90 void
91 kill_inferior (void)
92 {
93 if (inferior_pid == 0)
94 return;
95 ptrace (PTRACE_KILL, inferior_pid, 0, 0);
96 wait (0);
97 }
98
99 /* Return nonzero if the given thread is still alive. */
100 int
101 mythread_alive (int pid)
102 {
103 return 1;
104 }
105
106 /* Wait for process, returns status */
107
108 unsigned char
109 mywait (char *status)
110 {
111 int pid;
112 union wait w;
113
114 enable_async_io ();
115 pid = waitpid (inferior_pid, (int *)&w, 0);
116 disable_async_io ();
117 if (pid != inferior_pid)
118 perror_with_name ("wait");
119
120 if (WIFEXITED (w))
121 {
122 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
123 *status = 'W';
124 return ((unsigned char) WEXITSTATUS (w));
125 }
126 else if (!WIFSTOPPED (w))
127 {
128 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
129 *status = 'X';
130 return ((unsigned char) WTERMSIG (w));
131 }
132
133 fetch_inferior_registers (0);
134
135 *status = 'T';
136 return ((unsigned char) WSTOPSIG (w));
137 }
138
139 /* Resume execution of the inferior process.
140 If STEP is nonzero, single-step it.
141 If SIGNAL is nonzero, give it that signal. */
142
143 void
144 myresume (int step, int signal)
145 {
146 errno = 0;
147 ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, inferior_pid, 1, signal);
148 if (errno)
149 perror_with_name ("ptrace");
150 }
151
152 #define REGISTER_RAW_SIZE(regno) register_size((regno))
153
154 int
155 register_addr (int regnum)
156 {
157 int addr;
158
159 if (regnum < 0 || regnum >= num_regs)
160 error ("Invalid register number %d.", regnum);
161
162 addr = regmap[regnum];
163 if (addr == -1)
164 addr = 0;
165
166 return addr;
167 }
168
169 /* Fetch one register. */
170
171 static void
172 fetch_register (int regno)
173 {
174 CORE_ADDR regaddr;
175 register int i;
176
177 if (regno >= num_regs)
178 return;
179 if (cannot_fetch_register (regno))
180 return;
181
182 regaddr = register_addr (regno);
183 if (regaddr == -1)
184 return;
185 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
186 {
187 errno = 0;
188 *(PTRACE_XFER_TYPE *) (register_data (regno) + i) =
189 ptrace (PTRACE_PEEKUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, 0);
190 regaddr += sizeof (PTRACE_XFER_TYPE);
191 if (errno != 0)
192 {
193 /* Warning, not error, in case we are attached; sometimes the
194 kernel doesn't let us at the registers. */
195 char *err = strerror (errno);
196 char *msg = alloca (strlen (err) + 128);
197 sprintf (msg, "reading register %d: %s", regno, err);
198 error (msg);
199 goto error_exit;
200 }
201 }
202 error_exit:;
203 }
204
205 /* Fetch all registers, or just one, from the child process. */
206
207 void
208 fetch_inferior_registers (int regno)
209 {
210 if (regno == -1 || regno == 0)
211 for (regno = 0; regno < num_regs; regno++)
212 fetch_register (regno);
213 else
214 fetch_register (regno);
215 }
216
217 /* Store our register values back into the inferior.
218 If REGNO is -1, do this for all registers.
219 Otherwise, REGNO specifies which register (so we can save time). */
220
221 void
222 store_inferior_registers (int regno)
223 {
224 CORE_ADDR regaddr;
225 int i;
226
227 if (regno >= 0)
228 {
229 if (regno >= num_regs)
230 return;
231
232 if (cannot_store_register (regno))
233 return;
234
235 regaddr = register_addr (regno);
236 if (regaddr == -1)
237 return;
238 errno = 0;
239 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
240 {
241 errno = 0;
242 ptrace (PTRACE_POKEUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
243 *(int *) (register_data (regno) + i));
244 if (errno != 0)
245 {
246 /* Warning, not error, in case we are attached; sometimes the
247 kernel doesn't let us at the registers. */
248 char *err = strerror (errno);
249 char *msg = alloca (strlen (err) + 128);
250 sprintf (msg, "writing register %d: %s",
251 regno, err);
252 error (msg);
253 return;
254 }
255 regaddr += sizeof (int);
256 }
257 }
258 else
259 for (regno = 0; regno < num_regs; regno++)
260 store_inferior_registers (regno);
261 }
262
263 /* Copy LEN bytes from inferior's memory starting at MEMADDR
264 to debugger memory starting at MYADDR. */
265
266 void
267 read_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
268 {
269 register int i;
270 /* Round starting address down to longword boundary. */
271 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
272 /* Round ending address up; get number of longwords that makes. */
273 register int count
274 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
275 / sizeof (PTRACE_XFER_TYPE);
276 /* Allocate buffer of that many longwords. */
277 register PTRACE_XFER_TYPE *buffer
278 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
279
280 /* Read all the longwords */
281 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
282 {
283 buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
284 }
285
286 /* Copy appropriate bytes out of the buffer. */
287 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len);
288 }
289
290 /* Copy LEN bytes of data from debugger memory at MYADDR
291 to inferior's memory at MEMADDR.
292 On failure (cannot write the inferior)
293 returns the value of errno. */
294
295 int
296 write_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len)
297 {
298 register int i;
299 /* Round starting address down to longword boundary. */
300 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
301 /* Round ending address up; get number of longwords that makes. */
302 register int count
303 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE);
304 /* Allocate buffer of that many longwords. */
305 register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
306 extern int errno;
307
308 /* Fill start and end extra bytes of buffer with existing memory data. */
309
310 buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid,
311 (PTRACE_ARG3_TYPE) addr, 0);
312
313 if (count > 1)
314 {
315 buffer[count - 1]
316 = ptrace (PTRACE_PEEKTEXT, inferior_pid,
317 (PTRACE_ARG3_TYPE) (addr + (count - 1)
318 * sizeof (PTRACE_XFER_TYPE)),
319 0);
320 }
321
322 /* Copy data to be written over corresponding part of buffer */
323
324 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len);
325
326 /* Write the entire buffer. */
327
328 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
329 {
330 errno = 0;
331 ptrace (PTRACE_POKETEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
332 if (errno)
333 return errno;
334 }
335
336 return 0;
337 }
338 \f
339 void
340 initialize_low (void)
341 {
342 init_registers ();
343 }
This page took 0.036433 seconds and 4 git commands to generate.