1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef NETD_INCLUDE_BINDER_UTIL_H
18 #define NETD_INCLUDE_BINDER_UTIL_H
19
20 #ifdef ANDROID_BINDER_STATUS_H
21 #define IS_BINDER_OK(__ex__) (__ex__ == ::android::binder::Status::EX_NONE)
22
23 #define EXCEPTION_TO_STRING(__ex__, str) \
24 case ::android::binder::Status::__ex__: \
25 return str;
26
27 #define TO_EXCEPTION(__ex__) __ex__;
28
29 #else
30 #define IS_BINDER_OK(__ex__) (AStatus_isOk(AStatus_fromExceptionCode(__ex__)))
31
32 #define EXCEPTION_TO_STRING(__ex__, str) \
33 case __ex__: \
34 return str;
35
36 #define TO_EXCEPTION(__ex__) AStatus_getExceptionCode(AStatus_fromExceptionCode(__ex__));
37
38 #endif
39
exceptionToString(int32_t exception)40 std::string exceptionToString(int32_t exception) {
41 switch (exception) {
42 EXCEPTION_TO_STRING(EX_SECURITY, "SecurityException")
43 EXCEPTION_TO_STRING(EX_BAD_PARCELABLE, "BadParcelableException")
44 EXCEPTION_TO_STRING(EX_ILLEGAL_ARGUMENT, "IllegalArgumentException")
45 EXCEPTION_TO_STRING(EX_NULL_POINTER, "NullPointerException")
46 EXCEPTION_TO_STRING(EX_ILLEGAL_STATE, "IllegalStateException")
47 EXCEPTION_TO_STRING(EX_NETWORK_MAIN_THREAD, "NetworkMainThreadException")
48 EXCEPTION_TO_STRING(EX_UNSUPPORTED_OPERATION, "UnsupportedOperationException")
49 EXCEPTION_TO_STRING(EX_SERVICE_SPECIFIC, "ServiceSpecificException")
50 EXCEPTION_TO_STRING(EX_PARCELABLE, "ParcelableException")
51 EXCEPTION_TO_STRING(EX_TRANSACTION_FAILED, "TransactionFailedException")
52 default:
53 return "UnknownException";
54 }
55 }
56
57 using LogFn = std::function<void(const std::string& msg)>;
58
binderCallLogFn(const Json::Value & logTransaction,const LogFn & logFn)59 void binderCallLogFn(const Json::Value& logTransaction, const LogFn& logFn) {
60 using namespace std::string_literals;
61
62 bool hasReturnArgs;
63 std::string output;
64 const Json::Value& returnArgs = logTransaction["_aidl_return"];
65 const Json::Value& inputArgsArray = logTransaction["input_args"];
66
67 hasReturnArgs = !returnArgs.empty();
68 output.append(logTransaction["method_name"].asString() + "("s);
69
70 // input args
71 Json::FastWriter fastWriter;
72 fastWriter.omitEndingLineFeed();
73 for (Json::Value::ArrayIndex i = 0; i < inputArgsArray.size(); ++i) {
74 std::string value = fastWriter.write(inputArgsArray[i]["value"]);
75 output.append(value);
76 if (i != inputArgsArray.size() - 1) {
77 output.append(", "s);
78 }
79 }
80 output.append(")"s);
81
82 const int exceptionCode =
83 TO_EXCEPTION(logTransaction["binder_status"]["exception_code"].asInt());
84
85 if (hasReturnArgs || !IS_BINDER_OK(exceptionCode)) {
86 output.append(" -> "s);
87 }
88
89 // return status
90 if (!IS_BINDER_OK(exceptionCode)) {
91 // an exception occurred
92 const int errCode = logTransaction["binder_status"]["service_specific_error_code"].asInt();
93 output.append(::android::base::StringPrintf(
94 "%s(%d, \"%s\")", exceptionToString(exceptionCode).c_str(),
95 (errCode != 0) ? errCode : exceptionCode,
96 logTransaction["binder_status"]["exception_message"].asString().c_str()));
97 }
98 // return args
99 if (hasReturnArgs) {
100 output.append(::android::base::StringPrintf("{%s}", fastWriter.write(returnArgs).c_str()));
101 }
102 // duration time
103 output.append(
104 ::android::base::StringPrintf(" <%.2fms>", logTransaction["duration_ms"].asFloat()));
105 logFn(output);
106 }
107
108 #endif /* NETD_INCLUDE_BINDER_UTIL_H */
109