Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
Demangle.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
6#include <cstdlib> // to force __GLIBCXX__ define reference
7#include <mutex>
8
9#if defined(__GNUC__) && defined(__GLIBCXX__)
10#include <cxxabi.h>
11#elif qStroika_Foundation_Common_Platform_Windows
12#include <Windows.h>
13
14#include <Dbghelp.h>
15#endif
16
18
19#include "Demangle.h"
20
21using namespace Stroika::Foundation;
22
23#if qStroika_Foundation_Common_Platform_Windows
24// otherwise modules linking with this code will tend to get link errors without explicitly linking
25// to this module...
26#pragma comment(lib, "Dbghelp.lib")
27#endif
28
29/*
30 ********************************************************************************
31 ************************ Debug::DropIntoDebuggerIfPresent **********************
32 ********************************************************************************
33 */
34Characters::String Debug::Demangle (const Characters::String& originalName)
35{
36#if defined(__GNUC__) && defined(__GLIBCXX__)
37 int status{};
38 char* realname = abi::__cxa_demangle (originalName.AsNarrowSDKString (Characters::eIgnoreErrors).c_str (), 0, 0, &status);
39 [[maybe_unused]] auto&& cleanup = Execution::Finally ([&realname] () noexcept {
40 if (realname != nullptr) {
41 ::free (realname);
42 }
43 });
44 if (status == 0) {
46 }
47#elif qStroika_Foundation_Common_Platform_Windows
48 // From https://learn.microsoft.com/en-us/windows/win32/api/dbghelp/nf-dbghelp-undecoratesymbolname
49 // All DbgHelp functions, such as this one, are single threaded. Therefore,
50 // calls from more than one thread to this function will likely result in
51 // unexpected behavior or memory corruption. To avoid this, you must synchronize
52 // all concurrent calls from more than one thread to this function.
53 static mutex sMutex_;
54 lock_guard critSec{sMutex_};
55 char resultBuf[10 * 1024];
56 if (::UnDecorateSymbolName (originalName.AsNarrowSDKString (Characters::eIgnoreErrors).c_str (), resultBuf, sizeof (resultBuf), UNDNAME_COMPLETE) != 0) {
58 }
59#endif
60 return originalName;
61}
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
nonvirtual string AsNarrowSDKString() const
Definition String.inl:834
static String FromNarrowSDKString(const char *from)
Definition String.inl:470