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