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