Browse Source

Minor improvements in backtrace

Panagiotis Christopoulos Charitos 4 years ago
parent
commit
2b0d2e5385
5 changed files with 24 additions and 41 deletions
  1. 3 13
      AnKi/Core/App.cpp
  2. 3 13
      AnKi/Util/Assert.cpp
  3. 4 4
      AnKi/Util/System.cpp
  4. 12 11
      AnKi/Util/System.h
  5. 2 0
      CMakeLists.txt

+ 3 - 13
AnKi/Core/App.cpp

@@ -731,20 +731,10 @@ void App::setSignalHandlers()
 		else
 			printf("Caught signal %d\n", signum);
 
-		class BW : public BackTraceWalker
-		{
-		public:
-			U32 m_c = 0;
-
-			void operator()(const char* symbol)
-			{
-				printf("%.2u: %s\n", m_c++, symbol);
-			}
-		};
-
-		BW bw;
+		U32 count = 0;
 		printf("Backtrace:\n");
-		getBacktrace(bw);
+		backtrace(HeapAllocator<U8>(allocAligned, nullptr),
+				  [&count](CString symbol) { printf("%.2u: %s\n", count++, symbol.cstr()); });
 
 		ANKI_DEBUG_BREAK();
 	};

+ 3 - 13
AnKi/Util/Assert.cpp

@@ -34,20 +34,10 @@ void akassert(const char* exprTxt, const char* file, int line, const char* func)
 	}
 #	endif
 
-	class BW : public BackTraceWalker
-	{
-	public:
-		U32 m_c = 0;
-
-		void operator()(const char* symbol)
-		{
-			printf("%.2u: %s\n", m_c++, symbol);
-		}
-	};
-
-	BW bw;
 	printf("Backtrace:\n");
-	getBacktrace(bw);
+	U32 count = 0;
+	backtrace(HeapAllocator<U8>(allocAligned, nullptr),
+			  [&count](CString symbol) { printf("%.2u: %s\n", count++, symbol.cstr()); });
 
 	ANKI_DEBUG_BREAK();
 }

+ 4 - 4
AnKi/Util/System.cpp

@@ -38,7 +38,7 @@ U32 getCpuCoresCount()
 #endif
 }
 
-void getBacktrace(BackTraceWalker& walker)
+void backtraceInternal(const Function<void(CString)>& lambda)
 {
 #if ANKI_POSIX && !ANKI_OS_ANDROID
 	// Get addresses's for all entries on the stack
@@ -46,7 +46,7 @@ void getBacktrace(BackTraceWalker& walker)
 	void** array = static_cast<void**>(malloc(maxStackSize * sizeof(void*)));
 	if(array)
 	{
-		const I32 size = backtrace(array, I32(maxStackSize));
+		const I32 size = ::backtrace(array, I32(maxStackSize));
 
 		// Get symbols
 		char** strings = backtrace_symbols(array, size);
@@ -55,7 +55,7 @@ void getBacktrace(BackTraceWalker& walker)
 		{
 			for(I32 i = 0; i < size; ++i)
 			{
-				walker(strings[i]);
+				lambda(strings[i]);
 			}
 
 			free(strings);
@@ -64,7 +64,7 @@ void getBacktrace(BackTraceWalker& walker)
 		free(array);
 	}
 #else
-	walker("getBacktrace() not supported in " ANKI_OS_STR);
+	lambda("backtrace() not supported in " ANKI_OS_STR);
 #endif
 }
 

+ 12 - 11
AnKi/Util/System.h

@@ -6,6 +6,8 @@
 #pragma once
 
 #include <AnKi/Util/StdTypes.h>
+#include <AnKi/Util/Function.h>
+#include <AnKi/Util/String.h>
 #include <ctime>
 
 namespace anki
@@ -17,18 +19,17 @@ namespace anki
 /// Get the number of CPU cores
 U32 getCpuCoresCount();
 
-/// Backtrace walker.
-class BackTraceWalker
-{
-public:
-	virtual ~BackTraceWalker()
-	{
-	}
-
-	virtual void operator()(const char* symbol) = 0;
-};
+/// @internal
+void backtraceInternal(const Function<void(CString)>& lambda);
 
-void getBacktrace(BackTraceWalker& walker);
+/// Get a backtrace.
+template<typename TFunc>
+void backtrace(GenericMemoryPoolAllocator<U8> alloc, TFunc func)
+{
+	Function<void(CString)> f(alloc, func);
+	backtraceInternal(f);
+	f.destroy(alloc);
+}
 
 /// Return true if the engine is running from a terminal emulator.
 Bool runningFromATerminal();

+ 2 - 0
CMakeLists.txt

@@ -196,8 +196,10 @@ if(NOT MSVC)
 		add_definitions("-O3 -DNDEBUG")
 	elseif(${CMAKE_BUILD_TYPE} STREQUAL "RelWithDebInfo")
 		add_definitions("-O3 -g3 -fno-omit-frame-pointer")
+		set(LINKER_FLAGS "${LINKER_FLAGS} -rdynamic ") # For backtrace()
 	elseif(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
 		add_definitions("-O0 -g3")
+		set(LINKER_FLAGS "${LINKER_FLAGS} -rdynamic ") # For backtrace()
 	else()
 		message(FATAL_ERROR "Wrong CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
 	endif()