Commit | Line | Data |
---|---|---|
7da1e27d SG |
1 | /* Machine-dependent code which would otherwise be in infptrace.c, |
2 | for GDB, the GNU debugger. This code is for the HP PA-RISC cpu. | |
5140562f | 3 | Copyright 1986, 1987, 1989, 1990, 1991, 1992 Free Software Foundation, Inc. |
7da1e27d SG |
4 | |
5 | Contributed by the Center for Software Science at the | |
6 | University of Utah (pa-gdb-bugs@cs.utah.edu). | |
7 | ||
8 | /* Low level Unix child interface to ptrace, for GDB when running under Unix. | |
9 | Copyright (C) 1988, 1989, 1990, 1991 Free Software Foundation, Inc. | |
10 | ||
11 | This file is part of GDB. | |
12 | ||
13 | This program is free software; you can redistribute it and/or modify | |
14 | it under the terms of the GNU General Public License as published by | |
15 | the Free Software Foundation; either version 2 of the License, or | |
16 | (at your option) any later version. | |
17 | ||
18 | This program is distributed in the hope that it will be useful, | |
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | GNU General Public License for more details. | |
22 | ||
23 | You should have received a copy of the GNU General Public License | |
24 | along with this program; if not, write to the Free Software | |
25 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
26 | ||
7da1e27d SG |
27 | #include "defs.h" |
28 | #include "frame.h" | |
29 | #include "inferior.h" | |
30 | #include "target.h" | |
31 | ||
32 | #ifdef USG | |
33 | #include <sys/types.h> | |
34 | #endif | |
35 | ||
36 | #include <sys/param.h> | |
37 | #include <sys/dir.h> | |
38 | #include <signal.h> | |
39 | #include <sys/ioctl.h> | |
40 | #ifndef USG | |
41 | #include <sys/ptrace.h> | |
42 | #endif | |
43 | ||
44 | ||
45 | #ifndef PT_ATTACH | |
46 | #define PT_ATTACH PTRACE_ATTACH | |
47 | #endif | |
48 | #ifndef PT_DETACH | |
49 | #define PT_DETACH PTRACE_DETACH | |
50 | #endif | |
51 | ||
52 | #include "gdbcore.h" | |
53 | #include <sys/user.h> /* After a.out.h */ | |
54 | #include <sys/file.h> | |
55 | #include <sys/stat.h> | |
56 | \f | |
57 | /* This function simply calls ptrace with the given arguments. | |
58 | It exists so that all calls to ptrace are isolated in this | |
59 | machine-dependent file. */ | |
60 | int | |
61 | call_ptrace (request, pid, addr, data) | |
e676a15f FF |
62 | int request, pid; |
63 | PTRACE_ARG3_TYPE addr; | |
64 | int data; | |
7da1e27d SG |
65 | { |
66 | return ptrace (request, pid, addr, data); | |
67 | } | |
68 | ||
69 | #ifdef DEBUG_PTRACE | |
70 | /* For the rest of the file, use an extra level of indirection */ | |
71 | /* This lets us breakpoint usefully on call_ptrace. */ | |
72 | #define ptrace call_ptrace | |
73 | #endif | |
74 | ||
7da1e27d | 75 | void |
b28d3617 | 76 | kill_inferior () |
7da1e27d SG |
77 | { |
78 | if (inferior_pid == 0) | |
79 | return; | |
e676a15f | 80 | ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0); |
7da1e27d | 81 | wait ((int *)0); |
7da1e27d SG |
82 | target_mourn_inferior (); |
83 | } | |
84 | ||
85 | /* Resume execution of the inferior process. | |
86 | If STEP is nonzero, single-step it. | |
87 | If SIGNAL is nonzero, give it that signal. */ | |
88 | ||
89 | void | |
90 | child_resume (step, signal) | |
91 | int step; | |
92 | int signal; | |
93 | { | |
94 | errno = 0; | |
95 | ||
e676a15f FF |
96 | /* An address of (PTRACE_ARG3_TYPE) 1 tells ptrace to continue from where |
97 | it was. (If GDB wanted it to start some other way, we have already | |
98 | written a new PC value to the child.) */ | |
7da1e27d SG |
99 | |
100 | if (step) | |
e676a15f | 101 | ptrace (PT_STEP, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal); |
7da1e27d | 102 | else |
e676a15f | 103 | ptrace (PT_CONTINUE, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal); |
7da1e27d SG |
104 | |
105 | if (errno) | |
106 | perror_with_name ("ptrace"); | |
107 | } | |
108 | \f | |
109 | #ifdef ATTACH_DETACH | |
110 | /* Nonzero if we are debugging an attached process rather than | |
111 | an inferior. */ | |
112 | extern int attach_flag; | |
113 | ||
114 | /* Start debugging the process whose number is PID. */ | |
115 | int | |
116 | attach (pid) | |
117 | int pid; | |
118 | { | |
119 | errno = 0; | |
e676a15f | 120 | ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0); |
7da1e27d SG |
121 | if (errno) |
122 | perror_with_name ("ptrace"); | |
123 | attach_flag = 1; | |
124 | return pid; | |
125 | } | |
126 | ||
127 | /* Stop debugging the process whose number is PID | |
128 | and continue it with signal number SIGNAL. | |
129 | SIGNAL = 0 means just continue it. */ | |
130 | ||
131 | void | |
132 | detach (signal) | |
133 | int signal; | |
134 | { | |
135 | errno = 0; | |
e676a15f | 136 | ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal); |
7da1e27d SG |
137 | if (errno) |
138 | perror_with_name ("ptrace"); | |
139 | attach_flag = 0; | |
140 | } | |
141 | #endif /* ATTACH_DETACH */ | |
142 | \f | |
143 | #if !defined (FETCH_INFERIOR_REGISTERS) | |
144 | ||
145 | /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0 | |
146 | to get the offset in the core file of the register values. */ | |
147 | #if defined (KERNEL_U_ADDR_BSD) | |
148 | /* Get kernel_u_addr using BSD-style nlist(). */ | |
149 | CORE_ADDR kernel_u_addr; | |
150 | ||
151 | #include <a.out.gnu.h> /* For struct nlist */ | |
152 | ||
153 | void | |
154 | _initialize_kernel_u_addr () | |
155 | { | |
156 | struct nlist names[2]; | |
157 | ||
158 | names[0].n_un.n_name = "_u"; | |
159 | names[1].n_un.n_name = NULL; | |
160 | if (nlist ("/vmunix", names) == 0) | |
161 | kernel_u_addr = names[0].n_value; | |
162 | else | |
163 | fatal ("Unable to get kernel u area address."); | |
164 | } | |
165 | #endif /* KERNEL_U_ADDR_BSD. */ | |
166 | ||
167 | #if defined (KERNEL_U_ADDR_HPUX) | |
168 | /* Get kernel_u_addr using HPUX-style nlist(). */ | |
169 | CORE_ADDR kernel_u_addr; | |
170 | ||
171 | struct hpnlist { | |
172 | char * n_name; | |
173 | long n_value; | |
174 | unsigned char n_type; | |
175 | unsigned char n_length; | |
176 | short n_almod; | |
177 | short n_unused; | |
178 | }; | |
179 | static struct hpnlist nl[] = {{ "_u", -1, }, { (char *) 0, }}; | |
180 | ||
181 | /* read the value of the u area from the hp-ux kernel */ | |
182 | void _initialize_kernel_u_addr () | |
183 | { | |
184 | struct user u; | |
185 | nlist ("/hp-ux", &nl); | |
186 | kernel_u_addr = nl[0].n_value; | |
187 | } | |
188 | #endif /* KERNEL_U_ADDR_HPUX. */ | |
189 | ||
190 | #if !defined (offsetof) | |
191 | #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER) | |
192 | #endif | |
193 | ||
194 | /* U_REGS_OFFSET is the offset of the registers within the u area. */ | |
195 | #if !defined (U_REGS_OFFSET) | |
196 | #define U_REGS_OFFSET \ | |
197 | ptrace (PT_READ_U, inferior_pid, \ | |
e676a15f FF |
198 | (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \ |
199 | - KERNEL_U_ADDR | |
7da1e27d SG |
200 | #endif |
201 | ||
202 | /* Registers we shouldn't try to fetch. */ | |
203 | #if !defined (CANNOT_FETCH_REGISTER) | |
204 | #define CANNOT_FETCH_REGISTER(regno) 0 | |
205 | #endif | |
206 | ||
207 | /* Fetch one register. */ | |
208 | ||
209 | static void | |
210 | fetch_register (regno) | |
211 | int regno; | |
212 | { | |
213 | register unsigned int regaddr; | |
214 | char buf[MAX_REGISTER_RAW_SIZE]; | |
215 | char mess[128]; /* For messages */ | |
216 | register int i; | |
217 | ||
218 | /* Offset of registers within the u area. */ | |
219 | unsigned int offset; | |
220 | ||
221 | if (CANNOT_FETCH_REGISTER (regno)) | |
222 | { | |
223 | bzero (buf, REGISTER_RAW_SIZE (regno)); /* Supply zeroes */ | |
224 | supply_register (regno, buf); | |
225 | return; | |
226 | } | |
227 | ||
228 | offset = U_REGS_OFFSET; | |
229 | ||
230 | regaddr = register_addr (regno, offset); | |
231 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int)) | |
232 | { | |
233 | errno = 0; | |
e676a15f FF |
234 | *(int *) &buf[i] = ptrace (PT_RUREGS, inferior_pid, |
235 | (PTRACE_ARG3_TYPE) regaddr, 0); | |
7da1e27d SG |
236 | regaddr += sizeof (int); |
237 | if (errno != 0) | |
238 | { | |
239 | sprintf (mess, "reading register %s (#%d)", reg_names[regno], regno); | |
240 | perror_with_name (mess); | |
241 | } | |
242 | } | |
243 | if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM) | |
244 | buf[3] &= ~0x3; | |
245 | supply_register (regno, buf); | |
246 | } | |
247 | ||
248 | ||
249 | /* Fetch all registers, or just one, from the child process. */ | |
250 | ||
251 | void | |
252 | fetch_inferior_registers (regno) | |
253 | int regno; | |
254 | { | |
255 | if (regno == -1) | |
256 | for (regno = 0; regno < NUM_REGS; regno++) | |
257 | fetch_register (regno); | |
258 | else | |
259 | fetch_register (regno); | |
260 | } | |
261 | ||
262 | /* Registers we shouldn't try to store. */ | |
263 | #if !defined (CANNOT_STORE_REGISTER) | |
264 | #define CANNOT_STORE_REGISTER(regno) 0 | |
265 | #endif | |
266 | ||
267 | /* Store our register values back into the inferior. | |
268 | If REGNO is -1, do this for all registers. | |
269 | Otherwise, REGNO specifies which register (so we can save time). */ | |
270 | ||
271 | void | |
272 | store_inferior_registers (regno) | |
273 | int regno; | |
274 | { | |
275 | register unsigned int regaddr; | |
276 | char buf[80]; | |
277 | extern char registers[]; | |
278 | register int i; | |
279 | ||
280 | unsigned int offset = U_REGS_OFFSET; | |
281 | ||
282 | if (regno >= 0) | |
283 | { | |
284 | regaddr = register_addr (regno, offset); | |
285 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int)) | |
286 | { | |
287 | errno = 0; | |
e676a15f | 288 | ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, |
7da1e27d SG |
289 | *(int *) ®isters[REGISTER_BYTE (regno) + i]); |
290 | if (errno != 0) | |
291 | { | |
292 | sprintf (buf, "writing register number %d(%d)", regno, i); | |
293 | perror_with_name (buf); | |
294 | } | |
295 | regaddr += sizeof(int); | |
296 | } | |
297 | } | |
298 | else | |
299 | { | |
300 | for (regno = 0; regno < NUM_REGS; regno++) | |
301 | { | |
302 | if (CANNOT_STORE_REGISTER (regno)) | |
303 | continue; | |
304 | regaddr = register_addr (regno, offset); | |
305 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int)) | |
306 | { | |
307 | errno = 0; | |
e676a15f | 308 | ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, |
7da1e27d SG |
309 | *(int *) ®isters[REGISTER_BYTE (regno) + i]); |
310 | if (errno != 0) | |
311 | { | |
312 | sprintf (buf, "writing register number %d(%d)", regno, i); | |
313 | perror_with_name (buf); | |
314 | } | |
315 | regaddr += sizeof(int); | |
316 | } | |
317 | } | |
318 | } | |
319 | return; | |
320 | } | |
321 | #endif /* !defined (FETCH_INFERIOR_REGISTERS). */ | |
322 | \f | |
323 | /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory | |
324 | in the NEW_SUN_PTRACE case. | |
325 | It ought to be straightforward. But it appears that writing did | |
326 | not write the data that I specified. I cannot understand where | |
327 | it got the data that it actually did write. */ | |
328 | ||
329 | /* Copy LEN bytes to or from inferior's memory starting at MEMADDR | |
330 | to debugger memory starting at MYADDR. Copy to inferior if | |
331 | WRITE is nonzero. | |
332 | ||
333 | Returns the length copied, which is either the LEN argument or zero. | |
334 | This xfer function does not do partial moves, since child_ops | |
335 | doesn't allow memory operations to cross below us in the target stack | |
336 | anyway. */ | |
337 | ||
338 | int | |
339 | child_xfer_memory (memaddr, myaddr, len, write, target) | |
340 | CORE_ADDR memaddr; | |
341 | char *myaddr; | |
342 | int len; | |
343 | int write; | |
344 | struct target_ops *target; /* ignored */ | |
345 | { | |
346 | register int i; | |
347 | /* Round starting address down to longword boundary. */ | |
348 | register CORE_ADDR addr = memaddr & - sizeof (int); | |
349 | /* Round ending address up; get number of longwords that makes. */ | |
350 | register int count | |
351 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
352 | /* Allocate buffer of that many longwords. */ | |
353 | register int *buffer = (int *) alloca (count * sizeof (int)); | |
354 | ||
355 | if (write) | |
356 | { | |
357 | /* Fill start and end extra bytes of buffer with existing memory data. */ | |
358 | ||
359 | if (addr != memaddr || len < (int)sizeof (int)) { | |
360 | /* Need part of initial word -- fetch it. */ | |
e676a15f FF |
361 | buffer[0] = ptrace (PT_READ_I, inferior_pid, (PTRACE_ARG3_TYPE) addr, |
362 | 0); | |
7da1e27d SG |
363 | } |
364 | ||
365 | if (count > 1) /* FIXME, avoid if even boundary */ | |
366 | { | |
367 | buffer[count - 1] | |
368 | = ptrace (PT_READ_I, inferior_pid, | |
e676a15f FF |
369 | (PTRACE_ARG3_TYPE) (addr + (count - 1) * sizeof (int)), |
370 | 0); | |
7da1e27d SG |
371 | } |
372 | ||
373 | /* Copy data to be written over corresponding part of buffer */ | |
374 | ||
375 | bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len); | |
376 | ||
377 | /* Write the entire buffer. */ | |
378 | ||
379 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
380 | { | |
381 | errno = 0; | |
e676a15f FF |
382 | ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr, |
383 | buffer[i]); | |
7da1e27d SG |
384 | if (errno) |
385 | { | |
386 | /* Using the appropriate one (I or D) is necessary for | |
387 | Gould NP1, at least. */ | |
388 | errno = 0; | |
e676a15f FF |
389 | ptrace (PT_WRITE_I, inferior_pid, (PTRACE_ARG3_TYPE) addr, |
390 | buffer[i]); | |
7da1e27d SG |
391 | } |
392 | if (errno) | |
393 | return 0; | |
394 | } | |
395 | } | |
396 | else | |
397 | { | |
398 | /* Read all the longwords */ | |
399 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
400 | { | |
401 | errno = 0; | |
e676a15f FF |
402 | buffer[i] = ptrace (PT_READ_I, inferior_pid, |
403 | (PTRACE_ARG3_TYPE) addr, 0); | |
7da1e27d SG |
404 | if (errno) |
405 | return 0; | |
406 | QUIT; | |
407 | } | |
408 | ||
409 | /* Copy appropriate bytes out of the buffer. */ | |
410 | bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len); | |
411 | } | |
412 | return len; | |
413 | } |