Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve compilation times on Windows #172

Merged
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ target_sources(
src/unwind/unwind_with_nothing.cpp
src/unwind/unwind_with_unwind.cpp
src/unwind/unwind_with_winapi.cpp
src/utils/microfmt.cpp
src/utils/utils.cpp
src/platform/dbghelp_syminit_manager.cpp
)

target_include_directories(
Expand Down
1 change: 1 addition & 0 deletions cmake/has_stackwalk.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <dbghelp.h>

Expand Down
4 changes: 1 addition & 3 deletions src/binary/module_base.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include "binary/module_base.hpp"

#include "utils/common.hpp"
#include "platform/platform.hpp"
#include "utils/utils.hpp"

#include <string>
#include <vector>
#include <mutex>
#include <unordered_map>

Expand All @@ -17,7 +16,6 @@
#include "binary/elf.hpp"
#endif
#elif IS_WINDOWS
#include <windows.h>
#include "binary/pe.hpp"
#endif

Expand Down
1 change: 0 additions & 1 deletion src/binary/module_base.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef IMAGE_MODULE_BASE_HPP
#define IMAGE_MODULE_BASE_HPP

#include "utils/common.hpp"
#include "utils/utils.hpp"

#include <cstdint>
Expand Down
3 changes: 2 additions & 1 deletion src/binary/object.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "binary/object.hpp"

#include "utils/common.hpp"
#include "platform/platform.hpp"
#include "utils/utils.hpp"
#include "binary/module_base.hpp"

Expand All @@ -16,6 +16,7 @@
#include <link.h> // needed for dladdr1's link_map info
#endif
#elif IS_WINDOWS
#define WIN32_LEAN_AND_MEAN
Copy link
Owner

Choose a reason for hiding this comment

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

Do you think it'd be worthwhile to just do target_compile_definitions(... PRIVATE WIN32_LEAN_AND_MEAN)?

#include <windows.h>
#endif

Expand Down
12 changes: 7 additions & 5 deletions src/binary/object.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#ifndef OBJECT_HPP
#define OBJECT_HPP

#include "utils/common.hpp"
#include "utils/utils.hpp"
#include "binary/module_base.hpp"

#include <string>
#include <vector>
#include <cstdint>

namespace cpptrace {

struct object_frame;
struct safe_object_frame;

using frame_ptr = std::uintptr_t;
Copy link
Owner

Choose a reason for hiding this comment

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

Can we #include <cpptrace/cpptrace.hpp> here instead. My main concern is this falling out of line with the definition there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I measured this change making a noticeable difference via ClangBuildAnalyzer. If there is a mismatch between these forward declarations and cpptrace.hpp, compilation will fail with an hard error (i.e. you won't silently introduce any UB).

Given that, would you reconsider?

Copy link
Owner

Choose a reason for hiding this comment

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

I think that’s reasonable, I appreciate you measuring. I had also been thinking of splitting ip the cpptrace.hpp file so one option in the future might be to have a more minimal header providing the type.


namespace detail {
object_frame get_frame_object_info(frame_ptr address);

Expand Down
4 changes: 2 additions & 2 deletions src/binary/pe.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include "binary/pe.hpp"

#include "utils/common.hpp"
#include "platform/platform.hpp"
#include "utils/error.hpp"
#include "utils/utils.hpp"

#if IS_WINDOWS
#include <array>
#include <cstddef>
#include <cstdio>
#include <cstring>
#include <string>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

namespace cpptrace {
Expand Down
2 changes: 1 addition & 1 deletion src/binary/pe.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef PE_HPP
#define PE_HPP

#include "utils/common.hpp"
#include "platform/platform.hpp"
#include "utils/utils.hpp"

#if IS_WINDOWS
Expand Down
3 changes: 2 additions & 1 deletion src/cpptrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <new>
vittorioromeo marked this conversation as resolved.
Show resolved Hide resolved
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
Expand All @@ -16,6 +16,7 @@
#include "demangle/demangle.hpp"
#include "platform/exception_type.hpp"
#include "utils/common.hpp"
#include "utils/microfmt.hpp"
#include "utils/utils.hpp"
#include "binary/object.hpp"
#include "binary/safe_dl.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/demangle/demangle_with_winapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <string>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <dbghelp.h>

Expand Down
6 changes: 2 additions & 4 deletions src/from_current.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
#include <typeinfo>

#include "platform/platform.hpp"
#include "utils/common.hpp"
#include "utils/microfmt.hpp"
#include "utils/utils.hpp"

#ifndef _MSC_VER
#include <array>
#include <string.h>
#if IS_WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <sys/mman.h>
Expand All @@ -23,7 +21,7 @@
#include <mach/mach_vm.h>
#else
#include <fstream>
#include <iomanip>
#include <ios>
vittorioromeo marked this conversation as resolved.
Show resolved Hide resolved
#endif
#endif
#endif
Expand Down
40 changes: 40 additions & 0 deletions src/platform/dbghelp_syminit_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

#include "platform/dbghelp_syminit_manager.hpp"

#include "utils/error.hpp"
#include "utils/microfmt.hpp"

#include <unordered_set>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <dbghelp.h>

namespace cpptrace {
namespace detail {

dbghelp_syminit_manager::~dbghelp_syminit_manager() {
for(auto handle : set) {
if(!SymCleanup(handle)) {
ASSERT(false, microfmt::format("Cpptrace SymCleanup failed with code {}\n", GetLastError()).c_str());
}
}
}

void dbghelp_syminit_manager::init(HANDLE proc) {
if(set.count(proc) == 0) {
if(!SymInitialize(proc, NULL, TRUE)) {
throw internal_error("SymInitialize failed {}", GetLastError());
}
set.insert(proc);
}
}

// Thread-safety: Must only be called from symbols_with_dbghelp while the dbghelp_lock lock is held
dbghelp_syminit_manager& get_syminit_manager() {
static dbghelp_syminit_manager syminit_manager;
return syminit_manager;
}

}
}
31 changes: 4 additions & 27 deletions src/platform/dbghelp_syminit_manager.hpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,19 @@
#ifndef DBGHELP_SYMINIT_MANAGER_HPP
#define DBGHELP_SYMINIT_MANAGER_HPP

#include "utils/common.hpp"
#include "utils/utils.hpp"

#include <unordered_set>

#include <windows.h>
#include <dbghelp.h>

namespace cpptrace {
namespace detail {
struct dbghelp_syminit_manager {
std::unordered_set<HANDLE> set;

~dbghelp_syminit_manager() {
for(auto handle : set) {
if(!SymCleanup(handle)) {
ASSERT(false, microfmt::format("Cpptrace SymCleanup failed with code {}\n", GetLastError()).c_str());
}
}
}
std::unordered_set<void*> set;
vittorioromeo marked this conversation as resolved.
Show resolved Hide resolved

void init(HANDLE proc) {
if(set.count(proc) == 0) {
if(!SymInitialize(proc, NULL, TRUE)) {
throw internal_error("SymInitialize failed {}", GetLastError());
}
set.insert(proc);
}
}
~dbghelp_syminit_manager();
void init(void* proc);
};

// Thread-safety: Must only be called from symbols_with_dbghelp while the dbghelp_lock lock is held
inline dbghelp_syminit_manager& get_syminit_manager() {
static dbghelp_syminit_manager syminit_manager;
return syminit_manager;
}
dbghelp_syminit_manager& get_syminit_manager();
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/platform/path.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#ifndef PATH_HPP
#define PATH_HPP

#include "utils/common.hpp"
#include "platform/platform.hpp"

#include <string>
#include <cctype>

#if IS_WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif

Expand Down
1 change: 1 addition & 0 deletions src/platform/program_name.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "platform/platform.hpp"

#if IS_WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#define CPPTRACE_MAX_PATH MAX_PATH
Expand Down
1 change: 1 addition & 0 deletions src/snippets/snippet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <iostream>

#include "utils/common.hpp"
#include "utils/microfmt.hpp"
#include "utils/utils.hpp"

namespace cpptrace {
Expand Down
2 changes: 1 addition & 1 deletion src/symbols/dwarf/dwarf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

#include <cpptrace/cpptrace.hpp>
#include "utils/error.hpp"
#include "utils/microfmt.hpp"
#include "utils/utils.hpp"

#include <functional>
#include <stdexcept>
#include <type_traits>

#ifdef CPPTRACE_USE_NESTED_LIBDWARF_HEADER_PATH
Expand Down
5 changes: 3 additions & 2 deletions src/symbols/dwarf/dwarf_resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
#include "utils/utils.hpp"
#include "platform/path.hpp"
#include "platform/program_name.hpp" // For CPPTRACE_MAX_PATH

#if IS_APPLE
#include "binary/mach-o.hpp"
#endif

#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <functional>
#include <iterator>
#include <memory>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <unordered_map>
Expand Down
2 changes: 1 addition & 1 deletion src/symbols/dwarf/resolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <cpptrace/cpptrace.hpp>
#include "symbols/symbols.hpp"
#include "utils/common.hpp"
#include "platform/platform.hpp"

#include <memory>

Expand Down
3 changes: 0 additions & 3 deletions src/symbols/symbols.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@

#include <cpptrace/cpptrace.hpp>

#include <memory>
#include <vector>
#include <unordered_map>

#include "binary/object.hpp"

namespace cpptrace {
namespace detail {
using collated_vec = std::vector<
Expand Down
2 changes: 1 addition & 1 deletion src/symbols/symbols_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <vector>
#include <unordered_map>

#include "utils/common.hpp"
#include "utils/error.hpp"
#include "binary/object.hpp"

namespace cpptrace {
Expand Down
1 change: 1 addition & 0 deletions src/symbols/symbols_with_addr2line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cpptrace/cpptrace.hpp>
#include "symbols/symbols.hpp"
#include "utils/common.hpp"
#include "utils/microfmt.hpp"
#include "utils/utils.hpp"

#include <cstdint>
Expand Down
6 changes: 4 additions & 2 deletions src/symbols/symbols_with_dbghelp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
#include <cpptrace/cpptrace.hpp>
#include "symbols/symbols.hpp"
#include "platform/dbghelp_syminit_manager.hpp"
#include "binary/object.hpp"
#include "utils/common.hpp"
#include "utils/error.hpp"

#include <memory>
#include <mutex>
#include <regex>
#include <stdexcept>
#include <system_error>
#include <vector>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <dbghelp.h>

Expand Down
3 changes: 0 additions & 3 deletions src/symbols/symbols_with_libdwarf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <cpptrace/cpptrace.hpp>
#include "dwarf/resolver.hpp"
#include "utils/common.hpp"
#include "utils/error.hpp"
#include "utils/utils.hpp"

#include <cstdint>
Expand All @@ -15,8 +14,6 @@
#include <unordered_map>
#include <vector>

#include <iostream>
#include <iomanip>

namespace cpptrace {
namespace detail {
Expand Down
3 changes: 1 addition & 2 deletions src/unwind/unwind.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#ifndef UNWIND_HPP
#define UNWIND_HPP

#include "utils/common.hpp"
#include "utils/utils.hpp"
#include "cpptrace/cpptrace.hpp"
vittorioromeo marked this conversation as resolved.
Show resolved Hide resolved

#include <cstddef>
#include <vector>
Expand Down
Loading
Loading