Skip to content

Commit 34d16ec

Browse files
committed
format code
1 parent 6e40882 commit 34d16ec

File tree

5 files changed

+72
-97
lines changed

5 files changed

+72
-97
lines changed

examples/CMakeLists.txt

+16-36
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
function(example dir)
2-
cmake_parse_arguments(
3-
ARG
4-
"WIN32_EXECUTABLE"
5-
""
6-
""
7-
${ARGN}
8-
)
2+
cmake_parse_arguments(ARG "WIN32_EXECUTABLE" "" "" ${ARGN})
93

104
set(debug_dir ${CMAKE_CURRENT_BINARY_DIR}/${dir})
115

@@ -39,19 +33,19 @@ function(example dir)
3933
endif()
4034

4135
# Libraries providing a main function that prints stack traces on exceptions
42-
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
43-
# On Windows we have two different versions: main for "console applications" and
44-
# WinMain for normal Windows applications.
45-
if (${ARG_WIN32_EXECUTABLE})
46-
set (main_wrapper_libraries example-winmain)
47-
else()
48-
set (main_wrapper_libraries example-main)
49-
endif()
50-
# Add stack printing support
51-
set (main_wrapper_libraries ${main_wrapper_libraries} stacktrace-windows)
52-
set (main_wrapper_libraries ${main_wrapper_libraries} dbghelp.lib)
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)
5347
else()
54-
set (main_wrapper_libraries example-main)
48+
set(main_wrapper_libraries example-main)
5549
endif()
5650

5751
slang_add_target(
@@ -94,23 +88,9 @@ if(SLANG_ENABLE_EXAMPLES)
9488
$<$<BOOL:${SLANG_ENABLE_CUDA}>:CUDA::cuda_driver>
9589
FOLDER examples
9690
)
97-
slang_add_target(
98-
example-main
99-
STATIC
100-
FOLDER examples
101-
)
102-
slang_add_target(
103-
example-winmain
104-
STATIC
105-
FOLDER examples
106-
EXCLUDE_FROM_ALL
107-
)
108-
slang_add_target(
109-
stacktrace-windows
110-
STATIC
111-
FOLDER examples
112-
EXCLUDE_FROM_ALL
113-
)
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)
11494

11595
add_custom_target(
11696
all-examples

examples/example-base/example-base.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ void _Win32OutputDebugString(const char* str);
1414
#define SLANG_EXPAND_STRINGIFY(x) SLANG_STRINGIFY(x)
1515

1616
#ifdef _WIN32
17-
#define EXAMPLE_MAIN(innerMain) \
18-
extern const char*const g_logFileName = "stdout-" \
19-
SLANG_EXPAND_STRINGIFY(SLANG_EXAMPLE_NAME) \
20-
".txt"; \
17+
#define EXAMPLE_MAIN(innerMain) \
18+
extern const char* const g_logFileName = \
19+
"stdout-" SLANG_EXPAND_STRINGIFY(SLANG_EXAMPLE_NAME) ".txt"; \
2120
PLATFORM_UI_MAIN(innerMain);
2221

2322
#else

examples/example-main/main.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
#include "../stacktrace-windows/common.h"
2+
13
#include <stdio.h>
24
#include <stdlib.h>
3-
#include "../stacktrace-windows/common.h"
45

56
extern int exampleMain(int argc, char** argv);
67

examples/example-winmain/main.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
#include <windows.h>
1+
#include "../stacktrace-windows/common.h"
2+
23
#include <stdio.h>
34
#include <stdlib.h>
4-
#include "../stacktrace-windows/common.h"
5+
#include <windows.h>
56

67
extern int exampleMain(int argc, char** argv);
7-
extern const char *const g_logFileName;
8+
extern const char* const g_logFileName;
89

910
int WinMain(
10-
HINSTANCE /* instance */,
11-
HINSTANCE /* prevInstance */,
12-
LPSTR /* commandLine */,
13-
int /*showCommand*/)
11+
HINSTANCE /* instance */,
12+
HINSTANCE /* prevInstance */,
13+
LPSTR /* commandLine */,
14+
int /*showCommand*/)
1415

1516
{
1617
__try

examples/stacktrace-windows/common.cpp

+43-49
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
#include <vector>
1+
#include "common.h"
2+
3+
#include <dbghelp.h>
4+
#include <inttypes.h>
25
#include <string>
6+
#include <vector>
37
#include <windows.h>
4-
#include <inttypes.h>
5-
#include <dbghelp.h>
6-
#include "common.h"
78

8-
#define SLANG_EXAMPLE_LOG_ERROR(...) \
9-
printf("error: %s: %d: ", __FILE__, __LINE__); \
10-
print(__VA_ARGS__); \
9+
#define SLANG_EXAMPLE_LOG_ERROR(...) \
10+
printf("error: %s: %d: ", __FILE__, __LINE__); \
11+
print(__VA_ARGS__); \
1112
printf("\n");
1213

1314
static void print() {}
@@ -21,19 +22,16 @@ static bool getModuleFileNameAtAddress(DWORD64 const address, std::string& fileN
2122
{
2223
HMODULE module = NULL;
2324
{
24-
BOOL result =
25-
GetModuleHandleEx(
26-
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
27-
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
28-
(LPCTSTR)address,
29-
&module
30-
);
31-
if(result == 0)
25+
BOOL result = GetModuleHandleEx(
26+
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
27+
(LPCTSTR)address,
28+
&module);
29+
if (result == 0)
3230
{
3331
SLANG_EXAMPLE_LOG_ERROR(GetLastError());
3432
return false;
3533
}
36-
if(module == NULL)
34+
if (module == NULL)
3735
{
3836
SLANG_EXAMPLE_LOG_ERROR();
3937
return false;
@@ -42,15 +40,15 @@ static bool getModuleFileNameAtAddress(DWORD64 const address, std::string& fileN
4240

4341
std::vector<char> buffer(1U << 8U);
4442
uint32_t constexpr maxBufferSize = 1U << 20;
45-
while(buffer.size() < maxBufferSize)
43+
while (buffer.size() < maxBufferSize)
4644
{
4745
DWORD result = GetModuleFileNameA(module, buffer.data(), buffer.size());
48-
if(result == 0)
46+
if (result == 0)
4947
{
5048
SLANG_EXAMPLE_LOG_ERROR(GetLastError());
5149
return false;
5250
}
53-
else if(result == ERROR_INSUFFICIENT_BUFFER)
51+
else if (result == ERROR_INSUFFICIENT_BUFFER)
5452
{
5553
buffer.resize(buffer.size() << 1U);
5654
}
@@ -59,7 +57,7 @@ static bool getModuleFileNameAtAddress(DWORD64 const address, std::string& fileN
5957
break;
6058
}
6159
}
62-
if(buffer.size() == maxBufferSize)
60+
if (buffer.size() == maxBufferSize)
6361
{
6462
SLANG_EXAMPLE_LOG_ERROR();
6563
return false;
@@ -87,26 +85,24 @@ static bool printStack(HANDLE process, HANDLE thread, CONTEXT const& context)
8785
STACKFRAME64 frame = {};
8886
constexpr uint32_t maxFrameCount = 1U << 10;
8987
uint32_t frameIndex = 0U;
90-
while(frameIndex < maxFrameCount)
88+
while (frameIndex < maxFrameCount)
9189
{
9290
// Use the default routine
9391
PREAD_PROCESS_MEMORY_ROUTINE64 readMemoryRoutine = NULL;
9492
// Not sure what this is for, but documentation says most callers can pass NULL
9593
PTRANSLATE_ADDRESS_ROUTINE64 translateAddressRoutine = NULL;
9694
{
97-
BOOL result =
98-
StackWalk64(
99-
machineType,
100-
process,
101-
thread,
102-
&frame,
103-
&contextCopy,
104-
readMemoryRoutine,
105-
SymFunctionTableAccess64,
106-
SymGetModuleBase64,
107-
translateAddressRoutine
108-
);
109-
if(result == FALSE)
95+
BOOL result = StackWalk64(
96+
machineType,
97+
process,
98+
thread,
99+
&frame,
100+
&contextCopy,
101+
readMemoryRoutine,
102+
SymFunctionTableAccess64,
103+
SymGetModuleBase64,
104+
translateAddressRoutine);
105+
if (result == FALSE)
110106
break;
111107
}
112108

@@ -117,9 +113,8 @@ static bool printStack(HANDLE process, HANDLE thread, CONTEXT const& context)
117113
DWORD64 address = frame.AddrPC.Offset;
118114
// Not required, we want to look up the symbol exactly at the address
119115
PDWORD64 displacement = NULL;
120-
BOOL result =
121-
SymFromAddr(process, address, displacement, maybeSymbol);
122-
if(result == FALSE)
116+
BOOL result = SymFromAddr(process, address, displacement, maybeSymbol);
117+
if (result == FALSE)
123118
{
124119
SLANG_EXAMPLE_LOG_ERROR(GetLastError());
125120
maybeSymbol = NULL;
@@ -129,10 +124,10 @@ static bool printStack(HANDLE process, HANDLE thread, CONTEXT const& context)
129124
printf("%u", frameIndex);
130125

131126
std::string moduleFileName;
132-
if(getModuleFileNameAtAddress(frame.AddrPC.Offset, moduleFileName))
127+
if (getModuleFileNameAtAddress(frame.AddrPC.Offset, moduleFileName))
133128
printf(": %s", moduleFileName.c_str());
134129

135-
if(maybeSymbol)
130+
if (maybeSymbol)
136131
{
137132
PSYMBOL_INFO& symbol = maybeSymbol;
138133

@@ -142,8 +137,7 @@ static bool printStack(HANDLE process, HANDLE thread, CONTEXT const& context)
142137
DWORD displacement;
143138
if (SymGetLineFromAddr64(process, frame.AddrPC.Offset, &displacement, &line))
144139
{
145-
printf(": %s: %s: %lu",
146-
symbol->Name, line.FileName, line.LineNumber);
140+
printf(": %s: %s: %lu", symbol->Name, line.FileName, line.LineNumber);
147141
}
148142
else
149143
{
@@ -162,8 +156,9 @@ static bool printStack(HANDLE process, HANDLE thread, CONTEXT const& context)
162156

163157
int exceptionFilter(_EXCEPTION_POINTERS* exception)
164158
{
165-
printf("error: Exception 0x%x occurred. Stack trace:\n",
166-
exception->ExceptionRecord->ExceptionCode);
159+
printf(
160+
"error: Exception 0x%x occurred. Stack trace:\n",
161+
exception->ExceptionRecord->ExceptionCode);
167162

168163
HANDLE process = GetCurrentProcess();
169164
HANDLE thread = GetCurrentThread();
@@ -173,9 +168,8 @@ int exceptionFilter(_EXCEPTION_POINTERS* exception)
173168
// The default search paths should suffice
174169
PCSTR symbolFileSearchPath = NULL;
175170
BOOL loadSymbolsOfLoadedModules = TRUE;
176-
BOOL result =
177-
SymInitialize(process, symbolFileSearchPath, loadSymbolsOfLoadedModules);
178-
if(result == FALSE)
171+
BOOL result = SymInitialize(process, symbolFileSearchPath, loadSymbolsOfLoadedModules);
172+
if (result == FALSE)
179173
{
180174
printf("warning: Failed to load symbols\n");
181175
}
@@ -185,15 +179,15 @@ int exceptionFilter(_EXCEPTION_POINTERS* exception)
185179
}
186180
}
187181

188-
if(!printStack(process, thread, *exception->ContextRecord))
182+
if (!printStack(process, thread, *exception->ContextRecord))
189183
{
190184
printf("warning: Failed to print complete stack trace!\n");
191185
}
192186

193-
if(symbolsLoaded)
187+
if (symbolsLoaded)
194188
{
195189
BOOL result = SymCleanup(process);
196-
if(result == FALSE)
190+
if (result == FALSE)
197191
{
198192
SLANG_EXAMPLE_LOG_ERROR(GetLastError());
199193
}

0 commit comments

Comments
 (0)