English version

OCaml

Note to french readers:

Désolé mais cette page n'est disponible qu'en anglais. Je ne sais même pas si quelqu'un la lira un jour, et si c'est le cas, je suppose que ce quelqu'un comprendra l'(mon) anglais. Soyez donc déjà heureux qu'elle existe.

I'm a fervent user of the OCaml language. I won't try to convince you since there is already so much on the internet, so I'll just put there a few tips I had sometimes a hard time to find

OCamlDebug: the OCaml debugger

Even so it is much harder to do segmentation faults in OCaml, the OCaml debugger is a really useful program. What's really nice about it is that you can go "back" in time...

Using the debugger with a program that prints on stdout and reads on stdin...

If your program prints a lot of information, it starts getting difficult to understand what's printed on your screen, especially the difference between the output of your program and the output of the debugger. For instance, imagine debugging ocamldebug...

Similarly, if your program expects some input on stdin, it can get quite messy.

First solution: file redirection.

If your program is test, using :
% ocamldebug ./test > output.out
or
% ocamldebug ./test
        Objective Caml Debugger version 3.10.0

(ocd) set arguments < input.in
Then you can either check the output of your program with tail -f output.out, or controlling better the input by making input.in a fifo and writing in it with cat in another terminal:
% mkfifo input.in
% cat > input.in
12
42
(...)
This also facilitates backward execution in ocamldebug: since it's not really backward (see the documentation), it may ask you again an input number, which a finite file cannot provide.

Second solution: debugging through a socket.

That way, the program will run in it's window with it's own stdin and stdout, and the debugger will run in another terminal. First, launch the debugger and issue the following commands:
% ocamldebug ./test
        Objective Caml Debugger version 3.10.0

(ocd) set socket /tmp/debugsocket
(ocd) set loadingmode manual
You can also pass /tmp/debugsocket with the -s option on the command line. The manual loading mode tells ocamldebug not to launch it's own process but will wait instead for a program to be launched at the other end of the socket :
(ocd) run
Loading program... 
Waiting for connection...(the socket is /tmp/debugsocket)
Then, launch yourself the program in another terminal, passing it the CAML_DEBUG_SOCKET environment parameter:
% CAML_DEBUG_SOCKET=/tmp/debugsocket ./test
Note that if you don't set yourself the socket in the debugger, it will create one itself and tell which one, e.g. Waiting for connection...(the socket is /tmp/camldebug23856). You can also choose to set up an internet socket for remote debugging. The debugger run on myhost:
% ocamldebug -s localhost:8042 ./test
        Objective Caml Debugger version 3.10.0

(ocd) set loadingmode manual
(ocd) run
Loading program... 
Waiting for connection...(the socket is localhost:8042)
and the program on some other host:
% CAML_DEBUG_SOCKET=myhost:8042 ./test
There might be some problems due to the restriction of the socket created to connections from localhost (use netstat -nl to check). In that case, an ssh tunnel works: ssh otherhost -R 1234:myhost:8042, then CAML_DEBUG_SOCKET=localtost:1234 ./test on otherhost. (Other options involving netcat or 6tunnel should also work.)
Dernière modification : vendredi 11 décembre 2009
Boite aux lettres
Powered by the ENS Lyon