1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright 1986, 1987, 1993, 2000, 2001 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*********************/
30 static char my_registers
[REGISTER_BYTES
];
31 char *registers
= my_registers
;
32 /***************End MY defs*********************/
34 #include <sys/ptrace.h>
35 #include <machine/reg.h>
37 #define RF(dst, src) \
38 memcpy(®isters[REGISTER_BYTE(dst)], &src, sizeof(src))
40 #define RS(src, dst) \
41 memcpy(&dst, ®isters[REGISTER_BYTE(src)], sizeof(dst))
46 unsigned short control
;
48 unsigned short status
;
53 unsigned short code_seg
;
54 unsigned short opcode
;
55 unsigned long operand
;
56 unsigned short operand_seg
;
58 unsigned char regs
[8][10];
61 /* i386_register_raw_size[i] is the number of bytes of storage in the
62 actual machine representation for register i. */
63 int i386_register_raw_size
[MAX_NUM_REGS
] = {
77 int i386_register_byte
[MAX_NUM_REGS
];
80 initialize_arch (void)
82 /* Initialize the table saying where each register starts in the
88 for (i
= 0; i
< MAX_NUM_REGS
; i
++)
90 i386_register_byte
[i
] = offset
;
91 offset
+= i386_register_raw_size
[i
];
95 #endif /* !__i386__ */
99 initialize_arch (void)
102 #endif /* !__m68k__ */
106 initialize_arch (void)
109 #endif /* !__ns32k__ */
112 #include "ppc-tdep.h"
115 initialize_arch (void)
118 #endif /* !__powerpc__ */
121 /* Start an inferior process and returns its pid.
122 ALLARGS is a vector of program-name and args. */
125 create_inferior (char *program
, char **allargs
)
131 perror_with_name ("fork");
135 ptrace (PT_TRACE_ME
, 0, 0, 0);
137 execv (program
, allargs
);
139 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
140 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
148 /* Kill the inferior process. Make us have no inferior. */
153 if (inferior_pid
== 0)
155 ptrace (PT_KILL
, inferior_pid
, 0, 0);
157 /*************inferior_died ();****VK**************/
160 /* Return nonzero if the given thread is still alive. */
162 mythread_alive (int pid
)
167 /* Wait for process, returns status */
170 mywait (char *status
)
176 pid
= waitpid (inferior_pid
, &w
, 0);
178 if (pid
!= inferior_pid
)
179 perror_with_name ("wait");
183 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
185 return ((unsigned char) WEXITSTATUS (w
));
187 else if (!WIFSTOPPED (w
))
189 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
191 return ((unsigned char) WTERMSIG (w
));
194 fetch_inferior_registers (0);
197 return ((unsigned char) WSTOPSIG (w
));
200 /* Resume execution of the inferior process.
201 If STEP is nonzero, single-step it.
202 If SIGNAL is nonzero, give it that signal. */
205 myresume (int step
, int signal
)
208 ptrace (step
? PT_STEP
: PT_CONTINUE
, inferior_pid
,
209 (PTRACE_ARG3_TYPE
) 1, signal
);
211 perror_with_name ("ptrace");
216 /* Fetch one or more registers from the inferior. REGNO == -1 to get
217 them all. We actually fetch more than requested, when convenient,
218 marking them as valid so we won't fetch them again. */
221 fetch_inferior_registers (int ignored
)
223 struct reg inferior_registers
;
224 struct env387 inferior_fp_registers
;
226 ptrace (PT_GETREGS
, inferior_pid
,
227 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0);
228 ptrace (PT_GETFPREGS
, inferior_pid
,
229 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
, 0);
231 RF ( 0, inferior_registers
.r_eax
);
232 RF ( 1, inferior_registers
.r_ecx
);
233 RF ( 2, inferior_registers
.r_edx
);
234 RF ( 3, inferior_registers
.r_ebx
);
235 RF ( 4, inferior_registers
.r_esp
);
236 RF ( 5, inferior_registers
.r_ebp
);
237 RF ( 6, inferior_registers
.r_esi
);
238 RF ( 7, inferior_registers
.r_edi
);
239 RF ( 8, inferior_registers
.r_eip
);
240 RF ( 9, inferior_registers
.r_eflags
);
241 RF (10, inferior_registers
.r_cs
);
242 RF (11, inferior_registers
.r_ss
);
243 RF (12, inferior_registers
.r_ds
);
244 RF (13, inferior_registers
.r_es
);
245 RF (14, inferior_registers
.r_fs
);
246 RF (15, inferior_registers
.r_gs
);
248 RF (FP0_REGNUM
, inferior_fp_registers
.regs
[0]);
249 RF (FP0_REGNUM
+ 1, inferior_fp_registers
.regs
[1]);
250 RF (FP0_REGNUM
+ 2, inferior_fp_registers
.regs
[2]);
251 RF (FP0_REGNUM
+ 3, inferior_fp_registers
.regs
[3]);
252 RF (FP0_REGNUM
+ 4, inferior_fp_registers
.regs
[4]);
253 RF (FP0_REGNUM
+ 5, inferior_fp_registers
.regs
[5]);
254 RF (FP0_REGNUM
+ 6, inferior_fp_registers
.regs
[6]);
255 RF (FP0_REGNUM
+ 7, inferior_fp_registers
.regs
[7]);
257 RF (FCTRL_REGNUM
, inferior_fp_registers
.control
);
258 RF (FSTAT_REGNUM
, inferior_fp_registers
.status
);
259 RF (FTAG_REGNUM
, inferior_fp_registers
.tag
);
260 RF (FCS_REGNUM
, inferior_fp_registers
.code_seg
);
261 RF (FCOFF_REGNUM
, inferior_fp_registers
.eip
);
262 RF (FDS_REGNUM
, inferior_fp_registers
.operand_seg
);
263 RF (FDOFF_REGNUM
, inferior_fp_registers
.operand
);
264 RF (FOP_REGNUM
, inferior_fp_registers
.opcode
);
267 /* Store our register values back into the inferior.
268 If REGNO is -1, do this for all registers.
269 Otherwise, REGNO specifies which register (so we can save time). */
272 store_inferior_registers (int ignored
)
274 struct reg inferior_registers
;
275 struct env387 inferior_fp_registers
;
277 RS ( 0, inferior_registers
.r_eax
);
278 RS ( 1, inferior_registers
.r_ecx
);
279 RS ( 2, inferior_registers
.r_edx
);
280 RS ( 3, inferior_registers
.r_ebx
);
281 RS ( 4, inferior_registers
.r_esp
);
282 RS ( 5, inferior_registers
.r_ebp
);
283 RS ( 6, inferior_registers
.r_esi
);
284 RS ( 7, inferior_registers
.r_edi
);
285 RS ( 8, inferior_registers
.r_eip
);
286 RS ( 9, inferior_registers
.r_eflags
);
287 RS (10, inferior_registers
.r_cs
);
288 RS (11, inferior_registers
.r_ss
);
289 RS (12, inferior_registers
.r_ds
);
290 RS (13, inferior_registers
.r_es
);
291 RS (14, inferior_registers
.r_fs
);
292 RS (15, inferior_registers
.r_gs
);
294 RS (FP0_REGNUM
, inferior_fp_registers
.regs
[0]);
295 RS (FP0_REGNUM
+ 1, inferior_fp_registers
.regs
[1]);
296 RS (FP0_REGNUM
+ 2, inferior_fp_registers
.regs
[2]);
297 RS (FP0_REGNUM
+ 3, inferior_fp_registers
.regs
[3]);
298 RS (FP0_REGNUM
+ 4, inferior_fp_registers
.regs
[4]);
299 RS (FP0_REGNUM
+ 5, inferior_fp_registers
.regs
[5]);
300 RS (FP0_REGNUM
+ 6, inferior_fp_registers
.regs
[6]);
301 RS (FP0_REGNUM
+ 7, inferior_fp_registers
.regs
[7]);
303 RS (FCTRL_REGNUM
, inferior_fp_registers
.control
);
304 RS (FSTAT_REGNUM
, inferior_fp_registers
.status
);
305 RS (FTAG_REGNUM
, inferior_fp_registers
.tag
);
306 RS (FCS_REGNUM
, inferior_fp_registers
.code_seg
);
307 RS (FCOFF_REGNUM
, inferior_fp_registers
.eip
);
308 RS (FDS_REGNUM
, inferior_fp_registers
.operand_seg
);
309 RS (FDOFF_REGNUM
, inferior_fp_registers
.operand
);
310 RS (FOP_REGNUM
, inferior_fp_registers
.opcode
);
312 ptrace (PT_SETREGS
, inferior_pid
,
313 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0);
314 ptrace (PT_SETFPREGS
, inferior_pid
,
315 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
, 0);
317 #endif /* !__i386__ */
320 /* Fetch one or more registers from the inferior. REGNO == -1 to get
321 them all. We actually fetch more than requested, when convenient,
322 marking them as valid so we won't fetch them again. */
325 fetch_inferior_registers (int regno
)
327 struct reg inferior_registers
;
328 struct fpreg inferior_fp_registers
;
330 ptrace (PT_GETREGS
, inferior_pid
,
331 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
332 memcpy (®isters
[REGISTER_BYTE (0)], &inferior_registers
,
333 sizeof (inferior_registers
));
335 ptrace (PT_GETFPREGS
, inferior_pid
,
336 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
337 memcpy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], &inferior_fp_registers
,
338 sizeof (inferior_fp_registers
));
341 /* Store our register values back into the inferior.
342 If REGNO is -1, do this for all registers.
343 Otherwise, REGNO specifies which register (so we can save time). */
346 store_inferior_registers (int regno
)
348 struct reg inferior_registers
;
349 struct fpreg inferior_fp_registers
;
351 memcpy (&inferior_registers
, ®isters
[REGISTER_BYTE (0)],
352 sizeof (inferior_registers
));
353 ptrace (PT_SETREGS
, inferior_pid
,
354 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
356 memcpy (&inferior_fp_registers
, ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
357 sizeof (inferior_fp_registers
));
358 ptrace (PT_SETFPREGS
, inferior_pid
,
359 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
361 #endif /* !__m68k__ */
365 /* Fetch one or more registers from the inferior. REGNO == -1 to get
366 them all. We actually fetch more than requested, when convenient,
367 marking them as valid so we won't fetch them again. */
370 fetch_inferior_registers (int regno
)
372 struct reg inferior_registers
;
373 struct fpreg inferior_fpregisters
;
375 ptrace (PT_GETREGS
, inferior_pid
,
376 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
377 ptrace (PT_GETFPREGS
, inferior_pid
,
378 (PTRACE_ARG3_TYPE
) & inferior_fpregisters
, 0);
380 RF (R0_REGNUM
+ 0, inferior_registers
.r_r0
);
381 RF (R0_REGNUM
+ 1, inferior_registers
.r_r1
);
382 RF (R0_REGNUM
+ 2, inferior_registers
.r_r2
);
383 RF (R0_REGNUM
+ 3, inferior_registers
.r_r3
);
384 RF (R0_REGNUM
+ 4, inferior_registers
.r_r4
);
385 RF (R0_REGNUM
+ 5, inferior_registers
.r_r5
);
386 RF (R0_REGNUM
+ 6, inferior_registers
.r_r6
);
387 RF (R0_REGNUM
+ 7, inferior_registers
.r_r7
);
389 RF (SP_REGNUM
, inferior_registers
.r_sp
);
390 RF (FP_REGNUM
, inferior_registers
.r_fp
);
391 RF (PC_REGNUM
, inferior_registers
.r_pc
);
392 RF (PS_REGNUM
, inferior_registers
.r_psr
);
394 RF (FPS_REGNUM
, inferior_fpregisters
.r_fsr
);
395 RF (FP0_REGNUM
+ 0, inferior_fpregisters
.r_freg
[0]);
396 RF (FP0_REGNUM
+ 2, inferior_fpregisters
.r_freg
[2]);
397 RF (FP0_REGNUM
+ 4, inferior_fpregisters
.r_freg
[4]);
398 RF (FP0_REGNUM
+ 6, inferior_fpregisters
.r_freg
[6]);
399 RF (LP0_REGNUM
+ 1, inferior_fpregisters
.r_freg
[1]);
400 RF (LP0_REGNUM
+ 3, inferior_fpregisters
.r_freg
[3]);
401 RF (LP0_REGNUM
+ 5, inferior_fpregisters
.r_freg
[5]);
402 RF (LP0_REGNUM
+ 7, inferior_fpregisters
.r_freg
[7]);
405 /* Store our register values back into the inferior.
406 If REGNO is -1, do this for all registers.
407 Otherwise, REGNO specifies which register (so we can save time). */
410 store_inferior_registers (int regno
)
412 struct reg inferior_registers
;
413 struct fpreg inferior_fpregisters
;
415 RS (R0_REGNUM
+ 0, inferior_registers
.r_r0
);
416 RS (R0_REGNUM
+ 1, inferior_registers
.r_r1
);
417 RS (R0_REGNUM
+ 2, inferior_registers
.r_r2
);
418 RS (R0_REGNUM
+ 3, inferior_registers
.r_r3
);
419 RS (R0_REGNUM
+ 4, inferior_registers
.r_r4
);
420 RS (R0_REGNUM
+ 5, inferior_registers
.r_r5
);
421 RS (R0_REGNUM
+ 6, inferior_registers
.r_r6
);
422 RS (R0_REGNUM
+ 7, inferior_registers
.r_r7
);
424 RS (SP_REGNUM
, inferior_registers
.r_sp
);
425 RS (FP_REGNUM
, inferior_registers
.r_fp
);
426 RS (PC_REGNUM
, inferior_registers
.r_pc
);
427 RS (PS_REGNUM
, inferior_registers
.r_psr
);
429 RS (FPS_REGNUM
, inferior_fpregisters
.r_fsr
);
430 RS (FP0_REGNUM
+ 0, inferior_fpregisters
.r_freg
[0]);
431 RS (FP0_REGNUM
+ 2, inferior_fpregisters
.r_freg
[2]);
432 RS (FP0_REGNUM
+ 4, inferior_fpregisters
.r_freg
[4]);
433 RS (FP0_REGNUM
+ 6, inferior_fpregisters
.r_freg
[6]);
434 RS (LP0_REGNUM
+ 1, inferior_fpregisters
.r_freg
[1]);
435 RS (LP0_REGNUM
+ 3, inferior_fpregisters
.r_freg
[3]);
436 RS (LP0_REGNUM
+ 5, inferior_fpregisters
.r_freg
[5]);
437 RS (LP0_REGNUM
+ 7, inferior_fpregisters
.r_freg
[7]);
439 ptrace (PT_SETREGS
, inferior_pid
,
440 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
441 ptrace (PT_SETFPREGS
, inferior_pid
,
442 (PTRACE_ARG3_TYPE
) & inferior_fpregisters
, 0);
445 #endif /* !__ns32k__ */
448 /* Fetch one or more registers from the inferior. REGNO == -1 to get
449 them all. We actually fetch more than requested, when convenient,
450 marking them as valid so we won't fetch them again. */
453 fetch_inferior_registers (int regno
)
455 struct reg inferior_registers
;
457 struct fpreg inferior_fp_registers
;
461 ptrace (PT_GETREGS
, inferior_pid
,
462 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
463 for (i
= 0; i
< 32; i
++)
464 RF (i
, inferior_registers
.fixreg
[i
]);
465 RF (PPC_LR_REGNUM
, inferior_registers
.lr
);
466 RF (PPC_CR_REGNUM
, inferior_registers
.cr
);
467 RF (PPC_XER_REGNUM
, inferior_registers
.xer
);
468 RF (PPC_CTR_REGNUM
, inferior_registers
.ctr
);
469 RF (PC_REGNUM
, inferior_registers
.pc
);
472 ptrace (PT_GETFPREGS
, inferior_pid
,
473 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
474 for (i
= 0; i
< 32; i
++)
475 RF (FP0_REGNUM
+ i
, inferior_fp_registers
.r_regs
[i
]);
479 /* Store our register values back into the inferior.
480 If REGNO is -1, do this for all registers.
481 Otherwise, REGNO specifies which register (so we can save time). */
484 store_inferior_registers (int regno
)
486 struct reg inferior_registers
;
488 struct fpreg inferior_fp_registers
;
492 for (i
= 0; i
< 32; i
++)
493 RS (i
, inferior_registers
.fixreg
[i
]);
494 RS (PPC_LR_REGNUM
, inferior_registers
.lr
);
495 RS (PPC_CR_REGNUM
, inferior_registers
.cr
);
496 RS (PPC_XER_REGNUM
, inferior_registers
.xer
);
497 RS (PPC_CTR_REGNUM
, inferior_registers
.ctr
);
498 RS (PC_REGNUM
, inferior_registers
.pc
);
499 ptrace (PT_SETREGS
, inferior_pid
,
500 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
503 for (i
= 0; i
< 32; i
++)
504 RS (FP0_REGNUM
+ i
, inferior_fp_registers
.r_regs
[i
]);
505 ptrace (PT_SETFPREGS
, inferior_pid
,
506 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
509 #endif /* !__powerpc__ */
511 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
512 in the NEW_SUN_PTRACE case.
513 It ought to be straightforward. But it appears that writing did
514 not write the data that I specified. I cannot understand where
515 it got the data that it actually did write. */
517 /* Copy LEN bytes from inferior's memory starting at MEMADDR
518 to debugger memory starting at MYADDR. */
521 read_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
524 /* Round starting address down to longword boundary. */
525 register CORE_ADDR addr
= memaddr
& -sizeof (int);
526 /* Round ending address up; get number of longwords that makes. */
528 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
529 /* Allocate buffer of that many longwords. */
530 register int *buffer
= (int *) alloca (count
* sizeof (int));
532 /* Read all the longwords */
533 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
535 buffer
[i
] = ptrace (PT_READ_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
538 /* Copy appropriate bytes out of the buffer. */
539 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
542 /* Copy LEN bytes of data from debugger memory at MYADDR
543 to inferior's memory at MEMADDR.
544 On failure (cannot write the inferior)
545 returns the value of errno. */
548 write_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
551 /* Round starting address down to longword boundary. */
552 register CORE_ADDR addr
= memaddr
& -sizeof (int);
553 /* Round ending address up; get number of longwords that makes. */
555 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
556 /* Allocate buffer of that many longwords. */
557 register int *buffer
= (int *) alloca (count
* sizeof (int));
560 /* Fill start and end extra bytes of buffer with existing memory data. */
562 buffer
[0] = ptrace (PT_READ_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
567 = ptrace (PT_READ_D
, inferior_pid
,
568 (PTRACE_ARG3_TYPE
) addr
+ (count
- 1) * sizeof (int), 0);
571 /* Copy data to be written over corresponding part of buffer */
573 memcpy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
575 /* Write the entire buffer. */
577 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
580 ptrace (PT_WRITE_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
589 initialize_low (void)