-
Notifications
You must be signed in to change notification settings - Fork 7
/
back.odin
146 lines (117 loc) · 4.21 KB
/
back.odin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package back
import "core:fmt"
import "core:io"
import "core:os"
import "base:runtime"
import "core:text/table"
// Size of a constant backtrace, as used by the allocator for example.
BACKTRACE_SIZE :: #config(BACKTRACE_SIZE, 16)
// For targets that do not have native support (using debug info),
// backtraces are done through instrumentation, Odin only allows one enter/exit instrumentation
// procedure though, so you can set this to true, add your own instrumentation procs, and have
// them call `back.other_instrumentation_enter` and `back.other_instrumentation_exit` to hook
// up the backtraces.
//
// The custom proc must have `#force_inline`.
OTHER_CUSTOM_INSTRUMENTATION :: #config(BACK_OTHER_CUSTOM_INSTRUMENTATION, false)
// Force the fallback instrumentation based implementation instead of debug info based.
FORCE_FALLBACK :: #config(BACK_FORCE_FALLBACK, false)
USE_FALLBACK :: FORCE_FALLBACK || (ODIN_OS != .Darwin && ODIN_OS != .Linux && ODIN_OS != .Windows)
Trace :: []Trace_Entry
Trace_Const :: struct {
trace: [BACKTRACE_SIZE]Trace_Entry,
len: int,
}
// Platform specific.
Trace_Entry :: _Trace_Entry
Line :: struct {
location: string,
symbol: string,
}
EAGAIN :: os.EAGAIN when ODIN_OS == .Linux || ODIN_OS == .Darwin else 5
ENOMEM :: os.ENOMEM when ODIN_OS == .Linux || ODIN_OS == .Darwin else 6
EFAULT :: os.EFAULT when ODIN_OS == .Linux || ODIN_OS == .Darwin else 7
EMFILE :: os.EMFILE when ODIN_OS == .Linux || ODIN_OS == .Darwin else 8
ENFILE :: os.ENFILE when ODIN_OS == .Linux || ODIN_OS == .Darwin else 9
ENOSYS :: os.ENOSYS when ODIN_OS == .Linux || ODIN_OS == .Darwin else 10
Lines_Error :: enum {
None,
Parse_Address_Fail,
Addr2line_Unexpected_EOF,
Addr2line_Output_Error,
Addr2line_Unresolved,
Fork_Limited = int(EAGAIN),
Out_Of_Memory = int(ENOMEM),
Invalid_Fd = int(EFAULT),
Pipe_Process_Limited = int(EMFILE),
Pipe_System_Limited = int(ENFILE),
Fork_Not_Supported = int(ENOSYS),
Info_Not_Found,
}
trace :: #force_no_inline proc() -> (bt: Trace_Const) {
bt.len = #force_inline _trace(bt.trace[:])
return
}
trace_n :: #force_no_inline proc(max_len: i32, allocator := context.allocator) -> Trace {
context.allocator = allocator
bt := make([]Trace_Entry, max_len)
n := #force_inline _trace(bt[:])
return bt[:n]
}
trace_fill :: #force_no_inline proc(buf: Trace) -> int {
return #force_inline _trace(buf)
}
trace_n_destroy :: proc(b: Trace, allocator := context.allocator) {
delete(b, allocator)
}
// Processes the message trying to get more/useful information.
// This adds file and line information if the program is running in debug mode.
//
// If an error is returned the original message will be the result and is save to use.
lines :: proc {
lines_n,
lines_const,
}
lines_n :: proc(bt: Trace, allocator := context.allocator) -> (out: []Line, err: Lines_Error) {
context.allocator = allocator
return _lines(bt)
}
lines_const :: proc(bt: Trace_Const, allocator := context.allocator) -> (out: []Line, err: Lines_Error) {
context.allocator = allocator
bt := bt
return _lines(bt.trace[:bt.len])
}
lines_destroy :: proc(lines: []Line, allocator := context.allocator) {
context.allocator = allocator
_lines_destroy(lines)
}
assertion_failure_proc :: proc(prefix, message: string, loc: runtime.Source_Code_Location) -> ! {
t := trace()
lines, err := lines(t.trace[:t.len])
if err != nil {
fmt.eprintf("could not get backtrace for assertion failure: %v\n", err)
runtime.default_assertion_failure_proc(prefix, message, loc)
} else {
fmt.eprintln("[back trace]")
print(lines)
runtime.default_assertion_failure_proc(prefix, message, loc)
}
}
register_segfault_handler :: proc() {
_register_segfault_handler()
}
print :: proc(lines: []Line, padding := " ", w: Maybe(io.Writer) = nil, no_temp_guard := false) {
w := w.? or_else os.stream_from_handle(os.stderr)
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore=no_temp_guard)
tbl := table.init(&table.Table{}, context.temp_allocator, context.temp_allocator)
for line in lines {
table.row(tbl, padding, line.symbol, " - ", line.location)
}
table.build(tbl, table.unicode_width_proc)
for row in 0..<tbl.nr_rows {
for col in 0..<tbl.nr_cols {
table.write_table_cell(w, tbl, row, col)
}
io.write_byte(w, '\n')
}
}