janestreet / magic-trace

magic-trace collects and displays high-resolution traces of what a process is doing
https://magic-trace.org
MIT License
4.52k stars 87 forks source link

Unwind the stack for caml_stash_backtrace #262

Closed cgaebel closed 1 year ago

cgaebel commented 1 year ago

In hacky ocaml-exception-detection mode, unwind the stack one more level for caml_stash_backtrace. I did some digging in core_unix and I'm not actually sure when this changed, but traces are stairstep for the following trivial async program without this quick patch.

open! Core
open! Async

let rec go () =
    Reader.open_file "a.txt"
    >>= fun a ->
    Writer.open_file "b.txt"
    >>= fun b ->
    let p1 = Reader.lines a in
    Pipe.iter p1 ~f:(fun line ->
        Writer.write_line b line;
        Writer.flushed b)
    >>= fun () ->
    Reader.close a
    >>= fun () ->
    Writer.close b
    >>= fun () ->
    Unix.rename ~src:"b.txt" ~dst:"a.txt"
    >>= fun () ->
    go ()

let command =
    Command.async ~summary:"" (Command.Param.return go)

let () =
  Command_unix.run command

If you run this, you'll need to put some lines of text in a.txt before running it.