Commit | Line | Data |
---|---|---|
53569322 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
53569322 MD |
3 | * |
4 | * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
5 | * | |
c0c0989a | 6 | * LTTng UST application context provider. |
53569322 MD |
7 | */ |
8 | ||
3fbec7dc | 9 | #define _LGPL_SOURCE |
b4051ad8 | 10 | #include <stddef.h> |
fb31eb73 | 11 | #include <stdint.h> |
53569322 MD |
12 | #include <sys/types.h> |
13 | #include <unistd.h> | |
fb31eb73 | 14 | |
ae4b659d | 15 | #include <ust-context-provider.h> |
d8d2416d | 16 | |
53569322 MD |
17 | #include "lttng-tracer-core.h" |
18 | #include "jhash.h" | |
d8d2416d | 19 | #include "context-provider-internal.h" |
864a1eda | 20 | #include <ust-helper.h> |
53569322 MD |
21 | |
22 | #define CONTEXT_PROVIDER_HT_BITS 12 | |
23 | #define CONTEXT_PROVIDER_HT_SIZE (1U << CONTEXT_PROVIDER_HT_BITS) | |
24 | struct context_provider_ht { | |
25 | struct cds_hlist_head table[CONTEXT_PROVIDER_HT_SIZE]; | |
26 | }; | |
27 | ||
28 | static struct context_provider_ht context_provider_ht; | |
29 | ||
30 | static struct lttng_ust_context_provider * | |
31 | lookup_provider_by_name(const char *name) | |
32 | { | |
33 | struct cds_hlist_head *head; | |
34 | struct cds_hlist_node *node; | |
35 | struct lttng_ust_context_provider *provider; | |
36 | uint32_t hash; | |
37 | const char *end; | |
38 | size_t len; | |
39 | ||
40 | /* Lookup using everything before first ':' as key. */ | |
41 | end = strchr(name, ':'); | |
42 | if (end) | |
43 | len = end - name; | |
44 | else | |
45 | len = strlen(name); | |
46 | hash = jhash(name, len, 0); | |
47 | head = &context_provider_ht.table[hash & (CONTEXT_PROVIDER_HT_SIZE - 1)]; | |
48 | cds_hlist_for_each_entry(provider, node, head, node) { | |
49 | if (!strncmp(provider->name, name, len)) | |
50 | return provider; | |
51 | } | |
52 | return NULL; | |
53 | } | |
54 | ||
55 | int lttng_ust_context_provider_register(struct lttng_ust_context_provider *provider) | |
56 | { | |
57 | struct cds_hlist_head *head; | |
58 | size_t name_len = strlen(provider->name); | |
59 | uint32_t hash; | |
60 | int ret = 0; | |
61 | ||
c362addf MD |
62 | lttng_ust_fixup_tls(); |
63 | ||
53569322 | 64 | /* Provider name starts with "$app.". */ |
fe94775b | 65 | if (strncmp("$app.", provider->name, strlen("$app.")) != 0) |
53569322 | 66 | return -EINVAL; |
8b05d0d4 | 67 | /* Provider name cannot contain a colon character. */ |
53569322 MD |
68 | if (strchr(provider->name, ':')) |
69 | return -EINVAL; | |
70 | if (ust_lock()) { | |
71 | ret = -EBUSY; | |
72 | goto end; | |
73 | } | |
74 | if (lookup_provider_by_name(provider->name)) { | |
75 | ret = -EBUSY; | |
76 | goto end; | |
77 | } | |
78 | hash = jhash(provider->name, name_len, 0); | |
79 | head = &context_provider_ht.table[hash & (CONTEXT_PROVIDER_HT_SIZE - 1)]; | |
80 | cds_hlist_add_head(&provider->node, head); | |
d8d2416d | 81 | |
53569322 MD |
82 | lttng_ust_context_set_session_provider(provider->name, |
83 | provider->get_size, provider->record, | |
84 | provider->get_value); | |
d8d2416d FD |
85 | |
86 | lttng_ust_context_set_event_notifier_group_provider(provider->name, | |
87 | provider->get_size, provider->record, | |
88 | provider->get_value); | |
53569322 MD |
89 | end: |
90 | ust_unlock(); | |
91 | return ret; | |
92 | } | |
93 | ||
53569322 MD |
94 | void lttng_ust_context_provider_unregister(struct lttng_ust_context_provider *provider) |
95 | { | |
c362addf MD |
96 | lttng_ust_fixup_tls(); |
97 | ||
53569322 MD |
98 | if (ust_lock()) |
99 | goto end; | |
100 | lttng_ust_context_set_session_provider(provider->name, | |
ce7352a2 MD |
101 | lttng_ust_dummy_get_size, lttng_ust_dummy_record, |
102 | lttng_ust_dummy_get_value); | |
d8d2416d FD |
103 | |
104 | lttng_ust_context_set_event_notifier_group_provider(provider->name, | |
105 | lttng_ust_dummy_get_size, lttng_ust_dummy_record, | |
106 | lttng_ust_dummy_get_value); | |
107 | ||
53569322 MD |
108 | cds_hlist_del(&provider->node); |
109 | end: | |
110 | ust_unlock(); | |
111 | } | |
112 | ||
113 | /* | |
114 | * Called with ust mutex held. | |
115 | * Add application context to array of context, even if the application | |
116 | * context is not currently loaded by application. It will then use the | |
117 | * dummy callbacks in that case. | |
118 | * Always performed before tracing is started, since it modifies | |
119 | * metadata describing the context. | |
120 | */ | |
121 | int lttng_ust_add_app_context_to_ctx_rcu(const char *name, | |
122 | struct lttng_ctx **ctx) | |
123 | { | |
124 | struct lttng_ust_context_provider *provider; | |
125 | struct lttng_ctx_field new_field; | |
126 | int ret; | |
127 | ||
128 | if (*ctx && lttng_find_context(*ctx, name)) | |
129 | return -EEXIST; | |
130 | /* | |
131 | * For application context, add it by expanding | |
132 | * ctx array. | |
133 | */ | |
134 | memset(&new_field, 0, sizeof(new_field)); | |
135 | new_field.field_name = strdup(name); | |
136 | if (!new_field.field_name) | |
137 | return -ENOMEM; | |
138 | new_field.event_field.name = new_field.field_name; | |
139 | new_field.event_field.type.atype = atype_dynamic; | |
140 | /* | |
141 | * If provider is not found, we add the context anyway, but | |
142 | * it will provide a dummy context. | |
143 | */ | |
144 | provider = lookup_provider_by_name(name); | |
145 | if (provider) { | |
146 | new_field.get_size = provider->get_size; | |
147 | new_field.record = provider->record; | |
148 | new_field.get_value = provider->get_value; | |
149 | } else { | |
ce7352a2 MD |
150 | new_field.get_size = lttng_ust_dummy_get_size; |
151 | new_field.record = lttng_ust_dummy_record; | |
152 | new_field.get_value = lttng_ust_dummy_get_value; | |
53569322 MD |
153 | } |
154 | ret = lttng_context_add_rcu(ctx, &new_field); | |
155 | if (ret) { | |
156 | free(new_field.field_name); | |
157 | return ret; | |
158 | } | |
159 | return 0; | |
160 | } |