| 1 | /* |
| 2 | * (C) Copyright 2009-2011 - |
| 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * |
| 5 | * LTTng UST vtid context. |
| 6 | * |
| 7 | * Dual LGPL v2.1/GPL v2 license. |
| 8 | */ |
| 9 | |
| 10 | #include <sys/types.h> |
| 11 | #include <unistd.h> |
| 12 | #include <ust/lttng-events.h> |
| 13 | #include <ust/lttng-tracer.h> |
| 14 | #include <ust/ringbuffer-config.h> |
| 15 | |
| 16 | #ifdef __linux__ |
| 17 | #include <syscall.h> |
| 18 | #endif |
| 19 | |
| 20 | #if defined(_syscall0) |
| 21 | _syscall0(pid_t, gettid) |
| 22 | #elif defined(__NR_gettid) |
| 23 | static inline pid_t gettid(void) |
| 24 | { |
| 25 | return syscall(__NR_gettid); |
| 26 | } |
| 27 | #else |
| 28 | #warning "use pid as tid" |
| 29 | static inline pid_t gettid(void) |
| 30 | { |
| 31 | return getpid(); |
| 32 | } |
| 33 | #endif |
| 34 | |
| 35 | /* |
| 36 | * We cache the result to ensure we don't trigger a system call for |
| 37 | * each event. |
| 38 | */ |
| 39 | static __thread pid_t cached_vtid; |
| 40 | |
| 41 | /* |
| 42 | * Upon fork or clone, the TID assigned to our thread is not the same as |
| 43 | * we kept in cache. Luckily, we are the only thread surviving in the |
| 44 | * child process, so we can simply clear our cached version. |
| 45 | */ |
| 46 | void lttng_context_vtid_reset(void) |
| 47 | { |
| 48 | cached_vtid = 0; |
| 49 | } |
| 50 | |
| 51 | static |
| 52 | size_t vtid_get_size(size_t offset) |
| 53 | { |
| 54 | size_t size = 0; |
| 55 | |
| 56 | size += lib_ring_buffer_align(offset, lttng_alignof(pid_t)); |
| 57 | size += sizeof(pid_t); |
| 58 | return size; |
| 59 | } |
| 60 | |
| 61 | static |
| 62 | void vtid_record(struct lttng_ctx_field *field, |
| 63 | struct lib_ring_buffer_ctx *ctx, |
| 64 | struct ltt_channel *chan) |
| 65 | { |
| 66 | if (unlikely(!cached_vtid)) |
| 67 | cached_vtid = gettid(); |
| 68 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(cached_vtid)); |
| 69 | chan->ops->event_write(ctx, &cached_vtid, sizeof(cached_vtid)); |
| 70 | } |
| 71 | |
| 72 | int lttng_add_vtid_to_ctx(struct lttng_ctx **ctx) |
| 73 | { |
| 74 | struct lttng_ctx_field *field; |
| 75 | |
| 76 | field = lttng_append_context(ctx); |
| 77 | if (!field) |
| 78 | return -ENOMEM; |
| 79 | if (lttng_find_context(*ctx, "vtid")) { |
| 80 | lttng_remove_context_field(ctx, field); |
| 81 | return -EEXIST; |
| 82 | } |
| 83 | field->event_field.name = "vtid"; |
| 84 | field->event_field.type.atype = atype_integer; |
| 85 | field->event_field.type.u.basic.integer.size = sizeof(pid_t) * CHAR_BIT; |
| 86 | field->event_field.type.u.basic.integer.alignment = lttng_alignof(pid_t) * CHAR_BIT; |
| 87 | field->event_field.type.u.basic.integer.signedness = lttng_is_signed_type(pid_t); |
| 88 | field->event_field.type.u.basic.integer.reverse_byte_order = 0; |
| 89 | field->event_field.type.u.basic.integer.base = 10; |
| 90 | field->event_field.type.u.basic.integer.encoding = lttng_encode_none; |
| 91 | field->get_size = vtid_get_size; |
| 92 | field->record = vtid_record; |
| 93 | return 0; |
| 94 | } |