diff options
author | Luboš Luňák <l.lunak@collabora.com> | 2020-02-05 11:09:02 +0100 |
---|---|---|
committer | Luboš Luňák <l.lunak@collabora.com> | 2020-02-07 09:11:45 +0100 |
commit | ac9083f64fc064e4bad3dc522a90ca214b3f1c2f (patch) | |
tree | e70d6eb65a95033db6f532b150d5d577e686b6a8 /comphelper | |
parent | 397ad713cf9bc951b4882ca4b6baeb57541e318c (diff) |
make isDebuggerAttached() public comphelper API
So that it can be used also from other places.
Change-Id: Iab90350fd02872ffde180ce74f01f7ff5e4448b4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/88009
Tested-by: Jenkins
Reviewed-by: Luboš Luňák <l.lunak@collabora.com>
Diffstat (limited to 'comphelper')
-rw-r--r-- | comphelper/Library_comphelper.mk | 1 | ||||
-rw-r--r-- | comphelper/source/misc/debuggerinfo.cxx | 58 | ||||
-rw-r--r-- | comphelper/source/misc/threadpool.cxx | 31 |
3 files changed, 60 insertions, 30 deletions
diff --git a/comphelper/Library_comphelper.mk b/comphelper/Library_comphelper.mk index 6e9a6fc77f79..7a8d5bd6f3a6 100644 --- a/comphelper/Library_comphelper.mk +++ b/comphelper/Library_comphelper.mk @@ -102,6 +102,7 @@ $(eval $(call gb_Library_add_exception_objects,comphelper,\ comphelper/source/misc/componentmodule \ comphelper/source/misc/configuration \ comphelper/source/misc/configurationhelper \ + comphelper/source/misc/debuggerinfo \ comphelper/source/misc/DirectoryHelper \ comphelper/source/misc/dispatchcommand \ comphelper/source/misc/docpasswordhelper \ diff --git a/comphelper/source/misc/debuggerinfo.cxx b/comphelper/source/misc/debuggerinfo.cxx new file mode 100644 index 000000000000..9d452f174ab5 --- /dev/null +++ b/comphelper/source/misc/debuggerinfo.cxx @@ -0,0 +1,58 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <comphelper/debuggerinfo.hxx> + +#include <cassert> +#include <cstring> +#include <ctype.h> + +#if defined(_WIN32) +#define WIN32_LEAN_AND_MEAN +#include <windows.h> +#elif defined UNX +#include <unistd.h> +#include <fcntl.h> +#endif + +namespace comphelper +{ +#if defined DBG_UTIL && !defined NDEBUG +bool isDebuggerAttached() +{ +#if defined(_WIN32) + return IsDebuggerPresent(); +#elif defined LINUX + char buf[4096]; + int fd = open("/proc/self/status", O_RDONLY); + if (fd < 0) + return false; + int size = read(fd, buf, sizeof(buf) - 1); + close(fd); + if (size < 0) + return false; + assert(size < int(sizeof(buf)) - 1); + buf[sizeof(buf) - 1] = '\0'; + // "TracerPid: <pid>" for pid != 0 means something is attached + const char* pos = strstr(buf, "TracerPid:"); + if (pos == nullptr) + return false; + pos += strlen("TracerPid:"); + while (*pos != '\n' && isspace(*pos)) + ++pos; + return *pos != '\n' && *pos != '0'; +#else + return false; // feel free to add your platform +#endif +} +#endif + +} // namespace comphelper + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/comphelper/source/misc/threadpool.cxx b/comphelper/source/misc/threadpool.cxx index 9b1991b7f3a1..9dfbff0e2900 100644 --- a/comphelper/source/misc/threadpool.cxx +++ b/comphelper/source/misc/threadpool.cxx @@ -19,6 +19,7 @@ #include <memory> #include <thread> #include <chrono> +#include <comphelper/debuggerinfo.hxx> #if defined HAVE_VALGRIND_HEADERS #include <valgrind/memcheck.h> @@ -320,36 +321,6 @@ bool ThreadTaskTag::isDone() return mnTasksWorking == 0; } -#if defined DBG_UTIL && !defined NDEBUG -static bool isDebuggerAttached() -{ -#if defined(_WIN32) - return IsDebuggerPresent(); -#elif defined LINUX - char buf[ 4096 ]; - int fd = open( "/proc/self/status", O_RDONLY ); - if( fd < 0 ) - return false; - int size = read( fd, buf, sizeof( buf ) - 1 ); - close( fd ); - if( size < 0 ) - return false; - assert( size < int( sizeof( buf )) - 1 ); - buf[ sizeof( buf ) - 1 ] = '\0'; - // "TracerPid: <pid>" for pid != 0 means something is attached - const char* pos = strstr( buf, "TracerPid:" ); - if( pos == nullptr ) - return false; - pos += strlen( "TracerPid:" ); - while( *pos != '\n' && isspace( *pos )) - ++pos; - return *pos != '\n' && *pos != '0'; -#else - return false; // feel free to add your platform -#endif -} -#endif - void ThreadTaskTag::waitUntilDone() { std::unique_lock< std::mutex > aGuard( maMutex ); |