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