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