Reindented to GNU standard. No semantic changes. This checkin is to
[deliverable/binutils-gdb.git] / gdb / infptrace.c
1 /* Low level Unix child interface to ptrace, for GDB when running under Unix.
2 Copyright 1988, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "target.h"
24
25 #include "nm.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
36 #ifdef PTRACE_IN_WRONG_PLACE
37 #include <ptrace.h>
38 #else
39 #include <sys/ptrace.h>
40 #endif
41
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
49 #define PT_READ_D 2
50 #define PT_WRITE_I 4
51 #define PT_WRITE_D 5
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"
62 #ifndef NO_SYS_FILE
63 #include <sys/file.h>
64 #endif
65 #include <sys/stat.h>
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 */
73
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)
80 int request, pid;
81 PTRACE_ARG3_TYPE addr;
82 int data;
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
93 void
94 kill_inferior ()
95 {
96 if (inferior_pid == 0)
97 return;
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);
101 ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
102 wait ((int *)0);
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;
116
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
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. */
125
126 if (step)
127 ptrace (PT_STEP, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
128 else
129 ptrace (PT_CONTINUE, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
130
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;
146 ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
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;
162 ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
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 {
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, \
221 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
222 - KERNEL_U_ADDR
223 #endif
224
225 /* Registers we shouldn't try to fetch. */
226 #if !defined (CANNOT_FETCH_REGISTER)
227 #define CANNOT_FETCH_REGISTER(regno) 0
228 #endif
229
230 /* Fetch one register. */
231
232 static void
233 fetch_register (regno)
234 int regno;
235 {
236 register unsigned int regaddr;
237 char buf[MAX_REGISTER_RAW_SIZE];
238 char mess[128]; /* For messages */
239 register int i;
240
241 /* Offset of registers within the u area. */
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;
252
253 regaddr = register_addr (regno, offset);
254 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
255 {
256 errno = 0;
257 *(int *) &buf[i] = ptrace (PT_READ_U, inferior_pid,
258 (PTRACE_ARG3_TYPE) regaddr, 0);
259 regaddr += sizeof (int);
260 if (errno != 0)
261 {
262 sprintf (mess, "reading register %s (#%d)", reg_names[regno], regno);
263 perror_with_name (mess);
264 }
265 }
266 supply_register (regno, buf);
267 }
268
269
270 /* Fetch all registers, or just one, from the child process. */
271
272 void
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);
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
292 void
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;
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;
309 ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
310 *(int *) &registers[REGISTER_BYTE (regno) + i]);
311 if (errno != 0)
312 {
313 sprintf (buf, "writing register number %d(%d)", regno, i);
314 perror_with_name (buf);
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;
329 ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
330 *(int *) &registers[REGISTER_BYTE (regno) + i]);
331 if (errno != 0)
332 {
333 sprintf (buf, "writing register number %d(%d)", regno, i);
334 perror_with_name (buf);
335 }
336 regaddr += sizeof(int);
337 }
338 }
339 }
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
359 child_xfer_memory (memaddr, myaddr, len, write, target)
360 CORE_ADDR memaddr;
361 char *myaddr;
362 int len;
363 int write;
364 struct target_ops *target; /* ignored */
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. */
381 buffer[0] = ptrace (PT_READ_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
382 0);
383 }
384
385 if (count > 1) /* FIXME, avoid if even boundary */
386 {
387 buffer[count - 1]
388 = ptrace (PT_READ_I, inferior_pid,
389 (PTRACE_ARG3_TYPE) (addr + (count - 1) * sizeof (int)),
390 0);
391 }
392
393 /* Copy data to be written over corresponding part of buffer */
394
395 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
396
397 /* Write the entire buffer. */
398
399 for (i = 0; i < count; i++, addr += sizeof (int))
400 {
401 errno = 0;
402 ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr,
403 buffer[i]);
404 if (errno)
405 {
406 /* Using the appropriate one (I or D) is necessary for
407 Gould NP1, at least. */
408 errno = 0;
409 ptrace (PT_WRITE_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
410 buffer[i]);
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;
422 buffer[i] = ptrace (PT_READ_I, inferior_pid,
423 (PTRACE_ARG3_TYPE) addr, 0);
424 if (errno)
425 return 0;
426 QUIT;
427 }
428
429 /* Copy appropriate bytes out of the buffer. */
430 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
431 }
432 return len;
433 }
This page took 0.038594 seconds and 4 git commands to generate.