BinderUtil.h 3.9 KB

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