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*********************/
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 if (pid
!= inferior_pid
)
177 perror_with_name ("wait");
181 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
183 return ((unsigned char) WEXITSTATUS (w
));
185 else if (!WIFSTOPPED (w
))
187 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
189 return ((unsigned char) WTERMSIG (w
));
192 fetch_inferior_registers (0);
195 return ((unsigned char) WSTOPSIG (w
));
198 /* Resume execution of the inferior process.
199 If STEP is nonzero, single-step it.
200 If SIGNAL is nonzero, give it that signal. */
203 myresume (int step
, int signal
)
206 ptrace (step
? PT_STEP
: PT_CONTINUE
, inferior_pid
,
207 (PTRACE_ARG3_TYPE
) 1, signal
);
209 perror_with_name ("ptrace");
214 /* Fetch one or more registers from the inferior. REGNO == -1 to get
215 them all. We actually fetch more than requested, when convenient,
216 marking them as valid so we won't fetch them again. */
219 fetch_inferior_registers (int ignored
)
221 struct reg inferior_registers
;
222 struct env387 inferior_fp_registers
;
224 ptrace (PT_GETREGS
, inferior_pid
,
225 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0);
226 ptrace (PT_GETFPREGS
, inferior_pid
,
227 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
, 0);
229 RF ( 0, inferior_registers
.r_eax
);
230 RF ( 1, inferior_registers
.r_ecx
);
231 RF ( 2, inferior_registers
.r_edx
);
232 RF ( 3, inferior_registers
.r_ebx
);
233 RF ( 4, inferior_registers
.r_esp
);
234 RF ( 5, inferior_registers
.r_ebp
);
235 RF ( 6, inferior_registers
.r_esi
);
236 RF ( 7, inferior_registers
.r_edi
);
237 RF ( 8, inferior_registers
.r_eip
);
238 RF ( 9, inferior_registers
.r_eflags
);
239 RF (10, inferior_registers
.r_cs
);
240 RF (11, inferior_registers
.r_ss
);
241 RF (12, inferior_registers
.r_ds
);
242 RF (13, inferior_registers
.r_es
);
243 RF (14, inferior_registers
.r_fs
);
244 RF (15, inferior_registers
.r_gs
);
246 RF (FP0_REGNUM
, inferior_fp_registers
.regs
[0]);
247 RF (FP0_REGNUM
+ 1, inferior_fp_registers
.regs
[1]);
248 RF (FP0_REGNUM
+ 2, inferior_fp_registers
.regs
[2]);
249 RF (FP0_REGNUM
+ 3, inferior_fp_registers
.regs
[3]);
250 RF (FP0_REGNUM
+ 4, inferior_fp_registers
.regs
[4]);
251 RF (FP0_REGNUM
+ 5, inferior_fp_registers
.regs
[5]);
252 RF (FP0_REGNUM
+ 6, inferior_fp_registers
.regs
[6]);
253 RF (FP0_REGNUM
+ 7, inferior_fp_registers
.regs
[7]);
255 RF (FCTRL_REGNUM
, inferior_fp_registers
.control
);
256 RF (FSTAT_REGNUM
, inferior_fp_registers
.status
);
257 RF (FTAG_REGNUM
, inferior_fp_registers
.tag
);
258 RF (FCS_REGNUM
, inferior_fp_registers
.code_seg
);
259 RF (FCOFF_REGNUM
, inferior_fp_registers
.eip
);
260 RF (FDS_REGNUM
, inferior_fp_registers
.operand_seg
);
261 RF (FDOFF_REGNUM
, inferior_fp_registers
.operand
);
262 RF (FOP_REGNUM
, inferior_fp_registers
.opcode
);
265 /* Store our register values back into the inferior.
266 If REGNO is -1, do this for all registers.
267 Otherwise, REGNO specifies which register (so we can save time). */
270 store_inferior_registers (int ignored
)
272 struct reg inferior_registers
;
273 struct env387 inferior_fp_registers
;
275 RS ( 0, inferior_registers
.r_eax
);
276 RS ( 1, inferior_registers
.r_ecx
);
277 RS ( 2, inferior_registers
.r_edx
);
278 RS ( 3, inferior_registers
.r_ebx
);
279 RS ( 4, inferior_registers
.r_esp
);
280 RS ( 5, inferior_registers
.r_ebp
);
281 RS ( 6, inferior_registers
.r_esi
);
282 RS ( 7, inferior_registers
.r_edi
);
283 RS ( 8, inferior_registers
.r_eip
);
284 RS ( 9, inferior_registers
.r_eflags
);
285 RS (10, inferior_registers
.r_cs
);
286 RS (11, inferior_registers
.r_ss
);
287 RS (12, inferior_registers
.r_ds
);
288 RS (13, inferior_registers
.r_es
);
289 RS (14, inferior_registers
.r_fs
);
290 RS (15, inferior_registers
.r_gs
);
292 RS (FP0_REGNUM
, inferior_fp_registers
.regs
[0]);
293 RS (FP0_REGNUM
+ 1, inferior_fp_registers
.regs
[1]);
294 RS (FP0_REGNUM
+ 2, inferior_fp_registers
.regs
[2]);
295 RS (FP0_REGNUM
+ 3, inferior_fp_registers
.regs
[3]);
296 RS (FP0_REGNUM
+ 4, inferior_fp_registers
.regs
[4]);
297 RS (FP0_REGNUM
+ 5, inferior_fp_registers
.regs
[5]);
298 RS (FP0_REGNUM
+ 6, inferior_fp_registers
.regs
[6]);
299 RS (FP0_REGNUM
+ 7, inferior_fp_registers
.regs
[7]);
301 RS (FCTRL_REGNUM
, inferior_fp_registers
.control
);
302 RS (FSTAT_REGNUM
, inferior_fp_registers
.status
);
303 RS (FTAG_REGNUM
, inferior_fp_registers
.tag
);
304 RS (FCS_REGNUM
, inferior_fp_registers
.code_seg
);
305 RS (FCOFF_REGNUM
, inferior_fp_registers
.eip
);
306 RS (FDS_REGNUM
, inferior_fp_registers
.operand_seg
);
307 RS (FDOFF_REGNUM
, inferior_fp_registers
.operand
);
308 RS (FOP_REGNUM
, inferior_fp_registers
.opcode
);
310 ptrace (PT_SETREGS
, inferior_pid
,
311 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0);
312 ptrace (PT_SETFPREGS
, inferior_pid
,
313 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
, 0);
315 #endif /* !__i386__ */
318 /* Fetch one or more registers from the inferior. REGNO == -1 to get
319 them all. We actually fetch more than requested, when convenient,
320 marking them as valid so we won't fetch them again. */
323 fetch_inferior_registers (int regno
)
325 struct reg inferior_registers
;
326 struct fpreg inferior_fp_registers
;
328 ptrace (PT_GETREGS
, inferior_pid
,
329 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
330 memcpy (®isters
[REGISTER_BYTE (0)], &inferior_registers
,
331 sizeof (inferior_registers
));
333 ptrace (PT_GETFPREGS
, inferior_pid
,
334 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
335 memcpy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], &inferior_fp_registers
,
336 sizeof (inferior_fp_registers
));
339 /* Store our register values back into the inferior.
340 If REGNO is -1, do this for all registers.
341 Otherwise, REGNO specifies which register (so we can save time). */
344 store_inferior_registers (int regno
)
346 struct reg inferior_registers
;
347 struct fpreg inferior_fp_registers
;
349 memcpy (&inferior_registers
, ®isters
[REGISTER_BYTE (0)],
350 sizeof (inferior_registers
));
351 ptrace (PT_SETREGS
, inferior_pid
,
352 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
354 memcpy (&inferior_fp_registers
, ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
355 sizeof (inferior_fp_registers
));
356 ptrace (PT_SETFPREGS
, inferior_pid
,
357 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
359 #endif /* !__m68k__ */
363 /* Fetch one or more registers from the inferior. REGNO == -1 to get
364 them all. We actually fetch more than requested, when convenient,
365 marking them as valid so we won't fetch them again. */
368 fetch_inferior_registers (int regno
)
370 struct reg inferior_registers
;
371 struct fpreg inferior_fpregisters
;
373 ptrace (PT_GETREGS
, inferior_pid
,
374 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
375 ptrace (PT_GETFPREGS
, inferior_pid
,
376 (PTRACE_ARG3_TYPE
) & inferior_fpregisters
, 0);
378 RF (R0_REGNUM
+ 0, inferior_registers
.r_r0
);
379 RF (R0_REGNUM
+ 1, inferior_registers
.r_r1
);
380 RF (R0_REGNUM
+ 2, inferior_registers
.r_r2
);
381 RF (R0_REGNUM
+ 3, inferior_registers
.r_r3
);
382 RF (R0_REGNUM
+ 4, inferior_registers
.r_r4
);
383 RF (R0_REGNUM
+ 5, inferior_registers
.r_r5
);
384 RF (R0_REGNUM
+ 6, inferior_registers
.r_r6
);
385 RF (R0_REGNUM
+ 7, inferior_registers
.r_r7
);
387 RF (SP_REGNUM
, inferior_registers
.r_sp
);
388 RF (FP_REGNUM
, inferior_registers
.r_fp
);
389 RF (PC_REGNUM
, inferior_registers
.r_pc
);
390 RF (PS_REGNUM
, inferior_registers
.r_psr
);
392 RF (FPS_REGNUM
, inferior_fpregisters
.r_fsr
);
393 RF (FP0_REGNUM
+ 0, inferior_fpregisters
.r_freg
[0]);
394 RF (FP0_REGNUM
+ 2, inferior_fpregisters
.r_freg
[2]);
395 RF (FP0_REGNUM
+ 4, inferior_fpregisters
.r_freg
[4]);
396 RF (FP0_REGNUM
+ 6, inferior_fpregisters
.r_freg
[6]);
397 RF (LP0_REGNUM
+ 1, inferior_fpregisters
.r_freg
[1]);
398 RF (LP0_REGNUM
+ 3, inferior_fpregisters
.r_freg
[3]);
399 RF (LP0_REGNUM
+ 5, inferior_fpregisters
.r_freg
[5]);
400 RF (LP0_REGNUM
+ 7, inferior_fpregisters
.r_freg
[7]);
403 /* Store our register values back into the inferior.
404 If REGNO is -1, do this for all registers.
405 Otherwise, REGNO specifies which register (so we can save time). */
408 store_inferior_registers (int regno
)
410 struct reg inferior_registers
;
411 struct fpreg inferior_fpregisters
;
413 RS (R0_REGNUM
+ 0, inferior_registers
.r_r0
);
414 RS (R0_REGNUM
+ 1, inferior_registers
.r_r1
);
415 RS (R0_REGNUM
+ 2, inferior_registers
.r_r2
);
416 RS (R0_REGNUM
+ 3, inferior_registers
.r_r3
);
417 RS (R0_REGNUM
+ 4, inferior_registers
.r_r4
);
418 RS (R0_REGNUM
+ 5, inferior_registers
.r_r5
);
419 RS (R0_REGNUM
+ 6, inferior_registers
.r_r6
);
420 RS (R0_REGNUM
+ 7, inferior_registers
.r_r7
);
422 RS (SP_REGNUM
, inferior_registers
.r_sp
);
423 RS (FP_REGNUM
, inferior_registers
.r_fp
);
424 RS (PC_REGNUM
, inferior_registers
.r_pc
);
425 RS (PS_REGNUM
, inferior_registers
.r_psr
);
427 RS (FPS_REGNUM
, inferior_fpregisters
.r_fsr
);
428 RS (FP0_REGNUM
+ 0, inferior_fpregisters
.r_freg
[0]);
429 RS (FP0_REGNUM
+ 2, inferior_fpregisters
.r_freg
[2]);
430 RS (FP0_REGNUM
+ 4, inferior_fpregisters
.r_freg
[4]);
431 RS (FP0_REGNUM
+ 6, inferior_fpregisters
.r_freg
[6]);
432 RS (LP0_REGNUM
+ 1, inferior_fpregisters
.r_freg
[1]);
433 RS (LP0_REGNUM
+ 3, inferior_fpregisters
.r_freg
[3]);
434 RS (LP0_REGNUM
+ 5, inferior_fpregisters
.r_freg
[5]);
435 RS (LP0_REGNUM
+ 7, inferior_fpregisters
.r_freg
[7]);
437 ptrace (PT_SETREGS
, inferior_pid
,
438 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
439 ptrace (PT_SETFPREGS
, inferior_pid
,
440 (PTRACE_ARG3_TYPE
) & inferior_fpregisters
, 0);
443 #endif /* !__ns32k__ */
446 /* Fetch one or more registers from the inferior. REGNO == -1 to get
447 them all. We actually fetch more than requested, when convenient,
448 marking them as valid so we won't fetch them again. */
451 fetch_inferior_registers (int regno
)
453 struct reg inferior_registers
;
455 struct fpreg inferior_fp_registers
;
459 ptrace (PT_GETREGS
, inferior_pid
,
460 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
461 for (i
= 0; i
< 32; i
++)
462 RF (i
, inferior_registers
.fixreg
[i
]);
463 RF (PPC_LR_REGNUM
, inferior_registers
.lr
);
464 RF (PPC_CR_REGNUM
, inferior_registers
.cr
);
465 RF (PPC_XER_REGNUM
, inferior_registers
.xer
);
466 RF (PPC_CTR_REGNUM
, inferior_registers
.ctr
);
467 RF (PC_REGNUM
, inferior_registers
.pc
);
470 ptrace (PT_GETFPREGS
, inferior_pid
,
471 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
472 for (i
= 0; i
< 32; i
++)
473 RF (FP0_REGNUM
+ i
, inferior_fp_registers
.r_regs
[i
]);
477 /* Store our register values back into the inferior.
478 If REGNO is -1, do this for all registers.
479 Otherwise, REGNO specifies which register (so we can save time). */
482 store_inferior_registers (int regno
)
484 struct reg inferior_registers
;
486 struct fpreg inferior_fp_registers
;
490 for (i
= 0; i
< 32; i
++)
491 RS (i
, inferior_registers
.fixreg
[i
]);
492 RS (PPC_LR_REGNUM
, inferior_registers
.lr
);
493 RS (PPC_CR_REGNUM
, inferior_registers
.cr
);
494 RS (PPC_XER_REGNUM
, inferior_registers
.xer
);
495 RS (PPC_CTR_REGNUM
, inferior_registers
.ctr
);
496 RS (PC_REGNUM
, inferior_registers
.pc
);
497 ptrace (PT_SETREGS
, inferior_pid
,
498 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
501 for (i
= 0; i
< 32; i
++)
502 RS (FP0_REGNUM
+ i
, inferior_fp_registers
.r_regs
[i
]);
503 ptrace (PT_SETFPREGS
, inferior_pid
,
504 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
507 #endif /* !__powerpc__ */
509 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
510 in the NEW_SUN_PTRACE case.
511 It ought to be straightforward. But it appears that writing did
512 not write the data that I specified. I cannot understand where
513 it got the data that it actually did write. */
515 /* Copy LEN bytes from inferior's memory starting at MEMADDR
516 to debugger memory starting at MYADDR. */
518 read_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
521 /* Round starting address down to longword boundary. */
522 register CORE_ADDR addr
= memaddr
& -sizeof (int);
523 /* Round ending address up; get number of longwords that makes. */
525 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
526 /* Allocate buffer of that many longwords. */
527 register int *buffer
= (int *) alloca (count
* sizeof (int));
529 /* Read all the longwords */
530 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
532 buffer
[i
] = ptrace (PT_READ_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
535 /* Copy appropriate bytes out of the buffer. */
536 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
539 /* Copy LEN bytes of data from debugger memory at MYADDR
540 to inferior's memory at MEMADDR.
541 On failure (cannot write the inferior)
542 returns the value of errno. */
545 write_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
548 /* Round starting address down to longword boundary. */
549 register CORE_ADDR addr
= memaddr
& -sizeof (int);
550 /* Round ending address up; get number of longwords that makes. */
552 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
553 /* Allocate buffer of that many longwords. */
554 register int *buffer
= (int *) alloca (count
* sizeof (int));
557 /* Fill start and end extra bytes of buffer with existing memory data. */
559 buffer
[0] = ptrace (PT_READ_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
564 = ptrace (PT_READ_D
, inferior_pid
,
565 (PTRACE_ARG3_TYPE
) addr
+ (count
- 1) * sizeof (int), 0);
568 /* Copy data to be written over corresponding part of buffer */
570 memcpy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
572 /* Write the entire buffer. */
574 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
577 ptrace (PT_WRITE_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
586 initialize_low (void)