blob: a21e86cf0f5b5ee6e0dea615ec5bef6781868df8 [file] [log] [blame]
Yifan Hongb0dde932017-02-10 17:49:58 -08001/*
2 * Copyright (C) 2016 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_LSHAL_H_
18#define FRAMEWORK_NATIVE_CMDS_LSHAL_LSHAL_H_
19
20#include <stdint.h>
21
Yifan Hong4b865492017-02-28 19:38:24 -080022#include <fstream>
Yifan Hongb0dde932017-02-10 17:49:58 -080023#include <string>
24#include <vector>
25
26#include <android/hidl/manager/1.0/IServiceManager.h>
27
Yifan Hong4b865492017-02-28 19:38:24 -080028#include "NullableOStream.h"
Yifan Hongb0dde932017-02-10 17:49:58 -080029#include "TableEntry.h"
30
31namespace android {
32namespace lshal {
33
34enum : unsigned int {
35 OK = 0,
36 USAGE = 1 << 0,
37 NO_BINDERIZED_MANAGER = 1 << 1,
38 NO_PASSTHROUGH_MANAGER = 1 << 2,
39 DUMP_BINDERIZED_ERROR = 1 << 3,
40 DUMP_PASSTHROUGH_ERROR = 1 << 4,
41 DUMP_ALL_LIBS_ERROR = 1 << 5,
Yifan Hong4b865492017-02-28 19:38:24 -080042 IO_ERROR = 1 << 6,
Yifan Hongb0dde932017-02-10 17:49:58 -080043};
44using Status = unsigned int;
45
46class Lshal {
47public:
48 int main(int argc, char **argv);
49
50private:
51 Status parseArgs(int argc, char **argv);
52 Status fetch();
Yifan Hong38d53e02017-02-13 17:51:59 -080053 void postprocess();
Yifan Hong4b865492017-02-28 19:38:24 -080054 void dump();
Yifan Hongb0dde932017-02-10 17:49:58 -080055 void usage() const;
Yifan Honga3b87092017-03-02 19:19:29 -080056 void putEntry(TableEntrySource source, TableEntry &&entry);
Yifan Hongb0dde932017-02-10 17:49:58 -080057 Status fetchPassthrough(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
58 Status fetchBinderized(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
59 Status fetchAllLibraries(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
60 bool getReferencedPids(
61 pid_t serverPid, std::map<uint64_t, Pids> *objects) const;
Yifan Honga3b87092017-03-02 19:19:29 -080062 void dumpTable();
Yifan Hong4b865492017-02-28 19:38:24 -080063 void dumpVintf() const;
Yifan Hong38d53e02017-02-13 17:51:59 -080064 void printLine(
65 const std::string &interfaceName,
Yifan Hongb4479022017-03-02 16:54:11 -080066 const std::string &transport,
67 const std::string &arch,
68 const std::string &server,
Yifan Hongae09a3d2017-02-14 17:33:50 -080069 const std::string &serverCmdline,
70 const std::string &address, const std::string &clients,
71 const std::string &clientCmdlines) const ;
72 // Return /proc/{pid}/cmdline if it exists, else empty string.
73 const std::string &getCmdline(pid_t pid);
74 // Call getCmdline on all pid in pids. If it returns empty string, the process might
75 // have died, and the pid is removed from pids.
76 void removeDeadProcesses(Pids *pids);
Yifan Honga3b87092017-03-02 19:19:29 -080077 void forEachTable(const std::function<void(Table &)> &f);
78 void forEachTable(const std::function<void(const Table &)> &f) const;
Yifan Hongb0dde932017-02-10 17:49:58 -080079
Andreas Huber28d35912017-03-24 13:14:11 -070080 void emitDebugInfo(
81 const sp<hidl::manager::V1_0::IServiceManager> &serviceManager,
82 const std::string &interfaceName,
83 const std::string &instanceName) const;
84
Yifan Honga3b87092017-03-02 19:19:29 -080085 Table mServicesTable{};
86 Table mPassthroughRefTable{};
87 Table mImplementationsTable{};
88
Yifan Hong4b865492017-02-28 19:38:24 -080089 NullableOStream<std::ostream> mErr = std::cerr;
90 NullableOStream<std::ostream> mOut = std::cout;
91 NullableOStream<std::ofstream> mFileOutput = nullptr;
Yifan Hong38d53e02017-02-13 17:51:59 -080092 TableEntryCompare mSortColumn = nullptr;
93 TableEntrySelect mSelectedColumns = 0;
Yifan Hongae09a3d2017-02-14 17:33:50 -080094 // If true, cmdlines will be printed instead of pid.
Yifan Hong4b865492017-02-28 19:38:24 -080095 bool mEnableCmdlines = false;
Andreas Huber28d35912017-03-24 13:14:11 -070096
97 // If true, calls IBase::debug(...) on each service.
98 bool mEmitDebugInfo = false;
99
Yifan Hong4b865492017-02-28 19:38:24 -0800100 bool mVintf = false;
Yifan Hongae09a3d2017-02-14 17:33:50 -0800101 // If an entry does not exist, need to ask /proc/{pid}/cmdline to get it.
102 // If an entry exist but is an empty string, process might have died.
103 // If an entry exist and not empty, it contains the cached content of /proc/{pid}/cmdline.
104 std::map<pid_t, std::string> mCmdlines;
Yifan Hongb0dde932017-02-10 17:49:58 -0800105};
106
107
108} // namespace lshal
109} // namespace android
110
111#endif // FRAMEWORK_NATIVE_CMDS_LSHAL_LSHAL_H_