| 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 | #define _LGPL_SOURCE |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <common/common.h> |
| 26 | #include <common/defaults.h> |
| 27 | |
| 28 | #include "consumer.h" |
| 29 | #include "trace-kernel.h" |
| 30 | |
| 31 | /* |
| 32 | * Find the channel name for the given kernel session. |
| 33 | */ |
| 34 | struct ltt_kernel_channel *trace_kernel_get_channel_by_name( |
| 35 | char *name, struct ltt_kernel_session *session) |
| 36 | { |
| 37 | struct ltt_kernel_channel *chan; |
| 38 | |
| 39 | assert(session); |
| 40 | assert(name); |
| 41 | |
| 42 | /* |
| 43 | * If we receive an empty string for channel name, it means the |
| 44 | * default channel name is requested. |
| 45 | */ |
| 46 | if (name[0] == '\0') |
| 47 | name = DEFAULT_CHANNEL_NAME; |
| 48 | |
| 49 | DBG("Trying to find channel %s", name); |
| 50 | |
| 51 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { |
| 52 | if (strcmp(name, chan->channel->name) == 0) { |
| 53 | DBG("Found channel by name %s", name); |
| 54 | return chan; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Find the event for the given channel. |
| 63 | */ |
| 64 | struct ltt_kernel_event *trace_kernel_find_event( |
| 65 | char *name, struct ltt_kernel_channel *channel, |
| 66 | enum lttng_event_type type, |
| 67 | struct lttng_filter_bytecode *filter) |
| 68 | { |
| 69 | struct ltt_kernel_event *ev; |
| 70 | int found = 0; |
| 71 | |
| 72 | assert(name); |
| 73 | assert(channel); |
| 74 | |
| 75 | cds_list_for_each_entry(ev, &channel->events_list.head, list) { |
| 76 | if (type != LTTNG_EVENT_ALL && ev->type != type) { |
| 77 | continue; |
| 78 | } |
| 79 | if (strcmp(name, ev->event->name)) { |
| 80 | continue; |
| 81 | } |
| 82 | if ((ev->filter && !filter) || (!ev->filter && filter)) { |
| 83 | continue; |
| 84 | } |
| 85 | if (ev->filter && filter) { |
| 86 | if (ev->filter->len != filter->len || |
| 87 | memcmp(ev->filter->data, filter->data, |
| 88 | filter->len) != 0) { |
| 89 | continue; |
| 90 | } |
| 91 | } |
| 92 | found = 1; |
| 93 | break; |
| 94 | } |
| 95 | if (found) { |
| 96 | DBG("Found event %s for channel %s", name, |
| 97 | channel->channel->name); |
| 98 | return ev; |
| 99 | } else { |
| 100 | return NULL; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Find the event name for the given channel. |
| 106 | */ |
| 107 | struct ltt_kernel_event *trace_kernel_get_event_by_name( |
| 108 | char *name, struct ltt_kernel_channel *channel, |
| 109 | enum lttng_event_type type) |
| 110 | { |
| 111 | struct ltt_kernel_event *ev; |
| 112 | int found = 0; |
| 113 | |
| 114 | assert(name); |
| 115 | assert(channel); |
| 116 | |
| 117 | cds_list_for_each_entry(ev, &channel->events_list.head, list) { |
| 118 | if (type != LTTNG_EVENT_ALL && ev->type != type) { |
| 119 | continue; |
| 120 | } |
| 121 | if (strcmp(name, ev->event->name)) { |
| 122 | continue; |
| 123 | } |
| 124 | found = 1; |
| 125 | break; |
| 126 | } |
| 127 | if (found) { |
| 128 | DBG("Found event %s for channel %s", name, |
| 129 | channel->channel->name); |
| 130 | return ev; |
| 131 | } else { |
| 132 | return NULL; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Allocate and initialize a kernel session data structure. |
| 138 | * |
| 139 | * Return pointer to structure or NULL. |
| 140 | */ |
| 141 | struct ltt_kernel_session *trace_kernel_create_session(void) |
| 142 | { |
| 143 | struct ltt_kernel_session *lks = NULL; |
| 144 | |
| 145 | /* Allocate a new ltt kernel session */ |
| 146 | lks = zmalloc(sizeof(struct ltt_kernel_session)); |
| 147 | if (lks == NULL) { |
| 148 | PERROR("create kernel session zmalloc"); |
| 149 | goto alloc_error; |
| 150 | } |
| 151 | |
| 152 | /* Init data structure */ |
| 153 | lks->fd = -1; |
| 154 | lks->metadata_stream_fd = -1; |
| 155 | lks->channel_count = 0; |
| 156 | lks->stream_count_global = 0; |
| 157 | lks->metadata = NULL; |
| 158 | CDS_INIT_LIST_HEAD(&lks->channel_list.head); |
| 159 | |
| 160 | lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL); |
| 161 | if (lks->consumer == NULL) { |
| 162 | goto error; |
| 163 | } |
| 164 | |
| 165 | return lks; |
| 166 | |
| 167 | error: |
| 168 | free(lks); |
| 169 | |
| 170 | alloc_error: |
| 171 | return NULL; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Allocate and initialize a kernel channel data structure. |
| 176 | * |
| 177 | * Return pointer to structure or NULL. |
| 178 | */ |
| 179 | struct ltt_kernel_channel *trace_kernel_create_channel( |
| 180 | struct lttng_channel *chan) |
| 181 | { |
| 182 | struct ltt_kernel_channel *lkc; |
| 183 | |
| 184 | assert(chan); |
| 185 | |
| 186 | lkc = zmalloc(sizeof(struct ltt_kernel_channel)); |
| 187 | if (lkc == NULL) { |
| 188 | PERROR("ltt_kernel_channel zmalloc"); |
| 189 | goto error; |
| 190 | } |
| 191 | |
| 192 | lkc->channel = zmalloc(sizeof(struct lttng_channel)); |
| 193 | if (lkc->channel == NULL) { |
| 194 | PERROR("lttng_channel zmalloc"); |
| 195 | free(lkc); |
| 196 | goto error; |
| 197 | } |
| 198 | memcpy(lkc->channel, chan, sizeof(struct lttng_channel)); |
| 199 | |
| 200 | /* |
| 201 | * If we receive an empty string for channel name, it means the |
| 202 | * default channel name is requested. |
| 203 | */ |
| 204 | if (chan->name[0] == '\0') { |
| 205 | strncpy(lkc->channel->name, DEFAULT_CHANNEL_NAME, |
| 206 | sizeof(lkc->channel->name)); |
| 207 | } |
| 208 | lkc->channel->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 209 | |
| 210 | lkc->fd = -1; |
| 211 | lkc->stream_count = 0; |
| 212 | lkc->event_count = 0; |
| 213 | lkc->enabled = 1; |
| 214 | /* Init linked list */ |
| 215 | CDS_INIT_LIST_HEAD(&lkc->events_list.head); |
| 216 | CDS_INIT_LIST_HEAD(&lkc->stream_list.head); |
| 217 | CDS_INIT_LIST_HEAD(&lkc->ctx_list); |
| 218 | |
| 219 | return lkc; |
| 220 | |
| 221 | error: |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * Allocate and init a kernel context object. |
| 227 | * |
| 228 | * Return the allocated object or NULL on error. |
| 229 | */ |
| 230 | struct ltt_kernel_context *trace_kernel_create_context( |
| 231 | struct lttng_kernel_context *ctx) |
| 232 | { |
| 233 | struct ltt_kernel_context *kctx; |
| 234 | |
| 235 | kctx = zmalloc(sizeof(*kctx)); |
| 236 | if (!kctx) { |
| 237 | PERROR("zmalloc kernel context"); |
| 238 | goto error; |
| 239 | } |
| 240 | |
| 241 | if (ctx) { |
| 242 | memcpy(&kctx->ctx, ctx, sizeof(kctx->ctx)); |
| 243 | } |
| 244 | |
| 245 | CDS_INIT_LIST_HEAD(&kctx->list); |
| 246 | |
| 247 | error: |
| 248 | return kctx; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * Allocate and initialize a kernel event. Set name and event type. |
| 253 | * We own filter_expression, and filter. |
| 254 | * |
| 255 | * Return pointer to structure or NULL. |
| 256 | */ |
| 257 | struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev, |
| 258 | char *filter_expression, struct lttng_filter_bytecode *filter) |
| 259 | { |
| 260 | struct ltt_kernel_event *lke; |
| 261 | struct lttng_kernel_event *attr; |
| 262 | |
| 263 | assert(ev); |
| 264 | |
| 265 | lke = zmalloc(sizeof(struct ltt_kernel_event)); |
| 266 | attr = zmalloc(sizeof(struct lttng_kernel_event)); |
| 267 | if (lke == NULL || attr == NULL) { |
| 268 | PERROR("kernel event zmalloc"); |
| 269 | goto error; |
| 270 | } |
| 271 | |
| 272 | switch (ev->type) { |
| 273 | case LTTNG_EVENT_PROBE: |
| 274 | attr->instrumentation = LTTNG_KERNEL_KPROBE; |
| 275 | attr->u.kprobe.addr = ev->attr.probe.addr; |
| 276 | attr->u.kprobe.offset = ev->attr.probe.offset; |
| 277 | strncpy(attr->u.kprobe.symbol_name, |
| 278 | ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN); |
| 279 | attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 280 | break; |
| 281 | case LTTNG_EVENT_FUNCTION: |
| 282 | attr->instrumentation = LTTNG_KERNEL_KRETPROBE; |
| 283 | attr->u.kretprobe.addr = ev->attr.probe.addr; |
| 284 | attr->u.kretprobe.offset = ev->attr.probe.offset; |
| 285 | strncpy(attr->u.kretprobe.symbol_name, |
| 286 | ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN); |
| 287 | attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 288 | break; |
| 289 | case LTTNG_EVENT_FUNCTION_ENTRY: |
| 290 | attr->instrumentation = LTTNG_KERNEL_FUNCTION; |
| 291 | strncpy(attr->u.ftrace.symbol_name, |
| 292 | ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN); |
| 293 | attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 294 | break; |
| 295 | case LTTNG_EVENT_TRACEPOINT: |
| 296 | attr->instrumentation = LTTNG_KERNEL_TRACEPOINT; |
| 297 | break; |
| 298 | case LTTNG_EVENT_SYSCALL: |
| 299 | attr->instrumentation = LTTNG_KERNEL_SYSCALL; |
| 300 | break; |
| 301 | case LTTNG_EVENT_ALL: |
| 302 | attr->instrumentation = LTTNG_KERNEL_ALL; |
| 303 | break; |
| 304 | default: |
| 305 | ERR("Unknown kernel instrumentation type (%d)", ev->type); |
| 306 | goto error; |
| 307 | } |
| 308 | |
| 309 | /* Copy event name */ |
| 310 | strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN); |
| 311 | attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 312 | |
| 313 | /* Setting up a kernel event */ |
| 314 | lke->fd = -1; |
| 315 | lke->event = attr; |
| 316 | lke->enabled = 1; |
| 317 | lke->filter_expression = filter_expression; |
| 318 | lke->filter = filter; |
| 319 | |
| 320 | return lke; |
| 321 | |
| 322 | error: |
| 323 | free(filter_expression); |
| 324 | free(filter); |
| 325 | free(lke); |
| 326 | free(attr); |
| 327 | return NULL; |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Allocate and initialize a kernel metadata. |
| 332 | * |
| 333 | * Return pointer to structure or NULL. |
| 334 | */ |
| 335 | struct ltt_kernel_metadata *trace_kernel_create_metadata(void) |
| 336 | { |
| 337 | struct ltt_kernel_metadata *lkm; |
| 338 | struct lttng_channel *chan; |
| 339 | |
| 340 | lkm = zmalloc(sizeof(struct ltt_kernel_metadata)); |
| 341 | chan = zmalloc(sizeof(struct lttng_channel)); |
| 342 | if (lkm == NULL || chan == NULL) { |
| 343 | PERROR("kernel metadata zmalloc"); |
| 344 | goto error; |
| 345 | } |
| 346 | |
| 347 | /* Set default attributes */ |
| 348 | chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE; |
| 349 | chan->attr.subbuf_size = default_get_metadata_subbuf_size(); |
| 350 | chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM; |
| 351 | chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER; |
| 352 | chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER; |
| 353 | chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT; |
| 354 | |
| 355 | /* Init metadata */ |
| 356 | lkm->fd = -1; |
| 357 | lkm->conf = chan; |
| 358 | |
| 359 | return lkm; |
| 360 | |
| 361 | error: |
| 362 | free(lkm); |
| 363 | free(chan); |
| 364 | return NULL; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by |
| 369 | * default. |
| 370 | * |
| 371 | * Return pointer to structure or NULL. |
| 372 | */ |
| 373 | struct ltt_kernel_stream *trace_kernel_create_stream(const char *name, |
| 374 | unsigned int count) |
| 375 | { |
| 376 | int ret; |
| 377 | struct ltt_kernel_stream *lks; |
| 378 | |
| 379 | assert(name); |
| 380 | |
| 381 | lks = zmalloc(sizeof(struct ltt_kernel_stream)); |
| 382 | if (lks == NULL) { |
| 383 | PERROR("kernel stream zmalloc"); |
| 384 | goto error; |
| 385 | } |
| 386 | |
| 387 | /* Set name */ |
| 388 | ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count); |
| 389 | if (ret < 0) { |
| 390 | PERROR("snprintf stream name"); |
| 391 | goto error; |
| 392 | } |
| 393 | lks->name[sizeof(lks->name) - 1] = '\0'; |
| 394 | |
| 395 | /* Init stream */ |
| 396 | lks->fd = -1; |
| 397 | lks->state = 0; |
| 398 | lks->cpu = count; |
| 399 | |
| 400 | return lks; |
| 401 | |
| 402 | error: |
| 403 | return NULL; |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * Cleanup kernel stream structure. |
| 408 | */ |
| 409 | void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream) |
| 410 | { |
| 411 | assert(stream); |
| 412 | |
| 413 | DBG("[trace] Closing stream fd %d", stream->fd); |
| 414 | /* Close kernel fd */ |
| 415 | if (stream->fd >= 0) { |
| 416 | int ret; |
| 417 | |
| 418 | ret = close(stream->fd); |
| 419 | if (ret) { |
| 420 | PERROR("close"); |
| 421 | } |
| 422 | } |
| 423 | /* Remove from stream list */ |
| 424 | cds_list_del(&stream->list); |
| 425 | |
| 426 | free(stream); |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | * Cleanup kernel event structure. |
| 431 | */ |
| 432 | void trace_kernel_destroy_event(struct ltt_kernel_event *event) |
| 433 | { |
| 434 | assert(event); |
| 435 | |
| 436 | if (event->fd >= 0) { |
| 437 | int ret; |
| 438 | |
| 439 | DBG("[trace] Closing event fd %d", event->fd); |
| 440 | /* Close kernel fd */ |
| 441 | ret = close(event->fd); |
| 442 | if (ret) { |
| 443 | PERROR("close"); |
| 444 | } |
| 445 | } else { |
| 446 | DBG("[trace] Tearing down event (no associated fd)"); |
| 447 | } |
| 448 | |
| 449 | /* Remove from event list */ |
| 450 | cds_list_del(&event->list); |
| 451 | |
| 452 | free(event->filter_expression); |
| 453 | free(event->filter); |
| 454 | |
| 455 | free(event->event); |
| 456 | free(event); |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * Cleanup kernel context structure. |
| 461 | */ |
| 462 | void trace_kernel_destroy_context(struct ltt_kernel_context *ctx) |
| 463 | { |
| 464 | assert(ctx); |
| 465 | |
| 466 | cds_list_del(&ctx->list); |
| 467 | free(ctx); |
| 468 | } |
| 469 | |
| 470 | /* |
| 471 | * Cleanup kernel channel structure. |
| 472 | */ |
| 473 | void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel) |
| 474 | { |
| 475 | struct ltt_kernel_stream *stream, *stmp; |
| 476 | struct ltt_kernel_event *event, *etmp; |
| 477 | struct ltt_kernel_context *ctx, *ctmp; |
| 478 | int ret; |
| 479 | |
| 480 | assert(channel); |
| 481 | |
| 482 | DBG("[trace] Closing channel fd %d", channel->fd); |
| 483 | /* Close kernel fd */ |
| 484 | if (channel->fd >= 0) { |
| 485 | ret = close(channel->fd); |
| 486 | if (ret) { |
| 487 | PERROR("close"); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | /* For each stream in the channel list */ |
| 492 | cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) { |
| 493 | trace_kernel_destroy_stream(stream); |
| 494 | } |
| 495 | |
| 496 | /* For each event in the channel list */ |
| 497 | cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) { |
| 498 | trace_kernel_destroy_event(event); |
| 499 | } |
| 500 | |
| 501 | /* For each context in the channel list */ |
| 502 | cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) { |
| 503 | trace_kernel_destroy_context(ctx); |
| 504 | } |
| 505 | |
| 506 | /* Remove from channel list */ |
| 507 | cds_list_del(&channel->list); |
| 508 | |
| 509 | free(channel->channel); |
| 510 | free(channel); |
| 511 | } |
| 512 | |
| 513 | /* |
| 514 | * Cleanup kernel metadata structure. |
| 515 | */ |
| 516 | void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata) |
| 517 | { |
| 518 | assert(metadata); |
| 519 | |
| 520 | DBG("[trace] Closing metadata fd %d", metadata->fd); |
| 521 | /* Close kernel fd */ |
| 522 | if (metadata->fd >= 0) { |
| 523 | int ret; |
| 524 | |
| 525 | ret = close(metadata->fd); |
| 526 | if (ret) { |
| 527 | PERROR("close"); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | free(metadata->conf); |
| 532 | free(metadata); |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | * Cleanup kernel session structure |
| 537 | * |
| 538 | * Should *NOT* be called with RCU read-side lock held. |
| 539 | */ |
| 540 | void trace_kernel_destroy_session(struct ltt_kernel_session *session) |
| 541 | { |
| 542 | struct ltt_kernel_channel *channel, *ctmp; |
| 543 | int ret; |
| 544 | |
| 545 | assert(session); |
| 546 | |
| 547 | DBG("[trace] Closing session fd %d", session->fd); |
| 548 | /* Close kernel fds */ |
| 549 | if (session->fd >= 0) { |
| 550 | ret = close(session->fd); |
| 551 | if (ret) { |
| 552 | PERROR("close"); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | if (session->metadata_stream_fd >= 0) { |
| 557 | DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd); |
| 558 | ret = close(session->metadata_stream_fd); |
| 559 | if (ret) { |
| 560 | PERROR("close"); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | if (session->metadata != NULL) { |
| 565 | trace_kernel_destroy_metadata(session->metadata); |
| 566 | } |
| 567 | |
| 568 | cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) { |
| 569 | trace_kernel_destroy_channel(channel); |
| 570 | } |
| 571 | |
| 572 | /* Wipe consumer output object */ |
| 573 | consumer_output_put(session->consumer); |
| 574 | |
| 575 | free(session); |
| 576 | } |