-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathdebuggerstate.cpp
More file actions
1849 lines (1444 loc) · 44.8 KB
/
Copy pathdebuggerstate.cpp
File metadata and controls
1849 lines (1444 loc) · 44.8 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 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 <chrono>
#include <inttypes.h>
#include <thread>
#include <utility>
#include <filesystem>
#include "lowlevelilinstruction.h"
#include "mediumlevelilinstruction.h"
#include "highlevelilinstruction.h"
#include "debuggerexceptions.h"
#include "debuggerstate.h"
#include "debugadapter.h"
#include "debuggercontroller.h"
using namespace BinaryNinja;
using namespace std;
using namespace BinaryNinjaDebugger;
DebuggerRegisters::DebuggerRegisters(DebuggerState* state) : m_state(state)
{
MarkDirty();
}
void DebuggerRegisters::MarkDirty()
{
std::unique_lock lock(m_registersMutex);
m_dirty = true;
m_registerCache.clear();
}
void DebuggerRegisters::Update()
{
DebugAdapter* adapter = m_state->GetAdapter();
if (!adapter)
return;
if (!m_state->IsConnected())
return;
std::unique_lock lock(m_registersMutex);
m_registerCache = adapter->ReadAllRegisters();
m_dirty = false;
}
intx::uint512 DebuggerRegisters::GetRegisterValue(const std::string& name)
{
auto cachedRegs = GetCachedRegisters();
auto iter = cachedRegs.find(name);
if (iter == cachedRegs.end())
return 0x0;
return iter->second.m_value;
}
bool DebuggerRegisters::SetRegisterValue(const std::string& name, intx::uint512 value)
{
DebugAdapter* adapter = m_state->GetAdapter();
if (!adapter)
return false;
auto cachedRegs = GetCachedRegisters();
auto iter = cachedRegs.find(name);
if (iter == cachedRegs.end())
return false;
bool ok = adapter->WriteRegister(name, value);
if (!ok)
return false;
// Because some registers are correlated, changing the value of one register could invalidate the value of other
// registers as well.
MarkDirty();
m_state->GetController()->NotifyEvent(RegisterChangedEvent);
return true;
}
std::vector<DebugRegister> DebuggerRegisters::GetAllRegisters()
{
auto cachedRegs = GetCachedRegisters();
std::vector<DebugRegister> result {};
for (auto& [reg_name, reg] : cachedRegs)
result.push_back(reg);
std::sort(result.begin(), result.end(), [](const DebugRegister& lhs, const DebugRegister& rhs) {
return lhs.m_registerIndex < rhs.m_registerIndex;
});
// TODO: maybe we should not hold a m_state at all; instead we just hold a m_controller
auto controller = m_state->GetController();
if (!controller->GetState()->IsConnected())
return result;
std::map<intx::uint512, std::string> regHints;
for (auto& reg : result)
{
auto it = regHints.find(reg.m_value);
if (it != regHints.end())
{
reg.m_hint = it->second;
}
else
{
// TODO: create a new GetRegisterHint method that calls GetAddressInformation
const std::string hint = controller->GetAddressInformation(reg.m_value);
regHints[reg.m_value] = hint;
reg.m_hint = hint;
}
}
return result;
}
std::unordered_map<std::string, DebugRegister> DebuggerRegisters::GetCachedRegisters()
{
std::unique_lock lock(m_registersMutex);
if (IsDirty())
Update();
return m_registerCache;
}
DebuggerThreads::DebuggerThreads(DebuggerState* state) : m_state(state)
{
MarkDirty();
}
void DebuggerThreads::MarkDirty()
{
std::unique_lock lock(m_threadsMutex);
m_dirty = true;
// clearing these here corrupts thread state updating in ::Update() below
// m_threads.clear();
// m_frames.clear();
// TODO: consider also caching the last active thread
}
void DebuggerThreads::SymbolizeFrames(std::vector<DebugFrame>& frames)
{
if (!m_state || !m_state->GetController())
return;
auto data = m_state->GetController()->GetData();
if (!data)
return;
for (DebugFrame& frame: frames)
{
// Try to find a better symbol than the one provided by the debugger backend
auto funcs = data->GetAnalysisFunctionsContainingAddress(frame.m_pc);
if (!funcs.empty())
{
auto func = funcs[0];
if (!func)
continue;
if (func->GetStart() != frame.m_functionStart)
{
// Found a better function start from the analysis, use it
frame.m_functionStart = func->GetStart();
auto symbol = func->GetSymbol();
if (symbol)
frame.m_functionName = symbol->GetShortName();
else
frame.m_functionName = fmt::format("sub_{:x}", func->GetStart());
}
else
{
std::string symName;
auto symbol = func->GetSymbol();
if (symbol)
symName = symbol->GetShortName();
auto defaultName = fmt::format("sub_{:x}", func->GetStart());
if (frame.m_functionName.empty())
{
if (!symName.empty())
frame.m_functionName = symName;
else
frame.m_functionName = defaultName;
}
else
{
if ((!symName.empty()) && symName != defaultName)
frame.m_functionName = symName;
}
}
continue;
}
}
}
void DebuggerThreads::Update()
{
if (!m_state)
return;
if (!m_state->IsConnected())
return;
DebugAdapter* adapter = m_state->GetAdapter();
if (!adapter)
return;
std::unique_lock lock(m_threadsMutex);
m_frames.clear();
std::vector<DebugThread> newThreads = adapter->GetThreadList();
for (auto thread = newThreads.begin(); thread != newThreads.end(); thread++)
{
auto frames = adapter->GetFramesOfThread(thread->m_tid);
SymbolizeFrames(frames);
m_frames[thread->m_tid] = frames;
// update thread states in new thread list
auto oldThread = std::find_if(m_threads.begin(), m_threads.end(), [&](DebugThread const& t) {
return t.m_tid == thread->m_tid;
});
if (oldThread != m_threads.end() && thread->m_isFrozen != oldThread->m_isFrozen)
thread->m_isFrozen = oldThread->m_isFrozen;
}
m_threads.clear();
m_threads = newThreads;
m_dirty = false;
}
DebugThread DebuggerThreads::GetActiveThread() const
{
if (!m_state)
return DebugThread {};
if (!m_state->IsConnected())
return DebugThread {};
DebugAdapter* adapter = m_state->GetAdapter();
if (!adapter)
return DebugThread {};
return adapter->GetActiveThread();
}
bool DebuggerThreads::SetActiveThread(const DebugThread& thread)
{
if (!m_state)
return false;
if (!m_state->IsConnected())
return false;
DebugAdapter* adapter = m_state->GetAdapter();
if (!adapter)
return false;
return adapter->SetActiveThread(thread);
}
std::vector<DebugThread> DebuggerThreads::GetAllThreads()
{
std::unique_lock lock(m_threadsMutex);
if (IsDirty())
Update();
return m_threads;
}
std::map<uint32_t, std::vector<DebugFrame>> DebuggerThreads::GetAllFrames()
{
std::unique_lock lock(m_threadsMutex);
if (IsDirty())
Update();
return m_frames;
}
std::vector<DebugFrame> DebuggerThreads::GetFramesOfThread(uint32_t tid)
{
auto frame = GetAllFrames();
auto iter = frame.find(tid);
if (iter != frame.end())
return iter->second;
return {};
}
bool DebuggerThreads::SuspendThread(std::uint32_t tid)
{
if (!m_state)
return false;
DebugAdapter* adapter = m_state->GetAdapter();
if (!adapter)
return false;
auto threads = GetAllThreads();
auto thread = std::find_if(threads.begin(), threads.end(), [&](DebugThread const& t) {
return t.m_tid == tid;
});
if (thread == threads.end())
return false;
if (thread->m_isFrozen)
return true;
auto result = adapter->SuspendThread(tid);
if (!result)
return false;
thread->m_isFrozen = true;
return true;
}
bool DebuggerThreads::ResumeThread(std::uint32_t tid)
{
if (!m_state)
return false;
DebugAdapter* adapter = m_state->GetAdapter();
if (!adapter)
return false;
auto threads = GetAllThreads();
auto thread = std::find_if(threads.begin(), threads.end(), [&](DebugThread const& t) {
return t.m_tid == tid;
});
if (thread == threads.end())
return false;
if (!thread->m_isFrozen)
return true;
auto result = adapter->ResumeThread(tid);
if (!result)
return false;
thread->m_isFrozen = false;
return true;
}
DebuggerModules::DebuggerModules(DebuggerState* state) : m_state(state)
{
MarkDirty();
}
void DebuggerModules::MarkDirty()
{
std::unique_lock lock(m_modulesMutex);
m_dirty = true;
m_modules.clear();
}
void DebuggerModules::Update()
{
DebugAdapter* adapter = m_state->GetAdapter();
if (!adapter)
return;
if (!m_state->IsConnected())
return;
std::unique_lock lock(m_modulesMutex);
m_modules = adapter->GetModuleList();
m_dirty = false;
}
bool DebuggerModules::GetModuleBase(const std::string& name, uint64_t& address)
{
if (name.empty())
return false;
for (const DebugModule& module : GetAllModules())
{
if (module.IsSameBaseModule(name))
{
address = module.m_address;
return true;
}
}
return false;
}
DebugModule DebuggerModules::GetModuleByName(const std::string& name)
{
for (const DebugModule& module : GetAllModules())
{
if (module.IsSameBaseModule(name))
return module;
}
return DebugModule();
}
DebugModule DebuggerModules::GetModuleForAddress(uint64_t remoteAddress)
{
// lldb does not properly return the size of a module, so we have to find the nearest module base that is smaller
// than the remoteAddress
uint64_t closestAddress = 0;
DebugModule result {};
for (const DebugModule& module : GetAllModules())
{
// This is slighlty different from the Python implementation, which finds the largest module start that is
// smaller than the remoteAddress.
// if ((module.m_address <= remoteAddress) && (remoteAddress < module.m_address + module.m_size))
// return module;
if ((module.m_address <= remoteAddress) && (module.m_address > closestAddress))
{
closestAddress = module.m_address;
result = module;
}
}
return result;
}
ModuleNameAndOffset DebuggerModules::AbsoluteAddressToRelative(uint64_t absoluteAddress)
{
DebugModule module = GetModuleForAddress(absoluteAddress);
uint64_t relativeAddress;
if (module.m_name != "")
{
relativeAddress = absoluteAddress - module.m_address;
}
else
{
relativeAddress = absoluteAddress;
}
return ModuleNameAndOffset(module.m_name, relativeAddress);
}
uint64_t DebuggerModules::RelativeAddressToAbsolute(const ModuleNameAndOffset& relativeAddress)
{
if (!relativeAddress.module.empty())
{
for (const DebugModule& module : GetAllModules())
{
if (module.IsSameBaseModule(relativeAddress.module))
{
return module.m_address + relativeAddress.offset;
}
}
if (DebugModule::IsSameBaseModule(m_state->GetController()->GetData()->GetFile()->GetOriginalFilename(),
relativeAddress.module))
{
return m_state->GetController()->GetViewFileSegmentsStart() + relativeAddress.offset;
}
}
return relativeAddress.offset;
}
std::vector<DebugModule> DebuggerModules::GetAllModules()
{
std::unique_lock lock(m_modulesMutex);
if (IsDirty())
Update();
return m_modules;
}
DebuggerBreakpoints::DebuggerBreakpoints(DebuggerState* state, std::vector<ModuleNameAndOffset> initial) :
m_state(state)
{
for (const auto& addr : initial)
m_breakpoints.push_back({addr, true, ""});
}
std::vector<BreakpointEntry>::iterator DebuggerBreakpoints::FindBreakpoint(const ModuleNameAndOffset& address)
{
if (m_state->GetAdapter())
{
const uint64_t targetAbsolute = m_state->GetModules()->RelativeAddressToAbsolute(address);
for (auto it = m_breakpoints.begin(); it != m_breakpoints.end(); ++it)
{
if (m_state->GetModules()->RelativeAddressToAbsolute(it->location) == targetAbsolute)
return it;
}
}
else
{
for (auto it = m_breakpoints.begin(); it != m_breakpoints.end(); ++it)
{
if (it->location == address)
return it;
}
}
return m_breakpoints.end();
}
std::vector<BreakpointEntry>::const_iterator DebuggerBreakpoints::FindBreakpoint(const ModuleNameAndOffset& address) const
{
return const_cast<DebuggerBreakpoints*>(this)->FindBreakpoint(address);
}
bool DebuggerBreakpoints::AddAbsolute(uint64_t remoteAddress)
{
if (!m_state->GetAdapter())
return false;
bool result = false;
if (m_state->IsConnected())
{
m_state->GetAdapter()->AddBreakpoint(remoteAddress);
result = true;
}
if (!ContainsAbsolute(remoteAddress))
{
ModuleNameAndOffset info = m_state->GetModules()->AbsoluteAddressToRelative(remoteAddress);
m_breakpoints.push_back({info, true, ""});
SerializeMetadata();
}
return result;
}
bool DebuggerBreakpoints::AddOffset(const ModuleNameAndOffset& address)
{
if (!ContainsOffset(address))
{
m_breakpoints.push_back({address, true, ""});
SerializeMetadata();
if (m_state->GetAdapter() && m_state->IsConnected())
m_state->GetAdapter()->AddBreakpoint(address);
return true;
}
return false;
}
bool DebuggerBreakpoints::RemoveAbsolute(uint64_t remoteAddress)
{
if (!m_state->GetAdapter())
return false;
ModuleNameAndOffset info = m_state->GetModules()->AbsoluteAddressToRelative(remoteAddress);
auto it = FindBreakpoint(info);
if (it == m_breakpoints.end())
return false;
m_breakpoints.erase(it);
SerializeMetadata();
m_state->GetAdapter()->RemoveBreakpoint(remoteAddress);
return true;
}
bool DebuggerBreakpoints::RemoveOffset(const ModuleNameAndOffset& address)
{
auto it = FindBreakpoint(address);
if (it == m_breakpoints.end())
return false;
ModuleNameAndOffset actualAddr = it->location;
m_breakpoints.erase(it);
SerializeMetadata();
if (m_state->GetAdapter() && m_state->IsConnected())
{
uint64_t remoteAddress = m_state->GetModules()->RelativeAddressToAbsolute(actualAddr);
m_state->GetAdapter()->RemoveBreakpoint(remoteAddress);
}
return true;
}
bool DebuggerBreakpoints::EnableAbsolute(uint64_t remoteAddress)
{
ModuleNameAndOffset info = m_state->GetModules()->AbsoluteAddressToRelative(remoteAddress);
return EnableOffset(info);
}
bool DebuggerBreakpoints::EnableOffset(const ModuleNameAndOffset& address)
{
auto it = FindBreakpoint(address);
if (it == m_breakpoints.end())
return false;
it->enabled = true;
SerializeMetadata();
if (m_state->GetAdapter() && m_state->IsConnected())
{
uint64_t remoteAddress = m_state->GetModules()->RelativeAddressToAbsolute(it->location);
m_state->GetAdapter()->AddBreakpoint(remoteAddress);
}
return true;
}
bool DebuggerBreakpoints::DisableAbsolute(uint64_t remoteAddress)
{
ModuleNameAndOffset info = m_state->GetModules()->AbsoluteAddressToRelative(remoteAddress);
return DisableOffset(info);
}
bool DebuggerBreakpoints::DisableOffset(const ModuleNameAndOffset& address)
{
auto it = FindBreakpoint(address);
if (it == m_breakpoints.end())
return false;
it->enabled = false;
SerializeMetadata();
if (m_state->GetAdapter() && m_state->IsConnected())
{
uint64_t remoteAddress = m_state->GetModules()->RelativeAddressToAbsolute(it->location);
m_state->GetAdapter()->RemoveBreakpoint(remoteAddress);
}
return true;
}
bool DebuggerBreakpoints::IsEnabledAbsolute(uint64_t address)
{
ModuleNameAndOffset info = m_state->GetModules()->AbsoluteAddressToRelative(address);
return IsEnabledOffset(info);
}
bool DebuggerBreakpoints::IsEnabledOffset(const ModuleNameAndOffset& address)
{
auto it = FindBreakpoint(address);
if (it == m_breakpoints.end())
return true; // Default to enabled if breakpoint not found
return it->enabled;
}
bool DebuggerBreakpoints::ContainsOffset(const ModuleNameAndOffset& address)
{
return FindBreakpoint(address) != m_breakpoints.end();
}
bool DebuggerBreakpoints::ContainsAbsolute(uint64_t address)
{
if (!m_state->GetAdapter())
return false;
for (const auto& bp : m_breakpoints)
{
if (m_state->GetModules()->RelativeAddressToAbsolute(bp.location) == address)
return true;
}
return false;
}
bool DebuggerBreakpoints::SetConditionAbsolute(const uint64_t remoteAddress, const std::string& condition)
{
const ModuleNameAndOffset info = m_state->GetModules()->AbsoluteAddressToRelative(remoteAddress);
return SetConditionOffset(info, condition);
}
bool DebuggerBreakpoints::SetConditionOffset(const ModuleNameAndOffset& address, const std::string& condition)
{
auto it = FindBreakpoint(address);
if (it == m_breakpoints.end())
return false;
it->condition = condition;
SerializeMetadata();
return true;
}
std::string DebuggerBreakpoints::GetConditionAbsolute(const uint64_t address)
{
for (const auto& bp : m_breakpoints)
{
if (m_state->GetModules()->RelativeAddressToAbsolute(bp.location) == address)
return bp.condition;
}
return "";
}
std::string DebuggerBreakpoints::GetConditionOffset(const ModuleNameAndOffset& address)
{
auto it = FindBreakpoint(address);
return it != m_breakpoints.end() ? it->condition : "";
}
bool DebuggerBreakpoints::HasConditionAbsolute(const uint64_t address)
{
for (const auto& bp : m_breakpoints)
{
if (m_state->GetModules()->RelativeAddressToAbsolute(bp.location) == address)
return !bp.condition.empty();
}
return false;
}
bool DebuggerBreakpoints::HasConditionOffset(const ModuleNameAndOffset& address)
{
auto it = FindBreakpoint(address);
return it != m_breakpoints.end() && !it->condition.empty();
}
bool DebuggerBreakpoints::AddHardwareBreakpoint(uint64_t address, DebugBreakpointType type, size_t size)
{
// TODO: ARCHITECTURAL ISSUE - This dual-path breakpoint system is problematic:
// - Software breakpoints have AddBreakpoint(ModuleNameAndOffset) that works before adapter creation
// - Hardware breakpoints only have AddHardwareBreakpoint(uint64_t) which requires absolute address
// This creates API asymmetry and prevents adding hardware breakpoints before target launch.
//
// Future refactoring options:
// 1. Add AddHardwareBreakpoint(ModuleNameAndOffset, type, size) overload for symmetry
// 2. Create unified BreakpointLocation struct that can represent both relative and absolute addressing
// 3. Merge AddBreakpoint and AddHardwareBreakpoint into single API with type parameter
if (ContainsHardwareBreakpoint(address, type, size))
return true; // Already exists
// If adapter is connected, try to add there first - only add to internal storage if successful
if (m_state->GetAdapter() && m_state->IsConnected())
{
bool adapterResult = m_state->GetAdapter()->AddHardwareBreakpoint(address, type, size);
if (!adapterResult)
return false; // Adapter failed, don't add to internal storage
}
// Add to internal storage (either adapter succeeded, or no adapter connected yet)
// Convert absolute address to module+offset for ASLR-safe storage (like AddAbsolute does for software breakpoints)
ModuleNameAndOffset info = m_state->GetModules()->AbsoluteAddressToRelative(address);
BreakpointEntry bp;
bp.location = info;
bp.enabled = true;
bp.type = type;
bp.size = size;
bp.address = address;
bp.isRelative = true;
m_breakpoints.push_back(bp);
SerializeMetadata();
return true;
}
bool DebuggerBreakpoints::RemoveHardwareBreakpoint(uint64_t address, DebugBreakpointType type, size_t size)
{
// Find and remove from our list - need to handle both relative and absolute breakpoints
for (auto iter = m_breakpoints.begin(); iter != m_breakpoints.end(); ++iter)
{
if (iter->IsHardware() && iter->type == type && iter->size == size)
{
bool matches = false;
if (iter->isRelative)
{
// Convert module+offset to absolute address and compare
uint64_t absolute = m_state->GetModules()->RelativeAddressToAbsolute(iter->location);
matches = (absolute == address);
}
else
{
matches = (iter->address == address);
}
if (matches)
{
m_breakpoints.erase(iter);
SerializeMetadata();
break;
}
}
}
// Remove from the adapter if connected
if (m_state->GetAdapter() && m_state->IsConnected())
{
return m_state->GetAdapter()->RemoveHardwareBreakpoint(address, type, size);
}
return true;
}
bool DebuggerBreakpoints::ContainsHardwareBreakpoint(uint64_t address, DebugBreakpointType type, size_t size)
{
// Similar to ContainsAbsolute, we need to handle both relative and absolute hardware breakpoints
// For relative hardware breakpoints, convert to absolute and compare
for (const BreakpointEntry& breakpoint : m_breakpoints)
{
if (breakpoint.IsHardware() && breakpoint.type == type && breakpoint.size == size)
{
if (breakpoint.isRelative)
{
// Convert module+offset to absolute address and compare
uint64_t absolute = m_state->GetModules()->RelativeAddressToAbsolute(breakpoint.location);
if (absolute == address)
return true;
}
else if (breakpoint.address == address)
{
return true;
}
}
}
return false;
}
bool DebuggerBreakpoints::EnableHardwareBreakpoint(uint64_t address, DebugBreakpointType type, size_t size)
{
if (!ContainsHardwareBreakpoint(address, type, size))
return false;
// Find and enable the hardware breakpoint - need to handle both relative and absolute breakpoints
for (auto& bp : m_breakpoints)
{
if (bp.IsHardware() && bp.type == type && bp.size == size)
{
bool matches = false;
if (bp.isRelative)
{
uint64_t absolute = m_state->GetModules()->RelativeAddressToAbsolute(bp.location);
matches = (absolute == address);
}
else
{
matches = (bp.address == address);
}
if (matches)
{
bp.enabled = true;
break;
}
}
}
SerializeMetadata();
// If connected, make sure the breakpoint is active in the target
if (m_state->GetAdapter() && m_state->IsConnected())
{
return m_state->GetAdapter()->AddHardwareBreakpoint(address, type, size);
}
return true;
}
bool DebuggerBreakpoints::DisableHardwareBreakpoint(uint64_t address, DebugBreakpointType type, size_t size)
{
if (!ContainsHardwareBreakpoint(address, type, size))
return false;
// Find and disable the hardware breakpoint - need to handle both relative and absolute breakpoints
for (auto& bp : m_breakpoints)
{
if (bp.IsHardware() && bp.type == type && bp.size == size)
{
bool matches = false;
if (bp.isRelative)
{
uint64_t absolute = m_state->GetModules()->RelativeAddressToAbsolute(bp.location);
matches = (absolute == address);
}
else
{
matches = (bp.address == address);
}
if (matches)
{
bp.enabled = false;
break;
}
}
}
SerializeMetadata();
// If connected, remove the breakpoint from the target but keep it in our list
if (m_state->GetAdapter() && m_state->IsConnected())
{
return m_state->GetAdapter()->RemoveHardwareBreakpoint(address, type, size);
}
return true;
}
// ========== Hardware Breakpoint Module+Offset Methods (ASLR-safe) ==========
bool DebuggerBreakpoints::AddHardwareBreakpoint(const ModuleNameAndOffset& location, DebugBreakpointType type, size_t size)
{
if (ContainsHardwareBreakpoint(location, type, size))
return true; // Already exists
// If adapter is connected, try to add there first - only add to internal storage if successful
if (m_state->GetAdapter() && m_state->IsConnected())
{
bool adapterResult = m_state->GetAdapter()->AddHardwareBreakpoint(location, type, size);
if (!adapterResult)
return false; // Adapter failed, don't add to internal storage
}
// Add to internal storage (either adapter succeeded, or no adapter connected yet)
BreakpointEntry bp;
bp.location = location;
bp.enabled = true;
bp.type = type;
bp.size = size;
bp.isRelative = true;
m_breakpoints.push_back(bp);
SerializeMetadata();
return true;
}
bool DebuggerBreakpoints::RemoveHardwareBreakpoint(const ModuleNameAndOffset& location, DebugBreakpointType type, size_t size)
{
// Remove from our list
auto iter = std::find_if(m_breakpoints.begin(), m_breakpoints.end(), [&](const BreakpointEntry& bp) {
return bp.IsHardware() && bp.location == location && bp.type == type && bp.size == size;
});
if (iter != m_breakpoints.end())
{
m_breakpoints.erase(iter);
SerializeMetadata();
}
// Remove from the adapter if connected
if (m_state->GetAdapter() && m_state->IsConnected())
{
// Call adapter with module+offset directly - adapter will handle resolution
return m_state->GetAdapter()->RemoveHardwareBreakpoint(location, type, size);
}
return true;
}