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. */
27 #include <sys/types.h>
30 #include <sys/param.h>
33 #include <sys/ioctl.h>
35 #include <sys/ptrace.h>
38 #if !defined (PT_KILL)
48 #endif /* No PT_KILL. */
51 #define PT_ATTACH PTRACE_ATTACH
54 #define PT_DETACH PTRACE_DETACH
63 #if !defined (FETCH_INFERIOR_REGISTERS)
64 #include <sys/user.h> /* Probably need to poke the user structure */
65 #if defined (KERNEL_U_ADDR_BSD)
66 #include <a.out.h> /* For struct nlist */
67 #endif /* KERNEL_U_ADDR_BSD. */
68 #endif /* !FETCH_INFERIOR_REGISTERS */
70 /* This function simply calls ptrace with the given arguments.
71 It exists so that all calls to ptrace are isolated in this
72 machine-dependent file. */
74 call_ptrace (request
, pid
, addr
, data
)
75 int request
, pid
, *addr
, data
;
77 return ptrace (request
, pid
, addr
, data
);
81 /* For the rest of the file, use an extra level of indirection */
82 /* This lets us breakpoint usefully on call_ptrace. */
83 #define ptrace call_ptrace
86 /* This is used when GDB is exiting. It gives less chance of error.*/
91 if (inferior_pid
== 0)
93 ptrace (PT_KILL
, inferior_pid
, 0, 0);
100 kill_inferior_fast ();
101 target_mourn_inferior ();
104 /* Resume execution of the inferior process.
105 If STEP is nonzero, single-step it.
106 If SIGNAL is nonzero, give it that signal. */
109 child_resume (step
, signal
)
115 /* An address of (int *)1 tells ptrace to continue from where it was.
116 (If GDB wanted it to start some other way, we have already written
117 a new PC value to the child.) */
120 ptrace (PT_STEP
, inferior_pid
, (int *)1, signal
);
122 ptrace (PT_CONTINUE
, inferior_pid
, (int *)1, signal
);
125 perror_with_name ("ptrace");
129 /* Nonzero if we are debugging an attached process rather than
131 extern int attach_flag
;
133 /* Start debugging the process whose number is PID. */
139 ptrace (PT_ATTACH
, pid
, 0, 0);
141 perror_with_name ("ptrace");
146 /* Stop debugging the process whose number is PID
147 and continue it with signal number SIGNAL.
148 SIGNAL = 0 means just continue it. */
155 ptrace (PT_DETACH
, inferior_pid
, 1, signal
);
157 perror_with_name ("ptrace");
160 #endif /* ATTACH_DETACH */
162 #if !defined (FETCH_INFERIOR_REGISTERS)
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
;
171 _initialize_kernel_u_addr ()
173 struct nlist names
[2];
175 names
[0].n_un
.n_name
= "_u";
176 names
[1].n_un
.n_name
= NULL
;
177 if (nlist ("/vmunix", names
) == 0)
178 kernel_u_addr
= names
[0].n_value
;
180 fatal ("Unable to get kernel u area address.");
182 #endif /* KERNEL_U_ADDR_BSD. */
184 #if defined (KERNEL_U_ADDR_HPUX)
185 /* Get kernel_u_addr using HPUX-style nlist(). */
186 CORE_ADDR kernel_u_addr
;
191 unsigned char n_type
;
192 unsigned char n_length
;
196 static struct hpnlist nl
[] = {{ "_u", -1, }, { (char *) 0, }};
198 /* read the value of the u area from the hp-ux kernel */
199 void _initialize_kernel_u_addr ()
201 nlist ("/hp-ux", &nl
);
202 kernel_u_addr
= nl
[0].n_value
;
204 #endif /* KERNEL_U_ADDR_HPUX. */
206 #if !defined (offsetof)
207 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
210 /* U_REGS_OFFSET is the offset of the registers within the u area. */
211 #if !defined (U_REGS_OFFSET)
212 #define U_REGS_OFFSET \
213 ptrace (PT_READ_U, inferior_pid, \
214 (int *)(offsetof (struct user, u_ar0)), 0) - KERNEL_U_ADDR
217 /* Registers we shouldn't try to fetch. */
218 #if !defined (CANNOT_FETCH_REGISTER)
219 #define CANNOT_FETCH_REGISTER(regno) 0
222 /* Fetch one register. */
225 fetch_register (regno
)
228 register unsigned int regaddr
;
229 char buf
[MAX_REGISTER_RAW_SIZE
];
230 char mess
[128]; /* For messages */
233 /* Offset of registers within the u area. */
236 if (CANNOT_FETCH_REGISTER (regno
))
238 bzero (buf
, REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
239 supply_register (regno
, buf
);
243 offset
= U_REGS_OFFSET
;
245 regaddr
= register_addr (regno
, offset
);
246 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
249 *(int *) &buf
[i
] = ptrace (PT_READ_U
, inferior_pid
, (int *)regaddr
, 0);
250 regaddr
+= sizeof (int);
253 sprintf (mess
, "reading register %s (#%d)", reg_names
[regno
], regno
);
254 perror_with_name (mess
);
257 supply_register (regno
, buf
);
261 /* Fetch all registers, or just one, from the child process. */
264 fetch_inferior_registers (regno
)
268 for (regno
= 0; regno
< NUM_REGS
; regno
++)
269 fetch_register (regno
);
271 fetch_register (regno
);
274 /* Registers we shouldn't try to store. */
275 #if !defined (CANNOT_STORE_REGISTER)
276 #define CANNOT_STORE_REGISTER(regno) 0
279 /* Store our register values back into the inferior.
280 If REGNO is -1, do this for all registers.
281 Otherwise, REGNO specifies which register (so we can save time). */
284 store_inferior_registers (regno
)
287 register unsigned int regaddr
;
289 extern char registers
[];
293 unsigned int offset
= U_REGS_OFFSET
;
297 regaddr
= register_addr (regno
, offset
);
298 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof(int))
301 ptrace (PT_WRITE_U
, inferior_pid
, (int *)regaddr
,
302 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
]);
305 sprintf (buf
, "writing register number %d(%d)", regno
, i
);
306 perror_with_name (buf
);
309 regaddr
+= sizeof(int);
314 for (regno
= 0; regno
< NUM_REGS
; regno
++)
316 if (CANNOT_STORE_REGISTER (regno
))
318 regaddr
= register_addr (regno
, offset
);
319 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof(int))
322 ptrace (PT_WRITE_U
, inferior_pid
, (int *)regaddr
,
323 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
]);
326 sprintf (buf
, "writing register number %d(%d)", regno
, i
);
327 perror_with_name (buf
);
330 regaddr
+= sizeof(int);
336 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
338 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
339 in the NEW_SUN_PTRACE case.
340 It ought to be straightforward. But it appears that writing did
341 not write the data that I specified. I cannot understand where
342 it got the data that it actually did write. */
344 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
345 to debugger memory starting at MYADDR. Copy to inferior if
348 Returns the length copied, which is either the LEN argument or zero.
349 This xfer function does not do partial moves, since child_ops
350 doesn't allow memory operations to cross below us in the target stack
354 child_xfer_memory (memaddr
, myaddr
, len
, write
, target
)
359 struct target_ops
*target
; /* ignored */
362 /* Round starting address down to longword boundary. */
363 register CORE_ADDR addr
= memaddr
& - sizeof (int);
364 /* Round ending address up; get number of longwords that makes. */
366 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
367 /* Allocate buffer of that many longwords. */
368 register int *buffer
= (int *) alloca (count
* sizeof (int));
372 /* Fill start and end extra bytes of buffer with existing memory data. */
374 if (addr
!= memaddr
|| len
< (int)sizeof (int)) {
375 /* Need part of initial word -- fetch it. */
376 buffer
[0] = ptrace (PT_READ_I
, inferior_pid
, (int *)addr
, 0);
379 if (count
> 1) /* FIXME, avoid if even boundary */
382 = ptrace (PT_READ_I
, inferior_pid
,
383 (int *)(addr
+ (count
- 1) * sizeof (int)), 0);
386 /* Copy data to be written over corresponding part of buffer */
388 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
390 /* Write the entire buffer. */
392 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
395 ptrace (PT_WRITE_D
, inferior_pid
, (int *)addr
, buffer
[i
]);
398 /* Using the appropriate one (I or D) is necessary for
399 Gould NP1, at least. */
401 ptrace (PT_WRITE_I
, inferior_pid
, (int *)addr
, buffer
[i
]);
409 /* Read all the longwords */
410 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
413 buffer
[i
] = ptrace (PT_READ_I
, inferior_pid
, (int *)addr
, 0);
419 /* Copy appropriate bytes out of the buffer. */
420 bcopy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
This page took 0.038287 seconds and 4 git commands to generate.