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