From a27e272aac56c7869fe9dc0444ae124abafe35ac Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Sun, 13 May 2012 10:22:07 +0100 Subject: [PATCH] Factor out object swizzling. --- retrace/retrace.py | 16 ++++------------ retrace/retrace_swizzle.cpp | 32 ++++++++++++++++++++++++++++++++ retrace/retrace_swizzle.hpp | 10 ++++++++++ 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/retrace/retrace.py b/retrace/retrace.py index 7ee4a06..f9b800c 100644 --- a/retrace/retrace.py +++ b/retrace/retrace.py @@ -151,12 +151,7 @@ class ValueDeserializer(stdapi.Visitor): print ' %s = static_cast<%s>((%s).toPointer());' % (lvalue, pointer, rvalue) def visitObjPointer(self, pointer, lvalue, rvalue): - old_lvalue = '(%s).toUIntPtr()' % (rvalue,) - new_lvalue = '_obj_map[%s]' % (old_lvalue,) - print ' if (retrace::verbosity >= 2) {' - print ' std::cout << std::hex << "obj 0x" << size_t(%s) << " <- 0x" << size_t(%s) << std::dec <<"\\n";' % (old_lvalue, new_lvalue) - print ' }' - print ' %s = static_cast<%s>(%s);' % (lvalue, pointer, new_lvalue) + print ' %s = static_cast<%s>(retrace::toObjPointer(%s));' % (lvalue, pointer, rvalue) def visitLinearPointer(self, pointer, lvalue, rvalue): print ' %s = static_cast<%s>(retrace::toPointer(%s));' % (lvalue, pointer, rvalue) @@ -248,7 +243,7 @@ class SwizzledValueRegistrator(stdapi.Visitor): pass def visitObjPointer(self, pointer, lvalue, rvalue): - print r' _obj_map[(%s).toUIntPtr()] = %s;' % (rvalue, lvalue) + print r' retrace::addObj(%s, %s);' % (rvalue, lvalue) def visitLinearPointer(self, pointer, lvalue, rvalue): assert pointer.size is not None @@ -362,7 +357,7 @@ class Retracer: def deserializeThisPointer(self, interface): print r' %s *_this;' % (interface.name,) - print r' _this = static_cast<%s *>(_obj_map[call.arg(0).toUIntPtr()]);' % (interface.name,) + print r' _this = static_cast<%s *>(retrace::toObjPointer(call.arg(0)));' % (interface.name,) print r' if (!_this) {' print r' retrace::warning(call) << "NULL this pointer\n";' print r' return;' @@ -451,7 +446,7 @@ class Retracer: print ' if (call.ret->toUInt()) {' print ' return;' print ' }' - print ' _obj_map.erase(call.arg(0).toUIntPtr());' + print ' retrace::delObj(call.arg(0));' arg_names = ", ".join(method.argNames()) if method.type is not stdapi.Void: @@ -486,9 +481,6 @@ class Retracer: handle_names.add(handle.name) print - print 'static std::map _obj_map;' - print - functions = filter(self.filterFunction, api.functions) for function in functions: if function.sideeffects and not function.internal: diff --git a/retrace/retrace_swizzle.cpp b/retrace/retrace_swizzle.cpp index 52dfad2..52d1d74 100644 --- a/retrace/retrace_swizzle.cpp +++ b/retrace/retrace_swizzle.cpp @@ -239,4 +239,36 @@ toPointer(trace::Value &value, bool bind) { } + +static std::map _obj_map; + +void +addObj(trace::Value &value, void *obj) { + unsigned long long address = value.toUIntPtr(); + _obj_map[address] = obj; + + if (retrace::verbosity >= 2) { + std::cout << std::hex << "obj 0x" << address << " -> 0x" << size_t(obj) << std::dec << "\n"; + } +} + +void +delObj(trace::Value &value) { + unsigned long long address = value.toUIntPtr(); + _obj_map.erase(address); +} + +void * +toObjPointer(trace::Value &value) { + unsigned long long address = value.toUIntPtr(); + void *obj = address ? _obj_map[address] : NULL; + + if (retrace::verbosity >= 2) { + std::cout << std::hex << "obj 0x" << address << " <- 0x" << size_t(obj) << std::dec << "\n"; + } + + return obj; +} + + } /* retrace */ diff --git a/retrace/retrace_swizzle.hpp b/retrace/retrace_swizzle.hpp index fa5e008..66c2962 100644 --- a/retrace/retrace_swizzle.hpp +++ b/retrace/retrace_swizzle.hpp @@ -87,6 +87,16 @@ void * toPointer(trace::Value &value, bool bind = false); +void +addObj(trace::Value &value, void *obj); + +void +delObj(trace::Value &value); + +void * +toObjPointer(trace::Value &value); + + } /* namespace retrace */ #endif /* _RETRACE_SWIZZLE_HPP_ */ -- 2.43.0