karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame^] | 1 | -- Copyright 2011 the V8 project authors. All rights reserved. |
| 2 | -- Redistribution and use in source and binary forms, with or without |
| 3 | -- modification, are permitted provided that the following conditions are |
| 4 | -- met: |
| 5 | -- |
| 6 | -- * Redistributions of source code must retain the above copyright |
| 7 | -- notice, this list of conditions and the following disclaimer. |
| 8 | -- * Redistributions in binary form must reproduce the above |
| 9 | -- copyright notice, this list of conditions and the following |
| 10 | -- disclaimer in the documentation and/or other materials provided |
| 11 | -- with the distribution. |
| 12 | -- * Neither the name of Google Inc. nor the names of its |
| 13 | -- contributors may be used to endorse or promote products derived |
| 14 | -- from this software without specific prior written permission. |
| 15 | -- |
| 16 | -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | -- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | -- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | -- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | -- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | -- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | -- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | -- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | -- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | -- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | -- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | -- This is main driver for gcmole tool. See README for more details. |
| 29 | -- Usage: CLANG_BIN=clang-bin-dir lua tools/gcmole/gcmole.lua [arm|ia32|x64] |
| 30 | |
| 31 | local DIR = arg[0]:match("^(.+)/[^/]+$") |
| 32 | |
| 33 | local ARCHS = arg[1] and { arg[1] } or { 'ia32', 'arm', 'x64' } |
| 34 | |
| 35 | local io = require "io" |
| 36 | local os = require "os" |
| 37 | |
| 38 | function log(...) |
| 39 | io.stderr:write(string.format(...)) |
| 40 | io.stderr:write "\n" |
| 41 | end |
| 42 | |
| 43 | ------------------------------------------------------------------------------- |
| 44 | -- Clang invocation |
| 45 | |
| 46 | local CLANG_BIN = os.getenv "CLANG_BIN" |
| 47 | |
| 48 | if not CLANG_BIN or CLANG_BIN == "" then |
| 49 | error "CLANG_BIN not set" |
| 50 | end |
| 51 | |
| 52 | local function MakeClangCommandLine(plugin, triple, arch_define) |
| 53 | return CLANG_BIN .. "/clang -cc1 -load " .. DIR .. "/libgcmole.so" |
| 54 | .. " -plugin " .. plugin |
| 55 | .. " -triple " .. triple |
| 56 | .. " -D" .. arch_define |
| 57 | .. " -DENABLE_VMSTATE_TRACKING" |
| 58 | .. " -DENABLE_LOGGING_AND_PROFILING" |
| 59 | .. " -DENABLE_DEBUGGER_SUPPORT" |
| 60 | .. " -Isrc" |
| 61 | end |
| 62 | |
| 63 | function InvokeClangPluginForEachFile(filenames, cfg, func) |
| 64 | local cmd_line = MakeClangCommandLine(cfg.plugin, |
| 65 | cfg.triple, |
| 66 | cfg.arch_define) |
| 67 | |
| 68 | for _, filename in ipairs(filenames) do |
| 69 | log("-- %s", filename) |
| 70 | |
| 71 | local action = cmd_line .. " src/" .. filename .. " 2>&1" |
| 72 | |
| 73 | local pipe = io.popen(action) |
| 74 | func(filename, pipe:lines()) |
| 75 | pipe:close() |
| 76 | end |
| 77 | end |
| 78 | |
| 79 | ------------------------------------------------------------------------------- |
| 80 | -- SConscript parsing |
| 81 | |
| 82 | local function ParseSConscript() |
| 83 | local f = assert(io.open("src/SConscript"), "failed to open SConscript") |
| 84 | local sconscript = f:read('*a') |
| 85 | f:close() |
| 86 | |
| 87 | local SOURCES = sconscript:match "SOURCES = {(.-)}"; |
| 88 | |
| 89 | local sources = {} |
| 90 | |
| 91 | for condition, list in |
| 92 | SOURCES:gmatch "'([^']-)': Split%(\"\"\"(.-)\"\"\"%)" do |
| 93 | local files = {} |
| 94 | for file in list:gmatch "[^%s]+" do table.insert(files, file) end |
| 95 | sources[condition] = files |
| 96 | end |
| 97 | |
| 98 | for condition, list in SOURCES:gmatch "'([^']-)': %[(.-)%]" do |
| 99 | local files = {} |
| 100 | for file in list:gmatch "'([^']-)'" do table.insert(files, file) end |
| 101 | sources[condition] = files |
| 102 | end |
| 103 | |
| 104 | return sources |
| 105 | end |
| 106 | |
| 107 | local function EvaluateCondition(cond, props) |
| 108 | if cond == 'all' then return true end |
| 109 | |
| 110 | local p, v = cond:match "(%w+):(%w+)" |
| 111 | |
| 112 | assert(p and v, "failed to parse condition: " .. cond) |
| 113 | assert(props[p] ~= nil, "undefined configuration property: " .. p) |
| 114 | |
| 115 | return props[p] == v |
| 116 | end |
| 117 | |
| 118 | local function BuildFileList(sources, props) |
| 119 | local list = {} |
| 120 | for condition, files in pairs(sources) do |
| 121 | if EvaluateCondition(condition, props) then |
| 122 | for i = 1, #files do table.insert(list, files[i]) end |
| 123 | end |
| 124 | end |
| 125 | return list |
| 126 | end |
| 127 | |
| 128 | local sources = ParseSConscript() |
| 129 | |
| 130 | local function FilesForArch(arch) |
| 131 | return BuildFileList(sources, { os = 'linux', |
| 132 | arch = arch, |
| 133 | mode = 'debug', |
| 134 | simulator = ''}) |
| 135 | end |
| 136 | |
| 137 | local mtConfig = {} |
| 138 | |
| 139 | mtConfig.__index = mtConfig |
| 140 | |
| 141 | local function config (t) return setmetatable(t, mtConfig) end |
| 142 | |
| 143 | function mtConfig:extend(t) |
| 144 | local e = {} |
| 145 | for k, v in pairs(self) do e[k] = v end |
| 146 | for k, v in pairs(t) do e[k] = v end |
| 147 | return config(e) |
| 148 | end |
| 149 | |
| 150 | local ARCHITECTURES = { |
| 151 | ia32 = config { triple = "i586-unknown-linux", |
| 152 | arch_define = "V8_TARGET_ARCH_IA32" }, |
| 153 | arm = config { triple = "i586-unknown-linux", |
| 154 | arch_define = "V8_TARGET_ARCH_ARM" }, |
| 155 | x64 = config { triple = "x86_64-unknown-linux", |
| 156 | arch_define = "V8_TARGET_ARCH_X64" } |
| 157 | } |
| 158 | |
| 159 | ------------------------------------------------------------------------------- |
| 160 | -- GCSuspects Generation |
| 161 | |
| 162 | local gc = {} |
| 163 | local funcs = {} |
| 164 | |
| 165 | local function resolve(name) |
| 166 | local f = funcs[name] |
| 167 | |
| 168 | if not f then |
| 169 | f = {} |
| 170 | funcs[name] = f |
| 171 | |
| 172 | if name:match "Collect.*Garbage" then gc[name] = true end |
| 173 | end |
| 174 | |
| 175 | return f |
| 176 | end |
| 177 | |
| 178 | local function parse (filename, lines) |
| 179 | local scope |
| 180 | |
| 181 | for funcname in lines do |
| 182 | if funcname:sub(1, 1) ~= '\t' then |
| 183 | resolve(funcname) |
| 184 | scope = funcname |
| 185 | else |
| 186 | local name = funcname:sub(2) |
| 187 | resolve(name)[scope] = true |
| 188 | end |
| 189 | end |
| 190 | end |
| 191 | |
| 192 | local function propagate () |
| 193 | log "** Propagating GC information" |
| 194 | |
| 195 | local function mark(callers) |
| 196 | for caller, _ in pairs(callers) do |
| 197 | if not gc[caller] then |
| 198 | gc[caller] = true |
| 199 | mark(funcs[caller]) |
| 200 | end |
| 201 | end |
| 202 | end |
| 203 | |
| 204 | for funcname, callers in pairs(funcs) do |
| 205 | if gc[funcname] then mark(callers) end |
| 206 | end |
| 207 | end |
| 208 | |
| 209 | local function GenerateGCSuspects(arch, files, cfg) |
| 210 | log ("** Building GC Suspects for %s", arch) |
| 211 | InvokeClangPluginForEachFile (files, |
| 212 | cfg:extend { plugin = "dump-callees" }, |
| 213 | parse) |
| 214 | |
| 215 | propagate() |
| 216 | |
| 217 | local out = assert(io.open("gcsuspects", "w")) |
| 218 | for name, _ in pairs(gc) do out:write (name, '\n') end |
| 219 | out:close() |
| 220 | log ("** GCSuspects generated for %s", arch) |
| 221 | end |
| 222 | |
| 223 | ------------------------------------------------------------------------------- |
| 224 | -- Analysis |
| 225 | |
| 226 | local function CheckCorrectnessForArch(arch) |
| 227 | local files = FilesForArch(arch) |
| 228 | local cfg = ARCHITECTURES[arch] |
| 229 | |
| 230 | GenerateGCSuspects(arch, files, cfg) |
| 231 | |
| 232 | local processed_files = 0 |
| 233 | local errors_found = false |
| 234 | local function SearchForErrors(filename, lines) |
| 235 | processed_files = processed_files + 1 |
| 236 | for l in lines do |
| 237 | errors_found = errors_found or |
| 238 | l:match "^[^:]+:%d+:%d+:" or |
| 239 | l:match "error" or |
| 240 | l:match "warning" |
| 241 | print(l) |
| 242 | end |
| 243 | end |
| 244 | |
| 245 | log("** Searching for evaluation order problems for %s", arch) |
| 246 | InvokeClangPluginForEachFile(files, |
| 247 | cfg:extend { plugin = "find-problems" }, |
| 248 | SearchForErrors) |
| 249 | log("** Done processing %d files. %s", |
| 250 | processed_files, |
| 251 | errors_found and "Errors found" or "No errors found") |
| 252 | |
| 253 | return errors_found |
| 254 | end |
| 255 | |
| 256 | local function SafeCheckCorrectnessForArch(arch) |
| 257 | local status, errors = pcall(CheckCorrectnessForArch, arch) |
| 258 | if not status then |
| 259 | print(string.format("There was an error: %s", errors)) |
| 260 | errors = true |
| 261 | end |
| 262 | return errors |
| 263 | end |
| 264 | |
| 265 | local errors = false |
| 266 | |
| 267 | for _, arch in ipairs(ARCHS) do |
| 268 | if not ARCHITECTURES[arch] then |
| 269 | error ("Unknown arch: " .. arch) |
| 270 | end |
| 271 | |
| 272 | errors = SafeCheckCorrectnessForArch(arch, report) or errors |
| 273 | end |
| 274 | |
| 275 | os.exit(errors and 1 or 0) |