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__ */
111 initialize_arch (void)
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 (char *program
, char **allargs
)
128 perror_with_name ("fork");
132 ptrace (PT_TRACE_ME
, 0, 0, 0);
134 execv (program
, allargs
);
136 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
137 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
145 /* Kill the inferior process. Make us have no inferior. */
150 if (inferior_pid
== 0)
152 ptrace (PT_KILL
, inferior_pid
, 0, 0);
154 /*************inferior_died ();****VK**************/
157 /* Return nonzero if the given thread is still alive. */
159 mythread_alive (int pid
)
164 /* Wait for process, returns status */
167 mywait (char *status
)
173 if (pid
!= inferior_pid
)
174 perror_with_name ("wait");
178 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
180 return ((unsigned char) WEXITSTATUS (w
));
182 else if (!WIFSTOPPED (w
))
184 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
186 return ((unsigned char) WTERMSIG (w
));
189 fetch_inferior_registers (0);
192 return ((unsigned char) WSTOPSIG (w
));
195 /* Resume execution of the inferior process.
196 If STEP is nonzero, single-step it.
197 If SIGNAL is nonzero, give it that signal. */
200 myresume (int step
, int signal
)
203 ptrace (step
? PT_STEP
: PT_CONTINUE
, inferior_pid
,
204 (PTRACE_ARG3_TYPE
) 1, signal
);
206 perror_with_name ("ptrace");
211 /* Fetch one or more registers from the inferior. REGNO == -1 to get
212 them all. We actually fetch more than requested, when convenient,
213 marking them as valid so we won't fetch them again. */
216 fetch_inferior_registers (int ignored
)
218 struct reg inferior_registers
;
219 struct env387 inferior_fp_registers
;
221 ptrace (PT_GETREGS
, inferior_pid
,
222 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0);
223 ptrace (PT_GETFPREGS
, inferior_pid
,
224 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
, 0);
226 RF ( 0, inferior_registers
.r_eax
);
227 RF ( 1, inferior_registers
.r_ecx
);
228 RF ( 2, inferior_registers
.r_edx
);
229 RF ( 3, inferior_registers
.r_ebx
);
230 RF ( 4, inferior_registers
.r_esp
);
231 RF ( 5, inferior_registers
.r_ebp
);
232 RF ( 6, inferior_registers
.r_esi
);
233 RF ( 7, inferior_registers
.r_edi
);
234 RF ( 8, inferior_registers
.r_eip
);
235 RF ( 9, inferior_registers
.r_eflags
);
236 RF (10, inferior_registers
.r_cs
);
237 RF (11, inferior_registers
.r_ss
);
238 RF (12, inferior_registers
.r_ds
);
239 RF (13, inferior_registers
.r_es
);
240 RF (14, inferior_registers
.r_fs
);
241 RF (15, inferior_registers
.r_gs
);
243 RF (FP0_REGNUM
, inferior_fpregisters
.regs
[0]);
244 RF (FP0_REGNUM
+ 1, inferior_fpregisters
.regs
[1]);
245 RF (FP0_REGNUM
+ 2, inferior_fpregisters
.regs
[2]);
246 RF (FP0_REGNUM
+ 3, inferior_fpregisters
.regs
[3]);
247 RF (FP0_REGNUM
+ 4, inferior_fpregisters
.regs
[4]);
248 RF (FP0_REGNUM
+ 5, inferior_fpregisters
.regs
[5]);
249 RF (FP0_REGNUM
+ 6, inferior_fpregisters
.regs
[6]);
250 RF (FP0_REGNUM
+ 7, inferior_fpregisters
.regs
[7]);
252 RF (FCTRL_REGNUM
, inferior_fpregisters
.control
);
253 RF (FSTAT_REGNUM
, inferior_fpregisters
.status
);
254 RF (FTAG_REGNUM
, inferior_fpregisters
.tag
);
255 RF (FCS_REGNUM
, inferior_fpregisters
.code_seg
);
256 RF (FCOFF_REGNUM
, inferior_fpregisters
.eip
);
257 RF (FDS_REGNUM
, inferior_fpregisters
.operand_seg
);
258 RF (FDOFF_REGNUM
, inferior_fpregisters
.operand
);
259 RF (FOP_REGNUM
, inferior_fpregisters
.opcode
);
262 /* Store our register values back into the inferior.
263 If REGNO is -1, do this for all registers.
264 Otherwise, REGNO specifies which register (so we can save time). */
267 store_inferior_registers (int ignored
)
269 struct reg inferior_registers
;
270 struct env387 inferior_fp_registers
;
272 RS ( 0, inferior_registers
.r_eax
);
273 RS ( 1, inferior_registers
.r_ecx
);
274 RS ( 2, inferior_registers
.r_edx
);
275 RS ( 3, inferior_registers
.r_ebx
);
276 RS ( 4, inferior_registers
.r_esp
);
277 RS ( 5, inferior_registers
.r_ebp
);
278 RS ( 6, inferior_registers
.r_esi
);
279 RS ( 7, inferior_registers
.r_edi
);
280 RS ( 8, inferior_registers
.r_eip
);
281 RS ( 9, inferior_registers
.r_eflags
);
282 RS (10, inferior_registers
.r_cs
);
283 RS (11, inferior_registers
.r_ss
);
284 RS (12, inferior_registers
.r_ds
);
285 RS (13, inferior_registers
.r_es
);
286 RS (14, inferior_registers
.r_fs
);
287 RS (15, inferior_registers
.r_gs
);
289 RS (FP0_REGNUM
, inferior_fpregisters
.regs
[0]);
290 RS (FP0_REGNUM
+ 1, inferior_fpregisters
.regs
[1]);
291 RS (FP0_REGNUM
+ 2, inferior_fpregisters
.regs
[2]);
292 RS (FP0_REGNUM
+ 3, inferior_fpregisters
.regs
[3]);
293 RS (FP0_REGNUM
+ 4, inferior_fpregisters
.regs
[4]);
294 RS (FP0_REGNUM
+ 5, inferior_fpregisters
.regs
[5]);
295 RS (FP0_REGNUM
+ 6, inferior_fpregisters
.regs
[6]);
296 RS (FP0_REGNUM
+ 7, inferior_fpregisters
.regs
[7]);
298 RS (FCTRL_REGNUM
, inferior_fpregisters
.control
);
299 RS (FSTAT_REGNUM
, inferior_fpregisters
.status
);
300 RS (FTAG_REGNUM
, inferior_fpregisters
.tag
);
301 RS (FCS_REGNUM
, inferior_fpregisters
.code_seg
);
302 RS (FCOFF_REGNUM
, inferior_fpregisters
.eip
);
303 RS (FDS_REGNUM
, inferior_fpregisters
.operand_seg
);
304 RS (FDOFF_REGNUM
, inferior_fpregisters
.operand
);
305 RS (FOP_REGNUM
, inferior_fpregisters
.opcode
);
307 ptrace (PT_SETREGS
, inferior_pid
,
308 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0);
309 ptrace (PT_SETFPREGS
, inferior_pid
,
310 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
, 0);
312 #endif /* !__i386__ */
315 /* Fetch one or more registers from the inferior. REGNO == -1 to get
316 them all. We actually fetch more than requested, when convenient,
317 marking them as valid so we won't fetch them again. */
320 fetch_inferior_registers (int regno
)
322 struct reg inferior_registers
;
323 struct fpreg inferior_fp_registers
;
326 ptrace (PT_GETREGS
, inferior_pid
,
327 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
328 ptrace (PT_GETFPREGS
, inferior_pid
,
329 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
331 for (i
= 0; i
< 32; i
++)
332 RF (i
, inferior_registers
.fixreg
[i
]);
333 RF (LR_REGNUM
, inferior_registers
.lr
);
334 RF (CR_REGNUM
, inferior_registers
.cr
);
335 RF (XER_REGNUM
, inferior_registers
.xer
);
336 RF (CTR_REGNUM
, inferior_registers
.ctr
);
337 RF (PC_REGNUM
, inferior_registers
.pc
);
339 for (i
= 0; i
< 32; i
++)
340 RF (FP0_REGNUM
+ i
, inferior_fp_registers
.r_regs
[i
]);
343 /* Store our register values back into the inferior.
344 If REGNO is -1, do this for all registers.
345 Otherwise, REGNO specifies which register (so we can save time). */
348 store_inferior_registers (int regno
)
350 struct reg inferior_registers
;
351 struct fpreg inferior_fp_registers
;
354 for (i
= 0; i
< 32; i
++)
355 RS (i
, inferior_registers
.fixreg
[i
]);
356 RS (LR_REGNUM
, inferior_registers
.lr
);
357 RS (CR_REGNUM
, inferior_registers
.cr
);
358 RS (XER_REGNUM
, inferior_registers
.xer
);
359 RS (CTR_REGNUM
, inferior_registers
.ctr
);
360 RS (PC_REGNUM
, inferior_registers
.pc
);
362 for (i
= 0; i
< 32; i
++)
363 RS (FP0_REGNUM
+ i
, inferior_fp_registers
.r_regs
[i
]);
365 ptrace (PT_SETREGS
, inferior_pid
,
366 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
367 ptrace (PT_SETFPREGS
, inferior_pid
,
368 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
370 #endif /* !__powerpc__ */
372 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
373 in the NEW_SUN_PTRACE case.
374 It ought to be straightforward. But it appears that writing did
375 not write the data that I specified. I cannot understand where
376 it got the data that it actually did write. */
378 /* Copy LEN bytes from inferior's memory starting at MEMADDR
379 to debugger memory starting at MYADDR. */
381 read_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
384 /* Round starting address down to longword boundary. */
385 register CORE_ADDR addr
= memaddr
& -sizeof (int);
386 /* Round ending address up; get number of longwords that makes. */
388 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
389 /* Allocate buffer of that many longwords. */
390 register int *buffer
= (int *) alloca (count
* sizeof (int));
392 /* Read all the longwords */
393 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
395 buffer
[i
] = ptrace (PT_READ_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
398 /* Copy appropriate bytes out of the buffer. */
399 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
402 /* Copy LEN bytes of data from debugger memory at MYADDR
403 to inferior's memory at MEMADDR.
404 On failure (cannot write the inferior)
405 returns the value of errno. */
408 write_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
411 /* Round starting address down to longword boundary. */
412 register CORE_ADDR addr
= memaddr
& -sizeof (int);
413 /* Round ending address up; get number of longwords that makes. */
415 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
416 /* Allocate buffer of that many longwords. */
417 register int *buffer
= (int *) alloca (count
* sizeof (int));
420 /* Fill start and end extra bytes of buffer with existing memory data. */
422 buffer
[0] = ptrace (PT_READ_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
427 = ptrace (PT_READ_D
, inferior_pid
,
428 (PTRACE_ARG3_TYPE
) addr
+ (count
- 1) * sizeof (int), 0);
431 /* Copy data to be written over corresponding part of buffer */
433 memcpy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
435 /* Write the entire buffer. */
437 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
440 ptrace (PT_WRITE_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
449 initialize_low (void)