File indexing completed on 2024-05-19 05:44:07

0001 /*
0002     Copyright (C) 2015 Volker Krause <vkrause@kde.org>
0003 
0004     This program is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program.  If not, see <https://www.gnu.org/licenses/>.
0016 */
0017 
0018 #ifndef DWARFDIE_H
0019 #define DWARFDIE_H
0020 
0021 #include <QVariant>
0022 #include <QVector>
0023 
0024 #include <libdwarf.h>
0025 
0026 class DwarfInfo;
0027 class DwarfCuDie;
0028 class QString;
0029 
0030 class DwarfDie
0031 {
0032 public:
0033     DwarfDie(const DwarfDie&) = delete;
0034     ~DwarfDie();
0035 
0036     DwarfDie& operator=(const DwarfDie&) = delete;
0037 
0038     DwarfInfo* dwarfInfo() const;
0039     DwarfDie* parentDie() const;
0040     bool isCompilationUnit() const;
0041 
0042     /** Content of the name attribute. */
0043     QByteArray name() const;
0044     Dwarf_Half tag() const;
0045     QByteArray tagName() const;
0046     Dwarf_Off offset() const;
0047 
0048     /** If this DIE represents a type, this is the full type name. */
0049     QByteArray typeName() const;
0050     /** If this DIE represents a type, this is the size occupied by this type in bytes. */
0051     int typeSize() const;
0052     /** If this DIE represents a type, this returns the alignment needed for it. */
0053     int typeAlignment() const;
0054 
0055     /** If this is a DW_TAG_member, check if this is a static member, or a non-static one. */
0056     bool isStaticMember() const;
0057 
0058     /** Best effort human readable distplay string. */
0059     QString displayName() const;
0060     /** Fully qualified name (including class/namespaces etc). */
0061     QByteArray fullyQualifiedName() const;
0062     /** Path to the source file. Best effort attempt to make it absolute, but that can't be guaranteed. */
0063     QString sourceFilePath() const;
0064     /** Source code location, best effort to find an absolute path, and line number if present. */
0065     QString sourceLocation() const;
0066 
0067     QVector<Dwarf_Half> attributes() const;
0068     static QByteArray attributeName(Dwarf_Half attributeType);
0069     QVariant attribute(Dwarf_Half attributeType) const;
0070 
0071     QVector<DwarfDie*> children() const;
0072     DwarfDie* dieAtOffset(Dwarf_Off offset) const;
0073 
0074     /** If this DIE is inheriting attributes from another DIE, that's returned here. */
0075     DwarfDie* inheritedFrom() const;
0076 
0077     // internal
0078     const DwarfCuDie* compilationUnit() const;
0079     Dwarf_Die dieHandle() const;
0080 
0081 protected:
0082     friend class DwarfInfoPrivate;
0083     DwarfDie(Dwarf_Die die, DwarfDie* parent);
0084     DwarfDie(Dwarf_Die die, DwarfInfo* info);
0085 
0086     QVariant attributeLocal(Dwarf_Half attributeType) const;
0087 
0088     void scanChildren() const;
0089 
0090     Dwarf_Debug dwarfHandle() const;
0091 
0092     Dwarf_Die m_die = nullptr;
0093     union {
0094         DwarfDie *parent = nullptr;
0095         DwarfInfo *info;
0096     } m_parent;
0097 
0098     mutable QVector<DwarfDie*> m_children;
0099     mutable bool m_childrenScanned = false;
0100 };
0101 
0102 Q_DECLARE_METATYPE(DwarfDie*)
0103 
0104 #endif // DWARFDIE_H