The Gaudi Framework
v41r0 (bdbe7744)
Toggle main menu visibility
Loading...
Searching...
No Matches
ModuleInfo.cpp
Go to the documentation of this file.
1
/***********************************************************************************\
2
* (c) Copyright 1998-2026 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
\***********************************************************************************/
11
#include <
GaudiKernel/ModuleInfo.h
>
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
24
static
System::ImageHandle
ModuleHandle =
nullptr
;
25
static
std::vector<std::string> s_linkedModules;
26
28
const
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
44
const
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
62
System::ModuleType
System::moduleType
() {
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
80
void
*
System::processHandle
() {
81
static
long
pid = ::getpid();
82
static
void
* hP = (
void
*)pid;
83
return
hP;
84
}
85
86
void
System::setModuleHandle
(
System::ImageHandle
handle ) { ModuleHandle = handle; }
87
88
System::ImageHandle
System::moduleHandle
() {
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
102
System::ImageHandle
System::exeHandle
() {
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
128
const
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
::snprintf( cmd,
sizeof
( 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
145
const
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
::snprintf( ff,
sizeof
( 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
}
ModuleInfo.h
System.h
System::ModuleType
ModuleType
Definition
ModuleInfo.h:23
System::EXECUTABLE
@ EXECUTABLE
Definition
ModuleInfo.h:23
System::SHAREDLIB
@ SHAREDLIB
Definition
ModuleInfo.h:23
System::UNKNOWN
@ UNKNOWN
Definition
ModuleInfo.h:23
System::processHandle
GAUDI_API ProcessHandle processHandle()
Handle to running process.
Definition
ModuleInfo.cpp:80
System::ImageHandle
void * ImageHandle
Definition of an image handle.
Definition
ModuleInfo.h:25
System::exeHandle
GAUDI_API ImageHandle exeHandle()
Handle to the executable file running.
Definition
ModuleInfo.cpp:102
System::moduleType
GAUDI_API ModuleType moduleType()
Get type of the module.
Definition
ModuleInfo.cpp:62
System::moduleHandle
GAUDI_API ImageHandle moduleHandle()
Handle to currently executed module.
Definition
ModuleInfo.cpp:88
System::moduleNameFull
GAUDI_API const std::string & moduleNameFull()
Get the full name of the (executable/DLL) file.
Definition
ModuleInfo.cpp:44
System::moduleName
GAUDI_API const std::string & moduleName()
Get the name of the (executable/DLL) file without file-type.
Definition
ModuleInfo.cpp:28
System::linkedModules
GAUDI_API const std::vector< std::string > linkedModules()
Vector of names of linked modules.
Definition
ModuleInfo.cpp:145
System::setModuleHandle
GAUDI_API void setModuleHandle(ImageHandle handle)
Attach module handle.
Definition
ModuleInfo.cpp:86
System::exeName
GAUDI_API const std::string & exeName()
Name of the executable file running.
Definition
ModuleInfo.cpp:128
GaudiKernel
src
Lib
ModuleInfo.cpp
Generated on
for The Gaudi Framework by
1.17.0