LibreOffice Module comphelper (master) 1
debuggerinfo.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 */
9
11
12#include <cassert>
13#include <cstring>
14#include <ctype.h>
15
16#if defined(_WIN32)
17#define WIN32_LEAN_AND_MEAN
18#include <windows.h>
19#elif defined MACOSX
20#include <unistd.h>
21#include <sys/types.h>
22#include <sys/sysctl.h>
23#elif defined UNX
24#include <unistd.h>
25#include <fcntl.h>
26#endif
27
28namespace comphelper
29{
30#if defined DBG_UTIL
32{
33#if defined(_WIN32)
34 return IsDebuggerPresent();
35#elif defined MACOSX
36 // https://developer.apple.com/library/archive/qa/qa1361/_index.html
37 int junk;
38 int mib[4];
39 struct kinfo_proc info;
40 size_t size;
41
42 // Initialize the flags so that, if sysctl fails for some bizarre
43 // reason, we get a predictable result.
44
45 info.kp_proc.p_flag = 0;
46
47 // Initialize mib, which tells sysctl the info we want, in this case
48 // we're looking for information about a specific process ID.
49
50 mib[0] = CTL_KERN;
51 mib[1] = KERN_PROC;
52 mib[2] = KERN_PROC_PID;
53 mib[3] = getpid();
54
55 // Call sysctl.
56
57 size = sizeof(info);
58 junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, nullptr, 0);
59 assert(junk == 0);
60
61 // We're being debugged if the P_TRACED flag is set.
62
63 return ((info.kp_proc.p_flag & P_TRACED) != 0);
64#elif defined LINUX
65 char buf[4096];
66 int fd = open("/proc/self/status", O_RDONLY);
67 if (fd < 0)
68 return false;
69 int size = read(fd, buf, sizeof(buf) - 1);
70 close(fd);
71 if (size < 0)
72 return false;
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:");
77 if (pos == nullptr)
78 return false;
79 pos += strlen("TracerPid:");
80 while (*pos != '\n' && isspace(*pos))
81 ++pos;
82 return *pos != '\n' && *pos != '0';
83#else
84 return false; // feel free to add your platform
85#endif
86}
87#endif
88
89} // namespace comphelper
90
91/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool close
size
bool isDebuggerAttached()
Returns true if the process is running with a debugger attached.
size_t pos