-
-
Notifications
You must be signed in to change notification settings - Fork 342
Expand file tree
/
Copy pathedb.cpp
More file actions
1650 lines (1401 loc) · 42.3 KB
/
Copy pathedb.cpp
File metadata and controls
1650 lines (1401 loc) · 42.3 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2006 - 2025 Evan Teran <evan.teran@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "edb.h"
#include "ArchProcessor.h"
#include "BinaryString.h"
#include "Configuration.h"
#include "DebugEventHandlers.h"
#include "Debugger.h"
#include "DebuggerInternal.h"
#include "DialogInputBinaryString.h"
#include "DialogInputValue.h"
#include "DialogOptions.h"
#include "Expression.h"
#include "ExpressionDialog.h"
#include "IBreakpoint.h"
#include "IDebugger.h"
#include "IPlugin.h"
#include "IProcess.h"
#include "IRegion.h"
#include "IThread.h"
#include "MemoryRegions.h"
#include "Prototype.h"
#include "QHexView"
#include "QtHelper.h"
#include "State.h"
#include "Symbol.h"
#include "SymbolManager.h"
#include "version.h"
#include <QAction>
#include <QAtomicPointer>
#include <QByteArray>
#include <QCompleter>
#include <QCoreApplication>
#include <QCryptographicHash>
#include <QDebug>
#include <QDomDocument>
#include <QFile>
#include <QFileInfo>
#include <QMessageBox>
#include <cctype>
IDebugger *edb::v1::debugger_core = nullptr;
QWidget *edb::v1::debugger_ui = nullptr;
namespace edb {
Q_DECLARE_NAMESPACE_TR(edb)
namespace {
using BinaryInfoList = QList<IBinary::create_func_ptr_t>;
DebugEventHandlers g_DebugEventHandlers;
QAtomicPointer<IAnalyzer> g_Analyzer = nullptr;
QMap<QString, QObject *> g_GeneralPlugins;
BinaryInfoList g_BinaryInfoList;
CapstoneEDB::Formatter g_Formatter;
QHash<QString, edb::Prototype> g_FunctionDB;
Debugger *ui() {
return qobject_cast<Debugger *>(edb::v1::debugger_ui);
}
bool function_symbol_base(edb::address_t address, QString *value, int *offset) {
Q_ASSERT(value);
Q_ASSERT(offset);
if (const std::optional<Symbol> s = edb::v1::symbol_manager().findNearSymbol(address)) {
*value = s->name;
*offset = static_cast<int>(address - s->address);
return true;
}
*offset = 0;
return false;
}
}
namespace internal {
/**
* @brief
*/
bool register_plugin(const QString &filename, QObject *plugin) {
if (!g_GeneralPlugins.contains(filename)) {
g_GeneralPlugins[filename] = plugin;
return true;
}
return false;
}
/**
* @brief
*/
void load_function_db() {
QFile file(":/debugger/xml/functions.xml");
QDomDocument doc;
if (file.open(QIODevice::ReadOnly)) {
if (doc.setContent(&file)) {
QDomElement root = doc.firstChildElement("functions");
QDomElement function = root.firstChildElement("function");
for (; !function.isNull(); function = function.nextSiblingElement("function")) {
Prototype func;
func.name = function.attribute("name");
func.type = function.attribute("type");
func.noreturn = function.attribute("noreturn", "false") == "true";
QDomElement argument = function.firstChildElement("argument");
for (; !argument.isNull(); argument = argument.nextSiblingElement("argument")) {
Argument arg;
arg.name = argument.attribute("name");
arg.type = argument.attribute("type");
func.arguments.push_back(arg);
}
g_FunctionDB[func.name] = func;
}
}
}
}
}
namespace v1 {
bool debuggeeIs32Bit() {
return pointer_size() == sizeof(std::uint32_t);
}
bool debuggeeIs64Bit() {
return pointer_size() == sizeof(std::uint64_t);
}
/**
* @brief
*/
void set_cpu_selected_address(address_t address) {
ui()->cpuView_->setSelectedAddress(address);
ui()->cpuView_->update();
}
/**
* @brief
*/
address_t cpu_selected_address() {
return ui()->cpuView_->selectedAddress();
}
/**
* @brief
*/
std::shared_ptr<IRegion> current_cpu_view_region() {
return ui()->cpuView_->region();
}
/**
* @brief
*/
void repaint_cpu_view() {
ui()->cpuView_->update();
}
/**
* @brief
*/
ISymbolManager &symbol_manager() {
static SymbolManager symbolManager;
return symbolManager;
}
/**
* @brief
*/
MemoryRegions &memory_regions() {
static MemoryRegions memoryRegions;
return memoryRegions;
}
/**
* @brief
*/
ArchProcessor &arch_processor() {
static ArchProcessor archProcessor;
return archProcessor;
}
/**
* @brief
*/
IAnalyzer *set_analyzer(IAnalyzer *p) {
Q_ASSERT(p);
return g_Analyzer.fetchAndStoreAcquire(p);
}
/**
* @brief Returns the current active analyzer instance.
*
* @return A pointer to the current active analyzer instance, or nullptr if no analyzer is set.
*/
IAnalyzer *analyzer() {
return g_Analyzer.loadRelaxed();
}
/**
* @brief Executes all registered debug event handlers for the given debug event.
*
* @param e The debug event to execute handlers for.
* @return The status of the event execution.
*/
EventStatus execute_debug_event_handlers(const std::shared_ptr<IDebugEvent> &e) {
return g_DebugEventHandlers.execute(e);
}
/**
* @brief Adds a debug event handler to the list of registered handlers.
*
* @param p The debug event handler to add.
*/
void add_debug_event_handler(IDebugEventHandler *p) {
g_DebugEventHandlers.add(p);
}
/**
* @brief
*/
void remove_debug_event_handler(IDebugEventHandler *p) {
g_DebugEventHandlers.remove(p);
}
/**
* @brief Jumps to a specific address in the debugger UI.
*
* @param address The address to jump to.
* @return true if the jump was successful, false otherwise.
*/
bool jump_to_address(address_t address) {
Debugger *const gui = ui();
Q_ASSERT(gui);
return gui->jumpToAddress(address);
}
/**
* @brief Shows a given address through a given end address in the data view, optionally in a new tab.
*
* @param address The starting address to dump.
* @param end_address The ending address to dump.
* @param new_tab If true, opens the dump in a new tab; otherwise, uses the current tab.
* @return true if the dump was successful, false otherwise.
*/
bool dump_data_range(address_t address, address_t end_address, bool new_tab) {
Debugger *const gui = ui();
Q_ASSERT(gui);
return gui->dumpDataRange(address, end_address, new_tab);
}
/**
* @brief Shows a given address through a given end address in the data view.
*
* @param address The starting address to dump.
* @param end_address The ending address to dump.
* @return true if the dump was successful, false otherwise.
*/
bool dump_data_range(address_t address, address_t end_address) {
return dump_data_range(address, end_address, false);
}
/**
* @brief Shows a given address in the stack view.
*
* @param address The address to dump.
* @return true if the dump was successful, false otherwise.
*/
bool dump_stack(address_t address) {
return dump_stack(address, true);
}
/**
* @brief Shows a given address in the stack view, optionally scrolling to it.
*
* @param address The address to dump.
* @param scroll_to If true, scrolls to the address in the stack view; otherwise, does not scroll.
* @return true if the dump was successful, false otherwise.
*/
bool dump_stack(address_t address, bool scroll_to) {
Debugger *const gui = ui();
Q_ASSERT(gui);
return gui->dumpStack(address, scroll_to);
}
/**
* @brief Shows a given address in the data view, optionally in a new tab.
*
* @param address The address to dump.
* @param new_tab If true, opens the dump in a new tab; otherwise, uses the current tab.
* @return true if the dump was successful, false otherwise.
*/
bool dump_data(address_t address, bool new_tab) {
Debugger *const gui = ui();
Q_ASSERT(gui);
return gui->dumpData(address, new_tab);
}
/**
* @brief Shows a given address in the data view.
*
* @param address The address to dump.
* @return true if the dump was successful, false otherwise.
*/
bool dump_data(address_t address) {
return dump_data(address, false);
}
/**
* @brief Sets the condition for a breakpoint at a given address.
*
* @param address The address of the breakpoint.
* @param condition The condition to set.
*/
void set_breakpoint_condition(address_t address, const QString &condition) {
if (std::shared_ptr<IBreakpoint> bp = find_breakpoint(address)) {
bp->condition = condition;
}
}
/**
* @brief Returns the condition of a breakpoint at a given address.
*
* @param address The address of the breakpoint.
* @return The condition of the breakpoint, or an empty string if no breakpoint exists at the address.
*/
QString get_breakpoint_condition(address_t address) {
QString ret;
if (std::shared_ptr<IBreakpoint> bp = find_breakpoint(address)) {
ret = bp->condition;
}
return ret;
}
/**
* @brief Creates a breakpoint at the specified address, with user confirmation if necessary.
*
* @param address The address at which to create the breakpoint.
* @return A shared pointer to the created breakpoint, or nullptr if the breakpoint was not created
*/
std::shared_ptr<IBreakpoint> create_breakpoint(address_t address) {
std::shared_ptr<IBreakpoint> bp;
memory_regions().sync();
if (std::shared_ptr<IRegion> region = memory_regions().findRegion(address)) {
int ret = QMessageBox::Yes;
if (!region->executable() && config().warn_on_no_exec_bp) {
ret = QMessageBox::question(
nullptr,
tr("Suspicious breakpoint"),
tr("You want to place a breakpoint in a non-executable region.\n"
"An INT3 breakpoint set on data will not execute and may cause incorrect results or crashes.\n"
"Do you really want to set a breakpoint here?"),
QMessageBox::Yes | QMessageBox::No);
} else {
uint8_t buffer[Instruction::MaxSize + 1];
if (const int size = get_instruction_bytes(address, buffer)) {
Instruction inst(buffer, buffer + size, address);
if (!inst) {
ret = QMessageBox::question(
nullptr,
tr("Suspicious breakpoint"),
tr("It looks like you may be setting an INT3 breakpoint on data.\n"
"An INT3 breakpoint set on data will not execute and may cause incorrect results or crashes.\n"
"Do you really want to set a breakpoint here?"),
QMessageBox::Yes | QMessageBox::No);
}
}
}
if (ret == QMessageBox::Yes) {
bp = debugger_core->addBreakpoint(address);
if (!bp) {
QMessageBox::critical(
nullptr,
tr("Error Setting Breakpoint"),
tr("Failed to set breakpoint at address %1").arg(address.toPointerString()));
return bp;
}
repaint_cpu_view();
}
} else {
QMessageBox::critical(
nullptr,
tr("Error Setting Breakpoint"),
tr("Sorry, but setting a breakpoint which is not in a valid region is not allowed."));
}
return bp;
}
/**
* @brief Enables a breakpoint at the specified address.
*
* @param address The address of the breakpoint to enable.
* @return The address of the enabled breakpoint, or 0 if the breakpoint could not be enabled.
*/
address_t enable_breakpoint(address_t address) {
if (address != 0) {
std::shared_ptr<IBreakpoint> bp = find_breakpoint(address);
if (bp && bp->enable()) {
return address;
}
}
return 0;
}
/**
* @brief Disables a breakpoint at the specified address.
*
* @param address The address of the breakpoint to disable.
* @return The address of the disabled breakpoint, or 0 if the breakpoint was not found
*/
address_t disable_breakpoint(address_t address) {
if (address != 0) {
std::shared_ptr<IBreakpoint> bp = find_breakpoint(address);
if (bp && bp->disable()) {
return address;
}
}
return 0;
}
/**
* @brief Toggles the existence of a breakpoint at a given address.
*
* @param address The address at which to toggle the breakpoint.
*/
void toggle_breakpoint(address_t address) {
if (find_breakpoint(address)) {
remove_breakpoint(address);
} else {
create_breakpoint(address);
}
}
/**
* @brief Removes a breakpoint at the specified address.
*
* @param address The address of the breakpoint to remove.
*/
void remove_breakpoint(address_t address) {
debugger_core->removeBreakpoint(address);
repaint_cpu_view();
}
/**
* @brief Evaluates an expression and returns the result.
*
* @param expression The expression to evaluate.
* @param value A pointer to the variable where the result will be stored.
* @return true if the expression was evaluated successfully, false otherwise.
*/
bool eval_expression(const QString &expression, address_t *value) {
Q_ASSERT(value);
Expression<address_t> expr(expression, get_variable, get_value);
const Result<edb::address_t, ExpressionError> address = expr.evaluate();
if (!address) {
QMessageBox::critical(debugger_ui, tr("Error In Expression!"), address.error().what());
return false;
}
*value = *address;
return true;
}
/**
* @brief Gets an expression from the user and evaluates it.
*
* @param title The title of the input dialog.
* @param prompt The prompt for the input dialog.
* @param value A pointer to the variable where the result will be stored.
* @return true if the expression was evaluated successfully, false otherwise.
*/
bool get_expression_from_user(const QString &title, const QString &prompt, address_t *value) {
bool retval = false;
auto inputDialog = std::make_unique<ExpressionDialog>(title, prompt, edb::v1::debugger_ui);
if (inputDialog->exec()) {
*value = inputDialog->getAddress();
retval = true;
}
return retval;
}
/**
* @brief Gets a value from the user.
*
* @param value A reference to the register where the value will be stored.
* @return true if the value was obtained successfully, false otherwise.
*/
bool get_value_from_user(Register &value) {
return get_value_from_user(value, tr("Input Value"));
}
/**
* @brief Gets a value from the user with a custom title.
*
* @param value A reference to the register where the value will be stored.
* @param title The title of the input dialog.
* @return true if the value was obtained successfully, false otherwise.
*/
bool get_value_from_user(Register &value, const QString &title) {
static auto dlg = new DialogInputValue(debugger_ui);
bool ret = false;
dlg->setWindowTitle(title);
dlg->setValue(value);
if (dlg->exec() == QDialog::Accepted) {
value.setScalarValue(dlg->value());
ret = true;
}
return ret;
}
/**
* @brief Gets a binary string from the user.
*
* @param value A reference to the QByteArray where the value will be stored.
* @param title The title of the input dialog.
* @param max_length The maximum length of the binary string.
* @return true if the value was obtained successfully, false otherwise.
*/
bool get_binary_string_from_user(QByteArray &value, const QString &title, int max_length) {
static auto dlg = new DialogInputBinaryString(debugger_ui);
bool ret = false;
dlg->setWindowTitle(title);
BinaryString *const bs = dlg->binaryString();
// set the max length BEFORE the value! (or else we truncate incorrectly)
if (value.length() <= max_length) {
bs->setMaxLength(max_length);
}
bs->setValue(value);
if (dlg->exec() == QDialog::Accepted) {
value = bs->value();
ret = true;
}
return ret;
}
/**
* @brief Returns a pointer to the options dialog, creating it if it doesn't exist.
*
* @return A pointer to the options dialog.
*/
QPointer<QDialog> dialog_options() {
static QPointer<QDialog> dialog = new DialogOptions(debugger_ui);
return dialog;
}
/**
* @brief Returns a reference to the global configuration object.
*
* @return A reference to the global configuration object.
*/
Configuration &config() {
static Configuration g_Configuration;
return g_Configuration;
}
/**
* @brief Attempts to create a summary of the content at the specified address, suitable for display in a user interface.
*
* @param address The address to examine.
* @param s A reference to a QString where the summary will be stored.
* @return true if a human-readable string was found at the address, false otherwise.
*
* @note Strings are comprised of printable characters and whitespace.
* @note The found_length parameter is needed because characters that require an escape
* sequence may result in a longer resultant string. The found_length represents the
* original length of the string.
*/
bool get_human_string_at_address(address_t address, QString &s) {
bool ret = false;
if (address > 0x10000ULL) { // FIXME use page size
QString string_param;
int string_length;
if (get_ascii_string_at_address(address, string_param, edb::v1::config().min_string_length, 256, string_length)) {
ret = true;
s.append(
QStringLiteral("ASCII \"%1\" ").arg(string_param));
} else if (get_utf16_string_at_address(address, string_param, edb::v1::config().min_string_length, 256, string_length)) {
ret = true;
s.append(
QStringLiteral("UTF16 \"%1\" ").arg(string_param));
}
}
return ret;
}
/**
* @brief Attempts to get an ASCII string at a given address whose length is >= min_length and < max_length.
*
* @param address The address to read the string from.
* @param s A reference to a QString where the found string will be stored.
* @param min_length The minimum length of the string to be considered valid.
* @param max_length The maximum length of the string to be considered valid.
* @param found_length A reference to an integer where the original length of the found string will be stored.
* @return true if a valid ASCII string was found, false otherwise.
*
* @note Strings are comprised of printable characters and whitespace.
* @note The found_length parameter is needed because characters that require an escape
* sequence may result in a longer resultant string. The found_length represents the
* original length of the string.
*/
bool get_ascii_string_at_address(address_t address, QString &s, int min_length, int max_length, int &found_length) {
bool is_string = false;
if (debugger_core) {
if (IProcess *process = debugger_core->process()) {
s.clear();
if (min_length <= max_length) {
while (max_length--) {
char ch;
if (!process->readBytes(address++, &ch, sizeof(ch))) {
break;
}
const int ascii_char = static_cast<unsigned char>(ch);
if (ascii_char < 0x80 && (std::isprint(ascii_char) || std::isspace(ascii_char))) {
s += ch;
} else {
break;
}
}
}
is_string = s.length() >= min_length;
if (is_string) {
found_length = static_cast<int>(s.length());
s.replace("\r", "\\r");
s.replace("\n", "\\n");
s.replace("\t", "\\t");
s.replace("\v", "\\v");
s.replace("\"", "\\\"");
}
}
}
return is_string;
}
/**
* @brief Attempts to get a UTF-16 string at a given address whose length is >= min_length and < max_length.
*
* @param address The address to read the string from.
* @param s A reference to a QString where the found string will be stored.
* @param min_length The minimum length of the string to be considered valid.
* @param max_length The maximum length of the string to be considered valid.
* @param found_length A reference to an integer where the original length of the found string will be stored.
* @return true if a valid UTF-16 string was found, false otherwise.
*
* @note Strings are comprised of printable characters and whitespace.
* @note The found_length parameter is needed because characters that require an escape
* sequence may result in a longer resultant string. The found_length represents the
* original length of the string.
*/
bool get_utf16_string_at_address(address_t address, QString &s, int min_length, int max_length, int &found_length) {
bool is_string = false;
if (debugger_core) {
if (IProcess *process = debugger_core->process()) {
s.clear();
if (min_length <= max_length) {
while (max_length--) {
uint16_t val;
if (!process->readBytes(address, &val, sizeof(val))) {
break;
}
address += sizeof(val);
QChar ch(val);
// for now, we only acknowledge ASCII chars encoded as unicode
const int ascii_char = ch.toLatin1();
if (ascii_char >= 0x20 && ascii_char < 0x80) {
s += ch;
} else {
break;
}
}
}
is_string = s.length() >= min_length;
if (is_string) {
found_length = static_cast<int>(s.length());
s.replace("\r", "\\r");
s.replace("\n", "\\n");
s.replace("\t", "\\t");
s.replace("\v", "\\v");
s.replace("\"", "\\\"");
}
}
}
return is_string;
}
/**
* @brief
*/
QString find_function_symbol(address_t address, const QString &default_value, int *offset) {
QString symname(default_value);
int off;
if (function_symbol_base(address, &symname, &off)) {
if (config().function_offsets_in_hex) {
symname = QStringLiteral("%1+0x%2").arg(symname).arg(off, 0, 16);
} else {
symname = QStringLiteral("%1+%2").arg(symname).arg(off, 0, 10);
}
if (offset) {
*offset = off;
}
}
return symname;
}
/**
* @brief
*/
QString find_function_symbol(address_t address, const QString &default_value) {
return find_function_symbol(address, default_value, nullptr);
}
/**
* @brief
*/
QString find_function_symbol(address_t address) {
return find_function_symbol(address, QString(), nullptr);
}
/**
* @brief
*/
address_t get_variable(const QString &s, bool *ok, ExpressionError *err) {
Q_ASSERT(debugger_core);
Q_ASSERT(ok);
Q_ASSERT(err);
if (IProcess *process = debugger_core->process()) {
if (std::shared_ptr<IThread> thread = process->currentThread()) {
State state;
thread->getState(&state);
const Register reg = state.value(s);
*ok = reg.valid();
if (!*ok) {
if (const std::optional<Symbol> sym = edb::v1::symbol_manager().find(s)) {
*ok = true;
return sym->address;
}
*err = ExpressionError(ExpressionError::UnknownVariable);
return 0;
}
// FIXME: should this really return segment base, not selector?
// FIXME: if it's really meant to return base, then need to check whether
// State::operator[]() returned valid Register
if (reg.name() == "fs") {
return state["fs_base"].valueAsAddress();
}
if (reg.name() == "gs") {
return state["gs_base"].valueAsAddress();
}
if (reg.bitSize() > 8 * sizeof(edb::address_t)) {
*err = ExpressionError(ExpressionError::UnknownVariable);
return 0;
}
return reg.valueAsAddress();
}
}
*err = ExpressionError(ExpressionError::UnknownVariable);
return 0;
}
/**
* @brief
*/
address_t get_value(address_t address, bool *ok, ExpressionError *err) {
Q_ASSERT(debugger_core);
Q_ASSERT(ok);
Q_ASSERT(err);
address_t ret = 0;
*ok = false;
if (IProcess *process = edb::v1::debugger_core->process()) {
*ok = process->readBytes(address, &ret, pointer_size());
if (!*ok) {
*err = ExpressionError(ExpressionError::CannotReadMemory);
}
}
return ret;
}
/**
* @brief Attempts to read instruction bytes from a given address into a buffer.
*
* @param address The address to read the instruction bytes from.
* @param buf A pointer to the buffer where the instruction bytes will be stored.
* @param size A pointer to an integer representing the maximum number of bytes to read.
* On success, this will be updated to the actual number of bytes read.
* @return true if instruction bytes were successfully read, false otherwise.
*/
bool get_instruction_bytes(address_t address, uint8_t *buf, int *size) {
Q_ASSERT(debugger_core);
Q_ASSERT(size);
Q_ASSERT(*size >= 0);
if (IProcess *process = edb::v1::debugger_core->process()) {
*size = static_cast<int>(process->readBytes(address, buf, static_cast<size_t>(*size)));
if (*size) {
return true;
}
}
return false;
}
/**
* @brief Attempts to read instruction bytes from a given address into a buffer.
*
* @param address The address to read the instruction bytes from.
* @param buf A pointer to the buffer where the instruction bytes will be stored.
* @param size A pointer to an integer representing the maximum number of bytes to read.
* On success, this will be updated to the actual number of bytes read.
* @return true if instruction bytes were successfully read, false otherwise.
*/
bool get_instruction_bytes(address_t address, uint8_t *buf, size_t *size) {
Q_ASSERT(debugger_core);
Q_ASSERT(size);
if (IProcess *process = edb::v1::debugger_core->process()) {
*size = process->readBytes(address, buf, *size);
if (*size) {
return true;
}
}
return false;
}
/**
* @brief Gets an object which knows how to analyze the binary file provided.
*
* @param region The memory region representing the binary file to analyze.
* @return A unique pointer to an IBinary object that can analyze the binary file, or nullptr if no suitable analyzer was found.
*/
std::unique_ptr<IBinary> get_binary_info(const std::shared_ptr<IRegion> ®ion) {
const auto binaryInfoList = g_BinaryInfoList;
for (IBinary::create_func_ptr_t f : binaryInfoList) {
try {
std::unique_ptr<IBinary> p((*f)(region));
// reorder the list to put this successful plugin
// in front.
if (g_BinaryInfoList[0] != f) {
g_BinaryInfoList.removeOne(f);
g_BinaryInfoList.push_front(f);
}
return p;
} catch (const std::exception &) {
// let's just ignore it...
}
}
#if 0
qDebug() << "Failed to find any binary parser for region"
<< QString::number(region->start(), 16);
#endif
return nullptr;
}
/**
* @brief Locates the main function in the debugged process.
*
* @return The address of the main function, or 0 if not found.
*
* @note This function currently only works for glibc linked ELF files.
*/
address_t locate_main_function() {
if (debugger_core) {
if (IProcess *process = debugger_core->process()) {
const address_t main_func = process->calculateMain();
if (main_func != 0) {
return main_func;
}
return process->entryPoint();
}
}
return 0;
}
/**
* @brief Returns a reference to the global list of loaded plugins.
*
* @return A QMap containing the loaded plugins, where the key is the plugin filename and the value is a pointer to the plugin object.
*/
const QMap<QString, QObject *> &plugin_list() {
return g_GeneralPlugins;
}
/**
* @brief Finds a plugin by its class name.
*
* @param name The class name of the plugin to find.
* @return A pointer to the IPlugin interface of the found plugin, or nullptr if no plugin with the specified class name was found.
*/
IPlugin *find_plugin_by_name(const QString &name) {
for (QObject *p : g_GeneralPlugins) {
if (name == p->metaObject()->className()) {
return qobject_cast<IPlugin *>(p);
}
}
return nullptr;
}
/**
* @brief Reloads the symbol information.
*/
void reload_symbols() {
symbol_manager().clear();
}
/**
* @brief Gets information about a function.
*
* @param function The name of the function.
* @return A pointer to the function's prototype, or nullptr if not found.
*/
const Prototype *get_function_info(const QString &function) {
auto it = g_FunctionDB.find(function);
if (it != g_FunctionDB.end()) {
return &(it.value());
}
return nullptr;