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