1 /*
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
19 #include <hidl-hash/Hash.h>
20
21 #include "TableEntry.h"
22
23 #include "TextTable.h"
24 #include "utils.h"
25
26 namespace android {
27 namespace lshal {
28
getArchString(Architecture arch)29 static const std::string &getArchString(Architecture arch) {
30 static const std::string sStr64 = "64";
31 static const std::string sStr32 = "32";
32 static const std::string sStrBoth = "32+64";
33 static const std::string sStrUnknown = "";
34 switch (arch) {
35 case ARCH64:
36 return sStr64;
37 case ARCH32:
38 return sStr32;
39 case ARCH_BOTH:
40 return sStrBoth;
41 case ARCH_UNKNOWN: // fall through
42 default:
43 return sStrUnknown;
44 }
45 }
46
getTitle(TableColumnType type)47 static std::string getTitle(TableColumnType type) {
48 switch (type) {
49 case TableColumnType::INTERFACE_NAME: return "Interface";
50 case TableColumnType::TRANSPORT: return "Transport";
51 case TableColumnType::SERVER_PID: return "Server";
52 case TableColumnType::SERVER_CMD: return "Server CMD";
53 case TableColumnType::SERVER_ADDR: return "PTR";
54 case TableColumnType::CLIENT_PIDS: return "Clients";
55 case TableColumnType::CLIENT_CMDS: return "Clients CMD";
56 case TableColumnType::ARCH: return "Arch";
57 case TableColumnType::THREADS: return "Thread Use";
58 case TableColumnType::RELEASED: return "R";
59 case TableColumnType::HASH: return "Hash";
60 default:
61 LOG(FATAL) << __func__ << "Should not reach here. " << static_cast<int>(type);
62 return "";
63 }
64 }
65
getField(TableColumnType type) const66 std::string TableEntry::getField(TableColumnType type) const {
67 switch (type) {
68 case TableColumnType::INTERFACE_NAME:
69 return interfaceName;
70 case TableColumnType::TRANSPORT:
71 return transport;
72 case TableColumnType::SERVER_PID:
73 return serverPid == NO_PID ? "N/A" : std::to_string(serverPid);
74 case TableColumnType::SERVER_CMD:
75 return serverCmdline;
76 case TableColumnType::SERVER_ADDR:
77 return serverObjectAddress == NO_PTR ? "N/A" : toHexString(serverObjectAddress);
78 case TableColumnType::CLIENT_PIDS:
79 return join(clientPids, " ");
80 case TableColumnType::CLIENT_CMDS:
81 return join(clientCmdlines, ";");
82 case TableColumnType::ARCH:
83 return getArchString(arch);
84 case TableColumnType::THREADS:
85 return getThreadUsage();
86 case TableColumnType::RELEASED:
87 return isReleased();
88 case TableColumnType::HASH:
89 return hash;
90 default:
91 LOG(FATAL) << __func__ << "Should not reach here. " << static_cast<int>(type);
92 return "";
93 }
94 }
95
isReleased() const96 std::string TableEntry::isReleased() const {
97 static const std::string unreleased = Hash::hexString(Hash::kEmptyHash);
98
99 if (hash.empty() || hash == unreleased) {
100 return " "; // unknown or unreleased
101 }
102 return "Y"; // released
103 }
104
createTextTable(bool neat,const std::function<std::string (const std::string &)> & emitDebugInfo) const105 TextTable Table::createTextTable(bool neat,
106 const std::function<std::string(const std::string&)>& emitDebugInfo) const {
107
108 TextTable textTable;
109 std::vector<std::string> row;
110 if (!neat) {
111 textTable.add(mDescription);
112
113 row.clear();
114 for (TableColumnType type : mSelectedColumns) {
115 row.push_back(getTitle(type));
116 }
117 textTable.add(std::move(row));
118 }
119
120 for (const auto& entry : mEntries) {
121 row.clear();
122 for (TableColumnType type : mSelectedColumns) {
123 row.push_back(entry.getField(type));
124 }
125 textTable.add(std::move(row));
126
127 if (emitDebugInfo) {
128 std::string debugInfo = emitDebugInfo(entry.interfaceName);
129 if (!debugInfo.empty()) textTable.add(debugInfo);
130 }
131 }
132 return textTable;
133 }
134
createTextTable()135 TextTable MergedTable::createTextTable() {
136 TextTable textTable;
137 for (const Table* table : mTables) {
138 textTable.addAll(table->createTextTable());
139 }
140 return textTable;
141 }
142
operator ==(const TableEntry & other) const143 bool TableEntry::operator==(const TableEntry& other) const {
144 if (this == &other) {
145 return true;
146 }
147 return interfaceName == other.interfaceName && transport == other.transport &&
148 serverPid == other.serverPid && threadUsage == other.threadUsage &&
149 threadCount == other.threadCount && serverCmdline == other.serverCmdline &&
150 serverObjectAddress == other.serverObjectAddress && clientPids == other.clientPids &&
151 clientCmdlines == other.clientCmdlines && arch == other.arch;
152 }
153
to_string() const154 std::string TableEntry::to_string() const {
155 std::stringstream ss;
156 ss << "name=" << interfaceName << ";transport=" << transport << ";thread=" << getThreadUsage()
157 << ";server=" << serverPid
158 << "(" << serverObjectAddress << ";" << serverCmdline << ");clients=["
159 << join(clientPids, ";") << "](" << join(clientCmdlines, ";") << ");arch="
160 << getArchString(arch);
161 return ss.str();
162
163 }
164
165 } // namespace lshal
166 } // namespace android
167