1*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0 2*4882a593Smuzhiyun# 3*4882a593Smuzhiyun# Copyright 2019 Google LLC. 4*4882a593Smuzhiyun 5*4882a593Smuzhiyunimport binascii 6*4882a593Smuzhiyunimport gdb 7*4882a593Smuzhiyun 8*4882a593Smuzhiyunfrom linux import constants 9*4882a593Smuzhiyunfrom linux import cpus 10*4882a593Smuzhiyunfrom linux import rbtree 11*4882a593Smuzhiyunfrom linux import utils 12*4882a593Smuzhiyun 13*4882a593Smuzhiyuntimerqueue_node_type = utils.CachedType("struct timerqueue_node").get_type() 14*4882a593Smuzhiyunhrtimer_type = utils.CachedType("struct hrtimer").get_type() 15*4882a593Smuzhiyun 16*4882a593Smuzhiyun 17*4882a593Smuzhiyundef ktime_get(): 18*4882a593Smuzhiyun """Returns the current time, but not very accurately 19*4882a593Smuzhiyun 20*4882a593Smuzhiyun We can't read the hardware timer itself to add any nanoseconds 21*4882a593Smuzhiyun that need to be added since we last stored the time in the 22*4882a593Smuzhiyun timekeeper. But this is probably good enough for debug purposes.""" 23*4882a593Smuzhiyun tk_core = gdb.parse_and_eval("&tk_core") 24*4882a593Smuzhiyun 25*4882a593Smuzhiyun return tk_core['timekeeper']['tkr_mono']['base'] 26*4882a593Smuzhiyun 27*4882a593Smuzhiyun 28*4882a593Smuzhiyundef print_timer(rb_node, idx): 29*4882a593Smuzhiyun timerqueue = utils.container_of(rb_node, timerqueue_node_type.pointer(), 30*4882a593Smuzhiyun "node") 31*4882a593Smuzhiyun timer = utils.container_of(timerqueue, hrtimer_type.pointer(), "node") 32*4882a593Smuzhiyun 33*4882a593Smuzhiyun function = str(timer['function']).split(" ")[1].strip("<>") 34*4882a593Smuzhiyun softexpires = timer['_softexpires'] 35*4882a593Smuzhiyun expires = timer['node']['expires'] 36*4882a593Smuzhiyun now = ktime_get() 37*4882a593Smuzhiyun 38*4882a593Smuzhiyun text = " #{}: <{}>, {}, ".format(idx, timer, function) 39*4882a593Smuzhiyun text += "S:{:02x}\n".format(int(timer['state'])) 40*4882a593Smuzhiyun text += " # expires at {}-{} nsecs [in {} to {} nsecs]\n".format( 41*4882a593Smuzhiyun softexpires, expires, softexpires - now, expires - now) 42*4882a593Smuzhiyun return text 43*4882a593Smuzhiyun 44*4882a593Smuzhiyun 45*4882a593Smuzhiyundef print_active_timers(base): 46*4882a593Smuzhiyun curr = base['active']['next']['node'] 47*4882a593Smuzhiyun curr = curr.address.cast(rbtree.rb_node_type.get_type().pointer()) 48*4882a593Smuzhiyun idx = 0 49*4882a593Smuzhiyun while curr: 50*4882a593Smuzhiyun yield print_timer(curr, idx) 51*4882a593Smuzhiyun curr = rbtree.rb_next(curr) 52*4882a593Smuzhiyun idx += 1 53*4882a593Smuzhiyun 54*4882a593Smuzhiyun 55*4882a593Smuzhiyundef print_base(base): 56*4882a593Smuzhiyun text = " .base: {}\n".format(base.address) 57*4882a593Smuzhiyun text += " .index: {}\n".format(base['index']) 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun text += " .resolution: {} nsecs\n".format(constants.LX_hrtimer_resolution) 60*4882a593Smuzhiyun 61*4882a593Smuzhiyun text += " .get_time: {}\n".format(base['get_time']) 62*4882a593Smuzhiyun if constants.LX_CONFIG_HIGH_RES_TIMERS: 63*4882a593Smuzhiyun text += " .offset: {} nsecs\n".format(base['offset']) 64*4882a593Smuzhiyun text += "active timers:\n" 65*4882a593Smuzhiyun text += "".join([x for x in print_active_timers(base)]) 66*4882a593Smuzhiyun return text 67*4882a593Smuzhiyun 68*4882a593Smuzhiyun 69*4882a593Smuzhiyundef print_cpu(hrtimer_bases, cpu, max_clock_bases): 70*4882a593Smuzhiyun cpu_base = cpus.per_cpu(hrtimer_bases, cpu) 71*4882a593Smuzhiyun jiffies = gdb.parse_and_eval("jiffies_64") 72*4882a593Smuzhiyun tick_sched_ptr = gdb.parse_and_eval("&tick_cpu_sched") 73*4882a593Smuzhiyun ts = cpus.per_cpu(tick_sched_ptr, cpu) 74*4882a593Smuzhiyun 75*4882a593Smuzhiyun text = "cpu: {}\n".format(cpu) 76*4882a593Smuzhiyun for i in xrange(max_clock_bases): 77*4882a593Smuzhiyun text += " clock {}:\n".format(i) 78*4882a593Smuzhiyun text += print_base(cpu_base['clock_base'][i]) 79*4882a593Smuzhiyun 80*4882a593Smuzhiyun if constants.LX_CONFIG_HIGH_RES_TIMERS: 81*4882a593Smuzhiyun fmts = [(" .{} : {} nsecs", 'expires_next'), 82*4882a593Smuzhiyun (" .{} : {}", 'hres_active'), 83*4882a593Smuzhiyun (" .{} : {}", 'nr_events'), 84*4882a593Smuzhiyun (" .{} : {}", 'nr_retries'), 85*4882a593Smuzhiyun (" .{} : {}", 'nr_hangs'), 86*4882a593Smuzhiyun (" .{} : {}", 'max_hang_time')] 87*4882a593Smuzhiyun text += "\n".join([s.format(f, cpu_base[f]) for s, f in fmts]) 88*4882a593Smuzhiyun text += "\n" 89*4882a593Smuzhiyun 90*4882a593Smuzhiyun if constants.LX_CONFIG_TICK_ONESHOT: 91*4882a593Smuzhiyun fmts = [(" .{} : {}", 'nohz_mode'), 92*4882a593Smuzhiyun (" .{} : {} nsecs", 'last_tick'), 93*4882a593Smuzhiyun (" .{} : {}", 'tick_stopped'), 94*4882a593Smuzhiyun (" .{} : {}", 'idle_jiffies'), 95*4882a593Smuzhiyun (" .{} : {}", 'idle_calls'), 96*4882a593Smuzhiyun (" .{} : {}", 'idle_sleeps'), 97*4882a593Smuzhiyun (" .{} : {} nsecs", 'idle_entrytime'), 98*4882a593Smuzhiyun (" .{} : {} nsecs", 'idle_waketime'), 99*4882a593Smuzhiyun (" .{} : {} nsecs", 'idle_exittime'), 100*4882a593Smuzhiyun (" .{} : {} nsecs", 'idle_sleeptime'), 101*4882a593Smuzhiyun (" .{}: {} nsecs", 'iowait_sleeptime'), 102*4882a593Smuzhiyun (" .{} : {}", 'last_jiffies'), 103*4882a593Smuzhiyun (" .{} : {}", 'next_timer'), 104*4882a593Smuzhiyun (" .{} : {} nsecs", 'idle_expires')] 105*4882a593Smuzhiyun text += "\n".join([s.format(f, ts[f]) for s, f in fmts]) 106*4882a593Smuzhiyun text += "\njiffies: {}\n".format(jiffies) 107*4882a593Smuzhiyun 108*4882a593Smuzhiyun text += "\n" 109*4882a593Smuzhiyun 110*4882a593Smuzhiyun return text 111*4882a593Smuzhiyun 112*4882a593Smuzhiyun 113*4882a593Smuzhiyundef print_tickdevice(td, cpu): 114*4882a593Smuzhiyun dev = td['evtdev'] 115*4882a593Smuzhiyun text = "Tick Device: mode: {}\n".format(td['mode']) 116*4882a593Smuzhiyun if cpu < 0: 117*4882a593Smuzhiyun text += "Broadcast device\n" 118*4882a593Smuzhiyun else: 119*4882a593Smuzhiyun text += "Per CPU device: {}\n".format(cpu) 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun text += "Clock Event Device: " 122*4882a593Smuzhiyun if dev == 0: 123*4882a593Smuzhiyun text += "<NULL>\n" 124*4882a593Smuzhiyun return text 125*4882a593Smuzhiyun 126*4882a593Smuzhiyun text += "{}\n".format(dev['name']) 127*4882a593Smuzhiyun text += " max_delta_ns: {}\n".format(dev['max_delta_ns']) 128*4882a593Smuzhiyun text += " min_delta_ns: {}\n".format(dev['min_delta_ns']) 129*4882a593Smuzhiyun text += " mult: {}\n".format(dev['mult']) 130*4882a593Smuzhiyun text += " shift: {}\n".format(dev['shift']) 131*4882a593Smuzhiyun text += " mode: {}\n".format(dev['state_use_accessors']) 132*4882a593Smuzhiyun text += " next_event: {} nsecs\n".format(dev['next_event']) 133*4882a593Smuzhiyun 134*4882a593Smuzhiyun text += " set_next_event: {}\n".format(dev['set_next_event']) 135*4882a593Smuzhiyun 136*4882a593Smuzhiyun members = [('set_state_shutdown', " shutdown: {}\n"), 137*4882a593Smuzhiyun ('set_state_periodic', " periodic: {}\n"), 138*4882a593Smuzhiyun ('set_state_oneshot', " oneshot: {}\n"), 139*4882a593Smuzhiyun ('set_state_oneshot_stopped', " oneshot stopped: {}\n"), 140*4882a593Smuzhiyun ('tick_resume', " resume: {}\n")] 141*4882a593Smuzhiyun for member, fmt in members: 142*4882a593Smuzhiyun if dev[member]: 143*4882a593Smuzhiyun text += fmt.format(dev[member]) 144*4882a593Smuzhiyun 145*4882a593Smuzhiyun text += " event_handler: {}\n".format(dev['event_handler']) 146*4882a593Smuzhiyun text += " retries: {}\n".format(dev['retries']) 147*4882a593Smuzhiyun 148*4882a593Smuzhiyun return text 149*4882a593Smuzhiyun 150*4882a593Smuzhiyun 151*4882a593Smuzhiyundef pr_cpumask(mask): 152*4882a593Smuzhiyun nr_cpu_ids = 1 153*4882a593Smuzhiyun if constants.LX_NR_CPUS > 1: 154*4882a593Smuzhiyun nr_cpu_ids = gdb.parse_and_eval("nr_cpu_ids") 155*4882a593Smuzhiyun 156*4882a593Smuzhiyun inf = gdb.inferiors()[0] 157*4882a593Smuzhiyun bits = mask['bits'] 158*4882a593Smuzhiyun num_bytes = (nr_cpu_ids + 7) / 8 159*4882a593Smuzhiyun buf = utils.read_memoryview(inf, bits, num_bytes).tobytes() 160*4882a593Smuzhiyun buf = binascii.b2a_hex(buf) 161*4882a593Smuzhiyun 162*4882a593Smuzhiyun chunks = [] 163*4882a593Smuzhiyun i = num_bytes 164*4882a593Smuzhiyun while i > 0: 165*4882a593Smuzhiyun i -= 1 166*4882a593Smuzhiyun start = i * 2 167*4882a593Smuzhiyun end = start + 2 168*4882a593Smuzhiyun chunks.append(buf[start:end]) 169*4882a593Smuzhiyun if i != 0 and i % 4 == 0: 170*4882a593Smuzhiyun chunks.append(',') 171*4882a593Smuzhiyun 172*4882a593Smuzhiyun extra = nr_cpu_ids % 8 173*4882a593Smuzhiyun if 0 < extra <= 4: 174*4882a593Smuzhiyun chunks[0] = chunks[0][0] # Cut off the first 0 175*4882a593Smuzhiyun 176*4882a593Smuzhiyun return "".join(chunks) 177*4882a593Smuzhiyun 178*4882a593Smuzhiyun 179*4882a593Smuzhiyunclass LxTimerList(gdb.Command): 180*4882a593Smuzhiyun """Print /proc/timer_list""" 181*4882a593Smuzhiyun 182*4882a593Smuzhiyun def __init__(self): 183*4882a593Smuzhiyun super(LxTimerList, self).__init__("lx-timerlist", gdb.COMMAND_DATA) 184*4882a593Smuzhiyun 185*4882a593Smuzhiyun def invoke(self, arg, from_tty): 186*4882a593Smuzhiyun hrtimer_bases = gdb.parse_and_eval("&hrtimer_bases") 187*4882a593Smuzhiyun max_clock_bases = gdb.parse_and_eval("HRTIMER_MAX_CLOCK_BASES") 188*4882a593Smuzhiyun 189*4882a593Smuzhiyun text = "Timer List Version: gdb scripts\n" 190*4882a593Smuzhiyun text += "HRTIMER_MAX_CLOCK_BASES: {}\n".format(max_clock_bases) 191*4882a593Smuzhiyun text += "now at {} nsecs\n".format(ktime_get()) 192*4882a593Smuzhiyun 193*4882a593Smuzhiyun for cpu in cpus.each_online_cpu(): 194*4882a593Smuzhiyun text += print_cpu(hrtimer_bases, cpu, max_clock_bases) 195*4882a593Smuzhiyun 196*4882a593Smuzhiyun if constants.LX_CONFIG_GENERIC_CLOCKEVENTS: 197*4882a593Smuzhiyun if constants.LX_CONFIG_GENERIC_CLOCKEVENTS_BROADCAST: 198*4882a593Smuzhiyun bc_dev = gdb.parse_and_eval("&tick_broadcast_device") 199*4882a593Smuzhiyun text += print_tickdevice(bc_dev, -1) 200*4882a593Smuzhiyun text += "\n" 201*4882a593Smuzhiyun mask = gdb.parse_and_eval("tick_broadcast_mask") 202*4882a593Smuzhiyun mask = pr_cpumask(mask) 203*4882a593Smuzhiyun text += "tick_broadcast_mask: {}\n".format(mask) 204*4882a593Smuzhiyun if constants.LX_CONFIG_TICK_ONESHOT: 205*4882a593Smuzhiyun mask = gdb.parse_and_eval("tick_broadcast_oneshot_mask") 206*4882a593Smuzhiyun mask = pr_cpumask(mask) 207*4882a593Smuzhiyun text += "tick_broadcast_oneshot_mask: {}\n".format(mask) 208*4882a593Smuzhiyun text += "\n" 209*4882a593Smuzhiyun 210*4882a593Smuzhiyun tick_cpu_devices = gdb.parse_and_eval("&tick_cpu_device") 211*4882a593Smuzhiyun for cpu in cpus.each_online_cpu(): 212*4882a593Smuzhiyun tick_dev = cpus.per_cpu(tick_cpu_devices, cpu) 213*4882a593Smuzhiyun text += print_tickdevice(tick_dev, cpu) 214*4882a593Smuzhiyun text += "\n" 215*4882a593Smuzhiyun 216*4882a593Smuzhiyun gdb.write(text) 217*4882a593Smuzhiyun 218*4882a593Smuzhiyun 219*4882a593SmuzhiyunLxTimerList() 220