amd64-obsd-nat.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Native-dependent code for OpenBSD/amd64.
  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 "gdbcore.h"
  16. #include "regcache.h"
  17. #include "target.h"
  18. #include "amd64-tdep.h"
  19. #include "amd64-bsd-nat.h"
  20. #include "amd64-nat.h"
  21. #include "obsd-nat.h"
  22. /* Mapping between the general-purpose registers in OpenBSD/amd64
  23. `struct reg' format and GDB's register cache layout for
  24. OpenBSD/i386.
  25. Note that most (if not all) OpenBSD/amd64 registers are 64-bit,
  26. while the OpenBSD/i386 registers are all 32-bit, but since we're
  27. little-endian we get away with that. */
  28. /* From <machine/reg.h>. */
  29. static int amd64obsd32_r_reg_offset[] =
  30. {
  31. 14 * 8, /* %eax */
  32. 3 * 8, /* %ecx */
  33. 2 * 8, /* %edx */
  34. 13 * 8, /* %ebx */
  35. 15 * 8, /* %esp */
  36. 12 * 8, /* %ebp */
  37. 1 * 8, /* %esi */
  38. 0 * 8, /* %edi */
  39. 16 * 8, /* %eip */
  40. 17 * 8, /* %eflags */
  41. 18 * 8, /* %cs */
  42. 19 * 8, /* %ss */
  43. 20 * 8, /* %ds */
  44. 21 * 8, /* %es */
  45. 22 * 8, /* %fs */
  46. 23 * 8 /* %gs */
  47. };
  48. /* Support for debugging kernel virtual memory images. */
  49. #include <sys/types.h>
  50. #include <machine/frame.h>
  51. #include <machine/pcb.h>
  52. #include "bsd-kvm.h"
  53. static int
  54. amd64obsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
  55. {
  56. struct switchframe sf;
  57. int regnum;
  58. /* The following is true for OpenBSD 3.5:
  59. The pcb contains the stack pointer at the point of the context
  60. switch in cpu_switch(). At that point we have a stack frame as
  61. described by `struct switchframe', which for OpenBSD 3.5 has the
  62. following layout:
  63. interrupt level
  64. %r15
  65. %r14
  66. %r13
  67. %r12
  68. %rbp
  69. %rbx
  70. return address
  71. Together with %rsp in the pcb, this accounts for all callee-saved
  72. registers specified by the psABI. From this information we
  73. reconstruct the register state as it would look when we just
  74. returned from cpu_switch().
  75. For core dumps the pcb is saved by savectx(). In that case the
  76. stack frame only contains the return address, and there is no way
  77. to recover the other registers. */
  78. /* The stack pointer shouldn't be zero. */
  79. if (pcb->pcb_rsp == 0)
  80. return 0;
  81. /* Read the stack frame, and check its validity. */
  82. read_memory (pcb->pcb_rsp, (gdb_byte *) &sf, sizeof sf);
  83. if (sf.sf_rbp == pcb->pcb_rbp)
  84. {
  85. /* Yes, we have a frame that matches cpu_switch(). */
  86. pcb->pcb_rsp += sizeof (struct switchframe);
  87. regcache->raw_supply (12, &sf.sf_r12);
  88. regcache->raw_supply (13, &sf.sf_r13);
  89. regcache->raw_supply (14, &sf.sf_r14);
  90. regcache->raw_supply (15, &sf.sf_r15);
  91. regcache->raw_supply (AMD64_RBX_REGNUM, &sf.sf_rbx);
  92. regcache->raw_supply (AMD64_RIP_REGNUM, &sf.sf_rip);
  93. }
  94. else
  95. {
  96. /* No, the pcb must have been last updated by savectx(). */
  97. pcb->pcb_rsp += 8;
  98. regcache->raw_supply (AMD64_RIP_REGNUM, &sf);
  99. }
  100. regcache->raw_supply (AMD64_RSP_REGNUM, &pcb->pcb_rsp);
  101. regcache->raw_supply (AMD64_RBP_REGNUM, &pcb->pcb_rbp);
  102. return 1;
  103. }
  104. static amd64_bsd_nat_target<obsd_nat_target> the_amd64_obsd_nat_target;
  105. void _initialize_amd64obsd_nat ();
  106. void
  107. _initialize_amd64obsd_nat ()
  108. {
  109. amd64_native_gregset32_reg_offset = amd64obsd32_r_reg_offset;
  110. amd64_native_gregset32_num_regs = ARRAY_SIZE (amd64obsd32_r_reg_offset);
  111. amd64_native_gregset64_reg_offset = amd64obsd_r_reg_offset;
  112. add_inf_child_target (&the_amd64_obsd_nat_target);
  113. /* Support debugging kernel virtual memory images. */
  114. bsd_kvm_add_target (amd64obsd_supply_pcb);
  115. }