Skip to content

Support using dladdr1 for safe trace resolution #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/binary/object.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#ifndef OBJECT_HPP
#define OBJECT_HPP

#include <vector>
#include <cstdint>
#include <string>
#include <vector>

namespace cpptrace {

Expand All @@ -17,6 +18,8 @@ namespace detail {
std::vector<object_frame> get_frames_object_info(const std::vector<frame_ptr>& addresses);

object_frame resolve_safe_object_frame(const safe_object_frame& frame);

std::string resolve_l_name(const char* l_name);
}
}

Expand Down
31 changes: 30 additions & 1 deletion src/binary/safe_dl.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "binary/safe_dl.hpp"
#include "binary/module_base.hpp"
#include "binary/object.hpp"

#include "utils/common.hpp"
#include "utils/utils.hpp"
Expand All @@ -11,7 +13,7 @@
#include <cstring>
#include <iostream>

#ifdef CPPTRACE_HAS_DL_FIND_OBJECT
#if defined(CPPTRACE_HAS_DL_FIND_OBJECT) || defined(CPPTRACE_HAS_DLADDR1)
#if IS_LINUX || IS_APPLE
#include <unistd.h>
#include <dlfcn.h>
Expand All @@ -20,6 +22,7 @@

namespace cpptrace {
namespace detail {
#ifdef CPPTRACE_HAS_DL_FIND_OBJECT
void get_safe_object_frame(frame_ptr address, safe_object_frame* out) {
out->raw_address = address;
dl_find_object result;
Expand Down Expand Up @@ -53,6 +56,32 @@ namespace detail {
// may return the object that defines the function descriptor (and not the object that contains the code
// implementing the function), or fail to find any object at all.
}
#else
void get_safe_object_frame(frame_ptr address, safe_object_frame* out) {
out->raw_address = address;
Dl_info info;
link_map* link_map_info;

if(
// thread safe
dladdr1(reinterpret_cast<void*>(address), &info, reinterpret_cast<void**>(&link_map_info), RTLD_DL_LINKMAP)
) {
std::string lname = resolve_l_name(link_map_info->l_name);
std::strcpy(out->object_path, lname.data());
auto base = get_module_image_base(lname);
if(base.has_value()) {
out->address_relative_to_object_start = address
- reinterpret_cast<std::uintptr_t>(info.dli_fbase)
+ base.unwrap_value();
} else {
base.drop_error();
}
} else {
out->address_relative_to_object_start = 0;
out->object_path[0] = 0;
}
}
#endif
}
}
#else
Expand Down
Loading