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 **************************************************************************/
35 #include "trace_parser.hpp"
36 #include "retrace.hpp"
44 unsigned long long size;
47 typedef std::map<unsigned long long, Region> RegionMap;
48 static RegionMap regionMap;
50 // Iterator to the first region that contains the address
51 static RegionMap::iterator
52 lowerBound(unsigned long long address) {
53 RegionMap::iterator it = regionMap.lower_bound(address);
55 while (it != regionMap.begin() &&
56 it != regionMap.end() &&
57 it->first + it->second. size > address) {
64 // Iterator to the first region that not contains the address
65 static RegionMap::iterator
66 upperBound(unsigned long long address) {
67 RegionMap::iterator it = regionMap.upper_bound(address);
69 while (it != regionMap.end() &&
70 it->first + it->second.size > address) {
78 addRegion(unsigned long long address, void *buffer, unsigned long long size)
80 // Forget all regions that intersect this new one.
82 RegionMap::iterator start = lowerBound(address);
83 if (start != regionMap.end()) {
84 RegionMap::iterator stop = upperBound(address + size);
85 regionMap.erase(start, stop);
92 region.buffer = buffer;
95 regionMap[address] = region;
98 static RegionMap::iterator
99 lookupRegion(unsigned long long address) {
100 RegionMap::iterator it = regionMap.lower_bound(address);
102 if (it == regionMap.end() ||
103 it->first > address) {
104 if (it == regionMap.begin()) {
105 return regionMap.end();
111 assert(it->first <= address);
112 assert(it->first + it->second.size >= address);
117 delRegion(unsigned long long address) {
118 RegionMap::iterator it = lookupRegion(address);
119 if (it != regionMap.end()) {
128 delRegionByPointer(void *ptr) {
129 RegionMap::iterator it = regionMap.begin();
130 while (it != regionMap.end()) {
131 if (it->second.buffer == ptr) {
140 lookupAddress(unsigned long long address) {
141 RegionMap::iterator it = lookupRegion(address);
142 if (it != regionMap.end()) {
143 unsigned long long offset = address - it->first;
144 assert(offset < it->second.size);
145 return (char *)it->second.buffer + offset;
148 if (address >= 0x00400000) {
149 std::cerr << "warning: could not translate address 0x" << std::hex << address << std::dec << "\n";
152 return (void *)(uintptr_t)address;
156 class Translator : protected trace::Visitor
163 void visit(trace::Null *) {
167 void visit(trace::Blob *blob) {
168 result = blob->toPointer(bind);
171 void visit(trace::Pointer *p) {
172 result = lookupAddress(p->value);
176 Translator(bool _bind) :
181 void * operator() (trace::Value *node) {
189 toPointer(trace::Value &value, bool bind) {
190 return Translator(bind) (&value);
194 static void retrace_malloc(trace::Call &call) {
195 size_t size = call.arg(0).toUInt();
196 unsigned long long address = call.ret->toUIntPtr();
202 void *buffer = malloc(size);
204 std::cerr << "error: failed to allocated " << size << " bytes.";
208 addRegion(address, buffer, size);
212 static void retrace_memcpy(trace::Call &call) {
213 void * dest = toPointer(call.arg(0));
214 void * src = toPointer(call.arg(1));
215 size_t n = call.arg(2).toUInt();
217 if (!dest || !src || !n) {
221 memcpy(dest, src, n);
225 const retrace::Entry stdc_callbacks[] = {
226 {"malloc", &retrace_malloc},
227 {"memcpy", &retrace_memcpy},