ROSE 编译器框架/如何调试翻译器
很少有翻译器在您完成编码后就能正常工作。使用 gdb 调试您的代码对于确保代码按预期工作至关重要。此页面展示了如何调试翻译器的示例。
首先,确保您的 ROSE 安装和翻译器使用 -g 选项构建,并且没有启用 GCC 优化。这将确保最佳地保留所有调试信息。
要使用调试选项配置 ROSE 安装,您可以将以下选项添加到正常的配置中。
../rose/configure—with-CXX_DEBUG=-g --with-C_OPTIMIZE=-O0—with-CXX_OPTIMIZE=-O0 ...
如果您已经构建了 ROSE 但忘记了使用的选项,您可以转到 ROSE 的构建树以仔细检查是否使用了调试选项。
cd buildDebug/ -bash-4.2$ head config.log $ ../sourcetree/configure --with-java=/path/to/java/jdk/1.8.0_131 --with-boost=/path/to/boost/1_60_0/gcc/4.9.3 --with-CXX_DEBUG=-g --with-C_OPTIMIZE=-O0 --with-CXX_OPTIMIZE=-O0 --enable-languages=c++,fortran
在调试您自己的翻译器之前,您可能需要仔细检查 ROSE 的内置翻译器 (rose-compiler) 是否能够正确处理您的输入代码。如果不是,您应该向 ROSE 团队报告错误。
如果 rose-compiler 可以处理,但您的自定义翻译器无法处理。问题可能是由您在翻译器中引入的自定义引起的。
另一件事是尽可能减少您的输入代码,以便它仅触发您感兴趣的错误。这将极大地简化错误查找过程。调试处理数千行代码的翻译器非常困难。
gdb 是一个调试器。它为您提供了一个受控的执行环境,以便检查您的程序是否按预期运行。
从本质上讲,它允许您
- 在受控的调试环境中运行您的程序:使用 gdb—args <program> <args...>
- 或 libtool—mode=execute gdb—args <progra> <args...> 用于 libtool 构建的可执行文件。
- 在所需的执行点停止
- 普通断点(称为断点):使用 break <where>,<where> 可以是函数名、行号或文件:行号。
- 当给定变量的值发生变化时(称为观察点):使用 watch <where>
- 段错误:这将自动发生,因此您可以检查段错误是如何发生的。
- 断言失败:这将自动发生,因此您可以调试断言失败。
- 检查甚至更改变量、类型等内容,一旦您的程序在所需的执行点停止。
- 检查断点处的调用栈:使用 backtrace 或简写 bt。frame <frame#> 转到您感兴趣的栈帧。
- 查看断点附近相关的源代码:使用 list [+|-|filename:linenumber|filename:function]
- 检查变量和表达式的值:使用 print <what>,<what> 可以是任何变量、表达式,甚至函数调用。
- 检查变量的类型:whatis variable_name
- 将变量的内容更改为给定值:set <var_name>=<value>
- 调用函数:使用 print function_name,这有助于为某些类对象调用一些转储函数。
- 进一步控制执行
- 一次一步地执行程序:您可以在当前帧中单步执行(next)、向下单步执行到帧中(step)或退出当前栈帧(finish)。
- 继续执行直到下一个断点或观察点:使用 continue 或简写 c
- 立即从函数返回,传递给定值:return <expression>
- 以及其他操作。
要快速了解,您可以查看网上的速查表
来自 Rob,有一个基于 curses 的包装器称为“cgdb” [1]。
- 您将获得一个拆分窗口:底部是 GDB 控制台,顶部是语法高亮的源代码,该代码会自动跟踪您当前的位置并支持 PageUp/PageDn,这比 GDB 的“list”命令更容易使用。
- 它需要安装 ncurses-devl 和 readline-devel。
对于某些人来说,这也被称为源代码外构建。
如果翻译器使用 makefile 构建而没有使用 libtool。翻译器的调试步骤只是使用 gdb 的经典步骤。
- 确保您的翻译器使用 GNU 调试选项 编译-g以便在您的目标代码中存在调试信息。
以下是典型调试会话的步骤
1. 设置断点
2. 检查执行路径以确保程序按预期通过路径。
3. 检查本地数据以验证其值。
# how to print out information about a AST node #------------------------------------- (gdb) print n $1 = (SgNode *) 0xb7f12008 # Check the type of a node #------------------------------------- (gdb) print n->sage_class_name() $2 = 0x578b3af "SgFile" (gdb) print n->get_parent() $7 = (SgNode *) 0x95e75b8 # Convert a node to its real node type then call its member functions #--------------------------- (gdb) isSgFile(n)->getFileName () #------------------------------------- # When displaying a pointer to an object, identify the actual (derived) type of the object # rather than the declared type, using the virtual function table. #------------------------------------- (gdb) set print object on (gdb) print astNode $6 = (SgPragmaDeclaration *) 0xb7c68008 # unparse the AST from a node # Only works for AST pieces with full scope information # It will report error if scope information is not available at any ancestor level. #------------------------------------- (gdb) print n->unparseToString() # print out Sg_File_Info #------------------------------------- (gdb) print n->get_file_info()->display()
我们首先准备遍历 AST 以查找循环的基于 ROSE 的示例分析器。将其重命名为 demo.C
- wget https://raw.githubusercontent.com/rose-compiler/rose/develop/tutorial/visitorTraversal.C
- mv visitorTraversal.C demo.C
我们可以查看示例分析器的源代码:cat demo.C 从本质上讲,我们可以看到以下内容
4 #include "rose.h" 5 6 class visitorTraversal : public AstSimpleProcessing 7 { 8 public: 9 visitorTraversal(); 10 virtual void visit(SgNode* n); 11 virtual void atTraversalEnd(); 12 }; 13 14 visitorTraversal::visitorTraversal() 15 { 16 } 17 18 void visitorTraversal::visit(SgNode* n) 19 { 20 if (isSgForStatement(n) != NULL) 21 { 22 printf ("Found a for loop ... \n"); 23 } 24 } 25 26 void visitorTraversal::atTraversalEnd() 27 { 28 printf ("Traversal ends here. \n"); 29 } 30 31 int 32 main ( int argc, char* argv[] ) 33 { 34 // Initialize and check compatibility. See Rose::initialize 35 ROSE_INITIALIZE; 36 37 if (SgProject::get_verbose() > 0) 38 printf ("In visitorTraversal.C: main() \n"); 39 40 SgProject* project = frontend(argc,argv); 41 ROSE_ASSERT (project != NULL); 42 43 // Build the traversal object 44 visitorTraversal exampleTraversal; 45 46 // Call the traversal function (member function of AstSimpleProcessing) 47 // starting at the project node of the AST, using a preorder traversal. 48 exampleTraversal.traverseInputFiles(project,preorder); 49 50 return 0; 51 }
基于 ROSE 的工具首先初始化 ROSE(在第 35 行)。然后调用 frontend() 函数来解析输入代码并生成以 SgProject 类型项目的根节点的 AST(在第 40 行)。
之后,在第 44 行声明了一个遍历对象。该对象用于使用先序遍历遍历项目的输入文件。
遍历对象基于第 6 行的派生 visitorTraversal 类。此派生类具有成员函数来定义在构造期间(第 14 行)、访问节点(第 18 行)和遍历结束时(第 26 行)应该发生什么。
现在获取一个示例 makefile 以将源文件构建为可执行文件
makefile 应该不言自明。它在安装路径中使用 rose-config 来设置编译器、编译和链接标志、库路径等各种环境变量。
获取分析器的示例输入代码
- wget https://raw.githubusercontent.com/rose-compiler/rose/develop/tutorial/inputCode_ExampleTraversals.C
输入代码在第 20 行和第 41 行有两个 for 循环,如 链接 所示
准备用于指定 ROSE 安装位置的环境变量。
- export ROSE_HOME=/home/freecc/install/rose_install
构建分析器
- make -f SampleMakefile
当前目录下应该有一个名为 demo 的可执行文件。
最后,运行 demo 分析器来处理示例输入代码
- ./demo -c inputCode_ExampleTraversals.C
分析器应该找到两个 for 循环并报告遍历的结束。
Found a for loop ... Found a for loop ... Traversal ends here.
现在让我们调试这个简单的翻译器。
首先,使用 gdb -args 运行带有选项的翻译器
gdb -args ./demo -c inputCode_ExampleTraversals.C // r means run: It is usually a good practice to run the program without setting breakpoints first to see if it can run normally // Or to reproduce an assertion error or seg fault (gdb) r Starting program: /home/liao6/workspace/rose/2019-10-31_14-16-05_-0700/myTranslator/./demo -c inputCode_ExampleTraversals.C ... [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib64/libthread_db.so.1". Found a for loop ... Found a for loop ... Traversal ends here. [Inferior 1 (process 44697) exited normally] ... (gdb) // This program has no errors. So we set a break point at line 22 of demo.C (gdb) b demo.C:22 Breakpoint 1 at 0x40b0e2: file demo.C, line 22. // We expect this breakpoint will be hit twice since the input code has only two loops. We try to verify this: (gdb) r Starting program: /home/liao6/workspace/rose/2019-10-31_14-16-05_-0700/myTranslator/./demo -c inputCode_ExampleTraversals.C warning: File "/nfs/casc/overture/ROSE/opt/rhel7/x86_64/gcc/4.9.3/mpc/1.0/mpfr/3.1.2/gmp/5.1.2/lib64/libstdc++.so.6.0.20-gdb.py" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load:/usr/bin/mono-gdb.py". [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib64/libthread_db.so.1". Breakpoint 1, visitorTraversal::visit (this=0x7fffffffb430, n=0x7fffe87db010) at demo.C:22 22 printf ("Found a for loop ... \n"); // Hit breakpoint 1 once, try to continue to see what will happen (gdb) c Continuing. Found a for loop ... Breakpoint 1, visitorTraversal::visit (this=0x7fffffffb430, n=0x7fffe87db138) at demo.C:22 22 printf ("Found a for loop ... \n"); // Hit breakpoint 1 for the second time, try to continue (gdb) c Continuing. Found a for loop ... Traversal ends here. [Inferior 1 (process 46262) exited normally] // The program terminates now , no more stop at breakpoint 1. // ----------now we inspect the variable n at the breakpoint 1 // return the program and hit Breakpoint 1 (gdb) r Breakpoint 1, visitorTraversal::visit (this=0x7fffffffb430, n=0x7fffe87db010) at demo.C:22 22 printf ("Found a for loop ... \n"); //print out the casted n : it is indeed a SgForStatement (gdb) p isSgForStatement(n) $1 = (SgForStatement *) 0x7fffe87db010 // Inspect the file info of this ForStatement, understanding where it is coming from in the source code. (gdb) p isSgForStatement(n)->get_file_info()->display() Inside of Sg_File_Info::display() of this pointer = 0x7fffe94d58b0 isTransformation = false isCompilerGenerated = false isOutputInCodeGeneration = false isShared = false isFrontendSpecific = false isSourcePositionUnavailableInFrontend = false isCommentOrDirective = false isToken = false isDefaultArgument = false isImplicitCast = false filename = /home/liao6/workspace/rose/2019-10-31_14-16-05_-0700/myTranslator/inputCode_ExampleTraversals.C line = 20 column = 6 physical_file_id = 0 = /home/liao6/workspace/rose/2019-10-31_14-16-05_-0700/myTranslator/inputCode_ExampleTraversals.C physical_line = 20 source_sequence_number = 8726 $2 = void
在 post_construction_initialization() 处设置断点对于检查节点何时创建和/或节点在构造后是否设置了所需字段很有用。例如,遍历导致此函数调用的调用栈(在 gdb 中使用向上和向下命令)可以检查节点是否设置了父节点或作用域指针。如果没有,您可以添加此类操作来修复与空指针相关的错误。
// ----------- We want to inspect when the SgForStatement nodes are created in the execution // set a breakpoint at the post_construciton_initialization() method of SgForStatement (gdb) b SgForStatement::post_construction_initialization() Breakpoint 2 at 0x7ffff3d6495f: file Cxx_Grammar.C, line 139566. // Disable Breapoint 1 for now (gdb) disable 1 (gdb) info breakpoints Num Type Disp Enb Address What 1 breakpoint keep n 0x000000000040b0e2 in visitorTraversal::visit(SgNode*) at demo.C:22 breakpoint already hit 1 time 2 breakpoint keep y 0x00007ffff3d6495f in SgForStatement::post_construction_initialization() at Cxx_Grammar.C:139566 // run until the Breakpoint 2 is hit (gdb) r The program being debugged has been started already. Start it from the beginning? (y or n) y Breakpoint 2, SgForStatement::post_construction_initialization (this=0x7fffe87db010) at Cxx_Grammar.C:139566 139566 if (p_for_init_stmt == NULL) { // use backtrace to check the function call stacks leading to this stop of Breakpoint 2. // You can clearly see the callchain from main() all the way to the breakpoint. (gdb) bt #0 SgForStatement::post_construction_initialization (this=0x7fffe87db010) at Cxx_Grammar.C:139566 #1 0x00007ffff54e55d8 in SgForStatement::SgForStatement (this=0x7fffe87db010, test=0x0, increment=0x0, loop_body=0x0) at Cxx_GrammarNewConstructors.C:5258 #2 0x00007ffff5bb04ce in EDG_ROSE_Translation::parse_statement (sse=..., existingBasicBlock=0x0) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:49637 #3 0x00007ffff5bbb5ea in EDG_ROSE_Translation::parse_statement_list (sse=..., orig_kind=iek_statement, orig_ptr=0x115f810) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:53079 #4 0x00007ffff5bb0221 in EDG_ROSE_Translation::parse_statement (sse=..., existingBasicBlock=0x7fffe8934010) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:49492 #5 0x00007ffff5c09217 in EDG_ROSE_Translation::parse_function_body<SgFunctionDeclaration> (sse_base=..., p=0x1151ad0, decl=0x7fffe9e21698) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:36262 #6 0x00007ffff5b844fa in EDG_ROSE_Translation::convert_routine (p=0x1151ad0, forceTemplateDeclaration=false, edg_template=0x0, optional_nondefiningTemplateDeclaration=0x0) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:34343 #7 0x00007ffff5b703cf in EDG_ROSE_Translation::parse_routine (sse=..., forceTemplateDeclaration=false, edg_template=0x0, forceSecondaryDeclaration=false) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:29866 #8 0x00007ffff5be6f78 in EDG_ROSE_Translation::parse_global_or_namespace_scope_entity (sse=...) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:64638 #9 0x00007ffff5bea2df in EDG_ROSE_Translation::parse_global_scope (inputGlobalScope=0x7ffff7ec3120, sse=..., skip_ast_translation=false) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:65427 #10 0x00007ffff5bedbee in sage_back_end (sageFile=...) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:66777 #11 0x00007ffff5beea8a in cfe_main (argc=44, argv=0x702f80, sageFile=...) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:66992 #12 0x00007ffff5beebe7 in edg_main (argc=44, argv=0x702f80, sageFile=...) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:67093 #13 0x00007ffff3c14629 in SgSourceFile::build_C_and_Cxx_AST (this=0x7fffeb45e010, argv=..., inputCommandLine=...) at ../../../../sourcetree/src/frontend/SageIII/sage_support/sage_support.cpp:5430 #14 0x00007ffff3c1587a in SgSourceFile::buildAST (this=0x7fffeb45e010, argv=..., inputCommandLine=...) at ../../../../sourcetree/src/frontend/SageIII/sage_support/sage_support.cpp:5983 #15 0x00007ffff3c0e5b7 in SgFile::callFrontEnd (this=0x7fffeb45e010) at ../../../../sourcetree/src/frontend/SageIII/sage_support/sage_support.cpp:3119 #16 0x00007ffff3c0b576 in SgSourceFile::callFrontEnd (this=0x7fffeb45e010) at ../../../../sourcetree/src/frontend/SageIII/sage_support/sage_support.cpp:2137 #17 0x00007ffff3c0a005 in SgFile::runFrontend (this=0x7fffeb45e010, nextErrorCode=@0x7fffffffaadc: 0) at ../../../../sourcetree/src/frontend/SageIII/sage_support/sage_support.cpp:1606 #18 0x00007ffff3c12924 in Rose::Frontend::RunSerial (project=0x7fffeb555010) at ../../../../sourcetree/src/frontend/SageIII/sage_support/sage_support.cpp:4613 #19 0x00007ffff3c12593 in Rose::Frontend::Run (project=0x7fffeb555010) at ../../../../sourcetree/src/frontend/SageIII/sage_support/sage_support.cpp:4506 #20 0x00007ffff3c0b84d in SgProject::RunFrontend (this=0x7fffeb555010) at ../../../../sourcetree/src/frontend/SageIII/sage_support/sage_support.cpp:2209 #21 0x00007ffff3c0bcb2 in SgProject::parse (this=0x7fffeb555010) at ../../../../sourcetree/src/frontend/SageIII/sage_support/sage_support.cpp:2334 #22 0x00007ffff3c0b0d4 in SgProject::parse (this=0x7fffeb555010, argv=...) at ../../../../sourcetree/src/frontend/SageIII/sage_support/sage_support.cpp:2028 #23 0x00007ffff3cbd2e9 in SgProject::SgProject (this=0x7fffeb555010, argv=..., frontendConstantFolding=false) at Cxx_Grammar.C:29114 #24 0x00007ffff645fd54 in frontend (argv=..., frontendConstantFolding=false) at ../../../sourcetree/src/roseSupport/utility_functions.C:628 #25 0x00007ffff645fc10 in frontend (argc=3, argv=0x7fffffffb578, frontendConstantFolding=false) at ../../../sourcetree/src/roseSupport/utility_functions.C:590 #26 0x000000000040b152 in main (argc=3, argv=0x7fffffffb578) at demo.C:40 (gdb) // Again, Breakpoint 2 will be hit twice since we only have two for loops in the input code (gdb) c Continuing. Breakpoint 2, SgForStatement::post_construction_initialization (this=0x7fffe87db138) at Cxx_Grammar.C:139566 139566 if (p_for_init_stmt == NULL) { (gdb) c Continuing. Found a for loop ... Found a for loop ... Traversal ends here. [Inferior 1 (process 47292) exited normally]
在实际代码中,存在数百个相同类类型的对象(例如 SgForStatement)。其中许多来自头文件,并将出现在 AST 中。我们应该只在匹配我们想要检查的对象时停止。通常,我们可以使用对象的内存地址作为条件。
// Add a condition to Breakpoint 2: stop only when the this pointers is equal to a memory address (gdb) cond 2 (unsigned long)this==(unsigned long)0x7fffe87db138 // run the program: now it will stop only when the condition for Breakpoint 2 is met, skipping all other hits to Breakpoint 2. (gdb) r Starting program: /home/liao6/workspace/rose/2019-10-31_14-16-05_-0700/myTranslator/./demo -c inputCode_ExampleTraversals.C .. [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib64/libthread_db.so.1". Breakpoint 2, SgForStatement::post_construction_initialization (this=0x7fffe87db138) at Cxx_Grammar.C:139566 139566 if (p_for_init_stmt == NULL) { // continue the execution, after doing inspections you want. It should go to the normal termination, skipping other hits to Breakpoint 2. (gdb) c Continuing. Found a for loop ... Found a for loop ... Traversal ends here. [Inferior 1 (process 47785) exited normally]
您可以使用观察点,在表达式的值发生变化时停止执行,而无需预测可能发生此情况的特定位置。(这有时称为数据断点。)
观察点可以被视为特殊类型的断点。当监视的内存位置的值发生变化时,它们将停止。当您想知道某个变量(或对象的字段)何时被设置为某个值或其值被清除时,这尤其有用。例如,错误通常与节点某些字段的某些空值有关。这些字段可能在节点构造期间设置。但后来神秘地某个字段变成了 NULL。如果不使用观察点,要找到何时发生这种情况极其困难。
例如,我们希望监视与第二个循环的内存地址匹配的 SgForStatement 的父字段的值变化。
- 我们首先停在有一个断点,在这里我们可以访问节点的内部字段。这通常通过在 SgForStatement::post_construction_initialization() 处停止来完成。
- 一旦内部变量在 gdb 的适当断点处可见,我们就可以获取内部变量的内存地址。这需要您了解内部变量的命名方式。您可以查看对象的类声明,或根据约定猜测。例如,大多数具有访问函数(如 get_something())的东西在 ROSE AST 节点类型中都有一个名为 p_something 的相应内部变量。
- 最后,我们必须监视内存地址的解引用值(监视 *address)。监视内存地址(监视 address)是为了监视一个常量值。它不会起作用。
(gdb) info breakpoints Num Type Disp Enb Address What 1 breakpoint keep n 0x000000000040b0e2 in visitorTraversal::visit(SgNode*) at demo.C:22 2 breakpoint keep y 0x00007ffff3d6495f in SgForStatement::post_construction_initialization() at Cxx_Grammar.C:139566 stop only if (unsigned long)this==(unsigned long)0x7fffe87db138 breakpoint already hit 1 time (gdb) r Starting program: /home/liao6/workspace/rose/2019-10-31_14-16-05_-0700/myTranslator/./demo -c inputCode_ExampleTraversals.C [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib64/libthread_db.so.1". Breakpoint 2, SgForStatement::post_construction_initialization (this=0x7fffe87db138) at Cxx_Grammar.C:139566 139566 if (p_for_init_stmt == NULL) { // the data member storing parent pointer of an AST node is p_parent // it is now have NULL value (gdb) p p_parent $3 = (SgNode *) 0x0 // we obtain the memory address of p_parent (gdb) p &p_parent $4 = (SgNode **) 0x7fffe87db140 // watch value changes of this address // Must deference the address with * , or it will won't work by saying "Cannot watch constant value" (gdb) watch *0x7fffe87db140 // We can now watch the value changes to this memory address // Let's restart the program from the beginning: (gdb) r Starting program: /home/liao6/workspace/rose/2019-10-31_14-16-05_-0700/myTranslator/./demo -c inputCode_ExampleTraversals.C [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib64/libthread_db.so.1". Hardware watchpoint 2: *0x7fffe87db140 Old value = <unreadable> New value = 0 SgNode::SgNode (this=0x7fffe87db138) at Cxx_Grammar.C:2128 2128 p_isModified = false; // we check when the first time its value is changed: the constructor of ancestor node SgNode (gdb) bt #0 SgNode::SgNode (this=0x7fffe87db138) at Cxx_Grammar.C:2128 #1 0x00007ffff3d19f01 in SgLocatedNode::SgLocatedNode (this=0x7fffe87db138, startOfConstruct=0x0) at Cxx_Grammar.C:85278 #2 0x00007ffff3d59798 in SgStatement::SgStatement (this=0x7fffe87db138, startOfConstruct=0x0) at Cxx_Grammar.C:134029 #3 0x00007ffff3d59fcc in SgScopeStatement::SgScopeStatement (this=0x7fffe87db138, file_info=0x0) at Cxx_Grammar.C:134289 #4 0x00007ffff54e54e0 in SgForStatement::SgForStatement (this=0x7fffe87db138, test=0x0, increment=0x0, loop_body=0x0) at Cxx_GrammarNewConstructors.C:5230 #5 0x00007ffff5bb04ce in EDG_ROSE_Translation::parse_statement (sse=..., existingBasicBlock=0x0) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:49637 #6 0x00007ffff5bbb5ea in EDG_ROSE_Translation::parse_statement_list (sse=..., orig_kind=iek_statement, orig_ptr=0x1162200) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:53079 #7 0x00007ffff5bb0221 in EDG_ROSE_Translation::parse_statement (sse=..., existingBasicBlock=0x7fffe8934470) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:49492 #8 0x00007ffff5c09217 in EDG_ROSE_Translation::parse_function_body<SgFunctionDeclaration> (sse_base=..., p=0x1151fc0, decl=0x7fffe9e21e68) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:36262 #9 0x00007ffff5b844fa in EDG_ROSE_Translation::convert_routine (p=0x1151fc0, forceTemplateDeclaration=false, edg_template=0x0, optional_nondefiningTemplateDeclaration=0x0) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:34343 #10 0x00007ffff5b703cf in EDG_ROSE_Translation::parse_routine (sse=..., forceTemplateDeclaration=false, edg_template=0x0, forceSecondaryDeclaration=false) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:29866 #11 0x00007ffff5be6f78 in EDG_ROSE_Translation::parse_global_or_namespace_scope_entity (sse=...) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:64638 #12 0x00007ffff5bea2df in EDG_ROSE_Translation::parse_global_scope (inputGlobalScope=0x7ffff7ec3120, sse=..., skip_ast_translation=false) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:65427 #13 0x00007ffff5bedbee in sage_back_end (sageFile=...) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:66777 #14 0x00007ffff5beea8a in cfe_main (argc=44, argv=0x702f80, sageFile=...) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:66992 #15 0x00007ffff5beebe7 in edg_main (argc=44, argv=0x702f80, sageFile=...) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:67093 #16 0x00007ffff3c14629 in SgSourceFile::build_C_and_Cxx_AST (this=0x7fffeb45e010, argv=..., inputCommandLine=...) at ../../../../sourcetree/src/frontend/SageIII/sage_support/sage_support.cpp:5430 #17 0x00007ffff3c1587a in SgSourceFile::buildAST (this=0x7fffeb45e010, argv=..., inputCommandLine=...) at ../../../../sourcetree/src/frontend/SageIII/sage_support/sage_support.cpp:5983 #18 0x00007ffff3c0e5b7 in SgFile::callFrontEnd (this=0x7fffeb45e010) at ../../../../sourcetree/src/frontend/SageIII/sage_support/sage_support.cpp:3119 #19 0x00007ffff3c0b576 in SgSourceFile::callFrontEnd (this=0x7fffeb45e010) at ../../../../sourcetree/src/frontend/SageIII/sage_support/sage_support.cpp:2137 #20 0x00007ffff3c0a005 in SgFile::runFrontend (this=0x7fffeb45e010, nextErrorCode=@0x7fffffffaadc: 0) at ../../../../sourcetree/src/frontend/SageIII/sage_support/sage_support.cpp:1606 #21 0x00007ffff3c12924 in Rose::Frontend::RunSerial (project=0x7fffeb555010) at ../../../../sourcetree/src/frontend/SageIII/sage_support/sage_support.cpp:4613 #22 0x00007ffff3c12593 in Rose::Frontend::Run (project=0x7fffeb555010) at ../../../../sourcetree/src/frontend/SageIII/sage_support/sage_support.cpp:4506 #23 0x00007ffff3c0b84d in SgProject::RunFrontend (this=0x7fffeb555010) at ../../../../sourcetree/src/frontend/SageIII/sage_support/sage_support.cpp:2209 #24 0x00007ffff3c0bcb2 in SgProject::parse (this=0x7fffeb555010) at ../../../../sourcetree/src/frontend/SageIII/sage_support/sage_support.cpp:2334 #25 0x00007ffff3c0b0d4 in SgProject::parse (this=0x7fffeb555010, argv=...) at ../../../../sourcetree/src/frontend/SageIII/sage_support/sage_support.cpp:2028 #26 0x00007ffff3cbd2e9 in SgProject::SgProject (this=0x7fffeb555010, argv=..., frontendConstantFolding=false) at Cxx_Grammar.C:29114 #27 0x00007ffff645fd54 in frontend (argv=..., frontendConstantFolding=false) at ../../../sourcetree/src/roseSupport/utility_functions.C:628 #28 0x00007ffff645fc10 in frontend (argc=3, argv=0x7fffffffb578, frontendConstantFolding=false) at ../../../sourcetree/src/roseSupport/utility_functions.C:590 #29 0x000000000040b152 in main (argc=3, argv=0x7fffffffb578) at demo.C:40 // We continue the execution (gdb) c Continuing. Hardware watchpoint 2: *0x7fffe87db140 Old value = 0 New value = -393001872 SgNode::set_parent (this=0x7fffe87db138, parent=0x7fffe8934470) at Cxx_Grammar.C:1684 1684 if ( ( variantT() == V_SgClassDeclaration ) && ( parent != NULL && parent->variantT() == V_SgFunctionParameterList ) ) // Now we found that this p_parent field is set by calling set_parent(). We can inspect the call stack and other things of interests (gdb) bt #0 SgNode::set_parent (this=0x7fffe87db138, parent=0x7fffe8934470) at Cxx_Grammar.C:1684 #1 0x00007ffff5bb04ef in EDG_ROSE_Translation::parse_statement (sse=..., existingBasicBlock=0x0) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:49643 #2 0x00007ffff5bbb5ea in EDG_ROSE_Translation::parse_statement_list (sse=..., orig_kind=iek_statement, orig_ptr=0x1162200) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:53079 #3 0x00007ffff5bb0221 in EDG_ROSE_Translation::parse_statement (sse=..., existingBasicBlock=0x7fffe8934470) at ../../../../../../sourcetree/src/frontend/CxxFrontend/EDG/edgRose/edgRose.C:49492 .... // omitted (gdb) c Continuing. Found a for loop ... Found a for loop ... Traversal ends here. [Inferior 1 (process 54495) exited normally] // No more value changes to the same memory address, as expected.
这也被称为树内或源树内构建。libtool 用于构建翻译器。
ROSE 默认情况下打开 -O2 和 -g,因此与 ROSE 一起提供的翻译器应该已经提供了一些调试信息。但是某些变量可能会被优化掉。为了保留最大的调试信息,您可能需要重新配置/重新编译 rose 以关闭优化。
../sourcetree/configure—with-CXX_DEBUG=-g --with-C_OPTIMIZE=-O0—with-CXX_OPTIMIZE=-O0 ...
ROSE 使用 libtool,因此构建树中的可执行文件不是真实的——它们只是围绕实际可执行文件的包装器。您有两个选择
- 在 .lib 目录中找到真正的可执行文件,然后调试那里的真正可执行文件
- 使用 libtool 命令行,如下所示
$ libtool --mode=execute gdb --args ./built_in_translator file1.c
如果可以在 .bashrc 中设置别名命令,请添加以下内容
alias debug='libtool --mode=execute gdb -args'
然后所有调试会话都可以像这样简单地进行
$ debug ./built_in_translator file1.c
其余步骤与使用典型操作(如断点、打印数据等)的常规 gdb 会话相同。
1. 复制报告的错误
$ make check ... ./testVirtualCFG \ --edg:no_warnings -w -rose:verbose 0 --edg:restrict \ -I$ROSE/tests/CompileTests/virtualCFG_tests/../Cxx_tests \ -I$ROSE/sourcetree/tests/CompileTests/A++Code \ -c $ROSE/sourcetree/tests/CompileTests/virtualCFG_tests/../Cxx_tests/test2001_01.C ... lt-testVirtualCFG: $ROSE/src/frontend/SageIII/virtualCFG/virtualCFG.h:111: VirtualCFG::CFGEdge::CFGEdge(VirtualCFG::CFGNode, VirtualCFG::CFGNode): Assertion `src.getNode() != __null && tgt.getNode() != __null' failed.
啊,所以我们在virtualCFG.h头文件中的第 111 行失败了一个断言
Assertion `src.getNode() != __null && tgt.getNode() != __null' failed
并且错误是由运行lt-testVirtualCFGlibtool 可执行翻译器生成的,即实际的翻译器名称为testVirtualCFG(没有lt-前缀)。
2. 使用 Libtool 运行相同的翻译器命令行以启动 GDB 调试会话
$ libtool --mode=execute gdb --args ./testVirtualCFG \ --edg:no_warnings -w -rose:verbose 0 --edg:restrict \ -I$ROSE/tests/CompileTests/virtualCFG_tests/../Cxx_tests \ -I$ROSE/sourcetree/tests/CompileTests/A++Code \ -c $ROSE/sourcetree/tests/CompileTests/virtualCFG_tests/../Cxx_tests/test2001_01.C GNU gdb (GDB) Red Hat Enterprise Linux (7.0.1-42.el5_8.1) Copyright (C) 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-redhat-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from ${ROSE_BUILD_TREE}tests/CompileTests/virtualCFG_tests/.libs/lt-testVirtualCFG...done. (gdb)
GDB 会话已启动,我们提供了命令行提示以开始我们的调试。
3. 让我们运行程序,这将导致断言失败
(gdb) r Starting program: \ ${ROSE_BUILD_TREE}/tests/CompileTests/virtualCFG_tests/.libs/lt-testVirtualCFG \ --edg:no_warnings -w -rose:verbose 0 --edg:restrict \ -I${ROSE}/tests/CompileTests/virtualCFG_tests/../Cxx_tests \ -I../../../../sourcetree/tests/CompileTests/A++Code -c ${ROSE}/tests/CompileTests/virtualCFG_tests/../Cxx_tests/test2001_01.C warning: no loadable sections found in added symbol-file system-supplied DSO at 0x2aaaaaaab000 [Thread debugging using libthread_db enabled] lt-testVirtualCFG: ${ROSE}/src/frontend/SageIII/virtualCFG/virtualCFG.h:111: VirtualCFG::CFGEdge::CFGEdge(VirtualCFG::CFGNode, VirtualCFG::CFGNode): Assertion `src.getNode() != __null && tgt.getNode() != __null' failed. Program received signal SIGABRT, Aborted. 0x0000003752230285 in raise () from /lib64/libc.so.6
好的,我们在 GDB 会话中复制了问题。
4. 让我们检查回溯以了解我们如何最终到达此失败的断言
(gdb) bt #0 0x0000003752230285 in raise () from /lib64/libc.so.6 #1 0x0000003752231d30 in abort () from /lib64/libc.so.6 #2 0x0000003752229706 in __assert_fail () from /lib64/libc.so.6 #3 0x00002aaaad6437b2 in VirtualCFG::CFGEdge::CFGEdge (this=0x7fffffffb300, src=..., tgt=...) at ${ROSE}/../src/frontend/SageIII/virtualCFG/virtualCFG.h:111 #4 0x00002aaaad643b60 in makeEdge<VirtualCFG::CFGNode, VirtualCFG::CFGEdge> (from=..., to=..., result=...) at ${ROSE}/../src/frontend/SageIII/virtualCFG/memberFunctions.C:82 #5 0x00002aaaad62ef7d in SgReturnStmt::cfgOutEdges (this=0xbfaf10, idx=1) at ${ROSE}/../src/frontend/SageIII/virtualCFG/memberFunctions.C:1471 #6 0x00002aaaad647e69 in VirtualCFG::CFGNode::outEdges (this=0x7fffffffb530) at ${ROSE}/../src/frontend/SageIII/virtualCFG/virtualCFG.C:636 #7 0x000000000040bf7f in getReachableNodes (n=..., s=...) at ${ROSE}/tests/CompileTests/virtualCFG_tests/testVirtualCFG.C:13 ...
5. 接下来,我们将向后(或向上)移动程序以到达断言点
(gdb) up #1 0x0000003752231d30 in abort () from /lib64/libc.so.6 (gdb) up #2 0x0000003752229706 in __assert_fail () from /lib64/libc.so.6 (gdb) up #3 0x00002aaaad6437b2 in VirtualCFG::CFGEdge::CFGEdge (this=0x7fffffffb300, src=..., tgt=...) at ${ROSE}/src/frontend/SageIII/virtualCFG/virtualCFG.h:111 111 CFGEdge(CFGNode src, CFGNode tgt): src(src), tgt(tgt) \ { assert(src.getNode() != NULL && tgt.getNode() != NULL); }
好的,所以断言在CFGEdge:
CFGEdge(CFGNode src, CFGNode tgt): src(src), tgt(tgt) \ { assert(src.getNode() != NULL && tgt.getNode() != NULL); # This is the failed assertion }
的构造函数内部。不幸的是,我们无法一目了然地看出断言中的两个条件中的哪一个失败了。
6. 找出断言失败的原因
让我们检查断言中的两个条件
(gdb) p src.getNode() $1 = (SgNode *) 0xbfaf10
所以src.getNode()正在返回指向SgNode的非空指针。怎么样tgt.getNode()?
(gdb) p tgt.getNode() $2 = (SgNode *) 0x0
啊,这就是罪魁祸首。所以出于某种原因,tgt.getNode()正在返回一个空SgNode指针(0x0).
从这里,我们使用 GDBup命令在程序中回溯以找出tgt.getNode()返回的节点在哪里被分配了 NULL 值。
我们最终找到了对SgReturnStmt::cfgOutEdges的调用,它返回一个名为enclosingFunc的变量。在源代码中,当前没有断言来检查enclosingFunc的值,这就是为什么我们在程序的后面收到断言的原因。作为旁注,最好尽快在源代码中添加断言,这样在像这样的时候,我们就不必花费不必要的时间进行回溯。
在为enclosingFunc添加断言后,我们再次运行程序以到达这个新的断言点
lt-testVirtualCFG: ${ROSE}sourcetree/src/frontend/SageIII/virtualCFG/memberFunctions.C:1473: \ virtual std::vector<VirtualCFG::CFGEdge, std::allocator<VirtualCFG::CFGEdge> > \ SgReturnStmt::cfgOutEdges(unsigned int): \ Assertion `enclosingFunc != __null' failed.
好的,它失败了,所以我们知道对enclosingFunc的赋值为 NULL。
# enclosingFunc is definitely NULL (0x0) (gdb) p enclosingFunc $1 = (SgFunctionDefinition *) 0x0 # What is the current context? (gdb) p this $2 = (SgReturnStmt * const) 0xbfaf10
好的,我们在一个SgReturnStmt对象内部。让我们设置一个断点,其中enclosingFunc正在被赋值给
Breakpoint 1, SgReturnStmt::cfgOutEdges (this=0xbfaf10, idx=1) at ${ROSE}/src/frontend/SageIII/virtualCFG/memberFunctions.C:1472 1472 SgFunctionDefinition* enclosingFunc = SageInterface::getEnclosingProcedure(this);
所以这就是我们正在检查的行
SgFunctionDefinition* enclosingFunc = SageInterface::getEnclosingProcedure(this);
所以 NULL 值必须来自SageInterface::getEnclosingProcedure(this);.
在代码审查函数getEnclosingProcedure后,我们发现了算法中的一个缺陷。
该函数尝试返回一个SgNode,它是指定类型SgFunctionDefinition的封闭过程。但是,在检查函数在return点处的状态时,我们发现它错误地检测到一个SgBasicBlack作为SgReturnStmt.
(gdb) p parent->class_name() $12 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x7cd0e8 "SgBasicBlock"}}
的封闭过程。具体来说,最后一段0x7cd0e8 "SgBasicBlock".
但这是错误的,因为我们正在寻找SgFunctionDefinition,而不是SgBasicBlock.
经过进一步检查,我们发现该函数只是返回它找到的第一个封闭节点,而不是第一个匹配用户条件的封闭节点。
我们添加了必要的逻辑以使函数完整,对其进行了测试以验证其正确性,然后解决了错误。