ListCommand.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #ifndef FRAMEWORK_NATIVE_CMDS_LSHAL_LIST_COMMAND_H_
  17. #define FRAMEWORK_NATIVE_CMDS_LSHAL_LIST_COMMAND_H_
  18. #include <getopt.h>
  19. #include <stdint.h>
  20. #include <fstream>
  21. #include <string>
  22. #include <vector>
  23. #include <android-base/macros.h>
  24. #include <android/hidl/manager/1.0/IServiceManager.h>
  25. #include <hidl-util/FqInstance.h>
  26. #include <vintf/HalManifest.h>
  27. #include <vintf/VintfObject.h>
  28. #include "Command.h"
  29. #include "NullableOStream.h"
  30. #include "TableEntry.h"
  31. #include "TextTable.h"
  32. #include "utils.h"
  33. namespace android {
  34. namespace lshal {
  35. class Lshal;
  36. struct PidInfo {
  37. std::map<uint64_t, Pids> refPids; // pids that are referenced
  38. uint32_t threadUsage; // number of threads in use
  39. uint32_t threadCount; // number of threads total
  40. };
  41. enum class HalType {
  42. BINDERIZED_SERVICES = 0,
  43. PASSTHROUGH_CLIENTS,
  44. PASSTHROUGH_LIBRARIES,
  45. VINTF_MANIFEST,
  46. LAZY_HALS,
  47. };
  48. class ListCommand : public Command {
  49. public:
  50. explicit ListCommand(Lshal &lshal) : Command(lshal) {}
  51. virtual ~ListCommand() = default;
  52. Status main(const Arg &arg) override;
  53. void usage() const override;
  54. std::string getSimpleDescription() const override;
  55. std::string getName() const override { return GetName(); }
  56. static std::string GetName();
  57. struct RegisteredOption {
  58. // short alternative, e.g. 'v'. If '\0', no short options is available.
  59. char shortOption;
  60. // long alternative, e.g. 'init-vintf'
  61. std::string longOption;
  62. // no_argument, required_argument or optional_argument
  63. int hasArg;
  64. // value written to 'flag' by getopt_long
  65. int val;
  66. // operation when the argument is present
  67. std::function<Status(ListCommand* thiz, const char* arg)> op;
  68. // help message
  69. std::string help;
  70. const std::string& getHelpMessageForArgument() const;
  71. };
  72. // A list of acceptable command line options
  73. // key: value returned by getopt_long
  74. using RegisteredOptions = std::vector<RegisteredOption>;
  75. static std::string INIT_VINTF_NOTES;
  76. protected:
  77. Status parseArgs(const Arg &arg);
  78. // Retrieve first-hand information
  79. Status fetch();
  80. // Retrieve derived information base on existing table
  81. virtual void postprocess();
  82. Status dump();
  83. void putEntry(HalType type, TableEntry &&entry);
  84. Status fetchPassthrough(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
  85. Status fetchBinderized(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
  86. Status fetchAllLibraries(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
  87. Status fetchManifestHals();
  88. Status fetchLazyHals();
  89. Status fetchBinderizedEntry(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager,
  90. TableEntry *entry);
  91. // Get relevant information for a PID by parsing files under /d/binder.
  92. // It is a virtual member function so that it can be mocked.
  93. virtual bool getPidInfo(pid_t serverPid, PidInfo *info) const;
  94. // Retrieve from mCachedPidInfos and call getPidInfo if necessary.
  95. const PidInfo* getPidInfoCached(pid_t serverPid);
  96. void dumpTable(const NullableOStream<std::ostream>& out) const;
  97. void dumpVintf(const NullableOStream<std::ostream>& out) const;
  98. void addLine(TextTable *table, const std::string &interfaceName, const std::string &transport,
  99. const std::string &arch, const std::string &threadUsage, const std::string &server,
  100. const std::string &serverCmdline, const std::string &address,
  101. const std::string &clients, const std::string &clientCmdlines) const;
  102. void addLine(TextTable *table, const TableEntry &entry);
  103. // Read and return /proc/{pid}/cmdline.
  104. virtual std::string parseCmdline(pid_t pid) const;
  105. // Return /proc/{pid}/cmdline if it exists, else empty string.
  106. const std::string& getCmdline(pid_t pid);
  107. // Call getCmdline on all pid in pids. If it returns empty string, the process might
  108. // have died, and the pid is removed from pids.
  109. void removeDeadProcesses(Pids *pids);
  110. virtual Partition getPartition(pid_t pid);
  111. Partition resolvePartition(Partition processPartition, const FqInstance &fqInstance) const;
  112. VintfInfo getVintfInfo(const std::string &fqInstanceName, vintf::TransportArch ta) const;
  113. // Allow to mock these functions for testing.
  114. virtual std::shared_ptr<const vintf::HalManifest> getDeviceManifest() const;
  115. virtual std::shared_ptr<const vintf::CompatibilityMatrix> getDeviceMatrix() const;
  116. virtual std::shared_ptr<const vintf::HalManifest> getFrameworkManifest() const;
  117. virtual std::shared_ptr<const vintf::CompatibilityMatrix> getFrameworkMatrix() const;
  118. void forEachTable(const std::function<void(Table &)> &f);
  119. void forEachTable(const std::function<void(const Table &)> &f) const;
  120. Table* tableForType(HalType type);
  121. const Table* tableForType(HalType type) const;
  122. NullableOStream<std::ostream> err() const;
  123. NullableOStream<std::ostream> out() const;
  124. void registerAllOptions();
  125. // helper functions to dumpVintf.
  126. bool addEntryWithInstance(const TableEntry &entry, vintf::HalManifest *manifest) const;
  127. bool addEntryWithoutInstance(const TableEntry &entry, const vintf::HalManifest *manifest) const;
  128. // Helper function. Whether to fetch entries corresponding to a given HAL type.
  129. bool shouldFetchHalType(const HalType &type) const;
  130. void initFetchTypes();
  131. // Helper functions ti add HALs that are listed in VINTF manifest to LAZY_HALS table.
  132. bool hasHwbinderEntry(const TableEntry& entry) const;
  133. bool hasPassthroughEntry(const TableEntry& entry) const;
  134. Table mServicesTable{};
  135. Table mPassthroughRefTable{};
  136. Table mImplementationsTable{};
  137. Table mManifestHalsTable{};
  138. Table mLazyHalsTable{};
  139. std::string mFileOutputPath;
  140. TableEntryCompare mSortColumn = nullptr;
  141. bool mEmitDebugInfo = false;
  142. // If true, output in VINTF format. Output only entries from the specified partition.
  143. bool mVintf = false;
  144. Partition mVintfPartition = Partition::UNKNOWN;
  145. // If true, explanatory text are not emitted.
  146. bool mNeat = false;
  147. // Type(s) of HAL associations to list.
  148. std::vector<HalType> mListTypes{};
  149. // Type(s) of HAL associations to fetch.
  150. std::set<HalType> mFetchTypes{};
  151. // If an entry does not exist, need to ask /proc/{pid}/cmdline to get it.
  152. // If an entry exist but is an empty string, process might have died.
  153. // If an entry exist and not empty, it contains the cached content of /proc/{pid}/cmdline.
  154. std::map<pid_t, std::string> mCmdlines;
  155. // Cache for getPidInfo.
  156. std::map<pid_t, PidInfo> mCachedPidInfos;
  157. // Cache for getPartition.
  158. std::map<pid_t, Partition> mPartitions;
  159. RegisteredOptions mOptions;
  160. // All selected columns
  161. std::vector<TableColumnType> mSelectedColumns;
  162. // If true, emit cmdlines instead of PIDs
  163. bool mEnableCmdlines = false;
  164. private:
  165. DISALLOW_COPY_AND_ASSIGN(ListCommand);
  166. };
  167. } // namespace lshal
  168. } // namespace android
  169. #endif // FRAMEWORK_NATIVE_CMDS_LSHAL_LIST_COMMAND_H_