Checking value of a variable

Hello, dear friends!
Let’s say, I have a code fragment like this:
(* Read a binary file into an array of integers )
let make_bin_array f =
let ic = open_in_bin f in
let rec makelist lst =
(
Catch expected end-of-file error; exit gracefully *)
try let b = (input_byte ic) in b::(makelist lst)
with End_of_file → close_in ic; lst
in Array.of_list (makelist [])
let arr = make_bin_array “c:\Bin_Code.bin”;;

It was compiled: #ocamlc -g myCode.ml
I am trying to debug it in Visual Code on Debian 9.x using ocamlearlybird 1.1.0. The debugging session starts OK, i can put breakpoints and step into the code.
Three questions arise:
1.execution does not start at the entry point of the program but falls through into the system library, namely, stdlib.ml. What can be done about it?

2.Breakpoints do not stick on a arbitrary line, but just on some line where there is debugging event happening. What exactly prevents a breakpoint from sticking on certain lines?

3.When execution finally gets to the last line: let arr = make_bin_array “c:\Bin_Code.bin”;;
I would like to check the “value” of “arr”, i put a watch on it, is shows type, but is says “Not available”.
I have found this useful piece of info, which sheds some light on my problem:

Functional values are not data, therefore there is no reason or way to print them.
In general, in OCaml there is no canonical value representation or printing, like in Python, for example. For each type one can write a function that will print some representation of values of this type. It is totally arbitrary and up to a developer discretion.

Another thing to understand about OCaml, is that programs written in OCaml are compiled to machine code, and there are no types or any other runtime supporting information, available during program execution.

So, there is not notion of type, but what was the purpose of “-g” switch i used when compiling?

My third and last question is what should i do to see or print the value of my “arr”?
Could somebody be so kind and show an example of a function, which would print that value?

As you can see, i am coming from a completely different realm of C programming and debugging and am having really rough times here…

Thanks a lot in advance! your help is greatly appreciated!

Its purpose is the same as in C compilers: include line numbers and names in the file to produce more informative exception traces and give information to debuggers.

I’ve never used ocamlearlybird so I can’t answer questions about it specifically. However, that passage you quote is true: just like in C, there are no canonical printers. You can find/make one and use a debug print. There’s nothing wrong with debug prints.

In any case, the bigger the disconnect between the language and the machine code, the less useful stepping debuggers become. OCaml is a borderline case when a debugger is still not entirely useless, but the class of programs where it’s better than the alternatives (i.e. debug logging or formal reasoning) isn’t big.

By the way, if you want a mutable buffer, you may want to go for Bytes instead of a list.

1 Like