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 | { \ |
aa3514e9 | 187 | 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 | { \ |
aa3514e9 | 193 | 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 | { \ |
aa3514e9 | 199 | 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[] = { | |
238 | { "pid", CONTEXT_PID }, | |
95da1297 | 239 | { "procname", CONTEXT_PROCNAME }, |
b13d56d7 MD |
240 | { "prio", CONTEXT_PRIO }, |
241 | { "nice", CONTEXT_NICE }, | |
242 | { "vpid", CONTEXT_VPID }, | |
243 | { "tid", CONTEXT_TID }, | |
9197c5c4 | 244 | { "pthread_id", CONTEXT_PTHREAD_ID }, |
b13d56d7 MD |
245 | { "vtid", CONTEXT_VTID }, |
246 | { "ppid", CONTEXT_PPID }, | |
247 | { "vppid", CONTEXT_VPPID }, | |
54773d68 | 248 | { "hostname", CONTEXT_HOSTNAME }, |
7c612c2e | 249 | { "ip", CONTEXT_IP }, |
1ae5e83e JD |
250 | { "interruptible", CONTEXT_INTERRUPTIBLE }, |
251 | { "preemptible", CONTEXT_PREEMPTIBLE }, | |
252 | { "need_reschedule", CONTEXT_NEED_RESCHEDULE }, | |
253 | { "migratable", CONTEXT_MIGRATABLE }, | |
373148e9 FG |
254 | { "callstack-kernel", CONTEXT_CALLSTACK_KERNEL }, |
255 | #if HAVE_MODULES_USERSPACE_CALLSTACK_CONTEXT | |
256 | { "callstack-user", CONTEXT_CALLSTACK_USER }, | |
257 | #endif | |
40e14884 MJ |
258 | { "cgroup_ns", CONTEXT_CGROUP_NS }, |
259 | { "ipc_ns", CONTEXT_IPC_NS }, | |
260 | { "mnt_ns", CONTEXT_MNT_NS }, | |
261 | { "net_ns", CONTEXT_NET_NS }, | |
262 | { "pid_ns", CONTEXT_PID_NS }, | |
263 | { "user_ns", CONTEXT_USER_NS }, | |
264 | { "uts_ns", CONTEXT_UTS_NS }, | |
499cbfa1 MJ |
265 | { "uid", CONTEXT_UID }, |
266 | { "euid", CONTEXT_EUID }, | |
267 | { "suid", CONTEXT_SUID }, | |
268 | { "gid", CONTEXT_GID }, | |
269 | { "egid", CONTEXT_EGID }, | |
270 | { "sgid", CONTEXT_SGID }, | |
271 | { "vuid", CONTEXT_VUID }, | |
272 | { "veuid", CONTEXT_VEUID }, | |
273 | { "vsuid", CONTEXT_VSUID }, | |
274 | { "vgid", CONTEXT_VGID }, | |
275 | { "vegid", CONTEXT_VEGID }, | |
276 | { "vsgid", CONTEXT_VSGID }, | |
aa3514e9 | 277 | |
b13d56d7 | 278 | /* Perf options */ |
aa3514e9 MD |
279 | |
280 | /* Perf per-CPU counters */ | |
281 | PERF_HW("perf:cpu:cpu-cycles", CPU_CYCLES, | |
282 | CONTEXT_PERF_CPU_COUNTER, 0), | |
283 | PERF_HW("perf:cpu:cycles", CPU_CYCLES, | |
284 | CONTEXT_PERF_CPU_COUNTER, 0), | |
285 | PERF_HW("perf:cpu:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND, | |
286 | CONTEXT_PERF_CPU_COUNTER, 0), | |
287 | PERF_HW("perf:cpu:idle-cycles-frontend", STALLED_CYCLES_FRONTEND, | |
288 | CONTEXT_PERF_CPU_COUNTER, 0), | |
289 | PERF_HW("perf:cpu:stalled-cycles-backend", STALLED_CYCLES_BACKEND, | |
290 | CONTEXT_PERF_CPU_COUNTER, 0), | |
291 | PERF_HW("perf:cpu:idle-cycles-backend", STALLED_CYCLES_BACKEND, | |
292 | CONTEXT_PERF_CPU_COUNTER, 0), | |
293 | PERF_HW("perf:cpu:instructions", INSTRUCTIONS, | |
294 | CONTEXT_PERF_CPU_COUNTER, 0), | |
295 | PERF_HW("perf:cpu:cache-references", CACHE_REFERENCES, | |
296 | CONTEXT_PERF_CPU_COUNTER, 0), | |
297 | PERF_HW("perf:cpu:cache-misses", CACHE_MISSES, | |
298 | CONTEXT_PERF_CPU_COUNTER, 0), | |
299 | PERF_HW("perf:cpu:branch-instructions", BRANCH_INSTRUCTIONS, | |
300 | CONTEXT_PERF_CPU_COUNTER, 0), | |
301 | PERF_HW("perf:cpu:branches", BRANCH_INSTRUCTIONS, | |
302 | CONTEXT_PERF_CPU_COUNTER, 0), | |
303 | PERF_HW("perf:cpu:branch-misses", BRANCH_MISSES, | |
304 | CONTEXT_PERF_CPU_COUNTER, 0), | |
305 | PERF_HW("perf:cpu:bus-cycles", BUS_CYCLES, | |
306 | CONTEXT_PERF_CPU_COUNTER, 0), | |
307 | ||
308 | PERF_HW_CACHE("perf:cpu:L1-dcache", L1D, | |
309 | CONTEXT_PERF_CPU_COUNTER, 0), | |
310 | PERF_HW_CACHE("perf:cpu:L1-icache", L1I, | |
311 | CONTEXT_PERF_CPU_COUNTER, 0), | |
312 | PERF_HW_CACHE("perf:cpu:LLC", LL, | |
313 | CONTEXT_PERF_CPU_COUNTER, 0), | |
314 | PERF_HW_CACHE("perf:cpu:dTLB", DTLB, | |
315 | CONTEXT_PERF_CPU_COUNTER, 0), | |
316 | _PERF_HW_CACHE("perf:cpu:iTLB-loads", ITLB, | |
317 | CONTEXT_PERF_CPU_COUNTER, READ, ACCESS, 0), | |
318 | _PERF_HW_CACHE("perf:cpu:iTLB-load-misses", ITLB, | |
319 | CONTEXT_PERF_CPU_COUNTER, READ, MISS, 0), | |
320 | _PERF_HW_CACHE("perf:cpu:branch-loads", BPU, | |
321 | CONTEXT_PERF_CPU_COUNTER, READ, ACCESS, 0), | |
322 | _PERF_HW_CACHE("perf:cpu:branch-load-misses", BPU, | |
323 | CONTEXT_PERF_CPU_COUNTER, READ, MISS, 0), | |
324 | ||
325 | PERF_SW("perf:cpu:cpu-clock", CPU_CLOCK, | |
326 | CONTEXT_PERF_CPU_COUNTER, 0), | |
327 | PERF_SW("perf:cpu:task-clock", TASK_CLOCK, | |
328 | CONTEXT_PERF_CPU_COUNTER, 0), | |
329 | PERF_SW("perf:cpu:page-fault", PAGE_FAULTS, | |
330 | CONTEXT_PERF_CPU_COUNTER, 0), | |
331 | PERF_SW("perf:cpu:faults", PAGE_FAULTS, | |
332 | CONTEXT_PERF_CPU_COUNTER, 0), | |
333 | PERF_SW("perf:cpu:major-faults", PAGE_FAULTS_MAJ, | |
334 | CONTEXT_PERF_CPU_COUNTER, 0), | |
335 | PERF_SW("perf:cpu:minor-faults", PAGE_FAULTS_MIN, | |
336 | CONTEXT_PERF_CPU_COUNTER, 0), | |
337 | PERF_SW("perf:cpu:context-switches", CONTEXT_SWITCHES, | |
338 | CONTEXT_PERF_CPU_COUNTER, 0), | |
339 | PERF_SW("perf:cpu:cs", CONTEXT_SWITCHES, | |
340 | CONTEXT_PERF_CPU_COUNTER, 0), | |
341 | PERF_SW("perf:cpu:cpu-migrations", CPU_MIGRATIONS, | |
342 | CONTEXT_PERF_CPU_COUNTER, 0), | |
343 | PERF_SW("perf:cpu:migrations", CPU_MIGRATIONS, | |
344 | CONTEXT_PERF_CPU_COUNTER, 0), | |
345 | PERF_SW("perf:cpu:alignment-faults", ALIGNMENT_FAULTS, | |
346 | CONTEXT_PERF_CPU_COUNTER, 0), | |
347 | PERF_SW("perf:cpu:emulation-faults", EMULATION_FAULTS, | |
348 | CONTEXT_PERF_CPU_COUNTER, 0), | |
349 | ||
350 | /* Perf per-thread counters */ | |
351 | PERF_HW("perf:thread:cpu-cycles", CPU_CYCLES, | |
352 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
353 | PERF_HW("perf:thread:cycles", CPU_CYCLES, | |
354 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
355 | PERF_HW("perf:thread:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND, | |
356 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
357 | PERF_HW("perf:thread:idle-cycles-frontend", STALLED_CYCLES_FRONTEND, | |
358 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
359 | PERF_HW("perf:thread:stalled-cycles-backend", STALLED_CYCLES_BACKEND, | |
360 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
361 | PERF_HW("perf:thread:idle-cycles-backend", STALLED_CYCLES_BACKEND, | |
362 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
363 | PERF_HW("perf:thread:instructions", INSTRUCTIONS, | |
364 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
365 | PERF_HW("perf:thread:cache-references", CACHE_REFERENCES, | |
366 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
367 | PERF_HW("perf:thread:cache-misses", CACHE_MISSES, | |
368 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
369 | PERF_HW("perf:thread:branch-instructions", BRANCH_INSTRUCTIONS, | |
370 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
371 | PERF_HW("perf:thread:branches", BRANCH_INSTRUCTIONS, | |
372 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
373 | PERF_HW("perf:thread:branch-misses", BRANCH_MISSES, | |
374 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
375 | PERF_HW("perf:thread:bus-cycles", BUS_CYCLES, | |
376 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
377 | ||
378 | PERF_HW_CACHE("perf:thread:L1-dcache", L1D, | |
379 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
380 | PERF_HW_CACHE("perf:thread:L1-icache", L1I, | |
381 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
382 | PERF_HW_CACHE("perf:thread:LLC", LL, | |
383 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
384 | PERF_HW_CACHE("perf:thread:dTLB", DTLB, | |
385 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
386 | _PERF_HW_CACHE("perf:thread:iTLB-loads", ITLB, | |
387 | CONTEXT_PERF_THREAD_COUNTER, READ, ACCESS, 0), | |
388 | _PERF_HW_CACHE("perf:thread:iTLB-load-misses", ITLB, | |
389 | CONTEXT_PERF_THREAD_COUNTER, READ, MISS, 0), | |
390 | _PERF_HW_CACHE("perf:thread:branch-loads", BPU, | |
391 | CONTEXT_PERF_THREAD_COUNTER, READ, ACCESS, 0), | |
392 | _PERF_HW_CACHE("perf:thread:branch-load-misses", BPU, | |
393 | CONTEXT_PERF_THREAD_COUNTER, READ, MISS, 0), | |
394 | ||
395 | PERF_SW("perf:thread:cpu-clock", CPU_CLOCK, | |
396 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
397 | PERF_SW("perf:thread:task-clock", TASK_CLOCK, | |
398 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
399 | PERF_SW("perf:thread:page-fault", PAGE_FAULTS, | |
400 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
401 | PERF_SW("perf:thread:faults", PAGE_FAULTS, | |
402 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
403 | PERF_SW("perf:thread:major-faults", PAGE_FAULTS_MAJ, | |
404 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
405 | PERF_SW("perf:thread:minor-faults", PAGE_FAULTS_MIN, | |
406 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
407 | PERF_SW("perf:thread:context-switches", CONTEXT_SWITCHES, | |
408 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
409 | PERF_SW("perf:thread:cs", CONTEXT_SWITCHES, | |
410 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
411 | PERF_SW("perf:thread:cpu-migrations", CPU_MIGRATIONS, | |
412 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
413 | PERF_SW("perf:thread:migrations", CPU_MIGRATIONS, | |
414 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
415 | PERF_SW("perf:thread:alignment-faults", ALIGNMENT_FAULTS, | |
416 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
417 | PERF_SW("perf:thread:emulation-faults", EMULATION_FAULTS, | |
418 | CONTEXT_PERF_THREAD_COUNTER, 0), | |
419 | ||
420 | /* | |
421 | * Perf per-CPU counters, backward compatibilty for names. | |
422 | * Hidden from help listing. | |
423 | */ | |
424 | PERF_HW("perf:cpu-cycles", CPU_CYCLES, | |
425 | CONTEXT_PERF_COUNTER, 1), | |
426 | PERF_HW("perf:cycles", CPU_CYCLES, | |
427 | CONTEXT_PERF_COUNTER, 1), | |
428 | PERF_HW("perf:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND, | |
429 | CONTEXT_PERF_COUNTER, 1), | |
430 | PERF_HW("perf:idle-cycles-frontend", STALLED_CYCLES_FRONTEND, | |
431 | CONTEXT_PERF_COUNTER, 1), | |
432 | PERF_HW("perf:stalled-cycles-backend", STALLED_CYCLES_BACKEND, | |
433 | CONTEXT_PERF_COUNTER, 1), | |
434 | PERF_HW("perf:idle-cycles-backend", STALLED_CYCLES_BACKEND, | |
435 | CONTEXT_PERF_COUNTER, 1), | |
436 | PERF_HW("perf:instructions", INSTRUCTIONS, | |
437 | CONTEXT_PERF_COUNTER, 1), | |
438 | PERF_HW("perf:cache-references", CACHE_REFERENCES, | |
439 | CONTEXT_PERF_COUNTER, 1), | |
440 | PERF_HW("perf:cache-misses", CACHE_MISSES, | |
441 | CONTEXT_PERF_COUNTER, 1), | |
442 | PERF_HW("perf:branch-instructions", BRANCH_INSTRUCTIONS, | |
443 | CONTEXT_PERF_COUNTER, 1), | |
444 | PERF_HW("perf:branches", BRANCH_INSTRUCTIONS, | |
445 | CONTEXT_PERF_COUNTER, 1), | |
446 | PERF_HW("perf:branch-misses", BRANCH_MISSES, | |
447 | CONTEXT_PERF_COUNTER, 1), | |
448 | PERF_HW("perf:bus-cycles", BUS_CYCLES, | |
449 | CONTEXT_PERF_COUNTER, 1), | |
450 | ||
451 | PERF_HW_CACHE("perf:L1-dcache", L1D, | |
452 | CONTEXT_PERF_COUNTER, 1), | |
453 | PERF_HW_CACHE("perf:L1-icache", L1I, | |
454 | CONTEXT_PERF_COUNTER, 1), | |
455 | PERF_HW_CACHE("perf:LLC", LL, | |
456 | CONTEXT_PERF_COUNTER, 1), | |
457 | PERF_HW_CACHE("perf:dTLB", DTLB, | |
458 | CONTEXT_PERF_COUNTER, 1), | |
459 | _PERF_HW_CACHE("perf:iTLB-loads", ITLB, | |
460 | CONTEXT_PERF_COUNTER, READ, ACCESS, 1), | |
461 | _PERF_HW_CACHE("perf:iTLB-load-misses", ITLB, | |
462 | CONTEXT_PERF_COUNTER, READ, MISS, 1), | |
463 | _PERF_HW_CACHE("perf:branch-loads", BPU, | |
464 | CONTEXT_PERF_COUNTER, READ, ACCESS, 1), | |
465 | _PERF_HW_CACHE("perf:branch-load-misses", BPU, | |
466 | CONTEXT_PERF_COUNTER, READ, MISS, 1), | |
467 | ||
468 | PERF_SW("perf:cpu-clock", CPU_CLOCK, | |
469 | CONTEXT_PERF_COUNTER, 1), | |
470 | PERF_SW("perf:task-clock", TASK_CLOCK, | |
471 | CONTEXT_PERF_COUNTER, 1), | |
472 | PERF_SW("perf:page-fault", PAGE_FAULTS, | |
473 | CONTEXT_PERF_COUNTER, 1), | |
474 | PERF_SW("perf:faults", PAGE_FAULTS, | |
475 | CONTEXT_PERF_COUNTER, 1), | |
476 | PERF_SW("perf:major-faults", PAGE_FAULTS_MAJ, | |
477 | CONTEXT_PERF_COUNTER, 1), | |
478 | PERF_SW("perf:minor-faults", PAGE_FAULTS_MIN, | |
479 | CONTEXT_PERF_COUNTER, 1), | |
480 | PERF_SW("perf:context-switches", CONTEXT_SWITCHES, | |
481 | CONTEXT_PERF_COUNTER, 1), | |
482 | PERF_SW("perf:cs", CONTEXT_SWITCHES, | |
483 | CONTEXT_PERF_COUNTER, 1), | |
484 | PERF_SW("perf:cpu-migrations", CPU_MIGRATIONS, | |
485 | CONTEXT_PERF_COUNTER, 1), | |
486 | PERF_SW("perf:migrations", CPU_MIGRATIONS, | |
487 | CONTEXT_PERF_COUNTER, 1), | |
488 | PERF_SW("perf:alignment-faults", ALIGNMENT_FAULTS, | |
489 | CONTEXT_PERF_COUNTER, 1), | |
490 | PERF_SW("perf:emulation-faults", EMULATION_FAULTS, | |
491 | CONTEXT_PERF_COUNTER, 1), | |
492 | ||
b13d56d7 | 493 | { NULL, -1 }, /* Closure */ |
90b9a268 DG |
494 | }; |
495 | ||
aa3514e9 MD |
496 | #undef PERF_HW_CACHE |
497 | #undef _PERF_HW_CACHE | |
b13d56d7 MD |
498 | #undef PERF_SW |
499 | #undef PERF_HW | |
500 | ||
90b9a268 | 501 | /* |
b13d56d7 | 502 | * Context type for command line option parsing. |
90b9a268 | 503 | */ |
b13d56d7 | 504 | struct ctx_type { |
e9492550 | 505 | struct ctx_opts *opt; |
b13d56d7 | 506 | struct cds_list_head list; |
90b9a268 DG |
507 | }; |
508 | ||
509 | /* | |
510 | * List of context type. Use to enable multiple context on a single command | |
511 | * line entry. | |
512 | */ | |
513 | struct ctx_type_list { | |
514 | struct cds_list_head head; | |
515 | } ctx_type_list = { | |
516 | .head = CDS_LIST_HEAD_INIT(ctx_type_list.head), | |
517 | }; | |
518 | ||
90b9a268 | 519 | |
90b9a268 | 520 | |
90b9a268 DG |
521 | /* |
522 | * Find context numerical value from string. | |
e9492550 JG |
523 | * |
524 | * Return -1 if not found. | |
90b9a268 DG |
525 | */ |
526 | static int find_ctx_type_idx(const char *opt) | |
527 | { | |
e9492550 | 528 | int ret, i = 0; |
90b9a268 DG |
529 | |
530 | while (ctx_opts[i].symbol != NULL) { | |
531 | if (strcmp(opt, ctx_opts[i].symbol) == 0) { | |
532 | ret = i; | |
533 | goto end; | |
534 | } | |
535 | i++; | |
536 | } | |
537 | ||
e9492550 | 538 | ret = -1; |
90b9a268 DG |
539 | end: |
540 | return ret; | |
541 | } | |
542 | ||
e9492550 JG |
543 | static |
544 | enum lttng_domain_type get_domain(void) | |
545 | { | |
546 | if (opt_kernel) { | |
547 | return LTTNG_DOMAIN_KERNEL; | |
548 | } else if (opt_userspace) { | |
549 | return LTTNG_DOMAIN_UST; | |
550 | } else if (opt_jul) { | |
551 | return LTTNG_DOMAIN_JUL; | |
552 | } else if (opt_log4j) { | |
553 | return LTTNG_DOMAIN_LOG4J; | |
554 | } else { | |
555 | assert(0); | |
556 | } | |
557 | } | |
558 | ||
59deec0c JR |
559 | static |
560 | int mi_open(void) | |
561 | { | |
562 | int ret; | |
563 | ||
564 | /* MI check */ | |
565 | if (!lttng_opt_mi) { | |
566 | ret = 0; | |
567 | goto end; | |
568 | } | |
569 | ||
570 | ret = fileno(stdout); | |
571 | if (ret < 0) { | |
1e2d9dfa | 572 | PERROR("Unable to retrieve fileno of stdout"); |
59deec0c JR |
573 | ret = CMD_ERROR; |
574 | goto end; | |
575 | } | |
576 | ||
577 | writer = mi_lttng_writer_create(ret, lttng_opt_mi); | |
578 | if (!writer) { | |
579 | ret = CMD_ERROR; | |
580 | goto end; | |
581 | } | |
582 | ||
583 | /* Open command element */ | |
584 | ret = mi_lttng_writer_command_open(writer, | |
585 | mi_lttng_element_command_add_context); | |
586 | if (ret) { | |
587 | ret = CMD_ERROR; | |
588 | goto end; | |
589 | } | |
590 | ||
591 | /* Open output element */ | |
592 | ret = mi_lttng_writer_open_element(writer, | |
593 | mi_lttng_element_command_output); | |
594 | if (ret) { | |
595 | ret = CMD_ERROR; | |
596 | goto end; | |
597 | } | |
598 | end: | |
599 | return ret; | |
600 | } | |
601 | ||
602 | static | |
603 | int mi_close(enum cmd_error_code success) | |
604 | { | |
605 | int ret; | |
606 | ||
607 | /* MI closing */ | |
608 | if (!lttng_opt_mi) { | |
609 | ret = 0; | |
610 | goto end; | |
611 | } | |
612 | /* Close output element */ | |
613 | ret = mi_lttng_writer_close_element(writer); | |
614 | if (ret) { | |
615 | ret = CMD_ERROR; | |
616 | goto end; | |
617 | } | |
618 | ||
619 | /* Success ? */ | |
620 | ret = mi_lttng_writer_write_element_bool(writer, | |
621 | mi_lttng_element_command_success, !success); | |
622 | if (ret) { | |
623 | ret = CMD_ERROR; | |
624 | goto end; | |
625 | } | |
626 | ||
627 | /* Command element close */ | |
628 | ret = mi_lttng_writer_command_close(writer); | |
629 | if (ret) { | |
630 | ret = CMD_ERROR; | |
631 | goto end; | |
632 | } | |
633 | end: | |
634 | return ret; | |
635 | } | |
636 | ||
637 | static | |
638 | void populate_context(struct lttng_event_context *context, | |
639 | const struct ctx_opts *opt) | |
640 | { | |
641 | char *ptr; | |
642 | ||
643 | context->ctx = (enum lttng_event_context_type) opt->ctx_type; | |
644 | switch (context->ctx) { | |
645 | case LTTNG_EVENT_CONTEXT_PERF_COUNTER: | |
646 | case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER: | |
647 | case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER: | |
648 | context->u.perf_counter.type = opt->u.perf.type; | |
649 | context->u.perf_counter.config = opt->u.perf.config; | |
650 | strncpy(context->u.perf_counter.name, opt->symbol, | |
651 | LTTNG_SYMBOL_NAME_LEN); | |
652 | context->u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
653 | /* Replace : and - by _ */ | |
654 | while ((ptr = strchr(context->u.perf_counter.name, '-')) != NULL) { | |
655 | *ptr = '_'; | |
656 | } | |
657 | while ((ptr = strchr(context->u.perf_counter.name, ':')) != NULL) { | |
658 | *ptr = '_'; | |
659 | } | |
660 | break; | |
661 | case LTTNG_EVENT_CONTEXT_APP_CONTEXT: | |
662 | context->u.app_ctx.provider_name = | |
663 | opt->u.app_ctx.provider_name; | |
664 | context->u.app_ctx.ctx_name = | |
665 | opt->u.app_ctx.ctx_name; | |
666 | break; | |
667 | default: | |
668 | break; | |
669 | } | |
670 | } | |
671 | ||
672 | /* | |
673 | * Pretty print context type. | |
674 | */ | |
675 | static | |
676 | int print_ctx_type(void) | |
677 | { | |
678 | ||
679 | FILE *ofp = stdout; | |
680 | int i = 0; | |
681 | int ret; | |
682 | struct lttng_event_context context; | |
683 | ||
684 | memset(&context, 0, sizeof(context)); | |
685 | ||
686 | ret = mi_open(); | |
687 | if (ret) { | |
688 | ret = CMD_ERROR; | |
689 | goto end; | |
690 | } | |
691 | ||
692 | if (lttng_opt_mi) { | |
693 | /* Open a contexts element */ | |
694 | ret = mi_lttng_writer_open_element(writer, config_element_contexts); | |
695 | if (ret) { | |
696 | ret = CMD_ERROR; | |
697 | goto end; | |
698 | } | |
699 | } | |
700 | ||
701 | while (ctx_opts[i].symbol != NULL) { | |
702 | if (!ctx_opts[i].hide_help) { | |
703 | if (lttng_opt_mi) { | |
704 | populate_context(&context, &ctx_opts[i]); | |
705 | ret = mi_lttng_context(writer, &context, 1); | |
706 | if (ret) { | |
707 | ret = CMD_ERROR; | |
708 | goto end; | |
709 | } | |
710 | ||
711 | ret = mi_lttng_writer_write_element_string( | |
712 | writer, | |
713 | mi_lttng_element_context_symbol, | |
714 | ctx_opts[i].symbol); | |
715 | if (ret) { | |
716 | ret = CMD_ERROR; | |
717 | goto end; | |
718 | } | |
719 | ||
720 | ret = mi_lttng_writer_close_element(writer); | |
721 | if (ret) { | |
722 | ret = CMD_ERROR; | |
723 | goto end; | |
724 | } | |
725 | } else { | |
726 | fprintf(ofp, "%s\n", ctx_opts[i].symbol); | |
727 | } | |
728 | } | |
729 | i++; | |
730 | } | |
731 | ||
732 | if (lttng_opt_mi) { | |
733 | /* Close contexts element */ | |
734 | ret = mi_lttng_writer_close_element(writer); | |
735 | if (ret) { | |
736 | goto end; | |
737 | } | |
738 | } | |
739 | ||
740 | end: | |
741 | ret = mi_close(ret); | |
742 | if (ret) { | |
743 | ret = CMD_ERROR; | |
744 | } | |
745 | return ret; | |
746 | } | |
747 | ||
90b9a268 DG |
748 | /* |
749 | * Add context to channel or event. | |
d65106b1 | 750 | */ |
cd80958d | 751 | static int add_context(char *session_name) |
d65106b1 | 752 | { |
89b72577 | 753 | int ret = CMD_SUCCESS, warn = 0, success = 0; |
7d29a247 DG |
754 | struct lttng_event_context context; |
755 | struct lttng_domain dom; | |
3301e740 | 756 | struct ctx_type *type; |
d65106b1 | 757 | |
441c16a7 MD |
758 | memset(&context, 0, sizeof(context)); |
759 | memset(&dom, 0, sizeof(dom)); | |
760 | ||
e9492550 | 761 | dom.type = get_domain(); |
cd80958d DG |
762 | handle = lttng_create_handle(session_name, &dom); |
763 | if (handle == NULL) { | |
af87c45a | 764 | ret = CMD_ERROR; |
cd80958d DG |
765 | goto error; |
766 | } | |
767 | ||
89b72577 JRJ |
768 | if (lttng_opt_mi) { |
769 | /* Open a contexts element */ | |
770 | ret = mi_lttng_writer_open_element(writer, config_element_contexts); | |
771 | if (ret) { | |
772 | goto error; | |
773 | } | |
774 | } | |
775 | ||
5eb00805 | 776 | /* Iterate over all the context types given */ |
3301e740 | 777 | cds_list_for_each_entry(type, &ctx_type_list.head, list) { |
55cc08a6 DG |
778 | DBG("Adding context..."); |
779 | ||
59deec0c JR |
780 | populate_context(&context, type->opt); |
781 | ||
89b72577 JRJ |
782 | if (lttng_opt_mi) { |
783 | /* We leave context open the update the success of the command */ | |
784 | ret = mi_lttng_context(writer, &context, 1); | |
785 | if (ret) { | |
786 | ret = CMD_ERROR; | |
787 | goto error; | |
788 | } | |
59deec0c JR |
789 | |
790 | ret = mi_lttng_writer_write_element_string(writer, | |
791 | mi_lttng_element_context_symbol, | |
792 | type->opt->symbol); | |
793 | if (ret) { | |
794 | ret = CMD_ERROR; | |
795 | goto error; | |
796 | } | |
89b72577 JRJ |
797 | } |
798 | ||
601d5acf | 799 | ret = lttng_add_context(handle, &context, NULL, opt_channel_name); |
55cc08a6 | 800 | if (ret < 0) { |
b58471ff | 801 | ERR("%s: %s", type->opt->symbol, lttng_strerror(ret)); |
d16c1a4c | 802 | warn = 1; |
89b72577 | 803 | success = 0; |
d65106b1 | 804 | } else { |
601d5acf | 805 | if (opt_channel_name) { |
b58471ff | 806 | MSG("%s context %s added to channel %s", |
0df4a6dd | 807 | get_domain_str(dom.type), type->opt->symbol, |
b58471ff | 808 | opt_channel_name); |
b58471ff DG |
809 | } else { |
810 | MSG("%s context %s added to all channels", | |
62a7b8ed | 811 | get_domain_str(dom.type), type->opt->symbol); |
b58471ff | 812 | } |
89b72577 JRJ |
813 | success = 1; |
814 | } | |
815 | ||
816 | if (lttng_opt_mi) { | |
817 | /* Is the single operation a success ? */ | |
818 | ret = mi_lttng_writer_write_element_bool(writer, | |
819 | mi_lttng_element_success, success); | |
820 | if (ret) { | |
821 | ret = CMD_ERROR; | |
822 | goto error; | |
823 | } | |
824 | ||
825 | /* Close the context element */ | |
826 | ret = mi_lttng_writer_close_element(writer); | |
827 | if (ret) { | |
828 | ret = CMD_ERROR; | |
829 | goto error; | |
830 | } | |
831 | } | |
832 | } | |
833 | ||
834 | if (lttng_opt_mi) { | |
835 | /* Close contexts element */ | |
836 | ret = mi_lttng_writer_close_element(writer); | |
837 | if (ret) { | |
838 | goto error; | |
d65106b1 | 839 | } |
d65106b1 DG |
840 | } |
841 | ||
af87c45a DG |
842 | ret = CMD_SUCCESS; |
843 | ||
d65106b1 | 844 | error: |
cd80958d DG |
845 | lttng_destroy_handle(handle); |
846 | ||
d16c1a4c DG |
847 | /* |
848 | * This means that at least one add_context failed and tells the user to | |
849 | * look on stderr for error(s). | |
850 | */ | |
89b72577 | 851 | if (!ret && warn) { |
d16c1a4c DG |
852 | ret = CMD_WARNING; |
853 | } | |
d65106b1 DG |
854 | return ret; |
855 | } | |
856 | ||
e9492550 JG |
857 | static |
858 | void destroy_ctx_type(struct ctx_type *type) | |
859 | { | |
860 | if (!type) { | |
861 | return; | |
862 | } | |
1e19c0f6 JR |
863 | if (type->opt) { |
864 | free(type->opt->symbol); | |
865 | } | |
e9492550 JG |
866 | free(type->opt); |
867 | free(type); | |
868 | } | |
869 | ||
870 | static | |
871 | struct ctx_type *create_ctx_type(void) | |
872 | { | |
873 | struct ctx_type *type = zmalloc(sizeof(*type)); | |
874 | ||
875 | if (!type) { | |
876 | PERROR("malloc ctx_type"); | |
877 | goto end; | |
878 | } | |
879 | ||
880 | type->opt = zmalloc(sizeof(*type->opt)); | |
881 | if (!type->opt) { | |
882 | PERROR("malloc ctx_type options"); | |
883 | destroy_ctx_type(type); | |
8e32b63b | 884 | type = NULL; |
e9492550 JG |
885 | goto end; |
886 | } | |
887 | end: | |
888 | return type; | |
889 | } | |
890 | ||
4fe444da JD |
891 | static |
892 | int find_ctx_type_perf_raw(const char *ctx, struct ctx_type *type) | |
893 | { | |
894 | int ret; | |
895 | int field_pos = 0; | |
896 | char *tmp_list, *cur_list; | |
897 | ||
898 | cur_list = tmp_list = strdup(ctx); | |
899 | if (!tmp_list) { | |
900 | PERROR("strdup temp list"); | |
901 | ret = -ENOMEM; | |
902 | goto end; | |
903 | } | |
904 | ||
905 | /* Looking for "perf:[cpu|thread]:raw:<mask>:<name>". */ | |
906 | for (;;) { | |
907 | char *next; | |
908 | ||
909 | next = strtok(cur_list, ":"); | |
910 | if (!next) { | |
911 | break; | |
912 | } | |
913 | cur_list = NULL; | |
914 | switch (field_pos) { | |
915 | case 0: | |
916 | if (strncmp(next, "perf", 4) != 0) { | |
917 | ret = -1; | |
918 | goto end; | |
919 | } | |
920 | break; | |
921 | case 1: | |
922 | if (strncmp(next, "cpu", 3) == 0) { | |
923 | type->opt->ctx_type = CONTEXT_PERF_CPU_COUNTER; | |
924 | } else if (strncmp(next, "thread", 4) == 0) { | |
925 | type->opt->ctx_type = CONTEXT_PERF_THREAD_COUNTER; | |
926 | } else { | |
927 | ret = -1; | |
928 | goto end; | |
929 | } | |
930 | break; | |
931 | case 2: | |
932 | if (strncmp(next, "raw", 3) != 0) { | |
933 | ret = -1; | |
934 | goto end; | |
935 | } | |
936 | break; | |
937 | case 3: | |
938 | { | |
939 | char *endptr; | |
940 | ||
941 | if (strlen(next) < 2 || next[0] != 'r') { | |
942 | ERR("Wrong perf raw mask format: expected rNNN"); | |
943 | ret = -1; | |
944 | goto end; | |
945 | } | |
946 | errno = 0; | |
947 | type->opt->u.perf.config = strtoll(next + 1, &endptr, 16); | |
948 | if (errno != 0 || !endptr || *endptr) { | |
949 | ERR("Wrong perf raw mask format: expected rNNN"); | |
950 | ret = -1; | |
951 | goto end; | |
952 | } | |
953 | break; | |
954 | } | |
955 | case 4: | |
956 | /* name */ | |
957 | break; | |
958 | case 5: | |
959 | ERR("Too many ':' in perf raw format"); | |
960 | ret = -1; | |
961 | goto end; | |
962 | }; | |
963 | field_pos++; | |
964 | } | |
965 | ||
966 | if (field_pos < 5) { | |
967 | ERR("Invalid perf counter specifier, expected a specifier of " | |
968 | "the form perf:cpu:raw:rNNN:<name> or " | |
969 | "perf:thread:raw:rNNN:<name>"); | |
970 | ret = -1; | |
971 | goto end; | |
972 | } | |
973 | ||
974 | ret = 0; | |
975 | goto end; | |
976 | ||
977 | end: | |
978 | free(tmp_list); | |
979 | return ret; | |
980 | } | |
981 | ||
e9492550 JG |
982 | static |
983 | struct ctx_type *get_context_type(const char *ctx) | |
984 | { | |
4fe444da | 985 | int opt_index, ret; |
e9492550 JG |
986 | struct ctx_type *type = NULL; |
987 | const char app_ctx_prefix[] = "$app."; | |
988 | char *provider_name = NULL, *ctx_name = NULL; | |
989 | size_t i, len, colon_pos = 0, provider_name_len, ctx_name_len; | |
990 | ||
991 | if (!ctx) { | |
992 | goto not_found; | |
993 | } | |
994 | ||
995 | type = create_ctx_type(); | |
996 | if (!type) { | |
997 | goto not_found; | |
998 | } | |
999 | ||
1000 | /* Check if ctx matches a known static context. */ | |
1001 | opt_index = find_ctx_type_idx(ctx); | |
1002 | if (opt_index >= 0) { | |
1003 | *type->opt = ctx_opts[opt_index]; | |
1004 | type->opt->symbol = strdup(ctx_opts[opt_index].symbol); | |
1005 | goto found; | |
1006 | } | |
1007 | ||
4fe444da JD |
1008 | /* Check if ctx is a raw perf context. */ |
1009 | ret = find_ctx_type_perf_raw(ctx, type); | |
1010 | if (ret == 0) { | |
1011 | type->opt->u.perf.type = PERF_TYPE_RAW; | |
1012 | type->opt->symbol = strdup(ctx); | |
1013 | if (!type->opt->symbol) { | |
1014 | PERROR("Copy perf field name"); | |
1015 | goto not_found; | |
1016 | } | |
1017 | goto found; | |
1018 | } | |
1019 | ||
e9492550 JG |
1020 | /* |
1021 | * No match found against static contexts; check if it is an app | |
1022 | * context. | |
1023 | */ | |
1024 | len = strlen(ctx); | |
1025 | if (len <= sizeof(app_ctx_prefix) - 1) { | |
1026 | goto not_found; | |
1027 | } | |
1028 | ||
1029 | /* String starts with $app. */ | |
1030 | if (strncmp(ctx, app_ctx_prefix, sizeof(app_ctx_prefix) - 1)) { | |
1031 | goto not_found; | |
1032 | } | |
1033 | ||
1034 | /* Validate that the ':' separator is present. */ | |
1035 | for (i = sizeof(app_ctx_prefix); i < len; i++) { | |
1036 | const char c = ctx[i]; | |
1037 | ||
1038 | if (c == ':') { | |
1039 | colon_pos = i; | |
1040 | break; | |
1041 | } | |
1042 | } | |
1043 | ||
1044 | /* | |
1045 | * No colon found or no ctx name ("$app.provider:") or no provider name | |
1046 | * given ("$app.:..."), which is invalid. | |
1047 | */ | |
1048 | if (!colon_pos || colon_pos == len || | |
1049 | colon_pos == sizeof(app_ctx_prefix)) { | |
1050 | ERR("Invalid application context provided: no provider or context name provided."); | |
1051 | goto not_found; | |
1052 | } | |
1053 | ||
1054 | provider_name_len = colon_pos - sizeof(app_ctx_prefix) + 2; | |
1055 | provider_name = zmalloc(provider_name_len); | |
1056 | if (!provider_name) { | |
1057 | PERROR("malloc provider_name"); | |
1058 | goto not_found; | |
1059 | } | |
1060 | strncpy(provider_name, ctx + sizeof(app_ctx_prefix) - 1, | |
1061 | provider_name_len - 1); | |
1062 | type->opt->u.app_ctx.provider_name = provider_name; | |
1063 | ||
1064 | ctx_name_len = len - colon_pos; | |
1065 | ctx_name = zmalloc(ctx_name_len); | |
1066 | if (!ctx_name) { | |
1067 | PERROR("malloc ctx_name"); | |
1068 | goto not_found; | |
1069 | } | |
1070 | strncpy(ctx_name, ctx + colon_pos + 1, ctx_name_len - 1); | |
1071 | type->opt->u.app_ctx.ctx_name = ctx_name; | |
1072 | type->opt->ctx_type = CONTEXT_APP_CONTEXT; | |
1073 | type->opt->symbol = strdup(ctx); | |
1074 | found: | |
1075 | return type; | |
1076 | not_found: | |
1077 | free(provider_name); | |
1078 | free(ctx_name); | |
1079 | destroy_ctx_type(type); | |
1080 | return NULL; | |
1081 | } | |
1082 | ||
d65106b1 | 1083 | /* |
5eb00805 | 1084 | * Add context to channel or event. |
d65106b1 DG |
1085 | */ |
1086 | int cmd_add_context(int argc, const char **argv) | |
1087 | { | |
e9492550 | 1088 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS; |
d65106b1 | 1089 | static poptContext pc; |
b13d56d7 | 1090 | struct ctx_type *type, *tmptype; |
cd80958d | 1091 | char *session_name = NULL; |
68c7f6e5 | 1092 | const char *leftover = NULL; |
d65106b1 | 1093 | |
636167b6 | 1094 | if (argc < 2) { |
5eb00805 | 1095 | ret = CMD_ERROR; |
636167b6 DG |
1096 | goto end; |
1097 | } | |
1098 | ||
d65106b1 DG |
1099 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
1100 | poptReadDefaultConfig(pc, 0); | |
1101 | ||
1102 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
1103 | switch (opt) { | |
1104 | case OPT_HELP: | |
4ba92f18 | 1105 | SHOW_HELP(); |
d65106b1 | 1106 | goto end; |
83cea427 | 1107 | case OPT_LIST: |
59deec0c | 1108 | ret = print_ctx_type(); |
83cea427 | 1109 | goto end; |
d65106b1 | 1110 | case OPT_TYPE: |
e9492550 JG |
1111 | { |
1112 | type = get_context_type(opt_type); | |
1113 | if (!type) { | |
6caa2bcc | 1114 | ERR("Unknown context type %s", opt_type); |
5eb00805 TD |
1115 | ret = CMD_FATAL; |
1116 | goto end; | |
1117 | } | |
e9492550 | 1118 | cds_list_add_tail(&type->list, &ctx_type_list.head); |
d65106b1 | 1119 | break; |
e9492550 | 1120 | } |
eeac7d46 MD |
1121 | case OPT_USERSPACE: |
1122 | opt_userspace = 1; | |
eeac7d46 | 1123 | break; |
e9492550 JG |
1124 | case OPT_JUL: |
1125 | opt_jul = 1; | |
1126 | break; | |
1127 | case OPT_LOG4J: | |
1128 | opt_log4j = 1; | |
1129 | break; | |
679b4943 SM |
1130 | case OPT_LIST_OPTIONS: |
1131 | list_cmd_options(stdout, long_options); | |
679b4943 | 1132 | goto end; |
d65106b1 | 1133 | default: |
d65106b1 DG |
1134 | ret = CMD_UNDEFINED; |
1135 | goto end; | |
1136 | } | |
1137 | } | |
1138 | ||
68c7f6e5 JD |
1139 | leftover = poptGetArg(pc); |
1140 | if (leftover) { | |
1141 | ERR("Unknown argument: %s", leftover); | |
1142 | ret = CMD_ERROR; | |
1143 | goto end; | |
1144 | } | |
1145 | ||
e9492550 JG |
1146 | ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace + |
1147 | opt_jul + opt_log4j); | |
3ecec76a PP |
1148 | if (ret) { |
1149 | ret = CMD_ERROR; | |
1150 | goto end; | |
1151 | } | |
1152 | ||
ae856491 DG |
1153 | if (!opt_type) { |
1154 | ERR("Missing mandatory -t TYPE"); | |
ae856491 DG |
1155 | ret = CMD_ERROR; |
1156 | goto end; | |
1157 | } | |
1158 | ||
cd80958d DG |
1159 | if (!opt_session_name) { |
1160 | session_name = get_session_name(); | |
1161 | if (session_name == NULL) { | |
5eb00805 | 1162 | ret = CMD_ERROR; |
cd80958d DG |
1163 | goto end; |
1164 | } | |
1165 | } else { | |
1166 | session_name = opt_session_name; | |
1167 | } | |
1168 | ||
59deec0c JR |
1169 | ret = mi_open(); |
1170 | if (ret) { | |
1171 | goto end; | |
89b72577 JRJ |
1172 | } |
1173 | ||
1174 | command_ret = add_context(session_name); | |
59deec0c JR |
1175 | ret = mi_close(command_ret); |
1176 | if (ret) { | |
1177 | goto end; | |
89b72577 JRJ |
1178 | } |
1179 | ||
1180 | end: | |
1256f150 DT |
1181 | if (!opt_session_name) { |
1182 | free(session_name); | |
1183 | } | |
1184 | ||
89b72577 JRJ |
1185 | /* Mi clean-up */ |
1186 | if (writer && mi_lttng_writer_destroy(writer)) { | |
1187 | /* Preserve original error code */ | |
1188 | ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL; | |
1189 | } | |
1190 | ||
3301e740 | 1191 | /* Cleanup allocated memory */ |
b13d56d7 | 1192 | cds_list_for_each_entry_safe(type, tmptype, &ctx_type_list.head, list) { |
e9492550 | 1193 | destroy_ctx_type(type); |
3301e740 DG |
1194 | } |
1195 | ||
89b72577 JRJ |
1196 | /* Overwrite ret if an error occurred during add_context() */ |
1197 | ret = command_ret ? command_ret : ret; | |
1198 | ||
ca1c3607 | 1199 | poptFreeContext(pc); |
d65106b1 DG |
1200 | return ret; |
1201 | } |