| 1 | /* |
| 2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
| 3 | * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0-only |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include <stdint.h> |
| 10 | #define _LGPL_SOURCE |
| 11 | #include <inttypes.h> |
| 12 | #include <popt.h> |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | #include <assert.h> |
| 17 | |
| 18 | #include <common/mi-lttng.h> |
| 19 | #include <common/time.h> |
| 20 | #include <common/tracker.h> |
| 21 | #include <lttng/domain-internal.h> |
| 22 | #include <lttng/lttng.h> |
| 23 | |
| 24 | #include "../command.h" |
| 25 | |
| 26 | static int opt_userspace; |
| 27 | static int opt_kernel; |
| 28 | static int opt_jul; |
| 29 | static int opt_log4j; |
| 30 | static int opt_python; |
| 31 | static char *opt_channel; |
| 32 | static int opt_domain; |
| 33 | static int opt_fields; |
| 34 | static int opt_syscall; |
| 35 | |
| 36 | const char *indent4 = " "; |
| 37 | const char *indent6 = " "; |
| 38 | const char *indent8 = " "; |
| 39 | |
| 40 | #ifdef LTTNG_EMBED_HELP |
| 41 | static const char help_msg[] = |
| 42 | #include <lttng-list.1.h> |
| 43 | ; |
| 44 | #endif |
| 45 | |
| 46 | enum { |
| 47 | OPT_HELP = 1, |
| 48 | OPT_USERSPACE, |
| 49 | OPT_LIST_OPTIONS, |
| 50 | }; |
| 51 | |
| 52 | static struct lttng_handle *the_handle; |
| 53 | static struct mi_writer *the_writer; |
| 54 | |
| 55 | /* Only set when listing a single session. */ |
| 56 | static struct lttng_session the_listed_session; |
| 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 | {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, |
| 66 | {"channel", 'c', POPT_ARG_STRING, &opt_channel, 0, 0, 0}, |
| 67 | {"domain", 'd', POPT_ARG_VAL, &opt_domain, 1, 0, 0}, |
| 68 | {"fields", 'f', POPT_ARG_VAL, &opt_fields, 1, 0, 0}, |
| 69 | {"syscall", 'S', POPT_ARG_VAL, &opt_syscall, 1, 0, 0}, |
| 70 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 71 | {0, 0, 0, 0, 0, 0, 0} |
| 72 | }; |
| 73 | |
| 74 | /* |
| 75 | * Get command line from /proc for a specific pid. |
| 76 | * |
| 77 | * On success, return an allocated string pointer to the proc cmdline. |
| 78 | * On error, return NULL. |
| 79 | */ |
| 80 | static char *get_cmdline_by_pid(pid_t pid) |
| 81 | { |
| 82 | int ret; |
| 83 | FILE *fp = NULL; |
| 84 | char *cmdline = NULL; |
| 85 | /* Can't go bigger than /proc/LTTNG_MAX_PID/cmdline */ |
| 86 | char path[sizeof("/proc//cmdline") + sizeof(LTTNG_MAX_PID_STR) - 1]; |
| 87 | |
| 88 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); |
| 89 | fp = fopen(path, "r"); |
| 90 | if (fp == NULL) { |
| 91 | goto end; |
| 92 | } |
| 93 | |
| 94 | /* Caller must free() *cmdline */ |
| 95 | cmdline = zmalloc(PATH_MAX); |
| 96 | if (!cmdline) { |
| 97 | PERROR("malloc cmdline"); |
| 98 | goto end; |
| 99 | } |
| 100 | ret = fread(cmdline, 1, PATH_MAX, fp); |
| 101 | if (ret < 0) { |
| 102 | PERROR("fread proc list"); |
| 103 | } |
| 104 | |
| 105 | end: |
| 106 | if (fp) { |
| 107 | fclose(fp); |
| 108 | } |
| 109 | return cmdline; |
| 110 | } |
| 111 | |
| 112 | static |
| 113 | const char *active_string(int value) |
| 114 | { |
| 115 | switch (value) { |
| 116 | case 0: return "inactive"; |
| 117 | case 1: return "active"; |
| 118 | case -1: return ""; |
| 119 | default: return NULL; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | static const char *snapshot_string(int value) |
| 124 | { |
| 125 | switch (value) { |
| 126 | case 1: |
| 127 | return " snapshot"; |
| 128 | default: |
| 129 | return ""; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static |
| 134 | const char *enabled_string(int value) |
| 135 | { |
| 136 | switch (value) { |
| 137 | case 0: return " [disabled]"; |
| 138 | case 1: return " [enabled]"; |
| 139 | case -1: return ""; |
| 140 | default: return NULL; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | static |
| 145 | const char *safe_string(const char *str) |
| 146 | { |
| 147 | return str ? str : ""; |
| 148 | } |
| 149 | |
| 150 | static const char *logleveltype_string(enum lttng_loglevel_type value) |
| 151 | { |
| 152 | switch (value) { |
| 153 | case LTTNG_EVENT_LOGLEVEL_ALL: |
| 154 | return ":"; |
| 155 | case LTTNG_EVENT_LOGLEVEL_RANGE: |
| 156 | return " <="; |
| 157 | case LTTNG_EVENT_LOGLEVEL_SINGLE: |
| 158 | return " =="; |
| 159 | default: |
| 160 | return " <<TYPE UNKN>>"; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | static const char *bitness_event(enum lttng_event_flag flags) |
| 165 | { |
| 166 | if (flags & LTTNG_EVENT_FLAG_SYSCALL_32) { |
| 167 | if (flags & LTTNG_EVENT_FLAG_SYSCALL_64) { |
| 168 | return " [32/64-bit]"; |
| 169 | } else { |
| 170 | return " [32-bit]"; |
| 171 | } |
| 172 | } else if (flags & LTTNG_EVENT_FLAG_SYSCALL_64) { |
| 173 | return " [64-bit]"; |
| 174 | } else { |
| 175 | return ""; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Get exclusion names message for a single event. |
| 181 | * |
| 182 | * Returned pointer must be freed by caller. Returns NULL on error. |
| 183 | */ |
| 184 | static char *get_exclusion_names_msg(struct lttng_event *event) |
| 185 | { |
| 186 | int ret; |
| 187 | int exclusion_count; |
| 188 | char *exclusion_msg = NULL; |
| 189 | char *at; |
| 190 | size_t i; |
| 191 | const char * const exclusion_fmt = " [exclusions: "; |
| 192 | const size_t exclusion_fmt_len = strlen(exclusion_fmt); |
| 193 | |
| 194 | exclusion_count = lttng_event_get_exclusion_name_count(event); |
| 195 | if (exclusion_count < 0) { |
| 196 | goto end; |
| 197 | } else if (exclusion_count == 0) { |
| 198 | /* |
| 199 | * No exclusions: return copy of empty string so that |
| 200 | * it can be freed by caller. |
| 201 | */ |
| 202 | exclusion_msg = strdup(""); |
| 203 | goto end; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * exclusion_msg's size is bounded by the exclusion_fmt string, |
| 208 | * a comma per entry, the entry count (fixed-size), a closing |
| 209 | * bracket, and a trailing \0. |
| 210 | */ |
| 211 | exclusion_msg = malloc(exclusion_count + |
| 212 | exclusion_count * LTTNG_SYMBOL_NAME_LEN + |
| 213 | exclusion_fmt_len + 1); |
| 214 | if (!exclusion_msg) { |
| 215 | goto end; |
| 216 | } |
| 217 | |
| 218 | at = strcpy(exclusion_msg, exclusion_fmt) + exclusion_fmt_len; |
| 219 | for (i = 0; i < exclusion_count; ++i) { |
| 220 | const char *name; |
| 221 | |
| 222 | /* Append comma between exclusion names */ |
| 223 | if (i > 0) { |
| 224 | *at = ','; |
| 225 | at++; |
| 226 | } |
| 227 | |
| 228 | ret = lttng_event_get_exclusion_name(event, i, &name); |
| 229 | if (ret) { |
| 230 | /* Prints '?' on local error; should never happen */ |
| 231 | *at = '?'; |
| 232 | at++; |
| 233 | continue; |
| 234 | } |
| 235 | |
| 236 | /* Append exclusion name */ |
| 237 | at += sprintf(at, "%s", name); |
| 238 | } |
| 239 | |
| 240 | /* This also puts a final '\0' at the end of exclusion_msg */ |
| 241 | strcpy(at, "]"); |
| 242 | |
| 243 | end: |
| 244 | return exclusion_msg; |
| 245 | } |
| 246 | |
| 247 | static void print_userspace_probe_location(struct lttng_event *event) |
| 248 | { |
| 249 | const struct lttng_userspace_probe_location *location; |
| 250 | const struct lttng_userspace_probe_location_lookup_method *lookup_method; |
| 251 | enum lttng_userspace_probe_location_lookup_method_type lookup_type; |
| 252 | |
| 253 | location = lttng_event_get_userspace_probe_location(event); |
| 254 | if (!location) { |
| 255 | MSG("Event has no userspace probe location"); |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | lookup_method = lttng_userspace_probe_location_get_lookup_method(location); |
| 260 | if (!lookup_method) { |
| 261 | MSG("Event has no userspace probe location lookup method"); |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | MSG("%s%s (type: userspace-probe)%s", indent6, event->name, enabled_string(event->enabled)); |
| 266 | |
| 267 | lookup_type = lttng_userspace_probe_location_lookup_method_get_type(lookup_method); |
| 268 | |
| 269 | switch (lttng_userspace_probe_location_get_type(location)) { |
| 270 | case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_UNKNOWN: |
| 271 | MSG("%sType: Unknown", indent8); |
| 272 | break; |
| 273 | case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION: |
| 274 | { |
| 275 | const char *function_name; |
| 276 | char *binary_path; |
| 277 | |
| 278 | MSG("%sType: Function", indent8); |
| 279 | function_name = lttng_userspace_probe_location_function_get_function_name(location); |
| 280 | binary_path = realpath(lttng_userspace_probe_location_function_get_binary_path(location), NULL); |
| 281 | |
| 282 | MSG("%sBinary path: %s", indent8, binary_path ? binary_path : "NULL"); |
| 283 | MSG("%sFunction: %s()", indent8, function_name ? function_name : "NULL"); |
| 284 | switch (lookup_type) { |
| 285 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF: |
| 286 | MSG("%sLookup method: ELF", indent8); |
| 287 | break; |
| 288 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT: |
| 289 | MSG("%sLookup method: default", indent8); |
| 290 | break; |
| 291 | default: |
| 292 | MSG("%sLookup method: INVALID LOOKUP TYPE ENCOUNTERED", indent8); |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | free(binary_path); |
| 297 | break; |
| 298 | } |
| 299 | case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT: |
| 300 | { |
| 301 | const char *probe_name, *provider_name; |
| 302 | char *binary_path; |
| 303 | |
| 304 | MSG("%sType: Tracepoint", indent8); |
| 305 | probe_name = lttng_userspace_probe_location_tracepoint_get_probe_name(location); |
| 306 | provider_name = lttng_userspace_probe_location_tracepoint_get_provider_name(location); |
| 307 | binary_path = realpath(lttng_userspace_probe_location_tracepoint_get_binary_path(location), NULL); |
| 308 | MSG("%sBinary path: %s", indent8, binary_path ? binary_path : "NULL"); |
| 309 | MSG("%sTracepoint: %s:%s", indent8, provider_name ? provider_name : "NULL", probe_name ? probe_name : "NULL"); |
| 310 | switch (lookup_type) { |
| 311 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT: |
| 312 | MSG("%sLookup method: SDT", indent8); |
| 313 | break; |
| 314 | default: |
| 315 | MSG("%sLookup method: INVALID LOOKUP TYPE ENCOUNTERED", indent8); |
| 316 | break; |
| 317 | } |
| 318 | |
| 319 | free(binary_path); |
| 320 | break; |
| 321 | } |
| 322 | default: |
| 323 | ERR("Invalid probe type encountered"); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * Pretty print single event. |
| 329 | */ |
| 330 | static void print_events(struct lttng_event *event) |
| 331 | { |
| 332 | int ret; |
| 333 | const char *filter_str; |
| 334 | char *filter_msg = NULL; |
| 335 | char *exclusion_msg = NULL; |
| 336 | |
| 337 | ret = lttng_event_get_filter_expression(event, &filter_str); |
| 338 | |
| 339 | if (ret) { |
| 340 | filter_msg = strdup(" [failed to retrieve filter]"); |
| 341 | } else if (filter_str) { |
| 342 | const char * const filter_fmt = " [filter: '%s']"; |
| 343 | |
| 344 | filter_msg = malloc(strlen(filter_str) + |
| 345 | strlen(filter_fmt) + 1); |
| 346 | if (filter_msg) { |
| 347 | sprintf(filter_msg, filter_fmt, |
| 348 | filter_str); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | exclusion_msg = get_exclusion_names_msg(event); |
| 353 | if (!exclusion_msg) { |
| 354 | exclusion_msg = strdup(" [failed to retrieve exclusions]"); |
| 355 | } |
| 356 | |
| 357 | switch (event->type) { |
| 358 | case LTTNG_EVENT_TRACEPOINT: |
| 359 | { |
| 360 | if (event->loglevel != -1) { |
| 361 | MSG("%s%s (loglevel%s %s (%d)) (type: tracepoint)%s%s%s", |
| 362 | indent6, event->name, |
| 363 | logleveltype_string( |
| 364 | event->loglevel_type), |
| 365 | mi_lttng_loglevel_string( |
| 366 | event->loglevel, |
| 367 | the_handle->domain.type), |
| 368 | event->loglevel, |
| 369 | enabled_string(event->enabled), |
| 370 | safe_string(exclusion_msg), |
| 371 | safe_string(filter_msg)); |
| 372 | } else { |
| 373 | MSG("%s%s (type: tracepoint)%s%s%s", |
| 374 | indent6, |
| 375 | event->name, |
| 376 | enabled_string(event->enabled), |
| 377 | safe_string(exclusion_msg), |
| 378 | safe_string(filter_msg)); |
| 379 | } |
| 380 | break; |
| 381 | } |
| 382 | case LTTNG_EVENT_FUNCTION: |
| 383 | MSG("%s%s (type: function)%s%s", indent6, |
| 384 | event->name, enabled_string(event->enabled), |
| 385 | safe_string(filter_msg)); |
| 386 | if (event->attr.probe.addr != 0) { |
| 387 | MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr); |
| 388 | } else { |
| 389 | MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset); |
| 390 | MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name); |
| 391 | } |
| 392 | break; |
| 393 | case LTTNG_EVENT_PROBE: |
| 394 | MSG("%s%s (type: probe)%s%s", indent6, |
| 395 | event->name, enabled_string(event->enabled), |
| 396 | safe_string(filter_msg)); |
| 397 | if (event->attr.probe.addr != 0) { |
| 398 | MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr); |
| 399 | } else { |
| 400 | MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset); |
| 401 | MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name); |
| 402 | } |
| 403 | break; |
| 404 | case LTTNG_EVENT_USERSPACE_PROBE: |
| 405 | print_userspace_probe_location(event); |
| 406 | break; |
| 407 | case LTTNG_EVENT_FUNCTION_ENTRY: |
| 408 | MSG("%s%s (type: function)%s%s", indent6, |
| 409 | event->name, enabled_string(event->enabled), |
| 410 | safe_string(filter_msg)); |
| 411 | MSG("%ssymbol: \"%s\"", indent8, event->attr.ftrace.symbol_name); |
| 412 | break; |
| 413 | case LTTNG_EVENT_SYSCALL: |
| 414 | MSG("%s%s%s%s%s%s", indent6, event->name, |
| 415 | (opt_syscall ? "" : " (type:syscall)"), |
| 416 | enabled_string(event->enabled), |
| 417 | bitness_event(event->flags), |
| 418 | safe_string(filter_msg)); |
| 419 | break; |
| 420 | case LTTNG_EVENT_NOOP: |
| 421 | MSG("%s (type: noop)%s%s", indent6, |
| 422 | enabled_string(event->enabled), |
| 423 | safe_string(filter_msg)); |
| 424 | break; |
| 425 | case LTTNG_EVENT_ALL: |
| 426 | /* Fall-through. */ |
| 427 | default: |
| 428 | /* We should never have "all" events in list. */ |
| 429 | assert(0); |
| 430 | break; |
| 431 | } |
| 432 | |
| 433 | free(filter_msg); |
| 434 | free(exclusion_msg); |
| 435 | } |
| 436 | |
| 437 | static const char *field_type(struct lttng_event_field *field) |
| 438 | { |
| 439 | switch(field->type) { |
| 440 | case LTTNG_EVENT_FIELD_INTEGER: |
| 441 | return "integer"; |
| 442 | case LTTNG_EVENT_FIELD_ENUM: |
| 443 | return "enum"; |
| 444 | case LTTNG_EVENT_FIELD_FLOAT: |
| 445 | return "float"; |
| 446 | case LTTNG_EVENT_FIELD_STRING: |
| 447 | return "string"; |
| 448 | case LTTNG_EVENT_FIELD_OTHER: |
| 449 | default: /* fall-through */ |
| 450 | return "unknown"; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * Pretty print single event fields. |
| 456 | */ |
| 457 | static void print_event_field(struct lttng_event_field *field) |
| 458 | { |
| 459 | if (!field->field_name[0]) { |
| 460 | return; |
| 461 | } |
| 462 | MSG("%sfield: %s (%s)%s", indent8, field->field_name, |
| 463 | field_type(field), field->nowrite ? " [no write]" : ""); |
| 464 | } |
| 465 | |
| 466 | /* |
| 467 | * Machine interface |
| 468 | * Jul and ust event listing |
| 469 | */ |
| 470 | static int mi_list_agent_ust_events(struct lttng_event *events, int count, |
| 471 | struct lttng_domain *domain) |
| 472 | { |
| 473 | int ret, i; |
| 474 | pid_t cur_pid = 0; |
| 475 | char *cmdline = NULL; |
| 476 | int pid_element_open = 0; |
| 477 | |
| 478 | /* Open domains element */ |
| 479 | ret = mi_lttng_domains_open(the_writer); |
| 480 | if (ret) { |
| 481 | goto end; |
| 482 | } |
| 483 | |
| 484 | /* Write domain */ |
| 485 | ret = mi_lttng_domain(the_writer, domain, 1); |
| 486 | if (ret) { |
| 487 | goto end; |
| 488 | } |
| 489 | |
| 490 | /* Open pids element element */ |
| 491 | ret = mi_lttng_pids_open(the_writer); |
| 492 | if (ret) { |
| 493 | goto end; |
| 494 | } |
| 495 | |
| 496 | for (i = 0; i < count; i++) { |
| 497 | if (cur_pid != events[i].pid) { |
| 498 | if (pid_element_open) { |
| 499 | /* Close the previous events and pid element */ |
| 500 | ret = mi_lttng_close_multi_element( |
| 501 | the_writer, 2); |
| 502 | if (ret) { |
| 503 | goto end; |
| 504 | } |
| 505 | pid_element_open = 0; |
| 506 | } |
| 507 | |
| 508 | cur_pid = events[i].pid; |
| 509 | cmdline = get_cmdline_by_pid(cur_pid); |
| 510 | if (!cmdline) { |
| 511 | ret = CMD_ERROR; |
| 512 | goto end; |
| 513 | } |
| 514 | |
| 515 | if (!pid_element_open) { |
| 516 | /* Open and write a pid element */ |
| 517 | ret = mi_lttng_pid(the_writer, cur_pid, cmdline, |
| 518 | 1); |
| 519 | if (ret) { |
| 520 | goto error; |
| 521 | } |
| 522 | |
| 523 | /* Open events element */ |
| 524 | ret = mi_lttng_events_open(the_writer); |
| 525 | if (ret) { |
| 526 | goto error; |
| 527 | } |
| 528 | |
| 529 | pid_element_open = 1; |
| 530 | } |
| 531 | free(cmdline); |
| 532 | } |
| 533 | |
| 534 | /* Write an event */ |
| 535 | ret = mi_lttng_event(the_writer, &events[i], 0, |
| 536 | the_handle->domain.type); |
| 537 | if (ret) { |
| 538 | goto end; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | /* Close pids */ |
| 543 | ret = mi_lttng_writer_close_element(the_writer); |
| 544 | if (ret) { |
| 545 | goto end; |
| 546 | } |
| 547 | |
| 548 | /* Close domain, domains */ |
| 549 | ret = mi_lttng_close_multi_element(the_writer, 2); |
| 550 | end: |
| 551 | return ret; |
| 552 | error: |
| 553 | free(cmdline); |
| 554 | return ret; |
| 555 | } |
| 556 | |
| 557 | static int list_agent_events(void) |
| 558 | { |
| 559 | int i, size, ret = CMD_SUCCESS; |
| 560 | struct lttng_domain domain; |
| 561 | struct lttng_handle *handle = NULL; |
| 562 | struct lttng_event *event_list = NULL; |
| 563 | pid_t cur_pid = 0; |
| 564 | char *cmdline = NULL; |
| 565 | const char *agent_domain_str; |
| 566 | |
| 567 | memset(&domain, 0, sizeof(domain)); |
| 568 | if (opt_jul) { |
| 569 | domain.type = LTTNG_DOMAIN_JUL; |
| 570 | } else if (opt_log4j) { |
| 571 | domain.type = LTTNG_DOMAIN_LOG4J; |
| 572 | } else if (opt_python) { |
| 573 | domain.type = LTTNG_DOMAIN_PYTHON; |
| 574 | } else { |
| 575 | ERR("Invalid agent domain selected."); |
| 576 | ret = CMD_ERROR; |
| 577 | goto error; |
| 578 | } |
| 579 | |
| 580 | agent_domain_str = lttng_domain_type_str(domain.type); |
| 581 | |
| 582 | DBG("Getting %s tracing events", agent_domain_str); |
| 583 | |
| 584 | handle = lttng_create_handle(NULL, &domain); |
| 585 | if (handle == NULL) { |
| 586 | ret = CMD_ERROR; |
| 587 | goto end; |
| 588 | } |
| 589 | |
| 590 | size = lttng_list_tracepoints(handle, &event_list); |
| 591 | if (size < 0) { |
| 592 | ERR("Unable to list %s events: %s", agent_domain_str, |
| 593 | lttng_strerror(size)); |
| 594 | ret = CMD_ERROR; |
| 595 | goto end; |
| 596 | } |
| 597 | |
| 598 | if (lttng_opt_mi) { |
| 599 | /* Mi print */ |
| 600 | ret = mi_list_agent_ust_events(event_list, size, &domain); |
| 601 | if (ret) { |
| 602 | ret = CMD_ERROR; |
| 603 | goto error; |
| 604 | } |
| 605 | } else { |
| 606 | /* Pretty print */ |
| 607 | MSG("%s events (Logger name):\n-------------------------", |
| 608 | agent_domain_str); |
| 609 | |
| 610 | if (size == 0) { |
| 611 | MSG("None"); |
| 612 | } |
| 613 | |
| 614 | for (i = 0; i < size; i++) { |
| 615 | if (cur_pid != event_list[i].pid) { |
| 616 | cur_pid = event_list[i].pid; |
| 617 | cmdline = get_cmdline_by_pid(cur_pid); |
| 618 | if (cmdline == NULL) { |
| 619 | ret = CMD_ERROR; |
| 620 | goto error; |
| 621 | } |
| 622 | MSG("\nPID: %d - Name: %s", cur_pid, cmdline); |
| 623 | free(cmdline); |
| 624 | } |
| 625 | MSG("%s- %s", indent6, event_list[i].name); |
| 626 | } |
| 627 | |
| 628 | MSG(""); |
| 629 | } |
| 630 | |
| 631 | error: |
| 632 | free(event_list); |
| 633 | end: |
| 634 | lttng_destroy_handle(handle); |
| 635 | return ret; |
| 636 | } |
| 637 | |
| 638 | /* |
| 639 | * Ask session daemon for all user space tracepoints available. |
| 640 | */ |
| 641 | static int list_ust_events(void) |
| 642 | { |
| 643 | int i, size, ret = CMD_SUCCESS; |
| 644 | struct lttng_domain domain; |
| 645 | struct lttng_handle *handle; |
| 646 | struct lttng_event *event_list = NULL; |
| 647 | pid_t cur_pid = 0; |
| 648 | char *cmdline = NULL; |
| 649 | |
| 650 | memset(&domain, 0, sizeof(domain)); |
| 651 | |
| 652 | DBG("Getting UST tracing events"); |
| 653 | |
| 654 | domain.type = LTTNG_DOMAIN_UST; |
| 655 | |
| 656 | handle = lttng_create_handle(NULL, &domain); |
| 657 | if (handle == NULL) { |
| 658 | ret = CMD_ERROR; |
| 659 | goto end; |
| 660 | } |
| 661 | |
| 662 | size = lttng_list_tracepoints(handle, &event_list); |
| 663 | if (size < 0) { |
| 664 | ERR("Unable to list UST events: %s", lttng_strerror(size)); |
| 665 | ret = CMD_ERROR; |
| 666 | goto error; |
| 667 | } |
| 668 | |
| 669 | if (lttng_opt_mi) { |
| 670 | /* Mi print */ |
| 671 | ret = mi_list_agent_ust_events(event_list, size, &domain); |
| 672 | } else { |
| 673 | /* Pretty print */ |
| 674 | MSG("UST events:\n-------------"); |
| 675 | |
| 676 | if (size == 0) { |
| 677 | MSG("None"); |
| 678 | } |
| 679 | |
| 680 | for (i = 0; i < size; i++) { |
| 681 | if (cur_pid != event_list[i].pid) { |
| 682 | cur_pid = event_list[i].pid; |
| 683 | cmdline = get_cmdline_by_pid(cur_pid); |
| 684 | if (cmdline == NULL) { |
| 685 | ret = CMD_ERROR; |
| 686 | goto error; |
| 687 | } |
| 688 | MSG("\nPID: %d - Name: %s", cur_pid, cmdline); |
| 689 | free(cmdline); |
| 690 | } |
| 691 | print_events(&event_list[i]); |
| 692 | } |
| 693 | |
| 694 | MSG(""); |
| 695 | } |
| 696 | |
| 697 | error: |
| 698 | free(event_list); |
| 699 | end: |
| 700 | lttng_destroy_handle(handle); |
| 701 | return ret; |
| 702 | } |
| 703 | |
| 704 | /* |
| 705 | * Machine interface |
| 706 | * List all ust event with their fields |
| 707 | */ |
| 708 | static int mi_list_ust_event_fields(struct lttng_event_field *fields, int count, |
| 709 | struct lttng_domain *domain) |
| 710 | { |
| 711 | int ret, i; |
| 712 | pid_t cur_pid = 0; |
| 713 | char *cmdline = NULL; |
| 714 | int pid_element_open = 0; |
| 715 | int event_element_open = 0; |
| 716 | struct lttng_event cur_event; |
| 717 | |
| 718 | memset(&cur_event, 0, sizeof(cur_event)); |
| 719 | |
| 720 | /* Open domains element */ |
| 721 | ret = mi_lttng_domains_open(the_writer); |
| 722 | if (ret) { |
| 723 | goto end; |
| 724 | } |
| 725 | |
| 726 | /* Write domain */ |
| 727 | ret = mi_lttng_domain(the_writer, domain, 1); |
| 728 | if (ret) { |
| 729 | goto end; |
| 730 | } |
| 731 | |
| 732 | /* Open pids element */ |
| 733 | ret = mi_lttng_pids_open(the_writer); |
| 734 | if (ret) { |
| 735 | goto end; |
| 736 | } |
| 737 | |
| 738 | for (i = 0; i < count; i++) { |
| 739 | if (cur_pid != fields[i].event.pid) { |
| 740 | if (pid_element_open) { |
| 741 | if (event_element_open) { |
| 742 | /* Close the previous field element and event. */ |
| 743 | ret = mi_lttng_close_multi_element( |
| 744 | the_writer, 2); |
| 745 | if (ret) { |
| 746 | goto end; |
| 747 | } |
| 748 | event_element_open = 0; |
| 749 | } |
| 750 | /* Close the previous events, pid element */ |
| 751 | ret = mi_lttng_close_multi_element( |
| 752 | the_writer, 2); |
| 753 | if (ret) { |
| 754 | goto end; |
| 755 | } |
| 756 | pid_element_open = 0; |
| 757 | } |
| 758 | |
| 759 | cur_pid = fields[i].event.pid; |
| 760 | cmdline = get_cmdline_by_pid(cur_pid); |
| 761 | if (!pid_element_open) { |
| 762 | /* Open and write a pid element */ |
| 763 | ret = mi_lttng_pid(the_writer, cur_pid, cmdline, |
| 764 | 1); |
| 765 | if (ret) { |
| 766 | goto error; |
| 767 | } |
| 768 | |
| 769 | /* Open events element */ |
| 770 | ret = mi_lttng_events_open(the_writer); |
| 771 | if (ret) { |
| 772 | goto error; |
| 773 | } |
| 774 | pid_element_open = 1; |
| 775 | } |
| 776 | free(cmdline); |
| 777 | /* Wipe current event since we are about to print a new PID. */ |
| 778 | memset(&cur_event, 0, sizeof(cur_event)); |
| 779 | } |
| 780 | |
| 781 | if (strcmp(cur_event.name, fields[i].event.name) != 0) { |
| 782 | if (event_element_open) { |
| 783 | /* Close the previous fields element and the previous event */ |
| 784 | ret = mi_lttng_close_multi_element( |
| 785 | the_writer, 2); |
| 786 | if (ret) { |
| 787 | goto end; |
| 788 | } |
| 789 | event_element_open = 0; |
| 790 | } |
| 791 | |
| 792 | memcpy(&cur_event, &fields[i].event, |
| 793 | sizeof(cur_event)); |
| 794 | |
| 795 | if (!event_element_open) { |
| 796 | /* Open and write the event */ |
| 797 | ret = mi_lttng_event(the_writer, &cur_event, 1, |
| 798 | the_handle->domain.type); |
| 799 | if (ret) { |
| 800 | goto end; |
| 801 | } |
| 802 | |
| 803 | /* Open a fields element */ |
| 804 | ret = mi_lttng_event_fields_open(the_writer); |
| 805 | if (ret) { |
| 806 | goto end; |
| 807 | } |
| 808 | event_element_open = 1; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | /* Print the event_field */ |
| 813 | ret = mi_lttng_event_field(the_writer, &fields[i]); |
| 814 | if (ret) { |
| 815 | goto end; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | /* Close pids, domain, domains */ |
| 820 | ret = mi_lttng_close_multi_element(the_writer, 3); |
| 821 | end: |
| 822 | return ret; |
| 823 | error: |
| 824 | free(cmdline); |
| 825 | return ret; |
| 826 | } |
| 827 | |
| 828 | /* |
| 829 | * Ask session daemon for all user space tracepoint fields available. |
| 830 | */ |
| 831 | static int list_ust_event_fields(void) |
| 832 | { |
| 833 | int i, size, ret = CMD_SUCCESS; |
| 834 | struct lttng_domain domain; |
| 835 | struct lttng_handle *handle; |
| 836 | struct lttng_event_field *event_field_list; |
| 837 | pid_t cur_pid = 0; |
| 838 | char *cmdline = NULL; |
| 839 | |
| 840 | struct lttng_event cur_event; |
| 841 | |
| 842 | memset(&domain, 0, sizeof(domain)); |
| 843 | memset(&cur_event, 0, sizeof(cur_event)); |
| 844 | |
| 845 | DBG("Getting UST tracing event fields"); |
| 846 | |
| 847 | domain.type = LTTNG_DOMAIN_UST; |
| 848 | |
| 849 | handle = lttng_create_handle(NULL, &domain); |
| 850 | if (handle == NULL) { |
| 851 | ret = CMD_ERROR; |
| 852 | goto end; |
| 853 | } |
| 854 | |
| 855 | size = lttng_list_tracepoint_fields(handle, &event_field_list); |
| 856 | if (size < 0) { |
| 857 | ERR("Unable to list UST event fields: %s", lttng_strerror(size)); |
| 858 | ret = CMD_ERROR; |
| 859 | goto end; |
| 860 | } |
| 861 | |
| 862 | if (lttng_opt_mi) { |
| 863 | /* Mi print */ |
| 864 | ret = mi_list_ust_event_fields(event_field_list, size, &domain); |
| 865 | if (ret) { |
| 866 | ret = CMD_ERROR; |
| 867 | goto error; |
| 868 | } |
| 869 | } else { |
| 870 | /* Pretty print */ |
| 871 | MSG("UST events:\n-------------"); |
| 872 | |
| 873 | if (size == 0) { |
| 874 | MSG("None"); |
| 875 | } |
| 876 | |
| 877 | for (i = 0; i < size; i++) { |
| 878 | if (cur_pid != event_field_list[i].event.pid) { |
| 879 | cur_pid = event_field_list[i].event.pid; |
| 880 | cmdline = get_cmdline_by_pid(cur_pid); |
| 881 | if (cmdline == NULL) { |
| 882 | ret = CMD_ERROR; |
| 883 | goto error; |
| 884 | } |
| 885 | MSG("\nPID: %d - Name: %s", cur_pid, cmdline); |
| 886 | free(cmdline); |
| 887 | /* Wipe current event since we are about to print a new PID. */ |
| 888 | memset(&cur_event, 0, sizeof(cur_event)); |
| 889 | } |
| 890 | if (strcmp(cur_event.name, event_field_list[i].event.name) != 0) { |
| 891 | print_events(&event_field_list[i].event); |
| 892 | memcpy(&cur_event, &event_field_list[i].event, |
| 893 | sizeof(cur_event)); |
| 894 | } |
| 895 | print_event_field(&event_field_list[i]); |
| 896 | } |
| 897 | |
| 898 | MSG(""); |
| 899 | } |
| 900 | |
| 901 | error: |
| 902 | free(event_field_list); |
| 903 | end: |
| 904 | lttng_destroy_handle(handle); |
| 905 | return ret; |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | * Machine interface |
| 910 | * Print a list of kernel events |
| 911 | */ |
| 912 | static int mi_list_kernel_events(struct lttng_event *events, int count, |
| 913 | struct lttng_domain *domain) |
| 914 | { |
| 915 | int ret, i; |
| 916 | |
| 917 | /* Open domains element */ |
| 918 | ret = mi_lttng_domains_open(the_writer); |
| 919 | if (ret) { |
| 920 | goto end; |
| 921 | } |
| 922 | |
| 923 | /* Write domain */ |
| 924 | ret = mi_lttng_domain(the_writer, domain, 1); |
| 925 | if (ret) { |
| 926 | goto end; |
| 927 | } |
| 928 | |
| 929 | /* Open events */ |
| 930 | ret = mi_lttng_events_open(the_writer); |
| 931 | if (ret) { |
| 932 | goto end; |
| 933 | } |
| 934 | |
| 935 | for (i = 0; i < count; i++) { |
| 936 | ret = mi_lttng_event(the_writer, &events[i], 0, |
| 937 | the_handle->domain.type); |
| 938 | if (ret) { |
| 939 | goto end; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | /* close events, domain and domains */ |
| 944 | ret = mi_lttng_close_multi_element(the_writer, 3); |
| 945 | if (ret) { |
| 946 | goto end; |
| 947 | } |
| 948 | |
| 949 | end: |
| 950 | return ret; |
| 951 | } |
| 952 | |
| 953 | /* |
| 954 | * Ask for all trace events in the kernel |
| 955 | */ |
| 956 | static int list_kernel_events(void) |
| 957 | { |
| 958 | int i, size, ret = CMD_SUCCESS; |
| 959 | struct lttng_domain domain; |
| 960 | struct lttng_handle *handle; |
| 961 | struct lttng_event *event_list; |
| 962 | |
| 963 | memset(&domain, 0, sizeof(domain)); |
| 964 | |
| 965 | DBG("Getting kernel tracing events"); |
| 966 | |
| 967 | domain.type = LTTNG_DOMAIN_KERNEL; |
| 968 | |
| 969 | handle = lttng_create_handle(NULL, &domain); |
| 970 | if (handle == NULL) { |
| 971 | ret = CMD_ERROR; |
| 972 | goto error; |
| 973 | } |
| 974 | |
| 975 | size = lttng_list_tracepoints(handle, &event_list); |
| 976 | if (size < 0) { |
| 977 | ERR("Unable to list kernel events: %s", lttng_strerror(size)); |
| 978 | lttng_destroy_handle(handle); |
| 979 | return CMD_ERROR; |
| 980 | } |
| 981 | |
| 982 | if (lttng_opt_mi) { |
| 983 | /* Mi print */ |
| 984 | ret = mi_list_kernel_events(event_list, size, &domain); |
| 985 | if (ret) { |
| 986 | ret = CMD_ERROR; |
| 987 | goto end; |
| 988 | } |
| 989 | } else { |
| 990 | MSG("Kernel events:\n-------------"); |
| 991 | |
| 992 | for (i = 0; i < size; i++) { |
| 993 | print_events(&event_list[i]); |
| 994 | } |
| 995 | |
| 996 | MSG(""); |
| 997 | } |
| 998 | |
| 999 | end: |
| 1000 | free(event_list); |
| 1001 | |
| 1002 | lttng_destroy_handle(handle); |
| 1003 | return ret; |
| 1004 | |
| 1005 | error: |
| 1006 | lttng_destroy_handle(handle); |
| 1007 | return ret; |
| 1008 | } |
| 1009 | |
| 1010 | /* |
| 1011 | * Machine interface |
| 1012 | * Print a list of system calls. |
| 1013 | */ |
| 1014 | static int mi_list_syscalls(struct lttng_event *events, int count) |
| 1015 | { |
| 1016 | int ret, i; |
| 1017 | |
| 1018 | /* Open events */ |
| 1019 | ret = mi_lttng_events_open(the_writer); |
| 1020 | if (ret) { |
| 1021 | goto end; |
| 1022 | } |
| 1023 | |
| 1024 | for (i = 0; i < count; i++) { |
| 1025 | ret = mi_lttng_event(the_writer, &events[i], 0, |
| 1026 | the_handle->domain.type); |
| 1027 | if (ret) { |
| 1028 | goto end; |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | /* Close events. */ |
| 1033 | ret = mi_lttng_writer_close_element(the_writer); |
| 1034 | if (ret) { |
| 1035 | goto end; |
| 1036 | } |
| 1037 | |
| 1038 | end: |
| 1039 | return ret; |
| 1040 | } |
| 1041 | |
| 1042 | /* |
| 1043 | * Ask for kernel system calls. |
| 1044 | */ |
| 1045 | static int list_syscalls(void) |
| 1046 | { |
| 1047 | int i, size, ret = CMD_SUCCESS; |
| 1048 | struct lttng_event *event_list; |
| 1049 | |
| 1050 | DBG("Getting kernel system call events"); |
| 1051 | |
| 1052 | size = lttng_list_syscalls(&event_list); |
| 1053 | if (size < 0) { |
| 1054 | ERR("Unable to list system calls: %s", lttng_strerror(size)); |
| 1055 | ret = CMD_ERROR; |
| 1056 | goto error; |
| 1057 | } |
| 1058 | |
| 1059 | if (lttng_opt_mi) { |
| 1060 | /* Mi print */ |
| 1061 | ret = mi_list_syscalls(event_list, size); |
| 1062 | if (ret) { |
| 1063 | ret = CMD_ERROR; |
| 1064 | goto end; |
| 1065 | } |
| 1066 | } else { |
| 1067 | MSG("System calls:\n-------------"); |
| 1068 | |
| 1069 | for (i = 0; i < size; i++) { |
| 1070 | print_events(&event_list[i]); |
| 1071 | } |
| 1072 | |
| 1073 | MSG(""); |
| 1074 | } |
| 1075 | |
| 1076 | end: |
| 1077 | free(event_list); |
| 1078 | return ret; |
| 1079 | |
| 1080 | error: |
| 1081 | return ret; |
| 1082 | } |
| 1083 | |
| 1084 | /* |
| 1085 | * Machine Interface |
| 1086 | * Print a list of agent events |
| 1087 | */ |
| 1088 | static int mi_list_session_agent_events(struct lttng_event *events, int count) |
| 1089 | { |
| 1090 | int ret, i; |
| 1091 | |
| 1092 | /* Open events element */ |
| 1093 | ret = mi_lttng_events_open(the_writer); |
| 1094 | if (ret) { |
| 1095 | goto end; |
| 1096 | } |
| 1097 | |
| 1098 | for (i = 0; i < count; i++) { |
| 1099 | ret = mi_lttng_event(the_writer, &events[i], 0, |
| 1100 | the_handle->domain.type); |
| 1101 | if (ret) { |
| 1102 | goto end; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | /* Close events element */ |
| 1107 | ret = mi_lttng_writer_close_element(the_writer); |
| 1108 | |
| 1109 | end: |
| 1110 | return ret; |
| 1111 | } |
| 1112 | |
| 1113 | /* |
| 1114 | * List agent events for a specific session using the handle. |
| 1115 | * |
| 1116 | * Return CMD_SUCCESS on success else a negative value. |
| 1117 | */ |
| 1118 | static int list_session_agent_events(void) |
| 1119 | { |
| 1120 | int ret = CMD_SUCCESS, count, i; |
| 1121 | struct lttng_event *events = NULL; |
| 1122 | |
| 1123 | count = lttng_list_events(the_handle, "", &events); |
| 1124 | if (count < 0) { |
| 1125 | ret = CMD_ERROR; |
| 1126 | ERR("%s", lttng_strerror(count)); |
| 1127 | goto error; |
| 1128 | } |
| 1129 | |
| 1130 | if (lttng_opt_mi) { |
| 1131 | /* Mi print */ |
| 1132 | ret = mi_list_session_agent_events(events, count); |
| 1133 | if (ret) { |
| 1134 | ret = CMD_ERROR; |
| 1135 | goto end; |
| 1136 | } |
| 1137 | } else { |
| 1138 | /* Pretty print */ |
| 1139 | MSG("Events (Logger name):\n---------------------"); |
| 1140 | if (count == 0) { |
| 1141 | MSG("%sNone\n", indent6); |
| 1142 | goto end; |
| 1143 | } |
| 1144 | |
| 1145 | for (i = 0; i < count; i++) { |
| 1146 | const char *filter_str; |
| 1147 | char *filter_msg = NULL; |
| 1148 | struct lttng_event *event = &events[i]; |
| 1149 | |
| 1150 | ret = lttng_event_get_filter_expression(event, |
| 1151 | &filter_str); |
| 1152 | if (ret) { |
| 1153 | filter_msg = strdup(" [failed to retrieve filter]"); |
| 1154 | } else if (filter_str) { |
| 1155 | const char * const filter_fmt = |
| 1156 | " [filter: '%s']"; |
| 1157 | |
| 1158 | filter_msg = malloc(strlen(filter_str) + |
| 1159 | strlen(filter_fmt) + 1); |
| 1160 | if (filter_msg) { |
| 1161 | sprintf(filter_msg, filter_fmt, |
| 1162 | filter_str); |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | if (event->loglevel_type != |
| 1167 | LTTNG_EVENT_LOGLEVEL_ALL) { |
| 1168 | MSG("%s- %s%s (loglevel%s %s)%s", indent4, |
| 1169 | event->name, |
| 1170 | enabled_string(event->enabled), |
| 1171 | logleveltype_string( |
| 1172 | event->loglevel_type), |
| 1173 | mi_lttng_loglevel_string( |
| 1174 | event->loglevel, |
| 1175 | the_handle->domain |
| 1176 | .type), |
| 1177 | safe_string(filter_msg)); |
| 1178 | } else { |
| 1179 | MSG("%s- %s%s%s", indent4, event->name, |
| 1180 | enabled_string(event->enabled), |
| 1181 | safe_string(filter_msg)); |
| 1182 | } |
| 1183 | free(filter_msg); |
| 1184 | } |
| 1185 | |
| 1186 | MSG(""); |
| 1187 | } |
| 1188 | |
| 1189 | end: |
| 1190 | free(events); |
| 1191 | error: |
| 1192 | return ret; |
| 1193 | } |
| 1194 | |
| 1195 | /* |
| 1196 | * Machine interface |
| 1197 | * print a list of event |
| 1198 | */ |
| 1199 | static int mi_list_events(struct lttng_event *events, int count) |
| 1200 | { |
| 1201 | int ret, i; |
| 1202 | |
| 1203 | /* Open events element */ |
| 1204 | ret = mi_lttng_events_open(the_writer); |
| 1205 | if (ret) { |
| 1206 | goto end; |
| 1207 | } |
| 1208 | |
| 1209 | for (i = 0; i < count; i++) { |
| 1210 | ret = mi_lttng_event(the_writer, &events[i], 0, |
| 1211 | the_handle->domain.type); |
| 1212 | if (ret) { |
| 1213 | goto end; |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | /* Close events element */ |
| 1218 | ret = mi_lttng_writer_close_element(the_writer); |
| 1219 | |
| 1220 | end: |
| 1221 | return ret; |
| 1222 | } |
| 1223 | |
| 1224 | /* |
| 1225 | * List events of channel of session and domain. |
| 1226 | */ |
| 1227 | static int list_events(const char *channel_name) |
| 1228 | { |
| 1229 | int ret = CMD_SUCCESS, count, i; |
| 1230 | struct lttng_event *events = NULL; |
| 1231 | |
| 1232 | count = lttng_list_events(the_handle, channel_name, &events); |
| 1233 | if (count < 0) { |
| 1234 | ret = CMD_ERROR; |
| 1235 | ERR("%s", lttng_strerror(count)); |
| 1236 | goto error; |
| 1237 | } |
| 1238 | |
| 1239 | if (lttng_opt_mi) { |
| 1240 | /* Mi print */ |
| 1241 | ret = mi_list_events(events, count); |
| 1242 | if (ret) { |
| 1243 | ret = CMD_ERROR; |
| 1244 | goto end; |
| 1245 | } |
| 1246 | } else { |
| 1247 | /* Pretty print */ |
| 1248 | MSG("\n%sRecording event rules:", indent4); |
| 1249 | if (count == 0) { |
| 1250 | MSG("%sNone\n", indent6); |
| 1251 | goto end; |
| 1252 | } |
| 1253 | |
| 1254 | for (i = 0; i < count; i++) { |
| 1255 | print_events(&events[i]); |
| 1256 | } |
| 1257 | |
| 1258 | MSG(""); |
| 1259 | } |
| 1260 | end: |
| 1261 | free(events); |
| 1262 | error: |
| 1263 | return ret; |
| 1264 | } |
| 1265 | |
| 1266 | static |
| 1267 | void print_timer(const char *timer_name, uint32_t space_count, int64_t value) |
| 1268 | { |
| 1269 | uint32_t i; |
| 1270 | |
| 1271 | _MSG("%s%s:", indent6, timer_name); |
| 1272 | for (i = 0; i < space_count; i++) { |
| 1273 | _MSG(" "); |
| 1274 | } |
| 1275 | |
| 1276 | if (value) { |
| 1277 | MSG("%" PRId64 " %s", value, USEC_UNIT); |
| 1278 | } else { |
| 1279 | MSG("inactive"); |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | /* |
| 1284 | * Pretty print channel |
| 1285 | */ |
| 1286 | static void print_channel(struct lttng_channel *channel) |
| 1287 | { |
| 1288 | int ret; |
| 1289 | uint64_t discarded_events, lost_packets, monitor_timer_interval; |
| 1290 | int64_t blocking_timeout; |
| 1291 | |
| 1292 | ret = lttng_channel_get_discarded_event_count(channel, |
| 1293 | &discarded_events); |
| 1294 | if (ret) { |
| 1295 | ERR("Failed to retrieve discarded event count of channel"); |
| 1296 | return; |
| 1297 | } |
| 1298 | |
| 1299 | ret = lttng_channel_get_lost_packet_count(channel, |
| 1300 | &lost_packets); |
| 1301 | if (ret) { |
| 1302 | ERR("Failed to retrieve lost packet count of channel"); |
| 1303 | return; |
| 1304 | } |
| 1305 | |
| 1306 | ret = lttng_channel_get_monitor_timer_interval(channel, |
| 1307 | &monitor_timer_interval); |
| 1308 | if (ret) { |
| 1309 | ERR("Failed to retrieve monitor interval of channel"); |
| 1310 | return; |
| 1311 | } |
| 1312 | |
| 1313 | ret = lttng_channel_get_blocking_timeout(channel, |
| 1314 | &blocking_timeout); |
| 1315 | if (ret) { |
| 1316 | ERR("Failed to retrieve blocking timeout of channel"); |
| 1317 | return; |
| 1318 | } |
| 1319 | |
| 1320 | MSG("- %s:%s\n", channel->name, enabled_string(channel->enabled)); |
| 1321 | MSG("%sAttributes:", indent4); |
| 1322 | MSG("%sEvent-loss mode: %s", indent6, channel->attr.overwrite ? "overwrite" : "discard"); |
| 1323 | MSG("%sSub-buffer size: %" PRIu64 " bytes", indent6, channel->attr.subbuf_size); |
| 1324 | MSG("%sSub-buffer count: %" PRIu64, indent6, channel->attr.num_subbuf); |
| 1325 | |
| 1326 | print_timer("Switch timer", 5, channel->attr.switch_timer_interval); |
| 1327 | print_timer("Read timer", 7, channel->attr.read_timer_interval); |
| 1328 | print_timer("Monitor timer", 4, monitor_timer_interval); |
| 1329 | |
| 1330 | if (!channel->attr.overwrite) { |
| 1331 | if (blocking_timeout == -1) { |
| 1332 | MSG("%sBlocking timeout: infinite", indent6); |
| 1333 | } else { |
| 1334 | MSG("%sBlocking timeout: %" PRId64 " %s", indent6, |
| 1335 | blocking_timeout, USEC_UNIT); |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | MSG("%sTrace file count: %" PRIu64 " per stream", indent6, |
| 1340 | channel->attr.tracefile_count == 0 ? |
| 1341 | 1 : channel->attr.tracefile_count); |
| 1342 | if (channel->attr.tracefile_size != 0 ) { |
| 1343 | MSG("%sTrace file size: %" PRIu64 " bytes", indent6, |
| 1344 | channel->attr.tracefile_size); |
| 1345 | } else { |
| 1346 | MSG("%sTrace file size: %s", indent6, "unlimited"); |
| 1347 | } |
| 1348 | switch (channel->attr.output) { |
| 1349 | case LTTNG_EVENT_SPLICE: |
| 1350 | MSG("%sOutput mode: splice", indent6); |
| 1351 | break; |
| 1352 | case LTTNG_EVENT_MMAP: |
| 1353 | MSG("%sOutput mode: mmap", indent6); |
| 1354 | break; |
| 1355 | } |
| 1356 | |
| 1357 | MSG("\n%sStatistics:", indent4); |
| 1358 | if (the_listed_session.snapshot_mode) { |
| 1359 | /* |
| 1360 | * The lost packet count is omitted for sessions in snapshot |
| 1361 | * mode as it is misleading: it would indicate the number of |
| 1362 | * packets that the consumer could not extract during the |
| 1363 | * course of recording the snapshot. It does not have the |
| 1364 | * same meaning as the "regular" lost packet count that |
| 1365 | * would result from the consumer not keeping up with |
| 1366 | * event production in an overwrite-mode channel. |
| 1367 | * |
| 1368 | * A more interesting statistic would be the number of |
| 1369 | * packets lost between the first and last extracted |
| 1370 | * packets of a given snapshot (which prevents most analyses). |
| 1371 | */ |
| 1372 | MSG("%sNone", indent6); |
| 1373 | goto skip_stats_printing; |
| 1374 | } |
| 1375 | |
| 1376 | if (!channel->attr.overwrite) { |
| 1377 | MSG("%sDiscarded events: %" PRIu64, indent6, discarded_events); |
| 1378 | } else { |
| 1379 | MSG("%sLost packets: %" PRIu64, indent6, lost_packets); |
| 1380 | } |
| 1381 | skip_stats_printing: |
| 1382 | return; |
| 1383 | } |
| 1384 | |
| 1385 | /* |
| 1386 | * Machine interface |
| 1387 | * Print a list of channel |
| 1388 | * |
| 1389 | */ |
| 1390 | static int mi_list_channels(struct lttng_channel *channels, int count, |
| 1391 | const char *channel_name) |
| 1392 | { |
| 1393 | int i, ret; |
| 1394 | unsigned int chan_found = 0; |
| 1395 | |
| 1396 | /* Open channels element */ |
| 1397 | ret = mi_lttng_channels_open(the_writer); |
| 1398 | if (ret) { |
| 1399 | goto error; |
| 1400 | } |
| 1401 | |
| 1402 | for (i = 0; i < count; i++) { |
| 1403 | if (channel_name != NULL) { |
| 1404 | if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) { |
| 1405 | chan_found = 1; |
| 1406 | } else { |
| 1407 | continue; |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | /* Write channel element and leave it open */ |
| 1412 | ret = mi_lttng_channel(the_writer, &channels[i], 1); |
| 1413 | if (ret) { |
| 1414 | goto error; |
| 1415 | } |
| 1416 | |
| 1417 | /* Listing events per channel */ |
| 1418 | ret = list_events(channels[i].name); |
| 1419 | if (ret) { |
| 1420 | goto error; |
| 1421 | } |
| 1422 | |
| 1423 | /* Closing the channel element we opened earlier */ |
| 1424 | ret = mi_lttng_writer_close_element(the_writer); |
| 1425 | if (ret) { |
| 1426 | goto error; |
| 1427 | } |
| 1428 | |
| 1429 | if (chan_found) { |
| 1430 | break; |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | /* Close channels element */ |
| 1435 | ret = mi_lttng_writer_close_element(the_writer); |
| 1436 | if (ret) { |
| 1437 | goto error; |
| 1438 | } |
| 1439 | |
| 1440 | error: |
| 1441 | return ret; |
| 1442 | } |
| 1443 | |
| 1444 | /* |
| 1445 | * List channel(s) of session and domain. |
| 1446 | * |
| 1447 | * If channel_name is NULL, all channels are listed. |
| 1448 | */ |
| 1449 | static int list_channels(const char *channel_name) |
| 1450 | { |
| 1451 | int count, i, ret = CMD_SUCCESS; |
| 1452 | unsigned int chan_found = 0; |
| 1453 | struct lttng_channel *channels = NULL; |
| 1454 | |
| 1455 | DBG("Listing channel(s) (%s)", channel_name ? : "<all>"); |
| 1456 | |
| 1457 | count = lttng_list_channels(the_handle, &channels); |
| 1458 | if (count < 0) { |
| 1459 | switch (-count) { |
| 1460 | case LTTNG_ERR_KERN_CHAN_NOT_FOUND: |
| 1461 | if (lttng_opt_mi) { |
| 1462 | /* When printing mi this is not an error |
| 1463 | * but an empty channels element */ |
| 1464 | count = 0; |
| 1465 | } else { |
| 1466 | ret = CMD_SUCCESS; |
| 1467 | goto error_channels; |
| 1468 | } |
| 1469 | break; |
| 1470 | default: |
| 1471 | /* We had a real error */ |
| 1472 | ret = CMD_ERROR; |
| 1473 | ERR("%s", lttng_strerror(count)); |
| 1474 | goto error_channels; |
| 1475 | break; |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | if (lttng_opt_mi) { |
| 1480 | /* Mi print */ |
| 1481 | ret = mi_list_channels(channels, count, channel_name); |
| 1482 | if (ret) { |
| 1483 | ret = CMD_ERROR; |
| 1484 | goto error; |
| 1485 | } |
| 1486 | } else { |
| 1487 | /* Pretty print */ |
| 1488 | if (count) { |
| 1489 | MSG("Channels:\n-------------"); |
| 1490 | } |
| 1491 | |
| 1492 | for (i = 0; i < count; i++) { |
| 1493 | if (channel_name != NULL) { |
| 1494 | if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) { |
| 1495 | chan_found = 1; |
| 1496 | } else { |
| 1497 | continue; |
| 1498 | } |
| 1499 | } |
| 1500 | print_channel(&channels[i]); |
| 1501 | |
| 1502 | /* Listing events per channel */ |
| 1503 | ret = list_events(channels[i].name); |
| 1504 | if (ret) { |
| 1505 | goto error; |
| 1506 | } |
| 1507 | |
| 1508 | if (chan_found) { |
| 1509 | break; |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | if (!chan_found && channel_name != NULL) { |
| 1514 | ret = CMD_ERROR; |
| 1515 | ERR("Channel %s not found", channel_name); |
| 1516 | goto error; |
| 1517 | } |
| 1518 | } |
| 1519 | error: |
| 1520 | free(channels); |
| 1521 | |
| 1522 | error_channels: |
| 1523 | return ret; |
| 1524 | } |
| 1525 | |
| 1526 | static const char *get_capitalized_process_attr_str(enum lttng_process_attr process_attr) |
| 1527 | { |
| 1528 | switch (process_attr) { |
| 1529 | case LTTNG_PROCESS_ATTR_PROCESS_ID: |
| 1530 | return "Process ID"; |
| 1531 | case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID: |
| 1532 | return "Virtual process ID"; |
| 1533 | case LTTNG_PROCESS_ATTR_USER_ID: |
| 1534 | return "User ID"; |
| 1535 | case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID: |
| 1536 | return "Virtual user ID"; |
| 1537 | case LTTNG_PROCESS_ATTR_GROUP_ID: |
| 1538 | return "Group ID"; |
| 1539 | case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID: |
| 1540 | return "Virtual group ID"; |
| 1541 | default: |
| 1542 | return "Unknown"; |
| 1543 | } |
| 1544 | return NULL; |
| 1545 | } |
| 1546 | |
| 1547 | static int handle_process_attr_status(enum lttng_process_attr process_attr, |
| 1548 | enum lttng_process_attr_tracker_handle_status status) |
| 1549 | { |
| 1550 | int ret = CMD_SUCCESS; |
| 1551 | |
| 1552 | switch (status) { |
| 1553 | case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID_TRACKING_POLICY: |
| 1554 | case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK: |
| 1555 | /* Carry on. */ |
| 1556 | break; |
| 1557 | case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_COMMUNICATION_ERROR: |
| 1558 | ERR("Communication occurred while fetching %s tracker", |
| 1559 | lttng_process_attr_to_string(process_attr)); |
| 1560 | ret = CMD_ERROR; |
| 1561 | break; |
| 1562 | case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_SESSION_DOES_NOT_EXIST: |
| 1563 | ERR("Failed to get the inclusion set of the %s tracker: session `%s` no longer exists", |
| 1564 | lttng_process_attr_to_string(process_attr), |
| 1565 | the_handle->session_name); |
| 1566 | ret = CMD_ERROR; |
| 1567 | break; |
| 1568 | default: |
| 1569 | ERR("Unknown error occurred while fetching the inclusion set of the %s tracker", |
| 1570 | lttng_process_attr_to_string(process_attr)); |
| 1571 | ret = CMD_ERROR; |
| 1572 | break; |
| 1573 | } |
| 1574 | |
| 1575 | return ret; |
| 1576 | } |
| 1577 | |
| 1578 | static int mi_output_empty_tracker(enum lttng_process_attr process_attr) |
| 1579 | { |
| 1580 | int ret; |
| 1581 | |
| 1582 | ret = mi_lttng_process_attribute_tracker_open(the_writer, process_attr); |
| 1583 | if (ret) { |
| 1584 | goto end; |
| 1585 | } |
| 1586 | |
| 1587 | ret = mi_lttng_close_multi_element(the_writer, 2); |
| 1588 | end: |
| 1589 | return ret; |
| 1590 | } |
| 1591 | |
| 1592 | static inline bool is_value_type_name( |
| 1593 | enum lttng_process_attr_value_type value_type) |
| 1594 | { |
| 1595 | return value_type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME || |
| 1596 | value_type == LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME; |
| 1597 | } |
| 1598 | |
| 1599 | /* |
| 1600 | * List a process attribute tracker for a session and domain tuple. |
| 1601 | */ |
| 1602 | static int list_process_attr_tracker(enum lttng_process_attr process_attr) |
| 1603 | { |
| 1604 | int ret = 0; |
| 1605 | unsigned int count, i; |
| 1606 | enum lttng_tracking_policy policy; |
| 1607 | enum lttng_error_code ret_code; |
| 1608 | enum lttng_process_attr_tracker_handle_status handle_status; |
| 1609 | enum lttng_process_attr_values_status values_status; |
| 1610 | const struct lttng_process_attr_values *values; |
| 1611 | struct lttng_process_attr_tracker_handle *tracker_handle = NULL; |
| 1612 | |
| 1613 | ret_code = lttng_session_get_tracker_handle(the_handle->session_name, |
| 1614 | the_handle->domain.type, process_attr, &tracker_handle); |
| 1615 | if (ret_code != LTTNG_OK) { |
| 1616 | ERR("Failed to get process attribute tracker handle: %s", |
| 1617 | lttng_strerror(ret_code)); |
| 1618 | ret = CMD_ERROR; |
| 1619 | goto end; |
| 1620 | } |
| 1621 | |
| 1622 | handle_status = lttng_process_attr_tracker_handle_get_inclusion_set( |
| 1623 | tracker_handle, &values); |
| 1624 | ret = handle_process_attr_status(process_attr, handle_status); |
| 1625 | if (ret != CMD_SUCCESS) { |
| 1626 | goto end; |
| 1627 | } |
| 1628 | |
| 1629 | handle_status = lttng_process_attr_tracker_handle_get_tracking_policy( |
| 1630 | tracker_handle, &policy); |
| 1631 | ret = handle_process_attr_status(process_attr, handle_status); |
| 1632 | if (ret != CMD_SUCCESS) { |
| 1633 | goto end; |
| 1634 | } |
| 1635 | |
| 1636 | { |
| 1637 | char *process_attr_name; |
| 1638 | const int print_ret = asprintf(&process_attr_name, "%ss:", |
| 1639 | get_capitalized_process_attr_str(process_attr)); |
| 1640 | |
| 1641 | if (print_ret == -1) { |
| 1642 | ret = CMD_FATAL; |
| 1643 | goto end; |
| 1644 | } |
| 1645 | _MSG(" %-22s", process_attr_name); |
| 1646 | free(process_attr_name); |
| 1647 | } |
| 1648 | switch (policy) { |
| 1649 | case LTTNG_TRACKING_POLICY_INCLUDE_SET: |
| 1650 | break; |
| 1651 | case LTTNG_TRACKING_POLICY_EXCLUDE_ALL: |
| 1652 | if (the_writer) { |
| 1653 | mi_output_empty_tracker(process_attr); |
| 1654 | } |
| 1655 | MSG("none"); |
| 1656 | ret = CMD_SUCCESS; |
| 1657 | goto end; |
| 1658 | case LTTNG_TRACKING_POLICY_INCLUDE_ALL: |
| 1659 | MSG("all"); |
| 1660 | ret = CMD_SUCCESS; |
| 1661 | goto end; |
| 1662 | default: |
| 1663 | ERR("Unknown tracking policy encoutered while listing the %s process attribute tracker of session `%s`", |
| 1664 | lttng_process_attr_to_string(process_attr), |
| 1665 | the_handle->session_name); |
| 1666 | ret = CMD_FATAL; |
| 1667 | goto end; |
| 1668 | } |
| 1669 | |
| 1670 | values_status = lttng_process_attr_values_get_count(values, &count); |
| 1671 | if (values_status != LTTNG_PROCESS_ATTR_VALUES_STATUS_OK) { |
| 1672 | ERR("Failed to get the count of values in the inclusion set of the %s process attribute tracker of session `%s`", |
| 1673 | lttng_process_attr_to_string(process_attr), |
| 1674 | the_handle->session_name); |
| 1675 | ret = CMD_FATAL; |
| 1676 | goto end; |
| 1677 | } |
| 1678 | |
| 1679 | if (count == 0) { |
| 1680 | /* Functionally equivalent to the 'exclude all' policy. */ |
| 1681 | if (the_writer) { |
| 1682 | mi_output_empty_tracker(process_attr); |
| 1683 | } |
| 1684 | MSG("none"); |
| 1685 | ret = CMD_SUCCESS; |
| 1686 | goto end; |
| 1687 | } |
| 1688 | |
| 1689 | /* Mi tracker_id element */ |
| 1690 | if (the_writer) { |
| 1691 | /* Open tracker_id and targets elements */ |
| 1692 | ret = mi_lttng_process_attribute_tracker_open( |
| 1693 | the_writer, process_attr); |
| 1694 | if (ret) { |
| 1695 | goto end; |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | for (i = 0; i < count; i++) { |
| 1700 | const enum lttng_process_attr_value_type value_type = |
| 1701 | lttng_process_attr_values_get_type_at_index( |
| 1702 | values, i); |
| 1703 | int64_t integral_value = INT64_MAX; |
| 1704 | const char *name = "error"; |
| 1705 | |
| 1706 | if (i >= 1) { |
| 1707 | _MSG(", "); |
| 1708 | } |
| 1709 | switch (value_type) { |
| 1710 | case LTTNG_PROCESS_ATTR_VALUE_TYPE_PID: |
| 1711 | { |
| 1712 | pid_t pid; |
| 1713 | |
| 1714 | values_status = lttng_process_attr_values_get_pid_at_index( |
| 1715 | values, i, &pid); |
| 1716 | integral_value = (int64_t) pid; |
| 1717 | break; |
| 1718 | } |
| 1719 | case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID: |
| 1720 | { |
| 1721 | uid_t uid; |
| 1722 | |
| 1723 | values_status = lttng_process_attr_values_get_uid_at_index( |
| 1724 | values, i, &uid); |
| 1725 | integral_value = (int64_t) uid; |
| 1726 | break; |
| 1727 | } |
| 1728 | case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID: |
| 1729 | { |
| 1730 | gid_t gid; |
| 1731 | |
| 1732 | values_status = lttng_process_attr_values_get_gid_at_index( |
| 1733 | values, i, &gid); |
| 1734 | integral_value = (int64_t) gid; |
| 1735 | break; |
| 1736 | } |
| 1737 | case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME: |
| 1738 | values_status = lttng_process_attr_values_get_user_name_at_index( |
| 1739 | values, i, &name); |
| 1740 | break; |
| 1741 | case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME: |
| 1742 | values_status = lttng_process_attr_values_get_group_name_at_index( |
| 1743 | values, i, &name); |
| 1744 | break; |
| 1745 | default: |
| 1746 | ret = CMD_ERROR; |
| 1747 | goto end; |
| 1748 | } |
| 1749 | |
| 1750 | if (values_status != LTTNG_PROCESS_ATTR_VALUES_STATUS_OK) { |
| 1751 | /* |
| 1752 | * Not possible given the current liblttng-ctl |
| 1753 | * implementation. |
| 1754 | */ |
| 1755 | ERR("Unknown error occurred while fetching process attribute value in inclusion list"); |
| 1756 | ret = CMD_FATAL; |
| 1757 | goto end; |
| 1758 | } |
| 1759 | |
| 1760 | if (is_value_type_name(value_type)) { |
| 1761 | _MSG("`%s`", name); |
| 1762 | } else { |
| 1763 | _MSG("%" PRIi64, integral_value); |
| 1764 | } |
| 1765 | |
| 1766 | /* Mi */ |
| 1767 | if (the_writer) { |
| 1768 | ret = is_value_type_name(value_type) ? |
| 1769 | mi_lttng_string_process_attribute_value( |
| 1770 | the_writer, |
| 1771 | process_attr, name, |
| 1772 | false) : |
| 1773 | mi_lttng_integral_process_attribute_value( |
| 1774 | the_writer, |
| 1775 | process_attr, |
| 1776 | integral_value, false); |
| 1777 | if (ret) { |
| 1778 | goto end; |
| 1779 | } |
| 1780 | } |
| 1781 | } |
| 1782 | MSG(""); |
| 1783 | |
| 1784 | /* Mi close tracker_id and targets */ |
| 1785 | if (the_writer) { |
| 1786 | ret = mi_lttng_close_multi_element(the_writer, 2); |
| 1787 | if (ret) { |
| 1788 | goto end; |
| 1789 | } |
| 1790 | } |
| 1791 | end: |
| 1792 | lttng_process_attr_tracker_handle_destroy(tracker_handle); |
| 1793 | return ret; |
| 1794 | } |
| 1795 | |
| 1796 | /* |
| 1797 | * List all trackers of a domain |
| 1798 | */ |
| 1799 | static int list_trackers(const struct lttng_domain *domain) |
| 1800 | { |
| 1801 | int ret = 0; |
| 1802 | |
| 1803 | MSG("Tracked process attributes"); |
| 1804 | /* Trackers listing */ |
| 1805 | if (lttng_opt_mi) { |
| 1806 | ret = mi_lttng_trackers_open(the_writer); |
| 1807 | if (ret) { |
| 1808 | goto end; |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | switch (domain->type) { |
| 1813 | case LTTNG_DOMAIN_KERNEL: |
| 1814 | /* pid tracker */ |
| 1815 | ret = list_process_attr_tracker(LTTNG_PROCESS_ATTR_PROCESS_ID); |
| 1816 | if (ret) { |
| 1817 | goto end; |
| 1818 | } |
| 1819 | /* vpid tracker */ |
| 1820 | ret = list_process_attr_tracker( |
| 1821 | LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID); |
| 1822 | if (ret) { |
| 1823 | goto end; |
| 1824 | } |
| 1825 | /* uid tracker */ |
| 1826 | ret = list_process_attr_tracker(LTTNG_PROCESS_ATTR_USER_ID); |
| 1827 | if (ret) { |
| 1828 | goto end; |
| 1829 | } |
| 1830 | /* vuid tracker */ |
| 1831 | ret = list_process_attr_tracker( |
| 1832 | LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID); |
| 1833 | if (ret) { |
| 1834 | goto end; |
| 1835 | } |
| 1836 | /* gid tracker */ |
| 1837 | ret = list_process_attr_tracker(LTTNG_PROCESS_ATTR_GROUP_ID); |
| 1838 | if (ret) { |
| 1839 | goto end; |
| 1840 | } |
| 1841 | /* vgid tracker */ |
| 1842 | ret = list_process_attr_tracker( |
| 1843 | LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID); |
| 1844 | if (ret) { |
| 1845 | goto end; |
| 1846 | } |
| 1847 | break; |
| 1848 | case LTTNG_DOMAIN_UST: |
| 1849 | /* vpid tracker */ |
| 1850 | ret = list_process_attr_tracker( |
| 1851 | LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID); |
| 1852 | if (ret) { |
| 1853 | goto end; |
| 1854 | } |
| 1855 | /* vuid tracker */ |
| 1856 | ret = list_process_attr_tracker( |
| 1857 | LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID); |
| 1858 | if (ret) { |
| 1859 | goto end; |
| 1860 | } |
| 1861 | /* vgid tracker */ |
| 1862 | ret = list_process_attr_tracker( |
| 1863 | LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID); |
| 1864 | if (ret) { |
| 1865 | goto end; |
| 1866 | } |
| 1867 | break; |
| 1868 | default: |
| 1869 | break; |
| 1870 | } |
| 1871 | MSG(); |
| 1872 | if (lttng_opt_mi) { |
| 1873 | /* Close trackers element */ |
| 1874 | ret = mi_lttng_writer_close_element(the_writer); |
| 1875 | if (ret) { |
| 1876 | goto end; |
| 1877 | } |
| 1878 | } |
| 1879 | |
| 1880 | end: |
| 1881 | return ret; |
| 1882 | } |
| 1883 | |
| 1884 | static enum cmd_error_code print_periodic_rotation_schedule( |
| 1885 | const struct lttng_rotation_schedule *schedule) |
| 1886 | { |
| 1887 | enum cmd_error_code ret; |
| 1888 | enum lttng_rotation_status status; |
| 1889 | uint64_t value; |
| 1890 | |
| 1891 | status = lttng_rotation_schedule_periodic_get_period(schedule, |
| 1892 | &value); |
| 1893 | if (status != LTTNG_ROTATION_STATUS_OK) { |
| 1894 | ERR("Failed to retrieve period parameter from periodic rotation schedule."); |
| 1895 | ret = CMD_ERROR; |
| 1896 | goto end; |
| 1897 | } |
| 1898 | |
| 1899 | MSG(" timer period: %" PRIu64" %s", value, USEC_UNIT); |
| 1900 | ret = CMD_SUCCESS; |
| 1901 | end: |
| 1902 | return ret; |
| 1903 | } |
| 1904 | |
| 1905 | static enum cmd_error_code print_size_threshold_rotation_schedule( |
| 1906 | const struct lttng_rotation_schedule *schedule) |
| 1907 | { |
| 1908 | enum cmd_error_code ret; |
| 1909 | enum lttng_rotation_status status; |
| 1910 | uint64_t value; |
| 1911 | |
| 1912 | status = lttng_rotation_schedule_size_threshold_get_threshold(schedule, |
| 1913 | &value); |
| 1914 | if (status != LTTNG_ROTATION_STATUS_OK) { |
| 1915 | ERR("Failed to retrieve size parameter from size-based rotation schedule."); |
| 1916 | ret = CMD_ERROR; |
| 1917 | goto end; |
| 1918 | } |
| 1919 | |
| 1920 | MSG(" size threshold: %" PRIu64" bytes", value); |
| 1921 | ret = CMD_SUCCESS; |
| 1922 | end: |
| 1923 | return ret; |
| 1924 | } |
| 1925 | |
| 1926 | static enum cmd_error_code print_rotation_schedule( |
| 1927 | const struct lttng_rotation_schedule *schedule) |
| 1928 | { |
| 1929 | enum cmd_error_code ret; |
| 1930 | |
| 1931 | switch (lttng_rotation_schedule_get_type(schedule)) { |
| 1932 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: |
| 1933 | ret = print_size_threshold_rotation_schedule(schedule); |
| 1934 | break; |
| 1935 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: |
| 1936 | ret = print_periodic_rotation_schedule(schedule); |
| 1937 | break; |
| 1938 | default: |
| 1939 | ret = CMD_ERROR; |
| 1940 | } |
| 1941 | return ret; |
| 1942 | } |
| 1943 | |
| 1944 | /* |
| 1945 | * List the automatic rotation settings. |
| 1946 | */ |
| 1947 | static enum cmd_error_code list_rotate_settings(const char *session_name) |
| 1948 | { |
| 1949 | int ret; |
| 1950 | enum cmd_error_code cmd_ret = CMD_SUCCESS; |
| 1951 | unsigned int count, i; |
| 1952 | struct lttng_rotation_schedules *schedules = NULL; |
| 1953 | enum lttng_rotation_status status; |
| 1954 | |
| 1955 | ret = lttng_session_list_rotation_schedules(session_name, &schedules); |
| 1956 | if (ret != LTTNG_OK) { |
| 1957 | ERR("Failed to list session rotation schedules: %s", lttng_strerror(ret)); |
| 1958 | cmd_ret = CMD_ERROR; |
| 1959 | goto end; |
| 1960 | } |
| 1961 | |
| 1962 | status = lttng_rotation_schedules_get_count(schedules, &count); |
| 1963 | if (status != LTTNG_ROTATION_STATUS_OK) { |
| 1964 | ERR("Failed to retrieve the number of session rotation schedules."); |
| 1965 | cmd_ret = CMD_ERROR; |
| 1966 | goto end; |
| 1967 | } |
| 1968 | |
| 1969 | if (count == 0) { |
| 1970 | cmd_ret = CMD_SUCCESS; |
| 1971 | goto end; |
| 1972 | } |
| 1973 | |
| 1974 | MSG("Automatic rotation schedules:"); |
| 1975 | if (lttng_opt_mi) { |
| 1976 | ret = mi_lttng_writer_open_element(the_writer, |
| 1977 | mi_lttng_element_rotation_schedules); |
| 1978 | if (ret) { |
| 1979 | cmd_ret = CMD_ERROR; |
| 1980 | goto end; |
| 1981 | } |
| 1982 | } |
| 1983 | |
| 1984 | for (i = 0; i < count; i++) { |
| 1985 | enum cmd_error_code tmp_ret = CMD_SUCCESS; |
| 1986 | const struct lttng_rotation_schedule *schedule; |
| 1987 | |
| 1988 | schedule = lttng_rotation_schedules_get_at_index(schedules, i); |
| 1989 | if (!schedule) { |
| 1990 | ERR("Failed to retrieve session rotation schedule."); |
| 1991 | cmd_ret = CMD_ERROR; |
| 1992 | goto end; |
| 1993 | } |
| 1994 | |
| 1995 | if (lttng_opt_mi) { |
| 1996 | ret = mi_lttng_rotation_schedule(the_writer, schedule); |
| 1997 | if (ret) { |
| 1998 | tmp_ret = CMD_ERROR; |
| 1999 | } |
| 2000 | } else { |
| 2001 | tmp_ret = print_rotation_schedule(schedule); |
| 2002 | } |
| 2003 | |
| 2004 | /* |
| 2005 | * Report an error if the serialization of any of the |
| 2006 | * descriptors failed. |
| 2007 | */ |
| 2008 | cmd_ret = cmd_ret ? cmd_ret : tmp_ret; |
| 2009 | } |
| 2010 | |
| 2011 | _MSG("\n"); |
| 2012 | if (lttng_opt_mi) { |
| 2013 | /* Close the rotation_schedules element. */ |
| 2014 | ret = mi_lttng_writer_close_element(the_writer); |
| 2015 | if (ret) { |
| 2016 | cmd_ret = CMD_ERROR; |
| 2017 | goto end; |
| 2018 | } |
| 2019 | } |
| 2020 | end: |
| 2021 | lttng_rotation_schedules_destroy(schedules); |
| 2022 | return cmd_ret; |
| 2023 | } |
| 2024 | |
| 2025 | /* |
| 2026 | * Machine interface |
| 2027 | * Find the session with session_name as name |
| 2028 | * and print his informations. |
| 2029 | */ |
| 2030 | static int mi_list_session(const char *session_name, |
| 2031 | struct lttng_session *sessions, int count) |
| 2032 | { |
| 2033 | int ret, i; |
| 2034 | unsigned int session_found = 0; |
| 2035 | |
| 2036 | if (session_name == NULL) { |
| 2037 | ret = -LTTNG_ERR_SESS_NOT_FOUND; |
| 2038 | goto end; |
| 2039 | } |
| 2040 | |
| 2041 | for (i = 0; i < count; i++) { |
| 2042 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { |
| 2043 | /* We need to leave it open to append other informations |
| 2044 | * like domain, channel, events etc.*/ |
| 2045 | session_found = 1; |
| 2046 | ret = mi_lttng_session(the_writer, &sessions[i], 1); |
| 2047 | if (ret) { |
| 2048 | goto end; |
| 2049 | } |
| 2050 | break; |
| 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | if (!session_found) { |
| 2055 | ERR("Session '%s' not found", session_name); |
| 2056 | ret = -LTTNG_ERR_SESS_NOT_FOUND; |
| 2057 | goto end; |
| 2058 | } |
| 2059 | |
| 2060 | end: |
| 2061 | return ret; |
| 2062 | } |
| 2063 | |
| 2064 | /* |
| 2065 | * Machine interface |
| 2066 | * List all availables session |
| 2067 | */ |
| 2068 | static int mi_list_sessions(struct lttng_session *sessions, int count) |
| 2069 | { |
| 2070 | int ret, i; |
| 2071 | |
| 2072 | /* Opening sessions element */ |
| 2073 | ret = mi_lttng_sessions_open(the_writer); |
| 2074 | if (ret) { |
| 2075 | goto end; |
| 2076 | } |
| 2077 | |
| 2078 | /* Listing sessions */ |
| 2079 | for (i = 0; i < count; i++) { |
| 2080 | ret = mi_lttng_session(the_writer, &sessions[i], 0); |
| 2081 | if (ret) { |
| 2082 | goto end; |
| 2083 | } |
| 2084 | } |
| 2085 | |
| 2086 | /* Closing sessions element */ |
| 2087 | ret = mi_lttng_writer_close_element(the_writer); |
| 2088 | if (ret) { |
| 2089 | goto end; |
| 2090 | } |
| 2091 | |
| 2092 | end: |
| 2093 | return ret; |
| 2094 | } |
| 2095 | |
| 2096 | /* |
| 2097 | * List available tracing session. List only basic information. |
| 2098 | * |
| 2099 | * If session_name is NULL, all sessions are listed. |
| 2100 | */ |
| 2101 | static int list_sessions(const char *session_name) |
| 2102 | { |
| 2103 | int ret = CMD_SUCCESS; |
| 2104 | int count, i; |
| 2105 | unsigned int session_found = 0; |
| 2106 | struct lttng_session *sessions = NULL; |
| 2107 | |
| 2108 | count = lttng_list_sessions(&sessions); |
| 2109 | DBG("Session count %d", count); |
| 2110 | if (count < 0) { |
| 2111 | ret = CMD_ERROR; |
| 2112 | ERR("%s", lttng_strerror(count)); |
| 2113 | goto end; |
| 2114 | } |
| 2115 | |
| 2116 | if (lttng_opt_mi) { |
| 2117 | /* Mi */ |
| 2118 | if (session_name == NULL) { |
| 2119 | /* List all sessions */ |
| 2120 | ret = mi_list_sessions(sessions, count); |
| 2121 | } else { |
| 2122 | /* Note : this return an open session element */ |
| 2123 | ret = mi_list_session(session_name, sessions, count); |
| 2124 | } |
| 2125 | if (ret) { |
| 2126 | ret = CMD_ERROR; |
| 2127 | goto end; |
| 2128 | } |
| 2129 | } else { |
| 2130 | /* Pretty print */ |
| 2131 | if (count == 0) { |
| 2132 | MSG("Currently no available recording session"); |
| 2133 | goto end; |
| 2134 | } |
| 2135 | |
| 2136 | if (session_name == NULL) { |
| 2137 | MSG("Available recording sessions:"); |
| 2138 | } |
| 2139 | |
| 2140 | for (i = 0; i < count; i++) { |
| 2141 | if (session_name != NULL) { |
| 2142 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { |
| 2143 | session_found = 1; |
| 2144 | MSG("Recording session %s: [%s%s]", session_name, |
| 2145 | active_string(sessions[i].enabled), |
| 2146 | snapshot_string(sessions[i].snapshot_mode)); |
| 2147 | if (*sessions[i].path) { |
| 2148 | MSG("%sTrace output: %s\n", indent4, sessions[i].path); |
| 2149 | } |
| 2150 | memcpy(&the_listed_session, |
| 2151 | &sessions[i], |
| 2152 | sizeof(the_listed_session)); |
| 2153 | break; |
| 2154 | } |
| 2155 | } else { |
| 2156 | MSG(" %d) %s [%s%s]", i + 1, |
| 2157 | sessions[i].name, |
| 2158 | active_string(sessions[i].enabled), |
| 2159 | snapshot_string(sessions[i].snapshot_mode)); |
| 2160 | if (*sessions[i].path) { |
| 2161 | MSG("%sTrace output: %s", indent4, sessions[i].path); |
| 2162 | } |
| 2163 | if (sessions[i].live_timer_interval != 0) { |
| 2164 | MSG("%sLive timer interval: %u %s", indent4, |
| 2165 | sessions[i].live_timer_interval, |
| 2166 | USEC_UNIT); |
| 2167 | } |
| 2168 | MSG(""); |
| 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | if (!session_found && session_name != NULL) { |
| 2173 | ERR("Session '%s' not found", session_name); |
| 2174 | ret = CMD_ERROR; |
| 2175 | goto end; |
| 2176 | } |
| 2177 | |
| 2178 | if (session_name == NULL) { |
| 2179 | MSG("\nUse lttng list <session_name> for more details"); |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | end: |
| 2184 | free(sessions); |
| 2185 | return ret; |
| 2186 | } |
| 2187 | |
| 2188 | |
| 2189 | /* |
| 2190 | * Machine Interface |
| 2191 | * list available domain(s) for a session. |
| 2192 | */ |
| 2193 | static int mi_list_domains(struct lttng_domain *domains, int count) |
| 2194 | { |
| 2195 | int i, ret; |
| 2196 | /* Open domains element */ |
| 2197 | ret = mi_lttng_domains_open(the_writer); |
| 2198 | if (ret) { |
| 2199 | goto end; |
| 2200 | } |
| 2201 | |
| 2202 | for (i = 0; i < count; i++) { |
| 2203 | ret = mi_lttng_domain(the_writer, &domains[i], 0); |
| 2204 | if (ret) { |
| 2205 | goto end; |
| 2206 | } |
| 2207 | } |
| 2208 | |
| 2209 | /* Closing domains element */ |
| 2210 | ret = mi_lttng_writer_close_element(the_writer); |
| 2211 | if (ret) { |
| 2212 | goto end; |
| 2213 | } |
| 2214 | end: |
| 2215 | return ret; |
| 2216 | } |
| 2217 | |
| 2218 | /* |
| 2219 | * List available domain(s) for a session. |
| 2220 | */ |
| 2221 | static int list_domains(const char *session_name) |
| 2222 | { |
| 2223 | int i, count, ret = CMD_SUCCESS; |
| 2224 | struct lttng_domain *domains = NULL; |
| 2225 | |
| 2226 | |
| 2227 | count = lttng_list_domains(session_name, &domains); |
| 2228 | if (count < 0) { |
| 2229 | ret = CMD_ERROR; |
| 2230 | ERR("%s", lttng_strerror(count)); |
| 2231 | goto end; |
| 2232 | } |
| 2233 | |
| 2234 | if (lttng_opt_mi) { |
| 2235 | /* Mi output */ |
| 2236 | ret = mi_list_domains(domains, count); |
| 2237 | if (ret) { |
| 2238 | ret = CMD_ERROR; |
| 2239 | goto error; |
| 2240 | } |
| 2241 | } else { |
| 2242 | /* Pretty print */ |
| 2243 | MSG("Domains:\n-------------"); |
| 2244 | if (count == 0) { |
| 2245 | MSG(" None"); |
| 2246 | goto end; |
| 2247 | } |
| 2248 | |
| 2249 | for (i = 0; i < count; i++) { |
| 2250 | switch (domains[i].type) { |
| 2251 | case LTTNG_DOMAIN_KERNEL: |
| 2252 | MSG(" - Kernel"); |
| 2253 | break; |
| 2254 | case LTTNG_DOMAIN_UST: |
| 2255 | MSG(" - UST global"); |
| 2256 | break; |
| 2257 | case LTTNG_DOMAIN_JUL: |
| 2258 | MSG(" - JUL (Java Util Logging)"); |
| 2259 | break; |
| 2260 | case LTTNG_DOMAIN_LOG4J: |
| 2261 | MSG(" - LOG4j (Logging for Java)"); |
| 2262 | break; |
| 2263 | case LTTNG_DOMAIN_PYTHON: |
| 2264 | MSG(" - Python (logging)"); |
| 2265 | break; |
| 2266 | default: |
| 2267 | break; |
| 2268 | } |
| 2269 | } |
| 2270 | } |
| 2271 | |
| 2272 | error: |
| 2273 | free(domains); |
| 2274 | |
| 2275 | end: |
| 2276 | return ret; |
| 2277 | } |
| 2278 | |
| 2279 | /* |
| 2280 | * The 'list <options>' first level command |
| 2281 | */ |
| 2282 | int cmd_list(int argc, const char **argv) |
| 2283 | { |
| 2284 | int opt, ret = CMD_SUCCESS; |
| 2285 | const char *session_name, *leftover = NULL; |
| 2286 | static poptContext pc; |
| 2287 | struct lttng_domain domain; |
| 2288 | struct lttng_domain *domains = NULL; |
| 2289 | |
| 2290 | memset(&domain, 0, sizeof(domain)); |
| 2291 | |
| 2292 | if (argc < 1) { |
| 2293 | ret = CMD_ERROR; |
| 2294 | goto end; |
| 2295 | } |
| 2296 | |
| 2297 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 2298 | poptReadDefaultConfig(pc, 0); |
| 2299 | |
| 2300 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 2301 | switch (opt) { |
| 2302 | case OPT_HELP: |
| 2303 | SHOW_HELP(); |
| 2304 | goto end; |
| 2305 | case OPT_USERSPACE: |
| 2306 | opt_userspace = 1; |
| 2307 | break; |
| 2308 | case OPT_LIST_OPTIONS: |
| 2309 | list_cmd_options(stdout, long_options); |
| 2310 | goto end; |
| 2311 | default: |
| 2312 | ret = CMD_UNDEFINED; |
| 2313 | goto end; |
| 2314 | } |
| 2315 | } |
| 2316 | |
| 2317 | /* Mi check */ |
| 2318 | if (lttng_opt_mi) { |
| 2319 | the_writer = mi_lttng_writer_create( |
| 2320 | fileno(stdout), lttng_opt_mi); |
| 2321 | if (!the_writer) { |
| 2322 | ret = CMD_ERROR; |
| 2323 | goto end; |
| 2324 | } |
| 2325 | |
| 2326 | /* Open command element */ |
| 2327 | ret = mi_lttng_writer_command_open( |
| 2328 | the_writer, mi_lttng_element_command_list); |
| 2329 | if (ret) { |
| 2330 | ret = CMD_ERROR; |
| 2331 | goto end; |
| 2332 | } |
| 2333 | |
| 2334 | /* Open output element */ |
| 2335 | ret = mi_lttng_writer_open_element( |
| 2336 | the_writer, mi_lttng_element_command_output); |
| 2337 | if (ret) { |
| 2338 | ret = CMD_ERROR; |
| 2339 | goto end; |
| 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | /* Get session name (trailing argument) */ |
| 2344 | session_name = poptGetArg(pc); |
| 2345 | DBG2("Session name: %s", session_name); |
| 2346 | |
| 2347 | leftover = poptGetArg(pc); |
| 2348 | if (leftover) { |
| 2349 | ERR("Unknown argument: %s", leftover); |
| 2350 | ret = CMD_ERROR; |
| 2351 | goto end; |
| 2352 | } |
| 2353 | |
| 2354 | if (opt_kernel) { |
| 2355 | domain.type = LTTNG_DOMAIN_KERNEL; |
| 2356 | } else if (opt_userspace) { |
| 2357 | DBG2("Listing userspace global domain"); |
| 2358 | domain.type = LTTNG_DOMAIN_UST; |
| 2359 | } else if (opt_jul) { |
| 2360 | DBG2("Listing JUL domain"); |
| 2361 | domain.type = LTTNG_DOMAIN_JUL; |
| 2362 | } else if (opt_log4j) { |
| 2363 | domain.type = LTTNG_DOMAIN_LOG4J; |
| 2364 | } else if (opt_python) { |
| 2365 | domain.type = LTTNG_DOMAIN_PYTHON; |
| 2366 | } |
| 2367 | |
| 2368 | if (!opt_kernel && opt_syscall) { |
| 2369 | WARN("--syscall will only work with the Kernel domain (-k)"); |
| 2370 | ret = CMD_ERROR; |
| 2371 | goto end; |
| 2372 | } |
| 2373 | |
| 2374 | if (opt_kernel || opt_userspace || opt_jul || opt_log4j || opt_python) { |
| 2375 | the_handle = lttng_create_handle(session_name, &domain); |
| 2376 | if (the_handle == NULL) { |
| 2377 | ret = CMD_FATAL; |
| 2378 | goto end; |
| 2379 | } |
| 2380 | } |
| 2381 | |
| 2382 | if (session_name == NULL) { |
| 2383 | if (!opt_kernel && !opt_userspace && !opt_jul && !opt_log4j |
| 2384 | && !opt_python) { |
| 2385 | ret = list_sessions(NULL); |
| 2386 | if (ret) { |
| 2387 | goto end; |
| 2388 | } |
| 2389 | } |
| 2390 | if (opt_kernel) { |
| 2391 | if (opt_syscall) { |
| 2392 | ret = list_syscalls(); |
| 2393 | if (ret) { |
| 2394 | goto end; |
| 2395 | } |
| 2396 | } else { |
| 2397 | ret = list_kernel_events(); |
| 2398 | if (ret) { |
| 2399 | goto end; |
| 2400 | } |
| 2401 | } |
| 2402 | } |
| 2403 | if (opt_userspace) { |
| 2404 | if (opt_fields) { |
| 2405 | ret = list_ust_event_fields(); |
| 2406 | } else { |
| 2407 | ret = list_ust_events(); |
| 2408 | } |
| 2409 | if (ret) { |
| 2410 | goto end; |
| 2411 | } |
| 2412 | } |
| 2413 | if (opt_jul || opt_log4j || opt_python) { |
| 2414 | ret = list_agent_events(); |
| 2415 | if (ret) { |
| 2416 | goto end; |
| 2417 | } |
| 2418 | } |
| 2419 | } else { |
| 2420 | /* List session attributes */ |
| 2421 | if (lttng_opt_mi) { |
| 2422 | /* Open element sessions |
| 2423 | * Present for xml consistency */ |
| 2424 | ret = mi_lttng_sessions_open(the_writer); |
| 2425 | if (ret) { |
| 2426 | goto end; |
| 2427 | } |
| 2428 | } |
| 2429 | /* MI: the ouptut of list_sessions is an unclosed session element */ |
| 2430 | ret = list_sessions(session_name); |
| 2431 | if (ret) { |
| 2432 | goto end; |
| 2433 | } |
| 2434 | |
| 2435 | ret = list_rotate_settings(session_name); |
| 2436 | if (ret) { |
| 2437 | goto end; |
| 2438 | } |
| 2439 | |
| 2440 | /* Domain listing */ |
| 2441 | if (opt_domain) { |
| 2442 | ret = list_domains(session_name); |
| 2443 | goto end; |
| 2444 | } |
| 2445 | |
| 2446 | /* Channel listing */ |
| 2447 | if (opt_kernel || opt_userspace) { |
| 2448 | if (lttng_opt_mi) { |
| 2449 | /* Add of domains and domain element for xml |
| 2450 | * consistency and validation |
| 2451 | */ |
| 2452 | ret = mi_lttng_domains_open(the_writer); |
| 2453 | if (ret) { |
| 2454 | goto end; |
| 2455 | } |
| 2456 | |
| 2457 | /* Open domain and leave it open for |
| 2458 | * nested channels printing */ |
| 2459 | ret = mi_lttng_domain(the_writer, &domain, 1); |
| 2460 | if (ret) { |
| 2461 | goto end; |
| 2462 | } |
| 2463 | |
| 2464 | } |
| 2465 | |
| 2466 | |
| 2467 | /* Trackers */ |
| 2468 | ret = list_trackers(&domain); |
| 2469 | if (ret) { |
| 2470 | goto end; |
| 2471 | } |
| 2472 | |
| 2473 | /* Channels */ |
| 2474 | ret = list_channels(opt_channel); |
| 2475 | if (ret) { |
| 2476 | goto end; |
| 2477 | } |
| 2478 | |
| 2479 | if (lttng_opt_mi) { |
| 2480 | /* Close domain and domain element */ |
| 2481 | ret = mi_lttng_close_multi_element( |
| 2482 | the_writer, 2); |
| 2483 | } |
| 2484 | if (ret) { |
| 2485 | goto end; |
| 2486 | } |
| 2487 | |
| 2488 | |
| 2489 | } else { |
| 2490 | int i, nb_domain; |
| 2491 | |
| 2492 | /* We want all domain(s) */ |
| 2493 | nb_domain = lttng_list_domains(session_name, &domains); |
| 2494 | if (nb_domain < 0) { |
| 2495 | ret = CMD_ERROR; |
| 2496 | ERR("%s", lttng_strerror(nb_domain)); |
| 2497 | goto end; |
| 2498 | } |
| 2499 | |
| 2500 | if (lttng_opt_mi) { |
| 2501 | ret = mi_lttng_domains_open(the_writer); |
| 2502 | if (ret) { |
| 2503 | ret = CMD_ERROR; |
| 2504 | goto end; |
| 2505 | } |
| 2506 | } |
| 2507 | |
| 2508 | for (i = 0; i < nb_domain; i++) { |
| 2509 | switch (domains[i].type) { |
| 2510 | case LTTNG_DOMAIN_KERNEL: |
| 2511 | MSG("=== Domain: Linux kernel ===\n"); |
| 2512 | break; |
| 2513 | case LTTNG_DOMAIN_UST: |
| 2514 | MSG("=== Domain: User space ===\n"); |
| 2515 | MSG("Buffering scheme: %s\n", |
| 2516 | domains[i].buf_type == |
| 2517 | LTTNG_BUFFER_PER_PID ? "per-process" : "per-user"); |
| 2518 | break; |
| 2519 | case LTTNG_DOMAIN_JUL: |
| 2520 | MSG("=== Domain: java.util.logging (JUL) ===\n"); |
| 2521 | break; |
| 2522 | case LTTNG_DOMAIN_LOG4J: |
| 2523 | MSG("=== Domain: log4j ===\n"); |
| 2524 | break; |
| 2525 | case LTTNG_DOMAIN_PYTHON: |
| 2526 | MSG("=== Domain: Python logging ===\n"); |
| 2527 | break; |
| 2528 | default: |
| 2529 | MSG("=== Domain: Unimplemented ===\n"); |
| 2530 | break; |
| 2531 | } |
| 2532 | |
| 2533 | if (lttng_opt_mi) { |
| 2534 | ret = mi_lttng_domain(the_writer, |
| 2535 | &domains[i], 1); |
| 2536 | if (ret) { |
| 2537 | ret = CMD_ERROR; |
| 2538 | goto end; |
| 2539 | } |
| 2540 | } |
| 2541 | |
| 2542 | /* Clean handle before creating a new one */ |
| 2543 | if (the_handle) { |
| 2544 | lttng_destroy_handle(the_handle); |
| 2545 | } |
| 2546 | |
| 2547 | the_handle = lttng_create_handle( |
| 2548 | session_name, &domains[i]); |
| 2549 | if (the_handle == NULL) { |
| 2550 | ret = CMD_FATAL; |
| 2551 | goto end; |
| 2552 | } |
| 2553 | |
| 2554 | if (domains[i].type == LTTNG_DOMAIN_JUL || |
| 2555 | domains[i].type == LTTNG_DOMAIN_LOG4J || |
| 2556 | domains[i].type == LTTNG_DOMAIN_PYTHON) { |
| 2557 | ret = list_session_agent_events(); |
| 2558 | if (ret) { |
| 2559 | goto end; |
| 2560 | } |
| 2561 | |
| 2562 | goto next_domain; |
| 2563 | } |
| 2564 | |
| 2565 | switch (domains[i].type) { |
| 2566 | case LTTNG_DOMAIN_KERNEL: |
| 2567 | case LTTNG_DOMAIN_UST: |
| 2568 | ret = list_trackers(&domains[i]); |
| 2569 | if (ret) { |
| 2570 | goto end; |
| 2571 | } |
| 2572 | break; |
| 2573 | default: |
| 2574 | break; |
| 2575 | } |
| 2576 | |
| 2577 | ret = list_channels(opt_channel); |
| 2578 | if (ret) { |
| 2579 | goto end; |
| 2580 | } |
| 2581 | |
| 2582 | next_domain: |
| 2583 | if (lttng_opt_mi) { |
| 2584 | /* Close domain element */ |
| 2585 | ret = mi_lttng_writer_close_element( |
| 2586 | the_writer); |
| 2587 | if (ret) { |
| 2588 | ret = CMD_ERROR; |
| 2589 | goto end; |
| 2590 | } |
| 2591 | } |
| 2592 | |
| 2593 | } |
| 2594 | if (lttng_opt_mi) { |
| 2595 | /* Close the domains, session and sessions element */ |
| 2596 | ret = mi_lttng_close_multi_element( |
| 2597 | the_writer, 3); |
| 2598 | if (ret) { |
| 2599 | ret = CMD_ERROR; |
| 2600 | goto end; |
| 2601 | } |
| 2602 | } |
| 2603 | } |
| 2604 | } |
| 2605 | |
| 2606 | /* Mi closing */ |
| 2607 | if (lttng_opt_mi) { |
| 2608 | /* Close output element */ |
| 2609 | ret = mi_lttng_writer_close_element(the_writer); |
| 2610 | if (ret) { |
| 2611 | ret = CMD_ERROR; |
| 2612 | goto end; |
| 2613 | } |
| 2614 | |
| 2615 | /* Command element close */ |
| 2616 | ret = mi_lttng_writer_command_close(the_writer); |
| 2617 | if (ret) { |
| 2618 | ret = CMD_ERROR; |
| 2619 | goto end; |
| 2620 | } |
| 2621 | } |
| 2622 | end: |
| 2623 | /* Mi clean-up */ |
| 2624 | if (the_writer && mi_lttng_writer_destroy(the_writer)) { |
| 2625 | /* Preserve original error code */ |
| 2626 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; |
| 2627 | } |
| 2628 | |
| 2629 | free(domains); |
| 2630 | if (the_handle) { |
| 2631 | lttng_destroy_handle(the_handle); |
| 2632 | } |
| 2633 | |
| 2634 | poptFreeContext(pc); |
| 2635 | return ret; |
| 2636 | } |