Given a binary file compiled with OCaml, is there a way to find out if it has been compiled with profiling information (either using ocamlcp
/ocamloptp
, or with gprof
-specific data via ocamlopt -p
)?
2 Answers
If you run ocamlobjinfo
on a profiled bytecode file, it shows Profiling as one of the imported interfaces:
$ ocamlcp -c my.ml
$ ocamlobjinfo my.cmo
File my.cmo
Unit name: My
Interfaces imported:
720848e0b508273805ef38d884a57618 Array
d7e1aaf95ba3578d33efe347aefa6c49 My
db723a1798b122e08919a2bfed062514 Pervasives
6a6248bae49664a0bb530dd3f0c15b79 Profiling
Uses unsafe features: no
$
Update
On my system (OS X) A profiled native executable contains a definition of camlProfiling
and related symbols:
$ ocamlopt -o my my.ml
$ nm my | grep camlProfiling
$ ocamloptp -o my my.ml
$ nm my | grep camlProfiling
000000010003e240 D _camlProfiling
000000010003e2e0 d _camlProfiling__1
000000010003e300 d _camlProfiling__2
000000010003e318 d _camlProfiling__3
000000010003e268 d _camlProfiling__4
000000010003e280 d _camlProfiling__5
000000010003e2a0 d _camlProfiling__6
000000010003e2c0 d _camlProfiling__7
0000000100003760 T _camlProfiling__code_begin
0000000100003ac7 T _camlProfiling__code_end
000000010003e238 D _camlProfiling__data_begin
000000010003e328 D _camlProfiling__data_end
00000001000038d0 T _camlProfiling__dump_counters_1014
0000000100003a40 T _camlProfiling__entry
000000010003e32c D _camlProfiling__frametable
0000000100003770 T _camlProfiling__fun_1046
0000000100003800 T _camlProfiling__fun_1048
0000000100003890 T _camlProfiling__incr_1010
It seems very likely this will work on every system that supports nm
.

- 65,646
- 2
- 72
- 108
-
Thanks! Indeed it works for the bytecode version. Any ideas for native binaries? – anol Aug 08 '12 at 19:16
-
I was working on it just now, but I don't have `ocamloptp` (OCaml 3.12.1). Is it new in OCaml 4, perhaps? – Jeffrey Scofield Aug 08 '12 at 19:19
-
1I just checked and yes, it is a new tool, though the documentation doesn't state it very clearly (it becomes clear if you use the "enhanced documentation" that provides a diff between versions: http://www.askra.de/software/ocaml-doc/4.00/). For now, I'll try something using "ocamlopt -p". – anol Aug 08 '12 at 19:22
-
I'll look into this when I get a free moment, sorry for not being so helpful. I'm deep into porting OCaml 4 to iOS--lots to take care of. – Jeffrey Scofield Aug 10 '12 at 20:56
-
If you could please confirm if `nm ... | grep mcount` works for you (as described in my answer), I'd like to accept an answer containing a solution for both bytecode and native binaries. – anol Jul 03 '15 at 16:31
-
It seems better to grep for `camlProfiling`. It's essentially the same as the bytecode case then. So that's what I wrote up. – Jeffrey Scofield Jul 03 '15 at 17:04
-
Ok, I got it, the issue is that I was compiling using `ocamlopt -p` instead of `ocamloptp`. The former only generates information useful for `gprof` and does not produce the `camlProfiling` symbols in the binary, hence my comment. However, `ocamloptp` does. Also, in my tests, the binary produced by `ocamlopt -p` runs about twice as slower than the one with `ocamloptp`, so it should be deprecated. Edited the question to include both possibilities. – anol Jul 03 '15 at 17:31
Note: the old profiling flag (ocamlopt -p
) produces gprof
-specific information and does not produce camlProfiling
symbols as in Jeffrey's answer. But using ocamloptp
, his solution works.
If you need the "old" method, as indicated in this website, a somewhat reliable way to identify if a binary has been compiled with gprof
support is to check for the presence of symbol mcount
:
nm <native binary> | grep mcount
Only programs compiled with -p
should contain the mcount
symbol:
U mcount@@GLIBC_2.2.5
Otherwise, the program has not been compiled using the -p
flag.

- 8,264
- 3
- 34
- 78