Skip to content

Commit

Permalink
revue demangle usage
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Oct 25, 2024
1 parent ae49643 commit b255904
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 51 deletions.
17 changes: 4 additions & 13 deletions src/xtd.core.native.linux/src/xtd/native/linux/stack_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,7 @@ using namespace abi;
using namespace std;
using namespace xtd::native;

namespace {
string demangle(const string& name) {
auto status = 0;
auto demangled_name = __cxa_demangle(name.c_str(), nullptr, 0, &status);
auto result = status == 0 && demangled_name ? demangled_name : name;
free(demangled_name);
return result;
}
}

string __xtd_abi_demangle(const string& name);

size_t stack_trace::get_native_offset() {
return 3;
Expand All @@ -39,9 +30,9 @@ stack_trace::frame_collection stack_trace::get_frames(size_t skip_frames, bool n
for (auto index = skip_frames + get_native_offset(); index < nb_frames; ++index) {
auto dl_info = Dl_info {};
if (!dladdr(traces[index], &dl_info) || !dl_info.dli_sname) break;
if (!need_file_info) frames.push_back(make_tuple("", 0, 0, demangle(dl_info.dli_sname), 0));
else frames.push_back(make_tuple(dl_info.dli_fname, 0, 0, demangle(dl_info.dli_sname), reinterpret_cast<size_t>(dl_info.dli_saddr) - reinterpret_cast<size_t>(dl_info.dli_fbase)));
if (demangle(dl_info.dli_sname) == string("main")) break;
if (!need_file_info) frames.push_back(make_tuple("", 0, 0, __xtd_abi_demangle(dl_info.dli_sname), 0));
else frames.push_back(make_tuple(dl_info.dli_fname, 0, 0, __xtd_abi_demangle(dl_info.dli_sname), reinterpret_cast<size_t>(dl_info.dli_saddr) - reinterpret_cast<size_t>(dl_info.dli_fbase)));
if (__xtd_abi_demangle(dl_info.dli_sname) == string("main")) break;
}
return frames;
}
Expand Down
6 changes: 5 additions & 1 deletion src/xtd.core.native.linux/src/xtd/native/linux/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ using namespace abi;
using namespace std;
using namespace xtd::native;

string types::demangle(const string& name) {
string __xtd_abi_demangle(const string& name) {
auto status = 0;
auto demangled_name = __cxa_demangle(name.c_str(), nullptr, 0, &status);
auto result = status == 0 && demangled_name ? demangled_name : name;
free(demangled_name);
return result;
}

string types::demangle(const string& name) {
return __xtd_abi_demangle(name);
}

intmax_t types::invalid_handle() noexcept {
return 0;
}
12 changes: 3 additions & 9 deletions src/xtd.core.native.macos/src/xtd/native/macos/stack_trace.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,18 @@
using namespace std;
using namespace xtd::native;

string __xtd_abi_demangle(const string& name);

namespace {
using address = void*;
using address_collection = vector<address>;
using frame = tuple<string, size_t, size_t, string, size_t>;
using frame_collection = vector<frame>;

string demangle(const string& name) {
auto status = 0;
auto demangled_name = __cxa_demangle(name.c_str(), nullptr, 0, &status);
auto result = xtd::native::macos::strings::replace(status == 0 && demangled_name ? demangled_name : name, "std::__1::", "std::");
free(demangled_name);
return result;
}

tuple<string, size_t> get_method_and_offset_from_address(address address) {
auto dl_info = Dl_info {};
if (!dladdr(address, &dl_info)) return make_tuple("", numeric_limits<size_t>::max());
return make_tuple(demangle(dl_info.dli_sname ? dl_info.dli_sname : ""), reinterpret_cast<size_t>(address) - reinterpret_cast<size_t>(dl_info.dli_saddr));
return make_tuple(__xtd_abi_demangle(dl_info.dli_sname ? dl_info.dli_sname : ""), reinterpret_cast<size_t>(address) - reinterpret_cast<size_t>(dl_info.dli_saddr));
}

frame_collection get_frames_without_file_info_from_addresses(const address_collection& addresses) {
Expand Down
8 changes: 6 additions & 2 deletions src/xtd.core.native.macos/src/xtd/native/macos/types.mm
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
#include <cxxabi.h>
#define __XTD_CORE_NATIVE_LIBRARY__
#include <xtd/native/types>
#include "../../../../include/xtd/native/macos/semaphore.h"
#include "../../../../include/xtd/native/macos/strings.h"
#undef __XTD_CORE_NATIVE_LIBRARY__
#include <cxxabi.h>

using namespace abi;
using namespace std;
using namespace xtd::native;

string types::demangle(const string& name) {
string __xtd_abi_demangle(const string& name) {
auto status = 0;
auto demangled_name = __cxa_demangle(name.c_str(), nullptr, 0, &status);
auto result = xtd::native::macos::strings::replace(status == 0 && demangled_name ? demangled_name : name, "std::__1::", "std::");
free(demangled_name);
return result;
}

string types::demangle(const string& name) {
return __xtd_abi_demangle(name);
}

intmax_t types::invalid_handle() noexcept {
return reinterpret_cast<intmax_t>(SEM_FAILED);
}
16 changes: 4 additions & 12 deletions src/xtd.core.native.posix/src/xtd/native/posix/stack_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@ using namespace abi;
using namespace std;
using namespace xtd::native;

namespace {
string demangle(const string& name) {
auto status = 0;
auto demangled_name = __cxa_demangle(name.c_str(), nullptr, 0, &status);
auto result = status == 0 && demangled_name ? demangled_name : name;
free(demangled_name);
return result;
}
}
string __xtd_abi_demangle(const string& name);

size_t stack_trace::get_native_offset() {
return 3;
Expand All @@ -33,9 +25,9 @@ stack_trace::frame_collection stack_trace::get_frames(size_t skip_frames, bool n
for (auto index = skip_frames + get_native_offset(); index < nb_frames; ++index) {
auto dl_info = Dl_info {};
if (!dladdr(traces[index], &dl_info) || !dl_info.dli_sname) break;
if (!need_file_info) frames.push_back(make_tuple("", 0, 0, demangle(dl_info.dli_sname), 0));
else frames.push_back(make_tuple(dl_info.dli_fname, 0, 0, demangle(dl_info.dli_sname), reinterpret_cast<size_t>(dl_info.dli_saddr) - reinterpret_cast<size_t>(dl_info.dli_fbase)));
if (demangle(dl_info.dli_sname) == string("main")) break;
if (!need_file_info) frames.push_back(make_tuple("", 0, 0, __xtd_abi_demangle(dl_info.dli_sname), 0));
else frames.push_back(make_tuple(dl_info.dli_fname, 0, 0, __xtd_abi_demangle(dl_info.dli_sname), reinterpret_cast<size_t>(dl_info.dli_saddr) - reinterpret_cast<size_t>(dl_info.dli_fbase)));
if (__xtd_abi_demangle(dl_info.dli_sname) == string("main")) break;
}
return frames;
}
6 changes: 5 additions & 1 deletion src/xtd.core.native.posix/src/xtd/native/posix/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ using namespace abi;
using namespace std;
using namespace xtd::native;

string types::demangle(const string& name) {
string __xtd_abi_demangle(const string& name) {
auto status = 0;
auto demangled_name = __cxa_demangle(name.c_str(), nullptr, 0, &status);
auto result = status == 0 && demangled_name ? demangled_name : name;
free(demangled_name);
return result;
}

string types::demangle(const string& name) {
return __xtd_abi_demangle(name);
}

intmax_t types::invalid_handle() noexcept {
return 0;
}
16 changes: 4 additions & 12 deletions src/xtd.core.native.unix/src/xtd/native/unix/stack_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,7 @@
using namespace std;
using namespace xtd::native;

namespace {
string demangle(const string& name) {
auto status = 0;
auto demangled_name = __cxa_demangle(name.c_str(), nullptr, 0, &status);
auto result = status == 0 && demangled_name ? demangled_name : name;
free(demangled_name);
return result;
}
}
string __xtd_abi_demangle(const string& name);

size_t stack_trace::get_native_offset() {
return 3;
Expand All @@ -32,9 +24,9 @@ stack_trace::frame_collection stack_trace::get_frames(size_t skip_frames, bool n
for (auto index = skip_frames + get_native_offset(); index < nb_frames; ++index) {
auto dl_info = Dl_info {};
if (!dladdr(traces[index], &dl_info) || !dl_info.dli_sname) break;
if (!need_file_info) frames.push_back(make_tuple("", 0, 0, demangle(dl_info.dli_sname), 0));
else frames.push_back(make_tuple(dl_info.dli_fname, 0, 0, demangle(dl_info.dli_sname), reinterpret_cast<size_t>(dl_info.dli_saddr) - reinterpret_cast<size_t>(dl_info.dli_fbase)));
if (demangle(dl_info.dli_sname) == string("main")) break;
if (!need_file_info) frames.push_back(make_tuple("", 0, 0, __xtd_abi_demangle(dl_info.dli_sname), 0));
else frames.push_back(make_tuple(dl_info.dli_fname, 0, 0, __xtd_abi_demangle(dl_info.dli_sname), reinterpret_cast<size_t>(dl_info.dli_saddr) - reinterpret_cast<size_t>(dl_info.dli_fbase)));
if (__xtd_abi_demangle(dl_info.dli_sname) == string("main")) break;
}
return frames;
}
6 changes: 5 additions & 1 deletion src/xtd.core.native.unix/src/xtd/native/unix/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ using namespace abi;
using namespace std;
using namespace xtd::native;

string types::demangle(const string& name) {
string __xtd_abi_demangle(const string& name) {
auto status = 0;
auto demangled_name = __cxa_demangle(name.c_str(), nullptr, 0, &status);
auto result = status == 0 && demangled_name ? demangled_name : name;
free(demangled_name);
return result;
}

string types::demangle(const string& name) {
return __xtd_abi_demangle(name);
}

intmax_t types::invalid_handle() noexcept {
return 0;
}

0 comments on commit b255904

Please sign in to comment.