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