All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ModuleInfo.cpp
Go to the documentation of this file.
1 // $Header: /tmp/svngaudi/tmp.jEpFh25751/Gaudi/GaudiKernel/src/Lib/ModuleInfo.cpp,v 1.10 2008/10/28 10:40:19 marcocle Exp $
2 //====================================================================
3 // ModuleInfo.cpp
4 //--------------------------------------------------------------------
5 //
6 // Package : System (The LHCb System service)
7 //
8 // Description: Implementation of Systems internals
9 //
10 // Author : M.Frank
11 // Created : 13/1/99
12 // Changes :
13 //====================================================================
14 #define SYSTEM_MODULEINFO_CPP
15 
16 //#include <ctime>
17 #include <cstring>
18 #include <cstdlib>
19 //#include <iostream>
20 //#include <typeinfo>
21 
22 #include "GaudiKernel/ModuleInfo.h"
23 #include "GaudiKernel/System.h"
24 
25 #ifdef _WIN32
26  #define NOMSG
27  #define NOGDI
28  # define strcasecmp _stricmp
29  # define strncasecmp _strnicmp
30  #include "process.h"
31  #include "windows.h"
32  #include "Win32PsApi.h"
33 static PsApiFunctions _psApi;
34  #define getpid _getpid
35  #undef NOMSG
36  #undef NOGDI
37  #ifndef PATH_MAX
38  # define PATH_MAX 1024
39  #endif
40 #else // UNIX...: first the EGCS stuff, then the OS dependent includes
41  #include <errno.h>
42  #include <string.h>
43  #include "sys/times.h"
44  #include "sys/param.h"
45  #include "unistd.h"
46  #include "libgen.h"
47  #include <cstdio>
48  #include <dlfcn.h>
49 #endif
50 
51 static System::ImageHandle ModuleHandle = 0;
52 static std::vector<std::string> s_linkedModules;
53 
55 const std::string& System::moduleName() {
56  static std::string module("");
57  if ( module == "" ) {
58  if ( processHandle() && moduleHandle() ) {
59 #ifdef _WIN32
60  char moduleName[256] = {"Unknown.module"};
61  moduleName[0] = 0;
62  if ( _psApi.isValid() ) {
63  _psApi.GetModuleBaseNameA( processHandle(), (HINSTANCE)moduleHandle(), moduleName, sizeof(moduleName) );
64  }
65  std::string mod = moduleName;
66 #elif defined(__linux) || defined(__APPLE__)
67  std::string mod = ::basename((char*)((Dl_info*)moduleHandle())->dli_fname);
68 #elif __hpux
69  std::string mod = ::basename(((HMODULE*)moduleHandle())->dsc.filename);
70 #endif
71  module = mod.substr(0, mod.rfind('.'));
72  }
73  }
74  return module;
75 }
76 
78 const std::string& System::moduleNameFull() {
79  static std::string module("");
80  if ( module == "" ) {
81  if ( processHandle() && moduleHandle() ) {
82  char name[PATH_MAX] = {"Unknown.module"};
83  name[0] = 0;
84 #ifdef _WIN32
85  if ( _psApi.isValid() ) {
86  _psApi.GetModuleFileNameExA( processHandle(), (HINSTANCE)moduleHandle(), name,sizeof(name) );
87  module = name;
88  }
89 #else
90  const char *path =
91 # if defined(__linux) || defined(__APPLE__)
92  ((Dl_info*)moduleHandle())->dli_fname;
93 # elif __hpux
94  ((HMODULE*)moduleHandle())->dsc.filename;
95 # endif
96  if (::realpath(path, name))
97  module = name;
98 #endif
99  }
100  }
101  return module;
102 }
103 
106  static ModuleType type = UNKNOWN;
107  if ( type == UNKNOWN ) {
108  const std::string& module = moduleNameFull();
109  int loc = module.rfind('.')+1;
110  if ( loc == 0 )
111  type = EXECUTABLE;
112  else if ( module[loc] == 'e' || module[loc] == 'E' )
113  type = EXECUTABLE;
114 #ifdef _WIN32
115  else if ( module[loc] == 'd' || module[loc] == 'D' )
116 #else
117  else if ( module[loc] == 's' && module[loc+1] == 'o' )
118 #endif
119  type = SHAREDLIB;
120  else
121  type = UNKNOWN;
122  }
123  return type;
124 }
125 
128  static long pid = ::getpid();
129 #ifdef _WIN32
130  static HANDLE hP = ::OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE,pid);
131 #else
132  static void* hP = (void*)pid;
133 #endif
134  return hP;
135 }
136 
138  ModuleHandle = handle;
139 }
140 
142  if ( 0 == ModuleHandle ) {
143  if ( processHandle() ) {
144 #ifdef _WIN32
145  static HINSTANCE handle = 0;
146  DWORD cbNeeded;
147  if ( 0 == handle && _psApi.isValid() ) {
148  if ( _psApi.EnumProcessModules( processHandle(), &handle, sizeof(ModuleHandle), &cbNeeded) ) {
149  }
150  }
151  return handle;
152 #elif defined(__linux) || defined(__APPLE__)
153  static Dl_info info;
154  if ( 0 !=
155  ::dladdr(
156 #if __GNUC__ < 4
157  (void*)System::moduleHandle
158 #else
159  FuncPtrCast<void*>(System::moduleHandle)
160 #endif
161  , &info) ) {
162  return &info;
163  }
164 #elif __hpux
165  return 0; // Don't know how to solve this .....
166 #endif
167  }
168  }
169  return ModuleHandle;
170 }
171 
173 #ifdef _WIN32
174  if ( processHandle() ) {
175  static HINSTANCE handle = 0;
176  DWORD cbNeeded;
177  if ( 0 == handle && _psApi.isValid() ) {
178  if ( _psApi.EnumProcessModules( processHandle(), &handle, sizeof(ModuleHandle), &cbNeeded) ) {
179  }
180  }
181  return handle;
182  }
183  return 0;
184 #elif defined(__linux) || defined(__APPLE__)
185  // This does NOT work!
186  static Dl_info infoBuf, *info = &infoBuf;
187  if ( 0 == info ) {
188  void* handle = ::dlopen(0, RTLD_LAZY);
189  //printf("Exe handle:%X\n", handle);
190  if ( 0 != handle ) {
191  void* func = ::dlsym(handle, "main");
192  //printf("Exe:Func handle:%X\n", func);
193  if ( 0 != func ) {
194  if ( 0 != ::dladdr(func, &infoBuf) ) {
195  //std::cout << "All OK" << std::endl;
196  info = &infoBuf;
197  }
198  }
199  }
200  }
201  return info;
202 #elif __hpux
203  // Don't know how to solve this .....
204  return 0;
205 #endif
206 }
207 
208 const std::string& System::exeName() {
209  static std::string module("");
210  if ( module.length() == 0 ) {
211  char name[PATH_MAX] = {"Unknown.module"};
212  name[0] = 0;
213 #ifdef _WIN32
214  if ( _psApi.isValid() && processHandle() ) {
215  _psApi.GetModuleFileNameExA( processHandle(), (HINSTANCE)exeHandle(), name,sizeof(name) );
216  module = name;
217  }
218 #elif defined(__linux) || defined(__APPLE__)
219  char cmd[512];
220  ::sprintf(cmd, "/proc/%d/exe", ::getpid());
221  module = "Unknown";
222  if (::readlink(cmd, name, sizeof(name)) >= 0)
223  module = name;
224 #elif __hpux
225  if (::realpath(((HMODULE*)exeHandle())->dsc.filename, name))
226  module = name;
227 #endif
228  }
229  return module;
230 }
231 
232 const std::vector<std::string> System::linkedModules() {
233  if ( s_linkedModules.size() == 0 ) {
234 #ifdef _WIN32
235  char name[255]; // Maximum file name length on NT 4.0
236  DWORD cbNeeded;
237  HINSTANCE handle[1024];
238  if ( _psApi.isValid() ) {
239  if ( _psApi.EnumProcessModules(processHandle(),handle,sizeof(handle),&cbNeeded) ) {
240  for (size_t i = 0; i < cbNeeded/sizeof(HANDLE); i++ ) {
241  if ( 0 < _psApi.GetModuleFileNameExA( processHandle(), handle[i], name, sizeof(name)) ) {
242  s_linkedModules.push_back(name);
243  }
244  }
245  }
246  }
247 #elif defined(__linux) || defined(__APPLE__)
248  char ff[512], cmd[1024], fname[1024], buf1[64], buf2[64], buf3[64], buf4[64];
249  ::sprintf(ff, "/proc/%d/maps", ::getpid());
250  FILE* maps = ::fopen(ff, "r");
251  while( ::fgets(cmd, sizeof(cmd), maps) ) {
252  int len;
253  sscanf(cmd, "%s %s %s %s %d %s", buf1, buf2, buf3, buf4, &len, fname);
254  if ( len > 0 && strncmp(buf2,"r-xp",strlen("r-xp")) == 0 ) {
255  s_linkedModules.push_back(fname);
256  }
257  }
258  ::fclose(maps);
259 #endif
260  }
261  return s_linkedModules;
262 }
GAUDI_API ModuleType moduleType()
Get type of the module.
Definition: ModuleInfo.cpp:105
GAUDI_API void setModuleHandle(ImageHandle handle)
Attach module handle.
Definition: ModuleInfo.cpp:137
GAUDI_API const std::string & moduleNameFull()
Get the full name of the (executable/DLL) file.
Definition: ModuleInfo.cpp:78
GAUDI_API ProcessHandle processHandle()
Handle to running process.
Definition: ModuleInfo.cpp:127
void * ImageHandle
Definition of an image handle.
Definition: ModuleInfo.h:30
GAUDI_API const std::string & exeName()
Name of the executable file running.
Definition: ModuleInfo.cpp:208
string type
Definition: gaudirun.py:126
ModuleType
Definition: ModuleInfo.h:28
GAUDI_API ImageHandle moduleHandle()
Handle to currently executed module.
Definition: ModuleInfo.cpp:141
GAUDI_API ImageHandle exeHandle()
Handle to the executable file running.
Definition: ModuleInfo.cpp:172
GAUDI_API std::string path(const AIDA::IBaseHistogram *aida)
get the path in THS for AIDA histogram
GAUDI_API const std::vector< std::string > linkedModules()
Vector of names of linked modules.
Definition: ModuleInfo.cpp:232
GAUDI_API const std::string & moduleName()
Get the name of the (executable/DLL) file without file-type.
Definition: ModuleInfo.cpp:55
list i
Definition: ana.py:128