1 /* Low level interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1988, 1989 Free Software Foundation, Inc.
4 This file is part of GDB.
6 GDB 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 1, or (at your option)
11 GDB 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 GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
26 #include <sys/param.h>
29 #include <sys/ioctl.h>
30 /* #include <fcntl.h> Can we live without this? */
34 #define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
37 #include <sys/user.h> /* After a.out.h */
43 /* This function simply calls ptrace with the given arguments.
44 It exists so that all calls to ptrace are isolated in this
45 machine-dependent file. */
47 call_ptrace (request
, pid
, arg3
, arg4
)
48 int request
, pid
, arg3
, arg4
;
50 return ptrace (request
, pid
, arg3
, arg4
);
57 if (inferior_pid
== 0)
59 ptrace (8, inferior_pid
, 0, 0);
64 /* This is used when GDB is exiting. It gives less chance of error.*/
70 if (inferior_pid
== 0)
72 ptrace (8, inferior_pid
, 0, 0);
76 /* Resume execution of the inferior process.
77 If STEP is nonzero, single-step it.
78 If SIGNAL is nonzero, give it that signal. */
87 remote_resume (step
, signal
);
90 ptrace (step
? 9 : 7, inferior_pid
, 1, signal
);
92 perror_with_name ("ptrace");
97 fetch_inferior_registers ()
99 register int regno
, datum
;
100 register unsigned int regaddr
;
101 int reg_buf
[NUM_REGS
+1];
103 register int skipped_frames
= 0;
105 if (remote_debugging
)
106 remote_fetch_registers ();
109 for (regno
= 0; regno
< 64; regno
++) {
110 reg_buf
[regno
] = ptrace (3, inferior_pid
, regno
, 0);
112 #if defined(PYRAMID_CONTROL_FRAME_DEBUGGING)
113 printf ("Fetching %s from inferior, got %0x\n",
116 #endif /* PYRAMID_CONTROL_FRAME_DEBUGGING */
118 if (reg_buf
[regno
] == -1 && errno
== EIO
) {
119 printf("fetch_interior_registers: fetching %s from inferior\n",
123 supply_register (regno
, reg_buf
+regno
);
125 /* that leaves regs 64, 65, and 66 */
126 datum
= ptrace (3, inferior_pid
,
127 ((char *)&u
.u_pcb
.pcb_csp
) -
132 /* FIXME: Find the Current Frame Pointer (CFP). CFP is a global
133 register (ie, NOT windowed), that gets saved in a frame iff
134 the code for that frame has a prologue (ie, "adsf N"). If
135 there is a prologue, the adsf insn saves the old cfp in
136 pr13, cfp is set to sp, and N bytes of locals are allocated
137 (sp is decremented by n).
138 This makes finding CFP hard. I guess the right way to do it
140 - If this is the innermost frame, believe ptrace() or
143 Find the first insn of the current frame.
145 - find the call insn that saved it;
146 - figure out where the call is to;
147 - if the first insn is an adsf, we got a frame
151 /* Normal processors have separate stack pointers for user and
152 kernel mode. Getting the last user mode frame on such
153 machines is easy: the kernel context of the ptrace()'d
154 process is on the kernel stack, and the USP points to what
155 we want. But Pyramids only have a single cfp for both user and
156 kernel mode. And processes being ptrace()'d have some
157 kernel-context control frames on their stack.
158 To avoid tracing back into the kernel context of an inferior,
159 we skip 0 or more contiguous control frames where the pc is
163 register int inferior_saved_pc
;
164 inferior_saved_pc
= ptrace (1, inferior_pid
, datum
+((32+15)*4), 0);
165 if (inferior_saved_pc
> 0) break;
166 #if defined(PYRAMID_CONTROL_FRAME_DEBUGGING)
167 printf("skipping kernel frame %08x, pc=%08x\n", datum
,
169 #endif /* PYRAMID_CONTROL_FRAME_DEBUGGING */
171 datum
-= CONTROL_STACK_FRAME_SIZE
;
174 reg_buf
[CSP_REGNUM
] = datum
;
175 supply_register(CSP_REGNUM
, reg_buf
+CSP_REGNUM
);
176 #ifdef PYRAMID_CONTROL_FRAME_DEBUGGING
177 if (skipped_frames
) {
179 "skipped %d frames from %x to %x; cfp was %x, now %x\n",
180 skipped_frames
, reg_buf
[CSP_REGNUM
]);
182 #endif /* PYRAMID_CONTROL_FRAME_DEBUGGING */
186 /* Store our register values back into the inferior.
187 If REGNO is -1, do this for all registers.
188 Otherwise, REGNO specifies which register (so we can save time). */
190 store_inferior_registers (regno
)
193 register unsigned int regaddr
;
198 if ((0 <= regno
) && (regno
< 64)) {
199 /*regaddr = register_addr (regno, offset);*/
202 ptrace (6, inferior_pid
, regaddr
, read_register (regno
));
205 sprintf (buf
, "writing register number %d", regno
);
206 perror_with_name (buf
);
210 else for (regno
= 0; regno
< NUM_REGS
; regno
++)
212 /*regaddr = register_addr (regno, offset);*/
215 ptrace (6, inferior_pid
, regaddr
, read_register (regno
));
218 sprintf (buf
, "writing all regs, number %d", regno
);
219 perror_with_name (buf
);
224 /* Copy LEN bytes from inferior's memory starting at MEMADDR
225 to debugger memory starting at MYADDR.
226 On failure (cannot read from inferior, usually because address is out
227 of bounds) returns the value of errno. */
230 read_inferior_memory (memaddr
, myaddr
, len
)
236 /* Round starting address down to longword boundary. */
237 register CORE_ADDR addr
= memaddr
& - sizeof (int);
238 /* Round ending address up; get number of longwords that makes. */
240 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
241 /* Allocate buffer of that many longwords. */
242 register int *buffer
= (int *) alloca (count
* sizeof (int));
245 /* Read all the longwords */
246 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
250 /*This is now done by read_memory, because when this function did it,
251 reading a byte or short int hardware port read whole longs, causing
253 such as bus errors and unexpected hardware operation. This would
254 also be a problem with ptrace if the inferior process could read
255 or write hardware registers, but that's not usually the case. */
256 if (remote_debugging
)
257 buffer
[i
] = remote_fetch_word (addr
);
260 buffer
[i
] = ptrace (1, inferior_pid
, addr
, 0);
265 /* Copy appropriate bytes out of the buffer. */
266 bcopy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
270 /* Copy LEN bytes of data from debugger memory at MYADDR
271 to inferior's memory at MEMADDR.
272 On failure (cannot write the inferior)
273 returns the value of errno. */
276 write_inferior_memory (memaddr
, myaddr
, len
)
282 /* Round starting address down to longword boundary. */
283 register CORE_ADDR addr
= memaddr
& - sizeof (int);
284 /* Round ending address up; get number of longwords that makes. */
286 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
287 /* Allocate buffer of that many longwords. */
288 register int *buffer
= (int *) alloca (count
* sizeof (int));
291 /* Fill start and end extra bytes of buffer with existing memory data. */
293 if (remote_debugging
)
294 buffer
[0] = remote_fetch_word (addr
);
296 buffer
[0] = ptrace (1, inferior_pid
, addr
, 0);
300 if (remote_debugging
)
302 = remote_fetch_word (addr
+ (count
- 1) * sizeof (int));
305 = ptrace (1, inferior_pid
,
306 addr
+ (count
- 1) * sizeof (int), 0);
309 /* Copy data to be written over corresponding part of buffer */
311 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
313 /* Write the entire buffer. */
315 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
318 if (remote_debugging
)
319 remote_store_word (addr
, buffer
[i
]);
321 ptrace (4, inferior_pid
, addr
, buffer
[i
]);
329 /*** Extensions to core and dump files, for GDB. */
331 extern unsigned int last_frame_offset
;
335 /* Can't make definitions here static, since core.c needs them
336 to do bounds checking on the core-file areas. O well. */
338 /* have two stacks: one for data, one for register windows. */
339 extern CORE_ADDR reg_stack_start
;
340 extern CORE_ADDR reg_stack_end
;
342 /* need this so we can find the global registers: they never get saved. */
343 static CORE_ADDR global_reg_offset
;
344 static CORE_ADDR last_frame_address
;
345 static CORE_ADDR last_frame_offset
;
348 /* Address in core file of start of register window stack area.
349 Don't know if is this any of meaningful, useful or necessary. */
350 static CORE_ADDR reg_stack_offset
;
352 #endif /* PYRAMID_CORE */
355 /* Work with core dump and executable files, for GDB.
356 This code would be in core.c if it weren't machine-dependent. */
359 #define N_TXTADDR(hdr) 0
360 #endif /* no N_TXTADDR */
363 #define N_DATADDR(hdr) hdr.a_text
364 #endif /* no N_DATADDR */
366 /* Make COFF and non-COFF names for things a little more compatible
367 to reduce conditionals later. */
370 #define a_magic magic
375 #define AOUTHDR struct exec
379 extern char *sys_siglist
[];
382 /* Hook for `exec_file_command' command to call. */
384 extern void (*exec_file_display_hook
) ();
386 /* File names of core file and executable file. */
388 extern char *corefile
;
389 extern char *execfile
;
391 /* Descriptors on which core file and executable file are open.
392 Note that the execchan is closed when an inferior is created
393 and reopened if the inferior dies or is killed. */
398 /* Last modification time of executable file.
399 Also used in source.c to compare against mtime of a source file. */
401 extern int exec_mtime
;
403 /* Virtual addresses of bounds of the two areas of memory in the core file. */
405 extern CORE_ADDR data_start
;
406 extern CORE_ADDR data_end
;
407 extern CORE_ADDR stack_start
;
408 extern CORE_ADDR stack_end
;
411 /* Well, "two areas of memory" on most machines; but pyramids have a
412 third area, for the register-window stack, and we need its
413 base and bound too. */
415 extern CORE_ADDR reg_stack_start
;
416 extern CORE_ADDR reg_stack_start
;
417 #endif /* PYRAMID_CORE */
419 /* Virtual addresses of bounds of two areas of memory in the exec file.
420 Note that the data area in the exec file is used only when there is no core file. */
422 extern CORE_ADDR text_start
;
423 extern CORE_ADDR text_end
;
425 extern CORE_ADDR exec_data_start
;
426 extern CORE_ADDR exec_data_end
;
428 /* Address in executable file of start of text area data. */
430 extern int text_offset
;
432 /* Address in executable file of start of data area data. */
434 extern int exec_data_offset
;
436 /* Address in core file of start of data area data. */
438 extern int data_offset
;
440 /* Address in core file of start of stack area data. */
442 extern int stack_offset
;
445 /* various coff data structures */
447 extern FILHDR file_hdr
;
448 extern SCNHDR text_hdr
;
449 extern SCNHDR data_hdr
;
451 #endif /* not COFF_FORMAT */
453 /* a.out header saved in core file. */
455 extern AOUTHDR core_aouthdr
;
457 /* a.out header of exec file. */
459 extern AOUTHDR exec_aouthdr
;
461 extern void validate_files ();
463 core_file_command (filename
, from_tty
)
468 extern char registers
[];
470 /* Discard all vestiges of any previous core file
471 and mark data and stack spaces as empty. */
483 stack_start
= STACK_END_ADDR
;
484 stack_end
= STACK_END_ADDR
;
487 reg_stack_start
= CONTROL_STACK_ADDR
;
488 reg_stack_end
= CONTROL_STACK_ADDR
; /* this isn't strictly true...*/
489 #endif /* PYRAMID_CORE */
491 /* Now, if a new core file was specified, open it and digest it. */
495 filename
= tilde_expand (filename
);
496 make_cleanup (free
, filename
);
498 if (have_inferior_p ())
499 error ("To look at a core file, you must kill the inferior with \"kill\".");
500 corechan
= open (filename
, O_RDONLY
, 0);
502 perror_with_name (filename
);
503 /* 4.2-style (and perhaps also sysV-style) core dump file. */
507 unsigned int reg_offset
;
509 val
= myread (corechan
, &u
, sizeof u
);
511 perror_with_name ("Not a core file: reading upage");
513 error ("Not a core file: could only read %d bytes", val
);
514 data_start
= exec_data_start
;
516 data_end
= data_start
+ NBPG
* u
.u_dsize
;
517 data_offset
= NBPG
* UPAGES
;
518 stack_offset
= NBPG
* (UPAGES
+ u
.u_dsize
);
520 /* find registers in core file */
521 #ifdef PYRAMID_PTRACE
522 stack_start
= stack_end
- NBPG
* u
.u_ussize
;
523 reg_stack_offset
= stack_offset
+ (NBPG
*u
.u_ussize
);
524 reg_stack_end
= reg_stack_start
+ NBPG
* u
.u_cssize
;
526 last_frame_address
= ((int) u
.u_pcb
.pcb_csp
);
527 last_frame_offset
= reg_stack_offset
+ last_frame_address
528 - CONTROL_STACK_ADDR
;
529 global_reg_offset
= (char *)&u
- (char *)&u
.u_pcb
.pcb_gr0
;
531 /* skip any control-stack frames that were executed in the
536 val
= lseek (corechan
, last_frame_offset
+(47*4), 0);
538 perror_with_name (filename
);
539 val
= myread (corechan
, buf
, sizeof buf
);
541 perror_with_name (filename
);
543 if (*(int *)buf
>= 0)
545 printf ("skipping frame %0x\n", last_frame_address
);
546 last_frame_offset
-= CONTROL_STACK_FRAME_SIZE
;
547 last_frame_address
-= CONTROL_STACK_FRAME_SIZE
;
549 reg_offset
= last_frame_offset
;
551 #if 1 || defined(PYRAMID_CONTROL_FRAME_DEBUGGING)
552 printf ("Control stack pointer = 0x%08x\n",
554 printf ("offset to control stack %d outermost frame %d (%0x)\n",
555 reg_stack_offset
, reg_offset
, last_frame_address
);
556 #endif /* PYRAMID_CONTROL_FRAME_DEBUGGING */
558 #else /* not PYRAMID_CORE */
559 stack_start
= stack_end
- NBPG
* u
.u_ssize
;
560 reg_offset
= (int) u
.u_ar0
- KERNEL_U_ADDR
;
561 #endif /* not PYRAMID_CORE */
563 #ifdef __not_on_pyr_yet
564 /* Some machines put an absolute address in here and some put
565 the offset in the upage of the regs. */
566 reg_offset
= (int) u
.u_ar0
;
567 if (reg_offset
> NBPG
* UPAGES
)
568 reg_offset
-= KERNEL_U_ADDR
;
571 /* I don't know where to find this info.
572 So, for now, mark it as not available. */
573 N_SET_MAGIC (core_aouthdr
, 0);
575 /* Read the register values out of the core file and store
576 them where `read_register' will find them. */
581 for (regno
= 0; regno
< 64; regno
++)
583 char buf
[MAX_REGISTER_RAW_SIZE
];
585 val
= lseek (corechan
, register_addr (regno
, reg_offset
), 0);
587 || (val
= myread (corechan
, buf
, sizeof buf
)) < 0)
589 char * buffer
= (char *) alloca (strlen (reg_names
[regno
])
591 strcpy (buffer
, "Reading register ");
592 strcat (buffer
, reg_names
[regno
]);
594 perror_with_name (buffer
);
598 perror_with_name (filename
);
599 #ifdef PYRAMID_CONTROL_FRAME_DEBUGGING
600 printf ("[reg %s(%d), offset in file %s=0x%0x, addr =0x%0x, =%0x]\n",
601 reg_names
[regno
], regno
, filename
,
602 register_addr(regno
, reg_offset
),
603 regno
* 4 + last_frame_address
,
605 #endif /* PYRAMID_CONTROL_FRAME_DEBUGGING */
606 supply_register (regno
, buf
);
610 if (filename
[0] == '/')
611 corefile
= savestring (filename
, strlen (filename
));
614 corefile
= concat (current_directory
, "/", filename
);
617 #if 1 || defined(PYRAMID_CONTROL_FRAME_DEBUGGING)
618 printf ("Providing CSP (%0x) as nominal address of current frame.\n",
620 #endif PYRAMID_CONTROL_FRAME_DEBUGGING
621 /* FIXME: Which of the following is correct? */
623 set_current_frame ( create_new_frame (read_register (FP_REGNUM
),
626 set_current_frame ( create_new_frame (last_frame_address
,
630 select_frame (get_current_frame (), 0);
634 printf ("No core file now.\n");
637 exec_file_command (filename
, from_tty
)
643 /* Eliminate all traces of old exec file.
644 Mark text segment as empty. */
650 data_end
-= exec_data_start
;
659 /* Now open and digest the file the user requested, if any. */
663 filename
= tilde_expand (filename
);
664 make_cleanup (free
, filename
);
666 execchan
= openp (getenv ("PATH"), 1, filename
, O_RDONLY
, 0,
669 perror_with_name (filename
);
672 #else /* not COFF_FORMAT */
678 val
= myread (execchan
, &exec_aouthdr
, sizeof (AOUTHDR
));
681 perror_with_name (filename
);
683 text_start
= N_TXTADDR (exec_aouthdr
);
684 exec_data_start
= N_DATADDR (exec_aouthdr
);
687 text_offset
= N_TXTOFF (exec_aouthdr
);
688 exec_data_offset
= N_TXTOFF (exec_aouthdr
) + exec_aouthdr
.a_text
;
690 text_end
= text_start
+ exec_aouthdr
.a_text
;
691 exec_data_end
= exec_data_start
+ exec_aouthdr
.a_data
;
692 data_start
= exec_data_start
;
693 data_end
+= exec_data_start
;
695 fstat (execchan
, &st_exec
);
696 exec_mtime
= st_exec
.st_mtime
;
698 #endif /* not COFF_FORMAT */
703 printf ("No exec file now.\n");
705 /* Tell display code (if any) about the changed file name. */
706 if (exec_file_display_hook
)
707 (*exec_file_display_hook
) (filename
);
710 /*** Prettier register printing. ***/
712 /* Print registers in the same format as pyramid's dbx, adb, sdb. */
713 pyr_print_registers(reg_buf
, regnum
)
720 for (regno
= 0; regno
< 16; regno
++) {
721 printf
/*_filtered*/ ("%6.6s: %8x %6.6s: %8x %6s: %8x %6s: %8x\n",
722 reg_names
[regno
], reg_buf
[regno
],
723 reg_names
[regno
+16], reg_buf
[regno
+16],
724 reg_names
[regno
+32], reg_buf
[regno
+32],
725 reg_names
[regno
+48], reg_buf
[regno
+48]);
727 usp
= ptrace (3, inferior_pid
,
728 ((char *)&u
.u_pcb
.pcb_usp
) -
730 ksp
= ptrace (3, inferior_pid
,
731 ((char *)&u
.u_pcb
.pcb_ksp
) -
733 printf
/*_filtered*/ ("\n%6.6s: %8x %6.6s: %8x (%08x) %6.6s %8x\n",
734 reg_names
[CSP_REGNUM
],reg_buf
[CSP_REGNUM
],
735 reg_names
[KSP_REGNUM
], reg_buf
[KSP_REGNUM
], ksp
,
739 /* Print the register regnum, or all registers if regnum is -1. */
741 pyr_do_registers_info (regnum
)
744 /* On a pyr, we know a virtual register can always fit in an long.
745 Here (and elsewhere) we take advantage of that. Yuk. */
746 long raw_regs
[MAX_REGISTER_RAW_SIZE
*NUM_REGS
];
749 for (i
= 0 ; i
< 64 ; i
++) {
750 read_relative_register_raw_bytes(i
, raw_regs
+i
);
753 pyr_print_registers (raw_regs
, regnum
);
755 for (i
= 0; i
< NUM_REGS
; i
++)
757 long val
= raw_regs
[i
];
759 fputs_filtered (reg_names
[i
], stdout
);
760 printf_filtered(":");
761 print_spaces_filtered (6 - strlen (reg_names
[i
]), stdout
);
763 printf_filtered ("0");
765 printf_filtered ("0x%08x %d", val
, val
);
766 printf_filtered("\n");
770 /*** Debugging editions of various macros from m-pyr.h ****/
772 CORE_ADDR
frame_locals_address (frame
)
775 register int addr
= find_saved_register (frame
,CFP_REGNUM
);
776 register int result
= read_memory_integer (addr
, 4);
777 #ifdef PYRAMID_CONTROL_FRAME_DEBUGGING
779 "\t[[..frame_locals:%8x, %s= %x @%x fcfp= %x foo= %x\n\t gr13=%x pr13=%x tr13=%x @%x]]\n",
781 reg_names
[CFP_REGNUM
],
783 frame
->frame_cfp
, (CFP_REGNUM
),
786 read_register(13), read_register(29), read_register(61),
787 find_saved_register(frame
, 61));
788 #endif /* PYRAMID_CONTROL_FRAME_DEBUGGING */
790 /* FIXME: I thought read_register (CFP_REGNUM) should be the right answer;
791 or at least CFP_REGNUM relative to FRAME (ie, result).
792 There seems to be a bug in the way the innermost frame is set up. */
794 return ((frame
->next
) ? result
: frame
->frame_cfp
);
797 CORE_ADDR
frame_args_addr (frame
)
800 register int addr
= find_saved_register (frame
,CFP_REGNUM
);
801 register int result
= read_memory_integer (addr
, 4);
803 #ifdef PYRAMID_CONTROL_FRAME_DEBUGGING
805 "\t[[..frame_args:%8x, %s= %x @%x fcfp= %x r_r= %x\n\t gr13=%x pr13=%x tr13=%x @%x]]\n",
807 reg_names
[CFP_REGNUM
],
809 frame
->frame_cfp
, read_register(CFP_REGNUM
),
811 read_register(13), read_register(29), read_register(61),
812 find_saved_register(frame
, 61));
813 #endif /* PYRAMID_CONTROL_FRAME_DEBUGGING */
815 /* FIXME: I thought read_register (CFP_REGNUM) should be the right answer;
816 or at least CFP_REGNUM relative to FRAME (ie, result).
817 There seems to be a bug in the way the innermost frame is set up. */
818 return ((frame
->next
) ? result
: frame
->frame_cfp
);