1#!/usr/bin/env python
2#
3# Copyright 2016 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
30import os.path
31import re
32import subprocess
33import sys
34
35
36def get_address_bounds():
37  start = -1
38  end = -1
39  for arg in sys.argv:
40    if arg.startswith("--start-address="):
41      start = int(arg[-12:], 16)
42    if arg.startswith("--stop-address="):
43      end = int(arg[-12:], 16)
44  return start, end
45
46
47def format_line(line):
48  pieces = line.split(None, 3)
49  return " " + pieces[0][2:] + ":\t" + pieces[3]
50
51
52def is_comment(line):
53  stripped = line.strip()
54  return stripped.startswith("--") or stripped.startswith(";;")
55
56def main():
57  filename = sys.argv[-1]
58  match = re.match(r"/tmp/perf-(.*)\.map", filename)
59  if match:
60    start, end = get_address_bounds()
61    process_codefile = "code-" + match.group(1) + "-1.asm"
62    if os.path.exists(process_codefile):
63      codefile = open(process_codefile, "r")
64    else:
65      codefile = open("code.asm", "r")
66    with codefile:
67      printing = False
68      for line in codefile:
69        if line.startswith("0x"):
70          addr = int(line.split()[0], 0)
71          if start <= addr <= end:
72            printing = True
73            sys.stdout.write(format_line(line))
74          elif printing:
75            break
76        elif printing:
77          if not is_comment(line):
78            break
79          else:
80            sys.stdout.write(line)
81  else:
82    sys.argv[0] = "objdump"
83    sys.exit(subprocess.call(sys.argv))
84
85if __name__ == "__main__":
86  main()
87