blob: 4c826a2e08d8bc1f3c43b1ea8b8eb8b8826602cd [file] [log] [blame]
Thomas Gleixner2025cf92019-05-29 07:18:02 -07001// SPDX-License-Identifier: GPL-2.0-only
Adrian Hunter00447cc2014-10-30 16:09:42 +02002/*
3 * thread-stack.c: Synthesize a thread's stack using call / return events
4 * Copyright (c) 2014, Intel Corporation.
Adrian Hunter00447cc2014-10-30 16:09:42 +02005 */
6
Adrian Hunter92a9e4f2014-10-30 16:09:45 +02007#include <linux/rbtree.h>
8#include <linux/list.h>
Adrian Hunter256d92b2018-12-21 14:06:19 +02009#include <linux/log2.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030010#include <errno.h>
Adrian Hunter00447cc2014-10-30 16:09:42 +020011#include "thread.h"
12#include "event.h"
Adrian Hunter92a9e4f2014-10-30 16:09:45 +020013#include "machine.h"
Adrian Hunter3c0cd952019-01-09 11:18:35 +020014#include "env.h"
Adrian Hunter00447cc2014-10-30 16:09:42 +020015#include "util.h"
16#include "debug.h"
Adrian Hunter92a9e4f2014-10-30 16:09:45 +020017#include "symbol.h"
18#include "comm.h"
Chris Phlipot451db122016-04-28 01:19:07 -070019#include "call-path.h"
Adrian Hunter00447cc2014-10-30 16:09:42 +020020#include "thread-stack.h"
21
Adrian Hunter92a9e4f2014-10-30 16:09:45 +020022#define STACK_GROWTH 2048
23
Adrian Hunter3c0cd952019-01-09 11:18:35 +020024/*
25 * State of retpoline detection.
26 *
27 * RETPOLINE_NONE: no retpoline detection
28 * X86_RETPOLINE_POSSIBLE: x86 retpoline possible
29 * X86_RETPOLINE_DETECTED: x86 retpoline detected
30 */
31enum retpoline_state_t {
32 RETPOLINE_NONE,
33 X86_RETPOLINE_POSSIBLE,
34 X86_RETPOLINE_DETECTED,
35};
36
Adrian Hunter92a9e4f2014-10-30 16:09:45 +020037/**
38 * struct thread_stack_entry - thread stack entry.
39 * @ret_addr: return address
40 * @timestamp: timestamp (if known)
41 * @ref: external reference (e.g. db_id of sample)
42 * @branch_count: the branch count when the entry was created
Adrian Hunter003ccdc2019-05-20 14:37:19 +030043 * @insn_count: the instruction count when the entry was created
44 * @cyc_count the cycle count when the entry was created
Adrian Hunterf4358872019-02-28 15:00:24 +020045 * @db_id: id used for db-export
Adrian Hunter92a9e4f2014-10-30 16:09:45 +020046 * @cp: call path
47 * @no_call: a 'call' was not seen
Adrian Hunter4d60e5e2018-09-20 16:00:45 +030048 * @trace_end: a 'call' but trace ended
Adrian Hunterf08046c2019-01-09 11:18:33 +020049 * @non_call: a branch but not a 'call' to the start of a different symbol
Adrian Hunter92a9e4f2014-10-30 16:09:45 +020050 */
51struct thread_stack_entry {
52 u64 ret_addr;
53 u64 timestamp;
54 u64 ref;
55 u64 branch_count;
Adrian Hunter003ccdc2019-05-20 14:37:19 +030056 u64 insn_count;
57 u64 cyc_count;
Adrian Hunterf4358872019-02-28 15:00:24 +020058 u64 db_id;
Adrian Hunter92a9e4f2014-10-30 16:09:45 +020059 struct call_path *cp;
60 bool no_call;
Adrian Hunter4d60e5e2018-09-20 16:00:45 +030061 bool trace_end;
Adrian Hunterf08046c2019-01-09 11:18:33 +020062 bool non_call;
Adrian Hunter92a9e4f2014-10-30 16:09:45 +020063};
64
65/**
66 * struct thread_stack - thread stack constructed from 'call' and 'return'
67 * branch samples.
68 * @stack: array that holds the stack
69 * @cnt: number of entries in the stack
70 * @sz: current maximum stack size
71 * @trace_nr: current trace number
72 * @branch_count: running branch count
Adrian Hunter003ccdc2019-05-20 14:37:19 +030073 * @insn_count: running instruction count
74 * @cyc_count running cycle count
Adrian Hunter92a9e4f2014-10-30 16:09:45 +020075 * @kernel_start: kernel start address
76 * @last_time: last timestamp
77 * @crp: call/return processor
78 * @comm: current comm
Adrian Hunterf6060ac2018-12-21 14:06:16 +020079 * @arr_sz: size of array if this is the first element of an array
Adrian Hunter3c0cd952019-01-09 11:18:35 +020080 * @rstate: used to detect retpolines
Adrian Hunter92a9e4f2014-10-30 16:09:45 +020081 */
Adrian Hunter00447cc2014-10-30 16:09:42 +020082struct thread_stack {
83 struct thread_stack_entry *stack;
84 size_t cnt;
85 size_t sz;
86 u64 trace_nr;
Adrian Hunter92a9e4f2014-10-30 16:09:45 +020087 u64 branch_count;
Adrian Hunter003ccdc2019-05-20 14:37:19 +030088 u64 insn_count;
89 u64 cyc_count;
Adrian Hunter92a9e4f2014-10-30 16:09:45 +020090 u64 kernel_start;
91 u64 last_time;
92 struct call_return_processor *crp;
93 struct comm *comm;
Adrian Hunterf6060ac2018-12-21 14:06:16 +020094 unsigned int arr_sz;
Adrian Hunter3c0cd952019-01-09 11:18:35 +020095 enum retpoline_state_t rstate;
Adrian Hunter00447cc2014-10-30 16:09:42 +020096};
97
Adrian Hunter256d92b2018-12-21 14:06:19 +020098/*
99 * Assume pid == tid == 0 identifies the idle task as defined by
100 * perf_session__register_idle_thread(). The idle task is really 1 task per cpu,
101 * and therefore requires a stack for each cpu.
102 */
103static inline bool thread_stack__per_cpu(struct thread *thread)
104{
105 return !(thread->tid || thread->pid_);
106}
107
Adrian Hunter00447cc2014-10-30 16:09:42 +0200108static int thread_stack__grow(struct thread_stack *ts)
109{
110 struct thread_stack_entry *new_stack;
111 size_t sz, new_sz;
112
113 new_sz = ts->sz + STACK_GROWTH;
114 sz = new_sz * sizeof(struct thread_stack_entry);
115
116 new_stack = realloc(ts->stack, sz);
117 if (!new_stack)
118 return -ENOMEM;
119
120 ts->stack = new_stack;
121 ts->sz = new_sz;
122
123 return 0;
124}
125
Adrian Hunter2e9e8682018-12-21 14:06:17 +0200126static int thread_stack__init(struct thread_stack *ts, struct thread *thread,
127 struct call_return_processor *crp)
128{
129 int err;
130
131 err = thread_stack__grow(ts);
132 if (err)
133 return err;
134
Adrian Hunter3c0cd952019-01-09 11:18:35 +0200135 if (thread->mg && thread->mg->machine) {
136 struct machine *machine = thread->mg->machine;
137 const char *arch = perf_env__arch(machine->env);
138
139 ts->kernel_start = machine__kernel_start(machine);
140 if (!strcmp(arch, "x86"))
141 ts->rstate = X86_RETPOLINE_POSSIBLE;
142 } else {
Adrian Hunter2e9e8682018-12-21 14:06:17 +0200143 ts->kernel_start = 1ULL << 63;
Adrian Hunter3c0cd952019-01-09 11:18:35 +0200144 }
Adrian Hunter2e9e8682018-12-21 14:06:17 +0200145 ts->crp = crp;
146
147 return 0;
148}
149
Adrian Hunter256d92b2018-12-21 14:06:19 +0200150static struct thread_stack *thread_stack__new(struct thread *thread, int cpu,
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200151 struct call_return_processor *crp)
Adrian Hunter00447cc2014-10-30 16:09:42 +0200152{
Adrian Hunter139f42f2018-12-21 14:06:18 +0200153 struct thread_stack *ts = thread->ts, *new_ts;
154 unsigned int old_sz = ts ? ts->arr_sz : 0;
155 unsigned int new_sz = 1;
Adrian Hunter00447cc2014-10-30 16:09:42 +0200156
Adrian Hunter256d92b2018-12-21 14:06:19 +0200157 if (thread_stack__per_cpu(thread) && cpu > 0)
158 new_sz = roundup_pow_of_two(cpu + 1);
159
Adrian Hunter139f42f2018-12-21 14:06:18 +0200160 if (!ts || new_sz > old_sz) {
161 new_ts = calloc(new_sz, sizeof(*ts));
162 if (!new_ts)
163 return NULL;
164 if (ts)
165 memcpy(new_ts, ts, old_sz * sizeof(*ts));
166 new_ts->arr_sz = new_sz;
167 zfree(&thread->ts);
168 thread->ts = new_ts;
169 ts = new_ts;
Adrian Hunter00447cc2014-10-30 16:09:42 +0200170 }
171
Adrian Hunter256d92b2018-12-21 14:06:19 +0200172 if (thread_stack__per_cpu(thread) && cpu > 0 &&
173 (unsigned int)cpu < ts->arr_sz)
174 ts += cpu;
175
Adrian Hunter139f42f2018-12-21 14:06:18 +0200176 if (!ts->stack &&
177 thread_stack__init(ts, thread, crp))
178 return NULL;
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200179
Adrian Hunter00447cc2014-10-30 16:09:42 +0200180 return ts;
181}
182
Adrian Hunter256d92b2018-12-21 14:06:19 +0200183static struct thread_stack *thread__cpu_stack(struct thread *thread, int cpu)
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200184{
Adrian Hunter256d92b2018-12-21 14:06:19 +0200185 struct thread_stack *ts = thread->ts;
186
187 if (cpu < 0)
188 cpu = 0;
189
190 if (!ts || (unsigned int)cpu >= ts->arr_sz)
191 return NULL;
192
193 ts += cpu;
194
195 if (!ts->stack)
196 return NULL;
197
198 return ts;
199}
200
201static inline struct thread_stack *thread__stack(struct thread *thread,
202 int cpu)
203{
204 if (!thread)
205 return NULL;
206
207 if (thread_stack__per_cpu(thread))
208 return thread__cpu_stack(thread, cpu);
209
210 return thread->ts;
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200211}
212
Adrian Hunter4d60e5e2018-09-20 16:00:45 +0300213static int thread_stack__push(struct thread_stack *ts, u64 ret_addr,
214 bool trace_end)
Adrian Hunter00447cc2014-10-30 16:09:42 +0200215{
216 int err = 0;
217
218 if (ts->cnt == ts->sz) {
219 err = thread_stack__grow(ts);
220 if (err) {
221 pr_warning("Out of memory: discarding thread stack\n");
222 ts->cnt = 0;
223 }
224 }
225
Adrian Hunter4d60e5e2018-09-20 16:00:45 +0300226 ts->stack[ts->cnt].trace_end = trace_end;
Adrian Hunter00447cc2014-10-30 16:09:42 +0200227 ts->stack[ts->cnt++].ret_addr = ret_addr;
228
229 return err;
230}
231
232static void thread_stack__pop(struct thread_stack *ts, u64 ret_addr)
233{
234 size_t i;
235
236 /*
237 * In some cases there may be functions which are not seen to return.
238 * For example when setjmp / longjmp has been used. Or the perf context
239 * switch in the kernel which doesn't stop and start tracing in exactly
240 * the same code path. When that happens the return address will be
241 * further down the stack. If the return address is not found at all,
242 * we assume the opposite (i.e. this is a return for a call that wasn't
243 * seen for some reason) and leave the stack alone.
244 */
245 for (i = ts->cnt; i; ) {
246 if (ts->stack[--i].ret_addr == ret_addr) {
247 ts->cnt = i;
248 return;
249 }
250 }
251}
252
Adrian Hunter4d60e5e2018-09-20 16:00:45 +0300253static void thread_stack__pop_trace_end(struct thread_stack *ts)
254{
255 size_t i;
256
257 for (i = ts->cnt; i; ) {
258 if (ts->stack[--i].trace_end)
259 ts->cnt = i;
260 else
261 return;
262 }
263}
264
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200265static bool thread_stack__in_kernel(struct thread_stack *ts)
266{
267 if (!ts->cnt)
268 return false;
269
270 return ts->stack[ts->cnt - 1].cp->in_kernel;
271}
272
273static int thread_stack__call_return(struct thread *thread,
274 struct thread_stack *ts, size_t idx,
275 u64 timestamp, u64 ref, bool no_return)
276{
277 struct call_return_processor *crp = ts->crp;
278 struct thread_stack_entry *tse;
279 struct call_return cr = {
280 .thread = thread,
281 .comm = ts->comm,
282 .db_id = 0,
283 };
Adrian Hunterf4358872019-02-28 15:00:24 +0200284 u64 *parent_db_id;
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200285
286 tse = &ts->stack[idx];
287 cr.cp = tse->cp;
288 cr.call_time = tse->timestamp;
289 cr.return_time = timestamp;
290 cr.branch_count = ts->branch_count - tse->branch_count;
Adrian Hunter003ccdc2019-05-20 14:37:19 +0300291 cr.insn_count = ts->insn_count - tse->insn_count;
292 cr.cyc_count = ts->cyc_count - tse->cyc_count;
Adrian Hunterf4358872019-02-28 15:00:24 +0200293 cr.db_id = tse->db_id;
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200294 cr.call_ref = tse->ref;
295 cr.return_ref = ref;
296 if (tse->no_call)
297 cr.flags |= CALL_RETURN_NO_CALL;
298 if (no_return)
299 cr.flags |= CALL_RETURN_NO_RETURN;
Adrian Hunterf08046c2019-01-09 11:18:33 +0200300 if (tse->non_call)
301 cr.flags |= CALL_RETURN_NON_CALL;
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200302
Adrian Hunterf4358872019-02-28 15:00:24 +0200303 /*
304 * The parent db_id must be assigned before exporting the child. Note
305 * it is not possible to export the parent first because its information
306 * is not yet complete because its 'return' has not yet been processed.
307 */
308 parent_db_id = idx ? &(tse - 1)->db_id : NULL;
309
310 return crp->process(&cr, parent_db_id, crp->data);
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200311}
312
Adrian Huntera5499b32015-05-29 16:33:30 +0300313static int __thread_stack__flush(struct thread *thread, struct thread_stack *ts)
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200314{
315 struct call_return_processor *crp = ts->crp;
316 int err;
317
318 if (!crp) {
319 ts->cnt = 0;
320 return 0;
321 }
322
323 while (ts->cnt) {
324 err = thread_stack__call_return(thread, ts, --ts->cnt,
325 ts->last_time, 0, true);
326 if (err) {
327 pr_err("Error flushing thread stack!\n");
328 ts->cnt = 0;
329 return err;
330 }
331 }
332
333 return 0;
334}
335
Adrian Huntera5499b32015-05-29 16:33:30 +0300336int thread_stack__flush(struct thread *thread)
337{
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200338 struct thread_stack *ts = thread->ts;
Adrian Hunterf6060ac2018-12-21 14:06:16 +0200339 unsigned int pos;
340 int err = 0;
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200341
Adrian Hunterf6060ac2018-12-21 14:06:16 +0200342 if (ts) {
343 for (pos = 0; pos < ts->arr_sz; pos++) {
344 int ret = __thread_stack__flush(thread, ts + pos);
Adrian Huntera5499b32015-05-29 16:33:30 +0300345
Adrian Hunterf6060ac2018-12-21 14:06:16 +0200346 if (ret)
347 err = ret;
348 }
349 }
350
351 return err;
Adrian Huntera5499b32015-05-29 16:33:30 +0300352}
353
Adrian Hunter256d92b2018-12-21 14:06:19 +0200354int thread_stack__event(struct thread *thread, int cpu, u32 flags, u64 from_ip,
Adrian Hunter00447cc2014-10-30 16:09:42 +0200355 u64 to_ip, u16 insn_len, u64 trace_nr)
356{
Adrian Hunter256d92b2018-12-21 14:06:19 +0200357 struct thread_stack *ts = thread__stack(thread, cpu);
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200358
Adrian Hunter00447cc2014-10-30 16:09:42 +0200359 if (!thread)
360 return -EINVAL;
361
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200362 if (!ts) {
Adrian Hunter256d92b2018-12-21 14:06:19 +0200363 ts = thread_stack__new(thread, cpu, NULL);
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200364 if (!ts) {
Adrian Hunter00447cc2014-10-30 16:09:42 +0200365 pr_warning("Out of memory: no thread stack\n");
366 return -ENOMEM;
367 }
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200368 ts->trace_nr = trace_nr;
Adrian Hunter00447cc2014-10-30 16:09:42 +0200369 }
370
371 /*
372 * When the trace is discontinuous, the trace_nr changes. In that case
373 * the stack might be completely invalid. Better to report nothing than
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200374 * to report something misleading, so flush the stack.
Adrian Hunter00447cc2014-10-30 16:09:42 +0200375 */
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200376 if (trace_nr != ts->trace_nr) {
377 if (ts->trace_nr)
378 __thread_stack__flush(thread, ts);
379 ts->trace_nr = trace_nr;
Adrian Hunter00447cc2014-10-30 16:09:42 +0200380 }
381
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200382 /* Stop here if thread_stack__process() is in use */
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200383 if (ts->crp)
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200384 return 0;
385
Adrian Hunter00447cc2014-10-30 16:09:42 +0200386 if (flags & PERF_IP_FLAG_CALL) {
387 u64 ret_addr;
388
389 if (!to_ip)
390 return 0;
391 ret_addr = from_ip + insn_len;
392 if (ret_addr == to_ip)
393 return 0; /* Zero-length calls are excluded */
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200394 return thread_stack__push(ts, ret_addr,
Adrian Hunter4d60e5e2018-09-20 16:00:45 +0300395 flags & PERF_IP_FLAG_TRACE_END);
396 } else if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
397 /*
398 * If the caller did not change the trace number (which would
399 * have flushed the stack) then try to make sense of the stack.
400 * Possibly, tracing began after returning to the current
401 * address, so try to pop that. Also, do not expect a call made
402 * when the trace ended, to return, so pop that.
403 */
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200404 thread_stack__pop(ts, to_ip);
405 thread_stack__pop_trace_end(ts);
Adrian Hunter4d60e5e2018-09-20 16:00:45 +0300406 } else if ((flags & PERF_IP_FLAG_RETURN) && from_ip) {
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200407 thread_stack__pop(ts, to_ip);
Adrian Hunter00447cc2014-10-30 16:09:42 +0200408 }
409
410 return 0;
411}
412
Adrian Hunter256d92b2018-12-21 14:06:19 +0200413void thread_stack__set_trace_nr(struct thread *thread, int cpu, u64 trace_nr)
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200414{
Adrian Hunter256d92b2018-12-21 14:06:19 +0200415 struct thread_stack *ts = thread__stack(thread, cpu);
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200416
417 if (!ts)
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200418 return;
419
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200420 if (trace_nr != ts->trace_nr) {
421 if (ts->trace_nr)
422 __thread_stack__flush(thread, ts);
423 ts->trace_nr = trace_nr;
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200424 }
425}
426
Adrian Hunterf6060ac2018-12-21 14:06:16 +0200427static void __thread_stack__free(struct thread *thread, struct thread_stack *ts)
428{
429 __thread_stack__flush(thread, ts);
430 zfree(&ts->stack);
431}
432
433static void thread_stack__reset(struct thread *thread, struct thread_stack *ts)
434{
435 unsigned int arr_sz = ts->arr_sz;
436
437 __thread_stack__free(thread, ts);
438 memset(ts, 0, sizeof(*ts));
439 ts->arr_sz = arr_sz;
440}
441
Adrian Hunter00447cc2014-10-30 16:09:42 +0200442void thread_stack__free(struct thread *thread)
443{
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200444 struct thread_stack *ts = thread->ts;
Adrian Hunterf6060ac2018-12-21 14:06:16 +0200445 unsigned int pos;
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200446
447 if (ts) {
Adrian Hunterf6060ac2018-12-21 14:06:16 +0200448 for (pos = 0; pos < ts->arr_sz; pos++)
449 __thread_stack__free(thread, ts + pos);
Adrian Hunter00447cc2014-10-30 16:09:42 +0200450 zfree(&thread->ts);
451 }
452}
453
Adrian Hunter24248302018-10-31 11:10:42 +0200454static inline u64 callchain_context(u64 ip, u64 kernel_start)
Adrian Hunter00447cc2014-10-30 16:09:42 +0200455{
Adrian Hunter24248302018-10-31 11:10:42 +0200456 return ip < kernel_start ? PERF_CONTEXT_USER : PERF_CONTEXT_KERNEL;
457}
Adrian Hunter00447cc2014-10-30 16:09:42 +0200458
Adrian Hunter256d92b2018-12-21 14:06:19 +0200459void thread_stack__sample(struct thread *thread, int cpu,
460 struct ip_callchain *chain,
Adrian Hunter24248302018-10-31 11:10:42 +0200461 size_t sz, u64 ip, u64 kernel_start)
462{
Adrian Hunter256d92b2018-12-21 14:06:19 +0200463 struct thread_stack *ts = thread__stack(thread, cpu);
Adrian Hunter24248302018-10-31 11:10:42 +0200464 u64 context = callchain_context(ip, kernel_start);
465 u64 last_context;
466 size_t i, j;
Adrian Hunter00447cc2014-10-30 16:09:42 +0200467
Adrian Hunter24248302018-10-31 11:10:42 +0200468 if (sz < 2) {
469 chain->nr = 0;
470 return;
471 }
Adrian Hunter00447cc2014-10-30 16:09:42 +0200472
Adrian Hunter24248302018-10-31 11:10:42 +0200473 chain->ips[0] = context;
474 chain->ips[1] = ip;
475
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200476 if (!ts) {
Adrian Hunter24248302018-10-31 11:10:42 +0200477 chain->nr = 2;
478 return;
479 }
480
481 last_context = context;
482
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200483 for (i = 2, j = 1; i < sz && j <= ts->cnt; i++, j++) {
484 ip = ts->stack[ts->cnt - j].ret_addr;
Adrian Hunter24248302018-10-31 11:10:42 +0200485 context = callchain_context(ip, kernel_start);
486 if (context != last_context) {
487 if (i >= sz - 1)
488 break;
489 chain->ips[i++] = context;
490 last_context = context;
491 }
492 chain->ips[i] = ip;
493 }
494
495 chain->nr = i;
Adrian Hunter00447cc2014-10-30 16:09:42 +0200496}
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200497
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200498struct call_return_processor *
Adrian Hunterf4358872019-02-28 15:00:24 +0200499call_return_processor__new(int (*process)(struct call_return *cr, u64 *parent_db_id, void *data),
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200500 void *data)
501{
502 struct call_return_processor *crp;
503
504 crp = zalloc(sizeof(struct call_return_processor));
505 if (!crp)
506 return NULL;
507 crp->cpr = call_path_root__new();
508 if (!crp->cpr)
509 goto out_free;
510 crp->process = process;
511 crp->data = data;
512 return crp;
513
514out_free:
515 free(crp);
516 return NULL;
517}
518
519void call_return_processor__free(struct call_return_processor *crp)
520{
521 if (crp) {
522 call_path_root__free(crp->cpr);
523 free(crp);
524 }
525}
526
527static int thread_stack__push_cp(struct thread_stack *ts, u64 ret_addr,
528 u64 timestamp, u64 ref, struct call_path *cp,
Adrian Hunter2dcde4e2018-09-20 16:00:46 +0300529 bool no_call, bool trace_end)
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200530{
531 struct thread_stack_entry *tse;
532 int err;
533
Adrian Huntere7a3a052019-01-09 11:18:31 +0200534 if (!cp)
535 return -ENOMEM;
536
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200537 if (ts->cnt == ts->sz) {
538 err = thread_stack__grow(ts);
539 if (err)
540 return err;
541 }
542
543 tse = &ts->stack[ts->cnt++];
544 tse->ret_addr = ret_addr;
545 tse->timestamp = timestamp;
546 tse->ref = ref;
547 tse->branch_count = ts->branch_count;
Adrian Hunter003ccdc2019-05-20 14:37:19 +0300548 tse->insn_count = ts->insn_count;
549 tse->cyc_count = ts->cyc_count;
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200550 tse->cp = cp;
551 tse->no_call = no_call;
Adrian Hunter2dcde4e2018-09-20 16:00:46 +0300552 tse->trace_end = trace_end;
Adrian Hunterf08046c2019-01-09 11:18:33 +0200553 tse->non_call = false;
Adrian Hunterf4358872019-02-28 15:00:24 +0200554 tse->db_id = 0;
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200555
556 return 0;
557}
558
559static int thread_stack__pop_cp(struct thread *thread, struct thread_stack *ts,
560 u64 ret_addr, u64 timestamp, u64 ref,
561 struct symbol *sym)
562{
563 int err;
564
565 if (!ts->cnt)
566 return 1;
567
568 if (ts->cnt == 1) {
569 struct thread_stack_entry *tse = &ts->stack[0];
570
571 if (tse->cp->sym == sym)
572 return thread_stack__call_return(thread, ts, --ts->cnt,
573 timestamp, ref, false);
574 }
575
Adrian Hunterf08046c2019-01-09 11:18:33 +0200576 if (ts->stack[ts->cnt - 1].ret_addr == ret_addr &&
577 !ts->stack[ts->cnt - 1].non_call) {
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200578 return thread_stack__call_return(thread, ts, --ts->cnt,
579 timestamp, ref, false);
580 } else {
581 size_t i = ts->cnt - 1;
582
583 while (i--) {
Adrian Hunterf08046c2019-01-09 11:18:33 +0200584 if (ts->stack[i].ret_addr != ret_addr ||
585 ts->stack[i].non_call)
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200586 continue;
587 i += 1;
588 while (ts->cnt > i) {
589 err = thread_stack__call_return(thread, ts,
590 --ts->cnt,
591 timestamp, ref,
592 true);
593 if (err)
594 return err;
595 }
596 return thread_stack__call_return(thread, ts, --ts->cnt,
597 timestamp, ref, false);
598 }
599 }
600
601 return 1;
602}
603
Adrian Huntere0b89512018-12-21 14:06:14 +0200604static int thread_stack__bottom(struct thread_stack *ts,
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200605 struct perf_sample *sample,
606 struct addr_location *from_al,
607 struct addr_location *to_al, u64 ref)
608{
609 struct call_path_root *cpr = ts->crp->cpr;
610 struct call_path *cp;
611 struct symbol *sym;
612 u64 ip;
613
614 if (sample->ip) {
615 ip = sample->ip;
616 sym = from_al->sym;
617 } else if (sample->addr) {
618 ip = sample->addr;
619 sym = to_al->sym;
620 } else {
621 return 0;
622 }
623
624 cp = call_path__findnew(cpr, &cpr->call_path, sym, ip,
625 ts->kernel_start);
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200626
Adrian Huntere0b89512018-12-21 14:06:14 +0200627 return thread_stack__push_cp(ts, ip, sample->time, ref, cp,
Adrian Hunter2dcde4e2018-09-20 16:00:46 +0300628 true, false);
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200629}
630
Adrian Hunter97860b42019-06-19 09:44:28 +0300631static int thread_stack__pop_ks(struct thread *thread, struct thread_stack *ts,
632 struct perf_sample *sample, u64 ref)
633{
634 u64 tm = sample->time;
635 int err;
636
637 /* Return to userspace, so pop all kernel addresses */
638 while (thread_stack__in_kernel(ts)) {
639 err = thread_stack__call_return(thread, ts, --ts->cnt,
640 tm, ref, true);
641 if (err)
642 return err;
643 }
644
645 return 0;
646}
647
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200648static int thread_stack__no_call_return(struct thread *thread,
649 struct thread_stack *ts,
650 struct perf_sample *sample,
651 struct addr_location *from_al,
652 struct addr_location *to_al, u64 ref)
653{
654 struct call_path_root *cpr = ts->crp->cpr;
Adrian Hunter90c2cda2019-01-09 11:18:32 +0200655 struct call_path *root = &cpr->call_path;
656 struct symbol *fsym = from_al->sym;
657 struct symbol *tsym = to_al->sym;
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200658 struct call_path *cp, *parent;
659 u64 ks = ts->kernel_start;
Adrian Hunter90c2cda2019-01-09 11:18:32 +0200660 u64 addr = sample->addr;
661 u64 tm = sample->time;
662 u64 ip = sample->ip;
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200663 int err;
664
Adrian Hunter90c2cda2019-01-09 11:18:32 +0200665 if (ip >= ks && addr < ks) {
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200666 /* Return to userspace, so pop all kernel addresses */
667 while (thread_stack__in_kernel(ts)) {
668 err = thread_stack__call_return(thread, ts, --ts->cnt,
Adrian Hunter90c2cda2019-01-09 11:18:32 +0200669 tm, ref, true);
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200670 if (err)
671 return err;
672 }
673
674 /* If the stack is empty, push the userspace address */
675 if (!ts->cnt) {
Adrian Hunter90c2cda2019-01-09 11:18:32 +0200676 cp = call_path__findnew(cpr, root, tsym, addr, ks);
677 return thread_stack__push_cp(ts, 0, tm, ref, cp, true,
678 false);
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200679 }
Adrian Hunter90c2cda2019-01-09 11:18:32 +0200680 } else if (thread_stack__in_kernel(ts) && ip < ks) {
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200681 /* Return to userspace, so pop all kernel addresses */
682 while (thread_stack__in_kernel(ts)) {
683 err = thread_stack__call_return(thread, ts, --ts->cnt,
Adrian Hunter90c2cda2019-01-09 11:18:32 +0200684 tm, ref, true);
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200685 if (err)
686 return err;
687 }
688 }
689
690 if (ts->cnt)
691 parent = ts->stack[ts->cnt - 1].cp;
692 else
Adrian Hunter90c2cda2019-01-09 11:18:32 +0200693 parent = root;
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200694
Adrian Hunter1f35cd62019-01-09 11:18:34 +0200695 if (parent->sym == from_al->sym) {
696 /*
697 * At the bottom of the stack, assume the missing 'call' was
698 * before the trace started. So, pop the current symbol and push
699 * the 'to' symbol.
700 */
701 if (ts->cnt == 1) {
702 err = thread_stack__call_return(thread, ts, --ts->cnt,
703 tm, ref, false);
704 if (err)
705 return err;
706 }
707
708 if (!ts->cnt) {
709 cp = call_path__findnew(cpr, root, tsym, addr, ks);
710
711 return thread_stack__push_cp(ts, addr, tm, ref, cp,
712 true, false);
713 }
714
715 /*
716 * Otherwise assume the 'return' is being used as a jump (e.g.
717 * retpoline) and just push the 'to' symbol.
718 */
719 cp = call_path__findnew(cpr, parent, tsym, addr, ks);
720
721 err = thread_stack__push_cp(ts, 0, tm, ref, cp, true, false);
722 if (!err)
723 ts->stack[ts->cnt - 1].non_call = true;
724
725 return err;
726 }
727
728 /*
729 * Assume 'parent' has not yet returned, so push 'to', and then push and
730 * pop 'from'.
731 */
732
733 cp = call_path__findnew(cpr, parent, tsym, addr, ks);
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200734
Adrian Hunter90c2cda2019-01-09 11:18:32 +0200735 err = thread_stack__push_cp(ts, addr, tm, ref, cp, true, false);
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200736 if (err)
737 return err;
738
Adrian Hunter1f35cd62019-01-09 11:18:34 +0200739 cp = call_path__findnew(cpr, cp, fsym, ip, ks);
740
741 err = thread_stack__push_cp(ts, ip, tm, ref, cp, true, false);
742 if (err)
743 return err;
744
745 return thread_stack__call_return(thread, ts, --ts->cnt, tm, ref, false);
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200746}
747
748static int thread_stack__trace_begin(struct thread *thread,
749 struct thread_stack *ts, u64 timestamp,
750 u64 ref)
751{
752 struct thread_stack_entry *tse;
753 int err;
754
755 if (!ts->cnt)
756 return 0;
757
758 /* Pop trace end */
759 tse = &ts->stack[ts->cnt - 1];
Adrian Hunter2dcde4e2018-09-20 16:00:46 +0300760 if (tse->trace_end) {
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200761 err = thread_stack__call_return(thread, ts, --ts->cnt,
762 timestamp, ref, false);
763 if (err)
764 return err;
765 }
766
767 return 0;
768}
769
770static int thread_stack__trace_end(struct thread_stack *ts,
771 struct perf_sample *sample, u64 ref)
772{
773 struct call_path_root *cpr = ts->crp->cpr;
774 struct call_path *cp;
775 u64 ret_addr;
776
777 /* No point having 'trace end' on the bottom of the stack */
778 if (!ts->cnt || (ts->cnt == 1 && ts->stack[0].ref == ref))
779 return 0;
780
781 cp = call_path__findnew(cpr, ts->stack[ts->cnt - 1].cp, NULL, 0,
782 ts->kernel_start);
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200783
784 ret_addr = sample->ip + sample->insn_len;
785
786 return thread_stack__push_cp(ts, ret_addr, sample->time, ref, cp,
Adrian Hunter2dcde4e2018-09-20 16:00:46 +0300787 false, true);
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200788}
789
Adrian Hunter3c0cd952019-01-09 11:18:35 +0200790static bool is_x86_retpoline(const char *name)
791{
792 const char *p = strstr(name, "__x86_indirect_thunk_");
793
794 return p == name || !strcmp(name, "__indirect_thunk_start");
795}
796
797/*
798 * x86 retpoline functions pollute the call graph. This function removes them.
799 * This does not handle function return thunks, nor is there any improvement
800 * for the handling of inline thunks or extern thunks.
801 */
802static int thread_stack__x86_retpoline(struct thread_stack *ts,
803 struct perf_sample *sample,
804 struct addr_location *to_al)
805{
806 struct thread_stack_entry *tse = &ts->stack[ts->cnt - 1];
807 struct call_path_root *cpr = ts->crp->cpr;
808 struct symbol *sym = tse->cp->sym;
809 struct symbol *tsym = to_al->sym;
810 struct call_path *cp;
811
812 if (sym && is_x86_retpoline(sym->name)) {
813 /*
814 * This is a x86 retpoline fn. It pollutes the call graph by
815 * showing up everywhere there is an indirect branch, but does
816 * not itself mean anything. Here the top-of-stack is removed,
817 * by decrementing the stack count, and then further down, the
818 * resulting top-of-stack is replaced with the actual target.
819 * The result is that the retpoline functions will no longer
820 * appear in the call graph. Note this only affects the call
821 * graph, since all the original branches are left unchanged.
822 */
823 ts->cnt -= 1;
824 sym = ts->stack[ts->cnt - 2].cp->sym;
825 if (sym && sym == tsym && to_al->addr != tsym->start) {
826 /*
827 * Target is back to the middle of the symbol we came
828 * from so assume it is an indirect jmp and forget it
829 * altogether.
830 */
831 ts->cnt -= 1;
832 return 0;
833 }
834 } else if (sym && sym == tsym) {
835 /*
836 * Target is back to the symbol we came from so assume it is an
837 * indirect jmp and forget it altogether.
838 */
839 ts->cnt -= 1;
840 return 0;
841 }
842
843 cp = call_path__findnew(cpr, ts->stack[ts->cnt - 2].cp, tsym,
844 sample->addr, ts->kernel_start);
845 if (!cp)
846 return -ENOMEM;
847
848 /* Replace the top-of-stack with the actual target */
849 ts->stack[ts->cnt - 1].cp = cp;
850
851 return 0;
852}
853
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200854int thread_stack__process(struct thread *thread, struct comm *comm,
855 struct perf_sample *sample,
856 struct addr_location *from_al,
857 struct addr_location *to_al, u64 ref,
858 struct call_return_processor *crp)
859{
Adrian Hunter256d92b2018-12-21 14:06:19 +0200860 struct thread_stack *ts = thread__stack(thread, sample->cpu);
Adrian Hunter3c0cd952019-01-09 11:18:35 +0200861 enum retpoline_state_t rstate;
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200862 int err = 0;
863
Adrian Hunter03b32cb2018-12-21 14:06:13 +0200864 if (ts && !ts->crp) {
865 /* Supersede thread_stack__event() */
Adrian Hunterf6060ac2018-12-21 14:06:16 +0200866 thread_stack__reset(thread, ts);
Adrian Hunter03b32cb2018-12-21 14:06:13 +0200867 ts = NULL;
868 }
869
870 if (!ts) {
Adrian Hunter256d92b2018-12-21 14:06:19 +0200871 ts = thread_stack__new(thread, sample->cpu, crp);
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200872 if (!ts)
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200873 return -ENOMEM;
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200874 ts->comm = comm;
875 }
876
Adrian Hunter3c0cd952019-01-09 11:18:35 +0200877 rstate = ts->rstate;
878 if (rstate == X86_RETPOLINE_DETECTED)
879 ts->rstate = X86_RETPOLINE_POSSIBLE;
880
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200881 /* Flush stack on exec */
882 if (ts->comm != comm && thread->pid_ == thread->tid) {
Adrian Huntera5499b32015-05-29 16:33:30 +0300883 err = __thread_stack__flush(thread, ts);
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200884 if (err)
885 return err;
886 ts->comm = comm;
887 }
888
889 /* If the stack is empty, put the current symbol on the stack */
890 if (!ts->cnt) {
Adrian Huntere0b89512018-12-21 14:06:14 +0200891 err = thread_stack__bottom(ts, sample, from_al, to_al, ref);
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200892 if (err)
893 return err;
894 }
895
896 ts->branch_count += 1;
Adrian Hunter003ccdc2019-05-20 14:37:19 +0300897 ts->insn_count += sample->insn_cnt;
898 ts->cyc_count += sample->cyc_cnt;
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200899 ts->last_time = sample->time;
900
901 if (sample->flags & PERF_IP_FLAG_CALL) {
Adrian Hunter2dcde4e2018-09-20 16:00:46 +0300902 bool trace_end = sample->flags & PERF_IP_FLAG_TRACE_END;
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200903 struct call_path_root *cpr = ts->crp->cpr;
904 struct call_path *cp;
905 u64 ret_addr;
906
907 if (!sample->ip || !sample->addr)
908 return 0;
909
910 ret_addr = sample->ip + sample->insn_len;
911 if (ret_addr == sample->addr)
912 return 0; /* Zero-length calls are excluded */
913
914 cp = call_path__findnew(cpr, ts->stack[ts->cnt - 1].cp,
915 to_al->sym, sample->addr,
916 ts->kernel_start);
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200917 err = thread_stack__push_cp(ts, ret_addr, sample->time, ref,
Adrian Hunter2dcde4e2018-09-20 16:00:46 +0300918 cp, false, trace_end);
Adrian Hunter3c0cd952019-01-09 11:18:35 +0200919
920 /*
921 * A call to the same symbol but not the start of the symbol,
922 * may be the start of a x86 retpoline.
923 */
924 if (!err && rstate == X86_RETPOLINE_POSSIBLE && to_al->sym &&
925 from_al->sym == to_al->sym &&
926 to_al->addr != to_al->sym->start)
927 ts->rstate = X86_RETPOLINE_DETECTED;
928
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200929 } else if (sample->flags & PERF_IP_FLAG_RETURN) {
Adrian Hunter97860b42019-06-19 09:44:28 +0300930 if (!sample->addr) {
931 u32 return_from_kernel = PERF_IP_FLAG_SYSCALLRET |
932 PERF_IP_FLAG_INTERRUPT;
933
934 if (!(sample->flags & return_from_kernel))
935 return 0;
936
937 /* Pop kernel stack */
938 return thread_stack__pop_ks(thread, ts, sample, ref);
939 }
940
941 if (!sample->ip)
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200942 return 0;
943
Adrian Hunter3c0cd952019-01-09 11:18:35 +0200944 /* x86 retpoline 'return' doesn't match the stack */
945 if (rstate == X86_RETPOLINE_DETECTED && ts->cnt > 2 &&
946 ts->stack[ts->cnt - 1].ret_addr != sample->addr)
947 return thread_stack__x86_retpoline(ts, sample, to_al);
948
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200949 err = thread_stack__pop_cp(thread, ts, sample->addr,
950 sample->time, ref, from_al->sym);
951 if (err) {
952 if (err < 0)
953 return err;
954 err = thread_stack__no_call_return(thread, ts, sample,
955 from_al, to_al, ref);
956 }
957 } else if (sample->flags & PERF_IP_FLAG_TRACE_BEGIN) {
958 err = thread_stack__trace_begin(thread, ts, sample->time, ref);
959 } else if (sample->flags & PERF_IP_FLAG_TRACE_END) {
960 err = thread_stack__trace_end(ts, sample, ref);
Adrian Hunterf08046c2019-01-09 11:18:33 +0200961 } else if (sample->flags & PERF_IP_FLAG_BRANCH &&
962 from_al->sym != to_al->sym && to_al->sym &&
963 to_al->addr == to_al->sym->start) {
964 struct call_path_root *cpr = ts->crp->cpr;
965 struct call_path *cp;
966
967 /*
968 * The compiler might optimize a call/ret combination by making
969 * it a jmp. Make that visible by recording on the stack a
970 * branch to the start of a different symbol. Note, that means
971 * when a ret pops the stack, all jmps must be popped off first.
972 */
973 cp = call_path__findnew(cpr, ts->stack[ts->cnt - 1].cp,
974 to_al->sym, sample->addr,
975 ts->kernel_start);
976 err = thread_stack__push_cp(ts, 0, sample->time, ref, cp, false,
977 false);
978 if (!err)
979 ts->stack[ts->cnt - 1].non_call = true;
Adrian Hunter92a9e4f2014-10-30 16:09:45 +0200980 }
981
982 return err;
983}
Adrian Huntere2167082016-06-23 16:40:58 +0300984
Adrian Hunter256d92b2018-12-21 14:06:19 +0200985size_t thread_stack__depth(struct thread *thread, int cpu)
Adrian Huntere2167082016-06-23 16:40:58 +0300986{
Adrian Hunter256d92b2018-12-21 14:06:19 +0200987 struct thread_stack *ts = thread__stack(thread, cpu);
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200988
989 if (!ts)
Adrian Huntere2167082016-06-23 16:40:58 +0300990 return 0;
Adrian Hunterbd8e68a2018-12-21 14:06:15 +0200991 return ts->cnt;
Adrian Huntere2167082016-06-23 16:40:58 +0300992}