GDB显示LLVM IR变量失败 [英] GDB display LLVM IR variables fails

查看:114
本文介绍了GDB显示LLVM IR变量失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个简单的应用程序:

I build a simple application:

hello.c

#include <stdio.h>

void foo(int x)
{
    printf("%d", x);
}

int main()
{
    printf("Hello\n");
    foo(6+5);
    return 0;
}

具有LLVM IR表示形式:

With an LLVM IR representation:

; ModuleID = 'hello.ll'
source_filename = "hello.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
@.str.1 = private unnamed_addr constant [7 x i8] c"Hello\0A\00", align 1

; Function Attrs: noinline nounwind optnone uwtable
define dso_local void @foo(i32 %0) #0 {
  %2 = alloca i32, align 4
  store i32 %0, i32* %2, align 4
  %3 = load i32, i32* %2, align 4
  %4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %3)
  ret void
}

; Function Attrs: nounwind readnone speculatable willreturn
declare void @llvm.dbg.declare(metadata, metadata, metadata) #1

declare dso_local i32 @printf(i8*, ...) #2

; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  store i32 0, i32* %1, align 4
  %2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
  call void @foo(i32 11)
  ret i32 0
}

attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { nounwind readnone speculatable willreturn }
attributes #2 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }

使用 debug.ir 工具,将其转换为:

Using debug.ir tool, I convert it to:

./debugir hello.ll

hello.dbg.ll

hello.dbg.ll

; ModuleID = 'hello.ll'
source_filename = "hello.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
@.str.1 = private unnamed_addr constant [7 x i8] c"Hello\0A\00", align 1

; Function Attrs: noinline nounwind optnone uwtable
define dso_local void @foo(i32 %0) #0 !dbg !4 {
  %2 = alloca i32, align 4, !dbg !9
  store i32 %0, i32* %2, align 4, !dbg !10
  %3 = load i32, i32* %2, align 4, !dbg !11
  %4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %3), !dbg !12
  ret void, !dbg !13
}

; Function Attrs: nounwind readnone speculatable willreturn
declare void @llvm.dbg.declare(metadata, metadata, metadata) #1

declare dso_local i32 @printf(i8*, ...) #2

; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 @main() #0 !dbg !14 {
  %1 = alloca i32, align 4, !dbg !17
  store i32 0, i32* %1, align 4, !dbg !18
  %2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0)), !dbg !19
  call void @foo(i32 11), !dbg !20
  ret i32 0, !dbg !21
}

attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { nounwind readnone speculatable willreturn }
attributes #2 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!3}

!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "LLVM Version 10.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
!1 = !DIFile(filename: "hello.ll", directory: "")
!2 = !{}
!3 = !{i32 2, !"Debug Info Version", i32 3}
!4 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: !1, file: !1, line: 9, type: !5, scopeLine: 11, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
!5 = !DISubroutineType(types: !6)
!6 = !{!7, !8}
!7 = !DIBasicType(tag: DW_TAG_unspecified_type, name: "void")
!8 = !DIBasicType(name: "i32", size: 32, encoding: DW_ATE_unsigned)
!9 = !DILocation(line: 11, scope: !4)
!10 = !DILocation(line: 12, scope: !4)
!11 = !DILocation(line: 13, scope: !4)
!12 = !DILocation(line: 14, scope: !4)
!13 = !DILocation(line: 15, scope: !4)
!14 = distinct !DISubprogram(name: "main", linkageName: "main", scope: !1, file: !1, line: 23, type: !15, scopeLine: 25, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
!15 = !DISubroutineType(types: !16)
!16 = !{!8}
!17 = !DILocation(line: 25, scope: !14)
!18 = !DILocation(line: 26, scope: !14)
!19 = !DILocation(line: 27, scope: !14)
!20 = !DILocation(line: 28, scope: !14)
!21 = !DILocation(line: 29, scope: !14)

现在运行gdb:

gdb lli-10
(gdb) set args -jit-kind=mcjit hello.dbg.ll
(gdb) break hello.ll:14
(gdb) run
Starting program: /usr/bin/lli-10 -jit-kind=mcjit hello.dbg.ll
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Hello

Breakpoint 1, foo () at hello.ll:14
14    %4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %3)
(gdb) 

当我尝试获取变量值时,会出现错误:

When I try to get the variable value, I will get an error:

(gdb) print %3
A syntax error in expression, near `%3'.

如何获取%3 变量的值?

推荐答案

不幸的是,Debug-IR传递仅添加行号信息,而不添加变量位置和类型信息.因此,据我所知,您的要求无法实现.

The Debug-IR pass only adds line number information and not variable location and type information, unfortunately. So what you're asking for cannot be achieved as far as I know.

在传递代码( getOrCreateType())中有一些代码可以从LLVM类型构建debuginfo类型.我想这可以扩展,然后将可变位置信息添加到IR中以实现您想要的功能.不过,这是一项不平凡的工作.

There is some code in the pass (getOrCreateType()) to build debuginfo types from LLVM types. I suppose this can be extended, and then variable location information added to the IR to achieve what you want. It's non-trivial work though.

我已经更新了该工具,以注释一些可变的位置信息.GDB现在可以打印一些局部变量( info locals ).但是,它远非完美.

edit: I've updated the tool to annotate some variable location information. GDB can now print some local variables (info locals). It is far from perfect though.

这篇关于GDB显示LLVM IR变量失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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