This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[commit] OpenBSD/i386 kernel debugging improvements


This teaches GDB how to unwind OpenBSD/i386 kernel trap frames,
terminating backtraces at system calls.  This is a huge improvement
post-mortem debugging using kernel core dumps.

Committed,

Mark


Index: ChangeLog
from  Mark Kettenis  <kettenis@gnu.org>

	* i386-tdep.h (I386_SEL_RPL, I386_SEL_UPL, I386_SEL_KPL): New
	defines.
	* i386obsd-tdep.c: Include "frame-unwind.h" and "trad-frame.h".
	(i386obsd_tf_reg_offset, i386obsd_trapframe_unwind): New
	variables.
	(i386obsd_trapframe_cache, i386obsd_trapframe_this_id)
	(i386obsd_trapframe_prev_register, i386obsd_trapframe_sniffer):
	New functions.
	(i386obsd_init_abi): Append i386obsd_trapframe_sniffer.
	* Makefile.in (i386obsd-tdep.o): Update dependencies.

Index: Makefile.in
===================================================================
RCS file: /cvs/src/src/gdb/Makefile.in,v
retrieving revision 1.763
diff -u -p -r1.763 Makefile.in
--- Makefile.in 17 Dec 2005 22:33:59 -0000 1.763
+++ Makefile.in 19 Dec 2005 22:18:41 -0000
@@ -2072,9 +2072,10 @@ i386-nto-tdep.o: i386-nto-tdep.c $(defs_
 	$(i386_tdep_h) $(i387_tdep_h) $(nto_tdep_h) $(solib_svr4_h)
 i386obsd-nat.o: i386obsd-nat.c $(defs_h) $(i386_tdep_h)
 i386obsd-tdep.o: i386obsd-tdep.c $(defs_h) $(arch_utils_h) $(frame_h) \
-	$(gdbcore_h) $(regcache_h) $(regset_h) $(symtab_h) $(objfiles_h) \
-	$(osabi_h) $(target_h) $(gdb_assert_h) $(gdb_string_h) \
-	$(i386_tdep_h) $(i387_tdep_h) $(solib_svr4_h) $(bsd_uthread_h)
+	$(frame_unwind_h) $(gdbcore_h) $(regcache_h) $(regset_h) $(symtab_h) \
+	$(objfiles_h) $(osabi_h) $(target_h) $(trad_frame_h) $(gdb_assert_h) \
+	$(gdb_string_h) $(i386_tdep_h) $(i387_tdep_h) $(solib_svr4_h) \
+	$(bsd_uthread_h)
 i386-sol2-nat.o: i386-sol2-nat.c $(defs_h) $(regcache_h) $(gregset_h) \
 	$(amd64_nat_h) $(amd64_tdep_h)
 i386-sol2-tdep.o: i386-sol2-tdep.c $(defs_h) $(value_h) $(osabi_h) \
Index: i386-tdep.h
===================================================================
RCS file: /cvs/src/src/gdb/i386-tdep.h,v
retrieving revision 1.43
diff -u -p -r1.43 i386-tdep.h
--- i386-tdep.h 17 Dec 2005 22:34:01 -0000 1.43
+++ i386-tdep.h 19 Dec 2005 22:18:41 -0000
@@ -149,6 +149,11 @@ enum i386_regnum
 /* Size of the largest register.  */
 #define I386_MAX_REGISTER_SIZE	16
 
+/* Segment selectors.  */
+#define I386_SEL_RPL	0x0003  /* Requester's Privilege Level mask.  */
+#define I386_SEL_UPL	0x0003	/* User Privilige Level. */
+#define I386_SEL_KPL	0x0000	/* Kernel Privilige Level. */
+
 /* Functions exported from i386-tdep.c.  */
 extern CORE_ADDR i386_pe_skip_trampoline_code (CORE_ADDR pc, char *name);
 
Index: i386obsd-tdep.c
===================================================================
RCS file: /cvs/src/src/gdb/i386obsd-tdep.c,v
retrieving revision 1.25
diff -u -p -r1.25 i386obsd-tdep.c
--- i386obsd-tdep.c 17 Dec 2005 22:34:01 -0000 1.25
+++ i386obsd-tdep.c 19 Dec 2005 22:18:41 -0000
@@ -24,6 +24,7 @@
 #include "defs.h"
 #include "arch-utils.h"
 #include "frame.h"
+#include "frame-unwind.h"
 #include "gdbcore.h"
 #include "regcache.h"
 #include "regset.h"
@@ -31,6 +32,7 @@
 #include "objfiles.h"
 #include "osabi.h"
 #include "target.h"
+#include "trad-frame.h"
 
 #include "gdb_assert.h"
 #include "gdb_string.h"
@@ -265,7 +267,6 @@ i386obsd_supply_uthread (struct regcache
 	  regcache_raw_supply (regcache, i, buf);
 	}
     }
-
 }
 
 static void
@@ -313,6 +314,119 @@ i386obsd_collect_uthread (const struct r
 	}
     }
 }
+
+/* Kernel debugging support.  */
+
+/* From <machine/frame.h>.  Note that %esp and %ess are only saved in
+   a trap frame when entering the kernel from user space.  */
+static int i386obsd_tf_reg_offset[] =
+{
+  10 * 4,			/* %eax */
+  9 * 4,			/* %ecx */
+  8 * 4,			/* %edx */
+  7 * 4,			/* %ebx */
+  -1,				/* %esp */
+  6 * 4,			/* %ebp */
+  5 * 4,			/* %esi */
+  4 * 4,			/* %edi */
+  13 * 4,			/* %eip */
+  15 * 4,			/* %eflags */
+  14 * 4,			/* %cs */
+  -1,				/* %ss */
+  3 * 4,			/* %ds */
+  2 * 4,			/* %es */
+  0 * 4,			/* %fs */
+  1 * 4				/* %gs */
+};
+
+static struct trad_frame_cache *
+i386obsd_trapframe_cache(struct frame_info *next_frame, void **this_cache)
+{
+  struct trad_frame_cache *cache;
+  CORE_ADDR func, sp, addr;
+  ULONGEST cs;
+  int i;
+
+  if (*this_cache)
+    return *this_cache;
+
+  cache = trad_frame_cache_zalloc (next_frame);
+  *this_cache = cache;
+
+  func = frame_func_unwind (next_frame);
+  sp = frame_unwind_register_unsigned (next_frame, I386_ESP_REGNUM);
+  for (i = 0; i < ARRAY_SIZE (i386obsd_tf_reg_offset); i++)
+    if (i386obsd_tf_reg_offset[i] != -1)
+      trad_frame_set_reg_addr (cache, i, sp + i386obsd_tf_reg_offset[i]);
+
+  /* Read %cs from trap frame.  */
+  addr = sp + i386obsd_tf_reg_offset[I386_CS_REGNUM];
+  cs = read_memory_unsigned_integer (addr, 4); 
+  if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
+    {
+      /* Trap from use space; terminate backtrace.  */
+      trad_frame_set_id (cache, null_frame_id);
+    }
+  else
+    {
+      /* Construct the frame ID using the function start.  */
+      trad_frame_set_id (cache, frame_id_build (sp + 8, func));
+    }
+
+  return cache;
+}
+
+static void
+i386obsd_trapframe_this_id (struct frame_info *next_frame,
+			    void **this_cache, struct frame_id *this_id)
+{
+  struct trad_frame_cache *cache =
+    i386obsd_trapframe_cache (next_frame, this_cache);
+  
+  trad_frame_get_id (cache, this_id);
+}
+
+static void
+i386obsd_trapframe_prev_register (struct frame_info *next_frame,
+				  void **this_cache, int regnum,
+				  int *optimizedp, enum lval_type *lvalp,
+				  CORE_ADDR *addrp, int *realnump,
+				  gdb_byte *valuep)
+{
+  struct trad_frame_cache *cache =
+    i386obsd_trapframe_cache (next_frame, this_cache);
+
+  trad_frame_get_register (cache, next_frame, regnum,
+			   optimizedp, lvalp, addrp, realnump, valuep);
+}
+
+static const struct frame_unwind i386obsd_trapframe_unwind = {
+  /* FIXME: kettenis/20051219: This really is more like an interrupt
+     frame, but SIGTRAMP_FRAME would print <signal handler called>,
+     which really is not what we want here.  */
+  NORMAL_FRAME,
+  i386obsd_trapframe_this_id,
+  i386obsd_trapframe_prev_register
+};
+
+static const struct frame_unwind *
+i386obsd_trapframe_sniffer (struct frame_info *next_frame)
+{
+  ULONGEST cs;
+  char *name;
+
+  cs = frame_unwind_register_unsigned (next_frame, I386_CS_REGNUM);
+  if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
+    return NULL;
+
+  find_pc_partial_function (frame_pc_unwind (next_frame), &name, NULL, NULL);
+  if (name && ((strcmp ("calltrap", name) == 0)
+	       || (strcmp ("syscall1", name) == 0)))
+    return &i386obsd_trapframe_unwind;
+
+  return NULL;
+}
+
 
 static void 
 i386obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
@@ -343,6 +457,9 @@ i386obsd_init_abi (struct gdbarch_info i
   /* OpenBSD provides a user-level threads implementation.  */
   bsd_uthread_set_supply_uthread (gdbarch, i386obsd_supply_uthread);
   bsd_uthread_set_collect_uthread (gdbarch, i386obsd_collect_uthread);
+
+  /* Unwind kernel trap frames correctly.  */
+  frame_unwind_append_sniffer (gdbarch, i386obsd_trapframe_sniffer);
 }
 
 /* OpenBSD a.out.  */


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]