3 * =============================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #include "DebugReporter.h"
34 #include "PluginSys.h"
35 #include "sm_stringutil.h"
37 DebugReport g_DbgReporter;
39 /* I'm really lazy. This should probably be exported to ISourcePawnEngine someday,
40 * but we need to make sure the JIT will deal with the version bump.
42 extern const char *GetSourcePawnErrorMessage(int error);
44 void DebugReport::OnSourceModAllInitialized()
46 g_pSourcePawn->SetDebugListener(this);
49 void DebugReport::GenerateError(IPluginContext *ctx, cell_t func_idx, int err, const char *message, ...)
54 va_start(ap, message);
55 UTIL_FormatArgs(buffer, sizeof(buffer), message, ap);
58 const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename();
59 const char *error = GetSourcePawnErrorMessage(err);
63 g_Logger.LogError("[SM] Plugin \"%s\" encountered error %d: %s", plname, err, error);
65 g_Logger.LogError("[SM] Plugin \"%s\" encountered unknown error %d", plname, err);
68 g_Logger.LogError("[SM] %s", buffer);
75 sp_public_t *function;
76 if (ctx->GetPublicByIndex(func_idx, &function) == SP_ERROR_NONE)
78 g_Logger.LogError("[SM] Unable to call function \"%s\" due to above error(s).", function->name);
84 void DebugReport::GenerateCodeError(IPluginContext *pContext, uint32_t code_addr, int err, const char *message, ...)
89 va_start(ap, message);
90 UTIL_FormatArgs(buffer, sizeof(buffer), message, ap);
93 const char *plname = g_PluginSys.FindPluginByContext(pContext->GetContext())->GetFilename();
94 const char *error = GetSourcePawnErrorMessage(err);
98 g_Logger.LogError("[SM] Plugin \"%s\" encountered error %d: %s", plname, err, error);
100 g_Logger.LogError("[SM] Plugin \"%s\" encountered unknown error %d", plname, err);
103 g_Logger.LogError("[SM] %s", buffer);
105 IPluginDebugInfo *pDebug;
106 if ((pDebug = pContext->GetDebugInfo()) == NULL)
108 g_Logger.LogError("[SM] Debug mode is not enabled for \"%s\"", plname);
109 g_Logger.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on",
110 _GetPluginIndex(pContext));
115 if (pDebug->LookupFunction(code_addr, &name) == SP_ERROR_NONE)
117 g_Logger.LogError("[SM] Unable to call function \"%s\" due to above error(s).", name);
119 g_Logger.LogError("[SM] Unable to call function (name unknown, address \"%x\").", code_addr);
123 void DebugReport::OnContextExecuteError(IPluginContext *ctx, IContextTrace *error)
125 const char *lastname;
126 const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename();
127 int n_err = error->GetErrorCode();
129 if (n_err != SP_ERROR_NATIVE)
131 g_Logger.LogError("[SM] Plugin encountered error %d: %s",
133 error->GetErrorString());
136 if ((lastname=error->GetLastNative(NULL)) != NULL)
139 if ((custerr=error->GetCustomErrorString()) != NULL)
141 g_Logger.LogError("[SM] Native \"%s\" reported: %s", lastname, custerr);
143 g_Logger.LogError("[SM] Native \"%s\" encountered a generic error.", lastname);
147 if (!error->DebugInfoAvailable())
149 g_Logger.LogError("[SM] Debug mode is not enabled for \"%s\"", plname);
150 g_Logger.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on",
151 _GetPluginIndex(ctx));
155 CallStackInfo stk_info;
157 g_Logger.LogError("[SM] Displaying call stack trace for plugin \"%s\":", plname);
158 while (error->GetTraceInfo(&stk_info))
160 g_Logger.LogError("[SM] [%d] Line %d, %s::%s()",
168 int DebugReport::_GetPluginIndex(IPluginContext *ctx)
171 IPluginIterator *iter = g_PluginSys.GetPluginIterator();
173 for (; iter->MorePlugins(); iter->NextPlugin(), id++)
175 IPlugin *pl = iter->GetPlugin();
176 if (pl->GetBaseContext() == ctx)