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