^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) # task & thread tools
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) # Copyright (c) Siemens AG, 2011-2013
^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) # Jan Kiszka <jan.kiszka@siemens.com>
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) from linux import utils
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) task_type = utils.CachedType("struct task_struct")
^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) def task_lists():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) task_ptr_type = task_type.get_type().pointer()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) init_task = gdb.parse_and_eval("init_task").address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) t = g = init_task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) while True:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) while True:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) yield t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) t = utils.container_of(t['thread_group']['next'],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) task_ptr_type, "thread_group")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if t == g:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) break
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) t = g = utils.container_of(g['tasks']['next'],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) task_ptr_type, "tasks")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if t == init_task:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) def get_task_by_pid(pid):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) for task in task_lists():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if int(task['pid']) == pid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return None
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) class LxTaskByPidFunc(gdb.Function):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) """Find Linux task by PID and return the task_struct variable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) $lx_task_by_pid(PID): Given PID, iterate over all tasks of the target and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return that task_struct variable which PID matches."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) def __init__(self):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) super(LxTaskByPidFunc, self).__init__("lx_task_by_pid")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) def invoke(self, pid):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) task = get_task_by_pid(pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if task:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return task.dereference()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) raise gdb.GdbError("No task of PID " + str(pid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) LxTaskByPidFunc()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) class LxPs(gdb.Command):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) """Dump Linux tasks."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) def __init__(self):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) super(LxPs, self).__init__("lx-ps", gdb.COMMAND_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) def invoke(self, arg, from_tty):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) gdb.write("{:>10} {:>12} {:>7}\n".format("TASK", "PID", "COMM"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) for task in task_lists():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) gdb.write("{} {:^5} {}\n".format(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) task.format_string().split()[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) task["pid"].format_string(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) task["comm"].string()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) LxPs()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) thread_info_type = utils.CachedType("struct thread_info")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) ia64_task_size = None
^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) def get_thread_info(task):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) thread_info_ptr_type = thread_info_type.get_type().pointer()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if utils.is_target_arch("ia64"):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) global ia64_task_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if ia64_task_size is None:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) ia64_task_size = gdb.parse_and_eval("sizeof(struct task_struct)")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) thread_info_addr = task.address + ia64_task_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) thread_info = thread_info_addr.cast(thread_info_ptr_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if task.type.fields()[0].type == thread_info_type.get_type():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return task['thread_info']
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) thread_info = task['stack'].cast(thread_info_ptr_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return thread_info.dereference()
^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) class LxThreadInfoFunc (gdb.Function):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) """Calculate Linux thread_info from task variable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) $lx_thread_info(TASK): Given TASK, return the corresponding thread_info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) variable."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) def __init__(self):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) super(LxThreadInfoFunc, self).__init__("lx_thread_info")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) def invoke(self, task):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return get_thread_info(task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) LxThreadInfoFunc()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) class LxThreadInfoByPidFunc (gdb.Function):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) """Calculate Linux thread_info from task variable found by pid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) $lx_thread_info_by_pid(PID): Given PID, return the corresponding thread_info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) variable."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) def __init__(self):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) super(LxThreadInfoByPidFunc, self).__init__("lx_thread_info_by_pid")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) def invoke(self, pid):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) task = get_task_by_pid(pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if task:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return get_thread_info(task.dereference())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) raise gdb.GdbError("No task of PID " + str(pid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) LxThreadInfoByPidFunc()