• 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 
49     bool firstLayerInit_ = false;
50     int pid_ = 0;
51     int layer_ = 0;
52     std::string perfCmd_ = "";
53     std::string binderPath_ = "/proc/transaction_proc";
54     std::shared_ptr<SysEvent> event_ = nullptr;
55     std::set<int> catchedPids_ = {0};
56     std::map<int, std::list<PeerBinderCatcher::BinderInfo>> BinderInfoParser(std::ifstream& fin,
57         int fd, int jsonFd, std::set<int>& asyncPids) const;
58     void BinderInfoParser(std::ifstream& fin, int fd,
59         std::map<int, std::list<PeerBinderCatcher::BinderInfo>>& manager,
60         std::list<PeerBinderCatcher::OutputBinderInfo>& outputBinderInfoList, std::set<int>& asyncPids) const;
61     void BinderInfoLineParser(std::ifstream& fin, int fd,
62         std::map<int, std::list<PeerBinderCatcher::BinderInfo>>& manager,
63         std::list<PeerBinderCatcher::OutputBinderInfo>& outputBinderInfoList,
64         std::map<uint32_t, uint32_t>& asyncBinderMap,
65         std::vector<std::pair<uint32_t, uint64_t>>& freezeAsyncSpacePairs) const;
66     std::vector<std::string> GetFileToList(std::string line) const;
67     void ParseBinderCallChain(std::map<int, std::list<PeerBinderCatcher::BinderInfo>>& manager,
68         std::set<int>& pids, int pid);
69     std::set<int> GetBinderPeerPids(int fd, int jsonFd, std::set<int>& asyncPids);
70     bool IsAncoProc(int pid) const;
71     void CatcherFfrtStack(int fd, int pid) const;
72     void CatcherStacktrace(int fd, int pid, bool sync = true);
73     void AddBinderJsonInfo(std::list<OutputBinderInfo> outputBinderInfoList, int jsonFd) const;
74 #ifdef HAS_HIPERF
75     void DumpHiperf(const std::set<int>& pids, int processId, const std::string& perfCmd);
76     void DoExecHiperf(const std::string& fileName, const std::set<int>& pids, int processId,
77         const std::string& perfCmd);
78 #endif
79 };
80 #endif // BINDER_CATCHER_ENABLE
81 } // namespace HiviewDFX
82 } // namespace OHOS
83 #endif // EVENT_LOGGER_PEER_BINDER_LOG_CATCHER