* linux-low.c (regsets_fetch_inferior_registers): Fix memory leak.
[gdb/SamB.git] / gdb / sparcobsd-tdep.c
blob25b1ce5d61b891c3a16aea6154d8d8661893e2e0
1 /* Target-dependent code for OpenBSD/sparc.
3 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "defs.h"
22 #include "floatformat.h"
23 #include "frame.h"
24 #include "frame-unwind.h"
25 #include "gdbcore.h"
26 #include "osabi.h"
27 #include "regcache.h"
28 #include "symtab.h"
29 #include "trad-frame.h"
31 #include "gdb_assert.h"
33 #include "obsd-tdep.h"
34 #include "sparc-tdep.h"
35 #include "solib-svr4.h"
36 #include "bsd-uthread.h"
38 /* Signal trampolines. */
40 /* The OpenBSD kernel maps the signal trampoline at some random
41 location in user space, which means that the traditional BSD way of
42 detecting it won't work.
44 The signal trampoline will be mapped at an address that is page
45 aligned. We recognize the signal trampoline by looking for the
46 sigreturn system call. */
48 static const int sparc32obsd_page_size = 4096;
50 static int
51 sparc32obsd_pc_in_sigtramp (CORE_ADDR pc, char *name)
53 CORE_ADDR start_pc = (pc & ~(sparc32obsd_page_size - 1));
54 unsigned long insn;
56 if (name)
57 return 0;
59 /* Check for "restore %g0, SYS_sigreturn, %g1". */
60 insn = sparc_fetch_instruction (start_pc + 0xec);
61 if (insn != 0x83e82067)
62 return 0;
64 /* Check for "t ST_SYSCALL". */
65 insn = sparc_fetch_instruction (start_pc + 0xf4);
66 if (insn != 0x91d02000)
67 return 0;
69 return 1;
72 static struct sparc_frame_cache *
73 sparc32obsd_sigtramp_frame_cache (struct frame_info *this_frame,
74 void **this_cache)
76 struct sparc_frame_cache *cache;
77 CORE_ADDR addr;
79 if (*this_cache)
80 return *this_cache;
82 cache = sparc_frame_cache (this_frame, this_cache);
83 gdb_assert (cache == *this_cache);
85 /* If we couldn't find the frame's function, we're probably dealing
86 with an on-stack signal trampoline. */
87 if (cache->pc == 0)
89 cache->pc = get_frame_pc (this_frame);
90 cache->pc &= ~(sparc32obsd_page_size - 1);
92 /* Since we couldn't find the frame's function, the cache was
93 initialized under the assumption that we're frameless. */
94 cache->frameless_p = 0;
95 addr = get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM);
96 cache->base = addr;
99 cache->saved_regs = sparc32nbsd_sigcontext_saved_regs (this_frame);
101 return cache;
104 static void
105 sparc32obsd_sigtramp_frame_this_id (struct frame_info *this_frame,
106 void **this_cache,
107 struct frame_id *this_id)
109 struct sparc_frame_cache *cache =
110 sparc32obsd_sigtramp_frame_cache (this_frame, this_cache);
112 (*this_id) = frame_id_build (cache->base, cache->pc);
115 static struct value *
116 sparc32obsd_sigtramp_frame_prev_register (struct frame_info *this_frame,
117 void **this_cache, int regnum)
119 struct sparc_frame_cache *cache =
120 sparc32obsd_sigtramp_frame_cache (this_frame, this_cache);
122 return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
125 static int
126 sparc32obsd_sigtramp_frame_sniffer (const struct frame_unwind *self,
127 struct frame_info *this_frame,
128 void **this_cache)
130 CORE_ADDR pc = get_frame_pc (this_frame);
131 char *name;
133 find_pc_partial_function (pc, &name, NULL, NULL);
134 if (sparc32obsd_pc_in_sigtramp (pc, name))
135 return 1;
137 return 0;
139 static const struct frame_unwind sparc32obsd_sigtramp_frame_unwind =
141 SIGTRAMP_FRAME,
142 sparc32obsd_sigtramp_frame_this_id,
143 sparc32obsd_sigtramp_frame_prev_register,
144 NULL,
145 sparc32obsd_sigtramp_frame_sniffer
150 /* Offset wthin the thread structure where we can find %fp and %i7. */
151 #define SPARC32OBSD_UTHREAD_FP_OFFSET 128
152 #define SPARC32OBSD_UTHREAD_PC_OFFSET 132
154 static void
155 sparc32obsd_supply_uthread (struct regcache *regcache,
156 int regnum, CORE_ADDR addr)
158 CORE_ADDR fp, fp_addr = addr + SPARC32OBSD_UTHREAD_FP_OFFSET;
159 gdb_byte buf[4];
161 gdb_assert (regnum >= -1);
163 fp = read_memory_unsigned_integer (fp_addr, 4);
164 if (regnum == SPARC_SP_REGNUM || regnum == -1)
166 store_unsigned_integer (buf, 4, fp);
167 regcache_raw_supply (regcache, SPARC_SP_REGNUM, buf);
169 if (regnum == SPARC_SP_REGNUM)
170 return;
173 if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM
174 || regnum == -1)
176 CORE_ADDR i7, i7_addr = addr + SPARC32OBSD_UTHREAD_PC_OFFSET;
178 i7 = read_memory_unsigned_integer (i7_addr, 4);
179 if (regnum == SPARC32_PC_REGNUM || regnum == -1)
181 store_unsigned_integer (buf, 4, i7 + 8);
182 regcache_raw_supply (regcache, SPARC32_PC_REGNUM, buf);
184 if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
186 store_unsigned_integer (buf, 4, i7 + 12);
187 regcache_raw_supply (regcache, SPARC32_NPC_REGNUM, buf);
190 if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
191 return;
194 sparc_supply_rwindow (regcache, fp, regnum);
197 static void
198 sparc32obsd_collect_uthread(const struct regcache *regcache,
199 int regnum, CORE_ADDR addr)
201 CORE_ADDR sp;
202 gdb_byte buf[4];
204 gdb_assert (regnum >= -1);
206 if (regnum == SPARC_SP_REGNUM || regnum == -1)
208 CORE_ADDR fp_addr = addr + SPARC32OBSD_UTHREAD_FP_OFFSET;
210 regcache_raw_collect (regcache, SPARC_SP_REGNUM, buf);
211 write_memory (fp_addr,buf, 4);
214 if (regnum == SPARC32_PC_REGNUM || regnum == -1)
216 CORE_ADDR i7, i7_addr = addr + SPARC32OBSD_UTHREAD_PC_OFFSET;
218 regcache_raw_collect (regcache, SPARC32_PC_REGNUM, buf);
219 i7 = extract_unsigned_integer (buf, 4) - 8;
220 write_memory_unsigned_integer (i7_addr, 4, i7);
222 if (regnum == SPARC32_PC_REGNUM)
223 return;
226 regcache_raw_collect (regcache, SPARC_SP_REGNUM, buf);
227 sp = extract_unsigned_integer (buf, 4);
228 sparc_collect_rwindow (regcache, sp, regnum);
232 static void
233 sparc32obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
235 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
237 /* OpenBSD/sparc is very similar to NetBSD/sparc ELF. */
238 sparc32nbsd_elf_init_abi (info, gdbarch);
240 set_gdbarch_skip_solib_resolver (gdbarch, obsd_skip_solib_resolver);
242 frame_unwind_append_unwinder (gdbarch, &sparc32obsd_sigtramp_frame_unwind);
244 /* OpenBSD provides a user-level threads implementation. */
245 bsd_uthread_set_supply_uthread (gdbarch, sparc32obsd_supply_uthread);
246 bsd_uthread_set_collect_uthread (gdbarch, sparc32obsd_collect_uthread);
250 /* Provide a prototype to silence -Wmissing-prototypes. */
251 void _initialize_sparc32obsd_tdep (void);
253 void
254 _initialize_sparc32obsd_tdep (void)
256 gdbarch_register_osabi (bfd_arch_sparc, 0, GDB_OSABI_OPENBSD_ELF,
257 sparc32obsd_init_abi);