1 /*
2 * Copyright 2011 Joakim Sindholt <[email protected]>
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "nine_debug.h"
7
8 #include <ctype.h>
9 #include "c11/threads.h"
10
11 static const struct debug_named_value nine_debug_flags[] = {
12 { "unknown", DBG_UNKNOWN, "IUnknown implementation." },
13 { "adapter", DBG_ADAPTER, "ID3D9Adapter implementation." },
14 { "overlay", DBG_OVERLAYEXTENSION, "IDirect3D9ExOverlayExtension implementation." },
15 { "auth", DBG_AUTHENTICATEDCHANNEL, "IDirect3DAuthenticatedChannel9 implementation." },
16 { "basetex", DBG_BASETEXTURE, "IDirect3DBaseTexture9 implementation." },
17 { "crypto", DBG_CRYPTOSESSION, "IDirect3DCryptoSession9 implementation." },
18 { "cubetex", DBG_CUBETEXTURE, "IDirect3DCubeTexture9 implementation." },
19 { "device", DBG_DEVICE, "IDirect3DDevice9(Ex) implementation." },
20 { "video", DBG_DEVICEVIDEO, "IDirect3DDeviceVideo9 implementation." },
21 { "ibuf", DBG_INDEXBUFFER, "IDirect3DIndexBuffer9 implementation." },
22 { "ps", DBG_PIXELSHADER, "IDirect3DPixelShader9 implementation." },
23 { "query", DBG_QUERY, "IDirect3DQuery9 implementation." },
24 { "res", DBG_RESOURCE, "IDirect3DResource9 implementation." },
25 { "state", DBG_STATEBLOCK, "IDirect3DStateBlock9 implementation." },
26 { "surf", DBG_SURFACE, "IDirect3DSurface9 implementation." },
27 { "swap", DBG_SWAPCHAIN, "IDirect3DSwapChain9(Ex) implementation." },
28 { "tex", DBG_TEXTURE, "IDirect3DTexture9 implementation." },
29 { "vbuf", DBG_VERTEXBUFFER, "IDirect3DVertexBuffer9 implementation." },
30 { "vdecl", DBG_VERTEXDECLARATION, "IDirect3DVertexDeclaration9 implementation." },
31 { "vs", DBG_VERTEXSHADER, "IDirect3DVertexShader9 implementation." },
32 { "3dsurf", DBG_VOLUME, "IDirect3DVolume9 implementation." },
33 { "3dtex", DBG_VOLUMETEXTURE, "IDirect3DVolumeTexture9 implementation." },
34 { "shader", DBG_SHADER, "Shader token stream translator." },
35 { "ff", DBG_FF, "Fixed function emulation." },
36 { "user", DBG_USER, "User errors, both fixable and unfixable." },
37 { "error", DBG_ERROR, "Driver errors, always visible." },
38 { "warn", DBG_WARN, "Driver warnings, always visible in debug builds." },
39 { "tid", DBG_TID, "Display thread-ids." },
40 DEBUG_NAMED_VALUE_END
41 };
42
43 void
_nine_debug_printf(unsigned long flag,const char * func,const char * fmt,...)44 _nine_debug_printf( unsigned long flag,
45 const char *func,
46 const char *fmt,
47 ... )
48 {
49 static bool first = true;
50 static unsigned long dbg_flags = DBG_ERROR | DBG_WARN;
51 unsigned long tid = 0;
52
53 if (first) {
54 first = false;
55 dbg_flags |= debug_get_flags_option("NINE_DEBUG", nine_debug_flags, 0);
56 }
57
58 #if defined(HAVE_PTHREAD)
59 if (dbg_flags & DBG_TID)
60 tid = (unsigned long)pthread_self();
61 #endif
62
63 if (dbg_flags & flag) {
64 const char *f = func ? strrchr(func, '_') : NULL;
65 va_list ap;
66 /* inside a class this will print nine:tid:classinlowercase:func: while
67 * outside a class (rarely used) it will just print nine:tid:func
68 * the reason for lower case is simply to match the filenames, as it
69 * will also strip off the "Nine" */
70 if (f && strncmp(func, "Nine", 4) == 0) {
71 char klass[96]; /* no class name is this long */
72 char *ptr = klass;
73 for (func += 4; func != f; ++func) { *ptr++ = tolower(*func); }
74 *ptr = '\0';
75 if (tid)
76 _debug_printf("nine:0x%08lx:%s:%s: ", tid, klass, ++f);
77 else
78 _debug_printf("nine:%s:%s: ", klass, ++f);
79 } else if (func) {
80 if (tid)
81 _debug_printf("nine:0x%08lx:%s ", tid, func);
82 else
83 _debug_printf("nine:%s ", func);
84 }
85
86 va_start(ap, fmt);
87 _debug_vprintf(fmt, ap);
88 va_end(ap);
89 }
90 }
91
92 void
_nine_stub(const char * file,const char * func,unsigned line)93 _nine_stub( const char *file,
94 const char *func,
95 unsigned line )
96 {
97 const char *r = strrchr(file, '/');
98 if (r == NULL) { r = strrchr(file, '\\'); }
99 _debug_printf("nine:%s:%d: %s STUB!\n", r ? ++r : file, line, func);
100 }
101