Implement symbol resolution for non-funct ptr std::function objects

This commit is contained in:
Christophe Bedard 2019-07-03 13:53:08 +02:00
parent 64cb3a2952
commit f489192a7c
3 changed files with 50 additions and 33 deletions

View file

@ -18,18 +18,40 @@
#include <stddef.h>
#include <functional>
#define SYMBOL_UNKNOWN "UNKNOWN"
const char * _demangle_symbol(const char * mangled);
const char * _get_symbol_funcptr(void * funcptr);
template<typename T, typename ... U>
void * get_address(std::function<T(U...)> f)
const char * _get_symbol_non_funcptr(std::function<T(U...)> f)
{
typedef T (fnType)(U...);
fnType ** fnPointer = f.template target<fnType *>();
// Might be a lambda
if (fnPointer == nullptr) {
return 0;
}
return reinterpret_cast<void *>(*fnPointer);
#if defined(TRACETOOLS_LTTNG_ENABLED) && !defined(_WIN32)
return _demangle_symbol(f.target_type().name());
#else
(void)f;
return SYMBOL_UNKNOWN;
#endif
}
const char * get_symbol(void * funptr);
template<typename T, typename ... U>
const char * get_symbol(std::function<T(U...)> f)
{
#if defined(TRACETOOLS_LTTNG_ENABLED) && !defined(_WIN32)
typedef T (fnType)(U...);
fnType ** fnPointer = f.template target<fnType *>();
// If we get an actual address
if (fnPointer != nullptr) {
void * funcptr = reinterpret_cast<void *>(*fnPointer);
return _get_symbol_funcptr(funcptr);
}
// Otherwise we have to go through target_type()
return _get_symbol_non_funcptr(f);
#else
(void)f;
return SYMBOL_UNKNOWN;
#endif
}
#endif // TRACETOOLS__UTILS_HPP_