Fix: Clamp verbosity value read from config file
[lttng-tools.git] / src / bin / lttng-sessiond / ust-registry.c
1 /*
2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17 #define _GNU_SOURCE
18 #include <assert.h>
19 #include <inttypes.h>
20
21 #include <common/common.h>
22 #include <common/hashtable/utils.h>
23 #include <lttng/lttng.h>
24
25 #include "ust-registry.h"
26 #include "ust-app.h"
27 #include "utils.h"
28
29 /*
30 * Hash table match function for event in the registry.
31 */
32 static int ht_match_event(struct cds_lfht_node *node, const void *_key)
33 {
34 struct ust_registry_event *event;
35 const struct ust_registry_event *key;
36
37 assert(node);
38 assert(_key);
39
40 event = caa_container_of(node, struct ust_registry_event, node.node);
41 assert(event);
42 key = _key;
43
44 /* It has to be a perfect match. */
45 if (strncmp(event->name, key->name, sizeof(event->name)) != 0) {
46 goto no_match;
47 }
48
49 /* It has to be a perfect match. */
50 if (strncmp(event->signature, key->signature,
51 strlen(event->signature) != 0)) {
52 goto no_match;
53 }
54
55 /* Match */
56 return 1;
57
58 no_match:
59 return 0;
60 }
61
62 static unsigned long ht_hash_event(void *_key, unsigned long seed)
63 {
64 uint64_t xored_key;
65 struct ust_registry_event *key = _key;
66
67 assert(key);
68
69 xored_key = (uint64_t) (hash_key_str(key->name, seed) ^
70 hash_key_str(key->signature, seed));
71
72 return hash_key_u64(&xored_key, seed);
73 }
74
75 /*
76 * Return negative value on error, 0 if OK.
77 *
78 * TODO: we could add stricter verification of more types to catch
79 * errors in liblttng-ust implementation earlier than consumption by the
80 * trace reader.
81 */
82 static
83 int validate_event_field(struct ustctl_field *field,
84 const char *event_name,
85 struct ust_app *app)
86 {
87 switch(field->type.atype) {
88 case ustctl_atype_integer:
89 case ustctl_atype_enum:
90 case ustctl_atype_array:
91 case ustctl_atype_sequence:
92 case ustctl_atype_string:
93 break;
94
95 case ustctl_atype_float:
96 switch (field->type.u.basic._float.mant_dig) {
97 case 0:
98 WARN("UST application '%s' (pid: %d) has unknown float mantissa '%u' "
99 "in field '%s', rejecting event '%s'",
100 app->name, app->pid,
101 field->type.u.basic._float.mant_dig,
102 field->name,
103 event_name);
104 return -EINVAL;
105 default:
106 break;
107 }
108 break;
109
110 default:
111 return -ENOENT;
112 }
113 return 0;
114 }
115
116 static
117 int validate_event_fields(size_t nr_fields, struct ustctl_field *fields,
118 const char *event_name, struct ust_app *app)
119 {
120 unsigned int i;
121
122 for (i = 0; i < nr_fields; i++) {
123 if (validate_event_field(&fields[i], event_name, app) < 0)
124 return -EINVAL;
125 }
126 return 0;
127 }
128
129 /*
130 * Allocate event and initialize it. This does NOT set a valid event id from a
131 * registry.
132 */
133 static struct ust_registry_event *alloc_event(int session_objd,
134 int channel_objd, char *name, char *sig, size_t nr_fields,
135 struct ustctl_field *fields, int loglevel, char *model_emf_uri,
136 struct ust_app *app)
137 {
138 struct ust_registry_event *event = NULL;
139
140 /*
141 * Ensure that the field content is valid.
142 */
143 if (validate_event_fields(nr_fields, fields, name, app) < 0) {
144 return NULL;
145 }
146
147 event = zmalloc(sizeof(*event));
148 if (!event) {
149 PERROR("zmalloc ust registry event");
150 goto error;
151 }
152
153 event->session_objd = session_objd;
154 event->channel_objd = channel_objd;
155 /* Allocated by ustctl. */
156 event->signature = sig;
157 event->nr_fields = nr_fields;
158 event->fields = fields;
159 event->loglevel = loglevel;
160 event->model_emf_uri = model_emf_uri;
161 if (name) {
162 /* Copy event name and force NULL byte. */
163 strncpy(event->name, name, sizeof(event->name));
164 event->name[sizeof(event->name) - 1] = '\0';
165 }
166 cds_lfht_node_init(&event->node.node);
167
168 error:
169 return event;
170 }
171
172 /*
173 * Free event data structure. This does NOT delete it from any hash table. It's
174 * safe to pass a NULL pointer. This shoudl be called inside a call RCU if the
175 * event is previously deleted from a rcu hash table.
176 */
177 static void destroy_event(struct ust_registry_event *event)
178 {
179 if (!event) {
180 return;
181 }
182
183 free(event->fields);
184 free(event->model_emf_uri);
185 free(event->signature);
186 free(event);
187 }
188
189 /*
190 * Destroy event function call of the call RCU.
191 */
192 static void destroy_event_rcu(struct rcu_head *head)
193 {
194 struct lttng_ht_node_u64 *node =
195 caa_container_of(head, struct lttng_ht_node_u64, head);
196 struct ust_registry_event *event =
197 caa_container_of(node, struct ust_registry_event, node);
198
199 destroy_event(event);
200 }
201
202 /*
203 * Find an event using the name and signature in the given registry. RCU read
204 * side lock MUST be acquired before calling this function and as long as the
205 * event reference is kept by the caller.
206 *
207 * On success, the event pointer is returned else NULL.
208 */
209 struct ust_registry_event *ust_registry_find_event(
210 struct ust_registry_channel *chan, char *name, char *sig)
211 {
212 struct lttng_ht_node_u64 *node;
213 struct lttng_ht_iter iter;
214 struct ust_registry_event *event = NULL;
215 struct ust_registry_event key;
216
217 assert(chan);
218 assert(name);
219 assert(sig);
220
221 /* Setup key for the match function. */
222 strncpy(key.name, name, sizeof(key.name));
223 key.name[sizeof(key.name) - 1] = '\0';
224 key.signature = sig;
225
226 cds_lfht_lookup(chan->ht->ht, chan->ht->hash_fct(&key, lttng_ht_seed),
227 chan->ht->match_fct, &key, &iter.iter);
228 node = lttng_ht_iter_get_node_u64(&iter);
229 if (!node) {
230 goto end;
231 }
232 event = caa_container_of(node, struct ust_registry_event, node);
233
234 end:
235 return event;
236 }
237
238 /*
239 * Create a ust_registry_event from the given parameters and add it to the
240 * registry hash table. If event_id is valid, it is set with the newly created
241 * event id.
242 *
243 * On success, return 0 else a negative value. The created event MUST be unique
244 * so on duplicate entry -EINVAL is returned. On error, event_id is untouched.
245 *
246 * Should be called with session registry mutex held.
247 */
248 int ust_registry_create_event(struct ust_registry_session *session,
249 uint64_t chan_key, int session_objd, int channel_objd, char *name,
250 char *sig, size_t nr_fields, struct ustctl_field *fields, int loglevel,
251 char *model_emf_uri, int buffer_type, uint32_t *event_id_p,
252 struct ust_app *app)
253 {
254 int ret;
255 uint32_t event_id;
256 struct cds_lfht_node *nptr;
257 struct ust_registry_event *event = NULL;
258 struct ust_registry_channel *chan;
259
260 assert(session);
261 assert(name);
262 assert(sig);
263 assert(event_id_p);
264
265 rcu_read_lock();
266
267 /*
268 * This should not happen but since it comes from the UST tracer, an
269 * external party, don't assert and simply validate values.
270 */
271 if (session_objd < 0 || channel_objd < 0) {
272 ret = -EINVAL;
273 goto error_free;
274 }
275
276 chan = ust_registry_channel_find(session, chan_key);
277 if (!chan) {
278 ret = -EINVAL;
279 goto error_free;
280 }
281
282 /* Check if we've reached the maximum possible id. */
283 if (ust_registry_is_max_id(chan->used_event_id)) {
284 ret = -ENOENT;
285 goto error_free;
286 }
287
288 event = alloc_event(session_objd, channel_objd, name, sig, nr_fields,
289 fields, loglevel, model_emf_uri, app);
290 if (!event) {
291 ret = -ENOMEM;
292 goto error_free;
293 }
294
295 DBG3("UST registry creating event with event: %s, sig: %s, id: %u, "
296 "chan_objd: %u, sess_objd: %u, chan_id: %u", event->name,
297 event->signature, event->id, event->channel_objd,
298 event->session_objd, chan->chan_id);
299
300 /*
301 * This is an add unique with a custom match function for event. The node
302 * are matched using the event name and signature.
303 */
304 nptr = cds_lfht_add_unique(chan->ht->ht, chan->ht->hash_fct(event,
305 lttng_ht_seed), chan->ht->match_fct, event, &event->node.node);
306 if (nptr != &event->node.node) {
307 if (buffer_type == LTTNG_BUFFER_PER_UID) {
308 /*
309 * This is normal, we just have to send the event id of the
310 * returned node and make sure we destroy the previously allocated
311 * event object.
312 */
313 destroy_event(event);
314 event = caa_container_of(nptr, struct ust_registry_event,
315 node.node);
316 assert(event);
317 event_id = event->id;
318 } else {
319 ERR("UST registry create event add unique failed for event: %s, "
320 "sig: %s, id: %u, chan_objd: %u, sess_objd: %u",
321 event->name, event->signature, event->id,
322 event->channel_objd, event->session_objd);
323 ret = -EINVAL;
324 goto error_unlock;
325 }
326 } else {
327 /* Request next event id if the node was successfully added. */
328 event_id = event->id = ust_registry_get_next_event_id(chan);
329 }
330
331 *event_id_p = event_id;
332
333 if (!event->metadata_dumped) {
334 /* Append to metadata */
335 ret = ust_metadata_event_statedump(session, chan, event);
336 if (ret) {
337 ERR("Error appending event metadata (errno = %d)", ret);
338 rcu_read_unlock();
339 return ret;
340 }
341 }
342
343 rcu_read_unlock();
344 return 0;
345
346 error_free:
347 free(sig);
348 free(fields);
349 free(model_emf_uri);
350 error_unlock:
351 rcu_read_unlock();
352 destroy_event(event);
353 return ret;
354 }
355
356 /*
357 * For a given event in a registry, delete the entry and destroy the event.
358 * This MUST be called within a RCU read side lock section.
359 */
360 void ust_registry_destroy_event(struct ust_registry_channel *chan,
361 struct ust_registry_event *event)
362 {
363 int ret;
364 struct lttng_ht_iter iter;
365
366 assert(chan);
367 assert(event);
368
369 /* Delete the node first. */
370 iter.iter.node = &event->node.node;
371 ret = lttng_ht_del(chan->ht, &iter);
372 assert(!ret);
373
374 call_rcu(&event->node.head, destroy_event_rcu);
375
376 return;
377 }
378
379 /*
380 * We need to execute ht_destroy outside of RCU read-side critical
381 * section and outside of call_rcu thread, so we postpone its execution
382 * using ht_cleanup_push. It is simpler than to change the semantic of
383 * the many callers of delete_ust_app_session().
384 */
385 static
386 void destroy_channel_rcu(struct rcu_head *head)
387 {
388 struct ust_registry_channel *chan =
389 caa_container_of(head, struct ust_registry_channel, rcu_head);
390
391 if (chan->ht) {
392 ht_cleanup_push(chan->ht);
393 }
394 free(chan->ctx_fields);
395 free(chan);
396 }
397
398 /*
399 * Destroy every element of the registry and free the memory. This does NOT
400 * free the registry pointer since it might not have been allocated before so
401 * it's the caller responsability.
402 */
403 static void destroy_channel(struct ust_registry_channel *chan)
404 {
405 struct lttng_ht_iter iter;
406 struct ust_registry_event *event;
407
408 assert(chan);
409
410 rcu_read_lock();
411 /* Destroy all event associated with this registry. */
412 cds_lfht_for_each_entry(chan->ht->ht, &iter.iter, event, node.node) {
413 /* Delete the node from the ht and free it. */
414 ust_registry_destroy_event(chan, event);
415 }
416 rcu_read_unlock();
417 call_rcu(&chan->rcu_head, destroy_channel_rcu);
418 }
419
420 /*
421 * Initialize registry with default values.
422 */
423 int ust_registry_channel_add(struct ust_registry_session *session,
424 uint64_t key)
425 {
426 int ret = 0;
427 struct ust_registry_channel *chan;
428
429 assert(session);
430
431 chan = zmalloc(sizeof(*chan));
432 if (!chan) {
433 PERROR("zmalloc ust registry channel");
434 ret = -ENOMEM;
435 goto error_alloc;
436 }
437
438 chan->ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
439 if (!chan->ht) {
440 ret = -ENOMEM;
441 goto error;
442 }
443
444 /* Set custom match function. */
445 chan->ht->match_fct = ht_match_event;
446 chan->ht->hash_fct = ht_hash_event;
447
448 /*
449 * Assign a channel ID right now since the event notification comes
450 * *before* the channel notify so the ID needs to be set at this point so
451 * the metadata can be dumped for that event.
452 */
453 if (ust_registry_is_max_id(session->used_channel_id)) {
454 ret = -1;
455 goto error;
456 }
457 chan->chan_id = ust_registry_get_next_chan_id(session);
458
459 rcu_read_lock();
460 lttng_ht_node_init_u64(&chan->node, key);
461 lttng_ht_add_unique_u64(session->channels, &chan->node);
462 rcu_read_unlock();
463
464 return 0;
465
466 error:
467 destroy_channel(chan);
468 error_alloc:
469 return ret;
470 }
471
472 /*
473 * Find a channel in the given registry. RCU read side lock MUST be acquired
474 * before calling this function and as long as the event reference is kept by
475 * the caller.
476 *
477 * On success, the pointer is returned else NULL.
478 */
479 struct ust_registry_channel *ust_registry_channel_find(
480 struct ust_registry_session *session, uint64_t key)
481 {
482 struct lttng_ht_node_u64 *node;
483 struct lttng_ht_iter iter;
484 struct ust_registry_channel *chan = NULL;
485
486 assert(session);
487 assert(session->channels);
488
489 DBG3("UST registry channel finding key %" PRIu64, key);
490
491 lttng_ht_lookup(session->channels, &key, &iter);
492 node = lttng_ht_iter_get_node_u64(&iter);
493 if (!node) {
494 goto end;
495 }
496 chan = caa_container_of(node, struct ust_registry_channel, node);
497
498 end:
499 return chan;
500 }
501
502 /*
503 * Remove channel using key from registry and free memory.
504 */
505 void ust_registry_channel_del_free(struct ust_registry_session *session,
506 uint64_t key)
507 {
508 struct lttng_ht_iter iter;
509 struct ust_registry_channel *chan;
510 int ret;
511
512 assert(session);
513
514 rcu_read_lock();
515 chan = ust_registry_channel_find(session, key);
516 if (!chan) {
517 rcu_read_unlock();
518 goto end;
519 }
520
521 iter.iter.node = &chan->node.node;
522 ret = lttng_ht_del(session->channels, &iter);
523 assert(!ret);
524 rcu_read_unlock();
525 destroy_channel(chan);
526
527 end:
528 return;
529 }
530
531 /*
532 * Initialize registry with default values and set the newly allocated session
533 * pointer to sessionp.
534 *
535 * Return 0 on success and sessionp is set or else return -1 and sessionp is
536 * kept untouched.
537 */
538 int ust_registry_session_init(struct ust_registry_session **sessionp,
539 struct ust_app *app,
540 uint32_t bits_per_long,
541 uint32_t uint8_t_alignment,
542 uint32_t uint16_t_alignment,
543 uint32_t uint32_t_alignment,
544 uint32_t uint64_t_alignment,
545 uint32_t long_alignment,
546 int byte_order,
547 uint32_t major,
548 uint32_t minor)
549 {
550 int ret;
551 struct ust_registry_session *session;
552
553 assert(sessionp);
554
555 session = zmalloc(sizeof(*session));
556 if (!session) {
557 PERROR("zmalloc ust registry session");
558 goto error_alloc;
559 }
560
561 pthread_mutex_init(&session->lock, NULL);
562 session->bits_per_long = bits_per_long;
563 session->uint8_t_alignment = uint8_t_alignment;
564 session->uint16_t_alignment = uint16_t_alignment;
565 session->uint32_t_alignment = uint32_t_alignment;
566 session->uint64_t_alignment = uint64_t_alignment;
567 session->long_alignment = long_alignment;
568 session->byte_order = byte_order;
569
570 session->channels = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
571 if (!session->channels) {
572 goto error;
573 }
574
575 ret = lttng_uuid_generate(session->uuid);
576 if (ret) {
577 ERR("Failed to generate UST uuid (errno = %d)", ret);
578 goto error;
579 }
580
581 pthread_mutex_lock(&session->lock);
582 ret = ust_metadata_session_statedump(session, app, major, minor);
583 pthread_mutex_unlock(&session->lock);
584 if (ret) {
585 ERR("Failed to generate session metadata (errno = %d)", ret);
586 goto error;
587 }
588
589 *sessionp = session;
590
591 return 0;
592
593 error:
594 ust_registry_session_destroy(session);
595 free(session);
596 error_alloc:
597 return -1;
598 }
599
600 /*
601 * Destroy session registry. This does NOT free the given pointer since it
602 * might get passed as a reference. The registry lock should NOT be acquired.
603 */
604 void ust_registry_session_destroy(struct ust_registry_session *reg)
605 {
606 int ret;
607 struct lttng_ht_iter iter;
608 struct ust_registry_channel *chan;
609
610 assert(reg);
611
612 /* On error, EBUSY can be returned if lock. Code flow error. */
613 ret = pthread_mutex_destroy(&reg->lock);
614 assert(!ret);
615
616 if (reg->channels) {
617 rcu_read_lock();
618 /* Destroy all event associated with this registry. */
619 cds_lfht_for_each_entry(reg->channels->ht, &iter.iter, chan,
620 node.node) {
621 /* Delete the node from the ht and free it. */
622 ret = lttng_ht_del(reg->channels, &iter);
623 assert(!ret);
624 destroy_channel(chan);
625 }
626 rcu_read_unlock();
627 ht_cleanup_push(reg->channels);
628 }
629
630 free(reg->metadata);
631 }
This page took 0.044429 seconds and 4 git commands to generate.