From c9cde28a815b5b617771f7f11cc74f9a6fd6ac5c Mon Sep 17 00:00:00 2001 From: Thirumalai Shaktivel Date: Fri, 29 Sep 2023 09:24:58 +0530 Subject: [PATCH 1/3] Refactor: Move dat_convert and dwarf_convert to Libasr --- src/bin/CMakeLists.txt | 2 +- src/bin/lpython.cpp | 4 ++-- src/{bin => libasr}/dat_convert.py | 0 src/{bin => libasr}/dwarf_convert.py | 0 4 files changed, 3 insertions(+), 3 deletions(-) rename src/{bin => libasr}/dat_convert.py (100%) rename src/{bin => libasr}/dwarf_convert.py (100%) diff --git a/src/bin/CMakeLists.txt b/src/bin/CMakeLists.txt index 6d51014d56..72a1a1a11c 100644 --- a/src/bin/CMakeLists.txt +++ b/src/bin/CMakeLists.txt @@ -33,7 +33,7 @@ if (WITH_STACKTRACE AND APPLE AND CMAKE_CXX_COMPILER_ID MATCHES Clang) 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 + COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../libasr/dwarf_convert.py lpython.dSYM/raw.txt lpython.dSYM/lines.txt lpython.dSYM/lines.dat ) endif() endif() diff --git a/src/bin/lpython.cpp b/src/bin/lpython.cpp index f9b3176d5b..a6c3f45a4d 100644 --- a/src/bin/lpython.cpp +++ b/src/bin/lpython.cpp @@ -1901,14 +1901,14 @@ int main(int argc, char *argv[]) #else cmd += "llvm-dwarfdump --debug-line " + basename + ".out > "; #endif - cmd += basename + "_ldd.txt && (cd src/bin; ./dwarf_convert.py ../../" + cmd += basename + "_ldd.txt && (cd src/libasr; ./dwarf_convert.py ../../" + basename + "_ldd.txt ../../" + basename + "_lines.txt ../../" + basename + "_lines.dat && ./dat_convert.py ../../" + basename + "_lines.dat)"; int status = system(cmd.c_str()); if ( status != 0 ) { std::cerr << "Error in creating the files used to generate " - "the debug information. This might be caused because either" + "the debug information. This might be caused because either " "`llvm-dwarfdump` or `Python` are not available. " "Please activate the CONDA environment and compile again.\n"; return status; diff --git a/src/bin/dat_convert.py b/src/libasr/dat_convert.py similarity index 100% rename from src/bin/dat_convert.py rename to src/libasr/dat_convert.py diff --git a/src/bin/dwarf_convert.py b/src/libasr/dwarf_convert.py similarity index 100% rename from src/bin/dwarf_convert.py rename to src/libasr/dwarf_convert.py From 05025b0ef868430526f3e4afb02b90a797ee3668 Mon Sep 17 00:00:00 2001 From: Thirumalai Shaktivel Date: Fri, 29 Sep 2023 22:36:12 +0530 Subject: [PATCH 2/3] Refactor: Show `main_program` location in the stacktrace --- src/libasr/runtime/lfortran_intrinsics.c | 6 +++--- tests/reference/run_dbg-test_assert_01-2f34744.json | 2 +- tests/reference/run_dbg-test_assert_01-2f34744.stderr | 2 ++ tests/reference/run_dbg-test_assert_02-c6de25a.json | 2 +- tests/reference/run_dbg-test_assert_02-c6de25a.stderr | 2 ++ tests/reference/run_dbg-test_assert_03-bd7b7dd.json | 2 +- tests/reference/run_dbg-test_assert_03-bd7b7dd.stderr | 2 ++ tests/reference/run_dbg-test_quit_01-30889cc.json | 2 +- tests/reference/run_dbg-test_quit_01-30889cc.stderr | 2 ++ tests/reference/run_dbg-test_raise_01-dfd86ca.json | 2 +- tests/reference/run_dbg-test_raise_01-dfd86ca.stderr | 2 ++ 11 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/libasr/runtime/lfortran_intrinsics.c b/src/libasr/runtime/lfortran_intrinsics.c index 1d884d1345..6548592d5d 100644 --- a/src/libasr/runtime/lfortran_intrinsics.c +++ b/src/libasr/runtime/lfortran_intrinsics.c @@ -372,7 +372,7 @@ char** parse_fortran_format(char* format, int *count) { switch (tolower(format[index])) { case ',' : break; - case '/' : + case '/' : format_values_2[format_values_count++] = "/"; break; case '"' : @@ -2619,9 +2619,9 @@ LFORTRAN_API void print_stacktrace_addresses(char *filename, bool use_colors) { get_local_info_dwarfdump(&d); #ifdef HAVE_LFORTRAN_MACHO - for (int32_t i = d.local_pc_size-2; i >= 0; i--) { + for (int32_t i = d.local_pc_size-1; i >= 0; i--) { #else - for (int32_t i = d.local_pc_size-3; i >= 0; i--) { + for (int32_t i = d.local_pc_size-2; i >= 0; i--) { #endif uint64_t index = bisection(d.addresses, d.stack_size, d.local_pc[i]); if(use_colors) { diff --git a/tests/reference/run_dbg-test_assert_01-2f34744.json b/tests/reference/run_dbg-test_assert_01-2f34744.json index 127eb3e44f..f8ac5ccbe9 100644 --- a/tests/reference/run_dbg-test_assert_01-2f34744.json +++ b/tests/reference/run_dbg-test_assert_01-2f34744.json @@ -8,6 +8,6 @@ "stdout": null, "stdout_hash": null, "stderr": "run_dbg-test_assert_01-2f34744.stderr", - "stderr_hash": "5ded88da4106fc9a3cfbaad6f82cc820a79a6ef8cc1661ecfcb37924", + "stderr_hash": "4811af471c73572b285e9ea01c8689abdd3cb32c717b3cd4876d2669", "returncode": 1 } \ No newline at end of file diff --git a/tests/reference/run_dbg-test_assert_01-2f34744.stderr b/tests/reference/run_dbg-test_assert_01-2f34744.stderr index c561678d44..a7dc14d623 100644 --- a/tests/reference/run_dbg-test_assert_01-2f34744.stderr +++ b/tests/reference/run_dbg-test_assert_01-2f34744.stderr @@ -1,3 +1,5 @@ + File "tests/runtime_errors/test_assert_01.py", line 1 + def test(): File "tests/runtime_errors/test_assert_01.py", line 4 test() File "tests/runtime_errors/test_assert_01.py", line 2 diff --git a/tests/reference/run_dbg-test_assert_02-c6de25a.json b/tests/reference/run_dbg-test_assert_02-c6de25a.json index 25aa716a99..19b16179f5 100644 --- a/tests/reference/run_dbg-test_assert_02-c6de25a.json +++ b/tests/reference/run_dbg-test_assert_02-c6de25a.json @@ -8,6 +8,6 @@ "stdout": null, "stdout_hash": null, "stderr": "run_dbg-test_assert_02-c6de25a.stderr", - "stderr_hash": "ddba8a92bcfd5a30016735589da0dc56f2785e7636afcc0edeca4139", + "stderr_hash": "d5bfce55992e8d0630849442ee1f9b32864c64c328917c29fafc9424", "returncode": 1 } \ No newline at end of file diff --git a/tests/reference/run_dbg-test_assert_02-c6de25a.stderr b/tests/reference/run_dbg-test_assert_02-c6de25a.stderr index 4fe6972010..0b837dc18e 100644 --- a/tests/reference/run_dbg-test_assert_02-c6de25a.stderr +++ b/tests/reference/run_dbg-test_assert_02-c6de25a.stderr @@ -1,3 +1,5 @@ + File "tests/runtime_errors/test_assert_02.py", line 1 + def test(): File "tests/runtime_errors/test_assert_02.py", line 4 test() File "tests/runtime_errors/test_assert_02.py", line 2 diff --git a/tests/reference/run_dbg-test_assert_03-bd7b7dd.json b/tests/reference/run_dbg-test_assert_03-bd7b7dd.json index c05b63780b..f771107475 100644 --- a/tests/reference/run_dbg-test_assert_03-bd7b7dd.json +++ b/tests/reference/run_dbg-test_assert_03-bd7b7dd.json @@ -8,6 +8,6 @@ "stdout": null, "stdout_hash": null, "stderr": "run_dbg-test_assert_03-bd7b7dd.stderr", - "stderr_hash": "7f97899439260443b40867e81d7c481c2fc23ec84ee777e7b43984d8", + "stderr_hash": "cae7dd955478787917e9dbb0bc1f63631317b13da1d892c3ebab9097", "returncode": 1 } \ No newline at end of file diff --git a/tests/reference/run_dbg-test_assert_03-bd7b7dd.stderr b/tests/reference/run_dbg-test_assert_03-bd7b7dd.stderr index 8ee39fc2e8..65f2444ba9 100644 --- a/tests/reference/run_dbg-test_assert_03-bd7b7dd.stderr +++ b/tests/reference/run_dbg-test_assert_03-bd7b7dd.stderr @@ -1,3 +1,5 @@ + File "tests/runtime_errors/test_assert_03.py", line 1 + def f(): File "tests/runtime_errors/test_assert_03.py", line 10 main() File "tests/runtime_errors/test_assert_03.py", line 8 diff --git a/tests/reference/run_dbg-test_quit_01-30889cc.json b/tests/reference/run_dbg-test_quit_01-30889cc.json index 4e50fcce5f..d46739a952 100644 --- a/tests/reference/run_dbg-test_quit_01-30889cc.json +++ b/tests/reference/run_dbg-test_quit_01-30889cc.json @@ -8,6 +8,6 @@ "stdout": null, "stdout_hash": null, "stderr": "run_dbg-test_quit_01-30889cc.stderr", - "stderr_hash": "f5a660003a2da017d3ced437825a1e6f1c0c046d73cf68d183c92a40", + "stderr_hash": "b3dac87462f9f0650e5a6af68791137ca9d29f9a64139ba7718a9f96", "returncode": 10 } \ No newline at end of file diff --git a/tests/reference/run_dbg-test_quit_01-30889cc.stderr b/tests/reference/run_dbg-test_quit_01-30889cc.stderr index c7e212253e..c1c7a54e60 100644 --- a/tests/reference/run_dbg-test_quit_01-30889cc.stderr +++ b/tests/reference/run_dbg-test_quit_01-30889cc.stderr @@ -1,3 +1,5 @@ + File "tests/runtime_errors/test_quit_01.py", line 1 + def test(): File "tests/runtime_errors/test_quit_01.py", line 4 test() File "tests/runtime_errors/test_quit_01.py", line 2 diff --git a/tests/reference/run_dbg-test_raise_01-dfd86ca.json b/tests/reference/run_dbg-test_raise_01-dfd86ca.json index 199a810c97..2dbe004c7e 100644 --- a/tests/reference/run_dbg-test_raise_01-dfd86ca.json +++ b/tests/reference/run_dbg-test_raise_01-dfd86ca.json @@ -8,6 +8,6 @@ "stdout": null, "stdout_hash": null, "stderr": "run_dbg-test_raise_01-dfd86ca.stderr", - "stderr_hash": "073aae20bbe7cf78e116825e3e825365b07230972ff7bcb3a5dddb93", + "stderr_hash": "76085f527077a81ba6457af8f982a497038168f555ab4027d0d6340e", "returncode": 1 } \ No newline at end of file diff --git a/tests/reference/run_dbg-test_raise_01-dfd86ca.stderr b/tests/reference/run_dbg-test_raise_01-dfd86ca.stderr index 9c5a4dafd0..42bc707825 100644 --- a/tests/reference/run_dbg-test_raise_01-dfd86ca.stderr +++ b/tests/reference/run_dbg-test_raise_01-dfd86ca.stderr @@ -1,3 +1,5 @@ + File "tests/runtime_errors/test_raise_01.py", line 1 + def test(): File "tests/runtime_errors/test_raise_01.py", line 4 test() File "tests/runtime_errors/test_raise_01.py", line 2 From 10a304413d9b258b9e9ae40c6a1806a9570b5277 Mon Sep 17 00:00:00 2001 From: Thirumalai Shaktivel Date: Fri, 29 Sep 2023 22:48:16 +0530 Subject: [PATCH 3/3] Refactor: Throw an error instead of warning in Runtime Stacktrace handling --- src/bin/lpython.cpp | 43 +++++++++----------------------- src/lpython/python_evaluator.cpp | 13 ++++++++++ 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/src/bin/lpython.cpp b/src/bin/lpython.cpp index a6c3f45a4d..bdc2b92aaf 100644 --- a/src/bin/lpython.cpp +++ b/src/bin/lpython.cpp @@ -736,17 +736,6 @@ int emit_llvm(const std::string &infile, LCompilers::PythonCompiler fe(compiler_options); LCompilers::Result> res = fe.get_llvm3(*asr, pass_manager, diagnostics, infile); - if (compiler_options.emit_debug_info) { - if (!compiler_options.emit_debug_line_column) { - diagnostics.add(LCompilers::diag::Diagnostic( - "The `emit_debug_line_column` is not enabled; please use the " - "`--debug-with-line-column` option to get the correct " - "location information", - LCompilers::diag::Level::Warning, - LCompilers::diag::Stage::Semantic, {}) - ); - } - } std::cerr << diagnostics.render(lm, compiler_options); if (!res.ok) { LCOMPILERS_ASSERT(diagnostics.has_error()) @@ -819,6 +808,18 @@ int compile_python_to_object_file( diagnostics.diagnostics.clear(); // ASR -> LLVM + if (compiler_options.emit_debug_info) { +#ifndef HAVE_RUNTIME_STACKTRACE + diagnostics.add(LCompilers::diag::Diagnostic( + "The `runtime stacktrace` is not enabled. To get the stacktraces, " + "re-build LPython with `-DWITH_RUNTIME_STACKTRACE=yes`", + LCompilers::diag::Level::Error, + LCompilers::diag::Stage::Semantic, {}) + ); + std::cerr << diagnostics.render(lm, compiler_options); + return 1; +#endif + } LCompilers::PythonCompiler fe(compiler_options); LCompilers::LLVMEvaluator e(compiler_options.target); std::unique_ptr m; @@ -828,26 +829,6 @@ int compile_python_to_object_file( auto asr_to_llvm_end = std::chrono::high_resolution_clock::now(); times.push_back(std::make_pair("ASR to LLVM", std::chrono::duration(asr_to_llvm_end - asr_to_llvm_start).count())); - if (compiler_options.emit_debug_info) { -#ifdef HAVE_RUNTIME_STACKTRACE - if (!compiler_options.emit_debug_line_column) { - diagnostics.add(LCompilers::diag::Diagnostic( - "The `emit_debug_line_column` is not enabled; please use the " - "`--debug-with-line-column` option to get the correct " - "location information", - LCompilers::diag::Level::Warning, - LCompilers::diag::Stage::Semantic, {}) - ); - } -#else - diagnostics.add(LCompilers::diag::Diagnostic( - "The `runtime stacktrace` is not enabled. To get the stacktraces, " - "re-build LPython with `-DWITH_RUNTIME_STACKTRACE=yes`", - LCompilers::diag::Level::Warning, - LCompilers::diag::Stage::Semantic, {}) - ); -#endif - } std::cerr << diagnostics.render(lm, compiler_options); if (!res.ok) { LCOMPILERS_ASSERT(diagnostics.has_error()) diff --git a/src/lpython/python_evaluator.cpp b/src/lpython/python_evaluator.cpp index 2f76f5793d..c38fb92fea 100644 --- a/src/lpython/python_evaluator.cpp +++ b/src/lpython/python_evaluator.cpp @@ -50,6 +50,19 @@ Result> PythonCompiler::get_llvm3( eval_count++; run_fn = "__lfortran_evaluate_" + std::to_string(eval_count); + if (compiler_options.emit_debug_info) { + if (!compiler_options.emit_debug_line_column) { + diagnostics.add(LCompilers::diag::Diagnostic( + "The `emit_debug_line_column` is not enabled; please use the " + "`--debug-with-line-column` option to get the correct " + "location information", + LCompilers::diag::Level::Error, + LCompilers::diag::Stage::Semantic, {}) + ); + Error err; + return err; + } + } // ASR -> LLVM std::unique_ptr m; Result> res