如何使用dbg元数据获取变量定义行号等? [英] How to get variable definition line number etc. using dbg metadata?

查看:115
本文介绍了如何使用dbg元数据获取变量定义行号等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,当我需要获取局部变量的行号时,我不得不寻找llvm.dbg.declare内在函数的调用并获取dbg元数据(因为AllocaInst本身不包含任何dbg信息).但是,似乎不能保证此CallInstAllocaInst的下一条指令,并且我必须在指定的函数中遍历该指令,这效率很低.所以我想知道是否有AllocaInst直接获取llvm.dbg.declare指令的方法.

As far as I know, when I need to get the line number of a local variable I had to look for the invocation of the llvm.dbg.declare intrinsics and get the dbg metadata(since AllocaInst itself does not contain any dbg info). However there seems no guarantee that this CallInst is the next instruction of the the AllocaInst, and I have to traverse the instruction in a specified function, which is inefficient. So I'm wondering whether there is a method for AllocaInst to get the llvm.dbg.declare instruction directly.

例如,在名为foo.c的src中:

For example,in a src called foo.c:

int foo(){
     int a;
}

和相应的llvm ir:

and the corresponding llvm ir:

define i32 @foo() nounwind {
entry:
  %retval = alloca i32
  %a = alloca i32
  %"alloca point" = bitcast i32 0 to i32
  call void @llvm.dbg.declare(metadata !{i32* %a}, metadata !7), !dbg !9
  br label %return, !dbg !10

return:                                           ; preds = %entry
  %retval1 = load i32* %retval, !dbg !10                                                                                                     
  ret i32 %retval1, !dbg !10
}
......
!9 = metadata !{i32 3, i32 0, metadata !8, null}

如果我想知道foo.c中定义的int a;的行号,则必须遍历ir并从call void @llvm.dbg.declare(metadata !{i32* %a}, metadata !7), !dbg !9中获取!dbg !9.

If I need to know the line number of int a; defined in foo.c I have to traverse the ir and get !dbg !9 from call void @llvm.dbg.declare(metadata !{i32* %a}, metadata !7), !dbg !9.

顺便说一句,由于llvm.dbg.gv包含非常详细的信息,因此处理全局变量似乎没有困难.

BTW, there seems no difficulty when dealing with global variable since llvm.dbg.gv contains the very info.

推荐答案

我终于弄清楚了我们可以在DbgInfoPrinter.cpp中使用静态方法DbgDeclareInst* findDbgDeclare(const Value *V),这可以从

I finally figured out that we could use the static method DbgDeclareInst* findDbgDeclare(const Value *V) in DbgInfoPrinter.cpp,which can be seen here

破解这段代码也并不难^ _ ^

It's also not very difficult to hack this piece of code^_^

这篇关于如何使用dbg元数据获取变量定义行号等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆