Commit | Line | Data |
---|---|---|
9f36eaed MJ |
1 | /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1) |
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> | |
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" | |
51 | ||
b6ee48d2 | 52 | #include "lttng-context-callstack-legacy-impl.h" |
2fa2d39a FG |
53 | |
54 | static | |
55 | void field_data_free(struct field_data *fdata) | |
56 | { | |
2fa2d39a FG |
57 | if (!fdata) |
58 | return; | |
2fa2d39a FG |
59 | free_percpu(fdata->cs_percpu); |
60 | kfree(fdata); | |
61 | } | |
62 | ||
63 | static | |
0bb47c89 | 64 | struct field_data __percpu *field_data_create(enum lttng_cs_ctx_modes mode) |
2fa2d39a FG |
65 | { |
66 | int cpu, i; | |
2fa2d39a | 67 | struct lttng_cs __percpu *cs_set; |
64cc198b | 68 | struct field_data *fdata; |
2fa2d39a | 69 | |
64cc198b | 70 | fdata = kzalloc(sizeof(*fdata), GFP_KERNEL); |
2fa2d39a FG |
71 | if (!fdata) |
72 | return NULL; | |
73 | cs_set = alloc_percpu(struct lttng_cs); | |
74 | if (!cs_set) | |
75 | goto error_alloc; | |
76 | ||
77 | fdata->cs_percpu = cs_set; | |
78 | for_each_possible_cpu(cpu) { | |
64cc198b MD |
79 | struct lttng_cs *cs; |
80 | ||
2fa2d39a FG |
81 | cs = per_cpu_ptr(cs_set, cpu); |
82 | for (i = 0; i < RING_BUFFER_MAX_NESTING; i++) { | |
0bb47c89 | 83 | struct lttng_cs_dispatch *dispatch; |
64cc198b | 84 | |
0bb47c89 MD |
85 | dispatch = &cs->dispatch[i]; |
86 | dispatch->stack_trace.entries = dispatch->entries; | |
87 | dispatch->stack_trace.max_entries = MAX_ENTRIES; | |
2fa2d39a FG |
88 | } |
89 | } | |
0bb47c89 | 90 | fdata->mode = mode; |
2fa2d39a FG |
91 | return fdata; |
92 | ||
93 | error_alloc: | |
94 | field_data_free(fdata); | |
95 | return NULL; | |
96 | } | |
97 | ||
98 | static | |
99 | void lttng_callstack_destroy(struct lttng_ctx_field *field) | |
100 | { | |
3c1a57e8 | 101 | struct field_data *fdata = field->priv; |
2fa2d39a FG |
102 | |
103 | field_data_free(fdata); | |
104 | } | |
105 | ||
106 | static | |
0bb47c89 MD |
107 | int __lttng_add_callstack_generic(struct lttng_ctx **ctx, |
108 | enum lttng_cs_ctx_modes mode) | |
2fa2d39a FG |
109 | { |
110 | const char *ctx_name = cs_types[mode].name; | |
111 | struct lttng_ctx_field *field; | |
112 | struct field_data *fdata; | |
113 | int ret; | |
114 | ||
115 | ret = init_type(mode); | |
116 | if (ret) | |
117 | return ret; | |
118 | field = lttng_append_context(ctx); | |
119 | if (!field) | |
120 | return -ENOMEM; | |
121 | if (lttng_find_context(*ctx, ctx_name)) { | |
2fa2d39a FG |
122 | ret = -EEXIST; |
123 | goto error_find; | |
124 | } | |
64cc198b | 125 | fdata = field_data_create(mode); |
2fa2d39a FG |
126 | if (!fdata) { |
127 | ret = -ENOMEM; | |
128 | goto error_create; | |
129 | } | |
130 | ||
131 | field->event_field.name = ctx_name; | |
132 | field->event_field.type.atype = atype_sequence; | |
133 | field->event_field.type.u.sequence.elem_type.atype = atype_integer; | |
134 | field->event_field.type.u.sequence.elem_type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT; | |
135 | field->event_field.type.u.sequence.elem_type.u.basic.integer.alignment = lttng_alignof(long) * CHAR_BIT; | |
136 | field->event_field.type.u.sequence.elem_type.u.basic.integer.signedness = lttng_is_signed_type(unsigned long); | |
137 | field->event_field.type.u.sequence.elem_type.u.basic.integer.reverse_byte_order = 0; | |
138 | field->event_field.type.u.sequence.elem_type.u.basic.integer.base = 16; | |
139 | field->event_field.type.u.sequence.elem_type.u.basic.integer.encoding = lttng_encode_none; | |
140 | ||
141 | field->event_field.type.u.sequence.length_type.atype = atype_integer; | |
142 | field->event_field.type.u.sequence.length_type.u.basic.integer.size = sizeof(unsigned int) * CHAR_BIT; | |
143 | field->event_field.type.u.sequence.length_type.u.basic.integer.alignment = lttng_alignof(unsigned int) * CHAR_BIT; | |
144 | field->event_field.type.u.sequence.length_type.u.basic.integer.signedness = lttng_is_signed_type(unsigned int); | |
145 | field->event_field.type.u.sequence.length_type.u.basic.integer.reverse_byte_order = 0; | |
146 | field->event_field.type.u.sequence.length_type.u.basic.integer.base = 10; | |
147 | field->event_field.type.u.sequence.length_type.u.basic.integer.encoding = lttng_encode_none; | |
148 | ||
149 | field->get_size_arg = lttng_callstack_get_size; | |
150 | field->record = lttng_callstack_record; | |
3c1a57e8 | 151 | field->priv = fdata; |
2fa2d39a FG |
152 | field->destroy = lttng_callstack_destroy; |
153 | wrapper_vmalloc_sync_all(); | |
2fa2d39a FG |
154 | return 0; |
155 | ||
156 | error_create: | |
157 | field_data_free(fdata); | |
158 | error_find: | |
159 | lttng_remove_context_field(ctx, field); | |
160 | return ret; | |
161 | } | |
162 | ||
163 | /** | |
164 | * lttng_add_callstack_to_ctx - add callstack event context | |
165 | * | |
166 | * @ctx: the lttng_ctx pointer to initialize | |
167 | * @type: the context type | |
168 | * | |
169 | * Supported callstack type supported: | |
170 | * LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL | |
171 | * Records the callstack of the kernel | |
172 | * LTTNG_KERNEL_CONTEXT_CALLSTACK_USER | |
173 | * Records the callstack of the userspace program (from the kernel) | |
174 | * | |
175 | * Return 0 for success, or error code. | |
176 | */ | |
177 | int lttng_add_callstack_to_ctx(struct lttng_ctx **ctx, int type) | |
178 | { | |
179 | switch (type) { | |
180 | case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL: | |
181 | return __lttng_add_callstack_generic(ctx, CALLSTACK_KERNEL); | |
b874d3f3 | 182 | #ifdef CONFIG_X86 |
2fa2d39a FG |
183 | case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER: |
184 | return __lttng_add_callstack_generic(ctx, CALLSTACK_USER); | |
b874d3f3 | 185 | #endif |
2fa2d39a FG |
186 | default: |
187 | return -EINVAL; | |
188 | } | |
189 | } | |
190 | EXPORT_SYMBOL_GPL(lttng_add_callstack_to_ctx); |