blob: b52d472528803fd35c93e0cb4d27910dc774d622 [file] [log] [blame]
Alexey Ivanovcc01a9c2019-01-16 09:50:46 -08001#!/usr/bin/python
Brendan Greggaf2b46a2016-01-30 11:02:29 -08002#
3# offwaketime Summarize blocked time by kernel off-CPU stack + waker stack
4# For Linux, uses BCC, eBPF.
5#
ceeaspb47cecb62016-11-26 22:36:10 +00006# USAGE: offwaketime [-h] [-p PID | -u | -k] [-U | -K] [-f] [duration]
Brendan Greggaf2b46a2016-01-30 11:02:29 -08007#
8# Copyright 2016 Netflix, Inc.
9# Licensed under the Apache License, Version 2.0 (the "License")
10#
Alexei Starovoitov7583a4e2016-02-03 21:25:43 -080011# 20-Jan-2016 Brendan Gregg Created this.
Brendan Greggaf2b46a2016-01-30 11:02:29 -080012
13from __future__ import print_function
14from bcc import BPF
Alexei Starovoitov7583a4e2016-02-03 21:25:43 -080015from time import sleep
Brendan Greggaf2b46a2016-01-30 11:02:29 -080016import argparse
17import signal
ceeaspb47cecb62016-11-26 22:36:10 +000018import errno
19from sys import stderr
20
21# arg validation
22def positive_int(val):
lorddoskias263411b2020-06-08 09:33:46 +030023 dest = []
24 # Filter up to 5 pids, arbitrary
25 args_list = val.split(",", 5)
26 pids_to_add = min(len(args_list), 5)
27 for i in range(pids_to_add):
28 dest.append(_positive_int(args_list[i]))
29
30 return dest
31
32def _positive_int(val):
ceeaspb47cecb62016-11-26 22:36:10 +000033 try:
34 ival = int(val)
35 except ValueError:
36 raise argparse.ArgumentTypeError("must be an integer")
37
38 if ival < 0:
39 raise argparse.ArgumentTypeError("must be positive")
40 return ival
41
42def positive_nonzero_int(val):
lorddoskias263411b2020-06-08 09:33:46 +030043 ival = _positive_int(val)
ceeaspb47cecb62016-11-26 22:36:10 +000044 if ival == 0:
45 raise argparse.ArgumentTypeError("must be nonzero")
46 return ival
Brendan Greggaf2b46a2016-01-30 11:02:29 -080047
lorddoskias263411b2020-06-08 09:33:46 +030048def build_filter(filter_name, values):
49 filter_string = "((%s == %d)" % (filter_name, values[0])
50
51 for val in values[1:]:
52 filter_string += " || (%s == %d )" % (filter_name , val)
53
54 filter_string += ")"
55
56 return filter_string
57
Teng Qine778db02018-04-24 16:11:49 -070058def stack_id_err(stack_id):
Michael Prokopc14d02a2020-01-09 02:29:18 +010059 # -EFAULT in get_stackid normally means the stack-trace is not available,
Teng Qine778db02018-04-24 16:11:49 -070060 # Such as getting kernel stack trace in userspace code
61 return (stack_id < 0) and (stack_id != -errno.EFAULT)
62
Brendan Greggaf2b46a2016-01-30 11:02:29 -080063# arguments
64examples = """examples:
65 ./offwaketime # trace off-CPU + waker stack time until Ctrl-C
66 ./offwaketime 5 # trace for 5 seconds only
67 ./offwaketime -f 5 # 5 seconds, and output in folded format
ceeaspb47cecb62016-11-26 22:36:10 +000068 ./offwaketime -m 1000 # trace only events that last more than 1000 usec
69 ./offwaketime -M 9000 # trace only events that last less than 9000 usec
70 ./offwaketime -p 185 # only trace threads for PID 185
71 ./offwaketime -t 188 # only trace thread 188
72 ./offwaketime -u # only trace user threads (no kernel)
73 ./offwaketime -k # only trace kernel threads (no user)
74 ./offwaketime -U # only show user space stacks (no kernel)
75 ./offwaketime -K # only show kernel space stacks (no user)
Brendan Greggaf2b46a2016-01-30 11:02:29 -080076"""
77parser = argparse.ArgumentParser(
78 description="Summarize blocked time by kernel stack trace + waker stack",
79 formatter_class=argparse.RawDescriptionHelpFormatter,
80 epilog=examples)
ceeaspb47cecb62016-11-26 22:36:10 +000081thread_group = parser.add_mutually_exclusive_group()
82# Note: this script provides --pid and --tid flags but their arguments are
83# referred to internally using kernel nomenclature: TGID and PID.
lorddoskias263411b2020-06-08 09:33:46 +030084thread_group.add_argument("-p", "--pid", metavar="PIDS", dest="tgid",
85 type=positive_int,
86 help="trace these PIDS only. Can be a comma separated list of PIDS.")
87thread_group.add_argument("-t", "--tid", metavar="TIDS", dest="pid",
88 type=positive_int,
89 help="trace these TIDS only. Can be a comma separated list of TIDS.")
ceeaspb47cecb62016-11-26 22:36:10 +000090thread_group.add_argument("-u", "--user-threads-only", action="store_true",
Brendan Greggaf2b46a2016-01-30 11:02:29 -080091 help="user threads only (no kernel threads)")
ceeaspb47cecb62016-11-26 22:36:10 +000092thread_group.add_argument("-k", "--kernel-threads-only", action="store_true",
93 help="kernel threads only (no user threads)")
94stack_group = parser.add_mutually_exclusive_group()
95stack_group.add_argument("-U", "--user-stacks-only", action="store_true",
96 help="show stacks from user space only (no kernel space stacks)")
97stack_group.add_argument("-K", "--kernel-stacks-only", action="store_true",
98 help="show stacks from kernel space only (no user space stacks)")
99parser.add_argument("-d", "--delimited", action="store_true",
100 help="insert delimiter between kernel/user stacks")
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800101parser.add_argument("-f", "--folded", action="store_true",
102 help="output folded format")
ceeaspb47cecb62016-11-26 22:36:10 +0000103parser.add_argument("--stack-storage-size", default=1024,
104 type=positive_nonzero_int,
105 help="the number of unique stack traces that can be stored and "
106 "displayed (default 1024)")
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800107parser.add_argument("duration", nargs="?", default=99999999,
ceeaspb47cecb62016-11-26 22:36:10 +0000108 type=positive_nonzero_int,
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800109 help="duration of trace, in seconds")
ceeaspb47cecb62016-11-26 22:36:10 +0000110parser.add_argument("-m", "--min-block-time", default=1,
111 type=positive_nonzero_int,
112 help="the amount of time in microseconds over which we " +
113 "store traces (default 1)")
114parser.add_argument("-M", "--max-block-time", default=(1 << 64) - 1,
115 type=positive_nonzero_int,
116 help="the amount of time in microseconds under which we " +
117 "store traces (default U64_MAX)")
lorddoskias263411b2020-06-08 09:33:46 +0300118parser.add_argument("--state", type=_positive_int,
lorddoskiasb20f5e72020-05-30 19:17:33 +0300119 help="filter on this thread state bitmask (eg, 2 == TASK_UNINTERRUPTIBLE" +
120 ") see include/linux/sched.h")
Nathan Scottcf0792f2018-02-02 16:56:50 +1100121parser.add_argument("--ebpf", action="store_true",
122 help=argparse.SUPPRESS)
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800123args = parser.parse_args()
124folded = args.folded
125duration = int(args.duration)
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800126
127# signal handler
128def signal_ignore(signal, frame):
129 print()
130
131# define BPF program
132bpf_text = """
133#include <uapi/linux/ptrace.h>
134#include <linux/sched.h>
135
ceeaspb47cecb62016-11-26 22:36:10 +0000136#define MINBLOCK_US MINBLOCK_US_VALUEULL
137#define MAXBLOCK_US MAXBLOCK_US_VALUEULL
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800138
139struct key_t {
140 char waker[TASK_COMM_LEN];
141 char target[TASK_COMM_LEN];
Hengqi Chen08765a92021-10-31 23:20:10 +0800142 s64 w_k_stack_id;
143 s64 w_u_stack_id;
144 s64 t_k_stack_id;
145 s64 t_u_stack_id;
146 u64 t_pid;
147 u64 t_tgid;
ceeaspb47cecb62016-11-26 22:36:10 +0000148 u32 w_pid;
Teng Qine7432d42018-04-19 14:45:18 -0700149 u32 w_tgid;
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800150};
151BPF_HASH(counts, struct key_t);
Teng Qine7432d42018-04-19 14:45:18 -0700152
153// Key of this hash is PID of waiting Process,
154// value is timestamp when it went into waiting
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800155BPF_HASH(start, u32);
Teng Qine7432d42018-04-19 14:45:18 -0700156
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800157struct wokeby_t {
158 char name[TASK_COMM_LEN];
ceeaspb47cecb62016-11-26 22:36:10 +0000159 int k_stack_id;
160 int u_stack_id;
161 int w_pid;
Teng Qine7432d42018-04-19 14:45:18 -0700162 int w_tgid;
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800163};
Teng Qine7432d42018-04-19 14:45:18 -0700164// Key of the hash is PID of the Process to be waken, value is information
165// of the Process who wakes it
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800166BPF_HASH(wokeby, u32, struct wokeby_t);
167
Vladislav Bogdanov0a7da742020-02-07 15:22:42 +0300168BPF_STACK_TRACE(stack_traces, STACK_STORAGE_SIZE);
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800169
170int waker(struct pt_regs *ctx, struct task_struct *p) {
Teng Qine7432d42018-04-19 14:45:18 -0700171 // PID and TGID of the target Process to be waken
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800172 u32 pid = p->pid;
ceeaspb47cecb62016-11-26 22:36:10 +0000173 u32 tgid = p->tgid;
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800174
lorddoskiasb20f5e72020-05-30 19:17:33 +0300175 if (!((THREAD_FILTER) && (STATE_FILTER))) {
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800176 return 0;
ceeaspb47cecb62016-11-26 22:36:10 +0000177 }
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800178
Teng Qine7432d42018-04-19 14:45:18 -0700179 // Construct information about current (the waker) Process
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800180 struct wokeby_t woke = {};
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800181 bpf_get_current_comm(&woke.name, sizeof(woke.name));
ceeaspb47cecb62016-11-26 22:36:10 +0000182 woke.k_stack_id = KERNEL_STACK_GET;
183 woke.u_stack_id = USER_STACK_GET;
Teng Qine7432d42018-04-19 14:45:18 -0700184 woke.w_pid = bpf_get_current_pid_tgid();
185 woke.w_tgid = bpf_get_current_pid_tgid() >> 32;
186
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800187 wokeby.update(&pid, &woke);
188 return 0;
189}
190
191int oncpu(struct pt_regs *ctx, struct task_struct *p) {
Teng Qine7432d42018-04-19 14:45:18 -0700192 // PID and TGID of the previous Process (Process going into waiting)
193 u32 pid = p->pid;
ceeaspb47cecb62016-11-26 22:36:10 +0000194 u32 tgid = p->tgid;
Teng Qine7432d42018-04-19 14:45:18 -0700195 u64 *tsp;
196 u64 ts = bpf_ktime_get_ns();
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800197
Teng Qine7432d42018-04-19 14:45:18 -0700198 // Record timestamp for the previous Process (Process going into waiting)
lorddoskiasb20f5e72020-05-30 19:17:33 +0300199 if ((THREAD_FILTER) && (STATE_FILTER)) {
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800200 start.update(&pid, &ts);
201 }
202
Teng Qine7432d42018-04-19 14:45:18 -0700203 // Calculate current Process's wait time by finding the timestamp of when
204 // it went into waiting.
205 // pid and tgid are now the PID and TGID of the current (waking) Process.
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800206 pid = bpf_get_current_pid_tgid();
ceeaspb47cecb62016-11-26 22:36:10 +0000207 tgid = bpf_get_current_pid_tgid() >> 32;
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800208 tsp = start.lookup(&pid);
ceeaspb47cecb62016-11-26 22:36:10 +0000209 if (tsp == 0) {
Teng Qine7432d42018-04-19 14:45:18 -0700210 // Missed or filtered when the Process went into waiting
211 return 0;
ceeaspb47cecb62016-11-26 22:36:10 +0000212 }
Teng Qine7432d42018-04-19 14:45:18 -0700213 u64 delta = ts - *tsp;
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800214 start.delete(&pid);
215 delta = delta / 1000;
ceeaspb47cecb62016-11-26 22:36:10 +0000216 if ((delta < MINBLOCK_US) || (delta > MAXBLOCK_US)) {
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800217 return 0;
ceeaspb47cecb62016-11-26 22:36:10 +0000218 }
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800219
220 // create map key
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800221 struct key_t key = {};
222 struct wokeby_t *woke;
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800223
Teng Qine7432d42018-04-19 14:45:18 -0700224 bpf_get_current_comm(&key.target, sizeof(key.target));
225 key.t_pid = pid;
226 key.t_tgid = tgid;
ceeaspb47cecb62016-11-26 22:36:10 +0000227 key.t_k_stack_id = KERNEL_STACK_GET;
228 key.t_u_stack_id = USER_STACK_GET;
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800229
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800230 woke = wokeby.lookup(&pid);
231 if (woke) {
ceeaspb47cecb62016-11-26 22:36:10 +0000232 key.w_k_stack_id = woke->k_stack_id;
233 key.w_u_stack_id = woke->u_stack_id;
234 key.w_pid = woke->w_pid;
Teng Qine7432d42018-04-19 14:45:18 -0700235 key.w_tgid = woke->w_tgid;
Alexei Starovoitov7583a4e2016-02-03 21:25:43 -0800236 __builtin_memcpy(&key.waker, woke->name, TASK_COMM_LEN);
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800237 wokeby.delete(&pid);
238 }
239
Javier Honduvilla Coto64bf9652018-08-01 06:50:19 +0200240 counts.increment(key, delta);
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800241 return 0;
242}
243"""
ceeaspb47cecb62016-11-26 22:36:10 +0000244
245# set thread filter
ceeaspb47cecb62016-11-26 22:36:10 +0000246if args.tgid is not None:
lorddoskias263411b2020-06-08 09:33:46 +0300247 thread_filter = build_filter("tgid", args.tgid)
ceeaspb47cecb62016-11-26 22:36:10 +0000248elif args.pid is not None:
lorddoskias263411b2020-06-08 09:33:46 +0300249 thread_filter = build_filter("pid", args.pid)
ceeaspb47cecb62016-11-26 22:36:10 +0000250elif args.user_threads_only:
ceeaspb47cecb62016-11-26 22:36:10 +0000251 thread_filter = '!(p->flags & PF_KTHREAD)'
252elif args.kernel_threads_only:
ceeaspb47cecb62016-11-26 22:36:10 +0000253 thread_filter = 'p->flags & PF_KTHREAD'
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800254else:
ceeaspb47cecb62016-11-26 22:36:10 +0000255 thread_filter = '1'
lorddoskiasb20f5e72020-05-30 19:17:33 +0300256if args.state == 0:
Hengqi Chen08765a92021-10-31 23:20:10 +0800257 state_filter = 'p->STATE_FIELD == 0'
lorddoskiasb20f5e72020-05-30 19:17:33 +0300258elif args.state:
259 # these states are sometimes bitmask checked
Hengqi Chen08765a92021-10-31 23:20:10 +0800260 state_filter = 'p->STATE_FIELD & %d' % args.state
lorddoskiasb20f5e72020-05-30 19:17:33 +0300261else:
262 state_filter = '1'
ceeaspb47cecb62016-11-26 22:36:10 +0000263bpf_text = bpf_text.replace('THREAD_FILTER', thread_filter)
lorddoskiasb20f5e72020-05-30 19:17:33 +0300264bpf_text = bpf_text.replace('STATE_FILTER', state_filter)
Hengqi Chen08765a92021-10-31 23:20:10 +0800265if BPF.kernel_struct_has_field(b'task_struct', b'__state') == 1:
266 bpf_text = bpf_text.replace('STATE_FIELD', '__state')
267else:
268 bpf_text = bpf_text.replace('STATE_FIELD', 'state')
ceeaspb47cecb62016-11-26 22:36:10 +0000269
270# set stack storage size
271bpf_text = bpf_text.replace('STACK_STORAGE_SIZE', str(args.stack_storage_size))
272bpf_text = bpf_text.replace('MINBLOCK_US_VALUE', str(args.min_block_time))
273bpf_text = bpf_text.replace('MAXBLOCK_US_VALUE', str(args.max_block_time))
274
275# handle stack args
Teng Qine778db02018-04-24 16:11:49 -0700276kernel_stack_get = "stack_traces.get_stackid(ctx, 0)"
277user_stack_get = "stack_traces.get_stackid(ctx, BPF_F_USER_STACK)"
ceeaspb47cecb62016-11-26 22:36:10 +0000278stack_context = ""
279if args.user_stacks_only:
280 stack_context = "user"
281 kernel_stack_get = "-1"
282elif args.kernel_stacks_only:
283 stack_context = "kernel"
284 user_stack_get = "-1"
285else:
286 stack_context = "user + kernel"
287bpf_text = bpf_text.replace('USER_STACK_GET', user_stack_get)
288bpf_text = bpf_text.replace('KERNEL_STACK_GET', kernel_stack_get)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100289if args.ebpf:
290 print(bpf_text)
291 exit()
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800292
293# initialize BPF
294b = BPF(text=bpf_text)
Guodong Xu00b72fd2021-03-13 02:23:47 +0000295b.attach_kprobe(event_re="^finish_task_switch$|^finish_task_switch\.isra\.\d$",
296 fn_name="oncpu")
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800297b.attach_kprobe(event="try_to_wake_up", fn_name="waker")
298matched = b.num_open_kprobes()
299if matched == 0:
300 print("0 functions traced. Exiting.")
301 exit()
302
303# header
304if not folded:
ceeaspb47cecb62016-11-26 22:36:10 +0000305 print("Tracing blocked time (us) by %s off-CPU and waker stack" %
306 stack_context, end="")
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800307 if duration < 99999999:
308 print(" for %d secs." % duration)
309 else:
310 print("... Hit Ctrl-C to end.")
311
jeromemarchand09f9d3c2018-10-13 01:01:22 +0200312try:
313 sleep(duration)
314except KeyboardInterrupt:
315 # as cleanup can take many seconds, trap Ctrl-C:
316 # print a newline for folded output on Ctrl-C
317 signal.signal(signal.SIGINT, signal_ignore)
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800318
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800319
ceeaspb47cecb62016-11-26 22:36:10 +0000320if not folded:
321 print()
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800322
ceeaspb47cecb62016-11-26 22:36:10 +0000323missing_stacks = 0
324has_enomem = False
325counts = b.get_table("counts")
326stack_traces = b.get_table("stack_traces")
Teng Qine778db02018-04-24 16:11:49 -0700327need_delimiter = args.delimited and not (args.kernel_stacks_only or
328 args.user_stacks_only)
ceeaspb47cecb62016-11-26 22:36:10 +0000329for k, v in sorted(counts.items(), key=lambda counts: counts[1].value):
330 # handle get_stackid errors
Teng Qine778db02018-04-24 16:11:49 -0700331 if not args.user_stacks_only:
332 missing_stacks += int(stack_id_err(k.w_k_stack_id))
333 missing_stacks += int(stack_id_err(k.t_k_stack_id))
334 has_enomem = has_enomem or (k.w_k_stack_id == -errno.ENOMEM) or \
335 (k.t_k_stack_id == -errno.ENOMEM)
336 if not args.kernel_stacks_only:
337 missing_stacks += int(stack_id_err(k.w_u_stack_id))
338 missing_stacks += int(stack_id_err(k.t_u_stack_id))
339 has_enomem = has_enomem or (k.w_u_stack_id == -errno.ENOMEM) or \
340 (k.t_u_stack_id == -errno.ENOMEM)
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800341
ceeaspb47cecb62016-11-26 22:36:10 +0000342 waker_user_stack = [] if k.w_u_stack_id < 1 else \
343 reversed(list(stack_traces.walk(k.w_u_stack_id))[1:])
344 waker_kernel_stack = [] if k.w_k_stack_id < 1 else \
345 reversed(list(stack_traces.walk(k.w_k_stack_id))[1:])
346 target_user_stack = [] if k.t_u_stack_id < 1 else \
347 stack_traces.walk(k.t_u_stack_id)
348 target_kernel_stack = [] if k.t_k_stack_id < 1 else \
349 stack_traces.walk(k.t_k_stack_id)
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800350
ceeaspb47cecb62016-11-26 22:36:10 +0000351 if folded:
352 # print folded stack output
jeromemarchandb96ebcd2018-10-10 01:58:15 +0200353 line = [k.target.decode('utf-8', 'replace')]
Teng Qine778db02018-04-24 16:11:49 -0700354 if not args.kernel_stacks_only:
355 if stack_id_err(k.t_u_stack_id):
Jiri Olsaac00ac52019-11-15 12:45:59 +0100356 line.append("[Missed User Stack] %d" % k.t_u_stack_id)
Teng Qine778db02018-04-24 16:11:49 -0700357 else:
Jerome Marchandf03beca2019-02-15 17:35:37 +0100358 line.extend([b.sym(addr, k.t_tgid).decode('utf-8', 'replace')
Teng Qine778db02018-04-24 16:11:49 -0700359 for addr in reversed(list(target_user_stack)[1:])])
360 if not args.user_stacks_only:
361 line.extend(["-"] if (need_delimiter and k.t_k_stack_id > 0 and k.t_u_stack_id > 0) else [])
362 if stack_id_err(k.t_k_stack_id):
363 line.append("[Missed Kernel Stack]")
364 else:
Jerome Marchandf03beca2019-02-15 17:35:37 +0100365 line.extend([b.ksym(addr).decode('utf-8', 'replace')
Teng Qine778db02018-04-24 16:11:49 -0700366 for addr in reversed(list(target_kernel_stack)[1:])])
367 line.append("--")
368 if not args.user_stacks_only:
369 if stack_id_err(k.w_k_stack_id):
370 line.append("[Missed Kernel Stack]")
371 else:
Jerome Marchandf03beca2019-02-15 17:35:37 +0100372 line.extend([b.ksym(addr).decode('utf-8', 'replace')
Teng Qine778db02018-04-24 16:11:49 -0700373 for addr in reversed(list(waker_kernel_stack))])
374 if not args.kernel_stacks_only:
375 line.extend(["-"] if (need_delimiter and k.w_u_stack_id > 0 and k.w_k_stack_id > 0) else [])
376 if stack_id_err(k.w_u_stack_id):
Andrea Righi7813f8e2018-11-20 17:54:46 +0100377 line.append("[Missed User Stack]")
Teng Qine778db02018-04-24 16:11:49 -0700378 else:
Jerome Marchandf03beca2019-02-15 17:35:37 +0100379 line.extend([b.sym(addr, k.w_tgid).decode('utf-8', 'replace')
Teng Qine778db02018-04-24 16:11:49 -0700380 for addr in reversed(list(waker_user_stack))])
jeromemarchandb96ebcd2018-10-10 01:58:15 +0200381 line.append(k.waker.decode('utf-8', 'replace'))
ceeaspb47cecb62016-11-26 22:36:10 +0000382 print("%s %d" % (";".join(line), v.value))
ceeaspb47cecb62016-11-26 22:36:10 +0000383 else:
384 # print wakeup name then stack in reverse order
Yohei Ueda89bb40a2019-08-09 14:12:21 +0900385 print(" %-16s %s %s" % ("waker:", k.waker.decode('utf-8', 'replace'), k.w_pid))
Teng Qine778db02018-04-24 16:11:49 -0700386 if not args.kernel_stacks_only:
387 if stack_id_err(k.w_u_stack_id):
Jiri Olsaac00ac52019-11-15 12:45:59 +0100388 print(" [Missed User Stack] %d" % k.w_u_stack_id)
Teng Qine778db02018-04-24 16:11:49 -0700389 else:
390 for addr in waker_user_stack:
391 print(" %s" % b.sym(addr, k.w_tgid))
392 if not args.user_stacks_only:
393 if need_delimiter and k.w_u_stack_id > 0 and k.w_k_stack_id > 0:
394 print(" -")
395 if stack_id_err(k.w_k_stack_id):
396 print(" [Missed Kernel Stack]")
397 else:
398 for addr in waker_kernel_stack:
399 print(" %s" % b.ksym(addr))
Brendan Greggaf2b46a2016-01-30 11:02:29 -0800400
ceeaspb47cecb62016-11-26 22:36:10 +0000401 # print waker/wakee delimiter
402 print(" %-16s %s" % ("--", "--"))
Javier Honduvilla Coto64bf9652018-08-01 06:50:19 +0200403
Teng Qine778db02018-04-24 16:11:49 -0700404 if not args.user_stacks_only:
405 if stack_id_err(k.t_k_stack_id):
406 print(" [Missed Kernel Stack]")
407 else:
408 for addr in target_kernel_stack:
409 print(" %s" % b.ksym(addr))
410 if not args.kernel_stacks_only:
411 if need_delimiter and k.t_u_stack_id > 0 and k.t_k_stack_id > 0:
412 print(" -")
413 if stack_id_err(k.t_u_stack_id):
414 print(" [Missed User Stack]")
415 else:
416 for addr in target_user_stack:
417 print(" %s" % b.sym(addr, k.t_tgid))
Yohei Ueda89bb40a2019-08-09 14:12:21 +0900418 print(" %-16s %s %s" % ("target:", k.target.decode('utf-8', 'replace'), k.t_pid))
ceeaspb47cecb62016-11-26 22:36:10 +0000419 print(" %d\n" % v.value)
420
421if missing_stacks > 0:
422 enomem_str = " Consider increasing --stack-storage-size."
Teng Qine778db02018-04-24 16:11:49 -0700423 print("WARNING: %d stack traces lost and could not be displayed.%s" %
424 (missing_stacks, (enomem_str if has_enomem else "")),
ceeaspb47cecb62016-11-26 22:36:10 +0000425 file=stderr)