Vicent Marti | 0865f94 | 2016-04-27 16:57:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 GitHub, Inc. |
| 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 | */ |
Teng Qin | 488c119 | 2017-09-13 10:47:59 -0700 | [diff] [blame] | 16 | #include <algorithm> |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 17 | #include <cstring> |
Vicent Marti | 1a2ddac | 2016-05-01 13:41:33 +0200 | [diff] [blame] | 18 | #include <sstream> |
Yonghong Song | 0ba1507 | 2017-07-21 22:13:20 -0700 | [diff] [blame] | 19 | #include <unordered_set> |
Vicent Marti | 0865f94 | 2016-04-27 16:57:58 +0200 | [diff] [blame] | 20 | |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 21 | #include <fcntl.h> |
Javier Honduvilla Coto | dcb9b9a | 2018-05-14 13:02:05 +0200 | [diff] [blame] | 22 | #include <sys/stat.h> |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
| 25 | |
Vicent Marti | 0865f94 | 2016-04-27 16:57:58 +0200 | [diff] [blame] | 26 | #include "bcc_elf.h" |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 27 | #include "bcc_proc.h" |
Teng Qin | 9756295 | 2017-09-18 13:36:49 -0700 | [diff] [blame] | 28 | #include "common.h" |
Vicent Marti | 0865f94 | 2016-04-27 16:57:58 +0200 | [diff] [blame] | 29 | #include "usdt.h" |
| 30 | #include "vendor/tinyformat.hpp" |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 31 | #include "bcc_usdt.h" |
Vicent Marti | 0865f94 | 2016-04-27 16:57:58 +0200 | [diff] [blame] | 32 | |
| 33 | namespace USDT { |
| 34 | |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 35 | Location::Location(uint64_t addr, const std::string &bin_path, const char *arg_fmt) |
| 36 | : address_(addr), |
| 37 | bin_path_(bin_path) { |
| 38 | |
Yonghong Song | c2d9880 | 2017-10-26 10:55:05 -0700 | [diff] [blame] | 39 | #ifdef __aarch64__ |
| 40 | ArgumentParser_aarch64 parser(arg_fmt); |
| 41 | #elif __powerpc64__ |
Sandipan Das | 1f0f7b6 | 2017-09-26 15:29:18 +0530 | [diff] [blame] | 42 | ArgumentParser_powerpc64 parser(arg_fmt); |
Sandipan Das | 1f0f7b6 | 2017-09-26 15:29:18 +0530 | [diff] [blame] | 43 | #else |
Sandipan Das | 9ab1cee | 2017-10-10 10:29:29 +0530 | [diff] [blame] | 44 | ArgumentParser_x64 parser(arg_fmt); |
Sandipan Das | 1f0f7b6 | 2017-09-26 15:29:18 +0530 | [diff] [blame] | 45 | #endif |
Vicent Marti | 0865f94 | 2016-04-27 16:57:58 +0200 | [diff] [blame] | 46 | while (!parser.done()) { |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 47 | Argument arg; |
| 48 | if (!parser.parse(&arg)) |
Vicent Marti | 0865f94 | 2016-04-27 16:57:58 +0200 | [diff] [blame] | 49 | continue; |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 50 | arguments_.push_back(std::move(arg)); |
Vicent Marti | 0865f94 | 2016-04-27 16:57:58 +0200 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | Probe::Probe(const char *bin_path, const char *provider, const char *name, |
Vicent Marti | 96c1b8e | 2017-06-27 19:06:46 +0200 | [diff] [blame] | 55 | uint64_t semaphore, const optional<int> &pid, ProcMountNS *ns) |
Vicent Marti | 0865f94 | 2016-04-27 16:57:58 +0200 | [diff] [blame] | 56 | : bin_path_(bin_path), |
| 57 | provider_(provider), |
| 58 | name_(name), |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 59 | semaphore_(semaphore), |
Vicent Marti | 96c1b8e | 2017-06-27 19:06:46 +0200 | [diff] [blame] | 60 | pid_(pid), |
| 61 | mount_ns_(ns) {} |
Vicent Marti | 0865f94 | 2016-04-27 16:57:58 +0200 | [diff] [blame] | 62 | |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 63 | bool Probe::in_shared_object(const std::string &bin_path) { |
| 64 | if (object_type_map_.find(bin_path) == object_type_map_.end()) { |
| 65 | ProcMountNSGuard g(mount_ns_); |
| 66 | return (object_type_map_[bin_path] = bcc_elf_is_shared_obj(bin_path.c_str())); |
| 67 | } |
| 68 | return object_type_map_[bin_path]; |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 71 | bool Probe::resolve_global_address(uint64_t *global, const std::string &bin_path, |
| 72 | const uint64_t addr) { |
| 73 | if (in_shared_object(bin_path)) { |
Vicent Marti | 040df7d | 2016-05-04 16:59:57 +0200 | [diff] [blame] | 74 | return (pid_ && |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 75 | !bcc_resolve_global_addr(*pid_, bin_path.c_str(), addr, global)); |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | *global = addr; |
| 79 | return true; |
| 80 | } |
| 81 | |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 82 | bool Probe::add_to_semaphore(int16_t val) { |
Sasha Goldshtein | 9ca8ac2 | 2017-03-03 15:13:16 -0500 | [diff] [blame] | 83 | assert(pid_); |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 84 | |
| 85 | if (!attached_semaphore_) { |
| 86 | uint64_t addr; |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 87 | if (!resolve_global_address(&addr, bin_path_, semaphore_)) |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 88 | return false; |
| 89 | attached_semaphore_ = addr; |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 92 | off_t address = static_cast<off_t>(attached_semaphore_.value()); |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 93 | |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 94 | std::string procmem = tfm::format("/proc/%d/mem", pid_.value()); |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 95 | int memfd = ::open(procmem.c_str(), O_RDWR); |
| 96 | if (memfd < 0) |
| 97 | return false; |
| 98 | |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 99 | int16_t original; |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 100 | |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 101 | if (::lseek(memfd, address, SEEK_SET) < 0 || |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 102 | ::read(memfd, &original, 2) != 2) { |
| 103 | ::close(memfd); |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | original = original + val; |
| 108 | |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 109 | if (::lseek(memfd, address, SEEK_SET) < 0 || |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 110 | ::write(memfd, &original, 2) != 2) { |
| 111 | ::close(memfd); |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | ::close(memfd); |
| 116 | return true; |
| 117 | } |
| 118 | |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 119 | bool Probe::enable(const std::string &fn_name) { |
| 120 | if (attached_to_) |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 121 | return false; |
| 122 | |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 123 | if (need_enable()) { |
| 124 | if (!pid_) |
| 125 | return false; |
| 126 | |
| 127 | if (!add_to_semaphore(+1)) |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | attached_to_ = fn_name; |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 132 | return true; |
| 133 | } |
| 134 | |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 135 | bool Probe::disable() { |
| 136 | if (!attached_to_) |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 137 | return false; |
| 138 | |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 139 | attached_to_ = nullopt; |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 140 | |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 141 | if (need_enable()) { |
| 142 | assert(pid_); |
| 143 | return add_to_semaphore(-1); |
| 144 | } |
| 145 | return true; |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 148 | std::string Probe::largest_arg_type(size_t arg_n) { |
| 149 | Argument *largest = nullptr; |
| 150 | for (Location &location : locations_) { |
| 151 | Argument *candidate = &location.arguments_[arg_n]; |
| 152 | if (!largest || |
Vicent Marti | 1a2ddac | 2016-05-01 13:41:33 +0200 | [diff] [blame] | 153 | std::abs(candidate->arg_size()) > std::abs(largest->arg_size())) |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 154 | largest = candidate; |
| 155 | } |
| 156 | |
| 157 | assert(largest); |
| 158 | return largest->ctype(); |
| 159 | } |
| 160 | |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 161 | bool Probe::usdt_getarg(std::ostream &stream) { |
Teng Qin | 8265aca | 2018-06-26 08:33:34 -0700 | [diff] [blame] | 162 | if (!attached_to_ || attached_to_->empty()) |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 163 | return false; |
| 164 | |
Teng Qin | 8265aca | 2018-06-26 08:33:34 -0700 | [diff] [blame] | 165 | return usdt_getarg(stream, attached_to_.value()); |
| 166 | } |
| 167 | |
| 168 | bool Probe::usdt_getarg(std::ostream &stream, const std::string& probe_func) { |
| 169 | const size_t arg_count = locations_[0].arguments_.size(); |
| 170 | |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 171 | if (arg_count == 0) |
| 172 | return true; |
| 173 | |
| 174 | for (size_t arg_n = 0; arg_n < arg_count; ++arg_n) { |
| 175 | std::string ctype = largest_arg_type(arg_n); |
Yonghong Song | e09564d | 2017-06-05 17:55:24 -0700 | [diff] [blame] | 176 | std::string cptr = tfm::format("*((%s *)dest)", ctype); |
Vicent Marti | 4ea4af4 | 2016-05-04 13:01:55 +0200 | [diff] [blame] | 177 | |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 178 | tfm::format(stream, |
Sasha Goldshtein | 2070a2a | 2017-05-03 07:07:23 -0400 | [diff] [blame] | 179 | "static __always_inline int _bpf_readarg_%s_%d(" |
Vicent Marti | 040df7d | 2016-05-04 16:59:57 +0200 | [diff] [blame] | 180 | "struct pt_regs *ctx, void *dest, size_t len) {\n" |
| 181 | " if (len != sizeof(%s)) return -1;\n", |
Teng Qin | 8265aca | 2018-06-26 08:33:34 -0700 | [diff] [blame] | 182 | probe_func, arg_n + 1, ctype); |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 183 | |
| 184 | if (locations_.size() == 1) { |
| 185 | Location &location = locations_.front(); |
| 186 | stream << " "; |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 187 | if (!location.arguments_[arg_n].assign_to_local(stream, cptr, location.bin_path_, |
Vicent Marti | 040df7d | 2016-05-04 16:59:57 +0200 | [diff] [blame] | 188 | pid_)) |
Vicent Marti | 1a2ddac | 2016-05-01 13:41:33 +0200 | [diff] [blame] | 189 | return false; |
Vicent Marti | 4ea4af4 | 2016-05-04 13:01:55 +0200 | [diff] [blame] | 190 | stream << "\n return 0;\n}\n"; |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 191 | } else { |
Sandipan Das | 1f0f7b6 | 2017-09-26 15:29:18 +0530 | [diff] [blame] | 192 | stream << " switch(PT_REGS_IP(ctx)) {\n"; |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 193 | for (Location &location : locations_) { |
Vicent Marti | 1a2ddac | 2016-05-01 13:41:33 +0200 | [diff] [blame] | 194 | uint64_t global_address; |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 195 | |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 196 | if (!resolve_global_address(&global_address, location.bin_path_, |
| 197 | location.address_)) |
Vicent Marti | 1a2ddac | 2016-05-01 13:41:33 +0200 | [diff] [blame] | 198 | return false; |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 199 | |
Vicent Marti | 1a2ddac | 2016-05-01 13:41:33 +0200 | [diff] [blame] | 200 | tfm::format(stream, " case 0x%xULL: ", global_address); |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 201 | if (!location.arguments_[arg_n].assign_to_local(stream, cptr, location.bin_path_, |
Vicent Marti | 040df7d | 2016-05-04 16:59:57 +0200 | [diff] [blame] | 202 | pid_)) |
Vicent Marti | 1a2ddac | 2016-05-01 13:41:33 +0200 | [diff] [blame] | 203 | return false; |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 204 | |
Vicent Marti | 4ea4af4 | 2016-05-04 13:01:55 +0200 | [diff] [blame] | 205 | stream << " return 0;\n"; |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 206 | } |
| 207 | stream << " }\n"; |
Vicent Marti | 4ea4af4 | 2016-05-04 13:01:55 +0200 | [diff] [blame] | 208 | stream << " return -1;\n}\n"; |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 209 | } |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 210 | } |
| 211 | return true; |
| 212 | } |
| 213 | |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 214 | void Probe::add_location(uint64_t addr, const std::string &bin_path, const char *fmt) { |
| 215 | locations_.emplace_back(addr, bin_path, fmt); |
Vicent Marti | 0865f94 | 2016-04-27 16:57:58 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Teng Qin | 488c119 | 2017-09-13 10:47:59 -0700 | [diff] [blame] | 218 | void Probe::finalize_locations() { |
| 219 | std::sort(locations_.begin(), locations_.end(), |
| 220 | [](const Location &a, const Location &b) { |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 221 | return a.bin_path_ < b.bin_path_ || a.address_ < b.address_; |
Teng Qin | 488c119 | 2017-09-13 10:47:59 -0700 | [diff] [blame] | 222 | }); |
| 223 | auto last = std::unique(locations_.begin(), locations_.end(), |
| 224 | [](const Location &a, const Location &b) { |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 225 | return a.bin_path_ == b.bin_path_ && a.address_ == b.address_; |
Teng Qin | 488c119 | 2017-09-13 10:47:59 -0700 | [diff] [blame] | 226 | }); |
| 227 | locations_.erase(last, locations_.end()); |
| 228 | } |
| 229 | |
Vicent Marti | 0865f94 | 2016-04-27 16:57:58 +0200 | [diff] [blame] | 230 | void Context::_each_probe(const char *binpath, const struct bcc_elf_usdt *probe, |
| 231 | void *p) { |
| 232 | Context *ctx = static_cast<Context *>(p); |
| 233 | ctx->add_probe(binpath, probe); |
| 234 | } |
| 235 | |
Teng Qin | a0c5de1 | 2017-09-25 15:13:23 -0700 | [diff] [blame] | 236 | int Context::_each_module(const char *modpath, uint64_t, uint64_t, uint64_t, |
| 237 | bool, void *p) { |
Sasha Goldshtein | 69948a6 | 2017-03-03 08:00:55 -0500 | [diff] [blame] | 238 | Context *ctx = static_cast<Context *>(p); |
| 239 | // Modules may be reported multiple times if they contain more than one |
| 240 | // executable region. We are going to parse the ELF on disk anyway, so we |
| 241 | // don't need these duplicates. |
| 242 | if (ctx->modules_.insert(modpath).second /*inserted new?*/) { |
Vicent Marti | 96c1b8e | 2017-06-27 19:06:46 +0200 | [diff] [blame] | 243 | ProcMountNSGuard g(ctx->mount_ns_instance_.get()); |
Sasha Goldshtein | 69948a6 | 2017-03-03 08:00:55 -0500 | [diff] [blame] | 244 | bcc_elf_foreach_usdt(modpath, _each_probe, p); |
| 245 | } |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
Vicent Marti | 0865f94 | 2016-04-27 16:57:58 +0200 | [diff] [blame] | 249 | void Context::add_probe(const char *binpath, const struct bcc_elf_usdt *probe) { |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 250 | for (auto &p : probes_) { |
Vicent Marti | 0865f94 | 2016-04-27 16:57:58 +0200 | [diff] [blame] | 251 | if (p->provider_ == probe->provider && p->name_ == probe->name) { |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 252 | p->add_location(probe->pc, binpath, probe->arg_fmt); |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 253 | return; |
Vicent Marti | 0865f94 | 2016-04-27 16:57:58 +0200 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
Vicent Marti | 040df7d | 2016-05-04 16:59:57 +0200 | [diff] [blame] | 257 | probes_.emplace_back( |
Vicent Marti | 96c1b8e | 2017-06-27 19:06:46 +0200 | [diff] [blame] | 258 | new Probe(binpath, probe->provider, probe->name, probe->semaphore, pid_, |
| 259 | mount_ns_instance_.get())); |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 260 | probes_.back()->add_location(probe->pc, binpath, probe->arg_fmt); |
Vicent Marti | 0865f94 | 2016-04-27 16:57:58 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 263 | std::string Context::resolve_bin_path(const std::string &bin_path) { |
| 264 | std::string result; |
| 265 | |
| 266 | if (char *which = bcc_procutils_which(bin_path.c_str())) { |
| 267 | result = which; |
| 268 | ::free(which); |
Paul Chaignon | 81654bf | 2017-01-15 10:11:42 +0100 | [diff] [blame] | 269 | } else if (char *which_so = bcc_procutils_which_so(bin_path.c_str(), 0)) { |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 270 | result = which_so; |
Paul Chaignon | 81654bf | 2017-01-15 10:11:42 +0100 | [diff] [blame] | 271 | ::free(which_so); |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | return result; |
Vicent Marti | 0865f94 | 2016-04-27 16:57:58 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Vicent Marti | 1160608 | 2016-05-06 10:51:54 +0200 | [diff] [blame] | 277 | Probe *Context::get(const std::string &probe_name) { |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 278 | for (auto &p : probes_) { |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 279 | if (p->name_ == probe_name) |
Vicent Marti | 1160608 | 2016-05-06 10:51:54 +0200 | [diff] [blame] | 280 | return p.get(); |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 281 | } |
| 282 | return nullptr; |
| 283 | } |
Vicent Marti | 0865f94 | 2016-04-27 16:57:58 +0200 | [diff] [blame] | 284 | |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 285 | Probe *Context::get(const std::string &provider_name, |
| 286 | const std::string &probe_name) { |
| 287 | for (auto &p : probes_) { |
| 288 | if (p->provider_ == provider_name && p->name_ == probe_name) |
| 289 | return p.get(); |
| 290 | } |
| 291 | return nullptr; |
| 292 | } |
| 293 | |
Vicent Marti | 1a2ddac | 2016-05-01 13:41:33 +0200 | [diff] [blame] | 294 | bool Context::enable_probe(const std::string &probe_name, |
| 295 | const std::string &fn_name) { |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 296 | if (pid_stat_ && pid_stat_->is_stale()) |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 297 | return false; |
| 298 | |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 299 | // FIXME: we may have issues here if the context has two same probes's |
| 300 | // but different providers. For example, libc:setjmp and rtld:setjmp, |
| 301 | // libc:lll_futex_wait and rtld:lll_futex_wait. |
| 302 | Probe *found_probe = nullptr; |
| 303 | for (auto &p : probes_) { |
| 304 | if (p->name_ == probe_name) { |
| 305 | if (found_probe != nullptr) { |
| 306 | fprintf(stderr, "Two same-name probes (%s) but different providers\n", |
| 307 | probe_name.c_str()); |
| 308 | return false; |
| 309 | } |
| 310 | found_probe = p.get(); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if (found_probe != nullptr) { |
| 315 | found_probe->enable(fn_name); |
| 316 | return true; |
| 317 | } |
| 318 | |
| 319 | return false; |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 320 | } |
| 321 | |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 322 | void Context::each(each_cb callback) { |
| 323 | for (const auto &probe : probes_) { |
| 324 | struct bcc_usdt info = {0}; |
| 325 | info.provider = probe->provider().c_str(); |
| 326 | info.bin_path = probe->bin_path().c_str(); |
| 327 | info.name = probe->name().c_str(); |
| 328 | info.semaphore = probe->semaphore(); |
| 329 | info.num_locations = probe->num_locations(); |
| 330 | info.num_arguments = probe->num_arguments(); |
| 331 | callback(&info); |
| 332 | } |
| 333 | } |
| 334 | |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 335 | void Context::each_uprobe(each_uprobe_cb callback) { |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 336 | for (auto &p : probes_) { |
| 337 | if (!p->enabled()) |
| 338 | continue; |
| 339 | |
Sasha Goldshtein | 6e91a74 | 2016-10-06 18:18:18 +0300 | [diff] [blame] | 340 | for (Location &loc : p->locations_) { |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 341 | callback(loc.bin_path_.c_str(), p->attached_to_->c_str(), loc.address_, |
Vicent Marti | 1a2ddac | 2016-05-01 13:41:33 +0200 | [diff] [blame] | 342 | pid_.value_or(-1)); |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
Teng Qin | 137ee81 | 2017-08-25 16:29:16 -0700 | [diff] [blame] | 347 | Context::Context(const std::string &bin_path) |
| 348 | : mount_ns_instance_(new ProcMountNS(-1)), loaded_(false) { |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 349 | std::string full_path = resolve_bin_path(bin_path); |
| 350 | if (!full_path.empty()) { |
Yonghong Song | 0ba1507 | 2017-07-21 22:13:20 -0700 | [diff] [blame] | 351 | if (bcc_elf_foreach_usdt(full_path.c_str(), _each_probe, this) == 0) { |
| 352 | cmd_bin_path_ = full_path; |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 353 | loaded_ = true; |
Yonghong Song | 0ba1507 | 2017-07-21 22:13:20 -0700 | [diff] [blame] | 354 | } |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 355 | } |
Teng Qin | 488c119 | 2017-09-13 10:47:59 -0700 | [diff] [blame] | 356 | for (const auto &probe : probes_) |
| 357 | probe->finalize_locations(); |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 358 | } |
| 359 | |
Vicent Marti | 96c1b8e | 2017-06-27 19:06:46 +0200 | [diff] [blame] | 360 | Context::Context(int pid) : pid_(pid), pid_stat_(pid), |
| 361 | mount_ns_instance_(new ProcMountNS(pid)), loaded_(false) { |
Yonghong Song | 0ba1507 | 2017-07-21 22:13:20 -0700 | [diff] [blame] | 362 | if (bcc_procutils_each_module(pid, _each_module, this) == 0) { |
Teng Qin | 9756295 | 2017-09-18 13:36:49 -0700 | [diff] [blame] | 363 | cmd_bin_path_ = ebpf::get_pid_exe(pid); |
| 364 | if (cmd_bin_path_.empty()) |
Yonghong Song | 0ba1507 | 2017-07-21 22:13:20 -0700 | [diff] [blame] | 365 | return; |
Yonghong Song | 0ba1507 | 2017-07-21 22:13:20 -0700 | [diff] [blame] | 366 | |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 367 | loaded_ = true; |
Yonghong Song | 0ba1507 | 2017-07-21 22:13:20 -0700 | [diff] [blame] | 368 | } |
Teng Qin | 488c119 | 2017-09-13 10:47:59 -0700 | [diff] [blame] | 369 | for (const auto &probe : probes_) |
| 370 | probe->finalize_locations(); |
Vicent Marti | 9259841 | 2016-04-28 19:42:55 +0200 | [diff] [blame] | 371 | } |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 372 | |
Yonghong Song | fead16a | 2018-01-10 10:43:28 -0800 | [diff] [blame] | 373 | Context::Context(int pid, const std::string &bin_path) |
| 374 | : pid_(pid), pid_stat_(pid), |
| 375 | mount_ns_instance_(new ProcMountNS(pid)), loaded_(false) { |
| 376 | std::string full_path = resolve_bin_path(bin_path); |
| 377 | if (!full_path.empty()) { |
| 378 | if (bcc_elf_foreach_usdt(full_path.c_str(), _each_probe, this) == 0) { |
| 379 | cmd_bin_path_ = ebpf::get_pid_exe(pid); |
| 380 | if (cmd_bin_path_.empty()) |
| 381 | return; |
| 382 | loaded_ = true; |
| 383 | } |
| 384 | } |
| 385 | for (const auto &probe : probes_) |
| 386 | probe->finalize_locations(); |
| 387 | } |
| 388 | |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 389 | Context::~Context() { |
Vicent Marti | 1043645 | 2016-05-04 16:58:51 +0200 | [diff] [blame] | 390 | if (pid_stat_ && !pid_stat_->is_stale()) { |
Vicent Marti | 040df7d | 2016-05-04 16:59:57 +0200 | [diff] [blame] | 391 | for (auto &p : probes_) p->disable(); |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 392 | } |
| 393 | } |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | extern "C" { |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 397 | |
Yonghong Song | fead16a | 2018-01-10 10:43:28 -0800 | [diff] [blame] | 398 | void *bcc_usdt_new_frompid(int pid, const char *path) { |
| 399 | USDT::Context *ctx; |
| 400 | |
Javier Honduvilla Coto | dcb9b9a | 2018-05-14 13:02:05 +0200 | [diff] [blame] | 401 | if (!path) { |
Yonghong Song | fead16a | 2018-01-10 10:43:28 -0800 | [diff] [blame] | 402 | ctx = new USDT::Context(pid); |
Javier Honduvilla Coto | dcb9b9a | 2018-05-14 13:02:05 +0200 | [diff] [blame] | 403 | } else { |
| 404 | struct stat buffer; |
| 405 | if (strlen(path) >= 1 && path[0] != '/') { |
| 406 | fprintf(stderr, "HINT: Binary path should be absolute.\n\n"); |
| 407 | return nullptr; |
| 408 | } else if (stat(path, &buffer) == -1) { |
| 409 | fprintf(stderr, "HINT: Specified binary doesn't exist.\n\n"); |
| 410 | return nullptr; |
| 411 | } |
Yonghong Song | fead16a | 2018-01-10 10:43:28 -0800 | [diff] [blame] | 412 | ctx = new USDT::Context(pid, path); |
Javier Honduvilla Coto | dcb9b9a | 2018-05-14 13:02:05 +0200 | [diff] [blame] | 413 | } |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 414 | if (!ctx->loaded()) { |
| 415 | delete ctx; |
| 416 | return nullptr; |
| 417 | } |
| 418 | return static_cast<void *>(ctx); |
| 419 | } |
| 420 | |
| 421 | void *bcc_usdt_new_frompath(const char *path) { |
| 422 | USDT::Context *ctx = new USDT::Context(path); |
| 423 | if (!ctx->loaded()) { |
| 424 | delete ctx; |
| 425 | return nullptr; |
| 426 | } |
| 427 | return static_cast<void *>(ctx); |
| 428 | } |
| 429 | |
| 430 | void bcc_usdt_close(void *usdt) { |
Teng Qin | e060464 | 2018-02-09 17:49:47 -0800 | [diff] [blame] | 431 | if (usdt) { |
| 432 | USDT::Context *ctx = static_cast<USDT::Context *>(usdt); |
| 433 | delete ctx; |
| 434 | } |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 435 | } |
| 436 | |
Vicent Marti | 1a2ddac | 2016-05-01 13:41:33 +0200 | [diff] [blame] | 437 | int bcc_usdt_enable_probe(void *usdt, const char *probe_name, |
| 438 | const char *fn_name) { |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 439 | USDT::Context *ctx = static_cast<USDT::Context *>(usdt); |
| 440 | return ctx->enable_probe(probe_name, fn_name) ? 0 : -1; |
| 441 | } |
| 442 | |
Yonghong Song | 0ba1507 | 2017-07-21 22:13:20 -0700 | [diff] [blame] | 443 | const char *bcc_usdt_genargs(void **usdt_array, int len) { |
Brendan Gregg | 4f88a94 | 2016-07-22 17:11:51 -0700 | [diff] [blame] | 444 | static std::string storage_; |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 445 | std::ostringstream stream; |
Yonghong Song | 0ba1507 | 2017-07-21 22:13:20 -0700 | [diff] [blame] | 446 | |
Brenden Blanco | 4cb1edb | 2017-08-16 11:26:14 -0700 | [diff] [blame] | 447 | if (!len) |
| 448 | return ""; |
| 449 | |
Yonghong Song | 0ba1507 | 2017-07-21 22:13:20 -0700 | [diff] [blame] | 450 | stream << USDT::USDT_PROGRAM_HEADER; |
| 451 | // Generate genargs codes for an array of USDT Contexts. |
| 452 | // |
| 453 | // Each mnt_point + cmd_bin_path + probe_provider + probe_name |
| 454 | // uniquely identifies a probe. |
| 455 | std::unordered_set<std::string> generated_probes; |
| 456 | for (int i = 0; i < len; i++) { |
| 457 | USDT::Context *ctx = static_cast<USDT::Context *>(usdt_array[i]); |
| 458 | |
| 459 | for (size_t j = 0; j < ctx->num_probes(); j++) { |
| 460 | USDT::Probe *p = ctx->get(j); |
| 461 | if (p->enabled()) { |
| 462 | std::string key = std::to_string(ctx->inode()) + "*" |
| 463 | + ctx->cmd_bin_path() + "*" + p->provider() + "*" + p->name(); |
| 464 | if (generated_probes.find(key) != generated_probes.end()) |
| 465 | continue; |
| 466 | if (!p->usdt_getarg(stream)) |
| 467 | return nullptr; |
| 468 | generated_probes.insert(key); |
| 469 | } |
| 470 | } |
| 471 | } |
Brendan Gregg | 4f88a94 | 2016-07-22 17:11:51 -0700 | [diff] [blame] | 472 | |
| 473 | storage_ = stream.str(); |
| 474 | return storage_.c_str(); |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 475 | } |
| 476 | |
Teng Qin | 0615bff | 2016-09-28 08:19:40 -0700 | [diff] [blame] | 477 | const char *bcc_usdt_get_probe_argctype( |
| 478 | void *ctx, const char* probe_name, const int arg_index |
| 479 | ) { |
| 480 | USDT::Probe *p = static_cast<USDT::Context *>(ctx)->get(probe_name); |
jeromemarchand | b84714a | 2018-08-08 18:09:44 +0200 | [diff] [blame^] | 481 | if (p) |
| 482 | return p->get_arg_ctype(arg_index).c_str(); |
| 483 | return ""; |
Teng Qin | 0615bff | 2016-09-28 08:19:40 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Sasha Goldshtein | 69e361a | 2016-09-27 19:40:00 +0300 | [diff] [blame] | 486 | void bcc_usdt_foreach(void *usdt, bcc_usdt_cb callback) { |
| 487 | USDT::Context *ctx = static_cast<USDT::Context *>(usdt); |
| 488 | ctx->each(callback); |
| 489 | } |
| 490 | |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 491 | int bcc_usdt_get_location(void *usdt, const char *provider_name, |
| 492 | const char *probe_name, |
Sasha Goldshtein | 6e91a74 | 2016-10-06 18:18:18 +0300 | [diff] [blame] | 493 | int index, struct bcc_usdt_location *location) { |
Sasha Goldshtein | 8698bdb | 2017-02-19 20:29:45 +0000 | [diff] [blame] | 494 | USDT::Context *ctx = static_cast<USDT::Context *>(usdt); |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 495 | USDT::Probe *probe = ctx->get(provider_name, probe_name); |
Sasha Goldshtein | 8698bdb | 2017-02-19 20:29:45 +0000 | [diff] [blame] | 496 | if (!probe) |
| 497 | return -1; |
| 498 | if (index < 0 || (size_t)index >= probe->num_locations()) |
| 499 | return -1; |
| 500 | location->address = probe->address(index); |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 501 | location->bin_path = probe->location_bin_path(index); |
Sasha Goldshtein | 8698bdb | 2017-02-19 20:29:45 +0000 | [diff] [blame] | 502 | return 0; |
Sasha Goldshtein | 6e91a74 | 2016-10-06 18:18:18 +0300 | [diff] [blame] | 503 | } |
| 504 | |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 505 | int bcc_usdt_get_argument(void *usdt, const char *provider_name, |
| 506 | const char *probe_name, |
Sasha Goldshtein | 6e91a74 | 2016-10-06 18:18:18 +0300 | [diff] [blame] | 507 | int location_index, int argument_index, |
| 508 | struct bcc_usdt_argument *argument) { |
Sasha Goldshtein | 8698bdb | 2017-02-19 20:29:45 +0000 | [diff] [blame] | 509 | USDT::Context *ctx = static_cast<USDT::Context *>(usdt); |
Yonghong Song | 2489457 | 2018-01-04 22:08:51 -0800 | [diff] [blame] | 510 | USDT::Probe *probe = ctx->get(provider_name, probe_name); |
Sasha Goldshtein | 8698bdb | 2017-02-19 20:29:45 +0000 | [diff] [blame] | 511 | if (!probe) |
| 512 | return -1; |
| 513 | if (argument_index < 0 || (size_t)argument_index >= probe->num_arguments()) |
| 514 | return -1; |
| 515 | if (location_index < 0 || (size_t)location_index >= probe->num_locations()) |
| 516 | return -1; |
| 517 | auto const &location = probe->location(location_index); |
| 518 | auto const &arg = location.arguments_[argument_index]; |
| 519 | argument->size = arg.arg_size(); |
| 520 | argument->valid = BCC_USDT_ARGUMENT_NONE; |
| 521 | if (arg.constant()) { |
| 522 | argument->valid |= BCC_USDT_ARGUMENT_CONSTANT; |
| 523 | argument->constant = *(arg.constant()); |
| 524 | } |
| 525 | if (arg.deref_offset()) { |
| 526 | argument->valid |= BCC_USDT_ARGUMENT_DEREF_OFFSET; |
| 527 | argument->deref_offset = *(arg.deref_offset()); |
| 528 | } |
| 529 | if (arg.deref_ident()) { |
| 530 | argument->valid |= BCC_USDT_ARGUMENT_DEREF_IDENT; |
| 531 | argument->deref_ident = arg.deref_ident()->c_str(); |
| 532 | } |
| 533 | if (arg.base_register_name()) { |
| 534 | argument->valid |= BCC_USDT_ARGUMENT_BASE_REGISTER_NAME; |
| 535 | argument->base_register_name = arg.base_register_name()->c_str(); |
| 536 | } |
| 537 | if (arg.index_register_name()) { |
| 538 | argument->valid |= BCC_USDT_ARGUMENT_INDEX_REGISTER_NAME; |
| 539 | argument->index_register_name = arg.index_register_name()->c_str(); |
| 540 | } |
| 541 | if (arg.scale()) { |
| 542 | argument->valid |= BCC_USDT_ARGUMENT_SCALE; |
| 543 | argument->scale = *(arg.scale()); |
| 544 | } |
| 545 | return 0; |
Sasha Goldshtein | 6e91a74 | 2016-10-06 18:18:18 +0300 | [diff] [blame] | 546 | } |
| 547 | |
Vicent Marti | eca4783 | 2016-05-01 12:53:46 +0200 | [diff] [blame] | 548 | void bcc_usdt_foreach_uprobe(void *usdt, bcc_usdt_uprobe_cb callback) { |
| 549 | USDT::Context *ctx = static_cast<USDT::Context *>(usdt); |
| 550 | ctx->each_uprobe(callback); |
| 551 | } |
Vicent Marti | 0865f94 | 2016-04-27 16:57:58 +0200 | [diff] [blame] | 552 | } |