PStack  2.0
Stack trace printer for MSVC and GCC binaries
shared_library.hpp
Go to the documentation of this file.
1 // ===-- include/psystem/framwork/shared_library.hpp ------ -*- C++ -*- --=== //
2 // Copyright (c) 2015 Matt Bisson. All rights reserved.
3 
18 #pragma once
19 #ifndef PSYSTEM_FRAMEWORK_SHARED_LIBRARY_HPP
20 #define PSYSTEM_FRAMEWORK_SHARED_LIBRARY_HPP
21 
22 #include "platform.hpp"
23 
24 #include <algorithm>
25 #include <type_traits>
26 #include <vector>
27 
28 #include "not_copyable.hpp"
29 #include "shared_handle.hpp"
30 #include "type_traits.hpp"
31 
32 namespace psystem {
33 
56  : public psystem::not_copyable
57 {
61 public:
69  using library_version = uint32_t;
70 
74 protected:
85  explicit shared_library(
86  std::string library_name, size_t num_functions) noexcept;
87 public:
96 
100 public:
112 
122  bool is_loaded() const noexcept;
123 
135  void load();
136 
141  void unload() noexcept;
142 
146 protected:
159  FARPROC get_function(char const *export_name) const;
160 
180  template <size_t N>
181  void load_all_functions(char const *export_names[N])
182  {
183  ASSERT(is_loaded());
184  ASSERT(N == m_functions.size());
185 
186  std::transform(
187  export_names, export_names + N, // Input = export_names
188  m_functions.begin(), // Output = m_functions
189  [this] (char const *export_name) {
190  return get_function(export_name);
191  });
192  }
193 
197 protected:
199  std::vector<FARPROC> m_functions;
200 
203 
205  std::string m_library_name;
206 
210 private:
222  template <typename T>
224  {
225  static_assert(
226  std::is_function<T>::value,
227  "Template argument must be a function pointer.");
228 
235  loaded_function_base(FARPROC proc)
236  : m_function(reinterpret_cast<T*>(proc))
237  {
238  }
239 
250  void retarget(FARPROC proc) noexcept
251  {
252  m_function = reinterpret_cast<T*>(proc);
253  }
254 
259  T *get_ptr() const noexcept
260  {
261  return m_function;
262  }
263 
264  protected:
267  };
268 
269 protected:
297  template <typename T, typename is_void_ref = has_void_return_type<T>>
299  : public loaded_function_base<T>
300  {
310  loaded_function(FARPROC proc) : loaded_function_base<T>(proc) {}
311 
328  template <typename... Args>
329  auto operator()(Args&&... args)
330  // this->m_function is protected, so using it for the return type of
331  // a public function will be trouble. Instead, we can just use T,
332  // and make an assumption that this->m_function is ultimately a T
333  // type.
334  -> decltype(std::declval<T>()(std::forward<Args>(args)...))
335  {
336  ASSERT(this->m_function);
337  return (*this->m_function)(std::forward<Args>(args)...);
338  }
339  };
340 
354  template <typename T>
355  struct loaded_function<T, std::true_type>
356  : public loaded_function_base<T>
357  {
367  loaded_function(FARPROC proc) : loaded_function_base<T>(proc) {}
368 
383  template <typename... Args>
384  void operator()(Args&&... args)
385  {
386  ASSERT(this->m_function);
387  this->m_function(std::forward<Args>(args)...);
388  }
389  };
390 };
391 
392 } // namespace psystem
393 
394 #endif // PSYSTEM_FRAMEWORK_SHARED_LIBRARY_HPP
bool is_loaded() const noexcept
Determines if the shared library is mapped into the process space.
Definition: shared_library.cpp:222
library_version get_version() const
Access the version for the library that this instance wraps.
Definition: shared_library.cpp:163
Master header file for Platform-wide declarations.
loaded_function_base(FARPROC proc)
Construct a loaded_function from a function pointer.
Definition: shared_library.hpp:235
FARPROC get_function(char const *export_name) const
Acquire a function pointer (by name) from a loaded library.
Definition: shared_library.cpp:258
void retarget(FARPROC proc) noexcept
Re-assign a function pointer to this functor instance.
Definition: shared_library.hpp:250
Defines the psystem::not_assignable interface.
loaded_function(FARPROC proc)
Construct a loaded_function from a function pointer.
Definition: shared_library.hpp:367
Definition: shared_handle.hpp:782
~shared_library() noexcept
Clean up the shared library.
Definition: shared_library.cpp:154
#define noexcept
Replace keyword with something useful.
Definition: platform.hpp:71
Remove the ability to copy or reassign from derived classes.
Definition: not_copyable.hpp:43
T * m_function
The function pointer that may be invoked.
Definition: shared_library.hpp:266
#define ASSERT(cond)
Abort the process if the condition is not satisfied.
Definition: platform.hpp:131
Load shared libraries into the current process for execution.
Definition: shared_library.hpp:55
Declare an RAII container for sharing system API handles (psystem::shared_handle).
T * get_ptr() const noexcept
Access the function pointer contained here directly.
Definition: shared_library.hpp:259
void load()
Loads the requested library into the current process space.
Definition: shared_library.cpp:228
std::vector< FARPROC > m_functions
The cache of function pointers gathered from a loaded library.
Definition: shared_library.hpp:199
std::string m_library_name
The (passed-in) name of the library that this class represents.
Definition: shared_library.hpp:205
void load_all_functions(char const *export_names[N])
Fully populate the m_functions data with addresses.
Definition: shared_library.hpp:181
Contains the process examination "system" and basic frameworks.
Definition: pstack_event_handler.hpp:28
psystem::shared_handle< HMODULE, nullptr > m_library_handle
The system handle to the loaded library. It may be nullptr.
Definition: shared_library.hpp:202
Wraps the C++ standard header, type_traits, to provide a few more metafunctions.
void operator()(Args &&...args)
Invoke the library function.
Definition: shared_library.hpp:384
shared_library(std::string library_name, size_t num_functions) noexcept
Construct a shared library instance (without loading the module).
Definition: shared_library.cpp:146
uint32_t library_version
Type for shared library versions.
Definition: shared_library.hpp:69
Encapsulate a loaded function's address into a callable functor.
Definition: shared_library.hpp:298
Common base class for all specializations of loaded_function.
Definition: shared_library.hpp:223
auto operator()(Args &&...args) -> decltype(std::declval< T >()(std::forward< Args >(args)...))
Invoke the library function.
Definition: shared_library.hpp:329
loaded_function(FARPROC proc)
Construct a loaded_function from a function pointer.
Definition: shared_library.hpp:310
void unload() noexcept
Unload this library and clear the function pointers.
Definition: shared_library.cpp:247