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