[debug] improve platform dump
[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 #ifdef DEBUG_OCL_ICD_PROVIDE_DUMP_FIELD
29 #  define CL_USE_DEPRECATED_OPENCL_1_1_APIS
30 #endif
31 #include <CL/cl.h>
32
33 #pragma GCC visibility push(hidden)
34
35 #include "config.h"
36
37 #ifndef DEBUG_OCL_ICD
38 #  define DEBUG_OCL_ICD 0
39 #endif
40
41 #define D_WARN 1
42 #define D_LOG 2
43 #define D_TRACE 4
44 #define D_DUMP 8
45 #if DEBUG_OCL_ICD
46 #  pragma GCC visibility push(default)
47 #  include <stdio.h>
48 #  pragma GCC visibility pop
49 extern int debug_ocl_icd_mask;
50 #  define debug(mask, fmt, ...) do {\
51         if (debug_ocl_icd_mask & (mask)) {                      \
52                 fprintf(stderr, "ocl-icd(%s:%i): %s: " fmt "\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__); \
53         } \
54    } while(0)
55 #  define RETURN(val) do { \
56         __typeof__(val) ret=(val); \
57         debug(D_TRACE, "return: %ld/0x%lx", (long)ret, (long)ret);      \
58         return ret; \
59    } while(0)
60 #  define RETURN_STR(val) do { \
61         char* ret=(char*)(val);                 \
62         debug(D_TRACE, "return: %s", ret);      \
63         return ret; \
64    } while(0)
65 #  ifdef DEBUG_OCL_ICD_PROVIDE_DUMP_FIELD
66 typedef __typeof__(clGetExtensionFunctionAddress) *clGEFA_t;
67 void dump_platform(clGEFA_t f, cl_platform_id pid);
68 #  endif
69 static inline void debug_init(void) {
70   static int done=0;
71   if (!done) {
72     char *debug=getenv("OCL_ICD_DEBUG");
73     if (debug) {
74       debug_ocl_icd_mask=atoi(debug);
75       if (*debug == 0)
76         debug_ocl_icd_mask=1;
77     }
78     done=1;
79   }
80 }
81
82 #  define dump_field(pid, f, name) \
83     debug(D_DUMP, "%40s=%p [%p/%p]", #name, pid->dispatch->name, f(#name), ((long)(pid->dispatch->clGetExtensionFunctionAddressForPlatform)>10)?pid->dispatch->clGetExtensionFunctionAddressForPlatform(pid, #name):NULL)
84
85 #else
86 #  define debug(...) (void)0
87 #  define RETURN(val) return (val)
88 #  define RETURN_STR(val) return (val)
89 #  define debug_init() (void)0
90 #endif
91
92 #define debug_trace() debug(D_TRACE, "Entering")
93
94 #pragma GCC visibility pop
95
96 #endif