1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright 1986, 1987, 1993, 2000, 2001, 2002 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 /* Attaching is not supported. */
155 /* Kill the inferior process. Make us have no inferior. */
160 if (inferior_pid
== 0)
162 ptrace (PT_KILL
, inferior_pid
, 0, 0);
164 /*************inferior_died ();****VK**************/
167 /* Return nonzero if the given thread is still alive. */
169 mythread_alive (int pid
)
174 /* Wait for process, returns status */
177 mywait (char *status
)
183 pid
= waitpid (inferior_pid
, &w
, 0);
185 if (pid
!= inferior_pid
)
186 perror_with_name ("wait");
190 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
192 return ((unsigned char) WEXITSTATUS (w
));
194 else if (!WIFSTOPPED (w
))
196 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
198 return ((unsigned char) WTERMSIG (w
));
201 fetch_inferior_registers (0);
204 return ((unsigned char) WSTOPSIG (w
));
207 /* Resume execution of the inferior process.
208 If STEP is nonzero, single-step it.
209 If SIGNAL is nonzero, give it that signal. */
212 myresume (int step
, int signal
)
215 ptrace (step
? PT_STEP
: PT_CONTINUE
, inferior_pid
,
216 (PTRACE_ARG3_TYPE
) 1, signal
);
218 perror_with_name ("ptrace");
223 /* Fetch one or more registers from the inferior. REGNO == -1 to get
224 them all. We actually fetch more than requested, when convenient,
225 marking them as valid so we won't fetch them again. */
228 fetch_inferior_registers (int ignored
)
230 struct reg inferior_registers
;
231 struct env387 inferior_fp_registers
;
233 ptrace (PT_GETREGS
, inferior_pid
,
234 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0);
235 ptrace (PT_GETFPREGS
, inferior_pid
,
236 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
, 0);
238 RF ( 0, inferior_registers
.r_eax
);
239 RF ( 1, inferior_registers
.r_ecx
);
240 RF ( 2, inferior_registers
.r_edx
);
241 RF ( 3, inferior_registers
.r_ebx
);
242 RF ( 4, inferior_registers
.r_esp
);
243 RF ( 5, inferior_registers
.r_ebp
);
244 RF ( 6, inferior_registers
.r_esi
);
245 RF ( 7, inferior_registers
.r_edi
);
246 RF ( 8, inferior_registers
.r_eip
);
247 RF ( 9, inferior_registers
.r_eflags
);
248 RF (10, inferior_registers
.r_cs
);
249 RF (11, inferior_registers
.r_ss
);
250 RF (12, inferior_registers
.r_ds
);
251 RF (13, inferior_registers
.r_es
);
252 RF (14, inferior_registers
.r_fs
);
253 RF (15, inferior_registers
.r_gs
);
255 RF (FP0_REGNUM
, inferior_fp_registers
.regs
[0]);
256 RF (FP0_REGNUM
+ 1, inferior_fp_registers
.regs
[1]);
257 RF (FP0_REGNUM
+ 2, inferior_fp_registers
.regs
[2]);
258 RF (FP0_REGNUM
+ 3, inferior_fp_registers
.regs
[3]);
259 RF (FP0_REGNUM
+ 4, inferior_fp_registers
.regs
[4]);
260 RF (FP0_REGNUM
+ 5, inferior_fp_registers
.regs
[5]);
261 RF (FP0_REGNUM
+ 6, inferior_fp_registers
.regs
[6]);
262 RF (FP0_REGNUM
+ 7, inferior_fp_registers
.regs
[7]);
264 RF (FCTRL_REGNUM
, inferior_fp_registers
.control
);
265 RF (FSTAT_REGNUM
, inferior_fp_registers
.status
);
266 RF (FTAG_REGNUM
, inferior_fp_registers
.tag
);
267 RF (FCS_REGNUM
, inferior_fp_registers
.code_seg
);
268 RF (FCOFF_REGNUM
, inferior_fp_registers
.eip
);
269 RF (FDS_REGNUM
, inferior_fp_registers
.operand_seg
);
270 RF (FDOFF_REGNUM
, inferior_fp_registers
.operand
);
271 RF (FOP_REGNUM
, inferior_fp_registers
.opcode
);
274 /* Store our register values back into the inferior.
275 If REGNO is -1, do this for all registers.
276 Otherwise, REGNO specifies which register (so we can save time). */
279 store_inferior_registers (int ignored
)
281 struct reg inferior_registers
;
282 struct env387 inferior_fp_registers
;
284 RS ( 0, inferior_registers
.r_eax
);
285 RS ( 1, inferior_registers
.r_ecx
);
286 RS ( 2, inferior_registers
.r_edx
);
287 RS ( 3, inferior_registers
.r_ebx
);
288 RS ( 4, inferior_registers
.r_esp
);
289 RS ( 5, inferior_registers
.r_ebp
);
290 RS ( 6, inferior_registers
.r_esi
);
291 RS ( 7, inferior_registers
.r_edi
);
292 RS ( 8, inferior_registers
.r_eip
);
293 RS ( 9, inferior_registers
.r_eflags
);
294 RS (10, inferior_registers
.r_cs
);
295 RS (11, inferior_registers
.r_ss
);
296 RS (12, inferior_registers
.r_ds
);
297 RS (13, inferior_registers
.r_es
);
298 RS (14, inferior_registers
.r_fs
);
299 RS (15, inferior_registers
.r_gs
);
301 RS (FP0_REGNUM
, inferior_fp_registers
.regs
[0]);
302 RS (FP0_REGNUM
+ 1, inferior_fp_registers
.regs
[1]);
303 RS (FP0_REGNUM
+ 2, inferior_fp_registers
.regs
[2]);
304 RS (FP0_REGNUM
+ 3, inferior_fp_registers
.regs
[3]);
305 RS (FP0_REGNUM
+ 4, inferior_fp_registers
.regs
[4]);
306 RS (FP0_REGNUM
+ 5, inferior_fp_registers
.regs
[5]);
307 RS (FP0_REGNUM
+ 6, inferior_fp_registers
.regs
[6]);
308 RS (FP0_REGNUM
+ 7, inferior_fp_registers
.regs
[7]);
310 RS (FCTRL_REGNUM
, inferior_fp_registers
.control
);
311 RS (FSTAT_REGNUM
, inferior_fp_registers
.status
);
312 RS (FTAG_REGNUM
, inferior_fp_registers
.tag
);
313 RS (FCS_REGNUM
, inferior_fp_registers
.code_seg
);
314 RS (FCOFF_REGNUM
, inferior_fp_registers
.eip
);
315 RS (FDS_REGNUM
, inferior_fp_registers
.operand_seg
);
316 RS (FDOFF_REGNUM
, inferior_fp_registers
.operand
);
317 RS (FOP_REGNUM
, inferior_fp_registers
.opcode
);
319 ptrace (PT_SETREGS
, inferior_pid
,
320 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0);
321 ptrace (PT_SETFPREGS
, inferior_pid
,
322 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
, 0);
324 #endif /* !__i386__ */
327 /* Fetch one or more registers from the inferior. REGNO == -1 to get
328 them all. We actually fetch more than requested, when convenient,
329 marking them as valid so we won't fetch them again. */
332 fetch_inferior_registers (int regno
)
334 struct reg inferior_registers
;
335 struct fpreg inferior_fp_registers
;
337 ptrace (PT_GETREGS
, inferior_pid
,
338 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
339 memcpy (®isters
[REGISTER_BYTE (0)], &inferior_registers
,
340 sizeof (inferior_registers
));
342 ptrace (PT_GETFPREGS
, inferior_pid
,
343 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
344 memcpy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], &inferior_fp_registers
,
345 sizeof (inferior_fp_registers
));
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
;
356 struct fpreg inferior_fp_registers
;
358 memcpy (&inferior_registers
, ®isters
[REGISTER_BYTE (0)],
359 sizeof (inferior_registers
));
360 ptrace (PT_SETREGS
, inferior_pid
,
361 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
363 memcpy (&inferior_fp_registers
, ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
364 sizeof (inferior_fp_registers
));
365 ptrace (PT_SETFPREGS
, inferior_pid
,
366 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
368 #endif /* !__m68k__ */
372 /* Fetch one or more registers from the inferior. REGNO == -1 to get
373 them all. We actually fetch more than requested, when convenient,
374 marking them as valid so we won't fetch them again. */
377 fetch_inferior_registers (int regno
)
379 struct reg inferior_registers
;
380 struct fpreg inferior_fpregisters
;
382 ptrace (PT_GETREGS
, inferior_pid
,
383 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
384 ptrace (PT_GETFPREGS
, inferior_pid
,
385 (PTRACE_ARG3_TYPE
) & inferior_fpregisters
, 0);
387 RF (R0_REGNUM
+ 0, inferior_registers
.r_r0
);
388 RF (R0_REGNUM
+ 1, inferior_registers
.r_r1
);
389 RF (R0_REGNUM
+ 2, inferior_registers
.r_r2
);
390 RF (R0_REGNUM
+ 3, inferior_registers
.r_r3
);
391 RF (R0_REGNUM
+ 4, inferior_registers
.r_r4
);
392 RF (R0_REGNUM
+ 5, inferior_registers
.r_r5
);
393 RF (R0_REGNUM
+ 6, inferior_registers
.r_r6
);
394 RF (R0_REGNUM
+ 7, inferior_registers
.r_r7
);
396 RF (SP_REGNUM
, inferior_registers
.r_sp
);
397 RF (FP_REGNUM
, inferior_registers
.r_fp
);
398 RF (PC_REGNUM
, inferior_registers
.r_pc
);
399 RF (PS_REGNUM
, inferior_registers
.r_psr
);
401 RF (FPS_REGNUM
, inferior_fpregisters
.r_fsr
);
402 RF (FP0_REGNUM
+ 0, inferior_fpregisters
.r_freg
[0]);
403 RF (FP0_REGNUM
+ 2, inferior_fpregisters
.r_freg
[2]);
404 RF (FP0_REGNUM
+ 4, inferior_fpregisters
.r_freg
[4]);
405 RF (FP0_REGNUM
+ 6, inferior_fpregisters
.r_freg
[6]);
406 RF (LP0_REGNUM
+ 1, inferior_fpregisters
.r_freg
[1]);
407 RF (LP0_REGNUM
+ 3, inferior_fpregisters
.r_freg
[3]);
408 RF (LP0_REGNUM
+ 5, inferior_fpregisters
.r_freg
[5]);
409 RF (LP0_REGNUM
+ 7, inferior_fpregisters
.r_freg
[7]);
412 /* Store our register values back into the inferior.
413 If REGNO is -1, do this for all registers.
414 Otherwise, REGNO specifies which register (so we can save time). */
417 store_inferior_registers (int regno
)
419 struct reg inferior_registers
;
420 struct fpreg inferior_fpregisters
;
422 RS (R0_REGNUM
+ 0, inferior_registers
.r_r0
);
423 RS (R0_REGNUM
+ 1, inferior_registers
.r_r1
);
424 RS (R0_REGNUM
+ 2, inferior_registers
.r_r2
);
425 RS (R0_REGNUM
+ 3, inferior_registers
.r_r3
);
426 RS (R0_REGNUM
+ 4, inferior_registers
.r_r4
);
427 RS (R0_REGNUM
+ 5, inferior_registers
.r_r5
);
428 RS (R0_REGNUM
+ 6, inferior_registers
.r_r6
);
429 RS (R0_REGNUM
+ 7, inferior_registers
.r_r7
);
431 RS (SP_REGNUM
, inferior_registers
.r_sp
);
432 RS (FP_REGNUM
, inferior_registers
.r_fp
);
433 RS (PC_REGNUM
, inferior_registers
.r_pc
);
434 RS (PS_REGNUM
, inferior_registers
.r_psr
);
436 RS (FPS_REGNUM
, inferior_fpregisters
.r_fsr
);
437 RS (FP0_REGNUM
+ 0, inferior_fpregisters
.r_freg
[0]);
438 RS (FP0_REGNUM
+ 2, inferior_fpregisters
.r_freg
[2]);
439 RS (FP0_REGNUM
+ 4, inferior_fpregisters
.r_freg
[4]);
440 RS (FP0_REGNUM
+ 6, inferior_fpregisters
.r_freg
[6]);
441 RS (LP0_REGNUM
+ 1, inferior_fpregisters
.r_freg
[1]);
442 RS (LP0_REGNUM
+ 3, inferior_fpregisters
.r_freg
[3]);
443 RS (LP0_REGNUM
+ 5, inferior_fpregisters
.r_freg
[5]);
444 RS (LP0_REGNUM
+ 7, inferior_fpregisters
.r_freg
[7]);
446 ptrace (PT_SETREGS
, inferior_pid
,
447 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
448 ptrace (PT_SETFPREGS
, inferior_pid
,
449 (PTRACE_ARG3_TYPE
) & inferior_fpregisters
, 0);
452 #endif /* !__ns32k__ */
455 /* Fetch one or more registers from the inferior. REGNO == -1 to get
456 them all. We actually fetch more than requested, when convenient,
457 marking them as valid so we won't fetch them again. */
460 fetch_inferior_registers (int regno
)
462 struct reg inferior_registers
;
464 struct fpreg inferior_fp_registers
;
468 ptrace (PT_GETREGS
, inferior_pid
,
469 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
470 for (i
= 0; i
< 32; i
++)
471 RF (i
, inferior_registers
.fixreg
[i
]);
472 RF (PPC_LR_REGNUM
, inferior_registers
.lr
);
473 RF (PPC_CR_REGNUM
, inferior_registers
.cr
);
474 RF (PPC_XER_REGNUM
, inferior_registers
.xer
);
475 RF (PPC_CTR_REGNUM
, inferior_registers
.ctr
);
476 RF (PC_REGNUM
, inferior_registers
.pc
);
479 ptrace (PT_GETFPREGS
, inferior_pid
,
480 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
481 for (i
= 0; i
< 32; i
++)
482 RF (FP0_REGNUM
+ i
, inferior_fp_registers
.r_regs
[i
]);
486 /* Store our register values back into the inferior.
487 If REGNO is -1, do this for all registers.
488 Otherwise, REGNO specifies which register (so we can save time). */
491 store_inferior_registers (int regno
)
493 struct reg inferior_registers
;
495 struct fpreg inferior_fp_registers
;
499 for (i
= 0; i
< 32; i
++)
500 RS (i
, inferior_registers
.fixreg
[i
]);
501 RS (PPC_LR_REGNUM
, inferior_registers
.lr
);
502 RS (PPC_CR_REGNUM
, inferior_registers
.cr
);
503 RS (PPC_XER_REGNUM
, inferior_registers
.xer
);
504 RS (PPC_CTR_REGNUM
, inferior_registers
.ctr
);
505 RS (PC_REGNUM
, inferior_registers
.pc
);
506 ptrace (PT_SETREGS
, inferior_pid
,
507 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
510 for (i
= 0; i
< 32; i
++)
511 RS (FP0_REGNUM
+ i
, inferior_fp_registers
.r_regs
[i
]);
512 ptrace (PT_SETFPREGS
, inferior_pid
,
513 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
516 #endif /* !__powerpc__ */
518 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
519 in the NEW_SUN_PTRACE case.
520 It ought to be straightforward. But it appears that writing did
521 not write the data that I specified. I cannot understand where
522 it got the data that it actually did write. */
524 /* Copy LEN bytes from inferior's memory starting at MEMADDR
525 to debugger memory starting at MYADDR. */
528 read_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
531 /* Round starting address down to longword boundary. */
532 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (int);
533 /* Round ending address up; get number of longwords that makes. */
535 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
536 /* Allocate buffer of that many longwords. */
537 register int *buffer
= (int *) alloca (count
* sizeof (int));
539 /* Read all the longwords */
540 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
542 buffer
[i
] = ptrace (PT_READ_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
545 /* Copy appropriate bytes out of the buffer. */
546 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
549 /* Copy LEN bytes of data from debugger memory at MYADDR
550 to inferior's memory at MEMADDR.
551 On failure (cannot write the inferior)
552 returns the value of errno. */
555 write_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
558 /* Round starting address down to longword boundary. */
559 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (int);
560 /* Round ending address up; get number of longwords that makes. */
562 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
563 /* Allocate buffer of that many longwords. */
564 register int *buffer
= (int *) alloca (count
* sizeof (int));
567 /* Fill start and end extra bytes of buffer with existing memory data. */
569 buffer
[0] = ptrace (PT_READ_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
574 = ptrace (PT_READ_D
, inferior_pid
,
575 (PTRACE_ARG3_TYPE
) addr
+ (count
- 1) * sizeof (int), 0);
578 /* Copy data to be written over corresponding part of buffer */
580 memcpy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
582 /* Write the entire buffer. */
584 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
587 ptrace (PT_WRITE_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
596 initialize_low (void)