| 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
| 2 | * |
| 3 | * lttng/events-internal.h |
| 4 | * |
| 5 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 6 | */ |
| 7 | |
| 8 | #ifndef _LTTNG_EVENTS_INTERNAL_H |
| 9 | #define _LTTNG_EVENTS_INTERNAL_H |
| 10 | |
| 11 | #include <lttng/events.h> |
| 12 | |
| 13 | struct lttng_kernel_event_common_private { |
| 14 | struct lttng_kernel_event_common *pub; /* Public event interface */ |
| 15 | |
| 16 | const struct lttng_kernel_event_desc *desc; |
| 17 | /* Backward references: list of lttng_enabler_ref (ref to enablers) */ |
| 18 | struct list_head enablers_ref_head; |
| 19 | int registered; /* has reg'd tracepoint probe */ |
| 20 | uint64_t user_token; |
| 21 | |
| 22 | int has_enablers_without_filter_bytecode; |
| 23 | /* list of struct lttng_kernel_bytecode_runtime, sorted by seqnum */ |
| 24 | struct list_head filter_bytecode_runtime_head; |
| 25 | enum lttng_kernel_abi_instrumentation instrumentation; |
| 26 | /* Selected by instrumentation */ |
| 27 | union { |
| 28 | struct lttng_kprobe kprobe; |
| 29 | struct lttng_uprobe uprobe; |
| 30 | struct { |
| 31 | struct lttng_krp *lttng_krp; |
| 32 | char *symbol_name; |
| 33 | } kretprobe; |
| 34 | struct { |
| 35 | enum lttng_syscall_entryexit entryexit; |
| 36 | enum lttng_syscall_abi abi; |
| 37 | struct hlist_node node; /* chain registered syscall event_notifier */ |
| 38 | unsigned int syscall_id; |
| 39 | } syscall; |
| 40 | } u; |
| 41 | }; |
| 42 | |
| 43 | struct lttng_kernel_event_recorder_private { |
| 44 | struct lttng_kernel_event_common_private parent; |
| 45 | |
| 46 | struct lttng_kernel_event_recorder *pub; /* Public event interface */ |
| 47 | struct list_head node; /* Event recorder list */ |
| 48 | struct hlist_node hlist; /* Hash table of event recorders */ |
| 49 | struct lttng_kernel_ctx *ctx; |
| 50 | unsigned int id; |
| 51 | unsigned int metadata_dumped:1; |
| 52 | }; |
| 53 | |
| 54 | struct lttng_kernel_event_notifier_private { |
| 55 | struct lttng_kernel_event_common_private parent; |
| 56 | |
| 57 | struct lttng_kernel_event_notifier *pub; /* Public event notifier interface */ |
| 58 | struct lttng_event_notifier_group *group; /* weak ref */ |
| 59 | size_t num_captures; /* Needed to allocate the msgpack array. */ |
| 60 | uint64_t error_counter_index; |
| 61 | struct list_head node; /* Event notifier list */ |
| 62 | struct hlist_node hlist; /* Hash table of event notifiers */ |
| 63 | struct list_head capture_bytecode_runtime_head; |
| 64 | |
| 65 | }; |
| 66 | |
| 67 | enum lttng_kernel_bytecode_interpreter_ret { |
| 68 | LTTNG_KERNEL_BYTECODE_INTERPRETER_ERROR = -1, |
| 69 | LTTNG_KERNEL_BYTECODE_INTERPRETER_OK = 0, |
| 70 | }; |
| 71 | |
| 72 | enum lttng_kernel_bytecode_filter_result { |
| 73 | LTTNG_KERNEL_BYTECODE_FILTER_ACCEPT = 0, |
| 74 | LTTNG_KERNEL_BYTECODE_FILTER_REJECT = 1, |
| 75 | }; |
| 76 | |
| 77 | struct lttng_kernel_bytecode_filter_ctx { |
| 78 | enum lttng_kernel_bytecode_filter_result result; |
| 79 | }; |
| 80 | |
| 81 | /* |
| 82 | * Enabler field, within whatever object is enabling an event. Target of |
| 83 | * backward reference. |
| 84 | */ |
| 85 | struct lttng_enabler { |
| 86 | enum lttng_enabler_format_type format_type; |
| 87 | |
| 88 | /* head list of struct lttng_kernel_bytecode_node */ |
| 89 | struct list_head filter_bytecode_head; |
| 90 | |
| 91 | struct lttng_kernel_abi_event event_param; |
| 92 | unsigned int enabled:1; |
| 93 | |
| 94 | uint64_t user_token; /* User-provided token. */ |
| 95 | }; |
| 96 | |
| 97 | struct lttng_event_enabler { |
| 98 | struct lttng_enabler base; |
| 99 | struct list_head node; /* per-session list of enablers */ |
| 100 | struct lttng_channel *chan; |
| 101 | }; |
| 102 | |
| 103 | struct lttng_event_notifier_enabler { |
| 104 | struct lttng_enabler base; |
| 105 | uint64_t error_counter_index; |
| 106 | struct list_head node; /* List of event_notifier enablers */ |
| 107 | struct lttng_event_notifier_group *group; |
| 108 | |
| 109 | /* head list of struct lttng_kernel_bytecode_node */ |
| 110 | struct list_head capture_bytecode_head; |
| 111 | uint64_t num_captures; |
| 112 | }; |
| 113 | |
| 114 | struct lttng_ctx_value { |
| 115 | union { |
| 116 | int64_t s64; |
| 117 | const char *str; |
| 118 | double d; |
| 119 | } u; |
| 120 | }; |
| 121 | |
| 122 | /* |
| 123 | * We need to keep this perf counter field separately from struct |
| 124 | * lttng_kernel_ctx_field because cpu hotplug needs fixed-location addresses. |
| 125 | */ |
| 126 | struct lttng_perf_counter_field { |
| 127 | #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) |
| 128 | struct lttng_cpuhp_node cpuhp_prepare; |
| 129 | struct lttng_cpuhp_node cpuhp_online; |
| 130 | #else |
| 131 | struct notifier_block nb; |
| 132 | int hp_enable; |
| 133 | #endif |
| 134 | struct perf_event_attr *attr; |
| 135 | struct perf_event **e; /* per-cpu array */ |
| 136 | char *name; |
| 137 | struct lttng_kernel_event_field *event_field; |
| 138 | }; |
| 139 | |
| 140 | struct lttng_kernel_ctx_field { |
| 141 | const struct lttng_kernel_event_field *event_field; |
| 142 | size_t (*get_size)(void *priv, struct lttng_kernel_probe_ctx *probe_ctx, |
| 143 | size_t offset); |
| 144 | void (*record)(void *priv, struct lttng_kernel_probe_ctx *probe_ctx, |
| 145 | struct lib_ring_buffer_ctx *ctx, |
| 146 | struct lttng_channel *chan); |
| 147 | void (*get_value)(void *priv, struct lttng_kernel_probe_ctx *probe_ctx, |
| 148 | struct lttng_ctx_value *value); |
| 149 | void (*destroy)(void *priv); |
| 150 | void *priv; |
| 151 | }; |
| 152 | |
| 153 | struct lttng_kernel_ctx { |
| 154 | struct lttng_kernel_ctx_field *fields; |
| 155 | unsigned int nr_fields; |
| 156 | unsigned int allocated_fields; |
| 157 | size_t largest_align; /* in bytes */ |
| 158 | }; |
| 159 | |
| 160 | extern struct lttng_kernel_ctx *lttng_static_ctx; |
| 161 | |
| 162 | static inline |
| 163 | const struct lttng_kernel_type_integer *lttng_kernel_get_type_integer(const struct lttng_kernel_type_common *type) |
| 164 | { |
| 165 | if (type->type != lttng_kernel_type_integer) |
| 166 | return NULL; |
| 167 | return container_of(type, const struct lttng_kernel_type_integer, parent); |
| 168 | } |
| 169 | |
| 170 | static inline |
| 171 | const struct lttng_kernel_type_string *lttng_kernel_get_type_string(const struct lttng_kernel_type_common *type) |
| 172 | { |
| 173 | if (type->type != lttng_kernel_type_string) |
| 174 | return NULL; |
| 175 | return container_of(type, const struct lttng_kernel_type_string, parent); |
| 176 | } |
| 177 | |
| 178 | static inline |
| 179 | const struct lttng_kernel_type_enum *lttng_kernel_get_type_enum(const struct lttng_kernel_type_common *type) |
| 180 | { |
| 181 | if (type->type != lttng_kernel_type_enum) |
| 182 | return NULL; |
| 183 | return container_of(type, const struct lttng_kernel_type_enum, parent); |
| 184 | } |
| 185 | |
| 186 | static inline |
| 187 | const struct lttng_kernel_type_array *lttng_kernel_get_type_array(const struct lttng_kernel_type_common *type) |
| 188 | { |
| 189 | if (type->type != lttng_kernel_type_array) |
| 190 | return NULL; |
| 191 | return container_of(type, const struct lttng_kernel_type_array, parent); |
| 192 | } |
| 193 | |
| 194 | static inline |
| 195 | const struct lttng_kernel_type_sequence *lttng_kernel_get_type_sequence(const struct lttng_kernel_type_common *type) |
| 196 | { |
| 197 | if (type->type != lttng_kernel_type_sequence) |
| 198 | return NULL; |
| 199 | return container_of(type, const struct lttng_kernel_type_sequence, parent); |
| 200 | } |
| 201 | |
| 202 | static inline |
| 203 | const struct lttng_kernel_type_struct *lttng_kernel_get_type_struct(const struct lttng_kernel_type_common *type) |
| 204 | { |
| 205 | if (type->type != lttng_kernel_type_struct) |
| 206 | return NULL; |
| 207 | return container_of(type, const struct lttng_kernel_type_struct, parent); |
| 208 | } |
| 209 | |
| 210 | static inline |
| 211 | const struct lttng_kernel_type_variant *lttng_kernel_get_type_variant(const struct lttng_kernel_type_common *type) |
| 212 | { |
| 213 | if (type->type != lttng_kernel_type_variant) |
| 214 | return NULL; |
| 215 | return container_of(type, const struct lttng_kernel_type_variant, parent); |
| 216 | } |
| 217 | |
| 218 | static inline bool lttng_kernel_type_is_bytewise_integer(const struct lttng_kernel_type_common *type) |
| 219 | { |
| 220 | const struct lttng_kernel_type_integer *type_integer = lttng_kernel_get_type_integer(type); |
| 221 | |
| 222 | if (!type_integer) |
| 223 | return false; |
| 224 | switch (type_integer->size) { |
| 225 | case 8: /* Fall-through. */ |
| 226 | case 16: /* Fall-through. */ |
| 227 | case 32: /* Fall-through. */ |
| 228 | case 64: |
| 229 | break; |
| 230 | default: |
| 231 | return false; |
| 232 | } |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | int lttng_kernel_interpret_event_filter(const struct lttng_kernel_event_common *event, |
| 237 | const char *interpreter_stack_data, |
| 238 | struct lttng_kernel_probe_ctx *probe_ctx, |
| 239 | void *event_filter_ctx); |
| 240 | |
| 241 | static inline |
| 242 | struct lttng_enabler *lttng_event_enabler_as_enabler( |
| 243 | struct lttng_event_enabler *event_enabler) |
| 244 | { |
| 245 | return &event_enabler->base; |
| 246 | } |
| 247 | |
| 248 | static inline |
| 249 | struct lttng_enabler *lttng_event_notifier_enabler_as_enabler( |
| 250 | struct lttng_event_notifier_enabler *event_notifier_enabler) |
| 251 | { |
| 252 | return &event_notifier_enabler->base; |
| 253 | } |
| 254 | |
| 255 | int lttng_context_init(void); |
| 256 | void lttng_context_exit(void); |
| 257 | int lttng_kernel_context_append(struct lttng_kernel_ctx **ctx_p, |
| 258 | const struct lttng_kernel_ctx_field *f); |
| 259 | void lttng_kernel_context_remove_last(struct lttng_kernel_ctx **ctx_p); |
| 260 | struct lttng_kernel_ctx_field *lttng_kernel_get_context_field_from_index(struct lttng_kernel_ctx *ctx, |
| 261 | size_t index); |
| 262 | int lttng_kernel_find_context(struct lttng_kernel_ctx *ctx, const char *name); |
| 263 | int lttng_kernel_get_context_index(struct lttng_kernel_ctx *ctx, const char *name); |
| 264 | void lttng_kernel_destroy_context(struct lttng_kernel_ctx *ctx); |
| 265 | int lttng_add_pid_to_ctx(struct lttng_kernel_ctx **ctx); |
| 266 | int lttng_add_cpu_id_to_ctx(struct lttng_kernel_ctx **ctx); |
| 267 | int lttng_add_procname_to_ctx(struct lttng_kernel_ctx **ctx); |
| 268 | int lttng_add_prio_to_ctx(struct lttng_kernel_ctx **ctx); |
| 269 | int lttng_add_nice_to_ctx(struct lttng_kernel_ctx **ctx); |
| 270 | int lttng_add_vpid_to_ctx(struct lttng_kernel_ctx **ctx); |
| 271 | int lttng_add_tid_to_ctx(struct lttng_kernel_ctx **ctx); |
| 272 | int lttng_add_vtid_to_ctx(struct lttng_kernel_ctx **ctx); |
| 273 | int lttng_add_ppid_to_ctx(struct lttng_kernel_ctx **ctx); |
| 274 | int lttng_add_vppid_to_ctx(struct lttng_kernel_ctx **ctx); |
| 275 | int lttng_add_hostname_to_ctx(struct lttng_kernel_ctx **ctx); |
| 276 | int lttng_add_interruptible_to_ctx(struct lttng_kernel_ctx **ctx); |
| 277 | int lttng_add_need_reschedule_to_ctx(struct lttng_kernel_ctx **ctx); |
| 278 | #if defined(CONFIG_PREEMPT_RT_FULL) || defined(CONFIG_PREEMPT) |
| 279 | int lttng_add_preemptible_to_ctx(struct lttng_kernel_ctx **ctx); |
| 280 | #else |
| 281 | static inline |
| 282 | int lttng_add_preemptible_to_ctx(struct lttng_kernel_ctx **ctx) |
| 283 | { |
| 284 | return -ENOSYS; |
| 285 | } |
| 286 | #endif |
| 287 | #ifdef CONFIG_PREEMPT_RT_FULL |
| 288 | int lttng_add_migratable_to_ctx(struct lttng_kernel_ctx **ctx); |
| 289 | #else |
| 290 | static inline |
| 291 | int lttng_add_migratable_to_ctx(struct lttng_kernel_ctx **ctx) |
| 292 | { |
| 293 | return -ENOSYS; |
| 294 | } |
| 295 | #endif |
| 296 | |
| 297 | int lttng_add_callstack_to_ctx(struct lttng_kernel_ctx **ctx, int type); |
| 298 | |
| 299 | #if defined(CONFIG_CGROUPS) && \ |
| 300 | ((LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,6,0)) || \ |
| 301 | LTTNG_UBUNTU_KERNEL_RANGE(4,4,0,0, 4,5,0,0)) |
| 302 | int lttng_add_cgroup_ns_to_ctx(struct lttng_kernel_ctx **ctx); |
| 303 | #else |
| 304 | static inline |
| 305 | int lttng_add_cgroup_ns_to_ctx(struct lttng_kernel_ctx **ctx) |
| 306 | { |
| 307 | return -ENOSYS; |
| 308 | } |
| 309 | #endif |
| 310 | |
| 311 | #if defined(CONFIG_IPC_NS) && \ |
| 312 | (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,8,0)) |
| 313 | int lttng_add_ipc_ns_to_ctx(struct lttng_kernel_ctx **ctx); |
| 314 | #else |
| 315 | static inline |
| 316 | int lttng_add_ipc_ns_to_ctx(struct lttng_kernel_ctx **ctx) |
| 317 | { |
| 318 | return -ENOSYS; |
| 319 | } |
| 320 | #endif |
| 321 | |
| 322 | #if !defined(LTTNG_MNT_NS_MISSING_HEADER) && \ |
| 323 | (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,8,0)) |
| 324 | int lttng_add_mnt_ns_to_ctx(struct lttng_kernel_ctx **ctx); |
| 325 | #else |
| 326 | static inline |
| 327 | int lttng_add_mnt_ns_to_ctx(struct lttng_kernel_ctx **ctx) |
| 328 | { |
| 329 | return -ENOSYS; |
| 330 | } |
| 331 | #endif |
| 332 | |
| 333 | #if defined(CONFIG_NET_NS) && \ |
| 334 | (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,8,0)) |
| 335 | int lttng_add_net_ns_to_ctx(struct lttng_kernel_ctx **ctx); |
| 336 | #else |
| 337 | static inline |
| 338 | int lttng_add_net_ns_to_ctx(struct lttng_kernel_ctx **ctx) |
| 339 | { |
| 340 | return -ENOSYS; |
| 341 | } |
| 342 | #endif |
| 343 | |
| 344 | #if defined(CONFIG_PID_NS) && \ |
| 345 | (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,8,0)) |
| 346 | int lttng_add_pid_ns_to_ctx(struct lttng_kernel_ctx **ctx); |
| 347 | #else |
| 348 | static inline |
| 349 | int lttng_add_pid_ns_to_ctx(struct lttng_kernel_ctx **ctx) |
| 350 | { |
| 351 | return -ENOSYS; |
| 352 | } |
| 353 | #endif |
| 354 | |
| 355 | #if defined(CONFIG_USER_NS) && \ |
| 356 | (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,8,0)) |
| 357 | int lttng_add_user_ns_to_ctx(struct lttng_kernel_ctx **ctx); |
| 358 | #else |
| 359 | static inline |
| 360 | int lttng_add_user_ns_to_ctx(struct lttng_kernel_ctx **ctx) |
| 361 | { |
| 362 | return -ENOSYS; |
| 363 | } |
| 364 | #endif |
| 365 | |
| 366 | #if defined(CONFIG_UTS_NS) && \ |
| 367 | (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,8,0)) |
| 368 | int lttng_add_uts_ns_to_ctx(struct lttng_kernel_ctx **ctx); |
| 369 | #else |
| 370 | static inline |
| 371 | int lttng_add_uts_ns_to_ctx(struct lttng_kernel_ctx **ctx) |
| 372 | { |
| 373 | return -ENOSYS; |
| 374 | } |
| 375 | #endif |
| 376 | |
| 377 | #if defined(CONFIG_TIME_NS) && \ |
| 378 | (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,6,0)) |
| 379 | int lttng_add_time_ns_to_ctx(struct lttng_kernel_ctx **ctx); |
| 380 | #else |
| 381 | static inline |
| 382 | int lttng_add_time_ns_to_ctx(struct lttng_kernel_ctx **ctx) |
| 383 | { |
| 384 | return -ENOSYS; |
| 385 | } |
| 386 | #endif |
| 387 | |
| 388 | int lttng_add_uid_to_ctx(struct lttng_kernel_ctx **ctx); |
| 389 | int lttng_add_euid_to_ctx(struct lttng_kernel_ctx **ctx); |
| 390 | int lttng_add_suid_to_ctx(struct lttng_kernel_ctx **ctx); |
| 391 | int lttng_add_gid_to_ctx(struct lttng_kernel_ctx **ctx); |
| 392 | int lttng_add_egid_to_ctx(struct lttng_kernel_ctx **ctx); |
| 393 | int lttng_add_sgid_to_ctx(struct lttng_kernel_ctx **ctx); |
| 394 | int lttng_add_vuid_to_ctx(struct lttng_kernel_ctx **ctx); |
| 395 | int lttng_add_veuid_to_ctx(struct lttng_kernel_ctx **ctx); |
| 396 | int lttng_add_vsuid_to_ctx(struct lttng_kernel_ctx **ctx); |
| 397 | int lttng_add_vgid_to_ctx(struct lttng_kernel_ctx **ctx); |
| 398 | int lttng_add_vegid_to_ctx(struct lttng_kernel_ctx **ctx); |
| 399 | int lttng_add_vsgid_to_ctx(struct lttng_kernel_ctx **ctx); |
| 400 | |
| 401 | #if defined(CONFIG_PERF_EVENTS) |
| 402 | int lttng_add_perf_counter_to_ctx(uint32_t type, |
| 403 | uint64_t config, |
| 404 | const char *name, |
| 405 | struct lttng_kernel_ctx **ctx); |
| 406 | int lttng_cpuhp_perf_counter_online(unsigned int cpu, |
| 407 | struct lttng_cpuhp_node *node); |
| 408 | int lttng_cpuhp_perf_counter_dead(unsigned int cpu, |
| 409 | struct lttng_cpuhp_node *node); |
| 410 | #else |
| 411 | static inline |
| 412 | int lttng_add_perf_counter_to_ctx(uint32_t type, |
| 413 | uint64_t config, |
| 414 | const char *name, |
| 415 | struct lttng_kernel_ctx **ctx) |
| 416 | { |
| 417 | return -ENOSYS; |
| 418 | } |
| 419 | static inline |
| 420 | int lttng_cpuhp_perf_counter_online(unsigned int cpu, |
| 421 | struct lttng_cpuhp_node *node) |
| 422 | { |
| 423 | return 0; |
| 424 | } |
| 425 | static inline |
| 426 | int lttng_cpuhp_perf_counter_dead(unsigned int cpu, |
| 427 | struct lttng_cpuhp_node *node) |
| 428 | { |
| 429 | return 0; |
| 430 | } |
| 431 | #endif |
| 432 | |
| 433 | |
| 434 | |
| 435 | #define lttng_kernel_static_ctx_field(_event_field, _get_size, _record, _get_value, _destroy, _priv) \ |
| 436 | __LTTNG_COMPOUND_LITERAL(const struct lttng_kernel_ctx_field, { \ |
| 437 | .event_field = (_event_field), \ |
| 438 | .get_size = (_get_size), \ |
| 439 | .record = (_record), \ |
| 440 | .get_value = (_get_value), \ |
| 441 | .destroy = (_destroy), \ |
| 442 | .priv = (_priv), \ |
| 443 | }) |
| 444 | |
| 445 | #endif /* _LTTNG_EVENTS_INTERNAL_H */ |