xref: /third_party/node/deps/v8/tools/objdump-v8 (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci#!/usr/bin/env python
21cb0ef41Sopenharmony_ci#
31cb0ef41Sopenharmony_ci# Copyright 2016 the V8 project authors. All rights reserved.
41cb0ef41Sopenharmony_ci# Redistribution and use in source and binary forms, with or without
51cb0ef41Sopenharmony_ci# modification, are permitted provided that the following conditions are
61cb0ef41Sopenharmony_ci# met:
71cb0ef41Sopenharmony_ci#
81cb0ef41Sopenharmony_ci#     * Redistributions of source code must retain the above copyright
91cb0ef41Sopenharmony_ci#       notice, this list of conditions and the following disclaimer.
101cb0ef41Sopenharmony_ci#     * Redistributions in binary form must reproduce the above
111cb0ef41Sopenharmony_ci#       copyright notice, this list of conditions and the following
121cb0ef41Sopenharmony_ci#       disclaimer in the documentation and/or other materials provided
131cb0ef41Sopenharmony_ci#       with the distribution.
141cb0ef41Sopenharmony_ci#     * Neither the name of Google Inc. nor the names of its
151cb0ef41Sopenharmony_ci#       contributors may be used to endorse or promote products derived
161cb0ef41Sopenharmony_ci#       from this software without specific prior written permission.
171cb0ef41Sopenharmony_ci#
181cb0ef41Sopenharmony_ci# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
191cb0ef41Sopenharmony_ci# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
201cb0ef41Sopenharmony_ci# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
211cb0ef41Sopenharmony_ci# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
221cb0ef41Sopenharmony_ci# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
231cb0ef41Sopenharmony_ci# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
241cb0ef41Sopenharmony_ci# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
251cb0ef41Sopenharmony_ci# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
261cb0ef41Sopenharmony_ci# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
271cb0ef41Sopenharmony_ci# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
281cb0ef41Sopenharmony_ci# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciimport os.path
311cb0ef41Sopenharmony_ciimport re
321cb0ef41Sopenharmony_ciimport subprocess
331cb0ef41Sopenharmony_ciimport sys
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_cidef get_address_bounds():
371cb0ef41Sopenharmony_ci  start = -1
381cb0ef41Sopenharmony_ci  end = -1
391cb0ef41Sopenharmony_ci  for arg in sys.argv:
401cb0ef41Sopenharmony_ci    if arg.startswith("--start-address="):
411cb0ef41Sopenharmony_ci      start = int(arg[-12:], 16)
421cb0ef41Sopenharmony_ci    if arg.startswith("--stop-address="):
431cb0ef41Sopenharmony_ci      end = int(arg[-12:], 16)
441cb0ef41Sopenharmony_ci  return start, end
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_cidef format_line(line):
481cb0ef41Sopenharmony_ci  pieces = line.split(None, 3)
491cb0ef41Sopenharmony_ci  return " " + pieces[0][2:] + ":\t" + pieces[3]
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_cidef is_comment(line):
531cb0ef41Sopenharmony_ci  stripped = line.strip()
541cb0ef41Sopenharmony_ci  return stripped.startswith("--") or stripped.startswith(";;")
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_cidef main():
571cb0ef41Sopenharmony_ci  filename = sys.argv[-1]
581cb0ef41Sopenharmony_ci  match = re.match(r"/tmp/perf-(.*)\.map", filename)
591cb0ef41Sopenharmony_ci  if match:
601cb0ef41Sopenharmony_ci    start, end = get_address_bounds()
611cb0ef41Sopenharmony_ci    process_codefile = "code-" + match.group(1) + "-1.asm"
621cb0ef41Sopenharmony_ci    if os.path.exists(process_codefile):
631cb0ef41Sopenharmony_ci      codefile = open(process_codefile, "r")
641cb0ef41Sopenharmony_ci    else:
651cb0ef41Sopenharmony_ci      codefile = open("code.asm", "r")
661cb0ef41Sopenharmony_ci    with codefile:
671cb0ef41Sopenharmony_ci      printing = False
681cb0ef41Sopenharmony_ci      for line in codefile:
691cb0ef41Sopenharmony_ci        if line.startswith("0x"):
701cb0ef41Sopenharmony_ci          addr = int(line.split()[0], 0)
711cb0ef41Sopenharmony_ci          if start <= addr <= end:
721cb0ef41Sopenharmony_ci            printing = True
731cb0ef41Sopenharmony_ci            sys.stdout.write(format_line(line))
741cb0ef41Sopenharmony_ci          elif printing:
751cb0ef41Sopenharmony_ci            break
761cb0ef41Sopenharmony_ci        elif printing:
771cb0ef41Sopenharmony_ci          if not is_comment(line):
781cb0ef41Sopenharmony_ci            break
791cb0ef41Sopenharmony_ci          else:
801cb0ef41Sopenharmony_ci            sys.stdout.write(line)
811cb0ef41Sopenharmony_ci  else:
821cb0ef41Sopenharmony_ci    sys.argv[0] = "objdump"
831cb0ef41Sopenharmony_ci    sys.exit(subprocess.call(sys.argv))
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ciif __name__ == "__main__":
861cb0ef41Sopenharmony_ci  main()
87