blob: 40c3b9a25eba3190d5553fea136b4bb1b1fe818f [file] [log] [blame]
Ken Chen76ee6102022-01-15 01:11:53 +08001/*
2 * Copyright (C) 2022 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 "nettestutils/DumpService.h"
18
19#include <android-base/file.h>
Lorenzo Colitti49e4a982023-05-23 18:41:39 +090020#include <android/binder_status.h>
Ken Chen76ee6102022-01-15 01:11:53 +080021
22#include <sstream>
23#include <thread>
24
Lorenzo Colitti49e4a982023-05-23 18:41:39 +090025// Version for code using libbinder (e.g., AIDL interfaces with the C++ backend).
Ken Chen76ee6102022-01-15 01:11:53 +080026android::status_t dumpService(const android::sp<android::IBinder>& binder,
27 const std::vector<std::string>& args,
28 std::vector<std::string>& outputLines) {
29 if (!outputLines.empty()) return -EUCLEAN;
30
31 android::base::unique_fd localFd, remoteFd;
32 if (!Pipe(&localFd, &remoteFd)) return -errno;
33
34 android::Vector<android::String16> str16Args;
35 for (const auto& arg : args) {
36 str16Args.push(android::String16(arg.c_str()));
37 }
38 android::status_t ret;
39 // dump() blocks until another thread has consumed all its output.
40 std::thread dumpThread =
41 std::thread([&ret, binder, remoteFd{std::move(remoteFd)}, str16Args]() {
42 ret = binder->dump(remoteFd, str16Args);
43 });
44
45 std::string dumpContent;
46 if (!android::base::ReadFdToString(localFd.get(), &dumpContent)) {
47 return -errno;
48 }
49 dumpThread.join();
50 if (ret != android::OK) return ret;
51
52 std::stringstream dumpStream(std::move(dumpContent));
53 std::string line;
54 while (std::getline(dumpStream, line)) {
55 outputLines.push_back(line);
56 }
57
58 return android::OK;
59}
Lorenzo Colitti49e4a982023-05-23 18:41:39 +090060
61// Version for code using libbinder_ndk (e.g., AIDL interfaces with the NDK backend)..
62android::status_t dumpService(const ndk::SpAIBinder& binder,
63 const char** args,
64 uint32_t num_args,
65 std::vector<std::string>& outputLines) {
66 if (!outputLines.empty()) return -EUCLEAN;
67
68 android::base::unique_fd localFd, remoteFd;
69 if (!Pipe(&localFd, &remoteFd)) return -errno;
70
71 android::status_t ret;
72 // dump() blocks until another thread has consumed all its output.
73 std::thread dumpThread =
74 std::thread([&ret, binder, remoteFd{std::move(remoteFd)}, args, num_args]() {
75 ret = AIBinder_dump(binder.get(), remoteFd, args, num_args);
76 });
77
78 std::string dumpContent;
79 if (!android::base::ReadFdToString(localFd.get(), &dumpContent)) {
80 return -errno;
81 }
82 dumpThread.join();
83 if (ret != android::OK) return ret;
84
85 std::stringstream dumpStream(dumpContent);
86 std::string line;
87 while (std::getline(dumpStream, line)) {
88 outputLines.push_back(std::move(line));
89 }
90
91 return android::OK;
92}