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