The Gaudi Framework  v29r0 (ff2e7097)
DsoUtils.h
Go to the documentation of this file.
1 // note: This, I believe, should be part of Reflex::SharedLibrary
7 
8 #ifndef GAUDIKERNEL_DSOUTILS_H
9 #define GAUDIKERNEL_DSOUTILS_H
10 
11 // STL includes
12 #include <string>
13 
14 #include "GaudiKernel/System.h"
15 
16 namespace DsoUtils
17 {
18 
19  inline std::string libNativeName( const std::string& libName )
20  {
21 #if defined( _WIN32 )
22  return libName + ".dll";
23 #elif defined( __linux ) || defined( __APPLE__ )
24  return "lib" + libName + ".so";
25 #else
26  // variant of the GIGO design pattern
27  return libName;
28 #endif
29  }
30 
31 #ifdef _GNU_SOURCE
32 #include <dlfcn.h>
33  static std::string dsoName( void* addr )
34  {
35  Dl_info info;
36  if ( dladdr( addr, &info ) == 0 ) return "";
37 
38  const char* pos = strrchr( info.dli_fname, '/' );
39  if ( pos )
40  ++pos;
41  else
42  pos = info.dli_fname;
43  return pos;
44  }
45 #elif defined( _WIN32 )
46 #include <windows.h>
47 
48  static std::string dsoName( void* addr )
49  {
50  if ( addr ) {
51  MEMORY_BASIC_INFORMATION mbi;
52  if ( VirtualQuery( addr, &mbi, sizeof( mbi ) ) ) {
53  HMODULE h_module = (HMODULE)mbi.AllocationBase;
54  char mod[1024];
55  if ( GetModuleFileName( h_module, mod, sizeof( mod ) ) ) {
56  const char* pos = strrchr( mod, '\\' );
57  if ( pos )
58  ++pos;
59  else
60  pos = mod;
61  return pos;
62  }
63  }
64  }
65  return "";
66  }
67 
68 #else // dummy implementation for unknown platforms
69  static std::string dsoName( const ROOT::Reflex::Member& ) { return ""; }
70 
71 #endif
72 
73  static bool inDso( void* addr, const std::string& dsoname )
74  {
75 #ifdef _WIN32
76  char sep = '\\';
77 #else
78  char sep = '/';
79 #endif
80 
81  std::string srcname = dsoName( addr );
82  if ( srcname.empty() ) {
83  // we do not know the name of the library, let's guess it's OK
84  return true;
85  }
86 
87  std::string::size_type pos = dsoname.find_last_of( sep );
88  std::string curname;
89  if ( std::string::npos == pos ) {
90  curname = dsoname;
91  } else {
92  curname = dsoname.substr( pos + 1 );
93  }
94 
95  return srcname == curname;
96  }
97 
98 } // end namespace DsoUtils
99 
100 #endif // not GAUDIKERNEL_DSOUTILS_H
T empty(T...args)
STL class.
T find_last_of(T...args)
T substr(T...args)
std::string libNativeName(const std::string &libName)
Definition: DsoUtils.h:19