【问题标题】:GDB display LLVM IR variables failsGDB 显示 LLVM IR 变量失败
【发布时间】:2021-07-18 07:15:24
【问题描述】:

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

你好.c

#include <stdio.h>

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

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

使用 LLVM IR 表示:

; 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 工具,我将其转换为:

./debugir hello.ll

你好.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) 

当我尝试获取变量值时,会报错:

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

我应该如何获取%3变量的值?

【问题讨论】:

    标签: gdb llvm lldb llvm-ir lli


    【解决方案1】:

    很遗憾,Debug-IR pass 只添加了行号信息,而不是变量位置和类型信息。因此,据我所知,您所要求的无法实现。

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

    编辑:我更新了工具以注释一些可变位置信息。 GDB 现在可以打印一些局部变量 (info locals)。但它远非完美。

    【讨论】:

    • 如果我们不能浏览变量,那么调试器的意义何在?
    • @ar2015 跟踪控制流程(遵循“总比没有好”的理念)。至少在某人(或我)有时间将可变位置信息标记到 IR 之前。
    • @ar2015 我添加了一些对打印变量的基本支持。 github.com/vaivaswatha/debugir/commit/…。但它远非完美。
    【解决方案2】:

    (gdb) print %3

    GDB 对 LLVM IR 不了解(事实上,一无所知)。

    它不是 GDB 支持的语言之一(汇编、C、C++、Fortran、Ada 等)。

    Here 是 2012 年的相关主题。

    【讨论】:

    • 那么如何检查变量呢?
    • 嗯,它确实从位码文件中读取行信息,因此它也应该能够查找变量。也许阅读debug-ir pass 会有所帮助。
    • 我认为整个前提都在这里。据我了解,“%3”在任何意义上都不是变量,而是某种 SSA 的东西。没有任何一种假设的 LLVM IR 对 GDB 的支持会使检查“%3”成为可能,不是吗?它甚至不是传统意义上的“变量”。 [sigh 想要 llvmpf...]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 2013-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多