#+build linux

package main

import "libraries/back"
import "core:sys/posix"
import "base:runtime"
import "core:mem"
import "core:fmt"

_register_illegal_instruction_handler :: proc() {
	posix.signal(.SIGILL, proc "c" (code: posix.Signal) {
		context = runtime.default_context()

		space: [16*mem.Kilobyte]byte
		arena: mem.Arena
		mem.arena_init(&arena, space[:])
		allocator := mem.arena_allocator(&arena)

		context.allocator      = allocator
		context.temp_allocator = allocator

		backtrace: {
			lines, err := back.lines(back.trace(), allocator, allocator)
			if err != nil {
				fmt.eprintf("Exception (Code: %i)\nCould not get backtrace: %v\n", code, err)
				break backtrace
			}

			fmt.eprintf("Exception (Code: %i)\n[back trace]\n", code)
			back.print(lines, temp_allocator=allocator)
		}

		runtime.exit(int(code))
	})
}
