Skip to content

Commit

Permalink
Cleaning up address resolution and providing a backtrace_symbols back…
Browse files Browse the repository at this point in the history
…up implementation when BFD not used
  • Loading branch information
khuck committed Aug 4, 2022
1 parent 64a56c9 commit 82be301
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 50 deletions.
37 changes: 25 additions & 12 deletions src/apex/address_resolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ namespace apex {
if (it2 == ar->my_hash_table.end()) {
// ...no - so go get it!
node = new address_resolution::my_hash_node();
node->info.filename = nullptr;
node->info.funcname = nullptr;
node->info.lineno = 0;
node->info.demangled = nullptr;
node->location = nullptr;
#if defined(__APPLE__)
#if defined(APEX_HAVE_CORESYMBOLICATION)
static CSSymbolicatorRef symbolicator = CSSymbolicatorCreateWithPid(getpid());
Expand All @@ -70,22 +75,31 @@ namespace apex {
node->info.probeAddr = ip;
node->info.filename = strdup(info.dli_fname);
node->info.funcname = strdup(info.dli_sname);
node->info.lineno = 0; // Apple doesn't give us line numbers.
// Apple doesn't give us line numbers.
}
#endif
#else
#ifdef APEX_HAVE_BFD
Apex_bfd_resolveBfdInfo(ar->my_bfd_unit_handle, ip, node->info);
#else
const void * buffer[1] = {ip};
char ** names = backtrace_symbols(buffer, 1);
void * const buffer[1] = {(void *)ip};
char ** names = backtrace_symbols((void * const *)buffer, 1);
/* Split the backtrace strings into tokens, and get the 4th one */
std::vector<std::string> result;
std::istringstream iss(names[0]);
for (std::string s; iss >> s; ) {
result.push_back(s);
}
node->info.probeAddr = ip;
node->info.filename = strdup("??");
node->info.funcname = strdup(names[0]);
node->info.lineno = 0;
node->info.demangled = false;
node->info.filename = strdup("?");
node->info.funcname = strdup(result[3].c_str());
#endif
#endif
if (node->info.filename == nullptr) {
stringstream ss;
ss << "UNRESOLVED ADDR 0x" << hex << ip;
node->info.funcname = strdup(ss.str().c_str());
}

if (node->info.demangled) {
location << node->info.demangled ;
Expand Down Expand Up @@ -121,14 +135,13 @@ namespace apex {
} else {
node = it->second;
}
if (node->info.demangled && (strlen(node->info.demangled) == 0)) {
node->info.demangled = nullptr;
}
if (withFileInfo) {
return node->location;
} else {
if (node->info.funcname != nullptr) {
return new string(node->info.funcname);
} else {
return new string("<unknown>");
}
return new string(node->info.funcname);
}
}
}
2 changes: 1 addition & 1 deletion src/apex/apex_bfd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ char const * Apex_bfd_internal_tryDemangle(bfd * bfdImage,
#else
APEX_UNUSED(bfdImage);
#endif
if (demangled) return demangled;
if (demangled && strlen(demangled) > 0) return demangled;
return funcname;
}

Expand Down
37 changes: 0 additions & 37 deletions src/apex/thread_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@
# include <sys/sysctl.h>
#endif

#ifdef APEX_HAVE_BFD
#include "address_resolution.hpp"
#endif
// another method, just for the special Apple folks.
#if defined(__APPLE__)
#include <dlfcn.h>
#endif

using namespace std;

Expand Down Expand Up @@ -226,7 +220,6 @@ string thread_instance::map_addr_to_name(apex_function_address function_address)
return (*it).second;
} // else...
}
#ifdef APEX_HAVE_BFD
// resolve the address
string * name = lookup_address(function_address, false);
{
Expand All @@ -236,36 +229,6 @@ string thread_instance::map_addr_to_name(apex_function_address function_address)
}
read_lock_type l(common()._function_map_mutex);
return _function_map[function_address];
#else
#if defined(__APPLE__)
// resolve the address
Dl_info info;
int rc = dladdr((const void *)function_address, &info);
if (rc != 0) {
string name(info.dli_sname);
{
write_lock_type l(common()._function_map_mutex);
_function_map[function_address] = name;
}
read_lock_type l(common()._function_map_mutex);
return _function_map[function_address];
}
#endif
stringstream ss;
const char * progname = program_path();
if (progname == nullptr) {
ss << "UNRESOLVED ADDR 0x" << hex << function_address;
} else {
ss << "UNRESOLVED " << string(progname) << " ADDR "
<< hex << function_address;
}
string name = string(ss.str());
{
write_lock_type l(common()._function_map_mutex);
_function_map[function_address] = name;
}
return name;
#endif
}

void thread_instance::set_current_profiler(profiler * the_profiler) {
Expand Down

0 comments on commit 82be301

Please sign in to comment.