[英]How can I do code-path analysis in a debugger?
Are there any debuggers, tools, gdb-scripts that can be used to do code-path analysis? 是否有可用于进行代码路径分析的调试器,工具和gdb脚本?
Say I have an executable (in C++, but the question is not language restricted) that runs fine with one input and crashes with another. 假设我有一个可执行文件(在C ++中,但问题不受语言限制),可以在一个输入下正常运行,而在另一个输入下崩溃。 I would like to see the difference between the two execution paths, without having to step (or instrument) through potentially thousands of lines of code.
我希望看到两个执行路径之间的差异,而不必单步执行(或检测)成千上万行代码。
Ideally, I would be able to compare between 2 different streams of (C++) statements (preferably not assembler) and pinpoint the difference(s). 理想情况下,我将能够在2个不同的(C ++)语句流之间进行比较(最好不是汇编程序),并查明差异。 Maybe a certain if-branch is taken in one execution and not the other, etc.
也许某个分支执行某个分支,而不执行另一个分支,依此类推。
Is there a way to achieve / automate that? 有没有办法实现/自动化呢? Thanks in advance.
提前致谢。
So, provided the source of the bug could be located in one (or a few) source files, the simplest way to achieve comparative code-execution paths seems to be GDB scripting. 因此,假设该错误的源可以位于一个(或几个)源文件中,则实现比较代码执行路径的最简单方法似乎是GDB脚本。 You create a gdb script file:
您创建一个gdb脚本文件:
set args <arg_list>
set logging off
set logging file <log_file_1>
set logging on
set pagination off
set breakpoint pending on
b <source_file>:<line_1>
commands
frame
c
end
...
b <source_file>:<line_n>
commands
frame
c
end
with a preamble (all the set commands) and then breakpoint + command for each line in the source file (which can be easily generated by a script; don't worry about blank or commented lines, they will be skipped). 带有一个序言(所有set命令),然后是源文件中每一行的breakpoint +命令(可以通过脚本轻松生成;不要担心空白或注释行,它们将被跳过)。
Load the executable in gdb (properly built with debug flags, of course); 将可执行文件加载到gdb中(当然,它是用调试标志正确构建的); source the gdb script file above (call it gdb_script.txt) and run:
源上方gdb的脚本文件(称之为gdb_script.txt)并运行:
source gdb_script.txt
run
Then repeat the process above with a slightly changed script file (gdb_script.txt). 然后使用稍有更改的脚本文件(gdb_script.txt)重复上述过程。 Specifically, change the
<arg_list>
to modify the input; 具体来说,更改
<arg_list>
来修改输入; and set logging file to a different file <log_file_2>
. 并将日志文件设置为其他文件
<log_file_2>
。
Source and run. 源代码并运行。 Then compare
<log_file_1>
vs. <log_file_2>
with your preferred diffing tool (say, tkdiff). 然后将
<log_file_1>
与<log_file_2>
与首选的比较工具(例如tkdiff)进行比较。
This will not do a better job than gcov (suggested above). 这不会比gcov做得更好(上面建议)。 But, it can help better restrict your output to the suspicious region of code.
但是,它可以帮助更好地将您的输出限制在可疑的代码区域。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.