2 * lttng-context-callstack.c
4 * LTTng callstack event context.
6 * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Copyright (C) 2014 Francis Giraldeau <francis.giraldeau@gmail.com>
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.
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.
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
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.
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
33 * size = cpus * nest * depth * sizeof(unsigned long)
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.
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.
50 * The symbol name resolution is left to the trace reader.
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"
65 #define MAX_ENTRIES 128
67 enum lttng_cs_ctx_modes
{
73 struct lttng_cs_dispatch
{
74 struct stack_trace stack_trace
;
75 unsigned long entries
[MAX_ENTRIES
];
79 struct lttng_cs_dispatch dispatch
[RING_BUFFER_MAX_NESTING
];
83 struct lttng_cs __percpu
*cs_percpu
;
84 enum lttng_cs_ctx_modes mode
;
87 struct lttng_cs_type
{
89 const char *save_func_name
;
90 void (*save_func
)(struct stack_trace
*trace
);
93 static struct lttng_cs_type cs_types
[] = {
95 .name
= "callstack_kernel",
96 .save_func_name
= "save_stack_trace",
100 .name
= "callstack_user",
101 .save_func_name
= "save_stack_trace_user",
107 int init_type(enum lttng_cs_ctx_modes mode
)
111 if (cs_types
[mode
].save_func
)
113 func
= kallsyms_lookup_funcptr(cs_types
[mode
].save_func_name
);
115 printk(KERN_WARNING
"LTTng: symbol lookup failed: %s\n",
116 cs_types
[mode
].save_func_name
);
119 cs_types
[mode
].save_func
= (void *) func
;
123 /* Keep track of nesting inside userspace callstack context code */
124 DEFINE_PER_CPU(int, callstack_user_nesting
);
127 struct stack_trace
*stack_trace_context(struct lttng_ctx_field
*field
,
128 struct lib_ring_buffer_ctx
*ctx
)
130 int buffer_nesting
, cs_user_nesting
;
132 struct field_data
*fdata
= field
->priv
;
135 * Do not gather the userspace callstack context when the event was
136 * triggered by the userspace callstack context saving mechanism.
138 cs_user_nesting
= per_cpu(callstack_user_nesting
, ctx
->cpu
);
140 if (fdata
->mode
== CALLSTACK_USER
&& cs_user_nesting
>= 1)
144 * get_cpu() is not required, preemption is already
145 * disabled while event is written.
147 * max nesting is checked in lib_ring_buffer_get_cpu().
148 * Check it again as a safety net.
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
)
155 return &cs
->dispatch
[buffer_nesting
].stack_trace
;
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.
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
)
167 struct stack_trace
*trace
;
168 struct field_data
*fdata
= field
->priv
;
169 size_t orig_offset
= offset
;
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
;
180 /* reset stack trace, no need to clear memory */
181 trace
->nr_entries
= 0;
183 if (fdata
->mode
== CALLSTACK_USER
)
184 ++per_cpu(callstack_user_nesting
, ctx
->cpu
);
186 /* do the real work and reserve space */
187 cs_types
[fdata
->mode
].save_func(trace
);
189 if (fdata
->mode
== CALLSTACK_USER
)
190 per_cpu(callstack_user_nesting
, ctx
->cpu
)--;
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.
197 if (trace
->nr_entries
> 0
198 && trace
->entries
[trace
->nr_entries
- 1] == ULONG_MAX
) {
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
;
212 void lttng_callstack_record(struct lttng_ctx_field
*field
,
213 struct lib_ring_buffer_ctx
*ctx
,
214 struct lttng_channel
*chan
)
216 struct stack_trace
*trace
= stack_trace_context(field
, ctx
);
217 unsigned int nr_seq_entries
;
219 if (unlikely(!trace
)) {
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));
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
)
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
;
238 chan
->ops
->event_write(ctx
, &delim
, sizeof(unsigned long));
243 void field_data_free(struct field_data
*fdata
)
247 free_percpu(fdata
->cs_percpu
);
252 struct field_data __percpu
*field_data_create(enum lttng_cs_ctx_modes mode
)
255 struct lttng_cs __percpu
*cs_set
;
256 struct field_data
*fdata
;
258 fdata
= kzalloc(sizeof(*fdata
), GFP_KERNEL
);
261 cs_set
= alloc_percpu(struct lttng_cs
);
265 fdata
->cs_percpu
= cs_set
;
266 for_each_possible_cpu(cpu
) {
269 cs
= per_cpu_ptr(cs_set
, cpu
);
270 for (i
= 0; i
< RING_BUFFER_MAX_NESTING
; i
++) {
271 struct lttng_cs_dispatch
*dispatch
;
273 dispatch
= &cs
->dispatch
[i
];
274 dispatch
->stack_trace
.entries
= dispatch
->entries
;
275 dispatch
->stack_trace
.max_entries
= MAX_ENTRIES
;
282 field_data_free(fdata
);
287 void lttng_callstack_destroy(struct lttng_ctx_field
*field
)
289 struct field_data
*fdata
= field
->priv
;
291 field_data_free(fdata
);
295 int __lttng_add_callstack_generic(struct lttng_ctx
**ctx
,
296 enum lttng_cs_ctx_modes mode
)
298 const char *ctx_name
= cs_types
[mode
].name
;
299 struct lttng_ctx_field
*field
;
300 struct field_data
*fdata
;
303 ret
= init_type(mode
);
306 field
= lttng_append_context(ctx
);
309 if (lttng_find_context(*ctx
, ctx_name
)) {
313 fdata
= field_data_create(mode
);
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
;
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
;
337 field
->get_size_arg
= lttng_callstack_get_size
;
338 field
->record
= lttng_callstack_record
;
340 field
->destroy
= lttng_callstack_destroy
;
341 wrapper_vmalloc_sync_all();
345 field_data_free(fdata
);
347 lttng_remove_context_field(ctx
, field
);
352 * lttng_add_callstack_to_ctx - add callstack event context
354 * @ctx: the lttng_ctx pointer to initialize
355 * @type: the context type
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)
363 * Return 0 for success, or error code.
365 int lttng_add_callstack_to_ctx(struct lttng_ctx
**ctx
, int type
)
368 case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL
:
369 return __lttng_add_callstack_generic(ctx
, CALLSTACK_KERNEL
);
371 case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER
:
372 return __lttng_add_callstack_generic(ctx
, CALLSTACK_USER
);
378 EXPORT_SYMBOL_GPL(lttng_add_callstack_to_ctx
);