1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <comphelper/debuggerinfo.hxx>
17 #define WIN32_LEAN_AND_MEAN
21 #include <sys/types.h>
22 #include <sys/sysctl.h>
31 bool isDebuggerAttached()
34 return IsDebuggerPresent();
36 // https://developer.apple.com/library/archive/qa/qa1361/_index.html
39 struct kinfo_proc info
;
42 // Initialize the flags so that, if sysctl fails for some bizarre
43 // reason, we get a predictable result.
45 info
.kp_proc
.p_flag
= 0;
47 // Initialize mib, which tells sysctl the info we want, in this case
48 // we're looking for information about a specific process ID.
52 mib
[2] = KERN_PROC_PID
;
58 junk
= sysctl(mib
, sizeof(mib
) / sizeof(*mib
), &info
, &size
, nullptr, 0);
61 // We're being debugged if the P_TRACED flag is set.
63 return ((info
.kp_proc
.p_flag
& P_TRACED
) != 0);
66 int fd
= open("/proc/self/status", O_RDONLY
);
69 int size
= read(fd
, buf
, sizeof(buf
) - 1);
73 assert(size
< int(sizeof(buf
)) - 1);
74 buf
[sizeof(buf
) - 1] = '\0';
75 // "TracerPid: <pid>" for pid != 0 means something is attached
76 const char* pos
= strstr(buf
, "TracerPid:");
79 pos
+= strlen("TracerPid:");
80 while (*pos
!= '\n' && isspace(*pos
))
82 return *pos
!= '\n' && *pos
!= '0';
84 return false; // feel free to add your platform
89 } // namespace comphelper
91 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */