6/03/2013

Debug C++ binary from command line

blaze build -c dbg ...
gdb --args binary binary_args

(gdb) run
(gdb) backtrace

Another useful command is ldd, which can be used to check the linking result of a file.

Debug python segment fault:
gdb -ex r --args /usr/bin/python3 test.py

How to debug by using a core file?
==================================
gdb bin_file core_file
(gdb) bt
(gdb) f 8 // function number
Then you can print variable names.

# To show all the libraries of a binary
ldd binary

nm command to check function names
c++filt to unmask a function name
For .so files, use:
nm -D x.so to check the function names.

Another thing to remember is:
when linking static .a files, the link order MATTERS.

# To check all GLIB versions
strings /usr/lib/x86_64-linux-gnu/libstdc++.so.6 | grep GLIB
readelf -d bazel-bin/common/image/io_utils_test | grep NEEDED

objdump -t libopencv_imgcodecs.a | grep _ZN2cv8imencodeERKNS_6StringERKNS_11_InputArrayERSt6vectorIhSaIhEERKS6_IiSaIiEE

# To check the compiled file type
file ./build_qnx-aarch64/libpng/png.o
./build_qnx-aarch64/libpng/png.o: ELF 64-bit LSB relocatable, ARM aarch64, version 1 (SYSV), not stripped

# To find the sysroot
gcc -print-sysroot

No comments: