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