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