| 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 _LGPL_SOURCE |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | #include <lttng/event.h> |
| 25 | #include <lttng/lttng-error.h> |
| 26 | #include <lttng/userspace-probe.h> |
| 27 | #include <lttng/userspace-probe-internal.h> |
| 28 | |
| 29 | #include <common/common.h> |
| 30 | #include <common/defaults.h> |
| 31 | #include <common/trace-chunk.h> |
| 32 | |
| 33 | #include "consumer.h" |
| 34 | #include "trace-kernel.h" |
| 35 | #include "lttng-sessiond.h" |
| 36 | #include "notification-thread-commands.h" |
| 37 | |
| 38 | /* |
| 39 | * Find the channel name for the given kernel session. |
| 40 | */ |
| 41 | struct ltt_kernel_channel *trace_kernel_get_channel_by_name( |
| 42 | const char *name, struct ltt_kernel_session *session) |
| 43 | { |
| 44 | struct ltt_kernel_channel *chan; |
| 45 | |
| 46 | assert(session); |
| 47 | assert(name); |
| 48 | |
| 49 | /* |
| 50 | * If we receive an empty string for channel name, it means the |
| 51 | * default channel name is requested. |
| 52 | */ |
| 53 | if (name[0] == '\0') |
| 54 | name = DEFAULT_CHANNEL_NAME; |
| 55 | |
| 56 | DBG("Trying to find channel %s", name); |
| 57 | |
| 58 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { |
| 59 | if (strcmp(name, chan->channel->name) == 0) { |
| 60 | DBG("Found channel by name %s", name); |
| 61 | return chan; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Find the event for the given channel. |
| 70 | */ |
| 71 | struct ltt_kernel_event *trace_kernel_find_event( |
| 72 | char *name, struct ltt_kernel_channel *channel, |
| 73 | enum lttng_event_type type, |
| 74 | struct lttng_filter_bytecode *filter) |
| 75 | { |
| 76 | struct ltt_kernel_event *ev; |
| 77 | int found = 0; |
| 78 | |
| 79 | assert(name); |
| 80 | assert(channel); |
| 81 | |
| 82 | cds_list_for_each_entry(ev, &channel->events_list.head, list) { |
| 83 | if (type != LTTNG_EVENT_ALL && ev->type != type) { |
| 84 | continue; |
| 85 | } |
| 86 | if (strcmp(name, ev->event->name)) { |
| 87 | continue; |
| 88 | } |
| 89 | if ((ev->filter && !filter) || (!ev->filter && filter)) { |
| 90 | continue; |
| 91 | } |
| 92 | if (ev->filter && filter) { |
| 93 | if (ev->filter->len != filter->len || |
| 94 | memcmp(ev->filter->data, filter->data, |
| 95 | filter->len) != 0) { |
| 96 | continue; |
| 97 | } |
| 98 | } |
| 99 | found = 1; |
| 100 | break; |
| 101 | } |
| 102 | if (found) { |
| 103 | DBG("Found event %s for channel %s", name, |
| 104 | channel->channel->name); |
| 105 | return ev; |
| 106 | } else { |
| 107 | return NULL; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Find the event name for the given channel. |
| 113 | */ |
| 114 | struct ltt_kernel_event *trace_kernel_get_event_by_name( |
| 115 | char *name, struct ltt_kernel_channel *channel, |
| 116 | enum lttng_event_type type) |
| 117 | { |
| 118 | struct ltt_kernel_event *ev; |
| 119 | int found = 0; |
| 120 | |
| 121 | assert(name); |
| 122 | assert(channel); |
| 123 | |
| 124 | cds_list_for_each_entry(ev, &channel->events_list.head, list) { |
| 125 | if (type != LTTNG_EVENT_ALL && ev->type != type) { |
| 126 | continue; |
| 127 | } |
| 128 | if (strcmp(name, ev->event->name)) { |
| 129 | continue; |
| 130 | } |
| 131 | found = 1; |
| 132 | break; |
| 133 | } |
| 134 | if (found) { |
| 135 | DBG("Found event %s for channel %s", name, |
| 136 | channel->channel->name); |
| 137 | return ev; |
| 138 | } else { |
| 139 | return NULL; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Allocate and initialize a kernel session data structure. |
| 145 | * |
| 146 | * Return pointer to structure or NULL. |
| 147 | */ |
| 148 | struct ltt_kernel_session *trace_kernel_create_session(void) |
| 149 | { |
| 150 | struct ltt_kernel_session *lks = NULL; |
| 151 | |
| 152 | /* Allocate a new ltt kernel session */ |
| 153 | lks = zmalloc(sizeof(struct ltt_kernel_session)); |
| 154 | if (lks == NULL) { |
| 155 | PERROR("create kernel session zmalloc"); |
| 156 | goto alloc_error; |
| 157 | } |
| 158 | |
| 159 | /* Init data structure */ |
| 160 | lks->fd = -1; |
| 161 | lks->metadata_stream_fd = -1; |
| 162 | lks->channel_count = 0; |
| 163 | lks->stream_count_global = 0; |
| 164 | lks->metadata = NULL; |
| 165 | CDS_INIT_LIST_HEAD(&lks->channel_list.head); |
| 166 | |
| 167 | lks->tracker_list_pid = lttng_tracker_list_create(); |
| 168 | if (!lks->tracker_list_pid) { |
| 169 | goto error; |
| 170 | } |
| 171 | lks->tracker_list_vpid = lttng_tracker_list_create(); |
| 172 | if (!lks->tracker_list_vpid) { |
| 173 | goto error; |
| 174 | } |
| 175 | lks->tracker_list_uid = lttng_tracker_list_create(); |
| 176 | if (!lks->tracker_list_uid) { |
| 177 | goto error; |
| 178 | } |
| 179 | lks->tracker_list_vuid = lttng_tracker_list_create(); |
| 180 | if (!lks->tracker_list_vuid) { |
| 181 | goto error; |
| 182 | } |
| 183 | lks->tracker_list_gid = lttng_tracker_list_create(); |
| 184 | if (!lks->tracker_list_gid) { |
| 185 | goto error; |
| 186 | } |
| 187 | lks->tracker_list_vgid = lttng_tracker_list_create(); |
| 188 | if (!lks->tracker_list_vgid) { |
| 189 | goto error; |
| 190 | } |
| 191 | lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL); |
| 192 | if (lks->consumer == NULL) { |
| 193 | goto error; |
| 194 | } |
| 195 | |
| 196 | return lks; |
| 197 | |
| 198 | error: |
| 199 | lttng_tracker_list_destroy(lks->tracker_list_pid); |
| 200 | lttng_tracker_list_destroy(lks->tracker_list_vpid); |
| 201 | lttng_tracker_list_destroy(lks->tracker_list_uid); |
| 202 | lttng_tracker_list_destroy(lks->tracker_list_vuid); |
| 203 | lttng_tracker_list_destroy(lks->tracker_list_gid); |
| 204 | lttng_tracker_list_destroy(lks->tracker_list_vgid); |
| 205 | free(lks); |
| 206 | |
| 207 | alloc_error: |
| 208 | return NULL; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Allocate and initialize a kernel channel data structure. |
| 213 | * |
| 214 | * Return pointer to structure or NULL. |
| 215 | */ |
| 216 | struct ltt_kernel_channel *trace_kernel_create_channel( |
| 217 | struct lttng_channel *chan) |
| 218 | { |
| 219 | struct ltt_kernel_channel *lkc; |
| 220 | struct lttng_channel_extended *extended = NULL; |
| 221 | |
| 222 | assert(chan); |
| 223 | |
| 224 | lkc = zmalloc(sizeof(struct ltt_kernel_channel)); |
| 225 | if (lkc == NULL) { |
| 226 | PERROR("ltt_kernel_channel zmalloc"); |
| 227 | goto error; |
| 228 | } |
| 229 | |
| 230 | lkc->channel = zmalloc(sizeof(struct lttng_channel)); |
| 231 | if (lkc->channel == NULL) { |
| 232 | PERROR("lttng_channel zmalloc"); |
| 233 | goto error; |
| 234 | } |
| 235 | |
| 236 | extended = zmalloc(sizeof(struct lttng_channel_extended)); |
| 237 | if (!extended) { |
| 238 | PERROR("lttng_channel_channel zmalloc"); |
| 239 | goto error; |
| 240 | } |
| 241 | memcpy(lkc->channel, chan, sizeof(struct lttng_channel)); |
| 242 | memcpy(extended, chan->attr.extended.ptr, sizeof(struct lttng_channel_extended)); |
| 243 | lkc->channel->attr.extended.ptr = extended; |
| 244 | extended = NULL; |
| 245 | |
| 246 | /* |
| 247 | * If we receive an empty string for channel name, it means the |
| 248 | * default channel name is requested. |
| 249 | */ |
| 250 | if (chan->name[0] == '\0') { |
| 251 | strncpy(lkc->channel->name, DEFAULT_CHANNEL_NAME, |
| 252 | sizeof(lkc->channel->name)); |
| 253 | } |
| 254 | lkc->channel->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 255 | |
| 256 | lkc->fd = -1; |
| 257 | lkc->stream_count = 0; |
| 258 | lkc->event_count = 0; |
| 259 | lkc->enabled = 1; |
| 260 | lkc->published_to_notification_thread = false; |
| 261 | /* Init linked list */ |
| 262 | CDS_INIT_LIST_HEAD(&lkc->events_list.head); |
| 263 | CDS_INIT_LIST_HEAD(&lkc->stream_list.head); |
| 264 | CDS_INIT_LIST_HEAD(&lkc->ctx_list); |
| 265 | |
| 266 | return lkc; |
| 267 | |
| 268 | error: |
| 269 | if (lkc) { |
| 270 | free(lkc->channel); |
| 271 | } |
| 272 | free(extended); |
| 273 | free(lkc); |
| 274 | return NULL; |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Allocate and init a kernel context object. |
| 279 | * |
| 280 | * Return the allocated object or NULL on error. |
| 281 | */ |
| 282 | struct ltt_kernel_context *trace_kernel_create_context( |
| 283 | struct lttng_kernel_context *ctx) |
| 284 | { |
| 285 | struct ltt_kernel_context *kctx; |
| 286 | |
| 287 | kctx = zmalloc(sizeof(*kctx)); |
| 288 | if (!kctx) { |
| 289 | PERROR("zmalloc kernel context"); |
| 290 | goto error; |
| 291 | } |
| 292 | |
| 293 | if (ctx) { |
| 294 | memcpy(&kctx->ctx, ctx, sizeof(kctx->ctx)); |
| 295 | } |
| 296 | error: |
| 297 | return kctx; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Allocate and init a kernel context object from an existing kernel context |
| 302 | * object. |
| 303 | * |
| 304 | * Return the allocated object or NULL on error. |
| 305 | */ |
| 306 | struct ltt_kernel_context *trace_kernel_copy_context( |
| 307 | struct ltt_kernel_context *kctx) |
| 308 | { |
| 309 | struct ltt_kernel_context *kctx_copy; |
| 310 | |
| 311 | assert(kctx); |
| 312 | kctx_copy = zmalloc(sizeof(*kctx_copy)); |
| 313 | if (!kctx_copy) { |
| 314 | PERROR("zmalloc ltt_kernel_context"); |
| 315 | goto error; |
| 316 | } |
| 317 | |
| 318 | memcpy(kctx_copy, kctx, sizeof(*kctx_copy)); |
| 319 | memset(&kctx_copy->list, 0, sizeof(kctx_copy->list)); |
| 320 | |
| 321 | error: |
| 322 | return kctx_copy; |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * Allocate and initialize a kernel event. Set name and event type. |
| 327 | * We own filter_expression, and filter. |
| 328 | * |
| 329 | * Return pointer to structure or NULL. |
| 330 | */ |
| 331 | enum lttng_error_code trace_kernel_create_event( |
| 332 | struct lttng_event *ev, char *filter_expression, |
| 333 | struct lttng_filter_bytecode *filter, |
| 334 | struct ltt_kernel_event **kernel_event) |
| 335 | { |
| 336 | enum lttng_error_code ret; |
| 337 | struct lttng_kernel_event *attr; |
| 338 | struct ltt_kernel_event *local_kernel_event; |
| 339 | struct lttng_userspace_probe_location *userspace_probe_location = NULL; |
| 340 | |
| 341 | assert(ev); |
| 342 | |
| 343 | local_kernel_event = zmalloc(sizeof(struct ltt_kernel_event)); |
| 344 | attr = zmalloc(sizeof(struct lttng_kernel_event)); |
| 345 | if (local_kernel_event == NULL || attr == NULL) { |
| 346 | PERROR("kernel event zmalloc"); |
| 347 | ret = LTTNG_ERR_NOMEM; |
| 348 | goto error; |
| 349 | } |
| 350 | |
| 351 | switch (ev->type) { |
| 352 | case LTTNG_EVENT_PROBE: |
| 353 | attr->instrumentation = LTTNG_KERNEL_KPROBE; |
| 354 | attr->u.kprobe.addr = ev->attr.probe.addr; |
| 355 | attr->u.kprobe.offset = ev->attr.probe.offset; |
| 356 | strncpy(attr->u.kprobe.symbol_name, |
| 357 | ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN); |
| 358 | attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 359 | break; |
| 360 | case LTTNG_EVENT_USERSPACE_PROBE: |
| 361 | { |
| 362 | const struct lttng_userspace_probe_location* location = NULL; |
| 363 | const struct lttng_userspace_probe_location_lookup_method *lookup = NULL; |
| 364 | |
| 365 | location = lttng_event_get_userspace_probe_location(ev); |
| 366 | if (!location) { |
| 367 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 368 | goto error; |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * From this point on, the specific term 'uprobe' is used |
| 373 | * instead of the generic 'userspace probe' because it's the |
| 374 | * technology used at the moment for this instrumentation. |
| 375 | * LTTng currently implements userspace probes using uprobes. |
| 376 | * In the interactions with the kernel tracer, we use the |
| 377 | * uprobe term. |
| 378 | */ |
| 379 | attr->instrumentation = LTTNG_KERNEL_UPROBE; |
| 380 | |
| 381 | /* |
| 382 | * Only the elf lookup method is supported at the moment. |
| 383 | */ |
| 384 | lookup = lttng_userspace_probe_location_get_lookup_method( |
| 385 | location); |
| 386 | if (!lookup) { |
| 387 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 388 | goto error; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * From the kernel tracer's perspective, all userspace probe |
| 393 | * event types are all the same: a file and an offset. |
| 394 | */ |
| 395 | switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) { |
| 396 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF: |
| 397 | /* Get the file descriptor on the target binary. */ |
| 398 | attr->u.uprobe.fd = |
| 399 | lttng_userspace_probe_location_function_get_binary_fd(location); |
| 400 | |
| 401 | /* |
| 402 | * Save a reference to the probe location used during |
| 403 | * the listing of events. Close its FD since it won't |
| 404 | * be needed for listing. |
| 405 | */ |
| 406 | userspace_probe_location = |
| 407 | lttng_userspace_probe_location_copy(location); |
| 408 | ret = lttng_userspace_probe_location_function_set_binary_fd( |
| 409 | userspace_probe_location, -1); |
| 410 | if (ret) { |
| 411 | goto error; |
| 412 | } |
| 413 | break; |
| 414 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT: |
| 415 | /* Get the file descriptor on the target binary. */ |
| 416 | attr->u.uprobe.fd = |
| 417 | lttng_userspace_probe_location_tracepoint_get_binary_fd(location); |
| 418 | |
| 419 | /* |
| 420 | * Save a reference to the probe location used during the listing of |
| 421 | * events. Close its FD since it won't be needed for listing. |
| 422 | */ |
| 423 | userspace_probe_location = |
| 424 | lttng_userspace_probe_location_copy(location); |
| 425 | ret = lttng_userspace_probe_location_tracepoint_set_binary_fd( |
| 426 | userspace_probe_location, -1); |
| 427 | if (ret) { |
| 428 | goto error; |
| 429 | } |
| 430 | break; |
| 431 | default: |
| 432 | DBG("Unsupported lookup method type"); |
| 433 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; |
| 434 | goto error; |
| 435 | } |
| 436 | break; |
| 437 | } |
| 438 | case LTTNG_EVENT_FUNCTION: |
| 439 | attr->instrumentation = LTTNG_KERNEL_KRETPROBE; |
| 440 | attr->u.kretprobe.addr = ev->attr.probe.addr; |
| 441 | attr->u.kretprobe.offset = ev->attr.probe.offset; |
| 442 | strncpy(attr->u.kretprobe.symbol_name, |
| 443 | ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN); |
| 444 | attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 445 | break; |
| 446 | case LTTNG_EVENT_FUNCTION_ENTRY: |
| 447 | attr->instrumentation = LTTNG_KERNEL_FUNCTION; |
| 448 | strncpy(attr->u.ftrace.symbol_name, |
| 449 | ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN); |
| 450 | attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 451 | break; |
| 452 | case LTTNG_EVENT_TRACEPOINT: |
| 453 | attr->instrumentation = LTTNG_KERNEL_TRACEPOINT; |
| 454 | break; |
| 455 | case LTTNG_EVENT_SYSCALL: |
| 456 | attr->instrumentation = LTTNG_KERNEL_SYSCALL; |
| 457 | break; |
| 458 | case LTTNG_EVENT_ALL: |
| 459 | attr->instrumentation = LTTNG_KERNEL_ALL; |
| 460 | break; |
| 461 | default: |
| 462 | ERR("Unknown kernel instrumentation type (%d)", ev->type); |
| 463 | ret = LTTNG_ERR_INVALID; |
| 464 | goto error; |
| 465 | } |
| 466 | |
| 467 | /* Copy event name */ |
| 468 | strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN); |
| 469 | attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 470 | |
| 471 | /* Setting up a kernel event */ |
| 472 | local_kernel_event->fd = -1; |
| 473 | local_kernel_event->event = attr; |
| 474 | local_kernel_event->enabled = 1; |
| 475 | local_kernel_event->filter_expression = filter_expression; |
| 476 | local_kernel_event->filter = filter; |
| 477 | local_kernel_event->userspace_probe_location = userspace_probe_location; |
| 478 | |
| 479 | *kernel_event = local_kernel_event; |
| 480 | |
| 481 | return LTTNG_OK; |
| 482 | |
| 483 | error: |
| 484 | free(filter_expression); |
| 485 | free(filter); |
| 486 | free(local_kernel_event); |
| 487 | free(attr); |
| 488 | return ret; |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * Allocate and initialize a kernel metadata. |
| 493 | * |
| 494 | * Return pointer to structure or NULL. |
| 495 | */ |
| 496 | struct ltt_kernel_metadata *trace_kernel_create_metadata(void) |
| 497 | { |
| 498 | struct ltt_kernel_metadata *lkm; |
| 499 | struct lttng_channel *chan; |
| 500 | |
| 501 | lkm = zmalloc(sizeof(struct ltt_kernel_metadata)); |
| 502 | chan = zmalloc(sizeof(struct lttng_channel)); |
| 503 | if (lkm == NULL || chan == NULL) { |
| 504 | PERROR("kernel metadata zmalloc"); |
| 505 | goto error; |
| 506 | } |
| 507 | |
| 508 | /* Set default attributes */ |
| 509 | chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE; |
| 510 | chan->attr.subbuf_size = default_get_metadata_subbuf_size(); |
| 511 | chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM; |
| 512 | chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER; |
| 513 | chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER; |
| 514 | chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT; |
| 515 | |
| 516 | /* Init metadata */ |
| 517 | lkm->fd = -1; |
| 518 | lkm->conf = chan; |
| 519 | |
| 520 | return lkm; |
| 521 | |
| 522 | error: |
| 523 | free(lkm); |
| 524 | free(chan); |
| 525 | return NULL; |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by |
| 530 | * default. |
| 531 | * |
| 532 | * Return pointer to structure or NULL. |
| 533 | */ |
| 534 | struct ltt_kernel_stream *trace_kernel_create_stream(const char *name, |
| 535 | unsigned int count) |
| 536 | { |
| 537 | int ret; |
| 538 | struct ltt_kernel_stream *lks; |
| 539 | |
| 540 | assert(name); |
| 541 | |
| 542 | lks = zmalloc(sizeof(struct ltt_kernel_stream)); |
| 543 | if (lks == NULL) { |
| 544 | PERROR("kernel stream zmalloc"); |
| 545 | goto error; |
| 546 | } |
| 547 | |
| 548 | /* Set name */ |
| 549 | ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count); |
| 550 | if (ret < 0) { |
| 551 | PERROR("snprintf stream name"); |
| 552 | goto error; |
| 553 | } |
| 554 | lks->name[sizeof(lks->name) - 1] = '\0'; |
| 555 | |
| 556 | /* Init stream */ |
| 557 | lks->fd = -1; |
| 558 | lks->state = 0; |
| 559 | lks->cpu = count; |
| 560 | |
| 561 | return lks; |
| 562 | |
| 563 | error: |
| 564 | return NULL; |
| 565 | } |
| 566 | |
| 567 | /* |
| 568 | * Cleanup kernel stream structure. |
| 569 | */ |
| 570 | void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream) |
| 571 | { |
| 572 | assert(stream); |
| 573 | |
| 574 | DBG("[trace] Closing stream fd %d", stream->fd); |
| 575 | /* Close kernel fd */ |
| 576 | if (stream->fd >= 0) { |
| 577 | int ret; |
| 578 | |
| 579 | ret = close(stream->fd); |
| 580 | if (ret) { |
| 581 | PERROR("close"); |
| 582 | } |
| 583 | } |
| 584 | /* Remove from stream list */ |
| 585 | cds_list_del(&stream->list); |
| 586 | |
| 587 | free(stream); |
| 588 | } |
| 589 | |
| 590 | /* |
| 591 | * Cleanup kernel event structure. |
| 592 | */ |
| 593 | void trace_kernel_destroy_event(struct ltt_kernel_event *event) |
| 594 | { |
| 595 | assert(event); |
| 596 | |
| 597 | if (event->fd >= 0) { |
| 598 | int ret; |
| 599 | |
| 600 | DBG("[trace] Closing event fd %d", event->fd); |
| 601 | /* Close kernel fd */ |
| 602 | ret = close(event->fd); |
| 603 | if (ret) { |
| 604 | PERROR("close"); |
| 605 | } |
| 606 | } else { |
| 607 | DBG("[trace] Tearing down event (no associated fd)"); |
| 608 | } |
| 609 | |
| 610 | /* Remove from event list */ |
| 611 | cds_list_del(&event->list); |
| 612 | |
| 613 | free(event->filter_expression); |
| 614 | free(event->filter); |
| 615 | |
| 616 | free(event->event); |
| 617 | free(event); |
| 618 | } |
| 619 | |
| 620 | /* |
| 621 | * Cleanup kernel context structure. |
| 622 | */ |
| 623 | void trace_kernel_destroy_context(struct ltt_kernel_context *ctx) |
| 624 | { |
| 625 | assert(ctx); |
| 626 | |
| 627 | if (ctx->in_list) { |
| 628 | cds_list_del(&ctx->list); |
| 629 | } |
| 630 | free(ctx); |
| 631 | } |
| 632 | |
| 633 | /* |
| 634 | * Cleanup kernel channel structure. |
| 635 | */ |
| 636 | void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel) |
| 637 | { |
| 638 | struct ltt_kernel_stream *stream, *stmp; |
| 639 | struct ltt_kernel_event *event, *etmp; |
| 640 | struct ltt_kernel_context *ctx, *ctmp; |
| 641 | int ret; |
| 642 | enum lttng_error_code status; |
| 643 | |
| 644 | assert(channel); |
| 645 | |
| 646 | DBG("[trace] Closing channel fd %d", channel->fd); |
| 647 | /* Close kernel fd */ |
| 648 | if (channel->fd >= 0) { |
| 649 | ret = close(channel->fd); |
| 650 | if (ret) { |
| 651 | PERROR("close"); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | /* For each stream in the channel list */ |
| 656 | cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) { |
| 657 | trace_kernel_destroy_stream(stream); |
| 658 | } |
| 659 | |
| 660 | /* For each event in the channel list */ |
| 661 | cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) { |
| 662 | trace_kernel_destroy_event(event); |
| 663 | } |
| 664 | |
| 665 | /* For each context in the channel list */ |
| 666 | cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) { |
| 667 | trace_kernel_destroy_context(ctx); |
| 668 | } |
| 669 | |
| 670 | /* Remove from channel list */ |
| 671 | cds_list_del(&channel->list); |
| 672 | |
| 673 | if (notification_thread_handle |
| 674 | && channel->published_to_notification_thread) { |
| 675 | status = notification_thread_command_remove_channel( |
| 676 | notification_thread_handle, |
| 677 | channel->key, LTTNG_DOMAIN_KERNEL); |
| 678 | assert(status == LTTNG_OK); |
| 679 | } |
| 680 | free(channel->channel->attr.extended.ptr); |
| 681 | free(channel->channel); |
| 682 | free(channel); |
| 683 | } |
| 684 | |
| 685 | /* |
| 686 | * Cleanup kernel metadata structure. |
| 687 | */ |
| 688 | void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata) |
| 689 | { |
| 690 | assert(metadata); |
| 691 | |
| 692 | DBG("[trace] Closing metadata fd %d", metadata->fd); |
| 693 | /* Close kernel fd */ |
| 694 | if (metadata->fd >= 0) { |
| 695 | int ret; |
| 696 | |
| 697 | ret = close(metadata->fd); |
| 698 | if (ret) { |
| 699 | PERROR("close"); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | free(metadata->conf); |
| 704 | free(metadata); |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * Cleanup kernel session structure |
| 709 | * |
| 710 | * Should *NOT* be called with RCU read-side lock held. |
| 711 | */ |
| 712 | void trace_kernel_destroy_session(struct ltt_kernel_session *session) |
| 713 | { |
| 714 | struct ltt_kernel_channel *channel, *ctmp; |
| 715 | int ret; |
| 716 | |
| 717 | assert(session); |
| 718 | |
| 719 | DBG("[trace] Closing session fd %d", session->fd); |
| 720 | /* Close kernel fds */ |
| 721 | if (session->fd >= 0) { |
| 722 | ret = close(session->fd); |
| 723 | if (ret) { |
| 724 | PERROR("close"); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | if (session->metadata_stream_fd >= 0) { |
| 729 | DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd); |
| 730 | ret = close(session->metadata_stream_fd); |
| 731 | if (ret) { |
| 732 | PERROR("close"); |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | if (session->metadata != NULL) { |
| 737 | trace_kernel_destroy_metadata(session->metadata); |
| 738 | } |
| 739 | |
| 740 | cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) { |
| 741 | trace_kernel_destroy_channel(channel); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | /* Free elements needed by destroy notifiers. */ |
| 746 | void trace_kernel_free_session(struct ltt_kernel_session *session) |
| 747 | { |
| 748 | /* Wipe consumer output object */ |
| 749 | consumer_output_put(session->consumer); |
| 750 | |
| 751 | lttng_tracker_list_destroy(session->tracker_list_pid); |
| 752 | lttng_tracker_list_destroy(session->tracker_list_vpid); |
| 753 | lttng_tracker_list_destroy(session->tracker_list_uid); |
| 754 | lttng_tracker_list_destroy(session->tracker_list_vuid); |
| 755 | lttng_tracker_list_destroy(session->tracker_list_gid); |
| 756 | lttng_tracker_list_destroy(session->tracker_list_vgid); |
| 757 | |
| 758 | free(session); |
| 759 | } |