1*87305e5aSandvar# $NetBSD: stack,v 1.4 2023/08/01 21:26:27 andvar Exp $ 2f9b0e0c4Sjhawk 3f9b0e0c4Sjhawk# Copyright (c) 2000 The NetBSD Foundation, Inc. 4f9b0e0c4Sjhawk# All rights reserved. 5f9b0e0c4Sjhawk# 6f9b0e0c4Sjhawk# This code is derived from software contributed to The NetBSD Foundation 7f9b0e0c4Sjhawk# by John A. Hawkinson. 8f9b0e0c4Sjhawk# 9f9b0e0c4Sjhawk# Redistribution and use in source and binary forms, with or without 10f9b0e0c4Sjhawk# modification, are permitted provided that the following conditions 11f9b0e0c4Sjhawk# are met: 12f9b0e0c4Sjhawk# 1. Redistributions of source code must retain the above copyright 13f9b0e0c4Sjhawk# notice, this list of conditions and the following disclaimer. 14f9b0e0c4Sjhawk# 2. Redistributions in binary form must reproduce the above copyright 15f9b0e0c4Sjhawk# notice, this list of conditions and the following disclaimer in the 16f9b0e0c4Sjhawk# documentation and/or other materials provided with the distribution. 17f9b0e0c4Sjhawk# 18f9b0e0c4Sjhawk# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19f9b0e0c4Sjhawk# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20f9b0e0c4Sjhawk# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21f9b0e0c4Sjhawk# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22f9b0e0c4Sjhawk# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23f9b0e0c4Sjhawk# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24f9b0e0c4Sjhawk# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25f9b0e0c4Sjhawk# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26f9b0e0c4Sjhawk# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27f9b0e0c4Sjhawk# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28f9b0e0c4Sjhawk# POSSIBILITY OF SUCH DAMAGE. 29f9b0e0c4Sjhawk 30f9b0e0c4Sjhawk 31f9b0e0c4Sjhawk# Follow an i386 kernel stack trace. 32f9b0e0c4Sjhawk# This code makes presumptions about the way frames look, consistent 33f9b0e0c4Sjhawk# with arch/i386/i386/db_trace.c. It also steals algorithms from there. 34f9b0e0c4Sjhawk 35f9b0e0c4Sjhawk# Output looks like: 36f9b0e0c4Sjhawk# 37f9b0e0c4Sjhawk# 0xc03049cb <cpu_reboot+99>(0x100,0x0,0xc04fd728,0x0,0x6) 38f9b0e0c4Sjhawk# at 0xc01bc97d <panic+197> (frame at 0xc5633bd0) 39f9b0e0c4Sjhawk# 40f9b0e0c4Sjhawk# In this case, the initial hex number and offset should be disregarded, 41*87305e5aSandvar# and it should be interpreted as if it were: 42f9b0e0c4Sjhawk# 43f9b0e0c4Sjhawk# cpu_reboot(0x100,0x0,0xc04fd728,0x0,0x6) 44f9b0e0c4Sjhawk# at 0xc01bc97d <panic+197> (frame at 0xc5633bd0) 45f9b0e0c4Sjhawk# 46f9b0e0c4Sjhawk# due to limitations of gdb scripting. 47f9b0e0c4Sjhawk 483c6d754bSjhawkdefine stacktrace 49f9b0e0c4Sjhawk set $frame=$arg0 50f9b0e0c4Sjhawk set $retaddr=$arg1 51f9b0e0c4Sjhawk 52f9b0e0c4Sjhawk while ($frame != 0) 53f9b0e0c4Sjhawk set $callpc = $retaddr 543c6d754bSjhawk set $retaddr = *(long*)($frame+sizeof(long*)) 55f9b0e0c4Sjhawk 56f9b0e0c4Sjhawk set $inst=*(long*)$retaddr 57f9b0e0c4Sjhawk if (($inst & 0xff) == 0x59) 58f9b0e0c4Sjhawk# (popl %ecx) 59f9b0e0c4Sjhawk set $narg=1 60f9b0e0c4Sjhawk else if (($inst & 0xffff) == 0xc483) 61f9b0e0c4Sjhawk# (addl %n, %esp) 62f9b0e0c4Sjhawk set $narg = (($inst >> 16) & 0xff) / 4 63f9b0e0c4Sjhawk else 64f9b0e0c4Sjhawk set $narg = 5 65f9b0e0c4Sjhawk end 66f9b0e0c4Sjhawk 673c6d754bSjhawk set $argp = $frame+sizeof(long*)+sizeof(int) 68f9b0e0c4Sjhawk printf " " 69f9b0e0c4Sjhawk output/a $callpc 70f9b0e0c4Sjhawk printf "(" 71f9b0e0c4Sjhawk while ($narg != 0) 72f9b0e0c4Sjhawk printf "0x%lx", *(long*)$argp 733c6d754bSjhawk set $argp = $argp+sizeof(long*) 74f9b0e0c4Sjhawk set $narg = $narg-1 75f9b0e0c4Sjhawk if ($narg != 0) 76f9b0e0c4Sjhawk printf "," 77f9b0e0c4Sjhawk end 78f9b0e0c4Sjhawk end 79f9b0e0c4Sjhawk printf ")\n at " 80f9b0e0c4Sjhawk output/a $retaddr 81f9b0e0c4Sjhawk printf " (frame at %#x)\n", $frame 82f9b0e0c4Sjhawk 833c6d754bSjhawk set $frame=*(long*)$frame 84f9b0e0c4Sjhawk end 85f9b0e0c4Sjhawkend 86f9b0e0c4Sjhawk 873c6d754bSjhawkdocument stacktrace 883c6d754bSjhawk ==> (gdb) stacktrace FP IP 89f9b0e0c4Sjhawk print a backtrace of all stack frames, starting at frame pointer FP, 903c6d754bSjhawk and instruction pointer IP. 913c6d754bSjhawkend 923c6d754bSjhawk 933c6d754bSjhawkdefine stack 943c6d754bSjhawk stacktrace $ebp $eip 953c6d754bSjhawkend 963c6d754bSjhawk 973c6d754bSjhawkdocument stack 983c6d754bSjhawk => (gdb) stack 993c6d754bSjhawk Print a backtrace of all strack frames, starting at the current $ebp 1003c6d754bSjhawk and $eip. 101f9b0e0c4Sjhawkend 102