Skip to content

Commit

Permalink
Hide watch variable base address menu items when appropriate
Browse files Browse the repository at this point in the history
Now we show the menu items if relevant instead of requiring option to be held down.
  • Loading branch information
zorgiepoo committed Dec 22, 2023
1 parent 3cfd4e3 commit 5b14fd0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 41 deletions.
8 changes: 4 additions & 4 deletions Bit Slicer/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -668,21 +668,21 @@ CA
<action selector="watchVariable:" target="-1" id="1051"/>
</connections>
</menuItem>
<menuItem title="Write Accesses to Base Address…" alternate="YES" keyEquivalent="l" id="gOJ-hH-dww">
<menuItem title="Write Accesses to Base Address…" hidden="YES" keyEquivalent="l" id="gOJ-hH-dww">
<modifierMask key="keyEquivalentModifierMask" option="YES" command="YES"/>
<connections>
<action selector="watchVariable:" target="-1" id="vEa-Yp-bYI"/>
<action selector="watchVariableBaseAddress:" target="-1" id="sba-au-VgK"/>
</connections>
</menuItem>
<menuItem title="Read &amp; Write Accesses…" tag="1" keyEquivalent="L" id="1052">
<connections>
<action selector="watchVariable:" target="-1" id="1053"/>
</connections>
</menuItem>
<menuItem title="Read &amp; Write Accesses to Base Address…" tag="1" alternate="YES" keyEquivalent="L" id="KQm-HB-Xdh">
<menuItem title="Read &amp; Write Accesses to Base Address…" tag="1" hidden="YES" keyEquivalent="L" id="KQm-HB-Xdh">
<modifierMask key="keyEquivalentModifierMask" option="YES" command="YES"/>
<connections>
<action selector="watchVariable:" target="-1" id="FQr-XO-g8t"/>
<action selector="watchVariableBaseAddress:" target="-1" id="Xni-P4-PMU"/>
</connections>
</menuItem>
</items>
Expand Down
8 changes: 4 additions & 4 deletions Bit Slicer/Base.lproj/Search Document Window.xib
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@
<action selector="watchVariable:" target="-1" id="100794"/>
</connections>
</menuItem>
<menuItem title="Watch Write Accesses to Base Address…" alternate="YES" id="LKg-Ix-res">
<menuItem title="Watch Write Accesses to Base Address…" hidden="YES" id="LKg-Ix-res">
<modifierMask key="keyEquivalentModifierMask" option="YES"/>
<connections>
<action selector="watchVariable:" target="-1" id="Ja7-17-kya"/>
<action selector="watchVariableBaseAddress:" target="-1" id="c1o-bT-qCr"/>
</connections>
</menuItem>
<menuItem title="Watch Read &amp; Write Accesses…" tag="1" id="100885">
Expand All @@ -189,10 +189,10 @@
<action selector="watchVariable:" target="-1" id="100886"/>
</connections>
</menuItem>
<menuItem title="Watch Read &amp; Write Accesses to Base Address…" tag="1" alternate="YES" id="Ule-bH-KzF">
<menuItem title="Watch Read &amp; Write Accesses to Base Address…" tag="1" hidden="YES" id="Ule-bH-KzF">
<modifierMask key="keyEquivalentModifierMask" option="YES"/>
<connections>
<action selector="watchVariable:" target="-1" id="arU-1i-akx"/>
<action selector="watchVariableBaseAddress:" target="-1" id="mQ5-r8-1Il"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="100789"/>
Expand Down
95 changes: 62 additions & 33 deletions Bit Slicer/ZGDocumentWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -1434,7 +1434,7 @@ - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)userInterfa
}
}

else if (menuItem.action == @selector(watchVariable:))
else if (menuItem.action == @selector(watchVariable:) || menuItem.action == @selector(watchVariableBaseAddress:))
{
if ([_searchController canCancelTask] || !self.currentProcess.valid || [self selectedVariables].count != 1)
{
Expand All @@ -1443,32 +1443,52 @@ - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)userInterfa

ZGVariable *selectedVariable = [[self selectedVariables] objectAtIndex:0];

BOOL watchingBaseAccesses = (menuItem.action == @selector(watchVariableBaseAddress:));
if (watchingBaseAccesses)
{
menuItem.hidden = !selectedVariable.usesDynamicPointerAddress;
}

if (selectedVariable.type == ZGScript)
{
return NO;
}

BOOL usesDynamicPointerAddress = selectedVariable.usesDynamicPointerAddress;
BOOL watchingBaseAccesess = (usesDynamicPointerAddress && menuItem.alternate);

NSString *localizableTitleKey = [NSString stringWithFormat:@"watchAccesses_%ld_%d", menuItem.tag, watchingBaseAccesess];
NSString *localizableTitleKey = [NSString stringWithFormat:@"watchAccesses_%ld_%d", menuItem.tag, watchingBaseAccesses];
menuItem.title = ZGLocalizableSearchDocumentString(localizableTitleKey);

if (!watchingBaseAccesess)
ZGMemoryAddress targetMemoryAddress;
ZGMemorySize targetMemorySize;
if (watchingBaseAccesses)
{
ZGMemoryAddress memoryAddress = selectedVariable.address;
ZGMemorySize memorySize = selectedVariable.size;
ZGMemoryProtection memoryProtection;

if (!ZGMemoryProtectionInRegion(self.currentProcess.processTask, &memoryAddress, &memorySize, &memoryProtection))
ZGMemoryAddress baseAddress = 0x0;
if (![_tableController getBaseAddress:&baseAddress variable:selectedVariable])
{
return NO;
}

if (memoryAddress + memorySize < selectedVariable.address || memoryAddress > selectedVariable.address + selectedVariable.size)
{
return NO;
}
targetMemoryAddress = baseAddress;
targetMemorySize = self.currentProcess.pointerSize;
}
else
{
targetMemoryAddress = selectedVariable.address;
targetMemorySize = selectedVariable.size;
}

ZGMemoryProtection memoryProtection;

ZGMemoryAddress memoryAddress = targetMemoryAddress;
ZGMemoryAddress memorySize = targetMemorySize;

if (!ZGMemoryProtectionInRegion(self.currentProcess.processTask, &memoryAddress, &memorySize, &memoryProtection))
{
return NO;
}

if (memoryAddress + memorySize < targetMemoryAddress || memoryAddress > targetMemoryAddress + targetMemorySize)
{
return NO;
}
}

Expand Down Expand Up @@ -1869,30 +1889,14 @@ - (IBAction)relativizeVariablesAddress:(id)__unused sender

#pragma mark Variable Watching Handling

- (IBAction)watchVariable:(id)sender
- (void)_watchVariable:(ZGVariable *)variable watchPointType:(ZGWatchPointType)watchPointType
{
if (_watchVariableWindowController == nil)
{
_watchVariableWindowController = [[ZGWatchVariableWindowController alloc] initWithBreakPointController:_breakPointController delegate:self.delegate];
}

ZGVariable *selectedVariable = [[self selectedVariables] firstObject];

ZGVariable *watchVariable;
ZGMemoryAddress baseAddress = 0x0;
BOOL watchingBaseAccesess = (selectedVariable.usesDynamicPointerAddress && [(NSMenuItem *)sender isAlternate]);

if (watchingBaseAccesess && [_tableController getBaseAddress:&baseAddress variable:selectedVariable])
{
ZGMemorySize pointerSize = self.currentProcess.pointerSize;
watchVariable = [[ZGVariable alloc] initWithValue:NULL size:pointerSize address:baseAddress type:ZGPointer qualifier:0 pointerSize:pointerSize];
}
else
{
watchVariable = selectedVariable;
}

[_watchVariableWindowController watchVariable:watchVariable withWatchPointType:(ZGWatchPointType)[(NSControl *)sender tag] inProcess:self.currentProcess attachedToWindow:ZGUnwrapNullableObject(self.window) completionHandler:^(NSArray<ZGVariable *> *foundVariables) {
[_watchVariableWindowController watchVariable:variable withWatchPointType:watchPointType inProcess:self.currentProcess attachedToWindow:ZGUnwrapNullableObject(self.window) completionHandler:^(NSArray<ZGVariable *> *foundVariables) {
if (foundVariables.count > 0)
{
NSIndexSet *rowIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, foundVariables.count)];
Expand All @@ -1905,6 +1909,31 @@ - (IBAction)watchVariable:(id)sender
}];
}

- (IBAction)watchVariableBaseAddress:(id)sender
{
ZGVariable *selectedVariable = [[self selectedVariables] firstObject];

ZGMemoryAddress baseAddress = 0x0;
if ([_tableController getBaseAddress:&baseAddress variable:selectedVariable])
{
ZGMemorySize pointerSize = self.currentProcess.pointerSize;
ZGVariable *watchVariable = [[ZGVariable alloc] initWithValue:NULL size:pointerSize address:baseAddress type:ZGPointer qualifier:0 pointerSize:pointerSize];

[self _watchVariable:watchVariable watchPointType:(ZGWatchPointType)[(NSControl *)sender tag]];
}
else
{
NSLog(@"Error: failed to extract base address from variable with address: %@", selectedVariable.addressFormula);
}
}

- (IBAction)watchVariable:(id)sender
{
ZGVariable *selectedVariable = [[self selectedVariables] firstObject];

[self _watchVariable:selectedVariable watchPointType:(ZGWatchPointType)[(NSControl *)sender tag]];
}

#pragma mark Showing Other Controllers

- (IBAction)showMemoryViewer:(id)__unused sender
Expand Down

0 comments on commit 5b14fd0

Please sign in to comment.