Fix incorrect POPT_ARG_STRING usage
[lttng-tools.git] / lttng-sessiond / trace-ust.c
CommitLineData
97ee3a89
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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 as published by the Free
6 * Software Foundation; only version 2 of the License.
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., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307, USA.
16 */
17
18#define _GNU_SOURCE
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23
24#include <lttngerr.h>
25#include <lttng-share.h>
26
f6a9efaa 27#include "hashtable.h"
97ee3a89
DG
28#include "trace-ust.h"
29
0177d773 30/*
f6a9efaa 31 * Find the channel in the hashtable.
0177d773 32 */
f6a9efaa
DG
33struct ltt_ust_channel *trace_ust_find_channel_by_name(struct cds_lfht *ht,
34 char *name)
0177d773 35{
f6a9efaa
DG
36 struct cds_lfht_node *node;
37 struct cds_lfht_iter iter;
0177d773 38
f6a9efaa
DG
39 rcu_read_lock();
40 node = hashtable_lookup(ht, (void *) name, strlen(name), &iter);
41 if (node == NULL) {
42 rcu_read_unlock();
44d3bd01
DG
43 goto error;
44 }
f6a9efaa 45 rcu_read_unlock();
44d3bd01 46
f6a9efaa 47 DBG2("Trace UST channel %s found by name", name);
0177d773 48
f6a9efaa 49 return caa_container_of(node, struct ltt_ust_channel, node);
97ee3a89
DG
50
51error:
f6a9efaa 52 DBG2("Trace UST channel %s not found by name", name);
97ee3a89
DG
53 return NULL;
54}
55
56/*
f6a9efaa 57 * Find the event in the hashtable.
97ee3a89 58 */
f6a9efaa
DG
59struct ltt_ust_event *trace_ust_find_event_by_name(struct cds_lfht *ht,
60 char *name)
97ee3a89 61{
f6a9efaa
DG
62 struct cds_lfht_node *node;
63 struct cds_lfht_iter iter;
97ee3a89 64
f6a9efaa
DG
65 rcu_read_lock();
66 node = hashtable_lookup(ht, (void *) name, strlen(name), &iter);
67 if (node == NULL) {
68 rcu_read_unlock();
97ee3a89
DG
69 goto error;
70 }
f6a9efaa 71 rcu_read_unlock();
97ee3a89 72
f6a9efaa
DG
73 DBG2("Found UST event by name %s", name);
74
75 return caa_container_of(node, struct ltt_ust_event, node);
97ee3a89
DG
76
77error:
78 return NULL;
79}
80
81/*
82 * Allocate and initialize a ust session data structure.
83 *
84 * Return pointer to structure or NULL.
85 */
48842b30 86struct ltt_ust_session *trace_ust_create_session(char *path, unsigned int uid,
44d3bd01 87 struct lttng_domain *domain)
97ee3a89 88{
0177d773 89 int ret;
97ee3a89
DG
90 struct ltt_ust_session *lus;
91
92 /* Allocate a new ltt ust session */
93 lus = malloc(sizeof(struct ltt_ust_session));
94 if (lus == NULL) {
36dc12cc 95 PERROR("create ust session malloc");
97ee3a89
DG
96 goto error;
97 }
98
99 /* Init data structure */
3bd1e081 100 lus->consumer_fds_sent = 0;
48842b30 101 lus->uid = uid;
36dc12cc 102 lus->start_trace = 0;
97ee3a89 103
f6a9efaa
DG
104 /* Alloc UST domain hash tables */
105 lus->domain_pid = hashtable_new(0);
106 lus->domain_exec = hashtable_new_str(0);
107
108 /* Alloc UST global domain channels' HT */
109 lus->domain_global.channels = hashtable_new_str(0);
44d3bd01 110
0177d773 111 /* Set session path */
48842b30 112 ret = snprintf(lus->pathname, PATH_MAX, "%s/ust", path);
0177d773 113 if (ret < 0) {
44d3bd01 114 PERROR("snprintf kernel traces path");
44844c29 115 goto error_free_session;
0177d773
DG
116 }
117
44d3bd01
DG
118 DBG2("UST trace session create successful");
119
97ee3a89
DG
120 return lus;
121
44844c29
MD
122error_free_session:
123 free(lus);
97ee3a89
DG
124error:
125 return NULL;
126}
127
128/*
129 * Allocate and initialize a ust channel data structure.
130 *
131 * Return pointer to structure or NULL.
132 */
44d3bd01
DG
133struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
134 char *path)
97ee3a89
DG
135{
136 int ret;
137 struct ltt_ust_channel *luc;
138
139 luc = malloc(sizeof(struct ltt_ust_channel));
140 if (luc == NULL) {
141 perror("ltt_ust_channel malloc");
142 goto error;
143 }
144
44d3bd01 145 /* Copy UST channel attributes */
48842b30
DG
146 luc->attr.overwrite = chan->attr.overwrite;
147 luc->attr.subbuf_size = chan->attr.subbuf_size;
148 luc->attr.num_subbuf = chan->attr.num_subbuf;
149 luc->attr.switch_timer_interval = chan->attr.switch_timer_interval;
150 luc->attr.read_timer_interval = chan->attr.read_timer_interval;
151 luc->attr.output = chan->attr.output;
44d3bd01
DG
152
153 /* Translate to UST output enum */
154 switch (luc->attr.output) {
155 default:
156 luc->attr.output = LTTNG_UST_MMAP;
157 break;
97ee3a89 158 }
97ee3a89 159
97ee3a89 160 /* Copy channel name */
44d3bd01 161 strncpy(luc->name, chan->name, sizeof(&luc->name));
97ee3a89
DG
162 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
163
f6a9efaa
DG
164 /* Init node */
165 hashtable_node_init(&luc->node, (void *) luc->name, strlen(luc->name));
166 /* Alloc hash tables */
167 luc->events = hashtable_new_str(0);
168 luc->ctx = hashtable_new_str(0);
169
97ee3a89 170 /* Set trace output path */
48842b30 171 ret = snprintf(luc->pathname, PATH_MAX, "%s", path);
97ee3a89
DG
172 if (ret < 0) {
173 perror("asprintf ust create channel");
44844c29 174 goto error_free_channel;
97ee3a89
DG
175 }
176
f6a9efaa
DG
177 DBG2("Trace UST channel %s created", luc->name);
178
97ee3a89
DG
179 return luc;
180
44844c29
MD
181error_free_channel:
182 free(luc);
97ee3a89
DG
183error:
184 return NULL;
185}
186
187/*
188 * Allocate and initialize a ust event. Set name and event type.
189 *
190 * Return pointer to structure or NULL.
191 */
192struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev)
193{
194 struct ltt_ust_event *lue;
97ee3a89
DG
195
196 lue = malloc(sizeof(struct ltt_ust_event));
44d3bd01
DG
197 if (lue == NULL) {
198 PERROR("ust event malloc");
97ee3a89
DG
199 goto error;
200 }
201
202 switch (ev->type) {
203 case LTTNG_EVENT_PROBE:
44d3bd01 204 lue->attr.instrumentation = LTTNG_UST_PROBE;
97ee3a89
DG
205 break;
206 case LTTNG_EVENT_FUNCTION:
44d3bd01 207 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
208 break;
209 case LTTNG_EVENT_FUNCTION_ENTRY:
44d3bd01 210 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
211 break;
212 case LTTNG_EVENT_TRACEPOINT:
44d3bd01 213 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT;
97ee3a89
DG
214 break;
215 default:
216 ERR("Unknown ust instrumentation type (%d)", ev->type);
44844c29 217 goto error_free_event;
97ee3a89
DG
218 }
219
220 /* Copy event name */
44d3bd01
DG
221 strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
222 lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
97ee3a89 223
f6a9efaa
DG
224 /* Init node */
225 hashtable_node_init(&lue->node, (void *) lue->attr.name,
226 strlen(lue->attr.name));
227 /* Alloc context hash tables */
e03e3332 228 lue->ctx = hashtable_new_str(0);
97ee3a89
DG
229
230 return lue;
231
44844c29
MD
232error_free_event:
233 free(lue);
97ee3a89
DG
234error:
235 return NULL;
236}
237
238/*
239 * Allocate and initialize a ust metadata.
240 *
241 * Return pointer to structure or NULL.
242 */
243struct ltt_ust_metadata *trace_ust_create_metadata(char *path)
244{
245 int ret;
246 struct ltt_ust_metadata *lum;
97ee3a89
DG
247
248 lum = malloc(sizeof(struct ltt_ust_metadata));
44d3bd01 249 if (lum == NULL) {
97ee3a89
DG
250 perror("ust metadata malloc");
251 goto error;
252 }
253
254 /* Set default attributes */
44d3bd01
DG
255 lum->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
256 lum->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE;
257 lum->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
258 lum->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
259 lum->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
d41f73b7 260 lum->attr.output = LTTNG_UST_MMAP;
44d3bd01 261
97ee3a89
DG
262 lum->handle = -1;
263 /* Set metadata trace path */
f6a9efaa 264 ret = snprintf(lum->pathname, PATH_MAX, "%s/metadata", path);
97ee3a89
DG
265 if (ret < 0) {
266 perror("asprintf ust metadata");
44844c29 267 goto error_free_metadata;
97ee3a89
DG
268 }
269
270 return lum;
271
44844c29
MD
272error_free_metadata:
273 free(lum);
97ee3a89
DG
274error:
275 return NULL;
276}
277
f6a9efaa
DG
278/*
279 * RCU safe free context structure.
280 */
281static void destroy_context_rcu(struct rcu_head *head)
282{
283 struct cds_lfht_node *node =
284 caa_container_of(head, struct cds_lfht_node, head);
285 struct ltt_ust_context *ctx =
286 caa_container_of(node, struct ltt_ust_context, node);
287
288 free(ctx);
289}
290
291/*
292 * Cleanup UST context hash table.
293 */
294static void destroy_context(struct cds_lfht *ht)
295{
296 int ret;
297 struct cds_lfht_node *node;
298 struct cds_lfht_iter iter;
299
300 hashtable_get_first(ht, &iter);
301 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
302 ret = hashtable_del(ht, &iter);
303 if (!ret) {
304 call_rcu(&node->head, destroy_context_rcu);
305 }
306 hashtable_get_next(ht, &iter);
307 }
308}
309
97ee3a89
DG
310/*
311 * Cleanup ust event structure.
312 */
313void trace_ust_destroy_event(struct ltt_ust_event *event)
314{
f6a9efaa
DG
315 DBG2("Trace destroy UST event %s", event->attr.name);
316 destroy_context(event->ctx);
97ee3a89
DG
317 free(event);
318}
319
f6a9efaa
DG
320/*
321 * URCU intermediate call to complete destroy event.
322 */
323static void destroy_event_rcu(struct rcu_head *head)
324{
325 struct cds_lfht_node *node =
326 caa_container_of(head, struct cds_lfht_node, head);
327 struct ltt_ust_event *event =
328 caa_container_of(node, struct ltt_ust_event, node);
329
330 trace_ust_destroy_event(event);
331}
332
97ee3a89
DG
333/*
334 * Cleanup ust channel structure.
335 */
336void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
337{
f6a9efaa
DG
338 int ret;
339 struct cds_lfht_node *node;
340 struct cds_lfht_iter iter;
97ee3a89 341
f6a9efaa 342 DBG2("Trace destroy UST channel %s", channel->name);
97ee3a89 343
f6a9efaa
DG
344 rcu_read_lock();
345
346 hashtable_get_first(channel->events, &iter);
347 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
348 ret = hashtable_del(channel->events, &iter);
349 if (!ret) {
350 call_rcu(&node->head, destroy_event_rcu);
351 }
352 hashtable_get_next(channel->events, &iter);
97ee3a89
DG
353 }
354
f6a9efaa
DG
355 free(channel->events);
356 destroy_context(channel->ctx);
97ee3a89 357 free(channel);
f6a9efaa
DG
358
359 rcu_read_unlock();
360}
361
362/*
363 * URCU intermediate call to complete destroy channel.
364 */
365static void destroy_channel_rcu(struct rcu_head *head)
366{
367 struct cds_lfht_node *node =
368 caa_container_of(head, struct cds_lfht_node, head);
369 struct ltt_ust_channel *channel =
370 caa_container_of(node, struct ltt_ust_channel, node);
371
372 trace_ust_destroy_channel(channel);
97ee3a89
DG
373}
374
375/*
376 * Cleanup ust metadata structure.
377 */
378void trace_ust_destroy_metadata(struct ltt_ust_metadata *metadata)
379{
f6a9efaa 380 DBG2("Trace UST destroy metadata %d", metadata->handle);
97ee3a89 381
97ee3a89
DG
382 free(metadata);
383}
384
385/*
f6a9efaa 386 * Iterate over a hash table containing channels and cleanup safely.
97ee3a89 387 */
f6a9efaa
DG
388static void destroy_channels(struct cds_lfht *channels)
389{
390 int ret;
391 struct cds_lfht_node *node;
392 struct cds_lfht_iter iter;
393
394 hashtable_get_first(channels, &iter);
395 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
396 ret = hashtable_del(channels, &iter);
397 if (!ret) {
398 call_rcu(&node->head, destroy_channel_rcu);
399 }
400 hashtable_get_next(channels, &iter);
401 }
402}
403
404/*
405 * Cleanup UST pid domain.
406 */
407static void destroy_domain_pid(struct cds_lfht *ht)
408{
409 int ret;
410 struct cds_lfht_node *node;
411 struct cds_lfht_iter iter;
412 struct ltt_ust_domain_pid *d;
413
414 hashtable_get_first(ht, &iter);
415 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
416 ret = hashtable_del(ht , &iter);
417 if (!ret) {
418 d = caa_container_of(node, struct ltt_ust_domain_pid, node);
419 destroy_channels(d->channels);
420 }
421 hashtable_get_next(ht, &iter);
422 }
423}
424
425/*
426 * Cleanup UST exec name domain.
427 */
428static void destroy_domain_exec(struct cds_lfht *ht)
97ee3a89 429{
f6a9efaa
DG
430 int ret;
431 struct cds_lfht_node *node;
432 struct cds_lfht_iter iter;
433 struct ltt_ust_domain_exec *d;
434
435 hashtable_get_first(ht, &iter);
436 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
437 ret = hashtable_del(ht , &iter);
438 if (!ret) {
439 d = caa_container_of(node, struct ltt_ust_domain_exec, node);
440 destroy_channels(d->channels);
441 }
442 hashtable_get_next(ht, &iter);
443 }
444}
97ee3a89 445
f6a9efaa
DG
446/*
447 * Cleanup UST global domain.
448 */
449static void destroy_domain_global(struct ltt_ust_domain_global *dom)
450{
451 destroy_channels(dom->channels);
452}
97ee3a89 453
f6a9efaa
DG
454/*
455 * Cleanup ust session structure
456 */
457void trace_ust_destroy_session(struct ltt_ust_session *session)
458{
97ee3a89
DG
459 /* Extra safety */
460 if (session == NULL) {
461 return;
462 }
463
f6a9efaa
DG
464 rcu_read_lock();
465
48842b30 466 DBG2("Trace UST destroy session %d", session->uid);
f6a9efaa 467
48842b30
DG
468 //if (session->metadata != NULL) {
469 // trace_ust_destroy_metadata(session->metadata);
470 //}
97ee3a89 471
f6a9efaa
DG
472 /* Cleaning up UST domain */
473 destroy_domain_global(&session->domain_global);
474 destroy_domain_pid(session->domain_pid);
475 destroy_domain_exec(session->domain_exec);
44d3bd01 476
97ee3a89 477 free(session);
f6a9efaa
DG
478
479 rcu_read_unlock();
97ee3a89 480}
This page took 0.047974 seconds and 4 git commands to generate.