• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <map>
20 
21 #include <android-base/hex.h>
22 #include <android-base/strings.h>
23 #include <hidl-hash/Hash.h>
24 #include <vintf/parse_string.h>
25 
26 #include "TableEntry.h"
27 
28 #include "TextTable.h"
29 #include "utils.h"
30 
31 namespace android {
32 namespace lshal {
33 
getArchString(vintf::Arch arch)34 static const std::string &getArchString(vintf::Arch arch) {
35     static const std::string sStr64 = "64";
36     static const std::string sStr32 = "32";
37     static const std::string sStrBoth = "32+64";
38     static const std::string sStrUnknown = "?";
39     switch (arch) {
40         case vintf::Arch::ARCH_64:
41             return sStr64;
42         case vintf::Arch::ARCH_32:
43             return sStr32;
44         case vintf::Arch::ARCH_32_64:
45             return sStrBoth;
46         case vintf::Arch::ARCH_EMPTY: // fall through
47         default:
48             return sStrUnknown;
49     }
50 }
51 
getTitle(TableColumnType type)52 static std::string getTitle(TableColumnType type) {
53     switch (type) {
54         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";
63         case TableColumnType::RELEASED:         return "R";
64         case TableColumnType::HASH:             return "Hash";
65         case TableColumnType::VINTF:            return "VINTF";
66         case TableColumnType::SERVICE_STATUS:   return "Status";
67         default:
68             LOG(FATAL) << __func__ << "Should not reach here. " << static_cast<int>(type);
69             return "";
70     }
71 }
72 
getField(TableColumnType type) const73 std::string TableEntry::getField(TableColumnType type) const {
74     switch (type) {
75         case TableColumnType::INTERFACE_NAME:
76             return interfaceName;
77         case TableColumnType::TRANSPORT:
78             return vintf::to_string(transport);
79         case TableColumnType::SERVER_PID:
80             return serverPid == NO_PID ? "N/A" : std::to_string(serverPid);
81         case TableColumnType::SERVER_CMD:
82             return serverCmdline;
83         case TableColumnType::SERVER_ADDR:
84             return serverObjectAddress == NO_PTR ? "N/A" : toHexString(serverObjectAddress);
85         case TableColumnType::CLIENT_PIDS:
86             return join(clientPids, " ");
87         case TableColumnType::CLIENT_CMDS:
88             return join(clientCmdlines, ";");
89         case TableColumnType::ARCH:
90             return getArchString(arch);
91         case TableColumnType::THREADS:
92             return getThreadUsage();
93         case TableColumnType::RELEASED:
94             return isReleased();
95         case TableColumnType::HASH:
96             return hash;
97         case TableColumnType::VINTF:
98             return getVintfInfo();
99         case TableColumnType::SERVICE_STATUS:
100             return lshal::to_string(serviceStatus);
101         default:
102             LOG(FATAL) << __func__ << "Should not reach here. " << static_cast<int>(type);
103             return "";
104     }
105 }
106 
isReleased() const107 std::string TableEntry::isReleased() const {
108     static const std::string unreleased = android::base::HexString(Hash::kEmptyHash.data(),
109                                                                    Hash::kEmptyHash.size());
110 
111     if (hash.empty()) {
112         return "?";
113     }
114     if (hash == unreleased) {
115         return "N"; // unknown or unreleased
116     }
117     return "Y"; // released
118 }
119 
getVintfInfo() const120 std::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 
to_string(ServiceStatus s)137 std::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 
createTextTable(bool neat,const std::function<std::string (const std::string &)> & emitDebugInfo) const149 TextTable 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 
createTextTable()179 TextTable MergedTable::createTextTable() {
180     TextTable textTable;
181     for (const Table* table : mTables) {
182         textTable.addAll(table->createTextTable());
183     }
184     return textTable;
185 }
186 
operator ==(const TableEntry & other) const187 bool 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 
to_string() const198 std::string TableEntry::to_string() const {
199     using vintf::operator<<;
200     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 
210 } // namespace lshal
211 } // namespace android
212