Skip to content

Commit

Permalink
Optimise the number of GetMemoryRegions calls, as these calls on macO…
Browse files Browse the repository at this point in the history
…S take forever and slow rendering down considerably (#7)

Co-authored-by: Foundry Zero <info@foundryzero.co.uk>
  • Loading branch information
sam-f0 and Foundry Zero authored Jul 19, 2023
1 parent 4a8b75a commit 605256d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
14 changes: 7 additions & 7 deletions common/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
from typing import List

from lldb import SBError, SBFrame, SBMemoryRegionInfo, SBProcess, SBValue
from lldb import SBError, SBFrame, SBMemoryRegionInfo, SBMemoryRegionInfoList, SBProcess, SBValue

from common.constants import ALIGN, GLYPHS, MSG_TYPE, TERM_COLOURS

Expand Down Expand Up @@ -125,30 +125,30 @@ def attempt_to_read_string_from_memory(
return ret_string


def is_code(address: SBValue, process: SBProcess) -> bool:
def is_code(address: SBValue, process: SBProcess, regions: SBMemoryRegionInfoList) -> bool:
"""Determines whether an @address points to code"""
region = SBMemoryRegionInfo()
code_bool = False
if process.GetMemoryRegions().GetMemoryRegionContainingAddress(address, region):
if regions.GetMemoryRegionContainingAddress(address, region):
code_bool = region.IsExecutable()
return code_bool


def is_stack(address: SBValue, process: SBProcess) -> bool:
def is_stack(address: SBValue, process: SBProcess, regions: SBMemoryRegionInfoList) -> bool:
"""Determines whether an @address points to the stack"""
region = SBMemoryRegionInfo()
stack_bool = False
if process.GetMemoryRegions().GetMemoryRegionContainingAddress(address, region):
if regions.GetMemoryRegionContainingAddress(address, region):
if region.GetName() == "[stack]":
stack_bool = True
return stack_bool


def is_heap(address: SBValue, process: SBProcess) -> bool:
def is_heap(address: SBValue, process: SBProcess, regions: SBMemoryRegionInfoList) -> bool:
"""Determines whether an @address points to the heap"""
region = SBMemoryRegionInfo()
heap_bool = False
if process.GetMemoryRegions().GetMemoryRegionContainingAddress(address, region):
if regions.GetMemoryRegionContainingAddress(address, region):
if region.GetName() == "[heap]":
heap_bool = True
return heap_bool
Expand Down
7 changes: 4 additions & 3 deletions handlers/stop_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ def print_register(self, register: SBValue) -> None:
# Register value has changed so highlight
highlight = TERM_COLOURS.RED

if is_code(reg_value, self.process):
if is_code(reg_value, self.process, self.regions):
color = TERM_COLOURS.RED
elif is_stack(reg_value, self.process):
elif is_stack(reg_value, self.process, self.regions):
color = TERM_COLOURS.PINK
elif is_heap(reg_value, self.process):
elif is_heap(reg_value, self.process, self.regions):
color = TERM_COLOURS.GREEN
else:
color = TERM_COLOURS.ENDC
Expand Down Expand Up @@ -312,6 +312,7 @@ def handle_stop(self, exe_ctx: SBExecutionContext, _: SBStream) -> None:
self.target = exe_ctx.GetTarget()
self.thread = exe_ctx.GetThread()
self.arch = get_arch(self.target)
self.regions = self.process.GetMemoryRegions()

# Hack to print cursor at the top of the screen
clear_page()
Expand Down

0 comments on commit 605256d

Please sign in to comment.