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. */
26 #include <sys/param.h>
30 #include <sys/ioctl.h>
39 /* This function simply calls ptrace with the given arguments.
40 It exists so that all calls to ptrace are isolated in this
41 machine-dependent file. */
43 call_ptrace (request
, pid
, arg3
, arg4
)
44 int request
, pid
, arg3
, arg4
;
46 return ptrace (request
, pid
, arg3
, arg4
);
53 if (inferior_pid
== 0)
55 ptrace (8, inferior_pid
, 0, 0);
60 /* This is used when GDB is exiting. It gives less chance of error.*/
66 if (inferior_pid
== 0)
68 ptrace (8, inferior_pid
, 0, 0);
72 /* Resume execution of the inferior process.
73 If STEP is nonzero, single-step it.
74 If SIGNAL is nonzero, give it that signal. */
83 remote_resume (step
, signal
);
86 ptrace (step
? 9 : 7, inferior_pid
, 1, signal
);
88 perror_with_name ("ptrace");
93 fetch_inferior_registers ()
96 register unsigned int regaddr
;
97 char buf
[MAX_REGISTER_RAW_SIZE
];
101 unsigned int offset
= (char *) &u
.u_ar0
- (char *) &u
;
102 offset
= ptrace (3, inferior_pid
, offset
, 0) - KERNEL_U_ADDR
;
104 for (regno
= 0; regno
< NUM_REGS
; regno
++)
106 regaddr
= register_addr (regno
, offset
);
107 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
109 *(int *) &buf
[i
] = ptrace (3, inferior_pid
, regaddr
, 0);
110 regaddr
+= sizeof (int);
112 supply_register (regno
, buf
);
116 /* Store our register values back into the inferior.
117 If REGNO is -1, do this for all registers.
118 Otherwise, REGNO specifies which register (so we can save time). */
120 store_inferior_registers (regno
)
123 register unsigned int regaddr
;
127 unsigned int offset
= (char *) &u
.u_ar0
- (char *) &u
;
128 offset
= ptrace (3, inferior_pid
, offset
, 0) - KERNEL_U_ADDR
;
132 regaddr
= register_addr (regno
, offset
);
134 ptrace (6, inferior_pid
, regaddr
, read_register (regno
));
137 sprintf (buf
, "writing register number %d", regno
);
138 perror_with_name (buf
);
141 else for (regno
= 0; regno
< NUM_REGS
; regno
++)
143 regaddr
= register_addr (regno
, offset
);
145 ptrace (6, inferior_pid
, regaddr
, read_register (regno
));
148 sprintf (buf
, "writing register number %d", regno
);
149 perror_with_name (buf
);
154 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
155 in the NEW_SUN_PTRACE case.
156 It ought to be straightforward. But it appears that writing did
157 not write the data that I specified. I cannot understand where
158 it got the data that it actually did write. */
160 /* Copy LEN bytes from inferior's memory starting at MEMADDR
161 to debugger memory starting at MYADDR.
162 On failure (cannot read from inferior, usually because address is out
163 of bounds) returns the value of errno. */
166 read_inferior_memory (memaddr
, myaddr
, len
)
172 /* Round starting address down to longword boundary. */
173 register CORE_ADDR addr
= memaddr
& - sizeof (int);
174 /* Round ending address up; get number of longwords that makes. */
176 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
177 /* Allocate buffer of that many longwords. */
178 register int *buffer
= (int *) alloca (count
* sizeof (int));
181 /* Read all the longwords */
182 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
185 if (remote_debugging
)
186 buffer
[i
] = remote_fetch_word (addr
);
188 buffer
[i
] = ptrace (1, inferior_pid
, addr
, 0);
193 /* Copy appropriate bytes out of the buffer. */
194 bcopy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
198 /* Copy LEN bytes of data from debugger memory at MYADDR
199 to inferior's memory at MEMADDR.
200 On failure (cannot write the inferior)
201 returns the value of errno. */
204 write_inferior_memory (memaddr
, myaddr
, len
)
210 /* Round starting address down to longword boundary. */
211 register CORE_ADDR addr
= memaddr
& - sizeof (int);
212 /* Round ending address up; get number of longwords that makes. */
214 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
215 /* Allocate buffer of that many longwords. */
216 register int *buffer
= (int *) alloca (count
* sizeof (int));
219 /* Fill start and end extra bytes of buffer with existing memory data. */
221 if (remote_debugging
)
222 buffer
[0] = remote_fetch_word (addr
);
224 buffer
[0] = ptrace (1, inferior_pid
, addr
, 0);
228 if (remote_debugging
)
230 = remote_fetch_word (addr
+ (count
- 1) * sizeof (int));
233 = ptrace (1, inferior_pid
,
234 addr
+ (count
- 1) * sizeof (int), 0);
237 /* Copy data to be written over corresponding part of buffer */
239 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
241 /* Write the entire buffer. */
243 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
246 if (remote_debugging
)
247 remote_store_word (addr
, buffer
[i
]);
249 ptrace (4, inferior_pid
, addr
, buffer
[i
]);
257 /* Work with core dump and executable files, for GDB.
258 This code would be in core.c if it weren't machine-dependent. */
260 /* This should probably be deleted. */
261 /* Recognize COFF format systems because a.out.h defines AOUTHDR. */
267 #define N_TXTADDR(hdr) 0
268 #endif /* no N_TXTADDR */
271 #define N_DATADDR(hdr) hdr.a_text
272 #endif /* no N_DATADDR */
274 /* Make COFF and non-COFF names for things a little more compatible
275 to reduce conditionals later. */
278 #define a_magic magic
283 #define AOUTHDR struct exec
287 extern char *sys_siglist
[];
290 /* Hook for `exec_file_command' command to call. */
292 extern void (*exec_file_display_hook
) ();
294 /* File names of core file and executable file. */
296 extern char *corefile
;
297 extern char *execfile
;
299 /* Descriptors on which core file and executable file are open.
300 Note that the execchan is closed when an inferior is created
301 and reopened if the inferior dies or is killed. */
306 /* Last modification time of executable file.
307 Also used in source.c to compare against mtime of a source file. */
309 extern int exec_mtime
;
311 /* Virtual addresses of bounds of the two areas of memory in the core file. */
313 extern CORE_ADDR data_start
;
314 extern CORE_ADDR data_end
;
315 extern CORE_ADDR stack_start
;
316 extern CORE_ADDR stack_end
;
318 /* Virtual addresses of bounds of two areas of memory in the exec file.
319 Note that the data area in the exec file is used only when there is no core file. */
321 extern CORE_ADDR text_start
;
322 extern CORE_ADDR text_end
;
324 extern CORE_ADDR exec_data_start
;
325 extern CORE_ADDR exec_data_end
;
327 /* Address in executable file of start of text area data. */
329 extern int text_offset
;
331 /* Address in executable file of start of data area data. */
333 extern int exec_data_offset
;
335 /* Address in core file of start of data area data. */
337 extern int data_offset
;
339 /* Address in core file of start of stack area data. */
341 extern int stack_offset
;
344 /* various coff data structures */
346 extern FILHDR file_hdr
;
347 extern SCNHDR text_hdr
;
348 extern SCNHDR data_hdr
;
350 #endif /* not COFF_FORMAT */
352 /* a.out header saved in core file. */
354 extern AOUTHDR core_aouthdr
;
356 /* a.out header of exec file. */
358 extern AOUTHDR exec_aouthdr
;
360 extern void validate_files ();
362 core_file_command (filename
, from_tty
)
367 extern char registers
[];
369 /* Discard all vestiges of any previous core file
370 and mark data and stack spaces as empty. */
382 stack_start
= STACK_END_ADDR
;
383 stack_end
= STACK_END_ADDR
;
385 /* Now, if a new core file was specified, open it and digest it. */
389 filename
= tilde_expand (filename
);
390 make_cleanup (free
, filename
);
392 if (have_inferior_p ())
393 error ("To look at a core file, you must kill the inferior with \"kill\".");
394 corechan
= open (filename
, O_RDONLY
, 0);
396 perror_with_name (filename
);
397 /* 4.2-style (and perhaps also sysV-style) core dump file. */
402 val
= myread (corechan
, &u
, sizeof u
);
404 perror_with_name (filename
);
405 data_start
= exec_data_start
;
407 data_end
= data_start
+ NBPG
* u
.u_dsize
;
408 stack_start
= stack_end
- NBPG
* u
.u_ssize
;
409 data_offset
= NBPG
* UPAGES
;
410 stack_offset
= NBPG
* (UPAGES
+ u
.u_dsize
);
411 reg_offset
= (int) u
.u_ar0
- KERNEL_U_ADDR
;
413 /* I don't know where to find this info.
414 So, for now, mark it as not available. */
415 core_aouthdr
.a_magic
= 0;
417 /* Read the register values out of the core file and store
418 them where `read_register' will find them. */
423 for (regno
= 0; regno
< NUM_REGS
; regno
++)
425 char buf
[MAX_REGISTER_RAW_SIZE
];
427 val
= lseek (corechan
, register_addr (regno
, reg_offset
), 0);
429 perror_with_name (filename
);
431 val
= myread (corechan
, buf
, sizeof buf
);
433 perror_with_name (filename
);
434 supply_register (regno
, buf
);
438 if (filename
[0] == '/')
439 corefile
= savestring (filename
, strlen (filename
));
442 corefile
= concat (current_directory
, "/", filename
);
445 set_current_frame ( create_new_frame (read_register (FP_REGNUM
),
447 select_frame (get_current_frame (), 0);
451 printf ("No core file now.\n");
454 exec_file_command (filename
, from_tty
)
460 /* Eliminate all traces of old exec file.
461 Mark text segment as empty. */
467 data_end
-= exec_data_start
;
476 /* Now open and digest the file the user requested, if any. */
480 filename
= tilde_expand (filename
);
481 make_cleanup (free
, filename
);
483 execchan
= openp (getenv ("PATH"), 1, filename
, O_RDONLY
, 0,
486 perror_with_name (filename
);
493 if (read_file_hdr (execchan
, &file_hdr
) < 0)
494 error ("\"%s\": not in executable format.", execfile
);
496 aout_hdrsize
= file_hdr
.f_opthdr
;
497 num_sections
= file_hdr
.f_nscns
;
499 if (read_aout_hdr (execchan
, &exec_aouthdr
, aout_hdrsize
) < 0)
500 error ("\"%s\": can't read optional aouthdr", execfile
);
502 if (read_section_hdr (execchan
, _TEXT
, &text_hdr
, num_sections
,
504 error ("\"%s\": can't read text section header", execfile
);
506 if (read_section_hdr (execchan
, _DATA
, &data_hdr
, num_sections
,
508 error ("\"%s\": can't read data section header", execfile
);
510 text_start
= exec_aouthdr
.text_start
;
511 text_end
= text_start
+ exec_aouthdr
.tsize
;
512 text_offset
= text_hdr
.s_scnptr
;
513 exec_data_start
= exec_aouthdr
.data_start
;
514 exec_data_end
= exec_data_start
+ exec_aouthdr
.dsize
;
515 exec_data_offset
= data_hdr
.s_scnptr
;
516 data_start
= exec_data_start
;
517 data_end
+= exec_data_start
;
518 exec_mtime
= file_hdr
.f_timdat
;
520 #else /* not COFF_FORMAT */
526 val
= myread (execchan
, &exec_coffhdr
, sizeof exec_coffhdr
);
528 perror_with_name (filename
);
530 val
= myread (execchan
, &exec_aouthdr
, sizeof (AOUTHDR
));
533 perror_with_name (filename
);
535 text_start
= N_TXTADDR (exec_aouthdr
);
536 exec_data_start
= N_DATADDR (exec_aouthdr
);
538 text_offset
= N_TXTOFF (exec_coffhdr
, exec_aouthdr
);
539 exec_data_offset
= N_TXTOFF (exec_coffhdr
, exec_aouthdr
)
540 + exec_aouthdr
.a_text
;
542 text_end
= text_start
+ exec_aouthdr
.a_text
;
543 exec_data_end
= exec_data_start
+ exec_aouthdr
.a_data
;
544 data_start
= exec_data_start
;
545 data_end
+= exec_data_start
;
547 fstat (execchan
, &st_exec
);
548 exec_mtime
= st_exec
.st_mtime
;
550 #endif /* not COFF_FORMAT */
555 printf ("No exec file now.\n");
557 /* Tell display code (if any) about the changed file name. */
558 if (exec_file_display_hook
)
559 (*exec_file_display_hook
) (filename
);