Fix: leak of reply buffer on data pending check
[lttng-tools.git] / src / bin / lttng-sessiond / trace-ust.c
CommitLineData
97ee3a89
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
bdf64013 3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
97ee3a89 4 *
d14d33bf
AM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
97ee3a89 8 *
d14d33bf
AM
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
97ee3a89 13 *
d14d33bf
AM
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
97ee3a89
DG
17 */
18
6c1c0768 19#define _LGPL_SOURCE
97ee3a89
DG
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
d9bf3ca4 24#include <inttypes.h>
97ee3a89 25
990570ed
DG
26#include <common/common.h>
27#include <common/defaults.h>
97ee3a89 28
7972aab2 29#include "buffer-registry.h"
97ee3a89 30#include "trace-ust.h"
0b2dc8df 31#include "utils.h"
a9ad0c8f 32#include "ust-app.h"
7c1d2758 33#include "agent.h"
97ee3a89 34
025faf73
DG
35/*
36 * Match function for the events hash table lookup.
37 *
38 * Matches by name only. Used by the disable command.
39 */
18eace3b
DG
40int trace_ust_ht_match_event_by_name(struct cds_lfht_node *node,
41 const void *_key)
42{
43 struct ltt_ust_event *event;
44 const char *name;
45
46 assert(node);
47 assert(_key);
48
49 event = caa_container_of(node, struct ltt_ust_event, node.node);
50 name = _key;
51
52 /* Event name */
53 if (strncmp(event->attr.name, name, sizeof(event->attr.name)) != 0) {
54 goto no_match;
55 }
56
025faf73 57 /* Match */
18eace3b
DG
58 return 1;
59
60no_match:
61 return 0;
62}
63
025faf73
DG
64/*
65 * Match function for the hash table lookup.
66 *
67 * It matches an ust event based on three attributes which are the event name,
68 * the filter bytecode and the loglevel.
69 */
18eace3b
DG
70int trace_ust_ht_match_event(struct cds_lfht_node *node, const void *_key)
71{
72 struct ltt_ust_event *event;
73 const struct ltt_ust_ht_key *key;
2106efa0 74 int ev_loglevel_value;
19a97244 75 int ll_match;
18eace3b
DG
76
77 assert(node);
78 assert(_key);
79
80 event = caa_container_of(node, struct ltt_ust_event, node.node);
81 key = _key;
2106efa0 82 ev_loglevel_value = event->attr.loglevel;
18eace3b 83
f19e9f8b 84 /* Match the 4 elements of the key: name, filter, loglevel, exclusions. */
18eace3b
DG
85
86 /* Event name */
87 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
18eace3b
DG
88 goto no_match;
89 }
90
b953b8cd 91 /* Event loglevel value and type. */
19a97244
PP
92 ll_match = loglevels_match(event->attr.loglevel_type,
93 ev_loglevel_value, key->loglevel_type,
94 key->loglevel_value, LTTNG_UST_LOGLEVEL_ALL);
95
96 if (!ll_match) {
b953b8cd 97 goto no_match;
18eace3b
DG
98 }
99
100 /* Only one of the filters is NULL, fail. */
101 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
18eace3b
DG
102 goto no_match;
103 }
104
025faf73
DG
105 if (key->filter && event->filter) {
106 /* Both filters exists, check length followed by the bytecode. */
107 if (event->filter->len != key->filter->len ||
108 memcmp(event->filter->data, key->filter->data,
109 event->filter->len) != 0) {
110 goto no_match;
111 }
18eace3b
DG
112 }
113
f19e9f8b
JI
114 /* If only one of the exclusions is NULL, fail. */
115 if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) {
116 goto no_match;
117 }
118
119 if (key->exclusion && event->exclusion) {
a5b7e00c
PP
120 size_t i;
121
122 /* Check exclusion counts first. */
123 if (event->exclusion->count != key->exclusion->count) {
f19e9f8b
JI
124 goto no_match;
125 }
a5b7e00c
PP
126
127 /* Compare names individually. */
128 for (i = 0; i < event->exclusion->count; ++i) {
129 size_t j;
130 bool found = false;
131 const char *name_ev =
132 LTTNG_EVENT_EXCLUSION_NAME_AT(
133 event->exclusion, i);
134
135 /*
136 * Compare this exclusion name to all the key's
137 * exclusion names.
138 */
139 for (j = 0; j < key->exclusion->count; ++j) {
140 const char *name_key =
141 LTTNG_EVENT_EXCLUSION_NAME_AT(
142 key->exclusion, j);
143
144 if (!strncmp(name_ev, name_key,
145 LTTNG_SYMBOL_NAME_LEN)) {
146 /* Names match! */
147 found = true;
148 break;
149 }
150 }
151
152 /*
153 * If the current exclusion name was not found amongst
154 * the key's exclusion names, then there's no match.
155 */
156 if (!found) {
157 goto no_match;
158 }
159 }
f19e9f8b 160 }
025faf73
DG
161 /* Match. */
162 return 1;
18eace3b
DG
163
164no_match:
165 return 0;
18eace3b
DG
166}
167
0177d773 168/*
2223c96f
DG
169 * Find the channel in the hashtable and return channel pointer. RCU read side
170 * lock MUST be acquired before calling this.
0177d773 171 */
bec39940 172struct ltt_ust_channel *trace_ust_find_channel_by_name(struct lttng_ht *ht,
f6a9efaa 173 char *name)
0177d773 174{
bec39940
DG
175 struct lttng_ht_node_str *node;
176 struct lttng_ht_iter iter;
0177d773 177
85076754
MD
178 /*
179 * If we receive an empty string for channel name, it means the
180 * default channel name is requested.
181 */
182 if (name[0] == '\0')
183 name = DEFAULT_CHANNEL_NAME;
184
bec39940
DG
185 lttng_ht_lookup(ht, (void *)name, &iter);
186 node = lttng_ht_iter_get_node_str(&iter);
f6a9efaa 187 if (node == NULL) {
44d3bd01
DG
188 goto error;
189 }
190
f6a9efaa 191 DBG2("Trace UST channel %s found by name", name);
0177d773 192
f6a9efaa 193 return caa_container_of(node, struct ltt_ust_channel, node);
97ee3a89
DG
194
195error:
f6a9efaa 196 DBG2("Trace UST channel %s not found by name", name);
97ee3a89
DG
197 return NULL;
198}
199
200/*
2223c96f
DG
201 * Find the event in the hashtable and return event pointer. RCU read side lock
202 * MUST be acquired before calling this.
97ee3a89 203 */
18eace3b 204struct ltt_ust_event *trace_ust_find_event(struct lttng_ht *ht,
2106efa0 205 char *name, struct lttng_filter_bytecode *filter,
b953b8cd
PP
206 enum lttng_ust_loglevel_type loglevel_type, int loglevel_value,
207 struct lttng_event_exclusion *exclusion)
97ee3a89 208{
bec39940
DG
209 struct lttng_ht_node_str *node;
210 struct lttng_ht_iter iter;
18eace3b 211 struct ltt_ust_ht_key key;
97ee3a89 212
18eace3b
DG
213 assert(name);
214 assert(ht);
215
216 key.name = name;
217 key.filter = filter;
b953b8cd
PP
218 key.loglevel_type = loglevel_type;
219 key.loglevel_value = loglevel_value;
10646003 220 key.exclusion = exclusion;
18eace3b 221
18eace3b
DG
222 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
223 trace_ust_ht_match_event, &key, &iter.iter);
bec39940 224 node = lttng_ht_iter_get_node_str(&iter);
f6a9efaa 225 if (node == NULL) {
97ee3a89
DG
226 goto error;
227 }
228
18eace3b 229 DBG2("Trace UST event %s found", key.name);
f6a9efaa
DG
230
231 return caa_container_of(node, struct ltt_ust_event, node);
97ee3a89
DG
232
233error:
18eace3b 234 DBG2("Trace UST event %s NOT found", key.name);
97ee3a89
DG
235 return NULL;
236}
237
fefd409b
DG
238/*
239 * Lookup an agent in the session agents hash table by domain type and return
240 * the object if found else NULL.
4da703ad
JG
241 *
242 * RCU read side lock must be acquired before calling and only released
243 * once the agent is no longer in scope or being used.
fefd409b
DG
244 */
245struct agent *trace_ust_find_agent(struct ltt_ust_session *session,
246 enum lttng_domain_type domain_type)
247{
248 struct agent *agt = NULL;
249 struct lttng_ht_node_u64 *node;
250 struct lttng_ht_iter iter;
251 uint64_t key;
252
253 assert(session);
254
255 DBG3("Trace ust agent lookup for domain %d", domain_type);
256
257 key = domain_type;
258
259 lttng_ht_lookup(session->agents, &key, &iter);
260 node = lttng_ht_iter_get_node_u64(&iter);
261 if (!node) {
262 goto end;
263 }
264 agt = caa_container_of(node, struct agent, node);
265
266end:
267 return agt;
268}
269
97ee3a89
DG
270/*
271 * Allocate and initialize a ust session data structure.
272 *
273 * Return pointer to structure or NULL.
274 */
d9bf3ca4 275struct ltt_ust_session *trace_ust_create_session(uint64_t session_id)
97ee3a89
DG
276{
277 struct ltt_ust_session *lus;
278
279 /* Allocate a new ltt ust session */
ba7f0ae5 280 lus = zmalloc(sizeof(struct ltt_ust_session));
97ee3a89 281 if (lus == NULL) {
ba7f0ae5 282 PERROR("create ust session zmalloc");
97ee3a89
DG
283 goto error;
284 }
285
286 /* Init data structure */
a991f516 287 lus->id = session_id;
14fb1ebe 288 lus->active = 0;
97ee3a89 289
84ad93e8
DG
290 /* Set default metadata channel attribute. */
291 lus->metadata_attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
292 lus->metadata_attr.subbuf_size = default_get_metadata_subbuf_size();
293 lus->metadata_attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
294 lus->metadata_attr.switch_timer_interval = DEFAULT_METADATA_SWITCH_TIMER;
295 lus->metadata_attr.read_timer_interval = DEFAULT_METADATA_READ_TIMER;
296 lus->metadata_attr.output = LTTNG_UST_MMAP;
297
7972aab2
DG
298 /*
299 * Default buffer type. This can be changed through an enable channel
300 * requesting a different type. Note that this can only be changed once
301 * during the session lifetime which is at the first enable channel and
302 * only before start. The flag buffer_type_changed indicates the status.
303 */
8692d4e5 304 lus->buffer_type = LTTNG_BUFFER_PER_UID;
7972aab2
DG
305 /* Once set to 1, the buffer_type is immutable for the session. */
306 lus->buffer_type_changed = 0;
307 /* Init it in case it get used after allocation. */
308 CDS_INIT_LIST_HEAD(&lus->buffer_reg_uid_list);
f6a9efaa
DG
309
310 /* Alloc UST global domain channels' HT */
bec39940 311 lus->domain_global.channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
fefd409b
DG
312 /* Alloc agent hash table. */
313 lus->agents = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
44d3bd01 314
00e2e675
DG
315 lus->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
316 if (lus->consumer == NULL) {
09a90bcd 317 goto error_consumer;
00e2e675
DG
318 }
319
44d3bd01
DG
320 DBG2("UST trace session create successful");
321
97ee3a89
DG
322 return lus;
323
09a90bcd 324error_consumer:
0b2dc8df 325 ht_cleanup_push(lus->domain_global.channels);
fefd409b 326 ht_cleanup_push(lus->agents);
44844c29 327 free(lus);
97ee3a89
DG
328error:
329 return NULL;
330}
331
332/*
333 * Allocate and initialize a ust channel data structure.
334 *
335 * Return pointer to structure or NULL.
336 */
51755dc8
JG
337struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
338 enum lttng_domain_type domain)
97ee3a89 339{
97ee3a89
DG
340 struct ltt_ust_channel *luc;
341
0525e9ae 342 assert(chan);
0525e9ae 343
37357452 344 luc = zmalloc(sizeof(struct ltt_ust_channel));
97ee3a89 345 if (luc == NULL) {
df0f840b 346 PERROR("ltt_ust_channel zmalloc");
97ee3a89
DG
347 goto error;
348 }
349
51755dc8
JG
350 luc->domain = domain;
351
44d3bd01 352 /* Copy UST channel attributes */
48842b30
DG
353 luc->attr.overwrite = chan->attr.overwrite;
354 luc->attr.subbuf_size = chan->attr.subbuf_size;
355 luc->attr.num_subbuf = chan->attr.num_subbuf;
356 luc->attr.switch_timer_interval = chan->attr.switch_timer_interval;
357 luc->attr.read_timer_interval = chan->attr.read_timer_interval;
6775595e 358 luc->attr.output = (enum lttng_ust_output) chan->attr.output;
44d3bd01
DG
359
360 /* Translate to UST output enum */
361 switch (luc->attr.output) {
362 default:
363 luc->attr.output = LTTNG_UST_MMAP;
364 break;
97ee3a89 365 }
97ee3a89 366
85076754
MD
367 /*
368 * If we receive an empty string for channel name, it means the
369 * default channel name is requested.
370 */
371 if (chan->name[0] == '\0') {
372 strncpy(luc->name, DEFAULT_CHANNEL_NAME, sizeof(luc->name));
373 } else {
374 /* Copy channel name */
375 strncpy(luc->name, chan->name, sizeof(luc->name));
376 }
97ee3a89
DG
377 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
378
f6a9efaa 379 /* Init node */
bec39940 380 lttng_ht_node_init_str(&luc->node, luc->name);
31746f93
DG
381 CDS_INIT_LIST_HEAD(&luc->ctx_list);
382
f6a9efaa 383 /* Alloc hash tables */
bec39940
DG
384 luc->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
385 luc->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
f6a9efaa 386
1624d5b7
JD
387 /* On-disk circular buffer parameters */
388 luc->tracefile_size = chan->attr.tracefile_size;
389 luc->tracefile_count = chan->attr.tracefile_count;
390
f6a9efaa
DG
391 DBG2("Trace UST channel %s created", luc->name);
392
97ee3a89 393error:
7972aab2 394 return luc;
97ee3a89
DG
395}
396
ad11a1d3
PP
397/*
398 * Validates an exclusion list.
399 *
400 * Returns 0 if valid, negative value if invalid.
401 */
402static int validate_exclusion(struct lttng_event_exclusion *exclusion)
403{
404 size_t i;
405 int ret = 0;
406
407 assert(exclusion);
408
409 for (i = 0; i < exclusion->count; ++i) {
410 size_t j;
411 const char *name_a =
412 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i);
413
414 for (j = 0; j < i; ++j) {
415 const char *name_b =
416 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, j);
417
418 if (!strncmp(name_a, name_b, LTTNG_SYMBOL_NAME_LEN)) {
419 /* Match! */
420 ret = -1;
421 goto end;
422 }
423 }
424 }
425
426end:
427 return ret;
428}
429
97ee3a89
DG
430/*
431 * Allocate and initialize a ust event. Set name and event type.
49d21f93 432 * We own filter_expression, filter, and exclusion.
97ee3a89
DG
433 *
434 * Return pointer to structure or NULL.
435 */
025faf73 436struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev,
6b453b5e 437 char *filter_expression,
561c6897 438 struct lttng_filter_bytecode *filter,
88f06f15
JG
439 struct lttng_event_exclusion *exclusion,
440 bool internal_event)
97ee3a89
DG
441{
442 struct ltt_ust_event *lue;
97ee3a89 443
0525e9ae
DG
444 assert(ev);
445
ad11a1d3
PP
446 if (exclusion && validate_exclusion(exclusion)) {
447 goto error;
448 }
449
37357452 450 lue = zmalloc(sizeof(struct ltt_ust_event));
44d3bd01 451 if (lue == NULL) {
ba7f0ae5 452 PERROR("ust event zmalloc");
97ee3a89
DG
453 goto error;
454 }
455
88f06f15
JG
456 lue->internal = internal_event;
457
97ee3a89
DG
458 switch (ev->type) {
459 case LTTNG_EVENT_PROBE:
44d3bd01 460 lue->attr.instrumentation = LTTNG_UST_PROBE;
97ee3a89
DG
461 break;
462 case LTTNG_EVENT_FUNCTION:
44d3bd01 463 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
464 break;
465 case LTTNG_EVENT_FUNCTION_ENTRY:
44d3bd01 466 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
467 break;
468 case LTTNG_EVENT_TRACEPOINT:
44d3bd01 469 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT;
97ee3a89
DG
470 break;
471 default:
472 ERR("Unknown ust instrumentation type (%d)", ev->type);
44844c29 473 goto error_free_event;
97ee3a89
DG
474 }
475
476 /* Copy event name */
44d3bd01
DG
477 strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
478 lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
97ee3a89 479
0cda4f28 480 switch (ev->loglevel_type) {
8005f29a
MD
481 case LTTNG_EVENT_LOGLEVEL_ALL:
482 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_ALL;
22e25b71 483 lue->attr.loglevel = -1; /* Force to -1 */
0cda4f28 484 break;
8005f29a
MD
485 case LTTNG_EVENT_LOGLEVEL_RANGE:
486 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_RANGE;
22e25b71 487 lue->attr.loglevel = ev->loglevel;
8005f29a
MD
488 break;
489 case LTTNG_EVENT_LOGLEVEL_SINGLE:
490 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE;
22e25b71 491 lue->attr.loglevel = ev->loglevel;
0cda4f28
MD
492 break;
493 default:
4431494b 494 ERR("Unknown ust loglevel type (%d)", ev->loglevel_type);
0cda4f28
MD
495 goto error_free_event;
496 }
497
025faf73 498 /* Same layout. */
6b453b5e 499 lue->filter_expression = filter_expression;
51755dc8
JG
500 lue->filter = filter;
501 lue->exclusion = exclusion;
0cda4f28 502
f6a9efaa 503 /* Init node */
bec39940 504 lttng_ht_node_init_str(&lue->node, lue->attr.name);
97ee3a89 505
300b8fd5
MD
506 DBG2("Trace UST event %s, loglevel (%d,%d) created",
507 lue->attr.name, lue->attr.loglevel_type,
508 lue->attr.loglevel);
284d8f55 509
97ee3a89
DG
510 return lue;
511
44844c29
MD
512error_free_event:
513 free(lue);
97ee3a89 514error:
49d21f93
MD
515 free(filter_expression);
516 free(filter);
517 free(exclusion);
97ee3a89
DG
518 return NULL;
519}
520
aa3514e9 521static
bdf64013
JG
522int trace_ust_context_type_event_to_ust(
523 enum lttng_event_context_type type)
55cc08a6 524{
aa3514e9 525 int utype;
0525e9ae 526
aa3514e9 527 switch (type) {
9197c5c4
MD
528 case LTTNG_EVENT_CONTEXT_VTID:
529 utype = LTTNG_UST_CONTEXT_VTID;
530 break;
531 case LTTNG_EVENT_CONTEXT_VPID:
532 utype = LTTNG_UST_CONTEXT_VPID;
533 break;
534 case LTTNG_EVENT_CONTEXT_PTHREAD_ID:
535 utype = LTTNG_UST_CONTEXT_PTHREAD_ID;
536 break;
537 case LTTNG_EVENT_CONTEXT_PROCNAME:
538 utype = LTTNG_UST_CONTEXT_PROCNAME;
539 break;
7c612c2e
WP
540 case LTTNG_EVENT_CONTEXT_IP:
541 utype = LTTNG_UST_CONTEXT_IP;
542 break;
aa3514e9 543 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
354e561b
MD
544 if (!ustctl_has_perf_counters()) {
545 utype = -1;
546 WARN("Perf counters not implemented in UST");
547 } else {
548 utype = LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER;
549 }
aa3514e9 550 break;
bdf64013
JG
551 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
552 utype = LTTNG_UST_CONTEXT_APP_CONTEXT;
553 break;
9197c5c4 554 default:
aa3514e9
MD
555 utype = -1;
556 break;
557 }
558 return utype;
559}
560
561/*
562 * Return 1 if contexts match, 0 otherwise.
563 */
564int trace_ust_match_context(struct ltt_ust_context *uctx,
565 struct lttng_event_context *ctx)
566{
567 int utype;
568
569 utype = trace_ust_context_type_event_to_ust(ctx->ctx);
570 if (utype < 0) {
571 return 0;
572 }
573 if (uctx->ctx.ctx != utype) {
574 return 0;
575 }
576 switch (utype) {
577 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
578 if (uctx->ctx.u.perf_counter.type
579 != ctx->u.perf_counter.type) {
580 return 0;
581 }
582 if (uctx->ctx.u.perf_counter.config
583 != ctx->u.perf_counter.config) {
584 return 0;
585 }
586 if (strncmp(uctx->ctx.u.perf_counter.name,
587 ctx->u.perf_counter.name,
588 LTTNG_UST_SYM_NAME_LEN)) {
589 return 0;
590 }
591 break;
54fb33cf
JG
592 case LTTNG_UST_CONTEXT_APP_CONTEXT:
593 assert(uctx->ctx.u.app_ctx.provider_name);
594 assert(uctx->ctx.u.app_ctx.ctx_name);
595 if (strcmp(uctx->ctx.u.app_ctx.provider_name,
596 ctx->u.app_ctx.provider_name) ||
597 strcmp(uctx->ctx.u.app_ctx.ctx_name,
598 ctx->u.app_ctx.ctx_name)) {
599 return 0;
600 }
aa3514e9
MD
601 default:
602 break;
603
604 }
605 return 1;
606}
607
608/*
609 * Allocate and initialize an UST context.
610 *
611 * Return pointer to structure or NULL.
612 */
613struct ltt_ust_context *trace_ust_create_context(
614 struct lttng_event_context *ctx)
615{
bdf64013 616 struct ltt_ust_context *uctx = NULL;
aa3514e9
MD
617 int utype;
618
619 assert(ctx);
620
621 utype = trace_ust_context_type_event_to_ust(ctx->ctx);
622 if (utype < 0) {
1cf992b8 623 ERR("Invalid UST context");
bdf64013 624 goto end;
9197c5c4 625 }
55cc08a6
DG
626
627 uctx = zmalloc(sizeof(struct ltt_ust_context));
bdf64013 628 if (!uctx) {
55cc08a6 629 PERROR("zmalloc ltt_ust_context");
bdf64013 630 goto end;
55cc08a6
DG
631 }
632
aa3514e9
MD
633 uctx->ctx.ctx = (enum lttng_ust_context_type) utype;
634 switch (utype) {
635 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
636 uctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type;
637 uctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config;
638 strncpy(uctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name,
639 LTTNG_UST_SYM_NAME_LEN);
640 uctx->ctx.u.perf_counter.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
641 break;
bdf64013
JG
642 case LTTNG_UST_CONTEXT_APP_CONTEXT:
643 {
644 char *provider_name = NULL, *ctx_name = NULL;
645
646 provider_name = strdup(ctx->u.app_ctx.provider_name);
647 if (!provider_name) {
648 goto error;
649 }
650 uctx->ctx.u.app_ctx.provider_name = provider_name;
651
652 ctx_name = strdup(ctx->u.app_ctx.ctx_name);
653 if (!ctx_name) {
654 goto error;
655 }
656 uctx->ctx.u.app_ctx.ctx_name = ctx_name;
657 break;
658 }
aa3514e9
MD
659 default:
660 break;
661 }
bec39940 662 lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx);
bdf64013 663end:
55cc08a6 664 return uctx;
55cc08a6 665error:
bdf64013 666 trace_ust_destroy_context(uctx);
55cc08a6
DG
667 return NULL;
668}
669
a9ad0c8f
MD
670static
671void destroy_pid_tracker_node_rcu(struct rcu_head *head)
672{
673 struct ust_pid_tracker_node *tracker_node =
674 caa_container_of(head, struct ust_pid_tracker_node, node.head);
675 free(tracker_node);
676}
677
678static
679void destroy_pid_tracker_node(struct ust_pid_tracker_node *tracker_node)
680{
681
682 call_rcu(&tracker_node->node.head, destroy_pid_tracker_node_rcu);
683}
684
685static
686int init_pid_tracker(struct ust_pid_tracker *pid_tracker)
687{
688 int ret = 0;
689
690 pid_tracker->ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
691 if (!pid_tracker->ht) {
692 ret = -1;
693 goto end;
694 }
695
696end:
697 return ret;
698}
699
700/*
701 * Teardown pid tracker content, but don't free pid_tracker object.
702 */
703static
704void fini_pid_tracker(struct ust_pid_tracker *pid_tracker)
705{
706 struct ust_pid_tracker_node *tracker_node;
707 struct lttng_ht_iter iter;
708
709 if (!pid_tracker->ht) {
710 return;
711 }
712 rcu_read_lock();
713 cds_lfht_for_each_entry(pid_tracker->ht->ht,
714 &iter.iter, tracker_node, node.node) {
715 int ret = lttng_ht_del(pid_tracker->ht, &iter);
716
717 assert(!ret);
718 destroy_pid_tracker_node(tracker_node);
719 }
720 rcu_read_unlock();
721 ht_cleanup_push(pid_tracker->ht);
722 pid_tracker->ht = NULL;
723}
724
725static
726struct ust_pid_tracker_node *pid_tracker_lookup(
727 struct ust_pid_tracker *pid_tracker, int pid,
728 struct lttng_ht_iter *iter)
729{
730 unsigned long _pid = (unsigned long) pid;
731 struct lttng_ht_node_ulong *node;
732
733 lttng_ht_lookup(pid_tracker->ht, (void *) _pid, iter);
734 node = lttng_ht_iter_get_node_ulong(iter);
735 if (node) {
736 return caa_container_of(node, struct ust_pid_tracker_node,
737 node);
738 } else {
739 return NULL;
740 }
741}
742
743static
744int pid_tracker_add_pid(struct ust_pid_tracker *pid_tracker, int pid)
745{
746 int retval = LTTNG_OK;
747 struct ust_pid_tracker_node *tracker_node;
748 struct lttng_ht_iter iter;
749
750 if (pid < 0) {
751 retval = LTTNG_ERR_INVALID;
752 goto end;
753 }
754 tracker_node = pid_tracker_lookup(pid_tracker, pid, &iter);
755 if (tracker_node) {
756 /* Already exists. */
b93a4a13 757 retval = LTTNG_ERR_PID_TRACKED;
a9ad0c8f
MD
758 goto end;
759 }
760 tracker_node = zmalloc(sizeof(*tracker_node));
761 if (!tracker_node) {
762 retval = LTTNG_ERR_NOMEM;
763 goto end;
764 }
765 lttng_ht_node_init_ulong(&tracker_node->node, (unsigned long) pid);
766 lttng_ht_add_unique_ulong(pid_tracker->ht, &tracker_node->node);
767end:
768 return retval;
769}
770
771static
772int pid_tracker_del_pid(struct ust_pid_tracker *pid_tracker, int pid)
773{
774 int retval = LTTNG_OK, ret;
775 struct ust_pid_tracker_node *tracker_node;
776 struct lttng_ht_iter iter;
777
778 if (pid < 0) {
779 retval = LTTNG_ERR_INVALID;
780 goto end;
781 }
782 tracker_node = pid_tracker_lookup(pid_tracker, pid, &iter);
783 if (!tracker_node) {
784 /* Not found */
b93a4a13 785 retval = LTTNG_ERR_PID_NOT_TRACKED;
a9ad0c8f
MD
786 goto end;
787 }
788 ret = lttng_ht_del(pid_tracker->ht, &iter);
789 assert(!ret);
790
791 destroy_pid_tracker_node(tracker_node);
792end:
793 return retval;
794}
795
796/*
797 * The session lock is held when calling this function.
798 */
799int trace_ust_pid_tracker_lookup(struct ltt_ust_session *session, int pid)
800{
801 struct lttng_ht_iter iter;
802
803 if (!session->pid_tracker.ht) {
804 return 1;
805 }
806 if (pid_tracker_lookup(&session->pid_tracker, pid, &iter)) {
807 return 1;
808 }
809 return 0;
810}
811
812/*
813 * Called with the session lock held.
814 */
815int trace_ust_track_pid(struct ltt_ust_session *session, int pid)
816{
817 int retval = LTTNG_OK;
818
819 if (pid == -1) {
820 /* Track all pids: destroy tracker if exists. */
821 if (session->pid_tracker.ht) {
822 fini_pid_tracker(&session->pid_tracker);
823 /* Ensure all apps have session. */
824 ust_app_global_update_all(session);
825 }
826 } else {
827 int ret;
828
829 if (!session->pid_tracker.ht) {
830 /* Create tracker. */
831 if (init_pid_tracker(&session->pid_tracker)) {
832 ERR("Error initializing PID tracker");
833 retval = LTTNG_ERR_NOMEM;
834 goto end;
835 }
836 ret = pid_tracker_add_pid(&session->pid_tracker, pid);
837 if (ret != LTTNG_OK) {
838 retval = ret;
839 fini_pid_tracker(&session->pid_tracker);
840 goto end;
841 }
842 /* Remove all apps from session except pid. */
843 ust_app_global_update_all(session);
844 } else {
845 struct ust_app *app;
846
847 ret = pid_tracker_add_pid(&session->pid_tracker, pid);
848 if (ret != LTTNG_OK) {
849 retval = ret;
850 goto end;
851 }
852 /* Add session to application */
853 app = ust_app_find_by_pid(pid);
854 if (app) {
855 ust_app_global_update(session, app);
856 }
857 }
858 }
859end:
860 return retval;
861}
862
863/*
864 * Called with the session lock held.
865 */
866int trace_ust_untrack_pid(struct ltt_ust_session *session, int pid)
867{
868 int retval = LTTNG_OK;
869
870 if (pid == -1) {
871 /* Create empty tracker, replace old tracker. */
872 struct ust_pid_tracker tmp_tracker;
873
874 tmp_tracker = session->pid_tracker;
875 if (init_pid_tracker(&session->pid_tracker)) {
876 ERR("Error initializing PID tracker");
877 retval = LTTNG_ERR_NOMEM;
878 /* Rollback operation. */
879 session->pid_tracker = tmp_tracker;
880 goto end;
881 }
882 fini_pid_tracker(&tmp_tracker);
883
884 /* Remove session from all applications */
885 ust_app_global_update_all(session);
886 } else {
887 int ret;
888 struct ust_app *app;
889
890 if (!session->pid_tracker.ht) {
b894343d
JG
891 /* No PID being tracked. */
892 retval = LTTNG_ERR_PID_NOT_TRACKED;
a9ad0c8f
MD
893 goto end;
894 }
895 /* Remove PID from tracker */
896 ret = pid_tracker_del_pid(&session->pid_tracker, pid);
897 if (ret != LTTNG_OK) {
898 retval = ret;
899 goto end;
900 }
901 /* Remove session from application. */
902 app = ust_app_find_by_pid(pid);
903 if (app) {
904 ust_app_global_update(session, app);
905 }
906 }
907end:
908 return retval;
909}
910
a5dfbb9d
MD
911/*
912 * Called with session lock held.
913 */
914ssize_t trace_ust_list_tracker_pids(struct ltt_ust_session *session,
915 int32_t **_pids)
916{
917 struct ust_pid_tracker_node *tracker_node;
918 struct lttng_ht_iter iter;
919 unsigned long count, i = 0;
920 long approx[2];
921 int32_t *pids;
922 int ret = 0;
923
924 if (!session->pid_tracker.ht) {
925 /* Tracker disabled. Set first entry to -1. */
926 pids = zmalloc(sizeof(*pids));
927 if (!pids) {
928 ret = -1;
929 goto end;
930 }
931 pids[0] = -1;
932 *_pids = pids;
933 return 1;
934 }
935
936 rcu_read_lock();
937 cds_lfht_count_nodes(session->pid_tracker.ht->ht,
938 &approx[0], &count, &approx[1]);
939 pids = zmalloc(sizeof(*pids) * count);
940 if (!pids) {
941 ret = -1;
942 goto end;
943 }
944 cds_lfht_for_each_entry(session->pid_tracker.ht->ht,
945 &iter.iter, tracker_node, node.node) {
946 pids[i++] = tracker_node->node.key;
947 }
948 *_pids = pids;
949 ret = count;
950end:
951 rcu_read_unlock();
952 return ret;
953}
954
f6a9efaa
DG
955/*
956 * RCU safe free context structure.
957 */
958static void destroy_context_rcu(struct rcu_head *head)
959{
bec39940
DG
960 struct lttng_ht_node_ulong *node =
961 caa_container_of(head, struct lttng_ht_node_ulong, head);
f6a9efaa
DG
962 struct ltt_ust_context *ctx =
963 caa_container_of(node, struct ltt_ust_context, node);
964
bdf64013 965 trace_ust_destroy_context(ctx);
f6a9efaa
DG
966}
967
968/*
969 * Cleanup UST context hash table.
970 */
7bd39047 971static void destroy_contexts(struct lttng_ht *ht)
f6a9efaa
DG
972{
973 int ret;
bec39940
DG
974 struct lttng_ht_node_ulong *node;
975 struct lttng_ht_iter iter;
31746f93 976 struct ltt_ust_context *ctx;
f6a9efaa 977
0525e9ae
DG
978 assert(ht);
979
36b588ed 980 rcu_read_lock();
bec39940 981 cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) {
31746f93
DG
982 /* Remove from ordered list. */
983 ctx = caa_container_of(node, struct ltt_ust_context, node);
984 cds_list_del(&ctx->list);
985 /* Remove from channel's hash table. */
bec39940 986 ret = lttng_ht_del(ht, &iter);
f6a9efaa
DG
987 if (!ret) {
988 call_rcu(&node->head, destroy_context_rcu);
989 }
f6a9efaa 990 }
36b588ed 991 rcu_read_unlock();
ed52805d 992
0b2dc8df 993 ht_cleanup_push(ht);
f6a9efaa
DG
994}
995
97ee3a89
DG
996/*
997 * Cleanup ust event structure.
998 */
999void trace_ust_destroy_event(struct ltt_ust_event *event)
1000{
0525e9ae
DG
1001 assert(event);
1002
f6a9efaa 1003 DBG2("Trace destroy UST event %s", event->attr.name);
6b453b5e 1004 free(event->filter_expression);
53a80697 1005 free(event->filter);
40024f8a 1006 free(event->exclusion);
97ee3a89
DG
1007 free(event);
1008}
1009
bdf64013
JG
1010/*
1011 * Cleanup ust context structure.
1012 */
1013void trace_ust_destroy_context(struct ltt_ust_context *ctx)
1014{
1015 assert(ctx);
1016
1017 if (ctx->ctx.ctx == LTTNG_UST_CONTEXT_APP_CONTEXT) {
1018 free(ctx->ctx.u.app_ctx.provider_name);
1019 free(ctx->ctx.u.app_ctx.ctx_name);
1020 }
1021 free(ctx);
1022}
1023
f6a9efaa
DG
1024/*
1025 * URCU intermediate call to complete destroy event.
1026 */
1027static void destroy_event_rcu(struct rcu_head *head)
1028{
bec39940
DG
1029 struct lttng_ht_node_str *node =
1030 caa_container_of(head, struct lttng_ht_node_str, head);
f6a9efaa
DG
1031 struct ltt_ust_event *event =
1032 caa_container_of(node, struct ltt_ust_event, node);
1033
1034 trace_ust_destroy_event(event);
1035}
1036
284d8f55
DG
1037/*
1038 * Cleanup UST events hashtable.
1039 */
7bd39047 1040static void destroy_events(struct lttng_ht *events)
ed52805d
DG
1041{
1042 int ret;
bec39940
DG
1043 struct lttng_ht_node_str *node;
1044 struct lttng_ht_iter iter;
ed52805d 1045
0525e9ae
DG
1046 assert(events);
1047
36b588ed 1048 rcu_read_lock();
bec39940
DG
1049 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
1050 ret = lttng_ht_del(events, &iter);
7bd39047
DG
1051 assert(!ret);
1052 call_rcu(&node->head, destroy_event_rcu);
ed52805d 1053 }
36b588ed 1054 rcu_read_unlock();
ed52805d 1055
0b2dc8df 1056 ht_cleanup_push(events);
ed52805d
DG
1057}
1058
97ee3a89
DG
1059/*
1060 * Cleanup ust channel structure.
36b588ed
MD
1061 *
1062 * Should _NOT_ be called with RCU read lock held.
97ee3a89 1063 */
36b588ed 1064static void _trace_ust_destroy_channel(struct ltt_ust_channel *channel)
97ee3a89 1065{
0525e9ae
DG
1066 assert(channel);
1067
f6a9efaa 1068 DBG2("Trace destroy UST channel %s", channel->name);
97ee3a89 1069
97ee3a89 1070 free(channel);
f6a9efaa
DG
1071}
1072
1073/*
1074 * URCU intermediate call to complete destroy channel.
1075 */
1076static void destroy_channel_rcu(struct rcu_head *head)
1077{
bec39940
DG
1078 struct lttng_ht_node_str *node =
1079 caa_container_of(head, struct lttng_ht_node_str, head);
f6a9efaa
DG
1080 struct ltt_ust_channel *channel =
1081 caa_container_of(node, struct ltt_ust_channel, node);
1082
36b588ed
MD
1083 _trace_ust_destroy_channel(channel);
1084}
1085
1086void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
1087{
627cbbe7
MD
1088 /* Destroying all events of the channel */
1089 destroy_events(channel->events);
1090 /* Destroying all context of the channel */
1091 destroy_contexts(channel->ctx);
1092
36b588ed 1093 call_rcu(&channel->node.head, destroy_channel_rcu);
97ee3a89
DG
1094}
1095
d5979e4a
DG
1096/*
1097 * Remove an UST channel from a channel HT.
1098 */
1099void trace_ust_delete_channel(struct lttng_ht *ht,
1100 struct ltt_ust_channel *channel)
1101{
1102 int ret;
1103 struct lttng_ht_iter iter;
1104
1105 assert(ht);
1106 assert(channel);
1107
1108 iter.iter.node = &channel->node.node;
1109 ret = lttng_ht_del(ht, &iter);
1110 assert(!ret);
1111}
1112
97ee3a89 1113/*
f6a9efaa 1114 * Iterate over a hash table containing channels and cleanup safely.
97ee3a89 1115 */
bec39940 1116static void destroy_channels(struct lttng_ht *channels)
f6a9efaa 1117{
bec39940
DG
1118 struct lttng_ht_node_str *node;
1119 struct lttng_ht_iter iter;
f6a9efaa 1120
0525e9ae
DG
1121 assert(channels);
1122
24d1723f 1123 rcu_read_lock();
bec39940 1124 cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) {
627cbbe7
MD
1125 struct ltt_ust_channel *chan =
1126 caa_container_of(node, struct ltt_ust_channel, node);
1127
1128 trace_ust_delete_channel(channels, chan);
1129 trace_ust_destroy_channel(chan);
f6a9efaa 1130 }
36b588ed 1131 rcu_read_unlock();
ed52805d 1132
0b2dc8df 1133 ht_cleanup_push(channels);
f6a9efaa
DG
1134}
1135
f6a9efaa
DG
1136/*
1137 * Cleanup UST global domain.
1138 */
1139static void destroy_domain_global(struct ltt_ust_domain_global *dom)
1140{
0525e9ae
DG
1141 assert(dom);
1142
f6a9efaa
DG
1143 destroy_channels(dom->channels);
1144}
97ee3a89 1145
f6a9efaa
DG
1146/*
1147 * Cleanup ust session structure
36b588ed
MD
1148 *
1149 * Should *NOT* be called with RCU read-side lock held.
f6a9efaa
DG
1150 */
1151void trace_ust_destroy_session(struct ltt_ust_session *session)
1152{
fefd409b 1153 struct agent *agt;
7972aab2 1154 struct buffer_reg_uid *reg, *sreg;
fefd409b 1155 struct lttng_ht_iter iter;
7972aab2 1156
0525e9ae 1157 assert(session);
97ee3a89 1158
d9bf3ca4 1159 DBG2("Trace UST destroy session %" PRIu64, session->id);
f6a9efaa 1160
f6a9efaa
DG
1161 /* Cleaning up UST domain */
1162 destroy_domain_global(&session->domain_global);
fefd409b 1163
0c0fcd77 1164 rcu_read_lock();
fefd409b 1165 cds_lfht_for_each_entry(session->agents->ht, &iter.iter, agt, node.node) {
d0edf546
JG
1166 int ret = lttng_ht_del(session->agents, &iter);
1167
1168 assert(!ret);
fefd409b
DG
1169 agent_destroy(agt);
1170 }
0c0fcd77 1171 rcu_read_unlock();
7972aab2 1172
8ada2175
MD
1173 ht_cleanup_push(session->agents);
1174
7972aab2
DG
1175 /* Cleanup UID buffer registry object(s). */
1176 cds_list_for_each_entry_safe(reg, sreg, &session->buffer_reg_uid_list,
1177 lnode) {
1178 cds_list_del(&reg->lnode);
1179 buffer_reg_uid_remove(reg);
1180 buffer_reg_uid_destroy(reg, session->consumer);
1181 }
44d3bd01 1182
6addfa37 1183 consumer_output_put(session->consumer);
3f8e211f 1184
a9ad0c8f
MD
1185 fini_pid_tracker(&session->pid_tracker);
1186
97ee3a89
DG
1187 free(session);
1188}
This page took 0.106315 seconds and 4 git commands to generate.