backtrace support
Here is a new version of the backtrace support. I have written the ChangeLog and modified the code according to your advices.
To compile with the backtrace support use ./configure --with-debug and load grub2 with the kern_debug module. The kern module will be unloaded automatically after having registered all the kernel symbols in a linked list that also contains the module symbols. Any files that includes <grub/backtrace.h> can now call grub_backtrace().
The functions that handle the linked list with the debug symbols are generic and are put in kern/backtrace.c. The function grub_backtrace() however is architecture dependant and is put in kern/i386/pc/backtrace.c.
This is a problem because I would say that it breaks compilation for other architectures. I think that the other architectures should have a function grub_backtrace() that does nothing, or even better that really does print a backtrace(). However, I don't know how to do it and I can't test it anyway.
I have also slightly modified the way genmk.rb generates the rule to create the modules. I hope this slight change does not do anything silly
that I missed.
I hope this backtrace will be usefull, but not too much !
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
ChangeLog +42 -0
@@ -1,3 +1,45 @@ +2005-08-22 Vincent Guffens <[email protected]> + + * conf/i386-pc.rmk : Added kern/backtrace.c, kern/i386/pc/backtrace.c + in the kernel dependancy list and backtrace.h in the header list. Added a + new module kern_debug.mod. + + * grub2/configure.ac : Added a configure switch : --with-debug. + Turn the gcc optimization flag to -O0 and define a variable GRUB_DEBUG=1 + in Makefile.in if this switch is used. + + * gendebugkern.sh : New file : shell script which generates the debug + symbols for the kernel. + + * genmk.rb : Do not strip the uneeded symbols from the objects if + GRUB_DEBUG=1. Add a variable $(#{prefix}_OTHERDEP) in the dependancy list + of the modules. This variable can be set to something useful in i386-pc.rmk. + Do not compile the module with all the dependances, just use the sources. + + * include/grub/backtrace.h : New file + + * kern/backtrace.c : New file + (grub_register_debug_sym): New function + (grub_unregister_debug_sym): Likewise + (grub_print_debug_sym) : Likewise + + * kern/dl.c : (Un)Register the debug symbols when a module is (un)loaded. + + * kern/err.c (grub_fatal) : Call grub_backtrace() + + * kern/i386/pc/backtrace.c : New file + (get_fp) : New function + (grub_backtrace) : likewise + + * kern/kern_debug.c : New file : This is the source for the new module + kern_debug. It registers the kernel symbol during its initialisation and + does nothing else. It is automatically removed later. + + * kern/main.c (grub_load_modules) : Try to unregister the kern_debug module. + + * Makefile.in : Add a new variable GRUB_DEBUG which is set by configure. Add + the rule to create the kernel symbol list in kern/grub_debug_kern.sym. + 2005-08-20 Yoshinori K. Okuji <[email protected]> * loader/powerpc/ieee1275/linux.c (grub_rescue_cmd_linux): Specify
conf/i386-pc.rmk +9 -4
@@ -28,13 +28,13 @@ kern/i386/dl.c kern/i386/pc/init.c kern/partition.c \ kern/env.c disk/i386/pc/biosdisk.c \ term/i386/pc/console.c \ - symlist.c + symlist.c kern/backtrace.c kern/i386/pc/backtrace.c kernel_img_HEADERS = arg.h boot.h device.h disk.h dl.h elf.h env.h err.h \ file.h fs.h kernel.h loader.h misc.h mm.h net.h partition.h \ pc_partition.h rescue.h symbol.h term.h types.h \ machine/biosdisk.h machine/boot.h machine/console.h machine/init.h \ machine/memory.h machine/loader.h machine/time.h machine/vga.h \ - machine/vbe.h + machine/vbe.h backtrace.h kernel_img_CFLAGS = $(COMMON_CFLAGS) kernel_img_ASFLAGS = $(COMMON_ASFLAGS) kernel_img_LDFLAGS = -nostdlib -Wl,-N,-Ttext,8200
@@ -93,7 +93,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/backtrace.c kern/i386/pc/backtrace.c grub_emu_LDFLAGS = $(LIBCURSES)
@@ -113,7 +113,7 @@ terminal.mod fshelp.mod chain.mod multiboot.mod amiga.mod \ apple.mod pc.mod sun.mod loopback.mod reboot.mod halt.mod \ help.mod default.mod timeout.mod configfile.mod vbe.mod \ - vesafb.mod vbetest.mod vbeinfo.mod search.mod + vesafb.mod vbetest.mod vbeinfo.mod search.mod kern_debug.mod # For _chain.mod. _chain_mod_SOURCES = loader/i386/pc/chainloader.c
@@ -274,3 +274,8 @@ # For search.mod. search_mod_SOURCES = commands/search.c search_mod_CFLAGS = $(COMMON_CFLAGS) + +# For kern_debug.mod +kern_debug_mod_OTHERDEP = kern/grub_debug_kern.sym +kern_debug_mod_SOURCES = kern/kern_debug.c +kern_debug_mod_CFLAGS = $(COMMON_CFLAGS)
config.h.in +3 -0
@@ -16,6 +16,9 @@ /* Define it to either end or _end */ #undef END_SYMBOL +/* enable some debug mechanisms */ +#undef GRUB_DEBUG + /* Define if C symbols get an underscore after compilation */ #undef HAVE_ASM_USCORE
configure.ac +20 -0
@@ -57,10 +57,20 @@ AC_TRY_COMPILE(, , size_flag=yes, size_flag=no) ]) if test "x$size_flag" = xyes; then + if test "$with_debug" == "yes" + then + tmp_CFLAGS="$tmp_CFLAGS -O0" + else tmp_CFLAGS="$tmp_CFLAGS -Os" + fi + else + if test "$with_debug" == "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" fi + fi # Force no alignment to save space on i386. if test "x$host_cpu" = xi386; then
@@ -108,6 +118,16 @@ # This is not a "must". AC_PATH_PROG(RUBY, ruby) +# take care of the configure arguments + +AC_ARG_WITH(debug, [ --with-debug enable some debug mechanisms]) +if test "$with_debug" == "yes"; then + AC_DEFINE([GRUB_DEBUG], [], [enable some debug mechanisms]) + AC_SUBST(GRUB_DEBUG,1) +else + AC_SUBST(GRUB_DEBUG,0) +fi + # For cross-compiling. if test "x$build" != "x$host"; then AC_CHECK_PROGS(BUILD_CC, [gcc egcs cc],
gendebugkern.sh +14 -0
@@ -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 +8 -4
@@ -119,14 +119,18 @@ #{@name}: #{pre_obj} #{mod_obj} -rm -f $@ $(LD) -r -d -o $@ $^ +ifeq ($(GRUB_DEBUG),1) + $(STRIP) --strip-debug -K grub_mod_init -K grub_mod_fini -R .note -R .comment $@ +else $(STRIP) --strip-unneeded -K grub_mod_init -K grub_mod_fini -R .note -R .comment $@ +endif #{pre_obj}: #{objs_str} -rm -f $@ $(LD) -r -d -o $@ $^ -#{mod_obj}: #{mod_src} - $(CC) $(CPPFLAGS) $(CFLAGS) $(#{prefix}_CFLAGS) -c -o $@ $< +#{mod_obj}: $(#{prefix}_OTHERDEP) #{mod_src} + $(CC) $(CPPFLAGS) $(CFLAGS) $(#{prefix}_CFLAGS) -c -o $@ #{mod_src} #{mod_src}: moddep.lst genmodsrc.sh sh $(srcdir)/genmodsrc.sh '#{mod_name}' $< > $@ || (rm -f $@; exit 1)
@@ -147,8 +151,8 @@ flag = if /\.c$/ =~ src then 'CFLAGS' else 'ASFLAGS' end dir = File.dirname(src) - "#{obj}: #{src} - $(CC) -I#{dir} -I$(srcdir)/#{dir} $(CPPFLAGS) $(#{flag}) $(#{prefix}_#{flag}) -c -o $@ $< + "#{obj}: $(#{prefix}_OTHERDEP) #{src} + $(CC) -I#{dir} -I$(srcdir)/#{dir} $(CPPFLAGS) $(#{flag}) $(#{prefix}_#{flag}) -c -o $@ #{src} #{dep}: #{src} set -e; \
Les fichiers binaires grub2/grub_disk et grub2-backtrace/grub_disk sont différents.
include/grub/backtrace.h +12 -0
@@ -0,0 +1,12 @@ +#ifndef GRUB_BACKTRACE_H +#define GRUB_BACKTRACE_H + +#include <grub/types.h> +#include <grub/symbol.h> + +void EXPORT_FUNC (grub_register_debug_sym ) (const char *,void *,grub_size_t ); +void EXPORT_FUNC (grub_unregister_debug_sym ) (void *); +int EXPORT_FUNC (grub_print_debug_sym ) (grub_addr_t ); +void EXPORT_FUNC (grub_backtrace ) (void ); + +#endif
kern/backtrace.c +87 -0
87 lines not on this page · raw message
kern/dl.c +3 -0
@@ -29,6 +29,7 @@#include <grub/file.h> #include <grub/env.h> #include <grub/cache.h> +#include <grub/backtrace.h> #if GRUB_HOST_SIZEOF_VOID_P ==4
@@ -216,6 +217,7 @@if (sym->mod == mod) { *p = q; +grub_unregister_debug_sym (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_register_debug_sym (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;
kern/err.c +4 -0
@@ -20,6 +20,7 @@#include <grub/err.h> #include <grub/misc.h> +#include <grub/backtrace.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 +35 -0
@@ -0,0 +1,35 @@ +#include <grub/backtrace.h> +#include <grub/types.h> +#include <grub/misc.h> + +#ifdef GRUB_DEBUG + +#define BTSYM_DISPLAY 12 + +static grub_addr_t *get_fp (void ) { +grub_addr_t * a; + asm ("movl %%ebp, %0;" + :"=r" (a)/* output */ + ); +return a; +} + +void grub_backtrace (void ) { +grub_addr_t * a; +int i =1 ; + +grub_printf ("\nBacktrace:\n" ); + a =get_fp (); + +while ( (i <=BTSYM_DISPLAY ) &&grub_print_debug_sym (*(a+1 )) ) { + a= (grub_addr_t *)(*a); + i++; + } +} + + +#else + +inline void grub_backtrace (void ) {do {}while (0 );}; + +#endif
kern/kern_debug.c +47 -0
47 lines not on this page · raw message
kern/main.c +8 -0
@@ -35,6 +35,7 @@ {struct grub_module_info *modinfo;struct grub_module_header *header; +grub_dl_t mod;grub_addr_t modbase; modbase =grub_arch_modules_addr ();
@@ -55,6 +56,13 @@/* Add the region where modules reside into dynamic memory. */ grub_mm_init_region ((void *) modinfo, modinfo->size); + +/* remove the kern_debug module */ + mod =grub_dl_get ("kern_debug" ); +if (mod && (grub_dl_unref (mod) <=0 )) +grub_dl_unload (mod); + + }/* Write hook for the environment variables of root. Remove surrounding
Makefile.in +5 -0
@@ -69,6 +69,8 @@ LIBCURSES = @LIBCURSES@ LIBLZO = @LIBLZO@ +### Debug variable +GRUB_DEBUG = @GRUB_DEBUG@ ### General variables. RMKFILES = $(addprefix conf/,i386-pc.rmk powerpc-ieee1275.rmk)
@@ -239,5 +240,8 @@ .PHONY: all install install-strip uninstall clean mostlyclean distclean .PHONY: maintainer-clean info dvi dist check +kern/grub_debug_kern.sym: kernel.exec + ./gendebugkern.sh kernel.exec > $@ + # Prevent an overflow. .NOEXPORT:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Quoted 5 lines · Vincent Guffens
This is a problem because I would say that it breaks compilation for other architectures. I think that the other architectures should have a function
grub_backtrace()that does nothing, or even better that really does print abacktrace(). However, I don't know how to do it and I can't test it anyway.
For sparc64-ieee1275, there is a ctrace command in open firmware which does that work. grub_backtrace() could do a grub_ieee1275_interpret("ctrace",NULL);
thengrub_ieee1275_exit();
To make ctrace work the ELF has to be unstripped - for now I hardcoded it in conf/sparc64-ieee1275.rmk. To my experience, -O1 produces an easier to understand ASM than -O0 (sparc64-ieee1275 allows to run in step-by-step or breakpoint mode) because -O0 does really dumb things that is very verbose in ASM line number. But I don't know in details what -O1 does, and if it has drawbacks in optimisation (I think about local functions).
Quoted 1 line
I hope this backtrace will be usefull, but not too much !
I have already found a use for it : I can't make vbetest work, it fails with out of range pointer.
I hope it will be committed soon.
Oh, by the way, I'm trying to implement the OpenFirmware framebuffer primitives - for now with no luck - so we can have it on ieee1275 ports. There are lots of FCODE examples on Google, but all for PPC. My current problem is how to allocate memory for the framebuffer itself, and how to pass the address "to frame_buffer_adr" (the usual FCODE way to store a value), if possible without using grub_ieee1275_interpret.
Vincent Pelletier
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
iD8DBQFDCtRfFEQoKRQyjtURArwAAJ0QJ9cOK7WTW40UKVdf5O12vU9x5ACfcmXC wmVVj2BJem/7hlyZ4boBMp8=
=lCgj
-----END PGP SIGNATURE-----
___________________________________________________________________________
Appel audio GRATUIT partout dans le monde avec le nouveau Yahoo! Messenger Téléchargez cette version sur http://fr.messenger.yahoo.com
conf/i386-pc.rmk line 281
Quoted 4 lines · Vincent Guffens
conf/i386-pc.rmk +4
-0+# For kern_debug.mod +kern_debug_mod_OTHERDEP = kern/grub_debug_kern.sym +kern_debug_mod_SOURCES = kern/kern_debug.c +kern_debug_mod_CFLAGS = $(COMMON_CFLAGS)
I think it is a really good idea to make a variable for dependencies, but I would suggest a different name for this. Can this be just kern_debug_mod_DEPENDENCIES?
configure.ac
Quoted 3 lines
configure.ac +3
-0+# take care of the configure arguments + +AC_ARG_WITH(debug, [ --with-debug enable some debug
mechanisms])
I think it would be better to use AC_ARG_ENABLE with --enable-debug, because --with is used to specify external programs in autoconf.
gendebugkern.sh 2005-08-22 18:47:48.000000000 +0200 line 1
Quoted 5 lines
diff -ru -N -b -B
grub2/gendebugkern.shgrub2-backtrace/gendebugkern.shgendebugkern.sh 2005-08-22 18:47:48.000000000 +0200 renamed from gendebugkern.sh 1970-01-01 01:00:00.000000000 +0100 +1
-0@@ -0,0 +1,14 @@ +#!/bin/bash
Please do not use /bin/bash for portability. It should be:
#! /bin/sh
gendebugkern.sh 2005-08-22 18:47:48.000000000 +0200 line 2
Quoted 1 line
gendebugkern.sh 2005-08-22 18:47:48.000000000 +0200 renamed from gendebugkern.sh 1970-01-01 01:00:00.000000000 +0100 +1
-0+void EXPORT_FUNC(grub_register_debug_sym) (const char*, void*,
grub_size_t);
gendebugkern.sh 2005-08-22 18:47:48.000000000 +0200 line 5
Quoted 3 lines
gendebugkern.sh 2005-08-22 18:47:48.000000000 +0200 renamed from gendebugkern.sh 1970-01-01 01:00:00.000000000 +0100 +3
-0+void EXPORT_FUNC(grub_unregister_debug_sym) (void *); +int EXPORT_FUNC(grub_print_debug_sym) (grub_addr_t); +void EXPORT_FUNC(grub_backtrace) (void);
Why do you need to export these functions? Are they used outside the kernel?
Okuji
Hi,
I will make change to the patch according to these comments.
Quoted 11 lines
Quoted 1 line
+void
EXPORT_FUNC(grub_register_debug_sym)(const char*, void*,
grub_size_t);include/grub/backtrace.h line 10
Quoted 3 lines
include/grub/backtrace.h +3
-0+void EXPORT_FUNC (grub_unregister_debug_sym ) (void *); +int EXPORT_FUNC (grub_print_debug_sym ) (grub_addr_t ); +void EXPORT_FUNC (grub_backtrace ) (void );Why do you need to export these functions? Are they used outside the kernel?
grub_register_debug_sym is used by the module responsible for loading the debug symbols. grub_backtrace could be used by other modules.
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
Vincent Guffens <[email protected]> writes:
Hi Vincent,
Quoted 2 lines
Here is a new version of the backtrace support. I have written the ChangeLog and modified the code according to your advices.
Thanks a lot! Here are still some comments and questions about the patch.
Quoted 5 lines
To compile with the backtrace support use ./configure --with-debug and load grub2 with the
kern_debugmodule. The kern module will be unloaded automatically after having registered all the kernel symbols in a linked list that also contains the module symbols. Any files that includes <grub/backtrace.h> can now callgrub_backtrace().
I'm not too familiar with autoconf, but I had the idea that --enable-debug would be more logical and that --with is to tell configure where to find libraries. According to the autoconf manual it is to add external software like libraries.
I have the idea that another array for the symbols is not required. I think we can use one common table. The symbol name and symbol address are already in there. The hard thing is would getting the size, perhaps we could leave that open and fill it in with a function, or can we use a ld/gcc feature to do that? This will make the backtrace code simpler and it saves memory. Do you think that is somehow possible?
Quoted 10 lines
The functions that handle the linked list with the debug symbols are generic and are put in
kern/backtrace.c. The functiongrub_backtrace()however is architecture dependant and is put inkern/i386/pc/backtrace.c.This is a problem because I would say that it breaks compilation for other architectures. I think that the other architectures should have a function
grub_backtrace()that does nothing, or even better that really does print abacktrace(). However, I don't know how to do it and I can't test it anyway.
I can fix this for the PPC when it is applied, that's not a problem.
Quoted 3 lines
I have also slightly modified the way genmk.rb generates the rule to create the modules. I hope this slight change does not do anything silly that I missed.
Hopefully Okuji can have a look at these changes. I don't know ruby and this code. :(
Quoted 1 line
I hope this backtrace will be usefull, but not too much !
It will be. :-)
ChangeLog line 5
Quoted 3 lines
ChangeLog +3
-0+ * conf/i386-pc.rmk : Added kern/backtrace.c, kern/i386/pc/backtrace.c + in the kernel dependancy list and backtrace.h in the header list. Added a + new module kern_debug.mod.
Please be specific about to which variables they are added. You can see how we did that in the past in ChangeLog.
+
ChangeLog line 9
Quoted 3 lines
ChangeLog +3
-0+ * grub2/configure.ac : Added a configure switch : --with-debug. + Turn the gcc optimization flag to -O0 and define a variable GRUB_DEBUG=1 + in Makefile.in if this switch is used.
Same here.
ChangeLog line 12
Quoted 2 lines
ChangeLog +2
-0+ * gendebugkern.sh : New file : shell script which generates the debug + symbols for the kernel.
Just saying "New file." would be enough. After that you can say "Likewise." for every other new files that follows this line. Please make sure the ":" follows the filename directly without a space, and start the sentence with a capital letter.
Same for the rest of the changelog and please tell which function is changed. Have a look at the ChangeLog for an example.
As for the sourcecode, I have seen a lot of incorrect indention, etc. It does not yet comply with the GCS (which is the GNU Coding Style and also describes the changelog format:
http://www.gnu.org/prep/standards/). Can you please have a look at this, a consistent coding style is very important for a big problem like GRUB. If you want me to help you or point you to some problems in the code which you can't find after reading this, just ask me so I can help you.
Thanks,
Marco
Quoted 8 lines
I have the idea that another array for the symbols is not required. I think we can use one common table. The symbol name and symbol address are already in there. The hard thing is would getting the size, perhaps we could leave that open and fill it in with a function, or can we use a ld/gcc feature to do that? This will make the backtrace code simpler and it saves memory. Do you think that is somehow possible?
This is true, another list of symbols is not required for this backtrace support but it makes the implementation a lot less intrusive. It would be possible to use some #ifdef here and there around the structures that are used already to add a field for the symbol size but it would not be very nice. If you turn the debug flag off, the code comes back to the original so I thought it would not be a problem.
However, I found using a simple example that using -O2 still allows for using the frame pointer. This is to be verified but if it turns to be usable, than maybe using the --enable-debug will be less a problem and more popular so it would then be wise not to have the kernel symbol list twice. What do you think ?
To correct the rest, I think I will read the GCS in detail.
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
Quoted 8 lines · Vincent Guffens
Quoted 7 lines
I have the idea that another array for the symbols is not required. I think we can use one common table. The symbol name and symbol address are already in there. The hard thing is would getting the size, perhaps we could leave that open and fill it in with a function, or can we use a ld/gcc feature to do that? This will make the backtrace code simpler and it saves memory. Do you think that is somehow possible?
Quoted 7 lines
This is true, another list of symbols is not required for this backtrace support but it makes the implementation a lot less intrusive. It would be possible to use some #ifdef here and there around the structures that are used already to add a field for the symbol size but it would not be very nice. If you turn the debug flag off, the code comes back to the original so I thought it would not be a problem.
It won't be that many ifdefs. I think that would be better than yet another table.
Quoted 5 lines
However, I found using a simple example that using -O2 still allows for using the frame pointer. This is to be verified but if it turns to be usable, than maybe using the --enable-debug will be less a problem and more popular so it would then be wise not to have the kernel symbol list twice. What do you think ?
Right, I think -O2 is not a problem if you do not show line numbers. But if you show line numbers -O2 is not always accurate AFAIK because gcc can reorder lines.
Quoted 1 line
To correct the rest, I think I will read the GCS in detail.
Cool!
Thanks,
Marco