PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/rb/fix_macosx_stack.py

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