Enable HW BPs with context

This commit is contained in:
momo5502 2022-09-13 20:42:06 +02:00
parent 4a1044bab0
commit febfc1b5e1
2 changed files with 40 additions and 17 deletions

View File

@ -65,16 +65,21 @@ namespace utils::hardware_breakpoint
return &this->context_; return &this->context_;
} }
operator CONTEXT&()
{
return this->context_;
}
private: private:
thread::handle handle_; thread::handle handle_;
CONTEXT context_{}; CONTEXT context_{};
}; };
uint32_t find_free_index(debug_context& context) uint32_t find_free_index(const CONTEXT& context)
{ {
for (uint32_t i = 0; i < 4; ++i) for (uint32_t i = 0; i < 4; ++i)
{ {
if ((context->Dr7 & (1ull << (i << 1ull))) == 0) if ((context.Dr7 & (1ull << (i << 1ull))) == 0)
{ {
return i; return i;
} }
@ -84,38 +89,51 @@ namespace utils::hardware_breakpoint
} }
} }
uint32_t activate(const uint64_t address, uint32_t length, const condition cond, CONTEXT& context)
{
const auto index = find_free_index(context);
length = translate_length(length);
(&context.Dr0)[index] = address;
set_bits(context.Dr7, 16 + (index << 2ull), 2, cond);
set_bits(context.Dr7, 18 + (index << 2ull), 2, length);
set_bits(context.Dr7, index << 1ull, 1, 1);
return index;
}
uint32_t activate(void* address, const uint32_t length, const condition cond, const uint32_t thread_id) uint32_t activate(void* address, const uint32_t length, const condition cond, const uint32_t thread_id)
{ {
return activate(reinterpret_cast<uint64_t>(address), length, cond, thread_id); return activate(reinterpret_cast<uint64_t>(address), length, cond, thread_id);
} }
uint32_t activate(const uint64_t address, uint32_t length, const condition cond, const uint32_t thread_id) uint32_t activate(const uint64_t address, const uint32_t length, const condition cond, const uint32_t thread_id)
{ {
debug_context context(thread_id); debug_context context(thread_id);
return activate(address, length, cond, context);
}
const auto index = find_free_index(context); void deactivate(const uint32_t index, CONTEXT& context)
length = translate_length(length); {
validate_index(index);
(&context->Dr0)[index] = address; set_bits(context.Dr7, index << 1ull, 1, 0);
set_bits(context->Dr7, 16 + (index << 2ull), 2, cond);
set_bits(context->Dr7, 18 + (index << 2ull), 2, length);
set_bits(context->Dr7, index << 1ull, 1, 1);
return index;
} }
void deactivate(const uint32_t index, const uint32_t thread_id) void deactivate(const uint32_t index, const uint32_t thread_id)
{ {
validate_index(index);
debug_context context(thread_id); debug_context context(thread_id);
set_bits(context->Dr7, index << 1ull, 1, 0); deactivate(index, context);
}
void deactivate_all(CONTEXT& context)
{
context.Dr7 = 0;
} }
void deactivate_all(const uint32_t thread_id) void deactivate_all(const uint32_t thread_id)
{ {
debug_context context(thread_id); debug_context context(thread_id);
context->Dr7 = 0; deactivate_all(context);
} }
} }

View File

@ -11,8 +11,13 @@ namespace utils::hardware_breakpoint
read_write = 3 read_write = 3
}; };
uint32_t activate(uint64_t address, uint32_t length, condition cond, CONTEXT& context);
uint32_t activate(void* address, uint32_t length, condition cond, uint32_t thread_id = GetCurrentThreadId()); uint32_t activate(void* address, uint32_t length, condition cond, uint32_t thread_id = GetCurrentThreadId());
uint32_t activate(uint64_t address, uint32_t length, condition cond, uint32_t thread_id = GetCurrentThreadId()); uint32_t activate(uint64_t address, uint32_t length, condition cond, uint32_t thread_id = GetCurrentThreadId());
void deactivate(uint32_t index, CONTEXT& context);
void deactivate(uint32_t index, uint32_t thread_id = GetCurrentThreadId()); void deactivate(uint32_t index, uint32_t thread_id = GetCurrentThreadId());
void deactivate_all(CONTEXT& context);
void deactivate_all(uint32_t thread_id = GetCurrentThreadId()); void deactivate_all(uint32_t thread_id = GetCurrentThreadId());
} }