1 /* Machine-dependent code for a Hewlett-Packard 9000/300, running bsd.
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. */
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 */
48 #include <sys/ptrace.h>
50 CORE_ADDR kernel_u_addr
;
54 /* This function simply calls ptrace with the given arguments.
55 It exists so that all calls to ptrace are isolated in this
56 machine-dependent file. */
58 call_ptrace (request
, pid
, arg3
, arg4
)
59 int request
, pid
, arg3
, arg4
;
61 return ptrace (request
, pid
, arg3
, arg4
);
68 if (inferior_pid
== 0)
70 ptrace (PT_KILL
, inferior_pid
, 0, 0);
75 /* This is used when GDB is exiting. It gives less chance of error.*/
81 if (inferior_pid
== 0)
83 ptrace (PT_KILL
, inferior_pid
, 0, 0);
87 /* Resume execution of the inferior process.
88 If STEP is nonzero, single-step it.
89 If SIGNAL is nonzero, give it that signal. */
98 remote_resume (step
, signal
);
101 ptrace (step
? PT_STEP
: PT_CONTINUE
, inferior_pid
, 1, signal
);
103 perror_with_name ("ptrace");
108 fetch_inferior_registers ()
111 register unsigned int regaddr
;
112 char buf
[MAX_REGISTER_RAW_SIZE
];
116 unsigned int offset
= (char *) &u
.u_ar0
- (char *) &u
;
117 offset
= ptrace (PT_READ_U
, inferior_pid
, offset
, 0) - KERNEL_U_ADDR
;
119 for (regno
= 0; regno
< NUM_REGS
; regno
++)
121 regaddr
= register_addr (regno
, offset
);
122 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
124 *(int *) &buf
[i
] = ptrace (PT_READ_U
, inferior_pid
, regaddr
, 0);
125 regaddr
+= sizeof (int);
127 supply_register (regno
, buf
);
131 /* Store our register values back into the inferior.
132 If REGNO is -1, do this for all registers.
133 Otherwise, REGNO specifies which register (so we can save time). */
135 store_inferior_registers (regno
)
138 register unsigned int regaddr
;
140 extern char registers
[];
144 unsigned int offset
= (char *) &u
.u_ar0
- (char *) &u
;
145 offset
= ptrace (PT_READ_U
, inferior_pid
, offset
, 0) - KERNEL_U_ADDR
;
149 regaddr
= register_addr (regno
, offset
);
150 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof(int))
153 ptrace (PT_WRITE_U
, inferior_pid
, regaddr
,
154 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
]);
157 sprintf (buf
, "writing register number %d(%d)", regno
, i
);
158 perror_with_name (buf
);
160 regaddr
+= sizeof(int);
163 else for (regno
= 0; regno
< NUM_REGS
; regno
++)
165 regaddr
= register_addr (regno
, offset
);
166 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof(int))
169 ptrace (PT_WRITE_U
, inferior_pid
, regaddr
,
170 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
]);
173 sprintf (buf
, "writing register number %d(%d)", regno
, i
);
174 perror_with_name (buf
);
176 regaddr
+= sizeof(int);
181 /* Copy LEN bytes from inferior's memory starting at MEMADDR
182 to debugger memory starting at MYADDR.
183 On failure (cannot read from inferior, usually because address is out
184 of bounds) returns the value of errno. */
187 read_inferior_memory (memaddr
, myaddr
, len
)
193 /* Round starting address down to longword boundary. */
194 register CORE_ADDR addr
= memaddr
& - sizeof (int);
195 /* Round ending address up; get number of longwords that makes. */
197 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
198 /* Allocate buffer of that many longwords. */
199 register int *buffer
= (int *) alloca (count
* sizeof (int));
202 /* Read all the longwords */
203 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
207 /* This is now done by read_memory, because when this function did it,
208 reading a byte or short int hardware port read whole longs, causing
210 such as bus errors and unexpected hardware operation. This would
211 also be a problem with ptrace if the inferior process could read
212 or write hardware registers, but that's not usually the case. */
213 if (remote_debugging
)
214 buffer
[i
] = remote_fetch_word (addr
);
217 buffer
[i
] = ptrace (PT_READ_I
, inferior_pid
, addr
, 0);
222 /* Copy appropriate bytes out of the buffer. */
223 bcopy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
227 /* Copy LEN bytes of data from debugger memory at MYADDR
228 to inferior's memory at MEMADDR.
229 On failure (cannot write the inferior)
230 returns the value of errno. */
233 write_inferior_memory (memaddr
, myaddr
, len
)
239 /* Round starting address down to longword boundary. */
240 register CORE_ADDR addr
= memaddr
& - sizeof (int);
241 /* Round ending address up; get number of longwords that makes. */
243 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
244 /* Allocate buffer of that many longwords. */
245 register int *buffer
= (int *) alloca (count
* sizeof (int));
248 /* Fill start and end extra bytes of buffer with existing memory data. */
250 if (remote_debugging
)
251 buffer
[0] = remote_fetch_word (addr
);
253 buffer
[0] = ptrace (PT_READ_I
, inferior_pid
, addr
, 0);
257 if (remote_debugging
)
259 = remote_fetch_word (addr
+ (count
- 1) * sizeof (int));
262 = ptrace (PT_READ_I
, inferior_pid
,
263 addr
+ (count
- 1) * sizeof (int), 0);
266 /* Copy data to be written over corresponding part of buffer */
268 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
270 /* Write the entire buffer. */
272 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
275 if (remote_debugging
)
276 remote_store_word (addr
, buffer
[i
]);
278 ptrace (PT_WRITE_I
, inferior_pid
, addr
, buffer
[i
]);
286 /* Work with core dump and executable files, for GDB.
287 This code would be in core.c if it weren't machine-dependent. */
290 #define N_TXTADDR(hdr) 0
291 #endif /* no N_TXTADDR */
294 #define N_DATADDR(hdr) hdr.a_text
295 #endif /* no N_DATADDR */
297 /* Make COFF and non-COFF names for things a little more compatible
298 to reduce conditionals later. */
301 #define a_magic magic
306 #define AOUTHDR struct exec
310 extern char *sys_siglist
[];
313 /* Hook for `exec_file_command' command to call. */
315 extern void (*exec_file_display_hook
) ();
317 /* File names of core file and executable file. */
319 extern char *corefile
;
320 extern char *execfile
;
322 /* Descriptors on which core file and executable file are open.
323 Note that the execchan is closed when an inferior is created
324 and reopened if the inferior dies or is killed. */
329 /* Last modification time of executable file.
330 Also used in source.c to compare against mtime of a source file. */
332 extern int exec_mtime
;
334 /* Virtual addresses of bounds of the two areas of memory in the core file. */
336 extern CORE_ADDR data_start
;
337 extern CORE_ADDR data_end
;
338 extern CORE_ADDR stack_start
;
339 extern CORE_ADDR stack_end
;
341 /* Virtual addresses of bounds of two areas of memory in the exec file.
342 Note that the data area in the exec file is used only when there is no core file. */
344 extern CORE_ADDR text_start
;
345 extern CORE_ADDR text_end
;
347 extern CORE_ADDR exec_data_start
;
348 extern CORE_ADDR exec_data_end
;
350 /* Address in executable file of start of text area data. */
352 extern int text_offset
;
354 /* Address in executable file of start of data area data. */
356 extern int exec_data_offset
;
358 /* Address in core file of start of data area data. */
360 extern int data_offset
;
362 /* Address in core file of start of stack area data. */
364 extern int stack_offset
;
367 /* various coff data structures */
369 extern FILHDR file_hdr
;
370 extern SCNHDR text_hdr
;
371 extern SCNHDR data_hdr
;
373 #endif /* not COFF_FORMAT */
375 /* a.out header saved in core file. */
377 extern AOUTHDR core_aouthdr
;
379 /* a.out header of exec file. */
381 extern AOUTHDR exec_aouthdr
;
383 extern void validate_files ();
385 core_file_command (filename
, from_tty
)
390 extern char registers
[];
392 /* Discard all vestiges of any previous core file
393 and mark data and stack spaces as empty. */
405 stack_start
= STACK_END_ADDR
;
406 stack_end
= STACK_END_ADDR
;
408 /* Now, if a new core file was specified, open it and digest it. */
412 filename
= tilde_expand (filename
);
413 make_cleanup (free
, filename
);
415 if (have_inferior_p ())
416 error ("To look at a core file, you must kill the inferior with \"kill\".");
417 corechan
= open (filename
, O_RDONLY
, 0);
419 perror_with_name (filename
);
420 /* 4.2-style (and perhaps also sysV-style) core dump file. */
424 unsigned int reg_offset
;
426 val
= myread (corechan
, &u
, sizeof u
);
428 perror_with_name ("Not a core file: reading upage");
430 error ("Not a core file: could only read %d bytes", val
);
432 /* We are depending on exec_file_command having been called
433 previously to set exec_data_start. Since the executable
434 and the core file share the same text segment, the address
435 of the data segment will be the same in both. */
436 data_start
= exec_data_start
;
438 data_end
= data_start
+ NBPG
* u
.u_dsize
;
439 stack_start
= stack_end
- NBPG
* u
.u_ssize
;
440 data_offset
= NBPG
* UPAGES
;
441 stack_offset
= NBPG
* (UPAGES
+ u
.u_dsize
);
443 /* Some machines put an absolute address in here and some put
444 the offset in the upage of the regs. */
445 reg_offset
= (int) u
.u_ar0
;
446 if (reg_offset
> NBPG
* UPAGES
)
447 reg_offset
-= KERNEL_U_ADDR
;
449 /* I don't know where to find this info.
450 So, for now, mark it as not available. */
451 N_SET_MAGIC (core_aouthdr
, 0);
453 /* Read the register values out of the core file and store
454 them where `read_register' will find them. */
459 for (regno
= 0; regno
< NUM_REGS
; regno
++)
461 char buf
[MAX_REGISTER_RAW_SIZE
];
463 val
= lseek (corechan
, register_addr (regno
, reg_offset
), 0);
465 || (val
= myread (corechan
, buf
, sizeof buf
)) < 0)
467 char * buffer
= (char *) alloca (strlen (reg_names
[regno
])
469 strcpy (buffer
, "Reading register ");
470 strcat (buffer
, reg_names
[regno
]);
472 perror_with_name (buffer
);
475 supply_register (regno
, buf
);
479 if (filename
[0] == '/')
480 corefile
= savestring (filename
, strlen (filename
));
483 corefile
= concat (current_directory
, "/", filename
);
486 set_current_frame ( create_new_frame (read_register (FP_REGNUM
),
488 select_frame (get_current_frame (), 0);
492 printf ("No core file now.\n");
495 exec_file_command (filename
, from_tty
)
501 /* Eliminate all traces of old exec file.
502 Mark text segment as empty. */
508 data_end
-= exec_data_start
;
517 /* Now open and digest the file the user requested, if any. */
521 filename
= tilde_expand (filename
);
522 make_cleanup (free
, filename
);
524 execchan
= openp (getenv ("PATH"), 1, filename
, O_RDONLY
, 0,
527 perror_with_name (filename
);
534 if (read_file_hdr (execchan
, &file_hdr
) < 0)
535 error ("\"%s\": not in executable format.", execfile
);
537 aout_hdrsize
= file_hdr
.f_opthdr
;
538 num_sections
= file_hdr
.f_nscns
;
540 if (read_aout_hdr (execchan
, &exec_aouthdr
, aout_hdrsize
) < 0)
541 error ("\"%s\": can't read optional aouthdr", execfile
);
543 if (read_section_hdr (execchan
, _TEXT
, &text_hdr
, num_sections
,
545 error ("\"%s\": can't read text section header", execfile
);
547 if (read_section_hdr (execchan
, _DATA
, &data_hdr
, num_sections
,
549 error ("\"%s\": can't read data section header", execfile
);
551 text_start
= exec_aouthdr
.text_start
;
552 text_end
= text_start
+ exec_aouthdr
.tsize
;
553 text_offset
= text_hdr
.s_scnptr
;
554 exec_data_start
= exec_aouthdr
.data_start
;
555 exec_data_end
= exec_data_start
+ exec_aouthdr
.dsize
;
556 exec_data_offset
= data_hdr
.s_scnptr
;
557 data_start
= exec_data_start
;
558 data_end
+= exec_data_start
;
559 exec_mtime
= file_hdr
.f_timdat
;
561 #else /* not COFF_FORMAT */
565 #ifdef HEADER_SEEK_FD
566 HEADER_SEEK_FD (execchan
);
569 val
= myread (execchan
, &exec_aouthdr
, sizeof (AOUTHDR
));
572 perror_with_name (filename
);
574 text_start
= N_TXTADDR (exec_aouthdr
);
575 exec_data_start
= N_DATADDR (exec_aouthdr
);
577 text_offset
= N_TXTOFF (exec_aouthdr
);
578 exec_data_offset
= N_TXTOFF (exec_aouthdr
) + exec_aouthdr
.a_text
;
580 text_end
= text_start
+ exec_aouthdr
.a_text
;
581 exec_data_end
= exec_data_start
+ exec_aouthdr
.a_data
;
582 data_start
= exec_data_start
;
583 data_end
+= exec_data_start
;
585 fstat (execchan
, &st_exec
);
586 exec_mtime
= st_exec
.st_mtime
;
588 #endif /* not COFF_FORMAT */
593 printf ("No exec file now.\n");
595 /* Tell display code (if any) about the changed file name. */
596 if (exec_file_display_hook
)
597 (*exec_file_display_hook
) (filename
);
601 _initialize_hp300bsd_dep ()
603 struct nlist names
[2];
605 /* Get the address of the u area. */
606 names
[0].n_un
.n_name
= "_u";
607 names
[1].n_un
.n_name
= NULL
;
608 if (nlist ("/vmunix", names
) == 0)
609 kernel_u_addr
= names
[0].n_value
;
611 kernel_u_addr
= 0x00917000;