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)
93 if (retrace::verbosity >= 2) {
97 << "0x" << address << "-0x" << (address + size)
99 << "0x" << (uintptr_t)buffer << "-0x" << ((uintptr_t)buffer + size)
105 // Ignore NULL pointer
111 RegionMap::iterator start = lowerBound(address);
112 RegionMap::iterator stop = upperBound(address + size);
114 // Forget all regions that intersect this new one.
115 regionMap.erase(start, stop);
117 for (RegionMap::iterator it = start; it != stop; ++it) {
118 std::cerr << std::hex << "warning: "
119 "region 0x" << address << "-0x" << (address + size) << " "
120 "intersects existing region 0x" << it->first << "-0x" << (it->first + it->second.size) << "\n" << std::dec;
121 assert(intersects(it, address, size));
129 region.buffer = buffer;
132 regionMap[address] = region;
135 static RegionMap::iterator
136 lookupRegion(unsigned long long address) {
137 RegionMap::iterator it = regionMap.lower_bound(address);
139 if (it == regionMap.end() ||
140 it->first > address) {
141 if (it == regionMap.begin()) {
142 return regionMap.end();
148 assert(contains(it, address));
153 delRegion(unsigned long long address) {
154 RegionMap::iterator it = lookupRegion(address);
155 if (it != regionMap.end()) {
164 delRegionByPointer(void *ptr) {
165 for (RegionMap::iterator it = regionMap.begin(); it != regionMap.end(); ++it) {
166 if (it->second.buffer == ptr) {
175 lookupAddress(unsigned long long address) {
176 RegionMap::iterator it = lookupRegion(address);
177 if (it != regionMap.end()) {
178 unsigned long long offset = address - it->first;
179 assert(offset < it->second.size);
180 void *addr = (char *)it->second.buffer + offset;
182 if (retrace::verbosity >= 2) {
188 << "0x" << (uintptr_t)addr
196 if (address >= 0x00400000) {
197 std::cerr << "warning: could not translate address 0x" << std::hex << address << std::dec << "\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);
242 static void retrace_malloc(trace::Call &call) {
243 size_t size = call.arg(0).toUInt();
244 unsigned long long address = call.ret->toUIntPtr();
250 void *buffer = malloc(size);
252 std::cerr << "error: failed to allocated " << size << " bytes.";
256 addRegion(address, buffer, size);
260 static void retrace_memcpy(trace::Call &call) {
261 void * dest = toPointer(call.arg(0));
262 void * src = toPointer(call.arg(1));
263 size_t n = call.arg(2).toUInt();
265 if (!dest || !src || !n) {
269 memcpy(dest, src, n);
273 const retrace::Entry stdc_callbacks[] = {
274 {"malloc", &retrace_malloc},
275 {"memcpy", &retrace_memcpy},