1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
27 #include <sys/param.h>
31 #include <sys/ioctl.h>
37 /***************Begin MY defs*********************/
39 char registers
[REGISTER_BYTES
];
41 /* Index within `registers' of the first byte of the space for
45 char buf2
[MAX_REGISTER_RAW_SIZE
];
46 /***************End MY defs*********************/
48 #include <sys/ptrace.h>
50 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1)
54 extern char **environ
;
56 extern int inferior_pid
;
57 void quit (), perror_with_name ();
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. */
65 create_inferior (program
, allargs
)
73 perror_with_name ("fork");
77 ptrace (PTRACE_TRACEME
, 0, 0, 0);
79 execv (program
, allargs
);
81 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
82 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
90 /* Kill the inferior process. Make us have no inferior. */
95 if (inferior_pid
== 0)
97 ptrace (PTRACE_KILL
, inferior_pid
, 0, 0);
99 /*************inferior_died ();****VK**************/
102 /* Return nonzero if the given thread is still alive. */
110 /* Wait for process, returns status */
120 if (pid
!= inferior_pid
)
121 perror_with_name ("wait");
125 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
127 return ((unsigned char) WEXITSTATUS (w
));
129 else if (!WIFSTOPPED (w
))
131 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
133 return ((unsigned char) WTERMSIG (w
));
136 fetch_inferior_registers (0);
139 return ((unsigned char) WSTOPSIG (w
));
142 /* Resume execution of the inferior process.
143 If STEP is nonzero, single-step it.
144 If SIGNAL is nonzero, give it that signal. */
147 myresume (step
, signal
)
152 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, inferior_pid
, 1, signal
);
154 perror_with_name ("ptrace");
158 #if !defined (offsetof)
159 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
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) \
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
[] =
182 i386_register_u_addr (blockend
, regnum
)
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
191 if (regnum
>= FP0_REGNUM
&& regnum
<= FP7_REGNUM
)
195 ubase
= blockend
+ 4 * (SS
+ 1) - KSTKSZ
;
196 fpstate
= ubase
+ ((char *) &u
.u_fpstate
- (char *) &u
);
197 return (fpstate
+ 0x1c + 10 * (regnum
- FP0_REGNUM
));
201 return (blockend
+ 4 * regmap
[regnum
]);
204 #else /* TARGET_M68K */
205 /* This table must line up with REGISTER_NAMES in tm-m68k.h */
206 static int regmap
[] =
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
,
213 14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15,
217 PT_FP0
, PT_FP1
, PT_FP2
, PT_FP3
, PT_FP4
, PT_FP5
, PT_FP6
, PT_FP7
,
218 PT_FPCR
, PT_FPSR
, PT_FPIAR
220 21, 24, 27, 30, 33, 36, 39, 42, 45, 46, 47
224 /* BLOCKEND is the value of u.u_ar0, and points to the place where GS
228 m68k_linux_register_u_addr (blockend
, regnum
)
232 return (blockend
+ 4 * regmap
[regnum
]);
237 register_addr (regno
, blockend
)
243 if (regno
< 0 || regno
>= ARCH_NUM_REGS
)
244 error ("Invalid register number %d.", regno
);
246 REGISTER_U_ADDR (addr
, blockend
, regno
);
251 /* Fetch one register. */
254 fetch_register (regno
)
257 register unsigned int regaddr
;
260 /* Offset of registers within the u area. */
263 offset
= U_REGS_OFFSET
;
265 regaddr
= register_addr (regno
, offset
);
266 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
269 *(int *) ®isters
[regno
* 4 + i
] = ptrace (PTRACE_PEEKUSR
, inferior_pid
,
270 (PTRACE_ARG3_TYPE
) regaddr
, 0);
271 regaddr
+= sizeof (int);
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
);
286 /* Fetch all registers, or just one, from the child process. */
289 fetch_inferior_registers (regno
)
292 if (regno
== -1 || regno
== 0)
293 for (regno
= 0; regno
< NUM_REGS
- NUM_FREGS
; regno
++)
294 fetch_register (regno
);
296 fetch_register (regno
);
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). */
304 store_inferior_registers (regno
)
307 register unsigned int regaddr
;
309 unsigned int offset
= U_REGS_OFFSET
;
314 if (CANNOT_STORE_REGISTER (regno
))
317 regaddr
= register_addr (regno
, offset
);
320 if (regno
== PCOQ_HEAD_REGNUM
|| regno
== PCOQ_TAIL_REGNUM
)
322 scratch
= *(int *) ®isters
[REGISTER_BYTE (regno
)] | 0x3;
323 ptrace (PT_WUREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
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
);
335 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
338 ptrace (PTRACE_POKEUSR
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
339 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
]);
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",
351 regaddr
+= sizeof (int);
355 for (regno
= 0; regno
< NUM_REGS
- NUM_FREGS
; regno
++)
356 store_inferior_registers (regno
);
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. */
365 /* Copy LEN bytes from inferior's memory starting at MEMADDR
366 to debugger memory starting at MYADDR. */
369 read_inferior_memory (memaddr
, myaddr
, len
)
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. */
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));
383 /* Read all the longwords */
384 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
386 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, addr
, 0);
389 /* Copy appropriate bytes out of the buffer. */
390 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
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. */
399 write_inferior_memory (memaddr
, myaddr
, len
)
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. */
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));
414 /* Fill start and end extra bytes of buffer with existing memory data. */
416 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, addr
, 0);
421 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
422 addr
+ (count
- 1) * sizeof (int), 0);
425 /* Copy data to be written over corresponding part of buffer */
427 memcpy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
429 /* Write the entire buffer. */
431 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
434 ptrace (PTRACE_POKETEXT
, inferior_pid
, addr
, buffer
[i
]);
451 return inferior_pid
!= 0;
This page took 0.059273 seconds and 5 git commands to generate.