aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/callstack.cpp
blob: 905ab3d9eeabae672ab1fb8f6db31c0f0d16cc31 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright Epic Games, Inc. All Rights Reserved.

#include <zencore/callstack.h>
#include <zencore/thread.h>

#if ZEN_PLATFORM_WINDOWS
#	include <zencore/windows.h>
#	include <Dbghelp.h>
#endif

#if ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
#	include <execinfo.h>
#endif

#if ZEN_WITH_TESTS
#	include <zencore/testing.h>
#endif

#include <fmt/format.h>

namespace zen {
#if ZEN_PLATFORM_WINDOWS

class WinSymbolInit
{
public:
	WinSymbolInit() {}
	~WinSymbolInit()
	{
		m_CallstackLock.WithExclusiveLock([this]() {
			if (m_Initialized)
			{
				SymCleanup(m_CurrentProcess);
			}
		});
	}

	bool GetSymbol(void* Frame, SYMBOL_INFO* OutSymbolInfo, DWORD64& OutDisplacement)
	{
		bool Result = false;
		m_CallstackLock.WithExclusiveLock([&]() {
			if (!m_Initialized)
			{
				m_CurrentProcess = GetCurrentProcess();
				if (SymInitialize(m_CurrentProcess, NULL, TRUE) == TRUE)
				{
					m_Initialized = true;
				}
			}
			if (m_Initialized)
			{
				if (SymFromAddr(m_CurrentProcess, (DWORD64)Frame, &OutDisplacement, OutSymbolInfo) == TRUE)
				{
					Result = true;
				}
			}
		});
		return Result;
	}

private:
	HANDLE m_CurrentProcess = NULL;
	BOOL   m_Initialized	= FALSE;
	RwLock m_CallstackLock;
};

static WinSymbolInit WinSymbols;

#endif

CallstackFrames*
CreateCallstack(uint32_t FrameCount, void** Frames) noexcept
{
	if (FrameCount == 0)
	{
		return nullptr;
	}
	CallstackFrames* Callstack = (CallstackFrames*)malloc(sizeof(CallstackFrames) + sizeof(void*) * FrameCount);
	if (Callstack != nullptr)
	{
		Callstack->FrameCount = FrameCount;
		if (FrameCount == 0)
		{
			Callstack->Frames = nullptr;
		}
		else
		{
			Callstack->Frames = (void**)&Callstack[1];
			memcpy(Callstack->Frames, Frames, sizeof(void*) * FrameCount);
		}
	}
	return Callstack;
}

CallstackFrames*
CloneCallstack(const CallstackFrames* Callstack) noexcept
{
	if (Callstack == nullptr)
	{
		return nullptr;
	}
	return CreateCallstack(Callstack->FrameCount, Callstack->Frames);
}

void
FreeCallstack(CallstackFrames* Callstack) noexcept
{
	if (Callstack != nullptr)
	{
		free(Callstack);
	}
}

uint32_t
GetCallstack(int FramesToSkip, int FramesToCapture, void* OutAddresses[])
{
#if ZEN_PLATFORM_WINDOWS
	return (uint32_t)CaptureStackBackTrace(FramesToSkip, FramesToCapture, OutAddresses, 0);
#endif
#if ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
	void* Frames[FramesToSkip + FramesToCapture];
	int	  FrameCount = backtrace(Frames, FramesToSkip + FramesToCapture);
	if (FrameCount > FramesToSkip)
	{
		for (int Index = FramesToSkip; Index < FrameCount; Index++)
		{
			OutAddresses[Index - FramesToSkip] = Frames[Index];
		}
		return (uint32_t)(FrameCount - FramesToSkip);
	}
	else
	{
		return 0;
	}
#endif
}

std::vector<std::string>
GetFrameSymbols(uint32_t FrameCount, void** Frames)
{
	std::vector<std::string> FrameSymbols;
	if (FrameCount > 0)
	{
		FrameSymbols.resize(FrameCount);
#if ZEN_PLATFORM_WINDOWS
		char		 SymbolBuffer[sizeof(SYMBOL_INFO) + 1024];
		SYMBOL_INFO* SymbolInfo	 = (SYMBOL_INFO*)SymbolBuffer;
		SymbolInfo->SizeOfStruct = sizeof(SYMBOL_INFO);
		SymbolInfo->MaxNameLen	 = 1023;
		DWORD64 Displacement	 = 0;
		for (uint32_t FrameIndex = 0; FrameIndex < FrameCount; FrameIndex++)
		{
			if (WinSymbols.GetSymbol(Frames[FrameIndex], SymbolInfo, Displacement))
			{
				FrameSymbols[FrameIndex] = fmt::format("{}+{:#x} [{:#x}]", SymbolInfo->Name, Displacement, (uintptr_t)Frames[FrameIndex]);
			}
		}
#endif
#if ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
		char** messages = backtrace_symbols(Frames, (int)FrameCount);
		if (messages)
		{
			for (uint32_t FrameIndex = 0; FrameIndex < FrameCount; FrameIndex++)
			{
				FrameSymbols[FrameIndex] = messages[FrameIndex];
			}
			free(messages);
		}
#endif
	}
	return FrameSymbols;
}

void
FormatCallstack(const CallstackFrames* Callstack, StringBuilderBase& SB)
{
	bool First = true;
	for (const std::string& Symbol : GetFrameSymbols(Callstack))
	{
		if (!First)
		{
			SB.Append("\n");
		}
		else
		{
			First = false;
		}
		SB.Append(Symbol);
	}
}

std::string
CallstackToString(const CallstackFrames* Callstack)
{
	StringBuilder<2048> SB;
	FormatCallstack(Callstack, SB);
	return SB.ToString();
}

#if ZEN_WITH_TESTS

TEST_CASE("Callstack.Basic")
{
	void*	 Addresses[4];
	uint32_t FrameCount = GetCallstack(1, 4, Addresses);
	CHECK(FrameCount > 0);
	std::vector<std::string> Symbols = GetFrameSymbols(FrameCount, Addresses);
	for (const std::string& Symbol : Symbols)
	{
		CHECK(!Symbol.empty());
	}
}

void
callstack_forcelink()
{
}

#endif

}  // namespace zen