core/logic/DebugReporter.cpp
author David Anderson <dvander@alliedmods.net>
Sat Feb 16 15:57:00 2013 -0800 (2013-02-16)
changeset 3792 d8a07e40bcb6
parent 2989 core/DebugReporter.cpp@e8b0605226ce
child 3796 746d8f1b081f
permissions -rw-r--r--
Move DebugReporter from core to logic (bug 5607, r=psychonic).
     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 "DebugReporter.h"
    34 
    35 DebugReport g_DbgReporter;
    36 
    37 void DebugReport::OnSourceModAllInitialized()
    38 {
    39 	g_pSourcePawn->SetDebugListener(this);
    40 }
    41 
    42 void DebugReport::OnDebugSpew(const char *msg, ...)
    43 {
    44 	va_list ap;
    45 	char buffer[512];
    46 
    47 	va_start(ap, msg);
    48 	smcore.FormatArgs(buffer, sizeof(buffer), msg, ap);
    49 	va_end(ap);
    50 
    51 	smcore.Log("[SM] %s", buffer);
    52 }
    53 
    54 void DebugReport::GenerateError(IPluginContext *ctx, cell_t func_idx, int err, const char *message, ...)
    55 {
    56 	va_list ap;
    57 
    58 	va_start(ap, message);
    59 	GenerateErrorVA(ctx, func_idx, err, message, ap);
    60 	va_end(ap);
    61 }
    62 
    63 void DebugReport::GenerateErrorVA(IPluginContext *ctx, cell_t func_idx, int err, const char *message, va_list ap)
    64 {
    65 	char buffer[512];
    66 	smcore.FormatArgs(buffer, sizeof(buffer), message, ap);
    67 
    68 	const char *plname = pluginsys->FindPluginByContext(ctx->GetContext())->GetFilename();
    69 	const char *error = g_pSourcePawn2->GetErrorString(err);
    70 
    71 	if (error)
    72 	{
    73 		smcore.LogError("[SM] Plugin \"%s\" encountered error %d: %s", plname, err, error);
    74 	} else {
    75 		smcore.LogError("[SM] Plugin \"%s\" encountered unknown error %d", plname, err);
    76 	}
    77 
    78 	smcore.LogError("[SM] %s", buffer);
    79 
    80 	if (func_idx != -1)
    81 	{
    82 		if (func_idx & 1)
    83 		{
    84 			func_idx >>= 1;
    85 			sp_public_t *function;
    86 			if (ctx->GetRuntime()->GetPublicByIndex(func_idx, &function) == SP_ERROR_NONE)
    87 			{
    88 				smcore.LogError("[SM] Unable to call function \"%s\" due to above error(s).", function->name);
    89 			}
    90 		}
    91 	}
    92 }
    93 
    94 void DebugReport::GenerateCodeError(IPluginContext *pContext, uint32_t code_addr, int err, const char *message, ...)
    95 {
    96 	va_list ap;
    97 	char buffer[512];
    98 
    99 	va_start(ap, message);
   100 	smcore.FormatArgs(buffer, sizeof(buffer), message, ap);
   101 	va_end(ap);
   102 
   103 	const char *plname = pluginsys->FindPluginByContext(pContext->GetContext())->GetFilename();
   104 	const char *error = g_pSourcePawn2->GetErrorString(err);
   105 
   106 	if (error)
   107 	{
   108 		smcore.LogError("[SM] Plugin \"%s\" encountered error %d: %s", plname, err, error);
   109 	} else {
   110 		smcore.LogError("[SM] Plugin \"%s\" encountered unknown error %d", plname, err);
   111 	}
   112 
   113 	smcore.LogError("[SM] %s", buffer);
   114 
   115 	IPluginDebugInfo *pDebug;
   116 	if ((pDebug = pContext->GetRuntime()->GetDebugInfo()) == NULL)
   117 	{
   118 		smcore.LogError("[SM] Debug mode is not enabled for \"%s\"", plname);
   119 		smcore.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on",
   120 			_GetPluginIndex(pContext));
   121 		return;
   122 	}
   123 
   124 	const char *name;
   125 	if (pDebug->LookupFunction(code_addr, &name) == SP_ERROR_NONE)
   126 	{
   127 		smcore.LogError("[SM] Unable to call function \"%s\" due to above error(s).", name);
   128 	} else {
   129 		smcore.LogError("[SM] Unable to call function (name unknown, address \"%x\").", code_addr);
   130 	}
   131 }
   132 
   133 void DebugReport::OnContextExecuteError(IPluginContext *ctx, IContextTrace *error)
   134 {
   135 	const char *lastname;
   136 	const char *plname = pluginsys->FindPluginByContext(ctx->GetContext())->GetFilename();
   137 	int n_err = error->GetErrorCode();
   138 
   139 	if (n_err != SP_ERROR_NATIVE)
   140 	{
   141 		smcore.LogError("[SM] Plugin encountered error %d: %s",
   142 			n_err,
   143 			error->GetErrorString());
   144 	}
   145 
   146 	if ((lastname=error->GetLastNative(NULL)) != NULL)
   147 	{
   148 		const char *custerr;
   149 		if ((custerr=error->GetCustomErrorString()) != NULL)
   150 		{
   151 			smcore.LogError("[SM] Native \"%s\" reported: %s", lastname, custerr);
   152 		} else {
   153 			smcore.LogError("[SM] Native \"%s\" encountered a generic error.", lastname);
   154 		}
   155 	}
   156 
   157 	if (!error->DebugInfoAvailable())
   158 	{
   159 		smcore.LogError("[SM] Debug mode is not enabled for \"%s\"", plname);
   160 		smcore.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on",
   161 			_GetPluginIndex(ctx));
   162 		return;
   163 	}
   164 
   165 	CallStackInfo stk_info;
   166 	int i = 0;
   167 	smcore.LogError("[SM] Displaying call stack trace for plugin \"%s\":", plname);
   168 	while (error->GetTraceInfo(&stk_info))
   169 	{
   170 		smcore.LogError("[SM]   [%d]  Line %d, %s::%s()",
   171 			i++,
   172 			stk_info.line,
   173 			stk_info.filename,
   174 			stk_info.function);
   175 	}
   176 }
   177 
   178 int DebugReport::_GetPluginIndex(IPluginContext *ctx)
   179 {
   180 	int id = 1;
   181 	IPluginIterator *iter = pluginsys->GetPluginIterator();
   182 
   183 	for (; iter->MorePlugins(); iter->NextPlugin(), id++)
   184 	{
   185 		IPlugin *pl = iter->GetPlugin();
   186 		if (pl->GetBaseContext() == ctx)
   187 		{
   188 			iter->Release();
   189 			return id;
   190 		}
   191 	}
   192 
   193 	iter->Release();
   194 
   195 	/* If we don't know which plugin this is, it's one being loaded.  Fake its index for now. */
   196 
   197 	return pluginsys->GetPluginCount() + 1;
   198 }
   199