New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"unknown location" in backtraces in ocaml script #9701
Comments
I will add a regression testcase in the testsuite so that we track ocaml-script behavior with respect to backtraces. |
This reverts commit f0fae66. This new test appears to break something on the CI, possibly when flambda is used: > ... testing 'toplevel_script_backtrace.ml' with 1 (toplevel) => > failed (Running toplevel_script_backtrace.ml in bytecode toplevel > (expected exit status: 2): command > /home/barsac/ci/builds/workspace/extra-checks/runtime/ocamlrun > /home/barsac/ci/builds/workspace/extra-checks/ocaml -noinit -no-version -noprompt -nostdlib -I > /home/barsac/ci/builds/workspace/extra-checks/stdlib -I > /home/barsac/ci/builds/workspace/extra-checks/toplevel > toplevel_script_backtrace.ml failed with exit code 1)
(no change entry required)
Pinging prospective bug-fixers: this is an annoying regression that was introduced somewhat recently. |
If that's the case then I believe the following patch should be enough to fix the issue: diff --git a/toplevel/toploop.ml b/toplevel/toploop.ml
index 5e5fc436d..8de0347d6 100644
--- a/toplevel/toploop.ml
+++ b/toplevel/toploop.ml
@@ -639,6 +639,7 @@ let run_script ppf name args =
Location.report_exception ppf exn; exit 2
end;
Sys.interactive := false;
+ Clflags.debug := true;
run_hooks After_setup;
let explicit_name =
(* Prevent use_silently from searching in the path. *) however it doesn't appear to work for me. So perhaps I'm doing something wrong when trying to reproduce, or there is another issue. I do think the patch I wrote above is needed though. I'll let someone else try it. |
I double checked, this is what I obtained:
In short: @trefis' suggested fix indeed fixes the problem when applied to the commit that introduced the issue (3efba04), but is no longer enough to fix the issue when applied to |
Is this fixed somewhere?
where I used to have nice location. |
This is in compiled code, not even in script. I tried OCAMLRUNPARAM=b, and Printexc.record_backtrace true in my main.ml but still get only unknown location errors. |
Here is the same backtrace with 4.09.1
|
@aryx if the |
Ok, finally figure out what was going on ... It's because I was compiling my project with (modes byte) in the dune file, |
Also I just discovered I was shipping bytecode version of my programs all this time ... I got fooled by the presence |
I've been tracking this down since yesterday, and although debug was not enabled when running script files what is actually happening is that the bytecode for the script is being released before the actual backtrace is recorded. Which results on that unknown location being showed instead of the actual location of the backtrace slot. https://github.com/ocaml/ocaml/blob/trunk/toplevel/byte/topeval.ml#L87-L99 match
may_trace := true;
Fun.protect
~finally:(fun () -> may_trace := false;
if can_free then Meta.release_bytecode bytecode) (* Debugging info gets released together with bc *)
closure
with
| retval -> Result retval
| exception x ->
record_backtrace (); (* Being called after debugging information is released *)
toplevel_value_bindings := initial_bindings; (* PR#6211 *)
Symtable.restore_state initial_symtable;
Exception x Releasing bytecode removes debug information from the https://github.com/ocaml/ocaml/blob/trunk/runtime/meta.c#L148 CAMLprim value caml_static_release_bytecode(value bc)
{
code_t prog;
struct code_fragment *cf;
prog = Bytecode_val(bc)->prog;
caml_remove_debug_info(prog); It works after applying the following patch, but returns an unknown definition on the stack trace. diff --git a/toplevel/byte/topeval.ml b/toplevel/byte/topeval.ml
index 2132b4c60..796eaa9e1 100644
--- a/toplevel/byte/topeval.ml
+++ b/toplevel/byte/topeval.ml
@@ -87,13 +87,16 @@ let load_lambda ppf lam =
match
may_trace := true;
Fun.protect
- ~finally:(fun () -> may_trace := false;
- if can_free then Meta.release_bytecode bytecode)
+ ~finally:(fun () -> may_trace := false)
closure
with
- | retval -> Result retval
+ | retval ->
+ if can_free then Meta.release_bytecode bytecode;
+ Result retval
| exception x ->
record_backtrace ();
+ if can_free then Meta.release_bytecode bytecode;
+
toplevel_value_bindings := initial_bindings; (* PR#6211 *)
Symtable.restore_state initial_symtable;
Exception x
diff --git a/toplevel/toploop.ml b/toplevel/toploop.ml
index 62a5b0023..87f97c7cd 100644
--- a/toplevel/toploop.ml
+++ b/toplevel/toploop.ml
@@ -109,6 +109,7 @@ let load_file = load_file false
(* Execute a script. If [name] is "", read the script from stdin. *)
let run_script ppf name args =
+ Clflags.debug := true;
override_sys_argv args;
let filename = filename_of_input name in
Compmisc.init_path ~dir:(Filename.dirname filename) (); Result for the test case reproduced above
I've pushed that here as a proof of concept renatoalencar@a096eed. |
@renatoalencar your analysis seems reasonable to me; it looks like the bug you describe is fairly recent, introduced by #9855 which was merged in 4.12. Would you like to propose a Pull Request with your fix? Three remarks:
|
Definitely. And about |
I would not try to test |
I'm trying to get backtraces in simple test code:
Running
env OCAMLRUNPARAM=b ocaml test.ml
:Notice the "unknown location"
But for older version location in source file was provided
discuss thread for reference: https://discuss.ocaml.org/t/backtraces-in-script-run-by-ocaml/5970
The text was updated successfully, but these errors were encountered: