2007-01-25 23:36:38 +01:00
|
|
|
/**
|
2007-03-22 22:50:20 +01:00
|
|
|
* vim: set ts=4 :
|
2007-08-01 04:12:47 +02:00
|
|
|
* ================================================================
|
|
|
|
* SourceMod
|
|
|
|
* Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
|
|
|
|
* ================================================================
|
2007-01-25 23:36:38 +01:00
|
|
|
*
|
2007-08-01 04:12:47 +02:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License,
|
|
|
|
* version 3.0, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* As a special exception, AlliedModders LLC gives you permission to
|
|
|
|
* link the code of this program (as well as its derivative works) to
|
|
|
|
* "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any
|
|
|
|
* Game MODs that run on software by the Valve Corporation. You must
|
|
|
|
* obey the GNU General Public License in all respects for all other
|
|
|
|
* code used. Additionally, AlliedModders LLC grants this exception
|
|
|
|
* to all derivative works. AlliedModders LLC defines further
|
|
|
|
* exceptions, found in LICENSE.txt (as of this writing, version
|
|
|
|
* JULY-31-2007), or <http://www.sourcemod.net/license.php>.
|
2007-01-25 23:36:38 +01:00
|
|
|
*
|
|
|
|
* Version: $Id$
|
|
|
|
*/
|
|
|
|
|
2007-03-10 22:26:04 +01:00
|
|
|
#include "DebugReporter.h"
|
|
|
|
#include "Logger.h"
|
2007-01-13 00:56:02 +01:00
|
|
|
#include "PluginSys.h"
|
2007-08-08 04:27:10 +02:00
|
|
|
#include "sm_stringutil.h"
|
2007-01-13 00:56:02 +01:00
|
|
|
|
2007-03-15 05:45:17 +01:00
|
|
|
DebugReport g_DbgReporter;
|
2007-02-10 11:38:23 +01:00
|
|
|
|
2007-08-08 04:27:10 +02:00
|
|
|
/* I'm really lazy. This should probably be exported to ISourcePawnEngine someday,
|
|
|
|
* but we need to make sure the JIT will deal with the version bump.
|
|
|
|
*/
|
|
|
|
extern const char *GetSourcePawnErrorMessage(int error);
|
|
|
|
|
2007-03-15 05:45:17 +01:00
|
|
|
void DebugReport::OnSourceModAllInitialized()
|
2007-01-13 00:56:02 +01:00
|
|
|
{
|
|
|
|
g_pSourcePawn->SetDebugListener(this);
|
|
|
|
}
|
|
|
|
|
2007-08-08 04:27:10 +02:00
|
|
|
void DebugReport::GenerateError(IPluginContext *ctx, cell_t func_idx, int err, const char *message, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char buffer[512];
|
|
|
|
|
|
|
|
va_start(ap, message);
|
|
|
|
UTIL_FormatArgs(buffer, sizeof(buffer), message, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename();
|
|
|
|
const char *error = GetSourcePawnErrorMessage(err);
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
g_Logger.LogError("[SM] Plugin \"%s\" encountered error %d: %s", plname, err, error);
|
|
|
|
} else {
|
|
|
|
g_Logger.LogError("[SM] Plugin \"%s\" encountered unknown error %d", plname, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_Logger.LogError("[SM] %s", buffer);
|
|
|
|
|
|
|
|
if (func_idx != -1)
|
|
|
|
{
|
|
|
|
if (func_idx & 1)
|
|
|
|
{
|
|
|
|
func_idx >>= 1;
|
|
|
|
sp_public_t *function;
|
|
|
|
if (ctx->GetPublicByIndex(func_idx, &function) == SP_ERROR_NONE)
|
|
|
|
{
|
|
|
|
g_Logger.LogError("[SM] Unable to call function \"%s\" due to above errors.", function->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-15 05:45:17 +01:00
|
|
|
void DebugReport::OnContextExecuteError(IPluginContext *ctx, IContextTrace *error)
|
2007-01-13 00:56:02 +01:00
|
|
|
{
|
|
|
|
const char *lastname;
|
|
|
|
const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename();
|
|
|
|
int n_err = error->GetErrorCode();
|
|
|
|
|
|
|
|
if (n_err != SP_ERROR_NATIVE)
|
|
|
|
{
|
2007-07-03 22:46:17 +02:00
|
|
|
g_Logger.LogError("[SM] Plugin encountered error %d: %s",
|
2007-01-13 00:56:02 +01:00
|
|
|
n_err,
|
|
|
|
error->GetErrorString());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((lastname=error->GetLastNative(NULL)) != NULL)
|
|
|
|
{
|
|
|
|
const char *custerr;
|
|
|
|
if ((custerr=error->GetCustomErrorString()) != NULL)
|
|
|
|
{
|
2007-02-12 06:46:00 +01:00
|
|
|
g_Logger.LogError("[SM] Native \"%s\" reported: %s", lastname, custerr);
|
2007-01-13 00:56:02 +01:00
|
|
|
} else {
|
2007-01-17 07:49:40 +01:00
|
|
|
g_Logger.LogError("[SM] Native \"%s\" encountered a generic error.", lastname);
|
2007-01-13 00:56:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!error->DebugInfoAvailable())
|
|
|
|
{
|
2007-07-03 22:46:17 +02:00
|
|
|
g_Logger.LogError("[SM] Debug mode is not enabled for \"%s\"", plname);
|
2007-01-17 07:49:40 +01:00
|
|
|
g_Logger.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on",
|
2007-01-13 00:56:02 +01:00
|
|
|
_GetPluginIndex(ctx));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CallStackInfo stk_info;
|
|
|
|
int i = 0;
|
2007-01-17 07:49:40 +01:00
|
|
|
g_Logger.LogError("[SM] Displaying call stack trace:");
|
2007-01-13 00:56:02 +01:00
|
|
|
while (error->GetTraceInfo(&stk_info))
|
|
|
|
{
|
2007-01-17 07:49:40 +01:00
|
|
|
g_Logger.LogError("[SM] [%d] Line %d, %s::%s()",
|
2007-01-13 00:56:02 +01:00
|
|
|
i++,
|
|
|
|
stk_info.line,
|
|
|
|
stk_info.filename,
|
|
|
|
stk_info.function);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-15 05:45:17 +01:00
|
|
|
int DebugReport::_GetPluginIndex(IPluginContext *ctx)
|
2007-01-13 00:56:02 +01:00
|
|
|
{
|
|
|
|
int id = 1;
|
|
|
|
IPluginIterator *iter = g_PluginSys.GetPluginIterator();
|
|
|
|
|
|
|
|
for (; iter->MorePlugins(); iter->NextPlugin(), id++)
|
|
|
|
{
|
|
|
|
IPlugin *pl = iter->GetPlugin();
|
|
|
|
if (pl->GetBaseContext() == ctx)
|
|
|
|
{
|
|
|
|
iter->Release();
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
iter->Release();
|
|
|
|
return -1;
|
2007-01-25 10:19:38 +01:00
|
|
|
}
|
|
|
|
|