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> | |
46 | #include "lttng-events.h" | |
47 | #include "wrapper/ringbuffer/backend.h" | |
48 | #include "wrapper/ringbuffer/frontend.h" | |
2fa2d39a | 49 | #include "lttng-tracer.h" |
ceabb767 | 50 | #include "lttng-endian.h" |
2fa2d39a | 51 | |
b29c6286 MD |
52 | #ifdef CONFIG_ARCH_STACKWALK |
53 | #include "lttng-context-callstack-stackwalk-impl.h" | |
54 | #else | |
b6ee48d2 | 55 | #include "lttng-context-callstack-legacy-impl.h" |
b29c6286 | 56 | #endif |
2fa2d39a FG |
57 | |
58 | static | |
59 | void field_data_free(struct field_data *fdata) | |
60 | { | |
2fa2d39a FG |
61 | if (!fdata) |
62 | return; | |
2fa2d39a FG |
63 | free_percpu(fdata->cs_percpu); |
64 | kfree(fdata); | |
65 | } | |
66 | ||
67 | static | |
0bb47c89 | 68 | struct field_data __percpu *field_data_create(enum lttng_cs_ctx_modes mode) |
2fa2d39a | 69 | { |
2fa2d39a | 70 | struct lttng_cs __percpu *cs_set; |
64cc198b | 71 | struct field_data *fdata; |
2fa2d39a | 72 | |
64cc198b | 73 | fdata = kzalloc(sizeof(*fdata), GFP_KERNEL); |
2fa2d39a FG |
74 | if (!fdata) |
75 | return NULL; | |
76 | cs_set = alloc_percpu(struct lttng_cs); | |
77 | if (!cs_set) | |
78 | goto error_alloc; | |
b5a89a3f | 79 | lttng_cs_set_init(cs_set); |
2fa2d39a | 80 | fdata->cs_percpu = cs_set; |
0bb47c89 | 81 | fdata->mode = mode; |
2fa2d39a FG |
82 | return fdata; |
83 | ||
84 | error_alloc: | |
85 | field_data_free(fdata); | |
86 | return NULL; | |
87 | } | |
88 | ||
89 | static | |
ceabb767 | 90 | void lttng_callstack_sequence_destroy(struct lttng_ctx_field *field) |
2fa2d39a | 91 | { |
3c1a57e8 | 92 | struct field_data *fdata = field->priv; |
2fa2d39a FG |
93 | |
94 | field_data_free(fdata); | |
95 | } | |
96 | ||
ceabb767 MD |
97 | static const struct lttng_type sequence_elem_type = |
98 | __type_integer(unsigned long, 0, 0, -1, __BYTE_ORDER, 16, none); | |
99 | ||
2fa2d39a | 100 | static |
0bb47c89 MD |
101 | int __lttng_add_callstack_generic(struct lttng_ctx **ctx, |
102 | enum lttng_cs_ctx_modes mode) | |
2fa2d39a | 103 | { |
b5a89a3f | 104 | const char *ctx_name = lttng_cs_ctx_mode_name(mode); |
ceabb767 MD |
105 | const char *ctx_length_name = lttng_cs_ctx_mode_length_name(mode); |
106 | struct lttng_ctx_field *length_field, *sequence_field; | |
107 | struct lttng_event_field *field; | |
2fa2d39a FG |
108 | struct field_data *fdata; |
109 | int ret; | |
110 | ||
ceabb767 MD |
111 | length_field = lttng_append_context(ctx); |
112 | if (!length_field) | |
113 | return -ENOMEM; | |
114 | sequence_field = lttng_append_context(ctx); | |
115 | if (!sequence_field) { | |
116 | lttng_remove_context_field(ctx, length_field); | |
2fa2d39a | 117 | return -ENOMEM; |
ceabb767 | 118 | } |
2fa2d39a | 119 | if (lttng_find_context(*ctx, ctx_name)) { |
2fa2d39a FG |
120 | ret = -EEXIST; |
121 | goto error_find; | |
122 | } | |
64cc198b | 123 | fdata = field_data_create(mode); |
2fa2d39a FG |
124 | if (!fdata) { |
125 | ret = -ENOMEM; | |
126 | goto error_create; | |
127 | } | |
128 | ||
ceabb767 MD |
129 | field = &length_field->event_field; |
130 | field->name = ctx_length_name; | |
131 | field->type.atype = atype_integer; | |
132 | field->type.u.integer.size = sizeof(unsigned int) * CHAR_BIT; | |
133 | field->type.u.integer.alignment = lttng_alignof(unsigned int) * CHAR_BIT; | |
134 | field->type.u.integer.signedness = lttng_is_signed_type(unsigned int); | |
135 | field->type.u.integer.reverse_byte_order = 0; | |
136 | field->type.u.integer.base = 10; | |
137 | field->type.u.integer.encoding = lttng_encode_none; | |
138 | length_field->get_size_arg = lttng_callstack_length_get_size; | |
139 | length_field->record = lttng_callstack_length_record; | |
140 | ||
141 | field = &sequence_field->event_field; | |
142 | field->name = ctx_name; | |
143 | field->type.atype = atype_sequence_nestable; | |
144 | field->type.u.sequence_nestable.elem_type = &sequence_elem_type; | |
145 | field->type.u.sequence_nestable.alignment = 0; | |
146 | sequence_field->get_size_arg = lttng_callstack_sequence_get_size; | |
147 | sequence_field->record = lttng_callstack_sequence_record; | |
148 | sequence_field->priv = fdata; | |
149 | sequence_field->destroy = lttng_callstack_sequence_destroy; | |
150 | ||
2fa2d39a FG |
151 | return 0; |
152 | ||
153 | error_create: | |
154 | field_data_free(fdata); | |
155 | error_find: | |
ceabb767 MD |
156 | lttng_remove_context_field(ctx, sequence_field); |
157 | lttng_remove_context_field(ctx, length_field); | |
2fa2d39a FG |
158 | return ret; |
159 | } | |
160 | ||
161 | /** | |
162 | * lttng_add_callstack_to_ctx - add callstack event context | |
163 | * | |
164 | * @ctx: the lttng_ctx pointer to initialize | |
165 | * @type: the context type | |
166 | * | |
167 | * Supported callstack type supported: | |
168 | * LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL | |
169 | * Records the callstack of the kernel | |
170 | * LTTNG_KERNEL_CONTEXT_CALLSTACK_USER | |
171 | * Records the callstack of the userspace program (from the kernel) | |
172 | * | |
173 | * Return 0 for success, or error code. | |
174 | */ | |
175 | int lttng_add_callstack_to_ctx(struct lttng_ctx **ctx, int type) | |
176 | { | |
177 | switch (type) { | |
178 | case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL: | |
179 | return __lttng_add_callstack_generic(ctx, CALLSTACK_KERNEL); | |
b874d3f3 | 180 | #ifdef CONFIG_X86 |
2fa2d39a FG |
181 | case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER: |
182 | return __lttng_add_callstack_generic(ctx, CALLSTACK_USER); | |
b874d3f3 | 183 | #endif |
2fa2d39a FG |
184 | default: |
185 | return -EINVAL; | |
186 | } | |
187 | } | |
188 | EXPORT_SYMBOL_GPL(lttng_add_callstack_to_ctx); |