Adrian Hunter | 00447cc | 2014-10-30 16:09:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * thread-stack.c: Synthesize a thread's stack using call / return events |
| 3 | * Copyright (c) 2014, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | */ |
| 15 | |
Adrian Hunter | 92a9e4f | 2014-10-30 16:09:45 +0200 | [diff] [blame] | 16 | #include <linux/rbtree.h> |
| 17 | #include <linux/list.h> |
Adrian Hunter | 00447cc | 2014-10-30 16:09:42 +0200 | [diff] [blame] | 18 | #include "thread.h" |
| 19 | #include "event.h" |
Adrian Hunter | 92a9e4f | 2014-10-30 16:09:45 +0200 | [diff] [blame] | 20 | #include "machine.h" |
Adrian Hunter | 00447cc | 2014-10-30 16:09:42 +0200 | [diff] [blame] | 21 | #include "util.h" |
| 22 | #include "debug.h" |
Adrian Hunter | 92a9e4f | 2014-10-30 16:09:45 +0200 | [diff] [blame] | 23 | #include "symbol.h" |
| 24 | #include "comm.h" |
Chris Phlipot | 451db12 | 2016-04-28 01:19:07 -0700 | [diff] [blame^] | 25 | #include "call-path.h" |
Adrian Hunter | 00447cc | 2014-10-30 16:09:42 +0200 | [diff] [blame] | 26 | #include "thread-stack.h" |
| 27 | |
Adrian Hunter | 92a9e4f | 2014-10-30 16:09:45 +0200 | [diff] [blame] | 28 | /** |
| 29 | * struct call_return_processor - provides a call-back to consume call-return |
| 30 | * information. |
| 31 | * @cpr: call path root |
| 32 | * @process: call-back that accepts call/return information |
| 33 | * @data: anonymous data for call-back |
| 34 | */ |
| 35 | struct call_return_processor { |
| 36 | struct call_path_root *cpr; |
| 37 | int (*process)(struct call_return *cr, void *data); |
| 38 | void *data; |
| 39 | }; |
| 40 | |
| 41 | #define STACK_GROWTH 2048 |
| 42 | |
| 43 | /** |
| 44 | * struct thread_stack_entry - thread stack entry. |
| 45 | * @ret_addr: return address |
| 46 | * @timestamp: timestamp (if known) |
| 47 | * @ref: external reference (e.g. db_id of sample) |
| 48 | * @branch_count: the branch count when the entry was created |
| 49 | * @cp: call path |
| 50 | * @no_call: a 'call' was not seen |
| 51 | */ |
| 52 | struct thread_stack_entry { |
| 53 | u64 ret_addr; |
| 54 | u64 timestamp; |
| 55 | u64 ref; |
| 56 | u64 branch_count; |
| 57 | struct call_path *cp; |
| 58 | bool no_call; |
| 59 | }; |
| 60 | |
| 61 | /** |
| 62 | * struct thread_stack - thread stack constructed from 'call' and 'return' |
| 63 | * branch samples. |
| 64 | * @stack: array that holds the stack |
| 65 | * @cnt: number of entries in the stack |
| 66 | * @sz: current maximum stack size |
| 67 | * @trace_nr: current trace number |
| 68 | * @branch_count: running branch count |
| 69 | * @kernel_start: kernel start address |
| 70 | * @last_time: last timestamp |
| 71 | * @crp: call/return processor |
| 72 | * @comm: current comm |
| 73 | */ |
Adrian Hunter | 00447cc | 2014-10-30 16:09:42 +0200 | [diff] [blame] | 74 | struct thread_stack { |
| 75 | struct thread_stack_entry *stack; |
| 76 | size_t cnt; |
| 77 | size_t sz; |
| 78 | u64 trace_nr; |
Adrian Hunter | 92a9e4f | 2014-10-30 16:09:45 +0200 | [diff] [blame] | 79 | u64 branch_count; |
| 80 | u64 kernel_start; |
| 81 | u64 last_time; |
| 82 | struct call_return_processor *crp; |
| 83 | struct comm *comm; |
Adrian Hunter | 00447cc | 2014-10-30 16:09:42 +0200 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | static int thread_stack__grow(struct thread_stack *ts) |
| 87 | { |
| 88 | struct thread_stack_entry *new_stack; |
| 89 | size_t sz, new_sz; |
| 90 | |
| 91 | new_sz = ts->sz + STACK_GROWTH; |
| 92 | sz = new_sz * sizeof(struct thread_stack_entry); |
| 93 | |
| 94 | new_stack = realloc(ts->stack, sz); |
| 95 | if (!new_stack) |
| 96 | return -ENOMEM; |
| 97 | |
| 98 | ts->stack = new_stack; |
| 99 | ts->sz = new_sz; |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
Adrian Hunter | 92a9e4f | 2014-10-30 16:09:45 +0200 | [diff] [blame] | 104 | static struct thread_stack *thread_stack__new(struct thread *thread, |
| 105 | struct call_return_processor *crp) |
Adrian Hunter | 00447cc | 2014-10-30 16:09:42 +0200 | [diff] [blame] | 106 | { |
| 107 | struct thread_stack *ts; |
| 108 | |
| 109 | ts = zalloc(sizeof(struct thread_stack)); |
| 110 | if (!ts) |
| 111 | return NULL; |
| 112 | |
| 113 | if (thread_stack__grow(ts)) { |
| 114 | free(ts); |
| 115 | return NULL; |
| 116 | } |
| 117 | |
Adrian Hunter | 92a9e4f | 2014-10-30 16:09:45 +0200 | [diff] [blame] | 118 | if (thread->mg && thread->mg->machine) |
| 119 | ts->kernel_start = machine__kernel_start(thread->mg->machine); |
| 120 | else |
| 121 | ts->kernel_start = 1ULL << 63; |
| 122 | ts->crp = crp; |
| 123 | |
Adrian Hunter | 00447cc | 2014-10-30 16:09:42 +0200 | [diff] [blame] | 124 | return ts; |
| 125 | } |
| 126 | |
| 127 | static int thread_stack__push(struct thread_stack *ts, u64 ret_addr) |
| 128 | { |
| 129 | int err = 0; |
| 130 | |
| 131 | if (ts->cnt == ts->sz) { |
| 132 | err = thread_stack__grow(ts); |
| 133 | if (err) { |
| 134 | pr_warning("Out of memory: discarding thread stack\n"); |
| 135 | ts->cnt = 0; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | ts->stack[ts->cnt++].ret_addr = ret_addr; |
| 140 | |
| 141 | return err; |
| 142 | } |
| 143 | |
| 144 | static void thread_stack__pop(struct thread_stack *ts, u64 ret_addr) |
| 145 | { |
| 146 | size_t i; |
| 147 | |
| 148 | /* |
| 149 | * In some cases there may be functions which are not seen to return. |
| 150 | * For example when setjmp / longjmp has been used. Or the perf context |
| 151 | * switch in the kernel which doesn't stop and start tracing in exactly |
| 152 | * the same code path. When that happens the return address will be |
| 153 | * further down the stack. If the return address is not found at all, |
| 154 | * we assume the opposite (i.e. this is a return for a call that wasn't |
| 155 | * seen for some reason) and leave the stack alone. |
| 156 | */ |
| 157 | for (i = ts->cnt; i; ) { |
| 158 | if (ts->stack[--i].ret_addr == ret_addr) { |
| 159 | ts->cnt = i; |
| 160 | return; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
Adrian Hunter | 92a9e4f | 2014-10-30 16:09:45 +0200 | [diff] [blame] | 165 | static bool thread_stack__in_kernel(struct thread_stack *ts) |
| 166 | { |
| 167 | if (!ts->cnt) |
| 168 | return false; |
| 169 | |
| 170 | return ts->stack[ts->cnt - 1].cp->in_kernel; |
| 171 | } |
| 172 | |
| 173 | static int thread_stack__call_return(struct thread *thread, |
| 174 | struct thread_stack *ts, size_t idx, |
| 175 | u64 timestamp, u64 ref, bool no_return) |
| 176 | { |
| 177 | struct call_return_processor *crp = ts->crp; |
| 178 | struct thread_stack_entry *tse; |
| 179 | struct call_return cr = { |
| 180 | .thread = thread, |
| 181 | .comm = ts->comm, |
| 182 | .db_id = 0, |
| 183 | }; |
| 184 | |
| 185 | tse = &ts->stack[idx]; |
| 186 | cr.cp = tse->cp; |
| 187 | cr.call_time = tse->timestamp; |
| 188 | cr.return_time = timestamp; |
| 189 | cr.branch_count = ts->branch_count - tse->branch_count; |
| 190 | cr.call_ref = tse->ref; |
| 191 | cr.return_ref = ref; |
| 192 | if (tse->no_call) |
| 193 | cr.flags |= CALL_RETURN_NO_CALL; |
| 194 | if (no_return) |
| 195 | cr.flags |= CALL_RETURN_NO_RETURN; |
| 196 | |
| 197 | return crp->process(&cr, crp->data); |
| 198 | } |
| 199 | |
Adrian Hunter | a5499b3 | 2015-05-29 16:33:30 +0300 | [diff] [blame] | 200 | static int __thread_stack__flush(struct thread *thread, struct thread_stack *ts) |
Adrian Hunter | 92a9e4f | 2014-10-30 16:09:45 +0200 | [diff] [blame] | 201 | { |
| 202 | struct call_return_processor *crp = ts->crp; |
| 203 | int err; |
| 204 | |
| 205 | if (!crp) { |
| 206 | ts->cnt = 0; |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | while (ts->cnt) { |
| 211 | err = thread_stack__call_return(thread, ts, --ts->cnt, |
| 212 | ts->last_time, 0, true); |
| 213 | if (err) { |
| 214 | pr_err("Error flushing thread stack!\n"); |
| 215 | ts->cnt = 0; |
| 216 | return err; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
Adrian Hunter | a5499b3 | 2015-05-29 16:33:30 +0300 | [diff] [blame] | 223 | int thread_stack__flush(struct thread *thread) |
| 224 | { |
| 225 | if (thread->ts) |
| 226 | return __thread_stack__flush(thread, thread->ts); |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
Adrian Hunter | 00447cc | 2014-10-30 16:09:42 +0200 | [diff] [blame] | 231 | int thread_stack__event(struct thread *thread, u32 flags, u64 from_ip, |
| 232 | u64 to_ip, u16 insn_len, u64 trace_nr) |
| 233 | { |
| 234 | if (!thread) |
| 235 | return -EINVAL; |
| 236 | |
| 237 | if (!thread->ts) { |
Adrian Hunter | 92a9e4f | 2014-10-30 16:09:45 +0200 | [diff] [blame] | 238 | thread->ts = thread_stack__new(thread, NULL); |
Adrian Hunter | 00447cc | 2014-10-30 16:09:42 +0200 | [diff] [blame] | 239 | if (!thread->ts) { |
| 240 | pr_warning("Out of memory: no thread stack\n"); |
| 241 | return -ENOMEM; |
| 242 | } |
| 243 | thread->ts->trace_nr = trace_nr; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * When the trace is discontinuous, the trace_nr changes. In that case |
| 248 | * the stack might be completely invalid. Better to report nothing than |
Adrian Hunter | 92a9e4f | 2014-10-30 16:09:45 +0200 | [diff] [blame] | 249 | * to report something misleading, so flush the stack. |
Adrian Hunter | 00447cc | 2014-10-30 16:09:42 +0200 | [diff] [blame] | 250 | */ |
| 251 | if (trace_nr != thread->ts->trace_nr) { |
Adrian Hunter | 92a9e4f | 2014-10-30 16:09:45 +0200 | [diff] [blame] | 252 | if (thread->ts->trace_nr) |
Adrian Hunter | a5499b3 | 2015-05-29 16:33:30 +0300 | [diff] [blame] | 253 | __thread_stack__flush(thread, thread->ts); |
Adrian Hunter | 00447cc | 2014-10-30 16:09:42 +0200 | [diff] [blame] | 254 | thread->ts->trace_nr = trace_nr; |
Adrian Hunter | 00447cc | 2014-10-30 16:09:42 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Adrian Hunter | 92a9e4f | 2014-10-30 16:09:45 +0200 | [diff] [blame] | 257 | /* Stop here if thread_stack__process() is in use */ |
| 258 | if (thread->ts->crp) |
| 259 | return 0; |
| 260 | |
Adrian Hunter | 00447cc | 2014-10-30 16:09:42 +0200 | [diff] [blame] | 261 | if (flags & PERF_IP_FLAG_CALL) { |
| 262 | u64 ret_addr; |
| 263 | |
| 264 | if (!to_ip) |
| 265 | return 0; |
| 266 | ret_addr = from_ip + insn_len; |
| 267 | if (ret_addr == to_ip) |
| 268 | return 0; /* Zero-length calls are excluded */ |
| 269 | return thread_stack__push(thread->ts, ret_addr); |
| 270 | } else if (flags & PERF_IP_FLAG_RETURN) { |
| 271 | if (!from_ip) |
| 272 | return 0; |
| 273 | thread_stack__pop(thread->ts, to_ip); |
| 274 | } |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
Adrian Hunter | 92a9e4f | 2014-10-30 16:09:45 +0200 | [diff] [blame] | 279 | void thread_stack__set_trace_nr(struct thread *thread, u64 trace_nr) |
| 280 | { |
| 281 | if (!thread || !thread->ts) |
| 282 | return; |
| 283 | |
| 284 | if (trace_nr != thread->ts->trace_nr) { |
| 285 | if (thread->ts->trace_nr) |
Adrian Hunter | a5499b3 | 2015-05-29 16:33:30 +0300 | [diff] [blame] | 286 | __thread_stack__flush(thread, thread->ts); |
Adrian Hunter | 92a9e4f | 2014-10-30 16:09:45 +0200 | [diff] [blame] | 287 | thread->ts->trace_nr = trace_nr; |
| 288 | } |
| 289 | } |
| 290 | |
Adrian Hunter | 00447cc | 2014-10-30 16:09:42 +0200 | [diff] [blame] | 291 | void thread_stack__free(struct thread *thread) |
| 292 | { |
| 293 | if (thread->ts) { |
Adrian Hunter | a5499b3 | 2015-05-29 16:33:30 +0300 | [diff] [blame] | 294 | __thread_stack__flush(thread, thread->ts); |
Adrian Hunter | 00447cc | 2014-10-30 16:09:42 +0200 | [diff] [blame] | 295 | zfree(&thread->ts->stack); |
| 296 | zfree(&thread->ts); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | void thread_stack__sample(struct thread *thread, struct ip_callchain *chain, |
| 301 | size_t sz, u64 ip) |
| 302 | { |
| 303 | size_t i; |
| 304 | |
| 305 | if (!thread || !thread->ts) |
| 306 | chain->nr = 1; |
| 307 | else |
| 308 | chain->nr = min(sz, thread->ts->cnt + 1); |
| 309 | |
| 310 | chain->ips[0] = ip; |
| 311 | |
| 312 | for (i = 1; i < chain->nr; i++) |
| 313 | chain->ips[i] = thread->ts->stack[thread->ts->cnt - i].ret_addr; |
| 314 | } |
Adrian Hunter | 92a9e4f | 2014-10-30 16:09:45 +0200 | [diff] [blame] | 315 | |
Adrian Hunter | 92a9e4f | 2014-10-30 16:09:45 +0200 | [diff] [blame] | 316 | struct call_return_processor * |
| 317 | call_return_processor__new(int (*process)(struct call_return *cr, void *data), |
| 318 | void *data) |
| 319 | { |
| 320 | struct call_return_processor *crp; |
| 321 | |
| 322 | crp = zalloc(sizeof(struct call_return_processor)); |
| 323 | if (!crp) |
| 324 | return NULL; |
| 325 | crp->cpr = call_path_root__new(); |
| 326 | if (!crp->cpr) |
| 327 | goto out_free; |
| 328 | crp->process = process; |
| 329 | crp->data = data; |
| 330 | return crp; |
| 331 | |
| 332 | out_free: |
| 333 | free(crp); |
| 334 | return NULL; |
| 335 | } |
| 336 | |
| 337 | void call_return_processor__free(struct call_return_processor *crp) |
| 338 | { |
| 339 | if (crp) { |
| 340 | call_path_root__free(crp->cpr); |
| 341 | free(crp); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | static int thread_stack__push_cp(struct thread_stack *ts, u64 ret_addr, |
| 346 | u64 timestamp, u64 ref, struct call_path *cp, |
| 347 | bool no_call) |
| 348 | { |
| 349 | struct thread_stack_entry *tse; |
| 350 | int err; |
| 351 | |
| 352 | if (ts->cnt == ts->sz) { |
| 353 | err = thread_stack__grow(ts); |
| 354 | if (err) |
| 355 | return err; |
| 356 | } |
| 357 | |
| 358 | tse = &ts->stack[ts->cnt++]; |
| 359 | tse->ret_addr = ret_addr; |
| 360 | tse->timestamp = timestamp; |
| 361 | tse->ref = ref; |
| 362 | tse->branch_count = ts->branch_count; |
| 363 | tse->cp = cp; |
| 364 | tse->no_call = no_call; |
| 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | static int thread_stack__pop_cp(struct thread *thread, struct thread_stack *ts, |
| 370 | u64 ret_addr, u64 timestamp, u64 ref, |
| 371 | struct symbol *sym) |
| 372 | { |
| 373 | int err; |
| 374 | |
| 375 | if (!ts->cnt) |
| 376 | return 1; |
| 377 | |
| 378 | if (ts->cnt == 1) { |
| 379 | struct thread_stack_entry *tse = &ts->stack[0]; |
| 380 | |
| 381 | if (tse->cp->sym == sym) |
| 382 | return thread_stack__call_return(thread, ts, --ts->cnt, |
| 383 | timestamp, ref, false); |
| 384 | } |
| 385 | |
| 386 | if (ts->stack[ts->cnt - 1].ret_addr == ret_addr) { |
| 387 | return thread_stack__call_return(thread, ts, --ts->cnt, |
| 388 | timestamp, ref, false); |
| 389 | } else { |
| 390 | size_t i = ts->cnt - 1; |
| 391 | |
| 392 | while (i--) { |
| 393 | if (ts->stack[i].ret_addr != ret_addr) |
| 394 | continue; |
| 395 | i += 1; |
| 396 | while (ts->cnt > i) { |
| 397 | err = thread_stack__call_return(thread, ts, |
| 398 | --ts->cnt, |
| 399 | timestamp, ref, |
| 400 | true); |
| 401 | if (err) |
| 402 | return err; |
| 403 | } |
| 404 | return thread_stack__call_return(thread, ts, --ts->cnt, |
| 405 | timestamp, ref, false); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | return 1; |
| 410 | } |
| 411 | |
| 412 | static int thread_stack__bottom(struct thread *thread, struct thread_stack *ts, |
| 413 | struct perf_sample *sample, |
| 414 | struct addr_location *from_al, |
| 415 | struct addr_location *to_al, u64 ref) |
| 416 | { |
| 417 | struct call_path_root *cpr = ts->crp->cpr; |
| 418 | struct call_path *cp; |
| 419 | struct symbol *sym; |
| 420 | u64 ip; |
| 421 | |
| 422 | if (sample->ip) { |
| 423 | ip = sample->ip; |
| 424 | sym = from_al->sym; |
| 425 | } else if (sample->addr) { |
| 426 | ip = sample->addr; |
| 427 | sym = to_al->sym; |
| 428 | } else { |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | cp = call_path__findnew(cpr, &cpr->call_path, sym, ip, |
| 433 | ts->kernel_start); |
| 434 | if (!cp) |
| 435 | return -ENOMEM; |
| 436 | |
| 437 | return thread_stack__push_cp(thread->ts, ip, sample->time, ref, cp, |
| 438 | true); |
| 439 | } |
| 440 | |
| 441 | static int thread_stack__no_call_return(struct thread *thread, |
| 442 | struct thread_stack *ts, |
| 443 | struct perf_sample *sample, |
| 444 | struct addr_location *from_al, |
| 445 | struct addr_location *to_al, u64 ref) |
| 446 | { |
| 447 | struct call_path_root *cpr = ts->crp->cpr; |
| 448 | struct call_path *cp, *parent; |
| 449 | u64 ks = ts->kernel_start; |
| 450 | int err; |
| 451 | |
| 452 | if (sample->ip >= ks && sample->addr < ks) { |
| 453 | /* Return to userspace, so pop all kernel addresses */ |
| 454 | while (thread_stack__in_kernel(ts)) { |
| 455 | err = thread_stack__call_return(thread, ts, --ts->cnt, |
| 456 | sample->time, ref, |
| 457 | true); |
| 458 | if (err) |
| 459 | return err; |
| 460 | } |
| 461 | |
| 462 | /* If the stack is empty, push the userspace address */ |
| 463 | if (!ts->cnt) { |
| 464 | cp = call_path__findnew(cpr, &cpr->call_path, |
| 465 | to_al->sym, sample->addr, |
| 466 | ts->kernel_start); |
| 467 | if (!cp) |
| 468 | return -ENOMEM; |
| 469 | return thread_stack__push_cp(ts, 0, sample->time, ref, |
| 470 | cp, true); |
| 471 | } |
| 472 | } else if (thread_stack__in_kernel(ts) && sample->ip < ks) { |
| 473 | /* Return to userspace, so pop all kernel addresses */ |
| 474 | while (thread_stack__in_kernel(ts)) { |
| 475 | err = thread_stack__call_return(thread, ts, --ts->cnt, |
| 476 | sample->time, ref, |
| 477 | true); |
| 478 | if (err) |
| 479 | return err; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | if (ts->cnt) |
| 484 | parent = ts->stack[ts->cnt - 1].cp; |
| 485 | else |
| 486 | parent = &cpr->call_path; |
| 487 | |
| 488 | /* This 'return' had no 'call', so push and pop top of stack */ |
| 489 | cp = call_path__findnew(cpr, parent, from_al->sym, sample->ip, |
| 490 | ts->kernel_start); |
| 491 | if (!cp) |
| 492 | return -ENOMEM; |
| 493 | |
| 494 | err = thread_stack__push_cp(ts, sample->addr, sample->time, ref, cp, |
| 495 | true); |
| 496 | if (err) |
| 497 | return err; |
| 498 | |
| 499 | return thread_stack__pop_cp(thread, ts, sample->addr, sample->time, ref, |
| 500 | to_al->sym); |
| 501 | } |
| 502 | |
| 503 | static int thread_stack__trace_begin(struct thread *thread, |
| 504 | struct thread_stack *ts, u64 timestamp, |
| 505 | u64 ref) |
| 506 | { |
| 507 | struct thread_stack_entry *tse; |
| 508 | int err; |
| 509 | |
| 510 | if (!ts->cnt) |
| 511 | return 0; |
| 512 | |
| 513 | /* Pop trace end */ |
| 514 | tse = &ts->stack[ts->cnt - 1]; |
| 515 | if (tse->cp->sym == NULL && tse->cp->ip == 0) { |
| 516 | err = thread_stack__call_return(thread, ts, --ts->cnt, |
| 517 | timestamp, ref, false); |
| 518 | if (err) |
| 519 | return err; |
| 520 | } |
| 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | static int thread_stack__trace_end(struct thread_stack *ts, |
| 526 | struct perf_sample *sample, u64 ref) |
| 527 | { |
| 528 | struct call_path_root *cpr = ts->crp->cpr; |
| 529 | struct call_path *cp; |
| 530 | u64 ret_addr; |
| 531 | |
| 532 | /* No point having 'trace end' on the bottom of the stack */ |
| 533 | if (!ts->cnt || (ts->cnt == 1 && ts->stack[0].ref == ref)) |
| 534 | return 0; |
| 535 | |
| 536 | cp = call_path__findnew(cpr, ts->stack[ts->cnt - 1].cp, NULL, 0, |
| 537 | ts->kernel_start); |
| 538 | if (!cp) |
| 539 | return -ENOMEM; |
| 540 | |
| 541 | ret_addr = sample->ip + sample->insn_len; |
| 542 | |
| 543 | return thread_stack__push_cp(ts, ret_addr, sample->time, ref, cp, |
| 544 | false); |
| 545 | } |
| 546 | |
| 547 | int thread_stack__process(struct thread *thread, struct comm *comm, |
| 548 | struct perf_sample *sample, |
| 549 | struct addr_location *from_al, |
| 550 | struct addr_location *to_al, u64 ref, |
| 551 | struct call_return_processor *crp) |
| 552 | { |
| 553 | struct thread_stack *ts = thread->ts; |
| 554 | int err = 0; |
| 555 | |
| 556 | if (ts) { |
| 557 | if (!ts->crp) { |
| 558 | /* Supersede thread_stack__event() */ |
| 559 | thread_stack__free(thread); |
| 560 | thread->ts = thread_stack__new(thread, crp); |
| 561 | if (!thread->ts) |
| 562 | return -ENOMEM; |
| 563 | ts = thread->ts; |
| 564 | ts->comm = comm; |
| 565 | } |
| 566 | } else { |
| 567 | thread->ts = thread_stack__new(thread, crp); |
| 568 | if (!thread->ts) |
| 569 | return -ENOMEM; |
| 570 | ts = thread->ts; |
| 571 | ts->comm = comm; |
| 572 | } |
| 573 | |
| 574 | /* Flush stack on exec */ |
| 575 | if (ts->comm != comm && thread->pid_ == thread->tid) { |
Adrian Hunter | a5499b3 | 2015-05-29 16:33:30 +0300 | [diff] [blame] | 576 | err = __thread_stack__flush(thread, ts); |
Adrian Hunter | 92a9e4f | 2014-10-30 16:09:45 +0200 | [diff] [blame] | 577 | if (err) |
| 578 | return err; |
| 579 | ts->comm = comm; |
| 580 | } |
| 581 | |
| 582 | /* If the stack is empty, put the current symbol on the stack */ |
| 583 | if (!ts->cnt) { |
| 584 | err = thread_stack__bottom(thread, ts, sample, from_al, to_al, |
| 585 | ref); |
| 586 | if (err) |
| 587 | return err; |
| 588 | } |
| 589 | |
| 590 | ts->branch_count += 1; |
| 591 | ts->last_time = sample->time; |
| 592 | |
| 593 | if (sample->flags & PERF_IP_FLAG_CALL) { |
| 594 | struct call_path_root *cpr = ts->crp->cpr; |
| 595 | struct call_path *cp; |
| 596 | u64 ret_addr; |
| 597 | |
| 598 | if (!sample->ip || !sample->addr) |
| 599 | return 0; |
| 600 | |
| 601 | ret_addr = sample->ip + sample->insn_len; |
| 602 | if (ret_addr == sample->addr) |
| 603 | return 0; /* Zero-length calls are excluded */ |
| 604 | |
| 605 | cp = call_path__findnew(cpr, ts->stack[ts->cnt - 1].cp, |
| 606 | to_al->sym, sample->addr, |
| 607 | ts->kernel_start); |
| 608 | if (!cp) |
| 609 | return -ENOMEM; |
| 610 | err = thread_stack__push_cp(ts, ret_addr, sample->time, ref, |
| 611 | cp, false); |
| 612 | } else if (sample->flags & PERF_IP_FLAG_RETURN) { |
| 613 | if (!sample->ip || !sample->addr) |
| 614 | return 0; |
| 615 | |
| 616 | err = thread_stack__pop_cp(thread, ts, sample->addr, |
| 617 | sample->time, ref, from_al->sym); |
| 618 | if (err) { |
| 619 | if (err < 0) |
| 620 | return err; |
| 621 | err = thread_stack__no_call_return(thread, ts, sample, |
| 622 | from_al, to_al, ref); |
| 623 | } |
| 624 | } else if (sample->flags & PERF_IP_FLAG_TRACE_BEGIN) { |
| 625 | err = thread_stack__trace_begin(thread, ts, sample->time, ref); |
| 626 | } else if (sample->flags & PERF_IP_FLAG_TRACE_END) { |
| 627 | err = thread_stack__trace_end(ts, sample, ref); |
| 628 | } |
| 629 | |
| 630 | return err; |
| 631 | } |