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