Skip to content

Commit 066ea44

Browse files
Generate the runtime_stacktrace related files during the compile-time
1 parent d7aa430 commit 066ea44

File tree

2 files changed

+48
-58
lines changed

2 files changed

+48
-58
lines changed

src/bin/lpython.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,29 @@ int main(int argc, char *argv[])
16381638
err = link_executable({tmp_o}, outfile, runtime_library_dir,
16391639
backend, static_link, true, compiler_options);
16401640
if (err != 0) return err;
1641+
1642+
if (compiler_options.emit_debug_info) {
1643+
// TODO: Replace the following hardcoded part
1644+
std::string cmd = "";
1645+
#ifdef HAVE_LFORTRAN_MACHO
1646+
cmd += "dsymutil " + basename + ".out && llvm-dwarfdump --debug-line "
1647+
+ basename + ".out.dSYM > ";
1648+
#else
1649+
cmd += "llvm-dwarfdump --debug-line " + basename + ".out > ";
1650+
#endif
1651+
cmd += basename + "_ldd.txt && (cd src/bin; ./dwarf_convert.py ../../"
1652+
+ basename + "_ldd.txt ../../" + basename + "_lines.txt ../../"
1653+
+ basename + "_lines.dat && ./dat_convert.py ../../"
1654+
+ basename + "_lines.dat)";
1655+
int status = system(cmd.c_str());
1656+
if ( status != 0 ) {
1657+
std::cerr << "Error in creating the files used to generate "
1658+
"the debug information. This might be caused because either"
1659+
"`llvm-dwarfdump` or `Python` are not available. "
1660+
"Please activate the CONDA environment and compile again.\n";
1661+
return status;
1662+
}
1663+
}
16411664
#else
16421665
std::cerr << "Compiling Python files to object files requires the LLVM backend to be enabled. Recompile with `WITH_LLVM=yes`." << std::endl;
16431666
return 1;

src/libasr/runtime/lfortran_intrinsics.c

Lines changed: 25 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <limits.h>
1111
#include <ctype.h>
1212

13-
#include <libasr/runtime/lfortran_intrinsics.h>w
13+
#include <libasr/runtime/lfortran_intrinsics.h>
1414
#include <libasr/config.h>
1515

1616
#ifdef HAVE_RUNTIME_STACKTRACE
@@ -1501,78 +1501,45 @@ char *remove_whitespace(char *str) {
15011501
return str;
15021502
}
15031503

1504-
int generate_stacktrace_files() {
1505-
// TODO: Replace the hardcoded part
1506-
char *base_name = get_base_name(source_filename);
1507-
char *cmd = malloc(strlen(base_name)*7 + 200);
1508-
#ifdef HAVE_LFORTRAN_MACHO
1509-
strcpy(cmd, "dsymutil ");
1510-
strcat(cmd, base_name);
1511-
strcat(cmd, ".out && llvm-dwarfdump --debug-line ");
1512-
strcat(cmd, base_name);
1513-
strcat(cmd, ".out.dSYM > ");
1514-
#else
1515-
strcpy(cmd, "llvm-dwarfdump --debug-line ");
1516-
strcat(cmd, base_name);
1517-
strcat(cmd, ".out > ");
1518-
#endif
1519-
strcat(cmd, base_name);
1520-
strcat(cmd, "_ldd.txt && (cd src/bin; ./dwarf_convert.py ../../");
1521-
strcat(cmd, base_name);
1522-
strcat(cmd, "_ldd.txt ../../");
1523-
strcat(cmd, base_name);
1524-
strcat(cmd, "_lines.txt ../../");
1525-
strcat(cmd, base_name);
1526-
strcat(cmd, "_lines.dat && ./dat_convert.py ../../");
1527-
strcat(cmd, base_name);
1528-
strcat(cmd, "_lines.dat)");
1529-
int status = system(cmd);
1530-
free(cmd);
1531-
return status;
1532-
}
1533-
15341504
LFORTRAN_API void print_stacktrace_addresses(char *filename, bool use_colors) {
15351505
source_filename = filename;
1536-
int status = generate_stacktrace_files();
1537-
if (status == 0) {
1538-
struct Stacktrace d = get_stacktrace_addresses();
1539-
get_local_address(&d);
1540-
get_local_info_dwarfdump(&d);
1506+
struct Stacktrace d = get_stacktrace_addresses();
1507+
get_local_address(&d);
1508+
get_local_info_dwarfdump(&d);
15411509

15421510
#ifdef HAVE_LFORTRAN_MACHO
1543-
for (int32_t i = d.local_pc_size-2; i > 1; i--) {
1511+
for (int32_t i = d.local_pc_size-2; i > 1; i--) {
15441512
#else
1545-
for (int32_t i = d.local_pc_size-3; i >= 0; i--) {
1513+
for (int32_t i = d.local_pc_size-3; i >= 0; i--) {
15461514
#endif
1547-
uint64_t index = bisection(d.addresses, d.stack_size, d.local_pc[i]);
1548-
if(use_colors) {
1549-
fprintf(stderr, DIM " File " S_RESET
1550-
BOLD MAGENTA "\"%s\"" C_RESET S_RESET
1515+
uint64_t index = bisection(d.addresses, d.stack_size, d.local_pc[i]);
1516+
if(use_colors) {
1517+
fprintf(stderr, DIM " File " S_RESET
1518+
BOLD MAGENTA "\"%s\"" C_RESET S_RESET
15511519
#ifdef HAVE_LFORTRAN_MACHO
1552-
DIM ", line %lld\n" S_RESET
1520+
DIM ", line %lld\n" S_RESET
15531521
#else
1554-
DIM ", line %ld\n" S_RESET
1522+
DIM ", line %ld\n" S_RESET
15551523
#endif
1556-
" %s\n", source_filename, d.line_numbers[index],
1557-
remove_whitespace(read_line_from_file(source_filename,
1558-
d.line_numbers[index])));
1559-
} else {
1560-
fprintf(stderr, " File \"%s\", "
1524+
" %s\n", source_filename, d.line_numbers[index],
1525+
remove_whitespace(read_line_from_file(source_filename,
1526+
d.line_numbers[index])));
1527+
} else {
1528+
fprintf(stderr, " File \"%s\", "
15611529
#ifdef HAVE_LFORTRAN_MACHO
1562-
"line %lld\n %s\n",
1530+
"line %lld\n %s\n",
15631531
#else
1564-
"line %ld\n %s\n",
1532+
"line %ld\n %s\n",
15651533
#endif
1566-
source_filename, d.line_numbers[index],
1567-
remove_whitespace(read_line_from_file(source_filename,
1568-
d.line_numbers[index])));
1569-
}
1570-
#ifdef HAVE_LFORTRAN_MACHO
1534+
source_filename, d.line_numbers[index],
1535+
remove_whitespace(read_line_from_file(source_filename,
1536+
d.line_numbers[index])));
15711537
}
1538+
#ifdef HAVE_LFORTRAN_MACHO
1539+
}
15721540
#else
1573-
}
1574-
#endif
15751541
}
1542+
#endif
15761543
}
15771544

15781545
#endif // HAVE_RUNTIME_STACKTRACE

0 commit comments

Comments
 (0)