1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1986, 1987, 1993, 2000 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, Boston, MA 02111-1307, USA. */
21 #include <sys/types.h>
29 /***************Begin MY defs*********************/
31 static char my_registers
[REGISTER_BYTES
];
32 char *registers
= my_registers
;
34 /* Index within `registers' of the first byte of the space for
37 char buf2
[MAX_REGISTER_RAW_SIZE
];
38 /***************End MY defs*********************/
40 #include <sys/ptrace.h>
41 #include <machine/reg.h>
44 // extern char **sys_errlist;
45 extern char **environ
;
46 extern int inferior_pid
;
47 void quit (), perror_with_name ();
49 #define RF(dst, src) \
50 memcpy(®isters[REGISTER_BYTE(dst)], &src, sizeof(src))
52 #define RS(src, dst) \
53 memcpy(&dst, ®isters[REGISTER_BYTE(src)], sizeof(dst))
58 unsigned short control
;
60 unsigned short status
;
65 unsigned short code_seg
;
66 unsigned short opcode
;
67 unsigned long operand
;
68 unsigned short operand_seg
;
70 unsigned char regs
[8][10];
73 /* i386_register_raw_size[i] is the number of bytes of storage in the
74 actual machine representation for register i. */
75 int i386_register_raw_size
[MAX_NUM_REGS
] = {
89 int i386_register_byte
[MAX_NUM_REGS
];
94 /* Initialize the table saying where each register starts in the
100 for (i
= 0; i
< MAX_NUM_REGS
; i
++)
102 i386_register_byte
[i
] = offset
;
103 offset
+= i386_register_raw_size
[i
];
107 #endif /* !__i386__ */
114 #endif /* !__powerpc__ */
117 /* Start an inferior process and returns its pid.
118 ALLARGS is a vector of program-name and args.
119 ENV is the environment vector to pass. */
122 create_inferior (program
, allargs
)
130 perror_with_name ("fork");
134 ptrace (PT_TRACE_ME
, 0, 0, 0);
136 execv (program
, allargs
);
138 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
139 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
147 /* Kill the inferior process. Make us have no inferior. */
152 if (inferior_pid
== 0)
154 ptrace (PT_KILL
, inferior_pid
, 0, 0);
156 /*************inferior_died ();****VK**************/
159 /* Return nonzero if the given thread is still alive. */
167 /* Wait for process, returns status */
177 if (pid
!= inferior_pid
)
178 perror_with_name ("wait");
182 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
184 return ((unsigned char) WEXITSTATUS (w
));
186 else if (!WIFSTOPPED (w
))
188 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
190 return ((unsigned char) WTERMSIG (w
));
193 fetch_inferior_registers (0);
196 return ((unsigned char) WSTOPSIG (w
));
199 /* Resume execution of the inferior process.
200 If STEP is nonzero, single-step it.
201 If SIGNAL is nonzero, give it that signal. */
204 myresume (step
, signal
)
209 ptrace (step
? PT_STEP
: PT_CONTINUE
, inferior_pid
,
210 (PTRACE_ARG3_TYPE
) 1, signal
);
212 perror_with_name ("ptrace");
217 /* Fetch one or more registers from the inferior. REGNO == -1 to get
218 them all. We actually fetch more than requested, when convenient,
219 marking them as valid so we won't fetch them again. */
222 fetch_inferior_registers (ignored
)
225 struct reg inferior_registers
;
226 struct env387 inferior_fp_registers
;
228 ptrace (PT_GETREGS
, inferior_pid
,
229 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0);
230 ptrace (PT_GETFPREGS
, inferior_pid
,
231 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
, 0);
233 RF ( 0, inferior_registers
.r_eax
);
234 RF ( 1, inferior_registers
.r_ecx
);
235 RF ( 2, inferior_registers
.r_edx
);
236 RF ( 3, inferior_registers
.r_ebx
);
237 RF ( 4, inferior_registers
.r_esp
);
238 RF ( 5, inferior_registers
.r_ebp
);
239 RF ( 6, inferior_registers
.r_esi
);
240 RF ( 7, inferior_registers
.r_edi
);
241 RF ( 8, inferior_registers
.r_eip
);
242 RF ( 9, inferior_registers
.r_eflags
);
243 RF (10, inferior_registers
.r_cs
);
244 RF (11, inferior_registers
.r_ss
);
245 RF (12, inferior_registers
.r_ds
);
246 RF (13, inferior_registers
.r_es
);
247 RF (14, inferior_registers
.r_fs
);
248 RF (15, inferior_registers
.r_gs
);
250 RF (FP0_REGNUM
, inferior_fpregisters
.regs
[0]);
251 RF (FP0_REGNUM
+ 1, inferior_fpregisters
.regs
[1]);
252 RF (FP0_REGNUM
+ 2, inferior_fpregisters
.regs
[2]);
253 RF (FP0_REGNUM
+ 3, inferior_fpregisters
.regs
[3]);
254 RF (FP0_REGNUM
+ 4, inferior_fpregisters
.regs
[4]);
255 RF (FP0_REGNUM
+ 5, inferior_fpregisters
.regs
[5]);
256 RF (FP0_REGNUM
+ 6, inferior_fpregisters
.regs
[6]);
257 RF (FP0_REGNUM
+ 7, inferior_fpregisters
.regs
[7]);
259 RF (FCTRL_REGNUM
, inferior_fpregisters
.control
);
260 RF (FSTAT_REGNUM
, inferior_fpregisters
.status
);
261 RF (FTAG_REGNUM
, inferior_fpregisters
.tag
);
262 RF (FCS_REGNUM
, inferior_fpregisters
.code_seg
);
263 RF (FCOFF_REGNUM
, inferior_fpregisters
.eip
);
264 RF (FDS_REGNUM
, inferior_fpregisters
.operand_seg
);
265 RF (FDOFF_REGNUM
, inferior_fpregisters
.operand
);
266 RF (FOP_REGNUM
, inferior_fpregisters
.opcode
);
269 /* Store our register values back into the inferior.
270 If REGNO is -1, do this for all registers.
271 Otherwise, REGNO specifies which register (so we can save time). */
274 store_inferior_registers (ignored
)
277 struct reg inferior_registers
;
278 struct env387 inferior_fp_registers
;
280 RS ( 0, inferior_registers
.r_eax
);
281 RS ( 1, inferior_registers
.r_ecx
);
282 RS ( 2, inferior_registers
.r_edx
);
283 RS ( 3, inferior_registers
.r_ebx
);
284 RS ( 4, inferior_registers
.r_esp
);
285 RS ( 5, inferior_registers
.r_ebp
);
286 RS ( 6, inferior_registers
.r_esi
);
287 RS ( 7, inferior_registers
.r_edi
);
288 RS ( 8, inferior_registers
.r_eip
);
289 RS ( 9, inferior_registers
.r_eflags
);
290 RS (10, inferior_registers
.r_cs
);
291 RS (11, inferior_registers
.r_ss
);
292 RS (12, inferior_registers
.r_ds
);
293 RS (13, inferior_registers
.r_es
);
294 RS (14, inferior_registers
.r_fs
);
295 RS (15, inferior_registers
.r_gs
);
297 RS (FP0_REGNUM
, inferior_fpregisters
.regs
[0]);
298 RS (FP0_REGNUM
+ 1, inferior_fpregisters
.regs
[1]);
299 RS (FP0_REGNUM
+ 2, inferior_fpregisters
.regs
[2]);
300 RS (FP0_REGNUM
+ 3, inferior_fpregisters
.regs
[3]);
301 RS (FP0_REGNUM
+ 4, inferior_fpregisters
.regs
[4]);
302 RS (FP0_REGNUM
+ 5, inferior_fpregisters
.regs
[5]);
303 RS (FP0_REGNUM
+ 6, inferior_fpregisters
.regs
[6]);
304 RS (FP0_REGNUM
+ 7, inferior_fpregisters
.regs
[7]);
306 RS (FCTRL_REGNUM
, inferior_fpregisters
.control
);
307 RS (FSTAT_REGNUM
, inferior_fpregisters
.status
);
308 RS (FTAG_REGNUM
, inferior_fpregisters
.tag
);
309 RS (FCS_REGNUM
, inferior_fpregisters
.code_seg
);
310 RS (FCOFF_REGNUM
, inferior_fpregisters
.eip
);
311 RS (FDS_REGNUM
, inferior_fpregisters
.operand_seg
);
312 RS (FDOFF_REGNUM
, inferior_fpregisters
.operand
);
313 RS (FOP_REGNUM
, inferior_fpregisters
.opcode
);
315 ptrace (PT_SETREGS
, inferior_pid
,
316 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0);
317 ptrace (PT_SETFPREGS
, inferior_pid
,
318 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
, 0);
320 #endif /* !__i386__ */
323 /* Fetch one or more registers from the inferior. REGNO == -1 to get
324 them all. We actually fetch more than requested, when convenient,
325 marking them as valid so we won't fetch them again. */
328 fetch_inferior_registers (regno
)
331 struct reg inferior_registers
;
332 struct fpreg inferior_fp_registers
;
335 ptrace (PT_GETREGS
, inferior_pid
,
336 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
337 ptrace (PT_GETFPREGS
, inferior_pid
,
338 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
340 for (i
= 0; i
< 32; i
++)
341 RF (i
, inferior_registers
.fixreg
[i
]);
342 RF (LR_REGNUM
, inferior_registers
.lr
);
343 RF (CR_REGNUM
, inferior_registers
.cr
);
344 RF (XER_REGNUM
, inferior_registers
.xer
);
345 RF (CTR_REGNUM
, inferior_registers
.ctr
);
346 RF (PC_REGNUM
, inferior_registers
.pc
);
348 for (i
= 0; i
< 32; i
++)
349 RF (FP0_REGNUM
+ i
, inferior_fp_registers
.r_regs
[i
]);
352 /* Store our register values back into the inferior.
353 If REGNO is -1, do this for all registers.
354 Otherwise, REGNO specifies which register (so we can save time). */
357 store_inferior_registers (regno
)
360 struct reg inferior_registers
;
361 struct fpreg inferior_fp_registers
;
364 for (i
= 0; i
< 32; i
++)
365 RS (i
, inferior_registers
.fixreg
[i
]);
366 RS (LR_REGNUM
, inferior_registers
.lr
);
367 RS (CR_REGNUM
, inferior_registers
.cr
);
368 RS (XER_REGNUM
, inferior_registers
.xer
);
369 RS (CTR_REGNUM
, inferior_registers
.ctr
);
370 RS (PC_REGNUM
, inferior_registers
.pc
);
372 for (i
= 0; i
< 32; i
++)
373 RS (FP0_REGNUM
+ i
, inferior_fp_registers
.r_regs
[i
]);
375 ptrace (PT_SETREGS
, inferior_pid
,
376 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
377 ptrace (PT_SETFPREGS
, inferior_pid
,
378 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
380 #endif /* !__powerpc__ */
382 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
383 in the NEW_SUN_PTRACE case.
384 It ought to be straightforward. But it appears that writing did
385 not write the data that I specified. I cannot understand where
386 it got the data that it actually did write. */
388 /* Copy LEN bytes from inferior's memory starting at MEMADDR
389 to debugger memory starting at MYADDR. */
391 read_inferior_memory (memaddr
, myaddr
, len
)
397 /* Round starting address down to longword boundary. */
398 register CORE_ADDR addr
= memaddr
& -sizeof (int);
399 /* Round ending address up; get number of longwords that makes. */
401 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
402 /* Allocate buffer of that many longwords. */
403 register int *buffer
= (int *) alloca (count
* sizeof (int));
405 /* Read all the longwords */
406 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
408 buffer
[i
] = ptrace (PT_READ_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
411 /* Copy appropriate bytes out of the buffer. */
412 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
415 /* Copy LEN bytes of data from debugger memory at MYADDR
416 to inferior's memory at MEMADDR.
417 On failure (cannot write the inferior)
418 returns the value of errno. */
421 write_inferior_memory (memaddr
, myaddr
, len
)
427 /* Round starting address down to longword boundary. */
428 register CORE_ADDR addr
= memaddr
& -sizeof (int);
429 /* Round ending address up; get number of longwords that makes. */
431 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
432 /* Allocate buffer of that many longwords. */
433 register int *buffer
= (int *) alloca (count
* sizeof (int));
436 /* Fill start and end extra bytes of buffer with existing memory data. */
438 buffer
[0] = ptrace (PT_READ_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
443 = ptrace (PT_READ_D
, inferior_pid
,
444 (PTRACE_ARG3_TYPE
) addr
+ (count
- 1) * sizeof (int), 0);
447 /* Copy data to be written over corresponding part of buffer */
449 memcpy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
451 /* Write the entire buffer. */
453 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
456 ptrace (PT_WRITE_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);