PageRenderTime 296ms CodeModel.GetById 82ms app.highlight 14ms RepoModel.GetById 115ms app.codeStats 1ms

/indra/newview/generate_breakpad_symbols.py

https://bitbucket.org/lindenlab/viewer-beta/
Python | 136 lines | 82 code | 21 blank | 33 comment | 24 complexity | 6943890204bbc1e1bf1782bebb267d02 MD5 | raw file
  1#!/usr/bin/env python
  2"""\
  3@file generate_breakpad_symbols.py
  4@author Brad Kittenbrink <brad@lindenlab.com>
  5@brief Simple tool for generating google_breakpad symbol information
  6       for the crash reporter.
  7
  8$LicenseInfo:firstyear=2010&license=viewerlgpl$
  9Second Life Viewer Source Code
 10Copyright (C) 2010-2011, Linden Research, Inc.
 11
 12This library is free software; you can redistribute it and/or
 13modify it under the terms of the GNU Lesser General Public
 14License as published by the Free Software Foundation;
 15version 2.1 of the License only.
 16
 17This library is distributed in the hope that it will be useful,
 18but WITHOUT ANY WARRANTY; without even the implied warranty of
 19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 20Lesser General Public License for more details.
 21
 22You should have received a copy of the GNU Lesser General Public
 23License along with this library; if not, write to the Free Software
 24Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 25
 26Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 27$/LicenseInfo$
 28"""
 29
 30
 31import collections
 32import fnmatch
 33import itertools
 34import operator
 35import os
 36import re
 37import sys
 38import shlex
 39import subprocess
 40import tarfile
 41import StringIO
 42
 43def usage():
 44    print >>sys.stderr, "usage: %s viewer_dir viewer_exes libs_suffix dump_syms_tool viewer_symbol_file" % sys.argv[0]
 45
 46class MissingModuleError(Exception):
 47    def __init__(self, modules):
 48        Exception.__init__(self, "Failed to find required modules: %r" % modules)
 49        self.modules = modules
 50
 51def main(configuration, viewer_dir, viewer_exes, libs_suffix, dump_syms_tool, viewer_symbol_file):
 52    print "generate_breakpad_symbols run with args: %s" % str((configuration, viewer_dir, viewer_exes, libs_suffix, dump_syms_tool, viewer_symbol_file))
 53
 54    if not re.match("release", configuration, re.IGNORECASE):
 55        print "skipping breakpad symbol generation for non-release build."
 56        return 0
 57
 58    # split up list of viewer_exes
 59    # "'Second Life' SLPlugin" becomes ['Second Life', 'SLPlugin']
 60    viewer_exes = shlex.split(viewer_exes)
 61
 62    found_required = dict([(module, False) for module in viewer_exes])
 63
 64    def matches(f):
 65        if f in viewer_exes:
 66            found_required[f] = True
 67            return True
 68        return fnmatch.fnmatch(f, libs_suffix)
 69
 70    def list_files():
 71        for (dirname, subdirs, filenames) in os.walk(viewer_dir):
 72            #print "scanning '%s' for modules..." % dirname
 73            for f in itertools.ifilter(matches, filenames):
 74                yield os.path.join(dirname, f)
 75
 76    def dump_module(m):
 77        print "dumping module '%s' with '%s'..." % (m, dump_syms_tool)
 78        child = subprocess.Popen([dump_syms_tool, m] , stdout=subprocess.PIPE)
 79        out, err = child.communicate()
 80        return (m,child.returncode, out, err)
 81
 82    out = tarfile.open(viewer_symbol_file, 'w:bz2')
 83
 84    for (filename,status,symbols,err) in itertools.imap(dump_module, list_files()):
 85        if status == 0:
 86            module_line = symbols[:symbols.index('\n')]
 87            module_line = module_line.split()
 88            hash_id = module_line[3]
 89            module = ' '.join(module_line[4:])
 90            if sys.platform in ['win32', 'cygwin']:
 91                mod_name = module[:module.rindex('.pdb')]
 92            else:
 93                mod_name = module
 94            symbolfile = StringIO.StringIO(symbols)
 95            info = tarfile.TarInfo("%(module)s/%(hash_id)s/%(mod_name)s.sym" % dict(module=module, hash_id=hash_id, mod_name=mod_name))
 96            info.size = symbolfile.len
 97            out.addfile(info, symbolfile)
 98        else:
 99            print >>sys.stderr, "warning: failed to dump symbols for '%s': %s" % (filename, err)
100
101    out.close()
102
103    missing_modules = [m for (m,_) in
104        itertools.ifilter(lambda (k,v): not v, found_required.iteritems())
105    ]
106    if missing_modules:
107        print >> sys.stderr, "failed to generate %s" % viewer_symbol_file
108        os.remove(viewer_symbol_file)
109        raise MissingModuleError(missing_modules)
110
111    symbols = tarfile.open(viewer_symbol_file, 'r:bz2')
112    tarfile_members = symbols.getnames()
113    symbols.close()
114
115    for required_module in viewer_exes:
116        def match_module_basename(m):
117            return os.path.splitext(required_module)[0].lower() \
118                   == os.path.splitext(os.path.basename(m))[0].lower()
119        # there must be at least one .sym file in tarfile_members that matches
120        # each required module (ignoring file extensions)
121        if not reduce(operator.or_, itertools.imap(match_module_basename, tarfile_members)):
122            print >> sys.stderr, "failed to find required %s in generated %s" \
123                    % (required_module, viewer_symbol_file)
124            os.remove(viewer_symbol_file)
125            raise MissingModuleError([required_module])
126
127    print "successfully generated %s including required modules '%s'" % (viewer_symbol_file, viewer_exes)
128
129    return 0
130
131if __name__ == "__main__":
132    if len(sys.argv) != 7:
133        usage()
134        sys.exit(1)
135    sys.exit(main(*sys.argv[1:]))
136