xref: /aosp_15_r20/frameworks/native/libs/binderdebug/BinderDebug.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2020 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #include <android-base/logging.h>
18*38e8c45fSAndroid Build Coastguard Worker #include <android-base/parseint.h>
19*38e8c45fSAndroid Build Coastguard Worker #include <android-base/strings.h>
20*38e8c45fSAndroid Build Coastguard Worker #include <binder/Binder.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <sys/types.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <fstream>
23*38e8c45fSAndroid Build Coastguard Worker #include <regex>
24*38e8c45fSAndroid Build Coastguard Worker 
25*38e8c45fSAndroid Build Coastguard Worker #include <binderdebug/BinderDebug.h>
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker namespace android {
28*38e8c45fSAndroid Build Coastguard Worker 
contextToString(BinderDebugContext context)29*38e8c45fSAndroid Build Coastguard Worker static std::string contextToString(BinderDebugContext context) {
30*38e8c45fSAndroid Build Coastguard Worker     switch (context) {
31*38e8c45fSAndroid Build Coastguard Worker         case BinderDebugContext::BINDER:
32*38e8c45fSAndroid Build Coastguard Worker             return "binder";
33*38e8c45fSAndroid Build Coastguard Worker         case BinderDebugContext::HWBINDER:
34*38e8c45fSAndroid Build Coastguard Worker             return "hwbinder";
35*38e8c45fSAndroid Build Coastguard Worker         case BinderDebugContext::VNDBINDER:
36*38e8c45fSAndroid Build Coastguard Worker             return "vndbinder";
37*38e8c45fSAndroid Build Coastguard Worker         default:
38*38e8c45fSAndroid Build Coastguard Worker             return std::string();
39*38e8c45fSAndroid Build Coastguard Worker     }
40*38e8c45fSAndroid Build Coastguard Worker }
41*38e8c45fSAndroid Build Coastguard Worker 
scanBinderContext(pid_t pid,const std::string & contextName,std::function<void (const std::string &)> eachLine)42*38e8c45fSAndroid Build Coastguard Worker static status_t scanBinderContext(pid_t pid, const std::string& contextName,
43*38e8c45fSAndroid Build Coastguard Worker                                   std::function<void(const std::string&)> eachLine) {
44*38e8c45fSAndroid Build Coastguard Worker     std::ifstream ifs("/dev/binderfs/binder_logs/proc/" + std::to_string(pid));
45*38e8c45fSAndroid Build Coastguard Worker     if (!ifs.is_open()) {
46*38e8c45fSAndroid Build Coastguard Worker         ifs.open("/d/binder/proc/" + std::to_string(pid));
47*38e8c45fSAndroid Build Coastguard Worker         if (!ifs.is_open()) {
48*38e8c45fSAndroid Build Coastguard Worker             return -errno;
49*38e8c45fSAndroid Build Coastguard Worker         }
50*38e8c45fSAndroid Build Coastguard Worker     }
51*38e8c45fSAndroid Build Coastguard Worker 
52*38e8c45fSAndroid Build Coastguard Worker     bool isDesiredContext = false;
53*38e8c45fSAndroid Build Coastguard Worker     std::string line;
54*38e8c45fSAndroid Build Coastguard Worker     while (getline(ifs, line)) {
55*38e8c45fSAndroid Build Coastguard Worker         if (base::StartsWith(line, "context")) {
56*38e8c45fSAndroid Build Coastguard Worker             isDesiredContext = base::Split(line, " ").back() == contextName;
57*38e8c45fSAndroid Build Coastguard Worker             continue;
58*38e8c45fSAndroid Build Coastguard Worker         }
59*38e8c45fSAndroid Build Coastguard Worker         if (!isDesiredContext) {
60*38e8c45fSAndroid Build Coastguard Worker             continue;
61*38e8c45fSAndroid Build Coastguard Worker         }
62*38e8c45fSAndroid Build Coastguard Worker         eachLine(line);
63*38e8c45fSAndroid Build Coastguard Worker     }
64*38e8c45fSAndroid Build Coastguard Worker     return OK;
65*38e8c45fSAndroid Build Coastguard Worker }
66*38e8c45fSAndroid Build Coastguard Worker 
67*38e8c45fSAndroid Build Coastguard Worker // Examples of what we are looking at:
68*38e8c45fSAndroid Build Coastguard Worker // node 66730: u00007590061890e0 c0000759036130950 pri 0:120 hs 1 hw 1 ls 0 lw 0 is 2 iw 2 tr 1 proc 2300 1790
69*38e8c45fSAndroid Build Coastguard Worker // thread 2999: l 00 need_return 1 tr 0
getBinderPidInfo(BinderDebugContext context,pid_t pid,BinderPidInfo * pidInfo)70*38e8c45fSAndroid Build Coastguard Worker status_t getBinderPidInfo(BinderDebugContext context, pid_t pid, BinderPidInfo* pidInfo) {
71*38e8c45fSAndroid Build Coastguard Worker     std::smatch match;
72*38e8c45fSAndroid Build Coastguard Worker     static const std::regex kReferencePrefix("^\\s*node \\d+:\\s+u([0-9a-f]+)\\s+c([0-9a-f]+)\\s+");
73*38e8c45fSAndroid Build Coastguard Worker     static const std::regex kThreadPrefix("^\\s*thread \\d+:\\s+l\\s+(\\d)(\\d)");
74*38e8c45fSAndroid Build Coastguard Worker     std::string contextStr = contextToString(context);
75*38e8c45fSAndroid Build Coastguard Worker     status_t ret = scanBinderContext(pid, contextStr, [&](const std::string& line) {
76*38e8c45fSAndroid Build Coastguard Worker         if (base::StartsWith(line, "  node")) {
77*38e8c45fSAndroid Build Coastguard Worker             std::vector<std::string> splitString = base::Tokenize(line, " ");
78*38e8c45fSAndroid Build Coastguard Worker             bool pids = false;
79*38e8c45fSAndroid Build Coastguard Worker             uint64_t ptr = 0;
80*38e8c45fSAndroid Build Coastguard Worker             for (const auto& token : splitString) {
81*38e8c45fSAndroid Build Coastguard Worker                 if (base::StartsWith(token, "u")) {
82*38e8c45fSAndroid Build Coastguard Worker                     const std::string ptrString = "0x" + token.substr(1);
83*38e8c45fSAndroid Build Coastguard Worker                     if (!::android::base::ParseUint(ptrString.c_str(), &ptr)) {
84*38e8c45fSAndroid Build Coastguard Worker                         LOG(ERROR) << "Failed to parse pointer: " << ptrString;
85*38e8c45fSAndroid Build Coastguard Worker                         return;
86*38e8c45fSAndroid Build Coastguard Worker                     }
87*38e8c45fSAndroid Build Coastguard Worker                 } else {
88*38e8c45fSAndroid Build Coastguard Worker                     // The last numbers in the line after "proc" are all client PIDs
89*38e8c45fSAndroid Build Coastguard Worker                     if (token == "proc") {
90*38e8c45fSAndroid Build Coastguard Worker                         pids = true;
91*38e8c45fSAndroid Build Coastguard Worker                     } else if (pids) {
92*38e8c45fSAndroid Build Coastguard Worker                         int32_t pid;
93*38e8c45fSAndroid Build Coastguard Worker                         if (!::android::base::ParseInt(token, &pid)) {
94*38e8c45fSAndroid Build Coastguard Worker                             LOG(ERROR) << "Failed to parse pid int: " << token;
95*38e8c45fSAndroid Build Coastguard Worker                             return;
96*38e8c45fSAndroid Build Coastguard Worker                         }
97*38e8c45fSAndroid Build Coastguard Worker                         if (ptr == 0) {
98*38e8c45fSAndroid Build Coastguard Worker                             LOG(ERROR) << "We failed to parse the pointer, so we can't add the refPids";
99*38e8c45fSAndroid Build Coastguard Worker                             return;
100*38e8c45fSAndroid Build Coastguard Worker                         }
101*38e8c45fSAndroid Build Coastguard Worker                         pidInfo->refPids[ptr].push_back(pid);
102*38e8c45fSAndroid Build Coastguard Worker                     }
103*38e8c45fSAndroid Build Coastguard Worker                 }
104*38e8c45fSAndroid Build Coastguard Worker             }
105*38e8c45fSAndroid Build Coastguard Worker         } else if (base::StartsWith(line, "  thread")) {
106*38e8c45fSAndroid Build Coastguard Worker             auto pos = line.find("l ");
107*38e8c45fSAndroid Build Coastguard Worker             if (pos != std::string::npos) {
108*38e8c45fSAndroid Build Coastguard Worker                 // "1" is waiting in binder driver
109*38e8c45fSAndroid Build Coastguard Worker                 // "2" is poll. It's impossible to tell if these are in use.
110*38e8c45fSAndroid Build Coastguard Worker                 //     and HIDL default code doesn't use it.
111*38e8c45fSAndroid Build Coastguard Worker                 bool isInUse = line.at(pos + 2) != '1';
112*38e8c45fSAndroid Build Coastguard Worker                 // "0" is a thread that has called into binder
113*38e8c45fSAndroid Build Coastguard Worker                 // "1" is looper thread
114*38e8c45fSAndroid Build Coastguard Worker                 // "2" is main looper thread
115*38e8c45fSAndroid Build Coastguard Worker                 bool isBinderThread = line.at(pos + 3) != '0';
116*38e8c45fSAndroid Build Coastguard Worker                 if (!isBinderThread) {
117*38e8c45fSAndroid Build Coastguard Worker                     return;
118*38e8c45fSAndroid Build Coastguard Worker                 }
119*38e8c45fSAndroid Build Coastguard Worker                 if (isInUse) {
120*38e8c45fSAndroid Build Coastguard Worker                     pidInfo->threadUsage++;
121*38e8c45fSAndroid Build Coastguard Worker                 }
122*38e8c45fSAndroid Build Coastguard Worker 
123*38e8c45fSAndroid Build Coastguard Worker                 pidInfo->threadCount++;
124*38e8c45fSAndroid Build Coastguard Worker             }
125*38e8c45fSAndroid Build Coastguard Worker         }
126*38e8c45fSAndroid Build Coastguard Worker     });
127*38e8c45fSAndroid Build Coastguard Worker     return ret;
128*38e8c45fSAndroid Build Coastguard Worker }
129*38e8c45fSAndroid Build Coastguard Worker 
130*38e8c45fSAndroid Build Coastguard Worker // Examples of what we are looking at:
131*38e8c45fSAndroid Build Coastguard Worker // ref 52493: desc 910 node 52492 s 1 w 1 d 0000000000000000
132*38e8c45fSAndroid Build Coastguard Worker // node 29413: u00007803fc982e80 c000078042c982210 pri 0:139 hs 1 hw 1 ls 0 lw 0 is 2 iw 2 tr 1 proc 488 683
getBinderClientPids(BinderDebugContext context,pid_t pid,pid_t servicePid,int32_t handle,std::vector<pid_t> * pids)133*38e8c45fSAndroid Build Coastguard Worker status_t getBinderClientPids(BinderDebugContext context, pid_t pid, pid_t servicePid,
134*38e8c45fSAndroid Build Coastguard Worker                              int32_t handle, std::vector<pid_t>* pids) {
135*38e8c45fSAndroid Build Coastguard Worker     std::smatch match;
136*38e8c45fSAndroid Build Coastguard Worker     static const std::regex kNodeNumber("^\\s+ref \\d+:\\s+desc\\s+(\\d+)\\s+node\\s+(\\d+).*");
137*38e8c45fSAndroid Build Coastguard Worker     std::string contextStr = contextToString(context);
138*38e8c45fSAndroid Build Coastguard Worker     int32_t node;
139*38e8c45fSAndroid Build Coastguard Worker     status_t ret = scanBinderContext(pid, contextStr, [&](const std::string& line) {
140*38e8c45fSAndroid Build Coastguard Worker         if (!base::StartsWith(line, "  ref")) return;
141*38e8c45fSAndroid Build Coastguard Worker 
142*38e8c45fSAndroid Build Coastguard Worker         std::vector<std::string> splitString = base::Tokenize(line, " ");
143*38e8c45fSAndroid Build Coastguard Worker         if (splitString.size() < 12) {
144*38e8c45fSAndroid Build Coastguard Worker             LOG(ERROR) << "Failed to parse binder_logs ref entry. Expecting size greater than 11, but got: " << splitString.size();
145*38e8c45fSAndroid Build Coastguard Worker             return;
146*38e8c45fSAndroid Build Coastguard Worker         }
147*38e8c45fSAndroid Build Coastguard Worker         int32_t desc;
148*38e8c45fSAndroid Build Coastguard Worker         if (!::android::base::ParseInt(splitString[3].c_str(), &desc)) {
149*38e8c45fSAndroid Build Coastguard Worker             LOG(ERROR) << "Failed to parse desc int: " << splitString[3];
150*38e8c45fSAndroid Build Coastguard Worker             return;
151*38e8c45fSAndroid Build Coastguard Worker         }
152*38e8c45fSAndroid Build Coastguard Worker         if (handle != desc) {
153*38e8c45fSAndroid Build Coastguard Worker             return;
154*38e8c45fSAndroid Build Coastguard Worker         }
155*38e8c45fSAndroid Build Coastguard Worker         if (!::android::base::ParseInt(splitString[5].c_str(), &node)) {
156*38e8c45fSAndroid Build Coastguard Worker             LOG(ERROR) << "Failed to parse node int: " << splitString[5];
157*38e8c45fSAndroid Build Coastguard Worker             return;
158*38e8c45fSAndroid Build Coastguard Worker         }
159*38e8c45fSAndroid Build Coastguard Worker         LOG(INFO) << "Parsed the node: " << node;
160*38e8c45fSAndroid Build Coastguard Worker     });
161*38e8c45fSAndroid Build Coastguard Worker     if (ret != OK) {
162*38e8c45fSAndroid Build Coastguard Worker         return ret;
163*38e8c45fSAndroid Build Coastguard Worker     }
164*38e8c45fSAndroid Build Coastguard Worker 
165*38e8c45fSAndroid Build Coastguard Worker     ret = scanBinderContext(servicePid, contextStr, [&](const std::string& line) {
166*38e8c45fSAndroid Build Coastguard Worker         if (!base::StartsWith(line, "  node")) return;
167*38e8c45fSAndroid Build Coastguard Worker 
168*38e8c45fSAndroid Build Coastguard Worker         std::vector<std::string> splitString = base::Tokenize(line, " ");
169*38e8c45fSAndroid Build Coastguard Worker         if (splitString.size() < 21) {
170*38e8c45fSAndroid Build Coastguard Worker             LOG(ERROR) << "Failed to parse binder_logs node entry. Expecting size greater than 20, but got: " << splitString.size();
171*38e8c45fSAndroid Build Coastguard Worker             return;
172*38e8c45fSAndroid Build Coastguard Worker         }
173*38e8c45fSAndroid Build Coastguard Worker 
174*38e8c45fSAndroid Build Coastguard Worker         // remove the colon
175*38e8c45fSAndroid Build Coastguard Worker         const std::string nodeString = splitString[1].substr(0, splitString[1].size() - 1);
176*38e8c45fSAndroid Build Coastguard Worker         int32_t matchedNode;
177*38e8c45fSAndroid Build Coastguard Worker         if (!::android::base::ParseInt(nodeString.c_str(), &matchedNode)) {
178*38e8c45fSAndroid Build Coastguard Worker             LOG(ERROR) << "Failed to parse node int: " << nodeString;
179*38e8c45fSAndroid Build Coastguard Worker             return;
180*38e8c45fSAndroid Build Coastguard Worker         }
181*38e8c45fSAndroid Build Coastguard Worker 
182*38e8c45fSAndroid Build Coastguard Worker         if (node != matchedNode) {
183*38e8c45fSAndroid Build Coastguard Worker             return;
184*38e8c45fSAndroid Build Coastguard Worker         }
185*38e8c45fSAndroid Build Coastguard Worker         bool pidsSection = false;
186*38e8c45fSAndroid Build Coastguard Worker         for (const auto& token : splitString) {
187*38e8c45fSAndroid Build Coastguard Worker             if (token == "proc") {
188*38e8c45fSAndroid Build Coastguard Worker                 pidsSection = true;
189*38e8c45fSAndroid Build Coastguard Worker             } else if (pidsSection == true) {
190*38e8c45fSAndroid Build Coastguard Worker                 int32_t pid;
191*38e8c45fSAndroid Build Coastguard Worker                 if (!::android::base::ParseInt(token.c_str(), &pid)) {
192*38e8c45fSAndroid Build Coastguard Worker                     LOG(ERROR) << "Failed to parse PID int: " << token;
193*38e8c45fSAndroid Build Coastguard Worker                     return;
194*38e8c45fSAndroid Build Coastguard Worker                 }
195*38e8c45fSAndroid Build Coastguard Worker                 pids->push_back(pid);
196*38e8c45fSAndroid Build Coastguard Worker             }
197*38e8c45fSAndroid Build Coastguard Worker         }
198*38e8c45fSAndroid Build Coastguard Worker     });
199*38e8c45fSAndroid Build Coastguard Worker     return ret;
200*38e8c45fSAndroid Build Coastguard Worker }
201*38e8c45fSAndroid Build Coastguard Worker 
getBinderTransactions(pid_t pid,std::string & transactionsOutput)202*38e8c45fSAndroid Build Coastguard Worker status_t getBinderTransactions(pid_t pid, std::string& transactionsOutput) {
203*38e8c45fSAndroid Build Coastguard Worker     std::ifstream ifs("/dev/binderfs/binder_logs/transactions");
204*38e8c45fSAndroid Build Coastguard Worker     if (!ifs.is_open()) {
205*38e8c45fSAndroid Build Coastguard Worker         ifs.open("/d/binder/transactions");
206*38e8c45fSAndroid Build Coastguard Worker         if (!ifs.is_open()) {
207*38e8c45fSAndroid Build Coastguard Worker             LOG(ERROR) << "Could not open /dev/binderfs/binder_logs/transactions. "
208*38e8c45fSAndroid Build Coastguard Worker                        << "Likely a permissions issue. errno: " << errno;
209*38e8c45fSAndroid Build Coastguard Worker             return -errno;
210*38e8c45fSAndroid Build Coastguard Worker         }
211*38e8c45fSAndroid Build Coastguard Worker     }
212*38e8c45fSAndroid Build Coastguard Worker 
213*38e8c45fSAndroid Build Coastguard Worker     std::string line;
214*38e8c45fSAndroid Build Coastguard Worker     while (getline(ifs, line)) {
215*38e8c45fSAndroid Build Coastguard Worker         // The section for this pid ends with another "proc <pid>" for another
216*38e8c45fSAndroid Build Coastguard Worker         // process. There is only one entry per pid so we can stop looking after
217*38e8c45fSAndroid Build Coastguard Worker         // we've grabbed the whole section
218*38e8c45fSAndroid Build Coastguard Worker         if (base::StartsWith(line, "proc " + std::to_string(pid))) {
219*38e8c45fSAndroid Build Coastguard Worker             do {
220*38e8c45fSAndroid Build Coastguard Worker                 transactionsOutput += line + '\n';
221*38e8c45fSAndroid Build Coastguard Worker             } while (getline(ifs, line) && !base::StartsWith(line, "proc "));
222*38e8c45fSAndroid Build Coastguard Worker             return OK;
223*38e8c45fSAndroid Build Coastguard Worker         }
224*38e8c45fSAndroid Build Coastguard Worker     }
225*38e8c45fSAndroid Build Coastguard Worker 
226*38e8c45fSAndroid Build Coastguard Worker     return NAME_NOT_FOUND;
227*38e8c45fSAndroid Build Coastguard Worker }
228*38e8c45fSAndroid Build Coastguard Worker 
229*38e8c45fSAndroid Build Coastguard Worker } // namespace  android
230