Prettify channel listing
[lttng-tools.git] / src / bin / lttng / commands / add_context.c
CommitLineData
d65106b1
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
e9492550 3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
d65106b1 4 *
d14d33bf
AM
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.
d65106b1
DG
8 *
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.
13 *
d14d33bf
AM
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.
d65106b1
DG
17 */
18
6c1c0768 19#define _LGPL_SOURCE
90b9a268 20#include <ctype.h>
d65106b1
DG
21#include <popt.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <unistd.h>
3ecec76a 28#include <assert.h>
d65106b1 29
3301e740
DG
30#include <urcu/list.h>
31
89b72577
JRJ
32#include <common/mi-lttng.h>
33
c399183f 34#include "../command.h"
d65106b1 35
d65106b1 36static char *opt_channel_name;
5440dc42 37static char *opt_session_name;
55cc08a6 38static int opt_kernel;
d65106b1 39static int opt_userspace;
e9492550
JG
40static int opt_jul;
41static int opt_log4j;
d78d6610 42static char *opt_type;
89b72577 43
4fc83d94
PP
44#ifdef LTTNG_EMBED_HELP
45static const char help_msg[] =
46#include <lttng-add-context.1.h>
47;
48#endif
49
d65106b1
DG
50enum {
51 OPT_HELP = 1,
52 OPT_TYPE,
eeac7d46 53 OPT_USERSPACE,
e9492550
JG
54 OPT_JUL,
55 OPT_LOG4J,
679b4943 56 OPT_LIST_OPTIONS,
83cea427 57 OPT_LIST,
d65106b1
DG
58};
59
cd80958d 60static struct lttng_handle *handle;
89b72577 61static struct mi_writer *writer;
cd80958d 62
90b9a268
DG
63/*
64 * Taken from the LTTng ABI
65 */
66enum context_type {
67 CONTEXT_PID = 0,
aa3514e9 68 CONTEXT_PERF_COUNTER = 1, /* Backward compat. */
95da1297 69 CONTEXT_PROCNAME = 2,
90b9a268
DG
70 CONTEXT_PRIO = 3,
71 CONTEXT_NICE = 4,
72 CONTEXT_VPID = 5,
73 CONTEXT_TID = 6,
74 CONTEXT_VTID = 7,
75 CONTEXT_PPID = 8,
76 CONTEXT_VPPID = 9,
9197c5c4 77 CONTEXT_PTHREAD_ID = 10,
54773d68 78 CONTEXT_HOSTNAME = 11,
7c612c2e 79 CONTEXT_IP = 12,
aa3514e9
MD
80 CONTEXT_PERF_CPU_COUNTER = 13,
81 CONTEXT_PERF_THREAD_COUNTER = 14,
e9492550 82 CONTEXT_APP_CONTEXT = 15,
1ae5e83e
JD
83 CONTEXT_INTERRUPTIBLE = 16,
84 CONTEXT_PREEMPTIBLE = 17,
85 CONTEXT_NEED_RESCHEDULE = 18,
86 CONTEXT_MIGRATABLE = 19,
3301e740
DG
87};
88
90b9a268
DG
89/*
90 * Taken from the Perf ABI (all enum perf_*)
91 */
92enum perf_type {
93 PERF_TYPE_HARDWARE = 0,
94 PERF_TYPE_SOFTWARE = 1,
b13d56d7 95 PERF_TYPE_HW_CACHE = 3,
4fe444da 96 PERF_TYPE_RAW = 4,
3301e740
DG
97};
98
90b9a268 99enum perf_count_hard {
b13d56d7
MD
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,
90b9a268
DG
109};
110
111enum 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,
3301e740
DG
121};
122
b13d56d7
MD
123/*
124 * Generalized hardware cache events:
125 *
126 * { L1-D, L1-I, LLC, ITLB, DTLB, BPU } x
127 * { read, write, prefetch } x
128 * { accesses, misses }
129 */
130enum 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,
137
138 PERF_COUNT_HW_CACHE_MAX, /* non-ABI */
139};
140
141enum 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,
145
146 PERF_COUNT_HW_CACHE_OP_MAX, /* non-ABI */
147};
148
149enum perf_hw_cache_op_result_id {
150 PERF_COUNT_HW_CACHE_RESULT_ACCESS = 0,
151 PERF_COUNT_HW_CACHE_RESULT_MISS = 1,
152
153 PERF_COUNT_HW_CACHE_RESULT_MAX, /* non-ABI */
154};
155
d65106b1
DG
156static struct poptOption long_options[] = {
157 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
158 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 159 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
d65106b1 160 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
d65106b1 161 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
d78d6610 162 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
e9492550
JG
163 {"jul", 'j', POPT_ARG_NONE, 0, OPT_JUL, 0, 0},
164 {"log4j", 'l', POPT_ARG_NONE, 0, OPT_LOG4J, 0, 0},
6caa2bcc 165 {"type", 't', POPT_ARG_STRING, &opt_type, OPT_TYPE, 0, 0},
83cea427 166 {"list", 0, POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL},
679b4943 167 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
d65106b1
DG
168 {0, 0, 0, 0, 0, 0, 0}
169};
170
90b9a268 171/*
b13d56d7 172 * Context options
90b9a268 173 */
aa3514e9 174#define PERF_HW(optstr, name, type, hide) \
b13d56d7 175 { \
aa3514e9 176 optstr, type, hide, \
b13d56d7
MD
177 .u.perf = { PERF_TYPE_HARDWARE, PERF_COUNT_HW_##name, },\
178 }
90b9a268 179
aa3514e9 180#define PERF_SW(optstr, name, type, hide) \
b13d56d7 181 { \
aa3514e9 182 optstr, type, hide, \
b13d56d7
MD
183 .u.perf = { PERF_TYPE_SOFTWARE, PERF_COUNT_SW_##name, },\
184 }
90b9a268 185
aa3514e9 186#define _PERF_HW_CACHE(optstr, name, type, op, result, hide) \
b13d56d7 187 { \
aa3514e9 188 optstr, type, hide, \
b13d56d7
MD
189 .u.perf = { \
190 PERF_TYPE_HW_CACHE, \
191 (uint64_t) PERF_COUNT_HW_CACHE_##name \
18829107
MD
192 | ((uint64_t) PERF_COUNT_HW_CACHE_OP_##op << 8) \
193 | ((uint64_t) PERF_COUNT_HW_CACHE_RESULT_##result << 16), \
b13d56d7
MD
194 }, \
195 }
196
aa3514e9
MD
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, \
201 READ, MISS, hide), \
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)
b13d56d7
MD
210
211static
212const struct ctx_opts {
90b9a268 213 char *symbol;
b13d56d7 214 enum context_type ctx_type;
aa3514e9 215 int hide_help; /* Hide from --help */
b13d56d7
MD
216 union {
217 struct {
218 uint32_t type;
219 uint64_t config;
220 } perf;
e9492550
JG
221 struct {
222 char *provider_name;
223 char *ctx_name;
224 } app_ctx;
b13d56d7
MD
225 } u;
226} ctx_opts[] = {
227 { "pid", CONTEXT_PID },
95da1297 228 { "procname", CONTEXT_PROCNAME },
b13d56d7
MD
229 { "prio", CONTEXT_PRIO },
230 { "nice", CONTEXT_NICE },
231 { "vpid", CONTEXT_VPID },
232 { "tid", CONTEXT_TID },
9197c5c4 233 { "pthread_id", CONTEXT_PTHREAD_ID },
b13d56d7
MD
234 { "vtid", CONTEXT_VTID },
235 { "ppid", CONTEXT_PPID },
236 { "vppid", CONTEXT_VPPID },
54773d68 237 { "hostname", CONTEXT_HOSTNAME },
7c612c2e 238 { "ip", CONTEXT_IP },
1ae5e83e
JD
239 { "interruptible", CONTEXT_INTERRUPTIBLE },
240 { "preemptible", CONTEXT_PREEMPTIBLE },
241 { "need_reschedule", CONTEXT_NEED_RESCHEDULE },
242 { "migratable", CONTEXT_MIGRATABLE },
aa3514e9 243
b13d56d7 244 /* Perf options */
aa3514e9
MD
245
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),
273
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),
290
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),
315
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),
343
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),
360
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),
385
386 /*
387 * Perf per-CPU counters, backward compatibilty for names.
388 * Hidden from help listing.
389 */
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),
416
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),
433
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),
458
b13d56d7 459 { NULL, -1 }, /* Closure */
90b9a268
DG
460};
461
aa3514e9
MD
462#undef PERF_HW_CACHE
463#undef _PERF_HW_CACHE
b13d56d7
MD
464#undef PERF_SW
465#undef PERF_HW
466
90b9a268 467/*
b13d56d7 468 * Context type for command line option parsing.
90b9a268 469 */
b13d56d7 470struct ctx_type {
e9492550 471 struct ctx_opts *opt;
b13d56d7 472 struct cds_list_head list;
90b9a268
DG
473};
474
475/*
476 * List of context type. Use to enable multiple context on a single command
477 * line entry.
478 */
479struct ctx_type_list {
480 struct cds_list_head head;
481} ctx_type_list = {
482 .head = CDS_LIST_HEAD_INIT(ctx_type_list.head),
483};
484
90b9a268
DG
485/*
486 * Pretty print context type.
487 */
488static void print_ctx_type(FILE *ofp)
489{
83cea427 490 int i = 0;
90b9a268 491
90b9a268 492 while (ctx_opts[i].symbol != NULL) {
aa3514e9 493 if (!ctx_opts[i].hide_help) {
83cea427 494 fprintf(ofp, "%s\n", ctx_opts[i].symbol);
90b9a268
DG
495 }
496 i++;
497 }
90b9a268
DG
498}
499
90b9a268
DG
500/*
501 * Find context numerical value from string.
e9492550
JG
502 *
503 * Return -1 if not found.
90b9a268
DG
504 */
505static int find_ctx_type_idx(const char *opt)
506{
e9492550 507 int ret, i = 0;
90b9a268
DG
508
509 while (ctx_opts[i].symbol != NULL) {
510 if (strcmp(opt, ctx_opts[i].symbol) == 0) {
511 ret = i;
512 goto end;
513 }
514 i++;
515 }
516
e9492550 517 ret = -1;
90b9a268
DG
518end:
519 return ret;
520}
521
e9492550
JG
522static
523enum lttng_domain_type get_domain(void)
524{
525 if (opt_kernel) {
526 return LTTNG_DOMAIN_KERNEL;
527 } else if (opt_userspace) {
528 return LTTNG_DOMAIN_UST;
529 } else if (opt_jul) {
530 return LTTNG_DOMAIN_JUL;
531 } else if (opt_log4j) {
532 return LTTNG_DOMAIN_LOG4J;
533 } else {
534 assert(0);
535 }
536}
537
90b9a268
DG
538/*
539 * Add context to channel or event.
d65106b1 540 */
cd80958d 541static int add_context(char *session_name)
d65106b1 542{
89b72577 543 int ret = CMD_SUCCESS, warn = 0, success = 0;
7d29a247
DG
544 struct lttng_event_context context;
545 struct lttng_domain dom;
3301e740 546 struct ctx_type *type;
b13d56d7 547 char *ptr;
d65106b1 548
441c16a7
MD
549 memset(&context, 0, sizeof(context));
550 memset(&dom, 0, sizeof(dom));
551
e9492550 552 dom.type = get_domain();
cd80958d
DG
553 handle = lttng_create_handle(session_name, &dom);
554 if (handle == NULL) {
af87c45a 555 ret = CMD_ERROR;
cd80958d
DG
556 goto error;
557 }
558
89b72577
JRJ
559 if (lttng_opt_mi) {
560 /* Open a contexts element */
561 ret = mi_lttng_writer_open_element(writer, config_element_contexts);
562 if (ret) {
563 goto error;
564 }
565 }
566
5eb00805 567 /* Iterate over all the context types given */
3301e740 568 cds_list_for_each_entry(type, &ctx_type_list.head, list) {
6775595e 569 context.ctx = (enum lttng_event_context_type) type->opt->ctx_type;
aa3514e9
MD
570 switch (context.ctx) {
571 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
572 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
573 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
b13d56d7
MD
574 context.u.perf_counter.type = type->opt->u.perf.type;
575 context.u.perf_counter.config = type->opt->u.perf.config;
24546386
MD
576 strncpy(context.u.perf_counter.name, type->opt->symbol,
577 LTTNG_SYMBOL_NAME_LEN);
578 context.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
b13d56d7
MD
579 /* Replace : and - by _ */
580 while ((ptr = strchr(context.u.perf_counter.name, '-')) != NULL) {
581 *ptr = '_';
3301e740 582 }
b13d56d7
MD
583 while ((ptr = strchr(context.u.perf_counter.name, ':')) != NULL) {
584 *ptr = '_';
3301e740 585 }
aa3514e9 586 break;
e9492550
JG
587 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
588 context.u.app_ctx.provider_name =
589 type->opt->u.app_ctx.provider_name;
590 context.u.app_ctx.ctx_name =
591 type->opt->u.app_ctx.ctx_name;
592 break;
aa3514e9
MD
593 default:
594 break;
3301e740 595 }
55cc08a6
DG
596 DBG("Adding context...");
597
89b72577
JRJ
598 if (lttng_opt_mi) {
599 /* We leave context open the update the success of the command */
600 ret = mi_lttng_context(writer, &context, 1);
601 if (ret) {
602 ret = CMD_ERROR;
603 goto error;
604 }
605 }
606
601d5acf 607 ret = lttng_add_context(handle, &context, NULL, opt_channel_name);
55cc08a6 608 if (ret < 0) {
b58471ff 609 ERR("%s: %s", type->opt->symbol, lttng_strerror(ret));
d16c1a4c 610 warn = 1;
89b72577 611 success = 0;
d65106b1 612 } else {
601d5acf 613 if (opt_channel_name) {
b58471ff 614 MSG("%s context %s added to channel %s",
0df4a6dd 615 get_domain_str(dom.type), type->opt->symbol,
b58471ff 616 opt_channel_name);
b58471ff
DG
617 } else {
618 MSG("%s context %s added to all channels",
62a7b8ed 619 get_domain_str(dom.type), type->opt->symbol);
b58471ff 620 }
89b72577
JRJ
621 success = 1;
622 }
623
624 if (lttng_opt_mi) {
625 /* Is the single operation a success ? */
626 ret = mi_lttng_writer_write_element_bool(writer,
627 mi_lttng_element_success, success);
628 if (ret) {
629 ret = CMD_ERROR;
630 goto error;
631 }
632
633 /* Close the context element */
634 ret = mi_lttng_writer_close_element(writer);
635 if (ret) {
636 ret = CMD_ERROR;
637 goto error;
638 }
639 }
640 }
641
642 if (lttng_opt_mi) {
643 /* Close contexts element */
644 ret = mi_lttng_writer_close_element(writer);
645 if (ret) {
646 goto error;
d65106b1 647 }
d65106b1
DG
648 }
649
af87c45a
DG
650 ret = CMD_SUCCESS;
651
d65106b1 652error:
cd80958d
DG
653 lttng_destroy_handle(handle);
654
d16c1a4c
DG
655 /*
656 * This means that at least one add_context failed and tells the user to
657 * look on stderr for error(s).
658 */
89b72577 659 if (!ret && warn) {
d16c1a4c
DG
660 ret = CMD_WARNING;
661 }
d65106b1
DG
662 return ret;
663}
664
e9492550
JG
665static
666void destroy_ctx_type(struct ctx_type *type)
667{
668 if (!type) {
669 return;
670 }
1e19c0f6
JR
671 if (type->opt) {
672 free(type->opt->symbol);
673 }
e9492550
JG
674 free(type->opt);
675 free(type);
676}
677
678static
679struct ctx_type *create_ctx_type(void)
680{
681 struct ctx_type *type = zmalloc(sizeof(*type));
682
683 if (!type) {
684 PERROR("malloc ctx_type");
685 goto end;
686 }
687
688 type->opt = zmalloc(sizeof(*type->opt));
689 if (!type->opt) {
690 PERROR("malloc ctx_type options");
691 destroy_ctx_type(type);
8e32b63b 692 type = NULL;
e9492550
JG
693 goto end;
694 }
695end:
696 return type;
697}
698
4fe444da
JD
699static
700int find_ctx_type_perf_raw(const char *ctx, struct ctx_type *type)
701{
702 int ret;
703 int field_pos = 0;
704 char *tmp_list, *cur_list;
705
706 cur_list = tmp_list = strdup(ctx);
707 if (!tmp_list) {
708 PERROR("strdup temp list");
709 ret = -ENOMEM;
710 goto end;
711 }
712
713 /* Looking for "perf:[cpu|thread]:raw:<mask>:<name>". */
714 for (;;) {
715 char *next;
716
717 next = strtok(cur_list, ":");
718 if (!next) {
719 break;
720 }
721 cur_list = NULL;
722 switch (field_pos) {
723 case 0:
724 if (strncmp(next, "perf", 4) != 0) {
725 ret = -1;
726 goto end;
727 }
728 break;
729 case 1:
730 if (strncmp(next, "cpu", 3) == 0) {
731 type->opt->ctx_type = CONTEXT_PERF_CPU_COUNTER;
732 } else if (strncmp(next, "thread", 4) == 0) {
733 type->opt->ctx_type = CONTEXT_PERF_THREAD_COUNTER;
734 } else {
735 ret = -1;
736 goto end;
737 }
738 break;
739 case 2:
740 if (strncmp(next, "raw", 3) != 0) {
741 ret = -1;
742 goto end;
743 }
744 break;
745 case 3:
746 {
747 char *endptr;
748
749 if (strlen(next) < 2 || next[0] != 'r') {
750 ERR("Wrong perf raw mask format: expected rNNN");
751 ret = -1;
752 goto end;
753 }
754 errno = 0;
755 type->opt->u.perf.config = strtoll(next + 1, &endptr, 16);
756 if (errno != 0 || !endptr || *endptr) {
757 ERR("Wrong perf raw mask format: expected rNNN");
758 ret = -1;
759 goto end;
760 }
761 break;
762 }
763 case 4:
764 /* name */
765 break;
766 case 5:
767 ERR("Too many ':' in perf raw format");
768 ret = -1;
769 goto end;
770 };
771 field_pos++;
772 }
773
774 if (field_pos < 5) {
775 ERR("Invalid perf counter specifier, expected a specifier of "
776 "the form perf:cpu:raw:rNNN:<name> or "
777 "perf:thread:raw:rNNN:<name>");
778 ret = -1;
779 goto end;
780 }
781
782 ret = 0;
783 goto end;
784
785end:
786 free(tmp_list);
787 return ret;
788}
789
e9492550
JG
790static
791struct ctx_type *get_context_type(const char *ctx)
792{
4fe444da 793 int opt_index, ret;
e9492550
JG
794 struct ctx_type *type = NULL;
795 const char app_ctx_prefix[] = "$app.";
796 char *provider_name = NULL, *ctx_name = NULL;
797 size_t i, len, colon_pos = 0, provider_name_len, ctx_name_len;
798
799 if (!ctx) {
800 goto not_found;
801 }
802
803 type = create_ctx_type();
804 if (!type) {
805 goto not_found;
806 }
807
808 /* Check if ctx matches a known static context. */
809 opt_index = find_ctx_type_idx(ctx);
810 if (opt_index >= 0) {
811 *type->opt = ctx_opts[opt_index];
812 type->opt->symbol = strdup(ctx_opts[opt_index].symbol);
813 goto found;
814 }
815
4fe444da
JD
816 /* Check if ctx is a raw perf context. */
817 ret = find_ctx_type_perf_raw(ctx, type);
818 if (ret == 0) {
819 type->opt->u.perf.type = PERF_TYPE_RAW;
820 type->opt->symbol = strdup(ctx);
821 if (!type->opt->symbol) {
822 PERROR("Copy perf field name");
823 goto not_found;
824 }
825 goto found;
826 }
827
e9492550
JG
828 /*
829 * No match found against static contexts; check if it is an app
830 * context.
831 */
832 len = strlen(ctx);
833 if (len <= sizeof(app_ctx_prefix) - 1) {
834 goto not_found;
835 }
836
837 /* String starts with $app. */
838 if (strncmp(ctx, app_ctx_prefix, sizeof(app_ctx_prefix) - 1)) {
839 goto not_found;
840 }
841
842 /* Validate that the ':' separator is present. */
843 for (i = sizeof(app_ctx_prefix); i < len; i++) {
844 const char c = ctx[i];
845
846 if (c == ':') {
847 colon_pos = i;
848 break;
849 }
850 }
851
852 /*
853 * No colon found or no ctx name ("$app.provider:") or no provider name
854 * given ("$app.:..."), which is invalid.
855 */
856 if (!colon_pos || colon_pos == len ||
857 colon_pos == sizeof(app_ctx_prefix)) {
858 ERR("Invalid application context provided: no provider or context name provided.");
859 goto not_found;
860 }
861
862 provider_name_len = colon_pos - sizeof(app_ctx_prefix) + 2;
863 provider_name = zmalloc(provider_name_len);
864 if (!provider_name) {
865 PERROR("malloc provider_name");
866 goto not_found;
867 }
868 strncpy(provider_name, ctx + sizeof(app_ctx_prefix) - 1,
869 provider_name_len - 1);
870 type->opt->u.app_ctx.provider_name = provider_name;
871
872 ctx_name_len = len - colon_pos;
873 ctx_name = zmalloc(ctx_name_len);
874 if (!ctx_name) {
875 PERROR("malloc ctx_name");
876 goto not_found;
877 }
878 strncpy(ctx_name, ctx + colon_pos + 1, ctx_name_len - 1);
879 type->opt->u.app_ctx.ctx_name = ctx_name;
880 type->opt->ctx_type = CONTEXT_APP_CONTEXT;
881 type->opt->symbol = strdup(ctx);
882found:
883 return type;
884not_found:
885 free(provider_name);
886 free(ctx_name);
887 destroy_ctx_type(type);
888 return NULL;
889}
890
d65106b1 891/*
5eb00805 892 * Add context to channel or event.
d65106b1
DG
893 */
894int cmd_add_context(int argc, const char **argv)
895{
e9492550 896 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
89b72577 897 int success = 1;
d65106b1 898 static poptContext pc;
b13d56d7 899 struct ctx_type *type, *tmptype;
cd80958d 900 char *session_name = NULL;
d65106b1 901
636167b6 902 if (argc < 2) {
5eb00805 903 ret = CMD_ERROR;
636167b6
DG
904 goto end;
905 }
906
d65106b1
DG
907 pc = poptGetContext(NULL, argc, argv, long_options, 0);
908 poptReadDefaultConfig(pc, 0);
909
910 while ((opt = poptGetNextOpt(pc)) != -1) {
911 switch (opt) {
912 case OPT_HELP:
4ba92f18 913 SHOW_HELP();
d65106b1 914 goto end;
83cea427
PP
915 case OPT_LIST:
916 print_ctx_type(stdout);
917 goto end;
d65106b1 918 case OPT_TYPE:
e9492550
JG
919 {
920 type = get_context_type(opt_type);
921 if (!type) {
6caa2bcc 922 ERR("Unknown context type %s", opt_type);
5eb00805
TD
923 ret = CMD_FATAL;
924 goto end;
925 }
e9492550 926 cds_list_add_tail(&type->list, &ctx_type_list.head);
d65106b1 927 break;
e9492550 928 }
eeac7d46
MD
929 case OPT_USERSPACE:
930 opt_userspace = 1;
eeac7d46 931 break;
e9492550
JG
932 case OPT_JUL:
933 opt_jul = 1;
934 break;
935 case OPT_LOG4J:
936 opt_log4j = 1;
937 break;
679b4943
SM
938 case OPT_LIST_OPTIONS:
939 list_cmd_options(stdout, long_options);
679b4943 940 goto end;
d65106b1 941 default:
d65106b1
DG
942 ret = CMD_UNDEFINED;
943 goto end;
944 }
945 }
946
e9492550
JG
947 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace +
948 opt_jul + opt_log4j);
3ecec76a
PP
949 if (ret) {
950 ret = CMD_ERROR;
951 goto end;
952 }
953
ae856491
DG
954 if (!opt_type) {
955 ERR("Missing mandatory -t TYPE");
ae856491
DG
956 ret = CMD_ERROR;
957 goto end;
958 }
959
cd80958d
DG
960 if (!opt_session_name) {
961 session_name = get_session_name();
962 if (session_name == NULL) {
5eb00805 963 ret = CMD_ERROR;
cd80958d
DG
964 goto end;
965 }
966 } else {
967 session_name = opt_session_name;
968 }
969
89b72577
JRJ
970 /* Mi check */
971 if (lttng_opt_mi) {
972 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
973 if (!writer) {
974 ret = -LTTNG_ERR_NOMEM;
975 goto end;
976 }
977
978 /* Open command element */
979 ret = mi_lttng_writer_command_open(writer,
980 mi_lttng_element_command_add_context);
981 if (ret) {
982 ret = CMD_ERROR;
983 goto end;
984 }
985
986 /* Open output element */
987 ret = mi_lttng_writer_open_element(writer,
988 mi_lttng_element_command_output);
989 if (ret) {
990 ret = CMD_ERROR;
991 goto end;
992 }
993 }
994
995 command_ret = add_context(session_name);
996 if (command_ret) {
997 success = 0;
998 }
999
1000 /* Mi closing */
1001 if (lttng_opt_mi) {
1002 /* Close output element */
1003 ret = mi_lttng_writer_close_element(writer);
1004 if (ret) {
1005 ret = CMD_ERROR;
1006 goto end;
1007 }
3301e740 1008
89b72577
JRJ
1009 /* Success ? */
1010 ret = mi_lttng_writer_write_element_bool(writer,
1011 mi_lttng_element_command_success, success);
1012 if (ret) {
1013 ret = CMD_ERROR;
1014 goto end;
1015 }
1016
1017 /* Command element close */
1018 ret = mi_lttng_writer_command_close(writer);
1019 if (ret) {
1020 ret = CMD_ERROR;
1021 goto end;
1022 }
1023 }
1024
1025end:
1256f150
DT
1026 if (!opt_session_name) {
1027 free(session_name);
1028 }
1029
89b72577
JRJ
1030 /* Mi clean-up */
1031 if (writer && mi_lttng_writer_destroy(writer)) {
1032 /* Preserve original error code */
1033 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
1034 }
1035
3301e740 1036 /* Cleanup allocated memory */
b13d56d7 1037 cds_list_for_each_entry_safe(type, tmptype, &ctx_type_list.head, list) {
e9492550 1038 destroy_ctx_type(type);
3301e740
DG
1039 }
1040
89b72577
JRJ
1041 /* Overwrite ret if an error occurred during add_context() */
1042 ret = command_ret ? command_ret : ret;
1043
ca1c3607 1044 poptFreeContext(pc);
d65106b1
DG
1045 return ret;
1046}
This page took 0.096488 seconds and 4 git commands to generate.