1 /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
3 * lttng-context-callstack.c
5 * LTTng callstack event context.
7 * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Copyright (C) 2014 Francis Giraldeau <francis.giraldeau@gmail.com>
10 * The callstack context can be added to any kernel event. It records
11 * either the kernel or the userspace callstack, up to a max depth. The
12 * context is a CTF sequence, such that it uses only the space required
13 * for the number of callstack entries.
15 * It allocates callstack buffers per-CPU up to 4 interrupt nesting.
16 * This nesting limit is the same as defined in the ring buffer. It
17 * therefore uses a fixed amount of memory, proportional to the number
20 * size = cpus * nest * depth * sizeof(unsigned long)
22 * Which is 4096 bytes per CPU on 64-bit host and a depth of 128.
23 * The allocation is done at the initialization to avoid memory
24 * allocation overhead while tracing, using a shallow stack.
26 * The kernel callstack is recovered using save_stack_trace(), and the
27 * userspace callstack uses save_stack_trace_user(). They rely on frame
28 * pointers. These are usually available for the kernel, but the
29 * compiler option -fomit-frame-pointer frequently used in popular Linux
30 * distributions may cause the userspace callstack to be unreliable, and
31 * is a known limitation of this approach. If frame pointers are not
32 * available, it produces no error, but the callstack will be empty. We
33 * still provide the feature, because it works well for runtime
34 * environments having frame pointers. In the future, unwind support
35 * and/or last branch record may provide a solution to this problem.
37 * The symbol name resolution is left to the trace reader.
40 #include <linux/module.h>
41 #include <linux/slab.h>
42 #include <linux/sched.h>
43 #include <linux/utsname.h>
44 #include <linux/stacktrace.h>
45 #include <linux/spinlock.h>
46 #include "lttng-events.h"
47 #include "wrapper/ringbuffer/backend.h"
48 #include "wrapper/ringbuffer/frontend.h"
49 #include "wrapper/vmalloc.h"
50 #include "lttng-tracer.h"
52 #define MAX_ENTRIES 128
54 enum lttng_cs_ctx_modes
{
60 struct lttng_cs_dispatch
{
61 struct stack_trace stack_trace
;
62 unsigned long entries
[MAX_ENTRIES
];
66 struct lttng_cs_dispatch dispatch
[RING_BUFFER_MAX_NESTING
];
70 struct lttng_cs __percpu
*cs_percpu
;
71 enum lttng_cs_ctx_modes mode
;
74 struct lttng_cs_type
{
76 const char *save_func_name
;
77 void (*save_func
)(struct stack_trace
*trace
);
80 static struct lttng_cs_type cs_types
[] = {
82 .name
= "callstack_kernel",
83 .save_func_name
= "save_stack_trace",
87 .name
= "callstack_user",
88 .save_func_name
= "save_stack_trace_user",
94 int init_type(enum lttng_cs_ctx_modes mode
)
98 if (cs_types
[mode
].save_func
)
100 func
= kallsyms_lookup_funcptr(cs_types
[mode
].save_func_name
);
102 printk(KERN_WARNING
"LTTng: symbol lookup failed: %s\n",
103 cs_types
[mode
].save_func_name
);
106 cs_types
[mode
].save_func
= (void *) func
;
110 /* Keep track of nesting inside userspace callstack context code */
111 DEFINE_PER_CPU(int, callstack_user_nesting
);
114 struct stack_trace
*stack_trace_context(struct lttng_ctx_field
*field
,
115 struct lib_ring_buffer_ctx
*ctx
)
117 int buffer_nesting
, cs_user_nesting
;
119 struct field_data
*fdata
= field
->priv
;
122 * Do not gather the userspace callstack context when the event was
123 * triggered by the userspace callstack context saving mechanism.
125 cs_user_nesting
= per_cpu(callstack_user_nesting
, ctx
->cpu
);
127 if (fdata
->mode
== CALLSTACK_USER
&& cs_user_nesting
>= 1)
131 * get_cpu() is not required, preemption is already
132 * disabled while event is written.
134 * max nesting is checked in lib_ring_buffer_get_cpu().
135 * Check it again as a safety net.
137 cs
= per_cpu_ptr(fdata
->cs_percpu
, ctx
->cpu
);
138 buffer_nesting
= per_cpu(lib_ring_buffer_nesting
, ctx
->cpu
) - 1;
139 if (buffer_nesting
>= RING_BUFFER_MAX_NESTING
)
142 return &cs
->dispatch
[buffer_nesting
].stack_trace
;
146 * In order to reserve the correct size, the callstack is computed. The
147 * resulting callstack is saved to be accessed in the record step.
150 size_t lttng_callstack_get_size(size_t offset
, struct lttng_ctx_field
*field
,
151 struct lib_ring_buffer_ctx
*ctx
,
152 struct lttng_channel
*chan
)
154 struct stack_trace
*trace
;
155 struct field_data
*fdata
= field
->priv
;
156 size_t orig_offset
= offset
;
158 /* do not write data if no space is available */
159 trace
= stack_trace_context(field
, ctx
);
160 if (unlikely(!trace
)) {
161 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(unsigned int));
162 offset
+= sizeof(unsigned int);
163 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(unsigned long));
164 return offset
- orig_offset
;
167 /* reset stack trace, no need to clear memory */
168 trace
->nr_entries
= 0;
170 if (fdata
->mode
== CALLSTACK_USER
)
171 ++per_cpu(callstack_user_nesting
, ctx
->cpu
);
173 /* do the real work and reserve space */
174 cs_types
[fdata
->mode
].save_func(trace
);
176 if (fdata
->mode
== CALLSTACK_USER
)
177 per_cpu(callstack_user_nesting
, ctx
->cpu
)--;
180 * Remove final ULONG_MAX delimiter. If we cannot find it, add
181 * our own marker to show that the stack is incomplete. This is
182 * more compact for a trace.
184 if (trace
->nr_entries
> 0
185 && trace
->entries
[trace
->nr_entries
- 1] == ULONG_MAX
) {
188 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(unsigned int));
189 offset
+= sizeof(unsigned int);
190 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(unsigned long));
191 offset
+= sizeof(unsigned long) * trace
->nr_entries
;
192 /* Add our own ULONG_MAX delimiter to show incomplete stack. */
193 if (trace
->nr_entries
== trace
->max_entries
)
194 offset
+= sizeof(unsigned long);
195 return offset
- orig_offset
;
199 void lttng_callstack_record(struct lttng_ctx_field
*field
,
200 struct lib_ring_buffer_ctx
*ctx
,
201 struct lttng_channel
*chan
)
203 struct stack_trace
*trace
= stack_trace_context(field
, ctx
);
204 unsigned int nr_seq_entries
;
206 if (unlikely(!trace
)) {
208 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(unsigned int));
209 chan
->ops
->event_write(ctx
, &nr_seq_entries
, sizeof(unsigned int));
210 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(unsigned long));
213 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(unsigned int));
214 nr_seq_entries
= trace
->nr_entries
;
215 if (trace
->nr_entries
== trace
->max_entries
)
217 chan
->ops
->event_write(ctx
, &nr_seq_entries
, sizeof(unsigned int));
218 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(unsigned long));
219 chan
->ops
->event_write(ctx
, trace
->entries
,
220 sizeof(unsigned long) * trace
->nr_entries
);
221 /* Add our own ULONG_MAX delimiter to show incomplete stack. */
222 if (trace
->nr_entries
== trace
->max_entries
) {
223 unsigned long delim
= ULONG_MAX
;
225 chan
->ops
->event_write(ctx
, &delim
, sizeof(unsigned long));
230 void field_data_free(struct field_data
*fdata
)
234 free_percpu(fdata
->cs_percpu
);
239 struct field_data __percpu
*field_data_create(enum lttng_cs_ctx_modes mode
)
242 struct lttng_cs __percpu
*cs_set
;
243 struct field_data
*fdata
;
245 fdata
= kzalloc(sizeof(*fdata
), GFP_KERNEL
);
248 cs_set
= alloc_percpu(struct lttng_cs
);
252 fdata
->cs_percpu
= cs_set
;
253 for_each_possible_cpu(cpu
) {
256 cs
= per_cpu_ptr(cs_set
, cpu
);
257 for (i
= 0; i
< RING_BUFFER_MAX_NESTING
; i
++) {
258 struct lttng_cs_dispatch
*dispatch
;
260 dispatch
= &cs
->dispatch
[i
];
261 dispatch
->stack_trace
.entries
= dispatch
->entries
;
262 dispatch
->stack_trace
.max_entries
= MAX_ENTRIES
;
269 field_data_free(fdata
);
274 void lttng_callstack_destroy(struct lttng_ctx_field
*field
)
276 struct field_data
*fdata
= field
->priv
;
278 field_data_free(fdata
);
282 int __lttng_add_callstack_generic(struct lttng_ctx
**ctx
,
283 enum lttng_cs_ctx_modes mode
)
285 const char *ctx_name
= cs_types
[mode
].name
;
286 struct lttng_ctx_field
*field
;
287 struct field_data
*fdata
;
290 ret
= init_type(mode
);
293 field
= lttng_append_context(ctx
);
296 if (lttng_find_context(*ctx
, ctx_name
)) {
300 fdata
= field_data_create(mode
);
306 field
->event_field
.name
= ctx_name
;
307 field
->event_field
.type
.atype
= atype_sequence
;
308 field
->event_field
.type
.u
.sequence
.elem_type
.atype
= atype_integer
;
309 field
->event_field
.type
.u
.sequence
.elem_type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
310 field
->event_field
.type
.u
.sequence
.elem_type
.u
.basic
.integer
.alignment
= lttng_alignof(long) * CHAR_BIT
;
311 field
->event_field
.type
.u
.sequence
.elem_type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(unsigned long);
312 field
->event_field
.type
.u
.sequence
.elem_type
.u
.basic
.integer
.reverse_byte_order
= 0;
313 field
->event_field
.type
.u
.sequence
.elem_type
.u
.basic
.integer
.base
= 16;
314 field
->event_field
.type
.u
.sequence
.elem_type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
316 field
->event_field
.type
.u
.sequence
.length_type
.atype
= atype_integer
;
317 field
->event_field
.type
.u
.sequence
.length_type
.u
.basic
.integer
.size
= sizeof(unsigned int) * CHAR_BIT
;
318 field
->event_field
.type
.u
.sequence
.length_type
.u
.basic
.integer
.alignment
= lttng_alignof(unsigned int) * CHAR_BIT
;
319 field
->event_field
.type
.u
.sequence
.length_type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(unsigned int);
320 field
->event_field
.type
.u
.sequence
.length_type
.u
.basic
.integer
.reverse_byte_order
= 0;
321 field
->event_field
.type
.u
.sequence
.length_type
.u
.basic
.integer
.base
= 10;
322 field
->event_field
.type
.u
.sequence
.length_type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
324 field
->get_size_arg
= lttng_callstack_get_size
;
325 field
->record
= lttng_callstack_record
;
327 field
->destroy
= lttng_callstack_destroy
;
328 wrapper_vmalloc_sync_all();
332 field_data_free(fdata
);
334 lttng_remove_context_field(ctx
, field
);
339 * lttng_add_callstack_to_ctx - add callstack event context
341 * @ctx: the lttng_ctx pointer to initialize
342 * @type: the context type
344 * Supported callstack type supported:
345 * LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL
346 * Records the callstack of the kernel
347 * LTTNG_KERNEL_CONTEXT_CALLSTACK_USER
348 * Records the callstack of the userspace program (from the kernel)
350 * Return 0 for success, or error code.
352 int lttng_add_callstack_to_ctx(struct lttng_ctx
**ctx
, int type
)
355 case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL
:
356 return __lttng_add_callstack_generic(ctx
, CALLSTACK_KERNEL
);
358 case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER
:
359 return __lttng_add_callstack_generic(ctx
, CALLSTACK_USER
);
365 EXPORT_SYMBOL_GPL(lttng_add_callstack_to_ctx
);