facebook / infer

A static analyzer for Java, C, C++, and Objective-C
http://fbinfer.com/
MIT License
14.98k stars 2.01k forks source link

How to debug Infer line-by-line? #1412

Open jiseongg opened 3 years ago

jiseongg commented 3 years ago

I have a question about how to debug Infer line-by-line. According to #536, that debugging environment doesn't seem to be prepared. However, I'm confused because the section Debugging OCaml Code in CONTRIBUTING.md assumes someone may use ocamldebug. Following is the situation I've faced, which implies what I want to do, including some information of my environment.

(ocd) goto 0 Loading program... /home/vagrant/github/facebook/infer/examples/c_hello/../../infer/bin/infer is not a bytecode file. (ocd)


- [ ] If possible, a minimal example to reproduce your problem (for instance, some code where
    infer reports incorrectly, together with the way you run infer to reproduce the incorrect
    report).

I think this is because of executable file format of Infer, not a bytecode but a native code. Is there any way to produce a bytecode executable file for Infer in current build system? I hope there is a way to do that with some environment setup, to use `ocamldebug` like the section **Debugging OCaml Code** in [CONTRIBUTING.md](https://github.com/facebook/infer/blob/master/CONTRIBUTING.md).

If not, how about supporting it? If possible, I want to contribute for that support.  I think Infer framework is a quite large software, having a steep learning curve. And if line-by-line debugging is possible, it'll mitigate the difficulty of studying it.
jvillard commented 3 years ago

You need to build infer in bytecode mode first. make byte will do that for you.

jiseongg commented 3 years ago

@jvillard Should I do that from Infer root directory again? It seems that running make byte in the project root directory also builds the facebook-clang-plugins again, even though I had built Infer completely before with ./build-infer.sh. It will take really long time. Thus, I stopped it and did it again in infer/src directory to check if it works.

After running make BUILD_MODE=dev-noerror -C infer/src byte, infer.bc.exe is generated and infer binary is updated. However, "not a bytecode file" message still remains and I cannot debug it with ocamldebug.

jvillard commented 3 years ago

You're right, make byte doesn't produce something that ocamldebug can consume anymore, I think since 786a72574fc6943c5635f3cb1a7abb417013c9b5 so that way of debugging infer is not available anymore.

jiseongg commented 3 years ago

I think so, too. Is that normal situation for OCaml project or only for Infer? Since I'm newbie in such large project written in OCaml, I'm just curious.

Then the only way to debug Infer is to use Logging module to print internal data, right?

MarsMan13 commented 1 year ago

@jiseongg Have you found any other good debugging methods? and Can you recommend any debugging methods that you have used in your projects?

From. Seoul, Korea too :)

skcho commented 1 year ago

Currently, make byte is building infer with the byte_complete mode.

byte_complete for building a bytecode executable as a native self-contained executable, i.e., an executable that doesn’t require the ocamlrun program to run and doesn’t require the C stubs to be installed as shared object files.

https://dune.readthedocs.io/en/stable/dune-files.html#linking-modes

And it does not seem to work well with ocamldebug.

https://github.com/ocaml/ocaml/issues/9344

Therefore, to run ocamldebug, we need to build it as the byte mode and run it with proper libarary paths, e.g.

~/infer/infer/src$ make byte

~/infer/infer/src$ CAML_LD_LIBRARY_PATH=../_build/default/src/c_stubs:$CAML_LD_LIBRARY_PATH ocamldebug ../bin/infer.bc --version
    OCaml Debugger version 4.14.0

(ocd) goto 0
Loading program... done.
Time: 0
Beginning of program.
(ocd) run
Infer version v1.1.0-ed404b4acb
Copyright 2009 - present Facebook. All Rights Reserved.
Time: 11399552
Program exit.

Then the only way to debug Infer is to use Logging module to print internal data, right?

Yes. But, for me, Logging has worked well for developing/debugging Infer in most of the cases so far.

MarsMan13 commented 1 year ago

@skcho Thank you for your response! It has been a great help in using/debugging Infer!

jiseongg commented 1 year ago

@MarsMan13, At the time, I failed to figure out the solution about using ocamldebug, and I forgot about this issue. Hope @skcho 's answer helped. :)

Anyway, I used Logging.debug_dev function, which could print anything I wanted to html files. (I had read this from CONTRIBUTING.md)

MarsMan13 commented 1 year ago

@jiseongg Thank you for your response!!!! Yes! @skcho 's solution is very useful for me!! Thank you!!