1*6881a400Schristos /* IBM RS/6000 native-dependent code for GDB, the GNU debugger. 2*6881a400Schristos 3*6881a400Schristos Copyright (C) 1986-2023 Free Software Foundation, Inc. 4*6881a400Schristos 5*6881a400Schristos This file is part of GDB. 6*6881a400Schristos 7*6881a400Schristos This program is free software; you can redistribute it and/or modify 8*6881a400Schristos it under the terms of the GNU General Public License as published by 9*6881a400Schristos the Free Software Foundation; either version 3 of the License, or 10*6881a400Schristos (at your option) any later version. 11*6881a400Schristos 12*6881a400Schristos This program is distributed in the hope that it will be useful, 13*6881a400Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 14*6881a400Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*6881a400Schristos GNU General Public License for more details. 16*6881a400Schristos 17*6881a400Schristos You should have received a copy of the GNU General Public License 18*6881a400Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19*6881a400Schristos 20*6881a400Schristos #include "defs.h" 21*6881a400Schristos #include "inferior.h" 22*6881a400Schristos #include "target.h" 23*6881a400Schristos #include "gdbcore.h" 24*6881a400Schristos #include "symfile.h" 25*6881a400Schristos #include "objfiles.h" 26*6881a400Schristos #include "bfd.h" 27*6881a400Schristos #include "gdb-stabs.h" 28*6881a400Schristos #include "regcache.h" 29*6881a400Schristos #include "arch-utils.h" 30*6881a400Schristos #include "inf-child.h" 31*6881a400Schristos #include "inf-ptrace.h" 32*6881a400Schristos #include "ppc-tdep.h" 33*6881a400Schristos #include "rs6000-aix-tdep.h" 34*6881a400Schristos #include "exec.h" 35*6881a400Schristos #include "observable.h" 36*6881a400Schristos #include "xcoffread.h" 37*6881a400Schristos 38*6881a400Schristos #include <sys/ptrace.h> 39*6881a400Schristos #include <sys/reg.h> 40*6881a400Schristos 41*6881a400Schristos #include <sys/dir.h> 42*6881a400Schristos #include <sys/user.h> 43*6881a400Schristos #include <signal.h> 44*6881a400Schristos #include <sys/ioctl.h> 45*6881a400Schristos #include <fcntl.h> 46*6881a400Schristos 47*6881a400Schristos #include <a.out.h> 48*6881a400Schristos #include <sys/file.h> 49*6881a400Schristos #include <sys/stat.h> 50*6881a400Schristos #include "gdb_bfd.h" 51*6881a400Schristos #include <sys/core.h> 52*6881a400Schristos #define __LDINFO_PTRACE32__ /* for __ld_info32 */ 53*6881a400Schristos #define __LDINFO_PTRACE64__ /* for __ld_info64 */ 54*6881a400Schristos #include <sys/ldr.h> 55*6881a400Schristos #include <sys/systemcfg.h> 56*6881a400Schristos 57*6881a400Schristos /* Header files for getting ppid in AIX of a child process. */ 58*6881a400Schristos #include <procinfo.h> 59*6881a400Schristos #include <sys/types.h> 60*6881a400Schristos 61*6881a400Schristos /* On AIX4.3+, sys/ldr.h provides different versions of struct ld_info for 62*6881a400Schristos debugging 32-bit and 64-bit processes. Define a typedef and macros for 63*6881a400Schristos accessing fields in the appropriate structures. */ 64*6881a400Schristos 65*6881a400Schristos /* In 32-bit compilation mode (which is the only mode from which ptrace() 66*6881a400Schristos works on 4.3), __ld_info32 is #defined as equivalent to ld_info. */ 67*6881a400Schristos 68*6881a400Schristos #if defined (__ld_info32) || defined (__ld_info64) 69*6881a400Schristos # define ARCH3264 70*6881a400Schristos #endif 71*6881a400Schristos 72*6881a400Schristos /* Return whether the current architecture is 64-bit. */ 73*6881a400Schristos 74*6881a400Schristos #ifndef ARCH3264 75*6881a400Schristos # define ARCH64() 0 76*6881a400Schristos #else 77*6881a400Schristos # define ARCH64() (register_size (target_gdbarch (), 0) == 8) 78*6881a400Schristos #endif 79*6881a400Schristos 80*6881a400Schristos class rs6000_nat_target final : public inf_ptrace_target 81*6881a400Schristos { 82*6881a400Schristos public: 83*6881a400Schristos void fetch_registers (struct regcache *, int) override; 84*6881a400Schristos void store_registers (struct regcache *, int) override; 85*6881a400Schristos 86*6881a400Schristos enum target_xfer_status xfer_partial (enum target_object object, 87*6881a400Schristos const char *annex, 88*6881a400Schristos gdb_byte *readbuf, 89*6881a400Schristos const gdb_byte *writebuf, 90*6881a400Schristos ULONGEST offset, ULONGEST len, 91*6881a400Schristos ULONGEST *xfered_len) override; 92*6881a400Schristos 93*6881a400Schristos void create_inferior (const char *, const std::string &, 94*6881a400Schristos char **, int) override; 95*6881a400Schristos 96*6881a400Schristos ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override; 97*6881a400Schristos 98*6881a400Schristos /* Fork detection related functions, For adding multi process debugging 99*6881a400Schristos support. */ 100*6881a400Schristos void follow_fork (inferior *, ptid_t, target_waitkind, bool, bool) override; 101*6881a400Schristos 102*6881a400Schristos protected: 103*6881a400Schristos 104*6881a400Schristos void post_startup_inferior (ptid_t ptid) override; 105*6881a400Schristos 106*6881a400Schristos private: 107*6881a400Schristos enum target_xfer_status 108*6881a400Schristos xfer_shared_libraries (enum target_object object, 109*6881a400Schristos const char *annex, gdb_byte *readbuf, 110*6881a400Schristos const gdb_byte *writebuf, 111*6881a400Schristos ULONGEST offset, ULONGEST len, 112*6881a400Schristos ULONGEST *xfered_len); 113*6881a400Schristos }; 114*6881a400Schristos 115*6881a400Schristos static rs6000_nat_target the_rs6000_nat_target; 116*6881a400Schristos 117*6881a400Schristos /* The below declaration is to track number of times, parent has 118*6881a400Schristos reported fork event before its children. */ 119*6881a400Schristos 120*6881a400Schristos static std::list<pid_t> aix_pending_parent; 121*6881a400Schristos 122*6881a400Schristos /* The below declaration is for a child process event that 123*6881a400Schristos is reported before its corresponding parent process in 124*6881a400Schristos the event of a fork (). */ 125*6881a400Schristos 126*6881a400Schristos static std::list<pid_t> aix_pending_children; 127*6881a400Schristos 128*6881a400Schristos static void 129*6881a400Schristos aix_remember_child (pid_t pid) 130*6881a400Schristos { 131*6881a400Schristos aix_pending_children.push_front (pid); 132*6881a400Schristos } 133*6881a400Schristos 134*6881a400Schristos static void 135*6881a400Schristos aix_remember_parent (pid_t pid) 136*6881a400Schristos { 137*6881a400Schristos aix_pending_parent.push_front (pid); 138*6881a400Schristos } 139*6881a400Schristos 140*6881a400Schristos /* This function returns a parent of a child process. */ 141*6881a400Schristos 142*6881a400Schristos static pid_t 143*6881a400Schristos find_my_aix_parent (pid_t child_pid) 144*6881a400Schristos { 145*6881a400Schristos struct procsinfo ProcessBuffer1; 146*6881a400Schristos 147*6881a400Schristos if (getprocs (&ProcessBuffer1, sizeof (ProcessBuffer1), 148*6881a400Schristos NULL, 0, &child_pid, 1) != 1) 149*6881a400Schristos return 0; 150*6881a400Schristos else 151*6881a400Schristos return ProcessBuffer1.pi_ppid; 152*6881a400Schristos } 153*6881a400Schristos 154*6881a400Schristos /* In the below function we check if there was any child 155*6881a400Schristos process pending. If it exists we return it from the 156*6881a400Schristos list, otherwise we return a null. */ 157*6881a400Schristos 158*6881a400Schristos static pid_t 159*6881a400Schristos has_my_aix_child_reported (pid_t parent_pid) 160*6881a400Schristos { 161*6881a400Schristos pid_t child = 0; 162*6881a400Schristos auto it = std::find_if (aix_pending_children.begin (), 163*6881a400Schristos aix_pending_children.end (), 164*6881a400Schristos [=] (pid_t child_pid) 165*6881a400Schristos { 166*6881a400Schristos return find_my_aix_parent (child_pid) == parent_pid; 167*6881a400Schristos }); 168*6881a400Schristos if (it != aix_pending_children.end ()) 169*6881a400Schristos { 170*6881a400Schristos child = *it; 171*6881a400Schristos aix_pending_children.erase (it); 172*6881a400Schristos } 173*6881a400Schristos return child; 174*6881a400Schristos } 175*6881a400Schristos 176*6881a400Schristos /* In the below function we check if there was any parent 177*6881a400Schristos process pending. If it exists we return it from the 178*6881a400Schristos list, otherwise we return a null. */ 179*6881a400Schristos 180*6881a400Schristos static pid_t 181*6881a400Schristos has_my_aix_parent_reported (pid_t child_pid) 182*6881a400Schristos { 183*6881a400Schristos pid_t my_parent = find_my_aix_parent (child_pid); 184*6881a400Schristos auto it = std::find (aix_pending_parent.begin (), 185*6881a400Schristos aix_pending_parent.end (), 186*6881a400Schristos my_parent); 187*6881a400Schristos if (it != aix_pending_parent.end ()) 188*6881a400Schristos { 189*6881a400Schristos aix_pending_parent.erase (it); 190*6881a400Schristos return my_parent; 191*6881a400Schristos } 192*6881a400Schristos return 0; 193*6881a400Schristos } 194*6881a400Schristos 195*6881a400Schristos /* Given REGNO, a gdb register number, return the corresponding 196*6881a400Schristos number suitable for use as a ptrace() parameter. Return -1 if 197*6881a400Schristos there's no suitable mapping. Also, set the int pointed to by 198*6881a400Schristos ISFLOAT to indicate whether REGNO is a floating point register. */ 199*6881a400Schristos 200*6881a400Schristos static int 201*6881a400Schristos regmap (struct gdbarch *gdbarch, int regno, int *isfloat) 202*6881a400Schristos { 203*6881a400Schristos ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch); 204*6881a400Schristos 205*6881a400Schristos *isfloat = 0; 206*6881a400Schristos if (tdep->ppc_gp0_regnum <= regno 207*6881a400Schristos && regno < tdep->ppc_gp0_regnum + ppc_num_gprs) 208*6881a400Schristos return regno; 209*6881a400Schristos else if (tdep->ppc_fp0_regnum >= 0 210*6881a400Schristos && tdep->ppc_fp0_regnum <= regno 211*6881a400Schristos && regno < tdep->ppc_fp0_regnum + ppc_num_fprs) 212*6881a400Schristos { 213*6881a400Schristos *isfloat = 1; 214*6881a400Schristos return regno - tdep->ppc_fp0_regnum + FPR0; 215*6881a400Schristos } 216*6881a400Schristos else if (regno == gdbarch_pc_regnum (gdbarch)) 217*6881a400Schristos return IAR; 218*6881a400Schristos else if (regno == tdep->ppc_ps_regnum) 219*6881a400Schristos return MSR; 220*6881a400Schristos else if (regno == tdep->ppc_cr_regnum) 221*6881a400Schristos return CR; 222*6881a400Schristos else if (regno == tdep->ppc_lr_regnum) 223*6881a400Schristos return LR; 224*6881a400Schristos else if (regno == tdep->ppc_ctr_regnum) 225*6881a400Schristos return CTR; 226*6881a400Schristos else if (regno == tdep->ppc_xer_regnum) 227*6881a400Schristos return XER; 228*6881a400Schristos else if (tdep->ppc_fpscr_regnum >= 0 229*6881a400Schristos && regno == tdep->ppc_fpscr_regnum) 230*6881a400Schristos return FPSCR; 231*6881a400Schristos else if (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum) 232*6881a400Schristos return MQ; 233*6881a400Schristos else 234*6881a400Schristos return -1; 235*6881a400Schristos } 236*6881a400Schristos 237*6881a400Schristos /* Call ptrace(REQ, ID, ADDR, DATA, BUF). */ 238*6881a400Schristos 239*6881a400Schristos static int 240*6881a400Schristos rs6000_ptrace32 (int req, int id, int *addr, int data, int *buf) 241*6881a400Schristos { 242*6881a400Schristos #ifdef HAVE_PTRACE64 243*6881a400Schristos int ret = ptrace64 (req, id, (uintptr_t) addr, data, buf); 244*6881a400Schristos #else 245*6881a400Schristos int ret = ptrace (req, id, (int *)addr, data, buf); 246*6881a400Schristos #endif 247*6881a400Schristos #if 0 248*6881a400Schristos printf ("rs6000_ptrace32 (%d, %d, 0x%x, %08x, 0x%x) = 0x%x\n", 249*6881a400Schristos req, id, (unsigned int)addr, data, (unsigned int)buf, ret); 250*6881a400Schristos #endif 251*6881a400Schristos return ret; 252*6881a400Schristos } 253*6881a400Schristos 254*6881a400Schristos /* Call ptracex(REQ, ID, ADDR, DATA, BUF). */ 255*6881a400Schristos 256*6881a400Schristos static int 257*6881a400Schristos rs6000_ptrace64 (int req, int id, long long addr, int data, void *buf) 258*6881a400Schristos { 259*6881a400Schristos #ifdef ARCH3264 260*6881a400Schristos # ifdef HAVE_PTRACE64 261*6881a400Schristos int ret = ptrace64 (req, id, addr, data, (PTRACE_TYPE_ARG5) buf); 262*6881a400Schristos # else 263*6881a400Schristos int ret = ptracex (req, id, addr, data, (PTRACE_TYPE_ARG5) buf); 264*6881a400Schristos # endif 265*6881a400Schristos #else 266*6881a400Schristos int ret = 0; 267*6881a400Schristos #endif 268*6881a400Schristos #if 0 269*6881a400Schristos printf ("rs6000_ptrace64 (%d, %d, %s, %08x, 0x%x) = 0x%x\n", 270*6881a400Schristos req, id, hex_string (addr), data, (unsigned int)buf, ret); 271*6881a400Schristos #endif 272*6881a400Schristos return ret; 273*6881a400Schristos } 274*6881a400Schristos 275*6881a400Schristos void rs6000_nat_target::post_startup_inferior (ptid_t ptid) 276*6881a400Schristos { 277*6881a400Schristos 278*6881a400Schristos /* In AIX to turn on multi process debugging in ptrace 279*6881a400Schristos PT_MULTI is the option to be passed, 280*6881a400Schristos with the process ID which can fork () and 281*6881a400Schristos the data parameter [fourth parameter] must be 1. */ 282*6881a400Schristos 283*6881a400Schristos if (!ARCH64 ()) 284*6881a400Schristos rs6000_ptrace32 (PT_MULTI, ptid.pid(), 0, 1, 0); 285*6881a400Schristos else 286*6881a400Schristos rs6000_ptrace64 (PT_MULTI, ptid.pid(), 0, 1, 0); 287*6881a400Schristos } 288*6881a400Schristos 289*6881a400Schristos void 290*6881a400Schristos rs6000_nat_target::follow_fork (inferior *child_inf, ptid_t child_ptid, 291*6881a400Schristos target_waitkind fork_kind, bool follow_child, 292*6881a400Schristos bool detach_fork) 293*6881a400Schristos { 294*6881a400Schristos 295*6881a400Schristos /* Once the fork event is detected the infrun.c code 296*6881a400Schristos calls the target_follow_fork to take care of 297*6881a400Schristos follow child and detach the child activity which is 298*6881a400Schristos done using the function below. */ 299*6881a400Schristos 300*6881a400Schristos inf_ptrace_target::follow_fork (child_inf, child_ptid, fork_kind, 301*6881a400Schristos follow_child, detach_fork); 302*6881a400Schristos 303*6881a400Schristos /* If we detach fork and follow child we do not want the child 304*6881a400Schristos process to geneate events that ptrace can trace. Hence we 305*6881a400Schristos detach it. */ 306*6881a400Schristos 307*6881a400Schristos if (detach_fork && !follow_child) 308*6881a400Schristos { 309*6881a400Schristos if (ARCH64 ()) 310*6881a400Schristos rs6000_ptrace64 (PT_DETACH, child_ptid.pid (), 0, 0, 0); 311*6881a400Schristos else 312*6881a400Schristos rs6000_ptrace32 (PT_DETACH, child_ptid.pid (), 0, 0, 0); 313*6881a400Schristos } 314*6881a400Schristos } 315*6881a400Schristos 316*6881a400Schristos /* Fetch register REGNO from the inferior. */ 317*6881a400Schristos 318*6881a400Schristos static void 319*6881a400Schristos fetch_register (struct regcache *regcache, int regno) 320*6881a400Schristos { 321*6881a400Schristos struct gdbarch *gdbarch = regcache->arch (); 322*6881a400Schristos int addr[PPC_MAX_REGISTER_SIZE]; 323*6881a400Schristos int nr, isfloat; 324*6881a400Schristos pid_t pid = regcache->ptid ().pid (); 325*6881a400Schristos 326*6881a400Schristos /* Retrieved values may be -1, so infer errors from errno. */ 327*6881a400Schristos errno = 0; 328*6881a400Schristos 329*6881a400Schristos nr = regmap (gdbarch, regno, &isfloat); 330*6881a400Schristos 331*6881a400Schristos /* Floating-point registers. */ 332*6881a400Schristos if (isfloat) 333*6881a400Schristos rs6000_ptrace32 (PT_READ_FPR, pid, addr, nr, 0); 334*6881a400Schristos 335*6881a400Schristos /* Bogus register number. */ 336*6881a400Schristos else if (nr < 0) 337*6881a400Schristos { 338*6881a400Schristos if (regno >= gdbarch_num_regs (gdbarch)) 339*6881a400Schristos gdb_printf (gdb_stderr, 340*6881a400Schristos "gdb error: register no %d not implemented.\n", 341*6881a400Schristos regno); 342*6881a400Schristos return; 343*6881a400Schristos } 344*6881a400Schristos 345*6881a400Schristos /* Fixed-point registers. */ 346*6881a400Schristos else 347*6881a400Schristos { 348*6881a400Schristos if (!ARCH64 ()) 349*6881a400Schristos *addr = rs6000_ptrace32 (PT_READ_GPR, pid, (int *) nr, 0, 0); 350*6881a400Schristos else 351*6881a400Schristos { 352*6881a400Schristos /* PT_READ_GPR requires the buffer parameter to point to long long, 353*6881a400Schristos even if the register is really only 32 bits. */ 354*6881a400Schristos long long buf; 355*6881a400Schristos rs6000_ptrace64 (PT_READ_GPR, pid, nr, 0, &buf); 356*6881a400Schristos if (register_size (gdbarch, regno) == 8) 357*6881a400Schristos memcpy (addr, &buf, 8); 358*6881a400Schristos else 359*6881a400Schristos *addr = buf; 360*6881a400Schristos } 361*6881a400Schristos } 362*6881a400Schristos 363*6881a400Schristos if (!errno) 364*6881a400Schristos regcache->raw_supply (regno, (char *) addr); 365*6881a400Schristos else 366*6881a400Schristos { 367*6881a400Schristos #if 0 368*6881a400Schristos /* FIXME: this happens 3 times at the start of each 64-bit program. */ 369*6881a400Schristos perror (_("ptrace read")); 370*6881a400Schristos #endif 371*6881a400Schristos errno = 0; 372*6881a400Schristos } 373*6881a400Schristos } 374*6881a400Schristos 375*6881a400Schristos /* Store register REGNO back into the inferior. */ 376*6881a400Schristos 377*6881a400Schristos static void 378*6881a400Schristos store_register (struct regcache *regcache, int regno) 379*6881a400Schristos { 380*6881a400Schristos struct gdbarch *gdbarch = regcache->arch (); 381*6881a400Schristos int addr[PPC_MAX_REGISTER_SIZE]; 382*6881a400Schristos int nr, isfloat; 383*6881a400Schristos pid_t pid = regcache->ptid ().pid (); 384*6881a400Schristos 385*6881a400Schristos /* Fetch the register's value from the register cache. */ 386*6881a400Schristos regcache->raw_collect (regno, addr); 387*6881a400Schristos 388*6881a400Schristos /* -1 can be a successful return value, so infer errors from errno. */ 389*6881a400Schristos errno = 0; 390*6881a400Schristos 391*6881a400Schristos nr = regmap (gdbarch, regno, &isfloat); 392*6881a400Schristos 393*6881a400Schristos /* Floating-point registers. */ 394*6881a400Schristos if (isfloat) 395*6881a400Schristos rs6000_ptrace32 (PT_WRITE_FPR, pid, addr, nr, 0); 396*6881a400Schristos 397*6881a400Schristos /* Bogus register number. */ 398*6881a400Schristos else if (nr < 0) 399*6881a400Schristos { 400*6881a400Schristos if (regno >= gdbarch_num_regs (gdbarch)) 401*6881a400Schristos gdb_printf (gdb_stderr, 402*6881a400Schristos "gdb error: register no %d not implemented.\n", 403*6881a400Schristos regno); 404*6881a400Schristos } 405*6881a400Schristos 406*6881a400Schristos /* Fixed-point registers. */ 407*6881a400Schristos else 408*6881a400Schristos { 409*6881a400Schristos /* The PT_WRITE_GPR operation is rather odd. For 32-bit inferiors, 410*6881a400Schristos the register's value is passed by value, but for 64-bit inferiors, 411*6881a400Schristos the address of a buffer containing the value is passed. */ 412*6881a400Schristos if (!ARCH64 ()) 413*6881a400Schristos rs6000_ptrace32 (PT_WRITE_GPR, pid, (int *) nr, *addr, 0); 414*6881a400Schristos else 415*6881a400Schristos { 416*6881a400Schristos /* PT_WRITE_GPR requires the buffer parameter to point to an 8-byte 417*6881a400Schristos area, even if the register is really only 32 bits. */ 418*6881a400Schristos long long buf; 419*6881a400Schristos if (register_size (gdbarch, regno) == 8) 420*6881a400Schristos memcpy (&buf, addr, 8); 421*6881a400Schristos else 422*6881a400Schristos buf = *addr; 423*6881a400Schristos rs6000_ptrace64 (PT_WRITE_GPR, pid, nr, 0, &buf); 424*6881a400Schristos } 425*6881a400Schristos } 426*6881a400Schristos 427*6881a400Schristos if (errno) 428*6881a400Schristos { 429*6881a400Schristos perror (_("ptrace write")); 430*6881a400Schristos errno = 0; 431*6881a400Schristos } 432*6881a400Schristos } 433*6881a400Schristos 434*6881a400Schristos /* Read from the inferior all registers if REGNO == -1 and just register 435*6881a400Schristos REGNO otherwise. */ 436*6881a400Schristos 437*6881a400Schristos void 438*6881a400Schristos rs6000_nat_target::fetch_registers (struct regcache *regcache, int regno) 439*6881a400Schristos { 440*6881a400Schristos struct gdbarch *gdbarch = regcache->arch (); 441*6881a400Schristos if (regno != -1) 442*6881a400Schristos fetch_register (regcache, regno); 443*6881a400Schristos 444*6881a400Schristos else 445*6881a400Schristos { 446*6881a400Schristos ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch); 447*6881a400Schristos 448*6881a400Schristos /* Read 32 general purpose registers. */ 449*6881a400Schristos for (regno = tdep->ppc_gp0_regnum; 450*6881a400Schristos regno < tdep->ppc_gp0_regnum + ppc_num_gprs; 451*6881a400Schristos regno++) 452*6881a400Schristos { 453*6881a400Schristos fetch_register (regcache, regno); 454*6881a400Schristos } 455*6881a400Schristos 456*6881a400Schristos /* Read general purpose floating point registers. */ 457*6881a400Schristos if (tdep->ppc_fp0_regnum >= 0) 458*6881a400Schristos for (regno = 0; regno < ppc_num_fprs; regno++) 459*6881a400Schristos fetch_register (regcache, tdep->ppc_fp0_regnum + regno); 460*6881a400Schristos 461*6881a400Schristos /* Read special registers. */ 462*6881a400Schristos fetch_register (regcache, gdbarch_pc_regnum (gdbarch)); 463*6881a400Schristos fetch_register (regcache, tdep->ppc_ps_regnum); 464*6881a400Schristos fetch_register (regcache, tdep->ppc_cr_regnum); 465*6881a400Schristos fetch_register (regcache, tdep->ppc_lr_regnum); 466*6881a400Schristos fetch_register (regcache, tdep->ppc_ctr_regnum); 467*6881a400Schristos fetch_register (regcache, tdep->ppc_xer_regnum); 468*6881a400Schristos if (tdep->ppc_fpscr_regnum >= 0) 469*6881a400Schristos fetch_register (regcache, tdep->ppc_fpscr_regnum); 470*6881a400Schristos if (tdep->ppc_mq_regnum >= 0) 471*6881a400Schristos fetch_register (regcache, tdep->ppc_mq_regnum); 472*6881a400Schristos } 473*6881a400Schristos } 474*6881a400Schristos 475*6881a400Schristos /* Store our register values back into the inferior. 476*6881a400Schristos If REGNO is -1, do this for all registers. 477*6881a400Schristos Otherwise, REGNO specifies which register (so we can save time). */ 478*6881a400Schristos 479*6881a400Schristos void 480*6881a400Schristos rs6000_nat_target::store_registers (struct regcache *regcache, int regno) 481*6881a400Schristos { 482*6881a400Schristos struct gdbarch *gdbarch = regcache->arch (); 483*6881a400Schristos if (regno != -1) 484*6881a400Schristos store_register (regcache, regno); 485*6881a400Schristos 486*6881a400Schristos else 487*6881a400Schristos { 488*6881a400Schristos ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch); 489*6881a400Schristos 490*6881a400Schristos /* Write general purpose registers first. */ 491*6881a400Schristos for (regno = tdep->ppc_gp0_regnum; 492*6881a400Schristos regno < tdep->ppc_gp0_regnum + ppc_num_gprs; 493*6881a400Schristos regno++) 494*6881a400Schristos { 495*6881a400Schristos store_register (regcache, regno); 496*6881a400Schristos } 497*6881a400Schristos 498*6881a400Schristos /* Write floating point registers. */ 499*6881a400Schristos if (tdep->ppc_fp0_regnum >= 0) 500*6881a400Schristos for (regno = 0; regno < ppc_num_fprs; regno++) 501*6881a400Schristos store_register (regcache, tdep->ppc_fp0_regnum + regno); 502*6881a400Schristos 503*6881a400Schristos /* Write special registers. */ 504*6881a400Schristos store_register (regcache, gdbarch_pc_regnum (gdbarch)); 505*6881a400Schristos store_register (regcache, tdep->ppc_ps_regnum); 506*6881a400Schristos store_register (regcache, tdep->ppc_cr_regnum); 507*6881a400Schristos store_register (regcache, tdep->ppc_lr_regnum); 508*6881a400Schristos store_register (regcache, tdep->ppc_ctr_regnum); 509*6881a400Schristos store_register (regcache, tdep->ppc_xer_regnum); 510*6881a400Schristos if (tdep->ppc_fpscr_regnum >= 0) 511*6881a400Schristos store_register (regcache, tdep->ppc_fpscr_regnum); 512*6881a400Schristos if (tdep->ppc_mq_regnum >= 0) 513*6881a400Schristos store_register (regcache, tdep->ppc_mq_regnum); 514*6881a400Schristos } 515*6881a400Schristos } 516*6881a400Schristos 517*6881a400Schristos /* Implement the to_xfer_partial target_ops method. */ 518*6881a400Schristos 519*6881a400Schristos enum target_xfer_status 520*6881a400Schristos rs6000_nat_target::xfer_partial (enum target_object object, 521*6881a400Schristos const char *annex, gdb_byte *readbuf, 522*6881a400Schristos const gdb_byte *writebuf, 523*6881a400Schristos ULONGEST offset, ULONGEST len, 524*6881a400Schristos ULONGEST *xfered_len) 525*6881a400Schristos { 526*6881a400Schristos pid_t pid = inferior_ptid.pid (); 527*6881a400Schristos int arch64 = ARCH64 (); 528*6881a400Schristos 529*6881a400Schristos switch (object) 530*6881a400Schristos { 531*6881a400Schristos case TARGET_OBJECT_LIBRARIES_AIX: 532*6881a400Schristos return xfer_shared_libraries (object, annex, 533*6881a400Schristos readbuf, writebuf, 534*6881a400Schristos offset, len, xfered_len); 535*6881a400Schristos case TARGET_OBJECT_MEMORY: 536*6881a400Schristos { 537*6881a400Schristos union 538*6881a400Schristos { 539*6881a400Schristos PTRACE_TYPE_RET word; 540*6881a400Schristos gdb_byte byte[sizeof (PTRACE_TYPE_RET)]; 541*6881a400Schristos } buffer; 542*6881a400Schristos ULONGEST rounded_offset; 543*6881a400Schristos LONGEST partial_len; 544*6881a400Schristos 545*6881a400Schristos /* Round the start offset down to the next long word 546*6881a400Schristos boundary. */ 547*6881a400Schristos rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET); 548*6881a400Schristos 549*6881a400Schristos /* Since ptrace will transfer a single word starting at that 550*6881a400Schristos rounded_offset the partial_len needs to be adjusted down to 551*6881a400Schristos that (remember this function only does a single transfer). 552*6881a400Schristos Should the required length be even less, adjust it down 553*6881a400Schristos again. */ 554*6881a400Schristos partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset; 555*6881a400Schristos if (partial_len > len) 556*6881a400Schristos partial_len = len; 557*6881a400Schristos 558*6881a400Schristos if (writebuf) 559*6881a400Schristos { 560*6881a400Schristos /* If OFFSET:PARTIAL_LEN is smaller than 561*6881a400Schristos ROUNDED_OFFSET:WORDSIZE then a read/modify write will 562*6881a400Schristos be needed. Read in the entire word. */ 563*6881a400Schristos if (rounded_offset < offset 564*6881a400Schristos || (offset + partial_len 565*6881a400Schristos < rounded_offset + sizeof (PTRACE_TYPE_RET))) 566*6881a400Schristos { 567*6881a400Schristos /* Need part of initial word -- fetch it. */ 568*6881a400Schristos if (arch64) 569*6881a400Schristos buffer.word = rs6000_ptrace64 (PT_READ_I, pid, 570*6881a400Schristos rounded_offset, 0, NULL); 571*6881a400Schristos else 572*6881a400Schristos buffer.word = rs6000_ptrace32 (PT_READ_I, pid, 573*6881a400Schristos (int *) (uintptr_t) 574*6881a400Schristos rounded_offset, 575*6881a400Schristos 0, NULL); 576*6881a400Schristos } 577*6881a400Schristos 578*6881a400Schristos /* Copy data to be written over corresponding part of 579*6881a400Schristos buffer. */ 580*6881a400Schristos memcpy (buffer.byte + (offset - rounded_offset), 581*6881a400Schristos writebuf, partial_len); 582*6881a400Schristos 583*6881a400Schristos errno = 0; 584*6881a400Schristos if (arch64) 585*6881a400Schristos rs6000_ptrace64 (PT_WRITE_D, pid, 586*6881a400Schristos rounded_offset, buffer.word, NULL); 587*6881a400Schristos else 588*6881a400Schristos rs6000_ptrace32 (PT_WRITE_D, pid, 589*6881a400Schristos (int *) (uintptr_t) rounded_offset, 590*6881a400Schristos buffer.word, NULL); 591*6881a400Schristos if (errno) 592*6881a400Schristos return TARGET_XFER_EOF; 593*6881a400Schristos } 594*6881a400Schristos 595*6881a400Schristos if (readbuf) 596*6881a400Schristos { 597*6881a400Schristos errno = 0; 598*6881a400Schristos if (arch64) 599*6881a400Schristos buffer.word = rs6000_ptrace64 (PT_READ_I, pid, 600*6881a400Schristos rounded_offset, 0, NULL); 601*6881a400Schristos else 602*6881a400Schristos buffer.word = rs6000_ptrace32 (PT_READ_I, pid, 603*6881a400Schristos (int *)(uintptr_t)rounded_offset, 604*6881a400Schristos 0, NULL); 605*6881a400Schristos if (errno) 606*6881a400Schristos return TARGET_XFER_EOF; 607*6881a400Schristos 608*6881a400Schristos /* Copy appropriate bytes out of the buffer. */ 609*6881a400Schristos memcpy (readbuf, buffer.byte + (offset - rounded_offset), 610*6881a400Schristos partial_len); 611*6881a400Schristos } 612*6881a400Schristos 613*6881a400Schristos *xfered_len = (ULONGEST) partial_len; 614*6881a400Schristos return TARGET_XFER_OK; 615*6881a400Schristos } 616*6881a400Schristos 617*6881a400Schristos default: 618*6881a400Schristos return TARGET_XFER_E_IO; 619*6881a400Schristos } 620*6881a400Schristos } 621*6881a400Schristos 622*6881a400Schristos /* Wait for the child specified by PTID to do something. Return the 623*6881a400Schristos process ID of the child, or MINUS_ONE_PTID in case of error; store 624*6881a400Schristos the status in *OURSTATUS. */ 625*6881a400Schristos 626*6881a400Schristos ptid_t 627*6881a400Schristos rs6000_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus, 628*6881a400Schristos target_wait_flags options) 629*6881a400Schristos { 630*6881a400Schristos pid_t pid; 631*6881a400Schristos int status, save_errno; 632*6881a400Schristos 633*6881a400Schristos while (1) 634*6881a400Schristos { 635*6881a400Schristos set_sigint_trap (); 636*6881a400Schristos 637*6881a400Schristos do 638*6881a400Schristos { 639*6881a400Schristos pid = waitpid (ptid.pid (), &status, 0); 640*6881a400Schristos save_errno = errno; 641*6881a400Schristos } 642*6881a400Schristos while (pid == -1 && errno == EINTR); 643*6881a400Schristos 644*6881a400Schristos clear_sigint_trap (); 645*6881a400Schristos 646*6881a400Schristos if (pid == -1) 647*6881a400Schristos { 648*6881a400Schristos gdb_printf (gdb_stderr, 649*6881a400Schristos _("Child process unexpectedly missing: %s.\n"), 650*6881a400Schristos safe_strerror (save_errno)); 651*6881a400Schristos 652*6881a400Schristos ourstatus->set_ignore (); 653*6881a400Schristos return minus_one_ptid; 654*6881a400Schristos } 655*6881a400Schristos 656*6881a400Schristos /* Ignore terminated detached child processes. */ 657*6881a400Schristos if (!WIFSTOPPED (status) && find_inferior_pid (this, pid) == nullptr) 658*6881a400Schristos continue; 659*6881a400Schristos 660*6881a400Schristos /* Check for a fork () event. */ 661*6881a400Schristos if ((status & 0xff) == W_SFWTED) 662*6881a400Schristos { 663*6881a400Schristos /* Checking whether it is a parent or a child event. */ 664*6881a400Schristos 665*6881a400Schristos /* If the event is a child we check if there was a parent 666*6881a400Schristos event recorded before. If yes we got the parent child 667*6881a400Schristos relationship. If not we push this child and wait for 668*6881a400Schristos the next fork () event. */ 669*6881a400Schristos if (find_inferior_pid (this, pid) == nullptr) 670*6881a400Schristos { 671*6881a400Schristos pid_t parent_pid = has_my_aix_parent_reported (pid); 672*6881a400Schristos if (parent_pid > 0) 673*6881a400Schristos { 674*6881a400Schristos ourstatus->set_forked (ptid_t (pid)); 675*6881a400Schristos return ptid_t (parent_pid); 676*6881a400Schristos } 677*6881a400Schristos aix_remember_child (pid); 678*6881a400Schristos } 679*6881a400Schristos 680*6881a400Schristos /* If the event is a parent we check if there was a child 681*6881a400Schristos event recorded before. If yes we got the parent child 682*6881a400Schristos relationship. If not we push this parent and wait for 683*6881a400Schristos the next fork () event. */ 684*6881a400Schristos else 685*6881a400Schristos { 686*6881a400Schristos pid_t child_pid = has_my_aix_child_reported (pid); 687*6881a400Schristos if (child_pid > 0) 688*6881a400Schristos { 689*6881a400Schristos ourstatus->set_forked (ptid_t (child_pid)); 690*6881a400Schristos return ptid_t (pid); 691*6881a400Schristos } 692*6881a400Schristos aix_remember_parent (pid); 693*6881a400Schristos } 694*6881a400Schristos continue; 695*6881a400Schristos } 696*6881a400Schristos 697*6881a400Schristos break; 698*6881a400Schristos } 699*6881a400Schristos 700*6881a400Schristos /* AIX has a couple of strange returns from wait(). */ 701*6881a400Schristos 702*6881a400Schristos /* stop after load" status. */ 703*6881a400Schristos if (status == 0x57c) 704*6881a400Schristos ourstatus->set_loaded (); 705*6881a400Schristos /* 0x7f is signal 0. 0x17f and 0x137f are status returned 706*6881a400Schristos if we follow parent, a switch is made to a child post parent 707*6881a400Schristos execution and child continues its execution [user switches 708*6881a400Schristos to child and presses continue]. */ 709*6881a400Schristos else if (status == 0x7f || status == 0x17f || status == 0x137f) 710*6881a400Schristos ourstatus->set_spurious (); 711*6881a400Schristos /* A normal waitstatus. Let the usual macros deal with it. */ 712*6881a400Schristos else 713*6881a400Schristos *ourstatus = host_status_to_waitstatus (status); 714*6881a400Schristos 715*6881a400Schristos return ptid_t (pid); 716*6881a400Schristos } 717*6881a400Schristos 718*6881a400Schristos 719*6881a400Schristos /* Set the current architecture from the host running GDB. Called when 720*6881a400Schristos starting a child process. */ 721*6881a400Schristos 722*6881a400Schristos void 723*6881a400Schristos rs6000_nat_target::create_inferior (const char *exec_file, 724*6881a400Schristos const std::string &allargs, 725*6881a400Schristos char **env, int from_tty) 726*6881a400Schristos { 727*6881a400Schristos enum bfd_architecture arch; 728*6881a400Schristos unsigned long mach; 729*6881a400Schristos bfd abfd; 730*6881a400Schristos 731*6881a400Schristos inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty); 732*6881a400Schristos 733*6881a400Schristos if (__power_rs ()) 734*6881a400Schristos { 735*6881a400Schristos arch = bfd_arch_rs6000; 736*6881a400Schristos mach = bfd_mach_rs6k; 737*6881a400Schristos } 738*6881a400Schristos else 739*6881a400Schristos { 740*6881a400Schristos arch = bfd_arch_powerpc; 741*6881a400Schristos mach = bfd_mach_ppc; 742*6881a400Schristos } 743*6881a400Schristos 744*6881a400Schristos /* FIXME: schauer/2002-02-25: 745*6881a400Schristos We don't know if we are executing a 32 or 64 bit executable, 746*6881a400Schristos and have no way to pass the proper word size to rs6000_gdbarch_init. 747*6881a400Schristos So we have to avoid switching to a new architecture, if the architecture 748*6881a400Schristos matches already. 749*6881a400Schristos Blindly calling rs6000_gdbarch_init used to work in older versions of 750*6881a400Schristos GDB, as rs6000_gdbarch_init incorrectly used the previous tdep to 751*6881a400Schristos determine the wordsize. */ 752*6881a400Schristos if (current_program_space->exec_bfd ()) 753*6881a400Schristos { 754*6881a400Schristos const struct bfd_arch_info *exec_bfd_arch_info; 755*6881a400Schristos 756*6881a400Schristos exec_bfd_arch_info 757*6881a400Schristos = bfd_get_arch_info (current_program_space->exec_bfd ()); 758*6881a400Schristos if (arch == exec_bfd_arch_info->arch) 759*6881a400Schristos return; 760*6881a400Schristos } 761*6881a400Schristos 762*6881a400Schristos bfd_default_set_arch_mach (&abfd, arch, mach); 763*6881a400Schristos 764*6881a400Schristos gdbarch_info info; 765*6881a400Schristos info.bfd_arch_info = bfd_get_arch_info (&abfd); 766*6881a400Schristos info.abfd = current_program_space->exec_bfd (); 767*6881a400Schristos 768*6881a400Schristos if (!gdbarch_update_p (info)) 769*6881a400Schristos internal_error (_("rs6000_create_inferior: failed " 770*6881a400Schristos "to select architecture")); 771*6881a400Schristos } 772*6881a400Schristos 773*6881a400Schristos 774*6881a400Schristos /* Shared Object support. */ 775*6881a400Schristos 776*6881a400Schristos /* Return the LdInfo data for the given process. Raises an error 777*6881a400Schristos if the data could not be obtained. */ 778*6881a400Schristos 779*6881a400Schristos static gdb::byte_vector 780*6881a400Schristos rs6000_ptrace_ldinfo (ptid_t ptid) 781*6881a400Schristos { 782*6881a400Schristos const int pid = ptid.pid (); 783*6881a400Schristos gdb::byte_vector ldi (1024); 784*6881a400Schristos int rc = -1; 785*6881a400Schristos 786*6881a400Schristos while (1) 787*6881a400Schristos { 788*6881a400Schristos if (ARCH64 ()) 789*6881a400Schristos rc = rs6000_ptrace64 (PT_LDINFO, pid, (unsigned long) ldi.data (), 790*6881a400Schristos ldi.size (), NULL); 791*6881a400Schristos else 792*6881a400Schristos rc = rs6000_ptrace32 (PT_LDINFO, pid, (int *) ldi.data (), 793*6881a400Schristos ldi.size (), NULL); 794*6881a400Schristos 795*6881a400Schristos if (rc != -1) 796*6881a400Schristos break; /* Success, we got the entire ld_info data. */ 797*6881a400Schristos 798*6881a400Schristos if (errno != ENOMEM) 799*6881a400Schristos perror_with_name (_("ptrace ldinfo")); 800*6881a400Schristos 801*6881a400Schristos /* ldi is not big enough. Double it and try again. */ 802*6881a400Schristos ldi.resize (ldi.size () * 2); 803*6881a400Schristos } 804*6881a400Schristos 805*6881a400Schristos return ldi; 806*6881a400Schristos } 807*6881a400Schristos 808*6881a400Schristos /* Implement the to_xfer_partial target_ops method for 809*6881a400Schristos TARGET_OBJECT_LIBRARIES_AIX objects. */ 810*6881a400Schristos 811*6881a400Schristos enum target_xfer_status 812*6881a400Schristos rs6000_nat_target::xfer_shared_libraries 813*6881a400Schristos (enum target_object object, 814*6881a400Schristos const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf, 815*6881a400Schristos ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) 816*6881a400Schristos { 817*6881a400Schristos ULONGEST result; 818*6881a400Schristos 819*6881a400Schristos /* This function assumes that it is being run with a live process. 820*6881a400Schristos Core files are handled via gdbarch. */ 821*6881a400Schristos gdb_assert (target_has_execution ()); 822*6881a400Schristos 823*6881a400Schristos if (writebuf) 824*6881a400Schristos return TARGET_XFER_E_IO; 825*6881a400Schristos 826*6881a400Schristos gdb::byte_vector ldi_buf = rs6000_ptrace_ldinfo (inferior_ptid); 827*6881a400Schristos result = rs6000_aix_ld_info_to_xml (target_gdbarch (), ldi_buf.data (), 828*6881a400Schristos readbuf, offset, len, 1); 829*6881a400Schristos 830*6881a400Schristos if (result == 0) 831*6881a400Schristos return TARGET_XFER_EOF; 832*6881a400Schristos else 833*6881a400Schristos { 834*6881a400Schristos *xfered_len = result; 835*6881a400Schristos return TARGET_XFER_OK; 836*6881a400Schristos } 837*6881a400Schristos } 838*6881a400Schristos 839*6881a400Schristos void _initialize_rs6000_nat (); 840*6881a400Schristos void 841*6881a400Schristos _initialize_rs6000_nat () 842*6881a400Schristos { 843*6881a400Schristos add_inf_child_target (&the_rs6000_nat_target); 844*6881a400Schristos } 845