1 /* Host-dependent code for HP PA-RISC runing HP/UX, for GDB.
2 Copyright 1986, 1987, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
3 Contributed by the Center for Software Science at the
4 University of Utah (pa-gdb-bugs@cs.utah.edu).
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
28 #include <sys/types.h>
31 #include <sys/param.h>
34 #include <sys/ioctl.h>
36 #include <sys/ptrace.h>
40 #define PT_ATTACH PTRACE_ATTACH
43 #define PT_DETACH PTRACE_DETACH
48 /* This function simply calls ptrace with the given arguments.
49 It exists so that all calls to ptrace are isolated in this
50 machine-dependent file. */
52 call_ptrace (request
, pid
, addr
, data
)
54 PTRACE_ARG3_TYPE addr
;
57 return ptrace (request
, pid
, addr
, data
, 0);
61 /* For the rest of the file, use an extra level of indirection */
62 /* This lets us breakpoint usefully on call_ptrace. */
63 #define ptrace call_ptrace
69 if (inferior_pid
== 0)
71 ptrace (PT_EXIT
, inferior_pid
, (PTRACE_ARG3_TYPE
) 0, 0, 0); /* PT_EXIT = PT_KILL ? */
73 target_mourn_inferior ();
76 /* Resume execution of the inferior process.
77 If STEP is nonzero, single-step it.
78 If SIGNAL is nonzero, give it that signal. */
81 child_resume (step
, signal
)
87 /* An address of (PTRACE_ARG3_TYPE) 1 tells ptrace to continue from where
88 it was. (If GDB wanted it to start some other way, we have already
89 written a new PC value to the child.) */
92 ptrace (PT_SINGLE
, inferior_pid
, (PTRACE_ARG3_TYPE
) 1, signal
, 0);
94 ptrace (PT_CONTIN
, inferior_pid
, (PTRACE_ARG3_TYPE
) 1, signal
, 0);
97 perror_with_name ("ptrace");
101 /* Nonzero if we are debugging an attached process rather than
103 extern int attach_flag
;
105 /* Start debugging the process whose number is PID. */
111 ptrace (PT_ATTACH
, pid
, (PTRACE_ARG3_TYPE
) 0, 0, 0);
113 perror_with_name ("ptrace");
118 /* Stop debugging the process whose number is PID
119 and continue it with signal number SIGNAL.
120 SIGNAL = 0 means just continue it. */
127 ptrace (PT_DETACH
, inferior_pid
, (PTRACE_ARG3_TYPE
) 1, signal
, 0);
129 perror_with_name ("ptrace");
132 #endif /* ATTACH_DETACH */
134 #if !defined (FETCH_INFERIOR_REGISTERS)
136 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
137 to get the offset in the core file of the register values. */
138 #if defined (KERNEL_U_ADDR_BSD)
139 /* Get kernel_u_addr using BSD-style nlist(). */
140 CORE_ADDR kernel_u_addr
;
142 #include <a.out.gnu.h> /* For struct nlist */
145 _initialize_kernel_u_addr ()
147 struct nlist names
[2];
149 names
[0].n_un
.n_name
= "_u";
150 names
[1].n_un
.n_name
= NULL
;
151 if (nlist ("/vmunix", names
) == 0)
152 kernel_u_addr
= names
[0].n_value
;
154 fatal ("Unable to get kernel u area address.");
156 #endif /* KERNEL_U_ADDR_BSD. */
158 #if defined (KERNEL_U_ADDR_HPUX)
159 /* Get kernel_u_addr using HPUX-style nlist(). */
160 CORE_ADDR kernel_u_addr
;
165 unsigned char n_type
;
166 unsigned char n_length
;
170 static struct hpnlist nl
[] = {{ "_u", -1, }, { (char *) 0, }};
172 /* read the value of the u area from the hp-ux kernel */
173 void _initialize_kernel_u_addr ()
176 nlist ("/hp-ux", &nl
);
177 kernel_u_addr
= nl
[0].n_value
;
179 #endif /* KERNEL_U_ADDR_HPUX. */
181 #if !defined (offsetof)
182 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
185 /* U_REGS_OFFSET is the offset of the registers within the u area. */
186 #if !defined (U_REGS_OFFSET)
187 #define U_REGS_OFFSET \
188 ptrace (PT_READ_U, inferior_pid, \
189 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0, 0) \
193 /* Registers we shouldn't try to fetch. */
194 #if !defined (CANNOT_FETCH_REGISTER)
195 #define CANNOT_FETCH_REGISTER(regno) 0
198 /* Fetch one register. */
201 fetch_register (regno
)
204 register unsigned int regaddr
;
205 char buf
[MAX_REGISTER_RAW_SIZE
];
206 char mess
[128]; /* For messages */
209 /* Offset of registers within the u area. */
212 if (CANNOT_FETCH_REGISTER (regno
))
214 bzero (buf
, REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
215 supply_register (regno
, buf
);
219 offset
= U_REGS_OFFSET
;
221 regaddr
= register_addr (regno
, offset
);
222 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
225 *(int *) &buf
[i
] = ptrace (PT_RUREGS
, inferior_pid
,
226 (PTRACE_ARG3_TYPE
) regaddr
, 0, 0);
227 regaddr
+= sizeof (int);
230 sprintf (mess
, "reading register %s (#%d)", reg_names
[regno
], regno
);
231 perror_with_name (mess
);
234 supply_register (regno
, buf
);
238 /* Fetch all registers, or just one, from the child process. */
241 fetch_inferior_registers (regno
)
245 for (regno
= 0; regno
< NUM_REGS
; regno
++)
246 fetch_register (regno
);
248 fetch_register (regno
);
251 /* Registers we shouldn't try to store. */
252 #if !defined (CANNOT_STORE_REGISTER)
253 #define CANNOT_STORE_REGISTER(regno) 0
256 /* Store our register values back into the inferior.
257 If REGNO is -1, do this for all registers.
258 Otherwise, REGNO specifies which register (so we can save time). */
261 store_inferior_registers (regno
)
264 register unsigned int regaddr
;
266 extern char registers
[];
269 unsigned int offset
= U_REGS_OFFSET
;
273 regaddr
= register_addr (regno
, offset
);
274 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof(int))
277 ptrace (PT_WUAREA
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
278 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
], 0);
281 sprintf (buf
, "writing register number %d(%d)", regno
, i
);
282 perror_with_name (buf
);
284 regaddr
+= sizeof(int);
289 for (regno
= 0; regno
< NUM_REGS
; regno
++)
291 if (CANNOT_STORE_REGISTER (regno
))
293 regaddr
= register_addr (regno
, offset
);
294 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof(int))
297 ptrace (PT_WUAREA
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
298 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
], 0);
301 sprintf (buf
, "writing register number %d(%d)", regno
, i
);
302 perror_with_name (buf
);
304 regaddr
+= sizeof(int);
310 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
312 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
313 in the NEW_SUN_PTRACE case.
314 It ought to be straightforward. But it appears that writing did
315 not write the data that I specified. I cannot understand where
316 it got the data that it actually did write. */
318 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
319 to debugger memory starting at MYADDR. Copy to inferior if
322 Returns the length copied, which is either the LEN argument or zero.
323 This xfer function does not do partial moves, since child_ops
324 doesn't allow memory operations to cross below us in the target stack
328 child_xfer_memory (memaddr
, myaddr
, len
, write
, target
)
333 struct target_ops
*target
; /* ignored */
336 /* Round starting address down to longword boundary. */
337 register CORE_ADDR addr
= memaddr
& - sizeof (int);
338 /* Round ending address up; get number of longwords that makes. */
340 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
341 /* Allocate buffer of that many longwords. */
342 register int *buffer
= (int *) alloca (count
* sizeof (int));
346 /* Fill start and end extra bytes of buffer with existing memory data. */
348 if (addr
!= memaddr
|| len
< (int)sizeof (int)) {
349 /* Need part of initial word -- fetch it. */
350 buffer
[0] = ptrace (PT_RIUSER
, inferior_pid
,
351 (PTRACE_ARG3_TYPE
) addr
, 0, 0);
354 if (count
> 1) /* FIXME, avoid if even boundary */
357 = ptrace (PT_RIUSER
, inferior_pid
,
358 (PTRACE_ARG3_TYPE
) (addr
+ (count
- 1) * sizeof (int)),
362 /* Copy data to be written over corresponding part of buffer */
364 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
366 /* Write the entire buffer. */
368 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
371 /* The HP-UX kernel crashes if you use PT_WDUSER to write into the text
372 segment. FIXME -- does it work to write into the data segment using
373 WIUSER, or do these idiots really expect us to figure out which segment
374 the address is in, so we can use a separate system call for it??! */
376 ptrace (PT_WDUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
,
381 /* Using the appropriate one (I or D) is necessary for
382 Gould NP1, at least. */
384 ptrace (PT_WIUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
,
393 /* Read all the longwords */
394 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
397 buffer
[i
] = ptrace (PT_RIUSER
, inferior_pid
,
398 (PTRACE_ARG3_TYPE
) addr
, 0, 0);
404 /* Copy appropriate bytes out of the buffer. */
405 bcopy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);