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