Merge remote-tracking branch 'origin/master'
[ocl-icd] / ocl_icd_debug.h
1 /**
2 Copyright (c) 2012, Vincent Danjean <Vincent.Danjean@ens-lyon.org>
3 All rights reserved.
4       
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7     
8 1. Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13         
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25 #ifndef OCL_ICD_LOADER_DEBUG_H
26 #define OCL_ICD_LOADER_DEBUG_H
27
28 #include <CL/cl.h>
29
30 #pragma GCC visibility push(hidden)
31
32 #include "config.h"
33
34 #ifndef DEBUG_OCL_ICD
35 #  define DEBUG_OCL_ICD 0
36 #endif
37
38 #define D_WARN 1
39 #define D_LOG 2
40 #define D_TRACE 4
41 #define D_DUMP 8
42 #if DEBUG_OCL_ICD
43 #  pragma GCC visibility push(default)
44 #  include <stdio.h>
45 #  pragma GCC visibility pop
46 extern int debug_ocl_icd_mask;
47 #  define debug(mask, fmt, ...) do {\
48         if (debug_ocl_icd_mask & (mask)) {                      \
49                 fprintf(stderr, "ocl-icd: %s: " fmt "\n", __func__, ##__VA_ARGS__); \
50         } \
51    } while(0)
52 #  define RETURN(val) do { \
53         __typeof__(val) ret=(val); \
54         debug(D_TRACE, "return: %ld/0x%lx", (long)ret, (long)ret);      \
55         return ret; \
56    } while(0)
57 #  define RETURN_STR(val) do { \
58         char* ret=(char*)(val);                 \
59         debug(D_TRACE, "return: %s", ret);      \
60         return ret; \
61    } while(0)
62 void dump_platform(cl_platform_id pid);
63 static inline void debug_init(void) {
64   static int done=0;
65   if (!done) {
66     char *debug=getenv("OCL_ICD_DEBUG");
67     if (debug) {
68       debug_ocl_icd_mask=atoi(debug);
69       if (*debug == 0)
70         debug_ocl_icd_mask=1;
71     }
72     done=1;
73   }
74 }
75 #else
76 #  define debug(...) (void)0
77 #  define RETURN(val) return (val)
78 #  define RETURN_STR(val) return (val)
79 #  define debug_init() (void)0
80 #endif
81
82 #define debug_trace() debug(D_TRACE, "Entering")
83
84 #pragma GCC visibility pop
85
86 #endif