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 
12 #include "catch_platform.h"
13 
14 #include <string>
15 
16 namespace Catch{
17 
18     bool isDebuggerActive();
19     void writeToDebugConsole( std::string const& text );
20 }
21 
22 #ifdef CATCH_PLATFORM_MAC
23 
24     // The following code snippet based on:
25     // http://cocoawithlove.com/2008/03/break-into-debugger.html
26     #if defined(__ppc64__) || defined(__ppc__)
27         #define CATCH_TRAP() \
28                 __asm__("li r0, 20\nsc\nnop\nli r0, 37\nli r4, 2\nsc\nnop\n" \
29                 : : : "memory","r0","r3","r4" ) /* NOLINT */
30     #else
31         #define CATCH_TRAP() __asm__("int $3\n" : : /* NOLINT */ )
32     #endif
33 
34 #elif defined(CATCH_PLATFORM_LINUX)
35     // If we can use inline assembler, do it because this allows us to break
36     // directly at the location of the failing check instead of breaking inside
37     // raise() called from it, i.e. one stack frame below.
38     #if defined(__GNUC__) && (defined(__i386) || defined(__x86_64))
39         #define CATCH_TRAP() asm volatile ("int $3") /* NOLINT */
40     #else // Fall back to the generic way.
41         #include <signal.h>
42 
43         #define CATCH_TRAP() raise(SIGTRAP)
44     #endif
45 #elif defined(_MSC_VER)
46     #define CATCH_TRAP() __debugbreak()
47 #elif defined(__MINGW32__)
48     extern "C" __declspec(dllimport) void __stdcall DebugBreak();
49     #define CATCH_TRAP() DebugBreak()
50 #endif
51 
52 #ifdef CATCH_TRAP
53     #define CATCH_BREAK_INTO_DEBUGGER() if( Catch::isDebuggerActive() ) { CATCH_TRAP(); }
54 #else
55     #define CATCH_BREAK_INTO_DEBUGGER() Catch::alwaysTrue();
56 #endif
57 
58 #endif // TWOBLUECUBES_CATCH_DEBUGGER_H_INCLUDED
59