Skip to content

Commit

Permalink
Merge pull request #27 from slangbot/format-5973-aleino/examples-back…
Browse files Browse the repository at this point in the history
…trace

Format code for PR shader-slang#5973
  • Loading branch information
aleino-nv authored Dec 31, 2024
2 parents 6e40882 + 34d16ec commit 1e63875
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 97 deletions.
52 changes: 16 additions & 36 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
function(example dir)
cmake_parse_arguments(
ARG
"WIN32_EXECUTABLE"
""
""
${ARGN}
)
cmake_parse_arguments(ARG "WIN32_EXECUTABLE" "" "" ${ARGN})

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

Expand Down Expand Up @@ -39,19 +33,19 @@ function(example dir)
endif()

# Libraries providing a main function that prints stack traces on exceptions
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
# On Windows we have two different versions: main for "console applications" and
# WinMain for normal Windows applications.
if (${ARG_WIN32_EXECUTABLE})
set (main_wrapper_libraries example-winmain)
else()
set (main_wrapper_libraries example-main)
endif()
# Add stack printing support
set (main_wrapper_libraries ${main_wrapper_libraries} stacktrace-windows)
set (main_wrapper_libraries ${main_wrapper_libraries} dbghelp.lib)
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
# On Windows we have two different versions: main for "console applications" and
# WinMain for normal Windows applications.
if(${ARG_WIN32_EXECUTABLE})
set(main_wrapper_libraries example-winmain)
else()
set(main_wrapper_libraries example-main)
endif()
# Add stack printing support
set(main_wrapper_libraries ${main_wrapper_libraries} stacktrace-windows)
set(main_wrapper_libraries ${main_wrapper_libraries} dbghelp.lib)
else()
set (main_wrapper_libraries example-main)
set(main_wrapper_libraries example-main)
endif()

slang_add_target(
Expand Down Expand Up @@ -94,23 +88,9 @@ if(SLANG_ENABLE_EXAMPLES)
$<$<BOOL:${SLANG_ENABLE_CUDA}>:CUDA::cuda_driver>
FOLDER examples
)
slang_add_target(
example-main
STATIC
FOLDER examples
)
slang_add_target(
example-winmain
STATIC
FOLDER examples
EXCLUDE_FROM_ALL
)
slang_add_target(
stacktrace-windows
STATIC
FOLDER examples
EXCLUDE_FROM_ALL
)
slang_add_target(example-main STATIC FOLDER examples)
slang_add_target(example-winmain STATIC FOLDER examples EXCLUDE_FROM_ALL)
slang_add_target(stacktrace-windows STATIC FOLDER examples EXCLUDE_FROM_ALL)

add_custom_target(
all-examples
Expand Down
7 changes: 3 additions & 4 deletions examples/example-base/example-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ void _Win32OutputDebugString(const char* str);
#define SLANG_EXPAND_STRINGIFY(x) SLANG_STRINGIFY(x)

#ifdef _WIN32
#define EXAMPLE_MAIN(innerMain) \
extern const char*const g_logFileName = "stdout-" \
SLANG_EXPAND_STRINGIFY(SLANG_EXAMPLE_NAME) \
".txt"; \
#define EXAMPLE_MAIN(innerMain) \
extern const char* const g_logFileName = \
"stdout-" SLANG_EXPAND_STRINGIFY(SLANG_EXAMPLE_NAME) ".txt"; \
PLATFORM_UI_MAIN(innerMain);

#else
Expand Down
3 changes: 2 additions & 1 deletion examples/example-main/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "../stacktrace-windows/common.h"

#include <stdio.h>
#include <stdlib.h>
#include "../stacktrace-windows/common.h"

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

Expand Down
15 changes: 8 additions & 7 deletions examples/example-winmain/main.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#include <windows.h>
#include "../stacktrace-windows/common.h"

#include <stdio.h>
#include <stdlib.h>
#include "../stacktrace-windows/common.h"
#include <windows.h>

extern int exampleMain(int argc, char** argv);
extern const char *const g_logFileName;
extern const char* const g_logFileName;

int WinMain(
HINSTANCE /* instance */,
HINSTANCE /* prevInstance */,
LPSTR /* commandLine */,
int /*showCommand*/)
HINSTANCE /* instance */,
HINSTANCE /* prevInstance */,
LPSTR /* commandLine */,
int /*showCommand*/)

{
__try
Expand Down
92 changes: 43 additions & 49 deletions examples/stacktrace-windows/common.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include <vector>
#include "common.h"

#include <dbghelp.h>
#include <inttypes.h>
#include <string>
#include <vector>
#include <windows.h>
#include <inttypes.h>
#include <dbghelp.h>
#include "common.h"

#define SLANG_EXAMPLE_LOG_ERROR(...) \
printf("error: %s: %d: ", __FILE__, __LINE__); \
print(__VA_ARGS__); \
#define SLANG_EXAMPLE_LOG_ERROR(...) \
printf("error: %s: %d: ", __FILE__, __LINE__); \
print(__VA_ARGS__); \
printf("\n");

static void print() {}
Expand All @@ -21,19 +22,16 @@ static bool getModuleFileNameAtAddress(DWORD64 const address, std::string& fileN
{
HMODULE module = NULL;
{
BOOL result =
GetModuleHandleEx(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCTSTR)address,
&module
);
if(result == 0)
BOOL result = GetModuleHandleEx(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCTSTR)address,
&module);
if (result == 0)
{
SLANG_EXAMPLE_LOG_ERROR(GetLastError());
return false;
}
if(module == NULL)
if (module == NULL)
{
SLANG_EXAMPLE_LOG_ERROR();
return false;
Expand All @@ -42,15 +40,15 @@ static bool getModuleFileNameAtAddress(DWORD64 const address, std::string& fileN

std::vector<char> buffer(1U << 8U);
uint32_t constexpr maxBufferSize = 1U << 20;
while(buffer.size() < maxBufferSize)
while (buffer.size() < maxBufferSize)
{
DWORD result = GetModuleFileNameA(module, buffer.data(), buffer.size());
if(result == 0)
if (result == 0)
{
SLANG_EXAMPLE_LOG_ERROR(GetLastError());
return false;
}
else if(result == ERROR_INSUFFICIENT_BUFFER)
else if (result == ERROR_INSUFFICIENT_BUFFER)
{
buffer.resize(buffer.size() << 1U);
}
Expand All @@ -59,7 +57,7 @@ static bool getModuleFileNameAtAddress(DWORD64 const address, std::string& fileN
break;
}
}
if(buffer.size() == maxBufferSize)
if (buffer.size() == maxBufferSize)
{
SLANG_EXAMPLE_LOG_ERROR();
return false;
Expand Down Expand Up @@ -87,26 +85,24 @@ static bool printStack(HANDLE process, HANDLE thread, CONTEXT const& context)
STACKFRAME64 frame = {};
constexpr uint32_t maxFrameCount = 1U << 10;
uint32_t frameIndex = 0U;
while(frameIndex < maxFrameCount)
while (frameIndex < maxFrameCount)
{
// Use the default routine
PREAD_PROCESS_MEMORY_ROUTINE64 readMemoryRoutine = NULL;
// Not sure what this is for, but documentation says most callers can pass NULL
PTRANSLATE_ADDRESS_ROUTINE64 translateAddressRoutine = NULL;
{
BOOL result =
StackWalk64(
machineType,
process,
thread,
&frame,
&contextCopy,
readMemoryRoutine,
SymFunctionTableAccess64,
SymGetModuleBase64,
translateAddressRoutine
);
if(result == FALSE)
BOOL result = StackWalk64(
machineType,
process,
thread,
&frame,
&contextCopy,
readMemoryRoutine,
SymFunctionTableAccess64,
SymGetModuleBase64,
translateAddressRoutine);
if (result == FALSE)
break;
}

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

std::string moduleFileName;
if(getModuleFileNameAtAddress(frame.AddrPC.Offset, moduleFileName))
if (getModuleFileNameAtAddress(frame.AddrPC.Offset, moduleFileName))
printf(": %s", moduleFileName.c_str());

if(maybeSymbol)
if (maybeSymbol)
{
PSYMBOL_INFO& symbol = maybeSymbol;

Expand All @@ -142,8 +137,7 @@ static bool printStack(HANDLE process, HANDLE thread, CONTEXT const& context)
DWORD displacement;
if (SymGetLineFromAddr64(process, frame.AddrPC.Offset, &displacement, &line))
{
printf(": %s: %s: %lu",
symbol->Name, line.FileName, line.LineNumber);
printf(": %s: %s: %lu", symbol->Name, line.FileName, line.LineNumber);
}
else
{
Expand All @@ -162,8 +156,9 @@ static bool printStack(HANDLE process, HANDLE thread, CONTEXT const& context)

int exceptionFilter(_EXCEPTION_POINTERS* exception)
{
printf("error: Exception 0x%x occurred. Stack trace:\n",
exception->ExceptionRecord->ExceptionCode);
printf(
"error: Exception 0x%x occurred. Stack trace:\n",
exception->ExceptionRecord->ExceptionCode);

HANDLE process = GetCurrentProcess();
HANDLE thread = GetCurrentThread();
Expand All @@ -173,9 +168,8 @@ int exceptionFilter(_EXCEPTION_POINTERS* exception)
// The default search paths should suffice
PCSTR symbolFileSearchPath = NULL;
BOOL loadSymbolsOfLoadedModules = TRUE;
BOOL result =
SymInitialize(process, symbolFileSearchPath, loadSymbolsOfLoadedModules);
if(result == FALSE)
BOOL result = SymInitialize(process, symbolFileSearchPath, loadSymbolsOfLoadedModules);
if (result == FALSE)
{
printf("warning: Failed to load symbols\n");
}
Expand All @@ -185,15 +179,15 @@ int exceptionFilter(_EXCEPTION_POINTERS* exception)
}
}

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

if(symbolsLoaded)
if (symbolsLoaded)
{
BOOL result = SymCleanup(process);
if(result == FALSE)
if (result == FALSE)
{
SLANG_EXAMPLE_LOG_ERROR(GetLastError());
}
Expand Down

0 comments on commit 1e63875

Please sign in to comment.