1.1 --- a/core/DebugReporter.cpp Fri Jul 11 06:51:21 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,184 +0,0 @@
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 -void DebugReport::OnSourceModAllInitialized()
1.43 -{
1.44 - g_pSourcePawn->SetDebugListener(this);
1.45 -}
1.46 -
1.47 -void DebugReport::GenerateError(IPluginContext *ctx, cell_t func_idx, int err, const char *message, ...)
1.48 -{
1.49 - va_list ap;
1.50 - char buffer[512];
1.51 -
1.52 - va_start(ap, message);
1.53 - UTIL_FormatArgs(buffer, sizeof(buffer), message, ap);
1.54 - va_end(ap);
1.55 -
1.56 - const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename();
1.57 - const char *error = g_pSourcePawn2->GetErrorString(err);
1.58 -
1.59 - if (error)
1.60 - {
1.61 - g_Logger.LogError("[SM] Plugin \"%s\" encountered error %d: %s", plname, err, error);
1.62 - } else {
1.63 - g_Logger.LogError("[SM] Plugin \"%s\" encountered unknown error %d", plname, err);
1.64 - }
1.65 -
1.66 - g_Logger.LogError("[SM] %s", buffer);
1.67 -
1.68 - if (func_idx != -1)
1.69 - {
1.70 - if (func_idx & 1)
1.71 - {
1.72 - func_idx >>= 1;
1.73 - sp_public_t *function;
1.74 - if (ctx->GetPublicByIndex(func_idx, &function) == SP_ERROR_NONE)
1.75 - {
1.76 - g_Logger.LogError("[SM] Unable to call function \"%s\" due to above error(s).", function->name);
1.77 - }
1.78 - }
1.79 - }
1.80 -}
1.81 -
1.82 -void DebugReport::GenerateCodeError(IPluginContext *pContext, uint32_t code_addr, int err, const char *message, ...)
1.83 -{
1.84 - va_list ap;
1.85 - char buffer[512];
1.86 -
1.87 - va_start(ap, message);
1.88 - UTIL_FormatArgs(buffer, sizeof(buffer), message, ap);
1.89 - va_end(ap);
1.90 -
1.91 - const char *plname = g_PluginSys.FindPluginByContext(pContext->GetContext())->GetFilename();
1.92 - const char *error = g_pSourcePawn2->GetErrorString(err);
1.93 -
1.94 - if (error)
1.95 - {
1.96 - g_Logger.LogError("[SM] Plugin \"%s\" encountered error %d: %s", plname, err, error);
1.97 - } else {
1.98 - g_Logger.LogError("[SM] Plugin \"%s\" encountered unknown error %d", plname, err);
1.99 - }
1.100 -
1.101 - g_Logger.LogError("[SM] %s", buffer);
1.102 -
1.103 - IPluginDebugInfo *pDebug;
1.104 - if ((pDebug = pContext->GetDebugInfo()) == NULL)
1.105 - {
1.106 - g_Logger.LogError("[SM] Debug mode is not enabled for \"%s\"", plname);
1.107 - g_Logger.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on",
1.108 - _GetPluginIndex(pContext));
1.109 - return;
1.110 - }
1.111 -
1.112 - const char *name;
1.113 - if (pDebug->LookupFunction(code_addr, &name) == SP_ERROR_NONE)
1.114 - {
1.115 - g_Logger.LogError("[SM] Unable to call function \"%s\" due to above error(s).", name);
1.116 - } else {
1.117 - g_Logger.LogError("[SM] Unable to call function (name unknown, address \"%x\").", code_addr);
1.118 - }
1.119 -}
1.120 -
1.121 -void DebugReport::OnContextExecuteError(IPluginContext *ctx, IContextTrace *error)
1.122 -{
1.123 - const char *lastname;
1.124 - const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename();
1.125 - int n_err = error->GetErrorCode();
1.126 -
1.127 - if (n_err != SP_ERROR_NATIVE)
1.128 - {
1.129 - g_Logger.LogError("[SM] Plugin encountered error %d: %s",
1.130 - n_err,
1.131 - error->GetErrorString());
1.132 - }
1.133 -
1.134 - if ((lastname=error->GetLastNative(NULL)) != NULL)
1.135 - {
1.136 - const char *custerr;
1.137 - if ((custerr=error->GetCustomErrorString()) != NULL)
1.138 - {
1.139 - g_Logger.LogError("[SM] Native \"%s\" reported: %s", lastname, custerr);
1.140 - } else {
1.141 - g_Logger.LogError("[SM] Native \"%s\" encountered a generic error.", lastname);
1.142 - }
1.143 - }
1.144 -
1.145 - if (!error->DebugInfoAvailable())
1.146 - {
1.147 - g_Logger.LogError("[SM] Debug mode is not enabled for \"%s\"", plname);
1.148 - g_Logger.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on",
1.149 - _GetPluginIndex(ctx));
1.150 - return;
1.151 - }
1.152 -
1.153 - CallStackInfo stk_info;
1.154 - int i = 0;
1.155 - g_Logger.LogError("[SM] Displaying call stack trace for plugin \"%s\":", plname);
1.156 - while (error->GetTraceInfo(&stk_info))
1.157 - {
1.158 - g_Logger.LogError("[SM] [%d] Line %d, %s::%s()",
1.159 - i++,
1.160 - stk_info.line,
1.161 - stk_info.filename,
1.162 - stk_info.function);
1.163 - }
1.164 -}
1.165 -
1.166 -int DebugReport::_GetPluginIndex(IPluginContext *ctx)
1.167 -{
1.168 - int id = 1;
1.169 - IPluginIterator *iter = g_PluginSys.GetPluginIterator();
1.170 -
1.171 - for (; iter->MorePlugins(); iter->NextPlugin(), id++)
1.172 - {
1.173 - IPlugin *pl = iter->GetPlugin();
1.174 - if (pl->GetBaseContext() == ctx)
1.175 - {
1.176 - iter->Release();
1.177 - return id;
1.178 - }
1.179 - }
1.180 -
1.181 - iter->Release();
1.182 -
1.183 - /* If we don't know which plugin this is, it's one being loaded. Fake its index for now. */
1.184 -
1.185 - return g_PluginSys.GetPluginCount() + 1;
1.186 -}
1.187 -