-
Notifications
You must be signed in to change notification settings - Fork 3
/
debug.h
186 lines (171 loc) · 5.38 KB
/
debug.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* MiniCache Debugging
*
* Authors: Simon Kuenzer <[email protected]>
*
*
* Copyright (c) 2013-2017, NEC Europe Ltd., NEC Corporation All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef _DEBUG_H_
#define _DEBUG_H_
#ifndef __KERNEL__
#include <mini-os/os.h>
#include <mini-os/types.h>
#include <sys/time.h>
#include <inttypes.h>
#endif
extern struct timeval __debug_tsref;
#define init_debug() (gettimeofday(&__debug_tsref, NULL))
#ifndef STRINGIFY
#define STRINGIFY(s) #s
#endif
#ifndef XSTRINGIFY
#define XSTRINGIFY(s) STRINGIFY(s)
#endif
#ifdef ENABLE_DEBUG
#ifdef __MINIOS__
#if (defined __x86_64__ || defined __x86_32__)
#define CONFIG_DEBUG_CALLDEPTH
#endif
#define __debug_printf(fmt, ...) printk((fmt), ##__VA_ARGS__)
#else
#define __debug_printf(fmt, ...) fprintf(stderr, (fmt), ##__VA_ARGS__)
#endif /* __MINIOS__ */
#if defined __MINIOS__ && defined __x86_64__
#define __get_bp() \
({ \
unsigned long bp; \
asm("movq %%rbp, %0":"=r"(bp)); \
bp; \
})
#elif defined __MINIOS__ && defined __x86_32__
#define __get_bp() \
({ \
unsigned long bp; \
asm("movq %%ebp, %0":"=r"(bp)); \
bp; \
})
#endif
#if defined __MINIOS__ && (defined __x86_64__ || defined __x86_32__)
/**
* get_caller(): returns calling address for the current function
*
* Note: On non-x86 platforms, 0xBADC0DED is returned
*/
#define get_caller() \
({ \
unsigned long *frame = (void *) __get_bp(); \
frame[1]; \
})
/**
* get_calldepth(): returns the current number of invoked function calls
*
* Note: On non-x86 platforms, 0x0 is returned always
*/
#define get_calldepth() \
({ \
unsigned long depth = 0; \
unsigned long *frame = (void *) __get_bp(); \
while (frame[0]) { \
++depth; \
frame = (void *) frame[0]; \
} \
depth; \
})
#else
#define get_caller() (0xBADC0DED)
#define get_calldepth() (0x0)
#endif
/**
* printd(): prints a debug message to stdout
*/
#ifdef CONFIG_DEBUG_CALLDEPTH
#define printd(fmt, ...) \
do { \
struct timeval now; \
uint64_t mins, secs, usecs; \
unsigned long cd = get_calldepth(); \
\
gettimeofday(&now, NULL); \
if (now.tv_usec < __debug_tsref.tv_usec) { \
now.tv_usec += 1000000l; \
now.tv_sec--; \
} \
usecs = (now.tv_usec - __debug_tsref.tv_usec); \
\
secs = (now.tv_sec - __debug_tsref.tv_sec); \
secs += usecs / 1000000l; \
usecs %= 1000000l; \
mins = secs / 60; \
secs %= 60; \
\
__debug_printf("[%"PRIu64"m%02"PRIu64".%06"PRIu64"s] ", \
mins, secs, usecs); \
while (cd--) \
__debug_printf("-"); \
__debug_printf(" %s():%d: " \
fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
} while(0)
#else
#define printd(fmt, ...) \
do { \
struct timeval now; \
uint64_t mins, secs, usecs; \
\
gettimeofday(&now, NULL); \
if (now.tv_usec < __debug_tsref.tv_usec) { \
now.tv_usec += 1000000l; \
now.tv_sec--; \
} \
usecs = (now.tv_usec - __debug_tsref.tv_usec); \
\
secs = (now.tv_sec - __debug_tsref.tv_sec); \
secs += usecs / 1000000l; \
usecs %= 1000000l; \
mins = secs / 60; \
secs %= 60; \
\
__debug_printf("[%"PRIu64"m%02"PRIu64".%06"PRIu64"s] %s:%4d: %s(): " \
fmt, mins, secs, usecs, \
__FILE__, __LINE__, __FUNCTION__, \
##__VA_ARGS__); \
} while(0)
#endif
#else /* ENABLE_DEBUG */
#define printd(fmt, ...) do {} while(0)
#define get_caller() (0xDEADC0DE)
#define get_calldepth() (0x0)
#endif /* ENABLE_DEBUG */
#define tprobe_start(x) \
uint64_t x; \
x = target_now_ns();
#define tprobe_end(x) \
x = target_now_ns() - x; \
printk(STRINGIFY(x)": %01"PRIu64".%09"PRIu64"s\n", x / 1000000000ull, x % 1000000000ull);
#endif /* _DEBUG_H_ */