• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# Copyright 2019 the V8 project authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""This program writes a C++ file that can be used to look up whether a given
7address matches known object locations. The first argument is the directory
8containing the file v8heapconst.py; the second argument is the output .cc file.
9"""
10
11import sys
12sys.path.insert(0, sys.argv[1])
13import v8heapconst
14
15out = """
16#include <cstdint>
17#include <string>
18
19#include "src/common/ptr-compr-inl.h"
20#include "tools/debug_helper/debug-helper-internal.h"
21
22namespace v8 {
23namespace internal {
24namespace debug_helper_internal {
25"""
26
27def iterate_objects(target_space, camel_space_name):
28  global out
29  result = []
30  for (space, offset), (instance_type, name) in v8heapconst.KNOWN_MAPS.items():
31    if space == target_space:
32      result.append((offset, name))
33  for (space, offset), name in v8heapconst.KNOWN_OBJECTS.items():
34    if space == target_space and (space, offset) not in v8heapconst.KNOWN_MAPS:
35      result.append((offset, name))
36  out = out + '\nstd::string FindKnownObjectIn' + camel_space_name \
37      + '(uintptr_t offset) {\n  switch (offset) {\n'
38  for offset, name in result:
39    out = out + '    case ' + str(offset) + ': return "' + name + '";\n'
40  out = out + '    default: return "";\n  }\n}\n'
41
42iterate_objects('map_space', 'MapSpace')
43iterate_objects('old_space', 'OldSpace')
44iterate_objects('read_only_space', 'ReadOnlySpace')
45
46
47def iterate_maps(target_space, camel_space_name):
48  global out
49  out = out + '\nint FindKnownMapInstanceTypeIn' + camel_space_name \
50      + '(uintptr_t offset) {\n  switch (offset) {\n'
51  for (space, offset), (instance_type, name) in v8heapconst.KNOWN_MAPS.items():
52    if space == target_space:
53      out = out + '    case ' + str(offset) + ': return ' + str(instance_type) \
54          + ';\n'
55  out = out + '    default: return -1;\n  }\n}\n'
56
57iterate_maps('map_space', 'MapSpace')
58iterate_maps('old_space', 'OldSpace')
59iterate_maps('read_only_space', 'ReadOnlySpace')
60
61out = out + '\nvoid FillInUnknownHeapAddresses(' + \
62    'd::HeapAddresses* heap_addresses, uintptr_t any_uncompressed_ptr) {\n'
63if (hasattr(v8heapconst, 'HEAP_FIRST_PAGES')):  # Only exists in ptr-compr builds.
64  out = out + '  if (heap_addresses->any_heap_pointer == 0) {\n'
65  out = out + '    heap_addresses->any_heap_pointer = any_uncompressed_ptr;\n'
66  out = out + '  }\n'
67  expected_spaces = set(['map_space', 'read_only_space', 'old_space'])
68  for offset, space_name in v8heapconst.HEAP_FIRST_PAGES.items():
69    if (space_name in expected_spaces):
70      out = out + '  if (heap_addresses->' + space_name + '_first_page == 0) {\n'
71      out = out + '    heap_addresses->' + space_name + \
72          '_first_page = i::DecompressTaggedPointer(any_uncompressed_ptr, ' + \
73          str(offset) + ');\n'
74      out = out + '  }\n'
75out = out + '}\n'
76
77out = out + '\n}\n}\n}\n'
78
79try:
80  with open(sys.argv[2], "r") as out_file:
81    if out == out_file.read():
82      sys.exit(0)  # No modification needed.
83except:
84  pass  # File probably doesn't exist; write it.
85with open(sys.argv[2], "w") as out_file:
86  out_file.write(out)
87