32
32
33
33
#import " ZGCalculator.h"
34
34
#import " NSStringAdditions.h"
35
+ #import " ZGDocumentLabelManager.h"
35
36
#import " ZGVirtualMemory.h"
36
37
#import " ZGMachBinary.h"
37
38
#import " ZGMachBinaryInfo.h"
38
39
#import " ZGRegion.h"
40
+ #import " ZGLabel.h"
39
41
#import " ZGProcess.h"
40
42
#pragma clang diagnostic push
41
43
#pragma clang diagnostic ignored "-Wincomplete-umbrella"
49
51
50
52
#define ZGCalculatePointerFunction @" ZGCalculatePointerFunction"
51
53
#define ZGFindSymbolFunction @" symbol"
54
+ #define ZGFindLabelFunction @" label"
52
55
#define ZGProcessVariable @" ZGProcessVariable"
56
+ #define ZGDocumentLabelManagerVariable @" ZGDocumentLabelManagerVariable"
53
57
#define ZGFailedImagesVariable @" ZGFailedImagesVariable"
54
58
#define ZGSymbolicatesVariable @" ZGSymbolicatesVariable"
55
59
#define ZGDidFindSymbol @" ZGDidFindSymbol"
56
60
#define ZGLastSearchInfoVariable @" ZGLastSearchInfoVariable"
57
61
58
62
@implementation ZGVariable (ZGCalculatorAdditions)
59
63
64
+ - (BOOL )usesDynamicLabelAddress
65
+ {
66
+ return _addressFormula != nil && [_addressFormula rangeOfString: ZGFindLabelFunction].location != NSNotFound ;
67
+ }
68
+
60
69
- (BOOL )usesDynamicPointerAddress
61
70
{
62
- return _addressFormula != nil && [_addressFormula rangeOfString: @" [" ].location != NSNotFound && [_addressFormula rangeOfString: @" ]" ].location != NSNotFound ;
71
+ return _addressFormula != nil && ( [_addressFormula rangeOfString: @" [" ].location != NSNotFound && [_addressFormula rangeOfString: @" ]" ].location != NSNotFound ) ;
63
72
}
64
73
65
74
- (BOOL )usesDynamicBaseAddress
@@ -236,6 +245,62 @@ + (DDMathFunction)registerFindSymbolFunctionWithEvaluator:(DDMathEvaluator *)eva
236
245
return findSymbolFunction;
237
246
}
238
247
248
+ + (DDMathFunction)registerFindLabelFunctionWithEvaluator : (DDMathEvaluator *)evaluator
249
+ {
250
+ DDMathFunction findLabelFunction = ^DDExpression *(NSArray <DDExpression *> *args, NSDictionary <NSString *, id > *vars, DDMathEvaluator * __unused eval, NSError *__autoreleasing *error) {
251
+ ZGDocumentLabelManager *documentLabelManager = [vars objectForKey: ZGDocumentLabelManagerVariable];
252
+
253
+ __block NSNumber *labelAddressNumber = @(0 );
254
+
255
+ if (args.count == 0 || args.count > 1 )
256
+ {
257
+ if (error != NULL )
258
+ {
259
+ *error = [NSError errorWithDomain: DDMathParserErrorDomain code: DDErrorCodeInvalidNumberOfArguments userInfo: @{NSLocalizedDescriptionKey :ZGFindLabelFunction @" expects 1 argument" }];
260
+ }
261
+ }
262
+ else if (documentLabelManager == nil || [documentLabelManager.labels count ] == 0 )
263
+ {
264
+ if (error != NULL )
265
+ {
266
+ *error = [NSError errorWithDomain: DDMathParserErrorDomain code: DDErrorCodeUnresolvedVariable userInfo: @{NSLocalizedDescriptionKey :ZGFindLabelFunction @" expects a labels variable" }];
267
+ }
268
+ }
269
+ else
270
+ {
271
+ DDExpression *labelExpression = [args objectAtIndex: 0 ];
272
+
273
+ if (labelExpression.expressionType != DDExpressionTypeVariable)
274
+ {
275
+ if (error != NULL )
276
+ {
277
+ *error = [NSError errorWithDomain: DDMathParserErrorDomain code: DDErrorCodeUnresolvedVariable userInfo: @{NSLocalizedDescriptionKey :ZGFindLabelFunction @" expects a string variable" }];
278
+ }
279
+ }
280
+ else
281
+ {
282
+ NSString *labelString = labelExpression.variable ;
283
+ ZGLabel *label = [documentLabelManager.labels objectForKey: labelString];
284
+
285
+ labelAddressNumber = @(label.address );
286
+ if (labelAddressNumber == nil )
287
+ {
288
+ if (error != NULL )
289
+ {
290
+ *error = [NSError errorWithDomain: DDMathParserErrorDomain code: DDErrorCodeInvalidArgument userInfo: @{NSLocalizedDescriptionKey :ZGFindLabelFunction @" could not find requested label" }];
291
+ }
292
+ }
293
+ }
294
+ }
295
+
296
+ return [DDExpression numberExpressionWithNumber: labelAddressNumber];
297
+ };
298
+
299
+ [evaluator registerFunction: findLabelFunction forName: ZGFindLabelFunction];
300
+
301
+ return findLabelFunction;
302
+ }
303
+
239
304
+ (void )registerFunctionResolverWithEvaluator : (DDMathEvaluator *)evaluator findSymbolFunction : (DDMathFunction)findSymbolFunction
240
305
{
241
306
evaluator.functionResolver = (DDFunctionResolver)^(NSString *name) {
@@ -262,6 +327,7 @@ + (void)initialize
262
327
DDMathEvaluator *evaluator = [DDMathEvaluator defaultMathEvaluator ];
263
328
[self registerCalculatePointerFunctionWithEvaluator: evaluator];
264
329
[self registerBaseAddressFunctionWithEvaluator: evaluator];
330
+ [self registerFindLabelFunctionWithEvaluator: evaluator];
265
331
DDMathFunction findSymbolFunction = [self registerFindSymbolFunctionWithEvaluator: evaluator];
266
332
[self registerFunctionResolverWithEvaluator: evaluator findSymbolFunction: findSymbolFunction];
267
333
});
@@ -479,12 +545,17 @@ + (NSString *)expressionBySubstitutingCalculatePointerFunctionInExpression:(NSSt
479
545
return [[NSString alloc ] initWithData: newData encoding: NSUTF8StringEncoding];
480
546
}
481
547
482
- + (NSString *)evaluateExpression : (NSString *)expression process : (ZGProcess * __unsafe_unretained)process failedImages : (NSMutableArray <NSString *> * __unsafe_unretained)failedImages symbolicates : (BOOL )symbolicates foundSymbol : (BOOL *)foundSymbol currentAddress : (ZGMemoryAddress)currentAddress error : (NSError * __autoreleasing *)error
548
+ + (NSString *)evaluateExpression : (NSString *)expression documentLabelManager : (ZGDocumentLabelManager * __unsafe_unretained) documentLabelManager process : (ZGProcess * __unsafe_unretained)process failedImages : (NSMutableArray <NSString *> * __unsafe_unretained)failedImages symbolicates : (BOOL )symbolicates foundSymbol : (BOOL *)foundSymbol currentAddress : (ZGMemoryAddress)currentAddress error : (NSError * __autoreleasing *)error
483
549
{
484
550
NSString *newExpression = [self expressionBySubstitutingCalculatePointerFunctionInExpression: expression];
485
551
486
552
NSMutableDictionary <NSString *, id > *substitutions = [NSMutableDictionary dictionaryWithDictionary: @{ZGProcessVariable : process, ZGSymbolicatesVariable : @(symbolicates), ZGLastSearchInfoVariable : @(currentAddress), ZGDidFindSymbol : @(NO )}];
487
553
554
+ if (documentLabelManager != nil )
555
+ {
556
+ [substitutions setObject: documentLabelManager forKey: ZGDocumentLabelManagerVariable];
557
+ }
558
+
488
559
if (failedImages != nil )
489
560
{
490
561
[substitutions setObject: failedImages forKey: ZGFailedImagesVariable];
@@ -501,12 +572,12 @@ + (NSString *)evaluateExpression:(NSString *)expression process:(ZGProcess * __u
501
572
502
573
+ (NSString *)evaluateAndSymbolicateExpression : (NSString *)expression process : (ZGProcess * __unsafe_unretained)process currentAddress : (ZGMemoryAddress)currentAddress didSymbolicate : (BOOL *)didSymbolicate error : (NSError * __autoreleasing *)error
503
574
{
504
- return [self evaluateExpression: expression process: process failedImages: nil symbolicates: YES foundSymbol: didSymbolicate currentAddress: currentAddress error: error];
575
+ return [self evaluateExpression: expression documentLabelManager: NULL process: process failedImages: nil symbolicates: YES foundSymbol: didSymbolicate currentAddress: currentAddress error: error];
505
576
}
506
577
507
- + (NSString *)evaluateExpression : (NSString *)expression process : (ZGProcess * __unsafe_unretained)process failedImages : (NSMutableArray <NSString *> * __unsafe_unretained)failedImages error : (NSError * __autoreleasing *)error
578
+ + (NSString *)evaluateExpression : (NSString *)expression documentLabelManager : (ZGDocumentLabelManager * __unsafe_unretained) documentLabelManager process : (ZGProcess * __unsafe_unretained)process failedImages : (NSMutableArray <NSString *> * __unsafe_unretained)failedImages error : (NSError * __autoreleasing *)error
508
579
{
509
- return [self evaluateExpression: expression process: process failedImages: failedImages symbolicates: NO foundSymbol: NULL currentAddress: 0x0 error: error];
580
+ return [self evaluateExpression: expression documentLabelManager: documentLabelManager process: process failedImages: failedImages symbolicates: NO foundSymbol: NULL currentAddress: 0x0 error: error];
510
581
}
511
582
512
583
@end
0 commit comments