linux-nat-trad.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Generic GNU/Linux target using traditional ptrace register access.
  2. Copyright (C) 1988-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 "linux-nat-trad.h"
  16. #include "nat/gdb_ptrace.h"
  17. #include "inf-ptrace.h"
  18. #include "gdbarch.h"
  19. /* Fetch register REGNUM from the inferior. */
  20. void
  21. linux_nat_trad_target::fetch_register (struct regcache *regcache, int regnum)
  22. {
  23. struct gdbarch *gdbarch = regcache->arch ();
  24. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  25. CORE_ADDR addr;
  26. gdb_byte *buf;
  27. size_t size;
  28. pid_t pid;
  29. int i;
  30. /* This isn't really an address, but ptrace thinks of it as one. */
  31. addr = register_u_offset (gdbarch, regnum, 0);
  32. if (addr == (CORE_ADDR)-1
  33. || gdbarch_cannot_fetch_register (gdbarch, regnum))
  34. {
  35. regcache->raw_supply (regnum, NULL);
  36. return;
  37. }
  38. pid = get_ptrace_pid (regcache->ptid ());
  39. size = register_size (gdbarch, regnum);
  40. buf = (gdb_byte *) alloca (size);
  41. /* Read the register contents from the inferior a chunk at a time. */
  42. for (i = 0; i < size; i += sizeof (PTRACE_TYPE_RET))
  43. {
  44. size_t chunk = std::min (sizeof (PTRACE_TYPE_RET), size - i);
  45. PTRACE_TYPE_RET val;
  46. errno = 0;
  47. val = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3) (uintptr_t) addr, 0);
  48. if (errno != 0)
  49. error (_("Couldn't read register %s (#%d): %s."),
  50. gdbarch_register_name (gdbarch, regnum),
  51. regnum, safe_strerror (errno));
  52. store_unsigned_integer (buf + i, chunk, byte_order, val);
  53. addr += sizeof (PTRACE_TYPE_RET);
  54. }
  55. regcache->raw_supply (regnum, buf);
  56. }
  57. /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
  58. for all registers. */
  59. void
  60. linux_nat_trad_target::fetch_registers (struct regcache *regcache, int regnum)
  61. {
  62. if (regnum == -1)
  63. for (regnum = 0;
  64. regnum < gdbarch_num_regs (regcache->arch ());
  65. regnum++)
  66. fetch_register (regcache, regnum);
  67. else
  68. fetch_register (regcache, regnum);
  69. }
  70. /* Store register REGNUM into the inferior. */
  71. void
  72. linux_nat_trad_target::store_register (const struct regcache *regcache,
  73. int regnum)
  74. {
  75. struct gdbarch *gdbarch = regcache->arch ();
  76. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  77. CORE_ADDR addr;
  78. size_t size;
  79. gdb_byte *buf;
  80. pid_t pid;
  81. int i;
  82. /* This isn't really an address, but ptrace thinks of it as one. */
  83. addr = register_u_offset (gdbarch, regnum, 1);
  84. if (addr == (CORE_ADDR)-1
  85. || gdbarch_cannot_store_register (gdbarch, regnum))
  86. return;
  87. pid = get_ptrace_pid (regcache->ptid ());
  88. size = register_size (gdbarch, regnum);
  89. buf = (gdb_byte *) alloca (size);
  90. /* Write the register contents into the inferior a chunk at a time. */
  91. regcache->raw_collect (regnum, buf);
  92. for (i = 0; i < size; i += sizeof (PTRACE_TYPE_RET))
  93. {
  94. size_t chunk = std::min (sizeof (PTRACE_TYPE_RET), size - i);
  95. PTRACE_TYPE_RET val;
  96. val = extract_unsigned_integer (buf + i, chunk, byte_order);
  97. errno = 0;
  98. ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3) (uintptr_t) addr, val);
  99. if (errno != 0)
  100. error (_("Couldn't write register %s (#%d): %s."),
  101. gdbarch_register_name (gdbarch, regnum),
  102. regnum, safe_strerror (errno));
  103. addr += sizeof (PTRACE_TYPE_RET);
  104. }
  105. }
  106. /* Store register REGNUM back into the inferior. If REGNUM is -1, do
  107. this for all registers. */
  108. void
  109. linux_nat_trad_target::store_registers (struct regcache *regcache, int regnum)
  110. {
  111. if (regnum == -1)
  112. for (regnum = 0;
  113. regnum < gdbarch_num_regs (regcache->arch ());
  114. regnum++)
  115. store_register (regcache, regnum);
  116. else
  117. store_register (regcache, regnum);
  118. }