blob: 1753343debba02e6f82a2065524c4ac1cf2f3641 [file] [log] [blame]
Yifan Hongd4a77e82017-09-06 19:40:24 -07001/*
2 * Copyright (C) 2017 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#define LOG_TAG "lshal"
17#include <android-base/logging.h>
18
Yifan Hongbdf44f82018-05-25 14:20:00 -070019#include <map>
20
Steven Morelandb4d6c572021-07-29 12:17:25 -070021#include <android-base/hex.h>
Yifan Hongbdf44f82018-05-25 14:20:00 -070022#include <android-base/strings.h>
Yifan Hongfee209d2017-09-14 18:23:38 -070023#include <hidl-hash/Hash.h>
Yifan Hong8304e412018-05-25 15:05:36 -070024#include <vintf/parse_string.h>
Yifan Hongfee209d2017-09-14 18:23:38 -070025
Yifan Hongd4a77e82017-09-06 19:40:24 -070026#include "TableEntry.h"
27
28#include "TextTable.h"
29#include "utils.h"
30
31namespace android {
32namespace lshal {
33
Yifan Hong0ad64f52018-05-25 15:29:17 -070034static const std::string &getArchString(vintf::Arch arch) {
Yifan Hongd4a77e82017-09-06 19:40:24 -070035 static const std::string sStr64 = "64";
36 static const std::string sStr32 = "32";
37 static const std::string sStrBoth = "32+64";
Yifan Hong430f8982018-05-25 17:28:39 -070038 static const std::string sStrUnknown = "?";
Yifan Hongd4a77e82017-09-06 19:40:24 -070039 switch (arch) {
Yifan Hong0ad64f52018-05-25 15:29:17 -070040 case vintf::Arch::ARCH_64:
Yifan Hongd4a77e82017-09-06 19:40:24 -070041 return sStr64;
Yifan Hong0ad64f52018-05-25 15:29:17 -070042 case vintf::Arch::ARCH_32:
Yifan Hongd4a77e82017-09-06 19:40:24 -070043 return sStr32;
Yifan Hong0ad64f52018-05-25 15:29:17 -070044 case vintf::Arch::ARCH_32_64:
Yifan Hongd4a77e82017-09-06 19:40:24 -070045 return sStrBoth;
Yifan Hong0ad64f52018-05-25 15:29:17 -070046 case vintf::Arch::ARCH_EMPTY: // fall through
Yifan Hongd4a77e82017-09-06 19:40:24 -070047 default:
48 return sStrUnknown;
49 }
50}
51
52static std::string getTitle(TableColumnType type) {
53 switch (type) {
Yifan Hongd43d7052017-09-14 11:16:12 -070054 case TableColumnType::INTERFACE_NAME: return "Interface";
55 case TableColumnType::TRANSPORT: return "Transport";
56 case TableColumnType::SERVER_PID: return "Server";
57 case TableColumnType::SERVER_CMD: return "Server CMD";
58 case TableColumnType::SERVER_ADDR: return "PTR";
59 case TableColumnType::CLIENT_PIDS: return "Clients";
60 case TableColumnType::CLIENT_CMDS: return "Clients CMD";
61 case TableColumnType::ARCH: return "Arch";
62 case TableColumnType::THREADS: return "Thread Use";
Yifan Hongfee209d2017-09-14 18:23:38 -070063 case TableColumnType::RELEASED: return "R";
64 case TableColumnType::HASH: return "Hash";
Yifan Hongbdf44f82018-05-25 14:20:00 -070065 case TableColumnType::VINTF: return "VINTF";
Yifan Hong13ba0a92018-06-25 16:15:56 -070066 case TableColumnType::SERVICE_STATUS: return "Status";
Yifan Hongd43d7052017-09-14 11:16:12 -070067 default:
Yifan Hongfee209d2017-09-14 18:23:38 -070068 LOG(FATAL) << __func__ << "Should not reach here. " << static_cast<int>(type);
Yifan Hongd4a77e82017-09-06 19:40:24 -070069 return "";
Yifan Hongd4a77e82017-09-06 19:40:24 -070070 }
71}
72
73std::string TableEntry::getField(TableColumnType type) const {
74 switch (type) {
Yifan Hongd43d7052017-09-14 11:16:12 -070075 case TableColumnType::INTERFACE_NAME:
Yifan Hongd4a77e82017-09-06 19:40:24 -070076 return interfaceName;
Yifan Hongd43d7052017-09-14 11:16:12 -070077 case TableColumnType::TRANSPORT:
Yifan Hong8304e412018-05-25 15:05:36 -070078 return vintf::to_string(transport);
Yifan Hongd43d7052017-09-14 11:16:12 -070079 case TableColumnType::SERVER_PID:
Yifan Hongd4a77e82017-09-06 19:40:24 -070080 return serverPid == NO_PID ? "N/A" : std::to_string(serverPid);
Yifan Hongd43d7052017-09-14 11:16:12 -070081 case TableColumnType::SERVER_CMD:
Yifan Hongd4a77e82017-09-06 19:40:24 -070082 return serverCmdline;
Yifan Hongd43d7052017-09-14 11:16:12 -070083 case TableColumnType::SERVER_ADDR:
Yifan Hongd4a77e82017-09-06 19:40:24 -070084 return serverObjectAddress == NO_PTR ? "N/A" : toHexString(serverObjectAddress);
Yifan Hongd43d7052017-09-14 11:16:12 -070085 case TableColumnType::CLIENT_PIDS:
Yifan Hongd4a77e82017-09-06 19:40:24 -070086 return join(clientPids, " ");
Yifan Hongd43d7052017-09-14 11:16:12 -070087 case TableColumnType::CLIENT_CMDS:
Yifan Hongd4a77e82017-09-06 19:40:24 -070088 return join(clientCmdlines, ";");
Yifan Hongd43d7052017-09-14 11:16:12 -070089 case TableColumnType::ARCH:
Yifan Hongd4a77e82017-09-06 19:40:24 -070090 return getArchString(arch);
Yifan Hongd43d7052017-09-14 11:16:12 -070091 case TableColumnType::THREADS:
Yifan Hongd4a77e82017-09-06 19:40:24 -070092 return getThreadUsage();
Yifan Hongfee209d2017-09-14 18:23:38 -070093 case TableColumnType::RELEASED:
94 return isReleased();
95 case TableColumnType::HASH:
96 return hash;
Yifan Hongbdf44f82018-05-25 14:20:00 -070097 case TableColumnType::VINTF:
98 return getVintfInfo();
Yifan Hong13ba0a92018-06-25 16:15:56 -070099 case TableColumnType::SERVICE_STATUS:
100 return lshal::to_string(serviceStatus);
Yifan Hongd43d7052017-09-14 11:16:12 -0700101 default:
Yifan Hongfee209d2017-09-14 18:23:38 -0700102 LOG(FATAL) << __func__ << "Should not reach here. " << static_cast<int>(type);
Yifan Hongd4a77e82017-09-06 19:40:24 -0700103 return "";
Yifan Hongd4a77e82017-09-06 19:40:24 -0700104 }
105}
106
Yifan Hongfee209d2017-09-14 18:23:38 -0700107std::string TableEntry::isReleased() const {
Steven Morelandb4d6c572021-07-29 12:17:25 -0700108 static const std::string unreleased = android::base::HexString(Hash::kEmptyHash.data(),
109 Hash::kEmptyHash.size());
Yifan Hongfee209d2017-09-14 18:23:38 -0700110
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700111 if (hash.empty()) {
112 return "?";
113 }
114 if (hash == unreleased) {
115 return "N"; // unknown or unreleased
Yifan Hongfee209d2017-09-14 18:23:38 -0700116 }
117 return "Y"; // released
118}
119
Yifan Hongbdf44f82018-05-25 14:20:00 -0700120std::string TableEntry::getVintfInfo() const {
121 static const std::map<VintfInfo, std::string> values{
122 {DEVICE_MANIFEST, "DM"},
123 {DEVICE_MATRIX, "DC"},
124 {FRAMEWORK_MANIFEST, "FM"},
125 {FRAMEWORK_MATRIX, "FC"},
126 };
127 std::vector<std::string> ret;
128 for (const auto& pair : values) {
129 if (vintfInfo & pair.first) {
130 ret.push_back(pair.second);
131 }
132 }
133 auto joined = base::Join(ret, ',');
134 return joined.empty() ? "X" : joined;
135}
136
Yifan Hong13ba0a92018-06-25 16:15:56 -0700137std::string to_string(ServiceStatus s) {
138 switch (s) {
139 case ServiceStatus::ALIVE: return "alive";
140 case ServiceStatus::NON_RESPONSIVE: return "non-responsive";
141 case ServiceStatus::DECLARED: return "declared";
142 case ServiceStatus::UNKNOWN: return "N/A";
143 }
144
145 LOG(FATAL) << __func__ << "Should not reach here." << static_cast<int>(s);
146 return "";
147}
148
Yifan Hongd4a77e82017-09-06 19:40:24 -0700149TextTable Table::createTextTable(bool neat,
150 const std::function<std::string(const std::string&)>& emitDebugInfo) const {
151
152 TextTable textTable;
153 std::vector<std::string> row;
154 if (!neat) {
155 textTable.add(mDescription);
156
157 row.clear();
158 for (TableColumnType type : mSelectedColumns) {
159 row.push_back(getTitle(type));
160 }
161 textTable.add(std::move(row));
162 }
163
164 for (const auto& entry : mEntries) {
165 row.clear();
166 for (TableColumnType type : mSelectedColumns) {
167 row.push_back(entry.getField(type));
168 }
169 textTable.add(std::move(row));
170
171 if (emitDebugInfo) {
172 std::string debugInfo = emitDebugInfo(entry.interfaceName);
173 if (!debugInfo.empty()) textTable.add(debugInfo);
174 }
175 }
176 return textTable;
177}
178
179TextTable MergedTable::createTextTable() {
180 TextTable textTable;
181 for (const Table* table : mTables) {
182 textTable.addAll(table->createTextTable());
183 }
184 return textTable;
185}
186
Yifan Hong8bf73162017-09-07 18:06:13 -0700187bool TableEntry::operator==(const TableEntry& other) const {
188 if (this == &other) {
189 return true;
190 }
191 return interfaceName == other.interfaceName && transport == other.transport &&
192 serverPid == other.serverPid && threadUsage == other.threadUsage &&
193 threadCount == other.threadCount && serverCmdline == other.serverCmdline &&
194 serverObjectAddress == other.serverObjectAddress && clientPids == other.clientPids &&
195 clientCmdlines == other.clientCmdlines && arch == other.arch;
196}
197
198std::string TableEntry::to_string() const {
Yifan Hong8304e412018-05-25 15:05:36 -0700199 using vintf::operator<<;
Yifan Hong8bf73162017-09-07 18:06:13 -0700200 std::stringstream ss;
201 ss << "name=" << interfaceName << ";transport=" << transport << ";thread=" << getThreadUsage()
202 << ";server=" << serverPid
203 << "(" << serverObjectAddress << ";" << serverCmdline << ");clients=["
204 << join(clientPids, ";") << "](" << join(clientCmdlines, ";") << ");arch="
205 << getArchString(arch);
206 return ss.str();
207
208}
209
Yifan Hongd4a77e82017-09-06 19:40:24 -0700210} // namespace lshal
211} // namespace android