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,
90 * Taken from the Perf ABI (all enum perf_*)
93 PERF_TYPE_HARDWARE
= 0,
94 PERF_TYPE_SOFTWARE
= 1,
95 PERF_TYPE_HW_CACHE
= 3,
99 enum perf_count_hard
{
100 PERF_COUNT_HW_CPU_CYCLES
= 0,
101 PERF_COUNT_HW_INSTRUCTIONS
= 1,
102 PERF_COUNT_HW_CACHE_REFERENCES
= 2,
103 PERF_COUNT_HW_CACHE_MISSES
= 3,
104 PERF_COUNT_HW_BRANCH_INSTRUCTIONS
= 4,
105 PERF_COUNT_HW_BRANCH_MISSES
= 5,
106 PERF_COUNT_HW_BUS_CYCLES
= 6,
107 PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
= 7,
108 PERF_COUNT_HW_STALLED_CYCLES_BACKEND
= 8,
111 enum perf_count_soft
{
112 PERF_COUNT_SW_CPU_CLOCK
= 0,
113 PERF_COUNT_SW_TASK_CLOCK
= 1,
114 PERF_COUNT_SW_PAGE_FAULTS
= 2,
115 PERF_COUNT_SW_CONTEXT_SWITCHES
= 3,
116 PERF_COUNT_SW_CPU_MIGRATIONS
= 4,
117 PERF_COUNT_SW_PAGE_FAULTS_MIN
= 5,
118 PERF_COUNT_SW_PAGE_FAULTS_MAJ
= 6,
119 PERF_COUNT_SW_ALIGNMENT_FAULTS
= 7,
120 PERF_COUNT_SW_EMULATION_FAULTS
= 8,
124 * Generalized hardware cache events:
126 * { L1-D, L1-I, LLC, ITLB, DTLB, BPU } x
127 * { read, write, prefetch } x
128 * { accesses, misses }
130 enum perf_hw_cache_id
{
131 PERF_COUNT_HW_CACHE_L1D
= 0,
132 PERF_COUNT_HW_CACHE_L1I
= 1,
133 PERF_COUNT_HW_CACHE_LL
= 2,
134 PERF_COUNT_HW_CACHE_DTLB
= 3,
135 PERF_COUNT_HW_CACHE_ITLB
= 4,
136 PERF_COUNT_HW_CACHE_BPU
= 5,
138 PERF_COUNT_HW_CACHE_MAX
, /* non-ABI */
141 enum perf_hw_cache_op_id
{
142 PERF_COUNT_HW_CACHE_OP_READ
= 0,
143 PERF_COUNT_HW_CACHE_OP_WRITE
= 1,
144 PERF_COUNT_HW_CACHE_OP_PREFETCH
= 2,
146 PERF_COUNT_HW_CACHE_OP_MAX
, /* non-ABI */
149 enum perf_hw_cache_op_result_id
{
150 PERF_COUNT_HW_CACHE_RESULT_ACCESS
= 0,
151 PERF_COUNT_HW_CACHE_RESULT_MISS
= 1,
153 PERF_COUNT_HW_CACHE_RESULT_MAX
, /* non-ABI */
156 static struct poptOption long_options
[] = {
157 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
158 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
159 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
160 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
161 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
162 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
163 {"jul", 'j', POPT_ARG_NONE
, 0, OPT_JUL
, 0, 0},
164 {"log4j", 'l', POPT_ARG_NONE
, 0, OPT_LOG4J
, 0, 0},
165 {"type", 't', POPT_ARG_STRING
, &opt_type
, OPT_TYPE
, 0, 0},
166 {"list", 0, POPT_ARG_NONE
, NULL
, OPT_LIST
, NULL
, NULL
},
167 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
168 {0, 0, 0, 0, 0, 0, 0}
174 #define PERF_HW(optstr, name, type, hide) \
176 optstr, type, hide, \
177 .u.perf = { PERF_TYPE_HARDWARE, PERF_COUNT_HW_##name, },\
180 #define PERF_SW(optstr, name, type, hide) \
182 optstr, type, hide, \
183 .u.perf = { PERF_TYPE_SOFTWARE, PERF_COUNT_SW_##name, },\
186 #define _PERF_HW_CACHE(optstr, name, type, op, result, hide) \
188 optstr, type, hide, \
190 PERF_TYPE_HW_CACHE, \
191 (uint64_t) PERF_COUNT_HW_CACHE_##name \
192 | ((uint64_t) PERF_COUNT_HW_CACHE_OP_##op << 8) \
193 | ((uint64_t) PERF_COUNT_HW_CACHE_RESULT_##result << 16), \
197 #define PERF_HW_CACHE(optstr, name, type, hide) \
198 _PERF_HW_CACHE(optstr "-loads", name, type, \
199 READ, ACCESS, hide), \
200 _PERF_HW_CACHE(optstr "-load-misses", name, type, \
202 _PERF_HW_CACHE(optstr "-stores", name, type, \
203 WRITE, ACCESS, hide), \
204 _PERF_HW_CACHE(optstr "-store-misses", name, type, \
205 WRITE, MISS, hide), \
206 _PERF_HW_CACHE(optstr "-prefetches", name, type, \
207 PREFETCH, ACCESS, hide), \
208 _PERF_HW_CACHE(optstr "-prefetch-misses", name, type, \
209 PREFETCH, MISS, hide)
212 const struct ctx_opts
{
214 enum context_type ctx_type
;
215 int hide_help
; /* Hide from --help */
227 { "pid", CONTEXT_PID
},
228 { "procname", CONTEXT_PROCNAME
},
229 { "prio", CONTEXT_PRIO
},
230 { "nice", CONTEXT_NICE
},
231 { "vpid", CONTEXT_VPID
},
232 { "tid", CONTEXT_TID
},
233 { "pthread_id", CONTEXT_PTHREAD_ID
},
234 { "vtid", CONTEXT_VTID
},
235 { "ppid", CONTEXT_PPID
},
236 { "vppid", CONTEXT_VPPID
},
237 { "hostname", CONTEXT_HOSTNAME
},
238 { "ip", CONTEXT_IP
},
239 { "interruptible", CONTEXT_INTERRUPTIBLE
},
240 { "preemptible", CONTEXT_PREEMPTIBLE
},
241 { "need_reschedule", CONTEXT_NEED_RESCHEDULE
},
242 { "migratable", CONTEXT_MIGRATABLE
},
246 /* Perf per-CPU counters */
247 PERF_HW("perf:cpu:cpu-cycles", CPU_CYCLES
,
248 CONTEXT_PERF_CPU_COUNTER
, 0),
249 PERF_HW("perf:cpu:cycles", CPU_CYCLES
,
250 CONTEXT_PERF_CPU_COUNTER
, 0),
251 PERF_HW("perf:cpu:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND
,
252 CONTEXT_PERF_CPU_COUNTER
, 0),
253 PERF_HW("perf:cpu:idle-cycles-frontend", STALLED_CYCLES_FRONTEND
,
254 CONTEXT_PERF_CPU_COUNTER
, 0),
255 PERF_HW("perf:cpu:stalled-cycles-backend", STALLED_CYCLES_BACKEND
,
256 CONTEXT_PERF_CPU_COUNTER
, 0),
257 PERF_HW("perf:cpu:idle-cycles-backend", STALLED_CYCLES_BACKEND
,
258 CONTEXT_PERF_CPU_COUNTER
, 0),
259 PERF_HW("perf:cpu:instructions", INSTRUCTIONS
,
260 CONTEXT_PERF_CPU_COUNTER
, 0),
261 PERF_HW("perf:cpu:cache-references", CACHE_REFERENCES
,
262 CONTEXT_PERF_CPU_COUNTER
, 0),
263 PERF_HW("perf:cpu:cache-misses", CACHE_MISSES
,
264 CONTEXT_PERF_CPU_COUNTER
, 0),
265 PERF_HW("perf:cpu:branch-instructions", BRANCH_INSTRUCTIONS
,
266 CONTEXT_PERF_CPU_COUNTER
, 0),
267 PERF_HW("perf:cpu:branches", BRANCH_INSTRUCTIONS
,
268 CONTEXT_PERF_CPU_COUNTER
, 0),
269 PERF_HW("perf:cpu:branch-misses", BRANCH_MISSES
,
270 CONTEXT_PERF_CPU_COUNTER
, 0),
271 PERF_HW("perf:cpu:bus-cycles", BUS_CYCLES
,
272 CONTEXT_PERF_CPU_COUNTER
, 0),
274 PERF_HW_CACHE("perf:cpu:L1-dcache", L1D
,
275 CONTEXT_PERF_CPU_COUNTER
, 0),
276 PERF_HW_CACHE("perf:cpu:L1-icache", L1I
,
277 CONTEXT_PERF_CPU_COUNTER
, 0),
278 PERF_HW_CACHE("perf:cpu:LLC", LL
,
279 CONTEXT_PERF_CPU_COUNTER
, 0),
280 PERF_HW_CACHE("perf:cpu:dTLB", DTLB
,
281 CONTEXT_PERF_CPU_COUNTER
, 0),
282 _PERF_HW_CACHE("perf:cpu:iTLB-loads", ITLB
,
283 CONTEXT_PERF_CPU_COUNTER
, READ
, ACCESS
, 0),
284 _PERF_HW_CACHE("perf:cpu:iTLB-load-misses", ITLB
,
285 CONTEXT_PERF_CPU_COUNTER
, READ
, MISS
, 0),
286 _PERF_HW_CACHE("perf:cpu:branch-loads", BPU
,
287 CONTEXT_PERF_CPU_COUNTER
, READ
, ACCESS
, 0),
288 _PERF_HW_CACHE("perf:cpu:branch-load-misses", BPU
,
289 CONTEXT_PERF_CPU_COUNTER
, READ
, MISS
, 0),
291 PERF_SW("perf:cpu:cpu-clock", CPU_CLOCK
,
292 CONTEXT_PERF_CPU_COUNTER
, 0),
293 PERF_SW("perf:cpu:task-clock", TASK_CLOCK
,
294 CONTEXT_PERF_CPU_COUNTER
, 0),
295 PERF_SW("perf:cpu:page-fault", PAGE_FAULTS
,
296 CONTEXT_PERF_CPU_COUNTER
, 0),
297 PERF_SW("perf:cpu:faults", PAGE_FAULTS
,
298 CONTEXT_PERF_CPU_COUNTER
, 0),
299 PERF_SW("perf:cpu:major-faults", PAGE_FAULTS_MAJ
,
300 CONTEXT_PERF_CPU_COUNTER
, 0),
301 PERF_SW("perf:cpu:minor-faults", PAGE_FAULTS_MIN
,
302 CONTEXT_PERF_CPU_COUNTER
, 0),
303 PERF_SW("perf:cpu:context-switches", CONTEXT_SWITCHES
,
304 CONTEXT_PERF_CPU_COUNTER
, 0),
305 PERF_SW("perf:cpu:cs", CONTEXT_SWITCHES
,
306 CONTEXT_PERF_CPU_COUNTER
, 0),
307 PERF_SW("perf:cpu:cpu-migrations", CPU_MIGRATIONS
,
308 CONTEXT_PERF_CPU_COUNTER
, 0),
309 PERF_SW("perf:cpu:migrations", CPU_MIGRATIONS
,
310 CONTEXT_PERF_CPU_COUNTER
, 0),
311 PERF_SW("perf:cpu:alignment-faults", ALIGNMENT_FAULTS
,
312 CONTEXT_PERF_CPU_COUNTER
, 0),
313 PERF_SW("perf:cpu:emulation-faults", EMULATION_FAULTS
,
314 CONTEXT_PERF_CPU_COUNTER
, 0),
316 /* Perf per-thread counters */
317 PERF_HW("perf:thread:cpu-cycles", CPU_CYCLES
,
318 CONTEXT_PERF_THREAD_COUNTER
, 0),
319 PERF_HW("perf:thread:cycles", CPU_CYCLES
,
320 CONTEXT_PERF_THREAD_COUNTER
, 0),
321 PERF_HW("perf:thread:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND
,
322 CONTEXT_PERF_THREAD_COUNTER
, 0),
323 PERF_HW("perf:thread:idle-cycles-frontend", STALLED_CYCLES_FRONTEND
,
324 CONTEXT_PERF_THREAD_COUNTER
, 0),
325 PERF_HW("perf:thread:stalled-cycles-backend", STALLED_CYCLES_BACKEND
,
326 CONTEXT_PERF_THREAD_COUNTER
, 0),
327 PERF_HW("perf:thread:idle-cycles-backend", STALLED_CYCLES_BACKEND
,
328 CONTEXT_PERF_THREAD_COUNTER
, 0),
329 PERF_HW("perf:thread:instructions", INSTRUCTIONS
,
330 CONTEXT_PERF_THREAD_COUNTER
, 0),
331 PERF_HW("perf:thread:cache-references", CACHE_REFERENCES
,
332 CONTEXT_PERF_THREAD_COUNTER
, 0),
333 PERF_HW("perf:thread:cache-misses", CACHE_MISSES
,
334 CONTEXT_PERF_THREAD_COUNTER
, 0),
335 PERF_HW("perf:thread:branch-instructions", BRANCH_INSTRUCTIONS
,
336 CONTEXT_PERF_THREAD_COUNTER
, 0),
337 PERF_HW("perf:thread:branches", BRANCH_INSTRUCTIONS
,
338 CONTEXT_PERF_THREAD_COUNTER
, 0),
339 PERF_HW("perf:thread:branch-misses", BRANCH_MISSES
,
340 CONTEXT_PERF_THREAD_COUNTER
, 0),
341 PERF_HW("perf:thread:bus-cycles", BUS_CYCLES
,
342 CONTEXT_PERF_THREAD_COUNTER
, 0),
344 PERF_HW_CACHE("perf:thread:L1-dcache", L1D
,
345 CONTEXT_PERF_THREAD_COUNTER
, 0),
346 PERF_HW_CACHE("perf:thread:L1-icache", L1I
,
347 CONTEXT_PERF_THREAD_COUNTER
, 0),
348 PERF_HW_CACHE("perf:thread:LLC", LL
,
349 CONTEXT_PERF_THREAD_COUNTER
, 0),
350 PERF_HW_CACHE("perf:thread:dTLB", DTLB
,
351 CONTEXT_PERF_THREAD_COUNTER
, 0),
352 _PERF_HW_CACHE("perf:thread:iTLB-loads", ITLB
,
353 CONTEXT_PERF_THREAD_COUNTER
, READ
, ACCESS
, 0),
354 _PERF_HW_CACHE("perf:thread:iTLB-load-misses", ITLB
,
355 CONTEXT_PERF_THREAD_COUNTER
, READ
, MISS
, 0),
356 _PERF_HW_CACHE("perf:thread:branch-loads", BPU
,
357 CONTEXT_PERF_THREAD_COUNTER
, READ
, ACCESS
, 0),
358 _PERF_HW_CACHE("perf:thread:branch-load-misses", BPU
,
359 CONTEXT_PERF_THREAD_COUNTER
, READ
, MISS
, 0),
361 PERF_SW("perf:thread:cpu-clock", CPU_CLOCK
,
362 CONTEXT_PERF_THREAD_COUNTER
, 0),
363 PERF_SW("perf:thread:task-clock", TASK_CLOCK
,
364 CONTEXT_PERF_THREAD_COUNTER
, 0),
365 PERF_SW("perf:thread:page-fault", PAGE_FAULTS
,
366 CONTEXT_PERF_THREAD_COUNTER
, 0),
367 PERF_SW("perf:thread:faults", PAGE_FAULTS
,
368 CONTEXT_PERF_THREAD_COUNTER
, 0),
369 PERF_SW("perf:thread:major-faults", PAGE_FAULTS_MAJ
,
370 CONTEXT_PERF_THREAD_COUNTER
, 0),
371 PERF_SW("perf:thread:minor-faults", PAGE_FAULTS_MIN
,
372 CONTEXT_PERF_THREAD_COUNTER
, 0),
373 PERF_SW("perf:thread:context-switches", CONTEXT_SWITCHES
,
374 CONTEXT_PERF_THREAD_COUNTER
, 0),
375 PERF_SW("perf:thread:cs", CONTEXT_SWITCHES
,
376 CONTEXT_PERF_THREAD_COUNTER
, 0),
377 PERF_SW("perf:thread:cpu-migrations", CPU_MIGRATIONS
,
378 CONTEXT_PERF_THREAD_COUNTER
, 0),
379 PERF_SW("perf:thread:migrations", CPU_MIGRATIONS
,
380 CONTEXT_PERF_THREAD_COUNTER
, 0),
381 PERF_SW("perf:thread:alignment-faults", ALIGNMENT_FAULTS
,
382 CONTEXT_PERF_THREAD_COUNTER
, 0),
383 PERF_SW("perf:thread:emulation-faults", EMULATION_FAULTS
,
384 CONTEXT_PERF_THREAD_COUNTER
, 0),
387 * Perf per-CPU counters, backward compatibilty for names.
388 * Hidden from help listing.
390 PERF_HW("perf:cpu-cycles", CPU_CYCLES
,
391 CONTEXT_PERF_COUNTER
, 1),
392 PERF_HW("perf:cycles", CPU_CYCLES
,
393 CONTEXT_PERF_COUNTER
, 1),
394 PERF_HW("perf:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND
,
395 CONTEXT_PERF_COUNTER
, 1),
396 PERF_HW("perf:idle-cycles-frontend", STALLED_CYCLES_FRONTEND
,
397 CONTEXT_PERF_COUNTER
, 1),
398 PERF_HW("perf:stalled-cycles-backend", STALLED_CYCLES_BACKEND
,
399 CONTEXT_PERF_COUNTER
, 1),
400 PERF_HW("perf:idle-cycles-backend", STALLED_CYCLES_BACKEND
,
401 CONTEXT_PERF_COUNTER
, 1),
402 PERF_HW("perf:instructions", INSTRUCTIONS
,
403 CONTEXT_PERF_COUNTER
, 1),
404 PERF_HW("perf:cache-references", CACHE_REFERENCES
,
405 CONTEXT_PERF_COUNTER
, 1),
406 PERF_HW("perf:cache-misses", CACHE_MISSES
,
407 CONTEXT_PERF_COUNTER
, 1),
408 PERF_HW("perf:branch-instructions", BRANCH_INSTRUCTIONS
,
409 CONTEXT_PERF_COUNTER
, 1),
410 PERF_HW("perf:branches", BRANCH_INSTRUCTIONS
,
411 CONTEXT_PERF_COUNTER
, 1),
412 PERF_HW("perf:branch-misses", BRANCH_MISSES
,
413 CONTEXT_PERF_COUNTER
, 1),
414 PERF_HW("perf:bus-cycles", BUS_CYCLES
,
415 CONTEXT_PERF_COUNTER
, 1),
417 PERF_HW_CACHE("perf:L1-dcache", L1D
,
418 CONTEXT_PERF_COUNTER
, 1),
419 PERF_HW_CACHE("perf:L1-icache", L1I
,
420 CONTEXT_PERF_COUNTER
, 1),
421 PERF_HW_CACHE("perf:LLC", LL
,
422 CONTEXT_PERF_COUNTER
, 1),
423 PERF_HW_CACHE("perf:dTLB", DTLB
,
424 CONTEXT_PERF_COUNTER
, 1),
425 _PERF_HW_CACHE("perf:iTLB-loads", ITLB
,
426 CONTEXT_PERF_COUNTER
, READ
, ACCESS
, 1),
427 _PERF_HW_CACHE("perf:iTLB-load-misses", ITLB
,
428 CONTEXT_PERF_COUNTER
, READ
, MISS
, 1),
429 _PERF_HW_CACHE("perf:branch-loads", BPU
,
430 CONTEXT_PERF_COUNTER
, READ
, ACCESS
, 1),
431 _PERF_HW_CACHE("perf:branch-load-misses", BPU
,
432 CONTEXT_PERF_COUNTER
, READ
, MISS
, 1),
434 PERF_SW("perf:cpu-clock", CPU_CLOCK
,
435 CONTEXT_PERF_COUNTER
, 1),
436 PERF_SW("perf:task-clock", TASK_CLOCK
,
437 CONTEXT_PERF_COUNTER
, 1),
438 PERF_SW("perf:page-fault", PAGE_FAULTS
,
439 CONTEXT_PERF_COUNTER
, 1),
440 PERF_SW("perf:faults", PAGE_FAULTS
,
441 CONTEXT_PERF_COUNTER
, 1),
442 PERF_SW("perf:major-faults", PAGE_FAULTS_MAJ
,
443 CONTEXT_PERF_COUNTER
, 1),
444 PERF_SW("perf:minor-faults", PAGE_FAULTS_MIN
,
445 CONTEXT_PERF_COUNTER
, 1),
446 PERF_SW("perf:context-switches", CONTEXT_SWITCHES
,
447 CONTEXT_PERF_COUNTER
, 1),
448 PERF_SW("perf:cs", CONTEXT_SWITCHES
,
449 CONTEXT_PERF_COUNTER
, 1),
450 PERF_SW("perf:cpu-migrations", CPU_MIGRATIONS
,
451 CONTEXT_PERF_COUNTER
, 1),
452 PERF_SW("perf:migrations", CPU_MIGRATIONS
,
453 CONTEXT_PERF_COUNTER
, 1),
454 PERF_SW("perf:alignment-faults", ALIGNMENT_FAULTS
,
455 CONTEXT_PERF_COUNTER
, 1),
456 PERF_SW("perf:emulation-faults", EMULATION_FAULTS
,
457 CONTEXT_PERF_COUNTER
, 1),
459 { NULL
, -1 }, /* Closure */
463 #undef _PERF_HW_CACHE
468 * Context type for command line option parsing.
471 struct ctx_opts
*opt
;
472 struct cds_list_head list
;
476 * List of context type. Use to enable multiple context on a single command
479 struct ctx_type_list
{
480 struct cds_list_head head
;
482 .head
= CDS_LIST_HEAD_INIT(ctx_type_list
.head
),
488 * Find context numerical value from string.
490 * Return -1 if not found.
492 static int find_ctx_type_idx(const char *opt
)
496 while (ctx_opts
[i
].symbol
!= NULL
) {
497 if (strcmp(opt
, ctx_opts
[i
].symbol
) == 0) {
510 enum lttng_domain_type
get_domain(void)
513 return LTTNG_DOMAIN_KERNEL
;
514 } else if (opt_userspace
) {
515 return LTTNG_DOMAIN_UST
;
516 } else if (opt_jul
) {
517 return LTTNG_DOMAIN_JUL
;
518 } else if (opt_log4j
) {
519 return LTTNG_DOMAIN_LOG4J
;
536 ret
= fileno(stdout
);
538 PERROR("Unable to retrive fileno of stdout");
543 writer
= mi_lttng_writer_create(ret
, lttng_opt_mi
);
549 /* Open command element */
550 ret
= mi_lttng_writer_command_open(writer
,
551 mi_lttng_element_command_add_context
);
557 /* Open output element */
558 ret
= mi_lttng_writer_open_element(writer
,
559 mi_lttng_element_command_output
);
569 int mi_close(enum cmd_error_code success
)
578 /* Close output element */
579 ret
= mi_lttng_writer_close_element(writer
);
586 ret
= mi_lttng_writer_write_element_bool(writer
,
587 mi_lttng_element_command_success
, !success
);
593 /* Command element close */
594 ret
= mi_lttng_writer_command_close(writer
);
604 void populate_context(struct lttng_event_context
*context
,
605 const struct ctx_opts
*opt
)
609 context
->ctx
= (enum lttng_event_context_type
) opt
->ctx_type
;
610 switch (context
->ctx
) {
611 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
612 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
613 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
614 context
->u
.perf_counter
.type
= opt
->u
.perf
.type
;
615 context
->u
.perf_counter
.config
= opt
->u
.perf
.config
;
616 strncpy(context
->u
.perf_counter
.name
, opt
->symbol
,
617 LTTNG_SYMBOL_NAME_LEN
);
618 context
->u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
619 /* Replace : and - by _ */
620 while ((ptr
= strchr(context
->u
.perf_counter
.name
, '-')) != NULL
) {
623 while ((ptr
= strchr(context
->u
.perf_counter
.name
, ':')) != NULL
) {
627 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
628 context
->u
.app_ctx
.provider_name
=
629 opt
->u
.app_ctx
.provider_name
;
630 context
->u
.app_ctx
.ctx_name
=
631 opt
->u
.app_ctx
.ctx_name
;
639 * Pretty print context type.
642 int print_ctx_type(void)
648 struct lttng_event_context context
;
650 memset(&context
, 0, sizeof(context
));
659 /* Open a contexts element */
660 ret
= mi_lttng_writer_open_element(writer
, config_element_contexts
);
667 while (ctx_opts
[i
].symbol
!= NULL
) {
668 if (!ctx_opts
[i
].hide_help
) {
670 populate_context(&context
, &ctx_opts
[i
]);
671 ret
= mi_lttng_context(writer
, &context
, 1);
677 ret
= mi_lttng_writer_write_element_string(
679 mi_lttng_element_context_symbol
,
686 ret
= mi_lttng_writer_close_element(writer
);
692 fprintf(ofp
, "%s\n", ctx_opts
[i
].symbol
);
699 /* Close contexts element */
700 ret
= mi_lttng_writer_close_element(writer
);
715 * Add context to channel or event.
717 static int add_context(char *session_name
)
719 int ret
= CMD_SUCCESS
, warn
= 0, success
= 0;
720 struct lttng_event_context context
;
721 struct lttng_domain dom
;
722 struct ctx_type
*type
;
724 memset(&context
, 0, sizeof(context
));
725 memset(&dom
, 0, sizeof(dom
));
727 dom
.type
= get_domain();
728 handle
= lttng_create_handle(session_name
, &dom
);
729 if (handle
== NULL
) {
735 /* Open a contexts element */
736 ret
= mi_lttng_writer_open_element(writer
, config_element_contexts
);
742 /* Iterate over all the context types given */
743 cds_list_for_each_entry(type
, &ctx_type_list
.head
, list
) {
744 DBG("Adding context...");
746 populate_context(&context
, type
->opt
);
749 /* We leave context open the update the success of the command */
750 ret
= mi_lttng_context(writer
, &context
, 1);
756 ret
= mi_lttng_writer_write_element_string(writer
,
757 mi_lttng_element_context_symbol
,
765 ret
= lttng_add_context(handle
, &context
, NULL
, opt_channel_name
);
767 ERR("%s: %s", type
->opt
->symbol
, lttng_strerror(ret
));
771 if (opt_channel_name
) {
772 MSG("%s context %s added to channel %s",
773 get_domain_str(dom
.type
), type
->opt
->symbol
,
776 MSG("%s context %s added to all channels",
777 get_domain_str(dom
.type
), type
->opt
->symbol
);
783 /* Is the single operation a success ? */
784 ret
= mi_lttng_writer_write_element_bool(writer
,
785 mi_lttng_element_success
, success
);
791 /* Close the context element */
792 ret
= mi_lttng_writer_close_element(writer
);
801 /* Close contexts element */
802 ret
= mi_lttng_writer_close_element(writer
);
811 lttng_destroy_handle(handle
);
814 * This means that at least one add_context failed and tells the user to
815 * look on stderr for error(s).
824 void destroy_ctx_type(struct ctx_type
*type
)
830 free(type
->opt
->symbol
);
837 struct ctx_type
*create_ctx_type(void)
839 struct ctx_type
*type
= zmalloc(sizeof(*type
));
842 PERROR("malloc ctx_type");
846 type
->opt
= zmalloc(sizeof(*type
->opt
));
848 PERROR("malloc ctx_type options");
849 destroy_ctx_type(type
);
858 int find_ctx_type_perf_raw(const char *ctx
, struct ctx_type
*type
)
862 char *tmp_list
, *cur_list
;
864 cur_list
= tmp_list
= strdup(ctx
);
866 PERROR("strdup temp list");
871 /* Looking for "perf:[cpu|thread]:raw:<mask>:<name>". */
875 next
= strtok(cur_list
, ":");
882 if (strncmp(next
, "perf", 4) != 0) {
888 if (strncmp(next
, "cpu", 3) == 0) {
889 type
->opt
->ctx_type
= CONTEXT_PERF_CPU_COUNTER
;
890 } else if (strncmp(next
, "thread", 4) == 0) {
891 type
->opt
->ctx_type
= CONTEXT_PERF_THREAD_COUNTER
;
898 if (strncmp(next
, "raw", 3) != 0) {
907 if (strlen(next
) < 2 || next
[0] != 'r') {
908 ERR("Wrong perf raw mask format: expected rNNN");
913 type
->opt
->u
.perf
.config
= strtoll(next
+ 1, &endptr
, 16);
914 if (errno
!= 0 || !endptr
|| *endptr
) {
915 ERR("Wrong perf raw mask format: expected rNNN");
925 ERR("Too many ':' in perf raw format");
933 ERR("Invalid perf counter specifier, expected a specifier of "
934 "the form perf:cpu:raw:rNNN:<name> or "
935 "perf:thread:raw:rNNN:<name>");
949 struct ctx_type
*get_context_type(const char *ctx
)
952 struct ctx_type
*type
= NULL
;
953 const char app_ctx_prefix
[] = "$app.";
954 char *provider_name
= NULL
, *ctx_name
= NULL
;
955 size_t i
, len
, colon_pos
= 0, provider_name_len
, ctx_name_len
;
961 type
= create_ctx_type();
966 /* Check if ctx matches a known static context. */
967 opt_index
= find_ctx_type_idx(ctx
);
968 if (opt_index
>= 0) {
969 *type
->opt
= ctx_opts
[opt_index
];
970 type
->opt
->symbol
= strdup(ctx_opts
[opt_index
].symbol
);
974 /* Check if ctx is a raw perf context. */
975 ret
= find_ctx_type_perf_raw(ctx
, type
);
977 type
->opt
->u
.perf
.type
= PERF_TYPE_RAW
;
978 type
->opt
->symbol
= strdup(ctx
);
979 if (!type
->opt
->symbol
) {
980 PERROR("Copy perf field name");
987 * No match found against static contexts; check if it is an app
991 if (len
<= sizeof(app_ctx_prefix
) - 1) {
995 /* String starts with $app. */
996 if (strncmp(ctx
, app_ctx_prefix
, sizeof(app_ctx_prefix
) - 1)) {
1000 /* Validate that the ':' separator is present. */
1001 for (i
= sizeof(app_ctx_prefix
); i
< len
; i
++) {
1002 const char c
= ctx
[i
];
1011 * No colon found or no ctx name ("$app.provider:") or no provider name
1012 * given ("$app.:..."), which is invalid.
1014 if (!colon_pos
|| colon_pos
== len
||
1015 colon_pos
== sizeof(app_ctx_prefix
)) {
1016 ERR("Invalid application context provided: no provider or context name provided.");
1020 provider_name_len
= colon_pos
- sizeof(app_ctx_prefix
) + 2;
1021 provider_name
= zmalloc(provider_name_len
);
1022 if (!provider_name
) {
1023 PERROR("malloc provider_name");
1026 strncpy(provider_name
, ctx
+ sizeof(app_ctx_prefix
) - 1,
1027 provider_name_len
- 1);
1028 type
->opt
->u
.app_ctx
.provider_name
= provider_name
;
1030 ctx_name_len
= len
- colon_pos
;
1031 ctx_name
= zmalloc(ctx_name_len
);
1033 PERROR("malloc ctx_name");
1036 strncpy(ctx_name
, ctx
+ colon_pos
+ 1, ctx_name_len
- 1);
1037 type
->opt
->u
.app_ctx
.ctx_name
= ctx_name
;
1038 type
->opt
->ctx_type
= CONTEXT_APP_CONTEXT
;
1039 type
->opt
->symbol
= strdup(ctx
);
1043 free(provider_name
);
1045 destroy_ctx_type(type
);
1050 * Add context to channel or event.
1052 int cmd_add_context(int argc
, const char **argv
)
1054 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
1055 static poptContext pc
;
1056 struct ctx_type
*type
, *tmptype
;
1057 char *session_name
= NULL
;
1058 const char *leftover
= NULL
;
1065 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1066 poptReadDefaultConfig(pc
, 0);
1068 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1074 ret
= print_ctx_type();
1078 type
= get_context_type(opt_type
);
1080 ERR("Unknown context type %s", opt_type
);
1084 cds_list_add_tail(&type
->list
, &ctx_type_list
.head
);
1096 case OPT_LIST_OPTIONS
:
1097 list_cmd_options(stdout
, long_options
);
1100 ret
= CMD_UNDEFINED
;
1105 leftover
= poptGetArg(pc
);
1107 ERR("Unknown argument: %s", leftover
);
1112 ret
= print_missing_or_multiple_domains(opt_kernel
+ opt_userspace
+
1113 opt_jul
+ opt_log4j
);
1120 ERR("Missing mandatory -t TYPE");
1125 if (!opt_session_name
) {
1126 session_name
= get_session_name();
1127 if (session_name
== NULL
) {
1132 session_name
= opt_session_name
;
1140 command_ret
= add_context(session_name
);
1141 ret
= mi_close(command_ret
);
1147 if (!opt_session_name
) {
1152 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1153 /* Preserve original error code */
1154 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1157 /* Cleanup allocated memory */
1158 cds_list_for_each_entry_safe(type
, tmptype
, &ctx_type_list
.head
, list
) {
1159 destroy_ctx_type(type
);
1162 /* Overwrite ret if an error occurred during add_context() */
1163 ret
= command_ret
? command_ret
: ret
;
1165 poptFreeContext(pc
);