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