1c997a4a |
1 | /* Machine-dependent code for a Hewlett-Packard 9000/300, running bsd. |
2 | Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc. |
3 | |
4 | This file is part of GDB. |
5 | |
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) |
9 | any later version. |
10 | |
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. |
15 | |
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. */ |
e91b87a3 |
19 | |
7a67dd45 |
20 | #include <stdio.h> |
e91b87a3 |
21 | #include "defs.h" |
22 | #include "param.h" |
23 | #include "frame.h" |
24 | #include "inferior.h" |
25 | |
26 | #ifdef USG |
27 | #include <sys/types.h> |
28 | #endif |
29 | |
e91b87a3 |
30 | #include <sys/param.h> |
31 | #include <sys/dir.h> |
32 | #include <signal.h> |
33 | #include <sys/ioctl.h> |
7a67dd45 |
34 | /* #include <fcntl.h> Can we live without this? */ |
e91b87a3 |
35 | |
36 | #ifdef COFF_ENCAPSULATE |
37 | #include "a.out.encap.h" |
38 | #else |
39 | #include <a.out.h> |
40 | #endif |
41 | #ifndef N_SET_MAGIC |
42 | #define N_SET_MAGIC(exec, val) ((exec).a_magic = (val)) |
43 | #endif |
7a67dd45 |
44 | |
45 | #include <sys/user.h> /* After a.out.h */ |
e91b87a3 |
46 | #include <sys/file.h> |
47 | #include <sys/stat.h> |
4187119d |
48 | #include <sys/ptrace.h> |
e91b87a3 |
49 | |
7a67dd45 |
50 | CORE_ADDR kernel_u_addr; |
51 | |
e91b87a3 |
52 | extern int errno; |
53 | \f |
54 | /* This function simply calls ptrace with the given arguments. |
55 | It exists so that all calls to ptrace are isolated in this |
4187119d |
56 | machine-dependent file. */ |
e91b87a3 |
57 | int |
58 | call_ptrace (request, pid, arg3, arg4) |
59 | int request, pid, arg3, arg4; |
60 | { |
e91b87a3 |
61 | return ptrace (request, pid, arg3, arg4); |
e91b87a3 |
62 | } |
63 | |
64 | kill_inferior () |
65 | { |
66 | if (remote_debugging) |
67 | return; |
68 | if (inferior_pid == 0) |
69 | return; |
4187119d |
70 | ptrace (PT_KILL, inferior_pid, 0, 0); |
e91b87a3 |
71 | wait (0); |
72 | inferior_died (); |
73 | } |
74 | |
75 | /* This is used when GDB is exiting. It gives less chance of error.*/ |
76 | |
77 | kill_inferior_fast () |
78 | { |
79 | if (remote_debugging) |
80 | return; |
81 | if (inferior_pid == 0) |
82 | return; |
4187119d |
83 | ptrace (PT_KILL, inferior_pid, 0, 0); |
e91b87a3 |
84 | wait (0); |
85 | } |
86 | |
87 | /* Resume execution of the inferior process. |
88 | If STEP is nonzero, single-step it. |
89 | If SIGNAL is nonzero, give it that signal. */ |
90 | |
91 | void |
92 | resume (step, signal) |
93 | int step; |
94 | int signal; |
95 | { |
96 | errno = 0; |
97 | if (remote_debugging) |
98 | remote_resume (step, signal); |
99 | else |
100 | { |
4187119d |
101 | ptrace (step ? PT_STEP : PT_CONTINUE, inferior_pid, 1, signal); |
e91b87a3 |
102 | if (errno) |
103 | perror_with_name ("ptrace"); |
104 | } |
105 | } |
106 | \f |
107 | void |
108 | fetch_inferior_registers () |
109 | { |
110 | register int regno; |
111 | register unsigned int regaddr; |
112 | char buf[MAX_REGISTER_RAW_SIZE]; |
113 | register int i; |
114 | |
115 | struct user u; |
116 | unsigned int offset = (char *) &u.u_ar0 - (char *) &u; |
4187119d |
117 | offset = ptrace (PT_READ_U, inferior_pid, offset, 0) - KERNEL_U_ADDR; |
e91b87a3 |
118 | |
119 | for (regno = 0; regno < NUM_REGS; regno++) |
120 | { |
121 | regaddr = register_addr (regno, offset); |
122 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int)) |
123 | { |
4187119d |
124 | *(int *) &buf[i] = ptrace (PT_READ_U, inferior_pid, regaddr, 0); |
e91b87a3 |
125 | regaddr += sizeof (int); |
126 | } |
127 | supply_register (regno, buf); |
128 | } |
129 | } |
130 | |
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). */ |
134 | |
135 | store_inferior_registers (regno) |
136 | int regno; |
137 | { |
138 | register unsigned int regaddr; |
139 | char buf[80]; |
4187119d |
140 | extern char registers[]; |
141 | register int i; |
e91b87a3 |
142 | |
143 | struct user u; |
144 | unsigned int offset = (char *) &u.u_ar0 - (char *) &u; |
4187119d |
145 | offset = ptrace (PT_READ_U, inferior_pid, offset, 0) - KERNEL_U_ADDR; |
e91b87a3 |
146 | |
147 | if (regno >= 0) |
148 | { |
149 | regaddr = register_addr (regno, offset); |
4187119d |
150 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int)) |
e91b87a3 |
151 | { |
4187119d |
152 | errno = 0; |
153 | ptrace (PT_WRITE_U, inferior_pid, regaddr, |
154 | *(int *) ®isters[REGISTER_BYTE (regno) + i]); |
155 | if (errno != 0) |
156 | { |
157 | sprintf (buf, "writing register number %d(%d)", regno, i); |
158 | perror_with_name (buf); |
159 | } |
160 | regaddr += sizeof(int); |
e91b87a3 |
161 | } |
162 | } |
163 | else for (regno = 0; regno < NUM_REGS; regno++) |
164 | { |
165 | regaddr = register_addr (regno, offset); |
4187119d |
166 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int)) |
e91b87a3 |
167 | { |
4187119d |
168 | errno = 0; |
169 | ptrace (PT_WRITE_U, inferior_pid, regaddr, |
170 | *(int *) ®isters[REGISTER_BYTE (regno) + i]); |
171 | if (errno != 0) |
172 | { |
173 | sprintf (buf, "writing register number %d(%d)", regno, i); |
174 | perror_with_name (buf); |
175 | } |
176 | regaddr += sizeof(int); |
e91b87a3 |
177 | } |
178 | } |
179 | } |
180 | \f |
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. */ |
185 | |
186 | int |
187 | read_inferior_memory (memaddr, myaddr, len) |
188 | CORE_ADDR memaddr; |
189 | char *myaddr; |
190 | int len; |
191 | { |
192 | register int i; |
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. */ |
196 | register int count |
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)); |
200 | extern int errno; |
201 | |
202 | /* Read all the longwords */ |
203 | for (i = 0; i < count; i++, addr += sizeof (int)) |
204 | { |
205 | errno = 0; |
7a67dd45 |
206 | #if 0 |
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 |
209 | serious side effects |
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. */ |
e91b87a3 |
213 | if (remote_debugging) |
214 | buffer[i] = remote_fetch_word (addr); |
215 | else |
7a67dd45 |
216 | #endif |
4187119d |
217 | buffer[i] = ptrace (PT_READ_I, inferior_pid, addr, 0); |
e91b87a3 |
218 | if (errno) |
219 | return errno; |
220 | } |
221 | |
222 | /* Copy appropriate bytes out of the buffer. */ |
223 | bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len); |
224 | return 0; |
225 | } |
226 | |
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. */ |
231 | |
232 | int |
233 | write_inferior_memory (memaddr, myaddr, len) |
234 | CORE_ADDR memaddr; |
235 | char *myaddr; |
236 | int len; |
237 | { |
238 | register int i; |
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. */ |
242 | register int count |
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)); |
246 | extern int errno; |
247 | |
248 | /* Fill start and end extra bytes of buffer with existing memory data. */ |
249 | |
250 | if (remote_debugging) |
251 | buffer[0] = remote_fetch_word (addr); |
252 | else |
4187119d |
253 | buffer[0] = ptrace (PT_READ_I, inferior_pid, addr, 0); |
e91b87a3 |
254 | |
255 | if (count > 1) |
256 | { |
257 | if (remote_debugging) |
258 | buffer[count - 1] |
259 | = remote_fetch_word (addr + (count - 1) * sizeof (int)); |
260 | else |
261 | buffer[count - 1] |
4187119d |
262 | = ptrace (PT_READ_I, inferior_pid, |
e91b87a3 |
263 | addr + (count - 1) * sizeof (int), 0); |
264 | } |
265 | |
266 | /* Copy data to be written over corresponding part of buffer */ |
267 | |
268 | bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len); |
269 | |
270 | /* Write the entire buffer. */ |
271 | |
272 | for (i = 0; i < count; i++, addr += sizeof (int)) |
273 | { |
274 | errno = 0; |
275 | if (remote_debugging) |
276 | remote_store_word (addr, buffer[i]); |
277 | else |
4187119d |
278 | ptrace (PT_WRITE_I, inferior_pid, addr, buffer[i]); |
e91b87a3 |
279 | if (errno) |
280 | return errno; |
281 | } |
282 | |
283 | return 0; |
284 | } |
285 | \f |
286 | /* Work with core dump and executable files, for GDB. |
287 | This code would be in core.c if it weren't machine-dependent. */ |
288 | |
e91b87a3 |
289 | #ifndef N_TXTADDR |
290 | #define N_TXTADDR(hdr) 0 |
291 | #endif /* no N_TXTADDR */ |
292 | |
293 | #ifndef N_DATADDR |
294 | #define N_DATADDR(hdr) hdr.a_text |
295 | #endif /* no N_DATADDR */ |
296 | |
297 | /* Make COFF and non-COFF names for things a little more compatible |
298 | to reduce conditionals later. */ |
299 | |
300 | #ifdef COFF_FORMAT |
301 | #define a_magic magic |
302 | #endif |
303 | |
304 | #ifndef COFF_FORMAT |
7a67dd45 |
305 | #ifndef AOUTHDR |
e91b87a3 |
306 | #define AOUTHDR struct exec |
307 | #endif |
7a67dd45 |
308 | #endif |
e91b87a3 |
309 | |
310 | extern char *sys_siglist[]; |
311 | |
312 | |
313 | /* Hook for `exec_file_command' command to call. */ |
314 | |
315 | extern void (*exec_file_display_hook) (); |
316 | |
317 | /* File names of core file and executable file. */ |
318 | |
319 | extern char *corefile; |
320 | extern char *execfile; |
321 | |
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. */ |
325 | |
326 | extern int corechan; |
327 | extern int execchan; |
328 | |
329 | /* Last modification time of executable file. |
330 | Also used in source.c to compare against mtime of a source file. */ |
331 | |
332 | extern int exec_mtime; |
333 | |
334 | /* Virtual addresses of bounds of the two areas of memory in the core file. */ |
335 | |
336 | extern CORE_ADDR data_start; |
337 | extern CORE_ADDR data_end; |
338 | extern CORE_ADDR stack_start; |
339 | extern CORE_ADDR stack_end; |
340 | |
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. */ |
343 | |
344 | extern CORE_ADDR text_start; |
345 | extern CORE_ADDR text_end; |
346 | |
347 | extern CORE_ADDR exec_data_start; |
348 | extern CORE_ADDR exec_data_end; |
349 | |
350 | /* Address in executable file of start of text area data. */ |
351 | |
352 | extern int text_offset; |
353 | |
354 | /* Address in executable file of start of data area data. */ |
355 | |
356 | extern int exec_data_offset; |
357 | |
358 | /* Address in core file of start of data area data. */ |
359 | |
360 | extern int data_offset; |
361 | |
362 | /* Address in core file of start of stack area data. */ |
363 | |
364 | extern int stack_offset; |
365 | |
366 | #ifdef COFF_FORMAT |
367 | /* various coff data structures */ |
368 | |
369 | extern FILHDR file_hdr; |
370 | extern SCNHDR text_hdr; |
371 | extern SCNHDR data_hdr; |
372 | |
373 | #endif /* not COFF_FORMAT */ |
374 | |
375 | /* a.out header saved in core file. */ |
376 | |
377 | extern AOUTHDR core_aouthdr; |
378 | |
379 | /* a.out header of exec file. */ |
380 | |
381 | extern AOUTHDR exec_aouthdr; |
382 | |
383 | extern void validate_files (); |
384 | \f |
385 | core_file_command (filename, from_tty) |
386 | char *filename; |
387 | int from_tty; |
388 | { |
389 | int val; |
390 | extern char registers[]; |
391 | |
392 | /* Discard all vestiges of any previous core file |
393 | and mark data and stack spaces as empty. */ |
394 | |
395 | if (corefile) |
396 | free (corefile); |
397 | corefile = 0; |
398 | |
399 | if (corechan >= 0) |
400 | close (corechan); |
401 | corechan = -1; |
402 | |
403 | data_start = 0; |
404 | data_end = 0; |
405 | stack_start = STACK_END_ADDR; |
406 | stack_end = STACK_END_ADDR; |
407 | |
408 | /* Now, if a new core file was specified, open it and digest it. */ |
409 | |
410 | if (filename) |
411 | { |
4187119d |
412 | filename = tilde_expand (filename); |
413 | make_cleanup (free, filename); |
414 | |
e91b87a3 |
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); |
418 | if (corechan < 0) |
419 | perror_with_name (filename); |
420 | /* 4.2-style (and perhaps also sysV-style) core dump file. */ |
421 | { |
422 | struct user u; |
423 | |
7a67dd45 |
424 | unsigned int reg_offset; |
e91b87a3 |
425 | |
426 | val = myread (corechan, &u, sizeof u); |
427 | if (val < 0) |
7a67dd45 |
428 | perror_with_name ("Not a core file: reading upage"); |
429 | if (val != sizeof u) |
430 | error ("Not a core file: could only read %d bytes", val); |
431 | |
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. */ |
e91b87a3 |
436 | data_start = exec_data_start; |
437 | |
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); |
7a67dd45 |
442 | |
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; |
e91b87a3 |
448 | |
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); |
452 | |
453 | /* Read the register values out of the core file and store |
454 | them where `read_register' will find them. */ |
455 | |
456 | { |
457 | register int regno; |
458 | |
459 | for (regno = 0; regno < NUM_REGS; regno++) |
460 | { |
461 | char buf[MAX_REGISTER_RAW_SIZE]; |
462 | |
463 | val = lseek (corechan, register_addr (regno, reg_offset), 0); |
7a67dd45 |
464 | if (val < 0 |
465 | || (val = myread (corechan, buf, sizeof buf)) < 0) |
466 | { |
467 | char * buffer = (char *) alloca (strlen (reg_names[regno]) |
468 | + 30); |
469 | strcpy (buffer, "Reading register "); |
470 | strcat (buffer, reg_names[regno]); |
471 | |
472 | perror_with_name (buffer); |
473 | } |
e91b87a3 |
474 | |
e91b87a3 |
475 | supply_register (regno, buf); |
476 | } |
477 | } |
478 | } |
479 | if (filename[0] == '/') |
480 | corefile = savestring (filename, strlen (filename)); |
481 | else |
482 | { |
483 | corefile = concat (current_directory, "/", filename); |
484 | } |
485 | |
486 | set_current_frame ( create_new_frame (read_register (FP_REGNUM), |
487 | read_pc ())); |
488 | select_frame (get_current_frame (), 0); |
489 | validate_files (); |
490 | } |
491 | else if (from_tty) |
492 | printf ("No core file now.\n"); |
493 | } |
494 | \f |
495 | exec_file_command (filename, from_tty) |
496 | char *filename; |
497 | int from_tty; |
498 | { |
499 | int val; |
500 | |
501 | /* Eliminate all traces of old exec file. |
502 | Mark text segment as empty. */ |
503 | |
504 | if (execfile) |
505 | free (execfile); |
506 | execfile = 0; |
507 | data_start = 0; |
508 | data_end -= exec_data_start; |
509 | text_start = 0; |
510 | text_end = 0; |
511 | exec_data_start = 0; |
512 | exec_data_end = 0; |
513 | if (execchan >= 0) |
514 | close (execchan); |
515 | execchan = -1; |
516 | |
517 | /* Now open and digest the file the user requested, if any. */ |
518 | |
519 | if (filename) |
520 | { |
4187119d |
521 | filename = tilde_expand (filename); |
522 | make_cleanup (free, filename); |
523 | |
e91b87a3 |
524 | execchan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0, |
525 | &execfile); |
526 | if (execchan < 0) |
527 | perror_with_name (filename); |
528 | |
529 | #ifdef COFF_FORMAT |
530 | { |
531 | int aout_hdrsize; |
532 | int num_sections; |
533 | |
534 | if (read_file_hdr (execchan, &file_hdr) < 0) |
535 | error ("\"%s\": not in executable format.", execfile); |
536 | |
537 | aout_hdrsize = file_hdr.f_opthdr; |
538 | num_sections = file_hdr.f_nscns; |
539 | |
540 | if (read_aout_hdr (execchan, &exec_aouthdr, aout_hdrsize) < 0) |
541 | error ("\"%s\": can't read optional aouthdr", execfile); |
542 | |
7a67dd45 |
543 | if (read_section_hdr (execchan, _TEXT, &text_hdr, num_sections, |
544 | aout_hdrsize) < 0) |
e91b87a3 |
545 | error ("\"%s\": can't read text section header", execfile); |
546 | |
7a67dd45 |
547 | if (read_section_hdr (execchan, _DATA, &data_hdr, num_sections, |
548 | aout_hdrsize) < 0) |
e91b87a3 |
549 | error ("\"%s\": can't read data section header", execfile); |
550 | |
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; |
560 | } |
561 | #else /* not COFF_FORMAT */ |
562 | { |
563 | struct stat st_exec; |
564 | |
565 | #ifdef HEADER_SEEK_FD |
566 | HEADER_SEEK_FD (execchan); |
567 | #endif |
568 | |
569 | val = myread (execchan, &exec_aouthdr, sizeof (AOUTHDR)); |
570 | |
571 | if (val < 0) |
572 | perror_with_name (filename); |
573 | |
574 | text_start = N_TXTADDR (exec_aouthdr); |
575 | exec_data_start = N_DATADDR (exec_aouthdr); |
576 | |
577 | text_offset = N_TXTOFF (exec_aouthdr); |
578 | exec_data_offset = N_TXTOFF (exec_aouthdr) + exec_aouthdr.a_text; |
579 | |
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; |
584 | |
585 | fstat (execchan, &st_exec); |
586 | exec_mtime = st_exec.st_mtime; |
587 | } |
588 | #endif /* not COFF_FORMAT */ |
589 | |
590 | validate_files (); |
591 | } |
592 | else if (from_tty) |
593 | printf ("No exec file now.\n"); |
594 | |
595 | /* Tell display code (if any) about the changed file name. */ |
596 | if (exec_file_display_hook) |
597 | (*exec_file_display_hook) (filename); |
598 | } |
7a67dd45 |
599 | |
600 | void |
601 | _initialize_hp300bsd_dep () |
602 | { |
603 | struct nlist names[2]; |
604 | |
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; |
610 | else |
611 | kernel_u_addr = 0x00917000; |
612 | } |