1 /*
2 * Copyright (C) 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 #include <limits>
18 #include <sstream>
19 #include <string>
20 #include <vector>
21
22 #include <android-base/logging.h>
23 #include <android-base/properties.h>
24 #include <android-base/strings.h>
25 #include <android/lpdump/BnLpdump.h>
26 #include <android/lpdump/ILpdump.h>
27 #include <binder/IPCThreadState.h>
28 #include <binder/IServiceManager.h>
29 #include <binder/ProcessState.h>
30 #include <libsnapshot/snapshot.h>
31
32 int LpdumpMain(int argc, char* argv[], std::ostream&, std::ostream&);
33
34 namespace android {
35 namespace lpdump {
36
37 using binder::Status;
38
39 class Lpdump : public BnLpdump {
40 public:
41 Lpdump() = default;
42 virtual ~Lpdump() = default;
43
run(const std::vector<std::string> & args,std::string * aidl_return)44 Status run(const std::vector<std::string>& args, std::string* aidl_return) override {
45 std::vector<char*> local_argv;
46 std::vector<std::string> local_args = args;
47 for (auto& arg : local_args) {
48 local_argv.push_back(arg.data());
49 }
50 LOG(DEBUG) << "Dumping with args: " << base::Join(args, " ");
51 std::stringstream output;
52 std::stringstream error;
53 int ret = LpdumpMain((int)local_argv.size(), local_argv.data(), output, error);
54 std::string error_str = error.str();
55 if (ret != 0) {
56 return Status::fromServiceSpecificError(ret, error_str.c_str());
57 }
58
59 if (android::base::GetBoolProperty("ro.virtual_ab.enabled", false)) {
60 if (auto sm = android::snapshot::SnapshotManager::New()) {
61 output << "---------------\n";
62 output << "Snapshot state:\n";
63 output << "---------------\n";
64 sm->Dump(output);
65 }
66 }
67
68 if (!error_str.empty()) {
69 LOG(WARNING) << error_str;
70 }
71 *aidl_return = output.str();
72 return Status::ok();
73 }
74 };
75
76 } // namespace lpdump
77 } // namespace android
78
main(int,char **)79 int main(int, char**) {
80 using namespace android;
81
82 sp<lpdump::Lpdump> service = new lpdump::Lpdump();
83 defaultServiceManager()->addService(String16("lpdump_service"), service);
84 LOG(VERBOSE) << "lpdumpd starting";
85 ProcessState::self()->startThreadPool();
86 IPCThreadState::self()->joinThreadPool();
87 return 0;
88 }
89