1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "bt_gd_shim"
#include "dumpsys/dumpsys.h"
#include <future>
#include <string>
#include "dumpsys/filter.h"
#include "module.h"
#include "os/log.h"
#include "os/system_properties.h"
#include "shim/dumpsys.h"
#include "shim/dumpsys_args.h"
namespace bluetooth {
namespace shim {
static const std::string kReadOnlyDebuggableProperty = "ro.debuggable";
namespace {
constexpr char kModuleName[] = "shim::Dumpsys";
constexpr char kDumpsysTitle[] = "----- Gd Dumpsys ------";
} // namespace
struct Dumpsys::impl {
public:
void DumpWithArgsSync(int fd, const char** args, std::promise<void> promise);
int GetNumberOfBundledSchemas() const;
impl(const Dumpsys& dumpsys_module, const dumpsys::ReflectionSchema& reflection_schema);
~impl() = default;
protected:
void FilterAsUser(std::string* dumpsys_data);
void FilterAsDeveloper(std::string* dumpsys_data);
std::string PrintAsJson(std::string* dumpsys_data) const;
bool IsDebuggable() const;
private:
void DumpWithArgsAsync(int fd, const char** args);
const Dumpsys& dumpsys_module_;
const dumpsys::ReflectionSchema reflection_schema_;
};
const ModuleFactory Dumpsys::Factory =
ModuleFactory([]() { return new Dumpsys(bluetooth::dumpsys::GetBundledSchemaData()); });
Dumpsys::impl::impl(const Dumpsys& dumpsys_module, const dumpsys::ReflectionSchema& reflection_schema)
: dumpsys_module_(dumpsys_module), reflection_schema_(std::move(reflection_schema)) {}
int Dumpsys::impl::GetNumberOfBundledSchemas() const {
return reflection_schema_.GetNumberOfBundledSchemas();
}
bool Dumpsys::impl::IsDebuggable() const {
return (os::GetSystemProperty(kReadOnlyDebuggableProperty) == "1");
}
void Dumpsys::impl::FilterAsDeveloper(std::string* dumpsys_data) {
ASSERT(dumpsys_data != nullptr);
dumpsys::FilterInPlace(dumpsys::FilterType::AS_DEVELOPER, reflection_schema_, dumpsys_data);
}
void Dumpsys::impl::FilterAsUser(std::string* dumpsys_data) {
ASSERT(dumpsys_data != nullptr);
dumpsys::FilterInPlace(dumpsys::FilterType::AS_USER, reflection_schema_, dumpsys_data);
}
std::string Dumpsys::impl::PrintAsJson(std::string* dumpsys_data) const {
ASSERT(dumpsys_data != nullptr);
const std::string root_name = reflection_schema_.GetRootName();
if (root_name.empty()) {
char buf[255];
snprintf(buf, sizeof(buf), "ERROR: Unable to find root name in prebundled reflection schema\n");
LOG_WARN("%s", buf);
return std::string(buf);
}
const reflection::Schema* schema = reflection_schema_.FindInReflectionSchema(root_name);
if (schema == nullptr) {
char buf[255];
snprintf(buf, sizeof(buf), "ERROR: Unable to find schema root name:%s\n", root_name.c_str());
LOG_WARN("%s", buf);
return std::string(buf);
}
flatbuffers::Parser parser;
if (!parser.Deserialize(schema)) {
char buf[255];
snprintf(buf, sizeof(buf), "ERROR: Unable to deserialize bundle root name:%s\n", root_name.c_str());
LOG_WARN("%s", buf);
return std::string(buf);
}
std::string jsongen;
flatbuffers::GenerateText(parser, dumpsys_data->data(), &jsongen);
return jsongen;
}
void Dumpsys::impl::DumpWithArgsAsync(int fd, const char** args) {
ParsedDumpsysArgs parsed_dumpsys_args(args);
const auto registry = dumpsys_module_.GetModuleRegistry();
ModuleDumper dumper(*registry, kDumpsysTitle);
std::string dumpsys_data;
dumper.DumpState(&dumpsys_data);
dprintf(fd, " ----- Filtering as Developer -----\n");
FilterAsDeveloper(&dumpsys_data);
dprintf(fd, "%s", PrintAsJson(&dumpsys_data).c_str());
}
void Dumpsys::impl::DumpWithArgsSync(int fd, const char** args, std::promise<void> promise) {
DumpWithArgsAsync(fd, args);
promise.set_value();
}
Dumpsys::Dumpsys(const std::string& pre_bundled_schema)
: reflection_schema_(dumpsys::ReflectionSchema(pre_bundled_schema)) {}
void Dumpsys::Dump(int fd, const char** args) {
if (fd <= 0) {
return;
}
std::promise<void> promise;
auto future = promise.get_future();
CallOn(pimpl_.get(), &Dumpsys::impl::DumpWithArgsSync, fd, args, std::move(promise));
future.get();
}
void Dumpsys::Dump(int fd, const char** args, std::promise<void> promise) {
if (fd <= 0) {
promise.set_value();
return;
}
CallOn(pimpl_.get(), &Dumpsys::impl::DumpWithArgsSync, fd, args, std::move(promise));
}
os::Handler* Dumpsys::GetGdShimHandler() {
return GetHandler();
}
/**
* Module methods
*/
void Dumpsys::ListDependencies(ModuleList* list) const {}
void Dumpsys::Start() {
pimpl_ = std::make_unique<impl>(*this, reflection_schema_);
}
void Dumpsys::Stop() {
pimpl_.reset();
}
DumpsysDataFinisher Dumpsys::GetDumpsysData(flatbuffers::FlatBufferBuilder* fb_builder) const {
auto name = fb_builder->CreateString("----- Shim Dumpsys -----");
DumpsysModuleDataBuilder builder(*fb_builder);
builder.add_title(name);
builder.add_number_of_bundled_schemas(pimpl_->GetNumberOfBundledSchemas());
auto dumpsys_data = builder.Finish();
return [dumpsys_data](DumpsysDataBuilder* builder) { builder->add_shim_dumpsys_data(dumpsys_data); };
}
std::string Dumpsys::ToString() const {
return kModuleName;
}
} // namespace shim
} // namespace bluetooth
|