chiark / gitweb /
Initial revision
[ssr] / StraySrc / Libraries / Steel / s / calltrace
1 ;
2 ; calltrace
3 ;
4 ; Display a backtrace of the call stack
5 ;
6 ; © 1994-1998 Straylight
7 ;
8
9 ;----- Licensing note -------------------------------------------------------
10 ;
11 ; This file is part of Straylight's Steel library.
12 ;
13 ; Steel is free software; you can redistribute it and/or modify
14 ; it under the terms of the GNU General Public License as published by
15 ; the Free Software Foundation; either version 2, or (at your option)
16 ; any later version.
17 ;
18 ; Steel is distributed in the hope that it will be useful,
19 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ; GNU General Public License for more details.
22 ;
23 ; You should have received a copy of the GNU General Public License
24 ; along with Steel.  If not, write to the Free Software Foundation,
25 ; 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
27                 GET     libs:header
28                 GET     libs:swis
29
30                 IMPORT  |_kernel_unwind|
31                 IMPORT  |_kernel_procname|
32
33                 AREA    |C$$Code|,CODE,READONLY
34
35 99
36                 DCB     "_calltrace",0
37                 ALIGN
38                 DCD     &FF000000+{PC}-%b99
39
40                 EXPORT  |_calltrace|
41 |_calltrace|    ROUT
42
43                 MOV     ip,sp
44                 STMFD   sp!,{fp,ip,lr,pc}
45                 SUB     fp,ip,#4
46                 SUB     sp,sp,#20               ;Make a small buffer
47
48                 ; --- Start displaying the command window ---
49
50                 ADR     a1,wintitle
51                 SWI     Wimp_CommandWindow
52                 SWI     XOS_WriteI+14
53
54                 ; --- Set up dunny `frame pointer' ---
55
56                 MOV     ip,fp
57
58 00              ; --- Print address from which it's called ---
59
60                 LDR     a1,[ip,#-4]             ;Get the link regiser
61                 BIC     a1,a1,#&FC000003        ;Strip off saved PSR
62                 SUB     a1,a1,#4                ;Find the call address
63                 MOV     a2,sp                   ;Point to the buffer
64                 MOV     a3,#20                  ;Buffer size
65                 SWI     XOS_ConvertHex8         ;Convert the address
66                 SWI     XOS_Write0              ;Display it on screen
67                 SWI     XOS_WriteI+' '
68                 SWI     XOS_WriteI+'('
69
70                 ; --- Print procedure name ---
71
72                 LDR     ip,[ip,#-12]
73                 LDR     a4,[ip,#0]              ;Where's the stacked pc?
74                 BIC     a4,a4,#&FC000003        ;Mask off the PSR bits
75                 SUB     a4,a4,#20               ;Point to procedure word
76                 LDR     a2,[a4,#0]              ;And load it
77                 AND     a3,a2,#&FF000000        ;Check the top byte is right
78                 CMP     a3,#&FF000000           ;Is it the right pattern?
79                 BICEQ   a3,a2,#&FF000000        ;Yes: Find the string length
80                 SUBEQ   a1,a4,a3                ;     Point to the proc name
81                 ADRNE   a1,anon                 ;No:  Point to `anonymous'
82                 SWI     XOS_Write0              ;Display the procedure name
83                 SWI     XOS_WriteI+')'
84                 SWI     XOS_NewLine             ;Move on to next line
85
86                 ; --- Unwind the stack another level ---
87
88                 LDR     a1,[ip,#-12]            ;Get the stacked frame ptr
89                 CMP     a1,#0                   ;Is there a real stack frame?
90                 BNE     %00                     ;Yes -- dump that one out
91
92                 ; --- Turn the command window off ---
93
94 01              SWI     Wimp_CommandWindow
95
96                 LDMDB   fp,{fp,sp,pc}^
97
98 wintitle        DCB     "Stack backtrace",0
99 anon            DCB     "Anonymous function",0
100
101                 LTORG
102
103                 END