Commit | Line | Data |
---|---|---|
b7cdc182 | 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
b6ee48d2 MD |
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; | |
ceabb767 | 39 | const char *length_name; |
b6ee48d2 MD |
40 | }; |
41 | ||
42 | static struct lttng_cs_type cs_types[] = { | |
43 | { | |
44 | .name = "callstack_kernel", | |
ceabb767 | 45 | .length_name = "_callstack_kernel_length", |
b6ee48d2 MD |
46 | }, |
47 | { | |
48 | .name = "callstack_user", | |
ceabb767 | 49 | .length_name = "_callstack_user_length", |
b6ee48d2 MD |
50 | }, |
51 | }; | |
52 | ||
b5a89a3f MD |
53 | static |
54 | const char *lttng_cs_ctx_mode_name(enum lttng_cs_ctx_modes mode) | |
55 | { | |
56 | return cs_types[mode].name; | |
57 | } | |
58 | ||
ceabb767 MD |
59 | static |
60 | const char *lttng_cs_ctx_mode_length_name(enum lttng_cs_ctx_modes mode) | |
61 | { | |
62 | return cs_types[mode].length_name; | |
63 | } | |
64 | ||
b5a89a3f MD |
65 | static |
66 | void lttng_cs_set_init(struct lttng_cs __percpu *cs_set) | |
67 | { | |
68 | int cpu, i; | |
69 | ||
70 | for_each_possible_cpu(cpu) { | |
71 | struct lttng_cs *cs; | |
72 | ||
73 | cs = per_cpu_ptr(cs_set, cpu); | |
74 | for (i = 0; i < RING_BUFFER_MAX_NESTING; i++) { | |
75 | struct lttng_cs_dispatch *dispatch; | |
76 | ||
77 | dispatch = &cs->dispatch[i]; | |
78 | dispatch->stack_trace.entries = dispatch->entries; | |
79 | dispatch->stack_trace.max_entries = MAX_ENTRIES; | |
80 | } | |
81 | } | |
82 | } | |
83 | ||
b6ee48d2 MD |
84 | /* Keep track of nesting inside userspace callstack context code */ |
85 | DEFINE_PER_CPU(int, callstack_user_nesting); | |
86 | ||
87 | static | |
88 | struct stack_trace *stack_trace_context(struct lttng_ctx_field *field, | |
89 | struct lib_ring_buffer_ctx *ctx) | |
90 | { | |
91 | int buffer_nesting, cs_user_nesting; | |
92 | struct lttng_cs *cs; | |
93 | struct field_data *fdata = field->priv; | |
94 | ||
95 | /* | |
96 | * Do not gather the userspace callstack context when the event was | |
97 | * triggered by the userspace callstack context saving mechanism. | |
98 | */ | |
99 | cs_user_nesting = per_cpu(callstack_user_nesting, ctx->cpu); | |
100 | ||
101 | if (fdata->mode == CALLSTACK_USER && cs_user_nesting >= 1) | |
102 | return NULL; | |
103 | ||
104 | /* | |
105 | * get_cpu() is not required, preemption is already | |
106 | * disabled while event is written. | |
107 | * | |
108 | * max nesting is checked in lib_ring_buffer_get_cpu(). | |
109 | * Check it again as a safety net. | |
110 | */ | |
111 | cs = per_cpu_ptr(fdata->cs_percpu, ctx->cpu); | |
112 | buffer_nesting = per_cpu(lib_ring_buffer_nesting, ctx->cpu) - 1; | |
113 | if (buffer_nesting >= RING_BUFFER_MAX_NESTING) | |
114 | return NULL; | |
115 | ||
116 | return &cs->dispatch[buffer_nesting].stack_trace; | |
117 | } | |
118 | ||
ceabb767 MD |
119 | static |
120 | size_t lttng_callstack_length_get_size(size_t offset, struct lttng_ctx_field *field, | |
121 | struct lib_ring_buffer_ctx *ctx, | |
122 | struct lttng_channel *chan) | |
123 | { | |
124 | size_t orig_offset = offset; | |
125 | ||
126 | offset += lib_ring_buffer_align(offset, lttng_alignof(unsigned int)); | |
127 | offset += sizeof(unsigned int); | |
128 | return offset - orig_offset; | |
129 | } | |
130 | ||
b6ee48d2 MD |
131 | /* |
132 | * In order to reserve the correct size, the callstack is computed. The | |
133 | * resulting callstack is saved to be accessed in the record step. | |
134 | */ | |
135 | static | |
ceabb767 MD |
136 | size_t lttng_callstack_sequence_get_size(size_t offset, struct lttng_ctx_field *field, |
137 | struct lib_ring_buffer_ctx *ctx, | |
138 | struct lttng_channel *chan) | |
b6ee48d2 MD |
139 | { |
140 | struct stack_trace *trace; | |
141 | struct field_data *fdata = field->priv; | |
142 | size_t orig_offset = offset; | |
143 | ||
144 | /* do not write data if no space is available */ | |
145 | trace = stack_trace_context(field, ctx); | |
146 | if (unlikely(!trace)) { | |
b6ee48d2 MD |
147 | offset += lib_ring_buffer_align(offset, lttng_alignof(unsigned long)); |
148 | return offset - orig_offset; | |
149 | } | |
150 | ||
151 | /* reset stack trace, no need to clear memory */ | |
152 | trace->nr_entries = 0; | |
153 | ||
45ce950f | 154 | if (fdata->mode == CALLSTACK_USER) { |
b6ee48d2 | 155 | ++per_cpu(callstack_user_nesting, ctx->cpu); |
45ce950f MD |
156 | /* do the real work and reserve space */ |
157 | save_stack_trace_user(trace); | |
b6ee48d2 | 158 | per_cpu(callstack_user_nesting, ctx->cpu)--; |
45ce950f MD |
159 | } else { |
160 | save_stack_trace(trace); | |
161 | } | |
b6ee48d2 MD |
162 | |
163 | /* | |
164 | * Remove final ULONG_MAX delimiter. If we cannot find it, add | |
165 | * our own marker to show that the stack is incomplete. This is | |
166 | * more compact for a trace. | |
167 | */ | |
168 | if (trace->nr_entries > 0 | |
169 | && trace->entries[trace->nr_entries - 1] == ULONG_MAX) { | |
170 | trace->nr_entries--; | |
171 | } | |
b6ee48d2 MD |
172 | offset += lib_ring_buffer_align(offset, lttng_alignof(unsigned long)); |
173 | offset += sizeof(unsigned long) * trace->nr_entries; | |
174 | /* Add our own ULONG_MAX delimiter to show incomplete stack. */ | |
175 | if (trace->nr_entries == trace->max_entries) | |
176 | offset += sizeof(unsigned long); | |
177 | return offset - orig_offset; | |
178 | } | |
179 | ||
180 | static | |
ceabb767 | 181 | void lttng_callstack_length_record(struct lttng_ctx_field *field, |
b6ee48d2 MD |
182 | struct lib_ring_buffer_ctx *ctx, |
183 | struct lttng_channel *chan) | |
184 | { | |
185 | struct stack_trace *trace = stack_trace_context(field, ctx); | |
186 | unsigned int nr_seq_entries; | |
187 | ||
ceabb767 | 188 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(unsigned int)); |
b6ee48d2 MD |
189 | if (unlikely(!trace)) { |
190 | nr_seq_entries = 0; | |
ceabb767 MD |
191 | } else { |
192 | nr_seq_entries = trace->nr_entries; | |
193 | if (trace->nr_entries == trace->max_entries) | |
194 | nr_seq_entries++; | |
195 | } | |
196 | chan->ops->event_write(ctx, &nr_seq_entries, sizeof(unsigned int)); | |
197 | } | |
198 | static | |
199 | void lttng_callstack_sequence_record(struct lttng_ctx_field *field, | |
200 | struct lib_ring_buffer_ctx *ctx, | |
201 | struct lttng_channel *chan) | |
202 | { | |
203 | struct stack_trace *trace = stack_trace_context(field, ctx); | |
204 | unsigned int nr_seq_entries; | |
205 | ||
206 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(unsigned long)); | |
207 | if (unlikely(!trace)) { | |
b6ee48d2 MD |
208 | return; |
209 | } | |
b6ee48d2 MD |
210 | nr_seq_entries = trace->nr_entries; |
211 | if (trace->nr_entries == trace->max_entries) | |
212 | nr_seq_entries++; | |
b6ee48d2 MD |
213 | chan->ops->event_write(ctx, trace->entries, |
214 | sizeof(unsigned long) * trace->nr_entries); | |
215 | /* Add our own ULONG_MAX delimiter to show incomplete stack. */ | |
216 | if (trace->nr_entries == trace->max_entries) { | |
217 | unsigned long delim = ULONG_MAX; | |
218 | ||
219 | chan->ops->event_write(ctx, &delim, sizeof(unsigned long)); | |
220 | } | |
221 | } |