set print pretty on set print object on set print static-members off set print vtbl on set print demangle on set demangle-style gnu-v3 set print sevenbit-strings off
gdb> break foo if x>0 commands printf "x is %dn",x continue end
在x>0时,停止并执行一系列commands。
打印变量或内存print命令
1 2 3
gdb> p *arr@23 // 将arr视为长度为23的数组打印出来 gdb> p/x my_var // 以16进制打印my_var,除x外,还有c/d等 gdb> x mem_addr // 查看mem_addr地址对应的内存内容
打印格式的调整:
1 2 3
gdb> set print pretty // 打印struct/class时,gdb会带缩进 gdb> set print static-member off // 打印对象时,不打印对象的static变量 gdb> set print element 23 // 打印数组时,最多打印几个对象,0为不限
#include <unordered_map> using namespace std; struct Student { int val_1_; int val_2_; }; int main() { unordered_map<int, Student> m; for (int i = 0; i < 10; i++) { Student s; s.val_1_ = i; s.val_2_ = i; m.insert({i, s}); } abort(); // abort to get a coredump file return 0; }
1 2 3 4 5 6 7 8 9 10 11
define dump_map_new set $map = $arg0 set $head_node = $map._M_h._M_before_begin._M_nxt set $curr_node = $head_node while $curr_node set $iter_node = (std::__detail::_Hash_node<std::pair<int const, Student>, false> *)($curr_node) set $addr = &($iter_node->_M_storage._M_storage) set $iter_pair = (std::pair<int const, Student> *)($addr) printf "key=%d, value={%d, %d}\n", $iter_pair->first, $iter_pair->second.val_1_, $iter_pair->second.val_2_ set $curr_node = $curr_node->_M_nxt end