2006-04-12 13:01:15 +00:00
|
|
|
#if defined BINLOG_ENABLED
|
|
|
|
|
2006-03-15 13:43:06 +00:00
|
|
|
#include <time.h>
|
2006-03-14 19:36:18 +00:00
|
|
|
#include "amxmodx.h"
|
|
|
|
#include "binlog.h"
|
2006-08-19 18:44:12 +00:00
|
|
|
#include "debugger.h"
|
2006-03-14 19:36:18 +00:00
|
|
|
|
|
|
|
BinLog g_BinLog;
|
2006-03-16 06:36:01 +00:00
|
|
|
int g_binlog_level = 0;
|
2006-03-17 22:50:13 +00:00
|
|
|
int g_binlog_maxsize = 0;
|
2006-03-14 19:36:18 +00:00
|
|
|
|
2006-08-19 18:44:12 +00:00
|
|
|
// Helper function to get a filename index
|
|
|
|
#define USHR(x) ((unsigned int)(x)>>1)
|
|
|
|
int LookupFile(AMX_DBG *amxdbg, ucell address)
|
|
|
|
{
|
|
|
|
int high, low, mid;
|
|
|
|
|
|
|
|
high = amxdbg->hdr->files;
|
|
|
|
low = -1;
|
|
|
|
|
|
|
|
while (high - low > 1)
|
|
|
|
{
|
|
|
|
mid = USHR(low + high);
|
2006-08-19 20:21:00 +00:00
|
|
|
if (amxdbg->filetbl[mid]->address <= address)
|
2006-08-19 18:44:12 +00:00
|
|
|
{
|
|
|
|
low = mid;
|
|
|
|
} else {
|
|
|
|
high = mid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (low == -1)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return low;
|
|
|
|
}
|
|
|
|
|
2006-03-14 19:36:18 +00:00
|
|
|
bool BinLog::Open()
|
|
|
|
{
|
|
|
|
const char *data = get_localinfo("amxmodx_datadir", "addons/amxmodx/data");
|
|
|
|
char path[255];
|
|
|
|
build_pathname_r(path, sizeof(path)-1, "%s/binlogs", data);
|
|
|
|
|
|
|
|
if (!DirExists(path))
|
|
|
|
{
|
|
|
|
mkdir(path
|
|
|
|
#if defined __linux__
|
|
|
|
, 0755
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
if (!DirExists(path))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char file[255];
|
|
|
|
build_pathname_r(file, sizeof(file)-1, "%s/binlogs/lastlog", data);
|
|
|
|
|
|
|
|
unsigned int lastcntr = 0;
|
|
|
|
FILE *lastlog = fopen(file, "rb");
|
|
|
|
if (lastlog)
|
|
|
|
{
|
|
|
|
if (fread(&lastcntr, sizeof(int), 1, lastlog) != 1)
|
|
|
|
lastcntr = 0;
|
|
|
|
fclose(lastlog);
|
|
|
|
}
|
|
|
|
lastlog = fopen(file, "wb");
|
|
|
|
if (lastlog)
|
|
|
|
{
|
|
|
|
lastcntr++;
|
|
|
|
fwrite(&lastcntr, sizeof(int), 1, lastlog);
|
|
|
|
fclose(lastlog);
|
|
|
|
}
|
|
|
|
build_pathname_r(file, sizeof(file)-1, "%s/binlogs/binlog%04d.blg", data, lastcntr);
|
|
|
|
m_logfile.assign(file);
|
2006-03-19 21:41:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* it's now safe to create the binary log
|
|
|
|
*/
|
|
|
|
FILE *fp = fopen(m_logfile.c_str(), "wb");
|
|
|
|
if (!fp)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int magic = BINLOG_MAGIC;
|
|
|
|
short vers = BINLOG_VERSION;
|
|
|
|
char c = sizeof(time_t);
|
|
|
|
fwrite(&magic, sizeof(int), 1, fp);
|
|
|
|
fwrite(&vers, sizeof(short), 1, fp);
|
|
|
|
fwrite(&c, sizeof(char), 1, fp);
|
|
|
|
|
|
|
|
WritePluginDB(fp);
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
m_state = true;
|
|
|
|
|
|
|
|
WriteOp(BinLog_Start, -1);
|
2006-03-14 19:36:18 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BinLog::Close()
|
|
|
|
{
|
2006-03-16 06:36:01 +00:00
|
|
|
WriteOp(BinLog_End, -1);
|
2006-03-19 21:41:11 +00:00
|
|
|
m_state = false;
|
2006-03-14 19:36:18 +00:00
|
|
|
}
|
|
|
|
|
2006-03-15 13:43:06 +00:00
|
|
|
void BinLog::WriteOp(BinLogOp op, int plug, ...)
|
|
|
|
{
|
2006-03-19 21:41:11 +00:00
|
|
|
if (!m_state)
|
|
|
|
return;
|
|
|
|
|
2006-03-15 13:43:06 +00:00
|
|
|
FILE *fp = fopen(m_logfile.c_str(), "ab");
|
|
|
|
if (!fp)
|
|
|
|
return;
|
|
|
|
|
2006-03-24 19:29:49 +00:00
|
|
|
if (g_binlog_maxsize && op != BinLog_End)
|
2006-03-17 22:50:13 +00:00
|
|
|
{
|
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
|
if (ftell(fp) > (g_binlog_maxsize * (1024 * 1024)))
|
|
|
|
{
|
|
|
|
fclose(fp);
|
|
|
|
Close();
|
|
|
|
Open();
|
|
|
|
fp = fopen(m_logfile.c_str(), "ab");
|
|
|
|
if (!fp)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-15 13:43:06 +00:00
|
|
|
unsigned char c = static_cast<char>(op);
|
|
|
|
time_t t = time(NULL);
|
|
|
|
float gt = gpGlobals->time;
|
|
|
|
fwrite(&c, sizeof(char), 1, fp);
|
|
|
|
fwrite(&t, sizeof(time_t), 1, fp);
|
|
|
|
fwrite(>, sizeof(float), 1, fp);
|
|
|
|
fwrite(&plug, sizeof(int), 1, fp);
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, plug);
|
|
|
|
|
2006-08-19 18:44:12 +00:00
|
|
|
AMX *amx;
|
|
|
|
bool debug = false;
|
|
|
|
AMX_DBG *dbg = NULL;
|
|
|
|
CPluginMngr::CPlugin *pl = NULL;
|
|
|
|
|
|
|
|
if (plug != -1)
|
|
|
|
{
|
|
|
|
pl = g_plugins.findPlugin(plug);
|
|
|
|
if ((debug = pl->isDebug()))
|
|
|
|
{
|
|
|
|
amx = pl->getAMX();
|
|
|
|
dbg = static_cast<Debugger *>(amx->userdata[UD_DEBUGGER])->m_pAmxDbg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-15 13:43:06 +00:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case BinLog_Registered:
|
|
|
|
{
|
|
|
|
const char *title = va_arg(ap, const char *);
|
|
|
|
const char *vers = va_arg(ap, const char *);
|
|
|
|
c = (char)strlen(title);
|
|
|
|
fwrite(&c, sizeof(char), 1, fp);
|
|
|
|
fwrite(title, sizeof(char), c+1, fp);
|
|
|
|
c = (char)strlen(vers);
|
|
|
|
fwrite(&c, sizeof(char), 1 ,fp);
|
|
|
|
fwrite(vers, sizeof(char), c+1, fp);
|
|
|
|
break;
|
|
|
|
}
|
2006-03-16 06:36:01 +00:00
|
|
|
case BinLog_NativeCall:
|
|
|
|
{
|
2006-08-19 18:44:12 +00:00
|
|
|
int file;
|
2006-03-16 06:36:01 +00:00
|
|
|
int native = va_arg(ap, int);
|
|
|
|
int params = va_arg(ap, int);
|
|
|
|
fwrite(&native, sizeof(int), 1, fp);
|
|
|
|
fwrite(¶ms, sizeof(int), 1, fp);
|
2006-08-19 18:44:12 +00:00
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
file = LookupFile(dbg, amx->cip);
|
|
|
|
fwrite(&file, sizeof(int), 1, fp);
|
|
|
|
} else {
|
|
|
|
file = 0;
|
|
|
|
fwrite(&file, sizeof(int), 1, fp);
|
|
|
|
}
|
2006-03-16 06:36:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BinLog_NativeRet:
|
|
|
|
{
|
|
|
|
cell retval = va_arg(ap, cell);
|
|
|
|
fwrite(&retval, sizeof(cell), 1, fp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BinLog_NativeError:
|
|
|
|
{
|
|
|
|
int err = va_arg(ap, int);
|
|
|
|
const char *msg = va_arg(ap, const char *);
|
|
|
|
short len = (short)strlen(msg);
|
|
|
|
fwrite(&err, sizeof(int), 1, fp);
|
|
|
|
fwrite(&len, sizeof(short), 1, fp);
|
|
|
|
fwrite(msg, sizeof(char), len+1, fp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BinLog_CallPubFunc:
|
|
|
|
{
|
2006-08-19 18:44:12 +00:00
|
|
|
int file;
|
2006-03-16 06:36:01 +00:00
|
|
|
int num = va_arg(ap, int);
|
|
|
|
fwrite(&num, sizeof(int), 1, fp);
|
2006-08-19 18:44:12 +00:00
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
file = LookupFile(dbg, amx->cip);
|
|
|
|
fwrite(&file, sizeof(int), 1, fp);
|
|
|
|
} else {
|
|
|
|
file = 0;
|
|
|
|
fwrite(&file, sizeof(int), 1, fp);
|
|
|
|
}
|
2006-03-16 06:36:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BinLog_SetLine:
|
|
|
|
{
|
2006-08-21 03:46:20 +00:00
|
|
|
int file;
|
2006-03-16 06:36:01 +00:00
|
|
|
int line = va_arg(ap, int);
|
|
|
|
fwrite(&line, sizeof(int), 1, fp);
|
2006-08-21 03:46:20 +00:00
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
file = LookupFile(dbg, amx->cip);
|
|
|
|
fwrite(&file, sizeof(int), 1, fp);
|
|
|
|
} else {
|
|
|
|
file = 0;
|
|
|
|
fwrite(&file, sizeof(int), 1, fp);
|
|
|
|
}
|
2006-03-16 06:36:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BinLog_FormatString:
|
|
|
|
{
|
|
|
|
int param = va_arg(ap, int);
|
|
|
|
int maxlen = va_arg(ap, int);
|
|
|
|
const char *str = va_arg(ap, const char *);
|
|
|
|
short len = (short)strlen(str);
|
|
|
|
fwrite(¶m, sizeof(int), 1, fp);
|
|
|
|
fwrite(&maxlen, sizeof(int), 1, fp);
|
|
|
|
fwrite(&len, sizeof(short), 1, fp);
|
|
|
|
fwrite(str, sizeof(char), len+1, fp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BinLog_NativeParams:
|
|
|
|
{
|
|
|
|
cell *params = va_arg(ap, cell *);
|
|
|
|
cell num = params[0] / sizeof(cell);
|
|
|
|
fwrite(&num, sizeof(cell), 1, fp);
|
|
|
|
for (cell i=1; i<=num; i++)
|
|
|
|
fwrite(&(params[i]), sizeof(cell), 1, fp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BinLog_GetString:
|
|
|
|
{
|
|
|
|
cell addr = va_arg(ap, cell);
|
|
|
|
const char *str = va_arg(ap, const char *);
|
|
|
|
short len = (short)strlen(str);
|
|
|
|
fwrite(&addr, sizeof(cell), 1, fp);
|
|
|
|
fwrite(&len, sizeof(short), 1, fp);
|
|
|
|
fwrite(str, sizeof(char), len+1, fp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BinLog_SetString:
|
|
|
|
{
|
|
|
|
cell addr = va_arg(ap, cell);
|
|
|
|
int maxlen = va_arg(ap, int);
|
|
|
|
const char *str = va_arg(ap, const char *);
|
|
|
|
short len = (short)strlen(str);
|
|
|
|
fwrite(&addr, sizeof(cell), 1, fp);
|
|
|
|
fwrite(&maxlen, sizeof(int), 1, fp);
|
|
|
|
fwrite(&len, sizeof(short), 1, fp);
|
|
|
|
fwrite(str, sizeof(char), len+1, fp);
|
|
|
|
break;
|
|
|
|
}
|
2006-03-15 13:43:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
va_end(ap);
|
2006-03-16 06:36:01 +00:00
|
|
|
fclose(fp);
|
2006-03-15 13:43:06 +00:00
|
|
|
}
|
|
|
|
|
2006-03-19 21:41:11 +00:00
|
|
|
void BinLog::WritePluginDB(FILE *fp)
|
2006-03-14 19:36:18 +00:00
|
|
|
{
|
|
|
|
int num = g_plugins.getPluginsNum();
|
|
|
|
fwrite(&num, sizeof(int), 1, fp);
|
|
|
|
|
|
|
|
CPluginMngr::CPlugin *pl;
|
|
|
|
char c;
|
|
|
|
unsigned char len;
|
|
|
|
for (CPluginMngr::iterator iter = g_plugins.begin(); iter; ++iter)
|
|
|
|
{
|
|
|
|
pl = &(*iter);
|
|
|
|
if (pl->isValid())
|
|
|
|
c = 1;
|
|
|
|
else
|
|
|
|
c = 0;
|
2006-03-15 13:43:06 +00:00
|
|
|
if (c && pl->isDebug())
|
|
|
|
c = 2;
|
2006-03-14 19:36:18 +00:00
|
|
|
fwrite(&c, sizeof(char), 1, fp);
|
2006-06-28 10:07:20 +00:00
|
|
|
if (c)
|
2006-03-14 19:36:18 +00:00
|
|
|
{
|
2006-08-19 18:44:12 +00:00
|
|
|
Debugger *pd = NULL;
|
2006-06-28 10:07:20 +00:00
|
|
|
len = (char)strlen(pl->getName());
|
2006-03-14 19:36:18 +00:00
|
|
|
fwrite(&len, sizeof(char), 1, fp);
|
|
|
|
len++;
|
2006-06-28 10:07:20 +00:00
|
|
|
fwrite(pl->getName(), sizeof(char), len, fp);
|
2006-08-19 18:44:12 +00:00
|
|
|
int natives, publics, files;
|
2006-06-28 10:07:20 +00:00
|
|
|
AMX *amx = pl->getAMX();
|
2006-08-19 18:44:12 +00:00
|
|
|
// Write the number of Filenames
|
|
|
|
if (c == 2)
|
|
|
|
{
|
|
|
|
pd = static_cast<Debugger *>(amx->userdata[UD_DEBUGGER]);
|
|
|
|
files = pd->m_pAmxDbg->hdr->files;
|
|
|
|
fwrite(&files, sizeof(int), 1, fp);
|
|
|
|
}
|
2006-06-28 10:07:20 +00:00
|
|
|
amx_NumNatives(amx, &natives);
|
|
|
|
amx_NumPublics(amx, &publics);
|
|
|
|
fwrite(&natives, sizeof(int), 1, fp);
|
|
|
|
fwrite(&publics, sizeof(int), 1, fp);
|
|
|
|
char name[34];
|
2006-08-19 18:44:12 +00:00
|
|
|
// Write the Filenames to the binfile
|
|
|
|
if (c == 2)
|
|
|
|
{
|
|
|
|
AMX_DBG_FILE **ftable = pd->m_pAmxDbg->filetbl;
|
|
|
|
for (int i=0; i<files; i++)
|
|
|
|
{
|
|
|
|
len = (char)strlen(ftable[i]->name);
|
|
|
|
fwrite(&len, sizeof(char), 1, fp);
|
|
|
|
len++;
|
|
|
|
fwrite(ftable[i]->name, sizeof(char), len, fp);
|
|
|
|
}
|
|
|
|
}
|
2006-06-28 10:07:20 +00:00
|
|
|
for (int i=0; i<natives; i++)
|
|
|
|
{
|
|
|
|
amx_GetNative(amx, i, name);
|
|
|
|
len = (char)strlen(name);
|
|
|
|
fwrite(&len, sizeof(char), 1, fp);
|
|
|
|
len++;
|
|
|
|
fwrite(name, sizeof(char), len, fp);
|
|
|
|
}
|
|
|
|
for (int i=0; i<publics; i++)
|
|
|
|
{
|
|
|
|
amx_GetPublic(amx, i, name);
|
|
|
|
len = (char)strlen(name);
|
|
|
|
fwrite(&len, sizeof(char), 1, fp);
|
|
|
|
len++;
|
|
|
|
fwrite(name, sizeof(char), len, fp);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
char empty[] = " ";
|
|
|
|
len = 1;
|
2006-03-14 19:36:18 +00:00
|
|
|
fwrite(&len, sizeof(char), 1, fp);
|
2006-06-28 10:07:20 +00:00
|
|
|
fwrite(empty, sizeof(char), len, fp);
|
|
|
|
int no = 0;
|
|
|
|
fwrite(&no, sizeof(int), 1, fp);
|
|
|
|
fwrite(&no, sizeof(int), 1, fp);
|
2006-03-14 19:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //BINLOG_ENABLED
|