| Brian Duddie | 7f23a87 | 2020-07-01 14:27:31 -0700 | [diff] [blame] | 1 | # Debugging the CHRE Framework |
| 2 | |
| 3 | [TOC] |
| 4 | |
| 5 | This section lists the methods that can be used to aid in CHRE framework |
| 6 | debugging. |
| 7 | |
| 8 | ## Logging |
| 9 | |
| 10 | The CHRE framework invokes the `LOGx()` macros defined in |
| 11 | `platform/include/chre/platform/log.h` to log information into the system, |
| 12 | for printf-style debugging. This capability is also exposed to nanoapps via |
| 13 | `chreLog()`. Although it may not be strictly required, it is strongly |
| 14 | recommended that the platform implementation directs these log messages to |
| 15 | Android’s logcat system under the “CHRE” tag, where they will be automatically |
| 16 | collected in bug reports. These logs must not wake the applications processor |
| 17 | (AP), so they should be buffered on a best-effort basis and flushed to the AP |
| 18 | opportunistically when it wakes up. |
| 19 | |
| 20 | ### Log levels |
| 21 | |
| 22 | CHRE framework currently supports four levels, namely Error `LOGE()`, Warning |
| 23 | ` LOGW()`, Info `LOGI()` and Debug `LOGD()`. These correspond to the |
| 24 | equivalent [logcat |
| 25 | levels](https://source.android.com/setup/contribute/code-style#log-sparingly). |
| 26 | Choosing an appropriate level for logs, and logging only the necessary |
| 27 | information to identify and debug failures can help avoid issues with “log |
| 28 | spam”, such as log output that is difficult to read, or uninteresting “spammy” |
| 29 | logs causing useful log messages to be pushed out of limited buffering space. |
| 30 | |
| 31 | ### Log level filtering |
| 32 | |
| 33 | The CHRE framework currently only supports compile-time log level filtering. |
| 34 | While it’s recommended to leave it at the default setting, the |
| 35 | `CHRE_MINIMUM_LOG_LEVEL` build flag can be defined to one of the values set |
| 36 | in `util/include/chre/util/log_common.h` to control which log levels are |
| 37 | included in the binary. |
| 38 | |
| 39 | ## Debug Dumps |
| 40 | |
| 41 | A debug dump is human-readable text produced on-demand for debugging purposes. |
| 42 | While `LOGx()` is useful for logging events as they happen, the debug dump is |
| 43 | a complementary function typically used to output a snapshot of the framework's |
| 44 | state, history, vital statistics, etc. The debug dump is especially useful for |
| 45 | information that would be too spammy to log on every change, but is useful to |
| 46 | diagnose potential issues. The CHRE framework produces a debug dump when |
| 47 | requested via the Context Hub HAL’s built-in `debug()` method, which is |
| 48 | automatically collected as part of the bug report process, and can be manually |
| 49 | triggered via ADB using the following command: |
| 50 | |
| Arthur Ishiguro | e323f2a | 2021-09-24 08:45:37 -0700 | [diff] [blame] | 51 | (HIDL HAL) |
| Brian Duddie | 7f23a87 | 2020-07-01 14:27:31 -0700 | [diff] [blame] | 52 | `adb root && adb shell lshal debug android.hardware.contexthub@1.0::IContexthub/default` |
| 53 | |
| Arthur Ishiguro | e323f2a | 2021-09-24 08:45:37 -0700 | [diff] [blame] | 54 | (AIDL HAL) |
| 55 | `adb root && adb shell dumpsys android.hardware.contexthub.IContextHub/default` |
| 56 | |
| Brian Duddie | 7f23a87 | 2020-07-01 14:27:31 -0700 | [diff] [blame] | 57 | `DebugDumpManager` is the framework module responsible for collecting debug |
| 58 | dumps from the CHRE framework and nanoapps. Refer to the associated |
| 59 | documentation in the source code for details on this process. |
| 60 | |
| 61 | ## CHRE_ASSERT and CHRE_ASSERT_LOG |
| 62 | |
| 63 | `CHRE_ASSERT()` and `CHRE_ASSERT_LOG()` can be used to help catch |
| 64 | programmatic errors during development. However, since their use may have |
| 65 | memory impact, they can be disabled by setting `CHRE_ASSERTIONS_ENABLED` to |
| 66 | false in the Makefile. In general, assertions should be used sparingly - they |
| 67 | are best applied to situations that would lead to potentially unrecoverable |
| 68 | logical errors that are not handled by the code. For comparison, asserting that |
| 69 | a pointer is not null is generally an anti-pattern (though the current CHRE |
| 70 | codebase is not free of this), as dereferencing null would produce a crash |
| 71 | anyways which should have equivalent debuggability as an assertion, among other |
| 72 | reasons. |