gdb-3.5
[deliverable/binutils-gdb.git] / gdb / hp9k320-dep.c
1 /* Low level interface to ptrace, for GDB when running under Unix.
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. */
19
20 #include <stdio.h>
21 #include "defs.h"
22 #include "param.h"
23 #include "frame.h"
24 #include "inferior.h"
25
26 #define WOPR
27 #include <sys/param.h>
28 #include <sys/dir.h>
29 #include <signal.h>
30 #include <sys/user.h>
31 #include <sys/ioctl.h>
32 #include <fcntl.h>
33
34 #include <sys/ptrace.h>
35 #include <sys/reg.h>
36 #include <sys/trap.h>
37
38 #include <a.out.h>
39 #include <sys/file.h>
40 #include <sys/stat.h>
41
42 extern int errno;
43 \f
44 /* This function simply calls ptrace with the given arguments.
45 It exists so that all calls to ptrace are isolated in this
46 machine-dependent file. */
47 int
48 call_ptrace (request, pid, arg3, arg4)
49 int request, pid, arg3, arg4;
50 {
51 return ptrace (request, pid, arg3, arg4);
52 }
53
54 #ifdef ATTACH_DETACH
55
56 extern int attach_flag ;
57
58 /* Start debugging the process whose number is PID. */
59
60 attach (pid)
61 int pid;
62 {
63 errno = 0;
64 ptrace (PT_ATTACH, pid, 0, 0);
65 if (errno)
66 perror_with_name ("ptrace");
67 attach_flag = 1;
68 return pid;
69 }
70
71 /* Stop debugging the process whose number is PID
72 and continue it with signal number SIGNAL.
73 SIGNAL = 0 means just continue it. */
74
75 void
76 detach (signal)
77 int signal;
78 {
79 errno = 0;
80 ptrace (PT_DETACH, inferior_pid, 1, signal);
81 if (errno)
82 perror_with_name ("ptrace");
83 attach_flag = 0;
84 }
85 #endif /* ATTACH_DETACH */
86
87 kill_inferior ()
88 {
89 if (remote_debugging)
90 return;
91 if (inferior_pid == 0)
92 return;
93 ptrace (8, inferior_pid, 0, 0);
94 wait (0);
95 inferior_died ();
96 }
97
98 /* This is used when GDB is exiting. It gives less chance of error.*/
99
100 kill_inferior_fast ()
101 {
102 if (remote_debugging)
103 return;
104 if (inferior_pid == 0)
105 return;
106 ptrace (8, inferior_pid, 0, 0);
107 wait (0);
108 }
109
110 /* Resume execution of the inferior process.
111 If STEP is nonzero, single-step it.
112 If SIGNAL is nonzero, give it that signal. */
113
114 void
115 resume (step, signal)
116 int step;
117 int signal;
118 {
119 errno = 0;
120 if (remote_debugging)
121 remote_resume (step, signal);
122 else
123 {
124 ptrace (step ? 9 : 7, inferior_pid, 1, signal);
125 if (errno)
126 perror_with_name ("ptrace");
127 }
128 }
129 \f
130 #define INFERIOR_AR0(u) \
131 ((ptrace \
132 (PT_RUAREA, inferior_pid, ((char *) &u.u_ar0 - (char *) &u), 0)) \
133 - KERNEL_U_ADDR)
134
135 static void
136 fetch_inferior_register (regno, regaddr)
137 register int regno;
138 register unsigned int regaddr;
139 {
140 #ifndef HPUX_VERSION_5
141 if (regno == PS_REGNUM)
142 {
143 union { int i; short s[2]; } ps_val;
144 int regval;
145
146 ps_val.i = (ptrace (PT_RUAREA, inferior_pid, regaddr, 0));
147 regval = ps_val.s[0];
148 supply_register (regno, &regval);
149 }
150 else
151 #endif /* not HPUX_VERSION_5 */
152 {
153 char buf[MAX_REGISTER_RAW_SIZE];
154 register int i;
155
156 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
157 {
158 *(int *) &buf[i] = ptrace (PT_RUAREA, inferior_pid, regaddr, 0);
159 regaddr += sizeof (int);
160 }
161 supply_register (regno, buf);
162 }
163 return;
164 }
165
166 static void
167 store_inferior_register_1 (regno, regaddr, value)
168 int regno;
169 unsigned int regaddr;
170 int value;
171 {
172 errno = 0;
173 ptrace (PT_WUAREA, inferior_pid, regaddr, value);
174 #if 0
175 /* HP-UX randomly sets errno to non-zero for regno == 25.
176 However, the value is correctly written, so ignore errno. */
177 if (errno != 0)
178 {
179 char string_buf[64];
180
181 sprintf (string_buf, "writing register number %d", regno);
182 perror_with_name (string_buf);
183 }
184 #endif
185 return;
186 }
187
188 static void
189 store_inferior_register (regno, regaddr)
190 register int regno;
191 register unsigned int regaddr;
192 {
193 #ifndef HPUX_VERSION_5
194 if (regno == PS_REGNUM)
195 {
196 union { int i; short s[2]; } ps_val;
197
198 ps_val.i = (ptrace (PT_RUAREA, inferior_pid, regaddr, 0));
199 ps_val.s[0] = (read_register (regno));
200 store_inferior_register_1 (regno, regaddr, ps_val.i);
201 }
202 else
203 #endif /* not HPUX_VERSION_5 */
204 {
205 char buf[MAX_REGISTER_RAW_SIZE];
206 register int i;
207 extern char registers[];
208
209 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
210 {
211 store_inferior_register_1
212 (regno, regaddr,
213 (*(int *) &registers[(REGISTER_BYTE (regno)) + i]));
214 regaddr += sizeof (int);
215 }
216 }
217 return;
218 }
219
220 void
221 fetch_inferior_registers ()
222 {
223 struct user u;
224 register int regno;
225 register unsigned int ar0_offset;
226 extern char registers[];
227
228 if (remote_debugging)
229 remote_fetch_registers (registers);
230 else
231 {
232 ar0_offset = (INFERIOR_AR0 (u));
233 for (regno = 0; (regno < FP0_REGNUM); regno++)
234 fetch_inferior_register (regno, (REGISTER_ADDR (ar0_offset, regno)));
235 for (; (regno < NUM_REGS); regno++)
236 fetch_inferior_register (regno, (FP_REGISTER_ADDR (u, regno)));
237 }
238 }
239
240 /* Store our register values back into the inferior.
241 If REGNO is -1, do this for all registers.
242 Otherwise, REGNO specifies which register (so we can save time). */
243
244 store_inferior_registers (regno)
245 register int regno;
246 {
247 struct user u;
248 register unsigned int ar0_offset;
249 extern char registers[];
250
251 if (remote_debugging)
252 remote_store_registers (registers);
253 else
254 {
255 if (regno >= FP0_REGNUM)
256 {
257 store_inferior_register (regno, (FP_REGISTER_ADDR (u, regno)));
258 return;
259 }
260
261 ar0_offset = (INFERIOR_AR0 (u));
262 if (regno >= 0)
263 {
264 store_inferior_register (regno, (REGISTER_ADDR (ar0_offset, regno)));
265 return;
266 }
267
268 for (regno = 0; (regno < FP0_REGNUM); regno++)
269 store_inferior_register (regno, (REGISTER_ADDR (ar0_offset, regno)));
270 for (; (regno < NUM_REGS); regno++)
271 store_inferior_register (regno, (FP_REGISTER_ADDR (u, regno)));
272 }
273 return;
274 }
275
276 \f
277 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
278 in the NEW_SUN_PTRACE case.
279 It ought to be straightforward. But it appears that writing did
280 not write the data that I specified. I cannot understand where
281 it got the data that it actually did write. */
282
283 /* Copy LEN bytes from inferior's memory starting at MEMADDR
284 to debugger memory starting at MYADDR.
285 On failure (cannot read from inferior, usually because address is out
286 of bounds) returns the value of errno. */
287
288 int
289 read_inferior_memory (memaddr, myaddr, len)
290 CORE_ADDR memaddr;
291 char *myaddr;
292 int len;
293 {
294 register int i;
295 /* Round starting address down to longword boundary. */
296 register CORE_ADDR addr = memaddr & - sizeof (int);
297 /* Round ending address up; get number of longwords that makes. */
298 register int count
299 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
300 /* Allocate buffer of that many longwords. */
301 register int *buffer = (int *) alloca (count * sizeof (int));
302 extern int errno;
303
304 /* Read all the longwords */
305 for (i = 0; i < count; i++, addr += sizeof (int))
306 {
307 errno = 0;
308 if (remote_debugging)
309 buffer[i] = remote_fetch_word (addr);
310 else
311 buffer[i] = ptrace (1, inferior_pid, addr, 0);
312 if (errno)
313 return errno;
314 }
315
316 /* Copy appropriate bytes out of the buffer. */
317 bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
318 return 0;
319 }
320
321 /* Copy LEN bytes of data from debugger memory at MYADDR
322 to inferior's memory at MEMADDR.
323 On failure (cannot write the inferior)
324 returns the value of errno. */
325
326 int
327 write_inferior_memory (memaddr, myaddr, len)
328 CORE_ADDR memaddr;
329 char *myaddr;
330 int len;
331 {
332 register int i;
333 /* Round starting address down to longword boundary. */
334 register CORE_ADDR addr = memaddr & - sizeof (int);
335 /* Round ending address up; get number of longwords that makes. */
336 register int count
337 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
338 /* Allocate buffer of that many longwords. */
339 register int *buffer = (int *) alloca (count * sizeof (int));
340 extern int errno;
341
342 /* Fill start and end extra bytes of buffer with existing memory data. */
343
344 if (remote_debugging)
345 buffer[0] = remote_fetch_word (addr);
346 else
347 buffer[0] = ptrace (1, inferior_pid, addr, 0);
348
349 if (count > 1)
350 {
351 if (remote_debugging)
352 buffer[count - 1]
353 = remote_fetch_word (addr + (count - 1) * sizeof (int));
354 else
355 buffer[count - 1]
356 = ptrace (1, inferior_pid,
357 addr + (count - 1) * sizeof (int), 0);
358 }
359
360 /* Copy data to be written over corresponding part of buffer */
361
362 bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
363
364 /* Write the entire buffer. */
365
366 for (i = 0; i < count; i++, addr += sizeof (int))
367 {
368 errno = 0;
369 if (remote_debugging)
370 remote_store_word (addr, buffer[i]);
371 else
372 ptrace (4, inferior_pid, addr, buffer[i]);
373 if (errno)
374 return errno;
375 }
376
377 return 0;
378 }
379 \f
380 /* Work with core dump and executable files, for GDB.
381 This code would be in core.c if it weren't machine-dependent. */
382
383 /* This should probably be deleted. */
384 /* Recognize COFF format systems because a.out.h defines AOUTHDR. */
385 #ifdef AOUTHDR
386 #define COFF_FORMAT
387 #endif
388
389 #ifdef HPUX_VERSION_5
390 #define e_PS e_regs[PS]
391 #define e_PC e_regs[PC]
392 #endif /* HPUX_VERSION_5 */
393
394
395 #ifndef N_TXTADDR
396 #define N_TXTADDR(hdr) 0
397 #endif /* no N_TXTADDR */
398
399 #ifndef N_DATADDR
400 #define N_DATADDR(hdr) hdr.a_text
401 #endif /* no N_DATADDR */
402
403 /* Make COFF and non-COFF names for things a little more compatible
404 to reduce conditionals later. */
405
406 #ifdef COFF_FORMAT
407 #define a_magic magic
408 #endif
409
410 #ifndef COFF_FORMAT
411 #ifndef AOUTHDR
412 #define AOUTHDR struct exec
413 #endif
414 #endif
415
416 extern char *sys_siglist[];
417
418
419 /* Hook for `exec_file_command' command to call. */
420
421 extern void (*exec_file_display_hook) ();
422
423 /* File names of core file and executable file. */
424
425 extern char *corefile;
426 extern char *execfile;
427
428 /* Descriptors on which core file and executable file are open.
429 Note that the execchan is closed when an inferior is created
430 and reopened if the inferior dies or is killed. */
431
432 extern int corechan;
433 extern int execchan;
434
435 /* Last modification time of executable file.
436 Also used in source.c to compare against mtime of a source file. */
437
438 extern int exec_mtime;
439
440 /* Virtual addresses of bounds of the two areas of memory in the core file. */
441
442 extern CORE_ADDR data_start;
443 extern CORE_ADDR data_end;
444 extern CORE_ADDR stack_start;
445 extern CORE_ADDR stack_end;
446
447 /* Virtual addresses of bounds of two areas of memory in the exec file.
448 Note that the data area in the exec file is used only when there is no core file. */
449
450 extern CORE_ADDR text_start;
451 extern CORE_ADDR text_end;
452
453 extern CORE_ADDR exec_data_start;
454 extern CORE_ADDR exec_data_end;
455
456 /* Address in executable file of start of text area data. */
457
458 extern int text_offset;
459
460 /* Address in executable file of start of data area data. */
461
462 extern int exec_data_offset;
463
464 /* Address in core file of start of data area data. */
465
466 extern int data_offset;
467
468 /* Address in core file of start of stack area data. */
469
470 extern int stack_offset;
471
472 #ifdef COFF_FORMAT
473 /* various coff data structures */
474
475 extern FILHDR file_hdr;
476 extern SCNHDR text_hdr;
477 extern SCNHDR data_hdr;
478
479 #endif /* not COFF_FORMAT */
480
481 /* a.out header saved in core file. */
482
483 extern AOUTHDR core_aouthdr;
484
485 /* a.out header of exec file. */
486
487 extern AOUTHDR exec_aouthdr;
488
489 extern void validate_files ();
490 \f
491 core_file_command (filename, from_tty)
492 char *filename;
493 int from_tty;
494 {
495 int val;
496 extern char registers[];
497
498 /* Discard all vestiges of any previous core file
499 and mark data and stack spaces as empty. */
500
501 if (corefile)
502 free (corefile);
503 corefile = 0;
504
505 if (corechan >= 0)
506 close (corechan);
507 corechan = -1;
508
509 data_start = 0;
510 data_end = 0;
511 stack_start = STACK_END_ADDR;
512 stack_end = STACK_END_ADDR;
513
514 /* Now, if a new core file was specified, open it and digest it. */
515
516 if (filename)
517 {
518 filename = tilde_expand (filename);
519 make_cleanup (free, filename);
520
521 if (have_inferior_p ())
522 error ("To look at a core file, you must kill the inferior with \"kill\".");
523 corechan = open (filename, O_RDONLY, 0);
524 if (corechan < 0)
525 perror_with_name (filename);
526 /* 4.2-style (and perhaps also sysV-style) core dump file. */
527 {
528 struct user u;
529
530 int reg_offset;
531
532 val = myread (corechan, &u, sizeof u);
533 if (val < 0)
534 perror_with_name (filename);
535 data_start = exec_data_start;
536
537 data_end = data_start + NBPG * u.u_dsize;
538 stack_start = stack_end - NBPG * u.u_ssize;
539 data_offset = NBPG * UPAGES;
540 stack_offset = NBPG * (UPAGES + u.u_dsize);
541 reg_offset = (int) u.u_ar0 - KERNEL_U_ADDR;
542
543 /* I don't know where to find this info.
544 So, for now, mark it as not available. */
545 core_aouthdr.a_magic = 0;
546
547 /* Read the register values out of the core file and store
548 them where `read_register' will find them. */
549
550 {
551 register int regno;
552 struct exception_stack es;
553 int val;
554
555 val = lseek (corechan, (REGISTER_ADDR (reg_offset, 0)), 0);
556 if (val < 0)
557 perror_with_name (filename);
558 val = myread (corechan, es,
559 ((char *) &es.e_offset - (char *) &es.e_regs[R0]));
560 if (val < 0)
561 perror_with_name (filename);
562 for (regno = 0; (regno < PS_REGNUM); regno++)
563 supply_register (regno, &es.e_regs[regno + R0]);
564 val = es.e_PS;
565 supply_register (regno++, &val);
566 supply_register (regno++, &es.e_PC);
567 for (; (regno < NUM_REGS); regno++)
568 {
569 char buf[MAX_REGISTER_RAW_SIZE];
570
571 val = lseek (corechan, (FP_REGISTER_ADDR (u, regno)), 0);
572 if (val < 0)
573 perror_with_name (filename);
574
575 val = myread (corechan, buf, sizeof buf);
576 if (val < 0)
577 perror_with_name (filename);
578 supply_register (regno, buf);
579 }
580 }
581 }
582 if (filename[0] == '/')
583 corefile = savestring (filename, strlen (filename));
584 else
585 {
586 corefile = concat (current_directory, "/", filename);
587 }
588
589 set_current_frame ( create_new_frame (read_register (FP_REGNUM),
590 read_pc ()));
591 select_frame (get_current_frame (), 0);
592 validate_files ();
593 }
594 else if (from_tty)
595 printf ("No core file now.\n");
596 }
597 \f
598 exec_file_command (filename, from_tty)
599 char *filename;
600 int from_tty;
601 {
602 int val;
603
604 /* Eliminate all traces of old exec file.
605 Mark text segment as empty. */
606
607 if (execfile)
608 free (execfile);
609 execfile = 0;
610 data_start = 0;
611 data_end -= exec_data_start;
612 text_start = 0;
613 text_end = 0;
614 exec_data_start = 0;
615 exec_data_end = 0;
616 if (execchan >= 0)
617 close (execchan);
618 execchan = -1;
619
620 /* Now open and digest the file the user requested, if any. */
621
622 if (filename)
623 {
624 filename = tilde_expand (filename);
625 make_cleanup (free, filename);
626
627 execchan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
628 &execfile);
629 if (execchan < 0)
630 perror_with_name (filename);
631
632 #ifdef COFF_FORMAT
633 {
634 int aout_hdrsize;
635 int num_sections;
636
637 if (read_file_hdr (execchan, &file_hdr) < 0)
638 error ("\"%s\": not in executable format.", execfile);
639
640 aout_hdrsize = file_hdr.f_opthdr;
641 num_sections = file_hdr.f_nscns;
642
643 if (read_aout_hdr (execchan, &exec_aouthdr, aout_hdrsize) < 0)
644 error ("\"%s\": can't read optional aouthdr", execfile);
645
646 if (read_section_hdr (execchan, _TEXT, &text_hdr, num_sections,
647 aout_hdrsize) < 0)
648 error ("\"%s\": can't read text section header", execfile);
649
650 if (read_section_hdr (execchan, _DATA, &data_hdr, num_sections,
651 aout_hdrsize) < 0)
652 error ("\"%s\": can't read data section header", execfile);
653
654 text_start = exec_aouthdr.text_start;
655 text_end = text_start + exec_aouthdr.tsize;
656 text_offset = text_hdr.s_scnptr;
657 exec_data_start = exec_aouthdr.data_start;
658 exec_data_end = exec_data_start + exec_aouthdr.dsize;
659 exec_data_offset = data_hdr.s_scnptr;
660 data_start = exec_data_start;
661 data_end += exec_data_start;
662 exec_mtime = file_hdr.f_timdat;
663 }
664 #else /* not COFF_FORMAT */
665 {
666 struct stat st_exec;
667
668 val = myread (execchan, &exec_aouthdr, sizeof (AOUTHDR));
669
670 if (val < 0)
671 perror_with_name (filename);
672
673 text_start = N_TXTADDR (exec_aouthdr);
674 exec_data_start = N_DATADDR (exec_aouthdr);
675
676 text_offset = N_TXTOFF (exec_aouthdr);
677 exec_data_offset = N_TXTOFF (exec_aouthdr) + exec_aouthdr.a_text;
678
679 text_end = text_start + exec_aouthdr.a_text;
680 exec_data_end = exec_data_start + exec_aouthdr.a_data;
681 data_start = exec_data_start;
682 data_end += exec_data_start;
683
684 fstat (execchan, &st_exec);
685 exec_mtime = st_exec.st_mtime;
686 }
687 #endif /* not COFF_FORMAT */
688
689 validate_files ();
690 }
691 else if (from_tty)
692 printf ("No exec file now.\n");
693
694 /* Tell display code (if any) about the changed file name. */
695 if (exec_file_display_hook)
696 (*exec_file_display_hook) (filename);
697 }
698
This page took 0.046656 seconds and 4 git commands to generate.