hare

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

abort.ha (945B)


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