1 # 1 # 2 # gdb helper commands and functions for Linux 2 # gdb helper commands and functions for Linux kernel debugging 3 # 3 # 4 # task & thread tools 4 # task & thread tools 5 # 5 # 6 # Copyright (c) Siemens AG, 2011-2013 6 # Copyright (c) Siemens AG, 2011-2013 7 # 7 # 8 # Authors: 8 # Authors: 9 # Jan Kiszka <jan.kiszka@siemens.com> 9 # Jan Kiszka <jan.kiszka@siemens.com> 10 # 10 # 11 # This work is licensed under the terms of the 11 # This work is licensed under the terms of the GNU GPL version 2. 12 # 12 # 13 13 14 import gdb 14 import gdb 15 15 16 from linux import utils, lists !! 16 from linux import utils 17 17 18 18 19 task_type = utils.CachedType("struct task_stru 19 task_type = utils.CachedType("struct task_struct") 20 20 21 21 22 def task_lists(): 22 def task_lists(): 23 task_ptr_type = task_type.get_type().point 23 task_ptr_type = task_type.get_type().pointer() 24 init_task = gdb.parse_and_eval("init_task" 24 init_task = gdb.parse_and_eval("init_task").address 25 t = init_task !! 25 t = g = init_task 26 26 27 while True: 27 while True: 28 thread_head = t['signal']['thread_head !! 28 while True: 29 for thread in lists.list_for_each_entr !! 29 yield t 30 yield thread << 31 30 32 t = utils.container_of(t['tasks']['nex !! 31 t = utils.container_of(t['thread_group']['next'], 33 task_ptr_type, !! 32 task_ptr_type, "thread_group") >> 33 if t == g: >> 34 break >> 35 >> 36 t = g = utils.container_of(g['tasks']['next'], >> 37 task_ptr_type, "tasks") 34 if t == init_task: 38 if t == init_task: 35 return 39 return 36 40 37 41 38 def get_task_by_pid(pid): 42 def get_task_by_pid(pid): 39 for task in task_lists(): 43 for task in task_lists(): 40 if int(task['pid']) == pid: 44 if int(task['pid']) == pid: 41 return task 45 return task 42 return None 46 return None 43 47 44 48 45 class LxTaskByPidFunc(gdb.Function): 49 class LxTaskByPidFunc(gdb.Function): 46 """Find Linux task by PID and return the t 50 """Find Linux task by PID and return the task_struct variable. 47 51 48 $lx_task_by_pid(PID): Given PID, iterate over 52 $lx_task_by_pid(PID): Given PID, iterate over all tasks of the target and 49 return that task_struct variable which PID mat 53 return that task_struct variable which PID matches.""" 50 54 51 def __init__(self): 55 def __init__(self): 52 super(LxTaskByPidFunc, self).__init__( 56 super(LxTaskByPidFunc, self).__init__("lx_task_by_pid") 53 57 54 def invoke(self, pid): 58 def invoke(self, pid): 55 task = get_task_by_pid(pid) 59 task = get_task_by_pid(pid) 56 if task: 60 if task: 57 return task.dereference() 61 return task.dereference() 58 else: 62 else: 59 raise gdb.GdbError("No task of PID 63 raise gdb.GdbError("No task of PID " + str(pid)) 60 64 61 65 62 LxTaskByPidFunc() 66 LxTaskByPidFunc() 63 67 64 68 65 class LxPs(gdb.Command): 69 class LxPs(gdb.Command): 66 """Dump Linux tasks.""" 70 """Dump Linux tasks.""" 67 71 68 def __init__(self): 72 def __init__(self): 69 super(LxPs, self).__init__("lx-ps", gd 73 super(LxPs, self).__init__("lx-ps", gdb.COMMAND_DATA) 70 74 71 def invoke(self, arg, from_tty): 75 def invoke(self, arg, from_tty): 72 gdb.write("{:>10} {:>12} {:>7}\n".form 76 gdb.write("{:>10} {:>12} {:>7}\n".format("TASK", "PID", "COMM")) 73 for task in task_lists(): 77 for task in task_lists(): 74 gdb.write("{} {:^5} {}\n".format( 78 gdb.write("{} {:^5} {}\n".format( 75 task.format_string().split()[0 79 task.format_string().split()[0], 76 task["pid"].format_string(), 80 task["pid"].format_string(), 77 task["comm"].string())) 81 task["comm"].string())) 78 82 79 83 80 LxPs() 84 LxPs() 81 85 82 86 83 thread_info_type = utils.CachedType("struct th 87 thread_info_type = utils.CachedType("struct thread_info") 84 88 >> 89 ia64_task_size = None >> 90 85 91 86 def get_thread_info(task): 92 def get_thread_info(task): 87 thread_info_ptr_type = thread_info_type.ge 93 thread_info_ptr_type = thread_info_type.get_type().pointer() 88 if task_type.get_type().fields()[0].type = !! 94 if utils.is_target_arch("ia64"): 89 return task['thread_info'] !! 95 global ia64_task_size 90 thread_info = task['stack'].cast(thread_in !! 96 if ia64_task_size is None: >> 97 ia64_task_size = gdb.parse_and_eval("sizeof(struct task_struct)") >> 98 thread_info_addr = task.address + ia64_task_size >> 99 thread_info = thread_info_addr.cast(thread_info_ptr_type) >> 100 else: >> 101 if task.type.fields()[0].type == thread_info_type.get_type(): >> 102 return task['thread_info'] >> 103 thread_info = task['stack'].cast(thread_info_ptr_type) 91 return thread_info.dereference() 104 return thread_info.dereference() 92 105 93 106 94 class LxThreadInfoFunc (gdb.Function): 107 class LxThreadInfoFunc (gdb.Function): 95 """Calculate Linux thread_info from task v 108 """Calculate Linux thread_info from task variable. 96 109 97 $lx_thread_info(TASK): Given TASK, return the 110 $lx_thread_info(TASK): Given TASK, return the corresponding thread_info 98 variable.""" 111 variable.""" 99 112 100 def __init__(self): 113 def __init__(self): 101 super(LxThreadInfoFunc, self).__init__ 114 super(LxThreadInfoFunc, self).__init__("lx_thread_info") 102 115 103 def invoke(self, task): 116 def invoke(self, task): 104 return get_thread_info(task) 117 return get_thread_info(task) 105 118 106 119 107 LxThreadInfoFunc() 120 LxThreadInfoFunc() 108 121 109 122 110 class LxThreadInfoByPidFunc (gdb.Function): 123 class LxThreadInfoByPidFunc (gdb.Function): 111 """Calculate Linux thread_info from task v 124 """Calculate Linux thread_info from task variable found by pid 112 125 113 $lx_thread_info_by_pid(PID): Given PID, return 126 $lx_thread_info_by_pid(PID): Given PID, return the corresponding thread_info 114 variable.""" 127 variable.""" 115 128 116 def __init__(self): 129 def __init__(self): 117 super(LxThreadInfoByPidFunc, self).__i 130 super(LxThreadInfoByPidFunc, self).__init__("lx_thread_info_by_pid") 118 131 119 def invoke(self, pid): 132 def invoke(self, pid): 120 task = get_task_by_pid(pid) 133 task = get_task_by_pid(pid) 121 if task: 134 if task: 122 return get_thread_info(task.derefe 135 return get_thread_info(task.dereference()) 123 else: 136 else: 124 raise gdb.GdbError("No task of PID 137 raise gdb.GdbError("No task of PID " + str(pid)) 125 138 126 139 127 LxThreadInfoByPidFunc() 140 LxThreadInfoByPidFunc()
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.