1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - OS - include
3   File:     callTrace.h
4 
5   Copyright 2003-2008 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-09-18#$
14   $Rev: 8573 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef NITRO_OS_CALLTRACE_H_
19 #define NITRO_OS_CALLTRACE_H_
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #include <nitro/os/common/profile.h>
26 
27 // define if do checking callTrace stack over/underflow
28 #define OS_CALLTRACE_CHECK_OVERSTACK
29 
30 // define if record register
31 #define OS_CALLTRACE_RECORD_R0
32 #define OS_CALLTRACE_RECORD_R1
33 #define OS_CALLTRACE_RECORD_R2
34 #define OS_CALLTRACE_RECORD_R3
35 
36 // define if record call level
37 #define OS_CALLTRACE_LEVEL_AVAILABLE
38 
39 //================================================================================
40 //---------------- callTrace structure
41 typedef struct
42 {
43     u32     name;                      // pointer to function name
44     u32     returnAddress;             // value of lr register
45 
46 #ifdef OS_CALLTRACE_LEVEL_AVAILABLE
47     u32     level;                     // call level
48 #endif
49 
50 #ifdef OS_CALLTRACE_RECORD_R0
51     u32     r0;                        // value of r0 register
52 #endif
53 #ifdef OS_CALLTRACE_RECORD_R1
54     u32     r1;                        // value of r1 register
55 #endif
56 #ifdef OS_CALLTRACE_RECORD_R2
57     u32     r2;                        // value of r2 register
58 #endif
59 #ifdef OS_CALLTRACE_RECORD_R3
60     u32     r3;                        // value of r3 register
61 #endif
62 }
63 OSCallTrace;
64 
65 #define OSi_SIZEOF_CALLTRACE     sizeof(OSCallTrace)
66 
67 //---------------- callTraceInfo structure
68 typedef struct
69 {
70     OSCallTrace *current;              // current pointer
71     OSCallTrace *limit;                // buffer limit as hi-address
72     u16     enable;
73     u16     circular;
74 #ifdef OS_CALLTRACE_LEVEL_AVAILABLE
75     u32     level;                     // call level
76 #endif
77     OSCallTrace array[1];              // valiable length
78 }
79 OSCallTraceInfo;
80 
81 #define OSi_TRACEINFO_SIZEOF_HEADERPART (sizeof(OSCallTraceInfo) - sizeof(OSCallTrace))
82 
83 
84 //---------------- mode
85 typedef enum
86 {
87     OS_CALLTRACE_STACK = 0,
88     OS_CALLTRACE_LOG = 1
89 }
90 OSCallTraceMode;
91 
92 //================================================================================
93 /*---------------------------------------------------------------------------*
94   Name:         OS_InitCallTrace
95 
96   Description:  Initialize callTrace stack,
97                 and start record trace.
98 
99   Arguments:    buf     address to record trace infomation
100                 size    size of buffer (by byte)
101                 logFlag OS_CALLTRACE_LOG if remain record in __PROFILE_EXIT as log
102                         OS_CALLTRACE_NOLOG if not remain
103 
104   Returns:      None
105  *---------------------------------------------------------------------------*/
106 #ifndef OS_NO_CALLTRACE
107 void    OS_InitCallTrace(void *buf, u32 size, OSCallTraceMode mode);
108 #else
OS_InitCallTrace(void *,u32,OSCallTraceMode)109 static inline void OS_InitCallTrace(void *, u32, OSCallTraceMode)
110 {                                      /* do nothing */
111 }
112 #endif
113 
114 /*---------------------------------------------------------------------------*
115   Name:         OS_DumpCallTrace
116 
117   Description:  Dump callTrace
118 
119   Arguments:    None
120 
121   Returns:      None
122  *---------------------------------------------------------------------------*/
123 #ifndef OS_NO_CALLTRACE
124 void    OS_DumpCallTrace(void);
125 #else
OS_DumpCallTrace(void)126 static inline void OS_DumpCallTrace(void)
127 {                                      /* do nothing */
128 }
129 #endif
130 
131 /*---------------------------------------------------------------------------*
132   Name:         OS_DumpThreadCallTrace
133 
134   Description:  dump callStack of thread
135 
136   Arguments:    thread :    thread
137 
138   Returns:      None
139  *---------------------------------------------------------------------------*/
140 //  void OS_DumpThreadCallTrace( OSThread* thread );
141 //
142 // notice: This function is defined in os_thread.h because 'OSThread' declaration.
143 //
144 
145 /*---------------------------------------------------------------------------*
146   Name:         OS_CalcCallTraceLines
147 
148   Description:  calculate number of lines to be able to be allocated
149 
150   Arguments:    size :    CallTrace buffer size
151                           same as argument of OS_InitCallTrace()'s size
152 
153   Returns:      number of lines to be able to allocate in 'size' byte
154  *---------------------------------------------------------------------------*/
155 #ifndef OS_NO_CALLTRACE
156 int     OS_CalcCallTraceLines(u32 size);
157 #else
OS_CalcCallTraceLines(u32)158 static inline int OS_CalcCallTraceLines(u32)
159 {
160     return 0;
161 }
162 #endif
163 
164 /*---------------------------------------------------------------------------*
165   Name:         OS_CalcCallTraceBufferSize
166 
167   Description:  calculate buffer size to allocate specified lines
168 
169   Arguments:    lines :   lines to want to allocate
170 
171   Returns:      buffer size to need
172  *---------------------------------------------------------------------------*/
173 #ifndef OS_NO_CALLTRACE
174 u32     OS_CalcCallTraceBufferSize(int lines);
175 #else
OS_CalcCallTraceBufferSize(int)176 static inline u32 OS_CalcCallTraceBufferSize(int)
177 {
178     return 0;
179 }
180 #endif
181 
182 /*---------------------------------------------------------------------------*
183   Name:         OS_ClearCallTraceBuffer
184 
185   Description:  clear buffer if used for logging
186 
187   Arguments:    None
188 
189   Returns:      None
190  *---------------------------------------------------------------------------*/
191 #ifndef OS_NO_CALLTRACE
192 void    OS_ClearCallTraceBuffer(void);
193 #else
OS_ClearCallTraceBuffer(void)194 static inline void OS_ClearCallTraceBuffer(void)
195 {                                      /* do nothing */
196 }
197 #endif
198 
199 
200 /*---------------------------------------------------------------------------*
201   Name:         OS_EnableCallTrace
202 
203   Description:  enable callTrace
204 
205   Arguments:    None
206 
207   Returns:      previous status. TRUE if enable
208  *---------------------------------------------------------------------------*/
209 #ifndef OS_NO_CALLTRACE
210 BOOL    OS_EnableCallTrace();
211 #else
OS_EnableCallTrace()212 static inline BOOL OS_EnableCallTrace()
213 {
214     return FALSE;
215 }
216 #endif
217 
218 /*---------------------------------------------------------------------------*
219   Name:         OS_DisableCallTrace
220 
221   Description:  disable callTrace
222 
223   Arguments:    None
224 
225   Returns:      previous status. TRUE if enable
226  *---------------------------------------------------------------------------*/
227 #ifndef OS_NO_CALLTRACE
228 BOOL    OS_DisableCallTrace();
229 #else
OS_DisableCallTrace()230 static inline BOOL OS_DisableCallTrace()
231 {
232     return FALSE;
233 }
234 #endif
235 
236 /*---------------------------------------------------------------------------*
237   Name:         OS_RestoreCallTrace
238 
239   Description:  set status of callTrace
240 
241   Arguments:    enable :  TRUE if set to be enable, FALSE if not
242 
243   Returns:      previous status. TRUE if enable
244  *---------------------------------------------------------------------------*/
245 #ifndef OS_NO_CALLTRACE
246 BOOL    OS_RestoreCallTrace(BOOL enable);
247 #else
OS_RestoreCallTrace(BOOL)248 static inline BOOL OS_RestoreCallTrace(BOOL)
249 {
250     return FALSE;
251 }
252 #endif
253 
254 
255 //---- following functions are for calling from other os library.
256 //     don't call from user proc.
257 //
258 #ifndef OS_NO_CALLTRACE
259 void    OSi_SetCallTraceEntry(const char *name, u32 lr);
260 #else
261 static inline void OSi_SetCallTraceEntry(const char *name, u32 lr);
262 #endif
263 
264 
265 //--------------------------------------------------------------------------------
266 #ifdef __cplusplus
267 } /* extern "C" */
268 #endif
269 
270 /* NITRO_OS_CALLTRACE_H_ */
271 #endif
272