# Vim Terminal Debug ###### tags: `Vim`, `gdb`, `C`, `Go` ## Document [VIM REFERENCE MANUAL - Debugging terminal-debug](https://vimhelp.org/terminal.txt.html#terminal-debug) ## Debug C Application ### Have a C Code Example C source: ```c $ cat main.c #include <stdio.h> int main(void) { int a = 0, b = 0; a = 1; b = 2; printf("%d + %d = %d\n", a, b, a+b); } ``` Makefile: ```Makefile $ cat Makefile CC=cc SOURCE=main.c BINARY=test all: ${CC} ${SOURCE} -g -o ${BINARY} clean: rm ${BINARY} ``` ### Debug Procedure 1. `make` 2. `vim main.c` 3. Vim's command mode `:packadd termdebug` to load debug plugin, which has been included in Vim. 4. Start debug test application with commad `:Termdebug test`. It will split the terminal into 3 parts: * gdb window * program window * source window ![](https://hackmd.io/_uploads/ry040UuUh.png) 5. The gdb window is the gdb. So, just use gdb as usual in the window. 6. `Ctrl + w` **twice** can jump between the windows. 7. Jump to the source window. Then, can go to the line and add a break point at the line by `:Break`. Finally, jump back to the gdb window to run the program. It will stop at the line. ## Debug Go Application Go application can be debugged with **GDB** as well. https://go.dev/doc/gdb I try to make it go with debug symbols directly. ```shell $ go build -gcflags --help # example/hello usage: compile [options] file.go... -% int debug non-static initializers ... -N disable optimizations ... -l disable inlining ... ``` To have better debug information, add `-N -l` into build's C flags, which disables *optimizations* and *inlining*. ### Have a Go Code Example Go source: ```go $ cat hello.go package main import ( "fmt" ) func main() { a := 1 b := 2 fmt.Printf("%d + %d = %d\n", a, b, a+b) } ``` ``` $ cat go.mod module example/hello go 1.19 ``` Makefile: ```Makefile $ cat Makefile GO=go SOURCE=hello.go BINARY=hello CFLAGS="-N -l" all: ${GO} build -gcflags ${CFLAGS} -o ${BINARY} ${SOURCE} clean: rm ${BINARY} ``` ### Debug Procedure Same as [Debug C Application's Debug Procedure](#Debug-Procedure) ![](https://hackmd.io/_uploads/By4bALdIh.png)