GDB中使用command命令时如何将info stack输出记录到日志文件
解决GDB断点command中info命令输出不写入日志的问题
针对你遇到的断点command里info stack输出无法写入日志的问题,这里提供两种可行的解决方案:
方案1:用pipe+tee强制双输出
如果需要同时在控制台显示栈追踪结果并写入日志,可以在断点的command脚本中借助pipe命令将info stack的输出通过tee分流:
# 先配置基础日志(若未设置) set logging file myDbg.log set logging overwrite off set logging on # 设置__libc_malloc断点及自动执行逻辑 break __libc_malloc commands # 同时输出到控制台和日志文件 pipe info stack | tee -a myDbg.log cont end
方案2:调整GDB全局日志设置
GDB默认可能不会捕获断点command脚本中调试类命令的输出,开启debugredirect选项可解决这个问题:
# 配置日志参数,开启调试命令输出重定向 set logging file myDbg.log set logging overwrite off set logging redirect off # 保持控制台和日志双输出 set logging debugredirect on # 强制info类命令输出写入日志 set logging on # 设置断点及自动执行逻辑 break __libc_malloc commands info stack cont end
说明
set logging debugredirect on是核心:它会让GDB将所有调试命令(如info、show)的输出,无论是否在command脚本中执行,都同步写入日志文件。- 方案1适合仅需特定命令输出记录的场景,灵活性更高;方案2是全局设置,适合需要记录所有调试命令输出的场景。
内容的提问来源于stack exchange,提问作者Aelian
相关产品推荐
相关产品推荐

