lttng UI: read opt_cmd_name opt arg for each command
[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");
0177d773
DG
115 goto error;
116 }
117
44d3bd01
DG
118 DBG2("UST trace session create successful");
119
97ee3a89
DG
120 return lus;
121
122error:
123 return NULL;
124}
125
126/*
127 * Allocate and initialize a ust channel data structure.
128 *
129 * Return pointer to structure or NULL.
130 */
44d3bd01
DG
131struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
132 char *path)
97ee3a89
DG
133{
134 int ret;
135 struct ltt_ust_channel *luc;
136
137 luc = malloc(sizeof(struct ltt_ust_channel));
138 if (luc == NULL) {
139 perror("ltt_ust_channel malloc");
140 goto error;
141 }
142
44d3bd01 143 /* Copy UST channel attributes */
48842b30
DG
144 luc->attr.overwrite = chan->attr.overwrite;
145 luc->attr.subbuf_size = chan->attr.subbuf_size;
146 luc->attr.num_subbuf = chan->attr.num_subbuf;
147 luc->attr.switch_timer_interval = chan->attr.switch_timer_interval;
148 luc->attr.read_timer_interval = chan->attr.read_timer_interval;
149 luc->attr.output = chan->attr.output;
44d3bd01
DG
150
151 /* Translate to UST output enum */
152 switch (luc->attr.output) {
153 default:
154 luc->attr.output = LTTNG_UST_MMAP;
155 break;
97ee3a89 156 }
97ee3a89 157
97ee3a89 158 /* Copy channel name */
44d3bd01 159 strncpy(luc->name, chan->name, sizeof(&luc->name));
97ee3a89
DG
160 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
161
f6a9efaa
DG
162 /* Init node */
163 hashtable_node_init(&luc->node, (void *) luc->name, strlen(luc->name));
164 /* Alloc hash tables */
165 luc->events = hashtable_new_str(0);
166 luc->ctx = hashtable_new_str(0);
167
97ee3a89 168 /* Set trace output path */
48842b30 169 ret = snprintf(luc->pathname, PATH_MAX, "%s", path);
97ee3a89
DG
170 if (ret < 0) {
171 perror("asprintf ust create channel");
172 goto error;
173 }
174
f6a9efaa
DG
175 DBG2("Trace UST channel %s created", luc->name);
176
97ee3a89
DG
177 return luc;
178
179error:
180 return NULL;
181}
182
183/*
184 * Allocate and initialize a ust event. Set name and event type.
185 *
186 * Return pointer to structure or NULL.
187 */
188struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev)
189{
190 struct ltt_ust_event *lue;
97ee3a89
DG
191
192 lue = malloc(sizeof(struct ltt_ust_event));
44d3bd01
DG
193 if (lue == NULL) {
194 PERROR("ust event malloc");
97ee3a89
DG
195 goto error;
196 }
197
198 switch (ev->type) {
199 case LTTNG_EVENT_PROBE:
44d3bd01 200 lue->attr.instrumentation = LTTNG_UST_PROBE;
97ee3a89
DG
201 break;
202 case LTTNG_EVENT_FUNCTION:
44d3bd01 203 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
204 break;
205 case LTTNG_EVENT_FUNCTION_ENTRY:
44d3bd01 206 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
207 break;
208 case LTTNG_EVENT_TRACEPOINT:
44d3bd01 209 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT;
97ee3a89
DG
210 break;
211 default:
212 ERR("Unknown ust instrumentation type (%d)", ev->type);
213 goto error;
214 }
215
216 /* Copy event name */
44d3bd01
DG
217 strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
218 lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
97ee3a89 219
f6a9efaa
DG
220 /* Init node */
221 hashtable_node_init(&lue->node, (void *) lue->attr.name,
222 strlen(lue->attr.name));
223 /* Alloc context hash tables */
e03e3332 224 lue->ctx = hashtable_new_str(0);
97ee3a89
DG
225
226 return lue;
227
228error:
229 return NULL;
230}
231
232/*
233 * Allocate and initialize a ust metadata.
234 *
235 * Return pointer to structure or NULL.
236 */
237struct ltt_ust_metadata *trace_ust_create_metadata(char *path)
238{
239 int ret;
240 struct ltt_ust_metadata *lum;
97ee3a89
DG
241
242 lum = malloc(sizeof(struct ltt_ust_metadata));
44d3bd01 243 if (lum == NULL) {
97ee3a89
DG
244 perror("ust metadata malloc");
245 goto error;
246 }
247
248 /* Set default attributes */
44d3bd01
DG
249 lum->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
250 lum->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE;
251 lum->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
252 lum->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
253 lum->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
d41f73b7 254 lum->attr.output = LTTNG_UST_MMAP;
44d3bd01 255
97ee3a89
DG
256 lum->handle = -1;
257 /* Set metadata trace path */
f6a9efaa 258 ret = snprintf(lum->pathname, PATH_MAX, "%s/metadata", path);
97ee3a89
DG
259 if (ret < 0) {
260 perror("asprintf ust metadata");
261 goto error;
262 }
263
264 return lum;
265
266error:
267 return NULL;
268}
269
f6a9efaa
DG
270/*
271 * RCU safe free context structure.
272 */
273static void destroy_context_rcu(struct rcu_head *head)
274{
275 struct cds_lfht_node *node =
276 caa_container_of(head, struct cds_lfht_node, head);
277 struct ltt_ust_context *ctx =
278 caa_container_of(node, struct ltt_ust_context, node);
279
280 free(ctx);
281}
282
283/*
284 * Cleanup UST context hash table.
285 */
286static void destroy_context(struct cds_lfht *ht)
287{
288 int ret;
289 struct cds_lfht_node *node;
290 struct cds_lfht_iter iter;
291
292 hashtable_get_first(ht, &iter);
293 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
294 ret = hashtable_del(ht, &iter);
295 if (!ret) {
296 call_rcu(&node->head, destroy_context_rcu);
297 }
298 hashtable_get_next(ht, &iter);
299 }
300}
301
97ee3a89
DG
302/*
303 * Cleanup ust event structure.
304 */
305void trace_ust_destroy_event(struct ltt_ust_event *event)
306{
f6a9efaa
DG
307 DBG2("Trace destroy UST event %s", event->attr.name);
308 destroy_context(event->ctx);
97ee3a89
DG
309 free(event);
310}
311
f6a9efaa
DG
312/*
313 * URCU intermediate call to complete destroy event.
314 */
315static void destroy_event_rcu(struct rcu_head *head)
316{
317 struct cds_lfht_node *node =
318 caa_container_of(head, struct cds_lfht_node, head);
319 struct ltt_ust_event *event =
320 caa_container_of(node, struct ltt_ust_event, node);
321
322 trace_ust_destroy_event(event);
323}
324
97ee3a89
DG
325/*
326 * Cleanup ust channel structure.
327 */
328void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
329{
f6a9efaa
DG
330 int ret;
331 struct cds_lfht_node *node;
332 struct cds_lfht_iter iter;
97ee3a89 333
f6a9efaa 334 DBG2("Trace destroy UST channel %s", channel->name);
97ee3a89 335
f6a9efaa
DG
336 rcu_read_lock();
337
338 hashtable_get_first(channel->events, &iter);
339 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
340 ret = hashtable_del(channel->events, &iter);
341 if (!ret) {
342 call_rcu(&node->head, destroy_event_rcu);
343 }
344 hashtable_get_next(channel->events, &iter);
97ee3a89
DG
345 }
346
f6a9efaa
DG
347 free(channel->events);
348 destroy_context(channel->ctx);
97ee3a89 349 free(channel);
f6a9efaa
DG
350
351 rcu_read_unlock();
352}
353
354/*
355 * URCU intermediate call to complete destroy channel.
356 */
357static void destroy_channel_rcu(struct rcu_head *head)
358{
359 struct cds_lfht_node *node =
360 caa_container_of(head, struct cds_lfht_node, head);
361 struct ltt_ust_channel *channel =
362 caa_container_of(node, struct ltt_ust_channel, node);
363
364 trace_ust_destroy_channel(channel);
97ee3a89
DG
365}
366
367/*
368 * Cleanup ust metadata structure.
369 */
370void trace_ust_destroy_metadata(struct ltt_ust_metadata *metadata)
371{
f6a9efaa 372 DBG2("Trace UST destroy metadata %d", metadata->handle);
97ee3a89 373
97ee3a89
DG
374 free(metadata);
375}
376
377/*
f6a9efaa 378 * Iterate over a hash table containing channels and cleanup safely.
97ee3a89 379 */
f6a9efaa
DG
380static void destroy_channels(struct cds_lfht *channels)
381{
382 int ret;
383 struct cds_lfht_node *node;
384 struct cds_lfht_iter iter;
385
386 hashtable_get_first(channels, &iter);
387 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
388 ret = hashtable_del(channels, &iter);
389 if (!ret) {
390 call_rcu(&node->head, destroy_channel_rcu);
391 }
392 hashtable_get_next(channels, &iter);
393 }
394}
395
396/*
397 * Cleanup UST pid domain.
398 */
399static void destroy_domain_pid(struct cds_lfht *ht)
400{
401 int ret;
402 struct cds_lfht_node *node;
403 struct cds_lfht_iter iter;
404 struct ltt_ust_domain_pid *d;
405
406 hashtable_get_first(ht, &iter);
407 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
408 ret = hashtable_del(ht , &iter);
409 if (!ret) {
410 d = caa_container_of(node, struct ltt_ust_domain_pid, node);
411 destroy_channels(d->channels);
412 }
413 hashtable_get_next(ht, &iter);
414 }
415}
416
417/*
418 * Cleanup UST exec name domain.
419 */
420static void destroy_domain_exec(struct cds_lfht *ht)
97ee3a89 421{
f6a9efaa
DG
422 int ret;
423 struct cds_lfht_node *node;
424 struct cds_lfht_iter iter;
425 struct ltt_ust_domain_exec *d;
426
427 hashtable_get_first(ht, &iter);
428 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
429 ret = hashtable_del(ht , &iter);
430 if (!ret) {
431 d = caa_container_of(node, struct ltt_ust_domain_exec, node);
432 destroy_channels(d->channels);
433 }
434 hashtable_get_next(ht, &iter);
435 }
436}
97ee3a89 437
f6a9efaa
DG
438/*
439 * Cleanup UST global domain.
440 */
441static void destroy_domain_global(struct ltt_ust_domain_global *dom)
442{
443 destroy_channels(dom->channels);
444}
97ee3a89 445
f6a9efaa
DG
446/*
447 * Cleanup ust session structure
448 */
449void trace_ust_destroy_session(struct ltt_ust_session *session)
450{
97ee3a89
DG
451 /* Extra safety */
452 if (session == NULL) {
453 return;
454 }
455
f6a9efaa
DG
456 rcu_read_lock();
457
48842b30 458 DBG2("Trace UST destroy session %d", session->uid);
f6a9efaa 459
48842b30
DG
460 //if (session->metadata != NULL) {
461 // trace_ust_destroy_metadata(session->metadata);
462 //}
97ee3a89 463
f6a9efaa
DG
464 /* Cleaning up UST domain */
465 destroy_domain_global(&session->domain_global);
466 destroy_domain_pid(session->domain_pid);
467 destroy_domain_exec(session->domain_exec);
44d3bd01 468
97ee3a89 469 free(session);
f6a9efaa
DG
470
471 rcu_read_unlock();
97ee3a89 472}
This page took 0.056243 seconds and 4 git commands to generate.