1# 2# gdb helper commands and functions for Linux kernel debugging 3# 4# Kernel proc information reader 5# 6# Copyright (c) 2016 Linaro Ltd 7# 8# Authors: 9# Kieran Bingham <[email protected]> 10# 11# This work is licensed under the terms of the GNU GPL version 2. 12# 13 14import gdb 15 16 17class LxCmdLine(gdb.Command): 18 """ Report the Linux Commandline used in the current kernel. 19 Equivalent to cat /proc/cmdline on a running target""" 20 21 def __init__(self): 22 super(LxCmdLine, self).__init__("lx-cmdline", gdb.COMMAND_DATA) 23 24 def invoke(self, arg, from_tty): 25 gdb.write(gdb.parse_and_eval("saved_command_line").string() + "\n") 26 27LxCmdLine() 28 29 30class LxVersion(gdb.Command): 31 """ Report the Linux Version of the current kernel. 32 Equivalent to cat /proc/version on a running target""" 33 34 def __init__(self): 35 super(LxVersion, self).__init__("lx-version", gdb.COMMAND_DATA) 36 37 def invoke(self, arg, from_tty): 38 # linux_banner should contain a newline 39 gdb.write(gdb.parse_and_eval("linux_banner").string()) 40 41LxVersion() 42 43 44# Resource Structure Printers 45# /proc/iomem 46# /proc/ioports 47 48def get_resources(resource, depth): 49 while resource: 50 yield resource, depth 51 52 child = resource['child'] 53 if child: 54 for res, deep in get_resources(child, depth + 1): 55 yield res, deep 56 57 resource = resource['sibling'] 58 59 60def show_lx_resources(resource_str): 61 resource = gdb.parse_and_eval(resource_str) 62 width = 4 if resource['end'] < 0x10000 else 8 63 # Iterate straight to the first child 64 for res, depth in get_resources(resource['child'], 0): 65 start = int(res['start']) 66 end = int(res['end']) 67 gdb.write(" " * depth * 2 + 68 "{0:0{1}x}-".format(start, width) + 69 "{0:0{1}x} : ".format(end, width) + 70 res['name'].string() + "\n") 71 72 73class LxIOMem(gdb.Command): 74 """Identify the IO memory resource locations defined by the kernel 75 76Equivalent to cat /proc/iomem on a running target""" 77 78 def __init__(self): 79 super(LxIOMem, self).__init__("lx-iomem", gdb.COMMAND_DATA) 80 81 def invoke(self, arg, from_tty): 82 return show_lx_resources("iomem_resource") 83 84LxIOMem() 85 86 87class LxIOPorts(gdb.Command): 88 """Identify the IO port resource locations defined by the kernel 89 90Equivalent to cat /proc/ioports on a running target""" 91 92 def __init__(self): 93 super(LxIOPorts, self).__init__("lx-ioports", gdb.COMMAND_DATA) 94 95 def invoke(self, arg, from_tty): 96 return show_lx_resources("ioport_resource") 97 98LxIOPorts() 99