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