From b194d7b4e19e65dcbde017a1c7b2c889cf3f639e Mon Sep 17 00:00:00 2001 From: Ivan Fratric Date: Mon, 10 Jun 2024 19:45:58 +0200 Subject: [PATCH] Linux: Support instrumenting custom address range --- Linux/debugger.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Linux/debugger.cpp b/Linux/debugger.cpp index d8f0d2f..644f988 100644 --- a/Linux/debugger.cpp +++ b/Linux/debugger.cpp @@ -1480,9 +1480,11 @@ void Debugger::ExtractCodeRanges(void *module_base, size_t *code_size) { std::string elf_filename; - GetModuleFilename(module_base, &elf_filename); - if(elf_filename.empty()) FATAL("Error retrieving module path"); - ResolveSymlinks(&elf_filename); + if(module_base) { + GetModuleFilename(module_base, &elf_filename); + if(elf_filename.empty()) FATAL("Error retrieving module path"); + ResolveSymlinks(&elf_filename); + } *code_size = 0; @@ -1491,10 +1493,23 @@ void Debugger::ExtractCodeRanges(void *module_base, maps_parser.Parse(main_pid, map_entries); if(map_entries.empty()) FATAL("Error parsing /proc/%d/maps", main_pid); for(auto iter = map_entries.begin(); iter != map_entries.end(); iter++) { - if(iter->name != elf_filename) continue; + if(module_base && (iter->name != elf_filename)) continue; if(!(iter->permissions & PROT_EXEC)) continue; + if(!module_base) { + if(iter->addr_to < min_address) continue; + if(iter->addr_from > max_address) continue; + if(iter->addr_to > max_address) { + WARN("Asked to instrument address range that partially overlaps an allocation"); + iter->addr_to = max_address; + } + if(iter->addr_from < min_address) { + WARN("Asked to instrument address range that partially overlaps an allocation"); + iter->addr_from = min_address; + } + } + int ret = RemoteMprotect((void *)iter->addr_from, (iter->addr_to - iter->addr_from), iter->permissions ^ PROT_EXEC);