Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2011 the V8 project authors. All rights reserved. |
| 4 | # Redistribution and use in source and binary forms, with or without |
| 5 | # modification, are permitted provided that the following conditions are |
| 6 | # met: |
| 7 | # |
| 8 | # * Redistributions of source code must retain the above copyright |
| 9 | # notice, this list of conditions and the following disclaimer. |
| 10 | # * Redistributions in binary form must reproduce the above |
| 11 | # copyright notice, this list of conditions and the following |
| 12 | # disclaimer in the documentation and/or other materials provided |
| 13 | # with the distribution. |
| 14 | # * Neither the name of Google Inc. nor the names of its |
| 15 | # contributors may be used to endorse or promote products derived |
| 16 | # from this software without specific prior written permission. |
| 17 | # |
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | import os |
| 31 | import re |
| 32 | import subprocess |
| 33 | import tempfile |
| 34 | |
| 35 | |
| 36 | # Avoid using the slow (google-specific) wrapper around objdump. |
| 37 | OBJDUMP_BIN = "/usr/bin/objdump" |
| 38 | if not os.path.exists(OBJDUMP_BIN): |
| 39 | OBJDUMP_BIN = "objdump" |
| 40 | |
| 41 | |
| 42 | _COMMON_DISASM_OPTIONS = ["-M", "intel-mnemonic", "-C"] |
| 43 | |
| 44 | _DISASM_HEADER_RE = re.compile(r"[a-f0-9]+\s+<.*:$") |
| 45 | _DISASM_LINE_RE = re.compile(r"\s*([a-f0-9]+):\s*(\S.*)") |
| 46 | |
| 47 | # Keys must match constants in Logger::LogCodeInfo. |
| 48 | _ARCH_MAP = { |
| 49 | "ia32": "-m i386", |
| 50 | "x64": "-m i386 -M x86-64", |
| 51 | "arm": "-m arm" # Not supported by our objdump build. |
| 52 | } |
| 53 | |
| 54 | |
| 55 | def GetDisasmLines(filename, offset, size, arch, inplace): |
| 56 | tmp_name = None |
| 57 | if not inplace: |
| 58 | # Create a temporary file containing a copy of the code. |
| 59 | assert arch in _ARCH_MAP, "Unsupported architecture '%s'" % arch |
| 60 | arch_flags = _ARCH_MAP[arch] |
| 61 | tmp_name = tempfile.mktemp(".v8code") |
| 62 | command = "dd if=%s of=%s bs=1 count=%d skip=%d && " \ |
| 63 | "%s %s -D -b binary %s %s" % ( |
| 64 | filename, tmp_name, size, offset, |
| 65 | OBJDUMP_BIN, ' '.join(_COMMON_DISASM_OPTIONS), arch_flags, |
| 66 | tmp_name) |
| 67 | else: |
| 68 | command = "%s %s --start-address=%d --stop-address=%d -d %s " % ( |
| 69 | OBJDUMP_BIN, ' '.join(_COMMON_DISASM_OPTIONS), |
| 70 | offset, |
| 71 | offset + size, |
| 72 | filename) |
| 73 | process = subprocess.Popen(command, |
| 74 | shell=True, |
| 75 | stdout=subprocess.PIPE, |
| 76 | stderr=subprocess.STDOUT) |
| 77 | out, err = process.communicate() |
| 78 | lines = out.split("\n") |
| 79 | header_line = 0 |
| 80 | for i, line in enumerate(lines): |
| 81 | if _DISASM_HEADER_RE.match(line): |
| 82 | header_line = i |
| 83 | break |
| 84 | if tmp_name: |
| 85 | os.unlink(tmp_name) |
| 86 | split_lines = [] |
| 87 | for line in lines[header_line + 1:]: |
| 88 | match = _DISASM_LINE_RE.match(line) |
| 89 | if match: |
| 90 | line_address = int(match.group(1), 16) |
| 91 | split_lines.append((line_address, match.group(2))) |
| 92 | return split_lines |