1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "chre/platform/platform_debug_dump_manager.h"
18
19 #include "chre/core/event_loop_manager.h"
20 #include "chre/platform/log.h"
21 #include "chre/target_platform/host_link_base.h"
22 #include "chre/target_platform/platform_debug_dump_manager_base.h"
23 #include "chre/util/macros.h"
24
25 #ifdef CHPP_DEBUG_DUMP_ENABLED
26 #include "chpp/platform/chpp_init.h"
27 #endif // CHPP_DEBUG_DUMP_ENABLED
28
29 #ifdef CHRE_ENABLE_ASH_DEBUG_DUMP
30 #include "ash/debug.h"
31 #else // CHRE_ENABLE_ASH_DEBUG_DUMP
32 #include <cstring>
33 #endif // CHRE_ENABLE_ASH_DEBUG_DUMP
34
35 namespace chre {
36 namespace {
37
38 #ifdef CHRE_ENABLE_ASH_DEBUG_DUMP
onDebugDumpTriggered(void *,uint32_t handle)39 void onDebugDumpTriggered(void * /*cookie*/, uint32_t handle) {
40 auto &debugDumpManager =
41 EventLoopManagerSingleton::get()->getDebugDumpManager();
42
43 debugDumpManager.setHandle(handle);
44 debugDumpManager.trigger();
45 }
46
debugDumpReadyCb(void *,const char * debugStr,size_t debugStrSize,bool complete)47 void debugDumpReadyCb(void * /*cookie*/, const char *debugStr,
48 size_t debugStrSize, bool complete) {
49 EventLoopManagerSingleton::get()->getDebugDumpManager().sendDebugDumpResult(
50 debugStr, debugStrSize, complete);
51 }
52 #endif // CHRE_ENABLE_ASH_DEBUG_DUMP
53
54 } // namespace
55
sendDebugDump(const char * debugStr,bool complete)56 void PlatformDebugDumpManager::sendDebugDump(const char *debugStr,
57 bool complete) {
58 // DDM is guaranteed to call complete=true at the end of a debug dump session.
59 // However, sendDebugDumpResult may not get called with complete=true, for
60 // example when ASH times out. Therefore, mDataCount has to be reset here
61 // instead of in sendDebugDumpResult(), to properly start the next debug dump
62 // session.
63 if (mComplete) {
64 mDataCount = 0;
65 }
66 mComplete = complete;
67
68 #ifdef CHRE_ENABLE_ASH_DEBUG_DUMP
69 ashCommitDebugDump(mHandle, debugStr, complete);
70 #else // CHRE_ENABLE_ASH_DEBUG_DUMP
71 sendDebugDumpResult(debugStr, strlen(debugStr), complete);
72 #endif // CHRE_ENABLE_ASH_DEBUG_DUMP
73 }
74
logStateToBuffer(DebugDumpWrapper & debugDump)75 void PlatformDebugDumpManager::logStateToBuffer(DebugDumpWrapper &debugDump) {
76 #ifdef CHPP_DEBUG_DUMP_ENABLED
77 chpp::logStateToBuffer(debugDump);
78 #else
79 UNUSED_VAR(debugDump);
80 #endif // CHPP_DEBUG_DUMP_ENABLED
81 }
82
PlatformDebugDumpManagerBase()83 PlatformDebugDumpManagerBase::PlatformDebugDumpManagerBase() {
84 #ifdef CHRE_ENABLE_ASH_DEBUG_DUMP
85 if (!ashRegisterDebugDumpCallback("CHRE", onDebugDumpTriggered,
86 nullptr /* cookie */)) {
87 LOGE("Failed to register ASH debug dump callback");
88 }
89 #endif // CHRE_ENABLE_ASH_DEBUG_DUMP
90 }
91
~PlatformDebugDumpManagerBase()92 PlatformDebugDumpManagerBase::~PlatformDebugDumpManagerBase() {
93 #ifdef CHRE_ENABLE_ASH_DEBUG_DUMP
94 ashUnregisterDebugDumpCallback(onDebugDumpTriggered);
95 #endif // CHRE_ENABLE_ASH_DEBUG_DUMP
96 }
97
onDebugDumpRequested(uint16_t hostClientId)98 bool PlatformDebugDumpManagerBase::onDebugDumpRequested(uint16_t hostClientId) {
99 mHostClientId = hostClientId;
100
101 #ifdef CHRE_ENABLE_ASH_DEBUG_DUMP
102 return ashTriggerDebugDump(debugDumpReadyCb, nullptr /*cookie*/);
103 #else // CHRE_ENABLE_ASH_DEBUG_DUMP
104 EventLoopManagerSingleton::get()->getDebugDumpManager().trigger();
105 return true;
106 #endif // CHRE_ENABLE_ASH_DEBUG_DUMP
107 }
108
sendDebugDumpResult(const char * debugStr,size_t debugStrSize,bool complete)109 void PlatformDebugDumpManagerBase::sendDebugDumpResult(const char *debugStr,
110 size_t debugStrSize,
111 bool complete) {
112 if (debugStrSize > 0) {
113 mDataCount++;
114 }
115 sendDebugDumpResultToHost(mHostClientId, debugStr, debugStrSize, complete,
116 mDataCount);
117 }
118
119 } // namespace chre
120