core/DebugReporter.cpp
author David Anderson <dvander@alliedmods.net>
Wed Aug 08 02:30:03 2007 +0000 (2007-08-08)
changeset 1270 a6e3a3b8b7f3
parent 1269 27397584df9c
child 1330 ab94316bb52b
permissions -rw-r--r--
fixed linux build
     1 /**
     2  * vim: set ts=4 :
     3  * ================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * ================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or 
     9  * modify it under the terms of the GNU General Public License, 
    10  * version 3.0, as published by the Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful,
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  * GNU General Public License for more details.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to 
    21  * link the code of this program (as well as its derivative works) to 
    22  * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any 
    23  * Game MODs that run on software by the Valve Corporation.  You must 
    24  * obey the GNU General Public License in all respects for all other 
    25  * code used. Additionally, AlliedModders LLC grants this exception 
    26  * to all derivative works. AlliedModders LLC defines further 
    27  * exceptions, found in LICENSE.txt (as of this writing, version 
    28  * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #include "DebugReporter.h"
    34 #include "Logger.h"
    35 #include "PluginSys.h"
    36 #include "sm_stringutil.h"
    37 
    38 DebugReport g_DbgReporter;
    39 
    40 /* I'm really lazy.  This should probably be exported to ISourcePawnEngine someday,
    41  * but we need to make sure the JIT will deal with the version bump.
    42  */
    43 extern const char *GetSourcePawnErrorMessage(int error);
    44 
    45 void DebugReport::OnSourceModAllInitialized()
    46 {
    47 	g_pSourcePawn->SetDebugListener(this);
    48 }
    49 
    50 void DebugReport::GenerateError(IPluginContext *ctx, cell_t func_idx, int err, const char *message, ...)
    51 {
    52 	va_list ap;
    53 	char buffer[512];
    54 
    55 	va_start(ap, message);
    56 	UTIL_FormatArgs(buffer, sizeof(buffer), message, ap);
    57 	va_end(ap);
    58 
    59 	const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename();
    60 	const char *error = GetSourcePawnErrorMessage(err);
    61 
    62 	if (error)
    63 	{
    64 		g_Logger.LogError("[SM] Plugin \"%s\" encountered error %d: %s", plname, err, error);
    65 	} else {
    66 		g_Logger.LogError("[SM] Plugin \"%s\" encountered unknown error %d", plname, err);
    67 	}
    68 
    69 	g_Logger.LogError("[SM] %s", buffer);
    70 
    71 	if (func_idx != -1)
    72 	{
    73 		if (func_idx & 1)
    74 		{
    75 			func_idx >>= 1;
    76 			sp_public_t *function;
    77 			if (ctx->GetPublicByIndex(func_idx, &function) == SP_ERROR_NONE)
    78 			{
    79 				g_Logger.LogError("[SM] Unable to call function \"%s\" due to above errors.", function->name);
    80 			}
    81 		}
    82 	}
    83 }
    84 
    85 void DebugReport::OnContextExecuteError(IPluginContext *ctx, IContextTrace *error)
    86 {
    87 	const char *lastname;
    88 	const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename();
    89 	int n_err = error->GetErrorCode();
    90 
    91 	if (n_err != SP_ERROR_NATIVE)
    92 	{
    93 		g_Logger.LogError("[SM] Plugin encountered error %d: %s",
    94 			n_err,
    95 			error->GetErrorString());
    96 	}
    97 
    98 	if ((lastname=error->GetLastNative(NULL)) != NULL)
    99 	{
   100 		const char *custerr;
   101 		if ((custerr=error->GetCustomErrorString()) != NULL)
   102 		{
   103 			g_Logger.LogError("[SM] Native \"%s\" reported: %s", lastname, custerr);
   104 		} else {
   105 			g_Logger.LogError("[SM] Native \"%s\" encountered a generic error.", lastname);
   106 		}
   107 	}
   108 
   109 	if (!error->DebugInfoAvailable())
   110 	{
   111 		g_Logger.LogError("[SM] Debug mode is not enabled for \"%s\"", plname);
   112 		g_Logger.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on",
   113 			_GetPluginIndex(ctx));
   114 		return;
   115 	}
   116 
   117 	CallStackInfo stk_info;
   118 	int i = 0;
   119 	g_Logger.LogError("[SM] Displaying call stack trace:");
   120 	while (error->GetTraceInfo(&stk_info))
   121 	{
   122 		g_Logger.LogError("[SM]   [%d]  Line %d, %s::%s()",
   123 			i++,
   124 			stk_info.line,
   125 			stk_info.filename,
   126 			stk_info.function);
   127 	}
   128 }
   129 
   130 int DebugReport::_GetPluginIndex(IPluginContext *ctx)
   131 {
   132 	int id = 1;
   133 	IPluginIterator *iter = g_PluginSys.GetPluginIterator();
   134 
   135 	for (; iter->MorePlugins(); iter->NextPlugin(), id++)
   136 	{
   137 		IPlugin *pl = iter->GetPlugin();
   138 		if (pl->GetBaseContext() == ctx)
   139 		{
   140 			iter->Release();
   141 			return id;
   142 		}
   143 	}
   144 
   145 	iter->Release();
   146 	return -1;
   147 }
   148