trace_plugin/trace_api.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Sep 26 17:00:32 2013 -0500 (2013-09-26)
changeset 284 ca90ccdb8d49
parent 0 d15304fcb7a7
permissions -rw-r--r--
Fixed makefile error when issuing 'make clean' or 'make distclean'.
darope@0
     1
// vi: set ts=4 sw=4 :
darope@0
     2
// vim: set tw=75 :
darope@0
     3
darope@0
     4
// trace_api.cpp - (main) implementation of HL API tracing operations
darope@0
     5
darope@0
     6
/*
darope@0
     7
 * Copyright (c) 2001-2003 Will Day <[email protected]>
darope@0
     8
 *
darope@0
     9
 *    This file is part of Metamod.
darope@0
    10
 *
darope@0
    11
 *    Metamod is free software; you can redistribute it and/or modify it
darope@0
    12
 *    under the terms of the GNU General Public License as published by the
darope@0
    13
 *    Free Software Foundation; either version 2 of the License, or (at
darope@0
    14
 *    your option) any later version.
darope@0
    15
 *
darope@0
    16
 *    Metamod is distributed in the hope that it will be useful, but
darope@0
    17
 *    WITHOUT ANY WARRANTY; without even the implied warranty of
darope@0
    18
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
darope@0
    19
 *    General Public License for more details.
darope@0
    20
 *
darope@0
    21
 *    You should have received a copy of the GNU General Public License
darope@0
    22
 *    along with Metamod; if not, write to the Free Software Foundation,
darope@0
    23
 *    Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
darope@0
    24
 *
darope@0
    25
 *    In addition, as a special exception, the author gives permission to
darope@0
    26
 *    link the code of this program with the Half-Life Game Engine ("HL
darope@0
    27
 *    Engine") and Modified Game Libraries ("MODs") developed by Valve,
darope@0
    28
 *    L.L.C ("Valve").  You must obey the GNU General Public License in all
darope@0
    29
 *    respects for all of the code used other than the HL Engine and MODs
darope@0
    30
 *    from Valve.  If you modify this file, you may extend this exception
darope@0
    31
 *    to your version of the file, but you are not obligated to do so.  If
darope@0
    32
 *    you do not wish to do so, delete this exception statement from your
darope@0
    33
 *    version.
darope@0
    34
 *
darope@0
    35
 */
darope@0
    36
darope@0
    37
#include <extdll.h>			// always
darope@0
    38
darope@0
    39
#include <api_info.h>		// api_info_t, etc
darope@0
    40
#include <sdk_util.h>		// REG_SVR_COMMAND, etc
darope@0
    41
#include <meta_api.h>		// Plugin_info, etc
darope@0
    42
darope@0
    43
#include "trace_api.h"		// me
darope@0
    44
#include "log_plugin.h"		// LOG_MSG, etc
darope@0
    45
#include "vers_meta.h"		// OPT_TYPE
darope@0
    46
#include "vdate.h"			// COMPILE_TIME
darope@0
    47
darope@0
    48
cvar_t init_dllapi_trace = {"trace_dllapi", "0", FCVAR_EXTDLL, 0, NULL};
darope@0
    49
cvar_t init_newapi_trace = {"trace_newapi", "0", FCVAR_EXTDLL, 0, NULL};
darope@0
    50
cvar_t init_engine_trace = {"trace_engine", "0", FCVAR_EXTDLL, 0, NULL};
darope@0
    51
cvar_t init_unlimit_trace =  {"trace_unlimit", "0", FCVAR_EXTDLL, 0, NULL};
darope@0
    52
darope@0
    53
cvar_t *dllapi_trace = NULL;
darope@0
    54
cvar_t *newapi_trace = NULL;
darope@0
    55
cvar_t *engine_trace = NULL;
darope@0
    56
cvar_t *unlimit_trace = NULL;
darope@0
    57
darope@0
    58
const char *msg_dest_types[32];
darope@0
    59
darope@0
    60
time_t last_trace_log = 0;
darope@0
    61
darope@0
    62
// Plugin startup.  Register commands and cvars.
darope@0
    63
void trace_init(void) {
darope@0
    64
	CVAR_REGISTER(&init_dllapi_trace);
darope@0
    65
	CVAR_REGISTER(&init_newapi_trace);
darope@0
    66
	CVAR_REGISTER(&init_engine_trace);
darope@0
    67
	CVAR_REGISTER(&init_unlimit_trace);
darope@0
    68
darope@0
    69
	dllapi_trace=CVAR_GET_POINTER("trace_dllapi");
darope@0
    70
	newapi_trace=CVAR_GET_POINTER("trace_newapi");
darope@0
    71
	engine_trace=CVAR_GET_POINTER("trace_engine");
darope@0
    72
	unlimit_trace=CVAR_GET_POINTER("trace_unlimit");
darope@0
    73
darope@0
    74
	REG_SVR_COMMAND("trace", svr_trace);
darope@0
    75
darope@0
    76
	memset(msg_dest_types, 0, sizeof(msg_dest_types));
darope@0
    77
darope@0
    78
	msg_dest_types[MSG_BROADCAST]="all_unreliable";
darope@0
    79
	msg_dest_types[MSG_ONE]="one_reliable";
darope@0
    80
	msg_dest_types[MSG_ALL]="all_reliable";
darope@0
    81
	msg_dest_types[MSG_INIT]="init";
darope@0
    82
	msg_dest_types[MSG_PVS]="pvs_unreliable";
darope@0
    83
	msg_dest_types[MSG_PAS]="pas_unreliable";
darope@0
    84
	msg_dest_types[MSG_PVS_R]="pvs_reliable";
darope@0
    85
	msg_dest_types[MSG_PAS_R]="pas_reliable";
darope@0
    86
	msg_dest_types[MSG_ONE_UNRELIABLE]="one_unreliable";
darope@0
    87
}
darope@0
    88
darope@0
    89
// Parse "trace" console command.
darope@0
    90
void svr_trace(void) {
darope@0
    91
	const char *cmd;
darope@0
    92
	cmd=CMD_ARGV(1);
darope@0
    93
	if(!strcasecmp(cmd, "version"))
darope@0
    94
		cmd_trace_version();
darope@0
    95
	else if(!strcasecmp(cmd, "show"))
darope@0
    96
		cmd_trace_show();
darope@0
    97
	else if(!strcasecmp(cmd, "set"))
darope@0
    98
		cmd_trace_set();
darope@0
    99
	else if(!strcasecmp(cmd, "unset"))
darope@0
   100
		cmd_trace_unset();
darope@0
   101
	else if(!strcasecmp(cmd, "list"))
darope@0
   102
		cmd_trace_list();
darope@0
   103
	else {
darope@0
   104
		LOG_CONSOLE(PLID, "Unrecognized trace command: %s", cmd);
darope@0
   105
		cmd_trace_usage();
darope@0
   106
		return;
darope@0
   107
	}
darope@0
   108
}
darope@0
   109
darope@0
   110
// Print usage for "trace" console command.
darope@0
   111
void cmd_trace_usage(void) {
darope@0
   112
	LOG_CONSOLE(PLID, "usage: trace <command> [<arguments>]");
darope@0
   113
	LOG_CONSOLE(PLID, "valid commands are:");
darope@0
   114
	LOG_CONSOLE(PLID, "   version          - display plugin version info");
darope@0
   115
	LOG_CONSOLE(PLID, "   show             - show currently traced api routines");
darope@0
   116
	LOG_CONSOLE(PLID, "   set <routine>    - set tracing for given routine");
darope@0
   117
	LOG_CONSOLE(PLID, "   unset <routine>  - unset tracing for given routine");
darope@0
   118
	LOG_CONSOLE(PLID, "   list dllapi      - list all dllapi routines available for tracing");
darope@0
   119
	LOG_CONSOLE(PLID, "   list newapi      - list all newapi routines available for tracing");
darope@0
   120
	LOG_CONSOLE(PLID, "   list engine      - list all engine routines available for tracing");
darope@0
   121
	LOG_CONSOLE(PLID, "   list all         - list dllapi, neapi, and engine");
darope@0
   122
}
darope@0
   123
darope@0
   124
// "trace version" console command.
darope@0
   125
void cmd_trace_version(void) {
darope@0
   126
	LOG_CONSOLE(PLID, "%s v%s, %s", Plugin_info.name, Plugin_info.version, Plugin_info.date);
darope@0
   127
	LOG_CONSOLE(PLID, "by %s", Plugin_info.author);
darope@0
   128
	LOG_CONSOLE(PLID, "   %s", Plugin_info.url);
darope@0
   129
	LOG_CONSOLE(PLID, "compiled: %s Eastern (%s)", COMPILE_TIME, OPT_TYPE);
darope@0
   130
}
darope@0
   131
darope@0
   132
// "trace set" console command.
darope@0
   133
void cmd_trace_set(void) {
darope@0
   134
	int i, argc;
darope@0
   135
	const char *arg;
darope@0
   136
	const char *api;
darope@0
   137
	TRACE_RESULT ret;
darope@0
   138
	argc=CMD_ARGC();
darope@0
   139
	if(argc < 3) {
darope@0
   140
		LOG_CONSOLE(PLID, "usage: trace set <routine> [<routine> ...]");
darope@0
   141
		return;
darope@0
   142
	}
darope@0
   143
	for(i=2; i < argc; i++) {
darope@0
   144
		arg=CMD_ARGV(i);
darope@0
   145
		ret=trace_setflag(&arg, mTRUE, &api);
darope@0
   146
		if(ret==TR_SUCCESS)
darope@0
   147
			LOG_MESSAGE(PLID, "Tracing %s routine '%s'", api, arg);
darope@0
   148
		else if(ret==TR_ALREADY)
darope@0
   149
			LOG_CONSOLE(PLID, "Already tracing %s routine '%s'", api, arg);
darope@0
   150
		else
darope@0
   151
			LOG_CONSOLE(PLID, "Unrecognized API routine '%s'", arg);
darope@0
   152
	}
darope@0
   153
}
darope@0
   154
darope@0
   155
// "trace unset" console command.
darope@0
   156
void cmd_trace_unset(void) {
darope@0
   157
	int i, argc;
darope@0
   158
	const char *arg;
darope@0
   159
	const char *api;
darope@0
   160
	TRACE_RESULT ret;
darope@0
   161
	argc=CMD_ARGC();
darope@0
   162
	if(argc < 3) {
darope@0
   163
		LOG_CONSOLE(PLID, "usage: trace unset <routine>");
darope@0
   164
		return;
darope@0
   165
	}
darope@0
   166
	for(i=1; i < argc; i++) {
darope@0
   167
		arg=CMD_ARGV(i);
darope@0
   168
		ret=trace_setflag(&arg, mFALSE, &api);
darope@0
   169
		if(ret==TR_SUCCESS)
darope@0
   170
			LOG_MESSAGE(PLID, "Un-Tracing %s routine '%s'", api, arg);
darope@0
   171
		else if(ret==TR_ALREADY)
darope@0
   172
			LOG_CONSOLE(PLID, "Already not tracing %s routine '%s'", api, arg);
darope@0
   173
		else
darope@0
   174
			LOG_CONSOLE(PLID, "Unrecognized API routine '%s'", arg);
darope@0
   175
	}
darope@0
   176
}
darope@0
   177
darope@0
   178
// "trace show" console command.
darope@0
   179
void cmd_trace_show(void) {
darope@0
   180
	api_info_t *routine;
darope@0
   181
	int n=0;
darope@0
   182
	LOG_CONSOLE(PLID, "Tracing routines:");
darope@0
   183
	for(routine=(api_info_t *) &dllapi_info; routine->name; routine++) {
darope@0
   184
		if(routine->trace==mTRUE) {
darope@0
   185
			LOG_CONSOLE(PLID, "   %s (dllapi)", routine->name);
darope@0
   186
			n++;
darope@0
   187
		}
darope@0
   188
	}
darope@0
   189
	for(routine=(api_info_t *) &newapi_info; routine->name; routine++) {
darope@0
   190
		if(routine->trace==mTRUE) {
darope@0
   191
			LOG_CONSOLE(PLID, "   %s (newapi)", routine->name);
darope@0
   192
			n++;
darope@0
   193
		}
darope@0
   194
	}
darope@0
   195
	for(routine=(api_info_t *) &engine_info; routine->name; routine++) {
darope@0
   196
		if(routine->trace==mTRUE) {
darope@0
   197
			LOG_CONSOLE(PLID, "   %s (engine)", routine->name);
darope@0
   198
			n++;
darope@0
   199
		}
darope@0
   200
	}
darope@0
   201
	LOG_CONSOLE(PLID, "%d routines", n);
darope@0
   202
}
darope@0
   203
darope@0
   204
// "trace list" console command.
darope@0
   205
void cmd_trace_list(void) {
darope@0
   206
	api_info_t *routine;
darope@0
   207
	mBOOL valid=mFALSE;
darope@0
   208
	int n, t;
darope@0
   209
	const char *arg;
darope@0
   210
	arg=CMD_ARGV(2);
darope@0
   211
	if(!strcasecmp(arg, "all") || !strcasecmp(arg, "dllapi")) {
darope@0
   212
		valid=mTRUE;
darope@0
   213
		n=0; t=0;
darope@0
   214
		LOG_CONSOLE(PLID, "DLLAPI routines:");
darope@0
   215
		for(routine=(api_info_t *) &dllapi_info; routine->name; routine++) {
darope@0
   216
			LOG_CONSOLE(PLID, "  %c %s", 
darope@0
   217
					routine->trace ? '+' : ' ',
darope@0
   218
					routine->name);
darope@0
   219
			n++;
darope@0
   220
			if(routine->trace) t++;
darope@0
   221
		}
darope@0
   222
		LOG_CONSOLE(PLID, "%d DLLAPI routines, %d traced", n, t);
darope@0
   223
	}
darope@0
   224
	if(!strcasecmp(arg, "all") || !strcasecmp(arg, "newapi")) {
darope@0
   225
		valid=mTRUE;
darope@0
   226
		n=0; t=0;
darope@0
   227
		LOG_CONSOLE(PLID, "NEWAPI routines:");
darope@0
   228
		for(routine=(api_info_t *) &newapi_info; routine->name; routine++) {
darope@0
   229
			LOG_CONSOLE(PLID, "  %c %s", 
darope@0
   230
					routine->trace ? '+' : ' ',
darope@0
   231
					routine->name);
darope@0
   232
			n++;
darope@0
   233
			if(routine->trace) t++;
darope@0
   234
		}
darope@0
   235
		LOG_CONSOLE(PLID, "%d NEWAPI routines, %d traced", n, t);
darope@0
   236
	}
darope@0
   237
	if(!strcasecmp(arg, "all") || !strcasecmp(arg, "engine")) {
darope@0
   238
		valid=mTRUE;
darope@0
   239
		n=0; t=0;
darope@0
   240
		LOG_CONSOLE(PLID, "Engine routines:");
darope@0
   241
		for(routine=(api_info_t *) &engine_info; routine->name; routine++) {
darope@0
   242
			LOG_CONSOLE(PLID, "  %c %s", 
darope@0
   243
					routine->trace ? '+' : ' ',
darope@0
   244
					routine->name);
darope@0
   245
			n++;
darope@0
   246
			if(routine->trace) t++;
darope@0
   247
		}
darope@0
   248
		LOG_CONSOLE(PLID, "%d Engine routines, %d traced", n, t);
darope@0
   249
	}
darope@0
   250
	if(!valid) {
darope@0
   251
		LOG_CONSOLE(PLID, "usage: trace list <type>");
darope@0
   252
		LOG_CONSOLE(PLID, "where <type> is one of:");
darope@0
   253
		LOG_CONSOLE(PLID, "   dllapi    - list all dllapi routines available for tracing");
darope@0
   254
		LOG_CONSOLE(PLID, "   newapi    - list all newapi routines available for tracing");
darope@0
   255
		LOG_CONSOLE(PLID, "   engine    - list all engine routines available for tracing");
darope@0
   256
		LOG_CONSOLE(PLID, "   all       - list dllapi, newapi, engine");
darope@0
   257
	}
darope@0
   258
}
darope@0
   259
darope@0
   260
// Set or unset tracing of a given api routine string.  Searches all three
darope@0
   261
// API lists, in the order:
darope@0
   262
//    dllapi
darope@0
   263
//    newapi
darope@0
   264
//    engine
darope@0
   265
// Returns API list in which the routine was found, as well as the
darope@0
   266
// "canonicalized" routine name/string.
darope@0
   267
TRACE_RESULT trace_setflag(const char **pfn_string, mBOOL flagval, const char **api) {
darope@0
   268
	api_info_t *routine;
darope@0
   269
	for(routine=(api_info_t *) &dllapi_info; routine->name; routine++) {
darope@0
   270
		if(!strcasecmp(routine->name, *pfn_string)) {
darope@0
   271
			*pfn_string=routine->name;
darope@0
   272
			*api="DLLAPI";
darope@0
   273
			if(routine->trace==flagval)
darope@0
   274
				return(TR_ALREADY);
darope@0
   275
			routine->trace=flagval;
darope@0
   276
			return(TR_SUCCESS);
darope@0
   277
		}
darope@0
   278
	}
darope@0
   279
	for(routine=(api_info_t *) &newapi_info; routine->name; routine++) {
darope@0
   280
		if(!strcasecmp(routine->name, *pfn_string)) {
darope@0
   281
			*pfn_string=routine->name;
darope@0
   282
			*api="NEWAPI";
darope@0
   283
			if(routine->trace==flagval)
darope@0
   284
				return(TR_ALREADY);
darope@0
   285
			routine->trace=flagval;
darope@0
   286
			return(TR_SUCCESS);
darope@0
   287
		}
darope@0
   288
	}
darope@0
   289
	for(routine=(api_info_t *) &engine_info; routine->name; routine++) {
darope@0
   290
		if(!strcasecmp(routine->name, *pfn_string)) {
darope@0
   291
			*pfn_string=routine->name;
darope@0
   292
			*api="Engine";
darope@0
   293
			if(routine->trace==flagval)
darope@0
   294
				return(TR_ALREADY);
darope@0
   295
			routine->trace=flagval;
darope@0
   296
			return(TR_SUCCESS);
darope@0
   297
		}
darope@0
   298
	}
darope@0
   299
	return(TR_FAILURE);
darope@0
   300
}