diff --git a/Changes b/Changes index fe88aa900..cfd419244 100644 --- a/Changes +++ b/Changes @@ -54,10 +54,13 @@ Compilers: - PR#5571: incorrect ordinal number in error message Standard library: +- PR#5899: expose a way to inspect the current call stack + (Printexc.get_callstack) - PR#5986: new flag Marshal.Compat_32 for the serialization functions (Marshal.to_*), forcing the output to be readable on 32-bit hosts. - Add optimized composition operators |> and @@ in Pervasives + Runtime system: * PR#6019: more efficient implementation of caml_modify() and caml_initialize(). The new implementations are less lenient than the old ones: now, diff --git a/stdlib/printexc.ml b/stdlib/printexc.ml index a36e2d4e3..3324f6c4f 100644 --- a/stdlib/printexc.ml +++ b/stdlib/printexc.ml @@ -165,3 +165,6 @@ external backtrace_status: unit -> bool = "caml_backtrace_status" let register_printer fn = printers := fn :: !printers + + +external get_callstack: int -> raw_backtrace = "caml_get_current_callstack" diff --git a/stdlib/printexc.mli b/stdlib/printexc.mli index b65326552..773fed814 100644 --- a/stdlib/printexc.mli +++ b/stdlib/printexc.mli @@ -11,7 +11,7 @@ (* *) (***********************************************************************) -(** Facilities for printing exceptions. *) +(** Facilities for printing exceptions and inspecting current call stack. *) val to_string: exn -> string (** [Printexc.to_string e] returns a string representation of @@ -99,3 +99,16 @@ type raw_backtrace val get_raw_backtrace: unit -> raw_backtrace val print_raw_backtrace: out_channel -> raw_backtrace -> unit val raw_backtrace_to_string: raw_backtrace -> string + + +(** {6 Current call stack} *) + +val get_callstack: int -> raw_backtrace + +(** [Printexc.get_callstack n] returns a description of the top of the + call stack on the current program point (for the current thread), + with at most [n] entries. (Note: this function is not related to + exceptions at all, despite being part of the [Printexc] module.) + + @since 4.01.0 +*)