Commit | Line | Data |
---|---|---|
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 | ||
daf282ab | 27 | #include "../common/hashtable.h" |
97ee3a89 DG |
28 | #include "trace-ust.h" |
29 | ||
0177d773 | 30 | /* |
f6a9efaa | 31 | * Find the channel in the hashtable. |
0177d773 | 32 | */ |
f6a9efaa DG |
33 | struct 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 | |
51 | error: | |
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 |
59 | struct 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 | 65 | rcu_read_lock(); |
7f79d3a1 | 66 | node = hashtable_lookup(ht, (void *)name, strlen(name), &iter); |
f6a9efaa DG |
67 | if (node == NULL) { |
68 | rcu_read_unlock(); | |
97ee3a89 DG |
69 | goto error; |
70 | } | |
f6a9efaa | 71 | rcu_read_unlock(); |
97ee3a89 | 72 | |
284d8f55 | 73 | DBG2("Trace UST event found by name %s", name); |
f6a9efaa DG |
74 | |
75 | return caa_container_of(node, struct ltt_ust_event, node); | |
97ee3a89 DG |
76 | |
77 | error: | |
284d8f55 | 78 | DBG2("Trace UST event NOT found by name %s", name); |
97ee3a89 DG |
79 | return NULL; |
80 | } | |
81 | ||
82 | /* | |
83 | * Allocate and initialize a ust session data structure. | |
84 | * | |
85 | * Return pointer to structure or NULL. | |
86 | */ | |
a991f516 | 87 | struct ltt_ust_session *trace_ust_create_session(char *path, int session_id, |
44d3bd01 | 88 | struct lttng_domain *domain) |
97ee3a89 | 89 | { |
0177d773 | 90 | int ret; |
97ee3a89 DG |
91 | struct ltt_ust_session *lus; |
92 | ||
93 | /* Allocate a new ltt ust session */ | |
ba7f0ae5 | 94 | lus = zmalloc(sizeof(struct ltt_ust_session)); |
97ee3a89 | 95 | if (lus == NULL) { |
ba7f0ae5 | 96 | PERROR("create ust session zmalloc"); |
97ee3a89 DG |
97 | goto error; |
98 | } | |
99 | ||
100 | /* Init data structure */ | |
a991f516 | 101 | lus->id = session_id; |
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 |
122 | error_free_session: |
123 | free(lus); | |
97ee3a89 DG |
124 | error: |
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 |
133 | struct 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 | ||
37357452 | 139 | luc = zmalloc(sizeof(struct ltt_ust_channel)); |
97ee3a89 | 140 | if (luc == NULL) { |
ba7f0ae5 | 141 | perror("ltt_ust_channel zmalloc"); |
97ee3a89 DG |
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 */ |
0a1459bb | 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); | |
55cc08a6 | 168 | luc->ctx = hashtable_new(0); |
f6a9efaa | 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 |
181 | error_free_channel: |
182 | free(luc); | |
97ee3a89 DG |
183 | error: |
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 | */ | |
192 | struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev) | |
193 | { | |
194 | struct ltt_ust_event *lue; | |
97ee3a89 | 195 | |
37357452 | 196 | lue = zmalloc(sizeof(struct ltt_ust_event)); |
44d3bd01 | 197 | if (lue == NULL) { |
ba7f0ae5 | 198 | PERROR("ust event zmalloc"); |
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 | 214 | break; |
13dce3b7 MD |
215 | case LTTNG_EVENT_TRACEPOINT_LOGLEVEL: |
216 | lue->attr.instrumentation = LTTNG_UST_TRACEPOINT_LOGLEVEL; | |
217 | break; | |
97ee3a89 DG |
218 | default: |
219 | ERR("Unknown ust instrumentation type (%d)", ev->type); | |
44844c29 | 220 | goto error_free_event; |
97ee3a89 DG |
221 | } |
222 | ||
223 | /* Copy event name */ | |
44d3bd01 DG |
224 | strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN); |
225 | lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
97ee3a89 | 226 | |
f6a9efaa DG |
227 | /* Init node */ |
228 | hashtable_node_init(&lue->node, (void *) lue->attr.name, | |
229 | strlen(lue->attr.name)); | |
230 | /* Alloc context hash tables */ | |
55cc08a6 | 231 | lue->ctx = hashtable_new(0); |
97ee3a89 | 232 | |
284d8f55 DG |
233 | DBG2("Trace UST event %s created", lue->attr.name); |
234 | ||
97ee3a89 DG |
235 | return lue; |
236 | ||
44844c29 MD |
237 | error_free_event: |
238 | free(lue); | |
97ee3a89 DG |
239 | error: |
240 | return NULL; | |
241 | } | |
242 | ||
243 | /* | |
244 | * Allocate and initialize a ust metadata. | |
245 | * | |
246 | * Return pointer to structure or NULL. | |
247 | */ | |
248 | struct ltt_ust_metadata *trace_ust_create_metadata(char *path) | |
249 | { | |
250 | int ret; | |
251 | struct ltt_ust_metadata *lum; | |
97ee3a89 | 252 | |
3a009adb | 253 | lum = zmalloc(sizeof(struct ltt_ust_metadata)); |
44d3bd01 | 254 | if (lum == NULL) { |
ba7f0ae5 | 255 | perror("ust metadata zmalloc"); |
97ee3a89 DG |
256 | goto error; |
257 | } | |
258 | ||
259 | /* Set default attributes */ | |
44d3bd01 DG |
260 | lum->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE; |
261 | lum->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE; | |
262 | lum->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM; | |
263 | lum->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; | |
264 | lum->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; | |
d41f73b7 | 265 | lum->attr.output = LTTNG_UST_MMAP; |
44d3bd01 | 266 | |
97ee3a89 DG |
267 | lum->handle = -1; |
268 | /* Set metadata trace path */ | |
f6a9efaa | 269 | ret = snprintf(lum->pathname, PATH_MAX, "%s/metadata", path); |
97ee3a89 DG |
270 | if (ret < 0) { |
271 | perror("asprintf ust metadata"); | |
44844c29 | 272 | goto error_free_metadata; |
97ee3a89 DG |
273 | } |
274 | ||
275 | return lum; | |
276 | ||
44844c29 MD |
277 | error_free_metadata: |
278 | free(lum); | |
97ee3a89 DG |
279 | error: |
280 | return NULL; | |
281 | } | |
282 | ||
55cc08a6 DG |
283 | /* |
284 | * Allocate and initialize an UST context. | |
285 | * | |
286 | * Return pointer to structure or NULL. | |
287 | */ | |
288 | struct ltt_ust_context *trace_ust_create_context( | |
289 | struct lttng_event_context *ctx) | |
290 | { | |
291 | struct ltt_ust_context *uctx; | |
292 | ||
293 | uctx = zmalloc(sizeof(struct ltt_ust_context)); | |
294 | if (uctx == NULL) { | |
295 | PERROR("zmalloc ltt_ust_context"); | |
296 | goto error; | |
297 | } | |
298 | ||
299 | uctx->ctx.ctx = ctx->ctx; | |
300 | hashtable_node_init(&uctx->node, (void *)((unsigned long) uctx->ctx.ctx), | |
301 | sizeof(void *)); | |
302 | ||
303 | return uctx; | |
304 | ||
305 | error: | |
306 | return NULL; | |
307 | } | |
308 | ||
f6a9efaa DG |
309 | /* |
310 | * RCU safe free context structure. | |
311 | */ | |
312 | static void destroy_context_rcu(struct rcu_head *head) | |
313 | { | |
314 | struct cds_lfht_node *node = | |
315 | caa_container_of(head, struct cds_lfht_node, head); | |
316 | struct ltt_ust_context *ctx = | |
317 | caa_container_of(node, struct ltt_ust_context, node); | |
318 | ||
319 | free(ctx); | |
320 | } | |
321 | ||
322 | /* | |
323 | * Cleanup UST context hash table. | |
324 | */ | |
325 | static void destroy_context(struct cds_lfht *ht) | |
326 | { | |
327 | int ret; | |
328 | struct cds_lfht_node *node; | |
329 | struct cds_lfht_iter iter; | |
330 | ||
ed52805d | 331 | cds_lfht_for_each(ht, &iter, node) { |
f6a9efaa DG |
332 | ret = hashtable_del(ht, &iter); |
333 | if (!ret) { | |
334 | call_rcu(&node->head, destroy_context_rcu); | |
335 | } | |
f6a9efaa | 336 | } |
ed52805d DG |
337 | |
338 | hashtable_destroy(ht); | |
f6a9efaa DG |
339 | } |
340 | ||
97ee3a89 DG |
341 | /* |
342 | * Cleanup ust event structure. | |
343 | */ | |
344 | void trace_ust_destroy_event(struct ltt_ust_event *event) | |
345 | { | |
f6a9efaa DG |
346 | DBG2("Trace destroy UST event %s", event->attr.name); |
347 | destroy_context(event->ctx); | |
ed52805d | 348 | |
97ee3a89 DG |
349 | free(event); |
350 | } | |
351 | ||
f6a9efaa DG |
352 | /* |
353 | * URCU intermediate call to complete destroy event. | |
354 | */ | |
355 | static void destroy_event_rcu(struct rcu_head *head) | |
356 | { | |
357 | struct cds_lfht_node *node = | |
358 | caa_container_of(head, struct cds_lfht_node, head); | |
359 | struct ltt_ust_event *event = | |
360 | caa_container_of(node, struct ltt_ust_event, node); | |
361 | ||
362 | trace_ust_destroy_event(event); | |
363 | } | |
364 | ||
284d8f55 DG |
365 | /* |
366 | * Cleanup UST events hashtable. | |
367 | */ | |
368 | static void destroy_event(struct cds_lfht *events) | |
ed52805d DG |
369 | { |
370 | int ret; | |
371 | struct cds_lfht_node *node; | |
372 | struct cds_lfht_iter iter; | |
373 | ||
284d8f55 DG |
374 | cds_lfht_for_each(events, &iter, node) { |
375 | ret = hashtable_del(events, &iter); | |
ed52805d DG |
376 | if (!ret) { |
377 | call_rcu(&node->head, destroy_event_rcu); | |
378 | } | |
379 | } | |
380 | ||
284d8f55 | 381 | hashtable_destroy(events); |
ed52805d DG |
382 | } |
383 | ||
97ee3a89 DG |
384 | /* |
385 | * Cleanup ust channel structure. | |
386 | */ | |
387 | void trace_ust_destroy_channel(struct ltt_ust_channel *channel) | |
388 | { | |
f6a9efaa DG |
389 | int ret; |
390 | struct cds_lfht_node *node; | |
391 | struct cds_lfht_iter iter; | |
97ee3a89 | 392 | |
f6a9efaa | 393 | DBG2("Trace destroy UST channel %s", channel->name); |
97ee3a89 | 394 | |
f6a9efaa DG |
395 | rcu_read_lock(); |
396 | ||
ed52805d | 397 | cds_lfht_for_each(channel->events, &iter, node) { |
f6a9efaa DG |
398 | ret = hashtable_del(channel->events, &iter); |
399 | if (!ret) { | |
ed52805d | 400 | destroy_event(channel->events); |
f6a9efaa | 401 | } |
97ee3a89 DG |
402 | } |
403 | ||
f6a9efaa | 404 | destroy_context(channel->ctx); |
97ee3a89 | 405 | free(channel); |
f6a9efaa DG |
406 | |
407 | rcu_read_unlock(); | |
408 | } | |
409 | ||
410 | /* | |
411 | * URCU intermediate call to complete destroy channel. | |
412 | */ | |
413 | static void destroy_channel_rcu(struct rcu_head *head) | |
414 | { | |
415 | struct cds_lfht_node *node = | |
416 | caa_container_of(head, struct cds_lfht_node, head); | |
417 | struct ltt_ust_channel *channel = | |
418 | caa_container_of(node, struct ltt_ust_channel, node); | |
419 | ||
420 | trace_ust_destroy_channel(channel); | |
97ee3a89 DG |
421 | } |
422 | ||
423 | /* | |
424 | * Cleanup ust metadata structure. | |
425 | */ | |
426 | void trace_ust_destroy_metadata(struct ltt_ust_metadata *metadata) | |
427 | { | |
f6a9efaa | 428 | DBG2("Trace UST destroy metadata %d", metadata->handle); |
97ee3a89 | 429 | |
97ee3a89 DG |
430 | free(metadata); |
431 | } | |
432 | ||
433 | /* | |
f6a9efaa | 434 | * Iterate over a hash table containing channels and cleanup safely. |
97ee3a89 | 435 | */ |
f6a9efaa DG |
436 | static void destroy_channels(struct cds_lfht *channels) |
437 | { | |
438 | int ret; | |
439 | struct cds_lfht_node *node; | |
440 | struct cds_lfht_iter iter; | |
441 | ||
ed52805d | 442 | cds_lfht_for_each(channels, &iter, node) { |
f6a9efaa DG |
443 | ret = hashtable_del(channels, &iter); |
444 | if (!ret) { | |
445 | call_rcu(&node->head, destroy_channel_rcu); | |
446 | } | |
f6a9efaa | 447 | } |
ed52805d DG |
448 | |
449 | hashtable_destroy(channels); | |
f6a9efaa DG |
450 | } |
451 | ||
452 | /* | |
453 | * Cleanup UST pid domain. | |
454 | */ | |
455 | static void destroy_domain_pid(struct cds_lfht *ht) | |
456 | { | |
457 | int ret; | |
f6a9efaa | 458 | struct cds_lfht_iter iter; |
ed52805d | 459 | struct ltt_ust_domain_pid *dpid; |
f6a9efaa | 460 | |
ed52805d | 461 | cds_lfht_for_each_entry(ht, &iter, dpid, node) { |
f6a9efaa DG |
462 | ret = hashtable_del(ht , &iter); |
463 | if (!ret) { | |
ed52805d | 464 | destroy_channels(dpid->channels); |
f6a9efaa | 465 | } |
f6a9efaa | 466 | } |
ed52805d DG |
467 | |
468 | hashtable_destroy(ht); | |
f6a9efaa DG |
469 | } |
470 | ||
471 | /* | |
472 | * Cleanup UST exec name domain. | |
473 | */ | |
474 | static void destroy_domain_exec(struct cds_lfht *ht) | |
97ee3a89 | 475 | { |
f6a9efaa | 476 | int ret; |
f6a9efaa | 477 | struct cds_lfht_iter iter; |
ed52805d | 478 | struct ltt_ust_domain_exec *dexec; |
f6a9efaa | 479 | |
ed52805d | 480 | cds_lfht_for_each_entry(ht, &iter, dexec, node) { |
f6a9efaa DG |
481 | ret = hashtable_del(ht , &iter); |
482 | if (!ret) { | |
ed52805d | 483 | destroy_channels(dexec->channels); |
f6a9efaa | 484 | } |
f6a9efaa | 485 | } |
ed52805d DG |
486 | |
487 | hashtable_destroy(ht); | |
f6a9efaa | 488 | } |
97ee3a89 | 489 | |
f6a9efaa DG |
490 | /* |
491 | * Cleanup UST global domain. | |
492 | */ | |
493 | static void destroy_domain_global(struct ltt_ust_domain_global *dom) | |
494 | { | |
495 | destroy_channels(dom->channels); | |
496 | } | |
97ee3a89 | 497 | |
f6a9efaa DG |
498 | /* |
499 | * Cleanup ust session structure | |
500 | */ | |
501 | void trace_ust_destroy_session(struct ltt_ust_session *session) | |
502 | { | |
ed52805d | 503 | /* Extra protection */ |
97ee3a89 DG |
504 | if (session == NULL) { |
505 | return; | |
506 | } | |
507 | ||
f6a9efaa DG |
508 | rcu_read_lock(); |
509 | ||
a991f516 | 510 | DBG2("Trace UST destroy session %d", session->id); |
f6a9efaa | 511 | |
f6a9efaa DG |
512 | /* Cleaning up UST domain */ |
513 | destroy_domain_global(&session->domain_global); | |
514 | destroy_domain_pid(session->domain_pid); | |
515 | destroy_domain_exec(session->domain_exec); | |
44d3bd01 | 516 | |
97ee3a89 | 517 | free(session); |
f6a9efaa DG |
518 | |
519 | rcu_read_unlock(); | |
97ee3a89 | 520 | } |