Plasma Engine  2.0
Loading...
Searching...
No Matches
StackTracer_Posix.h
1#include <Foundation/FoundationInternal.h>
2PL_FOUNDATION_INTERNAL_HEADER
3
4#include <Foundation/System/StackTracer.h>
5
6#include <Foundation/Math/Math.h>
7
8#if __has_include(<execinfo.h>)
9# include <execinfo.h>
10# define HAS_EXECINFO 1
11#endif
12
13void plStackTracer::OnPluginEvent(const plPluginEvent& e)
14{
15}
16
17// static
18plUInt32 plStackTracer::GetStackTrace(plArrayPtr<void*>& trace, void* pContext)
19{
20#if HAS_EXECINFO
21 return backtrace(trace.GetPtr(), trace.GetCount());
22#else
23 return 0;
24#endif
25}
26
27// static
28void plStackTracer::ResolveStackTrace(const plArrayPtr<void*>& trace, PrintFunc printFunc)
29{
30#if HAS_EXECINFO
31 char szBuffer[512];
32
33 char** ppSymbols = backtrace_symbols(trace.GetPtr(), trace.GetCount());
34
35 if (ppSymbols != nullptr)
36 {
37 for (plUInt32 i = 0; i < trace.GetCount(); i++)
38 {
39 size_t uiLen = plMath::Min(strlen(ppSymbols[i]), static_cast<size_t>(PL_ARRAY_SIZE(szBuffer)) - 2);
40 memcpy(szBuffer, ppSymbols[i], uiLen);
41 szBuffer[uiLen] = '\n';
42 szBuffer[uiLen + 1] = '\0';
43
44 printFunc(szBuffer);
45 }
46
47 free(ppSymbols);
48 }
49#else
50 printFunc("Could not record stack trace on this Linux system, because execinfo.h is not available.");
51#endif
52}
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
PL_ALWAYS_INLINE plUInt32 GetCount() const
Returns the number of elements in the array.
Definition ArrayPtr.h:142
PL_ALWAYS_INLINE PointerType GetPtr() const
Returns the pointer to the array.
Definition ArrayPtr.h:118
static void ResolveStackTrace(const plArrayPtr< void * > &trace, PrintFunc printFunc)
Print a stack trace.
Definition StackTracer_NoImpl.h:12
static plUInt32 GetStackTrace(plArrayPtr< void * > &ref_trace, void *pContext=nullptr)
Captures the current stack trace.
Definition StackTracer_NoImpl.h:7
constexpr PL_ALWAYS_INLINE T Min(T f1, T f2)
Returns the smaller value, f1 or f2.
Definition Math_inl.h:27
The data that is broadcast whenever a plugin is (un-) loaded.
Definition Plugin.h:11