Jan Kiszka | 7704d58 | 2015-02-17 13:47:07 -0800 | [diff] [blame] | 1 | # |
| 2 | # gdb helper commands and functions for Linux kernel debugging |
| 3 | # |
| 4 | # task & thread tools |
| 5 | # |
| 6 | # Copyright (c) Siemens AG, 2011-2013 |
| 7 | # |
| 8 | # Authors: |
| 9 | # Jan Kiszka <jan.kiszka@siemens.com> |
| 10 | # |
| 11 | # This work is licensed under the terms of the GNU GPL version 2. |
| 12 | # |
| 13 | |
| 14 | import gdb |
| 15 | |
| 16 | from linux import utils |
| 17 | |
| 18 | |
| 19 | task_type = utils.CachedType("struct task_struct") |
| 20 | |
Thiébaud Weksteen | 6ad18b7 | 2015-06-30 14:58:18 -0700 | [diff] [blame] | 21 | |
Daniel Wagner | 54e2289 | 2015-02-17 13:47:41 -0800 | [diff] [blame] | 22 | def task_lists(): |
Daniel Wagner | 54e2289 | 2015-02-17 13:47:41 -0800 | [diff] [blame] | 23 | task_ptr_type = task_type.get_type().pointer() |
| 24 | init_task = gdb.parse_and_eval("init_task").address |
| 25 | t = g = init_task |
Jan Kiszka | 7704d58 | 2015-02-17 13:47:07 -0800 | [diff] [blame] | 26 | |
Daniel Wagner | 54e2289 | 2015-02-17 13:47:41 -0800 | [diff] [blame] | 27 | while True: |
| 28 | while True: |
| 29 | yield t |
Jan Kiszka | 7704d58 | 2015-02-17 13:47:07 -0800 | [diff] [blame] | 30 | |
Daniel Wagner | 54e2289 | 2015-02-17 13:47:41 -0800 | [diff] [blame] | 31 | t = utils.container_of(t['thread_group']['next'], |
| 32 | task_ptr_type, "thread_group") |
| 33 | if t == g: |
| 34 | break |
Jan Kiszka | 7704d58 | 2015-02-17 13:47:07 -0800 | [diff] [blame] | 35 | |
Daniel Wagner | 54e2289 | 2015-02-17 13:47:41 -0800 | [diff] [blame] | 36 | t = g = utils.container_of(g['tasks']['next'], |
| 37 | task_ptr_type, "tasks") |
| 38 | if t == init_task: |
| 39 | return |
Jan Kiszka | 4752871 | 2015-02-17 13:47:10 -0800 | [diff] [blame] | 40 | |
Thiébaud Weksteen | 6ad18b7 | 2015-06-30 14:58:18 -0700 | [diff] [blame] | 41 | |
Jan Kiszka | 4752871 | 2015-02-17 13:47:10 -0800 | [diff] [blame] | 42 | def get_task_by_pid(pid): |
Daniel Wagner | 54e2289 | 2015-02-17 13:47:41 -0800 | [diff] [blame] | 43 | for task in task_lists(): |
Jan Kiszka | 4752871 | 2015-02-17 13:47:10 -0800 | [diff] [blame] | 44 | if int(task['pid']) == pid: |
| 45 | return task |
| 46 | return None |
| 47 | |
| 48 | |
| 49 | class LxTaskByPidFunc(gdb.Function): |
| 50 | """Find Linux task by PID and return the task_struct variable. |
| 51 | |
| 52 | $lx_task_by_pid(PID): Given PID, iterate over all tasks of the target and |
| 53 | return that task_struct variable which PID matches.""" |
| 54 | |
| 55 | def __init__(self): |
| 56 | super(LxTaskByPidFunc, self).__init__("lx_task_by_pid") |
| 57 | |
| 58 | def invoke(self, pid): |
| 59 | task = get_task_by_pid(pid) |
| 60 | if task: |
| 61 | return task.dereference() |
| 62 | else: |
| 63 | raise gdb.GdbError("No task of PID " + str(pid)) |
| 64 | |
| 65 | |
| 66 | LxTaskByPidFunc() |
Jan Kiszka | cf7492e | 2015-02-17 13:47:15 -0800 | [diff] [blame] | 67 | |
| 68 | |
Thiébaud Weksteen | a930850 | 2015-06-30 14:58:21 -0700 | [diff] [blame] | 69 | class LxPs(gdb.Command): |
| 70 | """Dump Linux tasks.""" |
| 71 | |
| 72 | def __init__(self): |
| 73 | super(LxPs, self).__init__("lx-ps", gdb.COMMAND_DATA) |
| 74 | |
| 75 | def invoke(self, arg, from_tty): |
Ritesh Harjani | 4fbe310 | 2020-10-15 20:13:32 -0700 | [diff] [blame] | 76 | gdb.write("{:>10} {:>12} {:>7}\n".format("TASK", "PID", "COMM")) |
Thiébaud Weksteen | a930850 | 2015-06-30 14:58:21 -0700 | [diff] [blame] | 77 | for task in task_lists(): |
Ritesh Harjani | 4fbe310 | 2020-10-15 20:13:32 -0700 | [diff] [blame] | 78 | gdb.write("{} {:^5} {}\n".format( |
| 79 | task.format_string().split()[0], |
| 80 | task["pid"].format_string(), |
| 81 | task["comm"].string())) |
Thiébaud Weksteen | a930850 | 2015-06-30 14:58:21 -0700 | [diff] [blame] | 82 | |
Stephen Boyd | 494dbe0 | 2019-05-14 15:46:02 -0700 | [diff] [blame] | 83 | |
Thiébaud Weksteen | a930850 | 2015-06-30 14:58:21 -0700 | [diff] [blame] | 84 | LxPs() |
| 85 | |
| 86 | |
Jan Kiszka | cf7492e | 2015-02-17 13:47:15 -0800 | [diff] [blame] | 87 | thread_info_type = utils.CachedType("struct thread_info") |
| 88 | |
| 89 | ia64_task_size = None |
| 90 | |
| 91 | |
| 92 | def get_thread_info(task): |
Jan Kiszka | cf7492e | 2015-02-17 13:47:15 -0800 | [diff] [blame] | 93 | thread_info_ptr_type = thread_info_type.get_type().pointer() |
| 94 | if utils.is_target_arch("ia64"): |
| 95 | global ia64_task_size |
| 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: |
Xi Kangjie | 883d50f | 2018-01-18 16:34:00 -0800 | [diff] [blame] | 101 | if task.type.fields()[0].type == thread_info_type.get_type(): |
| 102 | return task['thread_info'] |
Jan Kiszka | cf7492e | 2015-02-17 13:47:15 -0800 | [diff] [blame] | 103 | thread_info = task['stack'].cast(thread_info_ptr_type) |
| 104 | return thread_info.dereference() |
| 105 | |
| 106 | |
| 107 | class LxThreadInfoFunc (gdb.Function): |
| 108 | """Calculate Linux thread_info from task variable. |
| 109 | |
| 110 | $lx_thread_info(TASK): Given TASK, return the corresponding thread_info |
| 111 | variable.""" |
| 112 | |
| 113 | def __init__(self): |
| 114 | super(LxThreadInfoFunc, self).__init__("lx_thread_info") |
| 115 | |
| 116 | def invoke(self, task): |
| 117 | return get_thread_info(task) |
| 118 | |
| 119 | |
| 120 | LxThreadInfoFunc() |
Kieran Bingham | 9f66dee | 2016-05-23 16:25:13 -0700 | [diff] [blame] | 121 | |
| 122 | |
| 123 | class LxThreadInfoByPidFunc (gdb.Function): |
| 124 | """Calculate Linux thread_info from task variable found by pid |
| 125 | |
| 126 | $lx_thread_info_by_pid(PID): Given PID, return the corresponding thread_info |
| 127 | variable.""" |
| 128 | |
| 129 | def __init__(self): |
| 130 | super(LxThreadInfoByPidFunc, self).__init__("lx_thread_info_by_pid") |
| 131 | |
| 132 | def invoke(self, pid): |
| 133 | task = get_task_by_pid(pid) |
| 134 | if task: |
| 135 | return get_thread_info(task.dereference()) |
| 136 | else: |
| 137 | raise gdb.GdbError("No task of PID " + str(pid)) |
| 138 | |
Stephen Boyd | 494dbe0 | 2019-05-14 15:46:02 -0700 | [diff] [blame] | 139 | |
Kieran Bingham | 9f66dee | 2016-05-23 16:25:13 -0700 | [diff] [blame] | 140 | LxThreadInfoByPidFunc() |