Commit | Line | Data |
---|---|---|
c1ef86f0 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
c1ef86f0 | 3 | * |
e92f3e28 MD |
4 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
5 | * | |
c0c0989a | 6 | * LTTng UST vpid context. |
c1ef86f0 MD |
7 | */ |
8 | ||
3fbec7dc | 9 | #define _LGPL_SOURCE |
9af5d97a | 10 | #include <limits.h> |
b4051ad8 | 11 | #include <stddef.h> |
c1ef86f0 MD |
12 | #include <sys/types.h> |
13 | #include <unistd.h> | |
4318ae1b MD |
14 | #include <lttng/ust-events.h> |
15 | #include <lttng/ust-tracer.h> | |
0b4b8811 | 16 | #include <lttng/ust-ringbuffer-context.h> |
c1ef86f0 | 17 | |
fc80554e MJ |
18 | #include "context-internal.h" |
19 | ||
c1ef86f0 MD |
20 | /* |
21 | * We cache the result to ensure we don't trigger a system call for | |
22 | * each event. | |
23 | */ | |
24 | static pid_t cached_vpid; | |
25 | ||
26 | static inline | |
98357ffd | 27 | pid_t wrapper_getvpid(void) |
c1ef86f0 | 28 | { |
98357ffd MD |
29 | pid_t vpid; |
30 | ||
31 | vpid = CMM_LOAD_SHARED(cached_vpid); | |
32 | if (caa_unlikely(!vpid)) { | |
33 | vpid = getpid(); | |
34 | CMM_STORE_SHARED(cached_vpid, vpid); | |
35 | } | |
36 | return vpid; | |
c1ef86f0 MD |
37 | } |
38 | ||
39 | /* | |
40 | * Upon fork or clone, the PID assigned to our thread is not the same as | |
41 | * we kept in cache. | |
42 | */ | |
43 | void lttng_context_vpid_reset(void) | |
44 | { | |
98357ffd | 45 | CMM_STORE_SHARED(cached_vpid, 0); |
c1ef86f0 | 46 | } |
c1ef86f0 MD |
47 | |
48 | static | |
4e48b5d2 | 49 | size_t vpid_get_size(void *priv __attribute__((unused)), |
b2e37d27 | 50 | struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)), |
2208d8b5 | 51 | size_t offset) |
c1ef86f0 MD |
52 | { |
53 | size_t size = 0; | |
54 | ||
b5457df5 | 55 | size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(pid_t)); |
c1ef86f0 MD |
56 | size += sizeof(pid_t); |
57 | return size; | |
58 | } | |
59 | ||
60 | static | |
4e48b5d2 | 61 | void vpid_record(void *priv __attribute__((unused)), |
b2e37d27 MD |
62 | struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)), |
63 | struct lttng_ust_ring_buffer_ctx *ctx, | |
64 | struct lttng_ust_channel_buffer *chan) | |
c1ef86f0 | 65 | { |
98357ffd | 66 | pid_t vpid = wrapper_getvpid(); |
c1ef86f0 | 67 | |
8936b6c0 | 68 | chan->ops->event_write(ctx, &vpid, sizeof(vpid), lttng_ust_rb_alignof(vpid)); |
c1ef86f0 MD |
69 | } |
70 | ||
77aa5901 | 71 | static |
4e48b5d2 | 72 | void vpid_get_value(void *priv __attribute__((unused)), |
b2e37d27 | 73 | struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)), |
daacdbfc | 74 | struct lttng_ust_ctx_value *value) |
77aa5901 | 75 | { |
98357ffd | 76 | value->u.s64 = wrapper_getvpid(); |
77aa5901 MD |
77 | } |
78 | ||
4e48b5d2 MD |
79 | static const struct lttng_ust_ctx_field *ctx_field = lttng_ust_static_ctx_field( |
80 | lttng_ust_static_event_field("vpid", | |
81 | lttng_ust_static_type_integer(sizeof(pid_t) * CHAR_BIT, | |
82 | lttng_ust_rb_alignof(pid_t) * CHAR_BIT, | |
83 | lttng_ust_is_signed_type(pid_t), | |
baa8acf3 | 84 | LTTNG_UST_BYTE_ORDER, 10), |
4e48b5d2 MD |
85 | false, false), |
86 | vpid_get_size, | |
87 | vpid_record, | |
88 | vpid_get_value, | |
89 | NULL, NULL); | |
90 | ||
daacdbfc | 91 | int lttng_add_vpid_to_ctx(struct lttng_ust_ctx **ctx) |
c1ef86f0 | 92 | { |
a084756d | 93 | int ret; |
c1ef86f0 | 94 | |
4e48b5d2 | 95 | if (lttng_find_context(*ctx, ctx_field->event_field->name)) { |
a084756d MD |
96 | ret = -EEXIST; |
97 | goto error_find_context; | |
c1ef86f0 | 98 | } |
4e48b5d2 MD |
99 | ret = lttng_ust_context_append(ctx, ctx_field); |
100 | if (ret) | |
101 | return ret; | |
c1ef86f0 | 102 | return 0; |
a084756d | 103 | |
a084756d | 104 | error_find_context: |
a084756d | 105 | return ret; |
c1ef86f0 | 106 | } |