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