Skip to contentSkip to conversation

backtrace support

PATCH Vincent Guffens 3 participants 4 messages Aug 18, 2005 – Aug 19, 2005 Kbuild

lore
Share & tools
lore
#0 lore raw

Hi,

After having searched for the reason of the unaligned pointer caused by the nested functions bug, I thought that it could be interesting to have a backtrace in grub. It would be triggered in grub_fatal and would print the last few function calls that triggered the problem.

I have implemented such a backtrace function using the stack base pointer. You can see how it looks like on the picture attached. This is a screenshot taken after the bug mentioned above and it points directly to the cause of the problem.

However, the price to pay for that, at least with my implementation, is quite high. One has to disable the optimization flag to prevent the -fomit-frame-pointer and the module must not be stripped. Still, during the developpement phase, it might be usefull to get good bug reports.

The feature is added with
./configure --with-backtrace
and grub must be compile with ./btmake instead of make which is a simple script which calls make twice instead of only once.

I include the patch as attachment, if not for inclusion in grub, at least for the potential interrested reader.

Signature · 6 lines
				Vincent Guffens
				PhD Student UCL/CESAME
				tel:   +32 10 47 80 30
Value your freedom, or you will lose it, teaches history.
"Don't bother us with politics," respond those who don't want to learn.
	      	-- Richard M. Stallman
btmake +9 -0
diff -ru -N -b -B grub2/btmake grub2-backtrace/btmake
@@ -0,0 +1,9 @@
+#!/bin/sh
+# Create a grub kernel image with backtrace support
+# configure must have been run with : --with-backtrace
+make
+./genbtsym.sh kernel.exec > kern/i386/pc/grub_btsym_list.txt
+rm kernel_img-kern_i386_pc_backtrace.d
+rm kernel_img-kern_i386_pc_backtrace.o
+make
+
conf/i386-pc.rmk +2 -2
diff -ru -N -b -B grub2/conf/i386-pc.rmk grub2-backtrace/conf/i386-pc.rmk
@@ -25,7 +25,7 @@
 kernel_img_SOURCES = kern/i386/pc/startup.S kern/main.c kern/device.c \
 	kern/disk.c kern/dl.c kern/file.c kern/fs.c kern/err.c \
 	kern/misc.c kern/mm.c kern/loader.c kern/rescue.c kern/term.c \
-	kern/i386/dl.c kern/i386/pc/init.c kern/partition.c \
+	kern/i386/dl.c kern/i386/pc/init.c kern/i386/pc/backtrace.c kern/partition.c \
 	kern/env.c disk/i386/pc/biosdisk.c \
 	term/i386/pc/console.c \
 	symlist.c
@@ -92,7 +92,7 @@
 	partmap/amiga.c	partmap/apple.c partmap/pc.c partmap/sun.c	\
 	util/console.c util/grub-emu.c util/misc.c			\
 	util/i386/pc/biosdisk.c util/i386/pc/getroot.c			\
-	util/i386/pc/misc.c
+	util/i386/pc/misc.c kern/i386/pc/backtrace.c
 
 grub_emu_LDFLAGS = $(LIBCURSES)
 
config.h.in +3 -0
diff -ru -N -b -B grub2/config.h.in grub2-backtrace/config.h.in
@@ -16,6 +16,9 @@
 /* Define it to either end or _end */
 #undef END_SYMBOL
 
+/* enable backtrace support */
+#undef GRUB_BACKTRACE
+
 /* Define if C symbols get an underscore after compilation */
 #undef HAVE_ASM_USCORE
 
configure.ac +21 -1
diff -ru -N -b -B grub2/configure.ac grub2-backtrace/configure.ac
@@ -57,9 +57,19 @@
     AC_TRY_COMPILE(, , size_flag=yes, size_flag=no)
   ])
   if test "x$size_flag" = xyes; then
+		if test "$with_backtrace" == "yes"
+		then
+			tmp_CFLAGS="$tmp_CFLAGS -O0"
+		else
     tmp_CFLAGS="$tmp_CFLAGS -Os"
+		fi
+  else
+		if test "$with_backtrace" == "yes"
+		then
+    	tmp_CFLAGS="$tmp_CFLAGS -O0 -fno-strength-reduce -fno-unroll-loops"
   else
-    tmp_CFLAGS="$tmp_CFLAGS -O2 -fno-strength-reduce -fno-unroll-loops"
+    	tmp_CFLAGS="$tmp_CFLAGS -Os -fno-strength-reduce -fno-unroll-loops"
+		fi
   fi
 
   # Force no alignment to save space on i386.
@@ -108,6 +118,16 @@
 # This is not a "must".
 AC_PATH_PROG(RUBY, ruby)
 
+# Include the stack trace support ?
+AC_ARG_WITH(backtrace, [  --with-backtrace            enable stack trace support for i386])
+if test "$with_backtrace" == "yes"
+then
+	case "$host_cpu" in
+  	i[[3456]]86) AC_DEFINE([GRUB_BACKTRACE], [], [enable backtrace support])  ;;
+  	*) AC_MSG_NOTICE([backtrace support available for i386 only]) ;;
+	esac
+fi
+
 # For cross-compiling.
 if test "x$build" != "x$host"; then
   AC_CHECK_PROGS(BUILD_CC, [gcc egcs cc],
genbtsym.sh +14 -0
diff -ru -N -b -B grub2/genbtsym.sh grub2-backtrace/genbtsym.sh
@@ -0,0 +1,14 @@
+#!/bin/bash
+# Generates the list of all the symbols
+# defined in the grub kernel. This is used
+# to print a stack trace in case of trouble
+
+
+OBJECT=$1
+
+if (test -e ${OBJECT}) ; then
+	objdump -t ${OBJECT} | grep -v 00000000 | awk '/.text/ {print "{\""$6"\",","0x"$1",","0x"$5 "},"}'  ;
+else
+	echo [0 ... MAX_BTSYMBOLS-2 ] = {"symbol", 0, 0},
+fi
+				
genmk.rb +3 -1
diff -ru -N -b -B grub2/genmk.rb grub2-backtrace/genmk.rb
@@ -119,7 +119,9 @@
 #{@name}: #{pre_obj} #{mod_obj}
 	-rm -f $@
 	$(LD) -r -d -o $@ $^
-	$(STRIP) --strip-unneeded -K grub_mod_init -K grub_mod_fini -R .note -R .comment $@
+	 cat config.h | grep define | grep BACKTRACE \
+	 && $(STRIP) --strip-debug -R .note -R .comment $@ \
+	 || $(STRIP) --strip-unneeded -K grub_mod_init -K grub_mod_fini -R .note -R .comment $@ 
 
 #{pre_obj}: #{objs_str}
 	-rm -f $@
include/grub/kernel.h +8 -0
diff -ru -N -b -B grub2/include/grub/kernel.h grub2-backtrace/include/grub/kernel.h
@@ -21,6 +21,7 @@
 #define GRUB_KERNEL_HEADER	1
 
 #include <grub/types.h>
+#include <grub/symbol.h>
 
 /* The module header.  */
 struct grub_module_header
@@ -58,4 +59,11 @@
 /* Register all the exported symbols. This is automatically generated.  */
 void grub_register_exported_symbols (void);
 
+/* backtrace support for i386 */
+void EXPORT_FUNC(grub_backtrace) (void);
+void EXPORT_FUNC(grub_btregister_symbol) (const char*,void*,grub_size_t);
+void EXPORT_FUNC(grub_btunregister_symbol) (void *);
+void EXPORT_FUNC(grub_init_btsym) (void);
+
+
 #endif /* ! GRUB_KERNEL_HEADER */
kern/dl.c +4 -0
diff -ru -N -b -B grub2/kern/dl.c grub2-backtrace/kern/dl.c
@@ -29,6 +29,7 @@
 #include <grub/file.h>
 #include <grub/env.h>
 #include <grub/cache.h>
+#include <grub/kernel.h>
 
 #if GRUB_HOST_SIZEOF_VOID_P == 4
 
@@ -216,6 +217,7 @@
 	  if (sym->mod == mod)
 	    {
 	      *p = q;
+				grub_btunregister_symbol(sym->addr);
 	      grub_free ((void *) sym->name);
 	      grub_free (sym);
 	    }
@@ -375,6 +377,7 @@
 	case STT_FUNC:
 	  sym->st_value += (Elf_Addr) grub_dl_get_section_addr (mod,
 								sym->st_shndx);
+		grub_btregister_symbol(name,(void *) sym->st_value, sym->st_size);
 	  if (bind != STB_LOCAL)
 	    if (grub_dl_register_symbol (name, (void *) sym->st_value, mod))
 	      return grub_errno;
@@ -654,6 +657,7 @@
   grub_dl_remove (mod);
   grub_dl_unregister_symbols (mod);
   
+  
   for (dep = mod->dep; dep; dep = depn)
     {
       depn = dep->next;
kern/err.c +4 -0
diff -ru -N -b -B grub2/kern/err.c grub2-backtrace/kern/err.c
@@ -20,6 +20,7 @@
 
 #include <grub/err.h>
 #include <grub/misc.h>
+#include <grub/kernel.h>
 #include <stdarg.h>
 
 #define GRUB_MAX_ERRMSG	256
@@ -50,6 +51,9 @@
   grub_vprintf (fmt, ap);
   va_end (ap);
 
+#ifndef GRUB_UTIL
+	grub_backtrace();
+#endif	
   grub_stop ();
 }
 
kern/i386/pc/backtrace.c +150 -0

150 lines not on this page · raw message

kern/main.c +14 -0
diff -ru -N -b -B grub2/kern/main.c grub2-backtrace/kern/main.c
@@ -28,6 +28,19 @@
 #include <grub/file.h>
 #include <grub/device.h>
 #include <grub/env.h>
+#include <config.h>
+
+#ifndef GRUB_BACKTRACE
+/* if GRUB_BACKTRACE is defined the backtrace functions
+ * are defined in kern/i386/pc/backtrace.c */
+#define NOOP {do {} while (0);}
+void grub_backtrace(void) NOOP;
+void grub_btregister_symbol(const char* name  __attribute__ ((unused)),
+														void* ad  __attribute__ ((unused)),
+														grub_size_t s __attribute__ ((unused))) NOOP;
+void grub_init_btsym (void) NOOP;
+void grub_btunregister_symbol (void * addr __attribute__((unused))) NOOP;
+#endif
 
 /* Load all modules in core.  */
 static void
@@ -112,6 +125,7 @@
 {
   /* First of all, initialize the machine.  */
   grub_machine_init ();
+	grub_init_btsym ();
 
   /* Hello.  */
   grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
Makefile.in +5 -1
diff -ru -N -b -B grub2/Makefile.in grub2-backtrace/Makefile.in
@@ -82,7 +82,7 @@
 CLEANFILES =
 MOSTLYCLEANFILES = 
 DISTCLEANFILES = config.status config.cache config.log config.h \
-	Makefile stamp-h include/grub/cpu include/grub/machine
+	Makefile stamp-h include/grub/cpu include/grub/machine tags
 MAINTAINER_CLEANFILES = $(srcdir)/configure $(addprefix $(srcdir)/,$(MKFILES))
 
 # The default target.
@@ -212,6 +212,10 @@
 	@echo "$(distdir).tar.gz is ready for distribution" | \
 	  sed 'h;s/./=/g;p;x;p;x'
 
+tags:
+	  CTAGSF=`ctags --version | grep -i exuberant >/dev/null || echo "-T -w -d"`
+		  find . -name \*.[hcS] | xargs ctags -a --typedefs  $(CTAGSF)
+
 check:
 
 .SUFFIX:
#1 lore raw

Vincent Guffens <[email protected]> writes:

Hi Vincent,

Quoted 4 lines

After having searched for the reason of the unaligned pointer caused by the nested functions bug, I thought that it could be interesting to have a backtrace in grub. It would be triggered in grub_fatal and would print the last few function calls that triggered the problem.

Neat! I would like to have this in GRUB2 for debugging. If Okuji likes it too and you can send in a Changelog entry as well, I will proofread the patch so it can be applied ASAP.

Thanks,
Marco

#2 lore raw
Quoted 11 lines · Marco Gerards

Vincent Guffens <[email protected]> writes:

Hi Vincent,

Quoted 4 lines

After having searched for the reason of the unaligned pointer caused by the nested functions bug, I thought that it could be interesting to have a backtrace in grub. It would be triggered in grub_fatal and would print the last few function calls that triggered the problem.

Quoted 3 lines

Neat! I would like to have this in GRUB2 for debugging. If Okuji likes it too and you can send in a Changelog entry as well, I will proofread the patch so it can be applied ASAP.

cool ! I will wait a little bit for your comments and those from Okuji as there are a few changes I made that I'm not sure if they are very clean, especially those in genmk.rb. I used some "cat config.h" and maybe there are some better ways. Also, I did not mention that the debuging symbol list is somehow redundant with the already existing symbol list but that way, it does not modify anything if the backtrace support is not compiled in.

Should the Changelog entries be included in the patch or is it generated with the cvs log and it should then be sent in the mail body ?

#3 lore raw
Quoted 3 lines · Vincent Guffens

cool ! I will wait a little bit for your comments and those from Okuji as there are a few changes I made that I'm not sure if they are very clean, especially those in genmk.rb.

Good job, Vincent.

Quoted 2 lines

I used some "cat config.h" and
maybe there are some better ways.

Possibly you can substitute a variable in Makefile.in and use the variable.

Quoted 2 lines

Should the Changelog entries be included in the patch or is it generated with the cvs log and it should then be sent in the mail body ?

ChangeLog must be handwritten. As long as it is written well, I don't care about how you send it. Using the Change Log mode in Emacs would help you a lot.

It is still necessary to clean up the patch. Running make twice does not sound good. One easy way is to build a separate backtrace module outside the kernel, and load it as soon as possible, like the normal module.

Also, I think it would be better to rename --enable-backtrace to --enable-debug, so that we can enable all debugging features by one option (when we get more debugging features, this could be useful).

Okuji