catch_debugger.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Created by Phil on 3/12/2013.
  3. * Copyright 2013 Two Blue Cubes Ltd. All rights reserved.
  4. *
  5. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. *
  8. */
  9. #ifndef TWOBLUECUBES_CATCH_DEBUGGER_H_INCLUDED
  10. #define TWOBLUECUBES_CATCH_DEBUGGER_H_INCLUDED
  11. #include "catch_platform.h"
  12. namespace Catch {
  13. bool isDebuggerActive();
  14. }
  15. #ifdef CATCH_PLATFORM_MAC
  16. #if defined(__i386__) || defined(__x86_64__)
  17. #define CATCH_TRAP() __asm__("int $3\n" : : ) /* NOLINT */
  18. #elif defined(__aarch64__)
  19. #define CATCH_TRAP() __asm__(".inst 0xd4200000")
  20. #endif
  21. #elif defined(CATCH_PLATFORM_IPHONE)
  22. // use inline assembler
  23. #if defined(__i386__) || defined(__x86_64__)
  24. #define CATCH_TRAP() __asm__("int $3")
  25. #elif defined(__aarch64__)
  26. #define CATCH_TRAP() __asm__(".inst 0xd4200000")
  27. #elif defined(__arm__) && !defined(__thumb__)
  28. #define CATCH_TRAP() __asm__(".inst 0xe7f001f0")
  29. #elif defined(__arm__) && defined(__thumb__)
  30. #define CATCH_TRAP() __asm__(".inst 0xde01")
  31. #endif
  32. #elif defined(CATCH_PLATFORM_LINUX)
  33. // If we can use inline assembler, do it because this allows us to break
  34. // directly at the location of the failing check instead of breaking inside
  35. // raise() called from it, i.e. one stack frame below.
  36. #if defined(__GNUC__) && (defined(__i386) || defined(__x86_64))
  37. #define CATCH_TRAP() asm volatile ("int $3") /* NOLINT */
  38. #else // Fall back to the generic way.
  39. #include <signal.h>
  40. #define CATCH_TRAP() raise(SIGTRAP)
  41. #endif
  42. #elif defined(_MSC_VER)
  43. #define CATCH_TRAP() __debugbreak()
  44. #elif defined(__MINGW32__)
  45. extern "C" __declspec(dllimport) void __stdcall DebugBreak();
  46. #define CATCH_TRAP() DebugBreak()
  47. #endif
  48. #ifndef CATCH_BREAK_INTO_DEBUGGER
  49. #ifdef CATCH_TRAP
  50. #define CATCH_BREAK_INTO_DEBUGGER() []{ if( Catch::isDebuggerActive() ) { CATCH_TRAP(); } }()
  51. #else
  52. #define CATCH_BREAK_INTO_DEBUGGER() []{}()
  53. #endif
  54. #endif
  55. #endif // TWOBLUECUBES_CATCH_DEBUGGER_H_INCLUDED