PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/rb/fix_macosx_stack.py

https://bitbucket.org/soko/mozilla-central
Python | 174 lines | 115 code | 5 blank | 54 comment | 5 complexity | b448df543bd055c676887089f827504e MD5 | raw file
Possible License(s): GPL-2.0, JSON, 0BSD, LGPL-3.0, AGPL-1.0, MIT, MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.1, Apache-2.0
  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
  5. #
  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/
  10. #
  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.
  15. #
  16. # The Original Code is fix-linux-stack.pl.
  17. #
  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.
  21. #
  22. # Contributor(s):
  23. # L. David Baron <dbaron@dbaron.org> (original author)
  24. #
  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.
  36. #
  37. # ***** END LICENSE BLOCK *****
  38. # This script uses atos to process the output of nsTraceRefcnt's Mac OS
  39. # X stack walking code. This is useful for two things:
  40. # (1) Getting line number information out of
  41. # |nsTraceRefcntImpl::WalkTheStack|'s output in debug builds.
  42. # (2) Getting function names out of |nsTraceRefcntImpl::WalkTheStack|'s
  43. # output on all builds (where it mostly prints UNKNOWN because only
  44. # a handful of symbols are exported from component libraries).
  45. #
  46. # Use the script by piping output containing stacks (such as raw stacks
  47. # or make-tree.pl balance trees) through this script.
  48. import subprocess
  49. import sys
  50. import re
  51. import os
  52. import pty
  53. import termios
  54. class unbufferedLineConverter:
  55. """
  56. Wrap a child process that responds to each line of input with one line of
  57. output. Uses pty to trick the child into providing unbuffered output.
  58. """
  59. def __init__(self, command, args = []):
  60. pid, fd = pty.fork()
  61. if pid == 0:
  62. # We're the child. Transfer control to command.
  63. os.execvp(command, [command] + args)
  64. else:
  65. # Disable echoing.
  66. attr = termios.tcgetattr(fd)
  67. attr[3] = attr[3] & ~termios.ECHO
  68. termios.tcsetattr(fd, termios.TCSANOW, attr)
  69. # Set up a file()-like interface to the child process
  70. self.r = os.fdopen(fd, "r", 1)
  71. self.w = os.fdopen(os.dup(fd), "w", 1)
  72. def convert(self, line):
  73. self.w.write(line + "\n")
  74. return self.r.readline().rstrip("\r\n")
  75. @staticmethod
  76. def test():
  77. assert unbufferedLineConverter("rev").convert("123") == "321"
  78. assert unbufferedLineConverter("cut", ["-c3"]).convert("abcde") == "c"
  79. print "Pass"
  80. def separate_debug_file_for(file):
  81. return None
  82. address_adjustments = {}
  83. def address_adjustment(file):
  84. if not file in address_adjustments:
  85. result = None
  86. otool = subprocess.Popen(["otool", "-l", file], stdout=subprocess.PIPE)
  87. while True:
  88. line = otool.stdout.readline()
  89. if line == "":
  90. break
  91. if line == " segname __TEXT\n":
  92. line = otool.stdout.readline()
  93. if not line.startswith(" vmaddr "):
  94. raise StandardError("unexpected otool output")
  95. result = int(line[10:], 16)
  96. break
  97. otool.stdout.close()
  98. if result is None:
  99. raise StandardError("unexpected otool output")
  100. address_adjustments[file] = result
  101. return address_adjustments[file]
  102. atoses = {}
  103. def addressToSymbol(file, address):
  104. converter = None
  105. if not file in atoses:
  106. debug_file = separate_debug_file_for(file) or file
  107. converter = unbufferedLineConverter('/usr/bin/atos', ['-o', debug_file])
  108. atoses[file] = converter
  109. else:
  110. converter = atoses[file]
  111. return converter.convert("0x%X" % address)
  112. cxxfilt_proc = None
  113. def cxxfilt(sym):
  114. if cxxfilt_proc is None:
  115. globals()["cxxfilt_proc"] = subprocess.Popen(['c++filt',
  116. '--no-strip-underscores',
  117. '--format', 'gnu-v3'],
  118. stdin=subprocess.PIPE,
  119. stdout=subprocess.PIPE)
  120. # strip underscores ourselves (works better than c++filt's
  121. # --strip-underscores)
  122. cxxfilt_proc.stdin.write(sym[1:] + "\n")
  123. return cxxfilt_proc.stdout.readline().rstrip("\n")
  124. line_re = re.compile("^(.*) ?\[([^ ]*) \+(0x[0-9A-F]{1,8})\](.*)$")
  125. balance_tree_re = re.compile("^([ \|0-9-]*)")
  126. atos_sym_re = re.compile("^(\S+) \(in ([^)]+)\) \((.+)\)$")
  127. def fixSymbols(line):
  128. result = line_re.match(line)
  129. if result is not None:
  130. # before allows preservation of balance trees
  131. # after allows preservation of counts
  132. (before, file, address, after) = result.groups()
  133. address = int(address, 16)
  134. if os.path.exists(file) and os.path.isfile(file):
  135. address += address_adjustment(file)
  136. info = addressToSymbol(file, address)
  137. # atos output seems to have three forms:
  138. # address
  139. # address (in foo.dylib)
  140. # symbol (in foo.dylib) (file:line)
  141. symresult = atos_sym_re.match(info)
  142. if symresult is not None:
  143. # Print the first two forms as-is, and transform the third
  144. (symbol, library, fileline) = symresult.groups()
  145. symbol = cxxfilt(symbol)
  146. info = "%s (%s, in %s)" % (symbol, fileline, library)
  147. # throw away the bad symbol, but keep balance tree structure
  148. before = balance_tree_re.match(before).groups()[0]
  149. return before + info + after + "\n"
  150. else:
  151. sys.stderr.write("Warning: File \"" + file + "\" does not exist.\n")
  152. return line
  153. else:
  154. return line
  155. if __name__ == "__main__":
  156. for line in sys.stdin:
  157. sys.stdout.write(fixSymbols(line))