-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwhatcall.js
99 lines (99 loc) · 2.74 KB
/
whatcall.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* To find out who the function at 0x1234 calls the next time it is called:
* start(ptr('0x1234'))
*
* Or to ask the same question about one or more Objective-C methods, whichever is called first:
* start('-[LicenseManager *]')
*
* Or any exported function named open:
* start('exports:*!open*')
*/
let listeners = [];
let activated = false;
function start (target) {
stop();
if (typeof target === 'string') {
const pattern = target;
const resolver = new ApiResolver((pattern.indexOf(' ') === -1) ? 'module' : 'objc');
const matches = resolver.enumerateMatchesSync(pattern);
if (matches.length === 0) {
throw new Error('No matching methods found');
}
for (const {name, address} of matches)
stalkMethod(name, address);
} else {
stalkMethod(target.toString(), target);
}
}
function stop () {
for (const listener of listeners)
listener.detach();
listeners = [];
activated = false;
}
function stalkMethod (name, impl) {
console.log('Stalking next call to ' + name);
const listener = Interceptor.attach(impl, {
onEnter(args) {
if (activated) {
return;
}
activated = true;
const targets = {};
this.targets = targets;
console.log(`\n\nStalker activated: ${name}`);
Stalker.follow({
events: {
call: true
},
onCallSummary(summary) {
for (const [target, count] of Object.entries(summary))
targets[target] = (targets[target] ?? 0) + count;
}
});
},
onLeave(reval) {
const {targets} = this;
if (targets === undefined) {
return;
}
Stalker.flush();
Stalker.unfollow();
console.log(`Stalker deactivated: ${name}`);
printSummary(targets);
}
});
listeners.push(listener);
}
function printSummary (targets) {
const items = [];
let total = 0;
for (const [target, count] of Object.entries(targets)) {
const name = DebugSymbol.fromAddress(ptr(target)).toString();
const tokens = name.split(' ', 2).map(t => t.toLowerCase());
items.push([name, count, tokens]);
total += count;
}
items.sort((a, b) => {
const tokensA = a[2];
const tokensB = b[2];
if (tokensA.length === tokensB.length) {
return tokensA[tokensA.length - 1].localeCompare(tokensB[tokensB.length - 1]);
} else if (tokensA.length > tokensB.length) {
return -1;
} else {
return 1;
}
});
if (items.length > 0) {
console.log('');
console.log('COUNT\tNAME');
console.log('-----\t----');
for (const [name, count] of items)
console.log(`${count}\t${name}`);
}
console.log('');
console.log('Unique functions called: ' + items.length);
console.log(' Total function calls: ' + total);
console.log('');
}