ds@1952: /** ds@1952: * vim: set ts=4 : ds@1952: * ============================================================================= ds@1952: * SourceMod ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. ds@1952: * ============================================================================= ds@1952: * ds@1952: * This program is free software; you can redistribute it and/or modify it under ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the ds@1952: * Free Software Foundation. ds@1952: * ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more ds@1952: * details. ds@1952: * ds@1952: * You should have received a copy of the GNU General Public License along with ds@1952: * this program. If not, see . ds@1952: * ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants ds@1952: * this exception to all derivative works. AlliedModders LLC defines further ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), ds@1952: * or . ds@1952: * ds@1952: * Version: $Id$ ds@1952: */ ds@1952: ds@1952: #include "DebugReporter.h" ds@1952: #include "Logger.h" ds@1952: #include "PluginSys.h" ds@1952: #include "sm_stringutil.h" ds@1952: ds@1952: DebugReport g_DbgReporter; ds@1952: ds@1952: void DebugReport::OnSourceModAllInitialized() ds@1952: { ds@1952: g_pSourcePawn->SetDebugListener(this); ds@1952: } ds@1952: dvander@2226: void DebugReport::OnDebugSpew(const char *msg, ...) dvander@2226: { dvander@2226: va_list ap; dvander@2226: char buffer[512]; dvander@2226: dvander@2226: va_start(ap, msg); dvander@2226: UTIL_FormatArgs(buffer, sizeof(buffer), msg, ap); dvander@2226: va_end(ap); dvander@2226: dvander@2226: g_Logger.LogMessage("[SM] %s", buffer); dvander@2226: } dvander@2226: ds@1952: void DebugReport::GenerateError(IPluginContext *ctx, cell_t func_idx, int err, const char *message, ...) ds@1952: { ds@1952: va_list ap; ds@1952: char buffer[512]; ds@1952: ds@1952: va_start(ap, message); ds@1952: UTIL_FormatArgs(buffer, sizeof(buffer), message, ap); ds@1952: va_end(ap); ds@1952: ds@1952: const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename(); dvander@2193: const char *error = g_pSourcePawn2->GetErrorString(err); ds@1952: ds@1952: if (error) ds@1952: { ds@1952: g_Logger.LogError("[SM] Plugin \"%s\" encountered error %d: %s", plname, err, error); ds@1952: } else { ds@1952: g_Logger.LogError("[SM] Plugin \"%s\" encountered unknown error %d", plname, err); ds@1952: } ds@1952: ds@1952: g_Logger.LogError("[SM] %s", buffer); ds@1952: ds@1952: if (func_idx != -1) ds@1952: { ds@1952: if (func_idx & 1) ds@1952: { ds@1952: func_idx >>= 1; ds@1952: sp_public_t *function; dvander@2195: if (ctx->GetRuntime()->GetPublicByIndex(func_idx, &function) == SP_ERROR_NONE) ds@1952: { ds@1952: g_Logger.LogError("[SM] Unable to call function \"%s\" due to above error(s).", function->name); ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: void DebugReport::GenerateCodeError(IPluginContext *pContext, uint32_t code_addr, int err, const char *message, ...) ds@1952: { ds@1952: va_list ap; ds@1952: char buffer[512]; ds@1952: ds@1952: va_start(ap, message); ds@1952: UTIL_FormatArgs(buffer, sizeof(buffer), message, ap); ds@1952: va_end(ap); ds@1952: ds@1952: const char *plname = g_PluginSys.FindPluginByContext(pContext->GetContext())->GetFilename(); dvander@2193: const char *error = g_pSourcePawn2->GetErrorString(err); ds@1952: ds@1952: if (error) ds@1952: { ds@1952: g_Logger.LogError("[SM] Plugin \"%s\" encountered error %d: %s", plname, err, error); ds@1952: } else { ds@1952: g_Logger.LogError("[SM] Plugin \"%s\" encountered unknown error %d", plname, err); ds@1952: } ds@1952: ds@1952: g_Logger.LogError("[SM] %s", buffer); ds@1952: ds@1952: IPluginDebugInfo *pDebug; dvander@2195: if ((pDebug = pContext->GetRuntime()->GetDebugInfo()) == NULL) ds@1952: { ds@1952: g_Logger.LogError("[SM] Debug mode is not enabled for \"%s\"", plname); ds@1952: g_Logger.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on", ds@1952: _GetPluginIndex(pContext)); ds@1952: return; ds@1952: } ds@1952: ds@1952: const char *name; ds@1952: if (pDebug->LookupFunction(code_addr, &name) == SP_ERROR_NONE) ds@1952: { ds@1952: g_Logger.LogError("[SM] Unable to call function \"%s\" due to above error(s).", name); ds@1952: } else { ds@1952: g_Logger.LogError("[SM] Unable to call function (name unknown, address \"%x\").", code_addr); ds@1952: } ds@1952: } ds@1952: ds@1952: void DebugReport::OnContextExecuteError(IPluginContext *ctx, IContextTrace *error) ds@1952: { ds@1952: const char *lastname; ds@1952: const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename(); ds@1952: int n_err = error->GetErrorCode(); ds@1952: ds@1952: if (n_err != SP_ERROR_NATIVE) ds@1952: { ds@1952: g_Logger.LogError("[SM] Plugin encountered error %d: %s", ds@1952: n_err, ds@1952: error->GetErrorString()); ds@1952: } ds@1952: ds@1952: if ((lastname=error->GetLastNative(NULL)) != NULL) ds@1952: { ds@1952: const char *custerr; ds@1952: if ((custerr=error->GetCustomErrorString()) != NULL) ds@1952: { ds@1952: g_Logger.LogError("[SM] Native \"%s\" reported: %s", lastname, custerr); ds@1952: } else { ds@1952: g_Logger.LogError("[SM] Native \"%s\" encountered a generic error.", lastname); ds@1952: } ds@1952: } ds@1952: ds@1952: if (!error->DebugInfoAvailable()) ds@1952: { ds@1952: g_Logger.LogError("[SM] Debug mode is not enabled for \"%s\"", plname); ds@1952: g_Logger.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on", ds@1952: _GetPluginIndex(ctx)); ds@1952: return; ds@1952: } ds@1952: ds@1952: CallStackInfo stk_info; ds@1952: int i = 0; ds@1952: g_Logger.LogError("[SM] Displaying call stack trace for plugin \"%s\":", plname); ds@1952: while (error->GetTraceInfo(&stk_info)) ds@1952: { ds@1952: g_Logger.LogError("[SM] [%d] Line %d, %s::%s()", ds@1952: i++, ds@1952: stk_info.line, ds@1952: stk_info.filename, ds@1952: stk_info.function); ds@1952: } ds@1952: } ds@1952: ds@1952: int DebugReport::_GetPluginIndex(IPluginContext *ctx) ds@1952: { ds@1952: int id = 1; ds@1952: IPluginIterator *iter = g_PluginSys.GetPluginIterator(); ds@1952: ds@1952: for (; iter->MorePlugins(); iter->NextPlugin(), id++) ds@1952: { ds@1952: IPlugin *pl = iter->GetPlugin(); ds@1952: if (pl->GetBaseContext() == ctx) ds@1952: { ds@1952: iter->Release(); ds@1952: return id; ds@1952: } ds@1952: } ds@1952: ds@1952: iter->Release(); dvander@2061: dvander@2061: /* If we don't know which plugin this is, it's one being loaded. Fake its index for now. */ dvander@2061: dvander@2061: return g_PluginSys.GetPluginCount() + 1; ds@1952: } ds@1952: