Fix: add missing semicolons after MSG, DBG, ERR print macros
[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
d65106b1
DG
44enum {
45 OPT_HELP = 1,
46 OPT_TYPE,
eeac7d46 47 OPT_USERSPACE,
e9492550
JG
48 OPT_JUL,
49 OPT_LOG4J,
679b4943 50 OPT_LIST_OPTIONS,
83cea427 51 OPT_LIST,
d65106b1
DG
52};
53
cd80958d 54static struct lttng_handle *handle;
89b72577 55static struct mi_writer *writer;
cd80958d 56
90b9a268
DG
57/*
58 * Taken from the LTTng ABI
59 */
60enum context_type {
61 CONTEXT_PID = 0,
aa3514e9 62 CONTEXT_PERF_COUNTER = 1, /* Backward compat. */
95da1297 63 CONTEXT_PROCNAME = 2,
90b9a268
DG
64 CONTEXT_PRIO = 3,
65 CONTEXT_NICE = 4,
66 CONTEXT_VPID = 5,
67 CONTEXT_TID = 6,
68 CONTEXT_VTID = 7,
69 CONTEXT_PPID = 8,
70 CONTEXT_VPPID = 9,
9197c5c4 71 CONTEXT_PTHREAD_ID = 10,
54773d68 72 CONTEXT_HOSTNAME = 11,
7c612c2e 73 CONTEXT_IP = 12,
aa3514e9
MD
74 CONTEXT_PERF_CPU_COUNTER = 13,
75 CONTEXT_PERF_THREAD_COUNTER = 14,
e9492550 76 CONTEXT_APP_CONTEXT = 15,
1ae5e83e
JD
77 CONTEXT_INTERRUPTIBLE = 16,
78 CONTEXT_PREEMPTIBLE = 17,
79 CONTEXT_NEED_RESCHEDULE = 18,
80 CONTEXT_MIGRATABLE = 19,
3301e740
DG
81};
82
90b9a268
DG
83/*
84 * Taken from the Perf ABI (all enum perf_*)
85 */
86enum perf_type {
87 PERF_TYPE_HARDWARE = 0,
88 PERF_TYPE_SOFTWARE = 1,
b13d56d7 89 PERF_TYPE_HW_CACHE = 3,
3301e740
DG
90};
91
90b9a268 92enum perf_count_hard {
b13d56d7
MD
93 PERF_COUNT_HW_CPU_CYCLES = 0,
94 PERF_COUNT_HW_INSTRUCTIONS = 1,
95 PERF_COUNT_HW_CACHE_REFERENCES = 2,
96 PERF_COUNT_HW_CACHE_MISSES = 3,
97 PERF_COUNT_HW_BRANCH_INSTRUCTIONS = 4,
98 PERF_COUNT_HW_BRANCH_MISSES = 5,
99 PERF_COUNT_HW_BUS_CYCLES = 6,
100 PERF_COUNT_HW_STALLED_CYCLES_FRONTEND = 7,
101 PERF_COUNT_HW_STALLED_CYCLES_BACKEND = 8,
90b9a268
DG
102};
103
104enum perf_count_soft {
105 PERF_COUNT_SW_CPU_CLOCK = 0,
106 PERF_COUNT_SW_TASK_CLOCK = 1,
107 PERF_COUNT_SW_PAGE_FAULTS = 2,
108 PERF_COUNT_SW_CONTEXT_SWITCHES = 3,
109 PERF_COUNT_SW_CPU_MIGRATIONS = 4,
110 PERF_COUNT_SW_PAGE_FAULTS_MIN = 5,
111 PERF_COUNT_SW_PAGE_FAULTS_MAJ = 6,
112 PERF_COUNT_SW_ALIGNMENT_FAULTS = 7,
113 PERF_COUNT_SW_EMULATION_FAULTS = 8,
3301e740
DG
114};
115
b13d56d7
MD
116/*
117 * Generalized hardware cache events:
118 *
119 * { L1-D, L1-I, LLC, ITLB, DTLB, BPU } x
120 * { read, write, prefetch } x
121 * { accesses, misses }
122 */
123enum perf_hw_cache_id {
124 PERF_COUNT_HW_CACHE_L1D = 0,
125 PERF_COUNT_HW_CACHE_L1I = 1,
126 PERF_COUNT_HW_CACHE_LL = 2,
127 PERF_COUNT_HW_CACHE_DTLB = 3,
128 PERF_COUNT_HW_CACHE_ITLB = 4,
129 PERF_COUNT_HW_CACHE_BPU = 5,
130
131 PERF_COUNT_HW_CACHE_MAX, /* non-ABI */
132};
133
134enum perf_hw_cache_op_id {
135 PERF_COUNT_HW_CACHE_OP_READ = 0,
136 PERF_COUNT_HW_CACHE_OP_WRITE = 1,
137 PERF_COUNT_HW_CACHE_OP_PREFETCH = 2,
138
139 PERF_COUNT_HW_CACHE_OP_MAX, /* non-ABI */
140};
141
142enum perf_hw_cache_op_result_id {
143 PERF_COUNT_HW_CACHE_RESULT_ACCESS = 0,
144 PERF_COUNT_HW_CACHE_RESULT_MISS = 1,
145
146 PERF_COUNT_HW_CACHE_RESULT_MAX, /* non-ABI */
147};
148
d65106b1
DG
149static struct poptOption long_options[] = {
150 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
151 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 152 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
d65106b1 153 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
d65106b1 154 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
d78d6610 155 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
e9492550
JG
156 {"jul", 'j', POPT_ARG_NONE, 0, OPT_JUL, 0, 0},
157 {"log4j", 'l', POPT_ARG_NONE, 0, OPT_LOG4J, 0, 0},
6caa2bcc 158 {"type", 't', POPT_ARG_STRING, &opt_type, OPT_TYPE, 0, 0},
83cea427 159 {"list", 0, POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL},
679b4943 160 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
d65106b1
DG
161 {0, 0, 0, 0, 0, 0, 0}
162};
163
90b9a268 164/*
b13d56d7 165 * Context options
90b9a268 166 */
aa3514e9 167#define PERF_HW(optstr, name, type, hide) \
b13d56d7 168 { \
aa3514e9 169 optstr, type, hide, \
b13d56d7
MD
170 .u.perf = { PERF_TYPE_HARDWARE, PERF_COUNT_HW_##name, },\
171 }
90b9a268 172
aa3514e9 173#define PERF_SW(optstr, name, type, hide) \
b13d56d7 174 { \
aa3514e9 175 optstr, type, hide, \
b13d56d7
MD
176 .u.perf = { PERF_TYPE_SOFTWARE, PERF_COUNT_SW_##name, },\
177 }
90b9a268 178
aa3514e9 179#define _PERF_HW_CACHE(optstr, name, type, op, result, hide) \
b13d56d7 180 { \
aa3514e9 181 optstr, type, hide, \
b13d56d7
MD
182 .u.perf = { \
183 PERF_TYPE_HW_CACHE, \
184 (uint64_t) PERF_COUNT_HW_CACHE_##name \
18829107
MD
185 | ((uint64_t) PERF_COUNT_HW_CACHE_OP_##op << 8) \
186 | ((uint64_t) PERF_COUNT_HW_CACHE_RESULT_##result << 16), \
b13d56d7
MD
187 }, \
188 }
189
aa3514e9
MD
190#define PERF_HW_CACHE(optstr, name, type, hide) \
191 _PERF_HW_CACHE(optstr "-loads", name, type, \
192 READ, ACCESS, hide), \
193 _PERF_HW_CACHE(optstr "-load-misses", name, type, \
194 READ, MISS, hide), \
195 _PERF_HW_CACHE(optstr "-stores", name, type, \
196 WRITE, ACCESS, hide), \
197 _PERF_HW_CACHE(optstr "-store-misses", name, type, \
198 WRITE, MISS, hide), \
199 _PERF_HW_CACHE(optstr "-prefetches", name, type, \
200 PREFETCH, ACCESS, hide), \
201 _PERF_HW_CACHE(optstr "-prefetch-misses", name, type, \
202 PREFETCH, MISS, hide)
b13d56d7
MD
203
204static
205const struct ctx_opts {
90b9a268 206 char *symbol;
b13d56d7 207 enum context_type ctx_type;
aa3514e9 208 int hide_help; /* Hide from --help */
b13d56d7
MD
209 union {
210 struct {
211 uint32_t type;
212 uint64_t config;
213 } perf;
e9492550
JG
214 struct {
215 char *provider_name;
216 char *ctx_name;
217 } app_ctx;
b13d56d7
MD
218 } u;
219} ctx_opts[] = {
220 { "pid", CONTEXT_PID },
95da1297 221 { "procname", CONTEXT_PROCNAME },
b13d56d7
MD
222 { "prio", CONTEXT_PRIO },
223 { "nice", CONTEXT_NICE },
224 { "vpid", CONTEXT_VPID },
225 { "tid", CONTEXT_TID },
9197c5c4 226 { "pthread_id", CONTEXT_PTHREAD_ID },
b13d56d7
MD
227 { "vtid", CONTEXT_VTID },
228 { "ppid", CONTEXT_PPID },
229 { "vppid", CONTEXT_VPPID },
54773d68 230 { "hostname", CONTEXT_HOSTNAME },
7c612c2e 231 { "ip", CONTEXT_IP },
1ae5e83e
JD
232 { "interruptible", CONTEXT_INTERRUPTIBLE },
233 { "preemptible", CONTEXT_PREEMPTIBLE },
234 { "need_reschedule", CONTEXT_NEED_RESCHEDULE },
235 { "migratable", CONTEXT_MIGRATABLE },
aa3514e9 236
b13d56d7 237 /* Perf options */
aa3514e9
MD
238
239 /* Perf per-CPU counters */
240 PERF_HW("perf:cpu:cpu-cycles", CPU_CYCLES,
241 CONTEXT_PERF_CPU_COUNTER, 0),
242 PERF_HW("perf:cpu:cycles", CPU_CYCLES,
243 CONTEXT_PERF_CPU_COUNTER, 0),
244 PERF_HW("perf:cpu:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND,
245 CONTEXT_PERF_CPU_COUNTER, 0),
246 PERF_HW("perf:cpu:idle-cycles-frontend", STALLED_CYCLES_FRONTEND,
247 CONTEXT_PERF_CPU_COUNTER, 0),
248 PERF_HW("perf:cpu:stalled-cycles-backend", STALLED_CYCLES_BACKEND,
249 CONTEXT_PERF_CPU_COUNTER, 0),
250 PERF_HW("perf:cpu:idle-cycles-backend", STALLED_CYCLES_BACKEND,
251 CONTEXT_PERF_CPU_COUNTER, 0),
252 PERF_HW("perf:cpu:instructions", INSTRUCTIONS,
253 CONTEXT_PERF_CPU_COUNTER, 0),
254 PERF_HW("perf:cpu:cache-references", CACHE_REFERENCES,
255 CONTEXT_PERF_CPU_COUNTER, 0),
256 PERF_HW("perf:cpu:cache-misses", CACHE_MISSES,
257 CONTEXT_PERF_CPU_COUNTER, 0),
258 PERF_HW("perf:cpu:branch-instructions", BRANCH_INSTRUCTIONS,
259 CONTEXT_PERF_CPU_COUNTER, 0),
260 PERF_HW("perf:cpu:branches", BRANCH_INSTRUCTIONS,
261 CONTEXT_PERF_CPU_COUNTER, 0),
262 PERF_HW("perf:cpu:branch-misses", BRANCH_MISSES,
263 CONTEXT_PERF_CPU_COUNTER, 0),
264 PERF_HW("perf:cpu:bus-cycles", BUS_CYCLES,
265 CONTEXT_PERF_CPU_COUNTER, 0),
266
267 PERF_HW_CACHE("perf:cpu:L1-dcache", L1D,
268 CONTEXT_PERF_CPU_COUNTER, 0),
269 PERF_HW_CACHE("perf:cpu:L1-icache", L1I,
270 CONTEXT_PERF_CPU_COUNTER, 0),
271 PERF_HW_CACHE("perf:cpu:LLC", LL,
272 CONTEXT_PERF_CPU_COUNTER, 0),
273 PERF_HW_CACHE("perf:cpu:dTLB", DTLB,
274 CONTEXT_PERF_CPU_COUNTER, 0),
275 _PERF_HW_CACHE("perf:cpu:iTLB-loads", ITLB,
276 CONTEXT_PERF_CPU_COUNTER, READ, ACCESS, 0),
277 _PERF_HW_CACHE("perf:cpu:iTLB-load-misses", ITLB,
278 CONTEXT_PERF_CPU_COUNTER, READ, MISS, 0),
279 _PERF_HW_CACHE("perf:cpu:branch-loads", BPU,
280 CONTEXT_PERF_CPU_COUNTER, READ, ACCESS, 0),
281 _PERF_HW_CACHE("perf:cpu:branch-load-misses", BPU,
282 CONTEXT_PERF_CPU_COUNTER, READ, MISS, 0),
283
284 PERF_SW("perf:cpu:cpu-clock", CPU_CLOCK,
285 CONTEXT_PERF_CPU_COUNTER, 0),
286 PERF_SW("perf:cpu:task-clock", TASK_CLOCK,
287 CONTEXT_PERF_CPU_COUNTER, 0),
288 PERF_SW("perf:cpu:page-fault", PAGE_FAULTS,
289 CONTEXT_PERF_CPU_COUNTER, 0),
290 PERF_SW("perf:cpu:faults", PAGE_FAULTS,
291 CONTEXT_PERF_CPU_COUNTER, 0),
292 PERF_SW("perf:cpu:major-faults", PAGE_FAULTS_MAJ,
293 CONTEXT_PERF_CPU_COUNTER, 0),
294 PERF_SW("perf:cpu:minor-faults", PAGE_FAULTS_MIN,
295 CONTEXT_PERF_CPU_COUNTER, 0),
296 PERF_SW("perf:cpu:context-switches", CONTEXT_SWITCHES,
297 CONTEXT_PERF_CPU_COUNTER, 0),
298 PERF_SW("perf:cpu:cs", CONTEXT_SWITCHES,
299 CONTEXT_PERF_CPU_COUNTER, 0),
300 PERF_SW("perf:cpu:cpu-migrations", CPU_MIGRATIONS,
301 CONTEXT_PERF_CPU_COUNTER, 0),
302 PERF_SW("perf:cpu:migrations", CPU_MIGRATIONS,
303 CONTEXT_PERF_CPU_COUNTER, 0),
304 PERF_SW("perf:cpu:alignment-faults", ALIGNMENT_FAULTS,
305 CONTEXT_PERF_CPU_COUNTER, 0),
306 PERF_SW("perf:cpu:emulation-faults", EMULATION_FAULTS,
307 CONTEXT_PERF_CPU_COUNTER, 0),
308
309 /* Perf per-thread counters */
310 PERF_HW("perf:thread:cpu-cycles", CPU_CYCLES,
311 CONTEXT_PERF_THREAD_COUNTER, 0),
312 PERF_HW("perf:thread:cycles", CPU_CYCLES,
313 CONTEXT_PERF_THREAD_COUNTER, 0),
314 PERF_HW("perf:thread:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND,
315 CONTEXT_PERF_THREAD_COUNTER, 0),
316 PERF_HW("perf:thread:idle-cycles-frontend", STALLED_CYCLES_FRONTEND,
317 CONTEXT_PERF_THREAD_COUNTER, 0),
318 PERF_HW("perf:thread:stalled-cycles-backend", STALLED_CYCLES_BACKEND,
319 CONTEXT_PERF_THREAD_COUNTER, 0),
320 PERF_HW("perf:thread:idle-cycles-backend", STALLED_CYCLES_BACKEND,
321 CONTEXT_PERF_THREAD_COUNTER, 0),
322 PERF_HW("perf:thread:instructions", INSTRUCTIONS,
323 CONTEXT_PERF_THREAD_COUNTER, 0),
324 PERF_HW("perf:thread:cache-references", CACHE_REFERENCES,
325 CONTEXT_PERF_THREAD_COUNTER, 0),
326 PERF_HW("perf:thread:cache-misses", CACHE_MISSES,
327 CONTEXT_PERF_THREAD_COUNTER, 0),
328 PERF_HW("perf:thread:branch-instructions", BRANCH_INSTRUCTIONS,
329 CONTEXT_PERF_THREAD_COUNTER, 0),
330 PERF_HW("perf:thread:branches", BRANCH_INSTRUCTIONS,
331 CONTEXT_PERF_THREAD_COUNTER, 0),
332 PERF_HW("perf:thread:branch-misses", BRANCH_MISSES,
333 CONTEXT_PERF_THREAD_COUNTER, 0),
334 PERF_HW("perf:thread:bus-cycles", BUS_CYCLES,
335 CONTEXT_PERF_THREAD_COUNTER, 0),
336
337 PERF_HW_CACHE("perf:thread:L1-dcache", L1D,
338 CONTEXT_PERF_THREAD_COUNTER, 0),
339 PERF_HW_CACHE("perf:thread:L1-icache", L1I,
340 CONTEXT_PERF_THREAD_COUNTER, 0),
341 PERF_HW_CACHE("perf:thread:LLC", LL,
342 CONTEXT_PERF_THREAD_COUNTER, 0),
343 PERF_HW_CACHE("perf:thread:dTLB", DTLB,
344 CONTEXT_PERF_THREAD_COUNTER, 0),
345 _PERF_HW_CACHE("perf:thread:iTLB-loads", ITLB,
346 CONTEXT_PERF_THREAD_COUNTER, READ, ACCESS, 0),
347 _PERF_HW_CACHE("perf:thread:iTLB-load-misses", ITLB,
348 CONTEXT_PERF_THREAD_COUNTER, READ, MISS, 0),
349 _PERF_HW_CACHE("perf:thread:branch-loads", BPU,
350 CONTEXT_PERF_THREAD_COUNTER, READ, ACCESS, 0),
351 _PERF_HW_CACHE("perf:thread:branch-load-misses", BPU,
352 CONTEXT_PERF_THREAD_COUNTER, READ, MISS, 0),
353
354 PERF_SW("perf:thread:cpu-clock", CPU_CLOCK,
355 CONTEXT_PERF_THREAD_COUNTER, 0),
356 PERF_SW("perf:thread:task-clock", TASK_CLOCK,
357 CONTEXT_PERF_THREAD_COUNTER, 0),
358 PERF_SW("perf:thread:page-fault", PAGE_FAULTS,
359 CONTEXT_PERF_THREAD_COUNTER, 0),
360 PERF_SW("perf:thread:faults", PAGE_FAULTS,
361 CONTEXT_PERF_THREAD_COUNTER, 0),
362 PERF_SW("perf:thread:major-faults", PAGE_FAULTS_MAJ,
363 CONTEXT_PERF_THREAD_COUNTER, 0),
364 PERF_SW("perf:thread:minor-faults", PAGE_FAULTS_MIN,
365 CONTEXT_PERF_THREAD_COUNTER, 0),
366 PERF_SW("perf:thread:context-switches", CONTEXT_SWITCHES,
367 CONTEXT_PERF_THREAD_COUNTER, 0),
368 PERF_SW("perf:thread:cs", CONTEXT_SWITCHES,
369 CONTEXT_PERF_THREAD_COUNTER, 0),
370 PERF_SW("perf:thread:cpu-migrations", CPU_MIGRATIONS,
371 CONTEXT_PERF_THREAD_COUNTER, 0),
372 PERF_SW("perf:thread:migrations", CPU_MIGRATIONS,
373 CONTEXT_PERF_THREAD_COUNTER, 0),
374 PERF_SW("perf:thread:alignment-faults", ALIGNMENT_FAULTS,
375 CONTEXT_PERF_THREAD_COUNTER, 0),
376 PERF_SW("perf:thread:emulation-faults", EMULATION_FAULTS,
377 CONTEXT_PERF_THREAD_COUNTER, 0),
378
379 /*
380 * Perf per-CPU counters, backward compatibilty for names.
381 * Hidden from help listing.
382 */
383 PERF_HW("perf:cpu-cycles", CPU_CYCLES,
384 CONTEXT_PERF_COUNTER, 1),
385 PERF_HW("perf:cycles", CPU_CYCLES,
386 CONTEXT_PERF_COUNTER, 1),
387 PERF_HW("perf:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND,
388 CONTEXT_PERF_COUNTER, 1),
389 PERF_HW("perf:idle-cycles-frontend", STALLED_CYCLES_FRONTEND,
390 CONTEXT_PERF_COUNTER, 1),
391 PERF_HW("perf:stalled-cycles-backend", STALLED_CYCLES_BACKEND,
392 CONTEXT_PERF_COUNTER, 1),
393 PERF_HW("perf:idle-cycles-backend", STALLED_CYCLES_BACKEND,
394 CONTEXT_PERF_COUNTER, 1),
395 PERF_HW("perf:instructions", INSTRUCTIONS,
396 CONTEXT_PERF_COUNTER, 1),
397 PERF_HW("perf:cache-references", CACHE_REFERENCES,
398 CONTEXT_PERF_COUNTER, 1),
399 PERF_HW("perf:cache-misses", CACHE_MISSES,
400 CONTEXT_PERF_COUNTER, 1),
401 PERF_HW("perf:branch-instructions", BRANCH_INSTRUCTIONS,
402 CONTEXT_PERF_COUNTER, 1),
403 PERF_HW("perf:branches", BRANCH_INSTRUCTIONS,
404 CONTEXT_PERF_COUNTER, 1),
405 PERF_HW("perf:branch-misses", BRANCH_MISSES,
406 CONTEXT_PERF_COUNTER, 1),
407 PERF_HW("perf:bus-cycles", BUS_CYCLES,
408 CONTEXT_PERF_COUNTER, 1),
409
410 PERF_HW_CACHE("perf:L1-dcache", L1D,
411 CONTEXT_PERF_COUNTER, 1),
412 PERF_HW_CACHE("perf:L1-icache", L1I,
413 CONTEXT_PERF_COUNTER, 1),
414 PERF_HW_CACHE("perf:LLC", LL,
415 CONTEXT_PERF_COUNTER, 1),
416 PERF_HW_CACHE("perf:dTLB", DTLB,
417 CONTEXT_PERF_COUNTER, 1),
418 _PERF_HW_CACHE("perf:iTLB-loads", ITLB,
419 CONTEXT_PERF_COUNTER, READ, ACCESS, 1),
420 _PERF_HW_CACHE("perf:iTLB-load-misses", ITLB,
421 CONTEXT_PERF_COUNTER, READ, MISS, 1),
422 _PERF_HW_CACHE("perf:branch-loads", BPU,
423 CONTEXT_PERF_COUNTER, READ, ACCESS, 1),
424 _PERF_HW_CACHE("perf:branch-load-misses", BPU,
425 CONTEXT_PERF_COUNTER, READ, MISS, 1),
426
427 PERF_SW("perf:cpu-clock", CPU_CLOCK,
428 CONTEXT_PERF_COUNTER, 1),
429 PERF_SW("perf:task-clock", TASK_CLOCK,
430 CONTEXT_PERF_COUNTER, 1),
431 PERF_SW("perf:page-fault", PAGE_FAULTS,
432 CONTEXT_PERF_COUNTER, 1),
433 PERF_SW("perf:faults", PAGE_FAULTS,
434 CONTEXT_PERF_COUNTER, 1),
435 PERF_SW("perf:major-faults", PAGE_FAULTS_MAJ,
436 CONTEXT_PERF_COUNTER, 1),
437 PERF_SW("perf:minor-faults", PAGE_FAULTS_MIN,
438 CONTEXT_PERF_COUNTER, 1),
439 PERF_SW("perf:context-switches", CONTEXT_SWITCHES,
440 CONTEXT_PERF_COUNTER, 1),
441 PERF_SW("perf:cs", CONTEXT_SWITCHES,
442 CONTEXT_PERF_COUNTER, 1),
443 PERF_SW("perf:cpu-migrations", CPU_MIGRATIONS,
444 CONTEXT_PERF_COUNTER, 1),
445 PERF_SW("perf:migrations", CPU_MIGRATIONS,
446 CONTEXT_PERF_COUNTER, 1),
447 PERF_SW("perf:alignment-faults", ALIGNMENT_FAULTS,
448 CONTEXT_PERF_COUNTER, 1),
449 PERF_SW("perf:emulation-faults", EMULATION_FAULTS,
450 CONTEXT_PERF_COUNTER, 1),
451
b13d56d7 452 { NULL, -1 }, /* Closure */
90b9a268
DG
453};
454
aa3514e9
MD
455#undef PERF_HW_CACHE
456#undef _PERF_HW_CACHE
b13d56d7
MD
457#undef PERF_SW
458#undef PERF_HW
459
90b9a268 460/*
b13d56d7 461 * Context type for command line option parsing.
90b9a268 462 */
b13d56d7 463struct ctx_type {
e9492550 464 struct ctx_opts *opt;
b13d56d7 465 struct cds_list_head list;
90b9a268
DG
466};
467
468/*
469 * List of context type. Use to enable multiple context on a single command
470 * line entry.
471 */
472struct ctx_type_list {
473 struct cds_list_head head;
474} ctx_type_list = {
475 .head = CDS_LIST_HEAD_INIT(ctx_type_list.head),
476};
477
90b9a268
DG
478/*
479 * Pretty print context type.
480 */
481static void print_ctx_type(FILE *ofp)
482{
83cea427 483 int i = 0;
90b9a268 484
90b9a268 485 while (ctx_opts[i].symbol != NULL) {
aa3514e9 486 if (!ctx_opts[i].hide_help) {
83cea427 487 fprintf(ofp, "%s\n", ctx_opts[i].symbol);
90b9a268
DG
488 }
489 i++;
490 }
90b9a268
DG
491}
492
90b9a268
DG
493/*
494 * Find context numerical value from string.
e9492550
JG
495 *
496 * Return -1 if not found.
90b9a268
DG
497 */
498static int find_ctx_type_idx(const char *opt)
499{
e9492550 500 int ret, i = 0;
90b9a268
DG
501
502 while (ctx_opts[i].symbol != NULL) {
503 if (strcmp(opt, ctx_opts[i].symbol) == 0) {
504 ret = i;
505 goto end;
506 }
507 i++;
508 }
509
e9492550 510 ret = -1;
90b9a268
DG
511end:
512 return ret;
513}
514
e9492550
JG
515static
516enum lttng_domain_type get_domain(void)
517{
518 if (opt_kernel) {
519 return LTTNG_DOMAIN_KERNEL;
520 } else if (opt_userspace) {
521 return LTTNG_DOMAIN_UST;
522 } else if (opt_jul) {
523 return LTTNG_DOMAIN_JUL;
524 } else if (opt_log4j) {
525 return LTTNG_DOMAIN_LOG4J;
526 } else {
527 assert(0);
528 }
529}
530
90b9a268
DG
531/*
532 * Add context to channel or event.
d65106b1 533 */
cd80958d 534static int add_context(char *session_name)
d65106b1 535{
89b72577 536 int ret = CMD_SUCCESS, warn = 0, success = 0;
7d29a247
DG
537 struct lttng_event_context context;
538 struct lttng_domain dom;
3301e740 539 struct ctx_type *type;
b13d56d7 540 char *ptr;
d65106b1 541
441c16a7
MD
542 memset(&context, 0, sizeof(context));
543 memset(&dom, 0, sizeof(dom));
544
e9492550 545 dom.type = get_domain();
cd80958d
DG
546 handle = lttng_create_handle(session_name, &dom);
547 if (handle == NULL) {
af87c45a 548 ret = CMD_ERROR;
cd80958d
DG
549 goto error;
550 }
551
89b72577
JRJ
552 if (lttng_opt_mi) {
553 /* Open a contexts element */
554 ret = mi_lttng_writer_open_element(writer, config_element_contexts);
555 if (ret) {
556 goto error;
557 }
558 }
559
5eb00805 560 /* Iterate over all the context types given */
3301e740 561 cds_list_for_each_entry(type, &ctx_type_list.head, list) {
6775595e 562 context.ctx = (enum lttng_event_context_type) type->opt->ctx_type;
aa3514e9
MD
563 switch (context.ctx) {
564 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
565 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
566 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
b13d56d7
MD
567 context.u.perf_counter.type = type->opt->u.perf.type;
568 context.u.perf_counter.config = type->opt->u.perf.config;
24546386
MD
569 strncpy(context.u.perf_counter.name, type->opt->symbol,
570 LTTNG_SYMBOL_NAME_LEN);
571 context.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
b13d56d7
MD
572 /* Replace : and - by _ */
573 while ((ptr = strchr(context.u.perf_counter.name, '-')) != NULL) {
574 *ptr = '_';
3301e740 575 }
b13d56d7
MD
576 while ((ptr = strchr(context.u.perf_counter.name, ':')) != NULL) {
577 *ptr = '_';
3301e740 578 }
aa3514e9 579 break;
e9492550
JG
580 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
581 context.u.app_ctx.provider_name =
582 type->opt->u.app_ctx.provider_name;
583 context.u.app_ctx.ctx_name =
584 type->opt->u.app_ctx.ctx_name;
585 break;
aa3514e9
MD
586 default:
587 break;
3301e740 588 }
55cc08a6
DG
589 DBG("Adding context...");
590
89b72577
JRJ
591 if (lttng_opt_mi) {
592 /* We leave context open the update the success of the command */
593 ret = mi_lttng_context(writer, &context, 1);
594 if (ret) {
595 ret = CMD_ERROR;
596 goto error;
597 }
598 }
599
601d5acf 600 ret = lttng_add_context(handle, &context, NULL, opt_channel_name);
55cc08a6 601 if (ret < 0) {
b58471ff 602 ERR("%s: %s", type->opt->symbol, lttng_strerror(ret));
d16c1a4c 603 warn = 1;
89b72577 604 success = 0;
d65106b1 605 } else {
601d5acf 606 if (opt_channel_name) {
b58471ff 607 MSG("%s context %s added to channel %s",
0df4a6dd 608 get_domain_str(dom.type), type->opt->symbol,
b58471ff 609 opt_channel_name);
b58471ff
DG
610 } else {
611 MSG("%s context %s added to all channels",
62a7b8ed 612 get_domain_str(dom.type), type->opt->symbol);
b58471ff 613 }
89b72577
JRJ
614 success = 1;
615 }
616
617 if (lttng_opt_mi) {
618 /* Is the single operation a success ? */
619 ret = mi_lttng_writer_write_element_bool(writer,
620 mi_lttng_element_success, success);
621 if (ret) {
622 ret = CMD_ERROR;
623 goto error;
624 }
625
626 /* Close the context element */
627 ret = mi_lttng_writer_close_element(writer);
628 if (ret) {
629 ret = CMD_ERROR;
630 goto error;
631 }
632 }
633 }
634
635 if (lttng_opt_mi) {
636 /* Close contexts element */
637 ret = mi_lttng_writer_close_element(writer);
638 if (ret) {
639 goto error;
d65106b1 640 }
d65106b1
DG
641 }
642
af87c45a
DG
643 ret = CMD_SUCCESS;
644
d65106b1 645error:
cd80958d
DG
646 lttng_destroy_handle(handle);
647
d16c1a4c
DG
648 /*
649 * This means that at least one add_context failed and tells the user to
650 * look on stderr for error(s).
651 */
89b72577 652 if (!ret && warn) {
d16c1a4c
DG
653 ret = CMD_WARNING;
654 }
d65106b1
DG
655 return ret;
656}
657
e9492550
JG
658static
659void destroy_ctx_type(struct ctx_type *type)
660{
661 if (!type) {
662 return;
663 }
664 free(type->opt->symbol);
665 free(type->opt);
666 free(type);
667}
668
669static
670struct ctx_type *create_ctx_type(void)
671{
672 struct ctx_type *type = zmalloc(sizeof(*type));
673
674 if (!type) {
675 PERROR("malloc ctx_type");
676 goto end;
677 }
678
679 type->opt = zmalloc(sizeof(*type->opt));
680 if (!type->opt) {
681 PERROR("malloc ctx_type options");
682 destroy_ctx_type(type);
8e32b63b 683 type = NULL;
e9492550
JG
684 goto end;
685 }
686end:
687 return type;
688}
689
690static
691struct ctx_type *get_context_type(const char *ctx)
692{
693 int opt_index;
694 struct ctx_type *type = NULL;
695 const char app_ctx_prefix[] = "$app.";
696 char *provider_name = NULL, *ctx_name = NULL;
697 size_t i, len, colon_pos = 0, provider_name_len, ctx_name_len;
698
699 if (!ctx) {
700 goto not_found;
701 }
702
703 type = create_ctx_type();
704 if (!type) {
705 goto not_found;
706 }
707
708 /* Check if ctx matches a known static context. */
709 opt_index = find_ctx_type_idx(ctx);
710 if (opt_index >= 0) {
711 *type->opt = ctx_opts[opt_index];
712 type->opt->symbol = strdup(ctx_opts[opt_index].symbol);
713 goto found;
714 }
715
716 /*
717 * No match found against static contexts; check if it is an app
718 * context.
719 */
720 len = strlen(ctx);
721 if (len <= sizeof(app_ctx_prefix) - 1) {
722 goto not_found;
723 }
724
725 /* String starts with $app. */
726 if (strncmp(ctx, app_ctx_prefix, sizeof(app_ctx_prefix) - 1)) {
727 goto not_found;
728 }
729
730 /* Validate that the ':' separator is present. */
731 for (i = sizeof(app_ctx_prefix); i < len; i++) {
732 const char c = ctx[i];
733
734 if (c == ':') {
735 colon_pos = i;
736 break;
737 }
738 }
739
740 /*
741 * No colon found or no ctx name ("$app.provider:") or no provider name
742 * given ("$app.:..."), which is invalid.
743 */
744 if (!colon_pos || colon_pos == len ||
745 colon_pos == sizeof(app_ctx_prefix)) {
746 ERR("Invalid application context provided: no provider or context name provided.");
747 goto not_found;
748 }
749
750 provider_name_len = colon_pos - sizeof(app_ctx_prefix) + 2;
751 provider_name = zmalloc(provider_name_len);
752 if (!provider_name) {
753 PERROR("malloc provider_name");
754 goto not_found;
755 }
756 strncpy(provider_name, ctx + sizeof(app_ctx_prefix) - 1,
757 provider_name_len - 1);
758 type->opt->u.app_ctx.provider_name = provider_name;
759
760 ctx_name_len = len - colon_pos;
761 ctx_name = zmalloc(ctx_name_len);
762 if (!ctx_name) {
763 PERROR("malloc ctx_name");
764 goto not_found;
765 }
766 strncpy(ctx_name, ctx + colon_pos + 1, ctx_name_len - 1);
767 type->opt->u.app_ctx.ctx_name = ctx_name;
768 type->opt->ctx_type = CONTEXT_APP_CONTEXT;
769 type->opt->symbol = strdup(ctx);
770found:
771 return type;
772not_found:
773 free(provider_name);
774 free(ctx_name);
775 destroy_ctx_type(type);
776 return NULL;
777}
778
d65106b1 779/*
5eb00805 780 * Add context to channel or event.
d65106b1
DG
781 */
782int cmd_add_context(int argc, const char **argv)
783{
e9492550 784 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
89b72577 785 int success = 1;
d65106b1 786 static poptContext pc;
b13d56d7 787 struct ctx_type *type, *tmptype;
cd80958d 788 char *session_name = NULL;
d65106b1 789
636167b6 790 if (argc < 2) {
5eb00805 791 ret = CMD_ERROR;
636167b6
DG
792 goto end;
793 }
794
d65106b1
DG
795 pc = poptGetContext(NULL, argc, argv, long_options, 0);
796 poptReadDefaultConfig(pc, 0);
797
798 while ((opt = poptGetNextOpt(pc)) != -1) {
799 switch (opt) {
800 case OPT_HELP:
4ba92f18 801 SHOW_HELP();
d65106b1 802 goto end;
83cea427
PP
803 case OPT_LIST:
804 print_ctx_type(stdout);
805 goto end;
d65106b1 806 case OPT_TYPE:
e9492550
JG
807 {
808 type = get_context_type(opt_type);
809 if (!type) {
6caa2bcc 810 ERR("Unknown context type %s", opt_type);
5eb00805
TD
811 ret = CMD_FATAL;
812 goto end;
813 }
e9492550 814 cds_list_add_tail(&type->list, &ctx_type_list.head);
d65106b1 815 break;
e9492550 816 }
eeac7d46
MD
817 case OPT_USERSPACE:
818 opt_userspace = 1;
eeac7d46 819 break;
e9492550
JG
820 case OPT_JUL:
821 opt_jul = 1;
822 break;
823 case OPT_LOG4J:
824 opt_log4j = 1;
825 break;
679b4943
SM
826 case OPT_LIST_OPTIONS:
827 list_cmd_options(stdout, long_options);
679b4943 828 goto end;
d65106b1 829 default:
d65106b1
DG
830 ret = CMD_UNDEFINED;
831 goto end;
832 }
833 }
834
e9492550
JG
835 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace +
836 opt_jul + opt_log4j);
3ecec76a
PP
837 if (ret) {
838 ret = CMD_ERROR;
839 goto end;
840 }
841
ae856491
DG
842 if (!opt_type) {
843 ERR("Missing mandatory -t TYPE");
ae856491
DG
844 ret = CMD_ERROR;
845 goto end;
846 }
847
cd80958d
DG
848 if (!opt_session_name) {
849 session_name = get_session_name();
850 if (session_name == NULL) {
5eb00805 851 ret = CMD_ERROR;
cd80958d
DG
852 goto end;
853 }
854 } else {
855 session_name = opt_session_name;
856 }
857
89b72577
JRJ
858 /* Mi check */
859 if (lttng_opt_mi) {
860 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
861 if (!writer) {
862 ret = -LTTNG_ERR_NOMEM;
863 goto end;
864 }
865
866 /* Open command element */
867 ret = mi_lttng_writer_command_open(writer,
868 mi_lttng_element_command_add_context);
869 if (ret) {
870 ret = CMD_ERROR;
871 goto end;
872 }
873
874 /* Open output element */
875 ret = mi_lttng_writer_open_element(writer,
876 mi_lttng_element_command_output);
877 if (ret) {
878 ret = CMD_ERROR;
879 goto end;
880 }
881 }
882
883 command_ret = add_context(session_name);
884 if (command_ret) {
885 success = 0;
886 }
887
888 /* Mi closing */
889 if (lttng_opt_mi) {
890 /* Close output element */
891 ret = mi_lttng_writer_close_element(writer);
892 if (ret) {
893 ret = CMD_ERROR;
894 goto end;
895 }
3301e740 896
89b72577
JRJ
897 /* Success ? */
898 ret = mi_lttng_writer_write_element_bool(writer,
899 mi_lttng_element_command_success, success);
900 if (ret) {
901 ret = CMD_ERROR;
902 goto end;
903 }
904
905 /* Command element close */
906 ret = mi_lttng_writer_command_close(writer);
907 if (ret) {
908 ret = CMD_ERROR;
909 goto end;
910 }
911 }
912
913end:
1256f150
DT
914 if (!opt_session_name) {
915 free(session_name);
916 }
917
89b72577
JRJ
918 /* Mi clean-up */
919 if (writer && mi_lttng_writer_destroy(writer)) {
920 /* Preserve original error code */
921 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
922 }
923
3301e740 924 /* Cleanup allocated memory */
b13d56d7 925 cds_list_for_each_entry_safe(type, tmptype, &ctx_type_list.head, list) {
e9492550 926 destroy_ctx_type(type);
3301e740
DG
927 }
928
89b72577
JRJ
929 /* Overwrite ret if an error occurred during add_context() */
930 ret = command_ret ? command_ret : ret;
931
ca1c3607 932 poptFreeContext(pc);
d65106b1
DG
933 return ret;
934}
This page took 0.140257 seconds and 4 git commands to generate.