【问题标题】:Acquiring the name(or the number) of the debug info in LLVM-IR获取 LLVM-IR 中调试信息的名称(或编号)
【发布时间】:2017-03-14 15:46:05
【问题描述】:

我想在 IR 中打印调试信息的确切数量,我该怎么做?

例如,考虑如下的 IR 块,

call void @llvm.dbg.declare(metadata i32* %a, metadata !10, metadata !11), !dbg !12!
!12 = !DILocation(line: 19, column: 7, scope: !6)

我想将!12 打印为字符串以进行调试。我可以通过做得到DILocation的对象

Instruction::getDebugLoc()->get()

但我得到的只是一个指针,没有这样的接口来获取数字。我可以假设 LLVM 在实际生成位码时给出了数字,因为转储 DILocation 会给出类似

的结果
<0x7342628> = !DILocation(line: 23, column: 3, scope: <0x733e5f8>)

这个。但是当我使用 Instruction::dump() 时,它给了我一些看起来像

call void @llvm.dbg.declare(metadata i32* %a, metadata !10, metadata !11), !dbg !12

这个,所以我很困惑它在运行时是否有调试信息的编号信息。

是否有编号信息?如果是这样,我如何获得该信息?如果没有,我应该在哪里检查以查找 LLVM 中位码的生成?

【问题讨论】:

    标签: llvm llvm-ir


    【解决方案1】:

    您是在谈论行号/列号吗?如果是这样,那么您可以直接从debugLoc 轻松访问它们:

    instruction->getDebugLoc()->getLine()
    instruction->getDebugLoc()->getColumn()
    

    查看DebugInfoMetadata的定义:

    unsigned getLine() const { return SubclassData32; }
    unsigned getColumn() const { return SubclassData16; }
    

    【讨论】:

    • 与行号和列号无关。我想打印出调试信息的编号(在 IR 中),在这种情况下为 !12
    • @izazu,哦,我的错。我将其解释为对调试信息节点的引用。
    【解决方案2】:

    现在回答这个问题可能还为时不晚。

    if (instruction->hasMetadata()) {
      instruction->dump();
    
      // one way
      SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
      instruction->getAllMetadata(MDs);
      for (auto &MD : MDs) {
        if (MDNode *N = MD.second) {
          N->printAsOperand(errs(), instruction->getModule());
          errs() << "\n";
        }
      }
    
      // second way
      instruction->getDebugLoc()->printAsOperand(errs(), instruction->getModule());
      errs() << "\n";
    
      // third way
      int debugInfoKindID = 0;
      MDNode *debug = instruction->getMetadata(debugInfoKindID);
      debug->printAsOperand(errs(), instruction->getModule());
      errs() << "\n";
    }
    

    输出是:

    %11 = add nsw i32 %9, %10, !dbg !29
    !29
    !29
    !29
    

    我通过查看llvm//unittests/IR/MetadataTest.cpp 找到了这个,它的测试TEST_F(MDNodeTest, PrintFromMetadataAsValue)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-11
      • 1970-01-01
      相关资源
      最近更新 更多