GDB basic
Figure out some basic function to debug in GDB. Below is a example about run gdb with pre-definited command which is wrote in gdb.init.
The program will be stop when a argument which equal 123 is passed to foo function
main.cpp
#include <iostream>
#include <memory>
using namespace std;
void foo(int a)
{
unique_ptr<int> upi = make_unique<int>(22);
cout << a << "\n";
auto x = a + 10;
upi.release();
}
int main()
{
for(auto i = 0; i < 200; i++)
foo(i);
return 0;
}
gdb.run
break foo(int) if a == 123
run a.out
next
next
info local
p x
1. build a binary with debug symbols : g++ -O0 -g main.cpp -o a.out
2. run gdb with pre-defined in gdb.run : gdb --batch -x gdb.run ./a.out
Check debug symbol whether is existed or not and which cflag had been used.
readelf -w hello | grep producer
Output like these
<d> DW_AT_producer : (indirect string, offset: 0x591): GNU C++14 11.2.0 -mshstk -mtune=generic -march=x86-64 -g -O2 -std=gnu++14 -fPIC -fno-builtin -fno-exceptions -fno-rtti -fomit-frame-pointer -funwind-tables -fvisibility=hidden -fno-ipa-icf -fcf-protection=full -fasynchronous-unwind-tables -fstack-protector-strong -fstack-clash-protection
=> clag is -g -O2 -std=gnu++14 ….
To examine symbol, value on sp -> gdb$ x/32ga $rsp
Note
Follow this : https://developers.redhat.com/blog/2021/04/30/the-gdb-developers-gnu-debugger-tutorial-part-1-getting-started-with-the-debugger#next_up
https://developers.redhat.com/articles/2022/01/10/gdb-developers-gnu-debugger-tutorial-part-2-all-about-debuginfo#how_to_inspect_debuginfo