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 // extern int sys_nerr;
38 // extern char **sys_errlist;
39 extern int inferior_pid
;
40 void perror_with_name ();
42 #define RF(dst, src) \
43 memcpy(®isters[REGISTER_BYTE(dst)], &src, sizeof(src))
45 #define RS(src, dst) \
46 memcpy(&dst, ®isters[REGISTER_BYTE(src)], sizeof(dst))
51 unsigned short control
;
53 unsigned short status
;
58 unsigned short code_seg
;
59 unsigned short opcode
;
60 unsigned long operand
;
61 unsigned short operand_seg
;
63 unsigned char regs
[8][10];
66 /* i386_register_raw_size[i] is the number of bytes of storage in the
67 actual machine representation for register i. */
68 int i386_register_raw_size
[MAX_NUM_REGS
] = {
82 int i386_register_byte
[MAX_NUM_REGS
];
85 initialize_arch (void)
87 /* Initialize the table saying where each register starts in the
93 for (i
= 0; i
< MAX_NUM_REGS
; i
++)
95 i386_register_byte
[i
] = offset
;
96 offset
+= i386_register_raw_size
[i
];
100 #endif /* !__i386__ */
104 initialize_arch (void)
107 #endif /* !__m68k__ */
111 initialize_arch (void)
114 #endif /* !__ns32k__ */
117 #include "ppc-tdep.h"
120 initialize_arch (void)
123 #endif /* !__powerpc__ */
126 /* Start an inferior process and returns its pid.
127 ALLARGS is a vector of program-name and args. */
130 create_inferior (char *program
, char **allargs
)
136 perror_with_name ("fork");
140 ptrace (PT_TRACE_ME
, 0, 0, 0);
142 execv (program
, allargs
);
144 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
145 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
153 /* Kill the inferior process. Make us have no inferior. */
158 if (inferior_pid
== 0)
160 ptrace (PT_KILL
, inferior_pid
, 0, 0);
162 /*************inferior_died ();****VK**************/
165 /* Return nonzero if the given thread is still alive. */
167 mythread_alive (int pid
)
172 /* Wait for process, returns status */
175 mywait (char *status
)
181 if (pid
!= inferior_pid
)
182 perror_with_name ("wait");
186 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
188 return ((unsigned char) WEXITSTATUS (w
));
190 else if (!WIFSTOPPED (w
))
192 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
194 return ((unsigned char) WTERMSIG (w
));
197 fetch_inferior_registers (0);
200 return ((unsigned char) WSTOPSIG (w
));
203 /* Resume execution of the inferior process.
204 If STEP is nonzero, single-step it.
205 If SIGNAL is nonzero, give it that signal. */
208 myresume (int step
, int signal
)
211 ptrace (step
? PT_STEP
: PT_CONTINUE
, inferior_pid
,
212 (PTRACE_ARG3_TYPE
) 1, signal
);
214 perror_with_name ("ptrace");
219 /* Fetch one or more registers from the inferior. REGNO == -1 to get
220 them all. We actually fetch more than requested, when convenient,
221 marking them as valid so we won't fetch them again. */
224 fetch_inferior_registers (int ignored
)
226 struct reg inferior_registers
;
227 struct env387 inferior_fp_registers
;
229 ptrace (PT_GETREGS
, inferior_pid
,
230 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0);
231 ptrace (PT_GETFPREGS
, inferior_pid
,
232 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
, 0);
234 RF ( 0, inferior_registers
.r_eax
);
235 RF ( 1, inferior_registers
.r_ecx
);
236 RF ( 2, inferior_registers
.r_edx
);
237 RF ( 3, inferior_registers
.r_ebx
);
238 RF ( 4, inferior_registers
.r_esp
);
239 RF ( 5, inferior_registers
.r_ebp
);
240 RF ( 6, inferior_registers
.r_esi
);
241 RF ( 7, inferior_registers
.r_edi
);
242 RF ( 8, inferior_registers
.r_eip
);
243 RF ( 9, inferior_registers
.r_eflags
);
244 RF (10, inferior_registers
.r_cs
);
245 RF (11, inferior_registers
.r_ss
);
246 RF (12, inferior_registers
.r_ds
);
247 RF (13, inferior_registers
.r_es
);
248 RF (14, inferior_registers
.r_fs
);
249 RF (15, inferior_registers
.r_gs
);
251 RF (FP0_REGNUM
, inferior_fp_registers
.regs
[0]);
252 RF (FP0_REGNUM
+ 1, inferior_fp_registers
.regs
[1]);
253 RF (FP0_REGNUM
+ 2, inferior_fp_registers
.regs
[2]);
254 RF (FP0_REGNUM
+ 3, inferior_fp_registers
.regs
[3]);
255 RF (FP0_REGNUM
+ 4, inferior_fp_registers
.regs
[4]);
256 RF (FP0_REGNUM
+ 5, inferior_fp_registers
.regs
[5]);
257 RF (FP0_REGNUM
+ 6, inferior_fp_registers
.regs
[6]);
258 RF (FP0_REGNUM
+ 7, inferior_fp_registers
.regs
[7]);
260 RF (FCTRL_REGNUM
, inferior_fp_registers
.control
);
261 RF (FSTAT_REGNUM
, inferior_fp_registers
.status
);
262 RF (FTAG_REGNUM
, inferior_fp_registers
.tag
);
263 RF (FCS_REGNUM
, inferior_fp_registers
.code_seg
);
264 RF (FCOFF_REGNUM
, inferior_fp_registers
.eip
);
265 RF (FDS_REGNUM
, inferior_fp_registers
.operand_seg
);
266 RF (FDOFF_REGNUM
, inferior_fp_registers
.operand
);
267 RF (FOP_REGNUM
, inferior_fp_registers
.opcode
);
270 /* Store our register values back into the inferior.
271 If REGNO is -1, do this for all registers.
272 Otherwise, REGNO specifies which register (so we can save time). */
275 store_inferior_registers (int ignored
)
277 struct reg inferior_registers
;
278 struct env387 inferior_fp_registers
;
280 RS ( 0, inferior_registers
.r_eax
);
281 RS ( 1, inferior_registers
.r_ecx
);
282 RS ( 2, inferior_registers
.r_edx
);
283 RS ( 3, inferior_registers
.r_ebx
);
284 RS ( 4, inferior_registers
.r_esp
);
285 RS ( 5, inferior_registers
.r_ebp
);
286 RS ( 6, inferior_registers
.r_esi
);
287 RS ( 7, inferior_registers
.r_edi
);
288 RS ( 8, inferior_registers
.r_eip
);
289 RS ( 9, inferior_registers
.r_eflags
);
290 RS (10, inferior_registers
.r_cs
);
291 RS (11, inferior_registers
.r_ss
);
292 RS (12, inferior_registers
.r_ds
);
293 RS (13, inferior_registers
.r_es
);
294 RS (14, inferior_registers
.r_fs
);
295 RS (15, inferior_registers
.r_gs
);
297 RS (FP0_REGNUM
, inferior_fp_registers
.regs
[0]);
298 RS (FP0_REGNUM
+ 1, inferior_fp_registers
.regs
[1]);
299 RS (FP0_REGNUM
+ 2, inferior_fp_registers
.regs
[2]);
300 RS (FP0_REGNUM
+ 3, inferior_fp_registers
.regs
[3]);
301 RS (FP0_REGNUM
+ 4, inferior_fp_registers
.regs
[4]);
302 RS (FP0_REGNUM
+ 5, inferior_fp_registers
.regs
[5]);
303 RS (FP0_REGNUM
+ 6, inferior_fp_registers
.regs
[6]);
304 RS (FP0_REGNUM
+ 7, inferior_fp_registers
.regs
[7]);
306 RS (FCTRL_REGNUM
, inferior_fp_registers
.control
);
307 RS (FSTAT_REGNUM
, inferior_fp_registers
.status
);
308 RS (FTAG_REGNUM
, inferior_fp_registers
.tag
);
309 RS (FCS_REGNUM
, inferior_fp_registers
.code_seg
);
310 RS (FCOFF_REGNUM
, inferior_fp_registers
.eip
);
311 RS (FDS_REGNUM
, inferior_fp_registers
.operand_seg
);
312 RS (FDOFF_REGNUM
, inferior_fp_registers
.operand
);
313 RS (FOP_REGNUM
, inferior_fp_registers
.opcode
);
315 ptrace (PT_SETREGS
, inferior_pid
,
316 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0);
317 ptrace (PT_SETFPREGS
, inferior_pid
,
318 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
, 0);
320 #endif /* !__i386__ */
323 /* Fetch one or more registers from the inferior. REGNO == -1 to get
324 them all. We actually fetch more than requested, when convenient,
325 marking them as valid so we won't fetch them again. */
328 fetch_inferior_registers (int regno
)
330 struct reg inferior_registers
;
331 struct fpreg inferior_fp_registers
;
333 ptrace (PT_GETREGS
, inferior_pid
,
334 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
335 memcpy (®isters
[REGISTER_BYTE (0)], &inferior_registers
,
336 sizeof (inferior_registers
));
338 ptrace (PT_GETFPREGS
, inferior_pid
,
339 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
340 memcpy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], &inferior_fp_registers
,
341 sizeof (inferior_fp_registers
));
344 /* Store our register values back into the inferior.
345 If REGNO is -1, do this for all registers.
346 Otherwise, REGNO specifies which register (so we can save time). */
349 store_inferior_registers (int regno
)
351 struct reg inferior_registers
;
352 struct fpreg inferior_fp_registers
;
354 memcpy (&inferior_registers
, ®isters
[REGISTER_BYTE (0)],
355 sizeof (inferior_registers
));
356 ptrace (PT_SETREGS
, inferior_pid
,
357 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
359 memcpy (&inferior_fp_registers
, ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
360 sizeof (inferior_fp_registers
));
361 ptrace (PT_SETFPREGS
, inferior_pid
,
362 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
364 #endif /* !__m68k__ */
368 /* Fetch one or more registers from the inferior. REGNO == -1 to get
369 them all. We actually fetch more than requested, when convenient,
370 marking them as valid so we won't fetch them again. */
373 fetch_inferior_registers (int regno
)
375 struct reg inferior_registers
;
376 struct fpreg inferior_fpregisters
;
378 ptrace (PT_GETREGS
, inferior_pid
,
379 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
380 ptrace (PT_GETFPREGS
, inferior_pid
,
381 (PTRACE_ARG3_TYPE
) & inferior_fpregisters
, 0);
383 RF (R0_REGNUM
+ 0, inferior_registers
.r_r0
);
384 RF (R0_REGNUM
+ 1, inferior_registers
.r_r1
);
385 RF (R0_REGNUM
+ 2, inferior_registers
.r_r2
);
386 RF (R0_REGNUM
+ 3, inferior_registers
.r_r3
);
387 RF (R0_REGNUM
+ 4, inferior_registers
.r_r4
);
388 RF (R0_REGNUM
+ 5, inferior_registers
.r_r5
);
389 RF (R0_REGNUM
+ 6, inferior_registers
.r_r6
);
390 RF (R0_REGNUM
+ 7, inferior_registers
.r_r7
);
392 RF (SP_REGNUM
, inferior_registers
.r_sp
);
393 RF (FP_REGNUM
, inferior_registers
.r_fp
);
394 RF (PC_REGNUM
, inferior_registers
.r_pc
);
395 RF (PS_REGNUM
, inferior_registers
.r_psr
);
397 RF (FPS_REGNUM
, inferior_fpregisters
.r_fsr
);
398 RF (FP0_REGNUM
+ 0, inferior_fpregisters
.r_freg
[0]);
399 RF (FP0_REGNUM
+ 2, inferior_fpregisters
.r_freg
[2]);
400 RF (FP0_REGNUM
+ 4, inferior_fpregisters
.r_freg
[4]);
401 RF (FP0_REGNUM
+ 6, inferior_fpregisters
.r_freg
[6]);
402 RF (LP0_REGNUM
+ 1, inferior_fpregisters
.r_freg
[1]);
403 RF (LP0_REGNUM
+ 3, inferior_fpregisters
.r_freg
[3]);
404 RF (LP0_REGNUM
+ 5, inferior_fpregisters
.r_freg
[5]);
405 RF (LP0_REGNUM
+ 7, inferior_fpregisters
.r_freg
[7]);
408 /* Store our register values back into the inferior.
409 If REGNO is -1, do this for all registers.
410 Otherwise, REGNO specifies which register (so we can save time). */
413 store_inferior_registers (int regno
)
415 struct reg inferior_registers
;
416 struct fpreg inferior_fpregisters
;
418 RS (R0_REGNUM
+ 0, inferior_registers
.r_r0
);
419 RS (R0_REGNUM
+ 1, inferior_registers
.r_r1
);
420 RS (R0_REGNUM
+ 2, inferior_registers
.r_r2
);
421 RS (R0_REGNUM
+ 3, inferior_registers
.r_r3
);
422 RS (R0_REGNUM
+ 4, inferior_registers
.r_r4
);
423 RS (R0_REGNUM
+ 5, inferior_registers
.r_r5
);
424 RS (R0_REGNUM
+ 6, inferior_registers
.r_r6
);
425 RS (R0_REGNUM
+ 7, inferior_registers
.r_r7
);
427 RS (SP_REGNUM
, inferior_registers
.r_sp
);
428 RS (FP_REGNUM
, inferior_registers
.r_fp
);
429 RS (PC_REGNUM
, inferior_registers
.r_pc
);
430 RS (PS_REGNUM
, inferior_registers
.r_psr
);
432 RS (FPS_REGNUM
, inferior_fpregisters
.r_fsr
);
433 RS (FP0_REGNUM
+ 0, inferior_fpregisters
.r_freg
[0]);
434 RS (FP0_REGNUM
+ 2, inferior_fpregisters
.r_freg
[2]);
435 RS (FP0_REGNUM
+ 4, inferior_fpregisters
.r_freg
[4]);
436 RS (FP0_REGNUM
+ 6, inferior_fpregisters
.r_freg
[6]);
437 RS (LP0_REGNUM
+ 1, inferior_fpregisters
.r_freg
[1]);
438 RS (LP0_REGNUM
+ 3, inferior_fpregisters
.r_freg
[3]);
439 RS (LP0_REGNUM
+ 5, inferior_fpregisters
.r_freg
[5]);
440 RS (LP0_REGNUM
+ 7, inferior_fpregisters
.r_freg
[7]);
442 ptrace (PT_SETREGS
, inferior_pid
,
443 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
444 ptrace (PT_SETFPREGS
, inferior_pid
,
445 (PTRACE_ARG3_TYPE
) & inferior_fpregisters
, 0);
448 #endif /* !__ns32k__ */
451 /* Fetch one or more registers from the inferior. REGNO == -1 to get
452 them all. We actually fetch more than requested, when convenient,
453 marking them as valid so we won't fetch them again. */
456 fetch_inferior_registers (int regno
)
458 struct reg inferior_registers
;
460 struct fpreg inferior_fp_registers
;
464 ptrace (PT_GETREGS
, inferior_pid
,
465 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
466 for (i
= 0; i
< 32; i
++)
467 RF (i
, inferior_registers
.fixreg
[i
]);
468 RF (PPC_LR_REGNUM
, inferior_registers
.lr
);
469 RF (PPC_CR_REGNUM
, inferior_registers
.cr
);
470 RF (PPC_XER_REGNUM
, inferior_registers
.xer
);
471 RF (PPC_CTR_REGNUM
, inferior_registers
.ctr
);
472 RF (PC_REGNUM
, inferior_registers
.pc
);
475 ptrace (PT_GETFPREGS
, inferior_pid
,
476 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
477 for (i
= 0; i
< 32; i
++)
478 RF (FP0_REGNUM
+ i
, inferior_fp_registers
.r_regs
[i
]);
482 /* Store our register values back into the inferior.
483 If REGNO is -1, do this for all registers.
484 Otherwise, REGNO specifies which register (so we can save time). */
487 store_inferior_registers (int regno
)
489 struct reg inferior_registers
;
491 struct fpreg inferior_fp_registers
;
495 for (i
= 0; i
< 32; i
++)
496 RS (i
, inferior_registers
.fixreg
[i
]);
497 RS (PPC_LR_REGNUM
, inferior_registers
.lr
);
498 RS (PPC_CR_REGNUM
, inferior_registers
.cr
);
499 RS (PPC_XER_REGNUM
, inferior_registers
.xer
);
500 RS (PPC_CTR_REGNUM
, inferior_registers
.ctr
);
501 RS (PC_REGNUM
, inferior_registers
.pc
);
502 ptrace (PT_SETREGS
, inferior_pid
,
503 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0);
506 for (i
= 0; i
< 32; i
++)
507 RS (FP0_REGNUM
+ i
, inferior_fp_registers
.r_regs
[i
]);
508 ptrace (PT_SETFPREGS
, inferior_pid
,
509 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0);
512 #endif /* !__powerpc__ */
514 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
515 in the NEW_SUN_PTRACE case.
516 It ought to be straightforward. But it appears that writing did
517 not write the data that I specified. I cannot understand where
518 it got the data that it actually did write. */
520 /* Copy LEN bytes from inferior's memory starting at MEMADDR
521 to debugger memory starting at MYADDR. */
523 read_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
526 /* Round starting address down to longword boundary. */
527 register CORE_ADDR addr
= memaddr
& -sizeof (int);
528 /* Round ending address up; get number of longwords that makes. */
530 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
531 /* Allocate buffer of that many longwords. */
532 register int *buffer
= (int *) alloca (count
* sizeof (int));
534 /* Read all the longwords */
535 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
537 buffer
[i
] = ptrace (PT_READ_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
540 /* Copy appropriate bytes out of the buffer. */
541 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
544 /* Copy LEN bytes of data from debugger memory at MYADDR
545 to inferior's memory at MEMADDR.
546 On failure (cannot write the inferior)
547 returns the value of errno. */
550 write_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
553 /* Round starting address down to longword boundary. */
554 register CORE_ADDR addr
= memaddr
& -sizeof (int);
555 /* Round ending address up; get number of longwords that makes. */
557 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
558 /* Allocate buffer of that many longwords. */
559 register int *buffer
= (int *) alloca (count
* sizeof (int));
562 /* Fill start and end extra bytes of buffer with existing memory data. */
564 buffer
[0] = ptrace (PT_READ_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
569 = ptrace (PT_READ_D
, inferior_pid
,
570 (PTRACE_ARG3_TYPE
) addr
+ (count
- 1) * sizeof (int), 0);
573 /* Copy data to be written over corresponding part of buffer */
575 memcpy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
577 /* Write the entire buffer. */
579 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
582 ptrace (PT_WRITE_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
591 initialize_low (void)