1 /**************************************************************************
3 * Copyright 2011-2012 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
31 #include "retrace.hpp"
32 #include "retrace_swizzle.hpp"
41 unsigned long long size;
44 typedef std::map<unsigned long long, Region> RegionMap;
45 static RegionMap regionMap;
49 contains(RegionMap::iterator &it, unsigned long long address) {
50 return it->first <= address && (it->first + it->second.size) > address;
55 intersects(RegionMap::iterator &it, unsigned long long start, unsigned long long size) {
56 unsigned long it_start = it->first;
57 unsigned long it_stop = it->first + it->second.size;
58 unsigned long stop = start + size;
59 return it_start < stop && start < it_stop;
63 // Iterator to the first region that contains the address, or the first after
64 static RegionMap::iterator
65 lowerBound(unsigned long long address) {
66 RegionMap::iterator it = regionMap.lower_bound(address);
68 while (it != regionMap.begin()) {
69 RegionMap::iterator pred = it;
71 if (contains(pred, address)) {
81 // Iterator to the first region that starts after the address
82 static RegionMap::iterator
83 upperBound(unsigned long long address) {
84 RegionMap::iterator it = regionMap.upper_bound(address);
90 addRegion(unsigned long long address, void *buffer, unsigned long long size)
92 if (retrace::verbosity >= 2) {
96 << "0x" << address << "-0x" << (address + size)
98 << "0x" << (uintptr_t)buffer << "-0x" << ((uintptr_t)buffer + size)
104 // Ignore NULL pointer
110 RegionMap::iterator start = lowerBound(address);
111 RegionMap::iterator stop = upperBound(address + size);
113 // Forget all regions that intersect this new one.
114 regionMap.erase(start, stop);
116 for (RegionMap::iterator it = start; it != stop; ++it) {
117 std::cerr << std::hex << "warning: "
118 "region 0x" << address << "-0x" << (address + size) << " "
119 "intersects existing region 0x" << it->first << "-0x" << (it->first + it->second.size) << "\n" << std::dec;
120 assert(intersects(it, address, size));
128 region.buffer = buffer;
131 regionMap[address] = region;
134 static RegionMap::iterator
135 lookupRegion(unsigned long long address) {
136 RegionMap::iterator it = regionMap.lower_bound(address);
138 if (it == regionMap.end() ||
139 it->first > address) {
140 if (it == regionMap.begin()) {
141 return regionMap.end();
147 assert(contains(it, address));
152 delRegion(unsigned long long address) {
153 RegionMap::iterator it = lookupRegion(address);
154 if (it != regionMap.end()) {
163 delRegionByPointer(void *ptr) {
164 for (RegionMap::iterator it = regionMap.begin(); it != regionMap.end(); ++it) {
165 if (it->second.buffer == ptr) {
174 lookupAddress(unsigned long long address) {
175 RegionMap::iterator it = lookupRegion(address);
176 if (it != regionMap.end()) {
177 unsigned long long offset = address - it->first;
178 assert(offset < it->second.size);
179 void *addr = (char *)it->second.buffer + offset;
181 if (retrace::verbosity >= 2) {
187 << "0x" << (uintptr_t)addr
195 if (retrace::debug && address >= 64 * 1024 * 1024) {
196 /* Likely not an offset, but an address that should had been swizzled */
197 std::cerr << "warning: passing high address 0x" << std::hex << address << std::dec << " as uintptr_t\n";
200 return (void *)(uintptr_t)address;
204 class Translator : protected trace::Visitor
211 void visit(trace::Null *) {
215 void visit(trace::Blob *blob) {
216 result = blob->toPointer(bind);
219 void visit(trace::Pointer *p) {
220 result = lookupAddress(p->value);
224 Translator(bool _bind) :
229 void * operator() (trace::Value *node) {
237 toPointer(trace::Value &value, bool bind) {
238 return Translator(bind) (&value);
243 static std::map<unsigned long long, void *> _obj_map;
246 addObj(trace::Value &value, void *obj) {
247 unsigned long long address = value.toUIntPtr();
248 _obj_map[address] = obj;
250 if (retrace::verbosity >= 2) {
251 std::cout << std::hex << "obj 0x" << address << " -> 0x" << size_t(obj) << std::dec << "\n";
256 delObj(trace::Value &value) {
257 unsigned long long address = value.toUIntPtr();
258 _obj_map.erase(address);
262 toObjPointer(trace::Value &value) {
263 unsigned long long address = value.toUIntPtr();
264 void *obj = address ? _obj_map[address] : NULL;
266 if (retrace::verbosity >= 2) {
267 std::cout << std::hex << "obj 0x" << address << " <- 0x" << size_t(obj) << std::dec << "\n";