Debian Codegraph @

debug/abort.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 use debug::image;
5 use fmt;
6 use fs;
7 use os;
8 use rt;
9
10 let default_abort: *rt::abort_handler = @undefined;
11
12 @init fn init_abort() void = {
13 default_abort = rt::onabort(&debug_abort);
14 };
15
16 // Note: take care not to get into an abort loop when working on this code
17 fn debug_abort(
18 path: *str,
19 line: u64,
20 col: u64,
21 msg: str,
22 ) never = {
23 begin_fatal();
24
25 const self = match (image::self()) {
26 case let img: image::image =>
27 yield img;
28 case =>
29 _ = fmt::errorfln("Abort: {}:{}:{}: {}", *path, line, col, msg);
30 halt();
31 };
32 defer image::close(&self);
33
34 const st = match (os::fstat(self.fd)) {
35 case let st: fs::filestat =>
36 yield st;
37 case fs::error =>
38 yield fs::filestat { mask = 0, ... };
39 };
40
41 _ = fmt::errorfln("Abort: {}:{}:{}: {}", *path, line, col, msg);
42 print_context(*path, line, col, st.mtime);
43
44 let frame = walk();
45 // Skip rt::abort and debug::debug_abort
46 for (let skip = 2; skip > 0; skip -= 1) {
47 match (next(frame)) {
48 case let next: stackframe =>
49 frame = next;
50 case done => halt();
51 };
52 };
53
54 backtrace(&self, frame);
55 halt();
56 };
57
58 fn halt() never = {
59 _ = rt::kill(rt::getpid(), rt::SIGABRT);
60 for (true) void;
61 };