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 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 #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
62 #if !defined (FETCH_INFERIOR_REGISTERS)
63 #include <sys/user.h> /* Probably need to poke the user structure */
64 #if defined (KERNEL_U_ADDR_BSD)
65 #include <a.out.h> /* For struct nlist */
66 #endif /* KERNEL_U_ADDR_BSD. */
67 #endif /* !FETCH_INFERIOR_REGISTERS */
69 /* This function simply calls ptrace with the given arguments.
70 It exists so that all calls to ptrace are isolated in this
71 machine-dependent file. */
73 call_ptrace (request
, pid
, addr
, data
)
74 int request
, pid
, *addr
, data
;
76 return ptrace (request
, pid
, addr
, data
);
80 /* For the rest of the file, use an extra level of indirection */
81 /* This lets us breakpoint usefully on call_ptrace. */
82 #define ptrace call_ptrace
85 /* This is used when GDB is exiting. It gives less chance of error.*/
90 if (inferior_pid
== 0)
92 ptrace (PT_KILL
, inferior_pid
, 0, 0);
97 kill_inferior (args
, from_tty
)
101 kill_inferior_fast ();
102 target_mourn_inferior ();
105 /* Resume execution of the inferior process.
106 If STEP is nonzero, single-step it.
107 If SIGNAL is nonzero, give it that signal. */
110 child_resume (step
, signal
)
116 /* An address of (int *)1 tells ptrace to continue from where it was.
117 (If GDB wanted it to start some other way, we have already written
118 a new PC value to the child.) */
121 ptrace (PT_STEP
, inferior_pid
, (int *)1, signal
);
123 ptrace (PT_CONTINUE
, inferior_pid
, (int *)1, signal
);
126 perror_with_name ("ptrace");
130 /* Nonzero if we are debugging an attached process rather than
132 extern int attach_flag
;
134 /* Start debugging the process whose number is PID. */
140 ptrace (PT_ATTACH
, pid
, 0, 0);
142 perror_with_name ("ptrace");
147 /* Stop debugging the process whose number is PID
148 and continue it with signal number SIGNAL.
149 SIGNAL = 0 means just continue it. */
156 ptrace (PT_DETACH
, inferior_pid
, 1, signal
);
158 perror_with_name ("ptrace");
161 #endif /* ATTACH_DETACH */
163 #if !defined (FETCH_INFERIOR_REGISTERS)
165 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
166 to get the offset in the core file of the register values. */
167 #if defined (KERNEL_U_ADDR_BSD)
168 /* Get kernel_u_addr using BSD-style nlist(). */
169 CORE_ADDR kernel_u_addr
;
170 #include <a.out.h> /* For struct nlist */
173 _initialize_kernel_u_addr ()
175 struct nlist names
[2];
177 names
[0].n_un
.n_name
= "_u";
178 names
[1].n_un
.n_name
= NULL
;
179 if (nlist ("/vmunix", names
) == 0)
180 kernel_u_addr
= names
[0].n_value
;
182 fatal ("Unable to get kernel u area address.");
184 #endif /* KERNEL_U_ADDR_BSD. */
186 #if defined (KERNEL_U_ADDR_HPUX)
187 /* Get kernel_u_addr using HPUX-style nlist(). */
188 CORE_ADDR kernel_u_addr
;
193 unsigned char n_type
;
194 unsigned char n_length
;
198 static struct hpnlist nl
[] = {{ "_u", -1, }, { (char *) 0, }};
200 /* read the value of the u area from the hp-ux kernel */
201 void _initialize_kernel_u_addr ()
203 nlist ("/hp-ux", &nl
);
204 kernel_u_addr
= nl
[0].n_value
;
206 #endif /* KERNEL_U_ADDR_HPUX. */
208 #if !defined (offsetof)
209 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
212 /* U_REGS_OFFSET is the offset of the registers within the u area. */
213 #if !defined (U_REGS_OFFSET)
214 #define U_REGS_OFFSET \
215 ptrace (PT_READ_U, inferior_pid, \
216 (int *)(offsetof (struct user, u_ar0)), 0) - KERNEL_U_ADDR
219 /* Registers we shouldn't try to fetch. */
220 #if !defined (CANNOT_FETCH_REGISTER)
221 #define CANNOT_FETCH_REGISTER(regno) 0
224 /* Fetch one register. */
227 fetch_register (regno
)
230 register unsigned int regaddr
;
231 char buf
[MAX_REGISTER_RAW_SIZE
];
232 char mess
[128]; /* For messages */
235 /* Offset of registers within the u area. */
238 if (CANNOT_FETCH_REGISTER (regno
))
240 bzero (buf
, REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
241 supply_register (regno
, buf
);
245 offset
= U_REGS_OFFSET
;
247 regaddr
= register_addr (regno
, offset
);
248 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
251 *(int *) &buf
[i
] = ptrace (PT_READ_U
, inferior_pid
, (int *)regaddr
, 0);
252 regaddr
+= sizeof (int);
255 sprintf (mess
, "reading register %s (#%d)", reg_names
[regno
], regno
);
256 perror_with_name (mess
);
259 supply_register (regno
, buf
);
263 /* Fetch all registers, or just one, from the child process. */
266 fetch_inferior_registers (regno
)
270 for (regno
= 0; regno
< NUM_REGS
; regno
++)
271 fetch_register (regno
);
273 fetch_register (regno
);
276 /* Registers we shouldn't try to store. */
277 #if !defined (CANNOT_STORE_REGISTER)
278 #define CANNOT_STORE_REGISTER(regno) 0
281 /* Store our register values back into the inferior.
282 If REGNO is -1, do this for all registers.
283 Otherwise, REGNO specifies which register (so we can save time). */
286 store_inferior_registers (regno
)
289 register unsigned int regaddr
;
291 extern char registers
[];
295 unsigned int offset
= U_REGS_OFFSET
;
299 regaddr
= register_addr (regno
, offset
);
300 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof(int))
303 ptrace (PT_WRITE_U
, inferior_pid
, (int *)regaddr
,
304 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
]);
307 sprintf (buf
, "writing register number %d(%d)", regno
, i
);
308 perror_with_name (buf
);
311 regaddr
+= sizeof(int);
316 for (regno
= 0; regno
< NUM_REGS
; regno
++)
318 if (CANNOT_STORE_REGISTER (regno
))
320 regaddr
= register_addr (regno
, offset
);
321 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof(int))
324 ptrace (PT_WRITE_U
, inferior_pid
, (int *)regaddr
,
325 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
]);
328 sprintf (buf
, "writing register number %d(%d)", regno
, i
);
329 perror_with_name (buf
);
332 regaddr
+= sizeof(int);
338 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
340 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
341 in the NEW_SUN_PTRACE case.
342 It ought to be straightforward. But it appears that writing did
343 not write the data that I specified. I cannot understand where
344 it got the data that it actually did write. */
346 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
347 to debugger memory starting at MYADDR. Copy to inferior if
350 Returns the length copied, which is either the LEN argument or zero.
351 This xfer function does not do partial moves, since child_ops
352 doesn't allow memory operations to cross below us in the target stack
356 child_xfer_memory (memaddr
, myaddr
, len
, write
, target
)
361 struct target_ops target
; /* ignored */
364 /* Round starting address down to longword boundary. */
365 register CORE_ADDR addr
= memaddr
& - sizeof (int);
366 /* Round ending address up; get number of longwords that makes. */
368 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
369 /* Allocate buffer of that many longwords. */
370 register int *buffer
= (int *) alloca (count
* sizeof (int));
374 /* Fill start and end extra bytes of buffer with existing memory data. */
376 if (addr
!= memaddr
|| len
< (int)sizeof (int)) {
377 /* Need part of initial word -- fetch it. */
378 buffer
[0] = ptrace (PT_READ_I
, inferior_pid
, (int *)addr
, 0);
381 if (count
> 1) /* FIXME, avoid if even boundary */
384 = ptrace (PT_READ_I
, inferior_pid
,
385 (int *)(addr
+ (count
- 1) * sizeof (int)), 0);
388 /* Copy data to be written over corresponding part of buffer */
390 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
392 /* Write the entire buffer. */
394 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
397 ptrace (PT_WRITE_D
, inferior_pid
, (int *)addr
, buffer
[i
]);
400 /* Using the appropriate one (I or D) is necessary for
401 Gould NP1, at least. */
403 ptrace (PT_WRITE_I
, inferior_pid
, (int *)addr
, buffer
[i
]);
411 /* Read all the longwords */
412 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
415 buffer
[i
] = ptrace (PT_READ_I
, inferior_pid
, (int *)addr
, 0);
421 /* Copy appropriate bytes out of the buffer. */
422 bcopy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
This page took 0.09285 seconds and 5 git commands to generate.