Commit | Line | Data |
---|---|---|
8ab5c06b | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
8ab5c06b | 3 | * |
c0c0989a MJ |
4 | * Copyright (C) 2016 EfficiOS Inc. |
5 | * Copyright (C) 2016 Alexandre Montplaisir <alexmonthy@efficios.com> | |
6 | * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
8ab5c06b AM |
7 | */ |
8 | ||
9 | #include "org_lttng_ust_agent_context_LttngContextApi.h" | |
10 | ||
11 | #include <string.h> | |
12 | #include <inttypes.h> | |
13 | #include <lttng/ust-events.h> | |
ef6ae9f9 | 14 | #include <lttng/ringbuffer-context.h> |
9d315d6d | 15 | #include <common/ust-context-provider.h> |
8ab5c06b | 16 | |
9d315d6d | 17 | #include "common/macros.h" |
8ab5c06b AM |
18 | #include "lttng_ust_context.h" |
19 | ||
8ab5c06b AM |
20 | enum lttng_ust_jni_type { |
21 | JNI_TYPE_NULL = 0, | |
22 | JNI_TYPE_INTEGER = 1, | |
23 | JNI_TYPE_LONG = 2, | |
24 | JNI_TYPE_DOUBLE = 3, | |
25 | JNI_TYPE_FLOAT = 4, | |
26 | JNI_TYPE_BYTE = 5, | |
27 | JNI_TYPE_SHORT = 6, | |
28 | JNI_TYPE_BOOLEAN = 7, | |
29 | JNI_TYPE_STRING = 8, | |
30 | }; | |
31 | ||
b1ca4c5f AM |
32 | struct lttng_ust_jni_ctx_entry { |
33 | int32_t context_name_offset; | |
8ab5c06b AM |
34 | char type; /* enum lttng_ust_jni_type */ |
35 | union { | |
36 | int32_t _integer; | |
37 | int64_t _long; | |
38 | double _double; | |
39 | float _float; | |
40 | signed char _byte; | |
41 | int16_t _short; | |
42 | signed char _boolean; | |
b1ca4c5f | 43 | int32_t _string_offset; |
8ab5c06b AM |
44 | } value; |
45 | } __attribute__((packed)); | |
46 | ||
4e48b5d2 MD |
47 | struct lttng_ust_jni_provider { |
48 | struct lttng_ust_registered_context_provider *reg_provider; | |
49 | char *name; | |
50 | struct lttng_ust_context_provider provider; | |
51 | }; | |
52 | ||
8ab5c06b | 53 | /* TLS passing context info from JNI to callbacks. */ |
16adecf1 | 54 | __thread struct lttng_ust_jni_tls lttng_ust_context_info_tls; |
8ab5c06b | 55 | |
b1ca4c5f AM |
56 | static const char *get_ctx_string_at_offset(int32_t offset) |
57 | { | |
58 | signed char *ctx_strings_array = lttng_ust_context_info_tls.ctx_strings; | |
59 | ||
60 | if (offset < 0 || offset >= lttng_ust_context_info_tls.ctx_strings_len) { | |
61 | return NULL; | |
62 | } | |
63 | return (const char *) (ctx_strings_array + offset); | |
64 | } | |
65 | ||
66 | static struct lttng_ust_jni_ctx_entry *lookup_ctx_by_name(const char *ctx_name) | |
8ab5c06b | 67 | { |
b1ca4c5f AM |
68 | struct lttng_ust_jni_ctx_entry *ctx_entries_array = lttng_ust_context_info_tls.ctx_entries; |
69 | int i, len = lttng_ust_context_info_tls.ctx_entries_len / sizeof(struct lttng_ust_jni_ctx_entry); | |
8ab5c06b AM |
70 | |
71 | for (i = 0; i < len; i++) { | |
b1ca4c5f AM |
72 | int32_t offset = ctx_entries_array[i].context_name_offset; |
73 | const char *string = get_ctx_string_at_offset(offset); | |
74 | ||
75 | if (string && strcmp(string, ctx_name) == 0) { | |
76 | return &ctx_entries_array[i]; | |
77 | } | |
8ab5c06b AM |
78 | } |
79 | return NULL; | |
8ab5c06b AM |
80 | } |
81 | ||
4e48b5d2 | 82 | static size_t get_size_cb(void *priv, size_t offset) |
8ab5c06b | 83 | { |
b1ca4c5f | 84 | struct lttng_ust_jni_ctx_entry *jctx; |
8ab5c06b | 85 | size_t size = 0; |
4e48b5d2 MD |
86 | struct lttng_ust_jni_provider *jni_provider = (struct lttng_ust_jni_provider *) priv; |
87 | const char *ctx_name = jni_provider->name; | |
8ab5c06b AM |
88 | enum lttng_ust_jni_type jni_type; |
89 | ||
dc325c1d | 90 | size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(char)); |
8ab5c06b AM |
91 | size += sizeof(char); /* tag */ |
92 | jctx = lookup_ctx_by_name(ctx_name); | |
93 | if (!jctx) { | |
94 | jni_type = JNI_TYPE_NULL; | |
95 | } else { | |
96 | jni_type = jctx->type; | |
97 | } | |
98 | switch (jni_type) { | |
99 | case JNI_TYPE_NULL: | |
100 | break; | |
101 | case JNI_TYPE_INTEGER: | |
dc325c1d | 102 | size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(int32_t)); |
8ab5c06b AM |
103 | size += sizeof(int32_t); /* variant */ |
104 | break; | |
105 | case JNI_TYPE_LONG: | |
dc325c1d | 106 | size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(int64_t)); |
8ab5c06b AM |
107 | size += sizeof(int64_t); /* variant */ |
108 | break; | |
109 | case JNI_TYPE_DOUBLE: | |
dc325c1d | 110 | size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(double)); |
8ab5c06b AM |
111 | size += sizeof(double); /* variant */ |
112 | break; | |
113 | case JNI_TYPE_FLOAT: | |
dc325c1d | 114 | size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(float)); |
8ab5c06b AM |
115 | size += sizeof(float); /* variant */ |
116 | break; | |
117 | case JNI_TYPE_SHORT: | |
dc325c1d | 118 | size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(int16_t)); |
8ab5c06b AM |
119 | size += sizeof(int16_t); /* variant */ |
120 | break; | |
121 | case JNI_TYPE_BYTE: /* Fall-through. */ | |
122 | case JNI_TYPE_BOOLEAN: | |
dc325c1d | 123 | size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(char)); |
8ab5c06b AM |
124 | size += sizeof(char); /* variant */ |
125 | break; | |
126 | case JNI_TYPE_STRING: | |
b1ca4c5f AM |
127 | { |
128 | /* The value is an offset, the string is in the "strings" array */ | |
129 | int32_t string_offset = jctx->value._string_offset; | |
130 | const char *string = get_ctx_string_at_offset(string_offset); | |
131 | ||
132 | if (string) { | |
133 | size += strlen(string) + 1; | |
134 | } | |
8ab5c06b | 135 | break; |
b1ca4c5f | 136 | } |
8ab5c06b AM |
137 | default: |
138 | abort(); | |
139 | } | |
140 | return size; | |
141 | ||
142 | } | |
143 | ||
4e48b5d2 | 144 | static void record_cb(void *priv, |
8ab5c06b | 145 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
e016c06a | 146 | struct lttng_ust_channel_buffer *lttng_chan_buf) |
8ab5c06b | 147 | { |
b1ca4c5f | 148 | struct lttng_ust_jni_ctx_entry *jctx; |
4e48b5d2 MD |
149 | struct lttng_ust_jni_provider *jni_provider = (struct lttng_ust_jni_provider *) priv; |
150 | const char *ctx_name = jni_provider->name; | |
8ab5c06b AM |
151 | enum lttng_ust_jni_type jni_type; |
152 | char sel_char; | |
153 | ||
154 | jctx = lookup_ctx_by_name(ctx_name); | |
155 | if (!jctx) { | |
156 | jni_type = JNI_TYPE_NULL; | |
157 | } else { | |
158 | jni_type = jctx->type; | |
159 | } | |
160 | ||
161 | switch (jni_type) { | |
162 | case JNI_TYPE_NULL: | |
163 | sel_char = LTTNG_UST_DYNAMIC_TYPE_NONE; | |
8936b6c0 | 164 | lttng_chan_buf->ops->event_write(ctx, &sel_char, sizeof(sel_char), lttng_ust_rb_alignof(char)); |
8ab5c06b AM |
165 | break; |
166 | case JNI_TYPE_INTEGER: | |
167 | { | |
168 | int32_t v = jctx->value._integer; | |
169 | ||
170 | sel_char = LTTNG_UST_DYNAMIC_TYPE_S32; | |
8936b6c0 MD |
171 | lttng_chan_buf->ops->event_write(ctx, &sel_char, sizeof(sel_char), lttng_ust_rb_alignof(char)); |
172 | lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v)); | |
8ab5c06b AM |
173 | break; |
174 | } | |
175 | case JNI_TYPE_LONG: | |
176 | { | |
177 | int64_t v = jctx->value._long; | |
178 | ||
179 | sel_char = LTTNG_UST_DYNAMIC_TYPE_S64; | |
8936b6c0 MD |
180 | lttng_chan_buf->ops->event_write(ctx, &sel_char, sizeof(sel_char), lttng_ust_rb_alignof(char)); |
181 | lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v)); | |
8ab5c06b AM |
182 | break; |
183 | } | |
184 | case JNI_TYPE_DOUBLE: | |
185 | { | |
186 | double v = jctx->value._double; | |
187 | ||
188 | sel_char = LTTNG_UST_DYNAMIC_TYPE_DOUBLE; | |
8936b6c0 MD |
189 | lttng_chan_buf->ops->event_write(ctx, &sel_char, sizeof(sel_char), lttng_ust_rb_alignof(char)); |
190 | lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v)); | |
8ab5c06b AM |
191 | break; |
192 | } | |
193 | case JNI_TYPE_FLOAT: | |
194 | { | |
195 | float v = jctx->value._float; | |
196 | ||
197 | sel_char = LTTNG_UST_DYNAMIC_TYPE_FLOAT; | |
8936b6c0 MD |
198 | lttng_chan_buf->ops->event_write(ctx, &sel_char, sizeof(sel_char), lttng_ust_rb_alignof(char)); |
199 | lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v)); | |
8ab5c06b AM |
200 | break; |
201 | } | |
202 | case JNI_TYPE_SHORT: | |
203 | { | |
204 | int16_t v = jctx->value._short; | |
205 | ||
206 | sel_char = LTTNG_UST_DYNAMIC_TYPE_S16; | |
8936b6c0 MD |
207 | lttng_chan_buf->ops->event_write(ctx, &sel_char, sizeof(sel_char), lttng_ust_rb_alignof(char)); |
208 | lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v)); | |
8ab5c06b AM |
209 | break; |
210 | } | |
211 | case JNI_TYPE_BYTE: | |
212 | { | |
213 | char v = jctx->value._byte; | |
214 | ||
215 | sel_char = LTTNG_UST_DYNAMIC_TYPE_S8; | |
8936b6c0 MD |
216 | lttng_chan_buf->ops->event_write(ctx, &sel_char, sizeof(sel_char), lttng_ust_rb_alignof(char)); |
217 | lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v)); | |
8ab5c06b AM |
218 | break; |
219 | } | |
220 | case JNI_TYPE_BOOLEAN: | |
221 | { | |
222 | char v = jctx->value._boolean; | |
223 | ||
224 | sel_char = LTTNG_UST_DYNAMIC_TYPE_S8; | |
8936b6c0 MD |
225 | lttng_chan_buf->ops->event_write(ctx, &sel_char, sizeof(sel_char), lttng_ust_rb_alignof(char)); |
226 | lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v)); | |
8ab5c06b AM |
227 | break; |
228 | } | |
229 | case JNI_TYPE_STRING: | |
230 | { | |
b1ca4c5f AM |
231 | int32_t offset = jctx->value._string_offset; |
232 | const char *str = get_ctx_string_at_offset(offset); | |
8ab5c06b | 233 | |
b1ca4c5f AM |
234 | if (str) { |
235 | sel_char = LTTNG_UST_DYNAMIC_TYPE_STRING; | |
236 | } else { | |
237 | sel_char = LTTNG_UST_DYNAMIC_TYPE_NONE; | |
238 | } | |
8936b6c0 | 239 | lttng_chan_buf->ops->event_write(ctx, &sel_char, sizeof(sel_char), lttng_ust_rb_alignof(char)); |
b1ca4c5f | 240 | if (str) { |
8936b6c0 | 241 | lttng_chan_buf->ops->event_write(ctx, str, strlen(str) + 1, 1); |
b1ca4c5f | 242 | } |
8ab5c06b AM |
243 | break; |
244 | } | |
245 | default: | |
246 | abort(); | |
247 | } | |
248 | } | |
249 | ||
4e48b5d2 | 250 | static void get_value_cb(void *priv, struct lttng_ust_ctx_value *value) |
8ab5c06b | 251 | { |
4e48b5d2 | 252 | struct lttng_ust_jni_provider *jni_provider = (struct lttng_ust_jni_provider *) priv; |
b1ca4c5f | 253 | struct lttng_ust_jni_ctx_entry *jctx; |
4e48b5d2 | 254 | const char *ctx_name = jni_provider->name; |
8ab5c06b AM |
255 | enum lttng_ust_jni_type jni_type; |
256 | ||
257 | jctx = lookup_ctx_by_name(ctx_name); | |
258 | if (!jctx) { | |
259 | jni_type = JNI_TYPE_NULL; | |
260 | } else { | |
261 | jni_type = jctx->type; | |
262 | } | |
263 | ||
264 | switch (jni_type) { | |
265 | case JNI_TYPE_NULL: | |
266 | value->sel = LTTNG_UST_DYNAMIC_TYPE_NONE; | |
267 | break; | |
268 | case JNI_TYPE_INTEGER: | |
269 | value->sel = LTTNG_UST_DYNAMIC_TYPE_S64; | |
270 | value->u.s64 = (int64_t) jctx->value._integer; | |
271 | break; | |
272 | case JNI_TYPE_LONG: | |
273 | value->sel = LTTNG_UST_DYNAMIC_TYPE_S64; | |
274 | value->u.s64 = jctx->value._long; | |
275 | break; | |
276 | case JNI_TYPE_DOUBLE: | |
277 | value->sel = LTTNG_UST_DYNAMIC_TYPE_DOUBLE; | |
278 | value->u.d = jctx->value._double; | |
279 | break; | |
280 | case JNI_TYPE_FLOAT: | |
281 | value->sel = LTTNG_UST_DYNAMIC_TYPE_DOUBLE; | |
282 | value->u.d = (double) jctx->value._float; | |
283 | break; | |
284 | case JNI_TYPE_SHORT: | |
285 | value->sel = LTTNG_UST_DYNAMIC_TYPE_S64; | |
286 | value->u.s64 = (int64_t) jctx->value._short; | |
287 | break; | |
288 | case JNI_TYPE_BYTE: | |
289 | value->sel = LTTNG_UST_DYNAMIC_TYPE_S64; | |
290 | value->u.s64 = (int64_t) jctx->value._byte; | |
291 | break; | |
292 | case JNI_TYPE_BOOLEAN: | |
293 | value->sel = LTTNG_UST_DYNAMIC_TYPE_S64; | |
294 | value->u.s64 = (int64_t) jctx->value._boolean; | |
295 | break; | |
296 | case JNI_TYPE_STRING: | |
b1ca4c5f AM |
297 | { |
298 | int32_t offset = jctx->value._string_offset; | |
299 | const char *str = get_ctx_string_at_offset(offset); | |
300 | ||
301 | if (str) { | |
302 | value->sel = LTTNG_UST_DYNAMIC_TYPE_STRING; | |
303 | value->u.str = str; | |
304 | } else { | |
305 | value->sel = LTTNG_UST_DYNAMIC_TYPE_NONE; | |
306 | } | |
8ab5c06b | 307 | break; |
b1ca4c5f | 308 | } |
8ab5c06b AM |
309 | default: |
310 | abort(); | |
311 | } | |
312 | } | |
313 | ||
314 | /* | |
315 | * Register a context provider to UST. | |
316 | * | |
317 | * Called from the Java side when an application registers a context retriever, | |
318 | * so we create and register a corresponding provider on the C side. | |
319 | */ | |
320 | JNIEXPORT jlong JNICALL Java_org_lttng_ust_agent_context_LttngContextApi_registerProvider(JNIEnv *env, | |
2208d8b5 | 321 | jobject jobj __attribute__((unused)), |
8ab5c06b AM |
322 | jstring provider_name) |
323 | { | |
324 | jboolean iscopy; | |
325 | const char *provider_name_jstr; | |
326 | char *provider_name_cstr; | |
327 | struct lttng_ust_context_provider *provider; | |
4e48b5d2 | 328 | struct lttng_ust_jni_provider *jni_provider; |
8ab5c06b AM |
329 | /* |
330 | * Note: a "jlong" is 8 bytes on all architectures, whereas a | |
331 | * C "long" varies. | |
332 | */ | |
333 | jlong provider_ref; | |
334 | ||
335 | provider_name_jstr = (*env)->GetStringUTFChars(env, provider_name, &iscopy); | |
336 | if (!provider_name_jstr) { | |
337 | goto error_jstr; | |
338 | } | |
339 | /* Keep our own copy of the string so UST can use it. */ | |
340 | provider_name_cstr = strdup(provider_name_jstr); | |
341 | (*env)->ReleaseStringUTFChars(env, provider_name, provider_name_jstr); | |
342 | if (!provider_name_cstr) { | |
343 | goto error_strdup; | |
344 | } | |
4e48b5d2 MD |
345 | jni_provider = zmalloc(sizeof(*jni_provider)); |
346 | if (!jni_provider) { | |
8ab5c06b AM |
347 | goto error_provider; |
348 | } | |
4e48b5d2 | 349 | provider = &jni_provider->provider; |
daacdbfc | 350 | provider->struct_size = sizeof(*provider); |
4e48b5d2 MD |
351 | jni_provider->name = provider_name_cstr; |
352 | provider->name = jni_provider->name; | |
8ab5c06b AM |
353 | provider->get_size = get_size_cb; |
354 | provider->record = record_cb; | |
355 | provider->get_value = get_value_cb; | |
4e48b5d2 | 356 | provider->priv = jni_provider; |
8ab5c06b | 357 | |
4e48b5d2 MD |
358 | jni_provider->reg_provider = lttng_ust_context_provider_register(provider); |
359 | if (!jni_provider->reg_provider) { | |
8ab5c06b AM |
360 | goto error_register; |
361 | } | |
362 | ||
4e48b5d2 | 363 | provider_ref = (jlong) (long) jni_provider; |
8ab5c06b AM |
364 | return provider_ref; |
365 | ||
366 | /* Error handling. */ | |
367 | error_register: | |
4e48b5d2 | 368 | free(jni_provider); |
8ab5c06b AM |
369 | error_provider: |
370 | free(provider_name_cstr); | |
371 | error_strdup: | |
372 | error_jstr: | |
373 | return 0; | |
374 | } | |
375 | ||
376 | /* | |
377 | * Unregister a previously-registered context provider. | |
378 | * | |
379 | * Called from the Java side when an application unregisters a context retriever, | |
380 | * so we unregister and delete the corresponding provider on the C side. | |
381 | */ | |
2208d8b5 MJ |
382 | JNIEXPORT void JNICALL Java_org_lttng_ust_agent_context_LttngContextApi_unregisterProvider(JNIEnv *env __attribute__((unused)), |
383 | jobject jobj __attribute__((unused)), | |
8ab5c06b AM |
384 | jlong provider_ref) |
385 | { | |
4e48b5d2 MD |
386 | struct lttng_ust_jni_provider *jni_provider = |
387 | (struct lttng_ust_jni_provider *) (unsigned long) provider_ref; | |
8ab5c06b | 388 | |
4e48b5d2 | 389 | if (!jni_provider) { |
8ab5c06b AM |
390 | return; |
391 | } | |
392 | ||
4e48b5d2 | 393 | lttng_ust_context_provider_unregister(jni_provider->reg_provider); |
8ab5c06b | 394 | |
4e48b5d2 MD |
395 | free(jni_provider->name); |
396 | free(jni_provider); | |
8ab5c06b | 397 | } |