1 /* Machine-dependent code which would otherwise be in inflow.c and core.c,
2 for GDB, the GNU debugger.
3 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
5 This file is part of GDB.
7 GDB is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 1, or (at your option)
12 GDB is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GDB; see the file COPYING. If not, write to
19 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>
35 #include <machine/reg.h>
43 extern int attach_flag
;
45 /* This function simply calls ptrace with the given arguments.
46 It exists so that all calls to ptrace are isolated in this
47 machine-dependent file. */
49 call_ptrace (request
, pid
, arg3
, arg4
)
50 int request
, pid
, arg3
, arg4
;
52 return ptrace (request
, pid
, arg3
, arg4
);
59 if (inferior_pid
== 0)
61 ptrace (8, inferior_pid
, 0, 0);
66 /* This is used when GDB is exiting. It gives less chance of error.*/
72 if (inferior_pid
== 0)
74 ptrace (8, inferior_pid
, 0, 0);
78 /* Resume execution of the inferior process.
79 If STEP is nonzero, single-step it.
80 If SIGNAL is nonzero, give it that signal. */
89 remote_resume (step
, signal
);
92 ptrace (step
? 9 : 7, inferior_pid
, 1, signal
);
94 perror_with_name ("ptrace");
100 /* Start debugging the process whose number is PID. */
106 ptrace (PTRACE_ATTACH
, pid
, 0, 0);
108 perror_with_name ("ptrace");
113 /* Stop debugging the process whose number is PID
114 and continue it with signal number SIGNAL.
115 SIGNAL = 0 means just continue it. */
122 ptrace (PTRACE_DETACH
, inferior_pid
, 1, signal
);
124 perror_with_name ("ptrace");
127 #endif /* ATTACH_DETACH */
130 fetch_inferior_registers ()
132 struct regs inferior_registers
;
134 struct fp_status inferior_fp_registers
;
136 extern char registers
[];
138 if (remote_debugging
)
139 remote_fetch_registers (registers
);
142 ptrace (PTRACE_GETREGS
, inferior_pid
, &inferior_registers
);
144 ptrace (PTRACE_GETFPREGS
, inferior_pid
, &inferior_fp_registers
);
147 bcopy (&inferior_registers
, registers
, 16 * 4);
149 bcopy (&inferior_fp_registers
, ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
150 sizeof inferior_fp_registers
.fps_regs
);
152 *(int *)®isters
[REGISTER_BYTE (PS_REGNUM
)] = inferior_registers
.r_ps
;
153 *(int *)®isters
[REGISTER_BYTE (PC_REGNUM
)] = inferior_registers
.r_pc
;
155 bcopy (&inferior_fp_registers
.fps_control
,
156 ®isters
[REGISTER_BYTE (FPC_REGNUM
)],
157 sizeof inferior_fp_registers
- sizeof inferior_fp_registers
.fps_regs
);
162 /* Store our register values back into the inferior.
163 If REGNO is -1, do this for all registers.
164 Otherwise, REGNO specifies which register (so we can save time). */
166 store_inferior_registers (regno
)
169 struct regs inferior_registers
;
170 struct fp_status inferior_fp_registers
;
171 extern char registers
[];
173 if (remote_debugging
)
174 remote_store_registers (registers
);
177 bcopy (registers
, &inferior_registers
, 16 * 4);
179 bcopy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], &inferior_fp_registers
,
180 sizeof inferior_fp_registers
.fps_regs
);
182 inferior_registers
.r_ps
= *(int *)®isters
[REGISTER_BYTE (PS_REGNUM
)];
183 inferior_registers
.r_pc
= *(int *)®isters
[REGISTER_BYTE (PC_REGNUM
)];
186 bcopy (®isters
[REGISTER_BYTE (FPC_REGNUM
)],
187 &inferior_fp_registers
.fps_control
,
188 sizeof inferior_fp_registers
- sizeof inferior_fp_registers
.fps_regs
);
191 ptrace (PTRACE_SETREGS
, inferior_pid
, &inferior_registers
);
193 ptrace (PTRACE_SETFPREGS
, inferior_pid
, &inferior_fp_registers
);
198 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
199 in the NEW_SUN_PTRACE case.
200 It ought to be straightforward. But it appears that writing did
201 not write the data that I specified. I cannot understand where
202 it got the data that it actually did write. */
204 /* Copy LEN bytes from inferior's memory starting at MEMADDR
205 to debugger memory starting at MYADDR.
206 On failure (cannot read from inferior, usually because address is out
207 of bounds) returns the value of errno. */
210 read_inferior_memory (memaddr
, myaddr
, len
)
216 /* Round starting address down to longword boundary. */
217 register CORE_ADDR addr
= memaddr
& - sizeof (int);
218 /* Round ending address up; get number of longwords that makes. */
220 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
221 /* Allocate buffer of that many longwords. */
222 register int *buffer
= (int *) alloca (count
* sizeof (int));
225 /* Read all the longwords */
226 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
229 if (remote_debugging
)
230 buffer
[i
] = remote_fetch_word (addr
);
232 buffer
[i
] = ptrace (1, inferior_pid
, addr
, 0);
237 /* Copy appropriate bytes out of the buffer. */
238 bcopy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
242 /* Copy LEN bytes of data from debugger memory at MYADDR
243 to inferior's memory at MEMADDR.
244 On failure (cannot write the inferior)
245 returns the value of errno. */
248 write_inferior_memory (memaddr
, myaddr
, len
)
254 /* Round starting address down to longword boundary. */
255 register CORE_ADDR addr
= memaddr
& - sizeof (int);
256 /* Round ending address up; get number of longwords that makes. */
258 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
259 /* Allocate buffer of that many longwords. */
260 register int *buffer
= (int *) alloca (count
* sizeof (int));
263 /* Fill start and end extra bytes of buffer with existing memory data. */
265 if (remote_debugging
)
266 buffer
[0] = remote_fetch_word (addr
);
268 buffer
[0] = ptrace (1, inferior_pid
, addr
, 0);
272 if (remote_debugging
)
274 = remote_fetch_word (addr
+ (count
- 1) * sizeof (int));
277 = ptrace (1, inferior_pid
,
278 addr
+ (count
- 1) * sizeof (int), 0);
281 /* Copy data to be written over corresponding part of buffer */
283 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
285 /* Write the entire buffer. */
287 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
290 if (remote_debugging
)
291 remote_store_word (addr
, buffer
[i
]);
293 ptrace (4, inferior_pid
, addr
, buffer
[i
]);
302 /* Machine-dependent code which would otherwise be in core.c */
303 /* Work with core dump and executable files, for GDB. */
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 /* Non-zero if this is an object (.o) file, rather than an executable.
314 Distinguishing between the two is rarely necessary (and seems like
315 a hack, but there is no other way to get the text and data
316 addresses--N_TXTADDR should probably take care of
317 this, but it doesn't). */
318 /* This definition will not work
319 if someone decides to make ld preserve relocation info. */
320 #define IS_OBJECT_FILE(hdr) (hdr.a_trsize != 0)
322 /* Make COFF and non-COFF names for things a little more compatible
323 to reduce conditionals later. */
326 #define a_magic magic
331 #define AOUTHDR struct exec
335 extern char *sys_siglist
[];
337 /* Hook for `exec_file_command' command to call. */
339 extern void (*exec_file_display_hook
) ();
341 /* File names of core file and executable file. */
343 extern char *corefile
;
344 extern char *execfile
;
346 /* Descriptors on which core file and executable file are open.
347 Note that the execchan is closed when an inferior is created
348 and reopened if the inferior dies or is killed. */
353 /* Last modification time of executable file.
354 Also used in source.c to compare against mtime of a source file. */
356 extern int exec_mtime
;
358 /* Virtual addresses of bounds of the two areas of memory in the core file. */
360 extern CORE_ADDR data_start
;
361 extern CORE_ADDR data_end
;
362 extern CORE_ADDR stack_start
;
363 extern CORE_ADDR stack_end
;
365 /* Virtual addresses of bounds of two areas of memory in the exec file.
366 Note that the data area in the exec file is used only when there is no core file. */
368 extern CORE_ADDR text_start
;
369 extern CORE_ADDR text_end
;
371 extern CORE_ADDR exec_data_start
;
372 extern CORE_ADDR exec_data_end
;
374 /* Address in executable file of start of text area data. */
376 extern int text_offset
;
378 /* Address in executable file of start of data area data. */
380 extern int exec_data_offset
;
382 /* Address in core file of start of data area data. */
384 extern int data_offset
;
386 /* Address in core file of start of stack area data. */
388 extern int stack_offset
;
391 /* various coff data structures */
393 extern FILHDR file_hdr
;
394 extern SCNHDR text_hdr
;
395 extern SCNHDR data_hdr
;
397 #endif /* not COFF_FORMAT */
399 /* a.out header saved in core file. */
401 extern AOUTHDR core_aouthdr
;
403 /* a.out header of exec file. */
405 extern AOUTHDR exec_aouthdr
;
407 extern void validate_files ();
409 core_file_command (filename
, from_tty
)
414 extern char registers
[];
416 /* Discard all vestiges of any previous core file
417 and mark data and stack spaces as empty. */
429 stack_start
= STACK_END_ADDR
;
430 stack_end
= STACK_END_ADDR
;
432 /* Now, if a new core file was specified, open it and digest it. */
436 filename
= tilde_expand (filename
);
437 make_cleanup (free
, filename
);
439 if (have_inferior_p ())
440 error ("To look at a core file, you must kill the inferior with \"kill\".");
441 corechan
= open (filename
, O_RDONLY
, 0);
443 perror_with_name (filename
);
448 val
= myread (corechan
, &corestr
, sizeof corestr
);
450 perror_with_name (filename
);
451 if (corestr
.c_magic
!= CORE_MAGIC
)
452 error ("\"%s\" does not appear to be a core dump file (magic 0x%x, expected 0x%x)",
453 filename
, corestr
.c_magic
, (int) CORE_MAGIC
);
454 else if (sizeof (struct core
) != corestr
.c_len
)
455 error ("\"%s\" has an invalid struct core length (%d, expected %d)",
456 filename
, corestr
.c_len
, (int) sizeof (struct core
));
458 data_start
= exec_data_start
;
459 data_end
= data_start
+ corestr
.c_dsize
;
460 stack_start
= stack_end
- corestr
.c_ssize
;
461 data_offset
= sizeof corestr
;
462 stack_offset
= sizeof corestr
+ corestr
.c_dsize
;
464 bcopy (&corestr
.c_regs
, registers
, 16 * 4);
465 *(int *)®isters
[REGISTER_BYTE (PS_REGNUM
)] = corestr
.c_regs
.r_ps
;
466 *(int *)®isters
[REGISTER_BYTE (PC_REGNUM
)] = corestr
.c_regs
.r_pc
;
469 bcopy (corestr
.c_fpu
.f_fpstatus
.fps_regs
,
470 ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
471 sizeof corestr
.c_fpu
.f_fpstatus
.fps_regs
);
472 bcopy (&corestr
.c_fpu
.f_fpstatus
.fps_control
,
473 ®isters
[REGISTER_BYTE (FPC_REGNUM
)],
474 sizeof corestr
.c_fpu
.f_fpstatus
- sizeof corestr
.c_fpu
.f_fpstatus
.fps_regs
);
476 bcopy (corestr
.c_fpstatus
.fps_regs
,
477 ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
478 sizeof corestr
.c_fpstatus
.fps_regs
);
479 bcopy (&corestr
.c_fpstatus
.fps_control
,
480 ®isters
[REGISTER_BYTE (FPC_REGNUM
)],
481 sizeof corestr
.c_fpstatus
- sizeof corestr
.c_fpstatus
.fps_regs
);
484 bcopy (&corestr
.c_aouthdr
, &core_aouthdr
, sizeof (struct exec
));
486 printf ("Core file is from \"%s\".\n", corestr
.c_cmdname
);
487 if (corestr
.c_signo
> 0)
488 printf ("Program terminated with signal %d, %s.\n",
490 corestr
.c_signo
< NSIG
491 ? sys_siglist
[corestr
.c_signo
]
494 if (filename
[0] == '/')
495 corefile
= savestring (filename
, strlen (filename
));
498 corefile
= concat (current_directory
, "/", filename
);
501 set_current_frame ( create_new_frame (read_register (FP_REGNUM
),
503 select_frame (get_current_frame (), 0);
507 printf ("No core file now.\n");
510 exec_file_command (filename
, from_tty
)
516 /* Eliminate all traces of old exec file.
517 Mark text segment as empty. */
523 data_end
-= exec_data_start
;
532 /* Now open and digest the file the user requested, if any. */
536 filename
= tilde_expand (filename
);
537 make_cleanup (free
, filename
);
539 execchan
= openp (getenv ("PATH"), 1, filename
, O_RDONLY
, 0,
542 perror_with_name (filename
);
549 if (read_file_hdr (execchan
, &file_hdr
) < 0)
550 error ("\"%s\": not in executable format.", execfile
);
552 aout_hdrsize
= file_hdr
.f_opthdr
;
553 num_sections
= file_hdr
.f_nscns
;
555 if (read_aout_hdr (execchan
, &exec_aouthdr
, aout_hdrsize
) < 0)
556 error ("\"%s\": can't read optional aouthdr", execfile
);
558 if (read_section_hdr (execchan
, _TEXT
, &text_hdr
, num_sections
,
560 error ("\"%s\": can't read text section header", execfile
);
562 if (read_section_hdr (execchan
, _DATA
, &data_hdr
, num_sections
,
564 error ("\"%s\": can't read data section header", execfile
);
566 text_start
= exec_aouthdr
.text_start
;
567 text_end
= text_start
+ exec_aouthdr
.tsize
;
568 text_offset
= text_hdr
.s_scnptr
;
569 exec_data_start
= exec_aouthdr
.data_start
;
570 exec_data_end
= exec_data_start
+ exec_aouthdr
.dsize
;
571 exec_data_offset
= data_hdr
.s_scnptr
;
572 data_start
= exec_data_start
;
573 data_end
+= exec_data_start
;
574 exec_mtime
= file_hdr
.f_timdat
;
576 #else /* not COFF_FORMAT */
579 val
= myread (execchan
, &exec_aouthdr
, sizeof (AOUTHDR
));
582 perror_with_name (filename
);
585 IS_OBJECT_FILE (exec_aouthdr
) ? 0 : N_TXTADDR (exec_aouthdr
);
586 exec_data_start
= IS_OBJECT_FILE (exec_aouthdr
)
587 ? exec_aouthdr
.a_text
: N_DATADDR (exec_aouthdr
);
588 text_offset
= N_TXTOFF (exec_aouthdr
);
589 exec_data_offset
= N_TXTOFF (exec_aouthdr
) + exec_aouthdr
.a_text
;
591 text_end
= text_start
+ exec_aouthdr
.a_text
;
592 exec_data_end
= exec_data_start
+ exec_aouthdr
.a_data
;
593 data_start
= exec_data_start
;
594 data_end
+= exec_data_start
;
596 fstat (execchan
, &st_exec
);
597 exec_mtime
= st_exec
.st_mtime
;
599 #endif /* not COFF_FORMAT */
604 printf ("No exec file now.\n");
606 /* Tell display code (if any) about the changed file name. */
607 if (exec_file_display_hook
)
608 (*exec_file_display_hook
) (filename
);