Commit | Line | Data |
---|---|---|
8173ec7c | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
8173ec7c | 3 | * |
e92f3e28 MD |
4 | * Copyright (C) 2009-2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
5 | * | |
c0c0989a | 6 | * LTTng UST pthread_id context. |
8173ec7c MD |
7 | */ |
8 | ||
3fbec7dc | 9 | #define _LGPL_SOURCE |
9af5d97a | 10 | #include <limits.h> |
b4051ad8 | 11 | #include <stddef.h> |
8173ec7c | 12 | #include <pthread.h> |
4318ae1b MD |
13 | #include <lttng/ust-events.h> |
14 | #include <lttng/ust-tracer.h> | |
0466ac28 | 15 | #include <lttng/ringbuffer-context.h> |
8173ec7c | 16 | |
fc80554e MJ |
17 | #include "context-internal.h" |
18 | ||
8173ec7c | 19 | static |
2208d8b5 MJ |
20 | size_t pthread_id_get_size(struct lttng_ust_ctx_field *field __attribute__((unused)), |
21 | size_t offset) | |
8173ec7c MD |
22 | { |
23 | size_t size = 0; | |
24 | ||
dc325c1d | 25 | size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(unsigned long)); |
8de38cf7 | 26 | size += sizeof(unsigned long); |
8173ec7c MD |
27 | return size; |
28 | } | |
29 | ||
30 | static | |
2208d8b5 | 31 | void pthread_id_record(struct lttng_ust_ctx_field *field __attribute__((unused)), |
4cfec15c | 32 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
e7bc0ef6 | 33 | struct lttng_ust_channel_buffer *chan) |
8173ec7c MD |
34 | { |
35 | unsigned long pthread_id; | |
36 | ||
37 | pthread_id = (unsigned long) pthread_self(); | |
8936b6c0 | 38 | chan->ops->event_write(ctx, &pthread_id, sizeof(pthread_id), lttng_ust_rb_alignof(pthread_id)); |
8173ec7c MD |
39 | } |
40 | ||
77aa5901 | 41 | static |
2208d8b5 | 42 | void pthread_id_get_value(struct lttng_ust_ctx_field *field __attribute__((unused)), |
daacdbfc | 43 | struct lttng_ust_ctx_value *value) |
77aa5901 | 44 | { |
6e9ac4ae | 45 | value->u.s64 = (unsigned long) pthread_self(); |
77aa5901 MD |
46 | } |
47 | ||
daacdbfc | 48 | int lttng_add_pthread_id_to_ctx(struct lttng_ust_ctx **ctx) |
8173ec7c | 49 | { |
daacdbfc | 50 | struct lttng_ust_ctx_field *field; |
a084756d MD |
51 | struct lttng_ust_type_common *type; |
52 | int ret; | |
8173ec7c | 53 | |
a084756d | 54 | type = lttng_ust_create_type_integer(sizeof(unsigned long) * CHAR_BIT, |
dc325c1d | 55 | lttng_ust_rb_alignof(unsigned long) * CHAR_BIT, |
eae3c729 | 56 | lttng_ust_is_signed_type(unsigned long), |
a084756d MD |
57 | BYTE_ORDER, 10); |
58 | if (!type) | |
8173ec7c | 59 | return -ENOMEM; |
a084756d MD |
60 | field = lttng_append_context(ctx); |
61 | if (!field) { | |
62 | ret = -ENOMEM; | |
63 | goto error_context; | |
64 | } | |
8173ec7c | 65 | if (lttng_find_context(*ctx, "pthread_id")) { |
a084756d MD |
66 | ret = -EEXIST; |
67 | goto error_find_context; | |
8173ec7c | 68 | } |
a084756d MD |
69 | field->event_field->name = strdup("pthread_id"); |
70 | if (!field->event_field->name) { | |
71 | ret = -ENOMEM; | |
72 | goto error_name; | |
73 | } | |
74 | field->event_field->type = type; | |
8173ec7c MD |
75 | field->get_size = pthread_id_get_size; |
76 | field->record = pthread_id_record; | |
77aa5901 | 77 | field->get_value = pthread_id_get_value; |
b2cc986a | 78 | lttng_context_update(*ctx); |
8173ec7c | 79 | return 0; |
a084756d MD |
80 | |
81 | error_name: | |
82 | error_find_context: | |
83 | lttng_remove_context_field(ctx, field); | |
84 | error_context: | |
85 | lttng_ust_destroy_type(type); | |
86 | return ret; | |
8173ec7c | 87 | } |