debug.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright 2010-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #ifndef BX_DEBUG_H_HEADER_GUARD
  6. #define BX_DEBUG_H_HEADER_GUARD
  7. #include "bx.h"
  8. #if BX_PLATFORM_ANDROID
  9. # include <android/log.h>
  10. #elif BX_PLATFORM_WINDOWS || BX_PLATFORM_WINRT || BX_PLATFORM_XBOX360
  11. extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const char* _str);
  12. #elif BX_PLATFORM_IOS || BX_PLATFORM_OSX
  13. # if defined(__OBJC__)
  14. # import <Foundation/NSObjCRuntime.h>
  15. # else
  16. # include <CoreFoundation/CFString.h>
  17. extern "C" void NSLog(CFStringRef _format, ...);
  18. # endif // defined(__OBJC__)
  19. #elif 0 // BX_PLATFORM_EMSCRIPTEN
  20. # include <emscripten.h>
  21. #else
  22. # include <stdio.h>
  23. #endif // BX_PLATFORM_WINDOWS
  24. namespace bx
  25. {
  26. #if BX_COMPILER_CLANG_ANALYZER
  27. inline __attribute__((analyzer_noreturn)) void debugBreak();
  28. #endif // BX_COMPILER_CLANG_ANALYZER
  29. inline void debugBreak()
  30. {
  31. #if BX_COMPILER_MSVC
  32. __debugbreak();
  33. #elif BX_CPU_ARM
  34. __builtin_trap();
  35. // asm("bkpt 0");
  36. #elif !BX_PLATFORM_NACL && BX_CPU_X86 && (BX_COMPILER_GCC || BX_COMPILER_CLANG)
  37. // NaCl doesn't like int 3:
  38. // NativeClient: NaCl module load failed: Validation failure. File violates Native Client safety rules.
  39. __asm__ ("int $3");
  40. #else // cross platform implementation
  41. int* int3 = (int*)3L;
  42. *int3 = 3;
  43. #endif // BX
  44. }
  45. inline void debugOutput(const char* _out)
  46. {
  47. #if BX_PLATFORM_ANDROID
  48. __android_log_write(ANDROID_LOG_DEBUG, "", _out);
  49. #elif BX_PLATFORM_WINDOWS || BX_PLATFORM_WINRT || BX_PLATFORM_XBOX360
  50. OutputDebugStringA(_out);
  51. #elif BX_PLATFORM_IOS || BX_PLATFORM_OSX
  52. # if defined(__OBJC__)
  53. NSLog(@"%s", _out);
  54. # else
  55. NSLog(__CFStringMakeConstantString("%s"), _out);
  56. # endif // defined(__OBJC__)
  57. #elif 0 // BX_PLATFORM_EMSCRIPTEN
  58. emscripten_log(EM_LOG_CONSOLE, "%s", _out);
  59. #else
  60. fputs(_out, stdout);
  61. fflush(stdout);
  62. #endif // BX_PLATFORM_
  63. }
  64. } // namespace bx
  65. #endif // BX_DEBUG_H_HEADER_GUARD