1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
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 <ringbuffer/backend.h>
47 #include <ringbuffer/frontend.h>
48 #include <lttng/events.h>
49 #include <lttng/tracer.h>
50 #include <lttng/endian.h>
51 #include "wrapper/vmalloc.h"
53 #ifdef CONFIG_ARCH_STACKWALK
54 #include "lttng-context-callstack-stackwalk-impl.h"
56 #include "lttng-context-callstack-legacy-impl.h"
60 void field_data_free(struct field_data
*fdata
)
64 free_percpu(fdata
->cs_percpu
);
69 struct field_data __percpu
*field_data_create(enum lttng_cs_ctx_modes mode
)
71 struct lttng_cs __percpu
*cs_set
;
72 struct field_data
*fdata
;
74 fdata
= kzalloc(sizeof(*fdata
), GFP_KERNEL
);
77 cs_set
= alloc_percpu(struct lttng_cs
);
80 lttng_cs_set_init(cs_set
);
81 fdata
->cs_percpu
= cs_set
;
86 field_data_free(fdata
);
91 void lttng_callstack_sequence_destroy(struct lttng_ctx_field
*field
)
93 struct field_data
*fdata
= field
->priv
;
95 field_data_free(fdata
);
98 static const struct lttng_type sequence_elem_type
=
99 __type_integer(unsigned long, 0, 0, -1, __BYTE_ORDER
, 16, none
);
102 int __lttng_add_callstack_generic(struct lttng_ctx
**ctx
,
103 enum lttng_cs_ctx_modes mode
)
105 const char *ctx_name
= lttng_cs_ctx_mode_name(mode
);
106 const char *ctx_length_name
= lttng_cs_ctx_mode_length_name(mode
);
107 struct lttng_ctx_field
*length_field
, *sequence_field
;
108 ssize_t length_index
, sequence_index
;
109 struct lttng_event_field
*field
;
110 struct field_data
*fdata
;
113 ret
= init_type(mode
);
116 if (lttng_find_context(*ctx
, ctx_name
))
118 length_index
= lttng_append_context_index(ctx
);
119 if (length_index
< 0) {
123 sequence_index
= lttng_append_context_index(ctx
);
124 if (sequence_index
< 0) {
128 length_field
= lttng_get_context_field_from_index(*ctx
, length_index
);
129 WARN_ON_ONCE(!length_field
);
130 sequence_field
= lttng_get_context_field_from_index(*ctx
, sequence_index
);
131 WARN_ON_ONCE(!sequence_field
);
132 fdata
= field_data_create(mode
);
138 field
= &length_field
->event_field
;
139 field
->name
= ctx_length_name
;
140 field
->type
.type
= lttng_kernel_type_integer
;
141 field
->type
.u
.integer
.size
= sizeof(unsigned int) * CHAR_BIT
;
142 field
->type
.u
.integer
.alignment
= lttng_alignof(unsigned int) * CHAR_BIT
;
143 field
->type
.u
.integer
.signedness
= lttng_is_signed_type(unsigned int);
144 field
->type
.u
.integer
.reverse_byte_order
= 0;
145 field
->type
.u
.integer
.base
= 10;
146 field
->type
.u
.integer
.encoding
= lttng_encode_none
;
147 length_field
->get_size_arg
= lttng_callstack_length_get_size
;
148 length_field
->record
= lttng_callstack_length_record
;
149 length_field
->priv
= fdata
;
151 field
= &sequence_field
->event_field
;
152 field
->name
= ctx_name
;
153 field
->type
.type
= lttng_kernel_type_sequence_nestable
;
154 field
->type
.u
.sequence_nestable
.length_name
= ctx_length_name
;
155 field
->type
.u
.sequence_nestable
.elem_type
= &sequence_elem_type
;
156 field
->type
.u
.sequence_nestable
.alignment
= 0;
157 sequence_field
->get_size_arg
= lttng_callstack_sequence_get_size
;
158 sequence_field
->record
= lttng_callstack_sequence_record
;
159 sequence_field
->priv
= fdata
;
160 sequence_field
->destroy
= lttng_callstack_sequence_destroy
;
162 wrapper_vmalloc_sync_mappings();
166 lttng_remove_context_field_index(ctx
, sequence_index
);
168 lttng_remove_context_field_index(ctx
, length_index
);
174 * lttng_add_callstack_to_ctx - add callstack event context
176 * @ctx: the lttng_ctx pointer to initialize
177 * @type: the context type
179 * Supported callstack type supported:
180 * LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL
181 * Records the callstack of the kernel
182 * LTTNG_KERNEL_CONTEXT_CALLSTACK_USER
183 * Records the callstack of the userspace program (from the kernel)
185 * Return 0 for success, or error code.
187 int lttng_add_callstack_to_ctx(struct lttng_ctx
**ctx
, int type
)
190 case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL
:
191 return __lttng_add_callstack_generic(ctx
, CALLSTACK_KERNEL
);
193 case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER
:
194 return __lttng_add_callstack_generic(ctx
, CALLSTACK_USER
);
200 EXPORT_SYMBOL_GPL(lttng_add_callstack_to_ctx
);