1 /* 2 * Created by Phil on 27/12/2010. 3 * Copyright 2010 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 10 #include "catch_debugger.h" 11 #include "catch_errno_guard.h" 12 #include "catch_stream.h" 13 #include "catch_platform.h" 14 15 #ifdef CATCH_PLATFORM_MAC 16 17 # include <assert.h> 18 # include <stdbool.h> 19 # include <sys/types.h> 20 # include <unistd.h> 21 # include <sys/sysctl.h> 22 # include <cstddef> 23 # include <ostream> 24 25 namespace Catch { 26 27 // The following function is taken directly from the following technical note: 28 // http://developer.apple.com/library/mac/#qa/qa2004/qa1361.html 29 30 // Returns true if the current process is being debugged (either 31 // running under the debugger or has a debugger attached post facto). isDebuggerActive()32 bool isDebuggerActive(){ 33 34 int mib[4]; 35 struct kinfo_proc info; 36 std::size_t size; 37 38 // Initialize the flags so that, if sysctl fails for some bizarre 39 // reason, we get a predictable result. 40 41 info.kp_proc.p_flag = 0; 42 43 // Initialize mib, which tells sysctl the info we want, in this case 44 // we're looking for information about a specific process ID. 45 46 mib[0] = CTL_KERN; 47 mib[1] = KERN_PROC; 48 mib[2] = KERN_PROC_PID; 49 mib[3] = getpid(); 50 51 // Call sysctl. 52 53 size = sizeof(info); 54 if( sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, nullptr, 0) != 0 ) { 55 Catch::cerr() << "\n** Call to sysctl failed - unable to determine if debugger is active **\n" << std::endl; 56 return false; 57 } 58 59 // We're being debugged if the P_TRACED flag is set. 60 61 return ( (info.kp_proc.p_flag & P_TRACED) != 0 ); 62 } 63 } // namespace Catch 64 65 #elif defined(CATCH_PLATFORM_LINUX) 66 #include <fstream> 67 #include <string> 68 69 namespace Catch{ 70 // The standard POSIX way of detecting a debugger is to attempt to 71 // ptrace() the process, but this needs to be done from a child and not 72 // this process itself to still allow attaching to this process later 73 // if wanted, so is rather heavy. Under Linux we have the PID of the 74 // "debugger" (which doesn't need to be gdb, of course, it could also 75 // be strace, for example) in /proc/$PID/status, so just get it from 76 // there instead. isDebuggerActive()77 bool isDebuggerActive(){ 78 // Libstdc++ has a bug, where std::ifstream sets errno to 0 79 // This way our users can properly assert over errno values 80 ErrnoGuard guard; 81 std::ifstream in("/proc/self/status"); 82 for( std::string line; std::getline(in, line); ) { 83 static const int PREFIX_LEN = 11; 84 if( line.compare(0, PREFIX_LEN, "TracerPid:\t") == 0 ) { 85 // We're traced if the PID is not 0 and no other PID starts 86 // with 0 digit, so it's enough to check for just a single 87 // character. 88 return line.length() > PREFIX_LEN && line[PREFIX_LEN] != '0'; 89 } 90 } 91 92 return false; 93 } 94 } // namespace Catch 95 #elif defined(_MSC_VER) 96 extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent(); 97 namespace Catch { isDebuggerActive()98 bool isDebuggerActive() { 99 return IsDebuggerPresent() != 0; 100 } 101 } 102 #elif defined(__MINGW32__) 103 extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent(); 104 namespace Catch { isDebuggerActive()105 bool isDebuggerActive() { 106 return IsDebuggerPresent() != 0; 107 } 108 } 109 #else 110 namespace Catch { isDebuggerActive()111 bool isDebuggerActive() { return false; } 112 } 113 #endif // Platform 114