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. */
30 #define PT_ATTACH PTRACE_ATTACH
33 #define PT_DETACH PTRACE_DETACH
36 /* This function simply calls ptrace with the given arguments.
37 It exists so that all calls to ptrace are isolated in this
38 machine-dependent file. */
39 #ifdef WANT_NATIVE_TARGET
41 call_ptrace (request
, pid
, addr
, data
)
43 PTRACE_ARG3_TYPE addr
;
46 return ptrace (request
, pid
, addr
, data
, 0);
48 #endif /* WANT_NATIVE_TARGET */
51 /* For the rest of the file, use an extra level of indirection */
52 /* This lets us breakpoint usefully on call_ptrace. */
53 #define ptrace call_ptrace
59 if (inferior_pid
== 0)
61 ptrace (PT_EXIT
, inferior_pid
, (PTRACE_ARG3_TYPE
) 0, 0, 0); /* PT_EXIT = PT_KILL ? */
63 target_mourn_inferior ();
67 /* Nonzero if we are debugging an attached process rather than
69 extern int attach_flag
;
71 /* Start debugging the process whose number is PID. */
77 ptrace (PT_ATTACH
, pid
, (PTRACE_ARG3_TYPE
) 0, 0, 0);
79 perror_with_name ("ptrace");
84 /* Stop debugging the process whose number is PID
85 and continue it with signal number SIGNAL.
86 SIGNAL = 0 means just continue it. */
93 ptrace (PT_DETACH
, inferior_pid
, (PTRACE_ARG3_TYPE
) 1, signal
, 0);
95 perror_with_name ("ptrace");
98 #endif /* ATTACH_DETACH */
100 /* Fetch all registers, or just one, from the child process. */
103 fetch_inferior_registers (regno
)
107 for (regno
= 0; regno
< NUM_REGS
; regno
++)
108 fetch_register (regno
);
110 fetch_register (regno
);
113 /* Registers we shouldn't try to store. */
114 #if !defined (CANNOT_STORE_REGISTER)
115 #define CANNOT_STORE_REGISTER(regno) 0
118 /* Store our register values back into the inferior.
119 If REGNO is -1, do this for all registers.
120 Otherwise, REGNO specifies which register (so we can save time). */
123 store_inferior_registers (regno
)
126 register unsigned int regaddr
;
128 extern char registers
[];
131 unsigned int offset
= U_REGS_OFFSET
;
135 regaddr
= register_addr (regno
, offset
);
136 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof(int))
139 ptrace (PT_WUAREA
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
140 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
], 0);
143 sprintf (buf
, "writing register number %d(%d)", regno
, i
);
144 perror_with_name (buf
);
146 regaddr
+= sizeof(int);
151 for (regno
= 0; regno
< NUM_REGS
; regno
++)
153 if (CANNOT_STORE_REGISTER (regno
))
155 regaddr
= register_addr (regno
, offset
);
156 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof(int))
159 ptrace (PT_WUAREA
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
160 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
], 0);
163 sprintf (buf
, "writing register number %d(%d)", regno
, i
);
164 perror_with_name (buf
);
166 regaddr
+= sizeof(int);
173 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
174 to get the offset in the core file of the register values. */
175 #if defined (KERNEL_U_ADDR_BSD)
176 /* Get kernel_u_addr using BSD-style nlist(). */
177 CORE_ADDR kernel_u_addr
;
179 #include <a.out.gnu.h> /* For struct nlist */
182 _initialize_kernel_u_addr ()
184 struct nlist names
[2];
186 names
[0].n_un
.n_name
= "_u";
187 names
[1].n_un
.n_name
= NULL
;
188 if (nlist ("/vmunix", names
) == 0)
189 kernel_u_addr
= names
[0].n_value
;
191 fatal ("Unable to get kernel u area address.");
193 #endif /* KERNEL_U_ADDR_BSD. */
195 #if defined (KERNEL_U_ADDR_HPUX)
196 /* Get kernel_u_addr using HPUX-style nlist(). */
197 CORE_ADDR kernel_u_addr
;
202 unsigned char n_type
;
203 unsigned char n_length
;
207 static struct hpnlist nl
[] = {{ "_u", -1, }, { (char *) 0, }};
209 /* read the value of the u area from the hp-ux kernel */
210 void _initialize_kernel_u_addr ()
213 nlist ("/hp-ux", &nl
);
214 kernel_u_addr
= nl
[0].n_value
;
216 #endif /* KERNEL_U_ADDR_HPUX. */
218 #if !defined (offsetof)
219 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
222 /* U_REGS_OFFSET is the offset of the registers within the u area. */
223 #if !defined (U_REGS_OFFSET)
224 #define U_REGS_OFFSET \
225 ptrace (PT_READ_U, inferior_pid, \
226 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0, 0) \
230 /* Registers we shouldn't try to fetch. */
231 #if !defined (CANNOT_FETCH_REGISTER)
232 #define CANNOT_FETCH_REGISTER(regno) 0
235 /* Fetch one register. */
238 fetch_register (regno
)
241 register unsigned int regaddr
;
242 char buf
[MAX_REGISTER_RAW_SIZE
];
243 char mess
[128]; /* For messages */
246 /* Offset of registers within the u area. */
249 if (CANNOT_FETCH_REGISTER (regno
))
251 bzero (buf
, REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
252 supply_register (regno
, buf
);
256 offset
= U_REGS_OFFSET
;
258 regaddr
= register_addr (regno
, offset
);
259 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
262 *(int *) &buf
[i
] = ptrace (PT_RUREGS
, inferior_pid
,
263 (PTRACE_ARG3_TYPE
) regaddr
, 0, 0);
264 regaddr
+= sizeof (int);
267 sprintf (mess
, "reading register %s (#%d)", reg_names
[regno
], regno
);
268 perror_with_name (mess
);
271 supply_register (regno
, buf
);
276 /* Resume execution of the inferior process.
277 If STEP is nonzero, single-step it.
278 If SIGNAL is nonzero, give it that signal. */
281 child_resume (step
, signal
)
287 /* An address of (PTRACE_ARG3_TYPE) 1 tells ptrace to continue from where
288 it was. (If GDB wanted it to start some other way, we have already
289 written a new PC value to the child.) */
292 ptrace (PT_SINGLE
, inferior_pid
, (PTRACE_ARG3_TYPE
) 1, signal
, 0);
294 ptrace (PT_CONTIN
, inferior_pid
, (PTRACE_ARG3_TYPE
) 1, signal
, 0);
297 perror_with_name ("ptrace");
300 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
301 in the NEW_SUN_PTRACE case.
302 It ought to be straightforward. But it appears that writing did
303 not write the data that I specified. I cannot understand where
304 it got the data that it actually did write. */
306 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
307 to debugger memory starting at MYADDR. Copy to inferior if
310 Returns the length copied, which is either the LEN argument or zero.
311 This xfer function does not do partial moves, since child_ops
312 doesn't allow memory operations to cross below us in the target stack
316 child_xfer_memory (memaddr
, myaddr
, len
, write
, target
)
321 struct target_ops
*target
; /* ignored */
324 /* Round starting address down to longword boundary. */
325 register CORE_ADDR addr
= memaddr
& - sizeof (int);
326 /* Round ending address up; get number of longwords that makes. */
328 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
329 /* Allocate buffer of that many longwords. */
330 register int *buffer
= (int *) alloca (count
* sizeof (int));
334 /* Fill start and end extra bytes of buffer with existing memory data. */
336 if (addr
!= memaddr
|| len
< (int)sizeof (int)) {
337 /* Need part of initial word -- fetch it. */
338 buffer
[0] = ptrace (PT_RIUSER
, inferior_pid
,
339 (PTRACE_ARG3_TYPE
) addr
, 0, 0);
342 if (count
> 1) /* FIXME, avoid if even boundary */
345 = ptrace (PT_RIUSER
, inferior_pid
,
346 (PTRACE_ARG3_TYPE
) (addr
+ (count
- 1) * sizeof (int)),
350 /* Copy data to be written over corresponding part of buffer */
352 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
354 /* Write the entire buffer. */
356 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
359 /* The HP-UX kernel crashes if you use PT_WDUSER to write into the text
360 segment. FIXME -- does it work to write into the data segment using
361 WIUSER, or do these idiots really expect us to figure out which segment
362 the address is in, so we can use a separate system call for it??! */
364 ptrace (PT_WDUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
,
369 /* Using the appropriate one (I or D) is necessary for
370 Gould NP1, at least. */
372 ptrace (PT_WIUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
,
381 /* Read all the longwords */
382 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
385 buffer
[i
] = ptrace (PT_RIUSER
, inferior_pid
,
386 (PTRACE_ARG3_TYPE
) addr
, 0, 0);
392 /* Copy appropriate bytes out of the buffer. */
393 bcopy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);