Bug 597811: Make mozJSComponentLoader use JSVERSION_LATEST. (r=sayrer)
[mozilla-central.git] / tools / rb / fix_macosx_stack.py
blob017a38518e5cb29129077c66e0f9c7a5e59afecd
1 #!/usr/bin/python
2 # vim:sw=4:ts=4:et:
3 # ***** BEGIN LICENSE BLOCK *****
4 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 # The contents of this file are subject to the Mozilla Public License Version
7 # 1.1 (the "License"); you may not use this file except in compliance with
8 # the License. You may obtain a copy of the License at
9 # http://www.mozilla.org/MPL/
11 # Software distributed under the License is distributed on an "AS IS" basis,
12 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 # for the specific language governing rights and limitations under the
14 # License.
16 # The Original Code is fix-linux-stack.pl.
18 # The Initial Developer of the Original Code is L. David Baron.
19 # Portions created by the Initial Developer are Copyright (C) 2003
20 # the Initial Developer. All Rights Reserved.
22 # Contributor(s):
23 # L. David Baron <dbaron@dbaron.org> (original author)
25 # Alternatively, the contents of this file may be used under the terms of
26 # either the GNU General Public License Version 2 or later (the "GPL"), or
27 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 # in which case the provisions of the GPL or the LGPL are applicable instead
29 # of those above. If you wish to allow use of your version of this file only
30 # under the terms of either the GPL or the LGPL, and not to allow others to
31 # use your version of this file under the terms of the MPL, indicate your
32 # decision by deleting the provisions above and replace them with the notice
33 # and other provisions required by the GPL or the LGPL. If you do not delete
34 # the provisions above, a recipient may use your version of this file under
35 # the terms of any one of the MPL, the GPL or the LGPL.
37 # ***** END LICENSE BLOCK *****
39 # This script uses atos to process the output of nsTraceRefcnt's Mac OS
40 # X stack walking code. This is useful for two things:
41 # (1) Getting line number information out of
42 # |nsTraceRefcntImpl::WalkTheStack|'s output in debug builds.
43 # (2) Getting function names out of |nsTraceRefcntImpl::WalkTheStack|'s
44 # output on all builds (where it mostly prints UNKNOWN because only
45 # a handful of symbols are exported from component libraries).
47 # Use the script by piping output containing stacks (such as raw stacks
48 # or make-tree.pl balance trees) through this script.
50 import subprocess
51 import sys
52 import re
53 import os
54 import pty
55 import termios
57 class unbufferedLineConverter:
58 """
59 Wrap a child process that responds to each line of input with one line of
60 output. Uses pty to trick the child into providing unbuffered output.
61 """
62 def __init__(self, command, args = []):
63 pid, fd = pty.fork()
64 if pid == 0:
65 # We're the child. Transfer control to command.
66 os.execvp(command, [command] + args)
67 else:
68 # Disable echoing.
69 attr = termios.tcgetattr(fd)
70 attr[3] = attr[3] & ~termios.ECHO
71 termios.tcsetattr(fd, termios.TCSANOW, attr)
72 # Set up a file()-like interface to the child process
73 self.r = os.fdopen(fd, "r", 1)
74 self.w = os.fdopen(os.dup(fd), "w", 1)
75 def convert(self, line):
76 self.w.write(line + "\n")
77 return self.r.readline().rstrip("\r\n")
78 @staticmethod
79 def test():
80 assert unbufferedLineConverter("rev").convert("123") == "321"
81 assert unbufferedLineConverter("cut", ["-c3"]).convert("abcde") == "c"
82 print "Pass"
84 def separate_debug_file_for(file):
85 return None
87 address_adjustments = {}
88 def address_adjustment(file):
89 if not file in address_adjustments:
90 result = None
91 otool = subprocess.Popen(["otool", "-l", file], stdout=subprocess.PIPE)
92 while True:
93 line = otool.stdout.readline()
94 if line == "":
95 break
96 if line == " segname __TEXT\n":
97 line = otool.stdout.readline()
98 if not line.startswith(" vmaddr "):
99 raise StandardError("unexpected otool output")
100 result = int(line[10:], 16)
101 break
102 otool.stdout.close()
104 if result is None:
105 raise StandardError("unexpected otool output")
107 address_adjustments[file] = result
109 return address_adjustments[file]
111 atoses = {}
112 def addressToSymbol(file, address):
113 converter = None
114 if not file in atoses:
115 debug_file = separate_debug_file_for(file) or file
116 converter = unbufferedLineConverter('/usr/bin/atos', ['-o', debug_file])
117 atoses[file] = converter
118 else:
119 converter = atoses[file]
120 return converter.convert("0x%X" % address)
122 cxxfilt_proc = None
123 def cxxfilt(sym):
124 if cxxfilt_proc is None:
125 globals()["cxxfilt_proc"] = subprocess.Popen(['c++filt',
126 '--no-strip-underscores',
127 '--format', 'gnu-v3'],
128 stdin=subprocess.PIPE,
129 stdout=subprocess.PIPE)
130 # strip underscores ourselves (works better than c++filt's
131 # --strip-underscores)
132 cxxfilt_proc.stdin.write(sym[1:] + "\n")
133 return cxxfilt_proc.stdout.readline().rstrip("\n")
135 line_re = re.compile("^(.*) ?\[([^ ]*) \+(0x[0-9A-F]{1,8})\](.*)$")
136 balance_tree_re = re.compile("^([ \|0-9-]*)")
137 atos_sym_re = re.compile("^(\S+) \(in ([^)]+)\) \((.+)\)$")
139 def fixSymbols(line):
140 result = line_re.match(line)
141 if result is not None:
142 # before allows preservation of balance trees
143 # after allows preservation of counts
144 (before, file, address, after) = result.groups()
145 address = int(address, 16)
147 if os.path.exists(file) and os.path.isfile(file):
148 address += address_adjustment(file)
149 info = addressToSymbol(file, address)
151 # atos output seems to have three forms:
152 # address
153 # address (in foo.dylib)
154 # symbol (in foo.dylib) (file:line)
155 symresult = atos_sym_re.match(info)
156 if symresult is not None:
157 # Print the first two forms as-is, and transform the third
158 (symbol, library, fileline) = symresult.groups()
159 symbol = cxxfilt(symbol)
160 info = "%s (%s, in %s)" % (symbol, fileline, library)
162 # throw away the bad symbol, but keep balance tree structure
163 before = balance_tree_re.match(before).groups()[0]
165 return before + info + after + "\n"
166 else:
167 sys.stderr.write("Warning: File \"" + file + "\" does not exist.\n")
168 return line
169 else:
170 return line
172 if __name__ == "__main__":
173 for line in sys.stdin:
174 sys.stdout.write(fixSymbols(line))