首页 > 解决方案 > 为什么我没有从 LLVM 获得精确的行/列调试信息

问题描述

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

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 程序上运行上述仪器传递时,我可以看到每条迭代指令的打印输出。但是,没有一条指令找到 debugloc 信息。该位置的所有打印输出都显示“未检测到调试位置信息\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!

找不到位置元数据有什么原因吗?提前致谢。

标签: clangllvmclang++llvm-ir

解决方案


我没有得到任何 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 选项启用调试信息。


推荐阅读