Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-13 08:51:48

0001 // Copyright Antony Polukhin, 2016-2025.
0002 //
0003 // Distributed under the Boost Software License, Version 1.0. (See
0004 // accompanying file LICENSE_1_0.txt or copy at
0005 // http://www.boost.org/LICENSE_1_0.txt)
0006 
0007 #ifndef BOOST_STACKTRACE_DETAIL_ADDR_BASE_MSVC_HPP
0008 #define BOOST_STACKTRACE_DETAIL_ADDR_BASE_MSVC_HPP
0009 
0010 #include <boost/config.hpp>
0011 #ifdef BOOST_HAS_PRAGMA_ONCE
0012 #   pragma once
0013 #endif
0014 
0015 #include <cstdio>
0016 #include <memory>
0017 
0018 #ifdef WIN32_LEAN_AND_MEAN
0019 #include <windows.h>
0020 #include <psapi.h>
0021 #else
0022 // Prevent inclusion of extra Windows SDK headers which can cause conflict
0023 // with other code using Windows SDK
0024 #define WIN32_LEAN_AND_MEAN
0025 #include <windows.h>
0026 #include <psapi.h>
0027 #undef WIN32_LEAN_AND_MEAN
0028 #endif
0029 
0030 namespace boost { namespace stacktrace { namespace detail {
0031   inline std::uintptr_t get_own_proc_addr_base(const void* addr) {
0032         // Try to avoid allocating memory for the modules array if possible.
0033         // The stack buffer should be large enough for most processes.
0034         HMODULE modules_stack[1024];
0035         std::unique_ptr<HMODULE[]> modules_allocated;
0036         HMODULE* modules = modules_stack;
0037 
0038         DWORD needed_bytes = 0;
0039         std::uintptr_t addr_base = 0;
0040 
0041         HANDLE process_handle = GetCurrentProcess();
0042         auto enum_process_is_ok = EnumProcessModules(process_handle, modules, sizeof(modules), &needed_bytes);
0043 
0044         // Check if the error is because the buffer is too small.
0045         if (!enum_process_is_ok && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
0046             modules_allocated.reset(new HMODULE[needed_bytes / sizeof(HMODULE)]);
0047             modules = modules_allocated.get();
0048             enum_process_is_ok = EnumProcessModules(process_handle, modules, needed_bytes, &needed_bytes);
0049         }
0050 
0051         if (enum_process_is_ok) {
0052             for (std::size_t i = 0; i < (needed_bytes / sizeof(HMODULE)); ++i) {
0053                 MODULEINFO module_info;
0054 
0055                 // Get the module name
0056                 if (GetModuleInformation(process_handle, modules[i], &module_info, sizeof(module_info))
0057                     && module_info.lpBaseOfDll <= addr && addr < LPBYTE(module_info.lpBaseOfDll) + module_info.SizeOfImage) {
0058                     // Module contains the address
0059                     addr_base = reinterpret_cast<std::uintptr_t>(module_info.lpBaseOfDll);
0060                     break;
0061                 }
0062             }
0063         }
0064 
0065         CloseHandle(process_handle);
0066 
0067         return addr_base;
0068     }
0069 
0070 }}} // namespace boost::stacktrace::detail
0071 
0072 #endif // BOOST_STACKTRACE_DETAIL_ADDR_BASE_MSVC_HPP