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.
4 This file is part of GDB.
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.
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.
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. */
28 #include <sys/types.h>
31 #include <sys/param.h>
34 #include <sys/ioctl.h>
36 #ifdef PTRACE_IN_WRONG_PLACE
39 #include <sys/ptrace.h>
42 #if !defined (PT_KILL)
52 #endif /* No PT_KILL. */
55 #define PT_ATTACH PTRACE_ATTACH
58 #define PT_DETACH PTRACE_DETACH
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 */
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. */
79 call_ptrace (request
, pid
, addr
, data
)
81 PTRACE_ARG3_TYPE addr
;
84 return ptrace (request
, pid
, addr
, data
);
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
96 if (inferior_pid
== 0)
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);
103 target_mourn_inferior ();
106 /* Resume execution of the inferior process.
107 If STEP is nonzero, single-step it.
108 If SIGNAL is nonzero, give it that signal. */
111 child_resume (step
, signal
)
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.)
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. */
127 ptrace (PT_STEP
, inferior_pid
, (PTRACE_ARG3_TYPE
) 1, signal
);
129 ptrace (PT_CONTINUE
, inferior_pid
, (PTRACE_ARG3_TYPE
) 1, signal
);
132 perror_with_name ("ptrace");
136 /* Nonzero if we are debugging an attached process rather than
138 extern int attach_flag
;
140 /* Start debugging the process whose number is PID. */
146 ptrace (PT_ATTACH
, pid
, (PTRACE_ARG3_TYPE
) 0, 0);
148 perror_with_name ("ptrace");
153 /* Stop debugging the process whose number is PID
154 and continue it with signal number SIGNAL.
155 SIGNAL = 0 means just continue it. */
162 ptrace (PT_DETACH
, inferior_pid
, (PTRACE_ARG3_TYPE
) 1, signal
);
164 perror_with_name ("ptrace");
167 #endif /* ATTACH_DETACH */
169 #if !defined (FETCH_INFERIOR_REGISTERS)
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
;
178 _initialize_kernel_u_addr ()
180 struct nlist names
[2];
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
;
187 fatal ("Unable to get kernel u area address.");
189 #endif /* KERNEL_U_ADDR_BSD. */
191 #if defined (KERNEL_U_ADDR_HPUX)
192 /* Get kernel_u_addr using HPUX-style nlist(). */
193 CORE_ADDR kernel_u_addr
;
198 unsigned char n_type
;
199 unsigned char n_length
;
203 static struct hpnlist nl
[] = {{ "_u", -1, }, { (char *) 0, }};
205 /* read the value of the u area from the hp-ux kernel */
206 void _initialize_kernel_u_addr ()
208 nlist ("/hp-ux", &nl
);
209 kernel_u_addr
= nl
[0].n_value
;
211 #endif /* KERNEL_U_ADDR_HPUX. */
213 #if !defined (offsetof)
214 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
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) \
225 /* Registers we shouldn't try to fetch. */
226 #if !defined (CANNOT_FETCH_REGISTER)
227 #define CANNOT_FETCH_REGISTER(regno) 0
230 /* Fetch one register. */
233 fetch_register (regno
)
236 register unsigned int regaddr
;
237 char buf
[MAX_REGISTER_RAW_SIZE
];
238 char mess
[128]; /* For messages */
241 /* Offset of registers within the u area. */
244 if (CANNOT_FETCH_REGISTER (regno
))
246 bzero (buf
, REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
247 supply_register (regno
, buf
);
251 offset
= U_REGS_OFFSET
;
253 regaddr
= register_addr (regno
, offset
);
254 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
257 *(int *) &buf
[i
] = ptrace (PT_READ_U
, inferior_pid
,
258 (PTRACE_ARG3_TYPE
) regaddr
, 0);
259 regaddr
+= sizeof (int);
262 sprintf (mess
, "reading register %s (#%d)", reg_names
[regno
], regno
);
263 perror_with_name (mess
);
266 supply_register (regno
, buf
);
270 /* Fetch all registers, or just one, from the child process. */
273 fetch_inferior_registers (regno
)
277 for (regno
= 0; regno
< NUM_REGS
; regno
++)
278 fetch_register (regno
);
280 fetch_register (regno
);
283 /* Registers we shouldn't try to store. */
284 #if !defined (CANNOT_STORE_REGISTER)
285 #define CANNOT_STORE_REGISTER(regno) 0
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). */
293 store_inferior_registers (regno
)
296 register unsigned int regaddr
;
298 extern char registers
[];
301 unsigned int offset
= U_REGS_OFFSET
;
305 regaddr
= register_addr (regno
, offset
);
306 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof(int))
309 ptrace (PT_WRITE_U
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
310 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
]);
313 sprintf (buf
, "writing register number %d(%d)", regno
, i
);
314 perror_with_name (buf
);
316 regaddr
+= sizeof(int);
321 for (regno
= 0; regno
< NUM_REGS
; regno
++)
323 if (CANNOT_STORE_REGISTER (regno
))
325 regaddr
= register_addr (regno
, offset
);
326 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof(int))
329 ptrace (PT_WRITE_U
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
330 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
]);
333 sprintf (buf
, "writing register number %d(%d)", regno
, i
);
334 perror_with_name (buf
);
336 regaddr
+= sizeof(int);
341 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
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. */
349 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
350 to debugger memory starting at MYADDR. Copy to inferior if
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
359 child_xfer_memory (memaddr
, myaddr
, len
, write
, target
)
364 struct target_ops
*target
; /* ignored */
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. */
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));
377 /* Fill start and end extra bytes of buffer with existing memory data. */
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
,
385 if (count
> 1) /* FIXME, avoid if even boundary */
388 = ptrace (PT_READ_I
, inferior_pid
,
389 (PTRACE_ARG3_TYPE
) (addr
+ (count
- 1) * sizeof (int)),
393 /* Copy data to be written over corresponding part of buffer */
395 memcpy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
397 /* Write the entire buffer. */
399 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
402 ptrace (PT_WRITE_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
,
406 /* Using the appropriate one (I or D) is necessary for
407 Gould NP1, at least. */
409 ptrace (PT_WRITE_I
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
,
418 /* Read all the longwords */
419 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
422 buffer
[i
] = ptrace (PT_READ_I
, inferior_pid
,
423 (PTRACE_ARG3_TYPE
) addr
, 0);
429 /* Copy appropriate bytes out of the buffer. */
430 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
This page took 0.038594 seconds and 4 git commands to generate.