clang-tidy: add Chrome-inspired checks
[lttng-tools.git] / src / bin / lttng / commands / add_context.cpp
... / ...
CommitLineData
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
26static char *opt_channel_name;
27static char *opt_session_name;
28static int opt_kernel;
29static int opt_userspace;
30static int opt_jul;
31static int opt_log4j;
32static char *opt_type;
33
34#ifdef LTTNG_EMBED_HELP
35static const char help_msg[] =
36#include <lttng-add-context.1.h>
37 ;
38#endif
39
40enum {
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
50static struct lttng_handle *handle;
51static struct mi_writer *writer;
52
53/*
54 * Taken from the LTTng ABI except for "UNKNOWN".
55 */
56enum 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 */
105enum 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
112enum 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
124enum 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 */
143enum 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
154enum 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
162enum 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
169static 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
211namespace {
212const 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 */
469struct 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 */
478struct 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 */
490static 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;
503end:
504 return ret;
505}
506
507static 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
522static 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 }
558end:
559 return ret;
560}
561
562static 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 }
592end:
593 return ret;
594}
595
596static 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 */
629static 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
692end:
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 */
703static 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
797error:
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
810static void destroy_ctx_type(struct ctx_type *type)
811{
812 if (!type) {
813 return;
814 }
815 if (type->opt) {
816 free(type->opt->symbol);
817 }
818 delete type->opt;
819 free(type);
820}
821
822static struct ctx_type *create_ctx_type()
823{
824 struct ctx_type *type = zmalloc<ctx_type>();
825
826 if (!type) {
827 PERROR("malloc ctx_type");
828 goto end;
829 }
830
831 type->opt = new struct ctx_opts;
832 if (!type->opt) {
833 PERROR("malloc ctx_type options");
834 destroy_ctx_type(type);
835 type = nullptr;
836 goto end;
837 }
838end:
839 return type;
840}
841
842static int find_ctx_type_perf_raw(const char *ctx, struct ctx_type *type)
843{
844 int ret;
845 int field_pos = 0;
846 char *tmp_list, *cur_list;
847
848 cur_list = tmp_list = strdup(ctx);
849 if (!tmp_list) {
850 PERROR("strdup temp list");
851 ret = -ENOMEM;
852 goto end;
853 }
854
855 /* Looking for "perf:[cpu|thread]:raw:<mask>:<name>". */
856 for (;;) {
857 char *next;
858
859 next = strtok(cur_list, ":");
860 if (!next) {
861 break;
862 }
863 cur_list = nullptr;
864 switch (field_pos) {
865 case 0:
866 if (strncmp(next, "perf", 4) != 0) {
867 ret = -1;
868 goto end;
869 }
870 break;
871 case 1:
872 if (strncmp(next, "cpu", 3) == 0) {
873 type->opt->ctx_type = CONTEXT_PERF_CPU_COUNTER;
874 } else if (strncmp(next, "thread", 4) == 0) {
875 type->opt->ctx_type = CONTEXT_PERF_THREAD_COUNTER;
876 } else {
877 ret = -1;
878 goto end;
879 }
880 break;
881 case 2:
882 if (strncmp(next, "raw", 3) != 0) {
883 ret = -1;
884 goto end;
885 }
886 break;
887 case 3:
888 {
889 char *endptr;
890
891 if (strlen(next) < 2 || next[0] != 'r') {
892 ERR("Wrong perf raw mask format: expected rNNN");
893 ret = -1;
894 goto end;
895 }
896 errno = 0;
897 type->opt->u.perf.config = strtoll(next + 1, &endptr, 16);
898 if (errno != 0 || !endptr || *endptr) {
899 ERR("Wrong perf raw mask format: expected rNNN");
900 ret = -1;
901 goto end;
902 }
903 break;
904 }
905 case 4:
906 /* name */
907 break;
908 case 5:
909 ERR("Too many ':' in perf raw format");
910 ret = -1;
911 goto end;
912 };
913 field_pos++;
914 }
915
916 if (field_pos < 5) {
917 ERR("Invalid perf counter specifier, expected a specifier of "
918 "the form perf:cpu:raw:rNNN:<name> or "
919 "perf:thread:raw:rNNN:<name>");
920 ret = -1;
921 goto end;
922 }
923
924 ret = 0;
925 goto end;
926
927end:
928 free(tmp_list);
929 return ret;
930}
931
932static struct ctx_type *get_context_type(const char *ctx)
933{
934 int opt_index, ret;
935 struct ctx_type *type = nullptr;
936 const char app_ctx_prefix[] = "$app.";
937 char *provider_name = nullptr, *ctx_name = nullptr;
938 size_t i, len, colon_pos = 0, provider_name_len, ctx_name_len;
939
940 if (!ctx) {
941 goto not_found;
942 }
943
944 type = create_ctx_type();
945 if (!type) {
946 goto not_found;
947 }
948
949 /* Check if ctx matches a known static context. */
950 opt_index = find_ctx_type_idx(ctx);
951 if (opt_index >= 0) {
952 *type->opt = ctx_opts[opt_index];
953 type->opt->symbol = strdup(ctx_opts[opt_index].symbol);
954 goto found;
955 }
956
957 /* Check if ctx is a raw perf context. */
958 ret = find_ctx_type_perf_raw(ctx, type);
959 if (ret == 0) {
960 type->opt->u.perf.type = PERF_TYPE_RAW;
961 type->opt->symbol = strdup(ctx);
962 if (!type->opt->symbol) {
963 PERROR("Copy perf field name");
964 goto not_found;
965 }
966 goto found;
967 }
968
969 /*
970 * No match found against static contexts; check if it is an app
971 * context.
972 */
973 len = strlen(ctx);
974 if (len <= sizeof(app_ctx_prefix) - 1) {
975 goto not_found;
976 }
977
978 /* String starts with $app. */
979 if (strncmp(ctx, app_ctx_prefix, sizeof(app_ctx_prefix) - 1)) {
980 goto not_found;
981 }
982
983 /* Validate that the ':' separator is present. */
984 for (i = sizeof(app_ctx_prefix); i < len; i++) {
985 const char c = ctx[i];
986
987 if (c == ':') {
988 colon_pos = i;
989 break;
990 }
991 }
992
993 /*
994 * No colon found or no ctx name ("$app.provider:") or no provider name
995 * given ("$app.:..."), which is invalid.
996 */
997 if (!colon_pos || colon_pos == len || colon_pos == sizeof(app_ctx_prefix)) {
998 ERR("Invalid application context provided: no provider or context name provided.");
999 goto not_found;
1000 }
1001
1002 provider_name_len = colon_pos - sizeof(app_ctx_prefix) + 2;
1003 provider_name = calloc<char>(provider_name_len);
1004 if (!provider_name) {
1005 PERROR("malloc provider_name");
1006 goto not_found;
1007 }
1008 strncpy(provider_name, ctx + sizeof(app_ctx_prefix) - 1, provider_name_len - 1);
1009 type->opt->u.app_ctx.provider_name = provider_name;
1010
1011 ctx_name_len = len - colon_pos;
1012 ctx_name = calloc<char>(ctx_name_len);
1013 if (!ctx_name) {
1014 PERROR("malloc ctx_name");
1015 goto not_found;
1016 }
1017 strncpy(ctx_name, ctx + colon_pos + 1, ctx_name_len - 1);
1018 type->opt->u.app_ctx.ctx_name = ctx_name;
1019 type->opt->ctx_type = CONTEXT_APP_CONTEXT;
1020 type->opt->symbol = strdup(ctx);
1021found:
1022 return type;
1023not_found:
1024 free(provider_name);
1025 free(ctx_name);
1026 destroy_ctx_type(type);
1027 return nullptr;
1028}
1029
1030/*
1031 * Add context to channel or event.
1032 */
1033int cmd_add_context(int argc, const char **argv)
1034{
1035 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
1036 static poptContext pc;
1037 struct ctx_type *type, *tmptype;
1038 char *session_name = nullptr;
1039 const char *leftover = nullptr;
1040
1041 if (argc < 2) {
1042 ret = CMD_ERROR;
1043 goto end;
1044 }
1045
1046 pc = poptGetContext(nullptr, argc, argv, long_options, 0);
1047 poptReadDefaultConfig(pc, 0);
1048
1049 while ((opt = poptGetNextOpt(pc)) != -1) {
1050 switch (opt) {
1051 case OPT_HELP:
1052 SHOW_HELP();
1053 goto end;
1054 case OPT_LIST:
1055 ret = print_ctx_type();
1056 goto end;
1057 case OPT_TYPE:
1058 {
1059 type = get_context_type(opt_type);
1060 if (!type) {
1061 ERR("Unknown context type %s", opt_type);
1062 ret = CMD_FATAL;
1063 goto end;
1064 }
1065 cds_list_add_tail(&type->list, &ctx_type_list.head);
1066 break;
1067 }
1068 case OPT_USERSPACE:
1069 opt_userspace = 1;
1070 break;
1071 case OPT_JUL:
1072 opt_jul = 1;
1073 break;
1074 case OPT_LOG4J:
1075 opt_log4j = 1;
1076 break;
1077 case OPT_LIST_OPTIONS:
1078 list_cmd_options(stdout, long_options);
1079 goto end;
1080 default:
1081 ret = CMD_UNDEFINED;
1082 goto end;
1083 }
1084 }
1085
1086 leftover = poptGetArg(pc);
1087 if (leftover) {
1088 ERR("Unknown argument: %s", leftover);
1089 ret = CMD_ERROR;
1090 goto end;
1091 }
1092
1093 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace + opt_jul + opt_log4j,
1094 true);
1095 if (ret) {
1096 ret = CMD_ERROR;
1097 goto end;
1098 }
1099
1100 if (!opt_type) {
1101 ERR("Missing mandatory -t TYPE");
1102 ret = CMD_ERROR;
1103 goto end;
1104 }
1105
1106 if (!opt_session_name) {
1107 session_name = get_session_name();
1108 if (session_name == nullptr) {
1109 ret = CMD_ERROR;
1110 goto end;
1111 }
1112 } else {
1113 session_name = opt_session_name;
1114 }
1115
1116 ret = mi_open();
1117 if (ret) {
1118 goto end;
1119 }
1120
1121 command_ret = add_context(session_name);
1122 ret = mi_close((cmd_error_code) command_ret);
1123 if (ret) {
1124 goto end;
1125 }
1126
1127end:
1128 if (!opt_session_name) {
1129 free(session_name);
1130 }
1131
1132 /* Mi clean-up */
1133 if (writer && mi_lttng_writer_destroy(writer)) {
1134 /* Preserve original error code */
1135 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
1136 }
1137
1138 /* Cleanup allocated memory */
1139 cds_list_for_each_entry_safe (type, tmptype, &ctx_type_list.head, list) {
1140 destroy_ctx_type(type);
1141 }
1142
1143 /* Overwrite ret if an error occurred during add_context() */
1144 ret = command_ret ? command_ret : ret;
1145
1146 poptFreeContext(pc);
1147 return ret;
1148}
This page took 0.026159 seconds and 4 git commands to generate.