Tips: all for hands-free.
Tips series: Keep a diary of everyday problem-solving Tips that free your hands.
The goal is only one: not to be paralyzed by the repetition of trivial things, can not be lazy.
Today we will talk about how to debug Go programs gracefully using GDB.
What’s GDB for?
It’s a great tool for understanding the underlying code, not to mention locating problems.
Specific SAO operation see Cao big use debugger learning Golang
However, GDB can have a lot of problems to solve from installation to availability (especially on the Mac), and how we can use it gracefully and avoid getting caught up in the problems is the focus of this article.
(It involves running on Docker and Mac platforms)
Let’s take a look at the recommended Docker method
Docker: 3 steps to debug
Docker’s blessing is very convenient and ready to use
Gdb-debug-go-in-docker can download the command
The specific steps are as follows :(fully return to the essence of the tool, debugging can also be done on another machine)
// 1. GDB has been started for the current directory mapping
docker run --rm -it --security-opt="apparmor=unconfined" \
--security-opt="seccomp=unconfined"\ -cap-add=SYS_PTRACE -v "$(pwd):/go/src/app" \
newbmiao/gdb-go114.rc1:latest bash
// 2. Compile go without compressdwarf, inline and escape
go build -ldflags=-compressdwarf=false -gcflags=all="-N -l" -o test test.go
// 3. You can debug happily
gdb ./test
Copy the code
One set, call it a day, simple as that. How to define docker will be attached at the end of the article.
You may have questions about what these parameters of docker do, attached below information, interested to check
For details, see Issue: Apparmor Denies ptrace to docker-default profile
- AppArmor
AppArmor is a Linux kernel security module that can be used to limit the functionality of processes running on a host operating system. Each process can have its own security profile. Security profiles are used to allow or disable certain functions, such as network access or file read/write/execute permissions.
See AppArmor Security profiles for Docker
- Seccomp
Seccomp, short for Secure Computing Mode, is an operation provided by the Linux kernel to limit the number of system calls a process can make. Of course, we need to have a configuration file that specifies which system calls the process can and cannot make. In Docker, it uses Seccomp to limit the number of system calls a container can make.
See Seccomp Security Profiles for Docker
- SYS_PTRACE
Combined with seccomp=unconfined, allow containers to run programs like strace/lTrace using pTrace.
Mac: Certificate signing is required
GDB 8.3.1 On macoOS High Serria 10.13.6
Steps:
- Creating a System Certificate
gdb-cert
The key is marked in red, and the rest is the next step. (Note that the signature is successful only after the certificate is successfully created.)
(If the creation fails, delete the certificate and restart the creation (recommended). Or try to create login certificate = “export =” = “load to system certificate)
- GDB code signature
A script has been created to execute:
sh debugger/gdb/installMac.sh
- GDB debugging (same as docker)
For details about certificates, see PermissionsDarwin
Also list the possible problems:
- codesign
Unable to find Mach task port for process-id 3884: (os/kern) failure (0x5).
(please check gdb is codesigned - see taskgated(8))
Copy the code
- Initial run stuck
$ gdb ./test
>>> r
Starting program: /Users/newbmiao/Documents/tech/Dig101-Go/test
[New Thread 0xd03 of process 7603]
#Stuck...
Copy the code
Solutions:
Directly find the corresponding process ID in another window and kill it
ps aux|grep gdb
kill -9 xxx
Copy the code
- SIG113 problem
See: GDB kind of Doesn’t work on macOS Sierra
Solutions:
#GDB configuration
$ cat ~/.gdbinit
# gdb-dashboard
// $ cat ~/.gdbinit.d/init
set startup-with-shell off
Copy the code
- This helps GDB better understand go debugging information
Make GDB print easier to read without compressing DWARF
go build -ldflags=-compressdwarf=false -gcflags=all="-N -l" -o test test.go
GDB USES $GOROOT/SRC/runtime/runtime – GDB. Py to load the go runtime support. Can see from the binary file to: strings test | grep GDB
- Libsystem_darwin dylib error
Just ignore it. See GDB giving Weird Errors
See NewbMiao/ Free-hands-Tips for more details
See more:
- cyrus-and/gdb-dashboard
- Debugging Go Code with GDB
This article is first published on newbmiao.
Recommended reading: Dig101-go series