glstate.cpp
glstate_params.cpp
retrace.cpp
+ retrace_stdc.cpp
glws.cpp
${glws_os}
${CMAKE_CURRENT_BINARY_DIR}/glproc.hpp
namespace Trace {
-#define TRACE_VERSION 1
+/*
+ * Trace file version number.
+ *
+ * We keep backwards compatability reading old traces, i.e., it should always be
+ * possible to parse and retrace old trace files.
+ *
+ * So the trace version number refers not only to changes in the binary format
+ * representation, but also semantic changes in the way certain functions
+ * should be retraced.
+ *
+ * Writing/editing old traces will not be supported however. An older version
+ * of apitrace should be used in such circunstances.
+ *
+ * Changelog:
+ *
+ * - version 0:
+ * - initial implementation
+ *
+ * - version 1:
+ * - support for GL user arrays -- a blob is provided whenever an user memory
+ * is referred (whereas before calls that operate wit user memory instead of
+ * VBOs should be ignore)
+ *
+ * - version 2:
+ * - malloc/free memory calls -- allow to pass user memory as malloc memory
+ * as opposed to blobs
+ * - glFlushMappedBufferRange will emit a memcpy only for the flushed range
+ * (whereas previously it would emit a memcpy for the whole mapped range)
+ */
+#define TRACE_VERSION 2
enum Event {
EVENT_ENTER = 0,
namespace Trace {
+static const char *memcpy_args[3] = {"dest", "src", "n"};
+const FunctionSig memcpy_sig = {0, "memcpy", 3, memcpy_args};
+
+static const char *malloc_args[1] = {"size"};
+const FunctionSig malloc_sig = {1, "malloc", 1, malloc_args};
+
+static const char *free_args[1] = {"ptr"};
+const FunctionSig free_sig = {2, "free", 1, free_args};
+
+static const char *realloc_args[2] = {"ptr", "size"};
+const FunctionSig realloc_sig = {3, "realloc", 2, realloc_args};
+
+
static void exceptionCallback(void)
{
localWriter.flush();
};
+ extern const FunctionSig memcpy_sig;
+ extern const FunctionSig malloc_sig;
+ extern const FunctionSig free_sig;
+ extern const FunctionSig realloc_sig;
+
/**
* A specialized Writer class, mean to trace the current process.
*
'glReadnPixelsARB',
])
+ map_function_names = set([
+ 'glMapBuffer',
+ 'glMapBufferARB',
+ 'glMapBufferRange',
+ 'glMapNamedBufferEXT',
+ 'glMapNamedBufferRangeEXT'
+ ])
+
+ unmap_function_names = set([
+ 'glUnmapBuffer',
+ 'glUnmapBufferARB',
+ 'glUnmapNamedBufferEXT',
+ ])
+
def retrace_function_body(self, function):
is_array_pointer = function.name in self.array_pointer_function_names
is_draw_array = function.name in self.draw_array_function_names
print r' retrace::warning(call) << infoLog << "\n";'
print r' delete [] infoLog;'
print r' }'
- if function.name in ('glMapBuffer', 'glMapBufferARB', 'glMapBufferRange', 'glMapNamedBufferEXT', 'glMapNamedBufferRangeEXT'):
+ if function.name in self.map_function_names:
print r' if (!__result) {'
print r' retrace::warning(call) << "failed to map buffer\n";'
print r' }'
print r' }'
print ' }'
+ # Update buffer mappings
+ if function.name in self.map_function_names:
+ print r' if (__result) {'
+ print r' unsigned long long __address = call.ret->toUIntPtr();'
+ if 'BufferRange' not in function.name:
+ print r' GLint length = 0;'
+ if function.name == 'glMapBuffer':
+ print r' glGetBufferParameteriv(target, GL_BUFFER_SIZE, &length);'
+ elif function.name == 'glMapBufferARB':
+ print r' glGetBufferParameterivARB(target, GL_BUFFER_SIZE_ARB, &length);'
+ elif function.name == 'glMapNamedBufferEXT':
+ print r' glGetNamedBufferParameterivEXT(buffer, GL_BUFFER_SIZE, &length);'
+ else:
+ assert False
+ print r' retrace::addRegion(__address, __result, length);'
+ print r' }'
+ if function.name in self.unmap_function_names:
+ print r' GLvoid *ptr = NULL;'
+ if function.name == 'glUnmapBuffer':
+ print r' glGetBufferPointerv(target, GL_BUFFER_MAP_POINTER, &ptr);'
+ elif function.name == 'glUnmapBufferARB':
+ print r' glGetBufferPointervARB(target, GL_BUFFER_MAP_POINTER_ARB, &ptr);'
+ elif function.name == 'glUnmapNamedBufferEXT':
+ print r' glGetNamedBufferPointervEXT(buffer, GL_BUFFER_MAP_POINTER, &ptr);'
+ else:
+ assert False
+ print r' if (ptr) {'
+ print r' retrace::delRegionByPointer(ptr);'
+ print r' }'
+
def extract_arg(self, function, arg, arg_type, lvalue, rvalue):
if function.name in self.array_pointer_function_names and arg.name == 'pointer':
- print ' %s = static_cast<%s>(%s.toPointer(true));' % (lvalue, arg_type, rvalue)
+ print ' %s = static_cast<%s>(retrace::toPointer(%s, true));' % (lvalue, arg_type, rvalue)
return
if function.name in self.draw_elements_function_names and arg.name == 'indices' or\
'''
api = glapi.glapi
- api.add_function(glapi.memcpy)
retracer = GlRetracer()
retracer.retrace_api(api)
print '}'
print
- # Generate memcpy's signature
- self.trace_function_decl(glapi.memcpy)
-
# Generate a helper function to determine whether a parameter name
# refers to a symbolic value or not
print 'static bool'
self.emit_memcpy('mapping->map', 'mapping->map', 'mapping->length')
print ' }'
if function.name in ('glFlushMappedBufferRange', 'glFlushMappedBufferRangeAPPLE'):
- # TODO: avoid copying [0, offset] bytes
print ' struct buffer_mapping *mapping = get_buffer_mapping(target);'
print ' if (mapping) {'
if function.name.endswith('APPLE'):
print ' GLsizeiptr length = size;'
print ' mapping->explicit_flush = true;'
print ' //assert(offset + length <= mapping->length);'
- self.emit_memcpy('mapping->map', 'mapping->map', 'offset + length')
+ self.emit_memcpy('(char *)mapping->map + offset', '(const char *)mapping->map + offset', 'length')
print ' }'
# FIXME: glFlushMappedNamedBufferRangeEXT
Tracer.dispatch_function(self, function)
def emit_memcpy(self, dest, src, length):
- print ' unsigned __call = Trace::localWriter.beginEnter(&__memcpy_sig);'
+ print ' unsigned __call = Trace::localWriter.beginEnter(&Trace::memcpy_sig);'
print ' Trace::localWriter.beginArg(0);'
print ' Trace::localWriter.writeOpaque(%s);' % dest
print ' Trace::localWriter.endArg();'
};
+void
+addRegion(unsigned long long address, void *buffer, unsigned long long size);
+
+void
+delRegionByPointer(void *ptr);
+
+void *
+toPointer(Trace::Value &value, bool bind = false);
+
+
/**
* Output verbosity when retracing files.
*/
};
+extern const Entry stdc_callbacks[];
+
+
class Retracer
{
typedef std::map<const char *, Callback, stringComparer> Map;
std::vector<Callback> callbacks;
public:
- Retracer() {}
+ Retracer() {
+ addCallbacks(stdc_callbacks);
+ }
virtual ~Retracer() {}
in the context of handles.'''
def visit_opaque(self, opaque, lvalue, rvalue):
- print ' %s = static_cast<%s>((%s).toPointer());' % (lvalue, opaque, rvalue)
+ print ' %s = static_cast<%s>(retrace::toPointer(%s));' % (lvalue, opaque, rvalue)
class ValueWrapper(stdapi.Visitor):
try:
ValueWrapper().visit(function.type, lvalue, rvalue)
except NotImplementedError:
- success = False
- print ' // FIXME: result'
+ print ' // XXX: result'
if not success:
if function.name[-1].islower():
sys.stderr.write('warning: unsupported %s call\n' % function.name)
--- /dev/null
+/**************************************************************************
+ *
+ * Copyright 2011 Jose Fonseca
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#include <assert.h>
+
+#include <string.h>
+
+#include "glproc.hpp"
+
+
+
+#include "trace_parser.hpp"
+#include "retrace.hpp"
+
+
+namespace retrace {
+
+struct Region
+{
+ void *buffer;
+ unsigned long long size;
+};
+
+typedef std::map<unsigned long long, Region> RegionMap;
+static RegionMap regionMap;
+
+// Iterator to the first region that contains the address
+static RegionMap::iterator
+lowerBound(unsigned long long address) {
+ RegionMap::iterator it = regionMap.lower_bound(address);
+
+ while (it != regionMap.begin() &&
+ it != regionMap.end() &&
+ it->first + it->second. size > address) {
+ --it;
+ }
+
+ return it;
+}
+
+// Iterator to the first region that not contains the address
+static RegionMap::iterator
+upperBound(unsigned long long address) {
+ RegionMap::iterator it = regionMap.upper_bound(address);
+
+ while (it != regionMap.end() &&
+ it->first + it->second.size > address) {
+ ++it;
+ }
+
+ return it;
+}
+
+void
+addRegion(unsigned long long address, void *buffer, unsigned long long size)
+{
+ // Forget all regions that intersect this new one.
+ if (0) {
+ RegionMap::iterator start = lowerBound(address);
+ if (start != regionMap.end()) {
+ RegionMap::iterator stop = upperBound(address + size);
+ regionMap.erase(start, stop);
+ }
+ }
+
+ assert(buffer);
+
+ Region region;
+ region.buffer = buffer;
+ region.size = size;
+
+ regionMap[address] = region;
+}
+
+static RegionMap::iterator
+lookupRegion(unsigned long long address) {
+ RegionMap::iterator it = regionMap.lower_bound(address);
+
+ if (it == regionMap.end() ||
+ it->first > address) {
+ if (it == regionMap.begin()) {
+ return regionMap.end();
+ } else {
+ --it;
+ }
+ }
+
+ assert(it->first <= address);
+ assert(it->first + it->second.size >= address);
+ return it;
+}
+
+void
+delRegion(unsigned long long address) {
+ RegionMap::iterator it = lookupRegion(address);
+ if (it != regionMap.end()) {
+ regionMap.erase(it);
+ } else {
+ assert(0);
+ }
+}
+
+
+void
+delRegionByPointer(void *ptr) {
+ RegionMap::iterator it = regionMap.begin();
+ while (it != regionMap.end()) {
+ if (it->second.buffer == ptr) {
+ regionMap.erase(it);
+ return;
+ }
+ }
+ assert(0);
+}
+
+void *
+lookupAddress(unsigned long long address) {
+ RegionMap::iterator it = lookupRegion(address);
+ if (it != regionMap.end()) {
+ unsigned long long offset = address - it->first;
+ assert(offset < it->second.size);
+ return (char *)it->second.buffer + offset;
+ }
+
+ if (address >= 0x00400000) {
+ std::cerr << "warning: could not translate address 0x" << std::hex << address << std::dec << "\n";
+ }
+
+ return (void *)(uintptr_t)address;
+}
+
+
+class Translator : protected Trace::Visitor
+{
+protected:
+ bool bind;
+
+ void *result;
+
+ void visit(Trace::Null *) {
+ result = NULL;
+ }
+
+ void visit(Trace::Blob *blob) {
+ result = blob->toPointer(bind);
+ }
+
+ void visit(Trace::Pointer *p) {
+ result = lookupAddress(p->value);
+ }
+
+public:
+ Translator(bool _bind) :
+ bind(_bind),
+ result(NULL)
+ {}
+
+ void * operator() (Trace::Value *node) {
+ _visit(node);
+ return result;
+ }
+};
+
+
+void *
+toPointer(Trace::Value &value, bool bind) {
+ return Translator(bind) (&value);
+}
+
+
+static void retrace_malloc(Trace::Call &call) {
+ size_t size = call.arg(0).toUInt();
+ unsigned long long address = call.ret->toUIntPtr();
+
+ if (!address) {
+ return;
+ }
+
+ void *buffer = malloc(size);
+ if (!buffer) {
+ std::cerr << "error: failed to allocated " << size << " bytes.";
+ return;
+ }
+
+ addRegion(address, buffer, size);
+}
+
+
+static void retrace_memcpy(Trace::Call &call) {
+ void * dest = toPointer(call.arg(0));
+ void * src = toPointer(call.arg(1));
+ size_t n = call.arg(2).toUInt();
+
+ if (!dest || !src || !n) {
+ return;
+ }
+
+ memcpy(dest, src, n);
+}
+
+
+const retrace::Entry stdc_callbacks[] = {
+ {"malloc", &retrace_malloc},
+ {"memcpy", &retrace_memcpy},
+ {NULL, NULL}
+};
+
+
+} /* retrace */
# GL_WIN_swap_hint
GlFunction(Void, "glAddSwapHintRectWIN", [(GLint, "x"), (GLint, "y"), (GLsizei, "width"), (GLsizei, "height")]),
])
-
-
-# memcpy's prototype. We don't really want to trace all memcpy calls -- just
-# emit a few fake memcpy calls --, which is why the prototype is not together
-# with the rest.
-memcpy = Function(Void, "memcpy", [(GLmap, "dest"), (Blob(Const(Void), "n"), "src"), (SizeT, "n")])
GLfragmentShaderATI = Handle("fragmentShaderATI", GLuint)
GLarray = Handle("array", GLuint)
GLregion = Handle("region", GLuint)
-GLmap = Handle("map", GLpointer)
+GLmap = GLpointer
GLpipeline = Handle("pipeline", GLuint)
GLsampler = Handle("sampler", GLuint)
GLfeedback = Handle("feedback", GLuint)
class Function:
- __id = 0
+ # 0-3 are reserved to memcpy, malloc, free, and realloc
+ __id = 4
def __init__(self, type, name, args, call = '', fail = None, sideeffects=True):
self.id = Function.__id