core/logic/DebugReporter.cpp
author David Anderson <dvander@alliedmods.net>
Sat Feb 16 17:28:17 2013 -0800 (2013-02-16)
changeset 3796 746d8f1b081f
parent 3792 d8a07e40bcb6
permissions -rw-r--r--
Fix more build bustage.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include <IPluginSys.h>
    33 #include <stdarg.h>
    34 #include "DebugReporter.h"
    35 
    36 DebugReport g_DbgReporter;
    37 
    38 void DebugReport::OnSourceModAllInitialized()
    39 {
    40 	g_pSourcePawn->SetDebugListener(this);
    41 }
    42 
    43 void DebugReport::OnDebugSpew(const char *msg, ...)
    44 {
    45 	va_list ap;
    46 	char buffer[512];
    47 
    48 	va_start(ap, msg);
    49 	smcore.FormatArgs(buffer, sizeof(buffer), msg, ap);
    50 	va_end(ap);
    51 
    52 	smcore.Log("[SM] %s", buffer);
    53 }
    54 
    55 void DebugReport::GenerateError(IPluginContext *ctx, cell_t func_idx, int err, const char *message, ...)
    56 {
    57 	va_list ap;
    58 
    59 	va_start(ap, message);
    60 	GenerateErrorVA(ctx, func_idx, err, message, ap);
    61 	va_end(ap);
    62 }
    63 
    64 void DebugReport::GenerateErrorVA(IPluginContext *ctx, cell_t func_idx, int err, const char *message, va_list ap)
    65 {
    66 	char buffer[512];
    67 	smcore.FormatArgs(buffer, sizeof(buffer), message, ap);
    68 
    69 	const char *plname = pluginsys->FindPluginByContext(ctx->GetContext())->GetFilename();
    70 	const char *error = g_pSourcePawn2->GetErrorString(err);
    71 
    72 	if (error)
    73 	{
    74 		smcore.LogError("[SM] Plugin \"%s\" encountered error %d: %s", plname, err, error);
    75 	} else {
    76 		smcore.LogError("[SM] Plugin \"%s\" encountered unknown error %d", plname, err);
    77 	}
    78 
    79 	smcore.LogError("[SM] %s", buffer);
    80 
    81 	if (func_idx != -1)
    82 	{
    83 		if (func_idx & 1)
    84 		{
    85 			func_idx >>= 1;
    86 			sp_public_t *function;
    87 			if (ctx->GetRuntime()->GetPublicByIndex(func_idx, &function) == SP_ERROR_NONE)
    88 			{
    89 				smcore.LogError("[SM] Unable to call function \"%s\" due to above error(s).", function->name);
    90 			}
    91 		}
    92 	}
    93 }
    94 
    95 void DebugReport::GenerateCodeError(IPluginContext *pContext, uint32_t code_addr, int err, const char *message, ...)
    96 {
    97 	va_list ap;
    98 	char buffer[512];
    99 
   100 	va_start(ap, message);
   101 	smcore.FormatArgs(buffer, sizeof(buffer), message, ap);
   102 	va_end(ap);
   103 
   104 	const char *plname = pluginsys->FindPluginByContext(pContext->GetContext())->GetFilename();
   105 	const char *error = g_pSourcePawn2->GetErrorString(err);
   106 
   107 	if (error)
   108 	{
   109 		smcore.LogError("[SM] Plugin \"%s\" encountered error %d: %s", plname, err, error);
   110 	} else {
   111 		smcore.LogError("[SM] Plugin \"%s\" encountered unknown error %d", plname, err);
   112 	}
   113 
   114 	smcore.LogError("[SM] %s", buffer);
   115 
   116 	IPluginDebugInfo *pDebug;
   117 	if ((pDebug = pContext->GetRuntime()->GetDebugInfo()) == NULL)
   118 	{
   119 		smcore.LogError("[SM] Debug mode is not enabled for \"%s\"", plname);
   120 		smcore.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on",
   121 			_GetPluginIndex(pContext));
   122 		return;
   123 	}
   124 
   125 	const char *name;
   126 	if (pDebug->LookupFunction(code_addr, &name) == SP_ERROR_NONE)
   127 	{
   128 		smcore.LogError("[SM] Unable to call function \"%s\" due to above error(s).", name);
   129 	} else {
   130 		smcore.LogError("[SM] Unable to call function (name unknown, address \"%x\").", code_addr);
   131 	}
   132 }
   133 
   134 void DebugReport::OnContextExecuteError(IPluginContext *ctx, IContextTrace *error)
   135 {
   136 	const char *lastname;
   137 	const char *plname = pluginsys->FindPluginByContext(ctx->GetContext())->GetFilename();
   138 	int n_err = error->GetErrorCode();
   139 
   140 	if (n_err != SP_ERROR_NATIVE)
   141 	{
   142 		smcore.LogError("[SM] Plugin encountered error %d: %s",
   143 			n_err,
   144 			error->GetErrorString());
   145 	}
   146 
   147 	if ((lastname=error->GetLastNative(NULL)) != NULL)
   148 	{
   149 		const char *custerr;
   150 		if ((custerr=error->GetCustomErrorString()) != NULL)
   151 		{
   152 			smcore.LogError("[SM] Native \"%s\" reported: %s", lastname, custerr);
   153 		} else {
   154 			smcore.LogError("[SM] Native \"%s\" encountered a generic error.", lastname);
   155 		}
   156 	}
   157 
   158 	if (!error->DebugInfoAvailable())
   159 	{
   160 		smcore.LogError("[SM] Debug mode is not enabled for \"%s\"", plname);
   161 		smcore.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on",
   162 			_GetPluginIndex(ctx));
   163 		return;
   164 	}
   165 
   166 	CallStackInfo stk_info;
   167 	int i = 0;
   168 	smcore.LogError("[SM] Displaying call stack trace for plugin \"%s\":", plname);
   169 	while (error->GetTraceInfo(&stk_info))
   170 	{
   171 		smcore.LogError("[SM]   [%d]  Line %d, %s::%s()",
   172 			i++,
   173 			stk_info.line,
   174 			stk_info.filename,
   175 			stk_info.function);
   176 	}
   177 }
   178 
   179 int DebugReport::_GetPluginIndex(IPluginContext *ctx)
   180 {
   181 	int id = 1;
   182 	IPluginIterator *iter = pluginsys->GetPluginIterator();
   183 
   184 	for (; iter->MorePlugins(); iter->NextPlugin(), id++)
   185 	{
   186 		IPlugin *pl = iter->GetPlugin();
   187 		if (pl->GetBaseContext() == ctx)
   188 		{
   189 			iter->Release();
   190 			return id;
   191 		}
   192 	}
   193 
   194 	iter->Release();
   195 
   196 	/* If we don't know which plugin this is, it's one being loaded.  Fake its index for now. */
   197 
   198 	return pluginsys->GetPluginCount() + 1;
   199 }
   200