Skip to content

Latest commit

 

History

History
170 lines (117 loc) · 4.71 KB

File metadata and controls

170 lines (117 loc) · 4.71 KB

Code:

  • .vscode/launch.json
  • .vscode/settings.json
  • 01.Kernel32/Makefile
  • 01.Kernel32/elf_i386.x
  • 02.Kernel64/Makefile
  • 02.Kernel64/elf_amd64.x
  • Makefile in root directory
// .vscode/launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Attach",
            "type": "cppdbg",
            "request": "launch",
            "cwd": ".",
            // "program": "${workspaceFolder}/Ch15/01.Kernel32/Temp/Kernel32_C.internal.elf",
            "program": "${workspaceFolder}/Ch24/02.Kernel64/Temp/Kernel64_C.elf",
            "stopAtEntry": true,
            "miDebuggerServerAddress": "localhost:1234",
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
// .vscode/settings.json
// add vertical lines to columns 80 and 100 in editor
{
    "editor.rulers": [
        80,
        100
    ],
    ...    
}
# part of 01.kernel32/Makefile

GCC32 = gcc -m32 -ggdb -c -ffreestanding -fno-pie
LD32 = ld -melf_i386 -T ../elf_i386.x -nostdlib
OBJCOPY32 = objcopy -j .text -j .data -j .rodata -j .bss -S -O binary


# part of 02.kernel64/Makefile

NASM64 = nasm -f elf64
GCC64 = gcc -m64 -ggdb -c -ffreestanding -fno-pie
LD64 = ld -T ../elf_amd64.x -nostdlib
OBJCOPY64 = objcopy -j .text -j .data -j .rodata -j .bss -S -O binary
# part of Makefile in the root directory


#  DEBUG_ARCH: decide whether to debug 32bit or 64bit code. 
#    possible values: [32, 64]
#    default: 64

QEMU_OPTIONS = -m 64 -hda ./Disk.img -hdb ./HDD.img -rtc base=localtime -M pc

DEBUG_ARCH ?= 64
DEBUG_COMMAND = qemu-system-x86_64
DEBUG_OPTIONS = -gdb tcp::1234 -S

ifeq ($(DEBUG_ARCH), 32)
DEBUG_COMMAND = qemu-system-i386
endif


run:
	qemu-system-x86_64 ${QEMU_OPTIONS}

debug_run:
	$(DEBUG_COMMAND) $(QEMU_OPTIONS) $(DEBUG_OPTIONS)

Explanation

Problem

As the code became more complicated, it became difficult to find bugs, so I felt the need to debug

Solution

Therefore, I created a configuration file for debugging in QEMU and vscode, and added options for the compiler and linker to generate debug information.

How to compile and link codes with debugging info

  • -ggdb gcc option compiles c code with debugging symbols

  • ld has an option to remove debugging symbols. You should disable this option. By default, this option is not used.

example

gcc -ggdb -o hello hello.c

What are the changes in codes?

01.Kernel32/elf_i386.x, 02.Kernel64/elf_amd64.x

  • Before Ch24_sub1, I added debugging sections to my own linker script, but for some reason, it was not able to debug in gdb. Therefore, I used default linker script provided by linux distro and modified it

01.Kernel32/Makefile, 02.Kernel64/Makefile

  • Prior to Ch24_sub1, the linker script generated binary files directly instead of elf files. For debugging, I modified the makefile, so linker generates an elf file and objdump extracts the binary.

  • The elf file contains debugging symbols, so it is used in gdb

Makefile in root directory

  • Since OS is running on QEMU, QEMU needs an option to run gdb server for debugging. From Ch24_sub1, Makefile in root directory has a new command make debug_run which runs gdb_server in the background.

  • Unfortunately, it is not possible to debug 32 bit and 64 bit on QEMU at the same time; The latest version I checked was 6.1

  • If you try to debug 32 bit code while running qemu-system-x86_64, you will encounter an error. You need to choose qemu-system-i386 program to debug 32 bit code, and this means you cannot debug 64 bit code and 32 bit code at the same time.

.vscode/launch.json

  • This file contains vscode conf files for debugging

  • Unfortunately, vscode doc for debugging does not explain in detail.

  • For example, we need to attach gdb to gdb server in QEMU which is kind of remote server. if you set request option to "remote", you will encouter an error. But there is no explanation how to use the option.

  • "program" option is used for adding debugging symbol table to gdb in my project.

  • Many websites suggest to use Native Debugger extension to debug, but you do not need to use extension to debug c and c++ debugging. Because I did not use Native Debugger, I do not know the strengh and weakneww of the extension.

.vscode/settings.json

  • add vertical lines to columns 80 and 100 in editor