【1000个GDB技巧之】GDB中使用python脚本的方法以及4个注意事项(define、python、end、gdb.execute、to_string=True)

本文详细介绍了如何在GDB中使用Python宏定义函数,涉及代码片段、Python与GDB交互的注意事项,以及如何处理gdb.execute的输出转换。内容包括python代码的正确格式、输出结果的处理和脚本导入方法。
摘要由CSDN通过智能技术生成

要点

  • 使用define 定义函数
  • 在define中使用python … end包围python使用的代码(python代码也可以不在包围中)
  • 在python中使用gdb命令用gdb.execute,并且需要指定to_strings
  • gdb可以单独写成文件然后source进来

坑:

坑1:python和gdb的end都不值直接注释,需要另起一行
坑2:gdb.execute执行后的输出结果包括$xxx 获取需要做转换
坑3:python代码必须定格严格按照table格式撰写(还有一个方法就是写成纯gdb文件然后import
坑4:gdb.execute代码中必须写to_string才能正确输出到返回值,进一步提供给gdb使用

代码:

# define macro
define csj_pv_vm_numa_stat
        core-file /proc/kcore
        p vm_numa_stat
        p vm_numa_stat[3].counter
end

define csj_pv_jiffies
        core-file /proc/kcore
        p jiffies_64

python
gv_jiffies_64=gdb.execute("p jiffies_64", to_string=True).replace("\n", "").split(" ")[-1]
print(gv_jiffies_64)
print("Current jiffies:\t" + str(gv_jiffies_64))
print("jiffies to sec :\t" + str(int(gv_jiffies_64)/1000) + " s")
#python end
end

p jiffies_64
#csj_pv_jiffies end
end

实战:

在这里插入图片描述
执行效果:
在这里插入图片描述

另外一种将python脚本写到单独文件调用

  • 单独文件定义函数
  • 在gdb中命令行或者脚本使用source 导入python文件
  • 使用python xxx() 类似python中调用方式调用脚本,注意python执行的时候需要打括号
    举例:
    在这里插入图片描述

参考链接

https://sourceware.org/gdb/current/onlinedocs/gdb.html/Python-Commands.html#Python-Commands

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值