hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

abort.ha (882B)


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