blob: 346af28589c2dd547c76fb50d0684651a6a195ff [file] [log] [blame]
Yifan Hong443df792017-05-09 18:49:45 -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
17#ifndef FRAMEWORK_NATIVE_CMDS_LSHAL_LIST_COMMAND_H_
18#define FRAMEWORK_NATIVE_CMDS_LSHAL_LIST_COMMAND_H_
19
20#include <stdint.h>
21
22#include <fstream>
23#include <string>
24#include <vector>
25
26#include <android-base/macros.h>
27#include <android/hidl/manager/1.0/IServiceManager.h>
28
Yifan Hongded398e2017-09-07 13:54:28 -070029#include "Command.h"
Yifan Hong443df792017-05-09 18:49:45 -070030#include "NullableOStream.h"
31#include "TableEntry.h"
Yifan Hong1bc1e9f2017-08-29 17:28:12 -070032#include "TextTable.h"
Yifan Hong443df792017-05-09 18:49:45 -070033#include "utils.h"
34
35namespace android {
36namespace lshal {
37
38class Lshal;
39
Yifan Hong8bf73162017-09-07 18:06:13 -070040struct PidInfo {
41 std::map<uint64_t, Pids> refPids; // pids that are referenced
42 uint32_t threadUsage; // number of threads in use
43 uint32_t threadCount; // number of threads total
44};
45
Yifan Hongded398e2017-09-07 13:54:28 -070046class ListCommand : public Command {
Yifan Hong443df792017-05-09 18:49:45 -070047public:
Yifan Hongded398e2017-09-07 13:54:28 -070048 ListCommand(Lshal &lshal) : Command(lshal) {}
Yifan Hong8bf73162017-09-07 18:06:13 -070049 virtual ~ListCommand() = default;
Yifan Honga8bedc62017-09-08 18:00:31 -070050 Status main(const Arg &arg) override;
51 void usage() const override;
Yifan Hong795b6ec2017-09-13 11:25:28 -070052 std::string getSimpleDescription() const override;
53 std::string getName() const override { return GetName(); }
54
55 static std::string GetName();
Yifan Hongb2a2ecb2017-09-07 15:08:22 -070056protected:
Yifan Honga8bedc62017-09-08 18:00:31 -070057 Status parseArgs(const Arg &arg);
Yifan Hong443df792017-05-09 18:49:45 -070058 Status fetch();
59 void postprocess();
Yifan Hongca3b6602017-09-07 16:44:27 -070060 Status dump();
Yifan Hong443df792017-05-09 18:49:45 -070061 void putEntry(TableEntrySource source, TableEntry &&entry);
62 Status fetchPassthrough(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
63 Status fetchBinderized(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
64 Status fetchAllLibraries(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
Steven Morelandd8e20192017-05-24 11:23:08 -070065
Yifan Hong8bf73162017-09-07 18:06:13 -070066 virtual bool getPidInfo(pid_t serverPid, PidInfo *info) const;
Steven Morelandd8e20192017-05-24 11:23:08 -070067
Yifan Hongca3b6602017-09-07 16:44:27 -070068 void dumpTable(const NullableOStream<std::ostream>& out) const;
69 void dumpVintf(const NullableOStream<std::ostream>& out) const;
Yifan Hong1bc1e9f2017-08-29 17:28:12 -070070 void addLine(TextTable *table, const std::string &interfaceName, const std::string &transport,
71 const std::string &arch, const std::string &threadUsage, const std::string &server,
72 const std::string &serverCmdline, const std::string &address,
73 const std::string &clients, const std::string &clientCmdlines) const;
74 void addLine(TextTable *table, const TableEntry &entry);
Yifan Hong8bf73162017-09-07 18:06:13 -070075 // Read and return /proc/{pid}/cmdline.
76 virtual std::string parseCmdline(pid_t pid) const;
Yifan Hong443df792017-05-09 18:49:45 -070077 // Return /proc/{pid}/cmdline if it exists, else empty string.
78 const std::string &getCmdline(pid_t pid);
79 // Call getCmdline on all pid in pids. If it returns empty string, the process might
80 // have died, and the pid is removed from pids.
81 void removeDeadProcesses(Pids *pids);
82 void forEachTable(const std::function<void(Table &)> &f);
83 void forEachTable(const std::function<void(const Table &)> &f) const;
84
Yifan Hong76ac14a2017-09-08 14:59:04 -070085 NullableOStream<std::ostream> err() const;
86 NullableOStream<std::ostream> out() const;
87
Yifan Hong443df792017-05-09 18:49:45 -070088 Table mServicesTable{};
89 Table mPassthroughRefTable{};
90 Table mImplementationsTable{};
91
Yifan Hongca3b6602017-09-07 16:44:27 -070092 std::string mFileOutputPath;
Yifan Hong443df792017-05-09 18:49:45 -070093 TableEntryCompare mSortColumn = nullptr;
Yifan Hong443df792017-05-09 18:49:45 -070094
Yifan Hong443df792017-05-09 18:49:45 -070095 bool mEmitDebugInfo = false;
96
Yifan Hong6da06912017-05-12 16:56:43 -070097 // If true, output in VINTF format.
Yifan Hong443df792017-05-09 18:49:45 -070098 bool mVintf = false;
Yifan Hong6da06912017-05-12 16:56:43 -070099
100 // If true, explanatory text are not emitted.
101 bool mNeat = false;
102
Yifan Hong443df792017-05-09 18:49:45 -0700103 // If an entry does not exist, need to ask /proc/{pid}/cmdline to get it.
104 // If an entry exist but is an empty string, process might have died.
105 // If an entry exist and not empty, it contains the cached content of /proc/{pid}/cmdline.
106 std::map<pid_t, std::string> mCmdlines;
107
Yifan Hong91e655d2017-09-13 15:44:56 -0700108private:
Yifan Hong443df792017-05-09 18:49:45 -0700109 DISALLOW_COPY_AND_ASSIGN(ListCommand);
110};
111
112
113} // namespace lshal
114} // namespace android
115
116#endif // FRAMEWORK_NATIVE_CMDS_LSHAL_LIST_COMMAND_H_