1 /* Low level interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1986, 1987, 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. */
27 #include <sys/param.h>
31 #include <sys/ioctl.h>
34 #include <sys/ptrace.h>
44 /* This function simply calls ptrace with the given arguments.
45 It exists so that all calls to ptrace are isolated in this
46 machine-dependent file. */
48 call_ptrace (request
, pid
, arg3
, arg4
)
49 int request
, pid
, arg3
, arg4
;
51 return ptrace (request
, pid
, arg3
, arg4
);
56 extern int attach_flag
;
58 /* Start debugging the process whose number is PID. */
64 ptrace (PT_ATTACH
, pid
, 0, 0);
66 perror_with_name ("ptrace");
71 /* Stop debugging the process whose number is PID
72 and continue it with signal number SIGNAL.
73 SIGNAL = 0 means just continue it. */
80 ptrace (PT_DETACH
, inferior_pid
, 1, signal
);
82 perror_with_name ("ptrace");
85 #endif /* ATTACH_DETACH */
91 if (inferior_pid
== 0)
93 ptrace (8, inferior_pid
, 0, 0);
98 /* This is used when GDB is exiting. It gives less chance of error.*/
100 kill_inferior_fast ()
102 if (remote_debugging
)
104 if (inferior_pid
== 0)
106 ptrace (8, inferior_pid
, 0, 0);
110 /* Resume execution of the inferior process.
111 If STEP is nonzero, single-step it.
112 If SIGNAL is nonzero, give it that signal. */
115 resume (step
, signal
)
120 if (remote_debugging
)
121 remote_resume (step
, signal
);
124 ptrace (step
? 9 : 7, inferior_pid
, 1, signal
);
126 perror_with_name ("ptrace");
130 #define INFERIOR_AR0(u) \
132 (PT_RUAREA, inferior_pid, ((char *) &u.u_ar0 - (char *) &u), 0)) \
136 fetch_inferior_register (regno
, regaddr
)
138 register unsigned int regaddr
;
140 #ifndef HPUX_VERSION_5
141 if (regno
== PS_REGNUM
)
143 union { int i
; short s
[2]; } ps_val
;
146 ps_val
.i
= (ptrace (PT_RUAREA
, inferior_pid
, regaddr
, 0));
147 regval
= ps_val
.s
[0];
148 supply_register (regno
, ®val
);
151 #endif /* not HPUX_VERSION_5 */
153 char buf
[MAX_REGISTER_RAW_SIZE
];
156 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
158 *(int *) &buf
[i
] = ptrace (PT_RUAREA
, inferior_pid
, regaddr
, 0);
159 regaddr
+= sizeof (int);
161 supply_register (regno
, buf
);
167 store_inferior_register_1 (regno
, regaddr
, value
)
169 unsigned int regaddr
;
173 ptrace (PT_WUAREA
, inferior_pid
, regaddr
, value
);
175 /* HP-UX randomly sets errno to non-zero for regno == 25.
176 However, the value is correctly written, so ignore errno. */
181 sprintf (string_buf
, "writing register number %d", regno
);
182 perror_with_name (string_buf
);
189 store_inferior_register (regno
, regaddr
)
191 register unsigned int regaddr
;
193 #ifndef HPUX_VERSION_5
194 if (regno
== PS_REGNUM
)
196 union { int i
; short s
[2]; } ps_val
;
198 ps_val
.i
= (ptrace (PT_RUAREA
, inferior_pid
, regaddr
, 0));
199 ps_val
.s
[0] = (read_register (regno
));
200 store_inferior_register_1 (regno
, regaddr
, ps_val
.i
);
203 #endif /* not HPUX_VERSION_5 */
205 char buf
[MAX_REGISTER_RAW_SIZE
];
207 extern char registers
[];
209 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
211 store_inferior_register_1
213 (*(int *) ®isters
[(REGISTER_BYTE (regno
)) + i
]));
214 regaddr
+= sizeof (int);
221 fetch_inferior_registers ()
225 register unsigned int ar0_offset
;
226 extern char registers
[];
228 if (remote_debugging
)
229 remote_fetch_registers (registers
);
232 ar0_offset
= (INFERIOR_AR0 (u
));
233 for (regno
= 0; (regno
< FP0_REGNUM
); regno
++)
234 fetch_inferior_register (regno
, (REGISTER_ADDR (ar0_offset
, regno
)));
235 for (; (regno
< NUM_REGS
); regno
++)
236 fetch_inferior_register (regno
, (FP_REGISTER_ADDR (u
, regno
)));
240 /* Store our register values back into the inferior.
241 If REGNO is -1, do this for all registers.
242 Otherwise, REGNO specifies which register (so we can save time). */
244 store_inferior_registers (regno
)
248 register unsigned int ar0_offset
;
249 extern char registers
[];
251 if (remote_debugging
)
252 remote_store_registers (registers
);
255 if (regno
>= FP0_REGNUM
)
257 store_inferior_register (regno
, (FP_REGISTER_ADDR (u
, regno
)));
261 ar0_offset
= (INFERIOR_AR0 (u
));
264 store_inferior_register (regno
, (REGISTER_ADDR (ar0_offset
, regno
)));
268 for (regno
= 0; (regno
< FP0_REGNUM
); regno
++)
269 store_inferior_register (regno
, (REGISTER_ADDR (ar0_offset
, regno
)));
270 for (; (regno
< NUM_REGS
); regno
++)
271 store_inferior_register (regno
, (FP_REGISTER_ADDR (u
, regno
)));
277 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
278 in the NEW_SUN_PTRACE case.
279 It ought to be straightforward. But it appears that writing did
280 not write the data that I specified. I cannot understand where
281 it got the data that it actually did write. */
283 /* Copy LEN bytes from inferior's memory starting at MEMADDR
284 to debugger memory starting at MYADDR.
285 On failure (cannot read from inferior, usually because address is out
286 of bounds) returns the value of errno. */
289 read_inferior_memory (memaddr
, myaddr
, len
)
295 /* Round starting address down to longword boundary. */
296 register CORE_ADDR addr
= memaddr
& - sizeof (int);
297 /* Round ending address up; get number of longwords that makes. */
299 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
300 /* Allocate buffer of that many longwords. */
301 register int *buffer
= (int *) alloca (count
* sizeof (int));
304 /* Read all the longwords */
305 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
308 if (remote_debugging
)
309 buffer
[i
] = remote_fetch_word (addr
);
311 buffer
[i
] = ptrace (1, inferior_pid
, addr
, 0);
316 /* Copy appropriate bytes out of the buffer. */
317 bcopy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
321 /* Copy LEN bytes of data from debugger memory at MYADDR
322 to inferior's memory at MEMADDR.
323 On failure (cannot write the inferior)
324 returns the value of errno. */
327 write_inferior_memory (memaddr
, myaddr
, len
)
333 /* Round starting address down to longword boundary. */
334 register CORE_ADDR addr
= memaddr
& - sizeof (int);
335 /* Round ending address up; get number of longwords that makes. */
337 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
338 /* Allocate buffer of that many longwords. */
339 register int *buffer
= (int *) alloca (count
* sizeof (int));
342 /* Fill start and end extra bytes of buffer with existing memory data. */
344 if (remote_debugging
)
345 buffer
[0] = remote_fetch_word (addr
);
347 buffer
[0] = ptrace (1, inferior_pid
, addr
, 0);
351 if (remote_debugging
)
353 = remote_fetch_word (addr
+ (count
- 1) * sizeof (int));
356 = ptrace (1, inferior_pid
,
357 addr
+ (count
- 1) * sizeof (int), 0);
360 /* Copy data to be written over corresponding part of buffer */
362 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
364 /* Write the entire buffer. */
366 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
369 if (remote_debugging
)
370 remote_store_word (addr
, buffer
[i
]);
372 ptrace (4, inferior_pid
, addr
, buffer
[i
]);
380 /* Work with core dump and executable files, for GDB.
381 This code would be in core.c if it weren't machine-dependent. */
383 /* This should probably be deleted. */
384 /* Recognize COFF format systems because a.out.h defines AOUTHDR. */
389 #ifdef HPUX_VERSION_5
390 #define e_PS e_regs[PS]
391 #define e_PC e_regs[PC]
392 #endif /* HPUX_VERSION_5 */
396 #define N_TXTADDR(hdr) 0
397 #endif /* no N_TXTADDR */
400 #define N_DATADDR(hdr) hdr.a_text
401 #endif /* no N_DATADDR */
403 /* Make COFF and non-COFF names for things a little more compatible
404 to reduce conditionals later. */
407 #define a_magic magic
412 #define AOUTHDR struct exec
416 extern char *sys_siglist
[];
419 /* Hook for `exec_file_command' command to call. */
421 extern void (*exec_file_display_hook
) ();
423 /* File names of core file and executable file. */
425 extern char *corefile
;
426 extern char *execfile
;
428 /* Descriptors on which core file and executable file are open.
429 Note that the execchan is closed when an inferior is created
430 and reopened if the inferior dies or is killed. */
435 /* Last modification time of executable file.
436 Also used in source.c to compare against mtime of a source file. */
438 extern int exec_mtime
;
440 /* Virtual addresses of bounds of the two areas of memory in the core file. */
442 extern CORE_ADDR data_start
;
443 extern CORE_ADDR data_end
;
444 extern CORE_ADDR stack_start
;
445 extern CORE_ADDR stack_end
;
447 /* Virtual addresses of bounds of two areas of memory in the exec file.
448 Note that the data area in the exec file is used only when there is no core file. */
450 extern CORE_ADDR text_start
;
451 extern CORE_ADDR text_end
;
453 extern CORE_ADDR exec_data_start
;
454 extern CORE_ADDR exec_data_end
;
456 /* Address in executable file of start of text area data. */
458 extern int text_offset
;
460 /* Address in executable file of start of data area data. */
462 extern int exec_data_offset
;
464 /* Address in core file of start of data area data. */
466 extern int data_offset
;
468 /* Address in core file of start of stack area data. */
470 extern int stack_offset
;
473 /* various coff data structures */
475 extern FILHDR file_hdr
;
476 extern SCNHDR text_hdr
;
477 extern SCNHDR data_hdr
;
479 #endif /* not COFF_FORMAT */
481 /* a.out header saved in core file. */
483 extern AOUTHDR core_aouthdr
;
485 /* a.out header of exec file. */
487 extern AOUTHDR exec_aouthdr
;
489 extern void validate_files ();
491 core_file_command (filename
, from_tty
)
496 extern char registers
[];
498 /* Discard all vestiges of any previous core file
499 and mark data and stack spaces as empty. */
511 stack_start
= STACK_END_ADDR
;
512 stack_end
= STACK_END_ADDR
;
514 /* Now, if a new core file was specified, open it and digest it. */
518 filename
= tilde_expand (filename
);
519 make_cleanup (free
, filename
);
521 if (have_inferior_p ())
522 error ("To look at a core file, you must kill the inferior with \"kill\".");
523 corechan
= open (filename
, O_RDONLY
, 0);
525 perror_with_name (filename
);
526 /* 4.2-style (and perhaps also sysV-style) core dump file. */
532 val
= myread (corechan
, &u
, sizeof u
);
534 perror_with_name (filename
);
535 data_start
= exec_data_start
;
537 data_end
= data_start
+ NBPG
* u
.u_dsize
;
538 stack_start
= stack_end
- NBPG
* u
.u_ssize
;
539 data_offset
= NBPG
* UPAGES
;
540 stack_offset
= NBPG
* (UPAGES
+ u
.u_dsize
);
541 reg_offset
= (int) u
.u_ar0
- KERNEL_U_ADDR
;
543 /* I don't know where to find this info.
544 So, for now, mark it as not available. */
545 core_aouthdr
.a_magic
= 0;
547 /* Read the register values out of the core file and store
548 them where `read_register' will find them. */
552 struct exception_stack es
;
555 val
= lseek (corechan
, (REGISTER_ADDR (reg_offset
, 0)), 0);
557 perror_with_name (filename
);
558 val
= myread (corechan
, es
,
559 ((char *) &es
.e_offset
- (char *) &es
.e_regs
[R0
]));
561 perror_with_name (filename
);
562 for (regno
= 0; (regno
< PS_REGNUM
); regno
++)
563 supply_register (regno
, &es
.e_regs
[regno
+ R0
]);
565 supply_register (regno
++, &val
);
566 supply_register (regno
++, &es
.e_PC
);
567 for (; (regno
< NUM_REGS
); regno
++)
569 char buf
[MAX_REGISTER_RAW_SIZE
];
571 val
= lseek (corechan
, (FP_REGISTER_ADDR (u
, regno
)), 0);
573 perror_with_name (filename
);
575 val
= myread (corechan
, buf
, sizeof buf
);
577 perror_with_name (filename
);
578 supply_register (regno
, buf
);
582 if (filename
[0] == '/')
583 corefile
= savestring (filename
, strlen (filename
));
586 corefile
= concat (current_directory
, "/", filename
);
589 set_current_frame ( create_new_frame (read_register (FP_REGNUM
),
591 select_frame (get_current_frame (), 0);
595 printf ("No core file now.\n");
598 exec_file_command (filename
, from_tty
)
604 /* Eliminate all traces of old exec file.
605 Mark text segment as empty. */
611 data_end
-= exec_data_start
;
620 /* Now open and digest the file the user requested, if any. */
624 filename
= tilde_expand (filename
);
625 make_cleanup (free
, filename
);
627 execchan
= openp (getenv ("PATH"), 1, filename
, O_RDONLY
, 0,
630 perror_with_name (filename
);
637 if (read_file_hdr (execchan
, &file_hdr
) < 0)
638 error ("\"%s\": not in executable format.", execfile
);
640 aout_hdrsize
= file_hdr
.f_opthdr
;
641 num_sections
= file_hdr
.f_nscns
;
643 if (read_aout_hdr (execchan
, &exec_aouthdr
, aout_hdrsize
) < 0)
644 error ("\"%s\": can't read optional aouthdr", execfile
);
646 if (read_section_hdr (execchan
, _TEXT
, &text_hdr
, num_sections
,
648 error ("\"%s\": can't read text section header", execfile
);
650 if (read_section_hdr (execchan
, _DATA
, &data_hdr
, num_sections
,
652 error ("\"%s\": can't read data section header", execfile
);
654 text_start
= exec_aouthdr
.text_start
;
655 text_end
= text_start
+ exec_aouthdr
.tsize
;
656 text_offset
= text_hdr
.s_scnptr
;
657 exec_data_start
= exec_aouthdr
.data_start
;
658 exec_data_end
= exec_data_start
+ exec_aouthdr
.dsize
;
659 exec_data_offset
= data_hdr
.s_scnptr
;
660 data_start
= exec_data_start
;
661 data_end
+= exec_data_start
;
662 exec_mtime
= file_hdr
.f_timdat
;
664 #else /* not COFF_FORMAT */
668 val
= myread (execchan
, &exec_aouthdr
, sizeof (AOUTHDR
));
671 perror_with_name (filename
);
673 text_start
= N_TXTADDR (exec_aouthdr
);
674 exec_data_start
= N_DATADDR (exec_aouthdr
);
676 text_offset
= N_TXTOFF (exec_aouthdr
);
677 exec_data_offset
= N_TXTOFF (exec_aouthdr
) + exec_aouthdr
.a_text
;
679 text_end
= text_start
+ exec_aouthdr
.a_text
;
680 exec_data_end
= exec_data_start
+ exec_aouthdr
.a_data
;
681 data_start
= exec_data_start
;
682 data_end
+= exec_data_start
;
684 fstat (execchan
, &st_exec
);
685 exec_mtime
= st_exec
.st_mtime
;
687 #endif /* not COFF_FORMAT */
692 printf ("No exec file now.\n");
694 /* Tell display code (if any) about the changed file name. */
695 if (exec_file_display_hook
)
696 (*exec_file_display_hook
) (filename
);