blob: 8e21975efa94fe6961f6635c7921ac411f96e84a [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
21#include <android-base/strings.h>
Yifan Hongfee209d2017-09-14 18:23:38 -070022#include <hidl-hash/Hash.h>
Yifan Hong8304e412018-05-25 15:05:36 -070023#include <vintf/parse_string.h>
Yifan Hongfee209d2017-09-14 18:23:38 -070024
Yifan Hongd4a77e82017-09-06 19:40:24 -070025#include "TableEntry.h"
26
27#include "TextTable.h"
28#include "utils.h"
29
30namespace android {
31namespace lshal {
32
Yifan Hong0ad64f52018-05-25 15:29:17 -070033static const std::string &getArchString(vintf::Arch arch) {
Yifan Hongd4a77e82017-09-06 19:40:24 -070034 static const std::string sStr64 = "64";
35 static const std::string sStr32 = "32";
36 static const std::string sStrBoth = "32+64";
Yifan Hong430f8982018-05-25 17:28:39 -070037 static const std::string sStrUnknown = "?";
Yifan Hongd4a77e82017-09-06 19:40:24 -070038 switch (arch) {
Yifan Hong0ad64f52018-05-25 15:29:17 -070039 case vintf::Arch::ARCH_64:
Yifan Hongd4a77e82017-09-06 19:40:24 -070040 return sStr64;
Yifan Hong0ad64f52018-05-25 15:29:17 -070041 case vintf::Arch::ARCH_32:
Yifan Hongd4a77e82017-09-06 19:40:24 -070042 return sStr32;
Yifan Hong0ad64f52018-05-25 15:29:17 -070043 case vintf::Arch::ARCH_32_64:
Yifan Hongd4a77e82017-09-06 19:40:24 -070044 return sStrBoth;
Yifan Hong0ad64f52018-05-25 15:29:17 -070045 case vintf::Arch::ARCH_EMPTY: // fall through
Yifan Hongd4a77e82017-09-06 19:40:24 -070046 default:
47 return sStrUnknown;
48 }
49}
50
51static std::string getTitle(TableColumnType type) {
52 switch (type) {
Yifan Hongd43d7052017-09-14 11:16:12 -070053 case TableColumnType::INTERFACE_NAME: return "Interface";
54 case TableColumnType::TRANSPORT: return "Transport";
55 case TableColumnType::SERVER_PID: return "Server";
56 case TableColumnType::SERVER_CMD: return "Server CMD";
57 case TableColumnType::SERVER_ADDR: return "PTR";
58 case TableColumnType::CLIENT_PIDS: return "Clients";
59 case TableColumnType::CLIENT_CMDS: return "Clients CMD";
60 case TableColumnType::ARCH: return "Arch";
61 case TableColumnType::THREADS: return "Thread Use";
Yifan Hongfee209d2017-09-14 18:23:38 -070062 case TableColumnType::RELEASED: return "R";
63 case TableColumnType::HASH: return "Hash";
Yifan Hongbdf44f82018-05-25 14:20:00 -070064 case TableColumnType::VINTF: return "VINTF";
Yifan Hong13ba0a92018-06-25 16:15:56 -070065 case TableColumnType::SERVICE_STATUS: return "Status";
Yifan Hongd43d7052017-09-14 11:16:12 -070066 default:
Yifan Hongfee209d2017-09-14 18:23:38 -070067 LOG(FATAL) << __func__ << "Should not reach here. " << static_cast<int>(type);
Yifan Hongd4a77e82017-09-06 19:40:24 -070068 return "";
Yifan Hongd4a77e82017-09-06 19:40:24 -070069 }
70}
71
72std::string TableEntry::getField(TableColumnType type) const {
73 switch (type) {
Yifan Hongd43d7052017-09-14 11:16:12 -070074 case TableColumnType::INTERFACE_NAME:
Yifan Hongd4a77e82017-09-06 19:40:24 -070075 return interfaceName;
Yifan Hongd43d7052017-09-14 11:16:12 -070076 case TableColumnType::TRANSPORT:
Yifan Hong8304e412018-05-25 15:05:36 -070077 return vintf::to_string(transport);
Yifan Hongd43d7052017-09-14 11:16:12 -070078 case TableColumnType::SERVER_PID:
Yifan Hongd4a77e82017-09-06 19:40:24 -070079 return serverPid == NO_PID ? "N/A" : std::to_string(serverPid);
Yifan Hongd43d7052017-09-14 11:16:12 -070080 case TableColumnType::SERVER_CMD:
Yifan Hongd4a77e82017-09-06 19:40:24 -070081 return serverCmdline;
Yifan Hongd43d7052017-09-14 11:16:12 -070082 case TableColumnType::SERVER_ADDR:
Yifan Hongd4a77e82017-09-06 19:40:24 -070083 return serverObjectAddress == NO_PTR ? "N/A" : toHexString(serverObjectAddress);
Yifan Hongd43d7052017-09-14 11:16:12 -070084 case TableColumnType::CLIENT_PIDS:
Yifan Hongd4a77e82017-09-06 19:40:24 -070085 return join(clientPids, " ");
Yifan Hongd43d7052017-09-14 11:16:12 -070086 case TableColumnType::CLIENT_CMDS:
Yifan Hongd4a77e82017-09-06 19:40:24 -070087 return join(clientCmdlines, ";");
Yifan Hongd43d7052017-09-14 11:16:12 -070088 case TableColumnType::ARCH:
Yifan Hongd4a77e82017-09-06 19:40:24 -070089 return getArchString(arch);
Yifan Hongd43d7052017-09-14 11:16:12 -070090 case TableColumnType::THREADS:
Yifan Hongd4a77e82017-09-06 19:40:24 -070091 return getThreadUsage();
Yifan Hongfee209d2017-09-14 18:23:38 -070092 case TableColumnType::RELEASED:
93 return isReleased();
94 case TableColumnType::HASH:
95 return hash;
Yifan Hongbdf44f82018-05-25 14:20:00 -070096 case TableColumnType::VINTF:
97 return getVintfInfo();
Yifan Hong13ba0a92018-06-25 16:15:56 -070098 case TableColumnType::SERVICE_STATUS:
99 return lshal::to_string(serviceStatus);
Yifan Hongd43d7052017-09-14 11:16:12 -0700100 default:
Yifan Hongfee209d2017-09-14 18:23:38 -0700101 LOG(FATAL) << __func__ << "Should not reach here. " << static_cast<int>(type);
Yifan Hongd4a77e82017-09-06 19:40:24 -0700102 return "";
Yifan Hongd4a77e82017-09-06 19:40:24 -0700103 }
104}
105
Yifan Hongfee209d2017-09-14 18:23:38 -0700106std::string TableEntry::isReleased() const {
107 static const std::string unreleased = Hash::hexString(Hash::kEmptyHash);
108
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700109 if (hash.empty()) {
110 return "?";
111 }
112 if (hash == unreleased) {
113 return "N"; // unknown or unreleased
Yifan Hongfee209d2017-09-14 18:23:38 -0700114 }
115 return "Y"; // released
116}
117
Yifan Hongbdf44f82018-05-25 14:20:00 -0700118std::string TableEntry::getVintfInfo() const {
119 static const std::map<VintfInfo, std::string> values{
120 {DEVICE_MANIFEST, "DM"},
121 {DEVICE_MATRIX, "DC"},
122 {FRAMEWORK_MANIFEST, "FM"},
123 {FRAMEWORK_MATRIX, "FC"},
124 };
125 std::vector<std::string> ret;
126 for (const auto& pair : values) {
127 if (vintfInfo & pair.first) {
128 ret.push_back(pair.second);
129 }
130 }
131 auto joined = base::Join(ret, ',');
132 return joined.empty() ? "X" : joined;
133}
134
Yifan Hong13ba0a92018-06-25 16:15:56 -0700135std::string to_string(ServiceStatus s) {
136 switch (s) {
137 case ServiceStatus::ALIVE: return "alive";
138 case ServiceStatus::NON_RESPONSIVE: return "non-responsive";
139 case ServiceStatus::DECLARED: return "declared";
140 case ServiceStatus::UNKNOWN: return "N/A";
141 }
142
143 LOG(FATAL) << __func__ << "Should not reach here." << static_cast<int>(s);
144 return "";
145}
146
Yifan Hongd4a77e82017-09-06 19:40:24 -0700147TextTable Table::createTextTable(bool neat,
148 const std::function<std::string(const std::string&)>& emitDebugInfo) const {
149
150 TextTable textTable;
151 std::vector<std::string> row;
152 if (!neat) {
153 textTable.add(mDescription);
154
155 row.clear();
156 for (TableColumnType type : mSelectedColumns) {
157 row.push_back(getTitle(type));
158 }
159 textTable.add(std::move(row));
160 }
161
162 for (const auto& entry : mEntries) {
163 row.clear();
164 for (TableColumnType type : mSelectedColumns) {
165 row.push_back(entry.getField(type));
166 }
167 textTable.add(std::move(row));
168
169 if (emitDebugInfo) {
170 std::string debugInfo = emitDebugInfo(entry.interfaceName);
171 if (!debugInfo.empty()) textTable.add(debugInfo);
172 }
173 }
174 return textTable;
175}
176
177TextTable MergedTable::createTextTable() {
178 TextTable textTable;
179 for (const Table* table : mTables) {
180 textTable.addAll(table->createTextTable());
181 }
182 return textTable;
183}
184
Yifan Hong8bf73162017-09-07 18:06:13 -0700185bool TableEntry::operator==(const TableEntry& other) const {
186 if (this == &other) {
187 return true;
188 }
189 return interfaceName == other.interfaceName && transport == other.transport &&
190 serverPid == other.serverPid && threadUsage == other.threadUsage &&
191 threadCount == other.threadCount && serverCmdline == other.serverCmdline &&
192 serverObjectAddress == other.serverObjectAddress && clientPids == other.clientPids &&
193 clientCmdlines == other.clientCmdlines && arch == other.arch;
194}
195
196std::string TableEntry::to_string() const {
Yifan Hong8304e412018-05-25 15:05:36 -0700197 using vintf::operator<<;
Yifan Hong8bf73162017-09-07 18:06:13 -0700198 std::stringstream ss;
199 ss << "name=" << interfaceName << ";transport=" << transport << ";thread=" << getThreadUsage()
200 << ";server=" << serverPid
201 << "(" << serverObjectAddress << ";" << serverCmdline << ");clients=["
202 << join(clientPids, ";") << "](" << join(clientCmdlines, ";") << ");arch="
203 << getArchString(arch);
204 return ss.str();
205
206}
207
Yifan Hongd4a77e82017-09-06 19:40:24 -0700208} // namespace lshal
209} // namespace android