^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) # gdb helper commands and functions for Linux kernel debugging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) # Kernel proc information reader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) # Copyright (c) 2016 Linaro Ltd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) # Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) # Kieran Bingham <kieran.bingham@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) # This work is licensed under the terms of the GNU GPL version 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) import gdb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) from linux import constants
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) from linux import utils
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) from linux import tasks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) from linux import lists
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) from struct import *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) class LxCmdLine(gdb.Command):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) """ Report the Linux Commandline used in the current kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) Equivalent to cat /proc/cmdline on a running target"""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) def __init__(self):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) super(LxCmdLine, self).__init__("lx-cmdline", gdb.COMMAND_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) def invoke(self, arg, from_tty):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) gdb.write(gdb.parse_and_eval("saved_command_line").string() + "\n")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) LxCmdLine()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) class LxVersion(gdb.Command):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) """ Report the Linux Version of the current kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) Equivalent to cat /proc/version on a running target"""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) def __init__(self):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) super(LxVersion, self).__init__("lx-version", gdb.COMMAND_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) def invoke(self, arg, from_tty):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) # linux_banner should contain a newline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) gdb.write(gdb.parse_and_eval("(char *)linux_banner").string())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) LxVersion()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) # Resource Structure Printers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) # /proc/iomem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) # /proc/ioports
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) def get_resources(resource, depth):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) while resource:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) yield resource, depth
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) child = resource['child']
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if child:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) for res, deep in get_resources(child, depth + 1):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) yield res, deep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) resource = resource['sibling']
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) def show_lx_resources(resource_str):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) resource = gdb.parse_and_eval(resource_str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) width = 4 if resource['end'] < 0x10000 else 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) # Iterate straight to the first child
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) for res, depth in get_resources(resource['child'], 0):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) start = int(res['start'])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) end = int(res['end'])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) gdb.write(" " * depth * 2 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) "{0:0{1}x}-".format(start, width) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) "{0:0{1}x} : ".format(end, width) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) res['name'].string() + "\n")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) class LxIOMem(gdb.Command):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) """Identify the IO memory resource locations defined by the kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) Equivalent to cat /proc/iomem on a running target"""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) def __init__(self):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) super(LxIOMem, self).__init__("lx-iomem", gdb.COMMAND_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) def invoke(self, arg, from_tty):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return show_lx_resources("iomem_resource")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) LxIOMem()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) class LxIOPorts(gdb.Command):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) """Identify the IO port resource locations defined by the kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) Equivalent to cat /proc/ioports on a running target"""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) def __init__(self):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) super(LxIOPorts, self).__init__("lx-ioports", gdb.COMMAND_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) def invoke(self, arg, from_tty):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return show_lx_resources("ioport_resource")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) LxIOPorts()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) # Mount namespace viewer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) # /proc/mounts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) def info_opts(lst, opt):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) opts = ""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) for key, string in lst.items():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if opt & key:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) opts += string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return opts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) FS_INFO = {constants.LX_SB_SYNCHRONOUS: ",sync",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) constants.LX_SB_MANDLOCK: ",mand",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) constants.LX_SB_DIRSYNC: ",dirsync",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) constants.LX_SB_NOATIME: ",noatime",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) constants.LX_SB_NODIRATIME: ",nodiratime"}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) MNT_INFO = {constants.LX_MNT_NOSUID: ",nosuid",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) constants.LX_MNT_NODEV: ",nodev",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) constants.LX_MNT_NOEXEC: ",noexec",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) constants.LX_MNT_NOATIME: ",noatime",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) constants.LX_MNT_NODIRATIME: ",nodiratime",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) constants.LX_MNT_RELATIME: ",relatime"}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) mount_type = utils.CachedType("struct mount")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) mount_ptr_type = mount_type.get_type().pointer()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) class LxMounts(gdb.Command):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) """Report the VFS mounts of the current process namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) Equivalent to cat /proc/mounts on a running target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) An integer value can be supplied to display the mount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) values of that process namespace"""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) def __init__(self):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) super(LxMounts, self).__init__("lx-mounts", gdb.COMMAND_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) # Equivalent to proc_namespace.c:show_vfsmnt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) # However, that has the ability to call into s_op functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) # whereas we cannot and must make do with the information we can obtain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) def invoke(self, arg, from_tty):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) argv = gdb.string_to_argv(arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if len(argv) >= 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) try:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) pid = int(argv[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) except gdb.error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) raise gdb.GdbError("Provide a PID as integer value")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) pid = 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) task = tasks.get_task_by_pid(pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if not task:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) raise gdb.GdbError("Couldn't find a process with PID {}"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) .format(pid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) namespace = task['nsproxy']['mnt_ns']
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if not namespace:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) raise gdb.GdbError("No namespace for current process")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) gdb.write("{:^18} {:^15} {:>9} {} {} options\n".format(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) "mount", "super_block", "devname", "pathname", "fstype"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) for vfs in lists.list_for_each_entry(namespace['list'],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) mount_ptr_type, "mnt_list"):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) devname = vfs['mnt_devname'].string()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) devname = devname if devname else "none"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) pathname = ""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) parent = vfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) while True:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) mntpoint = parent['mnt_mountpoint']
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) pathname = utils.dentry_name(mntpoint) + pathname
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (parent == parent['mnt_parent']):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) break
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) parent = parent['mnt_parent']
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (pathname == ""):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) pathname = "/"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) superblock = vfs['mnt']['mnt_sb']
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) fstype = superblock['s_type']['name'].string()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) s_flags = int(superblock['s_flags'])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) m_flags = int(vfs['mnt']['mnt_flags'])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) rd = "ro" if (s_flags & constants.LX_SB_RDONLY) else "rw"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) gdb.write("{} {} {} {} {} {}{}{} 0 0\n".format(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) vfs.format_string(), superblock.format_string(), devname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) pathname, fstype, rd, info_opts(FS_INFO, s_flags),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) info_opts(MNT_INFO, m_flags)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) LxMounts()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) class LxFdtDump(gdb.Command):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) """Output Flattened Device Tree header and dump FDT blob to the filename
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) specified as the command argument. Equivalent to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 'cat /proc/fdt > fdtdump.dtb' on a running target"""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) def __init__(self):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) super(LxFdtDump, self).__init__("lx-fdtdump", gdb.COMMAND_DATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) gdb.COMPLETE_FILENAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) def fdthdr_to_cpu(self, fdt_header):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) fdt_header_be = ">IIIIIII"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) fdt_header_le = "<IIIIIII"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if utils.get_target_endianness() == 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) output_fmt = fdt_header_le
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) output_fmt = fdt_header_be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return unpack(output_fmt, pack(fdt_header_be,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) fdt_header['magic'],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) fdt_header['totalsize'],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) fdt_header['off_dt_struct'],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) fdt_header['off_dt_strings'],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) fdt_header['off_mem_rsvmap'],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) fdt_header['version'],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) fdt_header['last_comp_version']))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) def invoke(self, arg, from_tty):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if not constants.LX_CONFIG_OF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) raise gdb.GdbError("Kernel not compiled with CONFIG_OF\n")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if len(arg) == 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) filename = "fdtdump.dtb"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) filename = arg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) py_fdt_header_ptr = gdb.parse_and_eval(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) "(const struct fdt_header *) initial_boot_params")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) py_fdt_header = py_fdt_header_ptr.dereference()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) fdt_header = self.fdthdr_to_cpu(py_fdt_header)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if fdt_header[0] != constants.LX_OF_DT_HEADER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) raise gdb.GdbError("No flattened device tree magic found\n")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) gdb.write("fdt_magic: 0x{:02X}\n".format(fdt_header[0]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) gdb.write("fdt_totalsize: 0x{:02X}\n".format(fdt_header[1]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) gdb.write("off_dt_struct: 0x{:02X}\n".format(fdt_header[2]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) gdb.write("off_dt_strings: 0x{:02X}\n".format(fdt_header[3]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) gdb.write("off_mem_rsvmap: 0x{:02X}\n".format(fdt_header[4]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) gdb.write("version: {}\n".format(fdt_header[5]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) gdb.write("last_comp_version: {}\n".format(fdt_header[6]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) inf = gdb.inferiors()[0]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) fdt_buf = utils.read_memoryview(inf, py_fdt_header_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) fdt_header[1]).tobytes()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) try:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) f = open(filename, 'wb')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) except gdb.error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) raise gdb.GdbError("Could not open file to dump fdt")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) f.write(fdt_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) f.close()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) gdb.write("Dumped fdt blob to " + filename + "\n")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) LxFdtDump()