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>
33 #endif /* I don't think that I need this file. */
41 /* This function simply calls ptrace with the given arguments.
42 It exists so that all calls to ptrace are isolated in this
43 machine-dependent file. */
45 call_ptrace (request
, pid
, arg3
, arg4
)
46 int request
, pid
, arg3
, arg4
;
48 return ptrace (request
, pid
, arg3
, arg4
);
55 if (inferior_pid
== 0)
57 ptrace (8, inferior_pid
, 0, 0);
62 /* This is used when GDB is exiting. It gives less chance of error.*/
68 if (inferior_pid
== 0)
70 ptrace (8, inferior_pid
, 0, 0);
74 /* Resume execution of the inferior process.
75 If STEP is nonzero, single-step it.
76 If SIGNAL is nonzero, give it that signal. */
85 remote_resume (step
, signal
);
88 ptrace (step
? 9 : 7, inferior_pid
, 1, signal
);
90 perror_with_name ("ptrace");
95 fetch_inferior_registers ()
98 register unsigned int regaddr
;
99 char buf
[MAX_REGISTER_RAW_SIZE
];
104 unsigned int offset
= (char *) &u
.u_pcb
.pcb_d0
- (char *) &u
;
106 unsigned int offset
= (char *) &u
.u_ar0
- (char *) &u
;
107 offset
= ptrace (3, inferior_pid
, offset
, 0) - KERNEL_U_ADDR
;
110 for (regno
= 0; regno
< NUM_REGS
; regno
++)
112 regaddr
= register_addr (regno
, offset
);
113 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
115 *(int *) &buf
[i
] = ptrace (3, inferior_pid
, regaddr
, 0);
116 regaddr
+= sizeof (int);
118 supply_register (regno
, buf
);
122 /* Store our register values back into the inferior.
123 If REGNO is -1, do this for all registers.
124 Otherwise, REGNO specifies which register (so we can save time). */
126 store_inferior_registers (regno
)
129 register unsigned int regaddr
;
131 extern char registers
[];
135 unsigned int offset
= (char *) &u
.u_pcb
.pcb_d0
- (char *) &u
;
137 unsigned int offset
= (char *) &u
.u_ar0
- (char *) &u
;
138 offset
= ptrace (3, inferior_pid
, offset
, 0) - KERNEL_U_ADDR
;
142 if (regno
>= FP0_REGNUM
)
143 printf ("warning: floating register num %d not written due to OS bug.\n",
150 int *p
= (int *) ®isters
[REGISTER_BYTE (regno
)];
153 if (regno
== FP_REGNUM
)
154 printf ("warning: ptrace bug for writing register number fp(a6).\n");
157 regaddr
= register_addr (regno
, offset
);
158 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
161 ptrace (6, inferior_pid
, regaddr
, *p
++);
164 sprintf (buf
, "writing register number %d[%d]", regno
, i
);
165 perror_with_name (buf
);
167 regaddr
+= sizeof (int);
173 for (regno
= 0; regno
< FP0_REGNUM
; regno
++)
175 for (regno
= 0; regno
< NUM_REGS
; regno
++)
179 int *p
= (int *) ®isters
[REGISTER_BYTE (regno
)];
180 regaddr
= register_addr (regno
, offset
);
181 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
184 ptrace (6, inferior_pid
, regaddr
, *p
++);
187 sprintf (buf
, "writing register number %d[%d]", regno
, i
);
188 perror_with_name (buf
);
190 regaddr
+= sizeof (int);
194 printf ("warning: ptrace bug for writing floating registers(no write).\n");
199 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
200 in the NEW_SUN_PTRACE case.
201 It ought to be straightforward. But it appears that writing did
202 not write the data that I specified. I cannot understand where
203 it got the data that it actually did write. */
205 /* Copy LEN bytes from inferior's memory starting at MEMADDR
206 to debugger memory starting at MYADDR.
207 On failure (cannot read from inferior, usually because address is out
208 of bounds) returns the value of errno. */
211 read_inferior_memory (memaddr
, myaddr
, len
)
217 /* Round starting address down to longword boundary. */
218 register CORE_ADDR addr
= memaddr
& - sizeof (int);
219 /* Round ending address up; get number of longwords that makes. */
221 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
222 /* Allocate buffer of that many longwords. */
223 register int *buffer
= (int *) alloca (count
* sizeof (int));
226 /* Read all the longwords */
227 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
230 if (remote_debugging
)
231 buffer
[i
] = remote_fetch_word (addr
);
233 buffer
[i
] = ptrace (1, inferior_pid
, addr
, 0);
238 /* Copy appropriate bytes out of the buffer. */
239 bcopy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
243 /* Copy LEN bytes of data from debugger memory at MYADDR
244 to inferior's memory at MEMADDR.
245 On failure (cannot write the inferior)
246 returns the value of errno. */
249 write_inferior_memory (memaddr
, myaddr
, len
)
255 /* Round starting address down to longword boundary. */
256 register CORE_ADDR addr
= memaddr
& - sizeof (int);
257 /* Round ending address up; get number of longwords that makes. */
259 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
260 /* Allocate buffer of that many longwords. */
261 register int *buffer
= (int *) alloca (count
* sizeof (int));
264 /* Fill start and end extra bytes of buffer with existing memory data. */
266 if (remote_debugging
)
267 buffer
[0] = remote_fetch_word (addr
);
269 buffer
[0] = ptrace (1, inferior_pid
, addr
, 0);
273 if (remote_debugging
)
275 = remote_fetch_word (addr
+ (count
- 1) * sizeof (int));
278 = ptrace (1, inferior_pid
,
279 addr
+ (count
- 1) * sizeof (int), 0);
282 /* Copy data to be written over corresponding part of buffer */
284 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
286 /* Write the entire buffer. */
288 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
291 if (remote_debugging
)
292 remote_store_word (addr
, buffer
[i
]);
294 ptrace (4, inferior_pid
, addr
, buffer
[i
]);
302 /* Work with core dump and executable files, for GDB.
303 This code would be in core.c if it weren't machine-dependent. */
306 #define N_TXTADDR(hdr) 0
307 #endif /* no N_TXTADDR */
310 #define N_DATADDR(hdr) hdr.a_text
311 #endif /* no N_DATADDR */
313 /* Make COFF and non-COFF names for things a little more compatible
314 to reduce conditionals later. */
317 #define a_magic magic
322 #define AOUTHDR struct exec
326 extern char *sys_siglist
[];
329 /* Hook for `exec_file_command' command to call. */
331 extern void (*exec_file_display_hook
) ();
333 /* File names of core file and executable file. */
335 extern char *corefile
;
336 extern char *execfile
;
338 /* Descriptors on which core file and executable file are open.
339 Note that the execchan is closed when an inferior is created
340 and reopened if the inferior dies or is killed. */
345 /* Last modification time of executable file.
346 Also used in source.c to compare against mtime of a source file. */
348 extern int exec_mtime
;
350 /* Virtual addresses of bounds of the two areas of memory in the core file. */
352 extern CORE_ADDR data_start
;
353 extern CORE_ADDR data_end
;
354 extern CORE_ADDR stack_start
;
355 extern CORE_ADDR stack_end
;
357 /* Virtual addresses of bounds of two areas of memory in the exec file.
358 Note that the data area in the exec file is used only when there is no core file. */
360 extern CORE_ADDR text_start
;
361 extern CORE_ADDR text_end
;
363 extern CORE_ADDR exec_data_start
;
364 extern CORE_ADDR exec_data_end
;
366 /* Address in executable file of start of text area data. */
368 extern int text_offset
;
370 /* Address in executable file of start of data area data. */
372 extern int exec_data_offset
;
374 /* Address in core file of start of data area data. */
376 extern int data_offset
;
378 /* Address in core file of start of stack area data. */
380 extern int stack_offset
;
383 /* various coff data structures */
385 extern FILHDR file_hdr
;
386 extern SCNHDR text_hdr
;
387 extern SCNHDR data_hdr
;
389 #endif /* not COFF_FORMAT */
391 /* a.out header saved in core file. */
393 extern AOUTHDR core_aouthdr
;
395 /* a.out header of exec file. */
397 extern AOUTHDR exec_aouthdr
;
399 extern void validate_files ();
401 core_file_command (filename
, from_tty
)
406 extern char registers
[];
408 /* Discard all vestiges of any previous core file
409 and mark data and stack spaces as empty. */
421 stack_start
= STACK_END_ADDR
;
422 stack_end
= STACK_END_ADDR
;
424 /* Now, if a new core file was specified, open it and digest it. */
428 filename
= tilde_expand (filename
);
429 make_cleanup (free
, filename
);
431 if (have_inferior_p ())
432 error ("To look at a core file, you must kill the inferior with \"kill\".");
433 corechan
= open (filename
, O_RDONLY
, 0);
435 perror_with_name (filename
);
436 /* 4.2-style (and perhaps also sysV-style) core dump file. */
441 val
= myread (corechan
, &u
, sizeof u
);
443 perror_with_name (filename
);
444 data_start
= exec_data_start
;
446 data_end
= data_start
+ NBPG
* u
.u_dsize
;
447 stack_start
= stack_end
- NBPG
* u
.u_ssize
;
448 data_offset
= NBPG
* UPAGES
;
449 stack_offset
= NBPG
* (UPAGES
+ u
.u_dsize
);
450 reg_offset
= (int) u
.u_ar0
- KERNEL_U_ADDR
;
452 /* I don't know where to find this info.
453 So, for now, mark it as not available. */
454 core_aouthdr
.a_magic
= 0;
456 /* Read the register values out of the core file and store
457 them where `read_register' will find them. */
462 for (regno
= 0; regno
< NUM_REGS
; regno
++)
464 char buf
[MAX_REGISTER_RAW_SIZE
];
466 val
= lseek (corechan
, register_addr (regno
, reg_offset
), 0);
468 perror_with_name (filename
);
470 val
= myread (corechan
, buf
, sizeof buf
);
472 perror_with_name (filename
);
473 supply_register (regno
, buf
);
477 if (filename
[0] == '/')
478 corefile
= savestring (filename
, strlen (filename
));
481 corefile
= concat (current_directory
, "/", filename
);
484 set_current_frame ( create_new_frame (read_register (FP_REGNUM
),
486 select_frame (get_current_frame (), 0);
490 printf ("No core file now.\n");
493 exec_file_command (filename
, from_tty
)
499 /* Eliminate all traces of old exec file.
500 Mark text segment as empty. */
506 data_end
-= exec_data_start
;
515 /* Now open and digest the file the user requested, if any. */
519 filename
= tilde_expand (filename
);
520 make_cleanup (free
, filename
);
522 execchan
= openp (getenv ("PATH"), 1, filename
, O_RDONLY
, 0,
525 perror_with_name (filename
);
532 if (read_file_hdr (execchan
, &file_hdr
) < 0)
533 error ("\"%s\": not in executable format.", execfile
);
535 aout_hdrsize
= file_hdr
.f_opthdr
;
536 num_sections
= file_hdr
.f_nscns
;
538 if (read_aout_hdr (execchan
, &exec_aouthdr
, aout_hdrsize
) < 0)
539 error ("\"%s\": can't read optional aouthdr", execfile
);
541 if (read_section_hdr (execchan
, _TEXT
, &text_hdr
, num_sections
) < 0)
542 error ("\"%s\": can't read text section header", execfile
);
544 if (read_section_hdr (execchan
, _DATA
, &data_hdr
, num_sections
) < 0)
545 error ("\"%s\": can't read data section header", execfile
);
547 text_start
= exec_aouthdr
.text_start
;
548 text_end
= text_start
+ exec_aouthdr
.tsize
;
549 text_offset
= text_hdr
.s_scnptr
;
550 exec_data_start
= exec_aouthdr
.data_start
;
551 exec_data_end
= exec_data_start
+ exec_aouthdr
.dsize
;
552 exec_data_offset
= data_hdr
.s_scnptr
;
553 data_start
= exec_data_start
;
554 data_end
+= exec_data_start
;
555 exec_mtime
= file_hdr
.f_timdat
;
557 #else /* not COFF_FORMAT */
561 val
= myread (execchan
, &exec_aouthdr
, sizeof (AOUTHDR
));
564 perror_with_name (filename
);
566 text_start
= N_TXTADDR (exec_aouthdr
);
567 exec_data_start
= N_DATADDR (exec_aouthdr
);
569 text_offset
= N_TXTOFF (exec_aouthdr
);
570 exec_data_offset
= N_TXTOFF (exec_aouthdr
) + exec_aouthdr
.a_text
;
572 text_end
= text_start
+ exec_aouthdr
.a_text
;
573 exec_data_end
= exec_data_start
+ exec_aouthdr
.a_data
;
574 data_start
= exec_data_start
;
575 data_end
+= exec_data_start
;
577 fstat (execchan
, &st_exec
);
578 exec_mtime
= st_exec
.st_mtime
;
580 #endif /* not COFF_FORMAT */
585 printf ("No exec file now.\n");
587 /* Tell display code (if any) about the changed file name. */
588 if (exec_file_display_hook
)
589 (*exec_file_display_hook
) (filename
);
593 /* Bad implement execle(3). It's depend for "/bin/cc".
598 execle(FILE, ARGS, envp);
604 pea LC5 ; call printf
607 pea _envp ; call execle
615 addw #32,sp ; delayed pop !!
620 pea L26 ; call printf
622 addq.l #4,sp ; <--- popd stack !!
623 pea _envp ; call execle
632 register char **env
= &args
;
635 execve(name
, (char **)&args
, (char **)*env
);