-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathsymboltable.cpp
More file actions
228 lines (185 loc) · 5.53 KB
/
Copy pathsymboltable.cpp
File metadata and controls
228 lines (185 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <progresstask.h>
#include "symboltable.h"
#include <QHeaderView>
#include "ui/fontsettings.h"
#include "binaryninjaapi.h"
using namespace BinaryNinja;
using namespace SharedCacheAPI;
SymbolTableModel::SymbolTableModel(SymbolTableView* parent) :
QAbstractTableModel(parent), m_parent(parent), m_displaySymbols(&m_symbols)
{
// TODO: Need to implement updating this font if it is changed by the user
m_font = getMonospaceFont(parent);
}
int SymbolTableModel::rowCount(const QModelIndex& parent) const {
Q_UNUSED(parent);
return static_cast<int>(m_displaySymbols->size());
}
int SymbolTableModel::columnCount(const QModelIndex& parent) const {
Q_UNUSED(parent);
// We have 3 columns: Address, Type, Name
return 3;
}
QVariant SymbolTableModel::data(const QModelIndex& index, int role) const {
if (!index.isValid() || (role != Qt::DisplayRole && role != Qt::FontRole)) {
return QVariant();
}
switch (role)
{
case Qt::DisplayRole:
{
auto symbol = symbolAt(index.row());
auto symbolType = GetSymbolTypeAsString(symbol.type);
switch (index.column())
{
case 0: // Address column
return QString("0x%1").arg(symbol.address, 0, 16); // Display address as hexadecimal
case 1: // Type column
return QString::fromUtf8(symbolType.c_str(), symbolType.size());
case 2: // Name column
return QString::fromUtf8(symbol.name.c_str(), symbol.name.size());
default:
return QVariant();
}
}
case Qt::FontRole:
return m_font;
default:
return QVariant();
}
}
QVariant SymbolTableModel::headerData(int section, Qt::Orientation orientation, int role) const {
if (role != Qt::DisplayRole || orientation != Qt::Horizontal) {
return QVariant();
}
switch (section) {
case 0:
return QString("Address");
case 1:
return QString("Type");
case 2:
return QString("Name");
default:
return QVariant();
}
}
void SymbolTableModel::sort(int column, Qt::SortOrder order)
{
beginResetModel();
std::function<bool(const CacheSymbol&, const CacheSymbol&)> comparator;
switch (column)
{
case 0: // Address column
comparator = [](const CacheSymbol& a, const CacheSymbol& b) {
return a.address < b.address;
};
break;
case 1: // Type column
comparator = [](const CacheSymbol& a, const CacheSymbol& b) {
return GetSymbolTypeAsString(a.type) < GetSymbolTypeAsString(b.type);
};
break;
case 2: // Name column
comparator = [](const CacheSymbol& a, const CacheSymbol& b) {
return a.name < b.name;
};
break;
default:
endResetModel();
return;
}
if (order == Qt::DescendingOrder)
{
std::sort(m_displaySymbols->begin(), m_displaySymbols->end(),
[&comparator](const CacheSymbol& a, const CacheSymbol& b) { return comparator(b, a); });
}
else
{
std::sort(m_displaySymbols->begin(), m_displaySymbols->end(), comparator);
}
endResetModel();
}
void SymbolTableModel::updateSymbols(std::vector<CacheSymbol> symbols)
{
m_symbols = std::move(symbols);
setFilter(m_filter);
}
const CacheSymbol& SymbolTableModel::symbolAt(int row) const
{
return m_displaySymbols->at(row);
}
void SymbolTableModel::setFilter(std::string text)
{
beginResetModel();
m_filter = std::move(text);
// Skip filtering if no filter applied.
if (m_filter.empty())
{
m_filteredSymbols = {};
m_displaySymbols = &m_symbols;
}
else
{
// Clear the filtered symbols while preserving the capacity
m_filteredSymbols.clear();
for (const auto& symbol : m_symbols)
{
if (symbol.name.find(m_filter) != std::string::npos)
m_filteredSymbols.push_back(symbol);
}
// If the filtered vector is using less than 25% of its capacity,
// shrink it to reduce memory usage.
if (m_filteredSymbols.size() < m_filteredSymbols.capacity() / 4)
m_filteredSymbols.shrink_to_fit();
m_displaySymbols = &m_filteredSymbols;
}
endResetModel();
}
SymbolTableView::SymbolTableView(QWidget* parent) :
QTableView(parent), m_model(new SymbolTableModel(this))
{
// Set up the filter model
setModel(m_model);
// Configure view settings
horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);
horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);
setEditTriggers(QAbstractItemView::NoEditTriggers);
setSelectionBehavior(QAbstractItemView::SelectRows);
setSelectionMode(QAbstractItemView::SingleSelection);
verticalHeader()->setVisible(false);
sortByColumn(0, Qt::AscendingOrder);
setSortingEnabled(true);
}
SymbolTableView::~SymbolTableView() = default;
void SymbolTableView::populateSymbols(BinaryView &view)
{
if (auto controller = SharedCacheController::GetController(view)) {
typedef std::vector<CacheSymbol> SymbolList;
// Retrieve the symbols from the controller in a future than pass that to the model.
QPointer<QFutureWatcher<SymbolList>> watcher = new QFutureWatcher<SymbolList>(this);
connect(watcher, &QFutureWatcher<SymbolList>::finished, this, [watcher, this]() {
if (watcher)
{
auto symbols = watcher->result();
m_model->updateSymbols(std::move(symbols));
// Reapply the current sort after repopulating the model
// TODO: The model should use `QSortFilterProxyModel`, but that's a bigger change.
setSortingEnabled(true);
}
});
QFuture<SymbolList> future = QtConcurrent::run([controller]() {
return controller->GetSymbols();
});
watcher->setFuture(future);
connect(this, &QObject::destroyed, this, [watcher]() {
if (watcher && watcher->isRunning()) {
watcher->cancel();
watcher->waitForFinished();
}
});
}
}
void SymbolTableView::setFilter(const std::string& filter) {
m_model->setFilter(filter);
}