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();
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 private:
36     struct BinderInfo {
37         int client;
38         int server;
39         int wait;
40     };
41     struct OutputBinderInfo {
42         std::string info = "";
43         int pid = 0;
44     };
45 
46     int pid_ = 0;
47     int layer_ = 0;
48     std::string perfCmd_ = "";
49     std::string binderPath_ = "/proc/transaction_proc";
50     std::shared_ptr<SysEvent> event_ = nullptr;
51     std::set<int> catchedPids_ = {0};
52     std::map<int, std::list<PeerBinderCatcher::BinderInfo>> BinderInfoParser(std::ifstream& fin,
53         int fd, int jsonFd) const;
54     void BinderInfoParser(std::ifstream& fin, int fd,
55         std::map<int, std::list<PeerBinderCatcher::BinderInfo>>& manager,
56         std::list<PeerBinderCatcher::OutputBinderInfo>& outputBinderInfoList) const;
57     void GetFileToList(std::string line, std::vector<std::string>& strList) const;
58     void ParseBinderCallChain(std::map<int, std::list<PeerBinderCatcher::BinderInfo>>& manager,
59     std::set<int>& pids, int pid) const;
60     std::set<int> GetBinderPeerPids(int fd, int jsonFd) const;
61     bool IsAncoProc(int pid) const;
62     void CatcherFfrtStack(int fd, int pid) const;
63     void CatcherStacktrace(int fd, int pid) const;
64     void AddBinderJsonInfo(std::list<OutputBinderInfo> outputBinderInfoList, int jsonFd) const;
65 #ifdef HAS_HIPERF
66     void ForkToDumpHiperf(const std::set<int>& pids);
67     void DoExecHiperf(const std::string& fileName, const std::set<int>& pids);
68 #endif
69 };
70 } // namespace HiviewDFX
71 } // namespace OHOS
72 #endif // EVENT_LOGGER_PEER_BINDER_LOG_CATCHER