Commit | Line | Data |
---|---|---|
bd5635a1 RP |
1 | /* Low level Unix child interface to ptrace, for GDB when running under Unix. |
2 | Copyright (C) 1988, 1989, 1990, 1991 Free Software Foundation, Inc. | |
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" | |
bd5635a1 RP |
58 | #include <sys/file.h> |
59 | #include <sys/stat.h> | |
44ff4c96 JG |
60 | |
61 | #if !defined (FETCH_INFERIOR_REGISTERS) | |
62 | #include <sys/user.h> /* Probably need to poke the user structure */ | |
63 | #if defined (KERNEL_U_ADDR_BSD) | |
64 | #include <a.out.h> /* For struct nlist */ | |
65 | #endif /* KERNEL_U_ADDR_BSD. */ | |
66 | #endif /* !FETCH_INFERIOR_REGISTERS */ | |
bd5635a1 RP |
67 | \f |
68 | /* This function simply calls ptrace with the given arguments. | |
69 | It exists so that all calls to ptrace are isolated in this | |
70 | machine-dependent file. */ | |
71 | int | |
72 | call_ptrace (request, pid, addr, data) | |
73 | int request, pid, *addr, data; | |
74 | { | |
75 | return ptrace (request, pid, addr, data); | |
76 | } | |
77 | ||
78 | #ifdef DEBUG_PTRACE | |
79 | /* For the rest of the file, use an extra level of indirection */ | |
80 | /* This lets us breakpoint usefully on call_ptrace. */ | |
81 | #define ptrace call_ptrace | |
82 | #endif | |
83 | ||
84 | /* This is used when GDB is exiting. It gives less chance of error.*/ | |
85 | ||
86 | void | |
87 | kill_inferior_fast () | |
88 | { | |
89 | if (inferior_pid == 0) | |
90 | return; | |
91 | ptrace (PT_KILL, inferior_pid, 0, 0); | |
92 | wait ((int *)0); | |
93 | } | |
94 | ||
95 | void | |
96 | kill_inferior (args, from_tty) | |
97 | char *args; | |
98 | int from_tty; | |
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; | |
44ff4c96 | 169 | #include <a.out.h> /* For struct nlist */ |
bd5635a1 RP |
170 | |
171 | void | |
172 | _initialize_kernel_u_addr () | |
173 | { | |
174 | struct nlist names[2]; | |
175 | ||
176 | names[0].n_un.n_name = "_u"; | |
177 | names[1].n_un.n_name = NULL; | |
178 | if (nlist ("/vmunix", names) == 0) | |
179 | kernel_u_addr = names[0].n_value; | |
180 | else | |
181 | fatal ("Unable to get kernel u area address."); | |
182 | } | |
183 | #endif /* KERNEL_U_ADDR_BSD. */ | |
184 | ||
185 | #if defined (KERNEL_U_ADDR_HPUX) | |
186 | /* Get kernel_u_addr using HPUX-style nlist(). */ | |
187 | CORE_ADDR kernel_u_addr; | |
188 | ||
189 | struct hpnlist { | |
190 | char * n_name; | |
191 | long n_value; | |
192 | unsigned char n_type; | |
193 | unsigned char n_length; | |
194 | short n_almod; | |
195 | short n_unused; | |
196 | }; | |
197 | static struct hpnlist nl[] = {{ "_u", -1, }, { (char *) 0, }}; | |
198 | ||
199 | /* read the value of the u area from the hp-ux kernel */ | |
200 | void _initialize_kernel_u_addr () | |
201 | { | |
bd5635a1 RP |
202 | nlist ("/hp-ux", &nl); |
203 | kernel_u_addr = nl[0].n_value; | |
204 | } | |
205 | #endif /* KERNEL_U_ADDR_HPUX. */ | |
206 | ||
207 | #if !defined (offsetof) | |
208 | #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER) | |
209 | #endif | |
210 | ||
211 | /* U_REGS_OFFSET is the offset of the registers within the u area. */ | |
212 | #if !defined (U_REGS_OFFSET) | |
213 | #define U_REGS_OFFSET \ | |
214 | ptrace (PT_READ_U, inferior_pid, \ | |
215 | (int *)(offsetof (struct user, u_ar0)), 0) - KERNEL_U_ADDR | |
216 | #endif | |
217 | ||
44ff4c96 JG |
218 | /* Registers we shouldn't try to fetch. */ |
219 | #if !defined (CANNOT_FETCH_REGISTER) | |
220 | #define CANNOT_FETCH_REGISTER(regno) 0 | |
221 | #endif | |
222 | ||
bd5635a1 | 223 | /* Fetch one register. */ |
44ff4c96 | 224 | |
bd5635a1 RP |
225 | static void |
226 | fetch_register (regno) | |
227 | int regno; | |
228 | { | |
229 | register unsigned int regaddr; | |
230 | char buf[MAX_REGISTER_RAW_SIZE]; | |
44ff4c96 | 231 | char mess[128]; /* For messages */ |
bd5635a1 RP |
232 | register int i; |
233 | ||
234 | /* Offset of registers within the u area. */ | |
44ff4c96 JG |
235 | unsigned int offset; |
236 | ||
237 | if (CANNOT_FETCH_REGISTER (regno)) | |
238 | { | |
239 | bzero (buf, REGISTER_RAW_SIZE (regno)); /* Supply zeroes */ | |
240 | supply_register (regno, buf); | |
241 | return; | |
242 | } | |
243 | ||
244 | offset = U_REGS_OFFSET; | |
bd5635a1 RP |
245 | |
246 | regaddr = register_addr (regno, offset); | |
247 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int)) | |
248 | { | |
44ff4c96 | 249 | errno = 0; |
bd5635a1 RP |
250 | *(int *) &buf[i] = ptrace (PT_READ_U, inferior_pid, (int *)regaddr, 0); |
251 | regaddr += sizeof (int); | |
44ff4c96 JG |
252 | if (errno != 0) |
253 | { | |
254 | sprintf (mess, "reading register %s (#%d)", reg_names[regno], regno); | |
255 | perror_with_name (mess); | |
256 | } | |
bd5635a1 RP |
257 | } |
258 | supply_register (regno, buf); | |
259 | } | |
260 | ||
44ff4c96 | 261 | |
5594d534 | 262 | /* Fetch all registers, or just one, from the child process. */ |
bd5635a1 | 263 | |
5594d534 | 264 | void |
bd5635a1 RP |
265 | fetch_inferior_registers (regno) |
266 | int regno; | |
267 | { | |
268 | if (regno == -1) | |
269 | for (regno = 0; regno < NUM_REGS; regno++) | |
270 | fetch_register (regno); | |
271 | else | |
272 | fetch_register (regno); | |
bd5635a1 RP |
273 | } |
274 | ||
275 | /* Registers we shouldn't try to store. */ | |
276 | #if !defined (CANNOT_STORE_REGISTER) | |
277 | #define CANNOT_STORE_REGISTER(regno) 0 | |
278 | #endif | |
279 | ||
280 | /* Store our register values back into the inferior. | |
281 | If REGNO is -1, do this for all registers. | |
282 | Otherwise, REGNO specifies which register (so we can save time). */ | |
283 | ||
284 | int | |
285 | store_inferior_registers (regno) | |
286 | int regno; | |
287 | { | |
288 | register unsigned int regaddr; | |
289 | char buf[80]; | |
290 | extern char registers[]; | |
291 | register int i; | |
292 | int result = 0; | |
293 | ||
294 | unsigned int offset = U_REGS_OFFSET; | |
295 | ||
296 | if (regno >= 0) | |
297 | { | |
298 | regaddr = register_addr (regno, offset); | |
299 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int)) | |
300 | { | |
301 | errno = 0; | |
302 | ptrace (PT_WRITE_U, inferior_pid, (int *)regaddr, | |
303 | *(int *) ®isters[REGISTER_BYTE (regno) + i]); | |
304 | if (errno != 0) | |
305 | { | |
306 | sprintf (buf, "writing register number %d(%d)", regno, i); | |
307 | perror_with_name (buf); | |
308 | result = -1; | |
309 | } | |
310 | regaddr += sizeof(int); | |
311 | } | |
312 | } | |
313 | else | |
314 | { | |
315 | for (regno = 0; regno < NUM_REGS; regno++) | |
316 | { | |
317 | if (CANNOT_STORE_REGISTER (regno)) | |
318 | continue; | |
319 | regaddr = register_addr (regno, offset); | |
320 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int)) | |
321 | { | |
322 | errno = 0; | |
323 | ptrace (PT_WRITE_U, inferior_pid, (int *)regaddr, | |
324 | *(int *) ®isters[REGISTER_BYTE (regno) + i]); | |
325 | if (errno != 0) | |
326 | { | |
327 | sprintf (buf, "writing register number %d(%d)", regno, i); | |
328 | perror_with_name (buf); | |
329 | result = -1; | |
330 | } | |
331 | regaddr += sizeof(int); | |
332 | } | |
333 | } | |
334 | } | |
335 | return result; | |
336 | } | |
337 | #endif /* !defined (FETCH_INFERIOR_REGISTERS). */ | |
338 | \f | |
339 | /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory | |
340 | in the NEW_SUN_PTRACE case. | |
341 | It ought to be straightforward. But it appears that writing did | |
342 | not write the data that I specified. I cannot understand where | |
343 | it got the data that it actually did write. */ | |
344 | ||
345 | /* Copy LEN bytes to or from inferior's memory starting at MEMADDR | |
346 | to debugger memory starting at MYADDR. Copy to inferior if | |
347 | WRITE is nonzero. | |
348 | ||
349 | Returns the length copied, which is either the LEN argument or zero. | |
350 | This xfer function does not do partial moves, since child_ops | |
351 | doesn't allow memory operations to cross below us in the target stack | |
352 | anyway. */ | |
353 | ||
354 | int | |
b6de2014 | 355 | child_xfer_memory (memaddr, myaddr, len, write, target) |
bd5635a1 RP |
356 | CORE_ADDR memaddr; |
357 | char *myaddr; | |
358 | int len; | |
359 | int write; | |
b6de2014 | 360 | struct target_ops target; /* ignored */ |
bd5635a1 RP |
361 | { |
362 | register int i; | |
363 | /* Round starting address down to longword boundary. */ | |
364 | register CORE_ADDR addr = memaddr & - sizeof (int); | |
365 | /* Round ending address up; get number of longwords that makes. */ | |
366 | register int count | |
367 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
368 | /* Allocate buffer of that many longwords. */ | |
369 | register int *buffer = (int *) alloca (count * sizeof (int)); | |
370 | ||
371 | if (write) | |
372 | { | |
373 | /* Fill start and end extra bytes of buffer with existing memory data. */ | |
374 | ||
375 | if (addr != memaddr || len < (int)sizeof (int)) { | |
376 | /* Need part of initial word -- fetch it. */ | |
377 | buffer[0] = ptrace (PT_READ_I, inferior_pid, (int *)addr, 0); | |
378 | } | |
379 | ||
380 | if (count > 1) /* FIXME, avoid if even boundary */ | |
381 | { | |
382 | buffer[count - 1] | |
383 | = ptrace (PT_READ_I, inferior_pid, | |
384 | (int *)(addr + (count - 1) * sizeof (int)), 0); | |
385 | } | |
386 | ||
387 | /* Copy data to be written over corresponding part of buffer */ | |
388 | ||
389 | bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len); | |
390 | ||
391 | /* Write the entire buffer. */ | |
392 | ||
393 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
394 | { | |
395 | errno = 0; | |
396 | ptrace (PT_WRITE_D, inferior_pid, (int *)addr, buffer[i]); | |
397 | if (errno) | |
398 | { | |
399 | /* Using the appropriate one (I or D) is necessary for | |
400 | Gould NP1, at least. */ | |
401 | errno = 0; | |
402 | ptrace (PT_WRITE_I, inferior_pid, (int *)addr, buffer[i]); | |
403 | } | |
404 | if (errno) | |
405 | return 0; | |
406 | } | |
407 | } | |
408 | else | |
409 | { | |
410 | /* Read all the longwords */ | |
411 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
412 | { | |
413 | errno = 0; | |
414 | buffer[i] = ptrace (PT_READ_I, inferior_pid, (int *)addr, 0); | |
415 | if (errno) | |
416 | return 0; | |
417 | QUIT; | |
418 | } | |
419 | ||
420 | /* Copy appropriate bytes out of the buffer. */ | |
421 | bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len); | |
422 | } | |
423 | return len; | |
424 | } |