Skip to content

Commit 5b99314

Browse files
aleino-nvslangbotcsyonghe
authored
Add backtraces to examples (shader-slang#5973)
* examples: Log stack trace on exceptions For now, this is only implemented on Windows. This helps to address shader-slang#5520. * examples: Print log file if there is any * format code --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> Co-authored-by: Yong He <yonghe@outlook.com>
1 parent c43f6fa commit 5b99314

File tree

22 files changed

+324
-33
lines changed

22 files changed

+324
-33
lines changed

.github/workflows/ci-examples.sh

+3
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ function run_sample {
170170
pushd "$bin_dir" 1>/dev/null 2>&1
171171
if [[ ! "$dry_run" = true ]]; then
172172
./"$sample" "${args[@]}" || result=$?
173+
if [[ -f ./"log-$sample.txt" ]]; then
174+
cat ./"log-$sample.txt"
175+
fi
173176
fi
174177
if [[ $result -eq 0 ]]; then
175178
summary=("${summary[@]}" " success")

examples/CMakeLists.txt

+23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
function(example dir)
2+
cmake_parse_arguments(ARG "WIN32_EXECUTABLE" "" "" ${ARGN})
3+
24
set(debug_dir ${CMAKE_CURRENT_BINARY_DIR}/${dir})
35

46
file(
@@ -30,6 +32,22 @@ function(example dir)
3032
)
3133
endif()
3234

35+
# Libraries providing a main function that prints stack traces on exceptions
36+
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
37+
# On Windows we have two different versions: main for "console applications" and
38+
# WinMain for normal Windows applications.
39+
if(${ARG_WIN32_EXECUTABLE})
40+
set(main_wrapper_libraries example-winmain)
41+
else()
42+
set(main_wrapper_libraries example-main)
43+
endif()
44+
# Add stack printing support
45+
set(main_wrapper_libraries ${main_wrapper_libraries} stacktrace-windows)
46+
set(main_wrapper_libraries ${main_wrapper_libraries} dbghelp.lib)
47+
else()
48+
set(main_wrapper_libraries example-main)
49+
endif()
50+
3351
slang_add_target(
3452
${dir}
3553
EXECUTABLE
@@ -42,7 +60,9 @@ function(example dir)
4260
gfx-util
4361
platform
4462
$<$<BOOL:${SLANG_ENABLE_CUDA}>:CUDA::cuda_driver>
63+
${main_wrapper_libraries}
4564
EXTRA_COMPILE_DEFINITIONS_PRIVATE
65+
SLANG_EXAMPLE_NAME=${dir}
4666
$<$<BOOL:${SLANG_ENABLE_XLIB}>:SLANG_ENABLE_XLIB>
4767
REQUIRED_BY all-examples
4868
OPTIONAL_REQUIRES ${copy_assets_target} copy-prebuilt-binaries
@@ -68,6 +88,9 @@ if(SLANG_ENABLE_EXAMPLES)
6888
$<$<BOOL:${SLANG_ENABLE_CUDA}>:CUDA::cuda_driver>
6989
FOLDER examples
7090
)
91+
slang_add_target(example-main STATIC FOLDER examples)
92+
slang_add_target(example-winmain STATIC FOLDER examples EXCLUDE_FROM_ALL)
93+
slang_add_target(stacktrace-windows STATIC FOLDER examples EXCLUDE_FROM_ALL)
7194

7295
add_custom_target(
7396
all-examples

examples/autodiff-texture/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -823,4 +823,4 @@ struct AutoDiffTexture : public WindowedAppBase
823823
}
824824
};
825825

826-
PLATFORM_UI_MAIN(innerMain<AutoDiffTexture>)
826+
EXAMPLE_MAIN(innerMain<AutoDiffTexture>);

examples/cpu-com-example/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ static SlangResult _innerMain(int argc, char** argv)
175175
return SLANG_OK;
176176
}
177177

178-
int main(int argc, char** argv)
178+
int exampleMain(int argc, char** argv)
179179
{
180180
return SLANG_SUCCEEDED(_innerMain(argc, argv)) ? 0 : -1;
181181
}

examples/cpu-hello-world/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ static SlangResult _innerMain(int argc, char** argv)
217217
return SLANG_OK;
218218
}
219219

220-
int main(int argc, char** argv)
220+
int exampleMain(int argc, char** argv)
221221
{
222222
return SLANG_SUCCEEDED(_innerMain(argc, argv)) ? 0 : -1;
223223
}

examples/example-base/example-base.h

+13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
1010
void _Win32OutputDebugString(const char* str);
1111
#endif
1212

13+
#define SLANG_STRINGIFY(x) #x
14+
#define SLANG_EXPAND_STRINGIFY(x) SLANG_STRINGIFY(x)
15+
16+
#ifdef _WIN32
17+
#define EXAMPLE_MAIN(innerMain) \
18+
extern const char* const g_logFileName = \
19+
"log-" SLANG_EXPAND_STRINGIFY(SLANG_EXAMPLE_NAME) ".txt"; \
20+
PLATFORM_UI_MAIN(innerMain);
21+
22+
#else
23+
#define EXAMPLE_MAIN(innerMain) PLATFORM_UI_MAIN(innerMain)
24+
#endif // _WIN32
25+
1326
struct WindowedAppBase : public TestBase
1427
{
1528
protected:

examples/example-main/main.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "../stacktrace-windows/common.h"
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
6+
extern int exampleMain(int argc, char** argv);
7+
8+
#if defined(_WIN32)
9+
10+
#include <windows.h>
11+
12+
int main(int argc, char** argv)
13+
{
14+
__try
15+
{
16+
return exampleMain(argc, argv);
17+
}
18+
__except (exceptionFilter(stdout, GetExceptionInformation()))
19+
{
20+
::exit(1);
21+
}
22+
}
23+
24+
#else // defined(_WIN32)
25+
26+
int main(int argc, char** argv)
27+
{
28+
// TODO: Catch exception and print stack trace also on non-Windows platforms.
29+
return exampleMain(argc, argv);
30+
}
31+
32+
#endif

examples/example-winmain/main.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "../stacktrace-windows/common.h"
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <windows.h>
6+
7+
extern int exampleMain(int argc, char** argv);
8+
extern const char* const g_logFileName;
9+
10+
int WinMain(
11+
HINSTANCE /* instance */,
12+
HINSTANCE /* prevInstance */,
13+
LPSTR /* commandLine */,
14+
int /*showCommand*/)
15+
16+
{
17+
FILE* logFile = fopen(g_logFileName, "w");
18+
__try
19+
{
20+
int argc = 0;
21+
char** argv = nullptr;
22+
return exampleMain(argc, argv);
23+
}
24+
__except (exceptionFilter(logFile, GetExceptionInformation()))
25+
{
26+
::exit(1);
27+
}
28+
}

examples/gpu-printing/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ struct ExampleProgram : public TestBase
152152
}
153153
};
154154

155-
int main(int argc, char* argv[])
155+
int exampleMain(int argc, char** argv)
156156
{
157157
ExampleProgram app;
158158
if (SLANG_FAILED(app.execute(argc, argv)))

examples/hello-world/main.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ struct HelloWorldExample : public TestBase
6666
~HelloWorldExample();
6767
};
6868

69-
int main(int argc, char* argv[])
69+
70+
int exampleMain(int argc, char** argv)
7071
{
7172
initDebugCallback();
7273
HelloWorldExample example;

examples/model-viewer/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -969,4 +969,4 @@ struct ModelViewer : WindowedAppBase
969969

970970
// This macro instantiates an appropriate main function to
971971
// run the application defined above.
972-
PLATFORM_UI_MAIN(innerMain<ModelViewer>)
972+
EXAMPLE_MAIN(innerMain<ModelViewer>);

examples/nv-aftermath-example/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -599,4 +599,4 @@ void AftermathCrashExample::renderFrame(int frameBufferIndex)
599599

600600
// This macro instantiates an appropriate main function to
601601
// run the application defined above.
602-
PLATFORM_UI_MAIN(innerMain<AftermathCrashExample>)
602+
EXAMPLE_MAIN(innerMain<AftermathCrashExample>)

examples/platform-test/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,4 @@ struct PlatformTest : public WindowedAppBase
122122

123123
// This macro instantiates an appropriate main function to
124124
// run the application defined above.
125-
PLATFORM_UI_MAIN(innerMain<PlatformTest>)
125+
EXAMPLE_MAIN(innerMain<PlatformTest>);

examples/ray-tracing-pipeline/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -712,4 +712,4 @@ struct RayTracing : public WindowedAppBase
712712

713713
// This macro instantiates an appropriate main function to
714714
// run the application defined above.
715-
PLATFORM_UI_MAIN(innerMain<RayTracing>)
715+
EXAMPLE_MAIN(innerMain<RayTracing>);

examples/ray-tracing/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -676,4 +676,4 @@ struct RayTracing : public WindowedAppBase
676676

677677
// This macro instantiates an appropriate main function to
678678
// run the application defined above.
679-
PLATFORM_UI_MAIN(innerMain<RayTracing>)
679+
EXAMPLE_MAIN(innerMain<RayTracing>);

examples/reflection-api/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,7 @@ struct ExampleProgram : public TestBase
14691469
}
14701470
};
14711471

1472-
int main(int argc, char* argv[])
1472+
int exampleMain(int argc, char** argv)
14731473
{
14741474
ExampleProgram app;
14751475
if (SLANG_FAILED(app.execute(argc, argv)))

examples/shader-object/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ Result loadShaderProgram(
131131
}
132132

133133
// Main body of the example.
134-
int main(int argc, char* argv[])
134+
int exampleMain(int argc, char** argv)
135135
{
136136
testBase.parseOption(argc, argv);
137137

examples/shader-toy/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -408,4 +408,4 @@ struct ShaderToyApp : public WindowedAppBase
408408

409409
// This macro instantiates an appropriate main function to
410410
// run the application defined above.
411-
PLATFORM_UI_MAIN(innerMain<ShaderToyApp>)
411+
EXAMPLE_MAIN(innerMain<ShaderToyApp>);

0 commit comments

Comments
 (0)