1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 tvcm.require('tracing.importer.v8.splaytree');
10 * @fileoverview Map addresses to dynamically created functions.
13 tvcm.exportTo('tracing.importer.v8', function() {
15 * Constructs a mapper that maps addresses into code entries.
21 * Dynamic code entries. Used for JIT compiled code.
23 this.dynamics_ = new tracing.importer.v8.SplayTree();
26 * Name generator for entries having duplicate names.
28 this.dynamicsNameGen_ = new tracing.importer.v8.CodeMap.NameGenerator();
31 * Static code entries. Used for statically compiled code.
33 this.statics_ = new tracing.importer.v8.SplayTree();
36 * Libraries entries. Used for the whole static code libraries.
38 this.libraries_ = new tracing.importer.v8.SplayTree();
41 * Map of memory pages occupied with static code.
48 * The number of alignment bits in a page address.
50 CodeMap.PAGE_ALIGNMENT = 12;
57 1 << CodeMap.PAGE_ALIGNMENT;
61 * Adds a dynamic (i.e. moveable and discardable) code entry.
63 * @param {number} start The starting address.
64 * @param {CodeMap.CodeEntry} codeEntry Code entry object.
66 CodeMap.prototype.addCode = function(start, codeEntry) {
67 this.deleteAllCoveredNodes_(this.dynamics_, start, start + codeEntry.size);
68 this.dynamics_.insert(start, codeEntry);
73 * Moves a dynamic code entry. Throws an exception if there is no dynamic
74 * code entry with the specified starting address.
76 * @param {number} from The starting address of the entry being moved.
77 * @param {number} to The destination address.
79 CodeMap.prototype.moveCode = function(from, to) {
80 var removedNode = this.dynamics_.remove(from);
81 this.deleteAllCoveredNodes_(this.dynamics_, to,
82 to + removedNode.value.size);
83 this.dynamics_.insert(to, removedNode.value);
88 * Discards a dynamic code entry. Throws an exception if there is no dynamic
89 * code entry with the specified starting address.
91 * @param {number} start The starting address of the entry being deleted.
93 CodeMap.prototype.deleteCode = function(start) {
94 var removedNode = this.dynamics_.remove(start);
99 * Adds a library entry.
101 * @param {number} start The starting address.
102 * @param {CodeMap.CodeEntry} codeEntry Code entry object.
104 CodeMap.prototype.addLibrary = function(
106 this.markPages_(start, start + codeEntry.size);
107 this.libraries_.insert(start, codeEntry);
112 * Adds a static code entry.
114 * @param {number} start The starting address.
115 * @param {CodeMap.CodeEntry} codeEntry Code entry object.
117 CodeMap.prototype.addStaticCode = function(
119 this.statics_.insert(start, codeEntry);
126 CodeMap.prototype.markPages_ = function(start, end) {
127 for (var addr = start; addr <= end;
128 addr += CodeMap.PAGE_SIZE) {
129 this.pages_[addr >>> CodeMap.PAGE_ALIGNMENT] = 1;
137 CodeMap.prototype.deleteAllCoveredNodes_ = function(tree, start, end) {
140 while (addr >= start) {
141 var node = tree.findGreatestLessThan(addr);
143 var start2 = node.key, end2 = start2 + node.value.size;
144 if (start2 < end && start < end2) to_delete.push(start2);
147 for (var i = 0, l = to_delete.length; i < l; ++i) tree.remove(to_delete[i]);
154 CodeMap.prototype.isAddressBelongsTo_ = function(addr, node) {
155 return addr >= node.key && addr < (node.key + node.value.size);
162 CodeMap.prototype.findInTree_ = function(tree, addr) {
163 var node = tree.findGreatestLessThan(addr);
164 return node && this.isAddressBelongsTo_(addr, node) ? node.value : null;
169 * Finds a code entry that contains the specified address. Both static and
170 * dynamic code entries are considered.
172 * @param {number} addr Address.
174 CodeMap.prototype.findEntry = function(addr) {
175 var pageAddr = addr >>> CodeMap.PAGE_ALIGNMENT;
176 if (pageAddr in this.pages_) {
177 // Static code entries can contain "holes" of unnamed code.
178 // In this case, the whole library is assigned to this address.
179 return this.findInTree_(this.statics_, addr) ||
180 this.findInTree_(this.libraries_, addr);
182 var min = this.dynamics_.findMin();
183 var max = this.dynamics_.findMax();
184 if (max != null && addr < (max.key + max.value.size) && addr >= min.key) {
185 var dynaEntry = this.findInTree_(this.dynamics_, addr);
186 if (dynaEntry == null) return null;
187 // Dedupe entry name.
188 if (!dynaEntry.nameUpdated_) {
189 dynaEntry.name = this.dynamicsNameGen_.getName(dynaEntry.name);
190 dynaEntry.nameUpdated_ = true;
199 * Returns a dynamic code entry using its starting address.
201 * @param {number} addr Address.
203 CodeMap.prototype.findDynamicEntryByStartAddress =
205 var node = this.dynamics_.find(addr);
206 return node ? node.value : null;
211 * Returns an array of all dynamic code entries.
213 CodeMap.prototype.getAllDynamicEntries = function() {
214 return this.dynamics_.exportValues();
219 * Returns an array of pairs of all dynamic code entries and their addresses.
221 CodeMap.prototype.getAllDynamicEntriesWithAddresses = function() {
222 return this.dynamics_.exportKeysAndValues();
227 * Returns an array of all static code entries.
229 CodeMap.prototype.getAllStaticEntries = function() {
230 return this.statics_.exportValues();
235 * Returns an array of all libraries entries.
237 CodeMap.prototype.getAllLibrariesEntries = function() {
238 return this.libraries_.exportValues();
243 * Creates a code entry object.
245 * @param {number} size Code entry size in bytes.
246 * @param {string=} opt_name Code entry name.
249 CodeMap.CodeEntry = function(size, opt_name) {
250 this.id = tvcm.GUID.allocate();
252 this.name = opt_name || '';
253 this.nameUpdated_ = false;
257 CodeMap.CodeEntry.prototype.getName = function() {
262 CodeMap.CodeEntry.prototype.toString = function() {
263 return this.name + ': ' + this.size.toString(16);
267 CodeMap.NameGenerator = function() {
268 this.knownNames_ = {};
272 CodeMap.NameGenerator.prototype.getName = function(name) {
273 if (!(name in this.knownNames_)) {
274 this.knownNames_[name] = 0;
277 var count = ++this.knownNames_[name];
278 return name + ' {' + count + '}';