-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I added a system that simplifies adding new demanglers and provides a fast path when demangling symbols Change-Id: Ie5ca43632b53e41c0a4214772193af09ca4593cc Reviewed-by: Ulf Hermann <[email protected]>
- Loading branch information
Showing
8 changed files
with
163 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/**************************************************************************** | ||
** | ||
** Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected], author Lieven Hey <[email protected]> | ||
** Contact: http://www.qt.io/licensing/ | ||
** | ||
** This file is part of the Qt Enterprise Perf Profiler Add-on. | ||
** | ||
** GNU General Public License Usage | ||
** This file may be used under the terms of the GNU General Public License | ||
** version 3 as published by the Free Software Foundation and appearing in | ||
** the file LICENSE.GPLv3 included in the packaging of this file. Please | ||
** review the following information to ensure the GNU General Public License | ||
** requirements will be met: https://www.gnu.org/licenses/gpl.html. | ||
** | ||
** If you have questions regarding the use of this file, please use | ||
** contact form at http://www.qt.io/contact-us | ||
** | ||
****************************************************************************/ | ||
|
||
#include "demangler.h" | ||
|
||
#include <QLibrary> | ||
#include <QDebug> | ||
|
||
/* Demangler specification | ||
* int demangler(const char* mangledSymbol, char* demangledBuffer, size_t bufferSize) | ||
* | ||
* size_t is platform dependent (4 bytes on 32 bit, 8 bytes on 64 bit) | ||
* */ | ||
|
||
namespace { | ||
bool startsWith(const char* string, const QByteArray& prefix) { | ||
return strcmp(string, prefix.constData()) == 0; | ||
} | ||
} | ||
|
||
Demangler::Demangler() | ||
{ | ||
loadDemangleLib(QStringLiteral("rustc_demangle"), "rustc_demangle", QByteArrayLiteral("_R")); | ||
loadDemangleLib(QStringLiteral("d_demangle"), "demangle_symbol", QByteArrayLiteral("_D")); | ||
} | ||
|
||
bool Demangler::demangle(const char *mangledSymbol, char *demangleBuffer, size_t demangleBufferLength) | ||
{ | ||
// fast path, some languages (like rust since 1.37 or d) share a common prefix | ||
// try these first | ||
for (const auto& demangler : m_demanglers) { | ||
if (startsWith(mangledSymbol, demangler.prefix)) { | ||
if (demangler.demangler(mangledSymbol, demangleBuffer, demangleBufferLength)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
for (const auto& demangler : m_demanglers) { | ||
if (demangler.demangler(mangledSymbol, demangleBuffer, demangleBufferLength)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
void Demangler::loadDemangleLib(const QString &name, const char* function, const QByteArray& prefix) | ||
{ | ||
QLibrary lib(name); | ||
if (!lib.load()) { | ||
qDebug() << QStringLiteral("failed to load %1 library").arg(name) | ||
<< lib.errorString(); | ||
return; | ||
} | ||
const auto rawSymbol = lib.resolve(function); | ||
if (!rawSymbol) { | ||
qDebug() << QStringLiteral("failed to resolve %1 function in library").arg(function) | ||
<< lib.fileName() << lib.errorString(); | ||
return; | ||
} | ||
|
||
m_demanglers.push_back({prefix, reinterpret_cast<Demangler::demangler_t>(rawSymbol)}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/**************************************************************************** | ||
** | ||
** Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected], author Lieven Hey <[email protected]> | ||
** Contact: http://www.qt.io/licensing/ | ||
** | ||
** This file is part of the Qt Enterprise Perf Profiler Add-on. | ||
** | ||
** GNU General Public License Usage | ||
** This file may be used under the terms of the GNU General Public License | ||
** version 3 as published by the Free Software Foundation and appearing in | ||
** the file LICENSE.GPLv3 included in the packaging of this file. Please | ||
** review the following information to ensure the GNU General Public License | ||
** requirements will be met: https://www.gnu.org/licenses/gpl.html. | ||
** | ||
** If you have questions regarding the use of this file, please use | ||
** contact form at http://www.qt.io/contact-us | ||
** | ||
****************************************************************************/ | ||
|
||
#ifndef DEMANGLER_H | ||
#define DEMANGLER_H | ||
|
||
#include <QVector> | ||
|
||
class Demangler | ||
{ | ||
public: | ||
Demangler(); | ||
|
||
bool demangle(const char* mangledSymbol, char* demangleBuffer, size_t demangleBufferLength); | ||
|
||
private: | ||
void loadDemangleLib(const QString& name, const char* function, const QByteArray& prefix); | ||
|
||
using demangler_t = int (*) (const char*, char *, size_t); | ||
struct DemangleInfo { | ||
QByteArray prefix; | ||
demangler_t demangler; | ||
}; | ||
|
||
QVector<DemangleInfo> m_demanglers; | ||
}; | ||
|
||
#endif // DEMANGLER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
if (LIBD_DEMANGLE_LIBRARIES) | ||
set (LibDDemangle_FIND_QUIETLY TRUE) | ||
endif() | ||
|
||
find_library(LIBD_DEMANGLE_LIBRARIES | ||
NAMES | ||
d_demangle | ||
PATHS | ||
/usr/lib | ||
/usr/local/lib | ||
/opt/local/lib | ||
/sw/lib | ||
ENV LIBRARY_PATH | ||
ENV LD_LIBRARY_PATH) | ||
|
||
include (FindPackageHandleStandardArgs) | ||
|
||
# handle the QUIETLY and REQUIRED arguments and set LIBRUSTC_DEMANGLE_FOUND to TRUE if all listed variables are TRUE | ||
find_package_handle_standard_args(LibDDemangle DEFAULT_MSG | ||
LIBD_DEMANGLE_LIBRARIES) | ||
|
||
mark_as_advanced(LIBD_DEMANGLE_LIBRARIES) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters