How can I use gdb to debug qemu? I have been googling but couldn't find anything concrete.
Asked
Active
Viewed 1.4k times
7
-
@ismail Hi..Can anyone help me get started with debugging qemu with gdb? The information provided below is not very clear to me. Configuring the debug flags alone is not meeting my requirements. (May be I am missing something totally) My requirement is to use gdb to debug segfaults happening to my custom qemu. Please note that I do NOT want to debug something else (kernel) with qemu and gdb. – Sandhya Kumar Jul 22 '15 at 12:53
2 Answers
10
I got an error with GDB 7.5 -> "Error accessing memory address"
It seems there is a problem with "Position Independet Executables" ...so use
./configure --enable-debug --disable-pie
and debug should work.

Max Mustermann
- 351
- 2
- 12
-
1+1 for --disable-pie. My gdb 6.7.1 sees the same issue if pie is enabled. – webbertiger Nov 06 '13 at 02:17
-
`--disable-pie` is not needed at least as of Ubuntu 18.04 GCC 7.4 QEMU 4.0.0. You will likely want PIE whenever possible to mitigate VM breakouts vulnerabilities: https://stackoverflow.com/questions/2463150/what-is-the-fpie-option-for-position-independent-executables-in-gcc-and-ld/51308031#51308031 – Ciro Santilli OurBigBook.com Aug 27 '19 at 18:01
6
Try the following:
./configure --enable-debug
By default qemu builds with "CFLAGS = -O2 -g" option which somehow doesn't allow debug symbols to be added. Using --enable-debug option will mean -O2 will not be added.

Sukanto
- 992
- 3
- 11
- 21
-
At least in Ubuntu 18.04 GCC 7.4 4.0.0, the default build does `-O2 -g` [according to `make V=1`](https://stackoverflow.com/questions/5820303/how-do-i-force-make-gcc-to-show-me-the-commands) which does add debug symbols and I can see the source. The problem is that a lot of stuff is optimized out and so you lose visibility and jump around weirdly. Without `-O2` we have the default `-O0`: https://stackoverflow.com/questions/1778538/how-many-gcc-optimization-levels-are-there Linux kernel boot slowdown was about 3x. – Ciro Santilli OurBigBook.com Aug 27 '19 at 17:59