I have a simple project which comes to be a very very simple kernel. I have some issues with the code and tried to debug it using GDB. Here's the first problem the code is not an executable to be started and debugged in the GDB.
After, I tried to debug via QEMU, but since I archived my codes into .iso to be able to run it on QEMU. When I tried to set breakpoint in the source file (e.g. main function of kernel.cpp) gdb said that
No symbol table is loaded. Use the "file" command.
Make breakpoint pending on future shared library load? (y or [n])
After I say yes, run the program - no breakpoints met.
I tried to google, but only things I find references to "How to debug linux kernel with gdb and qemu" and I couldn't find a way to adapt these suggestions with the "kernel" I have.
Here is my code
kernel.cpp
void printf(const char *string)`
{
volatile unsigned char *vmem = (volatile unsigned char *)0xb8000;
vmem[0] = 0x2f;
vmem[1] = 0x4b;
vmem[2] = 0x2f;
vmem[3] = 0x4f;
}
extern "C" void kernelMain()
{
printf("Hello world");
while(1);
}
loader.s
section .multiboot
header_start:
dd 0xe85250d6
dd 0
dd header_end - header_start
dd 0x100000000 - (0xe85250d6 + 0 + (header_end - header_start))
dw 0
dw 0
dd 8
header_end:
global start
global printf
extern kernelMain
section .text
bits 32
start:
call kernelMain
hlt
_loop:
jmp _loop
linker.ld
ENTRY(start)
OUTPUT_FORMAT("elf64-x86-64")
OUTPUT_ARCH(i386:x86-64)
SECTIONS {
. = 1M;
.boot :
{
*(.multiboot)
}
.text ALIGN(4k):
{
*(.text)
}
.bss :
{
*(.bss)
}
.data :
{
*(.data)
}
.rodata :
{
*(.rodata)
}
/DISCARD/ :
{
*(.fini_array*)
*(.note.*)
*(.eh_frame*)
*(.comment)
}
}
and finally
makefile
GXXPARAMS = -m64 -march=x86-64 -fno-use-cxa-atexit -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fno-leading-underscore -g
objects = kernel.o loader.o
.PHONY : all clean pack deploy target
target : clean all pack
all : os.bin
%.o : %.cpp
gcc -c $(GXXPARAMS) -o $@ $<
%.o : %.s
nasm -felf64 -o $@ $<
os.bin : $(objects)
ld -n -T linker.ld -o $@ $^
clean :
rm -fr *.o *.iso *.bin
pack : all
cp os.bin iso_tmp/boot/
grub-mkrescue -o os.iso iso_tmp/
deploy : pack
qemu-system-x86_64 -cdrom os.iso
UPDATE
In a response to @Michael Petch's question in comments - here's the GDB invoking command line
gdb qemu-system-x86_64
after GDB started, I write
b kernel.cpp:main
then y then
r -cdrom os.iso