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/events-internal.h>
50 #include <lttng/tracer.h>
51 #include <lttng/endian.h>
52 #include "wrapper/vmalloc.h"
54 #ifdef CONFIG_ARCH_STACKWALK
55 #include "lttng-context-callstack-stackwalk-impl.h"
57 #include "lttng-context-callstack-legacy-impl.h"
63 void field_data_free(struct field_data
*fdata
)
67 free_percpu(fdata
->cs_percpu
);
72 struct field_data __percpu
*field_data_create(enum lttng_cs_ctx_modes mode
)
74 struct lttng_cs __percpu
*cs_set
;
75 struct field_data
*fdata
;
77 fdata
= kzalloc(sizeof(*fdata
), GFP_KERNEL
);
80 cs_set
= alloc_percpu(struct lttng_cs
);
83 lttng_cs_set_init(cs_set
);
84 fdata
->cs_percpu
= cs_set
;
89 field_data_free(fdata
);
94 void lttng_callstack_sequence_destroy(void *priv
)
96 struct field_data
*fdata
= priv
;
98 field_data_free(fdata
);
101 static const struct lttng_kernel_event_field
*event_fields_kernel
[NR_FIELDS
] = {
102 lttng_kernel_static_event_field("_callstack_kernel_length",
103 lttng_kernel_static_type_integer_from_type(unsigned int, __BYTE_ORDER
, 10),
105 lttng_kernel_static_event_field("callstack_kernel",
106 lttng_kernel_static_type_sequence(NULL
,
107 lttng_kernel_static_type_integer_from_type(unsigned long, __BYTE_ORDER
, 16),
112 static const struct lttng_kernel_event_field
*event_fields_user
[NR_FIELDS
] = {
113 lttng_kernel_static_event_field("_callstack_user_length",
114 lttng_kernel_static_type_integer_from_type(unsigned int, __BYTE_ORDER
, 10),
116 lttng_kernel_static_event_field("callstack_user",
117 lttng_kernel_static_type_sequence(NULL
,
118 lttng_kernel_static_type_integer_from_type(unsigned long, __BYTE_ORDER
, 16),
123 const struct lttng_kernel_event_field
**lttng_cs_event_fields(enum lttng_cs_ctx_modes mode
)
126 case CALLSTACK_KERNEL
:
127 return event_fields_kernel
;
129 return event_fields_user
;
136 int __lttng_add_callstack_generic(struct lttng_kernel_ctx
**ctx
,
137 enum lttng_cs_ctx_modes mode
)
139 const struct lttng_kernel_event_field
**event_fields
;
140 struct lttng_kernel_ctx_field ctx_field
;
141 struct field_data
*fdata
;
144 ret
= init_type(mode
);
147 event_fields
= lttng_cs_event_fields(mode
);
151 for (i
= 0; i
< NR_FIELDS
; i
++) {
152 if (lttng_kernel_find_context(*ctx
, event_fields
[i
]->name
))
155 fdata
= field_data_create(mode
);
160 memset(&ctx_field
, 0, sizeof(ctx_field
));
161 ctx_field
.event_field
= event_fields
[0];
162 ctx_field
.get_size
= lttng_callstack_length_get_size
;
163 ctx_field
.record
= lttng_callstack_length_record
;
164 ctx_field
.priv
= fdata
;
165 ret
= lttng_kernel_context_append(ctx
, &ctx_field
);
171 memset(&ctx_field
, 0, sizeof(ctx_field
));
172 ctx_field
.event_field
= event_fields
[1];
173 ctx_field
.get_size
= lttng_callstack_sequence_get_size
;
174 ctx_field
.record
= lttng_callstack_sequence_record
;
175 ctx_field
.destroy
= lttng_callstack_sequence_destroy
;
176 ctx_field
.priv
= fdata
;
177 ret
= lttng_kernel_context_append(ctx
, &ctx_field
);
185 lttng_kernel_context_remove_last(ctx
);
187 field_data_free(fdata
);
193 * lttng_add_callstack_to_ctx - add callstack event context
195 * @ctx: the lttng_ctx pointer to initialize
196 * @type: the context type
198 * Supported callstack type supported:
199 * LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL
200 * Records the callstack of the kernel
201 * LTTNG_KERNEL_CONTEXT_CALLSTACK_USER
202 * Records the callstack of the userspace program (from the kernel)
204 * Return 0 for success, or error code.
206 int lttng_add_callstack_to_ctx(struct lttng_kernel_ctx
**ctx
, int type
)
209 case LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_KERNEL
:
210 return __lttng_add_callstack_generic(ctx
, CALLSTACK_KERNEL
);
212 case LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_USER
:
213 return __lttng_add_callstack_generic(ctx
, CALLSTACK_USER
);
219 EXPORT_SYMBOL_GPL(lttng_add_callstack_to_ctx
);