* alpha-nat.c (fetch_core_registers): Match Sep 4 gdbcore.h prototype
[deliverable/binutils-gdb.git] / gdb / gdbserver / low-linux.c
1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995, 1996 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, Boston, MA 02111-1307, USA. */
19
20 #include "defs.h"
21 #include <sys/wait.h>
22 #include "frame.h"
23 #include "inferior.h"
24
25 #include <stdio.h>
26 #include <sys/param.h>
27 #include <sys/dir.h>
28 #include <sys/user.h>
29 #include <signal.h>
30 #include <sys/ioctl.h>
31 #if 0
32 #include <sgtty.h>
33 #endif
34 #include <fcntl.h>
35
36 /***************Begin MY defs*********************/
37 int quit_flag = 0;
38 char registers[REGISTER_BYTES];
39
40 /* Index within `registers' of the first byte of the space for
41 register N. */
42
43
44 char buf2[MAX_REGISTER_RAW_SIZE];
45 /***************End MY defs*********************/
46
47 #include <sys/ptrace.h>
48 #if 0
49 #include <machine/reg.h>
50 #endif
51
52 extern char **environ;
53 extern int errno;
54 extern int inferior_pid;
55 void quit (), perror_with_name ();
56 int query ();
57
58 /* Start an inferior process and returns its pid.
59 ALLARGS is a vector of program-name and args.
60 ENV is the environment vector to pass. */
61
62 int
63 create_inferior (program, allargs)
64 char *program;
65 char **allargs;
66 {
67 int pid;
68
69 pid = fork ();
70 if (pid < 0)
71 perror_with_name ("fork");
72
73 if (pid == 0)
74 {
75 ptrace (PTRACE_TRACEME, 0, 0, 0);
76
77 execv (program, allargs);
78
79 fprintf (stderr, "Cannot exec %s: %s.\n", program,
80 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
81 fflush (stderr);
82 _exit (0177);
83 }
84
85 return pid;
86 }
87
88 /* Kill the inferior process. Make us have no inferior. */
89
90 void
91 kill_inferior ()
92 {
93 if (inferior_pid == 0)
94 return;
95 ptrace (PTRACE_KILL, inferior_pid, 0, 0);
96 wait (0);
97 /*************inferior_died ();****VK**************/
98 }
99
100 /* Return nonzero if the given thread is still alive. */
101 int
102 mythread_alive (pid)
103 int pid;
104 {
105 return 1;
106 }
107
108 /* Wait for process, returns status */
109
110 unsigned char
111 mywait (status)
112 char *status;
113 {
114 int pid;
115 union wait w;
116
117 pid = wait (&w);
118 if (pid != inferior_pid)
119 perror_with_name ("wait");
120
121 if (WIFEXITED (w))
122 {
123 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
124 *status = 'W';
125 return ((unsigned char) WEXITSTATUS (w));
126 }
127 else if (!WIFSTOPPED (w))
128 {
129 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
130 *status = 'X';
131 return ((unsigned char) WTERMSIG (w));
132 }
133
134 fetch_inferior_registers (0);
135
136 *status = 'T';
137 return ((unsigned char) WSTOPSIG (w));
138 }
139
140 /* Resume execution of the inferior process.
141 If STEP is nonzero, single-step it.
142 If SIGNAL is nonzero, give it that signal. */
143
144 void
145 myresume (step, signal)
146 int step;
147 int signal;
148 {
149 errno = 0;
150 ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, inferior_pid, 1, signal);
151 if (errno)
152 perror_with_name ("ptrace");
153 }
154
155
156 #if !defined (offsetof)
157 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
158 #endif
159
160 /* U_REGS_OFFSET is the offset of the registers within the u area. */
161 #if !defined (U_REGS_OFFSET)
162 #define U_REGS_OFFSET \
163 ptrace (PT_READ_U, inferior_pid, \
164 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
165 - KERNEL_U_ADDR
166 #endif
167
168 /* this table must line up with REGISTER_NAMES in tm-i386v.h */
169 /* symbols like 'EAX' come from <sys/reg.h> */
170 static int regmap[] =
171 {
172 EAX, ECX, EDX, EBX,
173 UESP, EBP, ESI, EDI,
174 EIP, EFL, CS, SS,
175 DS, ES, FS, GS,
176 };
177
178 int
179 i386_register_u_addr (blockend, regnum)
180 int blockend;
181 int regnum;
182 {
183 #if 0
184 /* this will be needed if fp registers are reinstated */
185 /* for now, you can look at them with 'info float'
186 * sys5 wont let you change them with ptrace anyway
187 */
188 if (regnum >= FP0_REGNUM && regnum <= FP7_REGNUM)
189 {
190 int ubase, fpstate;
191 struct user u;
192 ubase = blockend + 4 * (SS + 1) - KSTKSZ;
193 fpstate = ubase + ((char *)&u.u_fpstate - (char *)&u);
194 return (fpstate + 0x1c + 10 * (regnum - FP0_REGNUM));
195 }
196 else
197 #endif
198 return (blockend + 4 * regmap[regnum]);
199
200 }
201
202 CORE_ADDR
203 register_addr (regno, blockend)
204 int regno;
205 CORE_ADDR blockend;
206 {
207 CORE_ADDR addr;
208
209 if (regno < 0 || regno >= ARCH_NUM_REGS)
210 error ("Invalid register number %d.", regno);
211
212 REGISTER_U_ADDR (addr, blockend, regno);
213
214 return addr;
215 }
216
217 /* Fetch one register. */
218
219 static void
220 fetch_register (regno)
221 int regno;
222 {
223 register unsigned int regaddr;
224 char buf[MAX_REGISTER_RAW_SIZE];
225 register int i;
226
227 /* Offset of registers within the u area. */
228 unsigned int offset;
229
230 offset = U_REGS_OFFSET;
231
232 regaddr = register_addr (regno, offset);
233 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
234 {
235 errno = 0;
236 *(int *) &registers[ regno * 4 + i] = ptrace (PTRACE_PEEKUSR, inferior_pid,
237 (PTRACE_ARG3_TYPE) regaddr, 0);
238 regaddr += sizeof (int);
239 if (errno != 0)
240 {
241 /* Warning, not error, in case we are attached; sometimes the
242 kernel doesn't let us at the registers. */
243 char *err = strerror (errno);
244 char *msg = alloca (strlen (err) + 128);
245 sprintf (msg, "reading register %d: %s", regno, err);
246 error (msg);
247 goto error_exit;
248 }
249 }
250 error_exit:;
251 }
252
253 /* Fetch all registers, or just one, from the child process. */
254
255 void
256 fetch_inferior_registers (regno)
257 int regno;
258 {
259 if (regno == -1 || regno == 0)
260 for (regno = 0; regno < NUM_REGS-NUM_FREGS; regno++)
261 fetch_register (regno);
262 else
263 fetch_register (regno);
264 }
265
266 /* Store our register values back into the inferior.
267 If REGNO is -1, do this for all registers.
268 Otherwise, REGNO specifies which register (so we can save time). */
269
270 void
271 store_inferior_registers (regno)
272 int regno;
273 {
274 register unsigned int regaddr;
275 char buf[80];
276 register int i;
277 unsigned int offset = U_REGS_OFFSET;
278 int scratch;
279
280 if (regno >= 0)
281 {
282 #if 0
283 if (CANNOT_STORE_REGISTER (regno))
284 return;
285 #endif
286 regaddr = register_addr (regno, offset);
287 errno = 0;
288 #if 0
289 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
290 {
291 scratch = *(int *) &registers[REGISTER_BYTE (regno)] | 0x3;
292 ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
293 scratch, 0);
294 if (errno != 0)
295 {
296 /* Error, even if attached. Failing to write these two
297 registers is pretty serious. */
298 sprintf (buf, "writing register number %d", regno);
299 perror_with_name (buf);
300 }
301 }
302 else
303 #endif
304 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
305 {
306 errno = 0;
307 ptrace (PTRACE_POKEUSR, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
308 *(int *) &registers[REGISTER_BYTE (regno) + i]);
309 if (errno != 0)
310 {
311 /* Warning, not error, in case we are attached; sometimes the
312 kernel doesn't let us at the registers. */
313 char *err = strerror (errno);
314 char *msg = alloca (strlen (err) + 128);
315 sprintf (msg, "writing register %d: %s",
316 regno, err);
317 error (msg);
318 return;
319 }
320 regaddr += sizeof(int);
321 }
322 }
323 else
324 for (regno = 0; regno < NUM_REGS-NUM_FREGS; regno++)
325 store_inferior_registers (regno);
326 }
327
328 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
329 in the NEW_SUN_PTRACE case.
330 It ought to be straightforward. But it appears that writing did
331 not write the data that I specified. I cannot understand where
332 it got the data that it actually did write. */
333
334 /* Copy LEN bytes from inferior's memory starting at MEMADDR
335 to debugger memory starting at MYADDR. */
336
337 read_inferior_memory (memaddr, myaddr, len)
338 CORE_ADDR memaddr;
339 char *myaddr;
340 int len;
341 {
342 register int i;
343 /* Round starting address down to longword boundary. */
344 register CORE_ADDR addr = memaddr & -sizeof (int);
345 /* Round ending address up; get number of longwords that makes. */
346 register int count
347 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
348 /* Allocate buffer of that many longwords. */
349 register int *buffer = (int *) alloca (count * sizeof (int));
350
351 /* Read all the longwords */
352 for (i = 0; i < count; i++, addr += sizeof (int))
353 {
354 buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, addr, 0);
355 }
356
357 /* Copy appropriate bytes out of the buffer. */
358 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
359 }
360
361 /* Copy LEN bytes of data from debugger memory at MYADDR
362 to inferior's memory at MEMADDR.
363 On failure (cannot write the inferior)
364 returns the value of errno. */
365
366 int
367 write_inferior_memory (memaddr, myaddr, len)
368 CORE_ADDR memaddr;
369 char *myaddr;
370 int len;
371 {
372 register int i;
373 /* Round starting address down to longword boundary. */
374 register CORE_ADDR addr = memaddr & -sizeof (int);
375 /* Round ending address up; get number of longwords that makes. */
376 register int count
377 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
378 /* Allocate buffer of that many longwords. */
379 register int *buffer = (int *) alloca (count * sizeof (int));
380 extern int errno;
381
382 /* Fill start and end extra bytes of buffer with existing memory data. */
383
384 buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid, addr, 0);
385
386 if (count > 1)
387 {
388 buffer[count - 1]
389 = ptrace (PTRACE_PEEKTEXT, inferior_pid,
390 addr + (count - 1) * sizeof (int), 0);
391 }
392
393 /* Copy data to be written over corresponding part of buffer */
394
395 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
396
397 /* Write the entire buffer. */
398
399 for (i = 0; i < count; i++, addr += sizeof (int))
400 {
401 errno = 0;
402 ptrace (PTRACE_POKETEXT, inferior_pid, addr, buffer[i]);
403 if (errno)
404 return errno;
405 }
406
407 return 0;
408 }
409 \f
410 void
411 initialize ()
412 {
413 inferior_pid = 0;
414 }
415
416 int
417 have_inferior_p ()
418 {
419 return inferior_pid != 0;
420 }
This page took 0.038045 seconds and 5 git commands to generate.