3 Copyright (c) 2015-2019 Cray Inc. All Rights Reserved.
4 Utility to print unique stack traces
11 from pykdump.API import exec_crash_command
13 description_short = 'Print stack traces for each task.'
15 # outer loop indentifies PIDs
16 # inner loop looks for # until
17 # another PID is found
18 def sortInput(swapper, input):
21 ps = re.compile("^PID:\s+(\d+)\s+TASK:\s+([0-9A-Fa-f]+).*")
23 swap = re.compile((".*\"swapper/[0-9]+\""))
29 # Outer to check for PIDs
30 # this loop never breaks;
34 # Inner loop to check for # signs indicating lines we want.
35 # Having two loops allow for the PID and TSK to be associated
36 # with a particular trace.
37 # This loop breaks if a new PID is found (meaning the end of the
38 # current trace) or if there are no more lines available
40 if ps.match(line): break;
44 tmp += " ".join([line[2], line[3], line[4]])
45 if len(line) == 6 : tmp += " " + line[5]
47 line = input.readline()
52 info[tmp].append((PID,STK))
54 info[tmp] = [(PID,STK)]
58 PID, STK = m.group(1), m.group(2)
61 # if it's swapper line move on
62 # this prevents entry into inner loop
63 if not swapper and swap.match(line):
64 line = input.readline()
66 sort = sorted(info.items(), key=lambda info: len(info[1]))
69 def printRes(sort, printpid, printptr):
71 Prints out individual stack traces from lowest to highest.
73 for stack_trace, ptask_list in sort:
74 if printpid and not printptr:
75 print("PID: %s" % (', '.join(p[0] for p in ptask_list)))
76 elif printpid and printptr:
77 print("PID, TSK: %s" % (', '.join(p[0] + ': ' + p[1] for p in ptask_list)))
78 elif not printpid and printptr:
79 print("TSK: %s" % (', '.join(p[1] for p in ptask_list)))
80 print("TASKS: %d" %(len(ptask_list)))
81 print("\t%s" %(stack_trace))
85 parser = argparse.ArgumentParser()
87 parser.add_argument("-p", "--print-pid",
88 action="store_true", dest="printpid", default=False,
89 help="Print PIDS corresponding to each ST")
90 parser.add_argument("-q", "--print-taskpntr",
91 action="store_true", dest="printptr", default=False,
92 help="Print the task pointers for each ST")
93 parser.add_argument("-s", "--swapper",
94 action="store_true", dest="swapper", default=False,
95 help="Print swapper processes")
96 parser.add_argument("task_select", metavar="task_selection", nargs="*",
97 help="task selection argument (passed to foreach cmd)")
99 args = parser.parse_args()
101 com = "foreach {ts:s} bt".format(ts=" ".join(args.task_select))
103 result = exec_crash_command(com)
104 input = StringIO.StringIO(result)
105 printRes(sortInput(args.swapper, input), args.printpid, args.printptr)
107 if __name__ == '__main__':