简体   繁体   English

如何在调试器中进行代码路径分析?

[英]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.

相关问题 如何禁用Eclipse CDT中的所有代码分析? - How do I disable all code analysis in eclipse cdt? 我可以在 Visual Studio 代码中使用“编辑并继续”调试器工具吗? - Can I use the “Edit and continue” debugger tool in visual studio code? 如何通过编程检查特定代码是否可分页(使用静态分析方法) - How do I check whether a particular code is in pageable or not through programming(Using static analysis method) 如何从Visual Studio静态代码分析中排除库头? - How do I exclude library headers from my Visual Studio static code analysis? VS2013调试器无法再解析包含源代码路径的系统环境变量 - VS2013 debugger can no longer resolve system environment variable containing source code path 我最近更改了编译器路径以运行 c++ 代码,但现在我无法运行任何 python 代码。 我该如何解决? - I recently changed my compiler path to run c++ code, but now I can't run any python code. How do I fix this? 如何使用Eclipse CDT对C ++代码进行静态分析? - How to do static analysis for C++ code with Eclipse cdt? 如何在不使用开关的情况下对字符串进行频率分析 - How could I do frequency analysis on a string without using a switch 调试器失败时如何调试程序 - How can I debug a program when debugger fails C ++:如何计算方法的成本(算法分析) - C++ : How can I calculate a cost of a method (Algorithm Analysis)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM