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