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