The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
ModuleInfo.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
12#include <GaudiKernel/System.h>
13#include <cstdlib>
14#include <cstring>
15
16#include <cstdio>
17#include <dlfcn.h>
18#include <libgen.h>
19#include <string.h>
20#include <sys/param.h>
21#include <sys/times.h>
22#include <unistd.h>
23
24static System::ImageHandle ModuleHandle = nullptr;
25static std::vector<std::string> s_linkedModules;
26
28const std::string& System::moduleName() {
29 static std::string module( "" );
30 if ( module == "" ) {
31 if ( processHandle() && moduleHandle() ) {
32#if defined( __linux ) || defined( __APPLE__ )
33 std::string mod = ::basename( (char*)( (Dl_info*)moduleHandle() )->dli_fname );
34#elif __hpux
35 std::string mod = ::basename( ( (HMODULE*)moduleHandle() )->dsc.filename );
36#endif
37 module = mod.substr( 0, mod.rfind( '.' ) );
38 }
39 }
40 return module;
41}
42
44const std::string& System::moduleNameFull() {
45 static std::string module( "" );
46 if ( module == "" ) {
47 if ( processHandle() && moduleHandle() ) {
48 char name[PATH_MAX] = { "Unknown.module" };
49 name[0] = 0;
50#if defined( __linux ) || defined( __APPLE__ )
51 const char* path = ( (Dl_info*)moduleHandle() )->dli_fname;
52#elif __hpux
53 const char* path = ( (HMODULE*)moduleHandle() )->dsc.filename;
54#endif
55 if ( ::realpath( path, name ) ) module = name;
56 }
57 }
58 return module;
59}
60
63 static ModuleType type = UNKNOWN;
64 if ( type == UNKNOWN ) {
65 const std::string& module = moduleNameFull();
66 size_t loc = module.rfind( '.' ) + 1;
67 if ( loc == 0 )
68 type = EXECUTABLE;
69 else if ( module[loc] == 'e' || module[loc] == 'E' )
70 type = EXECUTABLE;
71 else if ( module[loc] == 's' && module[loc + 1] == 'o' )
72 type = SHAREDLIB;
73 else
74 type = UNKNOWN;
75 }
76 return type;
77}
78
81 static long pid = ::getpid();
82 static void* hP = (void*)pid;
83 return hP;
84}
85
86void System::setModuleHandle( System::ImageHandle handle ) { ModuleHandle = handle; }
87
89 if ( !ModuleHandle ) {
90 if ( processHandle() ) {
91#if defined( __linux ) || defined( __APPLE__ )
92 static Dl_info info;
93 if ( ::dladdr( reinterpret_cast<void*>( System::moduleHandle ), &info ) ) return &info;
94#elif __hpux
95 return 0; // Don't know how to solve this .....
96#endif
97 }
98 }
99 return ModuleHandle;
100}
101
103#if defined( __linux ) || defined( __APPLE__ )
104 // This does NOT work!
105 static Dl_info infoBuf, *info = &infoBuf;
106 if ( !info ) {
107 void* handle = ::dlopen( nullptr, RTLD_LAZY );
108 // printf("Exe handle:%X\n", handle);
109 if ( handle ) {
110 void* func = ::dlsym( handle, "main" );
111 // printf("Exe:Func handle:%X\n", func);
112 if ( func ) {
113 if ( 0 != ::dladdr( func, &infoBuf ) ) {
114 // std::cout << "All OK" << std::endl;
115 info = &infoBuf;
116 }
117 }
118 }
119 }
120 return info;
121#elif __hpux
122 // Don't know how to solve this .....
123 return 0;
124#endif
125 return 0;
126}
127
128const std::string& System::exeName() {
129 static std::string module( "" );
130 if ( module.length() == 0 ) {
131 char name[PATH_MAX] = { "Unknown.module" };
132 name[0] = 0;
133#if defined( __linux ) || defined( __APPLE__ )
134 char cmd[512];
135 ::sprintf( cmd, "/proc/%d/exe", ::getpid() );
136 module = "Unknown";
137 if ( ::readlink( cmd, name, sizeof( name ) ) >= 0 ) module = name;
138#elif __hpux
139 if ( ::realpath( ( (HMODULE*)exeHandle() )->dsc.filename, name ) ) module = name;
140#endif
141 }
142 return module;
143}
144
145const std::vector<std::string> System::linkedModules() {
146 if ( s_linkedModules.size() == 0 ) {
147#if defined( __linux ) || defined( __APPLE__ )
148 char ff[512], cmd[1024], fname[1024], buf1[64], buf2[64], buf3[64], buf4[64];
149 ::sprintf( ff, "/proc/%d/maps", ::getpid() );
150 FILE* maps = ::fopen( ff, "r" );
151 while ( ::fgets( cmd, sizeof( cmd ), maps ) ) {
152 int len;
153 sscanf( cmd, "%s %s %s %s %d %s", buf1, buf2, buf3, buf4, &len, fname );
154 if ( len > 0 && strncmp( buf2, "r-xp", strlen( "r-xp" ) ) == 0 ) { s_linkedModules.push_back( fname ); }
155 }
156 ::fclose( maps );
157#endif
158 }
159 return s_linkedModules;
160}
@ EXECUTABLE
Definition ModuleInfo.h:23
@ SHAREDLIB
Definition ModuleInfo.h:23
@ UNKNOWN
Definition ModuleInfo.h:23
GAUDI_API ProcessHandle processHandle()
Handle to running process.
void * ImageHandle
Definition of an image handle.
Definition ModuleInfo.h:25
GAUDI_API ImageHandle exeHandle()
Handle to the executable file running.
GAUDI_API ModuleType moduleType()
Get type of the module.
GAUDI_API ImageHandle moduleHandle()
Handle to currently executed module.
GAUDI_API const std::string & moduleNameFull()
Get the full name of the (executable/DLL) file.
GAUDI_API const std::string & moduleName()
Get the name of the (executable/DLL) file without file-type.
GAUDI_API const std::vector< std::string > linkedModules()
Vector of names of linked modules.
GAUDI_API void setModuleHandle(ImageHandle handle)
Attach module handle.
GAUDI_API const std::string & exeName()
Name of the executable file running.