amd64-bsd-nat.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Native-dependent code for AMD64 BSD's.
  2. Copyright (C) 2003-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "inferior.h"
  16. #include "regcache.h"
  17. #include "target.h"
  18. /* We include <signal.h> to make sure `struct fxsave64' is defined on
  19. NetBSD, since NetBSD's <machine/reg.h> needs it. */
  20. #include <signal.h>
  21. #include <sys/types.h>
  22. #include <sys/ptrace.h>
  23. #include <machine/reg.h>
  24. #include "amd64-tdep.h"
  25. #include "amd64-nat.h"
  26. #include "x86-bsd-nat.h"
  27. #include "inf-ptrace.h"
  28. #include "amd64-bsd-nat.h"
  29. static PTRACE_TYPE_RET
  30. gdb_ptrace (PTRACE_TYPE_ARG1 request, ptid_t ptid, PTRACE_TYPE_ARG3 addr,
  31. PTRACE_TYPE_ARG4 data)
  32. {
  33. #ifdef __NetBSD__
  34. gdb_assert (data == 0);
  35. /* Support for NetBSD threads: unlike other ptrace implementations in this
  36. file, NetBSD requires that we pass both the pid and lwp. */
  37. return ptrace (request, ptid.pid (), addr, ptid.lwp ());
  38. #else
  39. pid_t pid = get_ptrace_pid (ptid);
  40. return ptrace (request, pid, addr, data);
  41. #endif
  42. }
  43. /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
  44. for all registers (including the floating-point registers). */
  45. void
  46. amd64bsd_fetch_inferior_registers (struct regcache *regcache, int regnum)
  47. {
  48. struct gdbarch *gdbarch = regcache->arch ();
  49. ptid_t ptid = regcache->ptid ();
  50. if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
  51. {
  52. struct reg regs;
  53. if (gdb_ptrace (PT_GETREGS, ptid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
  54. perror_with_name (_("Couldn't get registers"));
  55. amd64_supply_native_gregset (regcache, &regs, -1);
  56. if (regnum != -1)
  57. return;
  58. }
  59. if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
  60. {
  61. struct fpreg fpregs;
  62. if (gdb_ptrace (PT_GETFPREGS, ptid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  63. perror_with_name (_("Couldn't get floating point status"));
  64. amd64_supply_fxsave (regcache, -1, &fpregs);
  65. }
  66. }
  67. /* Store register REGNUM back into the inferior. If REGNUM is -1, do
  68. this for all registers (including the floating-point registers). */
  69. void
  70. amd64bsd_store_inferior_registers (struct regcache *regcache, int regnum)
  71. {
  72. struct gdbarch *gdbarch = regcache->arch ();
  73. ptid_t ptid = regcache->ptid ();
  74. if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
  75. {
  76. struct reg regs;
  77. if (gdb_ptrace (PT_GETREGS, ptid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
  78. perror_with_name (_("Couldn't get registers"));
  79. amd64_collect_native_gregset (regcache, &regs, regnum);
  80. if (gdb_ptrace (PT_SETREGS, ptid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
  81. perror_with_name (_("Couldn't write registers"));
  82. if (regnum != -1)
  83. return;
  84. }
  85. if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
  86. {
  87. struct fpreg fpregs;
  88. if (gdb_ptrace (PT_GETFPREGS, ptid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  89. perror_with_name (_("Couldn't get floating point status"));
  90. amd64_collect_fxsave (regcache, regnum, &fpregs);
  91. if (gdb_ptrace (PT_SETFPREGS, ptid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
  92. perror_with_name (_("Couldn't write floating point status"));
  93. }
  94. }