From b255904f0454888d8e12ddf57b9c19f24acc386f Mon Sep 17 00:00:00 2001 From: Gammasoft Date: Fri, 25 Oct 2024 21:49:31 +0200 Subject: [PATCH] revue demangle usage --- .../src/xtd/native/linux/stack_trace.cpp | 17 ++++------------- .../src/xtd/native/linux/types.cpp | 6 +++++- .../src/xtd/native/macos/stack_trace.mm | 12 +++--------- .../src/xtd/native/macos/types.mm | 8 ++++++-- .../src/xtd/native/posix/stack_trace.cpp | 16 ++++------------ .../src/xtd/native/posix/types.cpp | 6 +++++- .../src/xtd/native/unix/stack_trace.cpp | 16 ++++------------ .../src/xtd/native/unix/types.cpp | 6 +++++- 8 files changed, 36 insertions(+), 51 deletions(-) diff --git a/src/xtd.core.native.linux/src/xtd/native/linux/stack_trace.cpp b/src/xtd.core.native.linux/src/xtd/native/linux/stack_trace.cpp index fb1813dc691..9e07a089e68 100644 --- a/src/xtd.core.native.linux/src/xtd/native/linux/stack_trace.cpp +++ b/src/xtd.core.native.linux/src/xtd/native/linux/stack_trace.cpp @@ -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; @@ -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(dl_info.dli_saddr) - reinterpret_cast(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(dl_info.dli_saddr) - reinterpret_cast(dl_info.dli_fbase))); + if (__xtd_abi_demangle(dl_info.dli_sname) == string("main")) break; } return frames; } diff --git a/src/xtd.core.native.linux/src/xtd/native/linux/types.cpp b/src/xtd.core.native.linux/src/xtd/native/linux/types.cpp index effc5c21da6..d697c4e10da 100644 --- a/src/xtd.core.native.linux/src/xtd/native/linux/types.cpp +++ b/src/xtd.core.native.linux/src/xtd/native/linux/types.cpp @@ -7,7 +7,7 @@ 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; @@ -15,6 +15,10 @@ string types::demangle(const string& name) { return result; } +string types::demangle(const string& name) { + return __xtd_abi_demangle(name); +} + intmax_t types::invalid_handle() noexcept { return 0; } diff --git a/src/xtd.core.native.macos/src/xtd/native/macos/stack_trace.mm b/src/xtd.core.native.macos/src/xtd/native/macos/stack_trace.mm index 34cda8fcf8a..07797698243 100644 --- a/src/xtd.core.native.macos/src/xtd/native/macos/stack_trace.mm +++ b/src/xtd.core.native.macos/src/xtd/native/macos/stack_trace.mm @@ -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
; using frame = tuple; using frame_collection = vector; - 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 get_method_and_offset_from_address(address address) { auto dl_info = Dl_info {}; if (!dladdr(address, &dl_info)) return make_tuple("", numeric_limits::max()); - return make_tuple(demangle(dl_info.dli_sname ? dl_info.dli_sname : ""), reinterpret_cast(address) - reinterpret_cast(dl_info.dli_saddr)); + return make_tuple(__xtd_abi_demangle(dl_info.dli_sname ? dl_info.dli_sname : ""), reinterpret_cast(address) - reinterpret_cast(dl_info.dli_saddr)); } frame_collection get_frames_without_file_info_from_addresses(const address_collection& addresses) { diff --git a/src/xtd.core.native.macos/src/xtd/native/macos/types.mm b/src/xtd.core.native.macos/src/xtd/native/macos/types.mm index 019821ec3fa..f5c9cdb12fa 100644 --- a/src/xtd.core.native.macos/src/xtd/native/macos/types.mm +++ b/src/xtd.core.native.macos/src/xtd/native/macos/types.mm @@ -1,15 +1,15 @@ -#include #define __XTD_CORE_NATIVE_LIBRARY__ #include #include "../../../../include/xtd/native/macos/semaphore.h" #include "../../../../include/xtd/native/macos/strings.h" #undef __XTD_CORE_NATIVE_LIBRARY__ +#include 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::"); @@ -17,6 +17,10 @@ return result; } +string types::demangle(const string& name) { + return __xtd_abi_demangle(name); +} + intmax_t types::invalid_handle() noexcept { return reinterpret_cast(SEM_FAILED); } diff --git a/src/xtd.core.native.posix/src/xtd/native/posix/stack_trace.cpp b/src/xtd.core.native.posix/src/xtd/native/posix/stack_trace.cpp index f660f73a69b..733a0f2a8db 100644 --- a/src/xtd.core.native.posix/src/xtd/native/posix/stack_trace.cpp +++ b/src/xtd.core.native.posix/src/xtd/native/posix/stack_trace.cpp @@ -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; @@ -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(dl_info.dli_saddr) - reinterpret_cast(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(dl_info.dli_saddr) - reinterpret_cast(dl_info.dli_fbase))); + if (__xtd_abi_demangle(dl_info.dli_sname) == string("main")) break; } return frames; } diff --git a/src/xtd.core.native.posix/src/xtd/native/posix/types.cpp b/src/xtd.core.native.posix/src/xtd/native/posix/types.cpp index effc5c21da6..d697c4e10da 100644 --- a/src/xtd.core.native.posix/src/xtd/native/posix/types.cpp +++ b/src/xtd.core.native.posix/src/xtd/native/posix/types.cpp @@ -7,7 +7,7 @@ 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; @@ -15,6 +15,10 @@ string types::demangle(const string& name) { return result; } +string types::demangle(const string& name) { + return __xtd_abi_demangle(name); +} + intmax_t types::invalid_handle() noexcept { return 0; } diff --git a/src/xtd.core.native.unix/src/xtd/native/unix/stack_trace.cpp b/src/xtd.core.native.unix/src/xtd/native/unix/stack_trace.cpp index 5698e9e4bf0..3e2ffdc4ca7 100644 --- a/src/xtd.core.native.unix/src/xtd/native/unix/stack_trace.cpp +++ b/src/xtd.core.native.unix/src/xtd/native/unix/stack_trace.cpp @@ -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; @@ -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(dl_info.dli_saddr) - reinterpret_cast(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(dl_info.dli_saddr) - reinterpret_cast(dl_info.dli_fbase))); + if (__xtd_abi_demangle(dl_info.dli_sname) == string("main")) break; } return frames; } diff --git a/src/xtd.core.native.unix/src/xtd/native/unix/types.cpp b/src/xtd.core.native.unix/src/xtd/native/unix/types.cpp index effc5c21da6..d697c4e10da 100644 --- a/src/xtd.core.native.unix/src/xtd/native/unix/types.cpp +++ b/src/xtd.core.native.unix/src/xtd/native/unix/types.cpp @@ -7,7 +7,7 @@ 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; @@ -15,6 +15,10 @@ string types::demangle(const string& name) { return result; } +string types::demangle(const string& name) { + return __xtd_abi_demangle(name); +} + intmax_t types::invalid_handle() noexcept { return 0; }