Skip to content

Commit

Permalink
Add System.dladdr for easier debugging of things that should not go w…
Browse files Browse the repository at this point in the history
…rong

- 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
  • Loading branch information
sjoelund committed Oct 29, 2014
1 parent 56fd672 commit eb68f0c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
25 changes: 25 additions & 0 deletions Compiler/Util/System.mo
Expand Up @@ -1102,5 +1102,30 @@ public function covertTextFileToCLiteral
external "C" success=SystemImpl__covertTextFileToCLiteral(textFile,outFile);
end covertTextFileToCLiteral;

public function dladdr<T>
input T symbol "Function pointer";
output String info;
output String file;
output String name;
algorithm
(file,name) := _dladdr(symbol);
info := file + ": " + name;
protected

function _dladdr<T>
input T symbol "Function pointer";
output String file;
output String name;
external "C" SystemImpl__dladdr(symbol, file, name) annotation(Library = {"omcruntime"},Documentation(info="<html>
<p>Like <a href=\"http://linux.die.net/man/3/dladdr\">dladdr(3)</a>.</p>
<p>Only works on Linux. Other platforms return dummy strings.</p>.
</html>"));
end _dladdr;
annotation(Documentation(info="<html>
<p>Like <a href=\"http://linux.die.net/man/3/dladdr\">dladdr(3)</a>.</p>
<p>Only works on Linux. Other platforms return dummy strings.</p>.
</html>"));
end dladdr;

annotation(__OpenModelica_Interface="util");
end System;
4 changes: 4 additions & 0 deletions Compiler/runtime/System_omc.c
Expand Up @@ -37,6 +37,10 @@ extern "C"
{
#endif

#if defined(__linux__) && !defined(_GNU_SOURCE)
#define _GNU_SOURCE 1
#endif

#include <ctype.h> /* for toupper */
#include <limits.h>
#include <stdlib.h>
Expand Down
20 changes: 19 additions & 1 deletion Compiler/runtime/systemimpl.c
Expand Up @@ -120,9 +120,10 @@ char *realpath(const char *path, char resolved_path[PATH_MAX]);
#include <sys/unistd.h>
#include <sys/wait.h> /* only available in Linux, not windows */
#include <unistd.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <spawn.h>

#include <dlfcn.h>
extern char **environ;

#define HAVE_SCANDIR
Expand Down Expand Up @@ -2671,6 +2672,23 @@ int SystemImpl__covertTextFileToCLiteral(const char *textFile, const char *outFi
return result;
}

void SystemImpl__dladdr(void *symbol, const char **file, const char **name)
{
#if defined(__MINGW32__) || defined(_MSC_VER)
*file = "dladdr failed";
*name = "not available on Windows";
#else
Dl_info info;
if (0 == dladdr((MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(symbol), 1))), &info)) {
*file = "dladdr failed";
*name = "";
} else {
*file = GC_strdup(info.dli_fname);
*name = GC_strdup(info.dli_sname);
}
#endif
}

#ifdef __cplusplus
}
#endif

0 comments on commit eb68f0c

Please sign in to comment.