Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions core/logic/DebugReporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
* Version: $Id$
*/

#include <ISourceMod.h>
#include <IPluginSys.h>
#include <stdarg.h>
#include "DebugReporter.h"
#include "Logger.h"
#include <am-string.h>

DebugReport g_DbgReporter;

Expand Down Expand Up @@ -194,35 +194,53 @@ void DebugReport::ReportError(const IErrorReport &report, IFrameIterator &iter)
g_Logger.LogError("[SM] Blaming: %s", blame);
}

if (!iter.Done())
ke::Vector<ke::AString> arr = GetStackTrace(&iter);
for (size_t i = 0; i < arr.length(); i++)
{
g_Logger.LogError("[SM] Call stack trace:");
g_Logger.LogError("%s", arr[i].chars());
}
}

ke::Vector<ke::AString> DebugReport::GetStackTrace(IFrameIterator *iter)
{
char temp[3072];
ke::Vector<ke::AString> trace;
iter->Reset();

if (!iter->Done())
{
trace.append("[SM] Call stack trace:");

for (int index = 0; !iter.Done(); iter.Next(), index++)
for (int index = 0; !iter->Done(); iter->Next(), index++)
{
const char *fn = iter.FunctionName();
const char *fn = iter->FunctionName();
if (!fn)
{
fn = "<unknown function>";
}
if (iter.IsNativeFrame())
if (iter->IsNativeFrame())
{
g_Logger.LogError("[SM] [%d] %s", index, fn);
g_pSM->Format(temp, sizeof(temp), "[SM] [%d] %s", index, fn);
trace.append(temp);
continue;
}
if (iter.IsScriptedFrame())
if (iter->IsScriptedFrame())
{
const char *file = iter.FilePath();
const char *file = iter->FilePath();
if (!file)
{
file = "<unknown>";
}
g_Logger.LogError("[SM] [%d] Line %d, %s::%s",
g_pSM->Format(temp, sizeof(temp), "[SM] [%d] Line %d, %s::%s",
index,
iter.LineNumber(),
iter->LineNumber(),
file,
fn);

trace.append(temp);
}
}
}

return trace;
}
3 changes: 3 additions & 0 deletions core/logic/DebugReporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

#include "sp_vm_api.h"
#include "common_logic.h"
#include <am-vector.h>
#include <am-string.h>

class DebugReport :
public SMGlobalClass,
Expand All @@ -48,6 +50,7 @@ class DebugReport :
void GenerateError(IPluginContext *ctx, cell_t func_idx, int err, const char *message, ...);
void GenerateErrorVA(IPluginContext *ctx, cell_t func_idx, int err, const char *message, va_list ap);
void GenerateCodeError(IPluginContext *ctx, uint32_t code_addr, int err, const char *message, ...);
ke::Vector<ke::AString> GetStackTrace(IFrameIterator *iter);
private:
int _GetPluginIndex(IPluginContext *ctx);
};
Expand Down
22 changes: 22 additions & 0 deletions core/logic/smn_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,27 @@ static cell_t FrameIterator_GetFilePath(IPluginContext *pContext, const cell_t *
return 0;
}

static cell_t LogStackTrace(IPluginContext *pContext, const cell_t *params)
{
char buffer[512];

g_pSM->FormatString(buffer, sizeof(buffer), pContext, params, 1);

IFrameIterator *it = pContext->CreateFrameIterator();
ke::Vector<ke::AString> arr = g_DbgReporter.GetStackTrace(it);
pContext->DestroyFrameIterator(it);

IPlugin *pPlugin = scripts->FindPluginByContext(pContext->GetContext());

g_Logger.LogError("[SM] Stack trace requested: %s", buffer);
g_Logger.LogError("[SM] Called from: %s", pPlugin->GetFilename());
for (size_t i = 0; i < arr.length(); i)
{
g_Logger.LogError("%s", arr[i].chars());
}
return 0;
}

REGISTER_NATIVES(coreNatives)
{
{"ThrowError", ThrowError},
Expand Down Expand Up @@ -967,6 +988,7 @@ REGISTER_NATIVES(coreNatives)
{"StoreToAddress", StoreToAddress},
{"IsNullVector", IsNullVector},
{"IsNullString", IsNullString},
{"LogStackTrace", LogStackTrace},

{"FrameIterator.FrameIterator", FrameIterator_Create},
{"FrameIterator.Next", FrameIterator_Next},
Expand Down