Skip to content

Commit

Permalink
Linux: Support instrumenting custom address range
Browse files Browse the repository at this point in the history
  • Loading branch information
ifratric committed Jun 10, 2024
1 parent 338dde5 commit b194d7b
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions Linux/debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down

0 comments on commit b194d7b

Please sign in to comment.