Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Low level Unix child interface to ptrace, for GDB when running under Unix. |
0a65a603 | 2 | Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, |
2689df5a | 3 | 1998, 1999, 2000, 2001, 2002, 2004 |
c906108c SS |
4 | Free Software Foundation, Inc. |
5 | ||
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
c906108c | 12 | |
c5aa993b JM |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c906108c | 17 | |
c5aa993b JM |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
22 | |
23 | #include "defs.h" | |
8cbba7c8 | 24 | #include "command.h" |
c906108c | 25 | #include "frame.h" |
8cbba7c8 | 26 | #include "gdbcore.h" |
c906108c | 27 | #include "inferior.h" |
4e052eda | 28 | #include "regcache.h" |
8cbba7c8 | 29 | #include "target.h" |
ed9a39eb | 30 | |
8cbba7c8 | 31 | #include "gdb_assert.h" |
03f2053f | 32 | #include "gdb_wait.h" |
8cbba7c8 | 33 | #include "gdb_string.h" |
c906108c | 34 | |
c906108c | 35 | #include <sys/param.h> |
4b14d3e4 | 36 | #include "gdb_dirent.h" |
c906108c SS |
37 | #include <signal.h> |
38 | #include <sys/ioctl.h> | |
39 | ||
11003ae3 | 40 | #include "gdb_ptrace.h" |
c906108c | 41 | |
c0ccb908 | 42 | #ifdef HAVE_SYS_FILE_H |
c906108c SS |
43 | #include <sys/file.h> |
44 | #endif | |
652fc137 | 45 | |
c906108c SS |
46 | #if !defined (FETCH_INFERIOR_REGISTERS) |
47 | #include <sys/user.h> /* Probably need to poke the user structure */ | |
c906108c SS |
48 | #endif /* !FETCH_INFERIOR_REGISTERS */ |
49 | ||
50 | #if !defined (CHILD_XFER_MEMORY) | |
a14ed312 | 51 | static void udot_info (char *, int); |
c906108c SS |
52 | #endif |
53 | ||
a14ed312 | 54 | void _initialize_infptrace (void); |
c906108c | 55 | \f |
c5aa993b | 56 | |
c906108c SS |
57 | /* This function simply calls ptrace with the given arguments. |
58 | It exists so that all calls to ptrace are isolated in this | |
59 | machine-dependent file. */ | |
60 | int | |
f8707cac | 61 | call_ptrace (int request, int pid, PTRACE_ARG3_TYPE addr, int data) |
c906108c SS |
62 | { |
63 | int pt_status = 0; | |
64 | ||
65 | #if 0 | |
66 | int saved_errno; | |
67 | ||
68 | printf ("call_ptrace(request=%d, pid=%d, addr=0x%x, data=0x%x)", | |
69 | request, pid, addr, data); | |
70 | #endif | |
71 | #if defined(PT_SETTRC) | |
72 | /* If the parent can be told to attach to us, try to do it. */ | |
c5aa993b JM |
73 | if (request == PT_SETTRC) |
74 | { | |
75 | errno = 0; | |
daa98270 | 76 | #ifndef PTRACE_TYPE_ARG5 |
ed9a39eb JM |
77 | pt_status = ptrace (PT_SETTRC, pid, addr, data); |
78 | #else | |
c5aa993b JM |
79 | /* Deal with HPUX 8.0 braindamage. We never use the |
80 | calls which require the fifth argument. */ | |
ed9a39eb | 81 | pt_status = ptrace (PT_SETTRC, pid, addr, data, 0); |
c906108c | 82 | #endif |
c5aa993b JM |
83 | if (errno) |
84 | perror_with_name ("ptrace"); | |
c906108c | 85 | #if 0 |
c5aa993b | 86 | printf (" = %d\n", pt_status); |
c906108c | 87 | #endif |
c5aa993b JM |
88 | if (pt_status < 0) |
89 | return pt_status; | |
90 | else | |
91 | return parent_attach_all (pid, addr, data); | |
92 | } | |
c906108c SS |
93 | #endif |
94 | ||
95 | #if defined(PT_CONTIN1) | |
96 | /* On HPUX, PT_CONTIN1 is a form of continue that preserves pending | |
97 | signals. If it's available, use it. */ | |
98 | if (request == PT_CONTINUE) | |
99 | request = PT_CONTIN1; | |
100 | #endif | |
101 | ||
102 | #if defined(PT_SINGLE1) | |
103 | /* On HPUX, PT_SINGLE1 is a form of step that preserves pending | |
104 | signals. If it's available, use it. */ | |
105 | if (request == PT_STEP) | |
106 | request = PT_SINGLE1; | |
107 | #endif | |
108 | ||
109 | #if 0 | |
110 | saved_errno = errno; | |
111 | errno = 0; | |
112 | #endif | |
daa98270 | 113 | #ifndef PTRACE_TYPE_ARG5 |
ed9a39eb JM |
114 | pt_status = ptrace (request, pid, addr, data); |
115 | #else | |
c5aa993b JM |
116 | /* Deal with HPUX 8.0 braindamage. We never use the |
117 | calls which require the fifth argument. */ | |
ed9a39eb | 118 | pt_status = ptrace (request, pid, addr, data, 0); |
c906108c | 119 | #endif |
ed9a39eb | 120 | |
c906108c SS |
121 | #if 0 |
122 | if (errno) | |
123 | printf (" [errno = %d]", errno); | |
124 | ||
125 | errno = saved_errno; | |
126 | printf (" = 0x%x\n", pt_status); | |
127 | #endif | |
128 | return pt_status; | |
129 | } | |
130 | ||
131 | ||
daa98270 | 132 | #if defined (DEBUG_PTRACE) || defined (PTRACE_TYPE_ARG5) |
c906108c SS |
133 | /* For the rest of the file, use an extra level of indirection */ |
134 | /* This lets us breakpoint usefully on call_ptrace. */ | |
135 | #define ptrace call_ptrace | |
136 | #endif | |
137 | ||
138 | /* Wait for a process to finish, possibly running a target-specific | |
139 | hook before returning. */ | |
140 | ||
ee21b650 AC |
141 | /* NOTE: cagney: 2004-09-29: Dependant on the native configuration, |
142 | "hppah-nat.c" may either call this or infttrace.c's implementation | |
143 | of ptrace_wait. See "hppahpux.mh". */ | |
144 | ||
c906108c | 145 | int |
39f77062 | 146 | ptrace_wait (ptid_t ptid, int *status) |
c906108c SS |
147 | { |
148 | int wstate; | |
149 | ||
150 | wstate = wait (status); | |
c906108c SS |
151 | return wstate; |
152 | } | |
153 | ||
adbef1f0 AC |
154 | #ifndef DEPRECATED_KILL_INFERIOR |
155 | /* NOTE: cagney/2004-09-12: Instead of definining this macro, code | |
156 | should call inf_ptrace_target to get a basic ptrace target and then | |
157 | locally update any necessary methods. See ppcnbsd-nat.c. */ | |
158 | ||
c906108c | 159 | void |
fba45db2 | 160 | kill_inferior (void) |
c906108c SS |
161 | { |
162 | int status; | |
39f77062 | 163 | int pid = PIDGET (inferior_ptid); |
c906108c | 164 | |
39f77062 | 165 | if (pid == 0) |
c906108c SS |
166 | return; |
167 | ||
168 | /* This once used to call "kill" to kill the inferior just in case | |
169 | the inferior was still running. As others have noted in the past | |
170 | (kingdon) there shouldn't be any way to get here if the inferior | |
171 | is still running -- else there's a major problem elsewere in gdb | |
172 | and it needs to be fixed. | |
173 | ||
174 | The kill call causes problems under hpux10, so it's been removed; | |
175 | if this causes problems we'll deal with them as they arise. */ | |
655c5466 | 176 | ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3) 0, 0); |
d3e05d0d | 177 | wait (&status); |
c906108c SS |
178 | target_mourn_inferior (); |
179 | } | |
adbef1f0 | 180 | #endif /* DEPRECATED_KILL_INFERIOR */ |
c906108c | 181 | |
adbef1f0 AC |
182 | #ifndef DEPRECATED_CHILD_RESUME |
183 | /* NOTE: cagney/2004-09-12: Instead of definining this macro, code | |
184 | should call inf_ptrace_target to get a basic ptrace target and then | |
185 | locally update any necessary methods. See ppcnbsd-nat.c. */ | |
c906108c SS |
186 | |
187 | /* Resume execution of the inferior process. | |
188 | If STEP is nonzero, single-step it. | |
189 | If SIGNAL is nonzero, give it that signal. */ | |
190 | ||
191 | void | |
39f77062 | 192 | child_resume (ptid_t ptid, int step, enum target_signal signal) |
c906108c | 193 | { |
8cbba7c8 | 194 | int request = PT_CONTINUE; |
39f77062 KB |
195 | int pid = PIDGET (ptid); |
196 | ||
c906108c SS |
197 | if (pid == -1) |
198 | /* Resume all threads. */ | |
199 | /* I think this only gets used in the non-threaded case, where "resume | |
39f77062 KB |
200 | all threads" and "resume inferior_ptid" are the same. */ |
201 | pid = PIDGET (inferior_ptid); | |
c906108c | 202 | |
c906108c SS |
203 | if (step) |
204 | { | |
8cbba7c8 MK |
205 | /* If this system does not support PT_STEP, a higher level |
206 | function will have called single_step() to transmute the step | |
207 | request into a continue request (by setting breakpoints on | |
208 | all possible successor instructions), so we don't have to | |
209 | worry about that here. */ | |
210 | ||
211 | gdb_assert (!SOFTWARE_SINGLE_STEP_P ()); | |
212 | request = PT_STEP; | |
c906108c | 213 | } |
c906108c | 214 | |
8cbba7c8 MK |
215 | /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from |
216 | where it was. If GDB wanted it to start some other way, we have | |
217 | already written a new PC value to the child. */ | |
218 | ||
219 | errno = 0; | |
220 | ptrace (request, pid, (PTRACE_TYPE_ARG3)1, target_signal_to_host (signal)); | |
221 | if (errno != 0) | |
222 | perror_with_name ("ptrace"); | |
c906108c | 223 | } |
adbef1f0 | 224 | #endif /* DEPRECATED_CHILD_RESUME */ |
c906108c | 225 | \f |
8cbba7c8 | 226 | |
c906108c | 227 | /* Start debugging the process whose number is PID. */ |
8cbba7c8 | 228 | |
c906108c | 229 | int |
fba45db2 | 230 | attach (int pid) |
c906108c | 231 | { |
d966f0cb | 232 | #ifdef PT_ATTACH |
11003ae3 | 233 | errno = 0; |
655c5466 | 234 | ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3) 0, 0); |
8cbba7c8 | 235 | if (errno != 0) |
c906108c SS |
236 | perror_with_name ("ptrace"); |
237 | attach_flag = 1; | |
238 | return pid; | |
d966f0cb AC |
239 | #else |
240 | error ("This system does not support attaching to a process"); | |
241 | #endif | |
c906108c SS |
242 | } |
243 | ||
8cbba7c8 MK |
244 | /* Stop debugging the process whose number is PID and continue it with |
245 | signal number SIGNAL. SIGNAL = 0 means just continue it. */ | |
c906108c SS |
246 | |
247 | void | |
fba45db2 | 248 | detach (int signal) |
c906108c | 249 | { |
d966f0cb | 250 | #ifdef PT_DETACH |
8cbba7c8 MK |
251 | int pid = PIDGET (inferior_ptid); |
252 | ||
11003ae3 | 253 | errno = 0; |
8cbba7c8 MK |
254 | ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3) 1, signal); |
255 | if (errno != 0) | |
256 | perror_with_name ("ptrace"); | |
c906108c | 257 | attach_flag = 0; |
d966f0cb AC |
258 | #else |
259 | error ("This system does not support detaching from a process"); | |
260 | #endif | |
c906108c | 261 | } |
c906108c | 262 | \f |
c906108c | 263 | |
652fc137 | 264 | #ifndef FETCH_INFERIOR_REGISTERS |
c906108c | 265 | |
652fc137 MK |
266 | /* U_REGS_OFFSET is the offset of the registers within the u area. */ |
267 | #ifndef U_REGS_OFFSET | |
268 | ||
269 | #ifndef offsetof | |
c906108c SS |
270 | #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER) |
271 | #endif | |
272 | ||
c906108c | 273 | #define U_REGS_OFFSET \ |
39f77062 | 274 | ptrace (PT_READ_U, PIDGET (inferior_ptid), \ |
655c5466 | 275 | (PTRACE_TYPE_ARG3) (offsetof (struct user, u_ar0)), 0) \ |
c906108c SS |
276 | - KERNEL_U_ADDR |
277 | #endif | |
278 | ||
652fc137 | 279 | /* Fetch register REGNUM from the inferior. */ |
c906108c SS |
280 | |
281 | static void | |
652fc137 | 282 | fetch_register (int regnum) |
c906108c | 283 | { |
652fc137 MK |
284 | CORE_ADDR addr; |
285 | size_t size; | |
286 | PTRACE_TYPE_RET *buf; | |
287 | int tid, i; | |
288 | ||
289 | if (CANNOT_FETCH_REGISTER (regnum)) | |
c906108c | 290 | { |
652fc137 | 291 | regcache_raw_supply (current_regcache, regnum, NULL); |
c906108c SS |
292 | return; |
293 | } | |
294 | ||
652fc137 MK |
295 | /* GNU/Linux LWP ID's are process ID's. */ |
296 | tid = TIDGET (inferior_ptid); | |
297 | if (tid == 0) | |
298 | tid = PIDGET (inferior_ptid); /* Not a threaded program. */ | |
299 | ||
300 | /* This isn't really an address. But ptrace thinks of it as one. */ | |
301 | addr = register_addr (regnum, U_REGS_OFFSET); | |
302 | size = register_size (current_gdbarch, regnum); | |
ed9a39eb | 303 | |
652fc137 MK |
304 | gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0); |
305 | buf = alloca (size); | |
c906108c | 306 | |
652fc137 MK |
307 | /* Read the register contents from the inferior a chuck at the time. */ |
308 | for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++) | |
c906108c SS |
309 | { |
310 | errno = 0; | |
652fc137 | 311 | buf[i] = ptrace (PT_READ_U, tid, (PTRACE_TYPE_ARG3) addr, 0); |
c906108c | 312 | if (errno != 0) |
652fc137 MK |
313 | error ("Couldn't read register %s (#%d): %s.", REGISTER_NAME (regnum), |
314 | regnum, safe_strerror (errno)); | |
315 | ||
316 | addr += sizeof (PTRACE_TYPE_RET); | |
c906108c | 317 | } |
652fc137 | 318 | regcache_raw_supply (current_regcache, regnum, buf); |
c906108c SS |
319 | } |
320 | ||
652fc137 MK |
321 | /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this |
322 | for all registers. */ | |
c906108c SS |
323 | |
324 | void | |
652fc137 | 325 | fetch_inferior_registers (int regnum) |
c906108c | 326 | { |
652fc137 MK |
327 | if (regnum == -1) |
328 | for (regnum = 0; regnum < NUM_REGS; regnum++) | |
329 | fetch_register (regnum); | |
c906108c | 330 | else |
652fc137 | 331 | fetch_register (regnum); |
c906108c SS |
332 | } |
333 | ||
652fc137 | 334 | /* Store register REGNUM into the inferior. */ |
c906108c SS |
335 | |
336 | static void | |
652fc137 | 337 | store_register (int regnum) |
c906108c | 338 | { |
652fc137 MK |
339 | CORE_ADDR addr; |
340 | size_t size; | |
341 | PTRACE_TYPE_RET *buf; | |
342 | int tid, i; | |
c906108c | 343 | |
652fc137 MK |
344 | if (CANNOT_STORE_REGISTER (regnum)) |
345 | return; | |
ed9a39eb | 346 | |
652fc137 MK |
347 | /* GNU/Linux LWP ID's are process ID's. */ |
348 | tid = TIDGET (inferior_ptid); | |
349 | if (tid == 0) | |
350 | tid = PIDGET (inferior_ptid); /* Not a threaded program. */ | |
c906108c | 351 | |
652fc137 MK |
352 | /* This isn't really an address. But ptrace thinks of it as one. */ |
353 | addr = register_addr (regnum, U_REGS_OFFSET); | |
354 | size = register_size (current_gdbarch, regnum); | |
8b6f1f3a | 355 | |
652fc137 MK |
356 | gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0); |
357 | buf = alloca (size); | |
8b6f1f3a | 358 | |
652fc137 MK |
359 | /* Write the register contents into the inferior a chunk at the time. */ |
360 | regcache_raw_collect (current_regcache, regnum, buf); | |
361 | for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++) | |
c906108c SS |
362 | { |
363 | errno = 0; | |
652fc137 | 364 | ptrace (PT_WRITE_U, tid, (PTRACE_TYPE_ARG3) addr, buf[i]); |
c906108c | 365 | if (errno != 0) |
652fc137 MK |
366 | error ("Couldn't write register %s (#%d): %s.", REGISTER_NAME (regnum), |
367 | regnum, safe_strerror (errno)); | |
368 | ||
369 | addr += sizeof (PTRACE_TYPE_RET); | |
c906108c SS |
370 | } |
371 | } | |
372 | ||
652fc137 MK |
373 | /* Store register REGNUM back into the inferior. If REGNUM is -1, do |
374 | this for all registers (including the floating point registers). */ | |
c906108c SS |
375 | |
376 | void | |
652fc137 | 377 | store_inferior_registers (int regnum) |
c906108c | 378 | { |
652fc137 MK |
379 | if (regnum == -1) |
380 | for (regnum = 0; regnum < NUM_REGS; regnum++) | |
381 | store_register (regnum); | |
c906108c | 382 | else |
652fc137 | 383 | store_register (regnum); |
c906108c | 384 | } |
652fc137 MK |
385 | |
386 | #endif /* not FETCH_INFERIOR_REGISTERS. */ | |
c906108c SS |
387 | \f |
388 | ||
94cd915f MS |
389 | /* Set an upper limit on alloca. */ |
390 | #ifndef GDB_MAX_ALLOCA | |
391 | #define GDB_MAX_ALLOCA 0x1000 | |
392 | #endif | |
393 | ||
c906108c SS |
394 | #if !defined (CHILD_XFER_MEMORY) |
395 | /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory | |
3c2fb7bd MK |
396 | in the NEW_SUN_PTRACE case. It ought to be straightforward. But |
397 | it appears that writing did not write the data that I specified. I | |
398 | cannot understand where it got the data that it actually did write. */ | |
c906108c | 399 | |
3c2fb7bd MK |
400 | /* Copy LEN bytes to or from inferior's memory starting at MEMADDR to |
401 | debugger memory starting at MYADDR. Copy to inferior if WRITE is | |
402 | nonzero. TARGET is ignored. | |
c5aa993b | 403 | |
3c2fb7bd MK |
404 | Returns the length copied, which is either the LEN argument or |
405 | zero. This xfer function does not do partial moves, since | |
1df84f13 AC |
406 | deprecated_child_ops doesn't allow memory operations to cross below |
407 | us in the target stack anyway. */ | |
c906108c SS |
408 | |
409 | int | |
73186089 | 410 | child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write, |
0a65a603 | 411 | struct mem_attrib *attrib, struct target_ops *target) |
c906108c | 412 | { |
3c2fb7bd | 413 | int i; |
c906108c | 414 | /* Round starting address down to longword boundary. */ |
88800403 | 415 | CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_TYPE_RET); |
c906108c | 416 | /* Round ending address up; get number of longwords that makes. */ |
88800403 MK |
417 | int count = ((((memaddr + len) - addr) + sizeof (PTRACE_TYPE_RET) - 1) |
418 | / sizeof (PTRACE_TYPE_RET)); | |
419 | int alloc = count * sizeof (PTRACE_TYPE_RET); | |
420 | PTRACE_TYPE_RET *buffer; | |
94cd915f MS |
421 | struct cleanup *old_chain = NULL; |
422 | ||
371a6e84 MK |
423 | #ifdef PT_IO |
424 | /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO request | |
425 | that promises to be much more efficient in reading and writing | |
426 | data in the traced process's address space. */ | |
427 | ||
428 | { | |
429 | struct ptrace_io_desc piod; | |
430 | ||
431 | /* NOTE: We assume that there are no distinct address spaces for | |
432 | instruction and data. */ | |
433 | piod.piod_op = write ? PIOD_WRITE_D : PIOD_READ_D; | |
434 | piod.piod_offs = (void *) memaddr; | |
435 | piod.piod_addr = myaddr; | |
436 | piod.piod_len = len; | |
437 | ||
438 | if (ptrace (PT_IO, PIDGET (inferior_ptid), (caddr_t) &piod, 0) == -1) | |
439 | { | |
440 | /* If the PT_IO request is somehow not supported, fallback on | |
441 | using PT_WRITE_D/PT_READ_D. Otherwise we will return zero | |
442 | to indicate failure. */ | |
443 | if (errno != EINVAL) | |
444 | return 0; | |
445 | } | |
446 | else | |
447 | { | |
448 | /* Return the actual number of bytes read or written. */ | |
449 | return piod.piod_len; | |
450 | } | |
451 | } | |
452 | #endif | |
453 | ||
c906108c | 454 | /* Allocate buffer of that many longwords. */ |
94cd915f MS |
455 | if (len < GDB_MAX_ALLOCA) |
456 | { | |
88800403 | 457 | buffer = (PTRACE_TYPE_RET *) alloca (alloc); |
94cd915f MS |
458 | } |
459 | else | |
460 | { | |
88800403 | 461 | buffer = (PTRACE_TYPE_RET *) xmalloc (alloc); |
94cd915f MS |
462 | old_chain = make_cleanup (xfree, buffer); |
463 | } | |
c906108c SS |
464 | |
465 | if (write) | |
466 | { | |
3c2fb7bd MK |
467 | /* Fill start and end extra bytes of buffer with existing memory |
468 | data. */ | |
88800403 | 469 | if (addr != memaddr || len < (int) sizeof (PTRACE_TYPE_RET)) |
c5aa993b JM |
470 | { |
471 | /* Need part of initial word -- fetch it. */ | |
39f77062 | 472 | buffer[0] = ptrace (PT_READ_I, PIDGET (inferior_ptid), |
655c5466 | 473 | (PTRACE_TYPE_ARG3) addr, 0); |
c5aa993b | 474 | } |
c906108c | 475 | |
3c2fb7bd | 476 | if (count > 1) /* FIXME, avoid if even boundary. */ |
c906108c | 477 | { |
3c2fb7bd MK |
478 | buffer[count - 1] = |
479 | ptrace (PT_READ_I, PIDGET (inferior_ptid), | |
655c5466 | 480 | ((PTRACE_TYPE_ARG3) |
88800403 | 481 | (addr + (count - 1) * sizeof (PTRACE_TYPE_RET))), 0); |
c906108c SS |
482 | } |
483 | ||
3c2fb7bd | 484 | /* Copy data to be written over corresponding part of buffer. */ |
88800403 | 485 | memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_TYPE_RET) - 1)), |
3c2fb7bd | 486 | myaddr, len); |
c906108c SS |
487 | |
488 | /* Write the entire buffer. */ | |
88800403 | 489 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_TYPE_RET)) |
c906108c SS |
490 | { |
491 | errno = 0; | |
39f77062 | 492 | ptrace (PT_WRITE_D, PIDGET (inferior_ptid), |
655c5466 | 493 | (PTRACE_TYPE_ARG3) addr, buffer[i]); |
c906108c | 494 | if (errno) |
c5aa993b | 495 | { |
c906108c | 496 | /* Using the appropriate one (I or D) is necessary for |
c5aa993b | 497 | Gould NP1, at least. */ |
c906108c | 498 | errno = 0; |
39f77062 | 499 | ptrace (PT_WRITE_I, PIDGET (inferior_ptid), |
655c5466 | 500 | (PTRACE_TYPE_ARG3) addr, buffer[i]); |
c906108c SS |
501 | } |
502 | if (errno) | |
503 | return 0; | |
504 | } | |
c906108c SS |
505 | } |
506 | else | |
507 | { | |
3c2fb7bd | 508 | /* Read all the longwords. */ |
88800403 | 509 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_TYPE_RET)) |
c906108c SS |
510 | { |
511 | errno = 0; | |
39f77062 | 512 | buffer[i] = ptrace (PT_READ_I, PIDGET (inferior_ptid), |
655c5466 | 513 | (PTRACE_TYPE_ARG3) addr, 0); |
c906108c SS |
514 | if (errno) |
515 | return 0; | |
516 | QUIT; | |
517 | } | |
518 | ||
519 | /* Copy appropriate bytes out of the buffer. */ | |
520 | memcpy (myaddr, | |
88800403 | 521 | (char *) buffer + (memaddr & (sizeof (PTRACE_TYPE_RET) - 1)), |
c906108c SS |
522 | len); |
523 | } | |
3c2fb7bd | 524 | |
94cd915f MS |
525 | if (old_chain != NULL) |
526 | do_cleanups (old_chain); | |
c906108c SS |
527 | return len; |
528 | } | |
c906108c | 529 | \f |
c5aa993b | 530 | |
c906108c | 531 | static void |
fba45db2 | 532 | udot_info (char *dummy1, int dummy2) |
c906108c SS |
533 | { |
534 | #if defined (KERNEL_U_SIZE) | |
7343d46a | 535 | long udot_off; /* Offset into user struct */ |
c5aa993b JM |
536 | int udot_val; /* Value from user struct at udot_off */ |
537 | char mess[128]; /* For messages */ | |
c906108c SS |
538 | #endif |
539 | ||
c5aa993b JM |
540 | if (!target_has_execution) |
541 | { | |
542 | error ("The program is not being run."); | |
543 | } | |
c906108c SS |
544 | |
545 | #if !defined (KERNEL_U_SIZE) | |
546 | ||
547 | /* Adding support for this command is easy. Typically you just add a | |
548 | routine, called "kernel_u_size" that returns the size of the user | |
549 | struct, to the appropriate *-nat.c file and then add to the native | |
550 | config file "#define KERNEL_U_SIZE kernel_u_size()" */ | |
551 | error ("Don't know how large ``struct user'' is in this version of gdb."); | |
552 | ||
553 | #else | |
554 | ||
555 | for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val)) | |
556 | { | |
557 | if ((udot_off % 24) == 0) | |
558 | { | |
559 | if (udot_off > 0) | |
560 | { | |
561 | printf_filtered ("\n"); | |
562 | } | |
7343d46a | 563 | printf_filtered ("%s:", paddr (udot_off)); |
c906108c | 564 | } |
655c5466 | 565 | udot_val = ptrace (PT_READ_U, PIDGET (inferior_ptid), (PTRACE_TYPE_ARG3) udot_off, 0); |
c906108c SS |
566 | if (errno != 0) |
567 | { | |
7343d46a AC |
568 | sprintf (mess, "\nreading user struct at offset 0x%s", |
569 | paddr_nz (udot_off)); | |
c906108c SS |
570 | perror_with_name (mess); |
571 | } | |
572 | /* Avoid using nonportable (?) "*" in print specs */ | |
573 | printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val); | |
574 | } | |
575 | printf_filtered ("\n"); | |
576 | ||
577 | #endif | |
578 | } | |
579 | #endif /* !defined (CHILD_XFER_MEMORY). */ | |
c906108c | 580 | \f |
c5aa993b | 581 | |
c906108c | 582 | void |
fba45db2 | 583 | _initialize_infptrace (void) |
c906108c SS |
584 | { |
585 | #if !defined (CHILD_XFER_MEMORY) | |
586 | add_info ("udot", udot_info, | |
587 | "Print contents of kernel ``struct user'' for current child."); | |
588 | #endif | |
589 | } |