1 /**************************************************************************
3 * Copyright 2011 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 **************************************************************************/
33 #include "trace_parser.hpp"
34 #include "retrace.hpp"
42 unsigned long long size;
45 typedef std::map<unsigned long long, Region> RegionMap;
46 static RegionMap regionMap;
50 contains(RegionMap::iterator &it, unsigned long long address) {
51 return it->first <= address && (it->first + it->second.size) > address;
56 intersects(RegionMap::iterator &it, unsigned long long start, unsigned long long size) {
57 unsigned long it_start = it->first;
58 unsigned long it_stop = it->first + it->second.size;
59 unsigned long stop = start + size;
60 return it_start < stop && start < it_stop;
64 // Iterator to the first region that contains the address, or the first after
65 static RegionMap::iterator
66 lowerBound(unsigned long long address) {
67 RegionMap::iterator it = regionMap.lower_bound(address);
69 while (it != regionMap.begin()) {
70 RegionMap::iterator pred = it;
72 if (contains(pred, address)) {
82 // Iterator to the first region that starts after the address
83 static RegionMap::iterator
84 upperBound(unsigned long long address) {
85 RegionMap::iterator it = regionMap.upper_bound(address);
91 addRegion(unsigned long long address, void *buffer, unsigned long long size)
94 // Ignore NULL pointer
100 RegionMap::iterator start = lowerBound(address);
101 RegionMap::iterator stop = upperBound(address + size);
103 // Forget all regions that intersect this new one.
104 regionMap.erase(start, stop);
106 for (RegionMap::iterator it = start; it != stop; ++it) {
107 std::cerr << std::hex << "warning: "
108 "region 0x" << address << "-0x" << (address + size) << " "
109 "intersects existing region 0x" << it->first << "-0x" << (it->first + it->second.size) << "\n" << std::dec;
110 assert(intersects(it, address, size));
118 region.buffer = buffer;
121 regionMap[address] = region;
124 static RegionMap::iterator
125 lookupRegion(unsigned long long address) {
126 RegionMap::iterator it = regionMap.lower_bound(address);
128 if (it == regionMap.end() ||
129 it->first > address) {
130 if (it == regionMap.begin()) {
131 return regionMap.end();
137 assert(contains(it, address));
142 delRegion(unsigned long long address) {
143 RegionMap::iterator it = lookupRegion(address);
144 if (it != regionMap.end()) {
153 delRegionByPointer(void *ptr) {
154 for (RegionMap::iterator it = regionMap.begin(); it != regionMap.end(); ++it) {
155 if (it->second.buffer == ptr) {
164 lookupAddress(unsigned long long address) {
165 RegionMap::iterator it = lookupRegion(address);
166 if (it != regionMap.end()) {
167 unsigned long long offset = address - it->first;
168 assert(offset < it->second.size);
169 return (char *)it->second.buffer + offset;
172 if (address >= 0x00400000) {
173 std::cerr << "warning: could not translate address 0x" << std::hex << address << std::dec << "\n";
176 return (void *)(uintptr_t)address;
180 class Translator : protected trace::Visitor
187 void visit(trace::Null *) {
191 void visit(trace::Blob *blob) {
192 result = blob->toPointer(bind);
195 void visit(trace::Pointer *p) {
196 result = lookupAddress(p->value);
200 Translator(bool _bind) :
205 void * operator() (trace::Value *node) {
213 toPointer(trace::Value &value, bool bind) {
214 return Translator(bind) (&value);
218 static void retrace_malloc(trace::Call &call) {
219 size_t size = call.arg(0).toUInt();
220 unsigned long long address = call.ret->toUIntPtr();
226 void *buffer = malloc(size);
228 std::cerr << "error: failed to allocated " << size << " bytes.";
232 addRegion(address, buffer, size);
236 static void retrace_memcpy(trace::Call &call) {
237 void * dest = toPointer(call.arg(0));
238 void * src = toPointer(call.arg(1));
239 size_t n = call.arg(2).toUInt();
241 if (!dest || !src || !n) {
245 memcpy(dest, src, n);
249 const retrace::Entry stdc_callbacks[] = {
250 {"malloc", &retrace_malloc},
251 {"memcpy", &retrace_memcpy},