Refactoring: type description structures
[lttng-modules.git] / src / lttng-context-callstack-stackwalk-impl.h
CommitLineData
b7cdc182 1/* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
b29c6286
MD
2 *
3 * lttng-context-callstack-stackwalk-impl.h
4 *
5 * LTTng callstack event context, stackwalk implementation. Targets
6 * kernels and architectures using the stacktrace common infrastructure
7 * introduced in the upstream Linux kernel by commit 214d8ca6ee
8 * "stacktrace: Provide common infrastructure" (merged in Linux 5.2,
9 * then gradually introduced within architectures).
10 *
11 * Copyright (C) 2014-2019 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
12 * Copyright (C) 2014 Francis Giraldeau <francis.giraldeau@gmail.com>
13 */
14
15#define MAX_ENTRIES 128
16
17enum lttng_cs_ctx_modes {
18 CALLSTACK_KERNEL = 0,
19 CALLSTACK_USER = 1,
20 NR_CALLSTACK_MODES,
21};
22
23struct lttng_stack_trace {
24 unsigned long entries[MAX_ENTRIES];
25 unsigned int nr_entries;
26};
27
28struct lttng_cs {
29 struct lttng_stack_trace stack_trace[RING_BUFFER_MAX_NESTING];
30};
31
32struct field_data {
33 struct lttng_cs __percpu *cs_percpu;
34 enum lttng_cs_ctx_modes mode;
35};
36
37static
38unsigned int (*save_func_kernel)(unsigned long *store, unsigned int size,
39 unsigned int skipnr);
40static
41unsigned int (*save_func_user)(unsigned long *store, unsigned int size);
42
b29c6286
MD
43static
44int init_type_callstack_kernel(void)
45{
46 unsigned long func;
47 const char *func_name = "stack_trace_save";
48
49 if (save_func_kernel)
50 return 0;
51 func = kallsyms_lookup_funcptr(func_name);
52 if (!func) {
53 printk(KERN_WARNING "LTTng: symbol lookup failed: %s\n",
54 func_name);
55 return -EINVAL;
56 }
57 save_func_kernel = (void *) func;
58 return 0;
59}
60
61static
62int init_type_callstack_user(void)
63{
64 unsigned long func;
65 const char *func_name = "stack_trace_save_user";
66
67 if (save_func_user)
68 return 0;
69 func = kallsyms_lookup_funcptr(func_name);
70 if (!func) {
71 printk(KERN_WARNING "LTTng: symbol lookup failed: %s\n",
72 func_name);
73 return -EINVAL;
74 }
75 save_func_user = (void *) func;
76 return 0;
77}
78
79static
80int init_type(enum lttng_cs_ctx_modes mode)
81{
82 switch (mode) {
83 case CALLSTACK_KERNEL:
84 return init_type_callstack_kernel();
85 case CALLSTACK_USER:
86 return init_type_callstack_user();
87 default:
88 return -EINVAL;
89 }
90}
91
92static
93void lttng_cs_set_init(struct lttng_cs __percpu *cs_set)
94{
95}
96
97/* Keep track of nesting inside userspace callstack context code */
98DEFINE_PER_CPU(int, callstack_user_nesting);
99
100static
437d5aa5 101struct lttng_stack_trace *stack_trace_context(struct lttng_kernel_ctx_field *field,
b29c6286
MD
102 struct lib_ring_buffer_ctx *ctx)
103{
104 int buffer_nesting, cs_user_nesting;
105 struct lttng_cs *cs;
106 struct field_data *fdata = field->priv;
107
108 /*
109 * Do not gather the userspace callstack context when the event was
110 * triggered by the userspace callstack context saving mechanism.
111 */
112 cs_user_nesting = per_cpu(callstack_user_nesting, ctx->cpu);
113
114 if (fdata->mode == CALLSTACK_USER && cs_user_nesting >= 1)
115 return NULL;
116
117 /*
118 * get_cpu() is not required, preemption is already
119 * disabled while event is written.
120 *
121 * max nesting is checked in lib_ring_buffer_get_cpu().
122 * Check it again as a safety net.
123 */
124 cs = per_cpu_ptr(fdata->cs_percpu, ctx->cpu);
125 buffer_nesting = per_cpu(lib_ring_buffer_nesting, ctx->cpu) - 1;
126 if (buffer_nesting >= RING_BUFFER_MAX_NESTING)
127 return NULL;
128
129 return &cs->stack_trace[buffer_nesting];
130}
131
ceabb767 132static
437d5aa5 133size_t lttng_callstack_length_get_size(size_t offset, struct lttng_kernel_ctx_field *field,
ceabb767
MD
134 struct lib_ring_buffer_ctx *ctx,
135 struct lttng_channel *chan)
136{
137 size_t orig_offset = offset;
138
139 offset += lib_ring_buffer_align(offset, lttng_alignof(unsigned int));
140 offset += sizeof(unsigned int);
141 return offset - orig_offset;
142}
143
b29c6286
MD
144/*
145 * In order to reserve the correct size, the callstack is computed. The
146 * resulting callstack is saved to be accessed in the record step.
147 */
148static
437d5aa5 149size_t lttng_callstack_sequence_get_size(size_t offset, struct lttng_kernel_ctx_field *field,
ceabb767
MD
150 struct lib_ring_buffer_ctx *ctx,
151 struct lttng_channel *chan)
b29c6286
MD
152{
153 struct lttng_stack_trace *trace;
154 struct field_data *fdata = field->priv;
155 size_t orig_offset = offset;
156
157 /* do not write data if no space is available */
158 trace = stack_trace_context(field, ctx);
159 if (unlikely(!trace)) {
b29c6286
MD
160 offset += lib_ring_buffer_align(offset, lttng_alignof(unsigned long));
161 return offset - orig_offset;
162 }
163
164 /* reset stack trace, no need to clear memory */
165 trace->nr_entries = 0;
166
167 switch (fdata->mode) {
168 case CALLSTACK_KERNEL:
169 /* do the real work and reserve space */
170 trace->nr_entries = save_func_kernel(trace->entries,
171 MAX_ENTRIES, 0);
172 break;
173 case CALLSTACK_USER:
174 ++per_cpu(callstack_user_nesting, ctx->cpu);
175 /* do the real work and reserve space */
176 trace->nr_entries = save_func_user(trace->entries,
177 MAX_ENTRIES);
178 per_cpu(callstack_user_nesting, ctx->cpu)--;
179 break;
180 default:
181 WARN_ON_ONCE(1);
182 }
183
184 /*
185 * If the array is filled, add our own marker to show that the
186 * stack is incomplete.
187 */
b29c6286
MD
188 offset += lib_ring_buffer_align(offset, lttng_alignof(unsigned long));
189 offset += sizeof(unsigned long) * trace->nr_entries;
190 /* Add our own ULONG_MAX delimiter to show incomplete stack. */
191 if (trace->nr_entries == MAX_ENTRIES)
192 offset += sizeof(unsigned long);
193 return offset - orig_offset;
194}
195
196static
437d5aa5 197void lttng_callstack_length_record(struct lttng_kernel_ctx_field *field,
b29c6286
MD
198 struct lib_ring_buffer_ctx *ctx,
199 struct lttng_channel *chan)
200{
201 struct lttng_stack_trace *trace = stack_trace_context(field, ctx);
202 unsigned int nr_seq_entries;
203
ceabb767 204 lib_ring_buffer_align_ctx(ctx, lttng_alignof(unsigned int));
b29c6286
MD
205 if (unlikely(!trace)) {
206 nr_seq_entries = 0;
ceabb767
MD
207 } else {
208 nr_seq_entries = trace->nr_entries;
209 if (trace->nr_entries == MAX_ENTRIES)
210 nr_seq_entries++;
211 }
212 chan->ops->event_write(ctx, &nr_seq_entries, sizeof(unsigned int));
213}
214
215static
437d5aa5 216void lttng_callstack_sequence_record(struct lttng_kernel_ctx_field *field,
ceabb767
MD
217 struct lib_ring_buffer_ctx *ctx,
218 struct lttng_channel *chan)
219{
220 struct lttng_stack_trace *trace = stack_trace_context(field, ctx);
221 unsigned int nr_seq_entries;
222
223 lib_ring_buffer_align_ctx(ctx, lttng_alignof(unsigned long));
224 if (unlikely(!trace)) {
b29c6286
MD
225 return;
226 }
b29c6286
MD
227 nr_seq_entries = trace->nr_entries;
228 if (trace->nr_entries == MAX_ENTRIES)
229 nr_seq_entries++;
b29c6286
MD
230 chan->ops->event_write(ctx, trace->entries,
231 sizeof(unsigned long) * trace->nr_entries);
232 /* Add our own ULONG_MAX delimiter to show incomplete stack. */
233 if (trace->nr_entries == MAX_ENTRIES) {
234 unsigned long delim = ULONG_MAX;
235
236 chan->ops->event_write(ctx, &delim, sizeof(unsigned long));
237 }
238}
This page took 0.039837 seconds and 4 git commands to generate.