-
Notifications
You must be signed in to change notification settings - Fork 805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add print capture variables for pblock #242
Open
dhcdht
wants to merge
4
commits into
facebook:main
Choose a base branch
from
dhcdht:printBlock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+118
−16
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,7 +111,7 @@ def run(self, arguments, options): | |
struct Block_literal_1 real = *((__bridge struct Block_literal_1 *)$block); | ||
NSMutableDictionary *dict = (id)[NSMutableDictionary dictionary]; | ||
|
||
[dict setObject:(id)[NSNumber numberWithLong:(long)real.invoke] forKey:@"invoke"]; | ||
[dict setValue:(id)[NSNumber numberWithLong:(long)real.invoke] forKey:@"invoke"]; | ||
|
||
if (real.flags & BLOCK_HAS_SIGNATURE) { | ||
char *signature; | ||
|
@@ -131,25 +131,119 @@ def run(self, arguments, options): | |
[types addObject:(id)[NSString stringWithUTF8String:type]]; | ||
} | ||
|
||
[dict setObject:types forKey:@"signature"]; | ||
[dict setValue:types forKey:@"signature"]; | ||
} | ||
|
||
RETURN(dict); | ||
""" | ||
command = string.Template(tmpString).substitute(block=block) | ||
json = fb.evaluate(command) | ||
|
||
variables_json = self.getBlockVariables(block) | ||
if variables_json is not None: | ||
json.update(variables_json) | ||
|
||
variablesStrs = [] | ||
for i in range(10): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why up to 10? |
||
varKey = 'variables['+str(i)+']' | ||
if varKey in json: | ||
variablesStrs.append(json[varKey]) | ||
variablesStr = '\n'.join(variablesStrs) | ||
|
||
signature = json['signature'] | ||
if not signature: | ||
print 'Imp: ' + hex(json['invoke']) | ||
print 'Imp: ' + hex(json['invoke']) + ' Variables : {\n'+variablesStr+'\n};' | ||
return | ||
|
||
sigStr = '{} ^('.format(decode(signature[0])) | ||
# the block`s implementation always take the block as it`s first argument, so we ignore it | ||
sigStr += ', '.join([decode(m) for m in signature[2:]]) | ||
sigStr += ');' | ||
sigStr += ')' | ||
|
||
print 'Imp: ' + hex(json['invoke']) + ' Signature: ' + sigStr | ||
print 'Imp: ' + hex(json['invoke']) + ' Signature: ' + sigStr + ' Variables : {\n'+variablesStr+'\n};' | ||
|
||
def getBlockVariables(self, block, min_var_count=1, max_var_count=20): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you explain |
||
''' | ||
no __Block_byref_xxx | ||
''' | ||
|
||
# http://clang.llvm.org/docs/Block-ABI-Apple.html | ||
tmpString = """ | ||
#define BLOCK_VARIABLES_COUNT ($variables_count) | ||
enum { | ||
BLOCK_HAS_COPY_DISPOSE = (1 << 25), | ||
BLOCK_HAS_CTOR = (1 << 26), // helpers have C++ code | ||
BLOCK_IS_GLOBAL = (1 << 28), | ||
BLOCK_HAS_STRET = (1 << 29), // IFF BLOCK_HAS_SIGNATURE | ||
BLOCK_HAS_SIGNATURE = (1 << 30), | ||
}; | ||
struct Block_literal_1 { | ||
void *isa; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock | ||
int flags; | ||
int reserved; | ||
void (*invoke)(void *, ...); | ||
struct Block_descriptor_1 { | ||
unsigned long int reserved; // NULL | ||
unsigned long int size; // sizeof(struct Block_literal_1) | ||
// optional helper functions | ||
void (*copy_helper)(void *dst, void *src); // IFF (1<<25) | ||
void (*dispose_helper)(void *src); // IFF (1<<25) | ||
// required ABI.2010.3.16 | ||
const char *signature; // IFF (1<<30) | ||
} *descriptor; | ||
// imported variables | ||
Class *variables[BLOCK_VARIABLES_COUNT]; | ||
}; | ||
struct Block_literal_1 real = *((__bridge struct Block_literal_1 *)$block); | ||
NSMutableDictionary *dict = (id)[NSMutableDictionary dictionary]; | ||
|
||
// Get the list of classes and look for testPointerClass | ||
NSInteger numClasses = (NSInteger)objc_getClassList(NULL, 0); | ||
Class *classesList = (Class*)malloc(sizeof(Class) * numClasses); | ||
numClasses = (NSInteger)objc_getClassList(classesList, numClasses); | ||
|
||
Class **blockVariables = real.variables; | ||
for (int i = 0; i < BLOCK_VARIABLES_COUNT; i++) { | ||
Class *obj = (Class*)blockVariables[i]; | ||
if (obj == NULL) { | ||
break; | ||
} | ||
|
||
Class testPointerClass = (Class)(*obj); | ||
BOOL isClass = NO; | ||
for (int i = 0; i < numClasses; i++) | ||
{ | ||
if (classesList[i] == testPointerClass) | ||
{ | ||
isClass = YES; | ||
break; | ||
} | ||
} | ||
// __Block_byref_xxx may break this | ||
/* | ||
if (!isClass) { | ||
break; | ||
} | ||
*/ | ||
|
||
NSString *key = [NSString stringWithFormat:@"variables[%d]", i]; | ||
NSString *value = [NSString stringWithFormat:@"%@", obj]; | ||
[dict setValue:value forKey:key]; | ||
} | ||
|
||
free(classesList); | ||
|
||
RETURN(dict); | ||
""" | ||
last_json = None | ||
for i in range(min_var_count, max_var_count): | ||
command = string.Template(tmpString).substitute(block=block, variables_count=i) | ||
json = fb.evaluate(command, printErrors=False) | ||
if json is not None: | ||
last_json = json | ||
else: | ||
break | ||
return last_json | ||
|
||
# helpers | ||
def isClassObject(arg): | ||
|
@@ -302,20 +396,20 @@ def getProperties(klass): | |
NSMutableDictionary *dict = (id)[NSMutableDictionary dictionary]; | ||
|
||
char *name = (char *)property_getName(props[i]); | ||
[dict setObject:(id)[NSString stringWithUTF8String:name] forKey:@"name"]; | ||
[dict setValue:(id)[NSString stringWithUTF8String:name] forKey:@"name"]; | ||
|
||
char *attrstr = (char *)property_getAttributes(props[i]); | ||
[dict setObject:(id)[NSString stringWithUTF8String:attrstr] forKey:@"attributes_string"]; | ||
[dict setValue:(id)[NSString stringWithUTF8String:attrstr] forKey:@"attributes_string"]; | ||
|
||
NSMutableDictionary *attrsDict = (id)[NSMutableDictionary dictionary]; | ||
unsigned int pcount; | ||
objc_property_attribute_t *attrs = (objc_property_attribute_t *)property_copyAttributeList(props[i], &pcount); | ||
for (int i = 0; i < pcount; i++) { | ||
NSString *name = (id)[NSString stringWithUTF8String:(char *)attrs[i].name]; | ||
NSString *value = (id)[NSString stringWithUTF8String:(char *)attrs[i].value]; | ||
[attrsDict setObject:value forKey:name]; | ||
[attrsDict setValue:value forKey:name]; | ||
} | ||
[dict setObject:attrsDict forKey:@"attributes"]; | ||
[dict setValue:attrsDict forKey:@"attributes"]; | ||
|
||
[result addObject:dict]; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the change to
setValue:forKey:
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That can fix #198 . There is another MR that can fix it separately
#241
I guess because function declaration of setValue:forKey doesn't need id , just id