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