| 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 <inttypes.h> |
| 20 | #include <popt.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <assert.h> |
| 25 | |
| 26 | #include "../command.h" |
| 27 | |
| 28 | static int opt_userspace; |
| 29 | static int opt_kernel; |
| 30 | static int opt_jul; |
| 31 | static char *opt_channel; |
| 32 | static int opt_domain; |
| 33 | static int opt_fields; |
| 34 | #if 0 |
| 35 | /* Not implemented yet */ |
| 36 | static char *opt_cmd_name; |
| 37 | static pid_t opt_pid; |
| 38 | #endif |
| 39 | |
| 40 | const char *indent4 = " "; |
| 41 | const char *indent6 = " "; |
| 42 | const char *indent8 = " "; |
| 43 | |
| 44 | enum { |
| 45 | OPT_HELP = 1, |
| 46 | OPT_USERSPACE, |
| 47 | OPT_LIST_OPTIONS, |
| 48 | }; |
| 49 | |
| 50 | static struct lttng_handle *handle; |
| 51 | |
| 52 | static struct poptOption long_options[] = { |
| 53 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 54 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
| 55 | {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, |
| 56 | {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0}, |
| 57 | #if 0 |
| 58 | /* Not implemented yet */ |
| 59 | {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0}, |
| 60 | {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0}, |
| 61 | #else |
| 62 | {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, |
| 63 | #endif |
| 64 | {"channel", 'c', POPT_ARG_STRING, &opt_channel, 0, 0, 0}, |
| 65 | {"domain", 'd', POPT_ARG_VAL, &opt_domain, 1, 0, 0}, |
| 66 | {"fields", 'f', POPT_ARG_VAL, &opt_fields, 1, 0, 0}, |
| 67 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 68 | {0, 0, 0, 0, 0, 0, 0} |
| 69 | }; |
| 70 | |
| 71 | /* |
| 72 | * usage |
| 73 | */ |
| 74 | static void usage(FILE *ofp) |
| 75 | { |
| 76 | fprintf(ofp, "usage: lttng list [OPTIONS] [SESSION [SESSION OPTIONS]]\n"); |
| 77 | fprintf(ofp, "\n"); |
| 78 | fprintf(ofp, "With no arguments, list available tracing session(s)\n"); |
| 79 | fprintf(ofp, "\n"); |
| 80 | fprintf(ofp, "Without a session, -k lists available kernel events\n"); |
| 81 | fprintf(ofp, "Without a session, -u lists available userspace events\n"); |
| 82 | fprintf(ofp, "\n"); |
| 83 | fprintf(ofp, " -h, --help Show this help\n"); |
| 84 | fprintf(ofp, " --list-options Simple listing of options\n"); |
| 85 | fprintf(ofp, " -k, --kernel Select kernel domain\n"); |
| 86 | fprintf(ofp, " -u, --userspace Select user-space domain.\n"); |
| 87 | fprintf(ofp, " -j, --jul Apply for Java application using JUL\n"); |
| 88 | fprintf(ofp, " -f, --fields List event fields.\n"); |
| 89 | #if 0 |
| 90 | fprintf(ofp, " -p, --pid PID List user-space events by PID\n"); |
| 91 | #endif |
| 92 | fprintf(ofp, "\n"); |
| 93 | fprintf(ofp, "Session Options:\n"); |
| 94 | fprintf(ofp, " -c, --channel NAME List details of a channel\n"); |
| 95 | fprintf(ofp, " -d, --domain List available domain(s)\n"); |
| 96 | fprintf(ofp, "\n"); |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Get command line from /proc for a specific pid. |
| 101 | * |
| 102 | * On success, return an allocated string pointer to the proc cmdline. |
| 103 | * On error, return NULL. |
| 104 | */ |
| 105 | static char *get_cmdline_by_pid(pid_t pid) |
| 106 | { |
| 107 | int ret; |
| 108 | FILE *fp; |
| 109 | char *cmdline = NULL; |
| 110 | char path[24]; /* Can't go bigger than /proc/65535/cmdline */ |
| 111 | |
| 112 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); |
| 113 | fp = fopen(path, "r"); |
| 114 | if (fp == NULL) { |
| 115 | goto end; |
| 116 | } |
| 117 | |
| 118 | /* Caller must free() *cmdline */ |
| 119 | cmdline = malloc(PATH_MAX); |
| 120 | ret = fread(cmdline, 1, PATH_MAX, fp); |
| 121 | if (ret < 0) { |
| 122 | perror("fread proc list"); |
| 123 | } |
| 124 | fclose(fp); |
| 125 | |
| 126 | end: |
| 127 | return cmdline; |
| 128 | } |
| 129 | |
| 130 | static |
| 131 | const char *active_string(int value) |
| 132 | { |
| 133 | switch (value) { |
| 134 | case 0: return "inactive"; |
| 135 | case 1: return "active"; |
| 136 | case -1: return ""; |
| 137 | default: return NULL; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | static const char *snapshot_string(int value) |
| 142 | { |
| 143 | switch (value) { |
| 144 | case 1: |
| 145 | return " snapshot"; |
| 146 | default: |
| 147 | return ""; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | static |
| 152 | const char *enabled_string(int value) |
| 153 | { |
| 154 | switch (value) { |
| 155 | case 0: return " [disabled]"; |
| 156 | case 1: return " [enabled]"; |
| 157 | case -1: return ""; |
| 158 | default: return NULL; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | static |
| 163 | const char *filter_string(int value) |
| 164 | { |
| 165 | switch (value) { |
| 166 | case 1: return " [with filter]"; |
| 167 | default: return ""; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | static const char *loglevel_string(int value) |
| 172 | { |
| 173 | switch (value) { |
| 174 | case -1: |
| 175 | return ""; |
| 176 | case LTTNG_LOGLEVEL_EMERG: |
| 177 | return "TRACE_EMERG"; |
| 178 | case LTTNG_LOGLEVEL_ALERT: |
| 179 | return "TRACE_ALERT"; |
| 180 | case LTTNG_LOGLEVEL_CRIT: |
| 181 | return "TRACE_CRIT"; |
| 182 | case LTTNG_LOGLEVEL_ERR: |
| 183 | return "TRACE_ERR"; |
| 184 | case LTTNG_LOGLEVEL_WARNING: |
| 185 | return "TRACE_WARNING"; |
| 186 | case LTTNG_LOGLEVEL_NOTICE: |
| 187 | return "TRACE_NOTICE"; |
| 188 | case LTTNG_LOGLEVEL_INFO: |
| 189 | return "TRACE_INFO"; |
| 190 | case LTTNG_LOGLEVEL_DEBUG_SYSTEM: |
| 191 | return "TRACE_DEBUG_SYSTEM"; |
| 192 | case LTTNG_LOGLEVEL_DEBUG_PROGRAM: |
| 193 | return "TRACE_DEBUG_PROGRAM"; |
| 194 | case LTTNG_LOGLEVEL_DEBUG_PROCESS: |
| 195 | return "TRACE_DEBUG_PROCESS"; |
| 196 | case LTTNG_LOGLEVEL_DEBUG_MODULE: |
| 197 | return "TRACE_DEBUG_MODULE"; |
| 198 | case LTTNG_LOGLEVEL_DEBUG_UNIT: |
| 199 | return "TRACE_DEBUG_UNIT"; |
| 200 | case LTTNG_LOGLEVEL_DEBUG_FUNCTION: |
| 201 | return "TRACE_DEBUG_FUNCTION"; |
| 202 | case LTTNG_LOGLEVEL_DEBUG_LINE: |
| 203 | return "TRACE_DEBUG_LINE"; |
| 204 | case LTTNG_LOGLEVEL_DEBUG: |
| 205 | return "TRACE_DEBUG"; |
| 206 | default: |
| 207 | return "<<UNKNOWN>>"; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Pretty print single event. |
| 213 | */ |
| 214 | static void print_events(struct lttng_event *event) |
| 215 | { |
| 216 | switch (event->type) { |
| 217 | case LTTNG_EVENT_TRACEPOINT: |
| 218 | { |
| 219 | if (event->loglevel != -1) { |
| 220 | MSG("%s%s (loglevel: %s (%d)) (type: tracepoint)%s%s", |
| 221 | indent6, |
| 222 | event->name, |
| 223 | loglevel_string(event->loglevel), |
| 224 | event->loglevel, |
| 225 | enabled_string(event->enabled), |
| 226 | filter_string(event->filter)); |
| 227 | } else { |
| 228 | MSG("%s%s (type: tracepoint)%s%s", |
| 229 | indent6, |
| 230 | event->name, |
| 231 | enabled_string(event->enabled), |
| 232 | filter_string(event->filter)); |
| 233 | } |
| 234 | break; |
| 235 | } |
| 236 | case LTTNG_EVENT_FUNCTION: |
| 237 | MSG("%s%s (type: function)%s%s", indent6, |
| 238 | event->name, enabled_string(event->enabled), |
| 239 | filter_string(event->filter)); |
| 240 | if (event->attr.probe.addr != 0) { |
| 241 | MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr); |
| 242 | } else { |
| 243 | MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset); |
| 244 | MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name); |
| 245 | } |
| 246 | break; |
| 247 | case LTTNG_EVENT_PROBE: |
| 248 | MSG("%s%s (type: probe)%s%s", indent6, |
| 249 | event->name, enabled_string(event->enabled), |
| 250 | filter_string(event->filter)); |
| 251 | if (event->attr.probe.addr != 0) { |
| 252 | MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr); |
| 253 | } else { |
| 254 | MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset); |
| 255 | MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name); |
| 256 | } |
| 257 | break; |
| 258 | case LTTNG_EVENT_FUNCTION_ENTRY: |
| 259 | MSG("%s%s (type: function)%s%s", indent6, |
| 260 | event->name, enabled_string(event->enabled), |
| 261 | filter_string(event->filter)); |
| 262 | MSG("%ssymbol: \"%s\"", indent8, event->attr.ftrace.symbol_name); |
| 263 | break; |
| 264 | case LTTNG_EVENT_SYSCALL: |
| 265 | MSG("%ssyscalls (type: syscall)%s%s", indent6, |
| 266 | enabled_string(event->enabled), |
| 267 | filter_string(event->filter)); |
| 268 | break; |
| 269 | case LTTNG_EVENT_NOOP: |
| 270 | MSG("%s (type: noop)%s%s", indent6, |
| 271 | enabled_string(event->enabled), |
| 272 | filter_string(event->filter)); |
| 273 | break; |
| 274 | case LTTNG_EVENT_ALL: |
| 275 | /* We should never have "all" events in list. */ |
| 276 | assert(0); |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | static const char *field_type(struct lttng_event_field *field) |
| 282 | { |
| 283 | switch(field->type) { |
| 284 | case LTTNG_EVENT_FIELD_INTEGER: |
| 285 | return "integer"; |
| 286 | case LTTNG_EVENT_FIELD_ENUM: |
| 287 | return "enum"; |
| 288 | case LTTNG_EVENT_FIELD_FLOAT: |
| 289 | return "float"; |
| 290 | case LTTNG_EVENT_FIELD_STRING: |
| 291 | return "string"; |
| 292 | case LTTNG_EVENT_FIELD_OTHER: |
| 293 | default: /* fall-through */ |
| 294 | return "unknown"; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Pretty print single event fields. |
| 300 | */ |
| 301 | static void print_event_field(struct lttng_event_field *field) |
| 302 | { |
| 303 | if (!field->field_name[0]) { |
| 304 | return; |
| 305 | } |
| 306 | MSG("%sfield: %s (%s)%s", indent8, field->field_name, |
| 307 | field_type(field), field->nowrite ? " [no write]" : ""); |
| 308 | } |
| 309 | |
| 310 | static int list_jul_events(void) |
| 311 | { |
| 312 | int i, size; |
| 313 | struct lttng_domain domain; |
| 314 | struct lttng_handle *handle; |
| 315 | struct lttng_event *event_list; |
| 316 | pid_t cur_pid = 0; |
| 317 | char *cmdline = NULL; |
| 318 | |
| 319 | DBG("Getting JUL tracing events"); |
| 320 | |
| 321 | memset(&domain, 0, sizeof(domain)); |
| 322 | domain.type = LTTNG_DOMAIN_JUL; |
| 323 | |
| 324 | handle = lttng_create_handle(NULL, &domain); |
| 325 | if (handle == NULL) { |
| 326 | goto error; |
| 327 | } |
| 328 | |
| 329 | size = lttng_list_tracepoints(handle, &event_list); |
| 330 | if (size < 0) { |
| 331 | ERR("Unable to list JUL events: %s", lttng_strerror(size)); |
| 332 | lttng_destroy_handle(handle); |
| 333 | return size; |
| 334 | } |
| 335 | |
| 336 | MSG("JUL events (Logger name):\n-------------------------"); |
| 337 | |
| 338 | if (size == 0) { |
| 339 | MSG("None"); |
| 340 | } |
| 341 | |
| 342 | for (i = 0; i < size; i++) { |
| 343 | if (cur_pid != event_list[i].pid) { |
| 344 | cur_pid = event_list[i].pid; |
| 345 | cmdline = get_cmdline_by_pid(cur_pid); |
| 346 | MSG("\nPID: %d - Name: %s", cur_pid, cmdline); |
| 347 | free(cmdline); |
| 348 | } |
| 349 | MSG("%s- %s", indent6, event_list[i].name); |
| 350 | } |
| 351 | |
| 352 | MSG(""); |
| 353 | |
| 354 | free(event_list); |
| 355 | lttng_destroy_handle(handle); |
| 356 | |
| 357 | return CMD_SUCCESS; |
| 358 | |
| 359 | error: |
| 360 | lttng_destroy_handle(handle); |
| 361 | return -1; |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * Ask session daemon for all user space tracepoints available. |
| 366 | */ |
| 367 | static int list_ust_events(void) |
| 368 | { |
| 369 | int i, size; |
| 370 | struct lttng_domain domain; |
| 371 | struct lttng_handle *handle; |
| 372 | struct lttng_event *event_list; |
| 373 | pid_t cur_pid = 0; |
| 374 | char *cmdline = NULL; |
| 375 | |
| 376 | memset(&domain, 0, sizeof(domain)); |
| 377 | |
| 378 | DBG("Getting UST tracing events"); |
| 379 | |
| 380 | domain.type = LTTNG_DOMAIN_UST; |
| 381 | |
| 382 | handle = lttng_create_handle(NULL, &domain); |
| 383 | if (handle == NULL) { |
| 384 | goto error; |
| 385 | } |
| 386 | |
| 387 | size = lttng_list_tracepoints(handle, &event_list); |
| 388 | if (size < 0) { |
| 389 | ERR("Unable to list UST events: %s", lttng_strerror(size)); |
| 390 | lttng_destroy_handle(handle); |
| 391 | return size; |
| 392 | } |
| 393 | |
| 394 | MSG("UST events:\n-------------"); |
| 395 | |
| 396 | if (size == 0) { |
| 397 | MSG("None"); |
| 398 | } |
| 399 | |
| 400 | for (i = 0; i < size; i++) { |
| 401 | if (cur_pid != event_list[i].pid) { |
| 402 | cur_pid = event_list[i].pid; |
| 403 | cmdline = get_cmdline_by_pid(cur_pid); |
| 404 | MSG("\nPID: %d - Name: %s", cur_pid, cmdline); |
| 405 | free(cmdline); |
| 406 | } |
| 407 | print_events(&event_list[i]); |
| 408 | } |
| 409 | |
| 410 | MSG(""); |
| 411 | |
| 412 | free(event_list); |
| 413 | lttng_destroy_handle(handle); |
| 414 | |
| 415 | return CMD_SUCCESS; |
| 416 | |
| 417 | error: |
| 418 | lttng_destroy_handle(handle); |
| 419 | return -1; |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * Ask session daemon for all user space tracepoint fields available. |
| 424 | */ |
| 425 | static int list_ust_event_fields(void) |
| 426 | { |
| 427 | int i, size; |
| 428 | struct lttng_domain domain; |
| 429 | struct lttng_handle *handle; |
| 430 | struct lttng_event_field *event_field_list; |
| 431 | pid_t cur_pid = 0; |
| 432 | char *cmdline = NULL; |
| 433 | |
| 434 | struct lttng_event cur_event; |
| 435 | |
| 436 | memset(&domain, 0, sizeof(domain)); |
| 437 | memset(&cur_event, 0, sizeof(cur_event)); |
| 438 | |
| 439 | DBG("Getting UST tracing event fields"); |
| 440 | |
| 441 | domain.type = LTTNG_DOMAIN_UST; |
| 442 | |
| 443 | handle = lttng_create_handle(NULL, &domain); |
| 444 | if (handle == NULL) { |
| 445 | goto error; |
| 446 | } |
| 447 | |
| 448 | size = lttng_list_tracepoint_fields(handle, &event_field_list); |
| 449 | if (size < 0) { |
| 450 | ERR("Unable to list UST event fields: %s", lttng_strerror(size)); |
| 451 | lttng_destroy_handle(handle); |
| 452 | return size; |
| 453 | } |
| 454 | |
| 455 | MSG("UST events:\n-------------"); |
| 456 | |
| 457 | if (size == 0) { |
| 458 | MSG("None"); |
| 459 | } |
| 460 | |
| 461 | for (i = 0; i < size; i++) { |
| 462 | if (cur_pid != event_field_list[i].event.pid) { |
| 463 | cur_pid = event_field_list[i].event.pid; |
| 464 | cmdline = get_cmdline_by_pid(cur_pid); |
| 465 | MSG("\nPID: %d - Name: %s", cur_pid, cmdline); |
| 466 | free(cmdline); |
| 467 | } |
| 468 | if (strcmp(cur_event.name, event_field_list[i].event.name) != 0) { |
| 469 | print_events(&event_field_list[i].event); |
| 470 | memcpy(&cur_event, &event_field_list[i].event, |
| 471 | sizeof(cur_event)); |
| 472 | } |
| 473 | print_event_field(&event_field_list[i]); |
| 474 | } |
| 475 | |
| 476 | MSG(""); |
| 477 | |
| 478 | free(event_field_list); |
| 479 | lttng_destroy_handle(handle); |
| 480 | |
| 481 | return CMD_SUCCESS; |
| 482 | |
| 483 | error: |
| 484 | lttng_destroy_handle(handle); |
| 485 | return -1; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * Ask for all trace events in the kernel and pretty print them. |
| 490 | */ |
| 491 | static int list_kernel_events(void) |
| 492 | { |
| 493 | int i, size; |
| 494 | struct lttng_domain domain; |
| 495 | struct lttng_handle *handle; |
| 496 | struct lttng_event *event_list; |
| 497 | |
| 498 | memset(&domain, 0, sizeof(domain)); |
| 499 | |
| 500 | DBG("Getting kernel tracing events"); |
| 501 | |
| 502 | domain.type = LTTNG_DOMAIN_KERNEL; |
| 503 | |
| 504 | handle = lttng_create_handle(NULL, &domain); |
| 505 | if (handle == NULL) { |
| 506 | goto error; |
| 507 | } |
| 508 | |
| 509 | size = lttng_list_tracepoints(handle, &event_list); |
| 510 | if (size < 0) { |
| 511 | ERR("Unable to list kernel events: %s", lttng_strerror(size)); |
| 512 | lttng_destroy_handle(handle); |
| 513 | return size; |
| 514 | } |
| 515 | |
| 516 | MSG("Kernel events:\n-------------"); |
| 517 | |
| 518 | for (i = 0; i < size; i++) { |
| 519 | print_events(&event_list[i]); |
| 520 | } |
| 521 | |
| 522 | MSG(""); |
| 523 | |
| 524 | free(event_list); |
| 525 | |
| 526 | lttng_destroy_handle(handle); |
| 527 | return CMD_SUCCESS; |
| 528 | |
| 529 | error: |
| 530 | lttng_destroy_handle(handle); |
| 531 | return -1; |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * List JUL events for a specific session using the handle. |
| 536 | * |
| 537 | * Return CMD_SUCCESS on success else a negative value. |
| 538 | */ |
| 539 | static int list_session_jul_events(void) |
| 540 | { |
| 541 | int ret, count, i; |
| 542 | struct lttng_event *events = NULL; |
| 543 | |
| 544 | count = lttng_list_events(handle, "", &events); |
| 545 | if (count < 0) { |
| 546 | ret = count; |
| 547 | ERR("%s", lttng_strerror(ret)); |
| 548 | goto error; |
| 549 | } |
| 550 | |
| 551 | MSG("Events (Logger name):\n---------------------"); |
| 552 | if (count == 0) { |
| 553 | MSG("%sNone\n", indent6); |
| 554 | goto end; |
| 555 | } |
| 556 | |
| 557 | for (i = 0; i < count; i++) { |
| 558 | MSG("%s- %s%s", indent4, events[i].name, |
| 559 | enabled_string(events[i].enabled)); |
| 560 | } |
| 561 | |
| 562 | MSG(""); |
| 563 | |
| 564 | end: |
| 565 | free(events); |
| 566 | ret = CMD_SUCCESS; |
| 567 | |
| 568 | error: |
| 569 | return ret; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * List events of channel of session and domain. |
| 574 | */ |
| 575 | static int list_events(const char *channel_name) |
| 576 | { |
| 577 | int ret, count, i; |
| 578 | struct lttng_event *events = NULL; |
| 579 | |
| 580 | count = lttng_list_events(handle, channel_name, &events); |
| 581 | if (count < 0) { |
| 582 | ret = count; |
| 583 | ERR("%s", lttng_strerror(ret)); |
| 584 | goto error; |
| 585 | } |
| 586 | |
| 587 | MSG("\n%sEvents:", indent4); |
| 588 | if (count == 0) { |
| 589 | MSG("%sNone\n", indent6); |
| 590 | goto end; |
| 591 | } |
| 592 | |
| 593 | for (i = 0; i < count; i++) { |
| 594 | print_events(&events[i]); |
| 595 | } |
| 596 | |
| 597 | MSG(""); |
| 598 | |
| 599 | end: |
| 600 | free(events); |
| 601 | ret = CMD_SUCCESS; |
| 602 | |
| 603 | error: |
| 604 | return ret; |
| 605 | } |
| 606 | |
| 607 | /* |
| 608 | * Pretty print channel |
| 609 | */ |
| 610 | static void print_channel(struct lttng_channel *channel) |
| 611 | { |
| 612 | MSG("- %s:%s\n", channel->name, enabled_string(channel->enabled)); |
| 613 | |
| 614 | MSG("%sAttributes:", indent4); |
| 615 | MSG("%soverwrite mode: %d", indent6, channel->attr.overwrite); |
| 616 | MSG("%ssubbufers size: %" PRIu64, indent6, channel->attr.subbuf_size); |
| 617 | MSG("%snumber of subbufers: %" PRIu64, indent6, channel->attr.num_subbuf); |
| 618 | MSG("%sswitch timer interval: %u", indent6, channel->attr.switch_timer_interval); |
| 619 | MSG("%sread timer interval: %u", indent6, channel->attr.read_timer_interval); |
| 620 | switch (channel->attr.output) { |
| 621 | case LTTNG_EVENT_SPLICE: |
| 622 | MSG("%soutput: splice()", indent6); |
| 623 | break; |
| 624 | case LTTNG_EVENT_MMAP: |
| 625 | MSG("%soutput: mmap()", indent6); |
| 626 | break; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | /* |
| 631 | * List channel(s) of session and domain. |
| 632 | * |
| 633 | * If channel_name is NULL, all channels are listed. |
| 634 | */ |
| 635 | static int list_channels(const char *channel_name) |
| 636 | { |
| 637 | int count, i, ret = CMD_SUCCESS; |
| 638 | unsigned int chan_found = 0; |
| 639 | struct lttng_channel *channels = NULL; |
| 640 | |
| 641 | DBG("Listing channel(s) (%s)", channel_name ? : "<all>"); |
| 642 | |
| 643 | count = lttng_list_channels(handle, &channels); |
| 644 | if (count < 0) { |
| 645 | switch (-count) { |
| 646 | case LTTNG_ERR_KERN_CHAN_NOT_FOUND: |
| 647 | ret = CMD_SUCCESS; |
| 648 | WARN("No kernel channel"); |
| 649 | break; |
| 650 | default: |
| 651 | /* We had a real error */ |
| 652 | ret = count; |
| 653 | ERR("%s", lttng_strerror(ret)); |
| 654 | break; |
| 655 | } |
| 656 | goto error_channels; |
| 657 | } |
| 658 | |
| 659 | if (channel_name == NULL) { |
| 660 | MSG("Channels:\n-------------"); |
| 661 | } |
| 662 | |
| 663 | for (i = 0; i < count; i++) { |
| 664 | if (channel_name != NULL) { |
| 665 | if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) { |
| 666 | chan_found = 1; |
| 667 | } else { |
| 668 | continue; |
| 669 | } |
| 670 | } |
| 671 | print_channel(&channels[i]); |
| 672 | |
| 673 | /* Listing events per channel */ |
| 674 | ret = list_events(channels[i].name); |
| 675 | if (ret < 0) { |
| 676 | ERR("%s", lttng_strerror(ret)); |
| 677 | } |
| 678 | |
| 679 | if (chan_found) { |
| 680 | break; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | if (!chan_found && channel_name != NULL) { |
| 685 | ERR("Channel %s not found", channel_name); |
| 686 | goto error; |
| 687 | } |
| 688 | |
| 689 | ret = CMD_SUCCESS; |
| 690 | |
| 691 | error: |
| 692 | free(channels); |
| 693 | |
| 694 | error_channels: |
| 695 | return ret; |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * List available tracing session. List only basic information. |
| 700 | * |
| 701 | * If session_name is NULL, all sessions are listed. |
| 702 | */ |
| 703 | static int list_sessions(const char *session_name) |
| 704 | { |
| 705 | int ret, count, i; |
| 706 | unsigned int session_found = 0; |
| 707 | struct lttng_session *sessions; |
| 708 | |
| 709 | count = lttng_list_sessions(&sessions); |
| 710 | DBG("Session count %d", count); |
| 711 | if (count < 0) { |
| 712 | ret = count; |
| 713 | ERR("%s", lttng_strerror(ret)); |
| 714 | goto error; |
| 715 | } else if (count == 0) { |
| 716 | MSG("Currently no available tracing session"); |
| 717 | goto end; |
| 718 | } |
| 719 | |
| 720 | if (session_name == NULL) { |
| 721 | MSG("Available tracing sessions:"); |
| 722 | } |
| 723 | |
| 724 | for (i = 0; i < count; i++) { |
| 725 | if (session_name != NULL) { |
| 726 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { |
| 727 | session_found = 1; |
| 728 | MSG("Tracing session %s: [%s%s]", session_name, |
| 729 | active_string(sessions[i].enabled), |
| 730 | snapshot_string(sessions[i].snapshot_mode)); |
| 731 | MSG("%sTrace path: %s\n", indent4, sessions[i].path); |
| 732 | break; |
| 733 | } |
| 734 | } else { |
| 735 | MSG(" %d) %s (%s) [%s%s]", i + 1, sessions[i].name, sessions[i].path, |
| 736 | active_string(sessions[i].enabled), |
| 737 | snapshot_string(sessions[i].snapshot_mode)); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | free(sessions); |
| 742 | |
| 743 | if (!session_found && session_name != NULL) { |
| 744 | ERR("Session '%s' not found", session_name); |
| 745 | ret = CMD_ERROR; |
| 746 | goto error; |
| 747 | } |
| 748 | |
| 749 | if (session_name == NULL) { |
| 750 | MSG("\nUse lttng list <session_name> for more details"); |
| 751 | } |
| 752 | |
| 753 | end: |
| 754 | return CMD_SUCCESS; |
| 755 | |
| 756 | error: |
| 757 | return ret; |
| 758 | } |
| 759 | |
| 760 | /* |
| 761 | * List available domain(s) for a session. |
| 762 | */ |
| 763 | static int list_domains(const char *session_name) |
| 764 | { |
| 765 | int i, count, ret = CMD_SUCCESS; |
| 766 | struct lttng_domain *domains = NULL; |
| 767 | |
| 768 | MSG("Domains:\n-------------"); |
| 769 | |
| 770 | count = lttng_list_domains(session_name, &domains); |
| 771 | if (count < 0) { |
| 772 | ret = count; |
| 773 | ERR("%s", lttng_strerror(ret)); |
| 774 | goto error; |
| 775 | } else if (count == 0) { |
| 776 | MSG(" None"); |
| 777 | goto end; |
| 778 | } |
| 779 | |
| 780 | for (i = 0; i < count; i++) { |
| 781 | switch (domains[i].type) { |
| 782 | case LTTNG_DOMAIN_KERNEL: |
| 783 | MSG(" - Kernel"); |
| 784 | break; |
| 785 | case LTTNG_DOMAIN_UST: |
| 786 | MSG(" - UST global"); |
| 787 | break; |
| 788 | case LTTNG_DOMAIN_JUL: |
| 789 | MSG(" - JUL (Java Util Logging)"); |
| 790 | break; |
| 791 | default: |
| 792 | break; |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | end: |
| 797 | free(domains); |
| 798 | |
| 799 | error: |
| 800 | return ret; |
| 801 | } |
| 802 | |
| 803 | /* |
| 804 | * The 'list <options>' first level command |
| 805 | */ |
| 806 | int cmd_list(int argc, const char **argv) |
| 807 | { |
| 808 | int opt, ret = CMD_SUCCESS; |
| 809 | const char *session_name; |
| 810 | static poptContext pc; |
| 811 | struct lttng_domain domain; |
| 812 | struct lttng_domain *domains = NULL; |
| 813 | |
| 814 | memset(&domain, 0, sizeof(domain)); |
| 815 | |
| 816 | if (argc < 1) { |
| 817 | usage(stderr); |
| 818 | ret = CMD_ERROR; |
| 819 | goto end; |
| 820 | } |
| 821 | |
| 822 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 823 | poptReadDefaultConfig(pc, 0); |
| 824 | |
| 825 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 826 | switch (opt) { |
| 827 | case OPT_HELP: |
| 828 | usage(stdout); |
| 829 | goto end; |
| 830 | case OPT_USERSPACE: |
| 831 | opt_userspace = 1; |
| 832 | break; |
| 833 | case OPT_LIST_OPTIONS: |
| 834 | list_cmd_options(stdout, long_options); |
| 835 | goto end; |
| 836 | default: |
| 837 | usage(stderr); |
| 838 | ret = CMD_UNDEFINED; |
| 839 | goto end; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | /* Get session name (trailing argument) */ |
| 844 | session_name = poptGetArg(pc); |
| 845 | DBG2("Session name: %s", session_name); |
| 846 | |
| 847 | if (opt_kernel) { |
| 848 | domain.type = LTTNG_DOMAIN_KERNEL; |
| 849 | } else if (opt_userspace) { |
| 850 | DBG2("Listing userspace global domain"); |
| 851 | domain.type = LTTNG_DOMAIN_UST; |
| 852 | } else if (opt_jul) { |
| 853 | DBG2("Listing JUL domain"); |
| 854 | domain.type = LTTNG_DOMAIN_JUL; |
| 855 | } |
| 856 | |
| 857 | if (opt_kernel || opt_userspace || opt_jul) { |
| 858 | handle = lttng_create_handle(session_name, &domain); |
| 859 | if (handle == NULL) { |
| 860 | ret = CMD_FATAL; |
| 861 | goto end; |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | if (session_name == NULL) { |
| 866 | if (!opt_kernel && !opt_userspace && !opt_jul) { |
| 867 | ret = list_sessions(NULL); |
| 868 | if (ret != 0) { |
| 869 | goto end; |
| 870 | } |
| 871 | } |
| 872 | if (opt_kernel) { |
| 873 | ret = list_kernel_events(); |
| 874 | if (ret < 0) { |
| 875 | ret = CMD_ERROR; |
| 876 | goto end; |
| 877 | } |
| 878 | } |
| 879 | if (opt_userspace) { |
| 880 | if (opt_fields) { |
| 881 | ret = list_ust_event_fields(); |
| 882 | } else { |
| 883 | ret = list_ust_events(); |
| 884 | } |
| 885 | if (ret < 0) { |
| 886 | ret = CMD_ERROR; |
| 887 | goto end; |
| 888 | } |
| 889 | } |
| 890 | if (opt_jul) { |
| 891 | ret = list_jul_events(); |
| 892 | if (ret < 0) { |
| 893 | ret = CMD_ERROR; |
| 894 | goto end; |
| 895 | } |
| 896 | } |
| 897 | } else { |
| 898 | /* List session attributes */ |
| 899 | ret = list_sessions(session_name); |
| 900 | if (ret != 0) { |
| 901 | goto end; |
| 902 | } |
| 903 | |
| 904 | /* Domain listing */ |
| 905 | if (opt_domain) { |
| 906 | ret = list_domains(session_name); |
| 907 | goto end; |
| 908 | } |
| 909 | |
| 910 | if (opt_kernel) { |
| 911 | /* Channel listing */ |
| 912 | ret = list_channels(opt_channel); |
| 913 | if (ret < 0) { |
| 914 | goto end; |
| 915 | } |
| 916 | } else { |
| 917 | int i, nb_domain; |
| 918 | |
| 919 | /* We want all domain(s) */ |
| 920 | nb_domain = lttng_list_domains(session_name, &domains); |
| 921 | if (nb_domain < 0) { |
| 922 | ret = nb_domain; |
| 923 | ERR("%s", lttng_strerror(ret)); |
| 924 | goto end; |
| 925 | } |
| 926 | |
| 927 | for (i = 0; i < nb_domain; i++) { |
| 928 | switch (domains[i].type) { |
| 929 | case LTTNG_DOMAIN_KERNEL: |
| 930 | MSG("=== Domain: Kernel ===\n"); |
| 931 | break; |
| 932 | case LTTNG_DOMAIN_UST: |
| 933 | MSG("=== Domain: UST global ===\n"); |
| 934 | MSG("Buffer type: %s\n", |
| 935 | domains[i].buf_type == |
| 936 | LTTNG_BUFFER_PER_PID ? "per PID" : "per UID"); |
| 937 | break; |
| 938 | case LTTNG_DOMAIN_JUL: |
| 939 | MSG("=== Domain: JUL (Java Util Logging) ===\n"); |
| 940 | break; |
| 941 | default: |
| 942 | MSG("=== Domain: Unimplemented ===\n"); |
| 943 | break; |
| 944 | } |
| 945 | |
| 946 | /* Clean handle before creating a new one */ |
| 947 | if (handle) { |
| 948 | lttng_destroy_handle(handle); |
| 949 | } |
| 950 | |
| 951 | handle = lttng_create_handle(session_name, &domains[i]); |
| 952 | if (handle == NULL) { |
| 953 | ret = CMD_FATAL; |
| 954 | goto end; |
| 955 | } |
| 956 | |
| 957 | if (domains[i].type == LTTNG_DOMAIN_JUL) { |
| 958 | ret = list_session_jul_events(); |
| 959 | if (ret < 0) { |
| 960 | goto end; |
| 961 | } |
| 962 | continue; |
| 963 | } |
| 964 | |
| 965 | ret = list_channels(opt_channel); |
| 966 | if (ret < 0) { |
| 967 | goto end; |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | end: |
| 974 | free(domains); |
| 975 | if (handle) { |
| 976 | lttng_destroy_handle(handle); |
| 977 | } |
| 978 | |
| 979 | poptFreeContext(pc); |
| 980 | return ret; |
| 981 | } |