How to compile kernel with debug symbols for my own OS? - gcc

Hi I'm a beginner and trying to write a Linux like kernel.
I use Qemu as my emulator and currently debug in a assembly level.
However, by previous experience, I can debug Linux kernel with Qemu at source code (.c files) level.
So I would like to ask if I can do it with my own kernel, so that I can debug it with efficiency.
In order to provide more info, the following is my compilation script:
# Complie head.S
gcc -E ./PysicCodes/head.S > head.s
as --64 -o head.o head.s
gcc -E ./PysicCodes/AP_Boot.S > AP_Boot.s
as --64 -o AP_Boot.o AP_Boot.s
# Compile main program
gcc -mcmodel=large -fno-builtin -fno-stack-protector -m64 -c ./PysicCodes/*.c
# Interrupt hander requires general register only(since no XMM,SEE registers are saved)
gcc -mcmodel=large -fno-builtin -fno-stack-protector -m64 -mgeneral-regs-only -c ./PysicCodes/g_reg_only/*.c
# Linkage: Must put head.o at first, so that kernel start at head.o
ld -b elf64-x86-64 -z muldefs -o system head.o 8529A.o ACPI.o AP_Boot.o APIC.o cpu.o INT.o keyboard.o main.o Mem.o PCI.o Printk.o SMP.o Task.o Time.o TSS.o fat32.o -T ./PysicCodes/Kernel.lds
# Dump kernel
objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O binary system Kernel.bin
Thanks for anyone who would spend time on helping. Any extra information needed, please comment.

Currently I tried to add -ggdb3 as the compiling and linking options.
Furthermore, using the compiled object as the option for gdb
It simply worked.
In addition, adding "miDebuggerServerAddress": "localhost:1234" into Vscode's "launch.json" file, it actually can connect to Qemu and debug c code in vscode.
However only 1 problem, that vscode will run Qemu at start, so I have to press pause button as soon as I can after start debugger, and using "-exec" to put a hardware break-point.
There is an issue on git and currently haven't see an answer.

Related

Specifying both -O2 and -O3 at same time

Some programs already uses -O2 flag, if I use -O3 flag, the program compiles with both -O2 and -O3 as shown by the task manager or by /proc/PID/cmdline.
For example, I'm using a Linux kernel built with Clang and full LTO. Even though I have these lines in the dkms configuration:
# /etc/dkms/framework.conf
export LLVM=1
export CC=clang
export CFLAGS="-O3 -march=native"
Now DKMS modules compile with both -O2 and -O3 flags. In this case, which flag is actually used?
For gcc, see https://gcc.gnu.org/onlinedocs/gcc-11.2.0/gcc/Optimize-Options.html#Optimize-Options
If you use multiple -O options, with or without level numbers, the last such option is the one that is effective.
clang doesn't document its option syntax as extensively, but it generally tries to be compatible with gcc, so it should be the same in this regard.

gccfilter and gcc 4.7.2 does not work, stops compiling

I have successfully installed the gccfilter (http://www.mixtion.org/gccfilter/) in my toolchain. The filter should actually work because all perl modules and other stuff has been installed the problem is that it does not work properly when I use for example the following command line:
gccfilter -c -a g++ -std=c++11 -O3 -DNDEBUG -I/"tonnes of includes" -o CMakeFiles/...../main.cpp.o
-c /...path.../App/main.cpp
.../variant.hpp:17:0,
from .../SceneParser.hpp:12,
from .../SimulationManager.hpp:12,
from .../main.cpp:8:
_ <-- Cursor is here
It compiles but after the error message it stops doing anything, the cursor is on the bottom line and nothing happens?
So the tool does not quite work, I am using gcc 4.7.2. i am not quite sure where the problem might be?

Mixed language C++, C and Fortran compilation with Cmake

Using g++, gcc and gfortran on GNU/Linux, I've written a simple script to compile and link together a number of source code files written in C++, C and Fortran. Here are the complete contents of the script. This script has been tested, and works well.
g++ -c test-Q.cpp -I./boost/boost_1_52_0/ -g
gcc -c paul2.c -g
gcc -c paul2_L1.c -g
gcc -c paul6.c -g
gcc -c paul6_L1.c -g
gcc -c fit_slope.c -g
gfortran -c getqpf.F -g
g++ -o test-Q test-Q.o paul2.o paul2_L1.o paul6.o paul6_L1.o fit_slope.o getqpf.o -g -lgfortran
To make this more cross-platform, I would like to re-write the script using Cmake. How might I handle mixed-language compilation?
The following test script listed below does not work, and will only selectively compile some of the files.
Is there perhaps another cross-platform build process that might be better suited for this type of compilation?
cmake_minimum_required (VERSION 2.6)
project (q-test)
include_directories(/media/RESEARCH/SAS2-version2/test-Q/boost/boost_1_52_0)
add_executable( q-test
test-Q.cpp
paul2.c
paul2_L1.c
paul6.c
paul6_L1.c
fit_slope.c
getqpf.F
) # end
You need to enable Fortran for the project like this:
project (q-test C CXX Fortran)
Also, you might want to use find_package(Boost) instead of hard coding an include path.

How to do source level debugging of x86 code with GDB inside QEMU?

I wrote a x86 assembly program for MBR section.
I compile it as follows:
nasm hellombr.asm -f bin -o hellombr.img
Then I run it in qemu:
qemu -fda hellombr.img -boot a
The question is how can I debug my program at source level?
You should let nasm create the debugging symbols in an ELF file and then dump this to a flat binary to be used in the MBR. You can then instruct GDB to read the necessary symbols from the ELF file.
The complete procedure would then become something like this:
$ nasm hellombr.asm -f elf -g -o hellombr.elf
$ objcopy -O binary hellombr.elf hellombr.img
$ qemu -s -S -fda hellombr.img -boot a
$ gdb
(gdb) symbol-file hellombr.elf
(gdb) target remote localhost:1234
For an explanation of the flags I pass to qemu see this answer.
Instead of using qemu, use bochs. It is completely compatible, albeit slower. It is also an emulator but if you make it from sources, using these flags and build it like this:
./configure --enable-debugger --enable-disasm --disable-docbook
make
make install
you can place breakpoints in your code, step through it, view GDT, IDT and everything you needed to know.
A really good (and simple) way is to use IDA with bochs, you find an excellent blog post on it here, along with some other hints/suggestions for bootloader development.

snapshot of memory for process on mac?

I want to take a snapshot of memory of process in action on mac. I have no idea how to do it.
I have IDA-PRO for mac with me. Can it be used? How?
Can anyone suggest me a way to do this? (some documentation or example).
May be some techniques from uni can be used but I am also not aware of that.
I dont want to kill the process as I want to see whats changing after execution of instructions/commands.
You can send a signal to a running process to dump core into a file, which can be used with gdb later for postmortem analysis.
kill -ABRT <process-id>
It seems that you must configure your system to enable core dump. See http://developer.apple.com/library/mac/#technotes/tn2124/_index.html for details.
UPDATE:
Well, above link introduces a third party implementation of gcore, a command line tool to make a core dump of running processes:
http://www.osxbook.com/book/bonus/chapter8/core/
You may just want to grab the source and try:
http://www.osxbook.com/book/bonus/chapter8/core/download/gcore-1.3.tar.gz
To make a single FAT binary to use with ppc/i386/x86_64, just modify following lines from Makefile:
gcore: gcore.c
gcc -O2 -arch ppc -arch i386 -Wall -o $# $<
as:
gcore: gcore.c
gcc -O2 -arch ppc -arch i386 -arch x86_64 -Wall -o $# $<

Resources