-
Notifications
You must be signed in to change notification settings - Fork 0
/
umdhview.wsf
268 lines (241 loc) · 11.7 KB
/
umdhview.wsf
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?xml version="1.0" encoding="UTF-8" ?>
<job id="umdhview">
<script language="JScript" src="tps.js"></script>
<script language="javascript">
// <![CDATA[
// TYPES:
// StackFrame: {module, func, offset, source, line}
// Stack: [StackFrame]
// Leak: {allocs, alloc_bytes, stack:Stack, culprit_position}
// LeakGroup (leaks that have same property) : {property, leaks:[Leak]}
var usage = "" +
"umdhview: UMDH diff result viewer." +"\n"+
"umdhview group leaks by 'culprit-frame', then output highly readable summary" +"\n"+
"information about culprits." +"\n"+
"" +"\n"+
"usage: cscript.exe umdhview.wsf [/D] [/S:number] UMDH_diff_result_file" +"\n"+
"" +"\n"+
" /D tell umdhview to dump rearranged leaks." +"\n"+
" rearranged leaks are saved to one directory named <srcfile>.result," +"\n"+
" each file for one culprit." +"\n"+
"" +"\n"+
" /S:<number> tell umdhview to hide culprit which cause leaks less than <number>" +"\n"+
" for example: /S:100" +"\n"+
" The default value is 10" +"\n"+
"" +"\n"+
"exit code:" +"\n"+
" 0: (clean run) no culprit causes >= <number> leaks" +"\n"+
" >0: the number of culprits which causes >= <number> leaks" +"\n"+
"";
// settings that can be overrided by config.js
var IsMySource = function(source) {
// return source.search(/enhancedupp/i) != -1;
return true;
}
var IsCulpritIgnored = function(frame) {
return false;
}
var scriptdir = tps.sys.GetScriptDir();
var configfile = scriptdir + "\\" + "umdhview.config.js";
if (fso.FileExists(configfile)) {
WScript.Echo("default settings are overrided by:");
WScript.Echo(configfile);
WScript.Echo("");
eval(tps.file.ReadTextFile(configfile, "UTF-8"));
}
if (WScript.Arguments.UnNamed.length != 1) {
WScript.Echo(usage);
WScript.Quit(-1);
}
var srcfile = WScript.Arguments.UnNamed(0);
var rawleaks = ReadLeakFromFile(srcfile, 16);
for (var i in rawleaks) {
rawleaks[i].culprit_position = FindCulprit(rawleaks[i].stack);
rawleaks[i].culprit_frame = rawleaks[i].stack[rawleaks[i].culprit_position];
}
// leaks_by_culprit : {culpritstr: LeakGroup}
var m = tps.util.Group(rawleaks, function(leak) { return GetFrameString(leak.culprit_frame); });
var leakgroups_by_culprit = [];
for (var prop in m) {
var leaks = m[prop];
var total_allocs = tps.util.Accumulate(leaks, "allocs");
var total_alloc_bytes = tps.util.Accumulate(leaks, "alloc_bytes");
var property = PaddingLeft(total_allocs, 10, " ") + " " + PaddingLeft(total_alloc_bytes, 10, " ") + " " + prop;
leakgroups_by_culprit.push({"property":property, "leaks":m[prop]});
}
if (WScript.Arguments.Named.Exists("D")) {
DumpLeakGroups(leakgroups_by_culprit, srcfile + ".result");
}
// sort leakgroups by total_allocs in reverse order
leakgroups_by_culprit.sort(function(a,b) {return a.property.localeCompare(b.property);});
leakgroups_by_culprit.reverse();
//
var visible_culprits = 0;
var threshold = parseInt(WScript.Arguments.Named("S") || "10");
WScript.echo("Culprits that cause >= " + threshold + " allocs:\n");
WScript.echo(PaddingLeft("allocs", 10, " ") + " " + PaddingLeft("leak bytes", 10, " ") + " " + "culprits");
WScript.echo(PaddingLeft("", 10, "=") + " " + PaddingLeft("", 10, "=") + " " + PaddingLeft("", 50, "="));
for (var index in leakgroups_by_culprit) {
var group = leakgroups_by_culprit[index];
if (IsCulpritIgnored(group.leaks[0].culprit_frame)) continue;
if (tps.util.Accumulate(group.leaks, "allocs") < threshold) continue;
visible_culprits++;
WScript.echo(group.property);
}
if (visible_culprits != leakgroups_by_culprit.length) {
var hidden_culprits = leakgroups_by_culprit.length - visible_culprits;
WScript.Echo("(" + hidden_culprits + " culprits are hidden, use /S:1 switch to show all)")
}
WScript.Quit(visible_culprits);
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// return: [Leak]
function ReadLeakFromFile(srcfile, radix) {
var rawleaks = [];
var reLeakHead1 = /^\+\s*(\S+)\s+\(\s*(\S+)\s*-\s*(\S+)\s*\)\s*(\S+)\s*allocs\s+BackTrace(\S+)\s*$/;
var reLeakHead2 = /^\+\s*(\S+)\s+\(\s*(\S+)\s*-\s*(\S+)\s*\)\s*BackTrace(\S+)\s+allocations\s*$/;
var reLeakHead3 = /^\-\s*(\S+)\s+\(\s*(\S+)\s*-\s*(\S+)\s*\)\s*(\S+)\s*allocs\s+BackTrace(\S+)\s*$/;
var reLeakHead4 = /^\-\s*(\S+)\s+\(\s*(\S+)\s*-\s*(\S+)\s*\)\s*BackTrace(\S+)\s+allocations\s*$/;
var reLeakLine1 = /^\s*(\S+)!([^+]+)\+(\S+)\s*\(([^,]+),\s*(\S+)\)\s*$/;
var reLeakLine2 = /^\s*(\S+)!([^+]+)\+(\S+)\s*$/;
var reLeakLine3 = /^\s*(\S+)!([^+]+)\s*$/;
var reLeakLine4 = /^\s*(\S+)\s*$/;
var f = fso.OpenTextFile(srcfile, 1);
var leak;
while (!f.AtEndOfStream) {
var l = f.ReadLine();
var m;
if (m = reLeakHead3.exec(l)) {
// currently decreased allocs are not processed
break;
}
if (m = reLeakHead1.exec(l)) {
if (leak) rawleaks.push(leak);
leak = new Object;
leak.allocs = 0;
leak.stack = new Array;
leak.alloc_bytes = parseInt(m[1], radix);
}
if (!leak) continue;
if (m = reLeakHead2.exec(l)) {
leak.allocs = parseInt(m[1], radix);
}
else if (m = reLeakLine1.exec(l)) {
leak.stack.push({ module: m[1], func: m[2], offset: m[3], source: m[4], line: m[5] });
}
else if (m = reLeakLine2.exec(l)) {
leak.stack.push({ module: m[1], func: m[2], offset: m[3], source: "", line: "" });
}
else if (m = reLeakLine3.exec(l)) {
leak.stack.push({ module: m[1], func: "", offset: m[2], source: "", line: "" });
}
else if (m = reLeakLine4.exec(l)) {
leak.stack.push({ module: "", func: "", offset: m[1], source: "", line: "" });
}
}
if (leak) rawleaks.push(leak);
f.Close();
return rawleaks;
}
// leaks: [Leak]
function SaveLeakToFile(leaks, destfile) {
var str = "";
for (var i in leaks) {
var leak = leaks[i];
str += "===========================================================\r\n";
str += " count: " + leak.allocs + "; total bytes: " + leak.alloc_bytes + "\r\n";
for (var j in leak.stack) {
var frame = leak.stack[j];
str += j == leak.culprit_position ? "*" : " ";
str += " " + frame.module + "!" + frame.func + "+" + frame.offset;
if (frame.source.length > 0) str += " (" + frame.source + ":" + frame.line + ")";
str += "\r\n";
}
str += "\r\n\r\n";
}
tps.file.WriteTextFile(str, destfile, "UTF-8");
}
// stack : Stack
// return: index
function FindCulprit(stack) {
// DEFINATIONS:
// cmodule: very common module, frame of these module wouldn't provide interesting information, for example: ntdll, kernel32
// umodule: module that doesn't belongs to cmodule
// uframe: frame of umodule
// cframe: frame of cmodule
// sframe: frame that has real source code
// ucframe: uframe that doesn't make special call
// CULPRIT ALGORITHM:
// 1. filter out frames below user32!dispatch
// 2. select first sframe if exists
// 3. select last ucframe if exists
// 4. select first uframe if exists
// 5. select first frame
var special_call = /operator new|alloc|winproc|wndproc|startroutine|ThreadRoutine|ThreadDispatch/i;
var first_sframe = -1;
var first_uframe = -1;
var last_ucframe = -1;
var pos_user32_dispatch = -1;
var common_module = {"ntdll":1, "kernel32":1, "user32":1, "msvcrt":1, "kernelbase":1};
for (var i in stack) {
var frame = stack[i];
var module = frame.module.toLowerCase();
if (module == "user32" && frame.func.search(/dispatch/i) != -1) {
break;
//if (pos_user32_dispatch == -1) pos_user32_dispatch = i;
}
if (!(frame.module.toLowerCase() in common_module)) {
if (first_uframe == -1) first_uframe = i;
if (pos_user32_dispatch == -1) {
if (frame.func.search(special_call) == -1) last_ucframe = i;
}
if (frame.source != "" && frame.func.search(/ATL\:\:/i) == -1 && IsMySource(frame.source)) {
if (first_sframe == -1) first_sframe = i;
}
}
}
if (first_sframe != -1) return first_sframe;
if (last_ucframe != -1) return last_ucframe;
if (first_uframe != -1) return first_uframe;
return 0;
}
// Get string representation of frame
// frame: StackFrame
function GetFrameString(frame) {
return frame.module + "!" + frame.func + "+" + frame.offset;
}
// Dump leak groups to disk
// groups: [LeakGroup]
// dir: directory to dump
// NOTE: contents in this directory will be deleted before dump
function DumpLeakGroups(groups, dir) {
// output to separate files
var srcdir = tps.file.GetDir(srcfile);
var resultdir = srcfile + ".result";
try {
fso.DeleteFolder(dir, true);
} catch (x) {}
// avoid "permission denied"
WScript.Sleep(500);
fso.CreateFolder(dir);
for (var index in groups) {
var group = groups[index];
var resultfile = resultdir + "\\" + tps.file.GetFeasibleFileName(group.property) + ".txt";
SaveLeakToFile(group.leaks, resultfile);
}
WScript.Echo("leaks are arranged by culprits and saved to:");
WScript.Echo(resultdir);
WScript.Echo("");
}
function PaddingLeft(n, l, c) {
var str = String(n);
while (str.length < l) str = c + str;
return str;
}
// ]]>
</script>
</job>