1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef EVENT_LOGGER_PEER_BINDER_LOG_CATCHER
16 #define EVENT_LOGGER_PEER_BINDER_LOG_CATCHER
17 #include <fstream>
18 #include <string>
19 #include <memory>
20 #include <vector>
21 
22 #include "sys_event.h"
23 
24 #include "event_log_catcher.h"
25 namespace OHOS {
26 namespace HiviewDFX {
27 class PeerBinderCatcher : public EventLogCatcher {
28 public:
29     explicit PeerBinderCatcher();
~PeerBinderCatcher()30     ~PeerBinderCatcher() override{};
31     bool Initialize(const std::string& perfCmd, int layer, int pid) override;
32     int Catch(int fd, int jsonFd) override;
33     void Init(std::shared_ptr<SysEvent> event, const std::string& filePath, std::set<int>& catchedPids);
34 
35     static const inline std::string LOGGER_EVENT_PEERBINDER = "PeerBinder";
36     static const inline std::string LOGGER_BINDER_DEBUG_PROC_PATH = "/proc/transaction_proc";
37     static constexpr int BP_CMD_LAYER_INDEX = 1;
38     static constexpr int BP_CMD_PERF_TYPE_INDEX = 2;
39     static constexpr int PERF_LOG_EXPIRE_TIME = 60;
40     static constexpr size_t BP_CMD_SZ = 3;
41 
42 private:
43     struct BinderInfo {
44         int client;
45         int server;
46         int wait;
47     };
48     struct OutputBinderInfo {
49         std::string info = "";
50         int pid = 0;
51     };
52     enum {
53         LOGGER_BINDER_STACK_ONE = 0,
54         LOGGER_BINDER_STACK_ALL = 1,
55     };
56 
57     int pid_ = 0;
58     int layer_ = 0;
59     std::string perfCmd_ = "";
60     std::string binderPath_ = LOGGER_BINDER_DEBUG_PROC_PATH;
61     std::shared_ptr<SysEvent> event_ = nullptr;
62     std::set<int> catchedPids_ = {0};
63     std::map<int, std::list<PeerBinderCatcher::BinderInfo>> BinderInfoParser(std::ifstream& fin,
64         int fd, int jsonFd, std::set<int>& asyncPids) const;
65     void BinderInfoParser(std::ifstream& fin, int fd,
66         std::map<int, std::list<PeerBinderCatcher::BinderInfo>>& manager,
67         std::list<PeerBinderCatcher::OutputBinderInfo>& outputBinderInfoList, std::set<int>& asyncPids) const;
68     void BinderInfoLineParser(std::ifstream& fin, int fd,
69         std::map<int, std::list<PeerBinderCatcher::BinderInfo>>& manager,
70         std::list<PeerBinderCatcher::OutputBinderInfo>& outputBinderInfoList,
71         std::map<uint32_t, uint32_t>& asyncBinderMap,
72         std::vector<std::pair<uint32_t, uint64_t>>& freezeAsyncSpacePairs) const;
73     std::vector<std::string> GetFileToList(std::string line) const;
74     void ParseBinderCallChain(std::map<int, std::list<PeerBinderCatcher::BinderInfo>>& manager,
75         std::set<int>& pids, int pid) const;
76     std::set<int> GetBinderPeerPids(int fd, int jsonFd, std::set<int>& asyncPids) const;
77     bool IsAncoProc(int pid) const;
78     void CatcherFfrtStack(int fd, int pid) const;
79     void CatcherStacktrace(int fd, int pid, bool sync = true) const;
80     void AddBinderJsonInfo(std::list<OutputBinderInfo> outputBinderInfoList, int jsonFd) const;
81 #ifdef HAS_HIPERF
82     void ForkToDumpHiperf(const std::set<int>& pids);
83     void DoExecHiperf(const std::string& fileName, const std::set<int>& pids);
84 #endif
85 };
86 } // namespace HiviewDFX
87 } // namespace OHOS
88 #endif // EVENT_LOGGER_PEER_BINDER_LOG_CATCHER