Repository navigation
Expand file tree
/
Copy pathuinotification.cpp
More file actions
353 lines (294 loc) · 11.6 KB
/
Copy pathuinotification.cpp
File metadata and controls
353 lines (294 loc) · 11.6 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
Copyright 2020-2026 Vector 35 Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "inttypes.h"
#include "uinotification.h"
#include "filecontext.h"
#include "viewframe.h"
#include "linearview.h"
#include "flowgraphwidget.h"
#include "hexeditor.h"
#include <QMessageBox>
#include <QFileInfo>
#include <QPushButton>
#include <QObject>
#include "ui.h"
#include <thread>
using namespace BinaryNinja;
NotificationListener* NotificationListener::m_instance = nullptr;
void NotificationListener::init()
{
m_instance = new NotificationListener;
UIContext::registerNotification(m_instance);
}
void NotificationListener::OnContextOpen(UIContext* context)
{
[[maybe_unused]] GlobalDebuggerUI* ui = GlobalDebuggerUI::CreateForContext(context);
}
void NotificationListener::OnContextClose(UIContext* context)
{
GlobalDebuggerUI::RemoveForContext(context);
}
bool NotificationListener::OnBeforeOpenDatabase(UIContext* context, FileMetadataRef metadata)
{
return true;
}
bool NotificationListener::OnAfterOpenDatabase(UIContext* context, FileMetadataRef metadata, BinaryViewRef data)
{
return true;
}
bool NotificationListener::OnBeforeOpenFile(UIContext* context, FileContext* file)
{
return true;
}
void NotificationListener::OnAfterOpenFile(UIContext* context, FileContext* file, ViewFrame* frame) {}
bool NotificationListener::OnBeforeSaveFile(UIContext* context, FileContext* file, ViewFrame* frame)
{
return true;
}
void NotificationListener::OnAfterSaveFile(UIContext* context, FileContext* file, ViewFrame* frame) {}
bool NotificationListener::OnBeforeCloseFile(UIContext* context, FileContext* file, ViewFrame* frame)
{
auto mainWindow = context->mainWindow();
size_t count = 0;
for (const auto& ctx: UIContext::allContexts())
{
auto tabs = ctx->getTabs();
for (auto tab : tabs)
{
auto viewFrame = ctx->getViewFrameForTab(tab);
if (viewFrame && (viewFrame->getFileContext() == file))
count++;
}
}
// If this is not the last tab of the file being closed, return
if (count != 1)
return true;
auto controller = DebuggerController::GetController(file->getMetadata());
if (!controller)
return true;
if (controller->IsConnected())
{
QMessageBox* msgBox = new QMessageBox(mainWindow);
msgBox->setAttribute(Qt::WA_DeleteOnClose);
msgBox->setIcon(QMessageBox::Question);
msgBox->setText(QObject::tr("The debugger file ") + file->getShortFileName(mainWindow)
+ QObject::tr(" is active. Do you want to stop it before closing?"));
msgBox->setWindowTitle(QObject::tr("Debugger Active"));
msgBox->setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
msgBox->setDefaultButton(QMessageBox::Yes);
msgBox->show();
msgBox->move(mainWindow->frameGeometry().center() - msgBox->rect().center());
msgBox->setAttribute(Qt::WA_KeyboardFocusChange);
int result = msgBox->exec();
if (result == QMessageBox::Cancel)
return false;
else if (result == QMessageBox::Yes)
{
std::thread([=]() {
// Since we cannot wait for the target to stop on the main thread, we must create a new thread and
// wait from there.
controller->QuitAndWait();
controller->Destroy();
}).detach();
return true;
}
}
else
{
controller->Destroy();
}
return true;
}
void NotificationListener::OnAfterCloseFile(UIContext* context, FileContext* file, ViewFrame* frame) {}
void NotificationListener::OnViewChange(UIContext* context, ViewFrame* frame, const QString& type)
{
auto ui = GlobalDebuggerUI::GetForContext(context);
if (ui != nullptr)
ui->SetActiveFrame(frame);
}
void NotificationListener::OnAddressChange(
UIContext* context, ViewFrame* frame, View* view, const ViewLocation& location)
{}
bool NotificationListener::GetNameForFile(UIContext* context, FileContext* file, QString& name)
{
return false;
}
bool NotificationListener::GetNameForPath(UIContext* context, const QString& path, QString& name)
{
return false;
}
void NotificationListener::OnContextMenuCreated(UIContext *context, View* view, Menu &menu)
{
// Only add the context menu to the linear/graph/hex views
if (!dynamic_cast<LinearView*>(view) && !dynamic_cast<FlowGraphWidget*>(view)
&& !dynamic_cast<HexEditor*>(view))
return;
menu.addAction("Debugger", "Toggle Breakpoint", "Breakpoint");
menu.addAction("Debugger", "Enable Breakpoint", "Breakpoint");
menu.addAction("Debugger", "Solo Breakpoint", "Breakpoint");
menu.addAction("Debugger", "Edit Condition...", "Breakpoint");
menu.addAction("Debugger", "Add Hardware Breakpoint...", "Breakpoint");
menu.addAction("Debugger", "Launch", "Control");
menu.addAction("Debugger", "Pause", "Control");
menu.addAction("Debugger", "Restart", "Control");
menu.addAction("Debugger", "Resume", "Control");
menu.addAction("Debugger", "Step Into", "Control");
menu.addAction("Debugger", "Step Over", "Control");
menu.addAction("Debugger", "Step Return", "Control");
menu.addAction("Debugger", "Run To Here", "Control");
menu.addAction("Debugger", "Run Back To Here", "Control");
menu.addAction("Debugger", "Create Stack View", "Misc");
menu.addAction("Debugger", "Override IP", "Misc");
menu.addAction("Debugger", "Rebase to Remote Base...", "Misc");
// TTD Memory Access context menu items
menu.addAction("Debugger", "Navigate to TTD Timestamp...", "TTD");
menu.addAction("Debugger", "Add TTD Bookmark...", "TTD");
menu.addAction("Debugger", "TTD Memory Access\\Read", "TTD");
menu.addAction("Debugger", "TTD Memory Access\\Write", "TTD");
menu.addAction("Debugger", "TTD Memory Access\\Read/Write", "TTD");
menu.addAction("Debugger", "TTD Memory Access\\Execute", "TTD");
menu.addAction("Debugger", "TTD Memory Access\\Read/Write/Execute", "TTD");
// TTD Memory Access (Next/Prev) — single dialog entry
menu.addAction("Debugger", "TTD Memory Access (Next/Prev)", "TTD");
// TTD Register Write navigation — only enabled when a register token is under the cursor
menu.addAction("Debugger", "Go to Previous Register Write", "TTD");
menu.addAction("Debugger", "Go to Next Register Write", "TTD");
// TTD Calls context menu item
menu.addAction("Debugger", "TTD Calls\\Kernel32 Calls", "TTD");
}
bool NotificationListener::OnTokenDoubleClicked(UIContext* context, ViewFrame* frame, View* view,
const ViewLocation& location, const HighlightTokenState& token)
{
// When a debug session is active and the user double-clicks a register or a
// variable, navigate to the address it refers to at the current stop location.
// If we cannot resolve it, return false so the default double-click behavior
// (navigation, rename dialog, etc.) still runs.
if (!view)
return false;
auto data = view->getData();
if (!data)
return false;
// Only act when a controller already exists for this view; don't spin one up
// just because a token was double-clicked.
if (!DebuggerController::ControllerExists(data))
return false;
auto controller = DebuggerController::GetController(data);
if (!controller || !controller->IsConnected() || controller->IsRunning())
return false;
uint64_t target = 0;
bool haveTarget = false;
if ((token.type == IntegerToken || token.type == PossibleAddressToken
|| token.type == CodeRelativeAddressToken)
&& token.addrValid)
{
// An address-literal token (a raw address/number in the operand) that resolves to an
// address. The default behavior navigates to it in the same view; while debugging we
// instead open it in another pane so the disassembly the user is looking at stays put.
// AddressDisplayToken is intentionally left out: those keep navigating in the current view.
// NOTE: opening address-valued literals in a new pane is arguably a better default
// and should probably become the standard behavior even outside of a debug session.
// If the target lands inside the function the user is already looking at, they most
// likely want to jump to it in place rather than open a second pane. Fall through to
// the default double-click behavior (in-pane navigation) in that case.
auto func = location.getFunction();
if (!func)
{
auto funcs = data->GetAnalysisFunctionsContainingAddress(location.getOffset());
if (!funcs.empty())
func = funcs[0];
}
if (func)
{
for (const auto& range : func->GetAddressRanges())
{
if ((token.addr >= range.start) && (token.addr < range.end))
return false;
}
}
target = token.addr;
haveTarget = true;
}
else if (token.type == DataSymbolToken || token.type == StringToken)
{
// A data symbol (e.g. data_100003fa9) or a string reference. The default behavior
// navigates to it in the same view; while debugging we instead open it in another
// pane so the disassembly the user is looking at stays put.
if ((token.type == StringToken) && (token.token.context != StringReferenceTokenContext)
&& (token.token.context != StringDataVariableTokenContext))
return false;
if (!token.addrValid)
return false;
target = token.addr;
haveTarget = true;
}
else if (token.type == RegisterToken)
{
// A raw register token: navigate to the address currently held in the register.
target = (uint64_t)controller->GetRegisterValue(token.token.text);
haveTarget = true;
}
else if ((token.type == LocalVariableToken || token.type == StackVariableToken) && token.localVarValid)
{
const auto& var = token.localVar;
if (var.type == RegisterVariableSourceType)
{
// The variable lives in a register: navigate to the address it holds.
auto arch = data->GetDefaultArchitecture();
if (!arch)
return false;
auto regName = arch->GetRegisterName((uint32_t)var.storage);
// GetRegisterValue expects the adapter's name for the frame pointer on arm64.
if (regName == "x29")
regName = "fp";
target = (uint64_t)controller->GetRegisterValue(regName);
haveTarget = true;
}
else if (var.type == StackVariableSourceType)
{
// A stack variable: navigate to its live address in the current stack frame.
// var.storage is an offset relative to the stack pointer at function entry.
auto arch = data->GetDefaultArchitecture();
if (!arch)
return false;
// Preferred: recover the entry-time stack pointer using the analyzed stack-frame
// offset at the current instruction, so we stay correct mid-function once the
// prologue has adjusted the stack.
// Fallback: if there is no clean frame offset here (e.g. we are stopped at the
// function entry before the prologue has run, so the variable has not been created
// yet), assume the current stack pointer is the frame base. This is exact at entry
// and a best effort otherwise.
uint64_t stackAtFuncEntry = controller->StackPointer();
auto func = location.getFunction();
if (!func)
{
auto funcs = data->GetAnalysisFunctionsContainingAddress(location.getOffset());
if (!funcs.empty())
func = funcs[0];
}
if (func)
{
auto stackReg = arch->GetStackPointerRegister();
auto stackValue = func->GetRegisterValueAtInstruction(arch, controller->GetLastIP(), stackReg);
if (stackValue.state == StackFrameOffset)
stackAtFuncEntry = controller->StackPointer() - stackValue.value;
}
target = stackAtFuncEntry + var.storage;
haveTarget = true;
}
}
if (!haveTarget)
return false;
// Open the target in another pane so the disassembly the user is looking at stays put.
view->navigateOnOtherPane(target);
return true;
}