Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 50 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include (CheckCSourceCompiles)
include (CheckCXXCompilerFlag)
include (CheckCXXSourceCompiles)
include (CheckCXXSourceRuns)
include (CheckFunctionExists)
include (CheckIncludeFile)
include (CheckIncludeFileCXX)
Expand All @@ -34,6 +35,7 @@ include (CheckStructHasMember)
include (CheckSymbolExists)
include (CheckTypeSize)
include (CMakePackageConfigHelpers)
include (CMakePushCheckState)
include (CPack)
include (CTest)
include (DetermineGflagsNamespace)
Expand Down Expand Up @@ -112,6 +114,7 @@ check_cxx_compiler_flag (-Wunnamed-type-template-args
check_symbol_exists (snprintf stdio.h HAVE_SNPRINTF)

check_library_exists (unwind get_static_proc_name "" HAVE_LIB_UNWIND)
check_library_exists (DbgHelp UnDecorateSymbolName "" HAVE_DBGHELP)

find_library (UNWIND_LIBRARY NAMES unwind DOC "unwind library")
mark_as_advanced (UNWIND_LIBRARY)
Expand Down Expand Up @@ -339,6 +342,49 @@ else (HAVE_USING_OPERATOR)
set (ac_cv_cxx_using_operator 0)
endif (HAVE_USING_OPERATOR)

if (HAVE_EXECINFO_H)
set (HAVE_STACKTRACE 1)
endif (HAVE_EXECINFO_H)

if (WIN32 OR CYGWIN)
cmake_push_check_state (RESET)
set (CMAKE_REQUIRED_LIBRARIES DbgHelp)

check_cxx_source_runs ([=[
#include <windows.h>
#include <dbghelp.h>
#include <cstdlib>

void foobar() { }

int main()
{
HANDLE process = GetCurrentProcess();

if (!SymInitialize(process, NULL, TRUE))
return EXIT_FAILURE;

char buf[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
SYMBOL_INFO *symbol = reinterpret_cast<SYMBOL_INFO *>(buf);
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
symbol->MaxNameLen = MAX_SYM_NAME;

void* const pc = reinterpret_cast<void*>(&foobar);
BOOL ret = SymFromAddr(process, reinterpret_cast<DWORD64>(pc), 0, symbol);

return ret ? EXIT_SUCCESS : EXIT_FAILURE;
}
]=] HAVE_SYMBOLIZE)

cmake_pop_check_state ()

if (HAVE_SYMBOLIZE)
set (HAVE_STACKTRACE 1)
endif (HAVE_SYMBOLIZE)
elseif (UNIX OR (APPLE AND HAVE_DLADDR))
set (HAVE_SYMBOLIZE 1)
endif (WIN32 OR CYGWIN)

set (SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P})

if (WITH_THREADS AND Threads_FOUND)
Expand Down Expand Up @@ -408,6 +454,10 @@ if (UNWIND_LIBRARY)
target_link_libraries (glog PUBLIC ${UNWIND_LIBRARY})
endif (UNWIND_LIBRARY)

if (HAVE_DBGHELP)
target_link_libraries (glog PUBLIC DbgHelp)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least with MSVC, this isn't necessary due to

#pragma comment(lib, "DbgHelp")

from here. But I'm guessing it's needed for MinGW / Cygwin? Let me get one installed and give it a whirl.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup saw below, that must be the case.

endif (HAVE_DBGHELP)

if (HAVE_PTHREAD)
target_link_libraries (glog PUBLIC ${CMAKE_THREAD_LIBS_INIT})
endif (HAVE_PTHREAD)
Expand Down Expand Up @@ -476,19 +526,6 @@ else (NOT BUILD_SHARED_LIBS)
"GOOGLE_GLOG_DLL_DECL_FOR_UNITTESTS=${_IMPORT}")
endif (NOT BUILD_SHARED_LIBS)

if (HAVE_EXECINFO_H)
set (HAVE_STACKTRACE 1)
endif (HAVE_EXECINFO_H)

if (WIN32)
set (HAVE_STACKTRACE 1)
set (HAVE_SYMBOLIZE 1)
endif (WIN32)

if (UNIX OR (APPLE AND HAVE_DLADDR))
set (HAVE_SYMBOLIZE 1)
endif (UNIX OR (APPLE AND HAVE_DLADDR))

# Unit testing

if (BUILD_TESTING)
Expand Down
3 changes: 3 additions & 0 deletions src/config.h.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@
/* define if your compiler has __sync_val_compare_and_swap */
#cmakedefine HAVE___SYNC_VAL_COMPARE_AND_SWAP

/* define if symbolize support is available */
#cmakedefine HAVE_SYMBOLIZE

/* Define to the sub-directory in which libtool stores uninstalled libraries.
*/
#cmakedefine LT_OBJDIR
Expand Down
12 changes: 8 additions & 4 deletions src/symbolize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -844,18 +844,22 @@ static ATTRIBUTE_NOINLINE bool SymbolizeAndDemangle(void *pc, char *out,

_END_GOOGLE_NAMESPACE_

#elif defined(OS_WINDOWS)
#elif defined(OS_WINDOWS) || defined(OS_CYGWIN)

#include <windows.h>
#include <DbgHelp.h>

#ifdef _MSC_VER
#pragma comment(lib, "DbgHelp")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yup, the above makes sense now.

#endif

_START_GOOGLE_NAMESPACE_

class SymInitializer {
public:
HANDLE process = NULL;
bool ready = false;
SymInitializer() {
HANDLE process;
bool ready;
SymInitializer() : process(NULL), ready(false) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// Initialize the symbol handler.
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms680344(v=vs.85).aspx
process = GetCurrentProcess();
Expand Down
6 changes: 3 additions & 3 deletions src/symbolize.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ void InstallSymbolizeCallback(SymbolizeCallback callback);
// file is opened successfully, returns the file descriptor. Otherwise,
// returns -1. |out_file_name_size| is the size of the file name buffer
// (including the null-terminator).
typedef int (*SymbolizeOpenObjectFileCallback)(uint64_t pc,
uint64_t &start_address,
uint64_t &base_address,
typedef int (*SymbolizeOpenObjectFileCallback)(uint64 pc,
uint64 &start_address,
uint64 &base_address,
char *out_file_name,
int out_file_name_size);
void InstallSymbolizeOpenObjectFileCallback(
Expand Down
14 changes: 11 additions & 3 deletions src/symbolize_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,12 @@ void ATTRIBUTE_NOINLINE TestWithReturnAddress() {
#endif
}

# elif defined(OS_WINDOWS)
# elif defined(OS_WINDOWS) || defined(OS_CYGWIN)

#ifdef _MSC_VER
#include <intrin.h>
#pragma intrinsic(_ReturnAddress)
#endif

struct Foo {
static void func(int x);
Expand All @@ -378,7 +380,13 @@ TEST(Symbolize, SymbolizeWithDemangling) {
}

__declspec(noinline) void TestWithReturnAddress() {
void *return_address = _ReturnAddress();
void *return_address =
#ifdef __GNUC__ // Cygwin and MinGW support
__builtin_return_address(0)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

#else
_ReturnAddress()
#endif
;
const char *symbol = TrySymbolize(return_address);
CHECK(symbol != NULL);
CHECK_STREQ(symbol, "main");
Expand All @@ -401,7 +409,7 @@ int main(int argc, char **argv) {
TestWithPCInsideNonInlineFunction();
TestWithReturnAddress();
return RUN_ALL_TESTS();
# elif defined(OS_WINDOWS)
# elif defined(OS_WINDOWS) || defined(OS_CYGWIN)
TestWithReturnAddress();
return RUN_ALL_TESTS();
# else // OS_WINDOWS
Expand Down
2 changes: 2 additions & 0 deletions src/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
# define HAVE_STACKTRACE
#endif

#ifndef HAVE_SYMBOLIZE
// defined by gcc
#if defined(__ELF__) && defined(OS_LINUX)
# define HAVE_SYMBOLIZE

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kind of wish we could move this check into configuration time, but then we'd need to do it in a couple build systems. Oh well.

Expand All @@ -137,6 +138,7 @@
// Use DbgHelp to symbolize
# define HAVE_SYMBOLIZE
#endif
#endif // !defined(HAVE_SYMBOLIZE)

#ifndef ARRAYSIZE
// There is a better way, but this is good enough for our purpose.
Expand Down