blob: 6defb0aef0e220d2aa5f59863bcd399b77f8c355 [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 Hongb2a2ecb2017-09-07 15:08:22 -070052protected:
Yifan Honga8bedc62017-09-08 18:00:31 -070053 Status parseArgs(const Arg &arg);
Yifan Hong443df792017-05-09 18:49:45 -070054 Status fetch();
55 void postprocess();
Yifan Hongca3b6602017-09-07 16:44:27 -070056 Status dump();
Yifan Hong443df792017-05-09 18:49:45 -070057 void putEntry(TableEntrySource source, TableEntry &&entry);
58 Status fetchPassthrough(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
59 Status fetchBinderized(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
60 Status fetchAllLibraries(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
Steven Morelandd8e20192017-05-24 11:23:08 -070061
Yifan Hong8bf73162017-09-07 18:06:13 -070062 virtual bool getPidInfo(pid_t serverPid, PidInfo *info) const;
Steven Morelandd8e20192017-05-24 11:23:08 -070063
Yifan Hongca3b6602017-09-07 16:44:27 -070064 void dumpTable(const NullableOStream<std::ostream>& out) const;
65 void dumpVintf(const NullableOStream<std::ostream>& out) const;
Yifan Hong1bc1e9f2017-08-29 17:28:12 -070066 void addLine(TextTable *table, const std::string &interfaceName, const std::string &transport,
67 const std::string &arch, const std::string &threadUsage, const std::string &server,
68 const std::string &serverCmdline, const std::string &address,
69 const std::string &clients, const std::string &clientCmdlines) const;
70 void addLine(TextTable *table, const TableEntry &entry);
Yifan Hong8bf73162017-09-07 18:06:13 -070071 // Read and return /proc/{pid}/cmdline.
72 virtual std::string parseCmdline(pid_t pid) const;
Yifan Hong443df792017-05-09 18:49:45 -070073 // Return /proc/{pid}/cmdline if it exists, else empty string.
74 const std::string &getCmdline(pid_t pid);
75 // Call getCmdline on all pid in pids. If it returns empty string, the process might
76 // have died, and the pid is removed from pids.
77 void removeDeadProcesses(Pids *pids);
78 void forEachTable(const std::function<void(Table &)> &f);
79 void forEachTable(const std::function<void(const Table &)> &f) const;
80
Yifan Hong76ac14a2017-09-08 14:59:04 -070081 NullableOStream<std::ostream> err() const;
82 NullableOStream<std::ostream> out() const;
83
Yifan Hong443df792017-05-09 18:49:45 -070084 Table mServicesTable{};
85 Table mPassthroughRefTable{};
86 Table mImplementationsTable{};
87
Yifan Hongca3b6602017-09-07 16:44:27 -070088 std::string mFileOutputPath;
Yifan Hong443df792017-05-09 18:49:45 -070089 TableEntryCompare mSortColumn = nullptr;
Yifan Hong443df792017-05-09 18:49:45 -070090
Yifan Hong443df792017-05-09 18:49:45 -070091 bool mEmitDebugInfo = false;
92
Yifan Hong6da06912017-05-12 16:56:43 -070093 // If true, output in VINTF format.
Yifan Hong443df792017-05-09 18:49:45 -070094 bool mVintf = false;
Yifan Hong6da06912017-05-12 16:56:43 -070095
96 // If true, explanatory text are not emitted.
97 bool mNeat = false;
98
Yifan Hong443df792017-05-09 18:49:45 -070099 // If an entry does not exist, need to ask /proc/{pid}/cmdline to get it.
100 // If an entry exist but is an empty string, process might have died.
101 // If an entry exist and not empty, it contains the cached content of /proc/{pid}/cmdline.
102 std::map<pid_t, std::string> mCmdlines;
103
Yifan Hong91e655d2017-09-13 15:44:56 -0700104private:
Yifan Hong443df792017-05-09 18:49:45 -0700105 DISALLOW_COPY_AND_ASSIGN(ListCommand);
106};
107
108
109} // namespace lshal
110} // namespace android
111
112#endif // FRAMEWORK_NATIVE_CMDS_LSHAL_LIST_COMMAND_H_