| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright (C) 2009-2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * LTTng UST pthread_id context. |
| 7 | */ |
| 8 | |
| 9 | #define _LGPL_SOURCE |
| 10 | #include <limits.h> |
| 11 | #include <stddef.h> |
| 12 | #include <pthread.h> |
| 13 | #include <lttng/ust-events.h> |
| 14 | #include <lttng/ust-tracer.h> |
| 15 | #include <lttng/ust-ringbuffer-context.h> |
| 16 | |
| 17 | #include "context-internal.h" |
| 18 | |
| 19 | static |
| 20 | size_t pthread_id_get_size(void *priv __attribute__((unused)), |
| 21 | struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)), |
| 22 | size_t offset) |
| 23 | { |
| 24 | size_t size = 0; |
| 25 | |
| 26 | size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(unsigned long)); |
| 27 | size += sizeof(unsigned long); |
| 28 | return size; |
| 29 | } |
| 30 | |
| 31 | static |
| 32 | void pthread_id_record(void *priv __attribute__((unused)), |
| 33 | struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)), |
| 34 | struct lttng_ust_ring_buffer_ctx *ctx, |
| 35 | struct lttng_ust_channel_buffer *chan) |
| 36 | { |
| 37 | unsigned long pthread_id; |
| 38 | |
| 39 | pthread_id = (unsigned long) pthread_self(); |
| 40 | chan->ops->event_write(ctx, &pthread_id, sizeof(pthread_id), lttng_ust_rb_alignof(pthread_id)); |
| 41 | } |
| 42 | |
| 43 | static |
| 44 | void pthread_id_get_value(void *priv __attribute__((unused)), |
| 45 | struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)), |
| 46 | struct lttng_ust_ctx_value *value) |
| 47 | { |
| 48 | value->u.u64 = (unsigned long) pthread_self(); |
| 49 | } |
| 50 | |
| 51 | static const struct lttng_ust_ctx_field *ctx_field = lttng_ust_static_ctx_field( |
| 52 | lttng_ust_static_event_field("pthread_id", |
| 53 | lttng_ust_static_type_integer(sizeof(unsigned long) * CHAR_BIT, |
| 54 | lttng_ust_rb_alignof(unsigned long) * CHAR_BIT, |
| 55 | lttng_ust_is_signed_type(unsigned long), |
| 56 | LTTNG_UST_BYTE_ORDER, 10), |
| 57 | false, false), |
| 58 | pthread_id_get_size, |
| 59 | pthread_id_record, |
| 60 | pthread_id_get_value, |
| 61 | NULL, NULL); |
| 62 | |
| 63 | int lttng_add_pthread_id_to_ctx(struct lttng_ust_ctx **ctx) |
| 64 | { |
| 65 | int ret; |
| 66 | |
| 67 | if (lttng_find_context(*ctx, ctx_field->event_field->name)) { |
| 68 | ret = -EEXIST; |
| 69 | goto error_find_context; |
| 70 | } |
| 71 | ret = lttng_ust_context_append(ctx, ctx_field); |
| 72 | if (ret) |
| 73 | return ret; |
| 74 | return 0; |
| 75 | |
| 76 | error_find_context: |
| 77 | return ret; |
| 78 | } |