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 #define LOG_TAG "uprobestats"
18
19 #include <filesystem>
20 #include <iostream>
21 #include <string>
22
23 #include <android-base/file.h>
24 #include <android-base/logging.h>
25 #include <android-base/parseint.h>
26 #include <android-base/properties.h>
27 #include <android-base/strings.h>
28 #include <config.pb.h>
29 #include <json/json.h>
30
31 #include "Art.h"
32 #include "ConfigResolver.h"
33 #include "Process.h"
34
35 namespace android {
36 namespace uprobestats {
37 namespace config_resolver {
38
operator <<(std::ostream & os,const ResolvedTask & c)39 std::ostream &operator<<(std::ostream &os, const ResolvedTask &c) {
40 os << "pid: " << c.pid << " task_config: " << c.task_config.DebugString();
41 return os;
42 }
43
operator <<(std::ostream & os,const ResolvedProbe & c)44 std::ostream &operator<<(std::ostream &os, const ResolvedProbe &c) {
45 os << "filename: " << c.filename << " offset: " << c.offset
46 << " probe_config: " << c.probe_config.DebugString();
47 return os;
48 }
49
50 // Reads probing configuration from a file, which should be the serialized
51 // bytes of a UprobestatsConfig proto.
52 std::optional<::uprobestats::protos::UprobestatsConfig>
readConfig(std::string configFilePath)53 readConfig(std::string configFilePath) {
54 std::string config_str;
55 if (!android::base::ReadFileToString(configFilePath, &config_str)) {
56 LOG(ERROR) << "Failed to open config file " << configFilePath;
57 return {};
58 }
59
60 ::uprobestats::protos::UprobestatsConfig config;
61 bool success = config.ParseFromString(config_str);
62 if (!success) {
63 LOG(ERROR) << "Failed to parse file " << configFilePath
64 << " to UprobestatsConfig";
65 return {};
66 }
67
68 return config;
69 }
70
71 std::optional<ResolvedTask>
resolveSingleTask(::uprobestats::protos::UprobestatsConfig config)72 resolveSingleTask(::uprobestats::protos::UprobestatsConfig config) {
73 auto task_count = config.tasks().size();
74 if (task_count == 0) {
75 LOG(ERROR) << "config has no tasks";
76 return {};
77 }
78 if (task_count > 1) {
79 LOG(ERROR) << "config has " << task_count
80 << " tasks. Only 1 is supported. The first task is read and the "
81 "rest are ignored.";
82 }
83 auto task_config = config.tasks().Get(0);
84 if (!task_config.has_duration_seconds()) {
85 LOG(ERROR) << "config task has no duration";
86 return {};
87 }
88 if (task_config.duration_seconds() <= 0) {
89 LOG(ERROR) << "config task cannot have zero or negative duration";
90 }
91 if (!task_config.has_target_process_name()) {
92 LOG(ERROR) << "task.target_process_name is required.";
93 return {};
94 }
95 auto process_name = task_config.target_process_name();
96 int pid = process::getPid(process_name);
97 if (pid < 0) {
98 LOG(ERROR) << "Unable to find pid of " << process_name;
99 return {};
100 }
101 ResolvedTask task;
102 task.task_config = task_config;
103 task.pid = pid;
104 return task;
105 }
106
107 std::optional<std::vector<ResolvedProbe>>
resolveProbes(::uprobestats::protos::UprobestatsConfig::Task task_config)108 resolveProbes(::uprobestats::protos::UprobestatsConfig::Task task_config) {
109 if (task_config.probe_configs().size() == 0) {
110 LOG(ERROR) << "task has no probe configs";
111 return {};
112 }
113 std::vector<ResolvedProbe> result;
114 for (auto &probe_config : task_config.probe_configs()) {
115 int offset = 0;
116 std::string matched_file_path;
117 for (auto &file_path : probe_config.file_paths()) {
118 offset = art::getMethodOffsetFromOatdump(file_path,
119 probe_config.method_signature());
120 if (offset > 0) {
121 matched_file_path = file_path;
122 break;
123 }
124 }
125 if (offset == 0) {
126 LOG(ERROR) << "Unable to find method offset for "
127 << probe_config.method_signature();
128 return {};
129 }
130
131 ResolvedProbe probe;
132 probe.filename = matched_file_path;
133 probe.offset = offset;
134 probe.probe_config = probe_config;
135 result.push_back(probe);
136 }
137
138 return result;
139 }
140
141 } // namespace config_resolver
142 } // namespace uprobestats
143 } // namespace android