TableEntry.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #include <map>
  19. #include <android-base/strings.h>
  20. #include <hidl-hash/Hash.h>
  21. #include <vintf/parse_string.h>
  22. #include "TableEntry.h"
  23. #include "TextTable.h"
  24. #include "utils.h"
  25. namespace android {
  26. namespace lshal {
  27. static const std::string &getArchString(vintf::Arch arch) {
  28. static const std::string sStr64 = "64";
  29. static const std::string sStr32 = "32";
  30. static const std::string sStrBoth = "32+64";
  31. static const std::string sStrUnknown = "?";
  32. switch (arch) {
  33. case vintf::Arch::ARCH_64:
  34. return sStr64;
  35. case vintf::Arch::ARCH_32:
  36. return sStr32;
  37. case vintf::Arch::ARCH_32_64:
  38. return sStrBoth;
  39. case vintf::Arch::ARCH_EMPTY: // fall through
  40. default:
  41. return sStrUnknown;
  42. }
  43. }
  44. static std::string getTitle(TableColumnType type) {
  45. switch (type) {
  46. case TableColumnType::INTERFACE_NAME: return "Interface";
  47. case TableColumnType::TRANSPORT: return "Transport";
  48. case TableColumnType::SERVER_PID: return "Server";
  49. case TableColumnType::SERVER_CMD: return "Server CMD";
  50. case TableColumnType::SERVER_ADDR: return "PTR";
  51. case TableColumnType::CLIENT_PIDS: return "Clients";
  52. case TableColumnType::CLIENT_CMDS: return "Clients CMD";
  53. case TableColumnType::ARCH: return "Arch";
  54. case TableColumnType::THREADS: return "Thread Use";
  55. case TableColumnType::RELEASED: return "R";
  56. case TableColumnType::HASH: return "Hash";
  57. case TableColumnType::VINTF: return "VINTF";
  58. case TableColumnType::SERVICE_STATUS: return "Status";
  59. default:
  60. LOG(FATAL) << __func__ << "Should not reach here. " << static_cast<int>(type);
  61. return "";
  62. }
  63. }
  64. std::string TableEntry::getField(TableColumnType type) const {
  65. switch (type) {
  66. case TableColumnType::INTERFACE_NAME:
  67. return interfaceName;
  68. case TableColumnType::TRANSPORT:
  69. return vintf::to_string(transport);
  70. case TableColumnType::SERVER_PID:
  71. return serverPid == NO_PID ? "N/A" : std::to_string(serverPid);
  72. case TableColumnType::SERVER_CMD:
  73. return serverCmdline;
  74. case TableColumnType::SERVER_ADDR:
  75. return serverObjectAddress == NO_PTR ? "N/A" : toHexString(serverObjectAddress);
  76. case TableColumnType::CLIENT_PIDS:
  77. return join(clientPids, " ");
  78. case TableColumnType::CLIENT_CMDS:
  79. return join(clientCmdlines, ";");
  80. case TableColumnType::ARCH:
  81. return getArchString(arch);
  82. case TableColumnType::THREADS:
  83. return getThreadUsage();
  84. case TableColumnType::RELEASED:
  85. return isReleased();
  86. case TableColumnType::HASH:
  87. return hash;
  88. case TableColumnType::VINTF:
  89. return getVintfInfo();
  90. case TableColumnType::SERVICE_STATUS:
  91. return lshal::to_string(serviceStatus);
  92. default:
  93. LOG(FATAL) << __func__ << "Should not reach here. " << static_cast<int>(type);
  94. return "";
  95. }
  96. }
  97. std::string TableEntry::isReleased() const {
  98. static const std::string unreleased = Hash::hexString(Hash::kEmptyHash);
  99. if (hash.empty()) {
  100. return "?";
  101. }
  102. if (hash == unreleased) {
  103. return "N"; // unknown or unreleased
  104. }
  105. return "Y"; // released
  106. }
  107. std::string TableEntry::getVintfInfo() const {
  108. static const std::map<VintfInfo, std::string> values{
  109. {DEVICE_MANIFEST, "DM"},
  110. {DEVICE_MATRIX, "DC"},
  111. {FRAMEWORK_MANIFEST, "FM"},
  112. {FRAMEWORK_MATRIX, "FC"},
  113. };
  114. std::vector<std::string> ret;
  115. for (const auto& pair : values) {
  116. if (vintfInfo & pair.first) {
  117. ret.push_back(pair.second);
  118. }
  119. }
  120. auto joined = base::Join(ret, ',');
  121. return joined.empty() ? "X" : joined;
  122. }
  123. std::string to_string(ServiceStatus s) {
  124. switch (s) {
  125. case ServiceStatus::ALIVE: return "alive";
  126. case ServiceStatus::NON_RESPONSIVE: return "non-responsive";
  127. case ServiceStatus::DECLARED: return "declared";
  128. case ServiceStatus::UNKNOWN: return "N/A";
  129. }
  130. LOG(FATAL) << __func__ << "Should not reach here." << static_cast<int>(s);
  131. return "";
  132. }
  133. TextTable Table::createTextTable(bool neat,
  134. const std::function<std::string(const std::string&)>& emitDebugInfo) const {
  135. TextTable textTable;
  136. std::vector<std::string> row;
  137. if (!neat) {
  138. textTable.add(mDescription);
  139. row.clear();
  140. for (TableColumnType type : mSelectedColumns) {
  141. row.push_back(getTitle(type));
  142. }
  143. textTable.add(std::move(row));
  144. }
  145. for (const auto& entry : mEntries) {
  146. row.clear();
  147. for (TableColumnType type : mSelectedColumns) {
  148. row.push_back(entry.getField(type));
  149. }
  150. textTable.add(std::move(row));
  151. if (emitDebugInfo) {
  152. std::string debugInfo = emitDebugInfo(entry.interfaceName);
  153. if (!debugInfo.empty()) textTable.add(debugInfo);
  154. }
  155. }
  156. return textTable;
  157. }
  158. TextTable MergedTable::createTextTable() {
  159. TextTable textTable;
  160. for (const Table* table : mTables) {
  161. textTable.addAll(table->createTextTable());
  162. }
  163. return textTable;
  164. }
  165. bool TableEntry::operator==(const TableEntry& other) const {
  166. if (this == &other) {
  167. return true;
  168. }
  169. return interfaceName == other.interfaceName && transport == other.transport &&
  170. serverPid == other.serverPid && threadUsage == other.threadUsage &&
  171. threadCount == other.threadCount && serverCmdline == other.serverCmdline &&
  172. serverObjectAddress == other.serverObjectAddress && clientPids == other.clientPids &&
  173. clientCmdlines == other.clientCmdlines && arch == other.arch;
  174. }
  175. std::string TableEntry::to_string() const {
  176. using vintf::operator<<;
  177. std::stringstream ss;
  178. ss << "name=" << interfaceName << ";transport=" << transport << ";thread=" << getThreadUsage()
  179. << ";server=" << serverPid
  180. << "(" << serverObjectAddress << ";" << serverCmdline << ");clients=["
  181. << join(clientPids, ";") << "](" << join(clientCmdlines, ";") << ");arch="
  182. << getArchString(arch);
  183. return ss.str();
  184. }
  185. } // namespace lshal
  186. } // namespace android