Automatic date update in version.in
[binutils-gdb.git] / gdb / i386-obsd-nat.c
blob2b3d3912430f60fed78e72520fe2d7d0e892df60
1 /* Native-dependent code for OpenBSD/i386.
3 Copyright (C) 2002-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "gdbcore.h"
21 #include "regcache.h"
22 #include "target.h"
24 #include <sys/sysctl.h>
25 #include <machine/frame.h>
26 #include <machine/pcb.h>
28 #include "i386-tdep.h"
29 #include "i386-bsd-nat.h"
30 #include "obsd-nat.h"
31 #include "bsd-kvm.h"
33 static int
34 i386obsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
36 struct gdbarch *gdbarch = regcache->arch ();
37 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
38 struct switchframe sf;
40 /* The following is true for OpenBSD 3.6:
42 The pcb contains %esp and %ebp at the point of the context switch
43 in cpu_switch(). At that point we have a stack frame as
44 described by `struct switchframe', which for OpenBSD 3.6 has the
45 following layout:
47 interrupt level
48 %edi
49 %esi
50 %ebx
51 %eip
53 we reconstruct the register state as it would look when we just
54 returned from cpu_switch(). */
56 /* The stack pointer shouldn't be zero. */
57 if (pcb->pcb_esp == 0)
58 return 0;
60 /* Read the stack frame, and check its validity. We do this by
61 checking if the saved interrupt priority level in the stack frame
62 looks reasonable.. */
63 #ifdef PCB_SAVECTX
64 if ((pcb->pcb_flags & PCB_SAVECTX) == 0)
66 /* Yes, we have a frame that matches cpu_switch(). */
67 read_memory (pcb->pcb_esp, (gdb_byte *) &sf, sizeof sf);
68 pcb->pcb_esp += sizeof (struct switchframe);
69 regcache->raw_supply (I386_EDI_REGNUM, &sf.sf_edi);
70 regcache->raw_supply (I386_ESI_REGNUM, &sf.sf_esi);
71 regcache->raw_supply (I386_EBX_REGNUM, &sf.sf_ebx);
72 regcache->raw_supply (I386_EIP_REGNUM, &sf.sf_eip);
74 else
75 #endif
77 /* No, the pcb must have been last updated by savectx(). */
78 pcb->pcb_esp = pcb->pcb_ebp;
79 pcb->pcb_ebp = read_memory_integer(pcb->pcb_esp, 4, byte_order);
80 sf.sf_eip = read_memory_integer(pcb->pcb_esp + 4, 4, byte_order);
81 regcache->raw_supply (I386_EIP_REGNUM, &sf.sf_eip);
84 regcache->raw_supply (I386_EBP_REGNUM, &pcb->pcb_ebp);
85 regcache->raw_supply (I386_ESP_REGNUM, &pcb->pcb_esp);
87 return 1;
90 static i386_bsd_nat_target<obsd_nat_target> the_i386_obsd_nat_target;
92 void _initialize_i386obsd_nat ();
93 void
94 _initialize_i386obsd_nat ()
96 add_inf_child_target (&i386_obsd_nat_target);
98 /* Support debugging kernel virtual memory images. */
99 bsd_kvm_add_target (i386obsd_supply_pcb);
101 /* OpenBSD provides a vm.psstrings sysctl that we can use to locate
102 the sigtramp. That way we can still recognize a sigtramp if its
103 location is changed in a new kernel. This is especially
104 important for OpenBSD, since it uses a different memory layout
105 than NetBSD, yet we cannot distinguish between the two.
107 Of course this is still based on the assumption that the sigtramp
108 is placed directly under the location where the program arguments
109 and environment can be found. */
110 #ifdef VM_PSSTRINGS
112 struct _ps_strings _ps;
113 int mib[2];
114 size_t len;
116 mib[0] = CTL_VM;
117 mib[1] = VM_PSSTRINGS;
118 len = sizeof (_ps);
119 if (sysctl (mib, 2, &_ps, &len, NULL, 0) == 0)
121 i386obsd_sigtramp_start_addr = (u_long) _ps.val - 128;
122 i386obsd_sigtramp_end_addr = (u_long) _ps.val;
125 #endif