3 Copyright (c) 2012 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
8 <link rel="import" href="/tracing/importer/v8/splaytree.html">
14 * @fileoverview Map addresses to dynamically created functions.
16 tvcm.exportTo('tracing.importer.v8', function() {
18 * Constructs a mapper that maps addresses into code entries.
24 * Dynamic code entries. Used for JIT compiled code.
26 this.dynamics_ = new tracing.importer.v8.SplayTree();
29 * Name generator for entries having duplicate names.
31 this.dynamicsNameGen_ = new tracing.importer.v8.CodeMap.NameGenerator();
34 * Static code entries. Used for statically compiled code.
36 this.statics_ = new tracing.importer.v8.SplayTree();
39 * Libraries entries. Used for the whole static code libraries.
41 this.libraries_ = new tracing.importer.v8.SplayTree();
44 * Map of memory pages occupied with static code.
50 * The number of alignment bits in a page address.
52 CodeMap.PAGE_ALIGNMENT = 12;
57 CodeMap.PAGE_SIZE = 1 << CodeMap.PAGE_ALIGNMENT;
60 * Adds a dynamic (i.e. moveable and discardable) code entry.
62 * @param {number} start The starting address.
63 * @param {CodeMap.CodeEntry} codeEntry Code entry object.
65 CodeMap.prototype.addCode = function(start, codeEntry) {
66 this.deleteAllCoveredNodes_(this.dynamics_, start, start + codeEntry.size);
67 this.dynamics_.insert(start, codeEntry);
71 * Moves a dynamic code entry. Throws an exception if there is no dynamic
72 * code entry with the specified starting address.
74 * @param {number} from The starting address of the entry being moved.
75 * @param {number} to The destination address.
77 CodeMap.prototype.moveCode = function(from, to) {
78 var removedNode = this.dynamics_.remove(from);
79 this.deleteAllCoveredNodes_(this.dynamics_, to,
80 to + removedNode.value.size);
81 this.dynamics_.insert(to, removedNode.value);
85 * Discards a dynamic code entry. Throws an exception if there is no dynamic
86 * code entry with the specified starting address.
88 * @param {number} start The starting address of the entry being deleted.
90 CodeMap.prototype.deleteCode = function(start) {
91 var removedNode = this.dynamics_.remove(start);
95 * Adds a library entry.
97 * @param {number} start The starting address.
98 * @param {CodeMap.CodeEntry} codeEntry Code entry object.
100 CodeMap.prototype.addLibrary = function(
102 this.markPages_(start, start + codeEntry.size);
103 this.libraries_.insert(start, codeEntry);
107 * Adds a static code entry.
109 * @param {number} start The starting address.
110 * @param {CodeMap.CodeEntry} codeEntry Code entry object.
112 CodeMap.prototype.addStaticCode = function(
114 this.statics_.insert(start, codeEntry);
120 CodeMap.prototype.markPages_ = function(start, end) {
121 for (var addr = start; addr <= end;
122 addr += CodeMap.PAGE_SIZE) {
123 this.pages_[addr >>> CodeMap.PAGE_ALIGNMENT] = 1;
130 CodeMap.prototype.deleteAllCoveredNodes_ = function(tree, start, end) {
133 while (addr >= start) {
134 var node = tree.findGreatestLessThan(addr);
136 var start2 = node.key, end2 = start2 + node.value.size;
137 if (start2 < end && start < end2) to_delete.push(start2);
140 for (var i = 0, l = to_delete.length; i < l; ++i) tree.remove(to_delete[i]);
146 CodeMap.prototype.isAddressBelongsTo_ = function(addr, node) {
147 return addr >= node.key && addr < (node.key + node.value.size);
153 CodeMap.prototype.findInTree_ = function(tree, addr) {
154 var node = tree.findGreatestLessThan(addr);
155 return node && this.isAddressBelongsTo_(addr, node) ? node.value : null;
159 * Finds a code entry that contains the specified address. Both static and
160 * dynamic code entries are considered.
162 * @param {number} addr Address.
164 CodeMap.prototype.findEntry = function(addr) {
165 var pageAddr = addr >>> CodeMap.PAGE_ALIGNMENT;
166 if (pageAddr in this.pages_) {
167 // Static code entries can contain "holes" of unnamed code.
168 // In this case, the whole library is assigned to this address.
169 return this.findInTree_(this.statics_, addr) ||
170 this.findInTree_(this.libraries_, addr);
172 var min = this.dynamics_.findMin();
173 var max = this.dynamics_.findMax();
174 if (max != null && addr < (max.key + max.value.size) && addr >= min.key) {
175 var dynaEntry = this.findInTree_(this.dynamics_, addr);
176 if (dynaEntry == null) return null;
177 // Dedupe entry name.
178 if (!dynaEntry.nameUpdated_) {
179 dynaEntry.name = this.dynamicsNameGen_.getName(dynaEntry.name);
180 dynaEntry.nameUpdated_ = true;
188 * Returns a dynamic code entry using its starting address.
190 * @param {number} addr Address.
192 CodeMap.prototype.findDynamicEntryByStartAddress =
194 var node = this.dynamics_.find(addr);
195 return node ? node.value : null;
199 * Returns an array of all dynamic code entries.
201 CodeMap.prototype.getAllDynamicEntries = function() {
202 return this.dynamics_.exportValues();
206 * Returns an array of pairs of all dynamic code entries and their addresses.
208 CodeMap.prototype.getAllDynamicEntriesWithAddresses = function() {
209 return this.dynamics_.exportKeysAndValues();
213 * Returns an array of all static code entries.
215 CodeMap.prototype.getAllStaticEntries = function() {
216 return this.statics_.exportValues();
220 * Returns an array of all libraries entries.
222 CodeMap.prototype.getAllLibrariesEntries = function() {
223 return this.libraries_.exportValues();
227 * Creates a code entry object.
229 * @param {number} size Code entry size in bytes.
230 * @param {string=} opt_name Code entry name.
233 CodeMap.CodeEntry = function(size, opt_name) {
234 this.id = tvcm.GUID.allocate();
236 this.name = opt_name || '';
237 this.nameUpdated_ = false;
240 CodeMap.CodeEntry.prototype.getName = function() {
244 CodeMap.CodeEntry.prototype.toString = function() {
245 return this.name + ': ' + this.size.toString(16);
248 CodeMap.NameGenerator = function() {
249 this.knownNames_ = {};
252 CodeMap.NameGenerator.prototype.getName = function(name) {
253 if (!(name in this.knownNames_)) {
254 this.knownNames_[name] = 0;
257 var count = ++this.knownNames_[name];
258 return name + ' {' + count + '}';