core/DebugReporter.cpp
changeset 1952 b7aa2b626807
child 2061 77bd041bbe72
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/DebugReporter.cpp	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,186 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod
     1.8 + * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id$
    1.33 + */
    1.34 +
    1.35 +#include "DebugReporter.h"
    1.36 +#include "Logger.h"
    1.37 +#include "PluginSys.h"
    1.38 +#include "sm_stringutil.h"
    1.39 +
    1.40 +DebugReport g_DbgReporter;
    1.41 +
    1.42 +/* I'm really lazy.  This should probably be exported to ISourcePawnEngine someday,
    1.43 + * but we need to make sure the JIT will deal with the version bump.
    1.44 + */
    1.45 +extern const char *GetSourcePawnErrorMessage(int error);
    1.46 +
    1.47 +void DebugReport::OnSourceModAllInitialized()
    1.48 +{
    1.49 +	g_pSourcePawn->SetDebugListener(this);
    1.50 +}
    1.51 +
    1.52 +void DebugReport::GenerateError(IPluginContext *ctx, cell_t func_idx, int err, const char *message, ...)
    1.53 +{
    1.54 +	va_list ap;
    1.55 +	char buffer[512];
    1.56 +
    1.57 +	va_start(ap, message);
    1.58 +	UTIL_FormatArgs(buffer, sizeof(buffer), message, ap);
    1.59 +	va_end(ap);
    1.60 +
    1.61 +	const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename();
    1.62 +	const char *error = GetSourcePawnErrorMessage(err);
    1.63 +
    1.64 +	if (error)
    1.65 +	{
    1.66 +		g_Logger.LogError("[SM] Plugin \"%s\" encountered error %d: %s", plname, err, error);
    1.67 +	} else {
    1.68 +		g_Logger.LogError("[SM] Plugin \"%s\" encountered unknown error %d", plname, err);
    1.69 +	}
    1.70 +
    1.71 +	g_Logger.LogError("[SM] %s", buffer);
    1.72 +
    1.73 +	if (func_idx != -1)
    1.74 +	{
    1.75 +		if (func_idx & 1)
    1.76 +		{
    1.77 +			func_idx >>= 1;
    1.78 +			sp_public_t *function;
    1.79 +			if (ctx->GetPublicByIndex(func_idx, &function) == SP_ERROR_NONE)
    1.80 +			{
    1.81 +				g_Logger.LogError("[SM] Unable to call function \"%s\" due to above error(s).", function->name);
    1.82 +			}
    1.83 +		}
    1.84 +	}
    1.85 +}
    1.86 +
    1.87 +void DebugReport::GenerateCodeError(IPluginContext *pContext, uint32_t code_addr, int err, const char *message, ...)
    1.88 +{
    1.89 +	va_list ap;
    1.90 +	char buffer[512];
    1.91 +
    1.92 +	va_start(ap, message);
    1.93 +	UTIL_FormatArgs(buffer, sizeof(buffer), message, ap);
    1.94 +	va_end(ap);
    1.95 +
    1.96 +	const char *plname = g_PluginSys.FindPluginByContext(pContext->GetContext())->GetFilename();
    1.97 +	const char *error = GetSourcePawnErrorMessage(err);
    1.98 +
    1.99 +	if (error)
   1.100 +	{
   1.101 +		g_Logger.LogError("[SM] Plugin \"%s\" encountered error %d: %s", plname, err, error);
   1.102 +	} else {
   1.103 +		g_Logger.LogError("[SM] Plugin \"%s\" encountered unknown error %d", plname, err);
   1.104 +	}
   1.105 +
   1.106 +	g_Logger.LogError("[SM] %s", buffer);
   1.107 +
   1.108 +	IPluginDebugInfo *pDebug;
   1.109 +	if ((pDebug = pContext->GetDebugInfo()) == NULL)
   1.110 +	{
   1.111 +		g_Logger.LogError("[SM] Debug mode is not enabled for \"%s\"", plname);
   1.112 +		g_Logger.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on",
   1.113 +			_GetPluginIndex(pContext));
   1.114 +		return;
   1.115 +	}
   1.116 +
   1.117 +	const char *name;
   1.118 +	if (pDebug->LookupFunction(code_addr, &name) == SP_ERROR_NONE)
   1.119 +	{
   1.120 +		g_Logger.LogError("[SM] Unable to call function \"%s\" due to above error(s).", name);
   1.121 +	} else {
   1.122 +		g_Logger.LogError("[SM] Unable to call function (name unknown, address \"%x\").", code_addr);
   1.123 +	}
   1.124 +}
   1.125 +
   1.126 +void DebugReport::OnContextExecuteError(IPluginContext *ctx, IContextTrace *error)
   1.127 +{
   1.128 +	const char *lastname;
   1.129 +	const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename();
   1.130 +	int n_err = error->GetErrorCode();
   1.131 +
   1.132 +	if (n_err != SP_ERROR_NATIVE)
   1.133 +	{
   1.134 +		g_Logger.LogError("[SM] Plugin encountered error %d: %s",
   1.135 +			n_err,
   1.136 +			error->GetErrorString());
   1.137 +	}
   1.138 +
   1.139 +	if ((lastname=error->GetLastNative(NULL)) != NULL)
   1.140 +	{
   1.141 +		const char *custerr;
   1.142 +		if ((custerr=error->GetCustomErrorString()) != NULL)
   1.143 +		{
   1.144 +			g_Logger.LogError("[SM] Native \"%s\" reported: %s", lastname, custerr);
   1.145 +		} else {
   1.146 +			g_Logger.LogError("[SM] Native \"%s\" encountered a generic error.", lastname);
   1.147 +		}
   1.148 +	}
   1.149 +
   1.150 +	if (!error->DebugInfoAvailable())
   1.151 +	{
   1.152 +		g_Logger.LogError("[SM] Debug mode is not enabled for \"%s\"", plname);
   1.153 +		g_Logger.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on",
   1.154 +			_GetPluginIndex(ctx));
   1.155 +		return;
   1.156 +	}
   1.157 +
   1.158 +	CallStackInfo stk_info;
   1.159 +	int i = 0;
   1.160 +	g_Logger.LogError("[SM] Displaying call stack trace for plugin \"%s\":", plname);
   1.161 +	while (error->GetTraceInfo(&stk_info))
   1.162 +	{
   1.163 +		g_Logger.LogError("[SM]   [%d]  Line %d, %s::%s()",
   1.164 +			i++,
   1.165 +			stk_info.line,
   1.166 +			stk_info.filename,
   1.167 +			stk_info.function);
   1.168 +	}
   1.169 +}
   1.170 +
   1.171 +int DebugReport::_GetPluginIndex(IPluginContext *ctx)
   1.172 +{
   1.173 +	int id = 1;
   1.174 +	IPluginIterator *iter = g_PluginSys.GetPluginIterator();
   1.175 +
   1.176 +	for (; iter->MorePlugins(); iter->NextPlugin(), id++)
   1.177 +	{
   1.178 +		IPlugin *pl = iter->GetPlugin();
   1.179 +		if (pl->GetBaseContext() == ctx)
   1.180 +		{
   1.181 +			iter->Release();
   1.182 +			return id;
   1.183 +		}
   1.184 +	}
   1.185 +
   1.186 +	iter->Release();
   1.187 +	return -1;
   1.188 +}
   1.189 +