i386-obsd-nat.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Native-dependent code for OpenBSD/i386.
  2. Copyright (C) 2002-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 <sys/sysctl.h>
  19. #include <machine/frame.h>
  20. #include <machine/pcb.h>
  21. #include "i386-tdep.h"
  22. #include "i386-bsd-nat.h"
  23. #include "obsd-nat.h"
  24. #include "bsd-kvm.h"
  25. static int
  26. i386obsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
  27. {
  28. struct gdbarch *gdbarch = regcache->arch ();
  29. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  30. struct switchframe sf;
  31. /* The following is true for OpenBSD 3.6:
  32. The pcb contains %esp and %ebp at the point of the context switch
  33. in cpu_switch(). At that point we have a stack frame as
  34. described by `struct switchframe', which for OpenBSD 3.6 has the
  35. following layout:
  36. interrupt level
  37. %edi
  38. %esi
  39. %ebx
  40. %eip
  41. we reconstruct the register state as it would look when we just
  42. returned from cpu_switch(). */
  43. /* The stack pointer shouldn't be zero. */
  44. if (pcb->pcb_esp == 0)
  45. return 0;
  46. /* Read the stack frame, and check its validity. We do this by
  47. checking if the saved interrupt priority level in the stack frame
  48. looks reasonable.. */
  49. #ifdef PCB_SAVECTX
  50. if ((pcb->pcb_flags & PCB_SAVECTX) == 0)
  51. {
  52. /* Yes, we have a frame that matches cpu_switch(). */
  53. read_memory (pcb->pcb_esp, (gdb_byte *) &sf, sizeof sf);
  54. pcb->pcb_esp += sizeof (struct switchframe);
  55. regcache->raw_supply (I386_EDI_REGNUM, &sf.sf_edi);
  56. regcache->raw_supply (I386_ESI_REGNUM, &sf.sf_esi);
  57. regcache->raw_supply (I386_EBX_REGNUM, &sf.sf_ebx);
  58. regcache->raw_supply (I386_EIP_REGNUM, &sf.sf_eip);
  59. }
  60. else
  61. #endif
  62. {
  63. /* No, the pcb must have been last updated by savectx(). */
  64. pcb->pcb_esp = pcb->pcb_ebp;
  65. pcb->pcb_ebp = read_memory_integer(pcb->pcb_esp, 4, byte_order);
  66. sf.sf_eip = read_memory_integer(pcb->pcb_esp + 4, 4, byte_order);
  67. regcache->raw_supply (I386_EIP_REGNUM, &sf.sf_eip);
  68. }
  69. regcache->raw_supply (I386_EBP_REGNUM, &pcb->pcb_ebp);
  70. regcache->raw_supply (I386_ESP_REGNUM, &pcb->pcb_esp);
  71. return 1;
  72. }
  73. static i386_bsd_nat_target<obsd_nat_target> the_i386_obsd_nat_target;
  74. void _initialize_i386obsd_nat ();
  75. void
  76. _initialize_i386obsd_nat ()
  77. {
  78. add_inf_child_target (&i386_obsd_nat_target);
  79. /* Support debugging kernel virtual memory images. */
  80. bsd_kvm_add_target (i386obsd_supply_pcb);
  81. /* OpenBSD provides a vm.psstrings sysctl that we can use to locate
  82. the sigtramp. That way we can still recognize a sigtramp if its
  83. location is changed in a new kernel. This is especially
  84. important for OpenBSD, since it uses a different memory layout
  85. than NetBSD, yet we cannot distinguish between the two.
  86. Of course this is still based on the assumption that the sigtramp
  87. is placed directly under the location where the program arguments
  88. and environment can be found. */
  89. #ifdef VM_PSSTRINGS
  90. {
  91. struct _ps_strings _ps;
  92. int mib[2];
  93. size_t len;
  94. mib[0] = CTL_VM;
  95. mib[1] = VM_PSSTRINGS;
  96. len = sizeof (_ps);
  97. if (sysctl (mib, 2, &_ps, &len, NULL, 0) == 0)
  98. {
  99. i386obsd_sigtramp_start_addr = (u_long) _ps.val - 128;
  100. i386obsd_sigtramp_end_addr = (u_long) _ps.val;
  101. }
  102. }
  103. #endif
  104. }