1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
3 * lttng-context-callstack-legacy-impl.h
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).
11 * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
12 * Copyright (C) 2014 Francis Giraldeau <francis.giraldeau@gmail.com>
15 #define MAX_ENTRIES 128
17 enum lttng_cs_ctx_modes
{
23 struct lttng_cs_dispatch
{
24 struct stack_trace stack_trace
;
25 unsigned long entries
[MAX_ENTRIES
];
29 struct lttng_cs_dispatch dispatch
[RING_BUFFER_MAX_NESTING
];
33 struct lttng_cs __percpu
*cs_percpu
;
34 enum lttng_cs_ctx_modes mode
;
37 struct lttng_cs_type
{
39 const char *length_name
;
40 const char *save_func_name
;
41 void (*save_func
)(struct stack_trace
*trace
);
44 static struct lttng_cs_type cs_types
[] = {
46 .name
= "callstack_kernel",
47 .length_name
= "_callstack_kernel_length",
48 .save_func_name
= "save_stack_trace",
52 .name
= "callstack_user",
53 .length_name
= "_callstack_user_length",
54 .save_func_name
= "save_stack_trace_user",
60 int init_type(enum lttng_cs_ctx_modes mode
)
64 if (cs_types
[mode
].save_func
)
66 func
= kallsyms_lookup_funcptr(cs_types
[mode
].save_func_name
);
68 printk(KERN_WARNING
"LTTng: symbol lookup failed: %s\n",
69 cs_types
[mode
].save_func_name
);
72 cs_types
[mode
].save_func
= (void *) func
;
77 void lttng_cs_set_init(struct lttng_cs __percpu
*cs_set
)
81 for_each_possible_cpu(cpu
) {
84 cs
= per_cpu_ptr(cs_set
, cpu
);
85 for (i
= 0; i
< RING_BUFFER_MAX_NESTING
; i
++) {
86 struct lttng_cs_dispatch
*dispatch
;
88 dispatch
= &cs
->dispatch
[i
];
89 dispatch
->stack_trace
.entries
= dispatch
->entries
;
90 dispatch
->stack_trace
.max_entries
= MAX_ENTRIES
;
95 /* Keep track of nesting inside userspace callstack context code */
96 DEFINE_PER_CPU(int, callstack_user_nesting
);
99 * Note: these callbacks expect to be invoked with preemption disabled across
100 * get_size and record due to its use of a per-cpu stack.
103 struct stack_trace
*stack_trace_context(struct field_data
*fdata
, int cpu
)
105 int buffer_nesting
, cs_user_nesting
;
109 * Do not gather the userspace callstack context when the event was
110 * triggered by the userspace callstack context saving mechanism.
112 cs_user_nesting
= per_cpu(callstack_user_nesting
, cpu
);
114 if (fdata
->mode
== CALLSTACK_USER
&& cs_user_nesting
>= 1)
118 * get_cpu() is not required, preemption is already
119 * disabled while event is written.
121 * max nesting is checked in lib_ring_buffer_get_cpu().
122 * Check it again as a safety net.
124 cs
= per_cpu_ptr(fdata
->cs_percpu
, cpu
);
125 buffer_nesting
= per_cpu(lib_ring_buffer_nesting
, cpu
) - 1;
126 if (buffer_nesting
>= RING_BUFFER_MAX_NESTING
)
129 return &cs
->dispatch
[buffer_nesting
].stack_trace
;
133 size_t lttng_callstack_length_get_size(void *priv
, struct lttng_probe_ctx
*probe_ctx
, size_t offset
)
135 size_t orig_offset
= offset
;
137 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(unsigned int));
138 offset
+= sizeof(unsigned int);
139 return offset
- orig_offset
;
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.
147 size_t lttng_callstack_sequence_get_size(void *priv
, struct lttng_probe_ctx
*probe_ctx
, size_t offset
)
149 struct stack_trace
*trace
;
150 struct field_data
*fdata
= (struct field_data
*) priv
;
151 size_t orig_offset
= offset
;
152 int cpu
= smp_processor_id();
154 /* do not write data if no space is available */
155 trace
= stack_trace_context(fdata
, cpu
);
156 if (unlikely(!trace
)) {
157 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(unsigned long));
158 return offset
- orig_offset
;
161 /* reset stack trace, no need to clear memory */
162 trace
->nr_entries
= 0;
164 if (fdata
->mode
== CALLSTACK_USER
)
165 ++per_cpu(callstack_user_nesting
, cpu
);
167 /* do the real work and reserve space */
168 cs_types
[fdata
->mode
].save_func(trace
);
170 if (fdata
->mode
== CALLSTACK_USER
)
171 per_cpu(callstack_user_nesting
, cpu
)--;
174 * Remove final ULONG_MAX delimiter. If we cannot find it, add
175 * our own marker to show that the stack is incomplete. This is
176 * more compact for a trace.
178 if (trace
->nr_entries
> 0
179 && trace
->entries
[trace
->nr_entries
- 1] == ULONG_MAX
) {
182 offset
+= lib_ring_buffer_align(offset
, lttng_alignof(unsigned long));
183 offset
+= sizeof(unsigned long) * trace
->nr_entries
;
184 /* Add our own ULONG_MAX delimiter to show incomplete stack. */
185 if (trace
->nr_entries
== trace
->max_entries
)
186 offset
+= sizeof(unsigned long);
187 return offset
- orig_offset
;
191 void lttng_callstack_length_record(void *priv
, struct lttng_probe_ctx
*probe_ctx
,
192 struct lib_ring_buffer_ctx
*ctx
,
193 struct lttng_channel
*chan
)
195 int cpu
= ctx
->priv
.reserve_cpu
;
196 struct field_data
*fdata
= (struct field_data
*) priv
;
197 struct stack_trace
*trace
= stack_trace_context(fdata
, cpu
);
198 unsigned int nr_seq_entries
;
200 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(unsigned int));
201 if (unlikely(!trace
)) {
204 nr_seq_entries
= trace
->nr_entries
;
205 if (trace
->nr_entries
== trace
->max_entries
)
208 chan
->ops
->event_write(ctx
, &nr_seq_entries
, sizeof(unsigned int));
211 void lttng_callstack_sequence_record(void *priv
, struct lttng_probe_ctx
*probe_ctx
,
212 struct lib_ring_buffer_ctx
*ctx
,
213 struct lttng_channel
*chan
)
215 int cpu
= ctx
->priv
.reserve_cpu
;
216 struct field_data
*fdata
= (struct field_data
*) priv
;
217 struct stack_trace
*trace
= stack_trace_context(fdata
, cpu
);
218 unsigned int nr_seq_entries
;
220 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(unsigned long));
221 if (unlikely(!trace
)) {
224 nr_seq_entries
= trace
->nr_entries
;
225 if (trace
->nr_entries
== trace
->max_entries
)
227 chan
->ops
->event_write(ctx
, trace
->entries
,
228 sizeof(unsigned long) * trace
->nr_entries
);
229 /* Add our own ULONG_MAX delimiter to show incomplete stack. */
230 if (trace
->nr_entries
== trace
->max_entries
) {
231 unsigned long delim
= ULONG_MAX
;
233 chan
->ops
->event_write(ctx
, &delim
, sizeof(unsigned long));