GDB显示LLVM IR变量失败

GDB显示LLVM IR变量失败,gdb,llvm,lldb,llvm-ir,lli,Gdb,Llvm,Lldb,Llvm Ir,Lli,我构建了一个简单的应用程序: 你好,c #include <stdio.h> void foo(int x) { printf("%d", x); } int main() { printf("Hello\n"); foo(6+5); return 0; } 使用工具,我将其转换为: ./debugir hello.ll 你好,dbg.ll ; ModuleID = 'hello.ll' source_

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

你好,c

#include <stdio.h>

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

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

使用工具,我将其转换为:

./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)打印%3

GDB对LLVM IR不了解(实际上什么都不知道)

这不是GDB支持的语言之一(汇编、C、C++、FORTRAN、艾达等)。 是2012年的一条相关线索

(gdb)打印%3

GDB对LLVM IR不了解(实际上什么都不知道)

这不是GDB支持的语言之一(汇编、C、C++、FORTRAN、艾达等)。


是2012年的相关线程。

不幸的是,调试IR过程只添加行号信息,而不添加变量位置和类型信息。就我所知,你所要求的是无法实现的

从LLVM类型构建debuginfo类型的过程(
getOrCreateType()
)中有一些代码。我想这可以扩展,然后将可变位置信息添加到IR中,以实现您想要的。但这不是一件小事


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

不幸的是,调试IR过程只添加了行号信息,而没有变量位置和类型信息。就我所知,你所要求的是无法实现的

从LLVM类型构建debuginfo类型的过程(
getOrCreateType()
)中有一些代码。我想这可以扩展,然后将可变位置信息添加到IR中,以实现您想要的。但这不是一件小事


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

那么如何检查变量呢?它确实从位码文件中读取行信息,所以它也应该能够查找变量。也许阅读
debug ir
pass会有所帮助。我认为这里的前提是错误的。“%3”在任何意义上都不是一个变量,而是某种SSA的东西,正如我所理解的。对GDB的任何假定LLVM IR支持都不会使检查“%3”成为可能,是吗?它甚至不是传统意义上的“变量”。[llvmpf…]那么如何检查变量?它确实从位码文件中读取行信息,因此它也应该能够查找变量。也许阅读
debug ir
pass会有所帮助。我认为这里的前提是错误的。“%3”在任何意义上都不是一个变量,而是某种SSA的东西,正如我所理解的。对GDB的任何假定LLVM IR支持都不会使检查“%3”成为可能,是吗?它甚至不是传统意义上的“变量”。[叹气希望llvmpf…]如果我们无法浏览变量,那么调试器的作用是什么?@ar2015跟踪控制流(遵循“有总比没有好”的理念)。至少在某人(或我)有时间将变量位置信息标记到IR之前。@ar2015我已经添加了一些打印变量的基本支持。但这还远远不够完美。如果我们无法浏览变量,那么调试器的作用是什么?@ar2015跟踪控制流(遵循“有总比没有好”的理念)。至少在某人(或我)有时间将变量位置信息标记到IR之前。@ar2015我已经添加了一些打印变量的基本支持。但这还远远不够完美。
(gdb) print %3
A syntax error in expression, near `%3'.