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
|
/*
* Copyright 2010-2017 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bx#license-bsd-2-clause
*/
#include <bx/debug.h>
#include <bx/string.h> // isPrint
#include <inttypes.h> // PRIx*
#if BX_PLATFORM_ANDROID
# include <android/log.h>
#elif BX_PLATFORM_WINDOWS || BX_PLATFORM_WINRT || BX_PLATFORM_XBOX360 || BX_PLATFORM_XBOXONE
extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const char* _str);
#elif BX_PLATFORM_IOS || BX_PLATFORM_OSX
# if defined(__OBJC__)
# import <Foundation/NSObjCRuntime.h>
# else
# include <CoreFoundation/CFString.h>
extern "C" void NSLog(CFStringRef _format, ...);
# endif // defined(__OBJC__)
#elif 0 // BX_PLATFORM_EMSCRIPTEN
# include <emscripten.h>
#else
# include <stdio.h> // fputs, fflush
#endif // BX_PLATFORM_WINDOWS
namespace bx
{
void debugBreak()
{
#if BX_COMPILER_MSVC
__debugbreak();
#elif BX_CPU_ARM
__builtin_trap();
// asm("bkpt 0");
#elif !BX_PLATFORM_NACL && BX_CPU_X86 && (BX_COMPILER_GCC || BX_COMPILER_CLANG)
// NaCl doesn't like int 3:
// NativeClient: NaCl module load failed: Validation failure. File violates Native Client safety rules.
__asm__ ("int $3");
#else // cross platform implementation
int* int3 = (int*)3L;
*int3 = 3;
#endif // BX
}
void debugOutput(const char* _out)
{
#if BX_PLATFORM_ANDROID
# ifndef BX_ANDROID_LOG_TAG
# define BX_ANDROID_LOG_TAG ""
# endif // BX_ANDROID_LOG_TAG
__android_log_write(ANDROID_LOG_DEBUG, BX_ANDROID_LOG_TAG, _out);
#elif BX_PLATFORM_WINDOWS || BX_PLATFORM_WINRT || BX_PLATFORM_XBOX360 || BX_PLATFORM_XBOXONE
OutputDebugStringA(_out);
#elif BX_PLATFORM_IOS || BX_PLATFORM_OSX
# if defined(__OBJC__)
NSLog(@"%s", _out);
# else
NSLog(__CFStringMakeConstantString("%s"), _out);
# endif // defined(__OBJC__)
#elif 0 // BX_PLATFORM_EMSCRIPTEN
emscripten_log(EM_LOG_CONSOLE, "%s", _out);
#elif !BX_CRT_NONE
fputs(_out, stdout);
fflush(stdout);
#else
BX_UNUSED(_out);
#endif // BX_PLATFORM_
}
void debugPrintfVargs(const char* _format, va_list _argList)
{
char temp[8192];
char* out = temp;
int32_t len = vsnprintf(out, sizeof(temp), _format, _argList);
if ( (int32_t)sizeof(temp) < len)
{
out = (char*)alloca(len+1);
len = vsnprintf(out, len, _format, _argList);
}
out[len] = '\0';
debugOutput(out);
}
void debugPrintf(const char* _format, ...)
{
va_list argList;
va_start(argList, _format);
debugPrintfVargs(_format, argList);
va_end(argList);
}
#define DBG_ADDRESS "%" PRIxPTR
void debugPrintfData(const void* _data, uint32_t _size, const char* _format, ...)
{
#define HEX_DUMP_WIDTH 16
#define HEX_DUMP_SPACE_WIDTH 48
#define HEX_DUMP_FORMAT "%-" BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "." BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "s"
va_list argList;
va_start(argList, _format);
debugPrintfVargs(_format, argList);
va_end(argList);
debugPrintf("\ndata: " DBG_ADDRESS ", size: %d\n", _data, _size);
if (NULL != _data)
{
const uint8_t* data = reinterpret_cast<const uint8_t*>(_data);
char hex[HEX_DUMP_WIDTH*3+1];
char ascii[HEX_DUMP_WIDTH+1];
uint32_t hexPos = 0;
uint32_t asciiPos = 0;
for (uint32_t ii = 0; ii < _size; ++ii)
{
snprintf(&hex[hexPos], sizeof(hex)-hexPos, "%02x ", data[asciiPos]);
hexPos += 3;
ascii[asciiPos] = isPrint(data[asciiPos]) ? data[asciiPos] : '.';
asciiPos++;
if (HEX_DUMP_WIDTH == asciiPos)
{
ascii[asciiPos] = '\0';
debugPrintf("\t" DBG_ADDRESS "\t" HEX_DUMP_FORMAT "\t%s\n", data, hex, ascii);
data += asciiPos;
hexPos = 0;
asciiPos = 0;
}
}
if (0 != asciiPos)
{
ascii[asciiPos] = '\0';
debugPrintf("\t" DBG_ADDRESS "\t" HEX_DUMP_FORMAT "\t%s\n", data, hex, ascii);
}
}
#undef HEX_DUMP_WIDTH
#undef HEX_DUMP_SPACE_WIDTH
#undef HEX_DUMP_FORMAT
}
} // namespace bx
|