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