• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2025 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 #ifdef BINDER_CATCHER_ENABLE
28 class PeerBinderCatcher : public EventLogCatcher {
29 public:
30     explicit PeerBinderCatcher();
~PeerBinderCatcher()31     ~PeerBinderCatcher() override{};
32     bool Initialize(const std::string& perfCmd, int layer, int pid) override;
33     int Catch(int fd, int jsonFd) override;
34     void Init(std::shared_ptr<SysEvent> event, const std::string& filePath, std::set<int>& catchedPids);
35 
36 private:
37     struct BinderInfo {
38         int clientPid;
39         int clientTid;
40         int serverPid;
41         int serverTid;
42         int wait;
43     };
44     struct OutputBinderInfo {
45         std::string info = "";
46         int pid = 0;
47     };
48     struct ParseBinderParam {
49         int eventPid;
50         int eventTid;
51     };
52 
53     bool firstLayerInit_ = false;
54     int pid_ = 0;
55     int layer_ = 0;
56     std::string perfCmd_ = "";
57     std::string binderPath_ = "/proc/transaction_proc";
58     std::shared_ptr<SysEvent> event_ = nullptr;
59     std::set<int> catchedPids_ = {0};
60     std::map<int, std::list<PeerBinderCatcher::BinderInfo>> BinderInfoParser(std::ifstream& fin,
61         int fd, int jsonFd, std::set<int>& asyncPids) const;
62     void BinderInfoParser(std::ifstream& fin, int fd,
63         std::map<int, std::list<PeerBinderCatcher::BinderInfo>>& manager,
64         std::list<PeerBinderCatcher::OutputBinderInfo>& outputBinderInfoList, std::set<int>& asyncPids) const;
65     void BinderInfoLineParser(std::ifstream& fin, int fd,
66         std::map<int, std::list<PeerBinderCatcher::BinderInfo>>& manager,
67         std::list<PeerBinderCatcher::OutputBinderInfo>& outputBinderInfoList,
68         std::map<uint32_t, uint32_t>& asyncBinderMap,
69         std::vector<std::pair<uint32_t, uint64_t>>& freezeAsyncSpacePairs) const;
70     std::vector<std::string> GetFileToList(std::string line) const;
71     std::string StrSplit(const std::string& str, uint16_t index) const;
72     void SaveBinderLineToFd(int fd, const std::string& line, bool& isBinderMatchup) const;
73     void ParseBinderCallChain(std::map<int, std::list<PeerBinderCatcher::BinderInfo>>& manager,
74         std::set<int>& pids, int pid, const PeerBinderCatcher::ParseBinderParam& params, bool getTerminal);
75     std::set<int> GetBinderPeerPids(int fd, int jsonFd, std::set<int>& asyncPids);
76     bool IsAncoProc(int pid) const;
77     void CatcherFfrtStack(int fd, int pid) const;
78     void CatcherStacktrace(int fd, int pid, bool sync = true);
79     void AddBinderJsonInfo(std::list<OutputBinderInfo> outputBinderInfoList, int jsonFd) const;
80 #ifdef HAS_HIPERF
81     void DumpHiperf(const std::set<int>& pids, int processId, const std::string& perfCmd);
82     void DoExecHiperf(const std::string& fileName, const std::set<int>& pids, int processId,
83         const std::string& perfCmd);
84 #endif
85 };
86 #endif // BINDER_CATCHER_ENABLE
87 } // namespace HiviewDFX
88 } // namespace OHOS
89 #endif // EVENT_LOGGER_PEER_BINDER_LOG_CATCHER