Commit | Line | Data |
---|---|---|
b7cdc182 | 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
9f36eaed | 2 | * |
2fa2d39a FG |
3 | * lttng-context-callstack.c |
4 | * | |
5 | * LTTng callstack event context. | |
6 | * | |
7 | * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
8 | * Copyright (C) 2014 Francis Giraldeau <francis.giraldeau@gmail.com> | |
9 | * | |
0bb47c89 MD |
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. | |
2fa2d39a | 14 | * |
0bb47c89 MD |
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 | |
18 | * of CPUs: | |
2fa2d39a FG |
19 | * |
20 | * size = cpus * nest * depth * sizeof(unsigned long) | |
21 | * | |
3685cc80 | 22 | * Which is 4096 bytes per CPU on 64-bit host and a depth of 128. |
0bb47c89 MD |
23 | * The allocation is done at the initialization to avoid memory |
24 | * allocation overhead while tracing, using a shallow stack. | |
2fa2d39a FG |
25 | * |
26 | * The kernel callstack is recovered using save_stack_trace(), and the | |
27 | * userspace callstack uses save_stack_trace_user(). They rely on frame | |
0bb47c89 MD |
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. | |
2fa2d39a FG |
36 | * |
37 | * The symbol name resolution is left to the trace reader. | |
38 | */ | |
39 | ||
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> | |
24591303 MD |
46 | #include <ringbuffer/backend.h> |
47 | #include <ringbuffer/frontend.h> | |
2df37e95 MD |
48 | #include <lttng/events.h> |
49 | #include <lttng/tracer.h> | |
50 | #include <lttng/endian.h> | |
2fa2d39a | 51 | #include "wrapper/vmalloc.h" |
2fa2d39a | 52 | |
b29c6286 MD |
53 | #ifdef CONFIG_ARCH_STACKWALK |
54 | #include "lttng-context-callstack-stackwalk-impl.h" | |
55 | #else | |
b6ee48d2 | 56 | #include "lttng-context-callstack-legacy-impl.h" |
b29c6286 | 57 | #endif |
2fa2d39a FG |
58 | |
59 | static | |
60 | void field_data_free(struct field_data *fdata) | |
61 | { | |
2fa2d39a FG |
62 | if (!fdata) |
63 | return; | |
2fa2d39a FG |
64 | free_percpu(fdata->cs_percpu); |
65 | kfree(fdata); | |
66 | } | |
67 | ||
68 | static | |
0bb47c89 | 69 | struct field_data __percpu *field_data_create(enum lttng_cs_ctx_modes mode) |
2fa2d39a | 70 | { |
2fa2d39a | 71 | struct lttng_cs __percpu *cs_set; |
64cc198b | 72 | struct field_data *fdata; |
2fa2d39a | 73 | |
64cc198b | 74 | fdata = kzalloc(sizeof(*fdata), GFP_KERNEL); |
2fa2d39a FG |
75 | if (!fdata) |
76 | return NULL; | |
77 | cs_set = alloc_percpu(struct lttng_cs); | |
78 | if (!cs_set) | |
79 | goto error_alloc; | |
b5a89a3f | 80 | lttng_cs_set_init(cs_set); |
2fa2d39a | 81 | fdata->cs_percpu = cs_set; |
0bb47c89 | 82 | fdata->mode = mode; |
2fa2d39a FG |
83 | return fdata; |
84 | ||
85 | error_alloc: | |
86 | field_data_free(fdata); | |
87 | return NULL; | |
88 | } | |
89 | ||
90 | static | |
ceabb767 | 91 | void lttng_callstack_sequence_destroy(struct lttng_ctx_field *field) |
2fa2d39a | 92 | { |
3c1a57e8 | 93 | struct field_data *fdata = field->priv; |
2fa2d39a FG |
94 | |
95 | field_data_free(fdata); | |
96 | } | |
97 | ||
ceabb767 MD |
98 | static const struct lttng_type sequence_elem_type = |
99 | __type_integer(unsigned long, 0, 0, -1, __BYTE_ORDER, 16, none); | |
100 | ||
2fa2d39a | 101 | static |
0bb47c89 MD |
102 | int __lttng_add_callstack_generic(struct lttng_ctx **ctx, |
103 | enum lttng_cs_ctx_modes mode) | |
2fa2d39a | 104 | { |
b5a89a3f | 105 | const char *ctx_name = lttng_cs_ctx_mode_name(mode); |
ceabb767 MD |
106 | const char *ctx_length_name = lttng_cs_ctx_mode_length_name(mode); |
107 | struct lttng_ctx_field *length_field, *sequence_field; | |
108 | struct lttng_event_field *field; | |
2fa2d39a FG |
109 | struct field_data *fdata; |
110 | int ret; | |
111 | ||
112 | ret = init_type(mode); | |
113 | if (ret) | |
114 | return ret; | |
ceabb767 MD |
115 | length_field = lttng_append_context(ctx); |
116 | if (!length_field) | |
117 | return -ENOMEM; | |
118 | sequence_field = lttng_append_context(ctx); | |
119 | if (!sequence_field) { | |
120 | lttng_remove_context_field(ctx, length_field); | |
2fa2d39a | 121 | return -ENOMEM; |
ceabb767 | 122 | } |
2fa2d39a | 123 | if (lttng_find_context(*ctx, ctx_name)) { |
2fa2d39a FG |
124 | ret = -EEXIST; |
125 | goto error_find; | |
126 | } | |
64cc198b | 127 | fdata = field_data_create(mode); |
2fa2d39a FG |
128 | if (!fdata) { |
129 | ret = -ENOMEM; | |
130 | goto error_create; | |
131 | } | |
132 | ||
ceabb767 MD |
133 | field = &length_field->event_field; |
134 | field->name = ctx_length_name; | |
135 | field->type.atype = atype_integer; | |
136 | field->type.u.integer.size = sizeof(unsigned int) * CHAR_BIT; | |
137 | field->type.u.integer.alignment = lttng_alignof(unsigned int) * CHAR_BIT; | |
138 | field->type.u.integer.signedness = lttng_is_signed_type(unsigned int); | |
139 | field->type.u.integer.reverse_byte_order = 0; | |
140 | field->type.u.integer.base = 10; | |
141 | field->type.u.integer.encoding = lttng_encode_none; | |
142 | length_field->get_size_arg = lttng_callstack_length_get_size; | |
143 | length_field->record = lttng_callstack_length_record; | |
144 | ||
145 | field = &sequence_field->event_field; | |
146 | field->name = ctx_name; | |
147 | field->type.atype = atype_sequence_nestable; | |
148 | field->type.u.sequence_nestable.elem_type = &sequence_elem_type; | |
149 | field->type.u.sequence_nestable.alignment = 0; | |
150 | sequence_field->get_size_arg = lttng_callstack_sequence_get_size; | |
151 | sequence_field->record = lttng_callstack_sequence_record; | |
152 | sequence_field->priv = fdata; | |
153 | sequence_field->destroy = lttng_callstack_sequence_destroy; | |
154 | ||
263b6c88 | 155 | wrapper_vmalloc_sync_mappings(); |
2fa2d39a FG |
156 | return 0; |
157 | ||
158 | error_create: | |
159 | field_data_free(fdata); | |
160 | error_find: | |
ceabb767 MD |
161 | lttng_remove_context_field(ctx, sequence_field); |
162 | lttng_remove_context_field(ctx, length_field); | |
2fa2d39a FG |
163 | return ret; |
164 | } | |
165 | ||
166 | /** | |
167 | * lttng_add_callstack_to_ctx - add callstack event context | |
168 | * | |
169 | * @ctx: the lttng_ctx pointer to initialize | |
170 | * @type: the context type | |
171 | * | |
172 | * Supported callstack type supported: | |
173 | * LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL | |
174 | * Records the callstack of the kernel | |
175 | * LTTNG_KERNEL_CONTEXT_CALLSTACK_USER | |
176 | * Records the callstack of the userspace program (from the kernel) | |
177 | * | |
178 | * Return 0 for success, or error code. | |
179 | */ | |
180 | int lttng_add_callstack_to_ctx(struct lttng_ctx **ctx, int type) | |
181 | { | |
182 | switch (type) { | |
183 | case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL: | |
184 | return __lttng_add_callstack_generic(ctx, CALLSTACK_KERNEL); | |
b874d3f3 | 185 | #ifdef CONFIG_X86 |
2fa2d39a FG |
186 | case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER: |
187 | return __lttng_add_callstack_generic(ctx, CALLSTACK_USER); | |
b874d3f3 | 188 | #endif |
2fa2d39a FG |
189 | default: |
190 | return -EINVAL; | |
191 | } | |
192 | } | |
193 | EXPORT_SYMBOL_GPL(lttng_add_callstack_to_ctx); |