Debugging GRUB2 with GDB and QEMU
I wrote some GDB macros that might be helpful for people that want to debug GRUB 2 modules with QEMU. [1]
[1] http://NetBSD.sk/~lkundrak/misc/grub2-gdb/
The file genmk.rb.diff is a patch to makefile-generator script, that makes the build system leave output files with debugging information. Of course, one has have ruby interpreter to regenerate the makefiles and compile GRUB with -g afterwards.
Second file is .gdbinit, the GDB initialization file. It is commented, so there is no need to describe it here. It basically passes the information extracted from list headed by grub_dl_head to the perl script, gmodule.pl, and loads its output. It also sets a breakpoint whose command list contains macro for loading proper symbol file each time a module is loaded. (I know the work could be done without the help of a Perl script, but it would be far more complicated, I guess)
Another note worth mentioning is that you'll probably want to add aliases for some functions, so that all gdb facilities will work correctly. You will at least want to define the symbol 'main' for backtraces to stop at the right place. Some facilities also want the function 'malloc' to be defined. So, you'll probably have to add something like
.globl main
main = codestart
to assembly language sources or
malloc() __attribute__ ((alias("grub_malloc")));
to C files.
I hope this will be useful to at least some of you. Best regards! Lubo.
Signature · 3 lines
o Lubomir Kundrak *O* <[email protected]>, http://skosi.org/~lkundrak/ (_) SKOSI -- Slovak OpenSource Initiative
Quoted 2 lines · Lubomir Kundrak
I wrote some GDB macros that might be helpful for people that want to debug GRUB 2 modules with QEMU. [1]
Great!
I guess it is necessary to compile the binary with -O0 or -O1 to get a reasonable backtrace... I'd appreciate if you can write a HOWTO. :)
Okuji
Hi Lubomir,
Thanks for the scripts. It is just what I was looking for. Meanwhile, since my last email, I have done something foolish. I've coded in grub's sources just the same algorithm you provide but I've clumbered the sources with new functions those purpose is only for debugging while you are doing all the job outside of the sources, and one might think it's better the way you do.
1) Got Ruby and change the makefiles just the way you do to get .elf files with debugging info. (I've setup for a different extension but I'll change for .elf)
2) Added a very few lines to grub_load_segments() to get the name of the section along with its load address in mod->segment.
(You might want that to get ride of the readelf and perl script)
3) Build in a buffer, during build_load_core() and grub_load_segments() processing, the string
"filename.elf load_addr -s .text 0x23888 ... "
print it on the screen with grub_dprintf.
4) Call a function with that string argument to be caught by a .gdbinit script to launch the add-symbol-file command before the call to (mod->init(mod)).
I first thought I could just get along with add-symbol-file command to gdb, but I was frustrated I could not set breakpoint ahead of time and then have it resolved when the module is loaded .... I was in the mood of doing C development so I've been overkilling again :
Before I even finished the .gdbinit script, I've changed my mind and done "one" step further :
5) Added the "filename.elf -s .text 0x23234 ... " to the list of link_maps maintained by ld.so (actually ld.so complains a little but I'll fix that later) and do the call to _ld_debug_state() to generate a shlib event in gdb.
6) Changed the solib-svr4.c in gdb 6.1 so that it can handle loading ELF file with instructions to load each segment separately at the right place.
So I am running with a modified gdb that understand grub's way of loading modules on the fly.
If you think it can be of general interest, we can start from here to define a GDB interface to Grub 2. Otherwise, I'll use Ludomir external scripts since I don't want to maintain separate source files from the official delivery.
Note that if we change somewhat the way grub load modules and allocate first the memory to contain all the segments, lying right one after another in memory, we won't have to change gdb anymore (step 6).
Best regards,
_________________________________________
Eric Salomé - Paris, France
Hi, thanks for the help with debugging modules. I have a module that I created and still need a little assistance in getting gdb to break in my
module's grub_cmd_*. I am able to connect to QEMU with gdb and when I
load the module, gdb loads the symbol table for my module.
But, when I set a breakpoint at the address of the first operation in my module, gdb doesn't break when I execute the command in GRUB. If it has something to do with the aliases you mention at the bottom of your message, could you elaborate a little more or direct me in the right direction?
Thanks
David Johnson
Quoted 39 lines · Lubomir Kundrak
I wrote some GDB macros that might be helpful for people that want to debug GRUB 2 modules with QEMU. [1]
[1] http://NetBSD.sk/~lkundrak/misc/grub2-gdb/
The file genmk.rb.diff is a patch to makefile-generator script, that makes the build system leave output files with debugging information. Of course, one has have ruby interpreter to regenerate the makefiles and compile GRUB with -g afterwards.
Second file is .gdbinit, the GDB initialization file. It is commented, so there is no need to describe it here. It basically passes the information extracted from list headed by
grub_dl_headto the perl script, gmodule.pl, and loads its output. It also sets a breakpoint whose command list contains macro for loading proper symbol file each time a module is loaded. (I know the work could be done without the help of a Perl script, but it would be far more complicated, I guess)Another note worth mentioning is that you'll probably want to add aliases for some functions, so that all gdb facilities will work correctly. You will at least want to define the symbol 'main' for backtraces to stop at the right place. Some facilities also want the function 'malloc' to be defined. So, you'll probably have to add something like
.globl main
main = codestartto assembly language sources or
malloc()__attribute__((alias("grub_malloc")));to C files.
I hope this will be useful to at least some of you. Best regards! Lubo.
Hi Lubomir,
I've tested your scripts.
They are great.
So I changed my code in kern/dl.c to reflect your .elf convention and I have all ready to use
With -DGRUB_COMPAT_GDB
You need to recompile gdb with a modified solib-svr4.c file that allows an ELF file to be loaded at different non-contigus memory locations, one for each section.
All is automatic using ld.so interface to gdb and you can set pending breakpoints waiting for the "shared lib" (the grub module) to be loaded.
(ld.so doesn't complain anymore when the program quits, I've added a grub_unload_all() before exiting grub-emu for modules loaded with the "insmod" command)
Without -DGRUB_COMPAT_GDB
Every thing is setup so that before calling grub_dl_mod_init() there is a call to grub_gdb_load_mod(mod) and mod->elf contains the string Filename.elf -s .text 0x233 -s .rodata 0x236
Ready to be feed to a .gdbinit script that generates the add-symbol-file command to be swallowed by any standard gdb.
I've noticed that the attachment files I've sent with my previous emails were not received on Grub 2 mailing list, so that I can send you (and to others) my source code if you wish to your personal email address.
Source code is ok, should work on both 32-bits and 64-bits architectures, though I don't do too many checks to prevent buffer overflow in grub-emu.
I hope you might find these source extensions to kern/dl.c useful. It's a great pleasure to be able to dynamically load modules and do the debugging task at ease.
Thanks again Lubomir. Your scripts have been a great help to finalize this GRUB / GDB interface.
PS: the following lines in genmk.rb didn't work too well on my pc (Ubuntu) :
+#{exec}: #{pre_obj} #{mod_obj}
-rm -f $@
$(CC) $(#{prefix}_LDFLAGS) $(LDFLAGS) -Wl,-r,-d -o $@ $^
ld (when called by gcc) complains it can't find -lgcc_s, so I changed back to
$(LD) $(#{prefix}_LDFLAGS) $(LDFLAGS) -r -d -o $@ $^Best Regards,
_______________________________________
Eric Salomé - Paris, France
Quoted 56 lines
-----Message d'origine-----
De : [email protected] [mailto:grub-devel- [email protected]] De la part de Lubomir Kundrak Envoyé : vendredi 12 mai 2006 15:20
À : [email protected]
Objet : Debugging GRUB2 with GDB and QEMUI wrote some GDB macros that might be helpful for people that want to debug GRUB 2 modules with QEMU. [1]
[1] http://NetBSD.sk/~lkundrak/misc/grub2-gdb/
The file genmk.rb.diff is a patch to makefile-generator script, that makes the build system leave output files with debugging information. Of course, one has have ruby interpreter to regenerate the makefiles and compile GRUB with -g afterwards.
Second file is .gdbinit, the GDB initialization file. It is commented, so there is no need to describe it here. It basically passes the information extracted from list headed by
grub_dl_headto the perl script, gmodule.pl, and loads its output. It also sets a breakpoint whose command list contains macro for loading proper symbol file each time a module is loaded. (I know the work could be done without the help of a Perl script, but it would be far more complicated, I guess)Another note worth mentioning is that you'll probably want to add aliases for some functions, so that all gdb facilities will work correctly. You will at least want to define the symbol 'main' for backtraces to stop at the right place. Some facilities also want the function 'malloc' to be defined. So, you'll probably have to add something like
.globl main
main = codestartto assembly language sources or
malloc()__attribute__((alias("grub_malloc")));to C files.
I hope this will be useful to at least some of you. Best regards! Lubo.
--
o Lubomir Kundrak
*O* <[email protected]>, http://skosi.org/~lkundrak/
(_) SKOSI -- Slovak OpenSource Initiative_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel
I noticed, that when any segment is 0 bytes long and starts at 0x0, gdb desn't resolve symbols correctly.
I have motified the gmodule.pl to reflect this. Please check an updated version.
regards,
lubo
Quoted 85 lines · devShadow
X-Original-To: [email protected]
Delivered-To: [email protected]
Date: Sat, 13 May 2006 14:45:37 -0400
From: devShadow <[email protected]>
User-Agent: Mozilla Thunderbird 1.0.7 (Windows/20050923)
X-Accept-Language: en-us, en
To: The development of GRUB 2 <[email protected]>
In-Reply-To: <[email protected]>
X-Virus-Scanned: Symantec AntiVirus Scan Engine
Subject: Re: Debugging GRUB2 with GDB and QEMU
X-BeenThere: [email protected]
X-Mailman-Version: 2.1.5
Precedence: list
Reply-To: The development of GRUB 2 <[email protected]>
List-Id: The development of GRUB 2 <grub-devel.gnu.org>
List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/grub-devel>,
<mailto:[email protected]?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/pipermail/grub-devel>
List-Post: <mailto:[email protected]>
List-Help: <mailto:[email protected]?subject=help>
List-Subscribe: <http://lists.gnu.org/mailman/listinfo/grub-devel>,
<mailto:[email protected]?subject=subscribe>
Errors-To: [email protected]Hi, thanks for the help with debugging modules. I have a module that I created and still need a little assistance in getting gdb to break in my
module's grub_cmd_*. I am able to connect to QEMU with gdb and when I
load the module, gdb loads the symbol table for my module.But, when I set a breakpoint at the address of the first operation in my module, gdb doesn't break when I execute the command in GRUB. If it has something to do with the aliases you mention at the bottom of your message, could you elaborate a little more or direct me in the right direction?
Thanks
David Johnson
Quoted 30 lines · Lubomir Kundrak
I wrote some GDB macros that might be helpful for people that want to debug GRUB 2 modules with QEMU. [1]
[1] http://NetBSD.sk/~lkundrak/misc/grub2-gdb/
The file genmk.rb.diff is a patch to makefile-generator script, that makes the build system leave output files with debugging information. Of course, one has have ruby interpreter to regenerate the makefiles and compile GRUB with -g afterwards.
Second file is .gdbinit, the GDB initialization file. It is commented, so there is no need to describe it here. It basically passes the information extracted from list headed by
grub_dl_headto the perl script, gmodule.pl, and loads its output. It also sets a breakpoint whose command list contains macro for loading proper symbol file each time a module is loaded. (I know the work could be done without the help of a Perl script, but it would be far more complicated, I guess)Another note worth mentioning is that you'll probably want to add aliases for some functions, so that all gdb facilities will work correctly. You will at least want to define the symbol 'main' for backtraces to stop at the right place. Some facilities also want the function 'malloc' to be defined. So, you'll probably have to add something like
.globl main
main = codestartQuoted 9 lines
to assembly language sources or
malloc()__attribute__((alias("grub_malloc")));to C files.
I hope this will be useful to at least some of you. Best regards! Lubo.
_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel
Signature · 3 lines
o Lubomir Kundrak *O* <[email protected]>, http://skosi.org/~lkundrak/ (_) SKOSI -- Slovak OpenSource Initiative
Thanks, I now have gdb running as I expect in my module.
David
Quoted 8 lines · Lubomir Kundrak
I noticed, that when any segment is 0 bytes long and starts at
0x0, gdb desn't resolve symbols correctly.
I have motified the gmodule.pl to reflect this. Please check an updated version.regards,
lubo
Quoted 6 lines · Yoshinori K. Okuji
Message-Id: <[email protected]>
Great!
I guess it is necessary to compile the binary with -O0 or -O1 to get a reasonable backtrace... I'd appreciate if you can write a HOWTO. :)
On i386 GCC generates full stack frames even with -O optizations. It only omits frame pointer on architectures on which it doesn't interfere with debugging.
I started a small HOWTO recently, so here is it: [1]. Enjoy :o)
[1] http://NetBSD.sk/~lkundrak/misc/grub2-gdb/howto.html
Quoted 2 lines
Okuji
Lubo
Signature · 3 lines
o Lubomir Kundrak *O* <[email protected]>, http://skosi.org/~lkundrak/ (_) SKOSI -- Slovak OpenSource Initiative
Quoted 8 lines · Lubomir Kundrak
On Sat, May 13, 2006 at 06:38:26AM +0200, Yoshinori K. Okuji wrote:
On i386 GCC generates full stack frames even with -O optizations. It only omits frame pointer on architectures on which it doesn't interfere with debugging.
I started a small HOWTO recently, so here is it: [1]. Enjoy :o)
[1] http://NetBSD.sk/~lkundrak/misc/grub2-gdb/howto.html
This looks like very interesting stuff, but wouldn't it be better to put this in the wiki? Please see http://grub.enbug.org.
-Hollis
Quoted 8 lines · Hollis Blanchard
Quoted 6 lines · Lubomir Kundrak
On Sat, May 13, 2006 at 06:38:26AM +0200, Yoshinori K. Okuji wrote:
On i386 GCC generates full stack frames even with -O optizations. It only omits frame pointer on architectures on which it doesn't interfere with debugging.
Quoted 7 lines
Quoted 2 lines
I started a small HOWTO recently, so here is it: [1]. Enjoy :o)
[1] http://NetBSD.sk/~lkundrak/misc/grub2-gdb/howto.htmlThis looks like very interesting stuff, but wouldn't it be better to put this in the wiki? Please see http://grub.enbug.org.
-Hollis
Yeah, I thought about it, but I don't have Internet connection at home, so I am unable to edit it in wiki when offline, so it's easier for me to have it as a standalone file. If anyone wants to change anything, I will accept a patch for that hapilly.
Signature · 3 lines
o Lubomir Kundrak *O* <[email protected]>, http://skosi.org/~lkundrak/ (_) SKOSI -- Slovak OpenSource Initiative
Quoted 11 lines · Lubomir Kundrak
Quoted 6 lines · Yoshinori K. Okuji
Message-Id: <[email protected]>
Great!
I guess it is necessary to compile the binary with -O0 or -O1 to get a reasonable backtrace... I'd appreciate if you can write a HOWTO. :)
On i386 GCC generates full stack frames even with -O optizations. It only omits frame pointer on architectures on which it doesn't interfere with debugging.
The problem is not the frame pointers. With -Os, gcc inlines functions used only once. For example, grub_dl_add is eliminated in gcc 4.x.
Okuji