| 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 |
| 5 | * it under the terms of the GNU General Public License, version 2 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 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. |
| 12 | * |
| 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. |
| 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 <common/common.h> |
| 25 | #include <common/defaults.h> |
| 26 | |
| 27 | #include "trace-ust.h" |
| 28 | |
| 29 | /* |
| 30 | * Find the channel in the hashtable. |
| 31 | */ |
| 32 | struct ltt_ust_channel *trace_ust_find_channel_by_name(struct lttng_ht *ht, |
| 33 | char *name) |
| 34 | { |
| 35 | struct lttng_ht_node_str *node; |
| 36 | struct lttng_ht_iter iter; |
| 37 | |
| 38 | rcu_read_lock(); |
| 39 | lttng_ht_lookup(ht, (void *)name, &iter); |
| 40 | node = lttng_ht_iter_get_node_str(&iter); |
| 41 | if (node == NULL) { |
| 42 | rcu_read_unlock(); |
| 43 | goto error; |
| 44 | } |
| 45 | rcu_read_unlock(); |
| 46 | |
| 47 | DBG2("Trace UST channel %s found by name", name); |
| 48 | |
| 49 | return caa_container_of(node, struct ltt_ust_channel, node); |
| 50 | |
| 51 | error: |
| 52 | DBG2("Trace UST channel %s not found by name", name); |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Find the event in the hashtable. |
| 58 | */ |
| 59 | struct ltt_ust_event *trace_ust_find_event_by_name(struct lttng_ht *ht, |
| 60 | char *name) |
| 61 | { |
| 62 | struct lttng_ht_node_str *node; |
| 63 | struct lttng_ht_iter iter; |
| 64 | |
| 65 | rcu_read_lock(); |
| 66 | lttng_ht_lookup(ht, (void *) name, &iter); |
| 67 | node = lttng_ht_iter_get_node_str(&iter); |
| 68 | if (node == NULL) { |
| 69 | rcu_read_unlock(); |
| 70 | goto error; |
| 71 | } |
| 72 | rcu_read_unlock(); |
| 73 | |
| 74 | DBG2("Trace UST event found by name %s", name); |
| 75 | |
| 76 | return caa_container_of(node, struct ltt_ust_event, node); |
| 77 | |
| 78 | error: |
| 79 | DBG2("Trace UST event NOT found by name %s", name); |
| 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Allocate and initialize a ust session data structure. |
| 85 | * |
| 86 | * Return pointer to structure or NULL. |
| 87 | */ |
| 88 | struct ltt_ust_session *trace_ust_create_session(char *path, int session_id, |
| 89 | struct lttng_domain *domain) |
| 90 | { |
| 91 | int ret; |
| 92 | struct ltt_ust_session *lus; |
| 93 | |
| 94 | /* Allocate a new ltt ust session */ |
| 95 | lus = zmalloc(sizeof(struct ltt_ust_session)); |
| 96 | if (lus == NULL) { |
| 97 | PERROR("create ust session zmalloc"); |
| 98 | goto error; |
| 99 | } |
| 100 | |
| 101 | /* Init data structure */ |
| 102 | lus->id = session_id; |
| 103 | lus->start_trace = 0; |
| 104 | |
| 105 | /* Alloc UST domain hash tables */ |
| 106 | lus->domain_pid = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 107 | lus->domain_exec = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 108 | |
| 109 | /* Alloc UST global domain channels' HT */ |
| 110 | lus->domain_global.channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 111 | |
| 112 | /* Set session path */ |
| 113 | ret = snprintf(lus->pathname, PATH_MAX, "%s/ust", path); |
| 114 | if (ret < 0) { |
| 115 | PERROR("snprintf kernel traces path"); |
| 116 | goto error_free_session; |
| 117 | } |
| 118 | |
| 119 | DBG2("UST trace session create successful"); |
| 120 | |
| 121 | return lus; |
| 122 | |
| 123 | error_free_session: |
| 124 | free(lus); |
| 125 | error: |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Allocate and initialize a ust channel data structure. |
| 131 | * |
| 132 | * Return pointer to structure or NULL. |
| 133 | */ |
| 134 | struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan, |
| 135 | char *path) |
| 136 | { |
| 137 | int ret; |
| 138 | struct ltt_ust_channel *luc; |
| 139 | |
| 140 | luc = zmalloc(sizeof(struct ltt_ust_channel)); |
| 141 | if (luc == NULL) { |
| 142 | PERROR("ltt_ust_channel zmalloc"); |
| 143 | goto error; |
| 144 | } |
| 145 | |
| 146 | /* Copy UST channel attributes */ |
| 147 | luc->attr.overwrite = chan->attr.overwrite; |
| 148 | luc->attr.subbuf_size = chan->attr.subbuf_size; |
| 149 | luc->attr.num_subbuf = chan->attr.num_subbuf; |
| 150 | luc->attr.switch_timer_interval = chan->attr.switch_timer_interval; |
| 151 | luc->attr.read_timer_interval = chan->attr.read_timer_interval; |
| 152 | luc->attr.output = chan->attr.output; |
| 153 | |
| 154 | /* Translate to UST output enum */ |
| 155 | switch (luc->attr.output) { |
| 156 | default: |
| 157 | luc->attr.output = LTTNG_UST_MMAP; |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | /* Copy channel name */ |
| 162 | strncpy(luc->name, chan->name, sizeof(luc->name)); |
| 163 | luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 164 | |
| 165 | /* Init node */ |
| 166 | lttng_ht_node_init_str(&luc->node, luc->name); |
| 167 | /* Alloc hash tables */ |
| 168 | luc->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 169 | luc->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 170 | |
| 171 | /* Set trace output path */ |
| 172 | ret = snprintf(luc->pathname, PATH_MAX, "%s", path); |
| 173 | if (ret < 0) { |
| 174 | PERROR("asprintf ust create channel"); |
| 175 | goto error_free_channel; |
| 176 | } |
| 177 | |
| 178 | DBG2("Trace UST channel %s created", luc->name); |
| 179 | |
| 180 | return luc; |
| 181 | |
| 182 | error_free_channel: |
| 183 | free(luc); |
| 184 | error: |
| 185 | return NULL; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Allocate and initialize a ust event. Set name and event type. |
| 190 | * |
| 191 | * Return pointer to structure or NULL. |
| 192 | */ |
| 193 | struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev) |
| 194 | { |
| 195 | struct ltt_ust_event *lue; |
| 196 | |
| 197 | lue = zmalloc(sizeof(struct ltt_ust_event)); |
| 198 | if (lue == NULL) { |
| 199 | PERROR("ust event zmalloc"); |
| 200 | goto error; |
| 201 | } |
| 202 | |
| 203 | switch (ev->type) { |
| 204 | case LTTNG_EVENT_PROBE: |
| 205 | lue->attr.instrumentation = LTTNG_UST_PROBE; |
| 206 | break; |
| 207 | case LTTNG_EVENT_FUNCTION: |
| 208 | lue->attr.instrumentation = LTTNG_UST_FUNCTION; |
| 209 | break; |
| 210 | case LTTNG_EVENT_FUNCTION_ENTRY: |
| 211 | lue->attr.instrumentation = LTTNG_UST_FUNCTION; |
| 212 | break; |
| 213 | case LTTNG_EVENT_TRACEPOINT: |
| 214 | lue->attr.instrumentation = LTTNG_UST_TRACEPOINT; |
| 215 | break; |
| 216 | default: |
| 217 | ERR("Unknown ust instrumentation type (%d)", ev->type); |
| 218 | goto error_free_event; |
| 219 | } |
| 220 | |
| 221 | /* Copy event name */ |
| 222 | strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN); |
| 223 | lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 224 | |
| 225 | switch (ev->loglevel_type) { |
| 226 | case LTTNG_EVENT_LOGLEVEL_ALL: |
| 227 | lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_ALL; |
| 228 | lue->attr.loglevel = -1; /* Force to -1 */ |
| 229 | break; |
| 230 | case LTTNG_EVENT_LOGLEVEL_RANGE: |
| 231 | lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_RANGE; |
| 232 | lue->attr.loglevel = ev->loglevel; |
| 233 | break; |
| 234 | case LTTNG_EVENT_LOGLEVEL_SINGLE: |
| 235 | lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE; |
| 236 | lue->attr.loglevel = ev->loglevel; |
| 237 | break; |
| 238 | default: |
| 239 | ERR("Unknown ust loglevel type (%d)", ev->loglevel_type); |
| 240 | goto error_free_event; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /* Init node */ |
| 245 | lttng_ht_node_init_str(&lue->node, lue->attr.name); |
| 246 | /* Alloc context hash tables */ |
| 247 | lue->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 248 | |
| 249 | DBG2("Trace UST event %s, loglevel (%d,%d) created", |
| 250 | lue->attr.name, lue->attr.loglevel_type, |
| 251 | lue->attr.loglevel); |
| 252 | |
| 253 | return lue; |
| 254 | |
| 255 | error_free_event: |
| 256 | free(lue); |
| 257 | error: |
| 258 | return NULL; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Allocate and initialize a ust metadata. |
| 263 | * |
| 264 | * Return pointer to structure or NULL. |
| 265 | */ |
| 266 | struct ltt_ust_metadata *trace_ust_create_metadata(char *path) |
| 267 | { |
| 268 | int ret; |
| 269 | struct ltt_ust_metadata *lum; |
| 270 | |
| 271 | lum = zmalloc(sizeof(struct ltt_ust_metadata)); |
| 272 | if (lum == NULL) { |
| 273 | PERROR("ust metadata zmalloc"); |
| 274 | goto error; |
| 275 | } |
| 276 | |
| 277 | /* Set default attributes */ |
| 278 | lum->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE; |
| 279 | lum->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE; |
| 280 | lum->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM; |
| 281 | lum->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; |
| 282 | lum->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; |
| 283 | lum->attr.output = LTTNG_UST_MMAP; |
| 284 | |
| 285 | lum->handle = -1; |
| 286 | /* Set metadata trace path */ |
| 287 | ret = snprintf(lum->pathname, PATH_MAX, "%s/metadata", path); |
| 288 | if (ret < 0) { |
| 289 | PERROR("asprintf ust metadata"); |
| 290 | goto error_free_metadata; |
| 291 | } |
| 292 | |
| 293 | return lum; |
| 294 | |
| 295 | error_free_metadata: |
| 296 | free(lum); |
| 297 | error: |
| 298 | return NULL; |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * Allocate and initialize an UST context. |
| 303 | * |
| 304 | * Return pointer to structure or NULL. |
| 305 | */ |
| 306 | struct ltt_ust_context *trace_ust_create_context( |
| 307 | struct lttng_event_context *ctx) |
| 308 | { |
| 309 | struct ltt_ust_context *uctx; |
| 310 | enum lttng_ust_context_type utype; |
| 311 | |
| 312 | switch (ctx->ctx) { |
| 313 | case LTTNG_EVENT_CONTEXT_VTID: |
| 314 | utype = LTTNG_UST_CONTEXT_VTID; |
| 315 | break; |
| 316 | case LTTNG_EVENT_CONTEXT_VPID: |
| 317 | utype = LTTNG_UST_CONTEXT_VPID; |
| 318 | break; |
| 319 | case LTTNG_EVENT_CONTEXT_PTHREAD_ID: |
| 320 | utype = LTTNG_UST_CONTEXT_PTHREAD_ID; |
| 321 | break; |
| 322 | case LTTNG_EVENT_CONTEXT_PROCNAME: |
| 323 | utype = LTTNG_UST_CONTEXT_PROCNAME; |
| 324 | break; |
| 325 | default: |
| 326 | ERR("Invalid UST context"); |
| 327 | return NULL; |
| 328 | } |
| 329 | |
| 330 | uctx = zmalloc(sizeof(struct ltt_ust_context)); |
| 331 | if (uctx == NULL) { |
| 332 | PERROR("zmalloc ltt_ust_context"); |
| 333 | goto error; |
| 334 | } |
| 335 | |
| 336 | uctx->ctx.ctx = utype; |
| 337 | lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx); |
| 338 | |
| 339 | return uctx; |
| 340 | |
| 341 | error: |
| 342 | return NULL; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * RCU safe free context structure. |
| 347 | */ |
| 348 | static void destroy_context_rcu(struct rcu_head *head) |
| 349 | { |
| 350 | struct lttng_ht_node_ulong *node = |
| 351 | caa_container_of(head, struct lttng_ht_node_ulong, head); |
| 352 | struct ltt_ust_context *ctx = |
| 353 | caa_container_of(node, struct ltt_ust_context, node); |
| 354 | |
| 355 | free(ctx); |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Cleanup UST context hash table. |
| 360 | */ |
| 361 | static void destroy_contexts(struct lttng_ht *ht) |
| 362 | { |
| 363 | int ret; |
| 364 | struct lttng_ht_node_ulong *node; |
| 365 | struct lttng_ht_iter iter; |
| 366 | |
| 367 | cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) { |
| 368 | ret = lttng_ht_del(ht, &iter); |
| 369 | if (!ret) { |
| 370 | call_rcu(&node->head, destroy_context_rcu); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | lttng_ht_destroy(ht); |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Cleanup ust event structure. |
| 379 | */ |
| 380 | void trace_ust_destroy_event(struct ltt_ust_event *event) |
| 381 | { |
| 382 | DBG2("Trace destroy UST event %s", event->attr.name); |
| 383 | destroy_contexts(event->ctx); |
| 384 | |
| 385 | free(event); |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * URCU intermediate call to complete destroy event. |
| 390 | */ |
| 391 | static void destroy_event_rcu(struct rcu_head *head) |
| 392 | { |
| 393 | struct lttng_ht_node_str *node = |
| 394 | caa_container_of(head, struct lttng_ht_node_str, head); |
| 395 | struct ltt_ust_event *event = |
| 396 | caa_container_of(node, struct ltt_ust_event, node); |
| 397 | |
| 398 | trace_ust_destroy_event(event); |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Cleanup UST events hashtable. |
| 403 | */ |
| 404 | static void destroy_events(struct lttng_ht *events) |
| 405 | { |
| 406 | int ret; |
| 407 | struct lttng_ht_node_str *node; |
| 408 | struct lttng_ht_iter iter; |
| 409 | |
| 410 | cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) { |
| 411 | ret = lttng_ht_del(events, &iter); |
| 412 | assert(!ret); |
| 413 | call_rcu(&node->head, destroy_event_rcu); |
| 414 | } |
| 415 | |
| 416 | lttng_ht_destroy(events); |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Cleanup ust channel structure. |
| 421 | */ |
| 422 | void trace_ust_destroy_channel(struct ltt_ust_channel *channel) |
| 423 | { |
| 424 | DBG2("Trace destroy UST channel %s", channel->name); |
| 425 | |
| 426 | rcu_read_lock(); |
| 427 | |
| 428 | /* Destroying all events of the channel */ |
| 429 | destroy_events(channel->events); |
| 430 | /* Destroying all context of the channel */ |
| 431 | destroy_contexts(channel->ctx); |
| 432 | |
| 433 | free(channel); |
| 434 | |
| 435 | rcu_read_unlock(); |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * URCU intermediate call to complete destroy channel. |
| 440 | */ |
| 441 | static void destroy_channel_rcu(struct rcu_head *head) |
| 442 | { |
| 443 | struct lttng_ht_node_str *node = |
| 444 | caa_container_of(head, struct lttng_ht_node_str, head); |
| 445 | struct ltt_ust_channel *channel = |
| 446 | caa_container_of(node, struct ltt_ust_channel, node); |
| 447 | |
| 448 | trace_ust_destroy_channel(channel); |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * Cleanup ust metadata structure. |
| 453 | */ |
| 454 | void trace_ust_destroy_metadata(struct ltt_ust_metadata *metadata) |
| 455 | { |
| 456 | DBG2("Trace UST destroy metadata %d", metadata->handle); |
| 457 | |
| 458 | free(metadata); |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * Iterate over a hash table containing channels and cleanup safely. |
| 463 | */ |
| 464 | static void destroy_channels(struct lttng_ht *channels) |
| 465 | { |
| 466 | int ret; |
| 467 | struct lttng_ht_node_str *node; |
| 468 | struct lttng_ht_iter iter; |
| 469 | |
| 470 | rcu_read_lock(); |
| 471 | |
| 472 | cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) { |
| 473 | ret = lttng_ht_del(channels, &iter); |
| 474 | assert(!ret); |
| 475 | call_rcu(&node->head, destroy_channel_rcu); |
| 476 | } |
| 477 | |
| 478 | lttng_ht_destroy(channels); |
| 479 | |
| 480 | rcu_read_unlock(); |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * Cleanup UST pid domain. |
| 485 | */ |
| 486 | static void destroy_domain_pid(struct lttng_ht *ht) |
| 487 | { |
| 488 | int ret; |
| 489 | struct lttng_ht_iter iter; |
| 490 | struct ltt_ust_domain_pid *dpid; |
| 491 | |
| 492 | cds_lfht_for_each_entry(ht->ht, &iter.iter, dpid, node.node) { |
| 493 | ret = lttng_ht_del(ht , &iter); |
| 494 | assert(!ret); |
| 495 | destroy_channels(dpid->channels); |
| 496 | } |
| 497 | |
| 498 | lttng_ht_destroy(ht); |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * Cleanup UST exec name domain. |
| 503 | */ |
| 504 | static void destroy_domain_exec(struct lttng_ht *ht) |
| 505 | { |
| 506 | int ret; |
| 507 | struct lttng_ht_iter iter; |
| 508 | struct ltt_ust_domain_exec *dexec; |
| 509 | |
| 510 | cds_lfht_for_each_entry(ht->ht, &iter.iter, dexec, node.node) { |
| 511 | ret = lttng_ht_del(ht , &iter); |
| 512 | assert(!ret); |
| 513 | destroy_channels(dexec->channels); |
| 514 | } |
| 515 | |
| 516 | lttng_ht_destroy(ht); |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * Cleanup UST global domain. |
| 521 | */ |
| 522 | static void destroy_domain_global(struct ltt_ust_domain_global *dom) |
| 523 | { |
| 524 | destroy_channels(dom->channels); |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | * Cleanup ust session structure |
| 529 | */ |
| 530 | void trace_ust_destroy_session(struct ltt_ust_session *session) |
| 531 | { |
| 532 | /* Extra protection */ |
| 533 | if (session == NULL) { |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | rcu_read_lock(); |
| 538 | |
| 539 | DBG2("Trace UST destroy session %d", session->id); |
| 540 | |
| 541 | /* Cleaning up UST domain */ |
| 542 | destroy_domain_global(&session->domain_global); |
| 543 | destroy_domain_pid(session->domain_pid); |
| 544 | destroy_domain_exec(session->domain_exec); |
| 545 | |
| 546 | free(session); |
| 547 | |
| 548 | rcu_read_unlock(); |
| 549 | } |