| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License, version 2 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #define _LGPL_SOURCE |
| 20 | #include <inttypes.h> |
| 21 | #include <popt.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <assert.h> |
| 26 | |
| 27 | #include <common/mi-lttng.h> |
| 28 | #include <lttng/constant.h> |
| 29 | |
| 30 | #include "../command.h" |
| 31 | |
| 32 | static int opt_userspace; |
| 33 | static int opt_kernel; |
| 34 | static int opt_jul; |
| 35 | static int opt_log4j; |
| 36 | static int opt_python; |
| 37 | static char *opt_channel; |
| 38 | static int opt_domain; |
| 39 | static int opt_fields; |
| 40 | static int opt_syscall; |
| 41 | |
| 42 | const char *indent4 = " "; |
| 43 | const char *indent6 = " "; |
| 44 | const char *indent8 = " "; |
| 45 | |
| 46 | enum { |
| 47 | OPT_HELP = 1, |
| 48 | OPT_USERSPACE, |
| 49 | OPT_LIST_OPTIONS, |
| 50 | }; |
| 51 | |
| 52 | static struct lttng_handle *handle; |
| 53 | static struct mi_writer *writer; |
| 54 | |
| 55 | static struct poptOption long_options[] = { |
| 56 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 57 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
| 58 | {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, |
| 59 | {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0}, |
| 60 | {"log4j", 'l', POPT_ARG_VAL, &opt_log4j, 1, 0, 0}, |
| 61 | {"python", 'p', POPT_ARG_VAL, &opt_python, 1, 0, 0}, |
| 62 | {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, |
| 63 | {"channel", 'c', POPT_ARG_STRING, &opt_channel, 0, 0, 0}, |
| 64 | {"domain", 'd', POPT_ARG_VAL, &opt_domain, 1, 0, 0}, |
| 65 | {"fields", 'f', POPT_ARG_VAL, &opt_fields, 1, 0, 0}, |
| 66 | {"syscall", 'S', POPT_ARG_VAL, &opt_syscall, 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, " -l, --log4j Apply for Java application using LOG4J\n"); |
| 89 | fprintf(ofp, " -p, --python Apply for Python application using logging\n"); |
| 90 | fprintf(ofp, " -f, --fields List event fields.\n"); |
| 91 | fprintf(ofp, " --syscall List available system calls.\n"); |
| 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 = NULL; |
| 109 | char *cmdline = NULL; |
| 110 | /* Can't go bigger than /proc/LTTNG_MAX_PID/cmdline */ |
| 111 | char path[sizeof("/proc//cmdline") + sizeof(LTTNG_MAX_PID_STR) - 1]; |
| 112 | |
| 113 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); |
| 114 | fp = fopen(path, "r"); |
| 115 | if (fp == NULL) { |
| 116 | goto end; |
| 117 | } |
| 118 | |
| 119 | /* Caller must free() *cmdline */ |
| 120 | cmdline = zmalloc(PATH_MAX); |
| 121 | if (!cmdline) { |
| 122 | PERROR("malloc cmdline"); |
| 123 | goto end; |
| 124 | } |
| 125 | ret = fread(cmdline, 1, PATH_MAX, fp); |
| 126 | if (ret < 0) { |
| 127 | PERROR("fread proc list"); |
| 128 | } |
| 129 | |
| 130 | end: |
| 131 | if (fp) { |
| 132 | fclose(fp); |
| 133 | } |
| 134 | return cmdline; |
| 135 | } |
| 136 | |
| 137 | static |
| 138 | const char *active_string(int value) |
| 139 | { |
| 140 | switch (value) { |
| 141 | case 0: return "inactive"; |
| 142 | case 1: return "active"; |
| 143 | case -1: return ""; |
| 144 | default: return NULL; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | static const char *snapshot_string(int value) |
| 149 | { |
| 150 | switch (value) { |
| 151 | case 1: |
| 152 | return " snapshot"; |
| 153 | default: |
| 154 | return ""; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | static |
| 159 | const char *enabled_string(int value) |
| 160 | { |
| 161 | switch (value) { |
| 162 | case 0: return " [disabled]"; |
| 163 | case 1: return " [enabled]"; |
| 164 | case -1: return ""; |
| 165 | default: return NULL; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | static |
| 170 | const char *filter_string(int value) |
| 171 | { |
| 172 | switch (value) { |
| 173 | case 1: return " [with filter]"; |
| 174 | default: return ""; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | static |
| 179 | const char *exclusion_string(int value) |
| 180 | { |
| 181 | switch (value) { |
| 182 | case 1: return " [has exclusions]"; |
| 183 | default: return ""; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | static const char *logleveltype_string(enum lttng_loglevel_type value) |
| 188 | { |
| 189 | switch (value) { |
| 190 | case LTTNG_EVENT_LOGLEVEL_ALL: |
| 191 | return ":"; |
| 192 | case LTTNG_EVENT_LOGLEVEL_RANGE: |
| 193 | return " <="; |
| 194 | case LTTNG_EVENT_LOGLEVEL_SINGLE: |
| 195 | return " =="; |
| 196 | default: |
| 197 | return " <<TYPE UNKN>>"; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | static const char *bitness_event(enum lttng_event_flag flags) |
| 202 | { |
| 203 | if (flags & LTTNG_EVENT_FLAG_SYSCALL_32) { |
| 204 | if (flags & LTTNG_EVENT_FLAG_SYSCALL_64) { |
| 205 | return " [32/64-bit]"; |
| 206 | } else { |
| 207 | return " [32-bit]"; |
| 208 | } |
| 209 | } else if (flags & LTTNG_EVENT_FLAG_SYSCALL_64) { |
| 210 | return " [64-bit]"; |
| 211 | } else { |
| 212 | return ""; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Pretty print single event. |
| 218 | */ |
| 219 | static void print_events(struct lttng_event *event) |
| 220 | { |
| 221 | switch (event->type) { |
| 222 | case LTTNG_EVENT_TRACEPOINT: |
| 223 | { |
| 224 | if (event->loglevel != -1) { |
| 225 | MSG("%s%s (loglevel%s %s (%d)) (type: tracepoint)%s%s%s", |
| 226 | indent6, |
| 227 | event->name, |
| 228 | logleveltype_string(event->loglevel_type), |
| 229 | mi_lttng_loglevel_string(event->loglevel, handle->domain.type), |
| 230 | event->loglevel, |
| 231 | enabled_string(event->enabled), |
| 232 | exclusion_string(event->exclusion), |
| 233 | filter_string(event->filter)); |
| 234 | } else { |
| 235 | MSG("%s%s (type: tracepoint)%s%s%s", |
| 236 | indent6, |
| 237 | event->name, |
| 238 | enabled_string(event->enabled), |
| 239 | exclusion_string(event->exclusion), |
| 240 | filter_string(event->filter)); |
| 241 | } |
| 242 | break; |
| 243 | } |
| 244 | case LTTNG_EVENT_FUNCTION: |
| 245 | MSG("%s%s (type: function)%s%s", indent6, |
| 246 | event->name, enabled_string(event->enabled), |
| 247 | filter_string(event->filter)); |
| 248 | if (event->attr.probe.addr != 0) { |
| 249 | MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr); |
| 250 | } else { |
| 251 | MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset); |
| 252 | MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name); |
| 253 | } |
| 254 | break; |
| 255 | case LTTNG_EVENT_PROBE: |
| 256 | MSG("%s%s (type: probe)%s%s", indent6, |
| 257 | event->name, enabled_string(event->enabled), |
| 258 | filter_string(event->filter)); |
| 259 | if (event->attr.probe.addr != 0) { |
| 260 | MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr); |
| 261 | } else { |
| 262 | MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset); |
| 263 | MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name); |
| 264 | } |
| 265 | break; |
| 266 | case LTTNG_EVENT_FUNCTION_ENTRY: |
| 267 | MSG("%s%s (type: function)%s%s", indent6, |
| 268 | event->name, enabled_string(event->enabled), |
| 269 | filter_string(event->filter)); |
| 270 | MSG("%ssymbol: \"%s\"", indent8, event->attr.ftrace.symbol_name); |
| 271 | break; |
| 272 | case LTTNG_EVENT_SYSCALL: |
| 273 | MSG("%s%s%s%s%s", indent6, event->name, |
| 274 | (opt_syscall ? "" : " (type:syscall)"), |
| 275 | enabled_string(event->enabled), |
| 276 | bitness_event(event->flags)); |
| 277 | break; |
| 278 | case LTTNG_EVENT_NOOP: |
| 279 | MSG("%s (type: noop)%s%s", indent6, |
| 280 | enabled_string(event->enabled), |
| 281 | filter_string(event->filter)); |
| 282 | break; |
| 283 | case LTTNG_EVENT_ALL: |
| 284 | /* We should never have "all" events in list. */ |
| 285 | assert(0); |
| 286 | break; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | static const char *field_type(struct lttng_event_field *field) |
| 291 | { |
| 292 | switch(field->type) { |
| 293 | case LTTNG_EVENT_FIELD_INTEGER: |
| 294 | return "integer"; |
| 295 | case LTTNG_EVENT_FIELD_ENUM: |
| 296 | return "enum"; |
| 297 | case LTTNG_EVENT_FIELD_FLOAT: |
| 298 | return "float"; |
| 299 | case LTTNG_EVENT_FIELD_STRING: |
| 300 | return "string"; |
| 301 | case LTTNG_EVENT_FIELD_OTHER: |
| 302 | default: /* fall-through */ |
| 303 | return "unknown"; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Pretty print single event fields. |
| 309 | */ |
| 310 | static void print_event_field(struct lttng_event_field *field) |
| 311 | { |
| 312 | if (!field->field_name[0]) { |
| 313 | return; |
| 314 | } |
| 315 | MSG("%sfield: %s (%s)%s", indent8, field->field_name, |
| 316 | field_type(field), field->nowrite ? " [no write]" : ""); |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * Machine interface |
| 321 | * Jul and ust event listing |
| 322 | */ |
| 323 | static int mi_list_agent_ust_events(struct lttng_event *events, int count, |
| 324 | struct lttng_domain *domain) |
| 325 | { |
| 326 | int ret, i; |
| 327 | pid_t cur_pid = 0; |
| 328 | char *cmdline = NULL; |
| 329 | int pid_element_open = 0; |
| 330 | |
| 331 | /* Open domains element */ |
| 332 | ret = mi_lttng_domains_open(writer); |
| 333 | if (ret) { |
| 334 | goto end; |
| 335 | } |
| 336 | |
| 337 | /* Write domain */ |
| 338 | ret = mi_lttng_domain(writer, domain, 1); |
| 339 | if (ret) { |
| 340 | goto end; |
| 341 | } |
| 342 | |
| 343 | /* Open pids element */ |
| 344 | ret = mi_lttng_pids_open(writer); |
| 345 | if (ret) { |
| 346 | goto end; |
| 347 | } |
| 348 | |
| 349 | for (i = 0; i < count; i++) { |
| 350 | if (cur_pid != events[i].pid) { |
| 351 | if (pid_element_open) { |
| 352 | /* Close the previous events and pid element */ |
| 353 | ret = mi_lttng_close_multi_element(writer, 2); |
| 354 | if (ret) { |
| 355 | goto end; |
| 356 | } |
| 357 | pid_element_open = 0; |
| 358 | } |
| 359 | |
| 360 | cur_pid = events[i].pid; |
| 361 | cmdline = get_cmdline_by_pid(cur_pid); |
| 362 | if (!cmdline) { |
| 363 | ret = CMD_ERROR; |
| 364 | goto end; |
| 365 | } |
| 366 | |
| 367 | if (!pid_element_open) { |
| 368 | /* Open and write a pid element */ |
| 369 | ret = mi_lttng_pid(writer, cur_pid, cmdline, 1); |
| 370 | if (ret) { |
| 371 | goto error; |
| 372 | } |
| 373 | |
| 374 | /* Open events element */ |
| 375 | ret = mi_lttng_events_open(writer); |
| 376 | if (ret) { |
| 377 | goto error; |
| 378 | } |
| 379 | |
| 380 | pid_element_open = 1; |
| 381 | } |
| 382 | free(cmdline); |
| 383 | } |
| 384 | |
| 385 | /* Write an event */ |
| 386 | ret = mi_lttng_event(writer, &events[i], 0, handle->domain.type); |
| 387 | if (ret) { |
| 388 | goto end; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /* Close pids */ |
| 393 | ret = mi_lttng_writer_close_element(writer); |
| 394 | if (ret) { |
| 395 | goto end; |
| 396 | } |
| 397 | |
| 398 | /* Close domain, domains */ |
| 399 | ret = mi_lttng_close_multi_element(writer, 2); |
| 400 | end: |
| 401 | return ret; |
| 402 | error: |
| 403 | free(cmdline); |
| 404 | return ret; |
| 405 | } |
| 406 | |
| 407 | static int list_agent_events(void) |
| 408 | { |
| 409 | int i, size, ret = CMD_SUCCESS; |
| 410 | struct lttng_domain domain; |
| 411 | struct lttng_handle *handle = NULL; |
| 412 | struct lttng_event *event_list = NULL; |
| 413 | pid_t cur_pid = 0; |
| 414 | char *cmdline = NULL; |
| 415 | const char *agent_domain_str; |
| 416 | |
| 417 | memset(&domain, 0, sizeof(domain)); |
| 418 | if (opt_jul) { |
| 419 | domain.type = LTTNG_DOMAIN_JUL; |
| 420 | } else if (opt_log4j) { |
| 421 | domain.type = LTTNG_DOMAIN_LOG4J; |
| 422 | } else if (opt_python) { |
| 423 | domain.type = LTTNG_DOMAIN_PYTHON; |
| 424 | } else { |
| 425 | ERR("Invalid agent domain selected."); |
| 426 | ret = CMD_ERROR; |
| 427 | goto error; |
| 428 | } |
| 429 | |
| 430 | agent_domain_str = get_domain_str(domain.type); |
| 431 | |
| 432 | DBG("Getting %s tracing events", agent_domain_str); |
| 433 | |
| 434 | handle = lttng_create_handle(NULL, &domain); |
| 435 | if (handle == NULL) { |
| 436 | ret = CMD_ERROR; |
| 437 | goto end; |
| 438 | } |
| 439 | |
| 440 | size = lttng_list_tracepoints(handle, &event_list); |
| 441 | if (size < 0) { |
| 442 | ERR("Unable to list %s events: %s", agent_domain_str, |
| 443 | lttng_strerror(size)); |
| 444 | ret = CMD_ERROR; |
| 445 | goto end; |
| 446 | } |
| 447 | |
| 448 | if (lttng_opt_mi) { |
| 449 | /* Mi print */ |
| 450 | ret = mi_list_agent_ust_events(event_list, size, &domain); |
| 451 | if (ret) { |
| 452 | ret = CMD_ERROR; |
| 453 | goto error; |
| 454 | } |
| 455 | } else { |
| 456 | /* Pretty print */ |
| 457 | MSG("%s events (Logger name):\n-------------------------", |
| 458 | agent_domain_str); |
| 459 | |
| 460 | if (size == 0) { |
| 461 | MSG("None"); |
| 462 | } |
| 463 | |
| 464 | for (i = 0; i < size; i++) { |
| 465 | if (cur_pid != event_list[i].pid) { |
| 466 | cur_pid = event_list[i].pid; |
| 467 | cmdline = get_cmdline_by_pid(cur_pid); |
| 468 | if (cmdline == NULL) { |
| 469 | ret = CMD_ERROR; |
| 470 | goto error; |
| 471 | } |
| 472 | MSG("\nPID: %d - Name: %s", cur_pid, cmdline); |
| 473 | free(cmdline); |
| 474 | } |
| 475 | MSG("%s- %s", indent6, event_list[i].name); |
| 476 | } |
| 477 | |
| 478 | MSG(""); |
| 479 | } |
| 480 | |
| 481 | error: |
| 482 | free(event_list); |
| 483 | end: |
| 484 | lttng_destroy_handle(handle); |
| 485 | return ret; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * Ask session daemon for all user space tracepoints available. |
| 490 | */ |
| 491 | static int list_ust_events(void) |
| 492 | { |
| 493 | int i, size, ret = CMD_SUCCESS; |
| 494 | struct lttng_domain domain; |
| 495 | struct lttng_handle *handle; |
| 496 | struct lttng_event *event_list; |
| 497 | pid_t cur_pid = 0; |
| 498 | char *cmdline = NULL; |
| 499 | |
| 500 | memset(&domain, 0, sizeof(domain)); |
| 501 | |
| 502 | DBG("Getting UST tracing events"); |
| 503 | |
| 504 | domain.type = LTTNG_DOMAIN_UST; |
| 505 | |
| 506 | handle = lttng_create_handle(NULL, &domain); |
| 507 | if (handle == NULL) { |
| 508 | ret = CMD_ERROR; |
| 509 | goto end; |
| 510 | } |
| 511 | |
| 512 | size = lttng_list_tracepoints(handle, &event_list); |
| 513 | if (size < 0) { |
| 514 | ERR("Unable to list UST events: %s", lttng_strerror(size)); |
| 515 | ret = CMD_ERROR; |
| 516 | goto error; |
| 517 | } |
| 518 | |
| 519 | if (lttng_opt_mi) { |
| 520 | /* Mi print */ |
| 521 | ret = mi_list_agent_ust_events(event_list, size, &domain); |
| 522 | } else { |
| 523 | /* Pretty print */ |
| 524 | MSG("UST events:\n-------------"); |
| 525 | |
| 526 | if (size == 0) { |
| 527 | MSG("None"); |
| 528 | } |
| 529 | |
| 530 | for (i = 0; i < size; i++) { |
| 531 | if (cur_pid != event_list[i].pid) { |
| 532 | cur_pid = event_list[i].pid; |
| 533 | cmdline = get_cmdline_by_pid(cur_pid); |
| 534 | if (cmdline == NULL) { |
| 535 | ret = CMD_ERROR; |
| 536 | goto error; |
| 537 | } |
| 538 | MSG("\nPID: %d - Name: %s", cur_pid, cmdline); |
| 539 | free(cmdline); |
| 540 | } |
| 541 | print_events(&event_list[i]); |
| 542 | } |
| 543 | |
| 544 | MSG(""); |
| 545 | } |
| 546 | |
| 547 | error: |
| 548 | free(event_list); |
| 549 | end: |
| 550 | lttng_destroy_handle(handle); |
| 551 | return ret; |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | * Machine interface |
| 556 | * List all ust event with their fields |
| 557 | */ |
| 558 | static int mi_list_ust_event_fields(struct lttng_event_field *fields, int count, |
| 559 | struct lttng_domain *domain) |
| 560 | { |
| 561 | int ret, i; |
| 562 | pid_t cur_pid = 0; |
| 563 | char *cmdline = NULL; |
| 564 | int pid_element_open = 0; |
| 565 | int event_element_open = 0; |
| 566 | struct lttng_event cur_event; |
| 567 | |
| 568 | /* Open domains element */ |
| 569 | ret = mi_lttng_domains_open(writer); |
| 570 | if (ret) { |
| 571 | goto end; |
| 572 | } |
| 573 | |
| 574 | /* Write domain */ |
| 575 | ret = mi_lttng_domain(writer, domain, 1); |
| 576 | if (ret) { |
| 577 | goto end; |
| 578 | } |
| 579 | |
| 580 | /* Open pids element */ |
| 581 | ret = mi_lttng_pids_open(writer); |
| 582 | if (ret) { |
| 583 | goto end; |
| 584 | } |
| 585 | |
| 586 | for (i = 0; i < count; i++) { |
| 587 | if (cur_pid != fields[i].event.pid) { |
| 588 | if (pid_element_open) { |
| 589 | if (event_element_open) { |
| 590 | |
| 591 | /* Close the previous field element and event. */ |
| 592 | ret = mi_lttng_close_multi_element(writer, 2); |
| 593 | if (ret) { |
| 594 | goto end; |
| 595 | } |
| 596 | event_element_open = 0; |
| 597 | } |
| 598 | /* Close the previous events, pid element */ |
| 599 | ret = mi_lttng_close_multi_element(writer, 2); |
| 600 | if (ret) { |
| 601 | goto end; |
| 602 | } |
| 603 | pid_element_open = 0; |
| 604 | } |
| 605 | |
| 606 | cur_pid = fields[i].event.pid; |
| 607 | cmdline = get_cmdline_by_pid(cur_pid); |
| 608 | if (!pid_element_open) { |
| 609 | /* Open and write a pid element */ |
| 610 | ret = mi_lttng_pid(writer, cur_pid, cmdline, 1); |
| 611 | if (ret) { |
| 612 | goto error; |
| 613 | } |
| 614 | |
| 615 | /* Open events element */ |
| 616 | ret = mi_lttng_events_open(writer); |
| 617 | if (ret) { |
| 618 | goto error; |
| 619 | } |
| 620 | pid_element_open = 1; |
| 621 | } |
| 622 | free(cmdline); |
| 623 | /* Wipe current event since we are about to print a new PID. */ |
| 624 | memset(&cur_event, 0, sizeof(cur_event)); |
| 625 | } |
| 626 | |
| 627 | if (strcmp(cur_event.name, fields[i].event.name) != 0) { |
| 628 | if (event_element_open) { |
| 629 | /* Close the previous fields element and the previous event */ |
| 630 | ret = mi_lttng_close_multi_element(writer, 2); |
| 631 | if (ret) { |
| 632 | goto end; |
| 633 | } |
| 634 | event_element_open = 0; |
| 635 | } |
| 636 | |
| 637 | memcpy(&cur_event, &fields[i].event, |
| 638 | sizeof(cur_event)); |
| 639 | |
| 640 | if (!event_element_open) { |
| 641 | /* Open and write the event */ |
| 642 | ret = mi_lttng_event(writer, &cur_event, 1, |
| 643 | handle->domain.type); |
| 644 | if (ret) { |
| 645 | goto end; |
| 646 | } |
| 647 | |
| 648 | /* Open a fields element */ |
| 649 | ret = mi_lttng_event_fields_open(writer); |
| 650 | if (ret) { |
| 651 | goto end; |
| 652 | } |
| 653 | event_element_open = 1; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | /* Print the event_field */ |
| 658 | ret = mi_lttng_event_field(writer, &fields[i]); |
| 659 | if (ret) { |
| 660 | goto end; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | /* Close pids, domain, domains */ |
| 665 | ret = mi_lttng_close_multi_element(writer, 3); |
| 666 | end: |
| 667 | return ret; |
| 668 | error: |
| 669 | free(cmdline); |
| 670 | return ret; |
| 671 | } |
| 672 | |
| 673 | /* |
| 674 | * Ask session daemon for all user space tracepoint fields available. |
| 675 | */ |
| 676 | static int list_ust_event_fields(void) |
| 677 | { |
| 678 | int i, size, ret = CMD_SUCCESS; |
| 679 | struct lttng_domain domain; |
| 680 | struct lttng_handle *handle; |
| 681 | struct lttng_event_field *event_field_list; |
| 682 | pid_t cur_pid = 0; |
| 683 | char *cmdline = NULL; |
| 684 | |
| 685 | struct lttng_event cur_event; |
| 686 | |
| 687 | memset(&domain, 0, sizeof(domain)); |
| 688 | memset(&cur_event, 0, sizeof(cur_event)); |
| 689 | |
| 690 | DBG("Getting UST tracing event fields"); |
| 691 | |
| 692 | domain.type = LTTNG_DOMAIN_UST; |
| 693 | |
| 694 | handle = lttng_create_handle(NULL, &domain); |
| 695 | if (handle == NULL) { |
| 696 | ret = CMD_ERROR; |
| 697 | goto end; |
| 698 | } |
| 699 | |
| 700 | size = lttng_list_tracepoint_fields(handle, &event_field_list); |
| 701 | if (size < 0) { |
| 702 | ERR("Unable to list UST event fields: %s", lttng_strerror(size)); |
| 703 | ret = CMD_ERROR; |
| 704 | goto end; |
| 705 | } |
| 706 | |
| 707 | if (lttng_opt_mi) { |
| 708 | /* Mi print */ |
| 709 | ret = mi_list_ust_event_fields(event_field_list, size, &domain); |
| 710 | if (ret) { |
| 711 | ret = CMD_ERROR; |
| 712 | goto error; |
| 713 | } |
| 714 | } else { |
| 715 | /* Pretty print */ |
| 716 | MSG("UST events:\n-------------"); |
| 717 | |
| 718 | if (size == 0) { |
| 719 | MSG("None"); |
| 720 | } |
| 721 | |
| 722 | for (i = 0; i < size; i++) { |
| 723 | if (cur_pid != event_field_list[i].event.pid) { |
| 724 | cur_pid = event_field_list[i].event.pid; |
| 725 | cmdline = get_cmdline_by_pid(cur_pid); |
| 726 | if (cmdline == NULL) { |
| 727 | ret = CMD_ERROR; |
| 728 | goto error; |
| 729 | } |
| 730 | MSG("\nPID: %d - Name: %s", cur_pid, cmdline); |
| 731 | free(cmdline); |
| 732 | /* Wipe current event since we are about to print a new PID. */ |
| 733 | memset(&cur_event, 0, sizeof(cur_event)); |
| 734 | } |
| 735 | if (strcmp(cur_event.name, event_field_list[i].event.name) != 0) { |
| 736 | print_events(&event_field_list[i].event); |
| 737 | memcpy(&cur_event, &event_field_list[i].event, |
| 738 | sizeof(cur_event)); |
| 739 | } |
| 740 | print_event_field(&event_field_list[i]); |
| 741 | } |
| 742 | |
| 743 | MSG(""); |
| 744 | } |
| 745 | |
| 746 | error: |
| 747 | free(event_field_list); |
| 748 | end: |
| 749 | lttng_destroy_handle(handle); |
| 750 | return ret; |
| 751 | } |
| 752 | |
| 753 | /* |
| 754 | * Machine interface |
| 755 | * Print a list of kernel events |
| 756 | */ |
| 757 | static int mi_list_kernel_events(struct lttng_event *events, int count, |
| 758 | struct lttng_domain *domain) |
| 759 | { |
| 760 | int ret, i; |
| 761 | |
| 762 | /* Open domains element */ |
| 763 | ret = mi_lttng_domains_open(writer); |
| 764 | if (ret) { |
| 765 | goto end; |
| 766 | } |
| 767 | |
| 768 | /* Write domain */ |
| 769 | ret = mi_lttng_domain(writer, domain, 1); |
| 770 | if (ret) { |
| 771 | goto end; |
| 772 | } |
| 773 | |
| 774 | /* Open events */ |
| 775 | ret = mi_lttng_events_open(writer); |
| 776 | if (ret) { |
| 777 | goto end; |
| 778 | } |
| 779 | |
| 780 | for (i = 0; i < count; i++) { |
| 781 | ret = mi_lttng_event(writer, &events[i], 0, handle->domain.type); |
| 782 | if (ret) { |
| 783 | goto end; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | /* close events, domain and domains */ |
| 788 | ret = mi_lttng_close_multi_element(writer, 3); |
| 789 | if (ret) { |
| 790 | goto end; |
| 791 | } |
| 792 | |
| 793 | end: |
| 794 | return ret; |
| 795 | } |
| 796 | |
| 797 | /* |
| 798 | * Ask for all trace events in the kernel |
| 799 | */ |
| 800 | static int list_kernel_events(void) |
| 801 | { |
| 802 | int i, size, ret = CMD_SUCCESS; |
| 803 | struct lttng_domain domain; |
| 804 | struct lttng_handle *handle; |
| 805 | struct lttng_event *event_list; |
| 806 | |
| 807 | memset(&domain, 0, sizeof(domain)); |
| 808 | |
| 809 | DBG("Getting kernel tracing events"); |
| 810 | |
| 811 | domain.type = LTTNG_DOMAIN_KERNEL; |
| 812 | |
| 813 | handle = lttng_create_handle(NULL, &domain); |
| 814 | if (handle == NULL) { |
| 815 | ret = CMD_ERROR; |
| 816 | goto error; |
| 817 | } |
| 818 | |
| 819 | size = lttng_list_tracepoints(handle, &event_list); |
| 820 | if (size < 0) { |
| 821 | ERR("Unable to list kernel events: %s", lttng_strerror(size)); |
| 822 | lttng_destroy_handle(handle); |
| 823 | return CMD_ERROR; |
| 824 | } |
| 825 | |
| 826 | if (lttng_opt_mi) { |
| 827 | /* Mi print */ |
| 828 | ret = mi_list_kernel_events(event_list, size, &domain); |
| 829 | if (ret) { |
| 830 | ret = CMD_ERROR; |
| 831 | goto end; |
| 832 | } |
| 833 | } else { |
| 834 | MSG("Kernel events:\n-------------"); |
| 835 | |
| 836 | for (i = 0; i < size; i++) { |
| 837 | print_events(&event_list[i]); |
| 838 | } |
| 839 | |
| 840 | MSG(""); |
| 841 | } |
| 842 | |
| 843 | end: |
| 844 | free(event_list); |
| 845 | |
| 846 | lttng_destroy_handle(handle); |
| 847 | return ret; |
| 848 | |
| 849 | error: |
| 850 | lttng_destroy_handle(handle); |
| 851 | return ret; |
| 852 | } |
| 853 | |
| 854 | /* |
| 855 | * Machine interface |
| 856 | * Print a list of system calls. |
| 857 | */ |
| 858 | static int mi_list_syscalls(struct lttng_event *events, int count) |
| 859 | { |
| 860 | int ret, i; |
| 861 | |
| 862 | /* Open events */ |
| 863 | ret = mi_lttng_events_open(writer); |
| 864 | if (ret) { |
| 865 | goto end; |
| 866 | } |
| 867 | |
| 868 | for (i = 0; i < count; i++) { |
| 869 | ret = mi_lttng_event(writer, &events[i], 0, handle->domain.type); |
| 870 | if (ret) { |
| 871 | goto end; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | /* Close events. */ |
| 876 | ret = mi_lttng_writer_close_element(writer); |
| 877 | if (ret) { |
| 878 | goto end; |
| 879 | } |
| 880 | |
| 881 | end: |
| 882 | return ret; |
| 883 | } |
| 884 | |
| 885 | /* |
| 886 | * Ask for kernel system calls. |
| 887 | */ |
| 888 | static int list_syscalls(void) |
| 889 | { |
| 890 | int i, size, ret = CMD_SUCCESS; |
| 891 | struct lttng_event *event_list; |
| 892 | |
| 893 | DBG("Getting kernel system call events"); |
| 894 | |
| 895 | size = lttng_list_syscalls(&event_list); |
| 896 | if (size < 0) { |
| 897 | ERR("Unable to list system calls: %s", lttng_strerror(size)); |
| 898 | ret = CMD_ERROR; |
| 899 | goto error; |
| 900 | } |
| 901 | |
| 902 | if (lttng_opt_mi) { |
| 903 | /* Mi print */ |
| 904 | ret = mi_list_syscalls(event_list, size); |
| 905 | if (ret) { |
| 906 | ret = CMD_ERROR; |
| 907 | goto end; |
| 908 | } |
| 909 | } else { |
| 910 | MSG("System calls:\n-------------"); |
| 911 | |
| 912 | for (i = 0; i < size; i++) { |
| 913 | print_events(&event_list[i]); |
| 914 | } |
| 915 | |
| 916 | MSG(""); |
| 917 | } |
| 918 | |
| 919 | end: |
| 920 | free(event_list); |
| 921 | return ret; |
| 922 | |
| 923 | error: |
| 924 | return ret; |
| 925 | } |
| 926 | |
| 927 | /* |
| 928 | * Machine Interface |
| 929 | * Print a list of agent events |
| 930 | */ |
| 931 | static int mi_list_session_agent_events(struct lttng_event *events, int count) |
| 932 | { |
| 933 | int ret, i; |
| 934 | |
| 935 | /* Open events element */ |
| 936 | ret = mi_lttng_events_open(writer); |
| 937 | if (ret) { |
| 938 | goto end; |
| 939 | } |
| 940 | |
| 941 | for (i = 0; i < count; i++) { |
| 942 | ret = mi_lttng_event(writer, &events[i], 0, handle->domain.type); |
| 943 | if (ret) { |
| 944 | goto end; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | /* Close events element */ |
| 949 | ret = mi_lttng_writer_close_element(writer); |
| 950 | |
| 951 | end: |
| 952 | return ret; |
| 953 | } |
| 954 | |
| 955 | /* |
| 956 | * List agent events for a specific session using the handle. |
| 957 | * |
| 958 | * Return CMD_SUCCESS on success else a negative value. |
| 959 | */ |
| 960 | static int list_session_agent_events(void) |
| 961 | { |
| 962 | int ret = CMD_SUCCESS, count, i; |
| 963 | struct lttng_event *events = NULL; |
| 964 | |
| 965 | count = lttng_list_events(handle, "", &events); |
| 966 | if (count < 0) { |
| 967 | ret = CMD_ERROR; |
| 968 | ERR("%s", lttng_strerror(count)); |
| 969 | goto error; |
| 970 | } |
| 971 | |
| 972 | if (lttng_opt_mi) { |
| 973 | /* Mi print */ |
| 974 | ret = mi_list_session_agent_events(events, count); |
| 975 | if (ret) { |
| 976 | ret = CMD_ERROR; |
| 977 | goto end; |
| 978 | } |
| 979 | } else { |
| 980 | /* Pretty print */ |
| 981 | MSG("Events (Logger name):\n---------------------"); |
| 982 | if (count == 0) { |
| 983 | MSG("%sNone\n", indent6); |
| 984 | goto end; |
| 985 | } |
| 986 | |
| 987 | for (i = 0; i < count; i++) { |
| 988 | MSG("%s- %s%s (loglevel%s %s)", indent4, events[i].name, |
| 989 | enabled_string(events[i].enabled), |
| 990 | logleveltype_string(events[i].loglevel_type), |
| 991 | mi_lttng_loglevel_string(events[i].loglevel, |
| 992 | handle->domain.type)); |
| 993 | } |
| 994 | |
| 995 | MSG(""); |
| 996 | } |
| 997 | |
| 998 | end: |
| 999 | free(events); |
| 1000 | error: |
| 1001 | return ret; |
| 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | * Machine interface |
| 1006 | * print a list of event |
| 1007 | */ |
| 1008 | static int mi_list_events(struct lttng_event *events, int count) |
| 1009 | { |
| 1010 | int ret, i; |
| 1011 | |
| 1012 | /* Open events element */ |
| 1013 | ret = mi_lttng_events_open(writer); |
| 1014 | if (ret) { |
| 1015 | goto end; |
| 1016 | } |
| 1017 | |
| 1018 | for (i = 0; i < count; i++) { |
| 1019 | ret = mi_lttng_event(writer, &events[i], 0, handle->domain.type); |
| 1020 | if (ret) { |
| 1021 | goto end; |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | /* Close events element */ |
| 1026 | ret = mi_lttng_writer_close_element(writer); |
| 1027 | |
| 1028 | end: |
| 1029 | return ret; |
| 1030 | } |
| 1031 | |
| 1032 | /* |
| 1033 | * List events of channel of session and domain. |
| 1034 | */ |
| 1035 | static int list_events(const char *channel_name) |
| 1036 | { |
| 1037 | int ret = CMD_SUCCESS, count, i; |
| 1038 | struct lttng_event *events = NULL; |
| 1039 | |
| 1040 | count = lttng_list_events(handle, channel_name, &events); |
| 1041 | if (count < 0) { |
| 1042 | ret = CMD_ERROR; |
| 1043 | ERR("%s", lttng_strerror(count)); |
| 1044 | goto error; |
| 1045 | } |
| 1046 | |
| 1047 | if (lttng_opt_mi) { |
| 1048 | /* Mi print */ |
| 1049 | ret = mi_list_events(events, count); |
| 1050 | if (ret) { |
| 1051 | ret = CMD_ERROR; |
| 1052 | goto end; |
| 1053 | } |
| 1054 | } else { |
| 1055 | /* Pretty print */ |
| 1056 | MSG("\n%sEvents:", indent4); |
| 1057 | if (count == 0) { |
| 1058 | MSG("%sNone\n", indent6); |
| 1059 | goto end; |
| 1060 | } |
| 1061 | |
| 1062 | for (i = 0; i < count; i++) { |
| 1063 | print_events(&events[i]); |
| 1064 | } |
| 1065 | |
| 1066 | MSG(""); |
| 1067 | } |
| 1068 | end: |
| 1069 | free(events); |
| 1070 | error: |
| 1071 | return ret; |
| 1072 | } |
| 1073 | |
| 1074 | /* |
| 1075 | * Pretty print channel |
| 1076 | */ |
| 1077 | static void print_channel(struct lttng_channel *channel) |
| 1078 | { |
| 1079 | MSG("- %s:%s\n", channel->name, enabled_string(channel->enabled)); |
| 1080 | |
| 1081 | MSG("%sAttributes:", indent4); |
| 1082 | MSG("%soverwrite mode: %d", indent6, channel->attr.overwrite); |
| 1083 | MSG("%ssubbufers size: %" PRIu64, indent6, channel->attr.subbuf_size); |
| 1084 | MSG("%snumber of subbufers: %" PRIu64, indent6, channel->attr.num_subbuf); |
| 1085 | MSG("%sswitch timer interval: %u", indent6, channel->attr.switch_timer_interval); |
| 1086 | MSG("%sread timer interval: %u", indent6, channel->attr.read_timer_interval); |
| 1087 | MSG("%strace file count: %" PRIu64, indent6, channel->attr.tracefile_count); |
| 1088 | MSG("%strace file size (bytes): %" PRIu64, indent6, channel->attr.tracefile_size); |
| 1089 | switch (channel->attr.output) { |
| 1090 | case LTTNG_EVENT_SPLICE: |
| 1091 | MSG("%soutput: splice()", indent6); |
| 1092 | break; |
| 1093 | case LTTNG_EVENT_MMAP: |
| 1094 | MSG("%soutput: mmap()", indent6); |
| 1095 | break; |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | /* |
| 1100 | * Machine interface |
| 1101 | * Print a list of channel |
| 1102 | * |
| 1103 | */ |
| 1104 | static int mi_list_channels(struct lttng_channel *channels, int count, |
| 1105 | const char *channel_name) |
| 1106 | { |
| 1107 | int i, ret; |
| 1108 | unsigned int chan_found = 0; |
| 1109 | |
| 1110 | /* Open channels element */ |
| 1111 | ret = mi_lttng_channels_open(writer); |
| 1112 | if (ret) { |
| 1113 | goto error; |
| 1114 | } |
| 1115 | |
| 1116 | for (i = 0; i < count; i++) { |
| 1117 | if (channel_name != NULL) { |
| 1118 | if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) { |
| 1119 | chan_found = 1; |
| 1120 | } else { |
| 1121 | continue; |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | /* Write channel element and leave it open */ |
| 1126 | ret = mi_lttng_channel(writer, &channels[i], 1); |
| 1127 | if (ret) { |
| 1128 | goto error; |
| 1129 | } |
| 1130 | |
| 1131 | /* Listing events per channel */ |
| 1132 | ret = list_events(channels[i].name); |
| 1133 | if (ret) { |
| 1134 | goto error; |
| 1135 | } |
| 1136 | |
| 1137 | /* Closing the channel element we opened earlier */ |
| 1138 | ret = mi_lttng_writer_close_element(writer); |
| 1139 | if (ret) { |
| 1140 | goto error; |
| 1141 | } |
| 1142 | |
| 1143 | if (chan_found) { |
| 1144 | break; |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | /* Close channels element */ |
| 1149 | ret = mi_lttng_writer_close_element(writer); |
| 1150 | if (ret) { |
| 1151 | goto error; |
| 1152 | } |
| 1153 | |
| 1154 | error: |
| 1155 | return ret; |
| 1156 | } |
| 1157 | |
| 1158 | /* |
| 1159 | * List channel(s) of session and domain. |
| 1160 | * |
| 1161 | * If channel_name is NULL, all channels are listed. |
| 1162 | */ |
| 1163 | static int list_channels(const char *channel_name) |
| 1164 | { |
| 1165 | int count, i, ret = CMD_SUCCESS; |
| 1166 | unsigned int chan_found = 0; |
| 1167 | struct lttng_channel *channels = NULL; |
| 1168 | |
| 1169 | DBG("Listing channel(s) (%s)", channel_name ? : "<all>"); |
| 1170 | |
| 1171 | count = lttng_list_channels(handle, &channels); |
| 1172 | if (count < 0) { |
| 1173 | switch (-count) { |
| 1174 | case LTTNG_ERR_KERN_CHAN_NOT_FOUND: |
| 1175 | if (lttng_opt_mi) { |
| 1176 | /* When printing mi this is not an error |
| 1177 | * but an empty channels element */ |
| 1178 | count = 0; |
| 1179 | } else { |
| 1180 | ret = CMD_SUCCESS; |
| 1181 | WARN("No kernel channel"); |
| 1182 | goto error_channels; |
| 1183 | } |
| 1184 | break; |
| 1185 | default: |
| 1186 | /* We had a real error */ |
| 1187 | ret = CMD_ERROR; |
| 1188 | ERR("%s", lttng_strerror(count)); |
| 1189 | goto error_channels; |
| 1190 | break; |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | if (lttng_opt_mi) { |
| 1195 | /* Mi print */ |
| 1196 | ret = mi_list_channels(channels, count, channel_name); |
| 1197 | if (ret) { |
| 1198 | ret = CMD_ERROR; |
| 1199 | goto error; |
| 1200 | } |
| 1201 | } else { |
| 1202 | /* Pretty print */ |
| 1203 | if (count) { |
| 1204 | MSG("Channels:\n-------------"); |
| 1205 | } |
| 1206 | |
| 1207 | for (i = 0; i < count; i++) { |
| 1208 | if (channel_name != NULL) { |
| 1209 | if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) { |
| 1210 | chan_found = 1; |
| 1211 | } else { |
| 1212 | continue; |
| 1213 | } |
| 1214 | } |
| 1215 | print_channel(&channels[i]); |
| 1216 | |
| 1217 | /* Listing events per channel */ |
| 1218 | ret = list_events(channels[i].name); |
| 1219 | if (ret) { |
| 1220 | goto error; |
| 1221 | } |
| 1222 | |
| 1223 | if (chan_found) { |
| 1224 | break; |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | if (!chan_found && channel_name != NULL) { |
| 1229 | ret = CMD_ERROR; |
| 1230 | ERR("Channel %s not found", channel_name); |
| 1231 | goto error; |
| 1232 | } |
| 1233 | } |
| 1234 | error: |
| 1235 | free(channels); |
| 1236 | |
| 1237 | error_channels: |
| 1238 | return ret; |
| 1239 | } |
| 1240 | |
| 1241 | /* |
| 1242 | * List tracker PID(s) of session and domain. |
| 1243 | */ |
| 1244 | static int list_tracker_pids(void) |
| 1245 | { |
| 1246 | int enabled, ret; |
| 1247 | int *pids = NULL; |
| 1248 | size_t nr_pids; |
| 1249 | |
| 1250 | ret = lttng_list_tracker_pids(handle, |
| 1251 | &enabled, &pids, &nr_pids); |
| 1252 | if (ret) { |
| 1253 | return ret; |
| 1254 | } |
| 1255 | if (enabled) { |
| 1256 | int i; |
| 1257 | _MSG("PID tracker: ["); |
| 1258 | |
| 1259 | for (i = 0; i < nr_pids; i++) { |
| 1260 | if (i) { |
| 1261 | _MSG(","); |
| 1262 | } |
| 1263 | _MSG(" %d", pids[i]); |
| 1264 | } |
| 1265 | _MSG(" ]\n\n"); |
| 1266 | } |
| 1267 | free(pids); |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
| 1271 | /* |
| 1272 | * Machine interface |
| 1273 | * Find the session with session_name as name |
| 1274 | * and print his informations. |
| 1275 | */ |
| 1276 | static int mi_list_session(const char *session_name, |
| 1277 | struct lttng_session *sessions, int count) |
| 1278 | { |
| 1279 | int ret, i; |
| 1280 | unsigned int session_found = 0; |
| 1281 | |
| 1282 | if (session_name == NULL) { |
| 1283 | ret = -LTTNG_ERR_SESS_NOT_FOUND; |
| 1284 | goto end; |
| 1285 | } |
| 1286 | |
| 1287 | for (i = 0; i < count; i++) { |
| 1288 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { |
| 1289 | /* We need to leave it open to append other informations |
| 1290 | * like domain, channel, events etc.*/ |
| 1291 | session_found = 1; |
| 1292 | ret = mi_lttng_session(writer, &sessions[i], 1); |
| 1293 | if (ret) { |
| 1294 | goto end; |
| 1295 | } |
| 1296 | break; |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | if (!session_found) { |
| 1301 | ERR("Session '%s' not found", session_name); |
| 1302 | ret = -LTTNG_ERR_SESS_NOT_FOUND; |
| 1303 | goto end; |
| 1304 | } |
| 1305 | |
| 1306 | end: |
| 1307 | return ret; |
| 1308 | } |
| 1309 | |
| 1310 | /* |
| 1311 | * Machine interface |
| 1312 | * List all availables session |
| 1313 | */ |
| 1314 | static int mi_list_sessions(struct lttng_session *sessions, int count) |
| 1315 | { |
| 1316 | int ret, i; |
| 1317 | |
| 1318 | /* Opening sessions element */ |
| 1319 | ret = mi_lttng_sessions_open(writer); |
| 1320 | if (ret) { |
| 1321 | goto end; |
| 1322 | } |
| 1323 | |
| 1324 | /* Listing sessions */ |
| 1325 | for (i = 0; i < count; i++) { |
| 1326 | ret = mi_lttng_session(writer, &sessions[i], 0); |
| 1327 | if (ret) { |
| 1328 | goto end; |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | /* Closing sessions element */ |
| 1333 | ret = mi_lttng_writer_close_element(writer); |
| 1334 | if (ret) { |
| 1335 | goto end; |
| 1336 | } |
| 1337 | |
| 1338 | end: |
| 1339 | return ret; |
| 1340 | } |
| 1341 | |
| 1342 | /* |
| 1343 | * List available tracing session. List only basic information. |
| 1344 | * |
| 1345 | * If session_name is NULL, all sessions are listed. |
| 1346 | */ |
| 1347 | static int list_sessions(const char *session_name) |
| 1348 | { |
| 1349 | int ret = CMD_SUCCESS; |
| 1350 | int count, i; |
| 1351 | unsigned int session_found = 0; |
| 1352 | struct lttng_session *sessions; |
| 1353 | |
| 1354 | count = lttng_list_sessions(&sessions); |
| 1355 | DBG("Session count %d", count); |
| 1356 | if (count < 0) { |
| 1357 | ret = CMD_ERROR; |
| 1358 | ERR("%s", lttng_strerror(count)); |
| 1359 | goto end; |
| 1360 | } |
| 1361 | |
| 1362 | if (lttng_opt_mi) { |
| 1363 | /* Mi */ |
| 1364 | if (session_name == NULL) { |
| 1365 | /* List all session */ |
| 1366 | ret = mi_list_sessions(sessions, count); |
| 1367 | } else { |
| 1368 | /* Note : this return an open session element */ |
| 1369 | ret = mi_list_session(session_name, sessions, count); |
| 1370 | } |
| 1371 | if (ret) { |
| 1372 | ret = CMD_ERROR; |
| 1373 | goto error; |
| 1374 | } |
| 1375 | } else { |
| 1376 | /* Pretty print */ |
| 1377 | if (count == 0) { |
| 1378 | MSG("Currently no available tracing session"); |
| 1379 | goto end; |
| 1380 | } |
| 1381 | |
| 1382 | if (session_name == NULL) { |
| 1383 | MSG("Available tracing sessions:"); |
| 1384 | } |
| 1385 | |
| 1386 | |
| 1387 | for (i = 0; i < count; i++) { |
| 1388 | if (session_name != NULL) { |
| 1389 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { |
| 1390 | session_found = 1; |
| 1391 | MSG("Tracing session %s: [%s%s]", session_name, |
| 1392 | active_string(sessions[i].enabled), |
| 1393 | snapshot_string(sessions[i].snapshot_mode)); |
| 1394 | MSG("%sTrace path: %s\n", indent4, sessions[i].path); |
| 1395 | break; |
| 1396 | } |
| 1397 | } else { |
| 1398 | MSG(" %d) %s (%s) [%s%s]", i + 1, |
| 1399 | sessions[i].name, sessions[i].path, |
| 1400 | active_string(sessions[i].enabled), |
| 1401 | snapshot_string(sessions[i].snapshot_mode)); |
| 1402 | MSG("%sTrace path: %s", indent4, sessions[i].path); |
| 1403 | if (sessions[i].live_timer_interval != 0) { |
| 1404 | MSG("%sLive timer interval (usec): %u", indent4, |
| 1405 | sessions[i].live_timer_interval); |
| 1406 | } |
| 1407 | MSG(""); |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | if (!session_found && session_name != NULL) { |
| 1412 | ERR("Session '%s' not found", session_name); |
| 1413 | ret = CMD_ERROR; |
| 1414 | goto error; |
| 1415 | } |
| 1416 | |
| 1417 | if (session_name == NULL) { |
| 1418 | MSG("\nUse lttng list <session_name> for more details"); |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | error: |
| 1423 | free(sessions); |
| 1424 | end: |
| 1425 | return ret; |
| 1426 | } |
| 1427 | |
| 1428 | |
| 1429 | /* |
| 1430 | * Machine Interface |
| 1431 | * list available domain(s) for a session. |
| 1432 | */ |
| 1433 | static int mi_list_domains(struct lttng_domain *domains, int count) |
| 1434 | { |
| 1435 | int i, ret; |
| 1436 | /* Open domains element */ |
| 1437 | ret = mi_lttng_domains_open(writer); |
| 1438 | if (ret) { |
| 1439 | goto end; |
| 1440 | } |
| 1441 | |
| 1442 | for (i = 0; i < count; i++) { |
| 1443 | ret = mi_lttng_domain(writer, &domains[i] , 0); |
| 1444 | if (ret) { |
| 1445 | goto end; |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | /* Closing domains element */ |
| 1450 | ret = mi_lttng_writer_close_element(writer); |
| 1451 | if (ret) { |
| 1452 | goto end; |
| 1453 | } |
| 1454 | end: |
| 1455 | return ret; |
| 1456 | } |
| 1457 | |
| 1458 | /* |
| 1459 | * List available domain(s) for a session. |
| 1460 | */ |
| 1461 | static int list_domains(const char *session_name) |
| 1462 | { |
| 1463 | int i, count, ret = CMD_SUCCESS; |
| 1464 | struct lttng_domain *domains = NULL; |
| 1465 | |
| 1466 | |
| 1467 | count = lttng_list_domains(session_name, &domains); |
| 1468 | if (count < 0) { |
| 1469 | ret = CMD_ERROR; |
| 1470 | ERR("%s", lttng_strerror(count)); |
| 1471 | goto end; |
| 1472 | } |
| 1473 | |
| 1474 | if (lttng_opt_mi) { |
| 1475 | /* Mi output */ |
| 1476 | ret = mi_list_domains(domains, count); |
| 1477 | if (ret) { |
| 1478 | ret = CMD_ERROR; |
| 1479 | goto error; |
| 1480 | } |
| 1481 | } else { |
| 1482 | /* Pretty print */ |
| 1483 | MSG("Domains:\n-------------"); |
| 1484 | if (count == 0) { |
| 1485 | MSG(" None"); |
| 1486 | goto end; |
| 1487 | } |
| 1488 | |
| 1489 | for (i = 0; i < count; i++) { |
| 1490 | switch (domains[i].type) { |
| 1491 | case LTTNG_DOMAIN_KERNEL: |
| 1492 | MSG(" - Kernel"); |
| 1493 | break; |
| 1494 | case LTTNG_DOMAIN_UST: |
| 1495 | MSG(" - UST global"); |
| 1496 | break; |
| 1497 | case LTTNG_DOMAIN_JUL: |
| 1498 | MSG(" - JUL (Java Util Logging)"); |
| 1499 | break; |
| 1500 | case LTTNG_DOMAIN_LOG4J: |
| 1501 | MSG(" - LOG4j (Logging for Java)"); |
| 1502 | break; |
| 1503 | case LTTNG_DOMAIN_PYTHON: |
| 1504 | MSG(" - Python (logging)"); |
| 1505 | break; |
| 1506 | default: |
| 1507 | break; |
| 1508 | } |
| 1509 | } |
| 1510 | } |
| 1511 | |
| 1512 | error: |
| 1513 | free(domains); |
| 1514 | |
| 1515 | end: |
| 1516 | return ret; |
| 1517 | } |
| 1518 | |
| 1519 | /* |
| 1520 | * The 'list <options>' first level command |
| 1521 | */ |
| 1522 | int cmd_list(int argc, const char **argv) |
| 1523 | { |
| 1524 | int opt, ret = CMD_SUCCESS; |
| 1525 | const char *session_name; |
| 1526 | static poptContext pc; |
| 1527 | struct lttng_domain domain; |
| 1528 | struct lttng_domain *domains = NULL; |
| 1529 | |
| 1530 | memset(&domain, 0, sizeof(domain)); |
| 1531 | |
| 1532 | if (argc < 1) { |
| 1533 | usage(stderr); |
| 1534 | ret = CMD_ERROR; |
| 1535 | goto end; |
| 1536 | } |
| 1537 | |
| 1538 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 1539 | poptReadDefaultConfig(pc, 0); |
| 1540 | |
| 1541 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 1542 | switch (opt) { |
| 1543 | case OPT_HELP: |
| 1544 | usage(stdout); |
| 1545 | goto end; |
| 1546 | case OPT_USERSPACE: |
| 1547 | opt_userspace = 1; |
| 1548 | break; |
| 1549 | case OPT_LIST_OPTIONS: |
| 1550 | list_cmd_options(stdout, long_options); |
| 1551 | goto end; |
| 1552 | default: |
| 1553 | usage(stderr); |
| 1554 | ret = CMD_UNDEFINED; |
| 1555 | goto end; |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | /* Mi check */ |
| 1560 | if (lttng_opt_mi) { |
| 1561 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
| 1562 | if (!writer) { |
| 1563 | ret = CMD_ERROR; |
| 1564 | goto end; |
| 1565 | } |
| 1566 | |
| 1567 | /* Open command element */ |
| 1568 | ret = mi_lttng_writer_command_open(writer, |
| 1569 | mi_lttng_element_command_list); |
| 1570 | if (ret) { |
| 1571 | ret = CMD_ERROR; |
| 1572 | goto end; |
| 1573 | } |
| 1574 | |
| 1575 | /* Open output element */ |
| 1576 | ret = mi_lttng_writer_open_element(writer, |
| 1577 | mi_lttng_element_command_output); |
| 1578 | if (ret) { |
| 1579 | ret = CMD_ERROR; |
| 1580 | goto end; |
| 1581 | } |
| 1582 | } |
| 1583 | |
| 1584 | /* Get session name (trailing argument) */ |
| 1585 | session_name = poptGetArg(pc); |
| 1586 | DBG2("Session name: %s", session_name); |
| 1587 | |
| 1588 | if (opt_kernel) { |
| 1589 | domain.type = LTTNG_DOMAIN_KERNEL; |
| 1590 | } else if (opt_userspace) { |
| 1591 | DBG2("Listing userspace global domain"); |
| 1592 | domain.type = LTTNG_DOMAIN_UST; |
| 1593 | } else if (opt_jul) { |
| 1594 | DBG2("Listing JUL domain"); |
| 1595 | domain.type = LTTNG_DOMAIN_JUL; |
| 1596 | } else if (opt_log4j) { |
| 1597 | domain.type = LTTNG_DOMAIN_LOG4J; |
| 1598 | } else if (opt_python) { |
| 1599 | domain.type = LTTNG_DOMAIN_PYTHON; |
| 1600 | } |
| 1601 | |
| 1602 | if (!opt_kernel && opt_syscall) { |
| 1603 | WARN("--syscall will only work with the Kernel domain (-k)"); |
| 1604 | ret = CMD_ERROR; |
| 1605 | goto end; |
| 1606 | } |
| 1607 | |
| 1608 | if (opt_kernel || opt_userspace || opt_jul || opt_log4j || opt_python) { |
| 1609 | handle = lttng_create_handle(session_name, &domain); |
| 1610 | if (handle == NULL) { |
| 1611 | ret = CMD_FATAL; |
| 1612 | goto end; |
| 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | if (session_name == NULL) { |
| 1617 | if (!opt_kernel && !opt_userspace && !opt_jul && !opt_log4j |
| 1618 | && !opt_python) { |
| 1619 | ret = list_sessions(NULL); |
| 1620 | if (ret) { |
| 1621 | goto end; |
| 1622 | } |
| 1623 | } |
| 1624 | if (opt_kernel) { |
| 1625 | if (opt_syscall) { |
| 1626 | ret = list_syscalls(); |
| 1627 | if (ret) { |
| 1628 | goto end; |
| 1629 | } |
| 1630 | } else { |
| 1631 | ret = list_kernel_events(); |
| 1632 | if (ret) { |
| 1633 | goto end; |
| 1634 | } |
| 1635 | } |
| 1636 | } |
| 1637 | if (opt_userspace) { |
| 1638 | if (opt_fields) { |
| 1639 | ret = list_ust_event_fields(); |
| 1640 | } else { |
| 1641 | ret = list_ust_events(); |
| 1642 | } |
| 1643 | if (ret) { |
| 1644 | goto end; |
| 1645 | } |
| 1646 | } |
| 1647 | if (opt_jul || opt_log4j || opt_python) { |
| 1648 | ret = list_agent_events(); |
| 1649 | if (ret) { |
| 1650 | goto end; |
| 1651 | } |
| 1652 | } |
| 1653 | } else { |
| 1654 | /* List session attributes */ |
| 1655 | if (lttng_opt_mi) { |
| 1656 | /* Open element sessions |
| 1657 | * Present for xml consistency */ |
| 1658 | ret = mi_lttng_sessions_open(writer); |
| 1659 | if (ret) { |
| 1660 | goto end; |
| 1661 | } |
| 1662 | } |
| 1663 | /* MI: the ouptut of list_sessions is an unclosed session element */ |
| 1664 | ret = list_sessions(session_name); |
| 1665 | if (ret) { |
| 1666 | goto end; |
| 1667 | } |
| 1668 | |
| 1669 | /* Domain listing */ |
| 1670 | if (opt_domain) { |
| 1671 | ret = list_domains(session_name); |
| 1672 | goto end; |
| 1673 | } |
| 1674 | |
| 1675 | /* Channel listing */ |
| 1676 | if (opt_kernel || opt_userspace) { |
| 1677 | if (lttng_opt_mi) { |
| 1678 | /* Add of domains and domain element for xml |
| 1679 | * consistency and validation |
| 1680 | */ |
| 1681 | ret = mi_lttng_domains_open(writer); |
| 1682 | if (ret) { |
| 1683 | goto end; |
| 1684 | } |
| 1685 | |
| 1686 | /* Open domain and leave it open for |
| 1687 | * nested channels printing */ |
| 1688 | ret = mi_lttng_domain(writer, &domain, 1); |
| 1689 | if (ret) { |
| 1690 | goto end; |
| 1691 | } |
| 1692 | |
| 1693 | } |
| 1694 | |
| 1695 | ret = list_tracker_pids(); |
| 1696 | if (ret) { |
| 1697 | goto end; |
| 1698 | } |
| 1699 | |
| 1700 | ret = list_channels(opt_channel); |
| 1701 | if (ret) { |
| 1702 | goto end; |
| 1703 | } |
| 1704 | |
| 1705 | if (lttng_opt_mi) { |
| 1706 | /* Close domain and domain element */ |
| 1707 | ret = mi_lttng_close_multi_element(writer, 2); |
| 1708 | } |
| 1709 | if (ret) { |
| 1710 | goto end; |
| 1711 | } |
| 1712 | |
| 1713 | |
| 1714 | } else { |
| 1715 | int i, nb_domain; |
| 1716 | |
| 1717 | /* We want all domain(s) */ |
| 1718 | nb_domain = lttng_list_domains(session_name, &domains); |
| 1719 | if (nb_domain < 0) { |
| 1720 | ret = CMD_ERROR; |
| 1721 | ERR("%s", lttng_strerror(nb_domain)); |
| 1722 | goto end; |
| 1723 | } |
| 1724 | |
| 1725 | if (lttng_opt_mi) { |
| 1726 | ret = mi_lttng_domains_open(writer); |
| 1727 | if (ret) { |
| 1728 | ret = CMD_ERROR; |
| 1729 | goto end; |
| 1730 | } |
| 1731 | } |
| 1732 | |
| 1733 | for (i = 0; i < nb_domain; i++) { |
| 1734 | switch (domains[i].type) { |
| 1735 | case LTTNG_DOMAIN_KERNEL: |
| 1736 | MSG("=== Domain: Kernel ===\n"); |
| 1737 | break; |
| 1738 | case LTTNG_DOMAIN_UST: |
| 1739 | MSG("=== Domain: UST global ===\n"); |
| 1740 | MSG("Buffer type: %s\n", |
| 1741 | domains[i].buf_type == |
| 1742 | LTTNG_BUFFER_PER_PID ? "per PID" : "per UID"); |
| 1743 | break; |
| 1744 | case LTTNG_DOMAIN_JUL: |
| 1745 | MSG("=== Domain: JUL (Java Util Logging) ===\n"); |
| 1746 | break; |
| 1747 | case LTTNG_DOMAIN_LOG4J: |
| 1748 | MSG("=== Domain: LOG4j (Logging for Java) ===\n"); |
| 1749 | break; |
| 1750 | case LTTNG_DOMAIN_PYTHON: |
| 1751 | MSG("=== Domain: Python (logging) ===\n"); |
| 1752 | break; |
| 1753 | default: |
| 1754 | MSG("=== Domain: Unimplemented ===\n"); |
| 1755 | break; |
| 1756 | } |
| 1757 | |
| 1758 | if (lttng_opt_mi) { |
| 1759 | ret = mi_lttng_domain(writer, &domains[i], 1); |
| 1760 | if (ret) { |
| 1761 | ret = CMD_ERROR; |
| 1762 | goto end; |
| 1763 | } |
| 1764 | } |
| 1765 | |
| 1766 | /* Clean handle before creating a new one */ |
| 1767 | if (handle) { |
| 1768 | lttng_destroy_handle(handle); |
| 1769 | } |
| 1770 | |
| 1771 | handle = lttng_create_handle(session_name, &domains[i]); |
| 1772 | if (handle == NULL) { |
| 1773 | ret = CMD_FATAL; |
| 1774 | goto end; |
| 1775 | } |
| 1776 | |
| 1777 | if (domains[i].type == LTTNG_DOMAIN_JUL || |
| 1778 | domains[i].type == LTTNG_DOMAIN_LOG4J || |
| 1779 | domains[i].type == LTTNG_DOMAIN_PYTHON) { |
| 1780 | ret = list_session_agent_events(); |
| 1781 | if (ret) { |
| 1782 | goto end; |
| 1783 | } |
| 1784 | continue; |
| 1785 | } |
| 1786 | |
| 1787 | switch (domains[i].type) { |
| 1788 | case LTTNG_DOMAIN_KERNEL: |
| 1789 | case LTTNG_DOMAIN_UST: |
| 1790 | ret = list_tracker_pids(); |
| 1791 | if (ret) { |
| 1792 | goto end; |
| 1793 | } |
| 1794 | break; |
| 1795 | default: |
| 1796 | break; |
| 1797 | } |
| 1798 | |
| 1799 | ret = list_channels(opt_channel); |
| 1800 | if (ret) { |
| 1801 | goto end; |
| 1802 | } |
| 1803 | |
| 1804 | if (lttng_opt_mi) { |
| 1805 | /* Close domain element */ |
| 1806 | ret = mi_lttng_writer_close_element(writer); |
| 1807 | if (ret) { |
| 1808 | ret = CMD_ERROR; |
| 1809 | goto end; |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | } |
| 1814 | if (lttng_opt_mi) { |
| 1815 | /* Close the domains, session and sessions element */ |
| 1816 | ret = mi_lttng_close_multi_element(writer, 3); |
| 1817 | if (ret) { |
| 1818 | ret = CMD_ERROR; |
| 1819 | goto end; |
| 1820 | } |
| 1821 | } |
| 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | /* Mi closing */ |
| 1826 | if (lttng_opt_mi) { |
| 1827 | /* Close output element */ |
| 1828 | ret = mi_lttng_writer_close_element(writer); |
| 1829 | if (ret) { |
| 1830 | ret = CMD_ERROR; |
| 1831 | goto end; |
| 1832 | } |
| 1833 | |
| 1834 | /* Command element close */ |
| 1835 | ret = mi_lttng_writer_command_close(writer); |
| 1836 | if (ret) { |
| 1837 | ret = CMD_ERROR; |
| 1838 | goto end; |
| 1839 | } |
| 1840 | } |
| 1841 | end: |
| 1842 | /* Mi clean-up */ |
| 1843 | if (writer && mi_lttng_writer_destroy(writer)) { |
| 1844 | /* Preserve original error code */ |
| 1845 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; |
| 1846 | } |
| 1847 | |
| 1848 | free(domains); |
| 1849 | if (handle) { |
| 1850 | lttng_destroy_handle(handle); |
| 1851 | } |
| 1852 | |
| 1853 | poptFreeContext(pc); |
| 1854 | return ret; |
| 1855 | } |