• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
18 
19 #include <android-base/stringprintf.h>
20 #include <json/value.h>
21 #include <json/writer.h>
22 
23 #ifdef ANDROID_BINDER_STATUS_H
24 #define IS_BINDER_OK(__ex__) (__ex__ == ::android::binder::Status::EX_NONE)
25 
26 #define EXCEPTION_TO_STRING(__ex__, str)    \
27     case ::android::binder::Status::__ex__: \
28         return str;
29 
30 #define TO_EXCEPTION(__ex__) __ex__;
31 
32 #else
33 #define IS_BINDER_OK(__ex__) (AStatus_isOk(AStatus_fromExceptionCode(__ex__)))
34 
35 #define EXCEPTION_TO_STRING(__ex__, str) \
36     case __ex__:                         \
37         return str;
38 
39 #define TO_EXCEPTION(__ex__) AStatus_getExceptionCode(AStatus_fromExceptionCode(__ex__));
40 
41 #endif
42 
exceptionToString(int32_t exception)43 std::string exceptionToString(int32_t exception) {
44     switch (exception) {
45         EXCEPTION_TO_STRING(EX_SECURITY, "SecurityException")
46         EXCEPTION_TO_STRING(EX_BAD_PARCELABLE, "BadParcelableException")
47         EXCEPTION_TO_STRING(EX_ILLEGAL_ARGUMENT, "IllegalArgumentException")
48         EXCEPTION_TO_STRING(EX_NULL_POINTER, "NullPointerException")
49         EXCEPTION_TO_STRING(EX_ILLEGAL_STATE, "IllegalStateException")
50         EXCEPTION_TO_STRING(EX_NETWORK_MAIN_THREAD, "NetworkMainThreadException")
51         EXCEPTION_TO_STRING(EX_UNSUPPORTED_OPERATION, "UnsupportedOperationException")
52         EXCEPTION_TO_STRING(EX_SERVICE_SPECIFIC, "ServiceSpecificException")
53         EXCEPTION_TO_STRING(EX_PARCELABLE, "ParcelableException")
54         EXCEPTION_TO_STRING(EX_TRANSACTION_FAILED, "TransactionFailedException")
55         default:
56             return "UnknownException";
57     }
58 }
59 
60 using LogFn = std::function<void(const std::string& msg)>;
61 
binderCallLogFn(const Json::Value & logTransaction,const LogFn & logFn)62 void binderCallLogFn(const Json::Value& logTransaction, const LogFn& logFn) {
63     using namespace std::string_literals;
64 
65     bool hasReturnArgs;
66     std::string output;
67     const Json::Value& returnArgs = logTransaction["_aidl_return"];
68     const Json::Value& inputArgsArray = logTransaction["input_args"];
69 
70     hasReturnArgs = !returnArgs.empty();
71     output.append(logTransaction["method_name"].asString() + "("s);
72 
73     // input args
74     Json::FastWriter fastWriter;
75     fastWriter.omitEndingLineFeed();
76     for (Json::Value::ArrayIndex i = 0; i < inputArgsArray.size(); ++i) {
77         std::string value = fastWriter.write(inputArgsArray[i]["value"]);
78         output.append(value);
79         if (i != inputArgsArray.size() - 1) {
80             output.append(", "s);
81         }
82     }
83     output.append(")"s);
84 
85     const int exceptionCode =
86             TO_EXCEPTION(logTransaction["binder_status"]["exception_code"].asInt());
87 
88     if (hasReturnArgs || !IS_BINDER_OK(exceptionCode)) {
89         output.append(" -> "s);
90     }
91 
92     // return status
93     if (!IS_BINDER_OK(exceptionCode)) {
94         // an exception occurred
95         const int errCode = logTransaction["binder_status"]["service_specific_error_code"].asInt();
96         output.append(::android::base::StringPrintf(
97                 "%s(%d, \"%s\")", exceptionToString(exceptionCode).c_str(),
98                 (errCode != 0) ? errCode : exceptionCode,
99                 logTransaction["binder_status"]["exception_message"].asString().c_str()));
100     }
101     // return args
102     if (hasReturnArgs) {
103         output.append(::android::base::StringPrintf("{%s}", fastWriter.write(returnArgs).c_str()));
104     }
105     // duration time
106     output.append(
107             ::android::base::StringPrintf(" <%.2fms>", logTransaction["duration_ms"].asFloat()));
108     logFn(output);
109 }
110