Fix: ASoC level IO tracing removed upstream
[lttng-modules.git] / lttng-context-callstack.c
1 /*
2 * lttng-context-callstack.c
3 *
4 * LTTng callstack event context.
5 *
6 * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Copyright (C) 2014 Francis Giraldeau <francis.giraldeau@gmail.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; only
12 * version 2.1 of the License.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 * The callstack context can be added to any kernel event. It records
24 * either the kernel or the userspace callstack, up to a max depth. The
25 * context is a CTF sequence, such that it uses only the space required
26 * for the number of callstack entries.
27 *
28 * It allocates callstack buffers per-CPU up to 4 interrupt nesting.
29 * This nesting limit is the same as defined in the ring buffer. It
30 * therefore uses a fixed amount of memory, proportional to the number
31 * of CPUs:
32 *
33 * size = cpus * nest * depth * sizeof(unsigned long)
34 *
35 * Which is 4096 bytes per CPU on 64-bit host and a depth of 128.
36 * The allocation is done at the initialization to avoid memory
37 * allocation overhead while tracing, using a shallow stack.
38 *
39 * The kernel callstack is recovered using save_stack_trace(), and the
40 * userspace callstack uses save_stack_trace_user(). They rely on frame
41 * pointers. These are usually available for the kernel, but the
42 * compiler option -fomit-frame-pointer frequently used in popular Linux
43 * distributions may cause the userspace callstack to be unreliable, and
44 * is a known limitation of this approach. If frame pointers are not
45 * available, it produces no error, but the callstack will be empty. We
46 * still provide the feature, because it works well for runtime
47 * environments having frame pointers. In the future, unwind support
48 * and/or last branch record may provide a solution to this problem.
49 *
50 * The symbol name resolution is left to the trace reader.
51 */
52
53 #include <linux/module.h>
54 #include <linux/slab.h>
55 #include <linux/sched.h>
56 #include <linux/utsname.h>
57 #include <linux/stacktrace.h>
58 #include <linux/spinlock.h>
59 #include "lttng-events.h"
60 #include "wrapper/ringbuffer/backend.h"
61 #include "wrapper/ringbuffer/frontend.h"
62 #include "wrapper/vmalloc.h"
63 #include "lttng-tracer.h"
64
65 #define MAX_ENTRIES 128
66
67 enum lttng_cs_ctx_modes {
68 CALLSTACK_KERNEL = 0,
69 CALLSTACK_USER = 1,
70 NR_CALLSTACK_MODES,
71 };
72
73 struct lttng_cs_dispatch {
74 struct stack_trace stack_trace;
75 unsigned long entries[MAX_ENTRIES];
76 };
77
78 struct lttng_cs {
79 struct lttng_cs_dispatch dispatch[RING_BUFFER_MAX_NESTING];
80 };
81
82 struct field_data {
83 struct lttng_cs __percpu *cs_percpu;
84 enum lttng_cs_ctx_modes mode;
85 };
86
87 struct lttng_cs_type {
88 const char *name;
89 const char *save_func_name;
90 void (*save_func)(struct stack_trace *trace);
91 };
92
93 static struct lttng_cs_type cs_types[] = {
94 {
95 .name = "callstack_kernel",
96 .save_func_name = "save_stack_trace",
97 .save_func = NULL,
98 },
99 {
100 .name = "callstack_user",
101 .save_func_name = "save_stack_trace_user",
102 .save_func = NULL,
103 },
104 };
105
106 static
107 int init_type(enum lttng_cs_ctx_modes mode)
108 {
109 unsigned long func;
110
111 if (cs_types[mode].save_func)
112 return 0;
113 func = kallsyms_lookup_funcptr(cs_types[mode].save_func_name);
114 if (!func) {
115 printk(KERN_WARNING "LTTng: symbol lookup failed: %s\n",
116 cs_types[mode].save_func_name);
117 return -EINVAL;
118 }
119 cs_types[mode].save_func = (void *) func;
120 return 0;
121 }
122
123 /* Keep track of nesting inside userspace callstack context code */
124 DEFINE_PER_CPU(int, callstack_user_nesting);
125
126 static
127 struct stack_trace *stack_trace_context(struct lttng_ctx_field *field,
128 struct lib_ring_buffer_ctx *ctx)
129 {
130 int buffer_nesting, cs_user_nesting;
131 struct lttng_cs *cs;
132 struct field_data *fdata = field->priv;
133
134 /*
135 * Do not gather the userspace callstack context when the event was
136 * triggered by the userspace callstack context saving mechanism.
137 */
138 cs_user_nesting = per_cpu(callstack_user_nesting, ctx->cpu);
139
140 if (fdata->mode == CALLSTACK_USER && cs_user_nesting >= 1)
141 return NULL;
142
143 /*
144 * get_cpu() is not required, preemption is already
145 * disabled while event is written.
146 *
147 * max nesting is checked in lib_ring_buffer_get_cpu().
148 * Check it again as a safety net.
149 */
150 cs = per_cpu_ptr(fdata->cs_percpu, ctx->cpu);
151 buffer_nesting = per_cpu(lib_ring_buffer_nesting, ctx->cpu) - 1;
152 if (buffer_nesting >= RING_BUFFER_MAX_NESTING)
153 return NULL;
154
155 return &cs->dispatch[buffer_nesting].stack_trace;
156 }
157
158 /*
159 * In order to reserve the correct size, the callstack is computed. The
160 * resulting callstack is saved to be accessed in the record step.
161 */
162 static
163 size_t lttng_callstack_get_size(size_t offset, struct lttng_ctx_field *field,
164 struct lib_ring_buffer_ctx *ctx,
165 struct lttng_channel *chan)
166 {
167 struct stack_trace *trace;
168 struct field_data *fdata = field->priv;
169 size_t orig_offset = offset;
170
171 /* do not write data if no space is available */
172 trace = stack_trace_context(field, ctx);
173 if (unlikely(!trace)) {
174 offset += lib_ring_buffer_align(offset, lttng_alignof(unsigned int));
175 offset += sizeof(unsigned int);
176 offset += lib_ring_buffer_align(offset, lttng_alignof(unsigned long));
177 return offset - orig_offset;
178 }
179
180 /* reset stack trace, no need to clear memory */
181 trace->nr_entries = 0;
182
183 if (fdata->mode == CALLSTACK_USER)
184 ++per_cpu(callstack_user_nesting, ctx->cpu);
185
186 /* do the real work and reserve space */
187 cs_types[fdata->mode].save_func(trace);
188
189 if (fdata->mode == CALLSTACK_USER)
190 per_cpu(callstack_user_nesting, ctx->cpu)--;
191
192 /*
193 * Remove final ULONG_MAX delimiter. If we cannot find it, add
194 * our own marker to show that the stack is incomplete. This is
195 * more compact for a trace.
196 */
197 if (trace->nr_entries > 0
198 && trace->entries[trace->nr_entries - 1] == ULONG_MAX) {
199 trace->nr_entries--;
200 }
201 offset += lib_ring_buffer_align(offset, lttng_alignof(unsigned int));
202 offset += sizeof(unsigned int);
203 offset += lib_ring_buffer_align(offset, lttng_alignof(unsigned long));
204 offset += sizeof(unsigned long) * trace->nr_entries;
205 /* Add our own ULONG_MAX delimiter to show incomplete stack. */
206 if (trace->nr_entries == trace->max_entries)
207 offset += sizeof(unsigned long);
208 return offset - orig_offset;
209 }
210
211 static
212 void lttng_callstack_record(struct lttng_ctx_field *field,
213 struct lib_ring_buffer_ctx *ctx,
214 struct lttng_channel *chan)
215 {
216 struct stack_trace *trace = stack_trace_context(field, ctx);
217 unsigned int nr_seq_entries;
218
219 if (unlikely(!trace)) {
220 nr_seq_entries = 0;
221 lib_ring_buffer_align_ctx(ctx, lttng_alignof(unsigned int));
222 chan->ops->event_write(ctx, &nr_seq_entries, sizeof(unsigned int));
223 lib_ring_buffer_align_ctx(ctx, lttng_alignof(unsigned long));
224 return;
225 }
226 lib_ring_buffer_align_ctx(ctx, lttng_alignof(unsigned int));
227 nr_seq_entries = trace->nr_entries;
228 if (trace->nr_entries == trace->max_entries)
229 nr_seq_entries++;
230 chan->ops->event_write(ctx, &nr_seq_entries, sizeof(unsigned int));
231 lib_ring_buffer_align_ctx(ctx, lttng_alignof(unsigned long));
232 chan->ops->event_write(ctx, trace->entries,
233 sizeof(unsigned long) * trace->nr_entries);
234 /* Add our own ULONG_MAX delimiter to show incomplete stack. */
235 if (trace->nr_entries == trace->max_entries) {
236 unsigned long delim = ULONG_MAX;
237
238 chan->ops->event_write(ctx, &delim, sizeof(unsigned long));
239 }
240 }
241
242 static
243 void field_data_free(struct field_data *fdata)
244 {
245 if (!fdata)
246 return;
247 free_percpu(fdata->cs_percpu);
248 kfree(fdata);
249 }
250
251 static
252 struct field_data __percpu *field_data_create(enum lttng_cs_ctx_modes mode)
253 {
254 int cpu, i;
255 struct lttng_cs __percpu *cs_set;
256 struct field_data *fdata;
257
258 fdata = kzalloc(sizeof(*fdata), GFP_KERNEL);
259 if (!fdata)
260 return NULL;
261 cs_set = alloc_percpu(struct lttng_cs);
262 if (!cs_set)
263 goto error_alloc;
264
265 fdata->cs_percpu = cs_set;
266 for_each_possible_cpu(cpu) {
267 struct lttng_cs *cs;
268
269 cs = per_cpu_ptr(cs_set, cpu);
270 for (i = 0; i < RING_BUFFER_MAX_NESTING; i++) {
271 struct lttng_cs_dispatch *dispatch;
272
273 dispatch = &cs->dispatch[i];
274 dispatch->stack_trace.entries = dispatch->entries;
275 dispatch->stack_trace.max_entries = MAX_ENTRIES;
276 }
277 }
278 fdata->mode = mode;
279 return fdata;
280
281 error_alloc:
282 field_data_free(fdata);
283 return NULL;
284 }
285
286 static
287 void lttng_callstack_destroy(struct lttng_ctx_field *field)
288 {
289 struct field_data *fdata = field->priv;
290
291 field_data_free(fdata);
292 }
293
294 static
295 int __lttng_add_callstack_generic(struct lttng_ctx **ctx,
296 enum lttng_cs_ctx_modes mode)
297 {
298 const char *ctx_name = cs_types[mode].name;
299 struct lttng_ctx_field *field;
300 struct field_data *fdata;
301 int ret;
302
303 ret = init_type(mode);
304 if (ret)
305 return ret;
306 field = lttng_append_context(ctx);
307 if (!field)
308 return -ENOMEM;
309 if (lttng_find_context(*ctx, ctx_name)) {
310 ret = -EEXIST;
311 goto error_find;
312 }
313 fdata = field_data_create(mode);
314 if (!fdata) {
315 ret = -ENOMEM;
316 goto error_create;
317 }
318
319 field->event_field.name = ctx_name;
320 field->event_field.type.atype = atype_sequence;
321 field->event_field.type.u.sequence.elem_type.atype = atype_integer;
322 field->event_field.type.u.sequence.elem_type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT;
323 field->event_field.type.u.sequence.elem_type.u.basic.integer.alignment = lttng_alignof(long) * CHAR_BIT;
324 field->event_field.type.u.sequence.elem_type.u.basic.integer.signedness = lttng_is_signed_type(unsigned long);
325 field->event_field.type.u.sequence.elem_type.u.basic.integer.reverse_byte_order = 0;
326 field->event_field.type.u.sequence.elem_type.u.basic.integer.base = 16;
327 field->event_field.type.u.sequence.elem_type.u.basic.integer.encoding = lttng_encode_none;
328
329 field->event_field.type.u.sequence.length_type.atype = atype_integer;
330 field->event_field.type.u.sequence.length_type.u.basic.integer.size = sizeof(unsigned int) * CHAR_BIT;
331 field->event_field.type.u.sequence.length_type.u.basic.integer.alignment = lttng_alignof(unsigned int) * CHAR_BIT;
332 field->event_field.type.u.sequence.length_type.u.basic.integer.signedness = lttng_is_signed_type(unsigned int);
333 field->event_field.type.u.sequence.length_type.u.basic.integer.reverse_byte_order = 0;
334 field->event_field.type.u.sequence.length_type.u.basic.integer.base = 10;
335 field->event_field.type.u.sequence.length_type.u.basic.integer.encoding = lttng_encode_none;
336
337 field->get_size_arg = lttng_callstack_get_size;
338 field->record = lttng_callstack_record;
339 field->priv = fdata;
340 field->destroy = lttng_callstack_destroy;
341 wrapper_vmalloc_sync_all();
342 return 0;
343
344 error_create:
345 field_data_free(fdata);
346 error_find:
347 lttng_remove_context_field(ctx, field);
348 return ret;
349 }
350
351 /**
352 * lttng_add_callstack_to_ctx - add callstack event context
353 *
354 * @ctx: the lttng_ctx pointer to initialize
355 * @type: the context type
356 *
357 * Supported callstack type supported:
358 * LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL
359 * Records the callstack of the kernel
360 * LTTNG_KERNEL_CONTEXT_CALLSTACK_USER
361 * Records the callstack of the userspace program (from the kernel)
362 *
363 * Return 0 for success, or error code.
364 */
365 int lttng_add_callstack_to_ctx(struct lttng_ctx **ctx, int type)
366 {
367 switch (type) {
368 case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL:
369 return __lttng_add_callstack_generic(ctx, CALLSTACK_KERNEL);
370 #ifdef CONFIG_X86
371 case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER:
372 return __lttng_add_callstack_generic(ctx, CALLSTACK_USER);
373 #endif
374 default:
375 return -EINVAL;
376 }
377 }
378 EXPORT_SYMBOL_GPL(lttng_add_callstack_to_ctx);
379
380 MODULE_LICENSE("GPL and additional rights");
381 MODULE_AUTHOR("Francis Giraldeau");
382 MODULE_DESCRIPTION("Linux Trace Toolkit Callstack Support");
This page took 0.037705 seconds and 4 git commands to generate.