1 /* Machine-dependent hooks for the unix child process stratum. This
2 code is for the HP PA-RISC cpu.
4 Copyright 1986, 1987, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
6 Contributed by the Center for Software Science at the
7 University of Utah (pa-gdb-bugs@cs.utah.edu).
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
29 #include <sys/ptrace.h>
30 #include <sys/param.h>
33 extern CORE_ADDR text_end
;
35 static void fetch_register ();
37 /* This function simply calls ptrace with the given arguments.
38 It exists so that all calls to ptrace are isolated in this
39 machine-dependent file. */
41 call_ptrace (request
, pid
, addr
, data
)
43 PTRACE_ARG3_TYPE addr
;
46 return ptrace (request
, pid
, addr
, data
, 0);
52 if (inferior_pid
== 0)
54 ptrace (PT_EXIT
, inferior_pid
, (PTRACE_ARG3_TYPE
) 0, 0, 0);
56 target_mourn_inferior ();
59 /* Start debugging the process whose number is PID. */
65 ptrace (PT_ATTACH
, pid
, (PTRACE_ARG3_TYPE
) 0, 0, 0);
67 perror_with_name ("ptrace");
72 /* Stop debugging the process whose number is PID
73 and continue it with signal number SIGNAL.
74 SIGNAL = 0 means just continue it. */
81 ptrace (PT_DETACH
, inferior_pid
, (PTRACE_ARG3_TYPE
) 1, signal
, 0);
83 perror_with_name ("ptrace");
87 /* Fetch all registers, or just one, from the child process. */
90 fetch_inferior_registers (regno
)
94 for (regno
= 0; regno
< NUM_REGS
; regno
++)
95 fetch_register (regno
);
97 fetch_register (regno
);
100 /* Store our register values back into the inferior.
101 If REGNO is -1, do this for all registers.
102 Otherwise, REGNO specifies which register (so we can save time). */
105 store_inferior_registers (regno
)
108 register unsigned int regaddr
;
110 extern char registers
[];
112 unsigned int offset
= U_REGS_OFFSET
;
117 regaddr
= register_addr (regno
, offset
);
119 if (regno
== PCOQ_HEAD_REGNUM
|| regno
== PCOQ_TAIL_REGNUM
)
121 scratch
= *(int *) ®isters
[REGISTER_BYTE (regno
)] | 0x3;
122 ptrace (PT_WUREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
126 sprintf (buf
, "writing register number %d(%d)", regno
, i
);
127 perror_with_name (buf
);
131 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof(int))
134 ptrace (PT_WUREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
135 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
], 0);
138 sprintf (buf
, "writing register number %d(%d)", regno
, i
);
139 perror_with_name (buf
);
141 regaddr
+= sizeof(int);
146 for (regno
= 0; regno
< NUM_REGS
; regno
++)
148 if (CANNOT_STORE_REGISTER (regno
))
150 regaddr
= register_addr (regno
, offset
);
152 if (regno
== PCOQ_HEAD_REGNUM
|| regno
== PCOQ_TAIL_REGNUM
)
154 scratch
= *(int *) ®isters
[REGISTER_BYTE (regno
)] | 0x3;
155 ptrace (PT_WUREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
159 sprintf (buf
, "writing register number %d(%d)", regno
, i
);
160 perror_with_name (buf
);
164 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof(int))
167 ptrace (PT_WUREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
168 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
], 0);
171 sprintf (buf
, "writing register number %d(%d)", regno
, i
);
172 perror_with_name (buf
);
174 regaddr
+= sizeof(int);
181 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
182 to get the offset in the core file of the register values. */
184 /* Get kernel_u_addr using HPUX-style nlist(). */
185 CORE_ADDR kernel_u_addr
;
190 unsigned char n_type
;
191 unsigned char n_length
;
195 static struct hpnlist nl
[] = {{ "_u", -1, }, { (char *) 0, }};
197 /* read the value of the u area from the hp-ux kernel */
198 void _initialize_kernel_u_addr ()
201 nlist ("/hp-ux", &nl
);
202 kernel_u_addr
= nl
[0].n_value
;
205 #if !defined (offsetof)
206 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
209 /* U_REGS_OFFSET is the offset of the registers within the u area. */
210 #if !defined (U_REGS_OFFSET)
211 #define U_REGS_OFFSET \
212 ptrace (PT_READ_U, inferior_pid, \
213 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0, 0) \
217 /* Fetch one register. */
220 fetch_register (regno
)
223 register unsigned int regaddr
;
224 char buf
[MAX_REGISTER_RAW_SIZE
];
225 char mess
[128]; /* For messages */
228 /* Offset of registers within the u area. */
231 offset
= U_REGS_OFFSET
;
233 regaddr
= register_addr (regno
, offset
);
234 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
237 *(int *) &buf
[i
] = ptrace (PT_RUREGS
, inferior_pid
,
238 (PTRACE_ARG3_TYPE
) regaddr
, 0, 0);
239 regaddr
+= sizeof (int);
242 sprintf (mess
, "reading register %s (#%d)", reg_names
[regno
], regno
);
243 perror_with_name (mess
);
246 if (regno
== PCOQ_HEAD_REGNUM
|| regno
== PCOQ_TAIL_REGNUM
)
248 supply_register (regno
, buf
);
251 /* Resume execution of the inferior process.
252 If STEP is nonzero, single-step it.
253 If SIGNAL is nonzero, give it that signal. */
256 child_resume (step
, signal
)
262 /* An address of (PTRACE_ARG3_TYPE) 1 tells ptrace to continue from where
263 it was. (If GDB wanted it to start some other way, we have already
264 written a new PC value to the child.) */
267 ptrace (PT_SINGLE
, inferior_pid
, (PTRACE_ARG3_TYPE
) 1, signal
, 0);
269 ptrace (PT_CONTIN
, inferior_pid
, (PTRACE_ARG3_TYPE
) 1, signal
, 0);
272 perror_with_name ("ptrace");
275 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
276 in the NEW_SUN_PTRACE case.
277 It ought to be straightforward. But it appears that writing did
278 not write the data that I specified. I cannot understand where
279 it got the data that it actually did write. */
281 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
282 to debugger memory starting at MYADDR. Copy to inferior if
285 Returns the length copied, which is either the LEN argument or zero.
286 This xfer function does not do partial moves, since child_ops
287 doesn't allow memory operations to cross below us in the target stack
291 child_xfer_memory (memaddr
, myaddr
, len
, write
, target
)
296 struct target_ops
*target
; /* ignored */
299 /* Round starting address down to longword boundary. */
300 register CORE_ADDR addr
= memaddr
& - sizeof (int);
301 /* Round ending address up; get number of longwords that makes. */
303 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
304 /* Allocate buffer of that many longwords. */
305 register int *buffer
= (int *) alloca (count
* sizeof (int));
309 /* Fill start and end extra bytes of buffer with existing memory data. */
311 if (addr
!= memaddr
|| len
< (int)sizeof (int)) {
312 /* Need part of initial word -- fetch it. */
313 buffer
[0] = ptrace (addr
< text_end
? PT_RIUSER
: PT_RDUSER
,
314 inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0, 0);
317 if (count
> 1) /* FIXME, avoid if even boundary */
320 = ptrace (addr
< text_end
? PT_RIUSER
: PT_RDUSER
, inferior_pid
,
321 (PTRACE_ARG3_TYPE
) (addr
+ (count
- 1) * sizeof (int)),
325 /* Copy data to be written over corresponding part of buffer */
327 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
329 /* Write the entire buffer. */
331 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
333 /* The HP-UX kernel crashes if you use PT_WDUSER to write into the text
334 segment. FIXME -- does it work to write into the data segment using
335 WIUSER, or do these idiots really expect us to figure out which segment
336 the address is in, so we can use a separate system call for it??! */
338 ptrace (addr
< text_end
? PT_WIUSER
: PT_WDUSER
, inferior_pid
,
339 (PTRACE_ARG3_TYPE
) addr
,
347 /* Read all the longwords */
348 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
351 buffer
[i
] = ptrace (addr
< text_end
? PT_RIUSER
: PT_RDUSER
,
352 inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0, 0);
358 /* Copy appropriate bytes out of the buffer. */
359 bcopy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);