2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/types.h>
30 #include <urcu/list.h>
32 #include <common/mi-lttng.h>
34 #include "../command.h"
36 static char *opt_channel_name
;
37 static char *opt_session_name
;
38 static int opt_kernel
;
39 static int opt_userspace
;
42 static char *opt_type
;
44 #ifdef LTTNG_EMBED_HELP
45 static const char help_msg
[] =
46 #include <lttng-add-context.1.h>
60 static struct lttng_handle
*handle
;
61 static struct mi_writer
*writer
;
64 * Taken from the LTTng ABI
68 CONTEXT_PERF_COUNTER
= 1, /* Backward compat. */
77 CONTEXT_PTHREAD_ID
= 10,
78 CONTEXT_HOSTNAME
= 11,
80 CONTEXT_PERF_CPU_COUNTER
= 13,
81 CONTEXT_PERF_THREAD_COUNTER
= 14,
82 CONTEXT_APP_CONTEXT
= 15,
83 CONTEXT_INTERRUPTIBLE
= 16,
84 CONTEXT_PREEMPTIBLE
= 17,
85 CONTEXT_NEED_RESCHEDULE
= 18,
86 CONTEXT_MIGRATABLE
= 19,
87 CONTEXT_CALLSTACK_KERNEL
= 20,
88 CONTEXT_CALLSTACK_USER
= 21,
89 CONTEXT_CGROUP_NS
= 22,
99 * Taken from the Perf ABI (all enum perf_*)
102 PERF_TYPE_HARDWARE
= 0,
103 PERF_TYPE_SOFTWARE
= 1,
104 PERF_TYPE_HW_CACHE
= 3,
108 enum perf_count_hard
{
109 PERF_COUNT_HW_CPU_CYCLES
= 0,
110 PERF_COUNT_HW_INSTRUCTIONS
= 1,
111 PERF_COUNT_HW_CACHE_REFERENCES
= 2,
112 PERF_COUNT_HW_CACHE_MISSES
= 3,
113 PERF_COUNT_HW_BRANCH_INSTRUCTIONS
= 4,
114 PERF_COUNT_HW_BRANCH_MISSES
= 5,
115 PERF_COUNT_HW_BUS_CYCLES
= 6,
116 PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
= 7,
117 PERF_COUNT_HW_STALLED_CYCLES_BACKEND
= 8,
120 enum perf_count_soft
{
121 PERF_COUNT_SW_CPU_CLOCK
= 0,
122 PERF_COUNT_SW_TASK_CLOCK
= 1,
123 PERF_COUNT_SW_PAGE_FAULTS
= 2,
124 PERF_COUNT_SW_CONTEXT_SWITCHES
= 3,
125 PERF_COUNT_SW_CPU_MIGRATIONS
= 4,
126 PERF_COUNT_SW_PAGE_FAULTS_MIN
= 5,
127 PERF_COUNT_SW_PAGE_FAULTS_MAJ
= 6,
128 PERF_COUNT_SW_ALIGNMENT_FAULTS
= 7,
129 PERF_COUNT_SW_EMULATION_FAULTS
= 8,
133 * Generalized hardware cache events:
135 * { L1-D, L1-I, LLC, ITLB, DTLB, BPU } x
136 * { read, write, prefetch } x
137 * { accesses, misses }
139 enum perf_hw_cache_id
{
140 PERF_COUNT_HW_CACHE_L1D
= 0,
141 PERF_COUNT_HW_CACHE_L1I
= 1,
142 PERF_COUNT_HW_CACHE_LL
= 2,
143 PERF_COUNT_HW_CACHE_DTLB
= 3,
144 PERF_COUNT_HW_CACHE_ITLB
= 4,
145 PERF_COUNT_HW_CACHE_BPU
= 5,
147 PERF_COUNT_HW_CACHE_MAX
, /* non-ABI */
150 enum perf_hw_cache_op_id
{
151 PERF_COUNT_HW_CACHE_OP_READ
= 0,
152 PERF_COUNT_HW_CACHE_OP_WRITE
= 1,
153 PERF_COUNT_HW_CACHE_OP_PREFETCH
= 2,
155 PERF_COUNT_HW_CACHE_OP_MAX
, /* non-ABI */
158 enum perf_hw_cache_op_result_id
{
159 PERF_COUNT_HW_CACHE_RESULT_ACCESS
= 0,
160 PERF_COUNT_HW_CACHE_RESULT_MISS
= 1,
162 PERF_COUNT_HW_CACHE_RESULT_MAX
, /* non-ABI */
165 static struct poptOption long_options
[] = {
166 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
167 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
168 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
169 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
170 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
171 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
172 {"jul", 'j', POPT_ARG_NONE
, 0, OPT_JUL
, 0, 0},
173 {"log4j", 'l', POPT_ARG_NONE
, 0, OPT_LOG4J
, 0, 0},
174 {"type", 't', POPT_ARG_STRING
, &opt_type
, OPT_TYPE
, 0, 0},
175 {"list", 0, POPT_ARG_NONE
, NULL
, OPT_LIST
, NULL
, NULL
},
176 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
177 {0, 0, 0, 0, 0, 0, 0}
183 #define PERF_HW(optstr, name, type, hide) \
185 optstr, type, hide, \
186 .u.perf = { PERF_TYPE_HARDWARE, PERF_COUNT_HW_##name, },\
189 #define PERF_SW(optstr, name, type, hide) \
191 optstr, type, hide, \
192 .u.perf = { PERF_TYPE_SOFTWARE, PERF_COUNT_SW_##name, },\
195 #define _PERF_HW_CACHE(optstr, name, type, op, result, hide) \
197 optstr, type, hide, \
199 PERF_TYPE_HW_CACHE, \
200 (uint64_t) PERF_COUNT_HW_CACHE_##name \
201 | ((uint64_t) PERF_COUNT_HW_CACHE_OP_##op << 8) \
202 | ((uint64_t) PERF_COUNT_HW_CACHE_RESULT_##result << 16), \
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, \
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)
221 const struct ctx_opts
{
223 enum context_type ctx_type
;
224 int hide_help
; /* Hide from --help */
236 { "pid", CONTEXT_PID
},
237 { "procname", CONTEXT_PROCNAME
},
238 { "prio", CONTEXT_PRIO
},
239 { "nice", CONTEXT_NICE
},
240 { "vpid", CONTEXT_VPID
},
241 { "tid", CONTEXT_TID
},
242 { "pthread_id", CONTEXT_PTHREAD_ID
},
243 { "vtid", CONTEXT_VTID
},
244 { "ppid", CONTEXT_PPID
},
245 { "vppid", CONTEXT_VPPID
},
246 { "hostname", CONTEXT_HOSTNAME
},
247 { "ip", CONTEXT_IP
},
248 { "interruptible", CONTEXT_INTERRUPTIBLE
},
249 { "preemptible", CONTEXT_PREEMPTIBLE
},
250 { "need_reschedule", CONTEXT_NEED_RESCHEDULE
},
251 { "migratable", CONTEXT_MIGRATABLE
},
252 { "callstack-kernel", CONTEXT_CALLSTACK_KERNEL
},
253 #if HAVE_MODULES_USERSPACE_CALLSTACK_CONTEXT
254 { "callstack-user", CONTEXT_CALLSTACK_USER
},
256 { "cgroup_ns", CONTEXT_CGROUP_NS
},
257 { "ipc_ns", CONTEXT_IPC_NS
},
258 { "mnt_ns", CONTEXT_MNT_NS
},
259 { "net_ns", CONTEXT_NET_NS
},
260 { "pid_ns", CONTEXT_PID_NS
},
261 { "user_ns", CONTEXT_USER_NS
},
262 { "uts_ns", CONTEXT_UTS_NS
},
266 /* Perf per-CPU counters */
267 PERF_HW("perf:cpu:cpu-cycles", CPU_CYCLES
,
268 CONTEXT_PERF_CPU_COUNTER
, 0),
269 PERF_HW("perf:cpu:cycles", CPU_CYCLES
,
270 CONTEXT_PERF_CPU_COUNTER
, 0),
271 PERF_HW("perf:cpu:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND
,
272 CONTEXT_PERF_CPU_COUNTER
, 0),
273 PERF_HW("perf:cpu:idle-cycles-frontend", STALLED_CYCLES_FRONTEND
,
274 CONTEXT_PERF_CPU_COUNTER
, 0),
275 PERF_HW("perf:cpu:stalled-cycles-backend", STALLED_CYCLES_BACKEND
,
276 CONTEXT_PERF_CPU_COUNTER
, 0),
277 PERF_HW("perf:cpu:idle-cycles-backend", STALLED_CYCLES_BACKEND
,
278 CONTEXT_PERF_CPU_COUNTER
, 0),
279 PERF_HW("perf:cpu:instructions", INSTRUCTIONS
,
280 CONTEXT_PERF_CPU_COUNTER
, 0),
281 PERF_HW("perf:cpu:cache-references", CACHE_REFERENCES
,
282 CONTEXT_PERF_CPU_COUNTER
, 0),
283 PERF_HW("perf:cpu:cache-misses", CACHE_MISSES
,
284 CONTEXT_PERF_CPU_COUNTER
, 0),
285 PERF_HW("perf:cpu:branch-instructions", BRANCH_INSTRUCTIONS
,
286 CONTEXT_PERF_CPU_COUNTER
, 0),
287 PERF_HW("perf:cpu:branches", BRANCH_INSTRUCTIONS
,
288 CONTEXT_PERF_CPU_COUNTER
, 0),
289 PERF_HW("perf:cpu:branch-misses", BRANCH_MISSES
,
290 CONTEXT_PERF_CPU_COUNTER
, 0),
291 PERF_HW("perf:cpu:bus-cycles", BUS_CYCLES
,
292 CONTEXT_PERF_CPU_COUNTER
, 0),
294 PERF_HW_CACHE("perf:cpu:L1-dcache", L1D
,
295 CONTEXT_PERF_CPU_COUNTER
, 0),
296 PERF_HW_CACHE("perf:cpu:L1-icache", L1I
,
297 CONTEXT_PERF_CPU_COUNTER
, 0),
298 PERF_HW_CACHE("perf:cpu:LLC", LL
,
299 CONTEXT_PERF_CPU_COUNTER
, 0),
300 PERF_HW_CACHE("perf:cpu:dTLB", DTLB
,
301 CONTEXT_PERF_CPU_COUNTER
, 0),
302 _PERF_HW_CACHE("perf:cpu:iTLB-loads", ITLB
,
303 CONTEXT_PERF_CPU_COUNTER
, READ
, ACCESS
, 0),
304 _PERF_HW_CACHE("perf:cpu:iTLB-load-misses", ITLB
,
305 CONTEXT_PERF_CPU_COUNTER
, READ
, MISS
, 0),
306 _PERF_HW_CACHE("perf:cpu:branch-loads", BPU
,
307 CONTEXT_PERF_CPU_COUNTER
, READ
, ACCESS
, 0),
308 _PERF_HW_CACHE("perf:cpu:branch-load-misses", BPU
,
309 CONTEXT_PERF_CPU_COUNTER
, READ
, MISS
, 0),
311 PERF_SW("perf:cpu:cpu-clock", CPU_CLOCK
,
312 CONTEXT_PERF_CPU_COUNTER
, 0),
313 PERF_SW("perf:cpu:task-clock", TASK_CLOCK
,
314 CONTEXT_PERF_CPU_COUNTER
, 0),
315 PERF_SW("perf:cpu:page-fault", PAGE_FAULTS
,
316 CONTEXT_PERF_CPU_COUNTER
, 0),
317 PERF_SW("perf:cpu:faults", PAGE_FAULTS
,
318 CONTEXT_PERF_CPU_COUNTER
, 0),
319 PERF_SW("perf:cpu:major-faults", PAGE_FAULTS_MAJ
,
320 CONTEXT_PERF_CPU_COUNTER
, 0),
321 PERF_SW("perf:cpu:minor-faults", PAGE_FAULTS_MIN
,
322 CONTEXT_PERF_CPU_COUNTER
, 0),
323 PERF_SW("perf:cpu:context-switches", CONTEXT_SWITCHES
,
324 CONTEXT_PERF_CPU_COUNTER
, 0),
325 PERF_SW("perf:cpu:cs", CONTEXT_SWITCHES
,
326 CONTEXT_PERF_CPU_COUNTER
, 0),
327 PERF_SW("perf:cpu:cpu-migrations", CPU_MIGRATIONS
,
328 CONTEXT_PERF_CPU_COUNTER
, 0),
329 PERF_SW("perf:cpu:migrations", CPU_MIGRATIONS
,
330 CONTEXT_PERF_CPU_COUNTER
, 0),
331 PERF_SW("perf:cpu:alignment-faults", ALIGNMENT_FAULTS
,
332 CONTEXT_PERF_CPU_COUNTER
, 0),
333 PERF_SW("perf:cpu:emulation-faults", EMULATION_FAULTS
,
334 CONTEXT_PERF_CPU_COUNTER
, 0),
336 /* Perf per-thread counters */
337 PERF_HW("perf:thread:cpu-cycles", CPU_CYCLES
,
338 CONTEXT_PERF_THREAD_COUNTER
, 0),
339 PERF_HW("perf:thread:cycles", CPU_CYCLES
,
340 CONTEXT_PERF_THREAD_COUNTER
, 0),
341 PERF_HW("perf:thread:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND
,
342 CONTEXT_PERF_THREAD_COUNTER
, 0),
343 PERF_HW("perf:thread:idle-cycles-frontend", STALLED_CYCLES_FRONTEND
,
344 CONTEXT_PERF_THREAD_COUNTER
, 0),
345 PERF_HW("perf:thread:stalled-cycles-backend", STALLED_CYCLES_BACKEND
,
346 CONTEXT_PERF_THREAD_COUNTER
, 0),
347 PERF_HW("perf:thread:idle-cycles-backend", STALLED_CYCLES_BACKEND
,
348 CONTEXT_PERF_THREAD_COUNTER
, 0),
349 PERF_HW("perf:thread:instructions", INSTRUCTIONS
,
350 CONTEXT_PERF_THREAD_COUNTER
, 0),
351 PERF_HW("perf:thread:cache-references", CACHE_REFERENCES
,
352 CONTEXT_PERF_THREAD_COUNTER
, 0),
353 PERF_HW("perf:thread:cache-misses", CACHE_MISSES
,
354 CONTEXT_PERF_THREAD_COUNTER
, 0),
355 PERF_HW("perf:thread:branch-instructions", BRANCH_INSTRUCTIONS
,
356 CONTEXT_PERF_THREAD_COUNTER
, 0),
357 PERF_HW("perf:thread:branches", BRANCH_INSTRUCTIONS
,
358 CONTEXT_PERF_THREAD_COUNTER
, 0),
359 PERF_HW("perf:thread:branch-misses", BRANCH_MISSES
,
360 CONTEXT_PERF_THREAD_COUNTER
, 0),
361 PERF_HW("perf:thread:bus-cycles", BUS_CYCLES
,
362 CONTEXT_PERF_THREAD_COUNTER
, 0),
364 PERF_HW_CACHE("perf:thread:L1-dcache", L1D
,
365 CONTEXT_PERF_THREAD_COUNTER
, 0),
366 PERF_HW_CACHE("perf:thread:L1-icache", L1I
,
367 CONTEXT_PERF_THREAD_COUNTER
, 0),
368 PERF_HW_CACHE("perf:thread:LLC", LL
,
369 CONTEXT_PERF_THREAD_COUNTER
, 0),
370 PERF_HW_CACHE("perf:thread:dTLB", DTLB
,
371 CONTEXT_PERF_THREAD_COUNTER
, 0),
372 _PERF_HW_CACHE("perf:thread:iTLB-loads", ITLB
,
373 CONTEXT_PERF_THREAD_COUNTER
, READ
, ACCESS
, 0),
374 _PERF_HW_CACHE("perf:thread:iTLB-load-misses", ITLB
,
375 CONTEXT_PERF_THREAD_COUNTER
, READ
, MISS
, 0),
376 _PERF_HW_CACHE("perf:thread:branch-loads", BPU
,
377 CONTEXT_PERF_THREAD_COUNTER
, READ
, ACCESS
, 0),
378 _PERF_HW_CACHE("perf:thread:branch-load-misses", BPU
,
379 CONTEXT_PERF_THREAD_COUNTER
, READ
, MISS
, 0),
381 PERF_SW("perf:thread:cpu-clock", CPU_CLOCK
,
382 CONTEXT_PERF_THREAD_COUNTER
, 0),
383 PERF_SW("perf:thread:task-clock", TASK_CLOCK
,
384 CONTEXT_PERF_THREAD_COUNTER
, 0),
385 PERF_SW("perf:thread:page-fault", PAGE_FAULTS
,
386 CONTEXT_PERF_THREAD_COUNTER
, 0),
387 PERF_SW("perf:thread:faults", PAGE_FAULTS
,
388 CONTEXT_PERF_THREAD_COUNTER
, 0),
389 PERF_SW("perf:thread:major-faults", PAGE_FAULTS_MAJ
,
390 CONTEXT_PERF_THREAD_COUNTER
, 0),
391 PERF_SW("perf:thread:minor-faults", PAGE_FAULTS_MIN
,
392 CONTEXT_PERF_THREAD_COUNTER
, 0),
393 PERF_SW("perf:thread:context-switches", CONTEXT_SWITCHES
,
394 CONTEXT_PERF_THREAD_COUNTER
, 0),
395 PERF_SW("perf:thread:cs", CONTEXT_SWITCHES
,
396 CONTEXT_PERF_THREAD_COUNTER
, 0),
397 PERF_SW("perf:thread:cpu-migrations", CPU_MIGRATIONS
,
398 CONTEXT_PERF_THREAD_COUNTER
, 0),
399 PERF_SW("perf:thread:migrations", CPU_MIGRATIONS
,
400 CONTEXT_PERF_THREAD_COUNTER
, 0),
401 PERF_SW("perf:thread:alignment-faults", ALIGNMENT_FAULTS
,
402 CONTEXT_PERF_THREAD_COUNTER
, 0),
403 PERF_SW("perf:thread:emulation-faults", EMULATION_FAULTS
,
404 CONTEXT_PERF_THREAD_COUNTER
, 0),
407 * Perf per-CPU counters, backward compatibilty for names.
408 * Hidden from help listing.
410 PERF_HW("perf:cpu-cycles", CPU_CYCLES
,
411 CONTEXT_PERF_COUNTER
, 1),
412 PERF_HW("perf:cycles", CPU_CYCLES
,
413 CONTEXT_PERF_COUNTER
, 1),
414 PERF_HW("perf:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND
,
415 CONTEXT_PERF_COUNTER
, 1),
416 PERF_HW("perf:idle-cycles-frontend", STALLED_CYCLES_FRONTEND
,
417 CONTEXT_PERF_COUNTER
, 1),
418 PERF_HW("perf:stalled-cycles-backend", STALLED_CYCLES_BACKEND
,
419 CONTEXT_PERF_COUNTER
, 1),
420 PERF_HW("perf:idle-cycles-backend", STALLED_CYCLES_BACKEND
,
421 CONTEXT_PERF_COUNTER
, 1),
422 PERF_HW("perf:instructions", INSTRUCTIONS
,
423 CONTEXT_PERF_COUNTER
, 1),
424 PERF_HW("perf:cache-references", CACHE_REFERENCES
,
425 CONTEXT_PERF_COUNTER
, 1),
426 PERF_HW("perf:cache-misses", CACHE_MISSES
,
427 CONTEXT_PERF_COUNTER
, 1),
428 PERF_HW("perf:branch-instructions", BRANCH_INSTRUCTIONS
,
429 CONTEXT_PERF_COUNTER
, 1),
430 PERF_HW("perf:branches", BRANCH_INSTRUCTIONS
,
431 CONTEXT_PERF_COUNTER
, 1),
432 PERF_HW("perf:branch-misses", BRANCH_MISSES
,
433 CONTEXT_PERF_COUNTER
, 1),
434 PERF_HW("perf:bus-cycles", BUS_CYCLES
,
435 CONTEXT_PERF_COUNTER
, 1),
437 PERF_HW_CACHE("perf:L1-dcache", L1D
,
438 CONTEXT_PERF_COUNTER
, 1),
439 PERF_HW_CACHE("perf:L1-icache", L1I
,
440 CONTEXT_PERF_COUNTER
, 1),
441 PERF_HW_CACHE("perf:LLC", LL
,
442 CONTEXT_PERF_COUNTER
, 1),
443 PERF_HW_CACHE("perf:dTLB", DTLB
,
444 CONTEXT_PERF_COUNTER
, 1),
445 _PERF_HW_CACHE("perf:iTLB-loads", ITLB
,
446 CONTEXT_PERF_COUNTER
, READ
, ACCESS
, 1),
447 _PERF_HW_CACHE("perf:iTLB-load-misses", ITLB
,
448 CONTEXT_PERF_COUNTER
, READ
, MISS
, 1),
449 _PERF_HW_CACHE("perf:branch-loads", BPU
,
450 CONTEXT_PERF_COUNTER
, READ
, ACCESS
, 1),
451 _PERF_HW_CACHE("perf:branch-load-misses", BPU
,
452 CONTEXT_PERF_COUNTER
, READ
, MISS
, 1),
454 PERF_SW("perf:cpu-clock", CPU_CLOCK
,
455 CONTEXT_PERF_COUNTER
, 1),
456 PERF_SW("perf:task-clock", TASK_CLOCK
,
457 CONTEXT_PERF_COUNTER
, 1),
458 PERF_SW("perf:page-fault", PAGE_FAULTS
,
459 CONTEXT_PERF_COUNTER
, 1),
460 PERF_SW("perf:faults", PAGE_FAULTS
,
461 CONTEXT_PERF_COUNTER
, 1),
462 PERF_SW("perf:major-faults", PAGE_FAULTS_MAJ
,
463 CONTEXT_PERF_COUNTER
, 1),
464 PERF_SW("perf:minor-faults", PAGE_FAULTS_MIN
,
465 CONTEXT_PERF_COUNTER
, 1),
466 PERF_SW("perf:context-switches", CONTEXT_SWITCHES
,
467 CONTEXT_PERF_COUNTER
, 1),
468 PERF_SW("perf:cs", CONTEXT_SWITCHES
,
469 CONTEXT_PERF_COUNTER
, 1),
470 PERF_SW("perf:cpu-migrations", CPU_MIGRATIONS
,
471 CONTEXT_PERF_COUNTER
, 1),
472 PERF_SW("perf:migrations", CPU_MIGRATIONS
,
473 CONTEXT_PERF_COUNTER
, 1),
474 PERF_SW("perf:alignment-faults", ALIGNMENT_FAULTS
,
475 CONTEXT_PERF_COUNTER
, 1),
476 PERF_SW("perf:emulation-faults", EMULATION_FAULTS
,
477 CONTEXT_PERF_COUNTER
, 1),
479 { NULL
, -1 }, /* Closure */
483 #undef _PERF_HW_CACHE
488 * Context type for command line option parsing.
491 struct ctx_opts
*opt
;
492 struct cds_list_head list
;
496 * List of context type. Use to enable multiple context on a single command
499 struct ctx_type_list
{
500 struct cds_list_head head
;
502 .head
= CDS_LIST_HEAD_INIT(ctx_type_list
.head
),
508 * Find context numerical value from string.
510 * Return -1 if not found.
512 static int find_ctx_type_idx(const char *opt
)
516 while (ctx_opts
[i
].symbol
!= NULL
) {
517 if (strcmp(opt
, ctx_opts
[i
].symbol
) == 0) {
530 enum lttng_domain_type
get_domain(void)
533 return LTTNG_DOMAIN_KERNEL
;
534 } else if (opt_userspace
) {
535 return LTTNG_DOMAIN_UST
;
536 } else if (opt_jul
) {
537 return LTTNG_DOMAIN_JUL
;
538 } else if (opt_log4j
) {
539 return LTTNG_DOMAIN_LOG4J
;
556 ret
= fileno(stdout
);
558 PERROR("Unable to retrieve fileno of stdout");
563 writer
= mi_lttng_writer_create(ret
, lttng_opt_mi
);
569 /* Open command element */
570 ret
= mi_lttng_writer_command_open(writer
,
571 mi_lttng_element_command_add_context
);
577 /* Open output element */
578 ret
= mi_lttng_writer_open_element(writer
,
579 mi_lttng_element_command_output
);
589 int mi_close(enum cmd_error_code success
)
598 /* Close output element */
599 ret
= mi_lttng_writer_close_element(writer
);
606 ret
= mi_lttng_writer_write_element_bool(writer
,
607 mi_lttng_element_command_success
, !success
);
613 /* Command element close */
614 ret
= mi_lttng_writer_command_close(writer
);
624 void populate_context(struct lttng_event_context
*context
,
625 const struct ctx_opts
*opt
)
629 context
->ctx
= (enum lttng_event_context_type
) opt
->ctx_type
;
630 switch (context
->ctx
) {
631 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
632 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
633 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
634 context
->u
.perf_counter
.type
= opt
->u
.perf
.type
;
635 context
->u
.perf_counter
.config
= opt
->u
.perf
.config
;
636 strncpy(context
->u
.perf_counter
.name
, opt
->symbol
,
637 LTTNG_SYMBOL_NAME_LEN
);
638 context
->u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
639 /* Replace : and - by _ */
640 while ((ptr
= strchr(context
->u
.perf_counter
.name
, '-')) != NULL
) {
643 while ((ptr
= strchr(context
->u
.perf_counter
.name
, ':')) != NULL
) {
647 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
648 context
->u
.app_ctx
.provider_name
=
649 opt
->u
.app_ctx
.provider_name
;
650 context
->u
.app_ctx
.ctx_name
=
651 opt
->u
.app_ctx
.ctx_name
;
659 * Pretty print context type.
662 int print_ctx_type(void)
668 struct lttng_event_context context
;
670 memset(&context
, 0, sizeof(context
));
679 /* Open a contexts element */
680 ret
= mi_lttng_writer_open_element(writer
, config_element_contexts
);
687 while (ctx_opts
[i
].symbol
!= NULL
) {
688 if (!ctx_opts
[i
].hide_help
) {
690 populate_context(&context
, &ctx_opts
[i
]);
691 ret
= mi_lttng_context(writer
, &context
, 1);
697 ret
= mi_lttng_writer_write_element_string(
699 mi_lttng_element_context_symbol
,
706 ret
= mi_lttng_writer_close_element(writer
);
712 fprintf(ofp
, "%s\n", ctx_opts
[i
].symbol
);
719 /* Close contexts element */
720 ret
= mi_lttng_writer_close_element(writer
);
735 * Add context to channel or event.
737 static int add_context(char *session_name
)
739 int ret
= CMD_SUCCESS
, warn
= 0, success
= 0;
740 struct lttng_event_context context
;
741 struct lttng_domain dom
;
742 struct ctx_type
*type
;
744 memset(&context
, 0, sizeof(context
));
745 memset(&dom
, 0, sizeof(dom
));
747 dom
.type
= get_domain();
748 handle
= lttng_create_handle(session_name
, &dom
);
749 if (handle
== NULL
) {
755 /* Open a contexts element */
756 ret
= mi_lttng_writer_open_element(writer
, config_element_contexts
);
762 /* Iterate over all the context types given */
763 cds_list_for_each_entry(type
, &ctx_type_list
.head
, list
) {
764 DBG("Adding context...");
766 populate_context(&context
, type
->opt
);
769 /* We leave context open the update the success of the command */
770 ret
= mi_lttng_context(writer
, &context
, 1);
776 ret
= mi_lttng_writer_write_element_string(writer
,
777 mi_lttng_element_context_symbol
,
785 ret
= lttng_add_context(handle
, &context
, NULL
, opt_channel_name
);
787 ERR("%s: %s", type
->opt
->symbol
, lttng_strerror(ret
));
791 if (opt_channel_name
) {
792 MSG("%s context %s added to channel %s",
793 get_domain_str(dom
.type
), type
->opt
->symbol
,
796 MSG("%s context %s added to all channels",
797 get_domain_str(dom
.type
), type
->opt
->symbol
);
803 /* Is the single operation a success ? */
804 ret
= mi_lttng_writer_write_element_bool(writer
,
805 mi_lttng_element_success
, success
);
811 /* Close the context element */
812 ret
= mi_lttng_writer_close_element(writer
);
821 /* Close contexts element */
822 ret
= mi_lttng_writer_close_element(writer
);
831 lttng_destroy_handle(handle
);
834 * This means that at least one add_context failed and tells the user to
835 * look on stderr for error(s).
844 void destroy_ctx_type(struct ctx_type
*type
)
850 free(type
->opt
->symbol
);
857 struct ctx_type
*create_ctx_type(void)
859 struct ctx_type
*type
= zmalloc(sizeof(*type
));
862 PERROR("malloc ctx_type");
866 type
->opt
= zmalloc(sizeof(*type
->opt
));
868 PERROR("malloc ctx_type options");
869 destroy_ctx_type(type
);
878 int find_ctx_type_perf_raw(const char *ctx
, struct ctx_type
*type
)
882 char *tmp_list
, *cur_list
;
884 cur_list
= tmp_list
= strdup(ctx
);
886 PERROR("strdup temp list");
891 /* Looking for "perf:[cpu|thread]:raw:<mask>:<name>". */
895 next
= strtok(cur_list
, ":");
902 if (strncmp(next
, "perf", 4) != 0) {
908 if (strncmp(next
, "cpu", 3) == 0) {
909 type
->opt
->ctx_type
= CONTEXT_PERF_CPU_COUNTER
;
910 } else if (strncmp(next
, "thread", 4) == 0) {
911 type
->opt
->ctx_type
= CONTEXT_PERF_THREAD_COUNTER
;
918 if (strncmp(next
, "raw", 3) != 0) {
927 if (strlen(next
) < 2 || next
[0] != 'r') {
928 ERR("Wrong perf raw mask format: expected rNNN");
933 type
->opt
->u
.perf
.config
= strtoll(next
+ 1, &endptr
, 16);
934 if (errno
!= 0 || !endptr
|| *endptr
) {
935 ERR("Wrong perf raw mask format: expected rNNN");
945 ERR("Too many ':' in perf raw format");
953 ERR("Invalid perf counter specifier, expected a specifier of "
954 "the form perf:cpu:raw:rNNN:<name> or "
955 "perf:thread:raw:rNNN:<name>");
969 struct ctx_type
*get_context_type(const char *ctx
)
972 struct ctx_type
*type
= NULL
;
973 const char app_ctx_prefix
[] = "$app.";
974 char *provider_name
= NULL
, *ctx_name
= NULL
;
975 size_t i
, len
, colon_pos
= 0, provider_name_len
, ctx_name_len
;
981 type
= create_ctx_type();
986 /* Check if ctx matches a known static context. */
987 opt_index
= find_ctx_type_idx(ctx
);
988 if (opt_index
>= 0) {
989 *type
->opt
= ctx_opts
[opt_index
];
990 type
->opt
->symbol
= strdup(ctx_opts
[opt_index
].symbol
);
994 /* Check if ctx is a raw perf context. */
995 ret
= find_ctx_type_perf_raw(ctx
, type
);
997 type
->opt
->u
.perf
.type
= PERF_TYPE_RAW
;
998 type
->opt
->symbol
= strdup(ctx
);
999 if (!type
->opt
->symbol
) {
1000 PERROR("Copy perf field name");
1007 * No match found against static contexts; check if it is an app
1011 if (len
<= sizeof(app_ctx_prefix
) - 1) {
1015 /* String starts with $app. */
1016 if (strncmp(ctx
, app_ctx_prefix
, sizeof(app_ctx_prefix
) - 1)) {
1020 /* Validate that the ':' separator is present. */
1021 for (i
= sizeof(app_ctx_prefix
); i
< len
; i
++) {
1022 const char c
= ctx
[i
];
1031 * No colon found or no ctx name ("$app.provider:") or no provider name
1032 * given ("$app.:..."), which is invalid.
1034 if (!colon_pos
|| colon_pos
== len
||
1035 colon_pos
== sizeof(app_ctx_prefix
)) {
1036 ERR("Invalid application context provided: no provider or context name provided.");
1040 provider_name_len
= colon_pos
- sizeof(app_ctx_prefix
) + 2;
1041 provider_name
= zmalloc(provider_name_len
);
1042 if (!provider_name
) {
1043 PERROR("malloc provider_name");
1046 strncpy(provider_name
, ctx
+ sizeof(app_ctx_prefix
) - 1,
1047 provider_name_len
- 1);
1048 type
->opt
->u
.app_ctx
.provider_name
= provider_name
;
1050 ctx_name_len
= len
- colon_pos
;
1051 ctx_name
= zmalloc(ctx_name_len
);
1053 PERROR("malloc ctx_name");
1056 strncpy(ctx_name
, ctx
+ colon_pos
+ 1, ctx_name_len
- 1);
1057 type
->opt
->u
.app_ctx
.ctx_name
= ctx_name
;
1058 type
->opt
->ctx_type
= CONTEXT_APP_CONTEXT
;
1059 type
->opt
->symbol
= strdup(ctx
);
1063 free(provider_name
);
1065 destroy_ctx_type(type
);
1070 * Add context to channel or event.
1072 int cmd_add_context(int argc
, const char **argv
)
1074 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
1075 static poptContext pc
;
1076 struct ctx_type
*type
, *tmptype
;
1077 char *session_name
= NULL
;
1078 const char *leftover
= NULL
;
1085 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1086 poptReadDefaultConfig(pc
, 0);
1088 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1094 ret
= print_ctx_type();
1098 type
= get_context_type(opt_type
);
1100 ERR("Unknown context type %s", opt_type
);
1104 cds_list_add_tail(&type
->list
, &ctx_type_list
.head
);
1116 case OPT_LIST_OPTIONS
:
1117 list_cmd_options(stdout
, long_options
);
1120 ret
= CMD_UNDEFINED
;
1125 leftover
= poptGetArg(pc
);
1127 ERR("Unknown argument: %s", leftover
);
1132 ret
= print_missing_or_multiple_domains(opt_kernel
+ opt_userspace
+
1133 opt_jul
+ opt_log4j
);
1140 ERR("Missing mandatory -t TYPE");
1145 if (!opt_session_name
) {
1146 session_name
= get_session_name();
1147 if (session_name
== NULL
) {
1152 session_name
= opt_session_name
;
1160 command_ret
= add_context(session_name
);
1161 ret
= mi_close(command_ret
);
1167 if (!opt_session_name
) {
1172 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1173 /* Preserve original error code */
1174 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1177 /* Cleanup allocated memory */
1178 cds_list_for_each_entry_safe(type
, tmptype
, &ctx_type_list
.head
, list
) {
1179 destroy_ctx_type(type
);
1182 /* Overwrite ret if an error occurred during add_context() */
1183 ret
= command_ret
? command_ret
: ret
;
1185 poptFreeContext(pc
);