← 输入框|github.com|刷新
Skip to content

Create runtime stacktraces #772

Open

Description

@certik

The generated LLVM code needs the ability to create Python stacktraces. So that when bounds check error, or string->int conversion error happens, we print a runtime stacktrace. So we need to get the stack addresses, convert them to LLVM functions, then use LLVM debugging information to find ASR functions that they correspond to, and finally ASR location information (that we need to store in the binary) to find the original Python file and line/column number where the error comes from. Then we can print Python style stacktraces at runtime.

Activity

  1. certik commented on Oct 20, 2022

    @certik
    ContributorAuthor
  2. certik commented on Oct 20, 2022

    @certik
    ContributorAuthor

    To create runtime stacktraces, I would do the following to get started:

  3. certik commented on Oct 20, 2022

    @certik
    ContributorAuthor

    One can test this by:

    int main() {
        int i;
        i = 1;
        i = i + 2;
        i = i + 3;
        i = i + 4;
        return i;
    }

    Compile using:

    clang -emit-llvm -S -g a.c
    

    and LLVM then stores debugging information for each instruction (and function), such as:

    define i32 @main() #0 !dbg !13 {
    ...
      store i32 1, i32* %2, align 4, !dbg !19
      %3 = load i32, i32* %2, align 4, !dbg !20
      %4 = add nsw i32 %3, 2, !dbg !21
      store i32 %4, i32* %2, align 4, !dbg !22
      %5 = load i32, i32* %2, align 4, !dbg !23
      %6 = add nsw i32 %5, 3, !dbg !24
      store i32 %6, i32* %2, align 4, !dbg !25
      %7 = load i32, i32* %2, align 4, !dbg !26
      %8 = add nsw i32 %7, 4, !dbg !27
      store i32 %8, i32* %2, align 4, !dbg !28
      %9 = load i32, i32* %2, align 4, !dbg !29
      ret i32 %9, !dbg !30
    }
    ...
    !13 = distinct !DISubprogram(name: "main", scope: !10, file: !10, line: 2, type: !14, scopeLine: 2, spFlags: DISPFlagDefinition, unit: !9, retainedNodes: !11)
    ...
    !18 = !DILocation(line: 3, column: 9, scope: !13)
    !19 = !DILocation(line: 4, column: 7, scope: !13)
    !20 = !DILocation(line: 5, column: 9, scope: !13)
    !21 = !DILocation(line: 5, column: 11, scope: !13)
    !22 = !DILocation(line: 5, column: 7, scope: !13)
    !23 = !DILocation(line: 6, column: 9, scope: !13)
    ...
  4. Thirumalai-Shaktivel commented on Nov 11, 2022

    @Thirumalai-Shaktivel
    Collaborator

    We should use the ASR node itself to pass information about the name of the array or list that is out of bounds and also store the line/column number with the error message.
    #1093 (comment)

    ..

  5. added
    enhancement
    llvm
    on Nov 12, 2022
  6. Thirumalai-Shaktivel commented on Nov 24, 2022

    @Thirumalai-Shaktivel
    Collaborator

    I get the following error while using dsymutil:

    $ lpython examples/expr2.py -g --debug-with-line-column -o a.out
    $ dsymutil a.out
    error: cannot parse the debug map for 'a.out': The file was not recognized as a valid object file

    Do we have any other alternative for this? Can we fix this error?

  7. Thirumalai-Shaktivel commented on Nov 24, 2022

    @Thirumalai-Shaktivel
    Collaborator

    I get the expected output using this:

    $ llvm-dwarfdump --debug-line a.out
    a.out:	file format elf64-x86-64
    
    .debug_line contents:
    debug_line[0x00000000]
    Line table prologue:
        total_length: 0x00000054
              format: DWARF32
             version: 4
     prologue_length: 0x00000029
     min_inst_length: 1
    max_ops_per_inst: 1
     default_is_stmt: 1
           line_base: -5
          line_range: 14
         opcode_base: 13
    standard_opcode_lengths[DW_LNS_copy] = 0
    standard_opcode_lengths[DW_LNS_advance_pc] = 1
    standard_opcode_lengths[DW_LNS_advance_line] = 1
    standard_opcode_lengths[DW_LNS_set_file] = 1
    standard_opcode_lengths[DW_LNS_set_column] = 1
    standard_opcode_lengths[DW_LNS_negate_stmt] = 0
    standard_opcode_lengths[DW_LNS_set_basic_block] = 0
    standard_opcode_lengths[DW_LNS_const_add_pc] = 0
    standard_opcode_lengths[DW_LNS_fixed_advance_pc] = 1
    standard_opcode_lengths[DW_LNS_set_prologue_end] = 0
    standard_opcode_lengths[DW_LNS_set_epilogue_begin] = 0
    standard_opcode_lengths[DW_LNS_set_isa] = 1
    include_directories[  1] = "examples"
    file_names[  1]:
               name: "expr2.py"
          dir_index: 1
           mod_time: 0x00000000
             length: 0x00000000
    
    Address            Line   Column File   ISA Discriminator Flags
    ------------------ ------ ------ ------ --- ------------- -------------
    0x0000000000001130      0      0      1   0             0  is_stmt
    0x0000000000001131      6      1      1   0             0  is_stmt prologue_end
    0x0000000000001140      0      0      1   0             0  is_stmt
    0x0000000000001140      3      5      1   0             0  is_stmt prologue_end
    0x0000000000001150      0      0      1   0             0  is_stmt
    0x0000000000001151      1      1      1   0             0  is_stmt prologue_end
    0x000000000000115a      1      1      1   0             0  is_stmt end_sequence
  8. Thirumalai-Shaktivel commented on Nov 24, 2022

    @Thirumalai-Shaktivel
    Collaborator

    I think I was able to generate lines.txt and lines.dat?
    How do I move forward with this issue?
    @certik

  9. Thirumalai-Shaktivel commented on Nov 25, 2022

    @Thirumalai-Shaktivel
    Collaborator

    if (WITH_DWARFDUMP)
    add_custom_command(
    TARGET lpython
    POST_BUILD
    COMMAND llvm-dwarfdump --debug-line lpython.dSYM > lpython.dSYM/raw.txt
    )
    add_custom_command(
    TARGET lpython
    POST_BUILD
    COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/dwarf_convert.py lpython.dSYM/raw.txt lpython.dSYM/lines.txt lpython.dSYM/lines.dat
    )
    endif()

    I used the same method as this. There seems to be a problem. lpython.dSYM/lines.dat is empty.

  10. Thirumalai-Shaktivel commented on Nov 25, 2022

    @Thirumalai-Shaktivel
    Collaborator
    $ llvm-dwarfdump --debug-line ./src/bin/lpython > a.txt
    $ ./src/bin/dwarf_convert.py a.txt lines.txt lines.dat 
    $ cat lines.txt 
    1
    /home/thirumalai/Open_Source/lpython/src/bin/lpython.cpp
    0
    $ ls -l lines.dat 
    -rw-rw-r-- 1 thirumalai thirumalai 0 Nov 25 16:31 lines.dat 
    $
    a.txt ./src/bin/lpython: file format elf64-x86-64

    .debug_line contents:
    debug_line[0x00000000]
    Line table prologue:
    total_length: 0x00031e7a
    format: DWARF32
    version: 5
    address_size: 8
    seg_select_size: 0
    prologue_length: 0x00000423
    min_inst_length: 1
    max_ops_per_inst: 1
    default_is_stmt: 1
    line_base: -5
    line_range: 14
    opcode_base: 13
    standard_opcode_lengths[DW_LNS_copy] = 0
    standard_opcode_lengths[DW_LNS_advance_pc] = 1
    standard_opcode_lengths[DW_LNS_advance_line] = 1
    standard_opcode_lengths[DW_LNS_set_file] = 1
    standard_opcode_lengths[DW_LNS_set_column] = 1
    standard_opcode_lengths[DW_LNS_negate_stmt] = 0
    standard_opcode_lengths[DW_LNS_set_basic_block] = 0
    standard_opcode_lengths[DW_LNS_const_add_pc] = 0
    standard_opcode_lengths[DW_LNS_fixed_advance_pc] = 1
    standard_opcode_lengths[DW_LNS_set_prologue_end] = 0
    standard_opcode_lengths[DW_LNS_set_epilogue_begin] = 0
    standard_opcode_lengths[DW_LNS_set_isa] = 1
    include_directories[ 0] = "/home/thirumalai/Open_Source/lpython/src/bin"
    include_directories[ 1] = "/home/thirumalai/Open_Source/lpython/src/bin"
    include_directories[ 2] = "/usr/include/c++/11"
    include_directories[ 3] = "/usr/include/c++/11/bits"
    include_directories[ 4] = "/usr/include/x86_64-linux-gnu/c++/11/bits"
    include_directories[ 5] = "/usr/include/c++/11/ext"
    include_directories[ 6] = "/home/thirumalai/Open_Source/lpython/src/libasr"
    include_directories[ 7] = "/home/thirumalai/Open_Source/lpython/src/libasr/pass"
    include_directories[ 8] = "/home/thirumalai/Open_Source/lpython/src/bin/tpl/cpp-terminal"
    include_directories[ 9] = "/usr/include/c++/11/debug"
    include_directories[ 10] = "/usr/include/x86_64-linux-gnu/bits"
    include_directories[ 11] = "/usr/include"
    include_directories[ 12] = "/usr/lib/gcc/x86_64-linux-gnu/11/include"
    include_directories[ 13] = "/usr/include/x86_64-linux-gnu/bits/types"
    include_directories[ 14] = "/usr/include/x86_64-linux-gnu/sys"
    include_directories[ 15] = "/usr/include/c++/11/pstl"
    include_directories[ 16] = "/home/thirumalai/Open_Source/lpython/src/lpython"
    include_directories[ 17] = "/home/thirumalai/Open_Source/lpython/src/lpython/semantics"
    include_directories[ 18] = "/home/thirumalai/Open_Source/lpython/src/libasr/codegen"
    include_directories[ 19] = "/home/thirumalai/Open_Source/lpython/src/lpython/parser"
    file_names[ 0]:
    name: "lpython.cpp"
    dir_index: 1
    file_names[ 1]:
    name: "limits"
    dir_index: 2
    file_names[ 2]:
    name: "chrono"
    dir_index: 2
    file_names[ 3]:
    name: "exception.h"
    dir_index: 3
    file_names[ 4]:
    name: "new"
    dir_index: 2
    file_names[ 5]:
    name: "stl_algobase.h"
    dir_index: 3
    file_names[ 6]:
    name: "char_traits.h"
    dir_index: 3
    file_names[ 7]:
    name: "basic_string.h"
    dir_index: 3
    file_names[ 8]:
    name: "charconv.h"
    dir_index: 3
    ...
    file_names[188]:
    name: "unistd.h"
    dir_index: 11
    file_names[189]:
    name: "system_error"
    dir_index: 2

    Address Line Column File ISA Discriminator Flags


    0x0000000000809ef7 1065 7 1 0 0 is_stmt
    0x0000000000809eff 1065 59 1 0 0 is_stmt
    0x0000000000809f04 1065 62 1 0 0 is_stmt
    0x0000000000809f06 1065 62 1 0 0 is_stmt end_sequence
    0x0000000000809f06 1068 7 1 0 0 is_stmt
    0x0000000000809f0e 1068 44 1 0 0 is_stmt
    0x0000000000809f13 1068 57 1 0 0 is_stmt
    0x0000000000809f15 1068 57 1 0 0 is_stmt end_sequence
    0x0000000000809f15 1748 7 1 0 0 is_stmt
    0x0000000000809f1d 1748 44 1 0 0 is_stmt
    0x0000000000809f2a 1748 57 1 0 0 is_stmt
    0x0000000000809f31 1748 57 1 0 0 is_stmt end_sequence
    0x0000000000809f31 1751 7 1 0 0 is_stmt
    0x0000000000809f39 1751 44 1 0 0 is_stmt
    0x0000000000809f46 1751 57 1 0 0 is_stmt
    0x0000000000809f4d 1751 57 1 0 0 is_stmt end_sequence
    0x0000000000809f4e 537 2 2 0 0 is_stmt
    0x0000000000809f5a 538 11 2 0 0 is_stmt
    0x0000000000809f61 538 16 2 0 0 is_stmt
    0x0000000000809f63 538 16 2 0 0 is_stmt end_sequence
    0x0000000000809f64 521 23 2 0 0 is_stmt
    0x0000000000809f74 522 10 2 0 0 is_stmt
    0x0000000000809f7b 522 6 2 0 0 is_stmt
    0x0000000000809f82 522 37 2 0 0 is_stmt
    0x0000000000809f85 522 37 2 0 0 is_stmt end_sequence
    0x0000000000809f85 432 2 2 0 0 is_stmt
    0x0000000000809f8d 433 17 2 0 0 is_stmt
    0x0000000000809f92 433 20 2 0 0 is_stmt
    0x0000000000809f94 433 20 2 0 0 is_stmt end_sequence
    0x0000000000809f94 521 23 2 0 0 is_stmt
    0x0000000000809fa4 522 10 2 0 0 is_stmt
    0x0000000000809fab 522 6 2 0 0 is_stmt
    0x0000000000809fb2 522 37 2 0 0 is_stmt
    0x0000000000809fb5 522 37 2 0 0 is_stmt end_sequence
    0x0000000000809fb6 64 5 3 0 0 is_stmt
    0x0000000000809fc2 64 34 3 0 0 is_stmt
    0x0000000000809fd0 64 36 3 0 0 is_stmt
    0x0000000000809fd3 64 36 3 0 0 is_stmt end_sequence
    0x0000000000809fd3 175 1 4 0 0 is_stmt
    0x0000000000809fe3 175 10 4 0 0 is_stmt
    0x0000000000809fe7 175 15 4 0 0 is_stmt
    0x0000000000809fe9 175 15 4 0 0 is_stmt end_sequence
    0x0000000000809fe9 180 67 4 0 0 is_stmt
    0x0000000000809ff9 180 69 4 0 0 is_stmt
    0x0000000000809ffc 180 69 4 0 0 is_stmt end_sequence
    0x0000000000809ffc 1010 40 5 0 0 is_stmt
    0x000000000080a008 1010 49 5 0 0 is_stmt
    0x000000000080a00c 1010 54 5 0 0 is_stmt
    0x000000000080a00e 1010 54 5 0 0 is_stmt end_sequence
    0x000000000080a00e 364 7 6 0 0 is_stmt
    0x000000000080a01e 367 37 6 0 0 is_stmt
    0x000000000080a025 367 10 6 0 0 is_stmt
    0x000000000080a027 368 32 6 0 0 is_stmt
    0x000000000080a02e 368 37 6 0 0 is_stmt
    0x000000000080a033 369 7 6 0 0 is_stmt
    0x000000000080a035 369 7 6 0 0 is_stmt end_sequence
    0x000000000080a035 372 7 6 0 0 is_stmt
    0x000000000080a04d 374 2 6 0 0 is_stmt
    0x000000000080a054 375 11 6 0 0 is_stmt
    0x000000000080a05e 379 6 6 0 1 is_stmt
    0x000000000080a063 377 2 6 0 1 is_stmt
    0x000000000080a067 381 18 6 0 0 is_stmt
    0x000000000080a06f 381 6 6 0 0 is_stmt
    0x000000000080a071 382 34 6 0 0 is_stmt
    0x000000000080a07c 382 23 6 0 0 is_stmt
    0x000000000080a087 382 14 6 0 0 is_stmt
    0x000000000080a092 382 8 6 0 0 is_stmt
    0x000000000080a096 383 11 6 0 0 is_stmt
    0x000000000080a09d 384 39 6 0 0 is_stmt
    0x000000000080a0a8 384 28 6 0 0 is_stmt
    0x000000000080a0b3 384 19 6 0 0 is_stmt
    0x000000000080a0be 384 13 6 0 0 is_stmt
    0x000000000080a0c2 385 10 6 0 0 is_stmt
    0x000000000080a0c9 381 6 6 0 2 is_stmt
    0x000000000080a0ce 381 31 6 0 1 is_stmt
    0x000000000080a0d8 386 13 6 0 0 is_stmt
    0x000000000080a0df 389 25 6 0 0 is_stmt
    0x000000000080a0f6 389 41 6 0 0 is_stmt
    0x000000000080a0f7 390 7 6 0 0 is_stmt
    0x000000000080a0f9 390 7 6 0 0 is_stmt end_sequence
    ...

  11. certik commented on Nov 25, 2022

    @certik
    ContributorAuthor

    Are you trying to get these files for LPython? CMake already does it correctly, so you can just do what it does. Run dsymtil, llvm-dwarfdump, dwarf_convert.py, in the right directory, etc. However all of this is already done and it works.

    What we need to do is something similar for binaries that we create.

  12. Thirumalai-Shaktivel commented on Nov 26, 2022

    @Thirumalai-Shaktivel
    Collaborator

    I got it, Thank you.
    I think I got confused or understood the above comments wrong.
    Now, I know what to do.

  13. Thirumalai-Shaktivel commented on Sep 28, 2023

    @Thirumalai-Shaktivel
    Collaborator

    Related PR:
    #1240
    #1344
    #1426

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancementllvm

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions