Fix: java application context segmentation fault
[lttng-ust.git] / src / lib / lttng-ust / lttng-context-provider.c
CommitLineData
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
9d315d6d 15#include <common/ust-context-provider.h>
d8d2416d 16
fc80554e 17#include "context-internal.h"
53569322 18#include "lttng-tracer-core.h"
e58e5ad5 19#include "common/jhash.h"
d8d2416d 20#include "context-provider-internal.h"
9d315d6d 21#include "common/macros.h"
8cd08025 22#include "common/tracer.h"
53569322 23
4e48b5d2
MD
24struct lttng_ust_registered_context_provider {
25 const struct lttng_ust_context_provider *provider;
26
27 struct cds_hlist_node node;
28};
29
53569322
MD
30#define CONTEXT_PROVIDER_HT_BITS 12
31#define CONTEXT_PROVIDER_HT_SIZE (1U << CONTEXT_PROVIDER_HT_BITS)
32struct context_provider_ht {
33 struct cds_hlist_head table[CONTEXT_PROVIDER_HT_SIZE];
34};
35
36static struct context_provider_ht context_provider_ht;
37
4e48b5d2 38static const struct lttng_ust_context_provider *
53569322
MD
39 lookup_provider_by_name(const char *name)
40{
41 struct cds_hlist_head *head;
42 struct cds_hlist_node *node;
4e48b5d2 43 struct lttng_ust_registered_context_provider *reg_provider;
53569322
MD
44 uint32_t hash;
45 const char *end;
46 size_t len;
47
48 /* Lookup using everything before first ':' as key. */
49 end = strchr(name, ':');
50 if (end)
51 len = end - name;
52 else
53 len = strlen(name);
54 hash = jhash(name, len, 0);
55 head = &context_provider_ht.table[hash & (CONTEXT_PROVIDER_HT_SIZE - 1)];
4e48b5d2
MD
56 cds_hlist_for_each_entry(reg_provider, node, head, node) {
57 if (!strncmp(reg_provider->provider->name, name, len))
58 return reg_provider->provider;
53569322
MD
59 }
60 return NULL;
61}
62
4e48b5d2 63struct lttng_ust_registered_context_provider *lttng_ust_context_provider_register(struct lttng_ust_context_provider *provider)
53569322 64{
4e48b5d2 65 struct lttng_ust_registered_context_provider *reg_provider = NULL;
53569322
MD
66 struct cds_hlist_head *head;
67 size_t name_len = strlen(provider->name);
68 uint32_t hash;
53569322 69
a9fd951a 70 lttng_ust_alloc_tls();
c362addf 71
53569322 72 /* Provider name starts with "$app.". */
fe94775b 73 if (strncmp("$app.", provider->name, strlen("$app.")) != 0)
4e48b5d2 74 return NULL;
8b05d0d4 75 /* Provider name cannot contain a colon character. */
53569322 76 if (strchr(provider->name, ':'))
4e48b5d2
MD
77 return NULL;
78 if (ust_lock())
53569322 79 goto end;
4e48b5d2 80 if (lookup_provider_by_name(provider->name))
53569322 81 goto end;
4e48b5d2
MD
82 reg_provider = zmalloc(sizeof(struct lttng_ust_registered_context_provider));
83 if (!reg_provider)
84 goto end;
85 reg_provider->provider = provider;
53569322
MD
86 hash = jhash(provider->name, name_len, 0);
87 head = &context_provider_ht.table[hash & (CONTEXT_PROVIDER_HT_SIZE - 1)];
4e48b5d2 88 cds_hlist_add_head(&reg_provider->node, head);
d8d2416d 89
53569322
MD
90 lttng_ust_context_set_session_provider(provider->name,
91 provider->get_size, provider->record,
4e48b5d2 92 provider->get_value, provider->priv);
d8d2416d
FD
93
94 lttng_ust_context_set_event_notifier_group_provider(provider->name,
95 provider->get_size, provider->record,
4e48b5d2 96 provider->get_value, provider->priv);
53569322
MD
97end:
98 ust_unlock();
4e48b5d2 99 return reg_provider;
53569322
MD
100}
101
4e48b5d2 102void lttng_ust_context_provider_unregister(struct lttng_ust_registered_context_provider *reg_provider)
53569322 103{
a9fd951a 104 lttng_ust_alloc_tls();
c362addf 105
53569322
MD
106 if (ust_lock())
107 goto end;
4e48b5d2 108 lttng_ust_context_set_session_provider(reg_provider->provider->name,
ce7352a2 109 lttng_ust_dummy_get_size, lttng_ust_dummy_record,
4e48b5d2 110 lttng_ust_dummy_get_value, NULL);
d8d2416d 111
4e48b5d2 112 lttng_ust_context_set_event_notifier_group_provider(reg_provider->provider->name,
d8d2416d 113 lttng_ust_dummy_get_size, lttng_ust_dummy_record,
4e48b5d2 114 lttng_ust_dummy_get_value, NULL);
d8d2416d 115
4e48b5d2 116 cds_hlist_del(&reg_provider->node);
53569322
MD
117end:
118 ust_unlock();
4e48b5d2 119 free(reg_provider);
53569322
MD
120}
121
122/*
123 * Called with ust mutex held.
124 * Add application context to array of context, even if the application
125 * context is not currently loaded by application. It will then use the
126 * dummy callbacks in that case.
127 * Always performed before tracing is started, since it modifies
128 * metadata describing the context.
129 */
130int lttng_ust_add_app_context_to_ctx_rcu(const char *name,
daacdbfc 131 struct lttng_ust_ctx **ctx)
53569322 132{
4e48b5d2 133 const struct lttng_ust_context_provider *provider;
f26f2f9b 134 struct lttng_ust_ctx_field new_field = { 0 };
4e48b5d2
MD
135 struct lttng_ust_event_field *event_field = NULL;
136 struct lttng_ust_type_common *type = NULL;
f26f2f9b 137 char *ctx_name;
53569322
MD
138 int ret;
139
140 if (*ctx && lttng_find_context(*ctx, name))
141 return -EEXIST;
4e48b5d2
MD
142 event_field = zmalloc(sizeof(struct lttng_ust_event_field));
143 if (!event_field) {
daacdbfc
MD
144 ret = -ENOMEM;
145 goto error_event_field_alloc;
146 }
f26f2f9b
MD
147 ctx_name = strdup(name);
148 if (!ctx_name) {
daacdbfc
MD
149 ret = -ENOMEM;
150 goto error_field_name_alloc;
151 }
4e48b5d2
MD
152 type = zmalloc(sizeof(struct lttng_ust_type_common));
153 if (!type) {
a084756d
MD
154 ret = -ENOMEM;
155 goto error_field_type_alloc;
156 }
f26f2f9b 157 event_field->name = ctx_name;
4e48b5d2
MD
158 type->type = lttng_ust_type_dynamic;
159 event_field->type = type;
f26f2f9b 160 new_field.event_field = event_field;
53569322
MD
161 /*
162 * If provider is not found, we add the context anyway, but
163 * it will provide a dummy context.
164 */
165 provider = lookup_provider_by_name(name);
166 if (provider) {
f26f2f9b
MD
167 new_field.get_size = provider->get_size;
168 new_field.record = provider->record;
169 new_field.get_value = provider->get_value;
239128ca 170 new_field.priv = provider->priv;
53569322 171 } else {
f26f2f9b
MD
172 new_field.get_size = lttng_ust_dummy_get_size;
173 new_field.record = lttng_ust_dummy_record;
174 new_field.get_value = lttng_ust_dummy_get_value;
239128ca 175 new_field.priv = NULL;
53569322 176 }
daacdbfc
MD
177 /*
178 * For application context, add it by expanding
f26f2f9b 179 * ctx array.
daacdbfc 180 */
f26f2f9b 181 ret = lttng_ust_context_append_rcu(ctx, &new_field);
53569322 182 if (ret) {
239128ca 183 goto error_append;
53569322
MD
184 }
185 return 0;
daacdbfc 186
239128ca 187error_append:
f26f2f9b 188 free(type);
a084756d 189error_field_type_alloc:
f26f2f9b 190 free(ctx_name);
daacdbfc 191error_field_name_alloc:
4e48b5d2 192 free(event_field);
daacdbfc 193error_event_field_alloc:
daacdbfc 194 return ret;
53569322 195}
This page took 0.038187 seconds and 4 git commands to generate.