Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Low level Unix child interface to ptrace, for GDB when running under Unix. |
29e57380 | 2 | Copyright 1988, 89, 90, 91, 92, 93, 94, 95, 96, 1998, 2001 |
c906108c SS |
3 | Free Software Foundation, Inc. |
4 | ||
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
c906108c | 11 | |
c5aa993b JM |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
c906108c | 16 | |
c5aa993b JM |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
21 | |
22 | #include "defs.h" | |
23 | #include "frame.h" | |
24 | #include "inferior.h" | |
25 | #include "target.h" | |
26 | #include "gdb_string.h" | |
4e052eda | 27 | #include "regcache.h" |
ed9a39eb | 28 | |
03f2053f | 29 | #include "gdb_wait.h" |
ed9a39eb | 30 | |
c906108c SS |
31 | #include "command.h" |
32 | ||
33 | #ifdef USG | |
34 | #include <sys/types.h> | |
35 | #endif | |
36 | ||
37 | #include <sys/param.h> | |
4b14d3e4 | 38 | #include "gdb_dirent.h" |
c906108c SS |
39 | #include <signal.h> |
40 | #include <sys/ioctl.h> | |
41 | ||
42 | #ifdef HAVE_PTRACE_H | |
c5aa993b | 43 | #include <ptrace.h> |
c906108c | 44 | #else |
c5aa993b JM |
45 | #ifdef HAVE_SYS_PTRACE_H |
46 | #include <sys/ptrace.h> | |
47 | #endif | |
c906108c SS |
48 | #endif |
49 | ||
50 | #if !defined (PT_READ_I) | |
51 | #define PT_READ_I 1 /* Read word from text space */ | |
52 | #endif | |
53 | #if !defined (PT_READ_D) | |
54 | #define PT_READ_D 2 /* Read word from data space */ | |
55 | #endif | |
56 | #if !defined (PT_READ_U) | |
57 | #define PT_READ_U 3 /* Read word from kernel user struct */ | |
58 | #endif | |
59 | #if !defined (PT_WRITE_I) | |
60 | #define PT_WRITE_I 4 /* Write word to text space */ | |
61 | #endif | |
62 | #if !defined (PT_WRITE_D) | |
63 | #define PT_WRITE_D 5 /* Write word to data space */ | |
64 | #endif | |
65 | #if !defined (PT_WRITE_U) | |
66 | #define PT_WRITE_U 6 /* Write word to kernel user struct */ | |
67 | #endif | |
68 | #if !defined (PT_CONTINUE) | |
69 | #define PT_CONTINUE 7 /* Continue after signal */ | |
70 | #endif | |
71 | #if !defined (PT_STEP) | |
72 | #define PT_STEP 9 /* Set flag for single stepping */ | |
73 | #endif | |
74 | #if !defined (PT_KILL) | |
75 | #define PT_KILL 8 /* Send child a SIGKILL signal */ | |
76 | #endif | |
77 | ||
78 | #ifndef PT_ATTACH | |
79 | #define PT_ATTACH PTRACE_ATTACH | |
80 | #endif | |
81 | #ifndef PT_DETACH | |
82 | #define PT_DETACH PTRACE_DETACH | |
83 | #endif | |
84 | ||
85 | #include "gdbcore.h" | |
86 | #ifndef NO_SYS_FILE | |
87 | #include <sys/file.h> | |
88 | #endif | |
89 | #if 0 | |
90 | /* Don't think this is used anymore. On the sequent (not sure whether it's | |
91 | dynix or ptx or both), it is included unconditionally by sys/user.h and | |
92 | not protected against multiple inclusion. */ | |
93 | #include "gdb_stat.h" | |
94 | #endif | |
95 | ||
96 | #if !defined (FETCH_INFERIOR_REGISTERS) | |
97 | #include <sys/user.h> /* Probably need to poke the user structure */ | |
98 | #if defined (KERNEL_U_ADDR_BSD) | |
99 | #include <a.out.h> /* For struct nlist */ | |
100 | #endif /* KERNEL_U_ADDR_BSD. */ | |
101 | #endif /* !FETCH_INFERIOR_REGISTERS */ | |
102 | ||
103 | #if !defined (CHILD_XFER_MEMORY) | |
a14ed312 | 104 | static void udot_info (char *, int); |
c906108c SS |
105 | #endif |
106 | ||
107 | #if !defined (FETCH_INFERIOR_REGISTERS) | |
a14ed312 KB |
108 | static void fetch_register (int); |
109 | static void store_register (int); | |
c906108c SS |
110 | #endif |
111 | ||
ed9a39eb JM |
112 | /* |
113 | * Some systems (Linux) may have threads implemented as pseudo-processes, | |
114 | * in which case we may be tracing more than one process at a time. | |
115 | * In that case, inferior_pid will contain the main process ID and the | |
116 | * individual thread (process) id mashed together. These macros are | |
117 | * used to separate them out. The definitions may be overridden in tm.h | |
118 | * | |
119 | * NOTE: default definitions here are for systems with no threads. | |
120 | * Useful definitions MUST be provided in tm.h | |
121 | */ | |
122 | ||
123 | #if !defined (PIDGET) /* Default definition for PIDGET/TIDGET. */ | |
124 | #define PIDGET(PID) PID | |
125 | #define TIDGET(PID) 0 | |
126 | #endif | |
127 | ||
a14ed312 KB |
128 | void _initialize_kernel_u_addr (void); |
129 | void _initialize_infptrace (void); | |
c906108c | 130 | \f |
c5aa993b | 131 | |
c906108c SS |
132 | /* This function simply calls ptrace with the given arguments. |
133 | It exists so that all calls to ptrace are isolated in this | |
134 | machine-dependent file. */ | |
135 | int | |
fba45db2 | 136 | call_ptrace (int request, int pid, PTRACE_ARG3_TYPE addr, int data) |
c906108c SS |
137 | { |
138 | int pt_status = 0; | |
139 | ||
140 | #if 0 | |
141 | int saved_errno; | |
142 | ||
143 | printf ("call_ptrace(request=%d, pid=%d, addr=0x%x, data=0x%x)", | |
144 | request, pid, addr, data); | |
145 | #endif | |
146 | #if defined(PT_SETTRC) | |
147 | /* If the parent can be told to attach to us, try to do it. */ | |
c5aa993b JM |
148 | if (request == PT_SETTRC) |
149 | { | |
150 | errno = 0; | |
ed9a39eb JM |
151 | #if !defined (FIVE_ARG_PTRACE) |
152 | pt_status = ptrace (PT_SETTRC, pid, addr, data); | |
153 | #else | |
c5aa993b JM |
154 | /* Deal with HPUX 8.0 braindamage. We never use the |
155 | calls which require the fifth argument. */ | |
ed9a39eb | 156 | pt_status = ptrace (PT_SETTRC, pid, addr, data, 0); |
c906108c | 157 | #endif |
c5aa993b JM |
158 | if (errno) |
159 | perror_with_name ("ptrace"); | |
c906108c | 160 | #if 0 |
c5aa993b | 161 | printf (" = %d\n", pt_status); |
c906108c | 162 | #endif |
c5aa993b JM |
163 | if (pt_status < 0) |
164 | return pt_status; | |
165 | else | |
166 | return parent_attach_all (pid, addr, data); | |
167 | } | |
c906108c SS |
168 | #endif |
169 | ||
170 | #if defined(PT_CONTIN1) | |
171 | /* On HPUX, PT_CONTIN1 is a form of continue that preserves pending | |
172 | signals. If it's available, use it. */ | |
173 | if (request == PT_CONTINUE) | |
174 | request = PT_CONTIN1; | |
175 | #endif | |
176 | ||
177 | #if defined(PT_SINGLE1) | |
178 | /* On HPUX, PT_SINGLE1 is a form of step that preserves pending | |
179 | signals. If it's available, use it. */ | |
180 | if (request == PT_STEP) | |
181 | request = PT_SINGLE1; | |
182 | #endif | |
183 | ||
184 | #if 0 | |
185 | saved_errno = errno; | |
186 | errno = 0; | |
187 | #endif | |
ed9a39eb JM |
188 | #if !defined (FIVE_ARG_PTRACE) |
189 | pt_status = ptrace (request, pid, addr, data); | |
190 | #else | |
c5aa993b JM |
191 | /* Deal with HPUX 8.0 braindamage. We never use the |
192 | calls which require the fifth argument. */ | |
ed9a39eb | 193 | pt_status = ptrace (request, pid, addr, data, 0); |
c906108c | 194 | #endif |
ed9a39eb | 195 | |
c906108c SS |
196 | #if 0 |
197 | if (errno) | |
198 | printf (" [errno = %d]", errno); | |
199 | ||
200 | errno = saved_errno; | |
201 | printf (" = 0x%x\n", pt_status); | |
202 | #endif | |
203 | return pt_status; | |
204 | } | |
205 | ||
206 | ||
207 | #if defined (DEBUG_PTRACE) || defined (FIVE_ARG_PTRACE) | |
208 | /* For the rest of the file, use an extra level of indirection */ | |
209 | /* This lets us breakpoint usefully on call_ptrace. */ | |
210 | #define ptrace call_ptrace | |
211 | #endif | |
212 | ||
213 | /* Wait for a process to finish, possibly running a target-specific | |
214 | hook before returning. */ | |
215 | ||
216 | int | |
fba45db2 | 217 | ptrace_wait (int pid, int *status) |
c906108c SS |
218 | { |
219 | int wstate; | |
220 | ||
221 | wstate = wait (status); | |
222 | target_post_wait (wstate, *status); | |
223 | return wstate; | |
224 | } | |
225 | ||
226 | void | |
fba45db2 | 227 | kill_inferior (void) |
c906108c SS |
228 | { |
229 | int status; | |
230 | ||
231 | if (inferior_pid == 0) | |
232 | return; | |
233 | ||
234 | /* This once used to call "kill" to kill the inferior just in case | |
235 | the inferior was still running. As others have noted in the past | |
236 | (kingdon) there shouldn't be any way to get here if the inferior | |
237 | is still running -- else there's a major problem elsewere in gdb | |
238 | and it needs to be fixed. | |
239 | ||
240 | The kill call causes problems under hpux10, so it's been removed; | |
241 | if this causes problems we'll deal with them as they arise. */ | |
242 | ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0); | |
243 | ptrace_wait (0, &status); | |
244 | target_mourn_inferior (); | |
245 | } | |
246 | ||
247 | #ifndef CHILD_RESUME | |
248 | ||
249 | /* Resume execution of the inferior process. | |
250 | If STEP is nonzero, single-step it. | |
251 | If SIGNAL is nonzero, give it that signal. */ | |
252 | ||
253 | void | |
fba45db2 | 254 | child_resume (int pid, int step, enum target_signal signal) |
c906108c SS |
255 | { |
256 | errno = 0; | |
257 | ||
258 | if (pid == -1) | |
259 | /* Resume all threads. */ | |
260 | /* I think this only gets used in the non-threaded case, where "resume | |
261 | all threads" and "resume inferior_pid" are the same. */ | |
262 | pid = inferior_pid; | |
263 | ||
264 | /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where | |
265 | it was. (If GDB wanted it to start some other way, we have already | |
266 | written a new PC value to the child.) | |
267 | ||
268 | If this system does not support PT_STEP, a higher level function will | |
269 | have called single_step() to transmute the step request into a | |
270 | continue request (by setting breakpoints on all possible successor | |
271 | instructions), so we don't have to worry about that here. */ | |
272 | ||
273 | if (step) | |
274 | { | |
275 | if (SOFTWARE_SINGLE_STEP_P) | |
e1e9e218 | 276 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); /* Make sure this doesn't happen. */ |
c906108c | 277 | else |
c5aa993b | 278 | ptrace (PT_STEP, pid, (PTRACE_ARG3_TYPE) 1, |
c906108c SS |
279 | target_signal_to_host (signal)); |
280 | } | |
281 | else | |
282 | ptrace (PT_CONTINUE, pid, (PTRACE_ARG3_TYPE) 1, | |
283 | target_signal_to_host (signal)); | |
284 | ||
285 | if (errno) | |
ed9a39eb JM |
286 | { |
287 | perror_with_name ("ptrace"); | |
288 | } | |
c906108c SS |
289 | } |
290 | #endif /* CHILD_RESUME */ | |
c906108c | 291 | \f |
c5aa993b | 292 | |
c906108c SS |
293 | #ifdef ATTACH_DETACH |
294 | /* Start debugging the process whose number is PID. */ | |
295 | int | |
fba45db2 | 296 | attach (int pid) |
c906108c SS |
297 | { |
298 | errno = 0; | |
299 | ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0); | |
300 | if (errno) | |
301 | perror_with_name ("ptrace"); | |
302 | attach_flag = 1; | |
303 | return pid; | |
304 | } | |
305 | ||
306 | /* Stop debugging the process whose number is PID | |
307 | and continue it with signal number SIGNAL. | |
308 | SIGNAL = 0 means just continue it. */ | |
309 | ||
310 | void | |
fba45db2 | 311 | detach (int signal) |
c906108c SS |
312 | { |
313 | errno = 0; | |
314 | ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal); | |
315 | if (errno) | |
316 | perror_with_name ("ptrace"); | |
317 | attach_flag = 0; | |
318 | } | |
319 | #endif /* ATTACH_DETACH */ | |
320 | \f | |
321 | /* Default the type of the ptrace transfer to int. */ | |
322 | #ifndef PTRACE_XFER_TYPE | |
323 | #define PTRACE_XFER_TYPE int | |
324 | #endif | |
325 | ||
326 | /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0 | |
327 | to get the offset in the core file of the register values. */ | |
328 | #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS) | |
329 | /* Get kernel_u_addr using BSD-style nlist(). */ | |
330 | CORE_ADDR kernel_u_addr; | |
331 | #endif /* KERNEL_U_ADDR_BSD. */ | |
332 | ||
333 | void | |
fba45db2 | 334 | _initialize_kernel_u_addr (void) |
c906108c SS |
335 | { |
336 | #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS) | |
337 | struct nlist names[2]; | |
338 | ||
339 | names[0].n_un.n_name = "_u"; | |
340 | names[1].n_un.n_name = NULL; | |
341 | if (nlist ("/vmunix", names) == 0) | |
342 | kernel_u_addr = names[0].n_value; | |
343 | else | |
8e65ff28 AC |
344 | internal_error (__FILE__, __LINE__, |
345 | "Unable to get kernel u area address."); | |
c906108c SS |
346 | #endif /* KERNEL_U_ADDR_BSD. */ |
347 | } | |
348 | ||
349 | #if !defined (FETCH_INFERIOR_REGISTERS) | |
350 | ||
351 | #if !defined (offsetof) | |
352 | #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER) | |
353 | #endif | |
354 | ||
355 | /* U_REGS_OFFSET is the offset of the registers within the u area. */ | |
356 | #if !defined (U_REGS_OFFSET) | |
357 | #define U_REGS_OFFSET \ | |
358 | ptrace (PT_READ_U, inferior_pid, \ | |
359 | (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \ | |
360 | - KERNEL_U_ADDR | |
361 | #endif | |
362 | ||
363 | /* Registers we shouldn't try to fetch. */ | |
364 | #if !defined (CANNOT_FETCH_REGISTER) | |
365 | #define CANNOT_FETCH_REGISTER(regno) 0 | |
366 | #endif | |
367 | ||
368 | /* Fetch one register. */ | |
369 | ||
370 | static void | |
fba45db2 | 371 | fetch_register (int regno) |
c906108c SS |
372 | { |
373 | /* This isn't really an address. But ptrace thinks of it as one. */ | |
374 | CORE_ADDR regaddr; | |
c5aa993b | 375 | char mess[128]; /* For messages */ |
c906108c | 376 | register int i; |
c5aa993b | 377 | unsigned int offset; /* Offset of registers within the u area. */ |
c906108c | 378 | char buf[MAX_REGISTER_RAW_SIZE]; |
ed9a39eb | 379 | int tid; |
c906108c SS |
380 | |
381 | if (CANNOT_FETCH_REGISTER (regno)) | |
382 | { | |
383 | memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */ | |
384 | supply_register (regno, buf); | |
385 | return; | |
386 | } | |
387 | ||
ed9a39eb JM |
388 | /* Overload thread id onto process id */ |
389 | if ((tid = TIDGET (inferior_pid)) == 0) | |
390 | tid = inferior_pid; /* no thread id, just use process id */ | |
391 | ||
c906108c SS |
392 | offset = U_REGS_OFFSET; |
393 | ||
394 | regaddr = register_addr (regno, offset); | |
395 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE)) | |
396 | { | |
397 | errno = 0; | |
ed9a39eb JM |
398 | *(PTRACE_XFER_TYPE *) & buf[i] = ptrace (PT_READ_U, tid, |
399 | (PTRACE_ARG3_TYPE) regaddr, 0); | |
c906108c SS |
400 | regaddr += sizeof (PTRACE_XFER_TYPE); |
401 | if (errno != 0) | |
402 | { | |
ed9a39eb JM |
403 | sprintf (mess, "reading register %s (#%d)", |
404 | REGISTER_NAME (regno), regno); | |
c906108c SS |
405 | perror_with_name (mess); |
406 | } | |
407 | } | |
408 | supply_register (regno, buf); | |
409 | } | |
410 | ||
411 | ||
412 | /* Fetch register values from the inferior. | |
413 | If REGNO is negative, do this for all registers. | |
414 | Otherwise, REGNO specifies which register (so we can save time). */ | |
415 | ||
416 | void | |
fba45db2 | 417 | fetch_inferior_registers (int regno) |
c906108c SS |
418 | { |
419 | if (regno >= 0) | |
420 | { | |
421 | fetch_register (regno); | |
422 | } | |
423 | else | |
424 | { | |
425 | for (regno = 0; regno < ARCH_NUM_REGS; regno++) | |
426 | { | |
427 | fetch_register (regno); | |
428 | } | |
429 | } | |
430 | } | |
431 | ||
432 | /* Registers we shouldn't try to store. */ | |
433 | #if !defined (CANNOT_STORE_REGISTER) | |
434 | #define CANNOT_STORE_REGISTER(regno) 0 | |
435 | #endif | |
436 | ||
437 | /* Store one register. */ | |
438 | ||
439 | static void | |
fba45db2 | 440 | store_register (int regno) |
c906108c SS |
441 | { |
442 | /* This isn't really an address. But ptrace thinks of it as one. */ | |
443 | CORE_ADDR regaddr; | |
c5aa993b | 444 | char mess[128]; /* For messages */ |
c906108c | 445 | register int i; |
c5aa993b | 446 | unsigned int offset; /* Offset of registers within the u area. */ |
ed9a39eb | 447 | int tid; |
c906108c SS |
448 | |
449 | if (CANNOT_STORE_REGISTER (regno)) | |
450 | { | |
451 | return; | |
452 | } | |
453 | ||
ed9a39eb JM |
454 | /* Overload thread id onto process id */ |
455 | if ((tid = TIDGET (inferior_pid)) == 0) | |
456 | tid = inferior_pid; /* no thread id, just use process id */ | |
457 | ||
c906108c SS |
458 | offset = U_REGS_OFFSET; |
459 | ||
460 | regaddr = register_addr (regno, offset); | |
c5aa993b | 461 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE)) |
c906108c SS |
462 | { |
463 | errno = 0; | |
ed9a39eb | 464 | ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr, |
c5aa993b | 465 | *(PTRACE_XFER_TYPE *) & registers[REGISTER_BYTE (regno) + i]); |
c906108c SS |
466 | regaddr += sizeof (PTRACE_XFER_TYPE); |
467 | if (errno != 0) | |
468 | { | |
ed9a39eb JM |
469 | sprintf (mess, "writing register %s (#%d)", |
470 | REGISTER_NAME (regno), regno); | |
c906108c SS |
471 | perror_with_name (mess); |
472 | } | |
473 | } | |
474 | } | |
475 | ||
476 | /* Store our register values back into the inferior. | |
477 | If REGNO is negative, do this for all registers. | |
478 | Otherwise, REGNO specifies which register (so we can save time). */ | |
479 | ||
480 | void | |
fba45db2 | 481 | store_inferior_registers (int regno) |
c906108c SS |
482 | { |
483 | if (regno >= 0) | |
484 | { | |
485 | store_register (regno); | |
486 | } | |
487 | else | |
488 | { | |
489 | for (regno = 0; regno < ARCH_NUM_REGS; regno++) | |
490 | { | |
491 | store_register (regno); | |
492 | } | |
493 | } | |
494 | } | |
495 | #endif /* !defined (FETCH_INFERIOR_REGISTERS). */ | |
496 | \f | |
497 | ||
498 | #if !defined (CHILD_XFER_MEMORY) | |
499 | /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory | |
500 | in the NEW_SUN_PTRACE case. | |
501 | It ought to be straightforward. But it appears that writing did | |
502 | not write the data that I specified. I cannot understand where | |
503 | it got the data that it actually did write. */ | |
504 | ||
505 | /* Copy LEN bytes to or from inferior's memory starting at MEMADDR | |
506 | to debugger memory starting at MYADDR. Copy to inferior if | |
73186089 | 507 | WRITE is nonzero. TARGET is ignored. |
c5aa993b | 508 | |
c906108c SS |
509 | Returns the length copied, which is either the LEN argument or zero. |
510 | This xfer function does not do partial moves, since child_ops | |
511 | doesn't allow memory operations to cross below us in the target stack | |
512 | anyway. */ | |
513 | ||
514 | int | |
73186089 | 515 | child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write, |
29e57380 | 516 | struct mem_attrib *attrib ATTRIBUTE_UNUSED, |
73186089 | 517 | struct target_ops *target) |
c906108c SS |
518 | { |
519 | register int i; | |
520 | /* Round starting address down to longword boundary. */ | |
c5aa993b | 521 | register CORE_ADDR addr = memaddr & -sizeof (PTRACE_XFER_TYPE); |
c906108c SS |
522 | /* Round ending address up; get number of longwords that makes. */ |
523 | register int count | |
c5aa993b JM |
524 | = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) |
525 | / sizeof (PTRACE_XFER_TYPE); | |
c906108c SS |
526 | /* Allocate buffer of that many longwords. */ |
527 | register PTRACE_XFER_TYPE *buffer | |
c5aa993b | 528 | = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE)); |
c906108c SS |
529 | |
530 | if (write) | |
531 | { | |
532 | /* Fill start and end extra bytes of buffer with existing memory data. */ | |
533 | ||
c5aa993b JM |
534 | if (addr != memaddr || len < (int) sizeof (PTRACE_XFER_TYPE)) |
535 | { | |
536 | /* Need part of initial word -- fetch it. */ | |
ed9a39eb JM |
537 | buffer[0] = ptrace (PT_READ_I, PIDGET (inferior_pid), |
538 | (PTRACE_ARG3_TYPE) addr, 0); | |
c5aa993b | 539 | } |
c906108c SS |
540 | |
541 | if (count > 1) /* FIXME, avoid if even boundary */ | |
542 | { | |
ed9a39eb JM |
543 | buffer[count - 1] |
544 | = ptrace (PT_READ_I, PIDGET (inferior_pid), | |
c906108c SS |
545 | ((PTRACE_ARG3_TYPE) |
546 | (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))), | |
547 | 0); | |
548 | } | |
549 | ||
550 | /* Copy data to be written over corresponding part of buffer */ | |
551 | ||
552 | memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), | |
553 | myaddr, | |
554 | len); | |
555 | ||
556 | /* Write the entire buffer. */ | |
557 | ||
558 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) | |
559 | { | |
560 | errno = 0; | |
ed9a39eb JM |
561 | ptrace (PT_WRITE_D, PIDGET (inferior_pid), |
562 | (PTRACE_ARG3_TYPE) addr, buffer[i]); | |
c906108c | 563 | if (errno) |
c5aa993b | 564 | { |
c906108c | 565 | /* Using the appropriate one (I or D) is necessary for |
c5aa993b | 566 | Gould NP1, at least. */ |
c906108c | 567 | errno = 0; |
ed9a39eb JM |
568 | ptrace (PT_WRITE_I, PIDGET (inferior_pid), |
569 | (PTRACE_ARG3_TYPE) addr, buffer[i]); | |
c906108c SS |
570 | } |
571 | if (errno) | |
572 | return 0; | |
573 | } | |
574 | #ifdef CLEAR_INSN_CACHE | |
c5aa993b | 575 | CLEAR_INSN_CACHE (); |
c906108c SS |
576 | #endif |
577 | } | |
578 | else | |
579 | { | |
580 | /* Read all the longwords */ | |
581 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) | |
582 | { | |
583 | errno = 0; | |
ed9a39eb | 584 | buffer[i] = ptrace (PT_READ_I, PIDGET (inferior_pid), |
c906108c SS |
585 | (PTRACE_ARG3_TYPE) addr, 0); |
586 | if (errno) | |
587 | return 0; | |
588 | QUIT; | |
589 | } | |
590 | ||
591 | /* Copy appropriate bytes out of the buffer. */ | |
592 | memcpy (myaddr, | |
593 | (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), | |
594 | len); | |
595 | } | |
596 | return len; | |
597 | } | |
c906108c | 598 | \f |
c5aa993b | 599 | |
c906108c | 600 | static void |
fba45db2 | 601 | udot_info (char *dummy1, int dummy2) |
c906108c SS |
602 | { |
603 | #if defined (KERNEL_U_SIZE) | |
c5aa993b JM |
604 | int udot_off; /* Offset into user struct */ |
605 | int udot_val; /* Value from user struct at udot_off */ | |
606 | char mess[128]; /* For messages */ | |
c906108c SS |
607 | #endif |
608 | ||
c5aa993b JM |
609 | if (!target_has_execution) |
610 | { | |
611 | error ("The program is not being run."); | |
612 | } | |
c906108c SS |
613 | |
614 | #if !defined (KERNEL_U_SIZE) | |
615 | ||
616 | /* Adding support for this command is easy. Typically you just add a | |
617 | routine, called "kernel_u_size" that returns the size of the user | |
618 | struct, to the appropriate *-nat.c file and then add to the native | |
619 | config file "#define KERNEL_U_SIZE kernel_u_size()" */ | |
620 | error ("Don't know how large ``struct user'' is in this version of gdb."); | |
621 | ||
622 | #else | |
623 | ||
624 | for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val)) | |
625 | { | |
626 | if ((udot_off % 24) == 0) | |
627 | { | |
628 | if (udot_off > 0) | |
629 | { | |
630 | printf_filtered ("\n"); | |
631 | } | |
632 | printf_filtered ("%04x:", udot_off); | |
633 | } | |
634 | udot_val = ptrace (PT_READ_U, inferior_pid, (PTRACE_ARG3_TYPE) udot_off, 0); | |
635 | if (errno != 0) | |
636 | { | |
637 | sprintf (mess, "\nreading user struct at offset 0x%x", udot_off); | |
638 | perror_with_name (mess); | |
639 | } | |
640 | /* Avoid using nonportable (?) "*" in print specs */ | |
641 | printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val); | |
642 | } | |
643 | printf_filtered ("\n"); | |
644 | ||
645 | #endif | |
646 | } | |
647 | #endif /* !defined (CHILD_XFER_MEMORY). */ | |
c906108c | 648 | \f |
c5aa993b | 649 | |
c906108c | 650 | void |
fba45db2 | 651 | _initialize_infptrace (void) |
c906108c SS |
652 | { |
653 | #if !defined (CHILD_XFER_MEMORY) | |
654 | add_info ("udot", udot_info, | |
655 | "Print contents of kernel ``struct user'' for current child."); | |
656 | #endif | |
657 | } |