PageRenderTime 82ms CodeModel.GetById 41ms app.highlight 6ms RepoModel.GetById 34ms app.codeStats 0ms

/mordor/assert.cpp

http://github.com/mozy/mordor
C++ | 71 lines | 62 code | 8 blank | 1 comment | 6 complexity | 7d03c035423b88b56cef325d7fcb9908 MD5 | raw file
 1// Copyright (c) 2010 - Mozy, Inc.
 2
 3#include "assert.h"
 4
 5#include "version.h"
 6
 7#ifdef LINUX
 8#include <fcntl.h>
 9#elif defined(OSX)
10#include <sys/sysctl.h>
11#endif
12
13namespace Mordor {
14
15bool Assertion::throwOnAssertion;
16
17bool isDebuggerAttached()
18{
19#ifdef WINDOWS
20    return !!IsDebuggerPresent();
21#elif defined(LINUX)
22    bool result = false;
23    char buffer[1024];
24    snprintf(buffer, 1024, "/proc/%d/status", getpid());
25    int fd = open(buffer, O_RDONLY);
26    if (fd >= 0) {
27        int rc = read(fd, buffer, 1024);
28        if (rc > 0) {
29            const char *tracerPidStr = strstr(buffer, "TracerPid:");
30            if (tracerPidStr) {
31                int tracingPid = atoi(tracerPidStr + 13);
32                if (tracingPid != 0) {
33                    result = true;
34                }
35            }
36        }
37        close(fd);
38    }
39    return result;
40#elif defined(OSX)
41    int mib[4];
42    kinfo_proc info;
43    size_t size;
44    mib[0] = CTL_KERN;
45    mib[1] = KERN_PROC;
46    mib[2] = KERN_PROC_PID;
47    mib[3] = getpid();
48    size = sizeof(kinfo_proc);
49    info.kp_proc.p_flag = 0;
50    sysctl(mib, 4, &info, &size, NULL, 0);
51    return !!(info.kp_proc.p_flag & P_TRACED);
52#else
53    return false;
54#endif
55}
56
57void debugBreak()
58{
59#ifdef WINDOWS
60    DebugBreak();
61#elif defined (GCC)
62#ifdef PPC
63    __asm__("li r0, 20\nsc\nnop\nli r0, 37\nli r4, 2\nsc\nnop\n"
64                    : : : "memory","r0","r3","r4" );
65#elif defined(X86) || defined(X86_64)
66    __asm__("int $3\n" : : );
67#endif
68#endif
69}
70
71}