From 915556d273381fdf5a2b94301859af947c54308c Mon Sep 17 00:00:00 2001 From: ragundo Date: Tue, 28 Jan 2020 21:08:31 +0100 Subject: [PATCH] Use hexadecimal for offsets Pointer to structures now show the offset --- dwarfexplorer/MainWindow.h | 2 +- dwarfexplorer/QtModel/df_model.cpp | 210 ++++++++++++++++------------- 2 files changed, 120 insertions(+), 92 deletions(-) diff --git a/dwarfexplorer/MainWindow.h b/dwarfexplorer/MainWindow.h index 709fea7..638cb82 100644 --- a/dwarfexplorer/MainWindow.h +++ b/dwarfexplorer/MainWindow.h @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include "QtModel/df_proxy_model.h" #include "df_model.h" diff --git a/dwarfexplorer/QtModel/df_model.cpp b/dwarfexplorer/QtModel/df_model.cpp index 877f83a..9492cee 100644 --- a/dwarfexplorer/QtModel/df_model.cpp +++ b/dwarfexplorer/QtModel/df_model.cpp @@ -18,13 +18,13 @@ * */ -#include -#include -#include -#include -#include #include "../df_model.h" #include "node.h" +#include +#include +#include +#include +#include using namespace rdf; @@ -84,10 +84,8 @@ QModelIndex DF_Model::parent(const QModelIndex& p_child_index) const return QModelIndex(); Node* l_gp_node = static_cast(grandparentNode); - auto it = std::find(l_gp_node->m_children.cbegin() - ,l_gp_node->m_children.cend() - , parentNode); - int row = std::distance(l_gp_node->m_children.cbegin(), it); + auto it = std::find(l_gp_node->m_children.cbegin(), l_gp_node->m_children.cend(), parentNode); + int row = std::distance(l_gp_node->m_children.cbegin(), it); //int row = l_gp_node->m_children.indexOf(parentNode); return createIndex(row, 0, parentNode); } @@ -105,7 +103,6 @@ int DF_Model::rowCount(const QModelIndex& p_parent) const return l_parent->m_children.size(); } - NodeBase* DF_Model::nodeFromIndex(const QModelIndex& p_index) const { if (p_index.isValid()) @@ -150,6 +147,46 @@ QString data_from_Refers_to(const NodeBase* p_node) return QString::fromStdString(base->m_refers_to); } +QString data_fromm_Offset(const NodeBase* p_node) +{ + const NodeBase* base = dynamic_cast(p_node); + NodeBase* base_compound = base->m_parent; + do + { + if (base_compound) + { + if (base_compound->m_node_type == NodeType::Compound) + // sucess + break; + if (base_compound->m_node_type == NodeType::Vector) + return "-"; + if (base_compound->m_node_type == NodeType::Pointer) + { + if (base_compound->m_address == 0) + return "-"; + break; + } + } + base_compound = base_compound->m_parent; + + } while (base_compound != nullptr); + + if (base_compound) + { + if (base_compound->m_node_type == NodeType::Pointer) + { + uint64_t* pointer = reinterpret_cast(base_compound->m_address); + uint64_t pointee = *pointer; + std::string offset_hex = to_hex(base->m_address - pointee); + return QString::fromStdString(offset_hex); + } + std::string offset_hex = to_hex(base->m_address - base_compound->m_address); + return QString::fromStdString(offset_hex); + } + + return "-"; +} + QVariant DF_Model::data(const QModelIndex& p_index, int p_role) const { if (m_root_node == nullptr) @@ -159,21 +196,12 @@ QVariant DF_Model::data(const QModelIndex& p_index, int p_role) const if ((p_index.column() == 0) && (p_role == Qt::DecorationRole)) { - if ((node->m_rdf_type == rdf::RDF_Type::uint8_t) - || (node->m_rdf_type == rdf::RDF_Type::uint16_t) - || (node->m_rdf_type == rdf::RDF_Type::uint32_t) - || (node->m_rdf_type == rdf::RDF_Type::uint64_t) - || (node->m_rdf_type == rdf::RDF_Type::int8_t) - || (node->m_rdf_type == rdf::RDF_Type::int16_t) - || (node->m_rdf_type == rdf::RDF_Type::int32_t) - || (node->m_rdf_type == rdf::RDF_Type::int64_t) - || (node->m_rdf_type == rdf::RDF_Type::Long) - || (node->m_rdf_type == rdf::RDF_Type::Bool)) + if ((node->m_rdf_type == rdf::RDF_Type::uint8_t) || (node->m_rdf_type == rdf::RDF_Type::uint16_t) || (node->m_rdf_type == rdf::RDF_Type::uint32_t) || (node->m_rdf_type == rdf::RDF_Type::uint64_t) || (node->m_rdf_type == rdf::RDF_Type::int8_t) || (node->m_rdf_type == rdf::RDF_Type::int16_t) || (node->m_rdf_type == rdf::RDF_Type::int32_t) || (node->m_rdf_type == rdf::RDF_Type::int64_t) || (node->m_rdf_type == rdf::RDF_Type::Long) || (node->m_rdf_type == rdf::RDF_Type::Bool)) return QPixmap(":/circle.png"); if ((node->m_rdf_type == rdf::RDF_Type::Stl_string) || - (node->m_rdf_type == rdf::RDF_Type::Static_string) || - (node->m_rdf_type == rdf::RDF_Type::Ptr_string)) + (node->m_rdf_type == rdf::RDF_Type::Static_string) || + (node->m_rdf_type == rdf::RDF_Type::Ptr_string)) return QPixmap(":/t2_small.png"); if ((node->m_rdf_type == rdf::RDF_Type::Compound)) @@ -210,6 +238,9 @@ QVariant DF_Model::data(const QModelIndex& p_index, int p_role) const return QPixmap(":/stripes.png"); } + if (p_role == Qt::ToolTipRole) + return QString("Tooltip"); + if (p_role != Qt::DisplayRole) return QVariant(); @@ -218,21 +249,23 @@ QVariant DF_Model::data(const QModelIndex& p_index, int p_role) const switch (p_index.column()) { - case 0 : - return data_from_Name(node); // Name - case 1 : + case 0: + return data_from_Name(node); // Name + case 1: return data_from_Structure(node); // Structure type - case 2 : - return data_from_Type(node); // Type - case 3 : - return data_from_Value(node); // Value - case 4 : - return data_from_Address(node); // Address - case 5 : - return QString::fromStdString(node->m_defined_in); // refers-to - case 6 : - return data_from_Comment(node); // Comment - case 7 : + case 2: + return data_from_Type(node); // Type + case 3: + return data_from_Value(node); // Value + case 4: + return data_from_Address(node); // Address + case 5: + return data_fromm_Offset(node); // Offset + case 6: + return QString::fromStdString(node->m_defined_in); // refers-to + case 7: + return data_from_Comment(node); // Comment + case 8: return data_from_Refers_to(node); // refers-to default: return QVariant(); @@ -246,21 +279,23 @@ QVariant DF_Model::headerData(int p_section, Qt::Orientation p_orientation, int { switch (p_section) { - case 0 : + case 0: return tr("Name"); - case 1 : + case 1: return tr("Structure"); - case 2 : + case 2: return tr("Type"); - case 3 : + case 3: return tr("Value"); - case 4 : + case 4: return tr("Address"); - case 5 : + case 5: + return tr("Offset"); + case 6: return tr("Defined in"); - case 6 : + case 7: return tr("Comment"); - case 7 : + case 8: return tr("Refers-to"); default: @@ -272,7 +307,7 @@ QVariant DF_Model::headerData(int p_section, Qt::Orientation p_orientation, int int DF_Model::columnCount(const QModelIndex& /*p_parent*/) const { - return 8; + return 9; } bool DF_Model::removeRows(int p_row, int p_count, const QModelIndex& /*p_parent*/) @@ -292,12 +327,11 @@ bool DF_Model::removeColumns(int p_column, int p_count, const QModelIndex& /*p_p void DF_Model::update_node_path(NodeBase* p_source, NodeBase* p_dest) { -// QListIterator l_iterator(p_source->m_path); -// while(l_iterator.hasNext()) -// p_dest->m_path.append(l_iterator.next()); + // QListIterator l_iterator(p_source->m_path); + // while(l_iterator.hasNext()) + // p_dest->m_path.append(l_iterator.next()); } - bool DF_Model::insertRowsBitfield(const QModelIndex& p_parent) { auto bitfield_node = dynamic_cast(nodeFromIndex(p_parent)); @@ -308,16 +342,16 @@ bool DF_Model::insertRowsBitfield(const QModelIndex& p_parent) auto bitfield_value = *pointer_bitfield; if (bitfield_node->m_index_enum == DF_Type::None) { - std::array < std::array, 32>& bitfield_data = get_bitfield_bits(bitfield_node->m_df_type); + std::array, 32>& bitfield_data = get_bitfield_bits(bitfield_node->m_df_type); beginInsertRows(p_parent, 0, bitfield_data.size()); unsigned int mask = 1; - for (unsigned int i = 0; i < bitfield_data.size() ; i++) + for (unsigned int i = 0; i < bitfield_data.size(); i++) { std::string field_name = bitfield_data[i][1]; if ((bitfield_value & mask) || (field_name.length() > 0)) { - auto* n_pve = new NodeBitfieldEntry(); - n_pve->m_field_name = field_name; + auto* n_pve = new NodeBitfieldEntry(); + n_pve->m_field_name = field_name; if (field_name.length() == 0) { if (i < 10) @@ -328,37 +362,36 @@ bool DF_Model::insertRowsBitfield(const QModelIndex& p_parent) else { if (i < 10) - n_pve->m_field_name = "[0" + std::to_string(i) + "] " + field_name; + n_pve->m_field_name = "[0" + std::to_string(i) + "] " + field_name; else - n_pve->m_field_name = "[" + std::to_string(i) + "] " + field_name; + n_pve->m_field_name = "[" + std::to_string(i) + "] " + field_name; } - n_pve->m_rdf_type = rdf::RDF_Type::Bool; - n_pve->m_df_type = rdf::DF_Type::Bool; - n_pve->m_parent = bitfield_node; - n_pve->m_index = i; - n_pve->m_comment = bitfield_data[i][2]; - n_pve->m_value = bitfield_value & mask; - n_pve->m_address = bitfield_node->m_address; + n_pve->m_rdf_type = rdf::RDF_Type::Bool; + n_pve->m_df_type = rdf::DF_Type::Bool; + n_pve->m_parent = bitfield_node; + n_pve->m_index = i; + n_pve->m_comment = bitfield_data[i][2]; + n_pve->m_value = bitfield_value & mask; + n_pve->m_address = bitfield_node->m_address; bitfield_node->m_children.push_back(n_pve); } mask = mask << 1; } } -// // Update this node path -// update_node_path(l_bitfield_node, n_pve); + // // Update this node path + // update_node_path(l_bitfield_node, n_pve); endInsertRows(); return true; } - bool DF_Model::insertRowsDFFlagArray(const QModelIndex& p_parent) { auto df_flag_array_node = dynamic_cast(nodeFromIndex(p_parent)); if (df_flag_array_node->m_children.size() > 0) return false; - uint8_t** a = reinterpret_cast(df_flag_array_node->m_address); - uint8_t* pointer_df_flag_array = *a; + uint8_t** a = reinterpret_cast(df_flag_array_node->m_address); + uint8_t* pointer_df_flag_array = *a; // This is a hack NodeEnum ne; ne.m_base_type = DF_Type::int8_t; @@ -366,19 +399,19 @@ bool DF_Model::insertRowsDFFlagArray(const QModelIndex& p_parent) beginInsertRows(p_parent, 0, df_flag_array_node->m_size); - unsigned int mask = 1; - uint8_t bitfield_value = 0; - for (unsigned int i = 0; i < df_flag_array_node->m_size ; i++) + unsigned int mask = 1; + uint8_t bitfield_value = 0; + for (unsigned int i = 0; i < df_flag_array_node->m_size; i++) { - ne.m_address = reinterpret_cast(&i); - auto enum_data = get_enum_decoded(&ne); + ne.m_address = reinterpret_cast(&i); + auto enum_data = get_enum_decoded(&ne); std::string field_name = std::get<1>(enum_data); if (i % 8 == 0) bitfield_value = *pointer_df_flag_array; if ((bitfield_value & mask) || (field_name.length() > 0)) { - auto* n_pve = new NodeBitfieldEntry(); - n_pve->m_field_name = field_name; + auto* n_pve = new NodeBitfieldEntry(); + n_pve->m_field_name = field_name; if (field_name.length() == 0) { if (i < 10) @@ -389,21 +422,21 @@ bool DF_Model::insertRowsDFFlagArray(const QModelIndex& p_parent) else { if (i < 10) - n_pve->m_field_name = "[0" + std::to_string(i) + "] " + field_name; + n_pve->m_field_name = "[0" + std::to_string(i) + "] " + field_name; else - n_pve->m_field_name = "[" + std::to_string(i) + "] " + field_name; + n_pve->m_field_name = "[" + std::to_string(i) + "] " + field_name; } - n_pve->m_rdf_type = rdf::RDF_Type::Bool; - n_pve->m_df_type = rdf::DF_Type::Bool; - n_pve->m_parent = df_flag_array_node; - n_pve->m_index = i; - n_pve->m_comment = ""; - n_pve->m_value = bitfield_value & mask; - n_pve->m_address = reinterpret_cast(pointer_df_flag_array); + n_pve->m_rdf_type = rdf::RDF_Type::Bool; + n_pve->m_df_type = rdf::DF_Type::Bool; + n_pve->m_parent = df_flag_array_node; + n_pve->m_index = i; + n_pve->m_comment = ""; + n_pve->m_value = bitfield_value & mask; + n_pve->m_address = reinterpret_cast(pointer_df_flag_array); df_flag_array_node->m_children.push_back(n_pve); } mask = mask << 1; - if ((i+1) % 8 == 0) + if ((i + 1) % 8 == 0) { pointer_df_flag_array++; mask = 1; @@ -413,7 +446,6 @@ bool DF_Model::insertRowsDFFlagArray(const QModelIndex& p_parent) return true; } - bool DF_Model::insertRowsCompound(const QModelIndex& p_parent, int p_num_rows) { Node* l_node = dynamic_cast(nodeFromIndex(p_parent)); @@ -426,7 +458,7 @@ bool DF_Model::insertRowsCompound(const QModelIndex& p_parent, int p_num_rows) return true; } -bool DF_Model::has_children_from_type( NodeBase* p_node) const +bool DF_Model::has_children_from_type(NodeBase* p_node) const { if (p_node->is_root_node()) return true; @@ -502,10 +534,9 @@ bool DF_Model::has_children_from_type( NodeBase* p_node) const break; } - if (p_node->m_rdf_type == rdf::RDF_Type::Pointer) { - NodePointer *l_pointer = dynamic_cast(p_node); + NodePointer* l_pointer = dynamic_cast(p_node); if (m_outdated) { if (l_pointer->m_children.size() > 0) @@ -577,14 +608,11 @@ bool DF_Model::hasChildren(const QModelIndex& p_parent) const //return nullptr; //} - //NodeBase* DF_Model::locate(rdf::RDFBase* p_object) //{ // return locate_aux(p_object, m_rootNode); //} - - void DF_Model::insert_child_nodes(NodeBase* p_node, const QModelIndex& p_index) { switch (p_node->m_rdf_type)