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 inline std::string libNativeName( const std::string& libName )
19 {
20 #if defined(_WIN32)
21  return libName+".dll";
22 #elif defined(__linux) || defined(__APPLE__)
23  return "lib"+libName+".so";
24 #else
25  // variant of the GIGO design pattern
26  return libName;
27 #endif
28 }
29 
30 #ifdef _GNU_SOURCE
31 #include <dlfcn.h>
32 static std::string dsoName(void* addr)
33 {
34  Dl_info info;
35  if (dladdr(addr, &info) == 0)
36  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& )
70 {
71  return "";
72 }
73 
74 #endif
75 
76 static bool inDso(void *addr, const std::string& dsoname)
77 {
78 #ifdef _WIN32
79  char sep = '\\';
80 #else
81  char sep = '/';
82 #endif
83 
84  std::string srcname = dsoName(addr);
85  if (srcname.empty()) {
86  // we do not know the name of the library, let's guess it's OK
87  return true;
88  }
89 
90  std::string::size_type pos = dsoname.find_last_of(sep);
91  std::string curname;
92  if (std::string::npos == pos) {
93  curname = dsoname;
94  } else {
95  curname = dsoname.substr(pos+1);
96  }
97 
98  return srcname == curname;
99 }
100 
101 } // end namespace DsoUtils
102 
103 #endif // not GAUDIKERNEL_DSOUTILS_H
104 
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:18