Skip to content

Commit

Permalink
Use a vector with static storage for the backtrace
Browse files Browse the repository at this point in the history
Avoid a dynamic memory allocation which may fail
  • Loading branch information
Flamefire committed Jan 4, 2025
1 parent fd1d563 commit a70e8d2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
4 changes: 2 additions & 2 deletions libs/s25client/s25client.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2005 - 2021 Settlers Freaks (sf-team at siedler25.org)
// Copyright (C) 2005 - 2025 Settlers Freaks (sf-team at siedler25.org)
//
// SPDX-License-Identifier: GPL-2.0-or-later

Expand Down Expand Up @@ -170,7 +170,7 @@ void showCrashMessage()

void handleException(void* pCtx = nullptr) noexcept
{
std::vector<void*> stacktrace = DebugInfo::GetStackTrace(pCtx);
const auto stacktrace = DebugInfo::GetStackTrace(pCtx);
try
{
LogTarget target = (LOG.getFileWriter()) ? LogTarget::FileAndStderr : LogTarget::Stderr;
Expand Down
24 changes: 12 additions & 12 deletions libs/s25main/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
#include "backtrace_config.h"
#include "network/GameClient.h"
#include "s25util/Log.h"
#include <boost/core/ignore_unused.hpp>
#include <boost/endian/arithmetic.hpp>
#include <boost/endian/conversion.hpp>
#include <boost/nowide/iostream.hpp>
#include <bzlib.h>
#include <memory>
#include <vector>

#if RTTR_BACKTRACE_HAS_DBGHELP
# include <windows.h>
Expand All @@ -43,7 +43,7 @@ typedef WINBOOL(WINAPI* StackWalkType)(DWORD MachineType, HANDLE hProcess, HANDL
namespace {
#if RTTR_BACKTRACE_HAS_DBGHELP
# define RTTR_CONTEXT_PTR_TYPE LPCONTEXT
bool captureBacktrace(std::vector<void*>& stacktrace, LPCONTEXT ctx = nullptr) noexcept
bool captureBacktrace(DebugInfo::stacktrace_t& stacktrace, LPCONTEXT ctx) noexcept
{
CONTEXT context;
# ifndef _MSC_VER
Expand Down Expand Up @@ -131,17 +131,17 @@ bool captureBacktrace(std::vector<void*>& stacktrace, LPCONTEXT ctx = nullptr) n
SymCleanup(process);
return true;
}
#elif RTTR_BACKTRACE_HAS_FUNCTION
bool captureBacktrace(std::vector<void*>& stacktrace, void* = nullptr) noexcept
#else
bool captureBacktrace(stacktrace_t& stacktrace, void*) noexcept
{
unsigned num_frames = backtrace(&stacktrace[0], stacktrace.size());
# if RTTR_BACKTRACE_HAS_FUNCTION
const auto num_frames = backtrace(&stacktrace[0], stacktrace.size());
stacktrace.resize(num_frames);
return true;
}
#else
bool captureBacktrace(std::vector<void*>&, void* = nullptr) noexcept
{
# else
boost::ignore_unused(stacktrace);
return false;
# endif
}
#endif
} // namespace
Expand Down Expand Up @@ -182,12 +182,12 @@ DebugInfo::~DebugInfo()
sock.Close();
}

std::vector<void*> DebugInfo::GetStackTrace(void* ctx) noexcept
DebugInfo::stacktrace_t DebugInfo::GetStackTrace(void* ctx) noexcept
{
#ifndef RTTR_CONTEXT_PTR_TYPE
using RTTR_CONTEXT_PTR_TYPE = void*;
#endif
std::vector<void*> stacktrace(256);
stacktrace_t stacktrace(stacktrace_t::static_capacity);
if(!captureBacktrace(stacktrace, static_cast<RTTR_CONTEXT_PTR_TYPE>(ctx)))
stacktrace.clear();
return stacktrace;
Expand Down Expand Up @@ -248,7 +248,7 @@ bool DebugInfo::SendString(const std::string& str)
return SendString(str.c_str(), str.length() + 1); // +1 to include nullptr terminator
}

bool DebugInfo::SendStackTrace(const std::vector<void*>& stacktrace)
bool DebugInfo::SendStackTrace(const stacktrace_t& stacktrace)
{
if(stacktrace.empty())
return false;
Expand Down
10 changes: 6 additions & 4 deletions libs/s25main/Debug.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright (C) 2005 - 2021 Settlers Freaks (sf-team at siedler25.org)
// Copyright (C) 2005 - 2025 Settlers Freaks (sf-team at siedler25.org)
//
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include "s25util/Socket.h"
#include <boost/container/static_vector.hpp>
#include <boost/filesystem/path.hpp>
#include <vector>

class BinaryFile;

Expand All @@ -16,18 +16,20 @@ class DebugInfo
Socket sock;

public:
using stacktrace_t = boost::container::static_vector<void*, 256>;

DebugInfo();
~DebugInfo();

static std::vector<void*> GetStackTrace(void* ctx = nullptr) noexcept;
static stacktrace_t GetStackTrace(void* ctx = nullptr) noexcept;

bool Send(const void* buffer, size_t length);
bool SendSigned(int32_t i);
bool SendUnsigned(uint32_t i);
bool SendString(const char* str, size_t len = 0);
bool SendString(const std::string& str);

bool SendStackTrace(const std::vector<void*>& stacktrace);
bool SendStackTrace(const stacktrace_t& stacktrace);
bool SendReplay();
bool SendAsyncLog(const boost::filesystem::path& asyncLogFilepath);
bool SendFile(BinaryFile& file);
Expand Down

0 comments on commit a70e8d2

Please sign in to comment.