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
];
92 initialize_arch (void)
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__ */
110 #include "ppc-tdep.h"
113 initialize_arch (void)
116 #endif /* !__powerpc__ */
119 /* Start an inferior process and returns its pid.
120 ALLARGS is a vector of program-name and args.
121 ENV is the environment vector to pass. */
124 create_inferior (char *program
, char **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. */
161 mythread_alive (int pid
)
166 /* Wait for process, returns status */
169 mywait (char *status
)
175 if (pid
!= inferior_pid
)
176 perror_with_name ("wait");
180 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
182 return ((unsigned char) WEXITSTATUS (w
));
184 else if (!WIFSTOPPED (w
))
186 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
188 return ((unsigned char) WTERMSIG (w
));
191 fetch_inferior_registers (0);
194 return ((unsigned char) WSTOPSIG (w
));
197 /* Resume execution of the inferior process.
198 If STEP is nonzero, single-step it.
199 If SIGNAL is nonzero, give it that signal. */
202 myresume (int step
, int signal
)
205 ptrace (step
? PT_STEP
: PT_CONTINUE
, inferior_pid
,
206 (PTRACE_ARG3_TYPE
) 1, signal
);
208 perror_with_name ("ptrace");
213 /* Fetch one or more registers from the inferior. REGNO == -1 to get
214 them all. We actually fetch more than requested, when convenient,
215 marking them as valid so we won't fetch them again. */
218 fetch_inferior_registers (int ignored
)
220 struct reg inferior_registers
;
221 struct env387 inferior_fp_registers
;
223 ptrace (PT_GETREGS
, inferior_pid
,
224 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0);
225 ptrace (PT_GETFPREGS
, inferior_pid
,
226 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
, 0);
228 RF ( 0, inferior_registers
.r_eax
);
229 RF ( 1, inferior_registers
.r_ecx
);
230 RF ( 2, inferior_registers
.r_edx
);
231 RF ( 3, inferior_registers
.r_ebx
);
232 RF ( 4, inferior_registers
.r_esp
);
233 RF ( 5, inferior_registers
.r_ebp
);
234 RF ( 6, inferior_registers
.r_esi
);
235 RF ( 7, inferior_registers
.r_edi
);
236 RF ( 8, inferior_registers
.r_eip
);
237 RF ( 9, inferior_registers
.r_eflags
);
238 RF (10, inferior_registers
.r_cs
);
239 RF (11, inferior_registers
.r_ss
);
240 RF (12, inferior_registers
.r_ds
);
241 RF (13, inferior_registers
.r_es
);
242 RF (14, inferior_registers
.r_fs
);
243 RF (15, inferior_registers
.r_gs
);
245 RF (FP0_REGNUM
, inferior_fp_registers
.regs
[0]);
246 RF (FP0_REGNUM
+ 1, inferior_fp_registers
.regs
[1]);
247 RF (FP0_REGNUM
+ 2, inferior_fp_registers
.regs
[2]);
248 RF (FP0_REGNUM
+ 3, inferior_fp_registers
.regs
[3]);
249 RF (FP0_REGNUM
+ 4, inferior_fp_registers
.regs
[4]);
250 RF (FP0_REGNUM
+ 5, inferior_fp_registers
.regs
[5]);
251 RF (FP0_REGNUM
+ 6, inferior_fp_registers
.regs
[6]);
252 RF (FP0_REGNUM
+ 7, inferior_fp_registers
.regs
[7]);
254 RF (FCTRL_REGNUM
, inferior_fp_registers
.control
);
255 RF (FSTAT_REGNUM
, inferior_fp_registers
.status
);
256 RF (FTAG_REGNUM
, inferior_fp_registers
.tag
);
257 RF (FCS_REGNUM
, inferior_fp_registers
.code_seg
);
258 RF (FCOFF_REGNUM
, inferior_fp_registers
.eip
);
259 RF (FDS_REGNUM
, inferior_fp_registers
.operand_seg
);
260 RF (FDOFF_REGNUM
, inferior_fp_registers
.operand
);
261 RF (FOP_REGNUM
, inferior_fp_registers
.opcode
);
264 /* Store our register values back into the inferior.
265 If REGNO is -1, do this for all registers.
266 Otherwise, REGNO specifies which register (so we can save time). */
269 store_inferior_registers (int ignored
)
271 struct reg inferior_registers
;
272 struct env387 inferior_fp_registers
;
274 RS ( 0, inferior_registers
.r_eax
);
275 RS ( 1, inferior_registers
.r_ecx
);
276 RS ( 2, inferior_registers
.r_edx
);
277 RS ( 3, inferior_registers
.r_ebx
);
278 RS ( 4, inferior_registers
.r_esp
);
279 RS ( 5, inferior_registers
.r_ebp
);
280 RS ( 6, inferior_registers
.r_esi
);
281 RS ( 7, inferior_registers
.r_edi
);
282 RS ( 8, inferior_registers
.r_eip
);
283 RS ( 9, inferior_registers
.r_eflags
);
284 RS (10, inferior_registers
.r_cs
);
285 RS (11, inferior_registers
.r_ss
);
286 RS (12, inferior_registers
.r_ds
);
287 RS (13, inferior_registers
.r_es
);
288 RS (14, inferior_registers
.r_fs
);
289 RS (15, inferior_registers
.r_gs
);
291 RS (FP0_REGNUM
, inferior_fp_registers
.regs
[0]);
292 RS (FP0_REGNUM
+ 1, inferior_fp_registers
.regs
[1]);
293 RS (FP0_REGNUM
+ 2, inferior_fp_registers
.regs
[2]);
294 RS (FP0_REGNUM
+ 3, inferior_fp_registers
.regs
[3]);
295 RS (FP0_REGNUM
+ 4, inferior_fp_registers
.regs
[4]);
296 RS (FP0_REGNUM
+ 5, inferior_fp_registers
.regs
[5]);
297 RS (FP0_REGNUM
+ 6, inferior_fp_registers
.regs
[6]);
298 RS (FP0_REGNUM
+ 7, inferior_fp_registers
.regs
[7]);
300 RS (FCTRL_REGNUM
, inferior_fp_registers
.control
);
301 RS (FSTAT_REGNUM
, inferior_fp_registers
.status
);
302 RS (FTAG_REGNUM
, inferior_fp_registers
.tag
);
303 RS (FCS_REGNUM
, inferior_fp_registers
.code_seg
);
304 RS (FCOFF_REGNUM
, inferior_fp_registers
.eip
);
305 RS (FDS_REGNUM
, inferior_fp_registers
.operand_seg
);
306 RS (FDOFF_REGNUM
, inferior_fp_registers
.operand
);
307 RS (FOP_REGNUM
, inferior_fp_registers
.opcode
);
309 ptrace (PT_SETREGS
, inferior_pid
,
310 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0);
311 ptrace (PT_SETFPREGS
, inferior_pid
,
312 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
, 0);
314 #endif /* !__i386__ */
317 /* Fetch one or more registers from the inferior. REGNO == -1 to get
318 them all. We actually fetch more than requested, when convenient,
319 marking them as valid so we won't fetch them again. */
322 fetch_inferior_registers (int regno
)
324 struct reg inferior_registers
;
326 struct fpreg inferior_fp_registers
;
330 ptrace (PT_GETREGS
, inferior_pid
,
331 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
332 for (i
= 0; i
< 32; i
++)
333 RF (i
, inferior_registers
.fixreg
[i
]);
334 RF (PPC_LR_REGNUM
, inferior_registers
.lr
);
335 RF (PPC_CR_REGNUM
, inferior_registers
.cr
);
336 RF (PPC_XER_REGNUM
, inferior_registers
.xer
);
337 RF (PPC_CTR_REGNUM
, inferior_registers
.ctr
);
338 RF (PC_REGNUM
, inferior_registers
.pc
);
341 ptrace (PT_GETFPREGS
, inferior_pid
,
342 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
343 for (i
= 0; i
< 32; i
++)
344 RF (FP0_REGNUM
+ i
, inferior_fp_registers
.r_regs
[i
]);
348 /* Store our register values back into the inferior.
349 If REGNO is -1, do this for all registers.
350 Otherwise, REGNO specifies which register (so we can save time). */
353 store_inferior_registers (int regno
)
355 struct reg inferior_registers
;
357 struct fpreg inferior_fp_registers
;
361 for (i
= 0; i
< 32; i
++)
362 RS (i
, inferior_registers
.fixreg
[i
]);
363 RS (PPC_LR_REGNUM
, inferior_registers
.lr
);
364 RS (PPC_CR_REGNUM
, inferior_registers
.cr
);
365 RS (PPC_XER_REGNUM
, inferior_registers
.xer
);
366 RS (PPC_CTR_REGNUM
, inferior_registers
.ctr
);
367 RS (PC_REGNUM
, inferior_registers
.pc
);
368 ptrace (PT_SETREGS
, inferior_pid
,
369 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
372 for (i
= 0; i
< 32; i
++)
373 RS (FP0_REGNUM
+ i
, inferior_fp_registers
.r_regs
[i
]);
374 ptrace (PT_SETFPREGS
, inferior_pid
,
375 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
378 #endif /* !__powerpc__ */
380 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
381 in the NEW_SUN_PTRACE case.
382 It ought to be straightforward. But it appears that writing did
383 not write the data that I specified. I cannot understand where
384 it got the data that it actually did write. */
386 /* Copy LEN bytes from inferior's memory starting at MEMADDR
387 to debugger memory starting at MYADDR. */
389 read_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
392 /* Round starting address down to longword boundary. */
393 register CORE_ADDR addr
= memaddr
& -sizeof (int);
394 /* Round ending address up; get number of longwords that makes. */
396 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
397 /* Allocate buffer of that many longwords. */
398 register int *buffer
= (int *) alloca (count
* sizeof (int));
400 /* Read all the longwords */
401 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
403 buffer
[i
] = ptrace (PT_READ_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
406 /* Copy appropriate bytes out of the buffer. */
407 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
410 /* Copy LEN bytes of data from debugger memory at MYADDR
411 to inferior's memory at MEMADDR.
412 On failure (cannot write the inferior)
413 returns the value of errno. */
416 write_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
419 /* Round starting address down to longword boundary. */
420 register CORE_ADDR addr
= memaddr
& -sizeof (int);
421 /* Round ending address up; get number of longwords that makes. */
423 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
424 /* Allocate buffer of that many longwords. */
425 register int *buffer
= (int *) alloca (count
* sizeof (int));
428 /* Fill start and end extra bytes of buffer with existing memory data. */
430 buffer
[0] = ptrace (PT_READ_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
435 = ptrace (PT_READ_D
, inferior_pid
,
436 (PTRACE_ARG3_TYPE
) addr
+ (count
- 1) * sizeof (int), 0);
439 /* Copy data to be written over corresponding part of buffer */
441 memcpy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
443 /* Write the entire buffer. */
445 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
448 ptrace (PT_WRITE_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
457 initialize_low (void)