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. */
27 #include <sys/types.h>
30 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 /* #include <fcntl.h> Can we live without this? */
36 #ifdef COFF_ENCAPSULATE
37 #include "a.out.encap.h"
42 #define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
45 #include <sys/user.h> /* After a.out.h */
51 /* This function simply calls ptrace with the given arguments.
52 It exists so that all calls to ptrace are isolated in this
53 machine-dependent file. */
55 call_ptrace (request
, pid
, arg3
, arg4
)
56 int request
, pid
, arg3
, arg4
;
58 return ptrace (request
, pid
, arg3
, arg4
);
65 if (inferior_pid
== 0)
67 ptrace (8, inferior_pid
, 0, 0);
72 /* This is used when GDB is exiting. It gives less chance of error.*/
78 if (inferior_pid
== 0)
80 ptrace (8, inferior_pid
, 0, 0);
84 /* Resume execution of the inferior process.
85 If STEP is nonzero, single-step it.
86 If SIGNAL is nonzero, give it that signal. */
95 remote_resume (step
, signal
);
98 ptrace (step
? 9 : 7, inferior_pid
, 1, signal
);
100 perror_with_name ("ptrace");
105 fetch_inferior_registers ()
108 register unsigned int regaddr
;
109 char buf
[MAX_REGISTER_RAW_SIZE
];
113 unsigned int offset
= (char *) &u
.u_ar0
- (char *) &u
;
114 offset
= ptrace (3, inferior_pid
, offset
, 0) - KERNEL_U_ADDR
;
116 for (regno
= 0; regno
< NUM_REGS
; regno
++)
118 regaddr
= register_addr (regno
, offset
);
119 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
121 *(int *) &buf
[i
] = ptrace (3, inferior_pid
, regaddr
, 0);
122 regaddr
+= sizeof (int);
124 supply_register (regno
, buf
);
128 /* Store our register values back into the inferior.
129 If REGNO is -1, do this for all registers.
130 Otherwise, REGNO specifies which register (so we can save time). */
132 store_inferior_registers (regno
)
135 register unsigned int regaddr
;
139 unsigned int offset
= (char *) &u
.u_ar0
- (char *) &u
;
140 offset
= ptrace (3, inferior_pid
, offset
, 0) - KERNEL_U_ADDR
;
144 regaddr
= register_addr (regno
, offset
);
146 ptrace (6, inferior_pid
, regaddr
, read_register (regno
));
149 sprintf (buf
, "writing register number %d", regno
);
150 perror_with_name (buf
);
153 else for (regno
= 0; regno
< NUM_REGS
; regno
++)
155 regaddr
= register_addr (regno
, offset
);
157 ptrace (6, inferior_pid
, regaddr
, read_register (regno
));
160 sprintf (buf
, "writing all regs, number %d", regno
);
161 perror_with_name (buf
);
166 /* Copy LEN bytes from inferior's memory starting at MEMADDR
167 to debugger memory starting at MYADDR.
168 On failure (cannot read from inferior, usually because address is out
169 of bounds) returns the value of errno. */
172 read_inferior_memory (memaddr
, myaddr
, len
)
178 /* Round starting address down to longword boundary. */
179 register CORE_ADDR addr
= memaddr
& - sizeof (int);
180 /* Round ending address up; get number of longwords that makes. */
182 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
183 /* Allocate buffer of that many longwords. */
184 register int *buffer
= (int *) alloca (count
* sizeof (int));
187 /* Read all the longwords */
188 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
192 /* This is now done by read_memory, because when this function did it,
193 reading a byte or short int hardware port read whole longs, causing
195 such as bus errors and unexpected hardware operation. This would
196 also be a problem with ptrace if the inferior process could read
197 or write hardware registers, but that's not usually the case. */
198 if (remote_debugging
)
199 buffer
[i
] = remote_fetch_word (addr
);
202 buffer
[i
] = ptrace (1, inferior_pid
, addr
, 0);
207 /* Copy appropriate bytes out of the buffer. */
208 bcopy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
212 /* Copy LEN bytes of data from debugger memory at MYADDR
213 to inferior's memory at MEMADDR.
214 On failure (cannot write the inferior)
215 returns the value of errno. */
218 write_inferior_memory (memaddr
, myaddr
, len
)
224 /* Round starting address down to longword boundary. */
225 register CORE_ADDR addr
= memaddr
& - sizeof (int);
226 /* Round ending address up; get number of longwords that makes. */
228 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
229 /* Allocate buffer of that many longwords. */
230 register int *buffer
= (int *) alloca (count
* sizeof (int));
233 /* Fill start and end extra bytes of buffer with existing memory data. */
235 if (remote_debugging
)
236 buffer
[0] = remote_fetch_word (addr
);
238 buffer
[0] = ptrace (1, inferior_pid
, addr
, 0);
242 if (remote_debugging
)
244 = remote_fetch_word (addr
+ (count
- 1) * sizeof (int));
247 = ptrace (1, inferior_pid
,
248 addr
+ (count
- 1) * sizeof (int), 0);
251 /* Copy data to be written over corresponding part of buffer */
253 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
255 /* Write the entire buffer. */
257 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
260 if (remote_debugging
)
261 remote_store_word (addr
, buffer
[i
]);
263 ptrace (4, inferior_pid
, addr
, buffer
[i
]);
271 /* Work with core dump and executable files, for GDB.
272 This code would be in core.c if it weren't machine-dependent. */
275 #define N_TXTADDR(hdr) 0
276 #endif /* no N_TXTADDR */
279 #define N_DATADDR(hdr) hdr.a_text
280 #endif /* no N_DATADDR */
282 /* Make COFF and non-COFF names for things a little more compatible
283 to reduce conditionals later. */
286 #define a_magic magic
291 #define AOUTHDR struct exec
295 extern char *sys_siglist
[];
298 /* Hook for `exec_file_command' command to call. */
300 extern void (*exec_file_display_hook
) ();
302 /* File names of core file and executable file. */
304 extern char *corefile
;
305 extern char *execfile
;
307 /* Descriptors on which core file and executable file are open.
308 Note that the execchan is closed when an inferior is created
309 and reopened if the inferior dies or is killed. */
314 /* Last modification time of executable file.
315 Also used in source.c to compare against mtime of a source file. */
317 extern int exec_mtime
;
319 /* Virtual addresses of bounds of the two areas of memory in the core file. */
321 extern CORE_ADDR data_start
;
322 extern CORE_ADDR data_end
;
323 extern CORE_ADDR stack_start
;
324 extern CORE_ADDR stack_end
;
326 /* Virtual addresses of bounds of two areas of memory in the exec file.
327 Note that the data area in the exec file is used only when there is no core file. */
329 extern CORE_ADDR text_start
;
330 extern CORE_ADDR text_end
;
332 extern CORE_ADDR exec_data_start
;
333 extern CORE_ADDR exec_data_end
;
335 /* Address in executable file of start of text area data. */
337 extern int text_offset
;
339 /* Address in executable file of start of data area data. */
341 extern int exec_data_offset
;
343 /* Address in core file of start of data area data. */
345 extern int data_offset
;
347 /* Address in core file of start of stack area data. */
349 extern int stack_offset
;
352 /* various coff data structures */
354 extern FILHDR file_hdr
;
355 extern SCNHDR text_hdr
;
356 extern SCNHDR data_hdr
;
358 #endif /* not COFF_FORMAT */
360 /* a.out header saved in core file. */
362 extern AOUTHDR core_aouthdr
;
364 /* a.out header of exec file. */
366 extern AOUTHDR exec_aouthdr
;
368 extern void validate_files ();
370 core_file_command (filename
, from_tty
)
375 extern char registers
[];
377 /* Discard all vestiges of any previous core file
378 and mark data and stack spaces as empty. */
390 stack_start
= STACK_END_ADDR
;
391 stack_end
= STACK_END_ADDR
;
393 /* Now, if a new core file was specified, open it and digest it. */
397 filename
= tilde_expand (filename
);
398 make_cleanup (free
, filename
);
400 if (have_inferior_p ())
401 error ("To look at a core file, you must kill the inferior with \"kill\".");
402 corechan
= open (filename
, O_RDONLY
, 0);
404 perror_with_name (filename
);
405 /* 4.2-style (and perhaps also sysV-style) core dump file. */
409 unsigned int reg_offset
;
411 val
= myread (corechan
, &u
, sizeof u
);
413 perror_with_name ("Not a core file: reading upage");
415 error ("Not a core file: could only read %d bytes", val
);
417 /* We are depending on exec_file_command having been called
418 previously to set exec_data_start. Since the executable
419 and the core file share the same text segment, the address
420 of the data segment will be the same in both. */
421 data_start
= exec_data_start
;
423 data_end
= data_start
+ NBPG
* u
.u_dsize
;
424 stack_start
= stack_end
- NBPG
* u
.u_ssize
;
425 data_offset
= NBPG
* UPAGES
;
426 stack_offset
= NBPG
* (UPAGES
+ u
.u_dsize
);
428 /* Some machines put an absolute address in here and some put
429 the offset in the upage of the regs. */
430 reg_offset
= (int) u
.u_ar0
;
431 if (reg_offset
> NBPG
* UPAGES
)
432 reg_offset
-= KERNEL_U_ADDR
;
434 /* I don't know where to find this info.
435 So, for now, mark it as not available. */
436 N_SET_MAGIC (core_aouthdr
, 0);
438 /* Read the register values out of the core file and store
439 them where `read_register' will find them. */
444 for (regno
= 0; regno
< NUM_REGS
; regno
++)
446 char buf
[MAX_REGISTER_RAW_SIZE
];
448 val
= lseek (corechan
, register_addr (regno
, reg_offset
), 0);
450 || (val
= myread (corechan
, buf
, sizeof buf
)) < 0)
452 char * buffer
= (char *) alloca (strlen (reg_names
[regno
])
454 strcpy (buffer
, "Reading register ");
455 strcat (buffer
, reg_names
[regno
]);
457 perror_with_name (buffer
);
460 supply_register (regno
, buf
);
464 if (filename
[0] == '/')
465 corefile
= savestring (filename
, strlen (filename
));
468 corefile
= concat (current_directory
, "/", filename
);
471 set_current_frame ( create_new_frame (read_register (FP_REGNUM
),
473 select_frame (get_current_frame (), 0);
477 printf ("No core file now.\n");
480 exec_file_command (filename
, from_tty
)
486 /* Eliminate all traces of old exec file.
487 Mark text segment as empty. */
493 data_end
-= exec_data_start
;
502 /* Now open and digest the file the user requested, if any. */
506 filename
= tilde_expand (filename
);
507 make_cleanup (free
, filename
);
509 execchan
= openp (getenv ("PATH"), 1, filename
, O_RDONLY
, 0,
512 perror_with_name (filename
);
519 if (read_file_hdr (execchan
, &file_hdr
) < 0)
520 error ("\"%s\": not in executable format.", execfile
);
522 aout_hdrsize
= file_hdr
.f_opthdr
;
523 num_sections
= file_hdr
.f_nscns
;
525 if (read_aout_hdr (execchan
, &exec_aouthdr
, aout_hdrsize
) < 0)
526 error ("\"%s\": can't read optional aouthdr", execfile
);
528 if (read_section_hdr (execchan
, _TEXT
, &text_hdr
, num_sections
,
530 error ("\"%s\": can't read text section header", execfile
);
532 if (read_section_hdr (execchan
, _DATA
, &data_hdr
, num_sections
,
534 error ("\"%s\": can't read data section header", execfile
);
536 text_start
= exec_aouthdr
.text_start
;
537 text_end
= text_start
+ exec_aouthdr
.tsize
;
538 text_offset
= text_hdr
.s_scnptr
;
539 exec_data_start
= exec_aouthdr
.data_start
;
540 exec_data_end
= exec_data_start
+ exec_aouthdr
.dsize
;
541 exec_data_offset
= data_hdr
.s_scnptr
;
542 data_start
= exec_data_start
;
543 data_end
+= exec_data_start
;
544 exec_mtime
= file_hdr
.f_timdat
;
546 #else /* not COFF_FORMAT */
550 #ifdef HEADER_SEEK_FD
551 HEADER_SEEK_FD (execchan
);
554 val
= myread (execchan
, &exec_aouthdr
, sizeof (AOUTHDR
));
557 perror_with_name (filename
);
559 text_start
= N_TXTADDR (exec_aouthdr
);
560 exec_data_start
= N_DATADDR (exec_aouthdr
);
562 text_offset
= N_TXTOFF (exec_aouthdr
);
563 exec_data_offset
= N_TXTOFF (exec_aouthdr
) + exec_aouthdr
.a_text
;
565 text_end
= text_start
+ exec_aouthdr
.a_text
;
566 exec_data_end
= exec_data_start
+ exec_aouthdr
.a_data
;
567 data_start
= exec_data_start
;
568 data_end
+= exec_data_start
;
570 if (fstat (execchan
, &st_exec
) < 0)
571 perror_with_name (filename
);
572 exec_mtime
= st_exec
.st_mtime
;
574 #endif /* not COFF_FORMAT */
579 printf ("No exec file now.\n");
581 /* Tell display code (if any) about the changed file name. */
582 if (exec_file_display_hook
)
583 (*exec_file_display_hook
) (filename
);