xref: /aosp_15_r20/external/perfetto/src/android_internal/tracing_service_proxy.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2021 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 #include "src/android_internal/tracing_service_proxy.h"
18 
19 #include <android/tracing/ITracingServiceProxy.h>
20 #include <android/tracing/TraceReportParams.h>
21 #include <binder/IBinder.h>
22 #include <binder/IServiceManager.h>
23 #include <binder/ParcelFileDescriptor.h>
24 #include <binder/Status.h>
25 #include <utils/String16.h>
26 
27 namespace perfetto {
28 namespace android_internal {
29 
30 using android::sp;
31 using android::binder::Status;
32 using android::os::ParcelFileDescriptor;
33 using android::tracing::ITracingServiceProxy;
34 using android::tracing::TraceReportParams;
35 
36 namespace {
37 static constexpr char kServiceName[] = "tracing.proxy";
38 }
39 
NotifyTraceSessionEnded(bool session_stolen)40 bool NotifyTraceSessionEnded(bool session_stolen) {
41   auto service = android::waitForService<ITracingServiceProxy>(
42       android::String16(kServiceName));
43   if (service == nullptr) {
44     return false;
45   }
46 
47   Status s = service->notifyTraceSessionEnded(session_stolen);
48   return s.isOk();
49 }
50 
ReportTrace(const char * reporter_package_name,const char * reporter_class_name,int owned_trace_fd,int64_t uuid_lsb,int64_t uuid_msb,bool use_pipe_in_framework_for_testing)51 bool ReportTrace(const char* reporter_package_name,
52                  const char* reporter_class_name,
53                  int owned_trace_fd,
54                  int64_t uuid_lsb,
55                  int64_t uuid_msb,
56                  bool use_pipe_in_framework_for_testing) {
57   // Keep this first so we recapture the raw fd in a RAII type as soon as
58   // possible.
59   android::base::unique_fd fd(owned_trace_fd);
60 
61   auto service = android::waitForService<ITracingServiceProxy>(
62       android::String16(kServiceName));
63   if (service == nullptr) {
64     return false;
65   }
66 
67   TraceReportParams params{};
68   params.reporterPackageName = android::String16(reporter_package_name);
69   params.reporterClassName = android::String16(reporter_class_name);
70   params.fd = ParcelFileDescriptor(std::move(fd));
71   params.uuidLsb = uuid_lsb;
72   params.uuidMsb = uuid_msb;
73   params.usePipeForTesting = use_pipe_in_framework_for_testing;
74 
75   Status s = service->reportTrace(std::move(params));
76   if (!s.isOk()) {
77     __android_log_print(ANDROID_LOG_ERROR, "perfetto", "reportTrace failed: %s",
78                         s.toString8().c_str());
79   }
80 
81   return s.isOk();
82 }
83 
84 }  // namespace android_internal
85 }  // namespace perfetto
86