Skip to content

Commit

Permalink
Fixes to native backtrace.
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Nov 12, 2023
1 parent dc0aa35 commit bca1e5b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 33 deletions.
33 changes: 9 additions & 24 deletions lib/std/core/builtin.c3
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,16 @@ struct CallstackElement
uint line;
}

fn bool print_backtrace(String message, int backtraces_to_ignore) @if(env::DARWIN || env::LINUX)
fn bool print_backtrace(String message, int backtraces_to_ignore) @if(env::NATIVE_STACKTRACE)
{
@pool()
{
BacktraceList! backtrace = backtrace::backtrace_load(mem::temp());
io::eprintn("A");
if (catch backtrace) return false;
io::eprintfn("B %d %d", backtraces_to_ignore, backtrace.len());
if (backtrace.len() <= backtraces_to_ignore) return false;
io::eprintn("C");
io::eprint("\nERROR: '");
io::eprint(message);
io::eprintn("'");
Expand All @@ -115,7 +118,7 @@ fn bool print_backtrace(String message, int backtraces_to_ignore) @if(env::DARWI
return true;
};
}
fn void default_panic(String message, String file, String function, uint line) @if(env::DARWIN)
fn void default_panic(String message, String file, String function, uint line) @if(env::NATIVE_STACKTRACE)
{
$if $defined(io::stderr):
if (!print_backtrace(message, 2))
Expand All @@ -127,7 +130,7 @@ fn void default_panic(String message, String file, String function, uint line) @
$$trap();

}
fn void default_panic(String message, String file, String function, uint line) @if(!env::DARWIN)
fn void default_panic(String message, String file, String function, uint line) @if(!env::NATIVE_STACKTRACE)
{
CallstackElement* stack = $$stacktrace();
$if $defined(io::stderr):
Expand Down Expand Up @@ -367,35 +370,17 @@ macro uint void*.hash(void* ptr) => ((ulong)(uptr)ptr).hash();
module std::core::builtin @if((env::LINUX || env::DARWIN) && env::COMPILER_SAFE_MODE && env::DEBUG_SYMBOLS);
import libc;

fn void sig_panic(String message) @if(env::DARWIN)
fn void sig_panic(String message)
{
default_panic(message, "???", "???", 0);
}

fn void sig_panic(String message) @if(!env::DARWIN)
{
$if $defined(io::stderr) && $defined(File.printf):
CallstackElement* stack = $$stacktrace();
if (stack) stack = stack.prev;
if (stack) stack = stack.prev;
(void)io::stderr().print("\nSYSTEM ERROR: '");
(void)io::stderr().print(message);
(void)io::stderr().printn("'");
while (stack)
{
(void)io::stderr().printfn(" in %s %s (%s:%s)", stack.location.name, stack.function, stack.file, stack.line);
if (stack == stack.prev) break;
stack = stack.prev;
}
$endif
}

SignalFunction old_bus_error;
SignalFunction old_segmentation_fault;

fn void sig_bus_error(CInt i)
{
$if !env::DARWIN:
$if !env::NATIVE_STACKTRACE:
sig_panic("Illegal memory access.");
$else
$if $defined(io::stderr):
Expand All @@ -410,7 +395,7 @@ fn void sig_bus_error(CInt i)

fn void sig_segmentation_fault(CInt i)
{
$if !env::DARWIN:
$if !env::NATIVE_STACKTRACE:
sig_panic("Out of bounds memory access.");
$else
$if $defined(io::stderr):
Expand Down
2 changes: 1 addition & 1 deletion lib/std/core/env.c3
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const MemoryEnvironment MEMORY_ENV = (MemoryEnvironment)$$MEMORY_ENVIRONMENT;
const bool TRACK_MEMORY = DEBUG_SYMBOLS && (COMPILER_SAFE_MODE || TESTING);
const bool X86_64 = ARCH_TYPE == X86_64;
const bool AARCH64 = ARCH_TYPE == AARCH64;

const bool NATIVE_STACKTRACE = LINUX || DARWIN;
const bool LINUX = LIBC && OS_TYPE == LINUX;
const bool DARWIN = LIBC && os_is_darwin();
const bool WIN32 = LIBC && OS_TYPE == WIN32;
Expand Down
10 changes: 6 additions & 4 deletions lib/std/os/linux/linux.c3
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,16 @@ fn Backtrace! backtrace_load_element(void* addr, Allocator* allocator = mem::hea
if (!addr) return backtrace::BACKTRACE_UNKNOWN;
char[] buf = mem::temp_array(char, 1024);
Linux_Dl_info info;
if (dladdr(addr, &info) == 0) return backtrace::BACKTRACE_UNKNOWN;
if (dladdr(addr, &info) == 0) return backtrace::BACKTRACE_UNKNOWN;
void* obj_address = addr - (uptr)info.dli_fbase + (uptr)elf_module_image_base(info.dli_fname.str_view())!;
ZString obj_path = info.dli_fname;
String s = process::execute_stdout_to_buffer(buf, { "addr2line", "-p", "-i", "-C", "-f", "-e", obj_path.str_view(), string::tformat("0x%x", obj_address) })!;
ZString sname = info.dli_sname ? info.dli_sname : (ZString)"???";
String s = process::execute_stdout_to_buffer(buf, { "addr2line", "-p", "-C", "-f", "-e", obj_path.str_view(), string::tformat("0x%x", obj_address) })!;
String[] parts = s.tsplit(" at ");
if (parts.len != 2)
{
return {
.function = info.dli_sname ? info.dli_sname.copy(allocator) : "???".copy(allocator),
.function = sname.copy(allocator),
.object_file = info.dli_fname.copy(allocator),
.offset = (uptr)addr,
.file = "".copy(allocator),
Expand Down Expand Up @@ -189,9 +190,10 @@ fn BacktraceList! backtrace_load(Allocator* allocator)
}
@pool(allocator)
{
for (usz i = 1; i < size; i++)
for (usz i = 0; i < size; i++)
{
Backtrace trace = backtrace_load_element(bt_buffer[i], allocator)!;
io::printn(trace);
list.append(trace);
}
};
Expand Down
18 changes: 15 additions & 3 deletions resources/linux_stack.c3
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@ import std::io;
import std::collections::map;
import std::os;

fn void! main()
def Foo = fn void();

fn void test2()
{
builtin::default_panic("FEHifej", "file", "fun", 123);
}

fn void test1()
{
test2();
/* Foo x = &test2;
x();*/
}
fn void main()
{
int x = 2;
builtin::print_backtrace("Hello", 0);
test1();
}
2 changes: 2 additions & 0 deletions src/compiler/linker.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ static void linker_setup_linux(const char ***args_ref, LinkerType linker_type)
if (active_target.debug_info == DEBUG_INFO_FULL)
{
add_arg("-rdynamic");
add_arg("-ldl");
}
add_arg("-pthread");
return;
Expand Down Expand Up @@ -432,6 +433,7 @@ static void linker_setup_linux(const char ***args_ref, LinkerType linker_type)
add_arg2("-L", crt_dir);
add_arg("--dynamic-linker=/lib64/ld-linux-x86-64.so.2");
add_arg("-lm");
add_arg("-ldl");
add_arg("-lpthread");
add_arg("-lc");
add_arg("-L/usr/lib/");
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/llvm_codegen_stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,7 @@ void llvm_emit_panic_if_true(GenContext *c, BEValue *value, const char *panic_na
}
}
llvm_emit_panic(c, panic_name, loc, fmt, values);
llvm_emit_br(c, ok_block);
llvm_emit_unreachable(c);
llvm_emit_block(c, ok_block);
}

Expand Down

0 comments on commit bca1e5b

Please sign in to comment.