Skip to content

Commit

Permalink
Implement tracing
Browse files Browse the repository at this point in the history
This implements a small tracing convenience library for debugging
purposess.
  • Loading branch information
brnguyen2 committed Aug 26, 2013
1 parent 31817fb commit 4ee8d21
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 1 deletion.
4 changes: 3 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,7 @@ dnl default CFLAGS
CFLAGS="$CFLAGS -Wall -Werror -std=gnu99 -include config.h -fvisibility=hidden $DEFINES"

AC_CONFIG_FILES([Makefile
src/Makefile])
src/Makefile
src/GLX/Makefile
src/util/trace/Makefile])
AC_OUTPUT
5 changes: 5 additions & 0 deletions src/GLX/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@
lib_LTLIBRARIES = libGLX.la

UTIL_DIR = ../util
TRACE_DIR = ../util/trace

SUBDIRS = $(TRACE_DIR)

# Warning settings
# Include paths
libGLX_la_CFLAGS = -I$(UTIL_DIR)
libGLX_la_CFLAGS += -I$(TRACE_DIR)
libGLX_la_CFLAGS += -I$(top_builddir)/include

# Required library flags
Expand All @@ -41,6 +45,7 @@ libGLX_la_CFLAGS += $(PTHREAD_CFLAGS)
libGLX_la_LIBADD = -ldl
libGLX_la_LIBADD += $(X11_LIBS)
libGLX_la_LIBADD += $(XEXT_LIBS)
libGLX_la_LIBADD += $(TRACE_DIR)/libtrace.la

libGLX_la_LDFLAGS = -shared

Expand Down
2 changes: 2 additions & 0 deletions src/GLX/libglx.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "libglxmapping.h"
#include "libglxcurrent.h"
#include "utils_misc.h"
#include "trace.h"
#include "GL/glxproto.h"

/* current version numbers */
Expand Down Expand Up @@ -510,6 +511,7 @@ void __attribute__ ((constructor)) __glXInit(void)
__glXLookupVendorByName(preloadedVendor);
}

DBG_PRINTF(0, "Loading GLX...\n");

}

Expand Down
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SUBDIRS = GLX
35 changes: 35 additions & 0 deletions src/util/trace/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright (c) 2013, NVIDIA CORPORATION.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and/or associated documentation files (the
# "Materials"), to deal in the Materials without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Materials, and to
# permit persons to whom the Materials are furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# unaltered in all copies or substantial portions of the Materials.
# Any additions, deletions, or changes to the original source files
# must be clearly indicated in accompanying documentation.
#
# If only executable code is distributed, then the accompanying
# documentation must state that "this software is based in part on the
# work of the Khronos Group."
#
# THE MATERIALS ARE 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
# MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.

# libtrace.la: convenience library for API library tracing routines

noinst_LTLIBRARIES = libtrace.la

libtrace_la_LIBADD = $(PTHREAD_LIBS)

libtrace_la_SOURCES = \
trace.c
84 changes: 84 additions & 0 deletions src/util/trace/trace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2013, NVIDIA CORPORATION.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and/or associated documentation files (the
* "Materials"), to deal in the Materials without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Materials, and to
* permit persons to whom the Materials are furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* unaltered in all copies or substantial portions of the Materials.
* Any additions, deletions, or changes to the original source files
* must be clearly indicated in accompanying documentation.
*
* If only executable code is distributed, then the accompanying
* documentation must state that "this software is based in part on the
* work of the Khronos Group."
*
* THE MATERIALS ARE 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
* MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/


#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <pthread.h>

#include "trace.h"

static int debugPrintfInitialized = 0;
static int debugPrintfLevel = -1;
static int showPrefix = 0;

void __glvnd_dbg_printf(
int level,
const char *file,
int line,
const char *function,
int thread_id,
const char *fmt,
...
)
{
va_list ap;
char *tmp;
int ret;

if (!debugPrintfInitialized) {
char *debugStr = getenv("__GL_DEBUG");
char *showPrefixStr = getenv("__GL_DEBUG_FILE_LINE_INFO");
if (debugStr) {
debugPrintfLevel = atoi(debugStr);
}
if (showPrefixStr) {
showPrefix = 1;
}
debugPrintfInitialized = 1;
}

if (level < debugPrintfLevel) {
va_start(ap, fmt);
ret = vasprintf(&tmp, fmt, ap);
va_end(ap);
if (ret == -1 || !tmp) {
return;
}
if (showPrefix) {
fprintf(stderr, "%s:%d:%s [tid=%x] %s", file, line, function,
thread_id, tmp);
} else {
fprintf(stderr, "%s", tmp);
}
free(tmp);
}
}
90 changes: 90 additions & 0 deletions src/util/trace/trace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2013, NVIDIA CORPORATION.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and/or associated documentation files (the
* "Materials"), to deal in the Materials without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Materials, and to
* permit persons to whom the Materials are furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* unaltered in all copies or substantial portions of the Materials.
* Any additions, deletions, or changes to the original source files
* must be clearly indicated in accompanying documentation.
*
* If only executable code is distributed, then the accompanying
* documentation must state that "this software is based in part on the
* work of the Khronos Group."
*
* THE MATERIALS ARE 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
* MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/

#if !defined(__TRACE_H)
#define __TRACE_H

/*!
* \defgroup trace Tracing module
*
* Code in this module implements routines useful for tracing. To enable
* tracing, set the __GL_DEBUG environment variable to a non-negative value
* on a -DDEBUG build. Higher values will enable more verbose tracing output.
*
* Optionally, setting the __GL_DEBUG_FILE_LINE_INFO variable will enable
* printing additional context such as file/line number, and thread id.
*
* @{
*/

#if defined(DEBUG)

/*!
* Macro to define debug-only code used for tracing.
*/
# define DBG_CODE(x) x

// Define DBG_PRINTF_THREAD_ID before including this file to enable per-thread
// logs
#ifndef DBG_PRINTF_THREAD_ID
# define DBG_PRINTF_THREAD_ID 0
#endif

extern void __glvnd_dbg_printf(
int level,
const char *file,
int line,
const char *function,
int thread_id,
const char *fmt,
...
) __attribute__((format(printf,6,7)));



/*!
* Macro to print a tracing message with urgency level given by the "level"
* parameter.
*/
# define DBG_PRINTF(level, ...) \
__glvnd_dbg_printf(level, \
__FILE__, \
__LINE__, \
__FUNCTION__, \
DBG_PRINTF_THREAD_ID, \
__VA_ARGS__)

#else
# define DBG_PRINTF(level, ...)
# define DBG_CODE(x)
#endif

/*! @} */

#endif // !defined(__TRACE_H)

0 comments on commit 4ee8d21

Please sign in to comment.