|
| 1 | +--- |
| 2 | +id: debugging |
| 3 | +title: Debugging |
| 4 | +sidebar_label: Debugging |
| 5 | +--- |
| 6 | + |
| 7 | +Some hints on how to debug Glean. |
| 8 | + |
| 9 | +## Logging and debug output |
| 10 | + |
| 11 | +### Glog |
| 12 | + |
| 13 | +Glean uses [glog](https://github.com/google/glog) for a lot of its |
| 14 | +logging both in C++ and Haskell. The Haskell API is provided by the |
| 15 | +`Util.Log` library in the `fb-util` package |
| 16 | +(`hsthrift/common/util`). |
| 17 | + |
| 18 | +Glog has various logging levels, INFO, WARN, ERROR, etc., |
| 19 | +corresponding to `logInfo`, `logWarn` and so on in `Util.Log`, and |
| 20 | +also verbose logging via `vlog`. |
| 21 | + |
| 22 | +By default, verbose logs are suppressed but all other logs are shown, |
| 23 | +except in `glean shell` where we suppress `logInfo` to avoid logs |
| 24 | +interfering with the interactive shell. To see more logs there are |
| 25 | +various ways. The most foolproof is with an environment variable. To |
| 26 | +see verbose logs up to level 3 for example: |
| 27 | + |
| 28 | +``` |
| 29 | +GLOG_v=3 glean-server ... |
| 30 | +``` |
| 31 | + |
| 32 | +The shell is a bit different, because it adds an implicit |
| 33 | +`--minloglevel=2` argument which overrides the environment |
| 34 | +variable. So for the shell we must use the command-line `-v`: |
| 35 | + |
| 36 | +``` |
| 37 | +glean -v 3 shell ... |
| 38 | +``` |
| 39 | + |
| 40 | +Glog is sometimes a bit spammy because you'll see logs from irrelevant |
| 41 | +library dependencies. For C++ you can limit the logs you see to just |
| 42 | +certain files. For example, to turn on verbose logging at level 3 for |
| 43 | +just `query.cpp`: |
| 44 | + |
| 45 | +``` |
| 46 | +GLOG_vmodule=query=3 glean-server ... |
| 47 | +``` |
| 48 | + |
| 49 | +This useful functionality unfortunately doesn't work for logs in |
| 50 | +Haskell code. |
| 51 | + |
| 52 | +### Glean debug logging |
| 53 | + |
| 54 | +Glean also has its own debug logs which are enabled via environment |
| 55 | +variables or command-line options. There are currently two of these: |
| 56 | + |
| 57 | +* `GLEAN_DEBUG=query` or `--debug-query`: enable logging from the query compiler |
| 58 | +* `GLEAN_DEBUG=tc` or `--debug-tc`: enable logging from the Angle type checker |
| 59 | + |
| 60 | +## Debugging the C++ code |
| 61 | + |
| 62 | +We recommend [Address |
| 63 | +Sanitizer](https://github.com/google/sanitizers/wiki/addresssanitizer) |
| 64 | +as a first port of call for debugging anything in C++. Address |
| 65 | +Sanitizer will detect most kinds of memory errors and space leaks. |
| 66 | + |
| 67 | +To make it easy to enable this we've provided a `cabal` flag: |
| 68 | + |
| 69 | +``` |
| 70 | +cabal build glean-server -fasan |
| 71 | +``` |
| 72 | + |
| 73 | +(Note you might need to `cabal clean` first, or selectively remove |
| 74 | +some build files, because Cabal isn't very good at rebuilding C++ code |
| 75 | +when it needs to. You'll need to clean again if you turn off `asan`.) |
| 76 | + |
| 77 | +## Profiling C++ |
| 78 | + |
| 79 | +`perf` is a pretty good way to identify hotspots quickly. |
| 80 | + |
| 81 | +``` |
| 82 | +perf record -g glean-server ... |
| 83 | +perf report |
| 84 | +``` |
0 commit comments