为什么我没有从 LLVM 获得精确的 line/column 调试信息

why I am not getting precise line/column debug info from LLVM

我正在编写一个非常简单的 LLVM pass 来简单地迭代函数中的所有指令。

bool TestInstrument::runOnFunction(Function &F)
{
    for (inst_iterator it = inst_begin(F), E = inst_end(F); it != E; ++it)
    {
        std::string str;
        llvm::raw_string_ostream ss(str);
        ss << *it;
        errs() << ss.str() << "\n";

        const DebugLoc &location = (&(*it))->getDebugLoc();
        if (location)
            std::cout << " see line:  " << location.getLine() << ", col " << location.getCol() << " \n";
        else
            std::cout << "no debugloc information detected\n";
    }
    return true;
}

我使用以下命令构建了我的通行证

clang -emit-llvm -S -fno-discard-value-names -c hello.c
opt -load ../build/InstrumentPass.so -Instrument -S hello.ll -o hello.instrumented.ll

opt -load ../build/SecondInstrumentPass.so -SecondInstrument -S hello.ll -o hello.second.instrumented.ll
clang -o hello ../lib/util.c hello.second.instrumented.ll

当我 运行 上面的工具传递一个非常简单的 hello world c 程序时,我可以看到每条迭代指令的打印输出。但是,指令的 none 找到了 debugloc 信息。该位置的所有打印输出都显示“没有 debugloc 信息 detected\n”。

这是打印的输出

  %retval = alloca i32, align 4
no debug info!
  store i32 0, i32* %retval, align 4
no debug info!
  %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i32 0, i32 0))
no debug info!
  ret i32 0
no debug info!

有没有找到位置元数据的原因?提前致谢。

我没有得到任何 debugloc 信息的原因是在我的构建命令中,我使用了

clang -emit-llvm -S -fno-discard-value-names -c hello.c

应该用过

clang -emit-llvm -S -fno-discard-value-names -c hello.c -g

-g 选项启用调试信息。