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