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.
4 This file is part of GDB.
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)
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.
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. */
28 #include <sys/types.h>
31 #include <sys/param.h>
34 #include <sys/ioctl.h>
36 #include <sys/ptrace.h>
39 #if !defined (PT_KILL)
49 #endif /* No PT_KILL. */
52 #define PT_ATTACH PTRACE_ATTACH
55 #define PT_DETACH PTRACE_DETACH
59 #include <sys/user.h> /* After a.out.h */
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. */
67 call_ptrace (request
, pid
, addr
, data
)
68 int request
, pid
, *addr
, data
;
70 return ptrace (request
, pid
, addr
, data
);
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
79 /* This is used when GDB is exiting. It gives less chance of error.*/
84 if (inferior_pid
== 0)
86 ptrace (PT_KILL
, inferior_pid
, 0, 0);
91 kill_inferior (args
, from_tty
)
95 kill_inferior_fast ();
96 target_mourn_inferior ();
99 /* Resume execution of the inferior process.
100 If STEP is nonzero, single-step it.
101 If SIGNAL is nonzero, give it that signal. */
104 child_resume (step
, signal
)
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.) */
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. */
121 ptrace (PT_CONTINUE
, inferior_pid
, (int *)1, signal
);
123 perror_with_name ("ptrace");
127 /* Nonzero if we are debugging an attached process rather than
129 extern int attach_flag
;
131 /* Start debugging the process whose number is PID. */
137 ptrace (PT_ATTACH
, pid
, 0, 0);
139 perror_with_name ("ptrace");
144 /* Stop debugging the process whose number is PID
145 and continue it with signal number SIGNAL.
146 SIGNAL = 0 means just continue it. */
153 ptrace (PT_DETACH
, inferior_pid
, 1, signal
);
155 perror_with_name ("ptrace");
158 #endif /* ATTACH_DETACH */
160 #if !defined (FETCH_INFERIOR_REGISTERS)
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
;
169 _initialize_kernel_u_addr ()
171 struct nlist names
[2];
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
;
178 fatal ("Unable to get kernel u area address.");
180 #endif /* KERNEL_U_ADDR_BSD. */
182 #if defined (KERNEL_U_ADDR_HPUX)
183 /* Get kernel_u_addr using HPUX-style nlist(). */
184 CORE_ADDR kernel_u_addr
;
189 unsigned char n_type
;
190 unsigned char n_length
;
194 static struct hpnlist nl
[] = {{ "_u", -1, }, { (char *) 0, }};
196 /* read the value of the u area from the hp-ux kernel */
197 void _initialize_kernel_u_addr ()
200 nlist ("/hp-ux", &nl
);
201 kernel_u_addr
= nl
[0].n_value
;
203 #endif /* KERNEL_U_ADDR_HPUX. */
205 #if !defined (offsetof)
206 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
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
216 /* Fetch one register. */
218 fetch_register (regno
)
221 register unsigned int regaddr
;
222 char buf
[MAX_REGISTER_RAW_SIZE
];
225 /* Offset of registers within the u area. */
226 unsigned int offset
= U_REGS_OFFSET
;
228 regaddr
= register_addr (regno
, offset
);
229 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
231 *(int *) &buf
[i
] = ptrace (PT_READ_U
, inferior_pid
, (int *)regaddr
, 0);
232 regaddr
+= sizeof (int);
234 supply_register (regno
, buf
);
237 /* Fetch all registers, or just one, from the child process.
238 We should check for errors, but we don't. FIXME. */
241 fetch_inferior_registers (regno
)
245 for (regno
= 0; regno
< NUM_REGS
; regno
++)
246 fetch_register (regno
);
248 fetch_register (regno
);
252 /* Registers we shouldn't try to store. */
253 #if !defined (CANNOT_STORE_REGISTER)
254 #define CANNOT_STORE_REGISTER(regno) 0
257 /* Store our register values back into the inferior.
258 If REGNO is -1, do this for all registers.
259 Otherwise, REGNO specifies which register (so we can save time). */
262 store_inferior_registers (regno
)
265 register unsigned int regaddr
;
267 extern char registers
[];
271 unsigned int offset
= U_REGS_OFFSET
;
275 regaddr
= register_addr (regno
, offset
);
276 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof(int))
279 ptrace (PT_WRITE_U
, inferior_pid
, (int *)regaddr
,
280 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
]);
283 sprintf (buf
, "writing register number %d(%d)", regno
, i
);
284 perror_with_name (buf
);
287 regaddr
+= sizeof(int);
292 for (regno
= 0; regno
< NUM_REGS
; regno
++)
294 if (CANNOT_STORE_REGISTER (regno
))
296 regaddr
= register_addr (regno
, offset
);
297 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof(int))
300 ptrace (PT_WRITE_U
, inferior_pid
, (int *)regaddr
,
301 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
]);
304 sprintf (buf
, "writing register number %d(%d)", regno
, i
);
305 perror_with_name (buf
);
308 regaddr
+= sizeof(int);
314 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
316 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
317 in the NEW_SUN_PTRACE case.
318 It ought to be straightforward. But it appears that writing did
319 not write the data that I specified. I cannot understand where
320 it got the data that it actually did write. */
322 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
323 to debugger memory starting at MYADDR. Copy to inferior if
326 Returns the length copied, which is either the LEN argument or zero.
327 This xfer function does not do partial moves, since child_ops
328 doesn't allow memory operations to cross below us in the target stack
332 child_xfer_memory (memaddr
, myaddr
, len
, write
)
339 /* Round starting address down to longword boundary. */
340 register CORE_ADDR addr
= memaddr
& - sizeof (int);
341 /* Round ending address up; get number of longwords that makes. */
343 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
344 /* Allocate buffer of that many longwords. */
345 register int *buffer
= (int *) alloca (count
* sizeof (int));
349 /* Fill start and end extra bytes of buffer with existing memory data. */
351 if (addr
!= memaddr
|| len
< (int)sizeof (int)) {
352 /* Need part of initial word -- fetch it. */
353 buffer
[0] = ptrace (PT_READ_I
, inferior_pid
, (int *)addr
, 0);
356 if (count
> 1) /* FIXME, avoid if even boundary */
359 = ptrace (PT_READ_I
, inferior_pid
,
360 (int *)(addr
+ (count
- 1) * sizeof (int)), 0);
363 /* Copy data to be written over corresponding part of buffer */
365 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
367 /* Write the entire buffer. */
369 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
372 ptrace (PT_WRITE_D
, inferior_pid
, (int *)addr
, buffer
[i
]);
375 /* Using the appropriate one (I or D) is necessary for
376 Gould NP1, at least. */
378 ptrace (PT_WRITE_I
, inferior_pid
, (int *)addr
, buffer
[i
]);
386 /* Read all the longwords */
387 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
390 buffer
[i
] = ptrace (PT_READ_I
, inferior_pid
, (int *)addr
, 0);
396 /* Copy appropriate bytes out of the buffer. */
397 bcopy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
This page took 0.051729 seconds and 5 git commands to generate.