1 /*
2 * Copyright 2019 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 #define LOG_TAG "bt_shim_storage"
18
19 #include "main/shim/dumpsys.h"
20
21 #include <com_android_bluetooth_flags.h>
22
23 #include <unordered_map>
24
25 #include "main/shim/entry.h"
26 #include "main/shim/shim.h"
27 #include "main/shim/stack.h"
28 #include "shim/dumpsys.h"
29
30 namespace {
31
32 constexpr char kModuleName[] = "shim::legacy::dumpsys";
33 static std::unordered_map<const void*, bluetooth::shim::DumpsysFunction>
34 dumpsys_functions_;
35
36 } // namespace
37
RegisterDumpsysFunction(const void * token,DumpsysFunction func)38 void bluetooth::shim::RegisterDumpsysFunction(const void* token,
39 DumpsysFunction func) {
40 log::assert_that(dumpsys_functions_.find(token) == dumpsys_functions_.end(),
41 "assert failed: dumpsys_functions_.find(token) == "
42 "dumpsys_functions_.end()");
43 dumpsys_functions_.insert({token, func});
44 }
45
UnregisterDumpsysFunction(const void * token)46 void bluetooth::shim::UnregisterDumpsysFunction(const void* token) {
47 log::assert_that(dumpsys_functions_.find(token) != dumpsys_functions_.end(),
48 "assert failed: dumpsys_functions_.find(token) != "
49 "dumpsys_functions_.end()");
50 dumpsys_functions_.erase(token);
51 }
52
Dump(int fd,const char ** args)53 void bluetooth::shim::Dump(int fd, const char** args) {
54 if (dumpsys_functions_.empty()) {
55 dprintf(fd, "%s No registered dumpsys shim legacy targets\n", kModuleName);
56 } else {
57 dprintf(fd, "%s Dumping shim legacy targets:%zd\n", kModuleName,
58 dumpsys_functions_.size());
59 for (auto& dumpsys : dumpsys_functions_) {
60 dumpsys.second(fd);
61 }
62 }
63 if (com::android::bluetooth::flags::dumpsys_acquire_stack_when_executing()) {
64 std::promise<void> promise;
65 std::future future = promise.get_future();
66 if (bluetooth::shim::Stack::GetInstance()->CallOnModule<shim::Dumpsys>(
67 [&promise, fd, args](shim::Dumpsys* mod) {
68 mod->Dump(fd, args, std::move(promise));
69 })) {
70 log::assert_that(
71 future.wait_for(std::chrono::seconds(1)) == std::future_status::ready,
72 "Timed out waiting for dumpsys to complete");
73 } else {
74 dprintf(fd, "%s NOTE: gd dumpsys module not loaded or started\n",
75 kModuleName);
76 }
77 } else { // !FLAG(dumpsys_acquire_stack_when_executing)
78 bluetooth::shim::Stack::GetInstance()->LockForDumpsys([=]() {
79 if (bluetooth::shim::is_gd_stack_started_up()) {
80 if (bluetooth::shim::is_gd_dumpsys_module_started()) {
81 bluetooth::shim::GetDumpsys()->Dump(fd, args);
82 } else {
83 dprintf(fd, "%s NOTE: gd dumpsys module not loaded or started\n",
84 kModuleName);
85 }
86 } else {
87 dprintf(fd, "%s gd stack is enabled but not started\n", kModuleName);
88 }
89 });
90 }
91 }
92