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