gdbmacros.txt 6.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#
# This file contains a few gdb macros (user defined commands) to extract
# useful information from kernel crashdump (kdump) like stack traces of
# all the processes or a particular process and trapinfo.
#
# These macros can be used by copying this file in .gdbinit (put in home
# directory or current directory) or by invoking gdb command with
# --command=<command-file-name> option
#
# Credits:
# Alexander Nyberg <alexn@telia.com>
# V Srivatsa <vatsa@in.ibm.com>
# Maneesh Soni <maneesh@in.ibm.com>
#

define bttnobp
	set $tasks_off=((size_t)&((struct task_struct *)0)->tasks)
18
	set $pid_off=((size_t)&((struct task_struct *)0)->thread_group.next)
19 20
	set $init_t=&init_task
	set $next_t=(((char *)($init_t->tasks).next) - $tasks_off)
21
	set var $stacksize = sizeof(union thread_union)
22 23 24 25
	while ($next_t != $init_t)
		set $next_t=(struct task_struct *)$next_t
		printf "\npid %d; comm %s:\n", $next_t.pid, $next_t.comm
		printf "===================\n"
26 27
		set var $stackp = $next_t.thread.sp
		set var $stack_top = ($stackp & ~($stacksize - 1)) + $stacksize
28 29 30 31 32 33 34

		while ($stackp < $stack_top)
			if (*($stackp) > _stext && *($stackp) < _sinittext)
				info symbol *($stackp)
			end
			set $stackp += 4
		end
35
		set $next_th=(((char *)$next_t->thread_group.next) - $pid_off)
36 37 38 39
		while ($next_th != $next_t)
			set $next_th=(struct task_struct *)$next_th
			printf "\npid %d; comm %s:\n", $next_t.pid, $next_t.comm
			printf "===================\n"
40 41
			set var $stackp = $next_t.thread.sp
			set var $stack_top = ($stackp & ~($stacksize - 1)) + stacksize
42 43 44 45 46 47 48

			while ($stackp < $stack_top)
				if (*($stackp) > _stext && *($stackp) < _sinittext)
					info symbol *($stackp)
				end
				set $stackp += 4
			end
49
			set $next_th=(((char *)$next_th->thread_group.next) - $pid_off)
50 51 52 53 54 55 56 57
		end
		set $next_t=(char *)($next_t->tasks.next) - $tasks_off
	end
end
document bttnobp
	dump all thread stack traces on a kernel compiled with !CONFIG_FRAME_POINTER
end

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
define btthreadstack
	set var $pid_task = $arg0

	printf "\npid %d; comm %s:\n", $pid_task.pid, $pid_task.comm
	printf "task struct: "
	print $pid_task
	printf "===================\n"
	set var $stackp = $pid_task.thread.sp
	set var $stacksize = sizeof(union thread_union)
	set var $stack_top = ($stackp & ~($stacksize - 1)) + $stacksize
	set var $stack_bot = ($stackp & ~($stacksize - 1))

	set $stackp = *((unsigned long *) $stackp)
	while (($stackp < $stack_top) && ($stackp > $stack_bot))
		set var $addr = *(((unsigned long *) $stackp) + 1)
		info symbol $addr
		set $stackp = *((unsigned long *) $stackp)
	end
end
document btthreadstack
	 dump a thread stack using the given task structure pointer
end


82 83
define btt
	set $tasks_off=((size_t)&((struct task_struct *)0)->tasks)
84
	set $pid_off=((size_t)&((struct task_struct *)0)->thread_group.next)
85 86 87 88
	set $init_t=&init_task
	set $next_t=(((char *)($init_t->tasks).next) - $tasks_off)
	while ($next_t != $init_t)
		set $next_t=(struct task_struct *)$next_t
89
		btthreadstack $next_t
90

91
		set $next_th=(((char *)$next_t->thread_group.next) - $pid_off)
92 93
		while ($next_th != $next_t)
			set $next_th=(struct task_struct *)$next_th
94 95
			btthreadstack $next_th
			set $next_th=(((char *)$next_th->thread_group.next) - $pid_off)
96 97 98 99 100 101 102 103 104 105 106
		end
		set $next_t=(char *)($next_t->tasks.next) - $tasks_off
	end
end
document btt
	dump all thread stack traces on a kernel compiled with CONFIG_FRAME_POINTER
end

define btpid
	set var $pid = $arg0
	set $tasks_off=((size_t)&((struct task_struct *)0)->tasks)
107
	set $pid_off=((size_t)&((struct task_struct *)0)->thread_group.next)
108 109 110 111 112 113 114 115 116 117 118
	set $init_t=&init_task
	set $next_t=(((char *)($init_t->tasks).next) - $tasks_off)
	set var $pid_task = 0

	while ($next_t != $init_t)
		set $next_t=(struct task_struct *)$next_t

		if ($next_t.pid == $pid)
			set $pid_task = $next_t
		end

119
		set $next_th=(((char *)$next_t->thread_group.next) - $pid_off)
120 121 122 123 124
		while ($next_th != $next_t)
			set $next_th=(struct task_struct *)$next_th
			if ($next_th.pid == $pid)
				set $pid_task = $next_th
			end
125
			set $next_th=(((char *)$next_th->thread_group.next) - $pid_off)
126 127 128 129
		end
		set $next_t=(char *)($next_t->tasks.next) - $tasks_off
	end

130
	btthreadstack $pid_task
131 132 133 134 135 136 137 138 139
end
document btpid
	backtrace of pid
end


define trapinfo
	set var $pid = $arg0
	set $tasks_off=((size_t)&((struct task_struct *)0)->tasks)
140
	set $pid_off=((size_t)&((struct task_struct *)0)->thread_group.next)
141 142 143 144 145 146 147 148 149 150 151
	set $init_t=&init_task
	set $next_t=(((char *)($init_t->tasks).next) - $tasks_off)
	set var $pid_task = 0

	while ($next_t != $init_t)
		set $next_t=(struct task_struct *)$next_t

		if ($next_t.pid == $pid)
			set $pid_task = $next_t
		end

152
		set $next_th=(((char *)$next_t->thread_group.next) - $pid_off)
153 154 155 156 157
		while ($next_th != $next_t)
			set $next_th=(struct task_struct *)$next_th
			if ($next_th.pid == $pid)
				set $pid_task = $next_th
			end
158
			set $next_th=(((char *)$next_th->thread_group.next) - $pid_off)
159 160 161 162 163 164 165 166 167 168 169
		end
		set $next_t=(char *)($next_t->tasks.next) - $tasks_off
	end

	printf "Trapno %ld, cr2 0x%lx, error_code %ld\n", $pid_task.thread.trap_no, \
				$pid_task.thread.cr2, $pid_task.thread.error_code

end
document trapinfo
	Run info threads and lookup pid of thread #1
	'trapinfo <pid>' will tell you by which trap & possibly
Lee Revell's avatar
Lee Revell committed
170
	address the kernel panicked.
171
end
172

173 174 175 176 177 178 179 180 181 182 183
define dump_log_idx
	set $idx = $arg0
	if ($argc > 1)
		set $prev_flags = $arg1
	else
		set $prev_flags = 0
	end
	set $msg = ((struct printk_log *) (log_buf + $idx))
	set $prefix = 1
	set $newline = 1
	set $log = log_buf + $idx + sizeof(*$msg)
184

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	# prev & LOG_CONT && !(msg->flags & LOG_PREIX)
	if (($prev_flags & 8) && !($msg->flags & 4))
		set $prefix = 0
	end

	# msg->flags & LOG_CONT
	if ($msg->flags & 8)
		# (prev & LOG_CONT && !(prev & LOG_NEWLINE))
		if (($prev_flags & 8) && !($prev_flags & 2))
			set $prefix = 0
		end
		# (!(msg->flags & LOG_NEWLINE))
		if (!($msg->flags & 2))
			set $newline = 0
		end
	end

	if ($prefix)
		printf "[%5lu.%06lu] ", $msg->ts_nsec / 1000000000, $msg->ts_nsec % 1000000000
	end
	if ($msg->text_len != 0)
		eval "printf \"%%%d.%ds\", $log", $msg->text_len, $msg->text_len
	end
	if ($newline)
		printf "\n"
	end
	if ($msg->dict_len > 0)
		set $dict = $log + $msg->text_len
		set $idx = 0
		set $line = 1
		while ($idx < $msg->dict_len)
			if ($line)
				printf " "
				set $line = 0
			end
			set $c = $dict[$idx]
			if ($c == '\0')
				printf "\n"
				set $line = 1
			else
				if ($c < ' ' || $c >= 127 || $c == '\\')
					printf "\\x%02x", $c
				else
					printf "%c", $c
				end
			end
			set $idx = $idx + 1
		end
		printf "\n"
	end
end
document dump_log_idx
	Dump a single log given its index in the log buffer.  The first
	parameter is the index into log_buf, the second is optional and
	specified the previous log buffer's flags, used for properly
	formatting continued lines.
end
242

243 244 245 246
define dmesg
	set $i = log_first_idx
	set $end_idx = log_first_idx
	set $prev_flags = 0
247

248 249 250 251
	while (1)
		set $msg = ((struct printk_log *) (log_buf + $i))
		if ($msg->len == 0)
			set $i = 0
252
		else
253 254 255 256 257 258
			dump_log_idx $i $prev_flags
			set $i = $i + $msg->len
			set $prev_flags = $msg->flags
		end
		if ($i == $end_idx)
			loop_break
259 260 261 262 263 264
		end
	end
end
document dmesg
	print the kernel ring buffer
end