debug.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 2010-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-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. # ifndef BX_ANDROID_LOG_TAG
  49. # define BX_ANDROID_LOG_TAG ""
  50. # endif // BX_ANDROID_LOG_TAG
  51. __android_log_write(ANDROID_LOG_DEBUG, BX_ANDROID_LOG_TAG, _out);
  52. #elif BX_PLATFORM_WINDOWS || BX_PLATFORM_WINRT || BX_PLATFORM_XBOX360
  53. OutputDebugStringA(_out);
  54. #elif BX_PLATFORM_IOS || BX_PLATFORM_OSX
  55. # if defined(__OBJC__)
  56. NSLog(@"%s", _out);
  57. # else
  58. NSLog(__CFStringMakeConstantString("%s"), _out);
  59. # endif // defined(__OBJC__)
  60. #elif 0 // BX_PLATFORM_EMSCRIPTEN
  61. emscripten_log(EM_LOG_CONSOLE, "%s", _out);
  62. #else
  63. fputs(_out, stdout);
  64. fflush(stdout);
  65. #endif // BX_PLATFORM_
  66. }
  67. } // namespace bx
  68. #endif // BX_DEBUG_H_HEADER_GUARD