/*---------------------------------------------------------------------------
  Copyright 2023-2024, Microsoft Research, Daan Leijen.

  This is free software; you can redistribute it and/or modify it under the
  terms of the Apache License, Version 2.0. A copy of the License can be
  found in the LICENSE file at the root of this distribution.
---------------------------------------------------------------------------*/

// Core debugging functions.
module std/core/debugstd/core/debug

import std/core/typesstd/core/types
import std/core/unsafestd/core/unsafe
import std/core/hndstd/core/hnd
import std/core/stringstd/core/string
import std/core/consolestd/core/console

extern import
  c file "inline/debug.c"

// ------------------------------------------------------------------------------
// File locations
// ------------------------------------------------------------------------------

// Compilation constant that is replaced with the current file's module name
pub val file/kk-modulestd/core/debug/file/kk-module: string : stringstd/core/types/string: V = ""literal: string
count= 0
// Compilation constant that is replaced with the current line number pub val file/kk-linestd/core/debug/file/kk-line: string : stringstd/core/types/string: V = ""literal: string
count= 0
// Compilation constant that is replaced with the current file name pub val file/kk-filestd/core/debug/file/kk-file: string : stringstd/core/types/string: V = ""literal: string
count= 0
pub fip fun file/kk-file-linestd/core/debug/file/kk-file-line: (@implicit/kk-file : string, @implicit/kk-line : string) -> string( @implicit/kk-file?kk-file: string, @implicit/kk-line?kk-line: string )result: -> total string @implicit/kk-file?kk-file: string ++std/core/types/(++): (x : string, y : string) -> string "("literal: string
count= 1
++std/core/types/(++): (x : string, y : string) -> string @implicit/kk-line?kk-line: string ++std/core/types/(++): (x : string, y : string) -> string ")"literal: string
count= 1
// ---------------------------------------------------------------------------- // Trace, assert, todo // ---------------------------------------------------------------------------- fip extern xtracestd/core/debug/xtrace: (message : string) -> () : ( message : stringstd/core/types/string: V ) -> (std/core/types/unit: V)std/core/types/unit: V c "kk_trace" cs "Primitive.Trace" js "$std_core_console._trace" fip extern xtrace-anystd/core/debug/xtrace-any: forall<a> (message : string, x : a) -> () : forall<aa: V> ( message: stringstd/core/types/string: V, x : aa: V ) -> (std/core/types/unit: V)std/core/types/unit: V c "kk_trace_any" cs "Primitive.TraceAny" js "$std_core_console._trace_any" val trace-enabledstd/core/debug/trace-enabled: ref<global,bool> : refstd/core/types/ref: (H, V) -> V<globalstd/core/types/global: H,boolstd/core/types/bool: V> = unsafe-totalstd/core/unsafe/unsafe-total: (action : () -> <alloc<global>|_130> ref<global,bool>) -> ref<global,bool>{ refstd/core/types/ref: (value : bool) -> <alloc<global>|_130> ref<global,bool>(Truestd/core/types/True: bool) } // Trace a message used for debug purposes. // The behaviour is system dependent. On a browser and node it uses // `console.log` by default. // Disabled if `notrace` is called. pub fip fun tracestd/core/debug/trace: (message : string) -> ()( messagemessage: string : stringstd/core/types/string: V )result: -> total () : (std/core/types/unit: V)std/core/types/unit: V unsafe-totalstd/core/unsafe/unsafe-total: (action : () -> <read<global>|_158> ()) -> () if !std/core/types/ref/(!): (ref : ref<global,bool>) -> <read<global>|_158> booltrace-enabledstd/core/debug/trace-enabled: ref<global,bool> then xtracestd/core/debug/xtrace: (message : string) -> <read<global>|_158> ()(messagemessage: string)std/core/types/Unit: () pub fip fun trace-infostd/core/debug/trace-info: (message : string, @implicit/kk-file-line : string) -> ()( messagemessage: string : stringstd/core/types/string: V, @implicit/kk-file-line?kk-file-line: string : stringstd/core/types/string: V )result: -> total () : (std/core/types/unit: V)std/core/types/unit: V tracestd/core/debug/trace: (message : string) -> ()(@implicit/kk-file-line?kk-file-line: string ++std/core/types/(++): (x : string, y : string) -> string ": "literal: string
count= 2
++std/core/types/(++): (x : string, y : string) -> string messagemessage: string
) pub fip fun trace-showstd/core/debug/trace-show: forall<a> (x : a, @implicit/show : (a) -> string, @implicit/kk-file-line : string) -> ()( xx: $193 : aa: V, @implicit/show?show: ($193) -> string : aa: V -> stringstd/core/types/string: V, @implicit/kk-file-line?kk-file-line: string : stringstd/core/types/string: V )result: -> total () : (std/core/types/unit: V)std/core/types/unit: V trace-infostd/core/debug/trace-info: (message : string, @implicit/kk-file-line : string) -> ()
?kk-file-line=?kk-file-line
(xx: $193.show?show: ($193) -> string
) pub fip fun trace-anystd/core/debug/trace-any: forall<a> (message : string, x : a) -> ()( messagemessage: string : stringstd/core/types/string: V, xx: $219 : aa: V )result: -> total () : (std/core/types/unit: V)std/core/types/unit: V unsafe-totalstd/core/unsafe/unsafe-total: (action : () -> <read<global>|_245> ()) -> () if !std/core/types/ref/(!): (ref : ref<global,bool>) -> <read<global>|_245> booltrace-enabledstd/core/debug/trace-enabled: ref<global,bool> then xtrace-anystd/core/debug/xtrace-any: (message : string, x : $219) -> <read<global>|_245> ()(messagemessage: string,xx: $219)std/core/types/Unit: () // Disable tracing completely. pub noinline fun notracestd/core/debug/notrace: () -> (st<global>) ()()result: -> (st<global>) () : ststd/core/types/st: H -> E<globalstd/core/types/global: H> (std/core/types/unit: V)std/core/types/unit: V trace-enabledstd/core/debug/trace-enabled: ref<global,bool> :=std/core/types/set: (ref : ref<global,bool>, assigned : bool) -> <write<global>,alloc<global>,read<global>> () Falsestd/core/types/False: bool noinline extern unsafe-abortstd/core/debug/unsafe-abort: forall<a> (msg : string) -> a( msgmsg: string : stringstd/core/types/string: V ) : astd/core/types/total: E c "kk_abort" js inline "function() { throw new Error(\"fatal error: \" + #1) }()" pub noinline fip fun impossiblestd/core/debug/impossible: forall<a> (message : ? string, @implicit/kk-file-line : string) -> a( messagemessage: ? string : stringstd/core/types/string: V = "?"literal: string
count= 1
, @implicit/kk-file-line?kk-file-line: string : stringstd/core/types/string: V )result: -> total 340 : astd/core/types/total: E unsafe-abortstd/core/debug/unsafe-abort: (msg : string) -> $307(kk-file-line?kk-file-line: string ++std/core/types/(++): (x : string, y : string) -> string ": impossible:"literal: string
count= 13
++std/core/types/(++): (x : string, y : string) -> string messagemessage: string
) pub fip fun assertstd/core/debug/assert: (message : string, condition : bool, @implicit/kk-file-line : string) -> ()( messagemessage: string : stringstd/core/types/string: V, conditioncondition: bool : boolstd/core/types/bool: V, @implicit/kk-file-line?kk-file-line: string : stringstd/core/types/string: V )result: -> total () : (std/core/types/unit: V)std/core/types/unit: V // Compiler removes assert calls in optimized builds if !std/core/types/bool/(!): (b : bool) -> boolconditioncondition: bool then unsafe-abortstd/core/debug/unsafe-abort: (msg : string) -> ()(kk-file-line?kk-file-line: string ++std/core/types/(++): (x : string, y : string) -> string ": assertion failed: "literal: string
count= 20
++std/core/types/(++): (x : string, y : string) -> string messagemessage: string
)std/core/types/Unit: () // Explicitly trigger a breakpoint pub fip extern breakpointstd/core/debug/breakpoint: () -> ndet ()() : ndetstd/core/types/ndet: X (std/core/types/unit: V)std/core/types/unit: V { c "kk_debugger_break" cs "System.Diagnostics.Debugger.Break" js inline "(function(){ debugger; })()" }result: -> ndet ()