| 1 | /* |
| 2 | * Copyright (C) 2011 EfficiOS Inc. |
| 3 | * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0-only |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #define _LGPL_SOURCE |
| 10 | #include "../command.hpp" |
| 11 | |
| 12 | #include <common/mi-lttng.hpp> |
| 13 | |
| 14 | #include <lttng/domain-internal.hpp> |
| 15 | |
| 16 | #include <ctype.h> |
| 17 | #include <popt.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <unistd.h> |
| 24 | #include <urcu/list.h> |
| 25 | |
| 26 | static char *opt_channel_name; |
| 27 | static char *opt_session_name; |
| 28 | static int opt_kernel; |
| 29 | static int opt_userspace; |
| 30 | static int opt_jul; |
| 31 | static int opt_log4j; |
| 32 | static char *opt_type; |
| 33 | |
| 34 | #ifdef LTTNG_EMBED_HELP |
| 35 | static const char help_msg[] = |
| 36 | #include <lttng-add-context.1.h> |
| 37 | ; |
| 38 | #endif |
| 39 | |
| 40 | enum { |
| 41 | OPT_HELP = 1, |
| 42 | OPT_TYPE, |
| 43 | OPT_USERSPACE, |
| 44 | OPT_JUL, |
| 45 | OPT_LOG4J, |
| 46 | OPT_LIST_OPTIONS, |
| 47 | OPT_LIST, |
| 48 | }; |
| 49 | |
| 50 | static struct lttng_handle *handle; |
| 51 | static struct mi_writer *writer; |
| 52 | |
| 53 | /* |
| 54 | * Taken from the LTTng ABI except for "UNKNOWN". |
| 55 | */ |
| 56 | enum context_type { |
| 57 | CONTEXT_UNKNOWN = -1, |
| 58 | CONTEXT_PID = 0, |
| 59 | CONTEXT_PERF_COUNTER = 1, /* Backward compat. */ |
| 60 | CONTEXT_PROCNAME = 2, |
| 61 | CONTEXT_PRIO = 3, |
| 62 | CONTEXT_NICE = 4, |
| 63 | CONTEXT_VPID = 5, |
| 64 | CONTEXT_TID = 6, |
| 65 | CONTEXT_VTID = 7, |
| 66 | CONTEXT_PPID = 8, |
| 67 | CONTEXT_VPPID = 9, |
| 68 | CONTEXT_PTHREAD_ID = 10, |
| 69 | CONTEXT_HOSTNAME = 11, |
| 70 | CONTEXT_IP = 12, |
| 71 | CONTEXT_PERF_CPU_COUNTER = 13, |
| 72 | CONTEXT_PERF_THREAD_COUNTER = 14, |
| 73 | CONTEXT_APP_CONTEXT = 15, |
| 74 | CONTEXT_INTERRUPTIBLE = 16, |
| 75 | CONTEXT_PREEMPTIBLE = 17, |
| 76 | CONTEXT_NEED_RESCHEDULE = 18, |
| 77 | CONTEXT_MIGRATABLE = 19, |
| 78 | CONTEXT_CALLSTACK_KERNEL = 20, |
| 79 | CONTEXT_CALLSTACK_USER = 21, |
| 80 | CONTEXT_CGROUP_NS = 22, |
| 81 | CONTEXT_IPC_NS = 23, |
| 82 | CONTEXT_MNT_NS = 24, |
| 83 | CONTEXT_NET_NS = 25, |
| 84 | CONTEXT_PID_NS = 26, |
| 85 | CONTEXT_USER_NS = 27, |
| 86 | CONTEXT_UTS_NS = 28, |
| 87 | CONTEXT_UID = 29, |
| 88 | CONTEXT_EUID = 30, |
| 89 | CONTEXT_SUID = 31, |
| 90 | CONTEXT_GID = 32, |
| 91 | CONTEXT_EGID = 33, |
| 92 | CONTEXT_SGID = 34, |
| 93 | CONTEXT_VUID = 35, |
| 94 | CONTEXT_VEUID = 36, |
| 95 | CONTEXT_VSUID = 37, |
| 96 | CONTEXT_VGID = 38, |
| 97 | CONTEXT_VEGID = 39, |
| 98 | CONTEXT_VSGID = 40, |
| 99 | CONTEXT_TIME_NS = 41, |
| 100 | }; |
| 101 | |
| 102 | /* |
| 103 | * Taken from the Perf ABI (all enum perf_*) |
| 104 | */ |
| 105 | enum perf_type { |
| 106 | PERF_TYPE_HARDWARE = 0, |
| 107 | PERF_TYPE_SOFTWARE = 1, |
| 108 | PERF_TYPE_HW_CACHE = 3, |
| 109 | PERF_TYPE_RAW = 4, |
| 110 | }; |
| 111 | |
| 112 | enum perf_count_hard { |
| 113 | PERF_COUNT_HW_CPU_CYCLES = 0, |
| 114 | PERF_COUNT_HW_INSTRUCTIONS = 1, |
| 115 | PERF_COUNT_HW_CACHE_REFERENCES = 2, |
| 116 | PERF_COUNT_HW_CACHE_MISSES = 3, |
| 117 | PERF_COUNT_HW_BRANCH_INSTRUCTIONS = 4, |
| 118 | PERF_COUNT_HW_BRANCH_MISSES = 5, |
| 119 | PERF_COUNT_HW_BUS_CYCLES = 6, |
| 120 | PERF_COUNT_HW_STALLED_CYCLES_FRONTEND = 7, |
| 121 | PERF_COUNT_HW_STALLED_CYCLES_BACKEND = 8, |
| 122 | }; |
| 123 | |
| 124 | enum perf_count_soft { |
| 125 | PERF_COUNT_SW_CPU_CLOCK = 0, |
| 126 | PERF_COUNT_SW_TASK_CLOCK = 1, |
| 127 | PERF_COUNT_SW_PAGE_FAULTS = 2, |
| 128 | PERF_COUNT_SW_CONTEXT_SWITCHES = 3, |
| 129 | PERF_COUNT_SW_CPU_MIGRATIONS = 4, |
| 130 | PERF_COUNT_SW_PAGE_FAULTS_MIN = 5, |
| 131 | PERF_COUNT_SW_PAGE_FAULTS_MAJ = 6, |
| 132 | PERF_COUNT_SW_ALIGNMENT_FAULTS = 7, |
| 133 | PERF_COUNT_SW_EMULATION_FAULTS = 8, |
| 134 | }; |
| 135 | |
| 136 | /* |
| 137 | * Generalized hardware cache events: |
| 138 | * |
| 139 | * { L1-D, L1-I, LLC, ITLB, DTLB, BPU } x |
| 140 | * { read, write, prefetch } x |
| 141 | * { accesses, misses } |
| 142 | */ |
| 143 | enum perf_hw_cache_id { |
| 144 | PERF_COUNT_HW_CACHE_L1D = 0, |
| 145 | PERF_COUNT_HW_CACHE_L1I = 1, |
| 146 | PERF_COUNT_HW_CACHE_LL = 2, |
| 147 | PERF_COUNT_HW_CACHE_DTLB = 3, |
| 148 | PERF_COUNT_HW_CACHE_ITLB = 4, |
| 149 | PERF_COUNT_HW_CACHE_BPU = 5, |
| 150 | |
| 151 | PERF_COUNT_HW_CACHE_MAX, /* non-ABI */ |
| 152 | }; |
| 153 | |
| 154 | enum perf_hw_cache_op_id { |
| 155 | PERF_COUNT_HW_CACHE_OP_READ = 0, |
| 156 | PERF_COUNT_HW_CACHE_OP_WRITE = 1, |
| 157 | PERF_COUNT_HW_CACHE_OP_PREFETCH = 2, |
| 158 | |
| 159 | PERF_COUNT_HW_CACHE_OP_MAX, /* non-ABI */ |
| 160 | }; |
| 161 | |
| 162 | enum perf_hw_cache_op_result_id { |
| 163 | PERF_COUNT_HW_CACHE_RESULT_ACCESS = 0, |
| 164 | PERF_COUNT_HW_CACHE_RESULT_MISS = 1, |
| 165 | |
| 166 | PERF_COUNT_HW_CACHE_RESULT_MAX, /* non-ABI */ |
| 167 | }; |
| 168 | |
| 169 | static struct poptOption long_options[] = { |
| 170 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 171 | { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr }, |
| 172 | { "session", 's', POPT_ARG_STRING, &opt_session_name, 0, nullptr, nullptr }, |
| 173 | { "channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, nullptr, nullptr }, |
| 174 | { "kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, nullptr, nullptr }, |
| 175 | { "userspace", 'u', POPT_ARG_NONE, nullptr, OPT_USERSPACE, nullptr, nullptr }, |
| 176 | { "jul", 'j', POPT_ARG_NONE, nullptr, OPT_JUL, nullptr, nullptr }, |
| 177 | { "log4j", 'l', POPT_ARG_NONE, nullptr, OPT_LOG4J, nullptr, nullptr }, |
| 178 | { "type", 't', POPT_ARG_STRING, &opt_type, OPT_TYPE, nullptr, nullptr }, |
| 179 | { "list", 0, POPT_ARG_NONE, nullptr, OPT_LIST, nullptr, nullptr }, |
| 180 | { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr }, |
| 181 | { nullptr, 0, 0, nullptr, 0, nullptr, nullptr } |
| 182 | }; |
| 183 | |
| 184 | /* |
| 185 | * Context options |
| 186 | */ |
| 187 | #define PERF_HW(optstr, name, type, hide) \ |
| 188 | { \ |
| 189 | optstr, type, PERF_COUNT_HW_##name, hide \ |
| 190 | } |
| 191 | |
| 192 | #define PERF_SW(optstr, name, type, hide) \ |
| 193 | { \ |
| 194 | optstr, type, PERF_COUNT_SW_##name, hide \ |
| 195 | } |
| 196 | |
| 197 | #define _PERF_HW_CACHE(optstr, name, type, op, result, hide) \ |
| 198 | { \ |
| 199 | optstr, type, PERF_COUNT_HW_CACHE_##name, PERF_COUNT_HW_CACHE_OP_##op, \ |
| 200 | PERF_COUNT_HW_CACHE_RESULT_##result, hide, \ |
| 201 | } |
| 202 | |
| 203 | #define PERF_HW_CACHE(optstr, name, type, hide) \ |
| 204 | _PERF_HW_CACHE(optstr "-loads", name, type, READ, ACCESS, hide), \ |
| 205 | _PERF_HW_CACHE(optstr "-load-misses", name, type, READ, MISS, hide), \ |
| 206 | _PERF_HW_CACHE(optstr "-stores", name, type, WRITE, ACCESS, hide), \ |
| 207 | _PERF_HW_CACHE(optstr "-store-misses", name, type, WRITE, MISS, hide), \ |
| 208 | _PERF_HW_CACHE(optstr "-prefetches", name, type, PREFETCH, ACCESS, hide), \ |
| 209 | _PERF_HW_CACHE(optstr "-prefetch-misses", name, type, PREFETCH, MISS, hide) |
| 210 | |
| 211 | namespace { |
| 212 | const struct ctx_opts { |
| 213 | /* Needed for end-of-list item. */ |
| 214 | ctx_opts() : ctx_opts(nullptr, CONTEXT_UNKNOWN) |
| 215 | { |
| 216 | } |
| 217 | |
| 218 | ctx_opts(const char *symbol_, context_type ctx_type_, bool hide_help_ = false) : |
| 219 | symbol((char *) symbol_), ctx_type(ctx_type_), hide_help(hide_help_) |
| 220 | { |
| 221 | } |
| 222 | |
| 223 | ctx_opts(const char *symbol_, |
| 224 | context_type ctx_type_, |
| 225 | perf_count_hard perf_count_hard, |
| 226 | bool hide_help_) : |
| 227 | ctx_opts(symbol_, ctx_type_, hide_help_) |
| 228 | { |
| 229 | u.perf.type = PERF_TYPE_HARDWARE; |
| 230 | u.perf.config = perf_count_hard; |
| 231 | } |
| 232 | |
| 233 | ctx_opts(const char *symbol_, |
| 234 | context_type ctx_type_, |
| 235 | perf_count_soft perf_count_soft, |
| 236 | bool hide_help_) : |
| 237 | ctx_opts(symbol_, ctx_type_, hide_help_) |
| 238 | { |
| 239 | u.perf.type = PERF_TYPE_SOFTWARE; |
| 240 | u.perf.config = perf_count_soft; |
| 241 | } |
| 242 | |
| 243 | ctx_opts(const char *symbol_, |
| 244 | context_type ctx_type_, |
| 245 | perf_hw_cache_id perf_hw_cache_id, |
| 246 | perf_hw_cache_op_id perf_hw_cache_op_id, |
| 247 | perf_hw_cache_op_result_id perf_hw_cache_op_result_id, |
| 248 | bool hide_help_) : |
| 249 | ctx_opts(symbol_, ctx_type_, hide_help_) |
| 250 | { |
| 251 | u.perf.type = PERF_TYPE_HW_CACHE; |
| 252 | u.perf.config = perf_hw_cache_id | perf_hw_cache_op_id << 8 | |
| 253 | perf_hw_cache_op_result_id << 16; |
| 254 | } |
| 255 | |
| 256 | char *symbol; |
| 257 | enum context_type ctx_type; |
| 258 | bool hide_help; /* Hide from --help */ |
| 259 | union { |
| 260 | struct { |
| 261 | uint32_t type; |
| 262 | uint64_t config; |
| 263 | } perf; |
| 264 | struct { |
| 265 | char *provider_name; |
| 266 | char *ctx_name; |
| 267 | } app_ctx; |
| 268 | } u; |
| 269 | } ctx_opts[] = { |
| 270 | /* |
| 271 | * These (char *) casts (as well as those in the PERF_* macros) are |
| 272 | * safe because we never free these instances of `struct ctx_opts`. |
| 273 | */ |
| 274 | { (char *) "pid", CONTEXT_PID }, |
| 275 | { (char *) "procname", CONTEXT_PROCNAME }, |
| 276 | { (char *) "prio", CONTEXT_PRIO }, |
| 277 | { (char *) "nice", CONTEXT_NICE }, |
| 278 | { (char *) "vpid", CONTEXT_VPID }, |
| 279 | { (char *) "tid", CONTEXT_TID }, |
| 280 | { (char *) "pthread_id", CONTEXT_PTHREAD_ID }, |
| 281 | { (char *) "vtid", CONTEXT_VTID }, |
| 282 | { (char *) "ppid", CONTEXT_PPID }, |
| 283 | { (char *) "vppid", CONTEXT_VPPID }, |
| 284 | { (char *) "hostname", CONTEXT_HOSTNAME }, |
| 285 | { (char *) "ip", CONTEXT_IP }, |
| 286 | { (char *) "interruptible", CONTEXT_INTERRUPTIBLE }, |
| 287 | { (char *) "preemptible", CONTEXT_PREEMPTIBLE }, |
| 288 | { (char *) "need_reschedule", CONTEXT_NEED_RESCHEDULE }, |
| 289 | { (char *) "migratable", CONTEXT_MIGRATABLE }, |
| 290 | { (char *) "callstack-kernel", CONTEXT_CALLSTACK_KERNEL }, |
| 291 | #ifdef HAVE_MODULES_USERSPACE_CALLSTACK_CONTEXT |
| 292 | { (char *) "callstack-user", CONTEXT_CALLSTACK_USER }, |
| 293 | #endif |
| 294 | { (char *) "cgroup_ns", CONTEXT_CGROUP_NS }, |
| 295 | { (char *) "ipc_ns", CONTEXT_IPC_NS }, |
| 296 | { (char *) "mnt_ns", CONTEXT_MNT_NS }, |
| 297 | { (char *) "net_ns", CONTEXT_NET_NS }, |
| 298 | { (char *) "pid_ns", CONTEXT_PID_NS }, |
| 299 | { (char *) "time_ns", CONTEXT_TIME_NS }, |
| 300 | { (char *) "user_ns", CONTEXT_USER_NS }, |
| 301 | { (char *) "uts_ns", CONTEXT_UTS_NS }, |
| 302 | { (char *) "uid", CONTEXT_UID }, |
| 303 | { (char *) "euid", CONTEXT_EUID }, |
| 304 | { (char *) "suid", CONTEXT_SUID }, |
| 305 | { (char *) "gid", CONTEXT_GID }, |
| 306 | { (char *) "egid", CONTEXT_EGID }, |
| 307 | { (char *) "sgid", CONTEXT_SGID }, |
| 308 | { (char *) "vuid", CONTEXT_VUID }, |
| 309 | { (char *) "veuid", CONTEXT_VEUID }, |
| 310 | { (char *) "vsuid", CONTEXT_VSUID }, |
| 311 | { (char *) "vgid", CONTEXT_VGID }, |
| 312 | { (char *) "vegid", CONTEXT_VEGID }, |
| 313 | { (char *) "vsgid", CONTEXT_VSGID }, |
| 314 | |
| 315 | /* Perf options */ |
| 316 | |
| 317 | /* Perf per-CPU counters */ |
| 318 | PERF_HW("perf:cpu:cpu-cycles", CPU_CYCLES, CONTEXT_PERF_CPU_COUNTER, 0), |
| 319 | PERF_HW("perf:cpu:cycles", CPU_CYCLES, CONTEXT_PERF_CPU_COUNTER, 0), |
| 320 | PERF_HW("perf:cpu:stalled-cycles-frontend", |
| 321 | STALLED_CYCLES_FRONTEND, |
| 322 | CONTEXT_PERF_CPU_COUNTER, |
| 323 | 0), |
| 324 | PERF_HW("perf:cpu:idle-cycles-frontend", |
| 325 | STALLED_CYCLES_FRONTEND, |
| 326 | CONTEXT_PERF_CPU_COUNTER, |
| 327 | 0), |
| 328 | PERF_HW("perf:cpu:stalled-cycles-backend", |
| 329 | STALLED_CYCLES_BACKEND, |
| 330 | CONTEXT_PERF_CPU_COUNTER, |
| 331 | 0), |
| 332 | PERF_HW("perf:cpu:idle-cycles-backend", STALLED_CYCLES_BACKEND, CONTEXT_PERF_CPU_COUNTER, 0), |
| 333 | PERF_HW("perf:cpu:instructions", INSTRUCTIONS, CONTEXT_PERF_CPU_COUNTER, 0), |
| 334 | PERF_HW("perf:cpu:cache-references", CACHE_REFERENCES, CONTEXT_PERF_CPU_COUNTER, 0), |
| 335 | PERF_HW("perf:cpu:cache-misses", CACHE_MISSES, CONTEXT_PERF_CPU_COUNTER, 0), |
| 336 | PERF_HW("perf:cpu:branch-instructions", BRANCH_INSTRUCTIONS, CONTEXT_PERF_CPU_COUNTER, 0), |
| 337 | PERF_HW("perf:cpu:branches", BRANCH_INSTRUCTIONS, CONTEXT_PERF_CPU_COUNTER, 0), |
| 338 | PERF_HW("perf:cpu:branch-misses", BRANCH_MISSES, CONTEXT_PERF_CPU_COUNTER, 0), |
| 339 | PERF_HW("perf:cpu:bus-cycles", BUS_CYCLES, CONTEXT_PERF_CPU_COUNTER, 0), |
| 340 | |
| 341 | PERF_HW_CACHE("perf:cpu:L1-dcache", L1D, CONTEXT_PERF_CPU_COUNTER, 0), |
| 342 | PERF_HW_CACHE("perf:cpu:L1-icache", L1I, CONTEXT_PERF_CPU_COUNTER, 0), |
| 343 | PERF_HW_CACHE("perf:cpu:LLC", LL, CONTEXT_PERF_CPU_COUNTER, 0), |
| 344 | PERF_HW_CACHE("perf:cpu:dTLB", DTLB, CONTEXT_PERF_CPU_COUNTER, 0), |
| 345 | _PERF_HW_CACHE("perf:cpu:iTLB-loads", ITLB, CONTEXT_PERF_CPU_COUNTER, READ, ACCESS, 0), |
| 346 | _PERF_HW_CACHE("perf:cpu:iTLB-load-misses", ITLB, CONTEXT_PERF_CPU_COUNTER, READ, MISS, 0), |
| 347 | _PERF_HW_CACHE("perf:cpu:branch-loads", BPU, CONTEXT_PERF_CPU_COUNTER, READ, ACCESS, 0), |
| 348 | _PERF_HW_CACHE("perf:cpu:branch-load-misses", BPU, CONTEXT_PERF_CPU_COUNTER, READ, MISS, 0), |
| 349 | |
| 350 | PERF_SW("perf:cpu:cpu-clock", CPU_CLOCK, CONTEXT_PERF_CPU_COUNTER, 0), |
| 351 | PERF_SW("perf:cpu:task-clock", TASK_CLOCK, CONTEXT_PERF_CPU_COUNTER, 0), |
| 352 | PERF_SW("perf:cpu:page-fault", PAGE_FAULTS, CONTEXT_PERF_CPU_COUNTER, 0), |
| 353 | PERF_SW("perf:cpu:faults", PAGE_FAULTS, CONTEXT_PERF_CPU_COUNTER, 0), |
| 354 | PERF_SW("perf:cpu:major-faults", PAGE_FAULTS_MAJ, CONTEXT_PERF_CPU_COUNTER, 0), |
| 355 | PERF_SW("perf:cpu:minor-faults", PAGE_FAULTS_MIN, CONTEXT_PERF_CPU_COUNTER, 0), |
| 356 | PERF_SW("perf:cpu:context-switches", CONTEXT_SWITCHES, CONTEXT_PERF_CPU_COUNTER, 0), |
| 357 | PERF_SW("perf:cpu:cs", CONTEXT_SWITCHES, CONTEXT_PERF_CPU_COUNTER, 0), |
| 358 | PERF_SW("perf:cpu:cpu-migrations", CPU_MIGRATIONS, CONTEXT_PERF_CPU_COUNTER, 0), |
| 359 | PERF_SW("perf:cpu:migrations", CPU_MIGRATIONS, CONTEXT_PERF_CPU_COUNTER, 0), |
| 360 | PERF_SW("perf:cpu:alignment-faults", ALIGNMENT_FAULTS, CONTEXT_PERF_CPU_COUNTER, 0), |
| 361 | PERF_SW("perf:cpu:emulation-faults", EMULATION_FAULTS, CONTEXT_PERF_CPU_COUNTER, 0), |
| 362 | |
| 363 | /* Perf per-thread counters */ |
| 364 | PERF_HW("perf:thread:cpu-cycles", CPU_CYCLES, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 365 | PERF_HW("perf:thread:cycles", CPU_CYCLES, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 366 | PERF_HW("perf:thread:stalled-cycles-frontend", |
| 367 | STALLED_CYCLES_FRONTEND, |
| 368 | CONTEXT_PERF_THREAD_COUNTER, |
| 369 | 0), |
| 370 | PERF_HW("perf:thread:idle-cycles-frontend", |
| 371 | STALLED_CYCLES_FRONTEND, |
| 372 | CONTEXT_PERF_THREAD_COUNTER, |
| 373 | 0), |
| 374 | PERF_HW("perf:thread:stalled-cycles-backend", |
| 375 | STALLED_CYCLES_BACKEND, |
| 376 | CONTEXT_PERF_THREAD_COUNTER, |
| 377 | 0), |
| 378 | PERF_HW("perf:thread:idle-cycles-backend", |
| 379 | STALLED_CYCLES_BACKEND, |
| 380 | CONTEXT_PERF_THREAD_COUNTER, |
| 381 | 0), |
| 382 | PERF_HW("perf:thread:instructions", INSTRUCTIONS, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 383 | PERF_HW("perf:thread:cache-references", CACHE_REFERENCES, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 384 | PERF_HW("perf:thread:cache-misses", CACHE_MISSES, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 385 | PERF_HW("perf:thread:branch-instructions", |
| 386 | BRANCH_INSTRUCTIONS, |
| 387 | CONTEXT_PERF_THREAD_COUNTER, |
| 388 | 0), |
| 389 | PERF_HW("perf:thread:branches", BRANCH_INSTRUCTIONS, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 390 | PERF_HW("perf:thread:branch-misses", BRANCH_MISSES, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 391 | PERF_HW("perf:thread:bus-cycles", BUS_CYCLES, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 392 | |
| 393 | PERF_HW_CACHE("perf:thread:L1-dcache", L1D, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 394 | PERF_HW_CACHE("perf:thread:L1-icache", L1I, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 395 | PERF_HW_CACHE("perf:thread:LLC", LL, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 396 | PERF_HW_CACHE("perf:thread:dTLB", DTLB, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 397 | _PERF_HW_CACHE("perf:thread:iTLB-loads", ITLB, CONTEXT_PERF_THREAD_COUNTER, READ, ACCESS, 0), |
| 398 | _PERF_HW_CACHE( |
| 399 | "perf:thread:iTLB-load-misses", ITLB, CONTEXT_PERF_THREAD_COUNTER, READ, MISS, 0), |
| 400 | _PERF_HW_CACHE( |
| 401 | "perf:thread:branch-loads", BPU, CONTEXT_PERF_THREAD_COUNTER, READ, ACCESS, 0), |
| 402 | _PERF_HW_CACHE( |
| 403 | "perf:thread:branch-load-misses", BPU, CONTEXT_PERF_THREAD_COUNTER, READ, MISS, 0), |
| 404 | |
| 405 | PERF_SW("perf:thread:cpu-clock", CPU_CLOCK, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 406 | PERF_SW("perf:thread:task-clock", TASK_CLOCK, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 407 | PERF_SW("perf:thread:page-fault", PAGE_FAULTS, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 408 | PERF_SW("perf:thread:faults", PAGE_FAULTS, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 409 | PERF_SW("perf:thread:major-faults", PAGE_FAULTS_MAJ, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 410 | PERF_SW("perf:thread:minor-faults", PAGE_FAULTS_MIN, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 411 | PERF_SW("perf:thread:context-switches", CONTEXT_SWITCHES, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 412 | PERF_SW("perf:thread:cs", CONTEXT_SWITCHES, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 413 | PERF_SW("perf:thread:cpu-migrations", CPU_MIGRATIONS, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 414 | PERF_SW("perf:thread:migrations", CPU_MIGRATIONS, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 415 | PERF_SW("perf:thread:alignment-faults", ALIGNMENT_FAULTS, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 416 | PERF_SW("perf:thread:emulation-faults", EMULATION_FAULTS, CONTEXT_PERF_THREAD_COUNTER, 0), |
| 417 | |
| 418 | /* |
| 419 | * Perf per-CPU counters, backward compatibilty for names. |
| 420 | * Hidden from help listing. |
| 421 | */ |
| 422 | PERF_HW("perf:cpu-cycles", CPU_CYCLES, CONTEXT_PERF_COUNTER, 1), |
| 423 | PERF_HW("perf:cycles", CPU_CYCLES, CONTEXT_PERF_COUNTER, 1), |
| 424 | PERF_HW("perf:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND, CONTEXT_PERF_COUNTER, 1), |
| 425 | PERF_HW("perf:idle-cycles-frontend", STALLED_CYCLES_FRONTEND, CONTEXT_PERF_COUNTER, 1), |
| 426 | PERF_HW("perf:stalled-cycles-backend", STALLED_CYCLES_BACKEND, CONTEXT_PERF_COUNTER, 1), |
| 427 | PERF_HW("perf:idle-cycles-backend", STALLED_CYCLES_BACKEND, CONTEXT_PERF_COUNTER, 1), |
| 428 | PERF_HW("perf:instructions", INSTRUCTIONS, CONTEXT_PERF_COUNTER, 1), |
| 429 | PERF_HW("perf:cache-references", CACHE_REFERENCES, CONTEXT_PERF_COUNTER, 1), |
| 430 | PERF_HW("perf:cache-misses", CACHE_MISSES, CONTEXT_PERF_COUNTER, 1), |
| 431 | PERF_HW("perf:branch-instructions", BRANCH_INSTRUCTIONS, CONTEXT_PERF_COUNTER, 1), |
| 432 | PERF_HW("perf:branches", BRANCH_INSTRUCTIONS, CONTEXT_PERF_COUNTER, 1), |
| 433 | PERF_HW("perf:branch-misses", BRANCH_MISSES, CONTEXT_PERF_COUNTER, 1), |
| 434 | PERF_HW("perf:bus-cycles", BUS_CYCLES, CONTEXT_PERF_COUNTER, 1), |
| 435 | |
| 436 | PERF_HW_CACHE("perf:L1-dcache", L1D, CONTEXT_PERF_COUNTER, 1), |
| 437 | PERF_HW_CACHE("perf:L1-icache", L1I, CONTEXT_PERF_COUNTER, 1), |
| 438 | PERF_HW_CACHE("perf:LLC", LL, CONTEXT_PERF_COUNTER, 1), |
| 439 | PERF_HW_CACHE("perf:dTLB", DTLB, CONTEXT_PERF_COUNTER, 1), |
| 440 | _PERF_HW_CACHE("perf:iTLB-loads", ITLB, CONTEXT_PERF_COUNTER, READ, ACCESS, 1), |
| 441 | _PERF_HW_CACHE("perf:iTLB-load-misses", ITLB, CONTEXT_PERF_COUNTER, READ, MISS, 1), |
| 442 | _PERF_HW_CACHE("perf:branch-loads", BPU, CONTEXT_PERF_COUNTER, READ, ACCESS, 1), |
| 443 | _PERF_HW_CACHE("perf:branch-load-misses", BPU, CONTEXT_PERF_COUNTER, READ, MISS, 1), |
| 444 | |
| 445 | PERF_SW("perf:cpu-clock", CPU_CLOCK, CONTEXT_PERF_COUNTER, 1), |
| 446 | PERF_SW("perf:task-clock", TASK_CLOCK, CONTEXT_PERF_COUNTER, 1), |
| 447 | PERF_SW("perf:page-fault", PAGE_FAULTS, CONTEXT_PERF_COUNTER, 1), |
| 448 | PERF_SW("perf:faults", PAGE_FAULTS, CONTEXT_PERF_COUNTER, 1), |
| 449 | PERF_SW("perf:major-faults", PAGE_FAULTS_MAJ, CONTEXT_PERF_COUNTER, 1), |
| 450 | PERF_SW("perf:minor-faults", PAGE_FAULTS_MIN, CONTEXT_PERF_COUNTER, 1), |
| 451 | PERF_SW("perf:context-switches", CONTEXT_SWITCHES, CONTEXT_PERF_COUNTER, 1), |
| 452 | PERF_SW("perf:cs", CONTEXT_SWITCHES, CONTEXT_PERF_COUNTER, 1), |
| 453 | PERF_SW("perf:cpu-migrations", CPU_MIGRATIONS, CONTEXT_PERF_COUNTER, 1), |
| 454 | PERF_SW("perf:migrations", CPU_MIGRATIONS, CONTEXT_PERF_COUNTER, 1), |
| 455 | PERF_SW("perf:alignment-faults", ALIGNMENT_FAULTS, CONTEXT_PERF_COUNTER, 1), |
| 456 | PERF_SW("perf:emulation-faults", EMULATION_FAULTS, CONTEXT_PERF_COUNTER, 1), |
| 457 | |
| 458 | {}, /* Closure */ |
| 459 | }; |
| 460 | |
| 461 | #undef PERF_HW_CACHE |
| 462 | #undef _PERF_HW_CACHE |
| 463 | #undef PERF_SW |
| 464 | #undef PERF_HW |
| 465 | |
| 466 | /* |
| 467 | * Context type for command line option parsing. |
| 468 | */ |
| 469 | struct ctx_type { |
| 470 | struct ctx_opts *opt; |
| 471 | struct cds_list_head list; |
| 472 | }; |
| 473 | |
| 474 | /* |
| 475 | * List of context type. Use to enable multiple context on a single command |
| 476 | * line entry. |
| 477 | */ |
| 478 | struct ctx_type_list { |
| 479 | struct cds_list_head head; |
| 480 | } ctx_type_list = { |
| 481 | .head = CDS_LIST_HEAD_INIT(ctx_type_list.head), |
| 482 | }; |
| 483 | } /* namespace */ |
| 484 | |
| 485 | /* |
| 486 | * Find context numerical value from string. |
| 487 | * |
| 488 | * Return -1 if not found. |
| 489 | */ |
| 490 | static int find_ctx_type_idx(const char *opt) |
| 491 | { |
| 492 | int ret, i = 0; |
| 493 | |
| 494 | while (ctx_opts[i].symbol != nullptr) { |
| 495 | if (strcmp(opt, ctx_opts[i].symbol) == 0) { |
| 496 | ret = i; |
| 497 | goto end; |
| 498 | } |
| 499 | i++; |
| 500 | } |
| 501 | |
| 502 | ret = -1; |
| 503 | end: |
| 504 | return ret; |
| 505 | } |
| 506 | |
| 507 | static enum lttng_domain_type get_domain() |
| 508 | { |
| 509 | if (opt_kernel) { |
| 510 | return LTTNG_DOMAIN_KERNEL; |
| 511 | } else if (opt_userspace) { |
| 512 | return LTTNG_DOMAIN_UST; |
| 513 | } else if (opt_jul) { |
| 514 | return LTTNG_DOMAIN_JUL; |
| 515 | } else if (opt_log4j) { |
| 516 | return LTTNG_DOMAIN_LOG4J; |
| 517 | } else { |
| 518 | abort(); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | static int mi_open() |
| 523 | { |
| 524 | int ret; |
| 525 | |
| 526 | /* MI check */ |
| 527 | if (!lttng_opt_mi) { |
| 528 | ret = 0; |
| 529 | goto end; |
| 530 | } |
| 531 | |
| 532 | ret = fileno(stdout); |
| 533 | if (ret < 0) { |
| 534 | PERROR("Unable to retrieve fileno of stdout"); |
| 535 | ret = CMD_ERROR; |
| 536 | goto end; |
| 537 | } |
| 538 | |
| 539 | writer = mi_lttng_writer_create(ret, lttng_opt_mi); |
| 540 | if (!writer) { |
| 541 | ret = CMD_ERROR; |
| 542 | goto end; |
| 543 | } |
| 544 | |
| 545 | /* Open command element */ |
| 546 | ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_add_context); |
| 547 | if (ret) { |
| 548 | ret = CMD_ERROR; |
| 549 | goto end; |
| 550 | } |
| 551 | |
| 552 | /* Open output element */ |
| 553 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); |
| 554 | if (ret) { |
| 555 | ret = CMD_ERROR; |
| 556 | goto end; |
| 557 | } |
| 558 | end: |
| 559 | return ret; |
| 560 | } |
| 561 | |
| 562 | static int mi_close(enum cmd_error_code success) |
| 563 | { |
| 564 | int ret; |
| 565 | |
| 566 | /* MI closing */ |
| 567 | if (!lttng_opt_mi) { |
| 568 | ret = 0; |
| 569 | goto end; |
| 570 | } |
| 571 | /* Close output element */ |
| 572 | ret = mi_lttng_writer_close_element(writer); |
| 573 | if (ret) { |
| 574 | ret = CMD_ERROR; |
| 575 | goto end; |
| 576 | } |
| 577 | |
| 578 | /* Success ? */ |
| 579 | ret = mi_lttng_writer_write_element_bool( |
| 580 | writer, mi_lttng_element_command_success, !success); |
| 581 | if (ret) { |
| 582 | ret = CMD_ERROR; |
| 583 | goto end; |
| 584 | } |
| 585 | |
| 586 | /* Command element close */ |
| 587 | ret = mi_lttng_writer_command_close(writer); |
| 588 | if (ret) { |
| 589 | ret = CMD_ERROR; |
| 590 | goto end; |
| 591 | } |
| 592 | end: |
| 593 | return ret; |
| 594 | } |
| 595 | |
| 596 | static void populate_context(struct lttng_event_context *context, const struct ctx_opts *opt) |
| 597 | { |
| 598 | char *ptr; |
| 599 | |
| 600 | context->ctx = (enum lttng_event_context_type) opt->ctx_type; |
| 601 | switch (context->ctx) { |
| 602 | case LTTNG_EVENT_CONTEXT_PERF_COUNTER: |
| 603 | case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER: |
| 604 | case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER: |
| 605 | context->u.perf_counter.type = opt->u.perf.type; |
| 606 | context->u.perf_counter.config = opt->u.perf.config; |
| 607 | strncpy(context->u.perf_counter.name, opt->symbol, LTTNG_SYMBOL_NAME_LEN); |
| 608 | context->u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 609 | /* Replace : and - by _ */ |
| 610 | while ((ptr = strchr(context->u.perf_counter.name, '-')) != nullptr) { |
| 611 | *ptr = '_'; |
| 612 | } |
| 613 | while ((ptr = strchr(context->u.perf_counter.name, ':')) != nullptr) { |
| 614 | *ptr = '_'; |
| 615 | } |
| 616 | break; |
| 617 | case LTTNG_EVENT_CONTEXT_APP_CONTEXT: |
| 618 | context->u.app_ctx.provider_name = opt->u.app_ctx.provider_name; |
| 619 | context->u.app_ctx.ctx_name = opt->u.app_ctx.ctx_name; |
| 620 | break; |
| 621 | default: |
| 622 | break; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | * Pretty print context type. |
| 628 | */ |
| 629 | static int print_ctx_type() |
| 630 | { |
| 631 | FILE *ofp = stdout; |
| 632 | int i = 0; |
| 633 | int ret; |
| 634 | struct lttng_event_context context; |
| 635 | |
| 636 | memset(&context, 0, sizeof(context)); |
| 637 | |
| 638 | ret = mi_open(); |
| 639 | if (ret) { |
| 640 | ret = CMD_ERROR; |
| 641 | goto end; |
| 642 | } |
| 643 | |
| 644 | if (lttng_opt_mi) { |
| 645 | /* Open a contexts element */ |
| 646 | ret = mi_lttng_writer_open_element(writer, config_element_contexts); |
| 647 | if (ret) { |
| 648 | ret = CMD_ERROR; |
| 649 | goto end; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | while (ctx_opts[i].symbol != nullptr) { |
| 654 | if (!ctx_opts[i].hide_help) { |
| 655 | if (lttng_opt_mi) { |
| 656 | populate_context(&context, &ctx_opts[i]); |
| 657 | ret = mi_lttng_context(writer, &context, 1); |
| 658 | if (ret) { |
| 659 | ret = CMD_ERROR; |
| 660 | goto end; |
| 661 | } |
| 662 | |
| 663 | ret = mi_lttng_writer_write_element_string( |
| 664 | writer, |
| 665 | mi_lttng_element_context_symbol, |
| 666 | ctx_opts[i].symbol); |
| 667 | if (ret) { |
| 668 | ret = CMD_ERROR; |
| 669 | goto end; |
| 670 | } |
| 671 | |
| 672 | ret = mi_lttng_writer_close_element(writer); |
| 673 | if (ret) { |
| 674 | ret = CMD_ERROR; |
| 675 | goto end; |
| 676 | } |
| 677 | } else { |
| 678 | fprintf(ofp, "%s\n", ctx_opts[i].symbol); |
| 679 | } |
| 680 | } |
| 681 | i++; |
| 682 | } |
| 683 | |
| 684 | if (lttng_opt_mi) { |
| 685 | /* Close contexts element */ |
| 686 | ret = mi_lttng_writer_close_element(writer); |
| 687 | if (ret) { |
| 688 | goto end; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | end: |
| 693 | ret = mi_close((cmd_error_code) ret); |
| 694 | if (ret) { |
| 695 | ret = CMD_ERROR; |
| 696 | } |
| 697 | return ret; |
| 698 | } |
| 699 | |
| 700 | /* |
| 701 | * Add context to channel or event. |
| 702 | */ |
| 703 | static int add_context(char *session_name) |
| 704 | { |
| 705 | int ret = CMD_SUCCESS, warn = 0, success = 0; |
| 706 | struct lttng_event_context context; |
| 707 | struct lttng_domain dom; |
| 708 | struct ctx_type *type; |
| 709 | |
| 710 | memset(&context, 0, sizeof(context)); |
| 711 | memset(&dom, 0, sizeof(dom)); |
| 712 | |
| 713 | dom.type = get_domain(); |
| 714 | handle = lttng_create_handle(session_name, &dom); |
| 715 | if (handle == nullptr) { |
| 716 | ret = CMD_ERROR; |
| 717 | goto error; |
| 718 | } |
| 719 | |
| 720 | if (lttng_opt_mi) { |
| 721 | /* Open a contexts element */ |
| 722 | ret = mi_lttng_writer_open_element(writer, config_element_contexts); |
| 723 | if (ret) { |
| 724 | goto error; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | /* Iterate over all the context types given */ |
| 729 | cds_list_for_each_entry (type, &ctx_type_list.head, list) { |
| 730 | DBG("Adding context..."); |
| 731 | |
| 732 | populate_context(&context, type->opt); |
| 733 | |
| 734 | if (lttng_opt_mi) { |
| 735 | /* We leave context open the update the success of the command */ |
| 736 | ret = mi_lttng_context(writer, &context, 1); |
| 737 | if (ret) { |
| 738 | ret = CMD_ERROR; |
| 739 | goto error; |
| 740 | } |
| 741 | |
| 742 | ret = mi_lttng_writer_write_element_string( |
| 743 | writer, mi_lttng_element_context_symbol, type->opt->symbol); |
| 744 | if (ret) { |
| 745 | ret = CMD_ERROR; |
| 746 | goto error; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | ret = lttng_add_context(handle, &context, nullptr, opt_channel_name); |
| 751 | if (ret < 0) { |
| 752 | ERR("%s: %s", type->opt->symbol, lttng_strerror(ret)); |
| 753 | warn = 1; |
| 754 | success = 0; |
| 755 | } else { |
| 756 | if (opt_channel_name) { |
| 757 | MSG("%s context %s added to channel %s", |
| 758 | lttng_domain_type_str(dom.type), |
| 759 | type->opt->symbol, |
| 760 | opt_channel_name); |
| 761 | } else { |
| 762 | MSG("%s context %s added to all channels", |
| 763 | lttng_domain_type_str(dom.type), |
| 764 | type->opt->symbol); |
| 765 | } |
| 766 | success = 1; |
| 767 | } |
| 768 | |
| 769 | if (lttng_opt_mi) { |
| 770 | /* Is the single operation a success ? */ |
| 771 | ret = mi_lttng_writer_write_element_bool( |
| 772 | writer, mi_lttng_element_success, success); |
| 773 | if (ret) { |
| 774 | ret = CMD_ERROR; |
| 775 | goto error; |
| 776 | } |
| 777 | |
| 778 | /* Close the context element */ |
| 779 | ret = mi_lttng_writer_close_element(writer); |
| 780 | if (ret) { |
| 781 | ret = CMD_ERROR; |
| 782 | goto error; |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | if (lttng_opt_mi) { |
| 788 | /* Close contexts element */ |
| 789 | ret = mi_lttng_writer_close_element(writer); |
| 790 | if (ret) { |
| 791 | goto error; |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | ret = CMD_SUCCESS; |
| 796 | |
| 797 | error: |
| 798 | lttng_destroy_handle(handle); |
| 799 | |
| 800 | /* |
| 801 | * This means that at least one add_context failed and tells the user to |
| 802 | * look on stderr for error(s). |
| 803 | */ |
| 804 | if (!ret && warn) { |
| 805 | ret = CMD_WARNING; |
| 806 | } |
| 807 | return ret; |
| 808 | } |
| 809 | |
| 810 | static void destroy_ctx_type(struct ctx_type *type) |
| 811 | { |
| 812 | if (!type) { |
| 813 | return; |
| 814 | } |
| 815 | |
| 816 | if (type->opt) { |
| 817 | free(type->opt->symbol); |
| 818 | |
| 819 | if (type->opt->ctx_type == CONTEXT_APP_CONTEXT) { |
| 820 | free(type->opt->u.app_ctx.ctx_name); |
| 821 | free(type->opt->u.app_ctx.provider_name); |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | delete type->opt; |
| 826 | free(type); |
| 827 | } |
| 828 | |
| 829 | static struct ctx_type *create_ctx_type() |
| 830 | { |
| 831 | struct ctx_type *type = zmalloc<ctx_type>(); |
| 832 | |
| 833 | if (!type) { |
| 834 | PERROR("malloc ctx_type"); |
| 835 | goto end; |
| 836 | } |
| 837 | |
| 838 | type->opt = new struct ctx_opts; |
| 839 | if (!type->opt) { |
| 840 | PERROR("malloc ctx_type options"); |
| 841 | destroy_ctx_type(type); |
| 842 | type = nullptr; |
| 843 | goto end; |
| 844 | } |
| 845 | end: |
| 846 | return type; |
| 847 | } |
| 848 | |
| 849 | static int find_ctx_type_perf_raw(const char *ctx, struct ctx_type *type) |
| 850 | { |
| 851 | int ret; |
| 852 | int field_pos = 0; |
| 853 | char *tmp_list, *cur_list; |
| 854 | |
| 855 | cur_list = tmp_list = strdup(ctx); |
| 856 | if (!tmp_list) { |
| 857 | PERROR("strdup temp list"); |
| 858 | ret = -ENOMEM; |
| 859 | goto end; |
| 860 | } |
| 861 | |
| 862 | /* Looking for "perf:[cpu|thread]:raw:<mask>:<name>". */ |
| 863 | for (;;) { |
| 864 | char *next; |
| 865 | |
| 866 | next = strtok(cur_list, ":"); |
| 867 | if (!next) { |
| 868 | break; |
| 869 | } |
| 870 | cur_list = nullptr; |
| 871 | switch (field_pos) { |
| 872 | case 0: |
| 873 | if (strncmp(next, "perf", 4) != 0) { |
| 874 | ret = -1; |
| 875 | goto end; |
| 876 | } |
| 877 | break; |
| 878 | case 1: |
| 879 | if (strncmp(next, "cpu", 3) == 0) { |
| 880 | type->opt->ctx_type = CONTEXT_PERF_CPU_COUNTER; |
| 881 | } else if (strncmp(next, "thread", 4) == 0) { |
| 882 | type->opt->ctx_type = CONTEXT_PERF_THREAD_COUNTER; |
| 883 | } else { |
| 884 | ret = -1; |
| 885 | goto end; |
| 886 | } |
| 887 | break; |
| 888 | case 2: |
| 889 | if (strncmp(next, "raw", 3) != 0) { |
| 890 | ret = -1; |
| 891 | goto end; |
| 892 | } |
| 893 | break; |
| 894 | case 3: |
| 895 | { |
| 896 | char *endptr; |
| 897 | |
| 898 | if (strlen(next) < 2 || next[0] != 'r') { |
| 899 | ERR("Wrong perf raw mask format: expected rNNN"); |
| 900 | ret = -1; |
| 901 | goto end; |
| 902 | } |
| 903 | errno = 0; |
| 904 | type->opt->u.perf.config = strtoll(next + 1, &endptr, 16); |
| 905 | if (errno != 0 || !endptr || *endptr) { |
| 906 | ERR("Wrong perf raw mask format: expected rNNN"); |
| 907 | ret = -1; |
| 908 | goto end; |
| 909 | } |
| 910 | break; |
| 911 | } |
| 912 | case 4: |
| 913 | /* name */ |
| 914 | break; |
| 915 | case 5: |
| 916 | ERR("Too many ':' in perf raw format"); |
| 917 | ret = -1; |
| 918 | goto end; |
| 919 | }; |
| 920 | field_pos++; |
| 921 | } |
| 922 | |
| 923 | if (field_pos < 5) { |
| 924 | ERR("Invalid perf counter specifier, expected a specifier of " |
| 925 | "the form perf:cpu:raw:rNNN:<name> or " |
| 926 | "perf:thread:raw:rNNN:<name>"); |
| 927 | ret = -1; |
| 928 | goto end; |
| 929 | } |
| 930 | |
| 931 | ret = 0; |
| 932 | goto end; |
| 933 | |
| 934 | end: |
| 935 | free(tmp_list); |
| 936 | return ret; |
| 937 | } |
| 938 | |
| 939 | static struct ctx_type *get_context_type(const char *ctx) |
| 940 | { |
| 941 | int opt_index, ret; |
| 942 | struct ctx_type *type = nullptr; |
| 943 | const char app_ctx_prefix[] = "$app."; |
| 944 | char *provider_name = nullptr, *ctx_name = nullptr; |
| 945 | size_t i, len, colon_pos = 0, provider_name_len, ctx_name_len; |
| 946 | |
| 947 | if (!ctx) { |
| 948 | goto not_found; |
| 949 | } |
| 950 | |
| 951 | type = create_ctx_type(); |
| 952 | if (!type) { |
| 953 | goto not_found; |
| 954 | } |
| 955 | |
| 956 | /* Check if ctx matches a known static context. */ |
| 957 | opt_index = find_ctx_type_idx(ctx); |
| 958 | if (opt_index >= 0) { |
| 959 | *type->opt = ctx_opts[opt_index]; |
| 960 | type->opt->symbol = strdup(ctx_opts[opt_index].symbol); |
| 961 | goto found; |
| 962 | } |
| 963 | |
| 964 | /* Check if ctx is a raw perf context. */ |
| 965 | ret = find_ctx_type_perf_raw(ctx, type); |
| 966 | if (ret == 0) { |
| 967 | type->opt->u.perf.type = PERF_TYPE_RAW; |
| 968 | type->opt->symbol = strdup(ctx); |
| 969 | if (!type->opt->symbol) { |
| 970 | PERROR("Copy perf field name"); |
| 971 | goto not_found; |
| 972 | } |
| 973 | goto found; |
| 974 | } |
| 975 | |
| 976 | /* |
| 977 | * No match found against static contexts; check if it is an app |
| 978 | * context. |
| 979 | */ |
| 980 | len = strlen(ctx); |
| 981 | if (len <= sizeof(app_ctx_prefix) - 1) { |
| 982 | goto not_found; |
| 983 | } |
| 984 | |
| 985 | /* String starts with $app. */ |
| 986 | if (strncmp(ctx, app_ctx_prefix, sizeof(app_ctx_prefix) - 1) != 0) { |
| 987 | goto not_found; |
| 988 | } |
| 989 | |
| 990 | /* Validate that the ':' separator is present. */ |
| 991 | for (i = sizeof(app_ctx_prefix); i < len; i++) { |
| 992 | const char c = ctx[i]; |
| 993 | |
| 994 | if (c == ':') { |
| 995 | colon_pos = i; |
| 996 | break; |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | /* |
| 1001 | * No colon found or no ctx name ("$app.provider:") or no provider name |
| 1002 | * given ("$app.:..."), which is invalid. |
| 1003 | */ |
| 1004 | if (!colon_pos || colon_pos == len || colon_pos == sizeof(app_ctx_prefix)) { |
| 1005 | ERR("Invalid application context provided: no provider or context name provided."); |
| 1006 | goto not_found; |
| 1007 | } |
| 1008 | |
| 1009 | provider_name_len = colon_pos - sizeof(app_ctx_prefix) + 2; |
| 1010 | provider_name = calloc<char>(provider_name_len); |
| 1011 | if (!provider_name) { |
| 1012 | PERROR("malloc provider_name"); |
| 1013 | goto not_found; |
| 1014 | } |
| 1015 | strncpy(provider_name, ctx + sizeof(app_ctx_prefix) - 1, provider_name_len - 1); |
| 1016 | type->opt->u.app_ctx.provider_name = provider_name; |
| 1017 | |
| 1018 | ctx_name_len = len - colon_pos; |
| 1019 | ctx_name = calloc<char>(ctx_name_len); |
| 1020 | if (!ctx_name) { |
| 1021 | PERROR("malloc ctx_name"); |
| 1022 | goto not_found; |
| 1023 | } |
| 1024 | strncpy(ctx_name, ctx + colon_pos + 1, ctx_name_len - 1); |
| 1025 | type->opt->u.app_ctx.ctx_name = ctx_name; |
| 1026 | type->opt->ctx_type = CONTEXT_APP_CONTEXT; |
| 1027 | type->opt->symbol = strdup(ctx); |
| 1028 | found: |
| 1029 | return type; |
| 1030 | not_found: |
| 1031 | free(provider_name); |
| 1032 | free(ctx_name); |
| 1033 | destroy_ctx_type(type); |
| 1034 | return nullptr; |
| 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * Add context to channel or event. |
| 1039 | */ |
| 1040 | int cmd_add_context(int argc, const char **argv) |
| 1041 | { |
| 1042 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS; |
| 1043 | static poptContext pc; |
| 1044 | struct ctx_type *type, *tmptype; |
| 1045 | char *session_name = nullptr; |
| 1046 | const char *leftover = nullptr; |
| 1047 | |
| 1048 | if (argc < 2) { |
| 1049 | ret = CMD_ERROR; |
| 1050 | goto end; |
| 1051 | } |
| 1052 | |
| 1053 | pc = poptGetContext(nullptr, argc, argv, long_options, 0); |
| 1054 | poptReadDefaultConfig(pc, 0); |
| 1055 | |
| 1056 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 1057 | switch (opt) { |
| 1058 | case OPT_HELP: |
| 1059 | SHOW_HELP(); |
| 1060 | goto end; |
| 1061 | case OPT_LIST: |
| 1062 | ret = print_ctx_type(); |
| 1063 | goto end; |
| 1064 | case OPT_TYPE: |
| 1065 | { |
| 1066 | type = get_context_type(opt_type); |
| 1067 | if (!type) { |
| 1068 | ERR("Unknown context type %s", opt_type); |
| 1069 | ret = CMD_FATAL; |
| 1070 | goto end; |
| 1071 | } |
| 1072 | cds_list_add_tail(&type->list, &ctx_type_list.head); |
| 1073 | break; |
| 1074 | } |
| 1075 | case OPT_USERSPACE: |
| 1076 | opt_userspace = 1; |
| 1077 | break; |
| 1078 | case OPT_JUL: |
| 1079 | opt_jul = 1; |
| 1080 | break; |
| 1081 | case OPT_LOG4J: |
| 1082 | opt_log4j = 1; |
| 1083 | break; |
| 1084 | case OPT_LIST_OPTIONS: |
| 1085 | list_cmd_options(stdout, long_options); |
| 1086 | goto end; |
| 1087 | default: |
| 1088 | ret = CMD_UNDEFINED; |
| 1089 | goto end; |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | leftover = poptGetArg(pc); |
| 1094 | if (leftover) { |
| 1095 | ERR("Unknown argument: %s", leftover); |
| 1096 | ret = CMD_ERROR; |
| 1097 | goto end; |
| 1098 | } |
| 1099 | |
| 1100 | ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace + opt_jul + opt_log4j, |
| 1101 | true); |
| 1102 | if (ret) { |
| 1103 | ret = CMD_ERROR; |
| 1104 | goto end; |
| 1105 | } |
| 1106 | |
| 1107 | if (!opt_type) { |
| 1108 | ERR("Missing mandatory -t TYPE"); |
| 1109 | ret = CMD_ERROR; |
| 1110 | goto end; |
| 1111 | } |
| 1112 | |
| 1113 | if (!opt_session_name) { |
| 1114 | session_name = get_session_name(); |
| 1115 | if (session_name == nullptr) { |
| 1116 | ret = CMD_ERROR; |
| 1117 | goto end; |
| 1118 | } |
| 1119 | } else { |
| 1120 | session_name = opt_session_name; |
| 1121 | } |
| 1122 | |
| 1123 | ret = mi_open(); |
| 1124 | if (ret) { |
| 1125 | goto end; |
| 1126 | } |
| 1127 | |
| 1128 | command_ret = add_context(session_name); |
| 1129 | ret = mi_close((cmd_error_code) command_ret); |
| 1130 | if (ret) { |
| 1131 | goto end; |
| 1132 | } |
| 1133 | |
| 1134 | end: |
| 1135 | if (!opt_session_name) { |
| 1136 | free(session_name); |
| 1137 | } |
| 1138 | |
| 1139 | /* Mi clean-up */ |
| 1140 | if (writer && mi_lttng_writer_destroy(writer)) { |
| 1141 | /* Preserve original error code */ |
| 1142 | ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL; |
| 1143 | } |
| 1144 | |
| 1145 | /* Cleanup allocated memory */ |
| 1146 | cds_list_for_each_entry_safe (type, tmptype, &ctx_type_list.head, list) { |
| 1147 | destroy_ctx_type(type); |
| 1148 | } |
| 1149 | |
| 1150 | /* Overwrite ret if an error occurred during add_context() */ |
| 1151 | ret = command_ret ? command_ret : ret; |
| 1152 | |
| 1153 | poptFreeContext(pc); |
| 1154 | return ret; |
| 1155 | } |