Merge remote branch 'origin/master' into gallium_draw_llvm
[mesa.git] / src / gallium / drivers / trace / tr_dump.c
index c711a56b947bdb8d9c570b253fbbe25c4c90a1c6..1affafdddc6c310ea95055b5b8bd075914ea2c86 100644 (file)
 /**
  * @file
  * Trace dumping functions.
- * 
+ *
  * For now we just use standard XML for dumping the trace calls, as this is
- * simple to write, parse, and visually inspect, but the actual representation 
- * is abstracted out of this file, so that we can switch to a binary 
+ * simple to write, parse, and visually inspect, but the actual representation
+ * is abstracted out of this file, so that we can switch to a binary
  * representation if/when it becomes justified.
- * 
- * @author Jose Fonseca <jrfonseca@tungstengraphics.com>   
+ *
+ * @author Jose Fonseca <jrfonseca@tungstengraphics.com>
  */
 
 #include "pipe/p_config.h"
 
-#if defined(PIPE_OS_LINUX)
+#if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE)
 #include <stdlib.h>
 #endif
 
 #include "pipe/p_compiler.h"
+#include "os/os_thread.h"
+#include "os/os_stream.h"
+#include "util/u_debug.h"
+#include "util/u_memory.h"
 #include "util/u_string.h"
 
-#include "tr_stream.h"
 #include "tr_dump.h"
+#include "tr_screen.h"
+#include "tr_texture.h"
+#include "tr_buffer.h"
 
 
-static struct trace_stream *stream = NULL;
+static struct os_stream *stream = NULL;
 static unsigned refcount = 0;
+static pipe_mutex call_mutex;
+static long unsigned call_no = 0;
+static boolean dumping = FALSE;
+static boolean initialized = FALSE;
 
 
-static INLINE void 
+static INLINE void
 trace_dump_write(const char *buf, size_t size)
 {
    if(stream)
-      trace_stream_write(stream, buf, size);
+      os_stream_write(stream, buf, size);
 }
 
 
-static INLINE void 
+static INLINE void
 trace_dump_writes(const char *s)
 {
    trace_dump_write(s, strlen(s));
 }
 
 
-static INLINE void 
+static INLINE void
 trace_dump_writef(const char *format, ...)
 {
    static char buf[1024];
@@ -83,8 +93,8 @@ trace_dump_writef(const char *format, ...)
 }
 
 
-static INLINE void 
-trace_dump_escape(const char *str) 
+static INLINE void
+trace_dump_escape(const char *str)
 {
    const unsigned char *p = (const unsigned char *)str;
    unsigned char c;
@@ -107,7 +117,7 @@ trace_dump_escape(const char *str)
 }
 
 
-static INLINE void 
+static INLINE void
 trace_dump_indent(unsigned level)
 {
    unsigned i;
@@ -116,14 +126,14 @@ trace_dump_indent(unsigned level)
 }
 
 
-static INLINE void 
-trace_dump_newline(void) 
+static INLINE void
+trace_dump_newline(void)
 {
    trace_dump_writes("\n");
 }
 
 
-static INLINE void 
+static INLINE void
 trace_dump_tag(const char *name)
 {
    trace_dump_writes("<");
@@ -132,7 +142,7 @@ trace_dump_tag(const char *name)
 }
 
 
-static INLINE void 
+static INLINE void
 trace_dump_tag_begin(const char *name)
 {
    trace_dump_writes("<");
@@ -140,8 +150,8 @@ trace_dump_tag_begin(const char *name)
    trace_dump_writes(">");
 }
 
-static INLINE void 
-trace_dump_tag_begin1(const char *name, 
+static INLINE void
+trace_dump_tag_begin1(const char *name,
                       const char *attr1, const char *value1)
 {
    trace_dump_writes("<");
@@ -154,8 +164,8 @@ trace_dump_tag_begin1(const char *name,
 }
 
 
-static INLINE void 
-trace_dump_tag_begin2(const char *name, 
+static INLINE void
+trace_dump_tag_begin2(const char *name,
                       const char *attr1, const char *value1,
                       const char *attr2, const char *value2)
 {
@@ -173,8 +183,8 @@ trace_dump_tag_begin2(const char *name,
 }
 
 
-static INLINE void 
-trace_dump_tag_begin3(const char *name, 
+static INLINE void
+trace_dump_tag_begin3(const char *name,
                       const char *attr1, const char *value1,
                       const char *attr2, const char *value2,
                       const char *attr3, const char *value3)
@@ -205,45 +215,62 @@ trace_dump_tag_end(const char *name)
    trace_dump_writes(">");
 }
 
-static void 
+static void
 trace_dump_trace_close(void)
 {
    if(stream) {
       trace_dump_writes("</trace>\n");
-      trace_stream_close(stream);
+      os_stream_close(stream);
       stream = NULL;
       refcount = 0;
+      call_no = 0;
+      pipe_mutex_destroy(call_mutex);
    }
 }
 
+void trace_dump_init()
+{
+   if (initialized)
+      return;
+
+   pipe_mutex_init(call_mutex);
+   dumping = FALSE;
+   initialized = TRUE;
+}
+
 boolean trace_dump_trace_begin()
 {
-   if(!debug_get_bool_option("GALLIUM_TRACE", FALSE))
+   const char *filename;
+
+   assert(initialized);
+
+   filename = debug_get_option("GALLIUM_TRACE", NULL);
+   if(!filename)
       return FALSE;
-   
+
    if(!stream) {
-   
-      stream = trace_stream_create("gallium", "trace");
+
+      stream = os_file_stream_create(filename);
       if(!stream)
          return FALSE;
-      
+
       trace_dump_writes("<?xml version='1.0' encoding='UTF-8'?>\n");
       trace_dump_writes("<?xml-stylesheet type='text/xsl' href='trace.xsl'?>\n");
       trace_dump_writes("<trace version='0.1'>\n");
-      
-#if defined(PIPE_OS_LINUX)
-      /* Linux applications rarely cleanup GL / Gallium resources so catch 
-       * application exit here */ 
+
+#if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE)
+      /* Linux applications rarely cleanup GL / Gallium resources so catch
+       * application exit here */
       atexit(trace_dump_trace_close);
 #endif
    }
-   
+
    ++refcount;
-   
+
    return TRUE;
 }
 
-boolean trace_dump_enabled(void)
+boolean trace_dump_trace_enabled(void)
 {
    return stream ? TRUE : FALSE;
 }
@@ -255,71 +282,184 @@ void trace_dump_trace_end(void)
          trace_dump_trace_close();
 }
 
-void trace_dump_call_begin(const char *klass, const char *method)
+/*
+ * Call lock
+ */
+
+void trace_dump_call_lock(void)
 {
+   pipe_mutex_lock(call_mutex);
+}
+
+void trace_dump_call_unlock(void)
+{
+   pipe_mutex_unlock(call_mutex);
+}
+
+/*
+ * Dumping control
+ */
+
+void trace_dumping_start_locked(void)
+{
+   dumping = TRUE;
+}
+
+void trace_dumping_stop_locked(void)
+{
+   dumping = FALSE;
+}
+
+boolean trace_dumping_enabled_locked(void)
+{
+   return dumping;
+}
+
+void trace_dumping_start(void)
+{
+   pipe_mutex_lock(call_mutex);
+   trace_dumping_start_locked();
+   pipe_mutex_unlock(call_mutex);
+}
+
+void trace_dumping_stop(void)
+{
+   pipe_mutex_lock(call_mutex);
+   trace_dumping_stop_locked();
+   pipe_mutex_unlock(call_mutex);
+}
+
+boolean trace_dumping_enabled(void)
+{
+   boolean ret;
+   pipe_mutex_lock(call_mutex);
+   ret = trace_dumping_enabled_locked();
+   pipe_mutex_unlock(call_mutex);
+   return ret;
+}
+
+/*
+ * Dump functions
+ */
+
+void trace_dump_call_begin_locked(const char *klass, const char *method)
+{
+   if (!dumping)
+      return;
+
+   ++call_no;
    trace_dump_indent(1);
-   trace_dump_tag_begin2("call", "class", klass, "method", method);
+   trace_dump_writes("<call no=\'");
+   trace_dump_writef("%lu", call_no);
+   trace_dump_writes("\' class=\'");
+   trace_dump_escape(klass);
+   trace_dump_writes("\' method=\'");
+   trace_dump_escape(method);
+   trace_dump_writes("\'>");
    trace_dump_newline();
 }
 
-void trace_dump_call_end(void)
+void trace_dump_call_end_locked(void)
 {
+   if (!dumping)
+      return;
+
    trace_dump_indent(1);
    trace_dump_tag_end("call");
    trace_dump_newline();
-   trace_stream_flush(stream);
+   os_stream_flush(stream);
+}
+
+void trace_dump_call_begin(const char *klass, const char *method)
+{
+   pipe_mutex_lock(call_mutex);
+   trace_dump_call_begin_locked(klass, method);
+}
+
+void trace_dump_call_end(void)
+{
+   trace_dump_call_end_locked();
+   pipe_mutex_unlock(call_mutex);
 }
 
 void trace_dump_arg_begin(const char *name)
 {
+   if (!dumping)
+      return;
+
    trace_dump_indent(2);
    trace_dump_tag_begin1("arg", "name", name);
 }
 
 void trace_dump_arg_end(void)
 {
+   if (!dumping)
+      return;
+
    trace_dump_tag_end("arg");
    trace_dump_newline();
 }
 
 void trace_dump_ret_begin(void)
 {
+   if (!dumping)
+      return;
+
    trace_dump_indent(2);
    trace_dump_tag_begin("ret");
 }
 
 void trace_dump_ret_end(void)
 {
+   if (!dumping)
+      return;
+
    trace_dump_tag_end("ret");
    trace_dump_newline();
 }
 
 void trace_dump_bool(int value)
 {
+   if (!dumping)
+      return;
+
    trace_dump_writef("<bool>%c</bool>", value ? '1' : '0');
 }
 
-void trace_dump_int(long int value)
+void trace_dump_int(long long int value)
 {
-   trace_dump_writef("<int>%li</int>", value);
+   if (!dumping)
+      return;
+
+   trace_dump_writef("<int>%lli</int>", value);
 }
 
-void trace_dump_uint(long unsigned value)
+void trace_dump_uint(long long unsigned value)
 {
-   trace_dump_writef("<uint>%lu</uint>", value);
+   if (!dumping)
+      return;
+
+   trace_dump_writef("<uint>%llu</uint>", value);
 }
 
 void trace_dump_float(double value)
 {
+   if (!dumping)
+      return;
+
    trace_dump_writef("<float>%g</float>", value);
 }
 
 void trace_dump_bytes(const void *data,
-                      long unsigned size)
+                      size_t size)
 {
    static const char hex_table[16] = "0123456789ABCDEF";
    const uint8_t *p = data;
-   long unsigned i;
+   size_t i;
+
+   if (!dumping)
+      return;
+
    trace_dump_writes("<bytes>");
    for(i = 0; i < size; ++i) {
       uint8_t byte = *p++;
@@ -333,6 +473,9 @@ void trace_dump_bytes(const void *data,
 
 void trace_dump_string(const char *str)
 {
+   if (!dumping)
+      return;
+
    trace_dump_writes("<string>");
    trace_dump_escape(str);
    trace_dump_writes("</string>");
@@ -340,6 +483,9 @@ void trace_dump_string(const char *str)
 
 void trace_dump_enum(const char *value)
 {
+   if (!dumping)
+      return;
+
    trace_dump_writes("<enum>");
    trace_dump_escape(value);
    trace_dump_writes("</enum>");
@@ -347,53 +493,135 @@ void trace_dump_enum(const char *value)
 
 void trace_dump_array_begin(void)
 {
+   if (!dumping)
+      return;
+
    trace_dump_writes("<array>");
 }
 
 void trace_dump_array_end(void)
 {
+   if (!dumping)
+      return;
+
    trace_dump_writes("</array>");
 }
 
 void trace_dump_elem_begin(void)
 {
+   if (!dumping)
+      return;
+
    trace_dump_writes("<elem>");
 }
 
 void trace_dump_elem_end(void)
 {
+   if (!dumping)
+      return;
+
    trace_dump_writes("</elem>");
 }
 
 void trace_dump_struct_begin(const char *name)
 {
+   if (!dumping)
+      return;
+
    trace_dump_writef("<struct name='%s'>", name);
 }
 
 void trace_dump_struct_end(void)
 {
+   if (!dumping)
+      return;
+
    trace_dump_writes("</struct>");
 }
 
 void trace_dump_member_begin(const char *name)
 {
+   if (!dumping)
+      return;
+
    trace_dump_writef("<member name='%s'>", name);
 }
 
 void trace_dump_member_end(void)
 {
+   if (!dumping)
+      return;
+
    trace_dump_writes("</member>");
 }
 
 void trace_dump_null(void)
 {
+   if (!dumping)
+      return;
+
    trace_dump_writes("<null/>");
 }
 
 void trace_dump_ptr(const void *value)
 {
+   if (!dumping)
+      return;
+
    if(value)
       trace_dump_writef("<ptr>0x%08lx</ptr>", (unsigned long)(uintptr_t)value);
    else
       trace_dump_null();
 }
+
+void trace_dump_buffer_ptr(struct pipe_buffer *_buffer)
+{
+   if (!dumping)
+      return;
+
+   if (_buffer) {
+      struct trace_buffer *tr_buf = trace_buffer(_buffer);
+      trace_dump_ptr(tr_buf->buffer);
+   } else {
+      trace_dump_null();
+   }
+}
+
+void trace_dump_texture_ptr(struct pipe_texture *_texture)
+{
+   if (!dumping)
+      return;
+
+   if (_texture) {
+      struct trace_texture *tr_tex = trace_texture(_texture);
+      trace_dump_ptr(tr_tex->texture);
+   } else {
+      trace_dump_null();
+   }
+}
+
+void trace_dump_surface_ptr(struct pipe_surface *_surface)
+{
+   if (!dumping)
+      return;
+
+   if (_surface) {
+      struct trace_surface *tr_surf = trace_surface(_surface);
+      trace_dump_ptr(tr_surf->surface);
+   } else {
+      trace_dump_null();
+   }
+}
+
+void trace_dump_transfer_ptr(struct pipe_transfer *_transfer)
+{
+   if (!dumping)
+      return;
+
+   if (_transfer) {
+      struct trace_transfer *tr_tran = trace_transfer(_transfer);
+      trace_dump_ptr(tr_tran->transfer);
+   } else {
+      trace_dump_null();
+   }
+}