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