| 1 | /* Low level Unix child interface to ptrace, for GDB when running under Unix. |
| 2 | Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, |
| 3 | 1998, 1999, 2000, 2001, 2002 |
| 4 | Free Software Foundation, Inc. |
| 5 | |
| 6 | This file is part of GDB. |
| 7 | |
| 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. |
| 12 | |
| 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. |
| 17 | |
| 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. */ |
| 22 | |
| 23 | #include "defs.h" |
| 24 | #include "frame.h" |
| 25 | #include "inferior.h" |
| 26 | #include "target.h" |
| 27 | #include "gdb_string.h" |
| 28 | #include "regcache.h" |
| 29 | |
| 30 | #include "gdb_wait.h" |
| 31 | |
| 32 | #include "command.h" |
| 33 | |
| 34 | #ifdef USG |
| 35 | #include <sys/types.h> |
| 36 | #endif |
| 37 | |
| 38 | #include <sys/param.h> |
| 39 | #include "gdb_dirent.h" |
| 40 | #include <signal.h> |
| 41 | #include <sys/ioctl.h> |
| 42 | |
| 43 | #ifdef HAVE_PTRACE_H |
| 44 | #include <ptrace.h> |
| 45 | #else |
| 46 | #ifdef HAVE_SYS_PTRACE_H |
| 47 | #include <sys/ptrace.h> |
| 48 | #endif |
| 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 | #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) |
| 98 | static void udot_info (char *, int); |
| 99 | #endif |
| 100 | |
| 101 | #if !defined (FETCH_INFERIOR_REGISTERS) |
| 102 | static void fetch_register (int); |
| 103 | static void store_register (int); |
| 104 | #endif |
| 105 | |
| 106 | void _initialize_kernel_u_addr (void); |
| 107 | void _initialize_infptrace (void); |
| 108 | \f |
| 109 | |
| 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 |
| 114 | call_ptrace (int request, int pid, PTRACE_ARG3_TYPE addr, int data) |
| 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. */ |
| 126 | if (request == PT_SETTRC) |
| 127 | { |
| 128 | errno = 0; |
| 129 | #if !defined (FIVE_ARG_PTRACE) |
| 130 | pt_status = ptrace (PT_SETTRC, pid, addr, data); |
| 131 | #else |
| 132 | /* Deal with HPUX 8.0 braindamage. We never use the |
| 133 | calls which require the fifth argument. */ |
| 134 | pt_status = ptrace (PT_SETTRC, pid, addr, data, 0); |
| 135 | #endif |
| 136 | if (errno) |
| 137 | perror_with_name ("ptrace"); |
| 138 | #if 0 |
| 139 | printf (" = %d\n", pt_status); |
| 140 | #endif |
| 141 | if (pt_status < 0) |
| 142 | return pt_status; |
| 143 | else |
| 144 | return parent_attach_all (pid, addr, data); |
| 145 | } |
| 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 |
| 166 | #if !defined (FIVE_ARG_PTRACE) |
| 167 | pt_status = ptrace (request, pid, addr, data); |
| 168 | #else |
| 169 | /* Deal with HPUX 8.0 braindamage. We never use the |
| 170 | calls which require the fifth argument. */ |
| 171 | pt_status = ptrace (request, pid, addr, data, 0); |
| 172 | #endif |
| 173 | |
| 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 |
| 195 | ptrace_wait (ptid_t ptid, int *status) |
| 196 | { |
| 197 | int wstate; |
| 198 | |
| 199 | wstate = wait (status); |
| 200 | target_post_wait (pid_to_ptid (wstate), *status); |
| 201 | return wstate; |
| 202 | } |
| 203 | |
| 204 | #ifndef KILL_INFERIOR |
| 205 | void |
| 206 | kill_inferior (void) |
| 207 | { |
| 208 | int status; |
| 209 | int pid = PIDGET (inferior_ptid); |
| 210 | |
| 211 | if (pid == 0) |
| 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. */ |
| 222 | ptrace (PT_KILL, pid, (PTRACE_ARG3_TYPE) 0, 0); |
| 223 | ptrace_wait (null_ptid, &status); |
| 224 | target_mourn_inferior (); |
| 225 | } |
| 226 | #endif /* KILL_INFERIOR */ |
| 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 |
| 235 | child_resume (ptid_t ptid, int step, enum target_signal signal) |
| 236 | { |
| 237 | int pid = PIDGET (ptid); |
| 238 | |
| 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 |
| 244 | all threads" and "resume inferior_ptid" are the same. */ |
| 245 | pid = PIDGET (inferior_ptid); |
| 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 | { |
| 258 | if (SOFTWARE_SINGLE_STEP_P ()) |
| 259 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); /* Make sure this doesn't happen. */ |
| 260 | else |
| 261 | ptrace (PT_STEP, pid, (PTRACE_ARG3_TYPE) 1, |
| 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) |
| 269 | { |
| 270 | perror_with_name ("ptrace"); |
| 271 | } |
| 272 | } |
| 273 | #endif /* CHILD_RESUME */ |
| 274 | \f |
| 275 | /* Start debugging the process whose number is PID. */ |
| 276 | int |
| 277 | attach (int pid) |
| 278 | { |
| 279 | errno = 0; |
| 280 | #ifndef PT_ATTACH |
| 281 | #ifdef PTRACE_ATTACH |
| 282 | #define PT_ATTACH PTRACE_ATTACH |
| 283 | #endif |
| 284 | #endif |
| 285 | #ifdef PT_ATTACH |
| 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; |
| 291 | #else |
| 292 | error ("This system does not support attaching to a process"); |
| 293 | #endif |
| 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 |
| 301 | detach (int signal) |
| 302 | { |
| 303 | errno = 0; |
| 304 | #ifndef PT_DETACH |
| 305 | #ifdef PTRACE_DETACH |
| 306 | #define PT_DETACH PTRACE_DETACH |
| 307 | #endif |
| 308 | #endif |
| 309 | #ifdef PT_DETACH |
| 310 | ptrace (PT_DETACH, PIDGET (inferior_ptid), (PTRACE_ARG3_TYPE) 1, |
| 311 | signal); |
| 312 | if (errno) |
| 313 | print_sys_errmsg ("ptrace", errno); |
| 314 | attach_flag = 0; |
| 315 | #else |
| 316 | error ("This system does not support detaching from a process"); |
| 317 | #endif |
| 318 | } |
| 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 |
| 333 | _initialize_kernel_u_addr (void) |
| 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 |
| 343 | internal_error (__FILE__, __LINE__, |
| 344 | "Unable to get kernel u area address."); |
| 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 \ |
| 357 | ptrace (PT_READ_U, PIDGET (inferior_ptid), \ |
| 358 | (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \ |
| 359 | - KERNEL_U_ADDR |
| 360 | #endif |
| 361 | |
| 362 | /* Fetch one register. */ |
| 363 | |
| 364 | static void |
| 365 | fetch_register (int regno) |
| 366 | { |
| 367 | /* This isn't really an address. But ptrace thinks of it as one. */ |
| 368 | CORE_ADDR regaddr; |
| 369 | char mess[128]; /* For messages */ |
| 370 | int i; |
| 371 | unsigned int offset; /* Offset of registers within the u area. */ |
| 372 | char buf[MAX_REGISTER_SIZE]; |
| 373 | int tid; |
| 374 | |
| 375 | if (CANNOT_FETCH_REGISTER (regno)) |
| 376 | { |
| 377 | memset (buf, '\0', DEPRECATED_REGISTER_RAW_SIZE (regno)); /* Supply zeroes */ |
| 378 | supply_register (regno, buf); |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | /* Overload thread id onto process id */ |
| 383 | if ((tid = TIDGET (inferior_ptid)) == 0) |
| 384 | tid = PIDGET (inferior_ptid); /* no thread id, just use process id */ |
| 385 | |
| 386 | offset = U_REGS_OFFSET; |
| 387 | |
| 388 | regaddr = register_addr (regno, offset); |
| 389 | for (i = 0; i < DEPRECATED_REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE)) |
| 390 | { |
| 391 | errno = 0; |
| 392 | *(PTRACE_XFER_TYPE *) & buf[i] = ptrace (PT_READ_U, tid, |
| 393 | (PTRACE_ARG3_TYPE) regaddr, 0); |
| 394 | regaddr += sizeof (PTRACE_XFER_TYPE); |
| 395 | if (errno != 0) |
| 396 | { |
| 397 | sprintf (mess, "reading register %s (#%d)", |
| 398 | REGISTER_NAME (regno), regno); |
| 399 | perror_with_name (mess); |
| 400 | } |
| 401 | } |
| 402 | supply_register (regno, buf); |
| 403 | } |
| 404 | |
| 405 | |
| 406 | /* Fetch register values from the inferior. |
| 407 | If REGNO is negative, do this for all registers. |
| 408 | Otherwise, REGNO specifies which register (so we can save time). */ |
| 409 | |
| 410 | void |
| 411 | fetch_inferior_registers (int regno) |
| 412 | { |
| 413 | if (regno >= 0) |
| 414 | { |
| 415 | fetch_register (regno); |
| 416 | } |
| 417 | else |
| 418 | { |
| 419 | for (regno = 0; regno < NUM_REGS; regno++) |
| 420 | { |
| 421 | fetch_register (regno); |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /* Store one register. */ |
| 427 | |
| 428 | static void |
| 429 | store_register (int regno) |
| 430 | { |
| 431 | /* This isn't really an address. But ptrace thinks of it as one. */ |
| 432 | CORE_ADDR regaddr; |
| 433 | char mess[128]; /* For messages */ |
| 434 | int i; |
| 435 | unsigned int offset; /* Offset of registers within the u area. */ |
| 436 | int tid; |
| 437 | char buf[MAX_REGISTER_SIZE]; |
| 438 | |
| 439 | if (CANNOT_STORE_REGISTER (regno)) |
| 440 | { |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | /* Overload thread id onto process id */ |
| 445 | if ((tid = TIDGET (inferior_ptid)) == 0) |
| 446 | tid = PIDGET (inferior_ptid); /* no thread id, just use process id */ |
| 447 | |
| 448 | offset = U_REGS_OFFSET; |
| 449 | |
| 450 | regaddr = register_addr (regno, offset); |
| 451 | |
| 452 | /* Put the contents of regno into a local buffer */ |
| 453 | regcache_collect (regno, buf); |
| 454 | |
| 455 | /* Store the local buffer into the inferior a chunk at the time. */ |
| 456 | for (i = 0; i < DEPRECATED_REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE)) |
| 457 | { |
| 458 | errno = 0; |
| 459 | ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr, |
| 460 | *(PTRACE_XFER_TYPE *) (buf + i)); |
| 461 | regaddr += sizeof (PTRACE_XFER_TYPE); |
| 462 | if (errno != 0) |
| 463 | { |
| 464 | sprintf (mess, "writing register %s (#%d)", |
| 465 | REGISTER_NAME (regno), regno); |
| 466 | perror_with_name (mess); |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /* Store our register values back into the inferior. |
| 472 | If REGNO is negative, do this for all registers. |
| 473 | Otherwise, REGNO specifies which register (so we can save time). */ |
| 474 | |
| 475 | void |
| 476 | store_inferior_registers (int regno) |
| 477 | { |
| 478 | if (regno >= 0) |
| 479 | { |
| 480 | store_register (regno); |
| 481 | } |
| 482 | else |
| 483 | { |
| 484 | for (regno = 0; regno < NUM_REGS; regno++) |
| 485 | { |
| 486 | store_register (regno); |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | #endif /* !defined (FETCH_INFERIOR_REGISTERS). */ |
| 491 | \f |
| 492 | |
| 493 | /* Set an upper limit on alloca. */ |
| 494 | #ifndef GDB_MAX_ALLOCA |
| 495 | #define GDB_MAX_ALLOCA 0x1000 |
| 496 | #endif |
| 497 | |
| 498 | #if !defined (CHILD_XFER_MEMORY) |
| 499 | /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory |
| 500 | in the NEW_SUN_PTRACE case. It ought to be straightforward. But |
| 501 | it appears that writing did not write the data that I specified. I |
| 502 | cannot understand where it got the data that it actually did write. */ |
| 503 | |
| 504 | /* Copy LEN bytes to or from inferior's memory starting at MEMADDR to |
| 505 | debugger memory starting at MYADDR. Copy to inferior if WRITE is |
| 506 | nonzero. TARGET is ignored. |
| 507 | |
| 508 | Returns the length copied, which is either the LEN argument or |
| 509 | zero. This xfer function does not do partial moves, since |
| 510 | child_ops doesn't allow memory operations to cross below us in the |
| 511 | target stack anyway. */ |
| 512 | |
| 513 | int |
| 514 | child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write, |
| 515 | struct mem_attrib *attrib, struct target_ops *target) |
| 516 | { |
| 517 | int i; |
| 518 | /* Round starting address down to longword boundary. */ |
| 519 | CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE); |
| 520 | /* Round ending address up; get number of longwords that makes. */ |
| 521 | int count = ((((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) |
| 522 | / sizeof (PTRACE_XFER_TYPE)); |
| 523 | int alloc = count * sizeof (PTRACE_XFER_TYPE); |
| 524 | PTRACE_XFER_TYPE *buffer; |
| 525 | struct cleanup *old_chain = NULL; |
| 526 | |
| 527 | #ifdef PT_IO |
| 528 | /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO request |
| 529 | that promises to be much more efficient in reading and writing |
| 530 | data in the traced process's address space. */ |
| 531 | |
| 532 | { |
| 533 | struct ptrace_io_desc piod; |
| 534 | |
| 535 | /* NOTE: We assume that there are no distinct address spaces for |
| 536 | instruction and data. */ |
| 537 | piod.piod_op = write ? PIOD_WRITE_D : PIOD_READ_D; |
| 538 | piod.piod_offs = (void *) memaddr; |
| 539 | piod.piod_addr = myaddr; |
| 540 | piod.piod_len = len; |
| 541 | |
| 542 | if (ptrace (PT_IO, PIDGET (inferior_ptid), (caddr_t) &piod, 0) == -1) |
| 543 | { |
| 544 | /* If the PT_IO request is somehow not supported, fallback on |
| 545 | using PT_WRITE_D/PT_READ_D. Otherwise we will return zero |
| 546 | to indicate failure. */ |
| 547 | if (errno != EINVAL) |
| 548 | return 0; |
| 549 | } |
| 550 | else |
| 551 | { |
| 552 | /* Return the actual number of bytes read or written. */ |
| 553 | return piod.piod_len; |
| 554 | } |
| 555 | } |
| 556 | #endif |
| 557 | |
| 558 | /* Allocate buffer of that many longwords. */ |
| 559 | if (len < GDB_MAX_ALLOCA) |
| 560 | { |
| 561 | buffer = (PTRACE_XFER_TYPE *) alloca (alloc); |
| 562 | } |
| 563 | else |
| 564 | { |
| 565 | buffer = (PTRACE_XFER_TYPE *) xmalloc (alloc); |
| 566 | old_chain = make_cleanup (xfree, buffer); |
| 567 | } |
| 568 | |
| 569 | if (write) |
| 570 | { |
| 571 | /* Fill start and end extra bytes of buffer with existing memory |
| 572 | data. */ |
| 573 | if (addr != memaddr || len < (int) sizeof (PTRACE_XFER_TYPE)) |
| 574 | { |
| 575 | /* Need part of initial word -- fetch it. */ |
| 576 | buffer[0] = ptrace (PT_READ_I, PIDGET (inferior_ptid), |
| 577 | (PTRACE_ARG3_TYPE) addr, 0); |
| 578 | } |
| 579 | |
| 580 | if (count > 1) /* FIXME, avoid if even boundary. */ |
| 581 | { |
| 582 | buffer[count - 1] = |
| 583 | ptrace (PT_READ_I, PIDGET (inferior_ptid), |
| 584 | ((PTRACE_ARG3_TYPE) |
| 585 | (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))), 0); |
| 586 | } |
| 587 | |
| 588 | /* Copy data to be written over corresponding part of buffer. */ |
| 589 | memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), |
| 590 | myaddr, len); |
| 591 | |
| 592 | /* Write the entire buffer. */ |
| 593 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) |
| 594 | { |
| 595 | errno = 0; |
| 596 | ptrace (PT_WRITE_D, PIDGET (inferior_ptid), |
| 597 | (PTRACE_ARG3_TYPE) addr, buffer[i]); |
| 598 | if (errno) |
| 599 | { |
| 600 | /* Using the appropriate one (I or D) is necessary for |
| 601 | Gould NP1, at least. */ |
| 602 | errno = 0; |
| 603 | ptrace (PT_WRITE_I, PIDGET (inferior_ptid), |
| 604 | (PTRACE_ARG3_TYPE) addr, buffer[i]); |
| 605 | } |
| 606 | if (errno) |
| 607 | return 0; |
| 608 | } |
| 609 | } |
| 610 | else |
| 611 | { |
| 612 | /* Read all the longwords. */ |
| 613 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) |
| 614 | { |
| 615 | errno = 0; |
| 616 | buffer[i] = ptrace (PT_READ_I, PIDGET (inferior_ptid), |
| 617 | (PTRACE_ARG3_TYPE) addr, 0); |
| 618 | if (errno) |
| 619 | return 0; |
| 620 | QUIT; |
| 621 | } |
| 622 | |
| 623 | /* Copy appropriate bytes out of the buffer. */ |
| 624 | memcpy (myaddr, |
| 625 | (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), |
| 626 | len); |
| 627 | } |
| 628 | |
| 629 | if (old_chain != NULL) |
| 630 | do_cleanups (old_chain); |
| 631 | return len; |
| 632 | } |
| 633 | \f |
| 634 | |
| 635 | static void |
| 636 | udot_info (char *dummy1, int dummy2) |
| 637 | { |
| 638 | #if defined (KERNEL_U_SIZE) |
| 639 | long udot_off; /* Offset into user struct */ |
| 640 | int udot_val; /* Value from user struct at udot_off */ |
| 641 | char mess[128]; /* For messages */ |
| 642 | #endif |
| 643 | |
| 644 | if (!target_has_execution) |
| 645 | { |
| 646 | error ("The program is not being run."); |
| 647 | } |
| 648 | |
| 649 | #if !defined (KERNEL_U_SIZE) |
| 650 | |
| 651 | /* Adding support for this command is easy. Typically you just add a |
| 652 | routine, called "kernel_u_size" that returns the size of the user |
| 653 | struct, to the appropriate *-nat.c file and then add to the native |
| 654 | config file "#define KERNEL_U_SIZE kernel_u_size()" */ |
| 655 | error ("Don't know how large ``struct user'' is in this version of gdb."); |
| 656 | |
| 657 | #else |
| 658 | |
| 659 | for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val)) |
| 660 | { |
| 661 | if ((udot_off % 24) == 0) |
| 662 | { |
| 663 | if (udot_off > 0) |
| 664 | { |
| 665 | printf_filtered ("\n"); |
| 666 | } |
| 667 | printf_filtered ("%s:", paddr (udot_off)); |
| 668 | } |
| 669 | udot_val = ptrace (PT_READ_U, PIDGET (inferior_ptid), (PTRACE_ARG3_TYPE) udot_off, 0); |
| 670 | if (errno != 0) |
| 671 | { |
| 672 | sprintf (mess, "\nreading user struct at offset 0x%s", |
| 673 | paddr_nz (udot_off)); |
| 674 | perror_with_name (mess); |
| 675 | } |
| 676 | /* Avoid using nonportable (?) "*" in print specs */ |
| 677 | printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val); |
| 678 | } |
| 679 | printf_filtered ("\n"); |
| 680 | |
| 681 | #endif |
| 682 | } |
| 683 | #endif /* !defined (CHILD_XFER_MEMORY). */ |
| 684 | \f |
| 685 | |
| 686 | void |
| 687 | _initialize_infptrace (void) |
| 688 | { |
| 689 | #if !defined (CHILD_XFER_MEMORY) |
| 690 | add_info ("udot", udot_info, |
| 691 | "Print contents of kernel ``struct user'' for current child."); |
| 692 | #endif |
| 693 | } |