モチベーション
llvmのフロントエンドを開発するとき,LLVM IRのアセンブラを直接デバッグ検証したいことがあると思います。
ネットに有用な情報がなかったので方法を探してみました。
他のフロントエンド開発者の方はどうしているのでしょうか・・・?
結論
-
debugify
という最適化パスを通せば,lldbでデバッグできる。 - ふつうのソースのデバッグよりかなりめんどい。
詳細
debugifyパスによるデバッグ情報付きのLLVMの生成
debugify
はもともとデバッグ情報が他の最適化パスによって不整合にならないことを検証するためのものでありすべてのLLVM IRの命令,変数,関数にデバッグ情報を付加するパスである。
ここからは例として,以下のtest.c
から生成したLLVM IRをベースに説明する。
/*[ test.c ]*/
int add(int x, int y);
int main() {
int a, b, c;
a = 1;
b = 99;
c = a + b;
c = 0;
add(a, b);
return 0;
}
int add(int x, int y) {
return x + y;
}
# [test.ll]
; ModuleID = 'test.c'
source_filename = "test.c"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
; Function Attrs: norecurse nounwind readnone uwtable
define dso_local i32 @main() local_unnamed_addr #0 {
ret i32 0
}
; Function Attrs: norecurse nounwind readnone uwtable
define dso_local i32 @add(i32, i32) local_unnamed_addr #0 {
%3 = add nsw i32 %1, %0
ret i32 %3
}
attributes #0 = { norecurse nounwind readnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-frame-pointer-elim"="false" "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"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
!llvm.module.flags = !{!0}
!llvm.ident = !{!1}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang version 8.0.0-3~ubuntu18.04.1 (tags/RELEASE_800/final)"}
以下のコマンドでdebugify
によりデバッグ情報を付加したLLVM IRを生成する。
opt test.bc -debugify -S -o test_dbg.ll
# [test_dbg.ll]
; ModuleID = 'test.bc'
source_filename = "test.c"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 @main() #0 !dbg !8 {
%1 = alloca i32, align 4, !dbg !23
call void @llvm.dbg.value(metadata i32* %1, metadata !11, metadata !DIExpression()), !dbg !23
%2 = alloca i32, align 4, !dbg !24
call void @llvm.dbg.value(metadata i32* %2, metadata !13, metadata !DIExpression()), !dbg !24
%3 = alloca i32, align 4, !dbg !25
call void @llvm.dbg.value(metadata i32* %3, metadata !14, metadata !DIExpression()), !dbg !25
%4 = alloca i32, align 4, !dbg !26
call void @llvm.dbg.value(metadata i32* %4, metadata !15, metadata !DIExpression()), !dbg !26
store i32 0, i32* %1, align 4, !dbg !27
store i32 1, i32* %2, align 4, !dbg !28
store i32 99, i32* %3, align 4, !dbg !29
%5 = load i32, i32* %2, align 4, !dbg !30
call void @llvm.dbg.value(metadata i32 %5, metadata !16, metadata !DIExpression()), !dbg !30
%6 = load i32, i32* %3, align 4, !dbg !31
call void @llvm.dbg.value(metadata i32 %6, metadata !18, metadata !DIExpression()), !dbg !31
%7 = add nsw i32 %5, %6, !dbg !32
call void @llvm.dbg.value(metadata i32 %7, metadata !19, metadata !DIExpression()), !dbg !32
store i32 %7, i32* %4, align 4, !dbg !33
store i32 0, i32* %4, align 4, !dbg !34
%8 = load i32, i32* %2, align 4, !dbg !35
call void @llvm.dbg.value(metadata i32 %8, metadata !20, metadata !DIExpression()), !dbg !35
%9 = load i32, i32* %3, align 4, !dbg !36
call void @llvm.dbg.value(metadata i32 %9, metadata !21, metadata !DIExpression()), !dbg !36
%10 = call i32 @add(i32 %8, i32 %9), !dbg !37
call void @llvm.dbg.value(metadata i32 %10, metadata !22, metadata !DIExpression()), !dbg !37
ret i32 0, !dbg !38
}
; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 @add(i32, i32) #0 !dbg !39 {
%3 = alloca i32, align 4, !dbg !46
call void @llvm.dbg.value(metadata i32* %3, metadata !41, metadata !DIExpression()), !dbg !46
%4 = alloca i32, align 4, !dbg !47
call void @llvm.dbg.value(metadata i32* %4, metadata !42, metadata !DIExpression()), !dbg !47
store i32 %0, i32* %3, align 4, !dbg !48
store i32 %1, i32* %4, align 4, !dbg !49
%5 = load i32, i32* %3, align 4, !dbg !50
call void @llvm.dbg.value(metadata i32 %5, metadata !43, metadata !DIExpression()), !dbg !50
%6 = load i32, i32* %4, align 4, !dbg !51
call void @llvm.dbg.value(metadata i32 %6, metadata !44, metadata !DIExpression()), !dbg !51
%7 = add nsw i32 %5, %6, !dbg !52
call void @llvm.dbg.value(metadata i32 %7, metadata !45, metadata !DIExpression()), !dbg !52
ret i32 %7, !dbg !53
}
; Function Attrs: nounwind readnone speculatable
declare void @llvm.dbg.value(metadata, metadata, metadata) #1
attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "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"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { nounwind readnone speculatable }
!llvm.module.flags = !{!0, !1}
!llvm.ident = !{!2}
!llvm.dbg.cu = !{!3}
!llvm.debugify = !{!6, !7}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 2, !"Debug Info Version", i32 3}
!2 = !{!"clang version 8.0.0-3~ubuntu18.04.1 (tags/RELEASE_800/final)"}
!3 = distinct !DICompileUnit(language: DW_LANG_C, file: !4, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !5)
!4 = !DIFile(filename: "test.bc", directory: "/")
!5 = !{}
!6 = !{i32 24}
!7 = !{i32 15}
!8 = distinct !DISubprogram(name: "main", linkageName: "main", scope: null, file: !4, line: 1, type: !9, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !3, retainedNodes: !10)
!9 = !DISubroutineType(types: !5)
!10 = !{!11, !13, !14, !15, !16, !18, !19, !20, !21, !22}
!11 = !DILocalVariable(name: "1", scope: !8, file: !4, line: 1, type: !12)
!12 = !DIBasicType(name: "ty64", size: 64, encoding: DW_ATE_unsigned)
!13 = !DILocalVariable(name: "2", scope: !8, file: !4, line: 2, type: !12)
!14 = !DILocalVariable(name: "3", scope: !8, file: !4, line: 3, type: !12)
!15 = !DILocalVariable(name: "4", scope: !8, file: !4, line: 4, type: !12)
!16 = !DILocalVariable(name: "5", scope: !8, file: !4, line: 8, type: !17)
!17 = !DIBasicType(name: "ty32", size: 32, encoding: DW_ATE_unsigned)
!18 = !DILocalVariable(name: "6", scope: !8, file: !4, line: 9, type: !17)
!19 = !DILocalVariable(name: "7", scope: !8, file: !4, line: 10, type: !17)
!20 = !DILocalVariable(name: "8", scope: !8, file: !4, line: 13, type: !17)
!21 = !DILocalVariable(name: "9", scope: !8, file: !4, line: 14, type: !17)
!22 = !DILocalVariable(name: "10", scope: !8, file: !4, line: 15, type: !17)
!23 = !DILocation(line: 1, column: 1, scope: !8)
!24 = !DILocation(line: 2, column: 1, scope: !8)
!25 = !DILocation(line: 3, column: 1, scope: !8)
!26 = !DILocation(line: 4, column: 1, scope: !8)
!27 = !DILocation(line: 5, column: 1, scope: !8)
!28 = !DILocation(line: 6, column: 1, scope: !8)
!29 = !DILocation(line: 7, column: 1, scope: !8)
!30 = !DILocation(line: 8, column: 1, scope: !8)
!31 = !DILocation(line: 9, column: 1, scope: !8)
!32 = !DILocation(line: 10, column: 1, scope: !8)
!33 = !DILocation(line: 11, column: 1, scope: !8)
!34 = !DILocation(line: 12, column: 1, scope: !8)
!35 = !DILocation(line: 13, column: 1, scope: !8)
!36 = !DILocation(line: 14, column: 1, scope: !8)
!37 = !DILocation(line: 15, column: 1, scope: !8)
!38 = !DILocation(line: 16, column: 1, scope: !8)
!39 = distinct !DISubprogram(name: "add", linkageName: "add", scope: null, file: !4, line: 17, type: !9, scopeLine: 17, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !3, retainedNodes: !40)
!40 = !{!41, !42, !43, !44, !45}
!41 = !DILocalVariable(name: "11", scope: !39, file: !4, line: 17, type: !12)
!42 = !DILocalVariable(name: "12", scope: !39, file: !4, line: 18, type: !12)
!43 = !DILocalVariable(name: "13", scope: !39, file: !4, line: 21, type: !17)
!44 = !DILocalVariable(name: "14", scope: !39, file: !4, line: 22, type: !17)
!45 = !DILocalVariable(name: "15", scope: !39, file: !4, line: 23, type: !17)
!46 = !DILocation(line: 17, column: 1, scope: !39)
!47 = !DILocation(line: 18, column: 1, scope: !39)
!48 = !DILocation(line: 19, column: 1, scope: !39)
!49 = !DILocation(line: 20, column: 1, scope: !39)
!50 = !DILocation(line: 21, column: 1, scope: !39)
!51 = !DILocation(line: 22, column: 1, scope: !39)
!52 = !DILocation(line: 23, column: 1, scope: !39)
!53 = !DILocation(line: 24, column: 1, scope: !39)
ブレークポイントの算出
前項で生成したtest_dbg.ll
のブレークしたい行の末尾に追記されている!dbg !<number>
に注目する。
この<number>
はファイル末尾に追加されたデバッグ情報と紐づいている。
例として,test_dbg.ll
の関数add()
の末尾ret i32 %7, !dbg !53
に対するデバッグ情報はファイル末尾の以下の行となる:
!53 = !DILocation(line: 24, column: 1, scope: !39)
また,以下の行から,デバッグ情報ソースファイルtest.bc
に紐づいていることがわかる:
!4 = !DIFile(filename: "test.bc", directory: "/")
ここで,デバッグ情報のline
やcolumn
は機械的に採番されておりtest.bc
の実際の行番号とは紐づいていないことに注意されたい。(optでパースしたLLVM IRの抽象表現ではパース元のソースファイル位置情報を知りえないため。)
ただし,上記の情報を得ることで,ブレークしたいプログラムアドレスのデバッグ位置情報はtest.bc
の24
行目ということがわかれば,lldbのブレークポイントをtest.bc:24
とすることで所望のプログラムアドレスでブレークできる。
デバッグ実行
上記で求めたブレークポイントでブレークし,変数をダンプした結果がこちら。
clang test_dbg.ll
lldb a.out
(lldb) target create "a.out"
Current executable set to 'a.out' (x86_64).
(lldb) b test.bc:24
Breakpoint 1: where = a.out`add + 18 at test.bc:24:1, address = 0x0000000000400502
(lldb) r
Process 5523 launched: '/home/muraak/test/a.out' (x86_64)
Process 5523 stopped
* thread #1, name = 'a.out', stop reason = breakpoint 1.1
frame #0: 0x0000000000400502 a.out`add at test.bc:24:1
(lldb) var
(unsigned int) 13 = <variable not available>
(unsigned int) 14 = 99
(unsigned int) 15 = 100
(unsigned long) 11 = <no location, value may have been optimized out>
(unsigned long) 12 = <no location, value may have been optimized out>
変数15
は
!45 = !DILocalVariable(name: "15", scope: !39, file: !4, line: 23, type: !17)
と,
call void @llvm.dbg.value(metadata i32 %7, metadata !45, metadata !DIExpression()), !dbg !52
より,仮想レジスタ%7
の値であり,
ret i32 %7, !dbg !53
より,add関数の戻り値である。
したがって,1 + 99 = 100
であることから戻り値が正しく算出されていることが確認できる。
(ややこしいけど。。。)
以上