1 /*
2 * Copyright (C) 2024 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 "Log.h"
18
19 #include <com_android_os_statsd_flags.h>
20 #include <dlfcn.h>
21
22 #include <string>
23
24 #include "src/statsd_config.pb.h" // Alert
25
26 namespace android {
27 namespace os {
28 namespace statsd {
29
30 namespace {
31 typedef int (*AUprobestatsClient_startUprobestatsFn)(const uint8_t* config, int64_t size);
32
33 const char kLibuprobestatsClientPath[] = "libuprobestats_client.so";
34
libInit()35 AUprobestatsClient_startUprobestatsFn libInit() {
36 if (__builtin_available(android __ANDROID_API_V__, *)) {
37 void* handle = dlopen(kLibuprobestatsClientPath, RTLD_NOW | RTLD_LOCAL);
38 if (!handle) {
39 ALOGE("dlopen error: %s %s", __func__, dlerror());
40 return nullptr;
41 }
42 auto f = reinterpret_cast<AUprobestatsClient_startUprobestatsFn>(
43 dlsym(handle, "AUprobestatsClient_startUprobestats"));
44 if (!f) {
45 ALOGE("dlsym error: %s %s", __func__, dlerror());
46 return nullptr;
47 }
48 return f;
49 }
50 return nullptr;
51 }
52
53 namespace flags = com::android::os::statsd::flags;
54
55 } // namespace
56
StartUprobeStats(const UprobestatsDetails & config)57 bool StartUprobeStats(const UprobestatsDetails& config) {
58 if (!flags::trigger_uprobestats()) {
59 return false;
60 }
61 static AUprobestatsClient_startUprobestatsFn AUprobestatsClient_startUprobestats = libInit();
62 if (AUprobestatsClient_startUprobestats == nullptr) {
63 return false;
64 }
65 if (!config.has_config()) {
66 ALOGE("The uprobestats trace config is empty, aborting");
67 return false;
68 }
69 const std::string& cfgProto = config.config();
70 AUprobestatsClient_startUprobestats(reinterpret_cast<const uint8_t*>(cfgProto.c_str()),
71 cfgProto.length());
72 return true;
73 }
74
75 } // namespace statsd
76 } // namespace os
77 } // namespace android
78