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