codemap.js 8.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
// Copyright 2009 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


/**
 * Constructs a mapper that maps addresses into code entries.
 *
 * @constructor
 */
34
function CodeMap() {
35 36 37
  /**
   * Dynamic code entries. Used for JIT compiled code.
   */
38
  this.dynamics_ = new SplayTree();
39 40

  /**
41
   * Name generator for entries having duplicate names.
42
   */
43
  this.dynamicsNameGen_ = new CodeMap.NameGenerator();
44

45
  /**
46
   * Static code entries. Used for statically compiled code.
47
   */
48
  this.statics_ = new SplayTree();
49

50 51 52
  /**
   * Libraries entries. Used for the whole static code libraries.
   */
53
  this.libraries_ = new SplayTree();
54

55 56 57 58 59 60 61 62 63 64
  /**
   * Map of memory pages occupied with static code.
   */
  this.pages_ = [];
};


/**
 * The number of alignment bits in a page address.
 */
65
CodeMap.PAGE_ALIGNMENT = 12;
66 67 68 69 70


/**
 * Page size in bytes.
 */
71 72
CodeMap.PAGE_SIZE =
    1 << CodeMap.PAGE_ALIGNMENT;
73 74 75 76 77 78


/**
 * Adds a dynamic (i.e. moveable and discardable) code entry.
 *
 * @param {number} start The starting address.
79
 * @param {CodeMap.CodeEntry} codeEntry Code entry object.
80
 */
81
CodeMap.prototype.addCode = function(start, codeEntry) {
82
  this.deleteAllCoveredNodes_(this.dynamics_, start, start + codeEntry.size);
83 84 85 86 87 88 89 90 91 92 93
  this.dynamics_.insert(start, codeEntry);
};


/**
 * Moves a dynamic code entry. Throws an exception if there is no dynamic
 * code entry with the specified starting address.
 *
 * @param {number} from The starting address of the entry being moved.
 * @param {number} to The destination address.
 */
94
CodeMap.prototype.moveCode = function(from, to) {
95
  var removedNode = this.dynamics_.remove(from);
96
  this.deleteAllCoveredNodes_(this.dynamics_, to, to + removedNode.value.size);
97 98 99 100 101 102
  this.dynamics_.insert(to, removedNode.value);
};


/**
 * Discards a dynamic code entry. Throws an exception if there is no dynamic
103
 * code entry with the specified starting address.
104 105 106
 *
 * @param {number} start The starting address of the entry being deleted.
 */
107
CodeMap.prototype.deleteCode = function(start) {
108 109 110 111
  var removedNode = this.dynamics_.remove(start);
};


112 113 114 115
/**
 * Adds a library entry.
 *
 * @param {number} start The starting address.
116
 * @param {CodeMap.CodeEntry} codeEntry Code entry object.
117
 */
118
CodeMap.prototype.addLibrary = function(
119 120 121 122 123 124
    start, codeEntry) {
  this.markPages_(start, start + codeEntry.size);
  this.libraries_.insert(start, codeEntry);
};


125 126 127 128
/**
 * Adds a static code entry.
 *
 * @param {number} start The starting address.
129
 * @param {CodeMap.CodeEntry} codeEntry Code entry object.
130
 */
131
CodeMap.prototype.addStaticCode = function(
132 133 134 135 136 137 138 139
    start, codeEntry) {
  this.statics_.insert(start, codeEntry);
};


/**
 * @private
 */
140
CodeMap.prototype.markPages_ = function(start, end) {
141
  for (var addr = start; addr <= end;
142
       addr += CodeMap.PAGE_SIZE) {
143
    this.pages_[(addr / CodeMap.PAGE_SIZE)|0] = 1;
144 145 146 147
  }
};


148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
/**
 * @private
 */
CodeMap.prototype.deleteAllCoveredNodes_ = function(tree, start, end) {
  var to_delete = [];
  var addr = end - 1;
  while (addr >= start) {
    var node = tree.findGreatestLessThan(addr);
    if (!node) break;
    var start2 = node.key, end2 = start2 + node.value.size;
    if (start2 < end && start < end2) to_delete.push(start2);
    addr = start2 - 1;
  }
  for (var i = 0, l = to_delete.length; i < l; ++i) tree.remove(to_delete[i]);
};


165 166 167
/**
 * @private
 */
168
CodeMap.prototype.isAddressBelongsTo_ = function(addr, node) {
169 170 171 172 173 174 175
  return addr >= node.key && addr < (node.key + node.value.size);
};


/**
 * @private
 */
176
CodeMap.prototype.findInTree_ = function(tree, addr) {
177
  var node = tree.findGreatestLessThan(addr);
178
  return node && this.isAddressBelongsTo_(addr, node) ? node : null;
179 180 181 182 183
};


/**
 * Finds a code entry that contains the specified address. Both static and
184 185
 * dynamic code entries are considered. Returns the code entry and the offset
 * within the entry.
186 187 188
 *
 * @param {number} addr Address.
 */
189
CodeMap.prototype.findAddress = function(addr) {
190
  var pageAddr = (addr / CodeMap.PAGE_SIZE)|0;
191
  if (pageAddr in this.pages_) {
192 193
    // Static code entries can contain "holes" of unnamed code.
    // In this case, the whole library is assigned to this address.
194 195 196 197 198
    var result = this.findInTree_(this.statics_, addr);
    if (!result) {
      result = this.findInTree_(this.libraries_, addr);
      if (!result) return null;
    }
199
    return { entry : result.value, offset : addr - result.key };
200 201 202 203
  }
  var min = this.dynamics_.findMin();
  var max = this.dynamics_.findMax();
  if (max != null && addr < (max.key + max.value.size) && addr >= min.key) {
204 205 206
    var dynaEntry = this.findInTree_(this.dynamics_, addr);
    if (dynaEntry == null) return null;
    // Dedupe entry name.
207 208 209 210
    var entry = dynaEntry.value;
    if (!entry.nameUpdated_) {
      entry.name = this.dynamicsNameGen_.getName(entry.name);
      entry.nameUpdated_ = true;
211
    }
212
    return { entry : entry, offset : addr - dynaEntry.key };
213 214 215 216 217
  }
  return null;
};


218 219 220 221 222 223 224 225 226 227 228 229
/**
 * Finds a code entry that contains the specified address. Both static and
 * dynamic code entries are considered.
 *
 * @param {number} addr Address.
 */
CodeMap.prototype.findEntry = function(addr) {
  var result = this.findAddress(addr);
  return result ? result.entry : null;
};


230 231 232 233 234
/**
 * Returns a dynamic code entry using its starting address.
 *
 * @param {number} addr Address.
 */
235
CodeMap.prototype.findDynamicEntryByStartAddress =
236 237 238 239 240 241
    function(addr) {
  var node = this.dynamics_.find(addr);
  return node ? node.value : null;
};


242
/**
243
 * Returns an array of all dynamic code entries.
244
 */
245
CodeMap.prototype.getAllDynamicEntries = function() {
246
  return this.dynamics_.exportValues();
247 248 249
};


250 251 252 253 254 255 256 257
/**
 * Returns an array of pairs of all dynamic code entries and their addresses.
 */
CodeMap.prototype.getAllDynamicEntriesWithAddresses = function() {
  return this.dynamics_.exportKeysAndValues();
};


258 259 260
/**
 * Returns an array of all static code entries.
 */
261
CodeMap.prototype.getAllStaticEntries = function() {
262 263 264 265
  return this.statics_.exportValues();
};


266 267 268 269 270 271 272 273
/**
 * Returns an array of pairs of all static code entries and their addresses.
 */
CodeMap.prototype.getAllStaticEntriesWithAddresses = function() {
  return this.statics_.exportKeysAndValues();
};


274 275 276
/**
 * Returns an array of all libraries entries.
 */
277
CodeMap.prototype.getAllLibrariesEntries = function() {
278 279 280 281
  return this.libraries_.exportValues();
};


282 283 284 285 286
/**
 * Creates a code entry object.
 *
 * @param {number} size Code entry size in bytes.
 * @param {string} opt_name Code entry name.
287
 * @param {string} opt_type Code entry type, e.g. SHARED_LIB, CPP.
288 289
 * @constructor
 */
290
CodeMap.CodeEntry = function(size, opt_name, opt_type) {
291 292
  this.size = size;
  this.name = opt_name || '';
293
  this.type = opt_type || '';
294
  this.nameUpdated_ = false;
295 296 297
};


298
CodeMap.CodeEntry.prototype.getName = function() {
299 300 301
  return this.name;
};

302

303
CodeMap.CodeEntry.prototype.toString = function() {
304 305
  return this.name + ': ' + this.size.toString(16);
};
306 307


308
CodeMap.NameGenerator = function() {
309
  this.knownNames_ = {};
310 311 312
};


313
CodeMap.NameGenerator.prototype.getName = function(name) {
314 315 316 317 318 319 320
  if (!(name in this.knownNames_)) {
    this.knownNames_[name] = 0;
    return name;
  }
  var count = ++this.knownNames_[name];
  return name + ' {' + count + '}';
};