Rename C++ header files to .hpp
[lttng-tools.git] / src / bin / lttng / commands / add_context.cpp
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 <ctype.h>
11 #include <popt.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <unistd.h>
18
19 #include <urcu/list.h>
20
21 #include <lttng/domain-internal.hpp>
22 #include <common/mi-lttng.hpp>
23
24 #include "../command.hpp"
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, 0, OPT_HELP, 0, 0},
172 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
173 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
174 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
175 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
176 {"jul", 'j', POPT_ARG_NONE, 0, OPT_JUL, 0, 0},
177 {"log4j", 'l', POPT_ARG_NONE, 0, OPT_LOG4J, 0, 0},
178 {"type", 't', POPT_ARG_STRING, &opt_type, OPT_TYPE, 0, 0},
179 {"list", 0, POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL},
180 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
181 {0, 0, 0, 0, 0, 0, 0}
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, \
200 PERF_COUNT_HW_CACHE_##name, \
201 PERF_COUNT_HW_CACHE_OP_##op, \
202 PERF_COUNT_HW_CACHE_RESULT_##result, \
203 hide, \
204 }
205
206 #define PERF_HW_CACHE(optstr, name, type, hide) \
207 _PERF_HW_CACHE(optstr "-loads", name, type, \
208 READ, ACCESS, hide), \
209 _PERF_HW_CACHE(optstr "-load-misses", name, type, \
210 READ, MISS, hide), \
211 _PERF_HW_CACHE(optstr "-stores", name, type, \
212 WRITE, ACCESS, hide), \
213 _PERF_HW_CACHE(optstr "-store-misses", name, type, \
214 WRITE, MISS, hide), \
215 _PERF_HW_CACHE(optstr "-prefetches", name, type, \
216 PREFETCH, ACCESS, hide), \
217 _PERF_HW_CACHE(optstr "-prefetch-misses", name, type, \
218 PREFETCH, MISS, hide)
219
220 static
221 const struct ctx_opts {
222 /* Needed for end-of-list item. */
223 ctx_opts()
224 : ctx_opts(nullptr, CONTEXT_UNKNOWN)
225 {}
226
227 ctx_opts(const char *symbol_, context_type ctx_type_, bool hide_help_ = false)
228 : symbol((char *) symbol_), ctx_type(ctx_type_), hide_help(hide_help_)
229 {}
230
231 ctx_opts(const char *symbol_, context_type ctx_type_, perf_count_hard perf_count_hard, bool hide_help_)
232 : ctx_opts(symbol_, ctx_type_, hide_help_)
233 {
234 u.perf.type = PERF_TYPE_HARDWARE;
235 u.perf.config = perf_count_hard;
236 }
237
238 ctx_opts(const char *symbol_, context_type ctx_type_, perf_count_soft perf_count_soft, bool hide_help_)
239 : ctx_opts(symbol_, ctx_type_, hide_help_)
240 {
241 u.perf.type = PERF_TYPE_SOFTWARE;
242 u.perf.config = perf_count_soft;
243 }
244
245 ctx_opts(const char *symbol_, context_type ctx_type_,
246 perf_hw_cache_id perf_hw_cache_id,
247 perf_hw_cache_op_id perf_hw_cache_op_id,
248 perf_hw_cache_op_result_id perf_hw_cache_op_result_id,
249 bool hide_help_)
250 : ctx_opts(symbol_, ctx_type_, hide_help_)
251 {
252 u.perf.type = PERF_TYPE_HW_CACHE;
253 u.perf.config = perf_hw_cache_id | perf_hw_cache_op_id << 8 | 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,
319 CONTEXT_PERF_CPU_COUNTER, 0),
320 PERF_HW("perf:cpu:cycles", CPU_CYCLES,
321 CONTEXT_PERF_CPU_COUNTER, 0),
322 PERF_HW("perf:cpu:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND,
323 CONTEXT_PERF_CPU_COUNTER, 0),
324 PERF_HW("perf:cpu:idle-cycles-frontend", STALLED_CYCLES_FRONTEND,
325 CONTEXT_PERF_CPU_COUNTER, 0),
326 PERF_HW("perf:cpu:stalled-cycles-backend", STALLED_CYCLES_BACKEND,
327 CONTEXT_PERF_CPU_COUNTER, 0),
328 PERF_HW("perf:cpu:idle-cycles-backend", STALLED_CYCLES_BACKEND,
329 CONTEXT_PERF_CPU_COUNTER, 0),
330 PERF_HW("perf:cpu:instructions", INSTRUCTIONS,
331 CONTEXT_PERF_CPU_COUNTER, 0),
332 PERF_HW("perf:cpu:cache-references", CACHE_REFERENCES,
333 CONTEXT_PERF_CPU_COUNTER, 0),
334 PERF_HW("perf:cpu:cache-misses", CACHE_MISSES,
335 CONTEXT_PERF_CPU_COUNTER, 0),
336 PERF_HW("perf:cpu:branch-instructions", BRANCH_INSTRUCTIONS,
337 CONTEXT_PERF_CPU_COUNTER, 0),
338 PERF_HW("perf:cpu:branches", BRANCH_INSTRUCTIONS,
339 CONTEXT_PERF_CPU_COUNTER, 0),
340 PERF_HW("perf:cpu:branch-misses", BRANCH_MISSES,
341 CONTEXT_PERF_CPU_COUNTER, 0),
342 PERF_HW("perf:cpu:bus-cycles", BUS_CYCLES,
343 CONTEXT_PERF_CPU_COUNTER, 0),
344
345 PERF_HW_CACHE("perf:cpu:L1-dcache", L1D,
346 CONTEXT_PERF_CPU_COUNTER, 0),
347 PERF_HW_CACHE("perf:cpu:L1-icache", L1I,
348 CONTEXT_PERF_CPU_COUNTER, 0),
349 PERF_HW_CACHE("perf:cpu:LLC", LL,
350 CONTEXT_PERF_CPU_COUNTER, 0),
351 PERF_HW_CACHE("perf:cpu:dTLB", DTLB,
352 CONTEXT_PERF_CPU_COUNTER, 0),
353 _PERF_HW_CACHE("perf:cpu:iTLB-loads", ITLB,
354 CONTEXT_PERF_CPU_COUNTER, READ, ACCESS, 0),
355 _PERF_HW_CACHE("perf:cpu:iTLB-load-misses", ITLB,
356 CONTEXT_PERF_CPU_COUNTER, READ, MISS, 0),
357 _PERF_HW_CACHE("perf:cpu:branch-loads", BPU,
358 CONTEXT_PERF_CPU_COUNTER, READ, ACCESS, 0),
359 _PERF_HW_CACHE("perf:cpu:branch-load-misses", BPU,
360 CONTEXT_PERF_CPU_COUNTER, READ, MISS, 0),
361
362 PERF_SW("perf:cpu:cpu-clock", CPU_CLOCK,
363 CONTEXT_PERF_CPU_COUNTER, 0),
364 PERF_SW("perf:cpu:task-clock", TASK_CLOCK,
365 CONTEXT_PERF_CPU_COUNTER, 0),
366 PERF_SW("perf:cpu:page-fault", PAGE_FAULTS,
367 CONTEXT_PERF_CPU_COUNTER, 0),
368 PERF_SW("perf:cpu:faults", PAGE_FAULTS,
369 CONTEXT_PERF_CPU_COUNTER, 0),
370 PERF_SW("perf:cpu:major-faults", PAGE_FAULTS_MAJ,
371 CONTEXT_PERF_CPU_COUNTER, 0),
372 PERF_SW("perf:cpu:minor-faults", PAGE_FAULTS_MIN,
373 CONTEXT_PERF_CPU_COUNTER, 0),
374 PERF_SW("perf:cpu:context-switches", CONTEXT_SWITCHES,
375 CONTEXT_PERF_CPU_COUNTER, 0),
376 PERF_SW("perf:cpu:cs", CONTEXT_SWITCHES,
377 CONTEXT_PERF_CPU_COUNTER, 0),
378 PERF_SW("perf:cpu:cpu-migrations", CPU_MIGRATIONS,
379 CONTEXT_PERF_CPU_COUNTER, 0),
380 PERF_SW("perf:cpu:migrations", CPU_MIGRATIONS,
381 CONTEXT_PERF_CPU_COUNTER, 0),
382 PERF_SW("perf:cpu:alignment-faults", ALIGNMENT_FAULTS,
383 CONTEXT_PERF_CPU_COUNTER, 0),
384 PERF_SW("perf:cpu:emulation-faults", EMULATION_FAULTS,
385 CONTEXT_PERF_CPU_COUNTER, 0),
386
387 /* Perf per-thread counters */
388 PERF_HW("perf:thread:cpu-cycles", CPU_CYCLES,
389 CONTEXT_PERF_THREAD_COUNTER, 0),
390 PERF_HW("perf:thread:cycles", CPU_CYCLES,
391 CONTEXT_PERF_THREAD_COUNTER, 0),
392 PERF_HW("perf:thread:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND,
393 CONTEXT_PERF_THREAD_COUNTER, 0),
394 PERF_HW("perf:thread:idle-cycles-frontend", STALLED_CYCLES_FRONTEND,
395 CONTEXT_PERF_THREAD_COUNTER, 0),
396 PERF_HW("perf:thread:stalled-cycles-backend", STALLED_CYCLES_BACKEND,
397 CONTEXT_PERF_THREAD_COUNTER, 0),
398 PERF_HW("perf:thread:idle-cycles-backend", STALLED_CYCLES_BACKEND,
399 CONTEXT_PERF_THREAD_COUNTER, 0),
400 PERF_HW("perf:thread:instructions", INSTRUCTIONS,
401 CONTEXT_PERF_THREAD_COUNTER, 0),
402 PERF_HW("perf:thread:cache-references", CACHE_REFERENCES,
403 CONTEXT_PERF_THREAD_COUNTER, 0),
404 PERF_HW("perf:thread:cache-misses", CACHE_MISSES,
405 CONTEXT_PERF_THREAD_COUNTER, 0),
406 PERF_HW("perf:thread:branch-instructions", BRANCH_INSTRUCTIONS,
407 CONTEXT_PERF_THREAD_COUNTER, 0),
408 PERF_HW("perf:thread:branches", BRANCH_INSTRUCTIONS,
409 CONTEXT_PERF_THREAD_COUNTER, 0),
410 PERF_HW("perf:thread:branch-misses", BRANCH_MISSES,
411 CONTEXT_PERF_THREAD_COUNTER, 0),
412 PERF_HW("perf:thread:bus-cycles", BUS_CYCLES,
413 CONTEXT_PERF_THREAD_COUNTER, 0),
414
415 PERF_HW_CACHE("perf:thread:L1-dcache", L1D,
416 CONTEXT_PERF_THREAD_COUNTER, 0),
417 PERF_HW_CACHE("perf:thread:L1-icache", L1I,
418 CONTEXT_PERF_THREAD_COUNTER, 0),
419 PERF_HW_CACHE("perf:thread:LLC", LL,
420 CONTEXT_PERF_THREAD_COUNTER, 0),
421 PERF_HW_CACHE("perf:thread:dTLB", DTLB,
422 CONTEXT_PERF_THREAD_COUNTER, 0),
423 _PERF_HW_CACHE("perf:thread:iTLB-loads", ITLB,
424 CONTEXT_PERF_THREAD_COUNTER, READ, ACCESS, 0),
425 _PERF_HW_CACHE("perf:thread:iTLB-load-misses", ITLB,
426 CONTEXT_PERF_THREAD_COUNTER, READ, MISS, 0),
427 _PERF_HW_CACHE("perf:thread:branch-loads", BPU,
428 CONTEXT_PERF_THREAD_COUNTER, READ, ACCESS, 0),
429 _PERF_HW_CACHE("perf:thread:branch-load-misses", BPU,
430 CONTEXT_PERF_THREAD_COUNTER, READ, MISS, 0),
431
432 PERF_SW("perf:thread:cpu-clock", CPU_CLOCK,
433 CONTEXT_PERF_THREAD_COUNTER, 0),
434 PERF_SW("perf:thread:task-clock", TASK_CLOCK,
435 CONTEXT_PERF_THREAD_COUNTER, 0),
436 PERF_SW("perf:thread:page-fault", PAGE_FAULTS,
437 CONTEXT_PERF_THREAD_COUNTER, 0),
438 PERF_SW("perf:thread:faults", PAGE_FAULTS,
439 CONTEXT_PERF_THREAD_COUNTER, 0),
440 PERF_SW("perf:thread:major-faults", PAGE_FAULTS_MAJ,
441 CONTEXT_PERF_THREAD_COUNTER, 0),
442 PERF_SW("perf:thread:minor-faults", PAGE_FAULTS_MIN,
443 CONTEXT_PERF_THREAD_COUNTER, 0),
444 PERF_SW("perf:thread:context-switches", CONTEXT_SWITCHES,
445 CONTEXT_PERF_THREAD_COUNTER, 0),
446 PERF_SW("perf:thread:cs", CONTEXT_SWITCHES,
447 CONTEXT_PERF_THREAD_COUNTER, 0),
448 PERF_SW("perf:thread:cpu-migrations", CPU_MIGRATIONS,
449 CONTEXT_PERF_THREAD_COUNTER, 0),
450 PERF_SW("perf:thread:migrations", CPU_MIGRATIONS,
451 CONTEXT_PERF_THREAD_COUNTER, 0),
452 PERF_SW("perf:thread:alignment-faults", ALIGNMENT_FAULTS,
453 CONTEXT_PERF_THREAD_COUNTER, 0),
454 PERF_SW("perf:thread:emulation-faults", EMULATION_FAULTS,
455 CONTEXT_PERF_THREAD_COUNTER, 0),
456
457 /*
458 * Perf per-CPU counters, backward compatibilty for names.
459 * Hidden from help listing.
460 */
461 PERF_HW("perf:cpu-cycles", CPU_CYCLES,
462 CONTEXT_PERF_COUNTER, 1),
463 PERF_HW("perf:cycles", CPU_CYCLES,
464 CONTEXT_PERF_COUNTER, 1),
465 PERF_HW("perf:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND,
466 CONTEXT_PERF_COUNTER, 1),
467 PERF_HW("perf:idle-cycles-frontend", STALLED_CYCLES_FRONTEND,
468 CONTEXT_PERF_COUNTER, 1),
469 PERF_HW("perf:stalled-cycles-backend", STALLED_CYCLES_BACKEND,
470 CONTEXT_PERF_COUNTER, 1),
471 PERF_HW("perf:idle-cycles-backend", STALLED_CYCLES_BACKEND,
472 CONTEXT_PERF_COUNTER, 1),
473 PERF_HW("perf:instructions", INSTRUCTIONS,
474 CONTEXT_PERF_COUNTER, 1),
475 PERF_HW("perf:cache-references", CACHE_REFERENCES,
476 CONTEXT_PERF_COUNTER, 1),
477 PERF_HW("perf:cache-misses", CACHE_MISSES,
478 CONTEXT_PERF_COUNTER, 1),
479 PERF_HW("perf:branch-instructions", BRANCH_INSTRUCTIONS,
480 CONTEXT_PERF_COUNTER, 1),
481 PERF_HW("perf:branches", BRANCH_INSTRUCTIONS,
482 CONTEXT_PERF_COUNTER, 1),
483 PERF_HW("perf:branch-misses", BRANCH_MISSES,
484 CONTEXT_PERF_COUNTER, 1),
485 PERF_HW("perf:bus-cycles", BUS_CYCLES,
486 CONTEXT_PERF_COUNTER, 1),
487
488 PERF_HW_CACHE("perf:L1-dcache", L1D,
489 CONTEXT_PERF_COUNTER, 1),
490 PERF_HW_CACHE("perf:L1-icache", L1I,
491 CONTEXT_PERF_COUNTER, 1),
492 PERF_HW_CACHE("perf:LLC", LL,
493 CONTEXT_PERF_COUNTER, 1),
494 PERF_HW_CACHE("perf:dTLB", DTLB,
495 CONTEXT_PERF_COUNTER, 1),
496 _PERF_HW_CACHE("perf:iTLB-loads", ITLB,
497 CONTEXT_PERF_COUNTER, READ, ACCESS, 1),
498 _PERF_HW_CACHE("perf:iTLB-load-misses", ITLB,
499 CONTEXT_PERF_COUNTER, READ, MISS, 1),
500 _PERF_HW_CACHE("perf:branch-loads", BPU,
501 CONTEXT_PERF_COUNTER, READ, ACCESS, 1),
502 _PERF_HW_CACHE("perf:branch-load-misses", BPU,
503 CONTEXT_PERF_COUNTER, READ, MISS, 1),
504
505 PERF_SW("perf:cpu-clock", CPU_CLOCK,
506 CONTEXT_PERF_COUNTER, 1),
507 PERF_SW("perf:task-clock", TASK_CLOCK,
508 CONTEXT_PERF_COUNTER, 1),
509 PERF_SW("perf:page-fault", PAGE_FAULTS,
510 CONTEXT_PERF_COUNTER, 1),
511 PERF_SW("perf:faults", PAGE_FAULTS,
512 CONTEXT_PERF_COUNTER, 1),
513 PERF_SW("perf:major-faults", PAGE_FAULTS_MAJ,
514 CONTEXT_PERF_COUNTER, 1),
515 PERF_SW("perf:minor-faults", PAGE_FAULTS_MIN,
516 CONTEXT_PERF_COUNTER, 1),
517 PERF_SW("perf:context-switches", CONTEXT_SWITCHES,
518 CONTEXT_PERF_COUNTER, 1),
519 PERF_SW("perf:cs", CONTEXT_SWITCHES,
520 CONTEXT_PERF_COUNTER, 1),
521 PERF_SW("perf:cpu-migrations", CPU_MIGRATIONS,
522 CONTEXT_PERF_COUNTER, 1),
523 PERF_SW("perf:migrations", CPU_MIGRATIONS,
524 CONTEXT_PERF_COUNTER, 1),
525 PERF_SW("perf:alignment-faults", ALIGNMENT_FAULTS,
526 CONTEXT_PERF_COUNTER, 1),
527 PERF_SW("perf:emulation-faults", EMULATION_FAULTS,
528 CONTEXT_PERF_COUNTER, 1),
529
530 {}, /* Closure */
531 };
532
533 #undef PERF_HW_CACHE
534 #undef _PERF_HW_CACHE
535 #undef PERF_SW
536 #undef PERF_HW
537
538 /*
539 * Context type for command line option parsing.
540 */
541 struct ctx_type {
542 struct ctx_opts *opt;
543 struct cds_list_head list;
544 };
545
546 /*
547 * List of context type. Use to enable multiple context on a single command
548 * line entry.
549 */
550 struct ctx_type_list {
551 struct cds_list_head head;
552 } ctx_type_list = {
553 .head = CDS_LIST_HEAD_INIT(ctx_type_list.head),
554 };
555
556
557
558 /*
559 * Find context numerical value from string.
560 *
561 * Return -1 if not found.
562 */
563 static int find_ctx_type_idx(const char *opt)
564 {
565 int ret, i = 0;
566
567 while (ctx_opts[i].symbol != NULL) {
568 if (strcmp(opt, ctx_opts[i].symbol) == 0) {
569 ret = i;
570 goto end;
571 }
572 i++;
573 }
574
575 ret = -1;
576 end:
577 return ret;
578 }
579
580 static
581 enum lttng_domain_type get_domain(void)
582 {
583 if (opt_kernel) {
584 return LTTNG_DOMAIN_KERNEL;
585 } else if (opt_userspace) {
586 return LTTNG_DOMAIN_UST;
587 } else if (opt_jul) {
588 return LTTNG_DOMAIN_JUL;
589 } else if (opt_log4j) {
590 return LTTNG_DOMAIN_LOG4J;
591 } else {
592 abort();
593 }
594 }
595
596 static
597 int mi_open(void)
598 {
599 int ret;
600
601 /* MI check */
602 if (!lttng_opt_mi) {
603 ret = 0;
604 goto end;
605 }
606
607 ret = fileno(stdout);
608 if (ret < 0) {
609 PERROR("Unable to retrieve fileno of stdout");
610 ret = CMD_ERROR;
611 goto end;
612 }
613
614 writer = mi_lttng_writer_create(ret, lttng_opt_mi);
615 if (!writer) {
616 ret = CMD_ERROR;
617 goto end;
618 }
619
620 /* Open command element */
621 ret = mi_lttng_writer_command_open(writer,
622 mi_lttng_element_command_add_context);
623 if (ret) {
624 ret = CMD_ERROR;
625 goto end;
626 }
627
628 /* Open output element */
629 ret = mi_lttng_writer_open_element(writer,
630 mi_lttng_element_command_output);
631 if (ret) {
632 ret = CMD_ERROR;
633 goto end;
634 }
635 end:
636 return ret;
637 }
638
639 static
640 int mi_close(enum cmd_error_code success)
641 {
642 int ret;
643
644 /* MI closing */
645 if (!lttng_opt_mi) {
646 ret = 0;
647 goto end;
648 }
649 /* Close output element */
650 ret = mi_lttng_writer_close_element(writer);
651 if (ret) {
652 ret = CMD_ERROR;
653 goto end;
654 }
655
656 /* Success ? */
657 ret = mi_lttng_writer_write_element_bool(writer,
658 mi_lttng_element_command_success, !success);
659 if (ret) {
660 ret = CMD_ERROR;
661 goto end;
662 }
663
664 /* Command element close */
665 ret = mi_lttng_writer_command_close(writer);
666 if (ret) {
667 ret = CMD_ERROR;
668 goto end;
669 }
670 end:
671 return ret;
672 }
673
674 static
675 void populate_context(struct lttng_event_context *context,
676 const struct ctx_opts *opt)
677 {
678 char *ptr;
679
680 context->ctx = (enum lttng_event_context_type) opt->ctx_type;
681 switch (context->ctx) {
682 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
683 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
684 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
685 context->u.perf_counter.type = opt->u.perf.type;
686 context->u.perf_counter.config = opt->u.perf.config;
687 strncpy(context->u.perf_counter.name, opt->symbol,
688 LTTNG_SYMBOL_NAME_LEN);
689 context->u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
690 /* Replace : and - by _ */
691 while ((ptr = strchr(context->u.perf_counter.name, '-')) != NULL) {
692 *ptr = '_';
693 }
694 while ((ptr = strchr(context->u.perf_counter.name, ':')) != NULL) {
695 *ptr = '_';
696 }
697 break;
698 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
699 context->u.app_ctx.provider_name =
700 opt->u.app_ctx.provider_name;
701 context->u.app_ctx.ctx_name =
702 opt->u.app_ctx.ctx_name;
703 break;
704 default:
705 break;
706 }
707 }
708
709 /*
710 * Pretty print context type.
711 */
712 static
713 int print_ctx_type(void)
714 {
715
716 FILE *ofp = stdout;
717 int i = 0;
718 int ret;
719 struct lttng_event_context context;
720
721 memset(&context, 0, sizeof(context));
722
723 ret = mi_open();
724 if (ret) {
725 ret = CMD_ERROR;
726 goto end;
727 }
728
729 if (lttng_opt_mi) {
730 /* Open a contexts element */
731 ret = mi_lttng_writer_open_element(writer, config_element_contexts);
732 if (ret) {
733 ret = CMD_ERROR;
734 goto end;
735 }
736 }
737
738 while (ctx_opts[i].symbol != NULL) {
739 if (!ctx_opts[i].hide_help) {
740 if (lttng_opt_mi) {
741 populate_context(&context, &ctx_opts[i]);
742 ret = mi_lttng_context(writer, &context, 1);
743 if (ret) {
744 ret = CMD_ERROR;
745 goto end;
746 }
747
748 ret = mi_lttng_writer_write_element_string(
749 writer,
750 mi_lttng_element_context_symbol,
751 ctx_opts[i].symbol);
752 if (ret) {
753 ret = CMD_ERROR;
754 goto end;
755 }
756
757 ret = mi_lttng_writer_close_element(writer);
758 if (ret) {
759 ret = CMD_ERROR;
760 goto end;
761 }
762 } else {
763 fprintf(ofp, "%s\n", ctx_opts[i].symbol);
764 }
765 }
766 i++;
767 }
768
769 if (lttng_opt_mi) {
770 /* Close contexts element */
771 ret = mi_lttng_writer_close_element(writer);
772 if (ret) {
773 goto end;
774 }
775 }
776
777 end:
778 ret = mi_close((cmd_error_code) ret);
779 if (ret) {
780 ret = CMD_ERROR;
781 }
782 return ret;
783 }
784
785 /*
786 * Add context to channel or event.
787 */
788 static int add_context(char *session_name)
789 {
790 int ret = CMD_SUCCESS, warn = 0, success = 0;
791 struct lttng_event_context context;
792 struct lttng_domain dom;
793 struct ctx_type *type;
794
795 memset(&context, 0, sizeof(context));
796 memset(&dom, 0, sizeof(dom));
797
798 dom.type = get_domain();
799 handle = lttng_create_handle(session_name, &dom);
800 if (handle == NULL) {
801 ret = CMD_ERROR;
802 goto error;
803 }
804
805 if (lttng_opt_mi) {
806 /* Open a contexts element */
807 ret = mi_lttng_writer_open_element(writer, config_element_contexts);
808 if (ret) {
809 goto error;
810 }
811 }
812
813 /* Iterate over all the context types given */
814 cds_list_for_each_entry(type, &ctx_type_list.head, list) {
815 DBG("Adding context...");
816
817 populate_context(&context, type->opt);
818
819 if (lttng_opt_mi) {
820 /* We leave context open the update the success of the command */
821 ret = mi_lttng_context(writer, &context, 1);
822 if (ret) {
823 ret = CMD_ERROR;
824 goto error;
825 }
826
827 ret = mi_lttng_writer_write_element_string(writer,
828 mi_lttng_element_context_symbol,
829 type->opt->symbol);
830 if (ret) {
831 ret = CMD_ERROR;
832 goto error;
833 }
834 }
835
836 ret = lttng_add_context(handle, &context, NULL, opt_channel_name);
837 if (ret < 0) {
838 ERR("%s: %s", type->opt->symbol, lttng_strerror(ret));
839 warn = 1;
840 success = 0;
841 } else {
842 if (opt_channel_name) {
843 MSG("%s context %s added to channel %s",
844 lttng_domain_type_str(dom.type),
845 type->opt->symbol,
846 opt_channel_name);
847 } else {
848 MSG("%s context %s added to all channels",
849 lttng_domain_type_str(dom.type),
850 type->opt->symbol);
851 }
852 success = 1;
853 }
854
855 if (lttng_opt_mi) {
856 /* Is the single operation a success ? */
857 ret = mi_lttng_writer_write_element_bool(writer,
858 mi_lttng_element_success, success);
859 if (ret) {
860 ret = CMD_ERROR;
861 goto error;
862 }
863
864 /* Close the context element */
865 ret = mi_lttng_writer_close_element(writer);
866 if (ret) {
867 ret = CMD_ERROR;
868 goto error;
869 }
870 }
871 }
872
873 if (lttng_opt_mi) {
874 /* Close contexts element */
875 ret = mi_lttng_writer_close_element(writer);
876 if (ret) {
877 goto error;
878 }
879 }
880
881 ret = CMD_SUCCESS;
882
883 error:
884 lttng_destroy_handle(handle);
885
886 /*
887 * This means that at least one add_context failed and tells the user to
888 * look on stderr for error(s).
889 */
890 if (!ret && warn) {
891 ret = CMD_WARNING;
892 }
893 return ret;
894 }
895
896 static
897 void destroy_ctx_type(struct ctx_type *type)
898 {
899 if (!type) {
900 return;
901 }
902 if (type->opt) {
903 free(type->opt->symbol);
904 }
905 free(type->opt);
906 free(type);
907 }
908
909 static
910 struct ctx_type *create_ctx_type(void)
911 {
912 struct ctx_type *type = (ctx_type *) zmalloc(sizeof(*type));
913
914 if (!type) {
915 PERROR("malloc ctx_type");
916 goto end;
917 }
918
919 type->opt = (struct ctx_opts *) zmalloc(sizeof(*type->opt));
920 if (!type->opt) {
921 PERROR("malloc ctx_type options");
922 destroy_ctx_type(type);
923 type = NULL;
924 goto end;
925 }
926 end:
927 return type;
928 }
929
930 static
931 int find_ctx_type_perf_raw(const char *ctx, struct ctx_type *type)
932 {
933 int ret;
934 int field_pos = 0;
935 char *tmp_list, *cur_list;
936
937 cur_list = tmp_list = strdup(ctx);
938 if (!tmp_list) {
939 PERROR("strdup temp list");
940 ret = -ENOMEM;
941 goto end;
942 }
943
944 /* Looking for "perf:[cpu|thread]:raw:<mask>:<name>". */
945 for (;;) {
946 char *next;
947
948 next = strtok(cur_list, ":");
949 if (!next) {
950 break;
951 }
952 cur_list = NULL;
953 switch (field_pos) {
954 case 0:
955 if (strncmp(next, "perf", 4) != 0) {
956 ret = -1;
957 goto end;
958 }
959 break;
960 case 1:
961 if (strncmp(next, "cpu", 3) == 0) {
962 type->opt->ctx_type = CONTEXT_PERF_CPU_COUNTER;
963 } else if (strncmp(next, "thread", 4) == 0) {
964 type->opt->ctx_type = CONTEXT_PERF_THREAD_COUNTER;
965 } else {
966 ret = -1;
967 goto end;
968 }
969 break;
970 case 2:
971 if (strncmp(next, "raw", 3) != 0) {
972 ret = -1;
973 goto end;
974 }
975 break;
976 case 3:
977 {
978 char *endptr;
979
980 if (strlen(next) < 2 || next[0] != 'r') {
981 ERR("Wrong perf raw mask format: expected rNNN");
982 ret = -1;
983 goto end;
984 }
985 errno = 0;
986 type->opt->u.perf.config = strtoll(next + 1, &endptr, 16);
987 if (errno != 0 || !endptr || *endptr) {
988 ERR("Wrong perf raw mask format: expected rNNN");
989 ret = -1;
990 goto end;
991 }
992 break;
993 }
994 case 4:
995 /* name */
996 break;
997 case 5:
998 ERR("Too many ':' in perf raw format");
999 ret = -1;
1000 goto end;
1001 };
1002 field_pos++;
1003 }
1004
1005 if (field_pos < 5) {
1006 ERR("Invalid perf counter specifier, expected a specifier of "
1007 "the form perf:cpu:raw:rNNN:<name> or "
1008 "perf:thread:raw:rNNN:<name>");
1009 ret = -1;
1010 goto end;
1011 }
1012
1013 ret = 0;
1014 goto end;
1015
1016 end:
1017 free(tmp_list);
1018 return ret;
1019 }
1020
1021 static
1022 struct ctx_type *get_context_type(const char *ctx)
1023 {
1024 int opt_index, ret;
1025 struct ctx_type *type = NULL;
1026 const char app_ctx_prefix[] = "$app.";
1027 char *provider_name = NULL, *ctx_name = NULL;
1028 size_t i, len, colon_pos = 0, provider_name_len, ctx_name_len;
1029
1030 if (!ctx) {
1031 goto not_found;
1032 }
1033
1034 type = create_ctx_type();
1035 if (!type) {
1036 goto not_found;
1037 }
1038
1039 /* Check if ctx matches a known static context. */
1040 opt_index = find_ctx_type_idx(ctx);
1041 if (opt_index >= 0) {
1042 *type->opt = ctx_opts[opt_index];
1043 type->opt->symbol = strdup(ctx_opts[opt_index].symbol);
1044 goto found;
1045 }
1046
1047 /* Check if ctx is a raw perf context. */
1048 ret = find_ctx_type_perf_raw(ctx, type);
1049 if (ret == 0) {
1050 type->opt->u.perf.type = PERF_TYPE_RAW;
1051 type->opt->symbol = strdup(ctx);
1052 if (!type->opt->symbol) {
1053 PERROR("Copy perf field name");
1054 goto not_found;
1055 }
1056 goto found;
1057 }
1058
1059 /*
1060 * No match found against static contexts; check if it is an app
1061 * context.
1062 */
1063 len = strlen(ctx);
1064 if (len <= sizeof(app_ctx_prefix) - 1) {
1065 goto not_found;
1066 }
1067
1068 /* String starts with $app. */
1069 if (strncmp(ctx, app_ctx_prefix, sizeof(app_ctx_prefix) - 1)) {
1070 goto not_found;
1071 }
1072
1073 /* Validate that the ':' separator is present. */
1074 for (i = sizeof(app_ctx_prefix); i < len; i++) {
1075 const char c = ctx[i];
1076
1077 if (c == ':') {
1078 colon_pos = i;
1079 break;
1080 }
1081 }
1082
1083 /*
1084 * No colon found or no ctx name ("$app.provider:") or no provider name
1085 * given ("$app.:..."), which is invalid.
1086 */
1087 if (!colon_pos || colon_pos == len ||
1088 colon_pos == sizeof(app_ctx_prefix)) {
1089 ERR("Invalid application context provided: no provider or context name provided.");
1090 goto not_found;
1091 }
1092
1093 provider_name_len = colon_pos - sizeof(app_ctx_prefix) + 2;
1094 provider_name = (char *) zmalloc(provider_name_len);
1095 if (!provider_name) {
1096 PERROR("malloc provider_name");
1097 goto not_found;
1098 }
1099 strncpy(provider_name, ctx + sizeof(app_ctx_prefix) - 1,
1100 provider_name_len - 1);
1101 type->opt->u.app_ctx.provider_name = provider_name;
1102
1103 ctx_name_len = len - colon_pos;
1104 ctx_name = (char *) zmalloc(ctx_name_len);
1105 if (!ctx_name) {
1106 PERROR("malloc ctx_name");
1107 goto not_found;
1108 }
1109 strncpy(ctx_name, ctx + colon_pos + 1, ctx_name_len - 1);
1110 type->opt->u.app_ctx.ctx_name = ctx_name;
1111 type->opt->ctx_type = CONTEXT_APP_CONTEXT;
1112 type->opt->symbol = strdup(ctx);
1113 found:
1114 return type;
1115 not_found:
1116 free(provider_name);
1117 free(ctx_name);
1118 destroy_ctx_type(type);
1119 return NULL;
1120 }
1121
1122 /*
1123 * Add context to channel or event.
1124 */
1125 int cmd_add_context(int argc, const char **argv)
1126 {
1127 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
1128 static poptContext pc;
1129 struct ctx_type *type, *tmptype;
1130 char *session_name = NULL;
1131 const char *leftover = NULL;
1132
1133 if (argc < 2) {
1134 ret = CMD_ERROR;
1135 goto end;
1136 }
1137
1138 pc = poptGetContext(NULL, argc, argv, long_options, 0);
1139 poptReadDefaultConfig(pc, 0);
1140
1141 while ((opt = poptGetNextOpt(pc)) != -1) {
1142 switch (opt) {
1143 case OPT_HELP:
1144 SHOW_HELP();
1145 goto end;
1146 case OPT_LIST:
1147 ret = print_ctx_type();
1148 goto end;
1149 case OPT_TYPE:
1150 {
1151 type = get_context_type(opt_type);
1152 if (!type) {
1153 ERR("Unknown context type %s", opt_type);
1154 ret = CMD_FATAL;
1155 goto end;
1156 }
1157 cds_list_add_tail(&type->list, &ctx_type_list.head);
1158 break;
1159 }
1160 case OPT_USERSPACE:
1161 opt_userspace = 1;
1162 break;
1163 case OPT_JUL:
1164 opt_jul = 1;
1165 break;
1166 case OPT_LOG4J:
1167 opt_log4j = 1;
1168 break;
1169 case OPT_LIST_OPTIONS:
1170 list_cmd_options(stdout, long_options);
1171 goto end;
1172 default:
1173 ret = CMD_UNDEFINED;
1174 goto end;
1175 }
1176 }
1177
1178 leftover = poptGetArg(pc);
1179 if (leftover) {
1180 ERR("Unknown argument: %s", leftover);
1181 ret = CMD_ERROR;
1182 goto end;
1183 }
1184
1185 ret = print_missing_or_multiple_domains(
1186 opt_kernel + opt_userspace + opt_jul + opt_log4j, true);
1187 if (ret) {
1188 ret = CMD_ERROR;
1189 goto end;
1190 }
1191
1192 if (!opt_type) {
1193 ERR("Missing mandatory -t TYPE");
1194 ret = CMD_ERROR;
1195 goto end;
1196 }
1197
1198 if (!opt_session_name) {
1199 session_name = get_session_name();
1200 if (session_name == NULL) {
1201 ret = CMD_ERROR;
1202 goto end;
1203 }
1204 } else {
1205 session_name = opt_session_name;
1206 }
1207
1208 ret = mi_open();
1209 if (ret) {
1210 goto end;
1211 }
1212
1213 command_ret = add_context(session_name);
1214 ret = mi_close((cmd_error_code) command_ret);
1215 if (ret) {
1216 goto end;
1217 }
1218
1219 end:
1220 if (!opt_session_name) {
1221 free(session_name);
1222 }
1223
1224 /* Mi clean-up */
1225 if (writer && mi_lttng_writer_destroy(writer)) {
1226 /* Preserve original error code */
1227 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
1228 }
1229
1230 /* Cleanup allocated memory */
1231 cds_list_for_each_entry_safe(type, tmptype, &ctx_type_list.head, list) {
1232 destroy_ctx_type(type);
1233 }
1234
1235 /* Overwrite ret if an error occurred during add_context() */
1236 ret = command_ret ? command_ret : ret;
1237
1238 poptFreeContext(pc);
1239 return ret;
1240 }
This page took 0.055582 seconds and 4 git commands to generate.