Hi all,<br><br>I'm generating code using CLANG + LLVM 2.9 and would like to inspect formal parameter value for x86 32-bit when -O2 -g is used.<br>It seems that when code is optimized by the compiler DWARF information generated doesn't allow to inspect value of parameter.<br>
Trying to inspect parameter value in GDB, parameter is marked as optimized by the compiler and thus I can't track its value.<br>A simple example to reproduce the problem is compile following code as follows:<br><br>clang -m32 -O2 -g -c argpass.c <br>
<br>extern void bar(int ) ;<br>extern void zzz() ;<br><br>void foo(int c)<br>{<br>    bar(c) ;<br>    if (c == 0)<br>        zzz() ;<br>}<br><br><br>Looking at LLVM code generated, I've got:<br><br>; ModuleID = 'argpass.c'<br>
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32"<br>target triple = "i386-unknown-linux-gnu"<br><br>define void @foo(i32 %c) nounwind {<br>
  tail call void @llvm.dbg.value(metadata !{i32 %c}, i64 0, metadata !5), !dbg !7<br>  tail call void @bar(i32 %c) nounwind, !dbg !8<br>  %1 = icmp eq i32 %c, 0, !dbg !10<br>  br i1 %1, label %2, label %3, !dbg !10<br><br>
; <label>:2                                       ; preds = %0<br>  tail call void (...)* @zzz() nounwind, !dbg !11<br>  br label %3, !dbg !11<br><br>; <label>:3                                       ; preds = %2, %0<br>
  ret void, !dbg !12<br>}<br><br>declare void @bar(i32)<br><br>declare void @zzz(...)<br><br>declare void @llvm.dbg.value(metadata, i64, metadata) nounwind readnone<br><br>!llvm.dbg.sp = !{!0}<br>!llvm.dbg.lv.foo = !{!5}<br>
<br>!0 = metadata !{i32 589870, i32 0, metadata !1, metadata !"foo", metadata !"foo", metadata !"", metadata !1, i32 6, metadata !3, i1 false, i1 true, i32 0, i32 0, i32 0, i32 256, i1 true, void (i32)* @foo} ; [ DW_TAG_subprogram ]<br>
!1 = metadata !{i32 589865, metadata !"argpass.c", metadata !"/home/deldon/Work/OpenCL/DEBUG", metadata !2} ; [ DW_TAG_file_type ]<br>!2 = metadata !{i32 589841, i32 0, i32 12, metadata !"argpass.c", metadata !"/home/deldon/Work/OpenCL/DEBUG", metadata !"clang version 2.9 (tags/RELEASE_29/final)", i1 true, i1 true, metadata !"", i32 0} ; [ DW_TAG_compile_unit ]<br>
!3 = metadata !{i32 589845, metadata !1, metadata !"", metadata !1, i32 0, i64 0, i64 0, i32 0, i32 0, i32 0, metadata !4, i32 0, i32 0} ; [ DW_TAG_subroutine_type ]<br>!4 = metadata !{null}<br>!5 = metadata !{i32 590081, metadata !0, metadata !"c", metadata !1, i32 16777221, metadata !6, i32 0} ; [ DW_TAG_arg_variable ]<br>
!6 = metadata !{i32 589860, metadata !2, metadata !"int", null, i32 0, i64 32, i64 32, i64 0, i32 0, i32 5} ; [ DW_TAG_base_type ]<br>!7 = metadata !{i32 5, i32 14, metadata !0, null}<br>!8 = metadata !{i32 7, i32 5, metadata !9, null}<br>
!9 = metadata !{i32 589835, metadata !0, i32 6, i32 1, metadata !1, i32 0} ; [ DW_TAG_lexical_block ]<br>!10 = metadata !{i32 8, i32 5, metadata !9, null}<br>!11 = metadata !{i32 9, i32 9, metadata !9, null}<br>!12 = metadata !{i32 10, i32 1, metadata !9, null}<br>
<br>As we can see 'c' parameter has been optimized and llvm.dbg.value is used to track its DWARF location.<br>Now when I look at DWARF informations for object file using readelf --debug-dump, I've got for 'c' param:<br>
<br>...<br><2><81>: Abbrev Number: 3 (DW_TAG_formal_parameter)<br>    <82>   DW_AT_name        : c    <br>    <84>   DW_AT_decl_file   : 1    <br>    <85>   DW_AT_decl_line   : 5    <br>    <86>   DW_AT_type        : <0x8d>    <br>
    <8a>   DW_AT_location    : 1 byte block: 54     (DW_OP_reg4)<br>...<br><br>Looking at assembly code I've got:<br><br>
...<br>foo:                                    # @foo<br>.Lfunc_begin0:<br>    .loc    1 6 0<br>.Ltmp3:<br># BB#0:<br>    pushl    %ebp<br>.Ltmp0:<br>    movl    %esp, %ebp<br>.Ltmp1:<br>    pushl    %esi<br>    pushl    %eax<br>
.Ltmp2:<br>    #DEBUG_VALUE: foo:c <- ESP+4294967295 # argpass.c:5:14<br>    movl    8(%ebp), %esi<br>    .loc    1 7 5<br>.Ltmp4:<br>    movl    %esi, (%esp)            # argpass.c:7:5<br>    calll    bar                     # argpass.c:7:5<br>
    testl    %esi, %esi<br>    je    .LBB0_2<br># BB#1:<br>    .loc    1 10 1<br>.Ltmp5:<br>    addl    $4, %esp                # argpass.c:10:1<br>.Ltmp6:<br>    #DEBUG_VALUE: foo:c <- ESP+4294967295<br>    popl    %esi                    # argpass.c:10:1<br>
    popl    %ebp                    # argpass.c:10:1<br>    ret                             # argpass.c:10:1<br><br>...<br><br>'c' parameter is indeed stored in location 8(%ebp). <br>If I use a different compiler (PGI C compiler):<br>
pgcc -tp=k8-32 -O2 -g -c argpass.c<br><br>I've got following DWARF information for 'c' parameter:<br><br>...<br> <2><68>: Abbrev Number: 3 (DW_TAG_formal_parameter)<br>    <69>   DW_AT_name        : c    <br>
    <6b>   DW_AT_start_scope : 0x3    <br>    <6f>   DW_AT_type        : <0x77>    <br>    <73>   DW_AT_location    : 2 byte block: 91 8     (DW_OP_fbreg: 8)<br>...<br><br>and value of 'c' can be inspected in GDB. So my questions are:<br>
<br>Is there a way to enforce clang to generate DWARF information similar to pgcc ?<br><br><font><font>For the x86 target, a C argument is passed by value on the stack.<br>
In the generated code, we can refer to the stack location of the<br>
argument as created the caller.  Ideally, for the dwarf DIE of the<br>
argument,  we would like see a DWARF location expression for the<br>
argument which represents its location on the (caller's) stack.<br>
<br>
Is there a method in the metadata to effect this type of location expression?</font></font><br>In other words, is there a way in LLVM to write metadata so that DWARF location would be stack related for x86 ?<br>
<br>Thanks for your answer<br>Best Regards<br>Seb<br><br><br>