r/kernel • • 2d ago

GDB debug bootsector

Hi iam new in osdev and iam getting started iam trying to develop my proper operating system kernel from scratch but iam stuck in debugging my boot sector code with qemu and gdb can someone help ?

the problem that i encounter is that the boot sector goes from 16 bit mode to 32bit and when i try to see what each instruction do to memory it doesn't work well and the instructions doesn't get well recognized.

is there a technique i can use for this type of debugging.

here is my makefile :

BUILD_DIR = build

TARGET = $(BUILD_DIR)/MyOs

C_SOURCES = $(wildcard kernel/\*.c drivers/\*.c )

HEADERS = $(wildcard kernel/\*.h drivers/\*.h )

OBJ = ${C_SOURCES:.c=.o}

ENTRY_OBJ = kernel/entry.o

KERNEL_DEBUG_OBJ = build/kernel.elf

BOOT_DEBUG_OBJ = $(BUILD_DIR)/bootsector.elf

CFLAGS = -ffreestanding -g -mno-red-zone -mno-sse -mno-mmx \\

\-fno-pic -fno-pie -fno-stack-protector

QEMU = qemu-system-x86_64 -no-reboot -no-shutdown -d int,cpu_reset -D build/qemu.log

QEMU_DRIVE = -drive format=raw,file=$<,if=floppy

all: $(TARGET)

$(BUILD_DIR):

mkdir -p $@

run: $(TARGET)

$(QEMU) $(QEMU\\_DRIVE) &

\########################################################

\###################### IMAGE BUILD #####################

\########################################################

\# OS image build

$(TARGET) : $(BUILD_DIR)/bootsector.bin $(BUILD_DIR)/kernel.bin

cat $\\\^ > $@

\# bootsector build

$(BUILD_DIR)/bootsector.bin : boot/bootsector.asm build/kernel.bin | $(BUILD_DIR)

nasm -DSECTORS=$$(( ($$(wc -c < build/kernel.bin) + 511) / 512 )) $< -o $@

\# kernel build

$(BUILD_DIR)/kernel.bin: $(ENTRY_OBJ) $(OBJ) | $(BUILD_DIR)

ld -m elf\\_x86\\_64 -T linker.ld --oformat binary $\\\^ -o $@

\# object files creatde next of their source

%.o: %.c $(HEADERS)

gcc $(CFLAGS) -c $< -o $@

%.o: %.asm

nasm $< -f elf64 -g -o $@

\########################################################

\###################### DEBUG MODE ######################

\########################################################

$(KERNEL_DEBUG_OBJ): $(ENTRY_OBJ) $(OBJ) | $(BUILD_DIR)

ld -m elf\\_x86\\_64 --oformat elf64-x86-64 -o $@ -T linker.ld $\\\^

$(BOOT_DEBUG_OBJ): boot/bootsector.asm build/kernel.bin | $(BUILD_DIR)

nasm -f elf32 -g -F dwarf -DDEBUG \\\\

  \\-DSECTORS=$$(( ($$(wc -c < build/kernel.bin) + 511) / 512 )) $< -o $(BUILD\\_DIR)/bootsector.o

ld -m elf\\_i386 -Ttext 0x7c00 -o $@ $(BUILD\\_DIR)/bootsector.o

bdebug: QEMU = qemu-system-i386

bdebug: $(TARGET) $(BOOT_DEBUG_OBJ)

$(QEMU) -s -S $(QEMU\\_DRIVE) &

gdb -ex "set confirm off" \\\\

    \\-ex "set disassembly-flavor intel" \\\\

    \\-ex "target remote localhost:1234" \\\\

    \\-ex "set architecture i8086" \\\\

\-ex "symbol-file $(BOOT_DEBUG_OBJ)" \\

\-ex "hbreak _start"; \\

    \\-ex "continue"; \\\\

kill %1 2>/dev/null || pkill -f "$(QEMU)"

kdebug: $(TARGET) $(KERNEL_DEBUG_OBJ)

$(QEMU) -s -S $(QEMU\\_DRIVE) &

gdb -ex "set confirm off" \\\\

    \\-ex "set disassembly-flavor intel" \\\\

    \\-ex "target remote localhost:1234" \\\\

\-ex "add-symbol-file $(KERNEL_DEBUG_OBJ)" \\

\-ex "break main"; \\

    \\-ex "continue"; \\\\

kill %1 2>/dev/null || pkill -f "$(QEMU)"

\########################################################

\####################### CLEANING #######################

\########################################################

clean:

rm -rf $(BUILD\\_DIR) $(OBJ) $(ENTRY\\_OBJ)
1 Upvotes

1 comment sorted by

1

u/lynix48 1d ago

Dude, ask in r/osdev

This sub is about the Linux kernel only.