stack revision 1.4
11.4Sandvar# $NetBSD: stack,v 1.4 2023/08/01 21:26:27 andvar Exp $
21.1Sjhawk
31.1Sjhawk# Copyright (c) 2000 The NetBSD Foundation, Inc.
41.1Sjhawk# All rights reserved.
51.1Sjhawk#
61.1Sjhawk# This code is derived from software contributed to The NetBSD Foundation
71.1Sjhawk# by John A. Hawkinson.
81.1Sjhawk#
91.1Sjhawk# Redistribution and use in source and binary forms, with or without
101.1Sjhawk# modification, are permitted provided that the following conditions
111.1Sjhawk# are met:
121.1Sjhawk# 1. Redistributions of source code must retain the above copyright
131.1Sjhawk#    notice, this list of conditions and the following disclaimer.
141.1Sjhawk# 2. Redistributions in binary form must reproduce the above copyright
151.1Sjhawk#    notice, this list of conditions and the following disclaimer in the
161.1Sjhawk#    documentation and/or other materials provided with the distribution.
171.1Sjhawk#
181.1Sjhawk# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
191.1Sjhawk# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
201.1Sjhawk# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
211.1Sjhawk# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
221.1Sjhawk# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
231.1Sjhawk# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
241.1Sjhawk# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
251.1Sjhawk# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
261.1Sjhawk# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
271.1Sjhawk# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
281.1Sjhawk# POSSIBILITY OF SUCH DAMAGE.
291.1Sjhawk
301.1Sjhawk
311.1Sjhawk# Follow an i386 kernel stack trace.
321.1Sjhawk# This code makes presumptions about the way frames look, consistent
331.1Sjhawk# with arch/i386/i386/db_trace.c. It also steals algorithms from there.
341.1Sjhawk
351.1Sjhawk# Output looks like:
361.1Sjhawk#
371.1Sjhawk#  0xc03049cb <cpu_reboot+99>(0x100,0x0,0xc04fd728,0x0,0x6)
381.1Sjhawk#	      at 0xc01bc97d <panic+197> (frame at 0xc5633bd0)
391.1Sjhawk#
401.1Sjhawk# In this case, the initial hex number and offset should be disregarded,
411.4Sandvar# and it should be interpreted as if it were:
421.1Sjhawk#
431.1Sjhawk#  cpu_reboot(0x100,0x0,0xc04fd728,0x0,0x6)
441.1Sjhawk#	      at 0xc01bc97d <panic+197> (frame at 0xc5633bd0)
451.1Sjhawk#
461.1Sjhawk# due to limitations of gdb scripting.
471.1Sjhawk
481.2Sjhawkdefine stacktrace
491.1Sjhawk  set $frame=$arg0
501.1Sjhawk  set $retaddr=$arg1
511.1Sjhawk
521.1Sjhawk  while ($frame != 0)
531.1Sjhawk    set $callpc = $retaddr
541.2Sjhawk    set $retaddr = *(long*)($frame+sizeof(long*))
551.1Sjhawk
561.1Sjhawk    set $inst=*(long*)$retaddr
571.1Sjhawk    if (($inst & 0xff) == 0x59)
581.1Sjhawk# (popl %ecx)
591.1Sjhawk	set $narg=1
601.1Sjhawk    else if (($inst & 0xffff) == 0xc483)
611.1Sjhawk# (addl %n, %esp)
621.1Sjhawk	   set $narg = (($inst >> 16) & 0xff) / 4
631.1Sjhawk         else
641.1Sjhawk	   set $narg = 5
651.1Sjhawk    end
661.1Sjhawk
671.2Sjhawk    set $argp = $frame+sizeof(long*)+sizeof(int)
681.1Sjhawk    printf "  "
691.1Sjhawk    output/a $callpc
701.1Sjhawk    printf "("
711.1Sjhawk    while ($narg != 0)
721.1Sjhawk      printf "0x%lx", *(long*)$argp
731.2Sjhawk      set $argp = $argp+sizeof(long*)
741.1Sjhawk      set $narg = $narg-1
751.1Sjhawk      if ($narg != 0)
761.1Sjhawk	printf ","
771.1Sjhawk      end
781.1Sjhawk    end
791.1Sjhawk    printf ")\n             at "
801.1Sjhawk    output/a $retaddr
811.1Sjhawk    printf " (frame at %#x)\n", $frame
821.1Sjhawk
831.2Sjhawk    set $frame=*(long*)$frame
841.1Sjhawk  end
851.1Sjhawkend
861.1Sjhawk
871.2Sjhawkdocument stacktrace
881.2Sjhawk  ==> (gdb) stacktrace FP IP
891.2Sjhawk  print a backtrace of all stack frames, starting at frame pointer FP,
901.2Sjhawk  and instruction pointer IP.
911.2Sjhawkend
921.2Sjhawk
931.2Sjhawkdefine stack
941.2Sjhawk  stacktrace $ebp $eip
951.2Sjhawkend
961.2Sjhawk
971.1Sjhawkdocument stack
981.2Sjhawk  => (gdb) stack
991.2Sjhawk  Print a backtrace of all strack frames, starting at the current $ebp
1001.2Sjhawk  and $eip.
1011.2Sjhawkend
102