Skip to content

Commit 51a54c3

Browse files
committed
Fixes to native backtrace.
1 parent dc0aa35 commit 51a54c3

6 files changed

Lines changed: 36 additions & 32 deletions

File tree

‎lib/std/core/builtin.c3‎

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,14 @@ struct CallstackElement
8787
uint line;
8888
}
8989

90-
fn bool print_backtrace(String message, int backtraces_to_ignore) @if(env::DARWIN || env::LINUX)
90+
fn bool print_backtrace(String message, int backtraces_to_ignore) @if(env::NATIVE_STACKTRACE)
9191
{
9292
@pool()
9393
{
9494
BacktraceList! backtrace = backtrace::backtrace_load(mem::temp());
95+
io::eprintn("A");
9596
if (catch backtrace) return false;
97+
io::eprintfn("B %d %d", backtraces_to_ignore, backtrace.len());
9698
if (backtrace.len() <= backtraces_to_ignore) return false;
9799
io::eprint("\nERROR: '");
98100
io::eprint(message);
@@ -115,7 +117,7 @@ fn bool print_backtrace(String message, int backtraces_to_ignore) @if(env::DARWI
115117
return true;
116118
};
117119
}
118-
fn void default_panic(String message, String file, String function, uint line) @if(env::DARWIN)
120+
fn void default_panic(String message, String file, String function, uint line) @if(env::NATIVE_STACKTRACE)
119121
{
120122
$if $defined(io::stderr):
121123
if (!print_backtrace(message, 2))
@@ -127,7 +129,7 @@ fn void default_panic(String message, String file, String function, uint line) @
127129
$$trap();
128130

129131
}
130-
fn void default_panic(String message, String file, String function, uint line) @if(!env::DARWIN)
132+
fn void default_panic(String message, String file, String function, uint line) @if(!env::NATIVE_STACKTRACE)
131133
{
132134
CallstackElement* stack = $$stacktrace();
133135
$if $defined(io::stderr):
@@ -367,35 +369,17 @@ macro uint void*.hash(void* ptr) => ((ulong)(uptr)ptr).hash();
367369
module std::core::builtin @if((env::LINUX || env::DARWIN) && env::COMPILER_SAFE_MODE && env::DEBUG_SYMBOLS);
368370
import libc;
369371

370-
fn void sig_panic(String message) @if(env::DARWIN)
372+
fn void sig_panic(String message)
371373
{
372374
default_panic(message, "???", "???", 0);
373375
}
374376

375-
fn void sig_panic(String message) @if(!env::DARWIN)
376-
{
377-
$if $defined(io::stderr) && $defined(File.printf):
378-
CallstackElement* stack = $$stacktrace();
379-
if (stack) stack = stack.prev;
380-
if (stack) stack = stack.prev;
381-
(void)io::stderr().print("\nSYSTEM ERROR: '");
382-
(void)io::stderr().print(message);
383-
(void)io::stderr().printn("'");
384-
while (stack)
385-
{
386-
(void)io::stderr().printfn(" in %s %s (%s:%s)", stack.location.name, stack.function, stack.file, stack.line);
387-
if (stack == stack.prev) break;
388-
stack = stack.prev;
389-
}
390-
$endif
391-
}
392-
393377
SignalFunction old_bus_error;
394378
SignalFunction old_segmentation_fault;
395379

396380
fn void sig_bus_error(CInt i)
397381
{
398-
$if !env::DARWIN:
382+
$if !env::NATIVE_STACKTRACE:
399383
sig_panic("Illegal memory access.");
400384
$else
401385
$if $defined(io::stderr):
@@ -410,7 +394,7 @@ fn void sig_bus_error(CInt i)
410394

411395
fn void sig_segmentation_fault(CInt i)
412396
{
413-
$if !env::DARWIN:
397+
$if !env::NATIVE_STACKTRACE:
414398
sig_panic("Out of bounds memory access.");
415399
$else
416400
$if $defined(io::stderr):

‎lib/std/core/env.c3‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ const MemoryEnvironment MEMORY_ENV = (MemoryEnvironment)$$MEMORY_ENVIRONMENT;
132132
const bool TRACK_MEMORY = DEBUG_SYMBOLS && (COMPILER_SAFE_MODE || TESTING);
133133
const bool X86_64 = ARCH_TYPE == X86_64;
134134
const bool AARCH64 = ARCH_TYPE == AARCH64;
135-
135+
const bool NATIVE_STACKTRACE = LINUX || DARWIN;
136136
const bool LINUX = LIBC && OS_TYPE == LINUX;
137137
const bool DARWIN = LIBC && os_is_darwin();
138138
const bool WIN32 = LIBC && OS_TYPE == WIN32;

‎lib/std/os/linux/linux.c3‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,16 @@ fn Backtrace! backtrace_load_element(void* addr, Allocator* allocator = mem::hea
140140
if (!addr) return backtrace::BACKTRACE_UNKNOWN;
141141
char[] buf = mem::temp_array(char, 1024);
142142
Linux_Dl_info info;
143-
if (dladdr(addr, &info) == 0) return backtrace::BACKTRACE_UNKNOWN;
143+
if (dladdr(addr, &info) == 0) return backtrace::BACKTRACE_UNKNOWN;
144144
void* obj_address = addr - (uptr)info.dli_fbase + (uptr)elf_module_image_base(info.dli_fname.str_view())!;
145145
ZString obj_path = info.dli_fname;
146-
String s = process::execute_stdout_to_buffer(buf, { "addr2line", "-p", "-i", "-C", "-f", "-e", obj_path.str_view(), string::tformat("0x%x", obj_address) })!;
146+
ZString sname = info.dli_sname ? info.dli_sname : (ZString)"???";
147+
String s = process::execute_stdout_to_buffer(buf, { "addr2line", "-p", "-C", "-f", "-e", obj_path.str_view(), string::tformat("0x%x", obj_address) })!;
147148
String[] parts = s.tsplit(" at ");
148149
if (parts.len != 2)
149150
{
150151
return {
151-
.function = info.dli_sname ? info.dli_sname.copy(allocator) : "???".copy(allocator),
152+
.function = sname.copy(allocator),
152153
.object_file = info.dli_fname.copy(allocator),
153154
.offset = (uptr)addr,
154155
.file = "".copy(allocator),
@@ -177,6 +178,7 @@ fn BacktraceList! backtrace_load(Allocator* allocator)
177178
{
178179
void*[256] bt_buffer;
179180
CInt size = posix::backtrace(&bt_buffer, 256);
181+
io::printfn("Backtrace list %s", size);
180182
BacktraceList list;
181183
list.init_new(size, allocator);
182184
defer catch
@@ -189,9 +191,10 @@ fn BacktraceList! backtrace_load(Allocator* allocator)
189191
}
190192
@pool(allocator)
191193
{
192-
for (usz i = 1; i < size; i++)
194+
for (usz i = 0; i < size; i++)
193195
{
194196
Backtrace trace = backtrace_load_element(bt_buffer[i], allocator)!;
197+
io::printn(trace);
195198
list.append(trace);
196199
}
197200
};

‎resources/linux_stack.c3‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,23 @@ import std::io;
33
import std::collections::map;
44
import std::os;
55

6-
fn void! main()
6+
fn void test2()
77
{
8+
//typeid y = int.typeid;
9+
(void)linux::backtrace_load(mem::heap());
10+
io::printn("Hello");
11+
builtin::panicf("FEHifej", "file", "fun", 123);
812
int x = 2;
9-
builtin::print_backtrace("Hello", 0);
13+
int[1] y;
14+
int z = y[x];
15+
//assert(false, "ofkef");
16+
}
17+
18+
fn void test1()
19+
{
20+
test2();
21+
}
22+
fn void main()
23+
{
24+
test1();
1025
}

‎src/compiler/linker.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ static void linker_setup_linux(const char ***args_ref, LinkerType linker_type)
389389
if (active_target.debug_info == DEBUG_INFO_FULL)
390390
{
391391
add_arg("-rdynamic");
392+
add_arg("-ldl");
392393
}
393394
add_arg("-pthread");
394395
return;
@@ -432,6 +433,7 @@ static void linker_setup_linux(const char ***args_ref, LinkerType linker_type)
432433
add_arg2("-L", crt_dir);
433434
add_arg("--dynamic-linker=/lib64/ld-linux-x86-64.so.2");
434435
add_arg("-lm");
436+
add_arg("-ldl");
435437
add_arg("-lpthread");
436438
add_arg("-lc");
437439
add_arg("-L/usr/lib/");

‎src/compiler/llvm_codegen_stmt.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,7 @@ void llvm_emit_panic_if_true(GenContext *c, BEValue *value, const char *panic_na
14081408
}
14091409
}
14101410
llvm_emit_panic(c, panic_name, loc, fmt, values);
1411-
llvm_emit_br(c, ok_block);
1411+
llvm_emit_unreachable(c);
14121412
llvm_emit_block(c, ok_block);
14131413
}
14141414

0 commit comments

Comments
 (0)