Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | /** |
michael@0 | 6 | * Memory leak hunter. Walks a tree of objects looking for DOM nodes. |
michael@0 | 7 | * Usage: |
michael@0 | 8 | * leakHunt({ |
michael@0 | 9 | * thing: thing, |
michael@0 | 10 | * otherthing: otherthing |
michael@0 | 11 | * }); |
michael@0 | 12 | */ |
michael@0 | 13 | function leakHunt(root) { |
michael@0 | 14 | var path = []; |
michael@0 | 15 | var seen = []; |
michael@0 | 16 | |
michael@0 | 17 | try { |
michael@0 | 18 | var output = leakHunt.inner(root, path, seen); |
michael@0 | 19 | output.forEach(function(line) { |
michael@0 | 20 | dump(line + '\n'); |
michael@0 | 21 | }); |
michael@0 | 22 | } |
michael@0 | 23 | catch (ex) { |
michael@0 | 24 | dump(ex + '\n'); |
michael@0 | 25 | } |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | leakHunt.inner = function LH_inner(root, path, seen) { |
michael@0 | 29 | var prefix = new Array(path.length).join(' '); |
michael@0 | 30 | |
michael@0 | 31 | var reply = []; |
michael@0 | 32 | function log(msg) { |
michael@0 | 33 | reply.push(msg); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | var direct |
michael@0 | 37 | try { |
michael@0 | 38 | direct = Object.keys(root); |
michael@0 | 39 | } |
michael@0 | 40 | catch (ex) { |
michael@0 | 41 | log(prefix + ' Error enumerating: ' + ex); |
michael@0 | 42 | return reply; |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | try { |
michael@0 | 46 | var index = 0; |
michael@0 | 47 | for (var data of root) { |
michael@0 | 48 | var prop = '' + index; |
michael@0 | 49 | leakHunt.digProperty(prop, data, path, seen, direct, log); |
michael@0 | 50 | index++; |
michael@0 | 51 | } |
michael@0 | 52 | } |
michael@0 | 53 | catch (ex) { /* Ignore things that are not enumerable */ } |
michael@0 | 54 | |
michael@0 | 55 | for (var prop in root) { |
michael@0 | 56 | var data; |
michael@0 | 57 | try { |
michael@0 | 58 | data = root[prop]; |
michael@0 | 59 | } |
michael@0 | 60 | catch (ex) { |
michael@0 | 61 | log(prefix + ' ' + prop + ' = Error: ' + ex.toString().substring(0, 30)); |
michael@0 | 62 | continue; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | leakHunt.digProperty(prop, data, path, seen, direct, log); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | return reply; |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | leakHunt.hide = [ /^string$/, /^number$/, /^boolean$/, /^null/, /^undefined/ ]; |
michael@0 | 72 | |
michael@0 | 73 | leakHunt.noRecurse = [ |
michael@0 | 74 | /^string$/, /^number$/, /^boolean$/, /^null/, /^undefined/, |
michael@0 | 75 | /^Window$/, /^Document$/, |
michael@0 | 76 | /^XULDocument$/, /^XULElement$/, |
michael@0 | 77 | /^DOMWindow$/, /^HTMLDocument$/, /^HTML.*Element$/, /^ChromeWindow$/ |
michael@0 | 78 | ]; |
michael@0 | 79 | |
michael@0 | 80 | leakHunt.digProperty = function LH_digProperty(prop, data, path, seen, direct, log) { |
michael@0 | 81 | var newPath = path.slice(); |
michael@0 | 82 | newPath.push(prop); |
michael@0 | 83 | var prefix = new Array(newPath.length).join(' '); |
michael@0 | 84 | |
michael@0 | 85 | var recurse = true; |
michael@0 | 86 | var message = leakHunt.getType(data); |
michael@0 | 87 | |
michael@0 | 88 | if (leakHunt.matchesAnyPattern(message, leakHunt.hide)) { |
michael@0 | 89 | return; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | if (message === 'function' && direct.indexOf(prop) == -1) { |
michael@0 | 93 | return; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | if (message === 'string') { |
michael@0 | 97 | var extra = data.length > 10 ? data.substring(0, 9) + '_' : data; |
michael@0 | 98 | message += ' "' + extra.replace(/\n/g, "|") + '"'; |
michael@0 | 99 | recurse = false; |
michael@0 | 100 | } |
michael@0 | 101 | else if (leakHunt.matchesAnyPattern(message, leakHunt.noRecurse)) { |
michael@0 | 102 | message += ' (no recurse)' |
michael@0 | 103 | recurse = false; |
michael@0 | 104 | } |
michael@0 | 105 | else if (seen.indexOf(data) !== -1) { |
michael@0 | 106 | message += ' (already seen)'; |
michael@0 | 107 | recurse = false; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | if (recurse) { |
michael@0 | 111 | seen.push(data); |
michael@0 | 112 | var lines = leakHunt.inner(data, newPath, seen); |
michael@0 | 113 | if (lines.length == 0) { |
michael@0 | 114 | if (message !== 'function') { |
michael@0 | 115 | log(prefix + prop + ' = ' + message + ' { }'); |
michael@0 | 116 | } |
michael@0 | 117 | } |
michael@0 | 118 | else { |
michael@0 | 119 | log(prefix + prop + ' = ' + message + ' {'); |
michael@0 | 120 | lines.forEach(function(line) { |
michael@0 | 121 | log(line); |
michael@0 | 122 | }); |
michael@0 | 123 | log(prefix + '}'); |
michael@0 | 124 | } |
michael@0 | 125 | } |
michael@0 | 126 | else { |
michael@0 | 127 | log(prefix + prop + ' = ' + message); |
michael@0 | 128 | } |
michael@0 | 129 | }; |
michael@0 | 130 | |
michael@0 | 131 | leakHunt.matchesAnyPattern = function LH_matchesAnyPattern(str, patterns) { |
michael@0 | 132 | var match = false; |
michael@0 | 133 | patterns.forEach(function(pattern) { |
michael@0 | 134 | if (str.match(pattern)) { |
michael@0 | 135 | match = true; |
michael@0 | 136 | } |
michael@0 | 137 | }); |
michael@0 | 138 | return match; |
michael@0 | 139 | }; |
michael@0 | 140 | |
michael@0 | 141 | leakHunt.getType = function LH_getType(data) { |
michael@0 | 142 | if (data === null) { |
michael@0 | 143 | return 'null'; |
michael@0 | 144 | } |
michael@0 | 145 | if (data === undefined) { |
michael@0 | 146 | return 'undefined'; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | var type = typeof data; |
michael@0 | 150 | if (type === 'object' || type === 'Object') { |
michael@0 | 151 | type = leakHunt.getCtorName(data); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | return type; |
michael@0 | 155 | }; |
michael@0 | 156 | |
michael@0 | 157 | leakHunt.getCtorName = function LH_getCtorName(aObj) { |
michael@0 | 158 | try { |
michael@0 | 159 | if (aObj.constructor && aObj.constructor.name) { |
michael@0 | 160 | return aObj.constructor.name; |
michael@0 | 161 | } |
michael@0 | 162 | } |
michael@0 | 163 | catch (ex) { |
michael@0 | 164 | return 'UnknownObject'; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | // If that fails, use Objects toString which sometimes gives something |
michael@0 | 168 | // better than 'Object', and at least defaults to Object if nothing better |
michael@0 | 169 | return Object.prototype.toString.call(aObj).slice(8, -1); |
michael@0 | 170 | }; |