| 1 | /* |
| 2 | * Copyright (C) 2019 - Jonathan Rajotte-Julien <jonathan.rajotte-julien@efficios.com> |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU Lesser General Public License, version 2.1 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This library is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 11 | * for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this library; if not, write to the Free Software Foundation, |
| 15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | #include <lttng/event-rule/event-rule-internal.h> |
| 19 | #include <lttng/event-rule/kprobe-internal.h> |
| 20 | #include <lttng/event-rule/kretprobe-internal.h> |
| 21 | #include <lttng/event-rule/uprobe-internal.h> |
| 22 | #include <lttng/event-rule/syscall-internal.h> |
| 23 | #include <lttng/event-rule/tracepoint-internal.h> |
| 24 | #include <common/macros.h> |
| 25 | #include <common/error.h> |
| 26 | #include <common/dynamic-buffer.h> |
| 27 | #include <common/buffer-view.h> |
| 28 | #include <stdbool.h> |
| 29 | #include <assert.h> |
| 30 | |
| 31 | enum lttng_event_rule_type lttng_event_rule_get_type( |
| 32 | const struct lttng_event_rule *event_rule) |
| 33 | { |
| 34 | return event_rule ? event_rule->type : LTTNG_EVENT_RULE_TYPE_UNKNOWN; |
| 35 | } |
| 36 | |
| 37 | LTTNG_HIDDEN |
| 38 | enum lttng_domain_type lttng_event_rule_get_domain_type( |
| 39 | const struct lttng_event_rule *event_rule) |
| 40 | { |
| 41 | enum lttng_domain_type domain_type; |
| 42 | |
| 43 | switch (lttng_event_rule_get_type(event_rule)) { |
| 44 | case LTTNG_EVENT_RULE_TYPE_TRACEPOINT: |
| 45 | (void) lttng_event_rule_tracepoint_get_domain_type(event_rule, &domain_type); |
| 46 | break; |
| 47 | case LTTNG_EVENT_RULE_TYPE_SYSCALL: |
| 48 | case LTTNG_EVENT_RULE_TYPE_KPROBE: |
| 49 | case LTTNG_EVENT_RULE_TYPE_KRETPROBE: |
| 50 | case LTTNG_EVENT_RULE_TYPE_UPROBE: |
| 51 | domain_type = LTTNG_DOMAIN_KERNEL; |
| 52 | break; |
| 53 | case LTTNG_EVENT_RULE_TYPE_UNKNOWN: |
| 54 | domain_type = LTTNG_DOMAIN_NONE; |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | return domain_type; |
| 59 | } |
| 60 | |
| 61 | static |
| 62 | void lttng_event_rule_release(struct urcu_ref *ref) |
| 63 | { |
| 64 | |
| 65 | struct lttng_event_rule *event_rule = container_of(ref, typeof(*event_rule), |
| 66 | ref); |
| 67 | |
| 68 | assert(event_rule->destroy); |
| 69 | event_rule->destroy(event_rule); |
| 70 | } |
| 71 | |
| 72 | void lttng_event_rule_destroy(struct lttng_event_rule *event_rule) |
| 73 | { |
| 74 | lttng_event_rule_put(event_rule); |
| 75 | } |
| 76 | |
| 77 | LTTNG_HIDDEN |
| 78 | bool lttng_event_rule_validate(const struct lttng_event_rule *event_rule) |
| 79 | { |
| 80 | bool valid; |
| 81 | |
| 82 | if (!event_rule) { |
| 83 | valid = false; |
| 84 | goto end; |
| 85 | } |
| 86 | |
| 87 | if (!event_rule->validate) { |
| 88 | /* Sub-class guarantees that it can never be invalid. */ |
| 89 | valid = true; |
| 90 | goto end; |
| 91 | } |
| 92 | |
| 93 | valid = event_rule->validate(event_rule); |
| 94 | end: |
| 95 | return valid; |
| 96 | } |
| 97 | |
| 98 | LTTNG_HIDDEN |
| 99 | int lttng_event_rule_serialize(const struct lttng_event_rule *event_rule, |
| 100 | struct lttng_dynamic_buffer *buf, |
| 101 | int *fd_to_send) |
| 102 | { |
| 103 | int ret; |
| 104 | struct lttng_event_rule_comm event_rule_comm = { 0 }; |
| 105 | |
| 106 | if (!event_rule) { |
| 107 | ret = -1; |
| 108 | goto end; |
| 109 | } |
| 110 | |
| 111 | event_rule_comm.event_rule_type = (int8_t) event_rule->type; |
| 112 | |
| 113 | ret = lttng_dynamic_buffer_append(buf, &event_rule_comm, |
| 114 | sizeof(event_rule_comm)); |
| 115 | if (ret) { |
| 116 | goto end; |
| 117 | } |
| 118 | |
| 119 | ret = event_rule->serialize(event_rule, buf, fd_to_send); |
| 120 | if (ret) { |
| 121 | goto end; |
| 122 | } |
| 123 | end: |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | LTTNG_HIDDEN |
| 128 | bool lttng_event_rule_is_equal(const struct lttng_event_rule *a, |
| 129 | const struct lttng_event_rule *b) |
| 130 | { |
| 131 | bool is_equal = false; |
| 132 | |
| 133 | if (!a || !b) { |
| 134 | goto end; |
| 135 | } |
| 136 | |
| 137 | if (a->type != b->type) { |
| 138 | goto end; |
| 139 | } |
| 140 | |
| 141 | if (a == b) { |
| 142 | is_equal = true; |
| 143 | goto end; |
| 144 | } |
| 145 | |
| 146 | is_equal = a->equal ? a->equal(a, b) : true; |
| 147 | end: |
| 148 | return is_equal; |
| 149 | } |
| 150 | |
| 151 | LTTNG_HIDDEN |
| 152 | ssize_t lttng_event_rule_create_from_buffer( |
| 153 | const struct lttng_buffer_view *buffer, |
| 154 | struct lttng_event_rule **event_rule) |
| 155 | { |
| 156 | ssize_t ret, event_rule_size = 0; |
| 157 | const struct lttng_event_rule_comm *event_rule_comm; |
| 158 | event_rule_create_from_buffer_cb create_from_buffer = NULL; |
| 159 | |
| 160 | if (!buffer || !event_rule) { |
| 161 | ret = -1; |
| 162 | goto end; |
| 163 | } |
| 164 | |
| 165 | DBG("Deserializing event_rule from buffer"); |
| 166 | event_rule_comm = (const struct lttng_event_rule_comm *) buffer->data; |
| 167 | event_rule_size += sizeof(*event_rule_comm); |
| 168 | |
| 169 | switch ((enum lttng_event_rule_type) event_rule_comm->event_rule_type) { |
| 170 | case LTTNG_EVENT_RULE_TYPE_TRACEPOINT: |
| 171 | create_from_buffer = lttng_event_rule_tracepoint_create_from_buffer; |
| 172 | break; |
| 173 | case LTTNG_EVENT_RULE_TYPE_KPROBE: |
| 174 | create_from_buffer = lttng_event_rule_kprobe_create_from_buffer; |
| 175 | break; |
| 176 | case LTTNG_EVENT_RULE_TYPE_KRETPROBE: |
| 177 | create_from_buffer = lttng_event_rule_kretprobe_create_from_buffer; |
| 178 | break; |
| 179 | case LTTNG_EVENT_RULE_TYPE_UPROBE: |
| 180 | create_from_buffer = lttng_event_rule_uprobe_create_from_buffer; |
| 181 | break; |
| 182 | case LTTNG_EVENT_RULE_TYPE_SYSCALL: |
| 183 | create_from_buffer = lttng_event_rule_syscall_create_from_buffer; |
| 184 | break; |
| 185 | default: |
| 186 | ERR("Attempted to create event rule of unknown type (%i)", |
| 187 | (int) event_rule_comm->event_rule_type); |
| 188 | ret = -1; |
| 189 | goto end; |
| 190 | } |
| 191 | |
| 192 | if (create_from_buffer) { |
| 193 | const struct lttng_buffer_view view = |
| 194 | lttng_buffer_view_from_view(buffer, |
| 195 | sizeof(*event_rule_comm), -1); |
| 196 | |
| 197 | ret = create_from_buffer(&view, event_rule); |
| 198 | if (ret < 0) { |
| 199 | goto end; |
| 200 | } |
| 201 | event_rule_size += ret; |
| 202 | |
| 203 | } else { |
| 204 | abort(); |
| 205 | } |
| 206 | |
| 207 | ret = event_rule_size; |
| 208 | end: |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | LTTNG_HIDDEN |
| 213 | void lttng_event_rule_init(struct lttng_event_rule *event_rule, |
| 214 | enum lttng_event_rule_type type) |
| 215 | { |
| 216 | urcu_ref_init(&event_rule->ref); |
| 217 | event_rule->type = type; |
| 218 | } |
| 219 | |
| 220 | LTTNG_HIDDEN |
| 221 | bool lttng_event_rule_get(struct lttng_event_rule *event_rule) |
| 222 | { |
| 223 | return urcu_ref_get_unless_zero(&event_rule->ref); |
| 224 | } |
| 225 | |
| 226 | LTTNG_HIDDEN |
| 227 | void lttng_event_rule_put(struct lttng_event_rule *event_rule) |
| 228 | { |
| 229 | if (!event_rule) { |
| 230 | return; |
| 231 | } |
| 232 | assert(event_rule->ref.refcount); |
| 233 | urcu_ref_put(&event_rule->ref, lttng_event_rule_release); |
| 234 | } |
| 235 | |
| 236 | LTTNG_HIDDEN |
| 237 | enum lttng_error_code lttng_event_rule_populate(struct lttng_event_rule *rule, uid_t uid, gid_t gid) |
| 238 | { |
| 239 | assert(rule->populate); |
| 240 | return rule->populate(rule, uid, gid); |
| 241 | } |
| 242 | |
| 243 | /* If not present return NULL |
| 244 | * Caller DO NOT own the returned object |
| 245 | */ |
| 246 | LTTNG_HIDDEN |
| 247 | const char *lttng_event_rule_get_filter(const struct lttng_event_rule *rule) |
| 248 | { |
| 249 | assert(rule->get_filter); |
| 250 | return rule->get_filter(rule); |
| 251 | } |
| 252 | |
| 253 | /* If not present return NULL |
| 254 | * Caller DO NOT own the returned object |
| 255 | */ |
| 256 | LTTNG_HIDDEN |
| 257 | const struct lttng_filter_bytecode *lttng_event_rule_get_filter_bytecode( |
| 258 | const struct lttng_event_rule *rule) |
| 259 | { |
| 260 | assert(rule->get_filter_bytecode); |
| 261 | return rule->get_filter_bytecode(rule); |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * If not present return NULL |
| 266 | * Caller OWN the returned object |
| 267 | * TODO: should this be done another way? |
| 268 | */ |
| 269 | LTTNG_HIDDEN |
| 270 | struct lttng_event_exclusion *lttng_event_rule_generate_exclusions( |
| 271 | struct lttng_event_rule *rule) |
| 272 | { |
| 273 | assert(rule->generate_exclusions); |
| 274 | return rule->generate_exclusions(rule); |
| 275 | } |
| 276 | |
| 277 | LTTNG_HIDDEN |
| 278 | struct lttng_event *lttng_event_rule_generate_lttng_event( |
| 279 | const struct lttng_event_rule *rule) |
| 280 | { |
| 281 | assert(rule->generate_lttng_event); |
| 282 | return rule->generate_lttng_event(rule); |
| 283 | } |
| 284 | |
| 285 | LTTNG_HIDDEN |
| 286 | bool lttng_event_rule_is_agent(const struct lttng_event_rule *rule) |
| 287 | { |
| 288 | bool ret = false; |
| 289 | enum lttng_domain_type type = lttng_event_rule_get_domain_type(rule); |
| 290 | |
| 291 | switch (type) { |
| 292 | case LTTNG_DOMAIN_JUL: |
| 293 | case LTTNG_DOMAIN_LOG4J: |
| 294 | case LTTNG_DOMAIN_PYTHON: |
| 295 | ret = true; |
| 296 | break; |
| 297 | case LTTNG_DOMAIN_UST: |
| 298 | case LTTNG_DOMAIN_KERNEL: |
| 299 | ret = false; |
| 300 | break; |
| 301 | default: |
| 302 | assert(0); |
| 303 | }; |
| 304 | |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | LTTNG_HIDDEN |
| 309 | const char *lttng_event_rule_type_str(enum lttng_event_rule_type type) |
| 310 | { |
| 311 | switch (type) { |
| 312 | case LTTNG_EVENT_RULE_TYPE_UNKNOWN: |
| 313 | return "unknown"; |
| 314 | |
| 315 | case LTTNG_EVENT_RULE_TYPE_TRACEPOINT: |
| 316 | return "tracepoint"; |
| 317 | |
| 318 | case LTTNG_EVENT_RULE_TYPE_SYSCALL: |
| 319 | return "syscall"; |
| 320 | |
| 321 | case LTTNG_EVENT_RULE_TYPE_KPROBE: |
| 322 | return "probe"; |
| 323 | |
| 324 | case LTTNG_EVENT_RULE_TYPE_KRETPROBE: |
| 325 | return "function"; |
| 326 | |
| 327 | case LTTNG_EVENT_RULE_TYPE_UPROBE: |
| 328 | return "userspace-probe"; |
| 329 | |
| 330 | default: |
| 331 | abort(); |
| 332 | } |
| 333 | } |