Skip to content

Commit eb68f0c

Browse files
committed
Add System.dladdr for easier debugging of things that should not go wrong
- dladdr returns the C name of a symbol in the file (typically a function pointer) git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@23040 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 56fd672 commit eb68f0c

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

Compiler/Util/System.mo

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,5 +1102,30 @@ public function covertTextFileToCLiteral
11021102
external "C" success=SystemImpl__covertTextFileToCLiteral(textFile,outFile);
11031103
end covertTextFileToCLiteral;
11041104

1105+
public function dladdr<T>
1106+
input T symbol "Function pointer";
1107+
output String info;
1108+
output String file;
1109+
output String name;
1110+
algorithm
1111+
(file,name) := _dladdr(symbol);
1112+
info := file + ": " + name;
1113+
protected
1114+
1115+
function _dladdr<T>
1116+
input T symbol "Function pointer";
1117+
output String file;
1118+
output String name;
1119+
external "C" SystemImpl__dladdr(symbol, file, name) annotation(Library = {"omcruntime"},Documentation(info="<html>
1120+
<p>Like <a href=\"http://linux.die.net/man/3/dladdr\">dladdr(3)</a>.</p>
1121+
<p>Only works on Linux. Other platforms return dummy strings.</p>.
1122+
</html>"));
1123+
end _dladdr;
1124+
annotation(Documentation(info="<html>
1125+
<p>Like <a href=\"http://linux.die.net/man/3/dladdr\">dladdr(3)</a>.</p>
1126+
<p>Only works on Linux. Other platforms return dummy strings.</p>.
1127+
</html>"));
1128+
end dladdr;
1129+
11051130
annotation(__OpenModelica_Interface="util");
11061131
end System;

Compiler/runtime/System_omc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ extern "C"
3737
{
3838
#endif
3939

40+
#if defined(__linux__) && !defined(_GNU_SOURCE)
41+
#define _GNU_SOURCE 1
42+
#endif
43+
4044
#include <ctype.h> /* for toupper */
4145
#include <limits.h>
4246
#include <stdlib.h>

Compiler/runtime/systemimpl.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,10 @@ char *realpath(const char *path, char resolved_path[PATH_MAX]);
120120
#include <sys/unistd.h>
121121
#include <sys/wait.h> /* only available in Linux, not windows */
122122
#include <unistd.h>
123-
#include <dlfcn.h>
124123
#include <stdlib.h>
125124
#include <spawn.h>
125+
126+
#include <dlfcn.h>
126127
extern char **environ;
127128

128129
#define HAVE_SCANDIR
@@ -2671,6 +2672,23 @@ int SystemImpl__covertTextFileToCLiteral(const char *textFile, const char *outFi
26712672
return result;
26722673
}
26732674

2675+
void SystemImpl__dladdr(void *symbol, const char **file, const char **name)
2676+
{
2677+
#if defined(__MINGW32__) || defined(_MSC_VER)
2678+
*file = "dladdr failed";
2679+
*name = "not available on Windows";
2680+
#else
2681+
Dl_info info;
2682+
if (0 == dladdr((MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(symbol), 1))), &info)) {
2683+
*file = "dladdr failed";
2684+
*name = "";
2685+
} else {
2686+
*file = GC_strdup(info.dli_fname);
2687+
*name = GC_strdup(info.dli_sname);
2688+
}
2689+
#endif
2690+
}
2691+
26742692
#ifdef __cplusplus
26752693
}
26762694
#endif

0 commit comments

Comments
 (0)