Skip to content

Latest commit

 

History

History
234 lines (154 loc) · 6.77 KB

Memo.md

File metadata and controls

234 lines (154 loc) · 6.77 KB

GDB

LLDB vs GDB

LLDB的文档不太完善(比如想拓展自定义命令行),暂倾向于使用GDB

之前使用LLDB,但是拓展时遇到了一些奇怪的问题,例如:无法使用PDB ;以及其相关的文档和案例不足;CLion也暂时只支持Bundled LLDB(使用第三方库时需要一些migration)。于是回归GDB。暂时感觉LLDB有的功能(自己用过的功能),GDB也有。

修改一个变量的值(CLion中对应的快捷键为F2

(gdb) set var width=47

Breakpoint

# 某行打断点
(gdb) break linenum
# 具体到某个文件
(gdb) break filename:linenum
# 具体到某个函数
(gdb) break filename:function

CLI

命令行 abbreviation / example 作用
python-interactive [command] pi 进入Python交互模式
python [command] py [command] 执行Python命令行
break [line] break 23 打断点
info vtlb 查看虚函数表
print <variable_name> 查看变量
info threads 查看线程信息
info locals [variable_name] 查看函数栈的局部变量

Configuration

$ echo "set history save on" >> ~/.gdbinit
$ echo "set print thread-events off" >> ~/.gdbinit
$ echo "set extended-prompt \w (gdb) " >> ~/.gdbinit
  • 隐藏启动时的提示信息和版权信息,details
# 方案一(CLI):设置别名
$ alias gdb="gdb -q"

# 方案二(配置文档):注意不是gdbinit (from gdb 11)
$ echo "set startup-quietly on" >> ~/.gdbearlyinit

Disassemble

使用 disassemble 进一步看出现 dump core 出现的汇编位置

img

Frame

Backtrace

# 查看调用栈
(gdb) backtrace
(gdb) where
(gdb) info stack

Frame

# 切换到某一帧
(gdb) f <num>

# 查看该帧的局部变量
(gdb) info locals

# 查看形参
(gdb) info args

Library

# 查看链接的动态库
$ info share

image-20220810232749699

# 查看已有的pretty printer,包括关闭的
(gdb) info pretty-printer
Print the list of installed pretty-printers. This includes disabled pretty-printers, which are marked as such.

# 关闭pretty printer
(gdb) disable pretty-printer

# 启动pretty printer
(gdb) enable pretty-printer

Python

$ gdb python 

(gdb) set args <python文件名>
(gdb) run (gdb)
  • 一般会使用regex来判断输入变量的类型是否符合需求
def vec_lookup_function(val):
    lookup_tag = val.type.tag
    if lookup_tag == None:
        return None

    regex = re.compile("^.*vector_base<.*,.*>$")
    if regex.match(lookup_tag):
        return VectorPrinter(val)

    return None

Practice

  • 启动GDB调试
# 追加tag
launch-prefix="gdb -ex run --args"

# option:
# -ex <command> 执行给定的GDB command

Practice

  • ROS rviz增加 cameraimage display时,会出现段错误(segmentation fault)

img

步骤一:执行程序

$ gdb python
(gdb) run <py_file>.py

步骤二:添加display触发异常

img

步骤三:查看调用栈的情况,可定位到是哪个函数产生段错误(加上full会同时输出局部变量

(gdb) bt full

img

原因未知,可通过下发相关信号解决

$ kill -CONT <pid of the process>

Extension

暂时没感觉新颖的地方

  • Install
$ pip install gdbgui
$ gdbgui

Reference

TODO