1 /* Low level interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1988 Free Software Foundation, Inc.
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
27 #include <sys/types.h>
31 #include <sys/param.h>
35 #include <sys/ioctl.h>
38 #ifdef COFF_ENCAPSULATE
39 #include "a.out.encap.h"
44 #define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
49 #include <sys/ptrace.h>
52 extern int attach_flag
;
53 #endif /* ATTACH_DETACH */
56 * Mapping of register numbers to their position in the stack
58 #include <machine/reg.h>
60 R0
, R1
, R2
, R3
, R4
, R5
, R6
, R7
, R8
, R9
, R10
, R11
, R12
, R13
, FP
, SP
, PS
, PC
65 /* This function simply calls ptrace with the given arguments.
66 It exists so that all calls to ptrace are isolated in this
67 machine-dependent file. */
69 call_ptrace (request
, pid
, arg3
, arg4
)
70 int request
, pid
, arg3
, arg4
;
72 return ptrace (request
, pid
, arg3
, arg4
);
76 /* Start debugging the process whose number is PID. */
82 oldParent
= ptrace (PT_ATTACH
, pid
, 0, 0);
84 perror_with_name ("ptrace");
89 /* Stop debugging the process whose number is PID
90 and continue it with signal number SIGNAL.
91 SIGNAL = 0 means just continue it. */
98 ptrace (PT_DETACH
, inferior_pid
, signal
, oldParent
);
100 perror_with_name ("ptrace");
104 #endif /* ATTACH_DETACH */
108 if (remote_debugging
)
110 if (inferior_pid
== 0)
116 * Need to detach so the old parent gets notified of the death.
120 #endif /* ATTACH_DETACH */
121 ptrace (PT_KILL
, inferior_pid
, 0, 0);
125 #endif /* ATTACH_DETACH */
129 /* This is used when GDB is exiting. It gives less chance of error.*/
131 kill_inferior_fast ()
133 if (remote_debugging
)
135 if (inferior_pid
== 0)
141 #endif /* ATTACH_DETACH */
142 ptrace (PT_KILL
, inferior_pid
, 0, 0);
146 #endif /* ATTACH_DETACH */
149 /* Resume execution of the inferior process.
150 If STEP is nonzero, single-step it.
151 If SIGNAL is nonzero, give it that signal. */
154 resume (step
, signal
)
159 if (remote_debugging
)
160 remote_resume (step
, signal
);
163 ptrace (step
? PT_STEP
: PT_CONTINUE
, inferior_pid
, 1, signal
);
165 perror_with_name ("ptrace");
170 fetch_inferior_registers ()
173 register unsigned int regaddr
;
174 char buf
[MAX_REGISTER_RAW_SIZE
];
178 unsigned int offset
= (char *) &u
.u_ar0
- (char *) &u
;
179 offset
= ptrace (3, inferior_pid
, offset
, 0) - KERNEL_U_ADDR
;
181 for (regno
= 0; regno
< NUM_REGS
; regno
++)
183 regaddr
= register_addr (regno
, offset
);
184 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
186 *(int *) &buf
[i
] = ptrace (3, inferior_pid
, regaddr
, 0);
187 regaddr
+= sizeof (int);
189 supply_register (regno
, buf
);
193 /* Store our register values back into the inferior.
194 If REGNO is -1, do this for all registers.
195 Otherwise, REGNO specifies which register (so we can save time). */
197 store_inferior_registers (regno
)
200 register unsigned int regaddr
;
204 unsigned int offset
= (char *) &u
.u_ar0
- (char *) &u
;
205 offset
= ptrace (3, inferior_pid
, offset
, 0) - KERNEL_U_ADDR
;
209 regaddr
= register_addr (regno
, offset
);
211 ptrace (6, inferior_pid
, regaddr
, read_register (regno
));
214 sprintf (buf
, "writing register number %d", regno
);
215 perror_with_name (buf
);
218 else for (regno
= 0; regno
< NUM_REGS
; regno
++)
220 regaddr
= register_addr (regno
, offset
);
222 ptrace (6, inferior_pid
, regaddr
, read_register (regno
));
225 sprintf (buf
, "writing register number %d", regno
);
226 perror_with_name (buf
);
231 /* Copy LEN bytes from inferior's memory starting at MEMADDR
232 to debugger memory starting at MYADDR.
233 On failure (cannot read from inferior, usually because address is out
234 of bounds) returns the value of errno. */
237 read_inferior_memory (memaddr
, myaddr
, len
)
243 /* Round starting address down to longword boundary. */
244 register CORE_ADDR addr
= memaddr
& - sizeof (int);
245 /* Round ending address up; get number of longwords that makes. */
247 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
248 /* Allocate buffer of that many longwords. */
249 register int *buffer
= (int *) alloca (count
* sizeof (int));
252 /* Read all the longwords */
253 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
256 if (remote_debugging
)
257 buffer
[i
] = remote_fetch_word (addr
);
259 buffer
[i
] = ptrace (1, inferior_pid
, addr
, 0);
264 /* Copy appropriate bytes out of the buffer. */
265 bcopy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
269 /* Copy LEN bytes of data from debugger memory at MYADDR
270 to inferior's memory at MEMADDR.
271 On failure (cannot write the inferior)
272 returns the value of errno. */
275 write_inferior_memory (memaddr
, myaddr
, len
)
281 /* Round starting address down to longword boundary. */
282 register CORE_ADDR addr
= memaddr
& - sizeof (int);
283 /* Round ending address up; get number of longwords that makes. */
285 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
286 /* Allocate buffer of that many longwords. */
287 register int *buffer
= (int *) alloca (count
* sizeof (int));
290 /* Fill start and end extra bytes of buffer with existing memory data. */
292 if (remote_debugging
)
293 buffer
[0] = remote_fetch_word (addr
);
295 buffer
[0] = ptrace (1, inferior_pid
, addr
, 0);
299 if (remote_debugging
)
301 = remote_fetch_word (addr
+ (count
- 1) * sizeof (int));
304 = ptrace (1, inferior_pid
,
305 addr
+ (count
- 1) * sizeof (int), 0);
308 /* Copy data to be written over corresponding part of buffer */
310 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
312 /* Write the entire buffer. */
314 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
317 if (remote_debugging
)
318 remote_store_word (addr
, buffer
[i
]);
320 ptrace (4, inferior_pid
, addr
, buffer
[i
]);
328 /* Work with core dump and executable files, for GDB.
329 This code would be in core.c if it weren't machine-dependent. */
331 /* Recognize COFF format systems because a.out.h defines AOUTHDR. */
337 #define N_TXTADDR(hdr) 0
338 #endif /* no N_TXTADDR */
341 #define N_DATADDR(hdr) hdr.a_text
342 #endif /* no N_DATADDR */
344 /* Make COFF and non-COFF names for things a little more compatible
345 to reduce conditionals later. */
348 #define a_magic magic
352 #define AOUTHDR struct exec
355 extern char *sys_siglist
[];
358 /* Hook for `exec_file_command' command to call. */
360 extern void (*exec_file_display_hook
) ();
362 /* File names of core file and executable file. */
364 extern char *corefile
;
365 extern char *execfile
;
367 /* Descriptors on which core file and executable file are open.
368 Note that the execchan is closed when an inferior is created
369 and reopened if the inferior dies or is killed. */
374 /* Last modification time of executable file.
375 Also used in source.c to compare against mtime of a source file. */
377 extern int exec_mtime
;
379 /* Virtual addresses of bounds of the two areas of memory in the core file. */
381 extern CORE_ADDR data_start
;
382 extern CORE_ADDR data_end
;
383 extern CORE_ADDR stack_start
;
384 extern CORE_ADDR stack_end
;
386 /* Virtual addresses of bounds of two areas of memory in the exec file.
387 Note that the data area in the exec file is used only when there is no core file. */
389 extern CORE_ADDR text_start
;
390 extern CORE_ADDR text_end
;
392 extern CORE_ADDR exec_data_start
;
393 extern CORE_ADDR exec_data_end
;
395 /* Address in executable file of start of text area data. */
397 extern int text_offset
;
399 /* Address in executable file of start of data area data. */
401 extern int exec_data_offset
;
403 /* Address in core file of start of data area data. */
405 extern int data_offset
;
407 /* Address in core file of start of stack area data. */
409 extern int stack_offset
;
412 /* various coff data structures */
414 extern FILHDR file_hdr
;
415 extern SCNHDR text_hdr
;
416 extern SCNHDR data_hdr
;
418 #endif /* not COFF_FORMAT */
420 /* a.out header saved in core file. */
422 extern AOUTHDR core_aouthdr
;
424 /* a.out header of exec file. */
426 extern AOUTHDR exec_aouthdr
;
428 extern void validate_files ();
430 core_file_command (filename
, from_tty
)
435 extern char registers
[];
437 /* Discard all vestiges of any previous core file
438 and mark data and stack spaces as empty. */
450 stack_start
= STACK_END_ADDR
;
451 stack_end
= STACK_END_ADDR
;
453 /* Now, if a new core file was specified, open it and digest it. */
457 filename
= tilde_expand (filename
);
458 make_cleanup (free
, filename
);
460 if (have_inferior_p ())
461 error ("To look at a core file, you must kill the inferior with \"kill\".");
462 corechan
= open (filename
, O_RDONLY
, 0);
464 perror_with_name (filename
);
465 /* 4.2-style (and perhaps also sysV-style) core dump file. */
471 val
= myread (corechan
, &u
, sizeof u
);
473 perror_with_name (filename
);
474 data_start
= exec_data_start
;
476 data_end
= data_start
+ NBPG
* u
.u_dsize
;
477 stack_start
= stack_end
- NBPG
* u
.u_ssize
;
478 data_offset
= NBPG
* UPAGES
;
479 stack_offset
= NBPG
* (UPAGES
+ u
.u_dsize
);
480 reg_offset
= (int) u
.u_ar0
- KERNEL_U_ADDR
;
482 /* I don't know where to find this info.
483 So, for now, mark it as not available. */
484 N_SET_MAGIC (core_aouthdr
, 0);
486 /* Read the register values out of the core file and store
487 them where `read_register' will find them. */
492 for (regno
= 0; regno
< NUM_REGS
; regno
++)
494 char buf
[MAX_REGISTER_RAW_SIZE
];
496 val
= lseek (corechan
, register_addr (regno
, reg_offset
), 0);
498 perror_with_name (filename
);
500 val
= myread (corechan
, buf
, sizeof buf
);
502 perror_with_name (filename
);
503 supply_register (regno
, buf
);
507 if (filename
[0] == '/')
508 corefile
= savestring (filename
, strlen (filename
));
511 corefile
= concat (current_directory
, "/", filename
);
514 set_current_frame ( create_new_frame (read_register (FP_REGNUM
),
516 select_frame (get_current_frame (), 0);
520 printf ("No core file now.\n");
523 exec_file_command (filename
, from_tty
)
529 /* Eliminate all traces of old exec file.
530 Mark text segment as empty. */
536 data_end
-= exec_data_start
;
545 /* Now open and digest the file the user requested, if any. */
549 filename
= tilde_expand (filename
);
550 make_cleanup (free
, filename
);
552 execchan
= openp (getenv ("PATH"), 1, filename
, O_RDONLY
, 0,
555 perror_with_name (filename
);
562 if (read_file_hdr (execchan
, &file_hdr
) < 0)
563 error ("\"%s\": not in executable format.", execfile
);
565 aout_hdrsize
= file_hdr
.f_opthdr
;
566 num_sections
= file_hdr
.f_nscns
;
568 if (read_aout_hdr (execchan
, &exec_aouthdr
, aout_hdrsize
) < 0)
569 error ("\"%s\": can't read optional aouthdr", execfile
);
571 if (read_section_hdr (execchan
, _TEXT
, &text_hdr
, num_sections
) < 0)
572 error ("\"%s\": can't read text section header", execfile
);
574 if (read_section_hdr (execchan
, _DATA
, &data_hdr
, num_sections
) < 0)
575 error ("\"%s\": can't read data section header", execfile
);
577 text_start
= exec_aouthdr
.text_start
;
578 text_end
= text_start
+ exec_aouthdr
.tsize
;
579 text_offset
= text_hdr
.s_scnptr
;
580 exec_data_start
= exec_aouthdr
.data_start
;
581 exec_data_end
= exec_data_start
+ exec_aouthdr
.dsize
;
582 exec_data_offset
= data_hdr
.s_scnptr
;
583 data_start
= exec_data_start
;
584 data_end
+= exec_data_start
;
585 exec_mtime
= file_hdr
.f_timdat
;
587 #else /* not COFF_FORMAT */
591 #ifdef HEADER_SEEK_FD
592 HEADER_SEEK_FD (execchan
);
595 val
= myread (execchan
, &exec_aouthdr
, sizeof (AOUTHDR
));
598 perror_with_name (filename
);
600 text_start
= N_TXTADDR (exec_aouthdr
);
601 exec_data_start
= N_DATADDR (exec_aouthdr
);
603 text_offset
= N_TXTOFF (exec_aouthdr
);
604 exec_data_offset
= N_TXTOFF (exec_aouthdr
) + exec_aouthdr
.a_text
;
606 text_end
= text_start
+ exec_aouthdr
.a_text
;
607 exec_data_end
= exec_data_start
+ exec_aouthdr
.a_data
;
608 data_start
= exec_data_start
;
609 data_end
+= exec_data_start
;
611 fstat (execchan
, &st_exec
);
612 exec_mtime
= st_exec
.st_mtime
;
614 #endif /* not COFF_FORMAT */
619 printf ("No exec file now.\n");
621 /* Tell display code (if any) about the changed file name. */
622 if (exec_file_display_hook
)
623 (*exec_file_display_hook
) (filename
);