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