darope@0: // vi: set ts=4 sw=4 : darope@0: // vim: set tw=75 : darope@0: darope@0: // trace_api.cpp - (main) implementation of HL API tracing operations darope@0: darope@0: /* darope@0: * Copyright (c) 2001-2003 Will Day darope@0: * darope@0: * This file is part of Metamod. darope@0: * darope@0: * Metamod is free software; you can redistribute it and/or modify it darope@0: * under the terms of the GNU General Public License as published by the darope@0: * Free Software Foundation; either version 2 of the License, or (at darope@0: * your option) any later version. darope@0: * darope@0: * Metamod is distributed in the hope that it will be useful, but darope@0: * WITHOUT ANY WARRANTY; without even the implied warranty of darope@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU darope@0: * General Public License for more details. darope@0: * darope@0: * You should have received a copy of the GNU General Public License darope@0: * along with Metamod; if not, write to the Free Software Foundation, darope@0: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA darope@0: * darope@0: * In addition, as a special exception, the author gives permission to darope@0: * link the code of this program with the Half-Life Game Engine ("HL darope@0: * Engine") and Modified Game Libraries ("MODs") developed by Valve, darope@0: * L.L.C ("Valve"). You must obey the GNU General Public License in all darope@0: * respects for all of the code used other than the HL Engine and MODs darope@0: * from Valve. If you modify this file, you may extend this exception darope@0: * to your version of the file, but you are not obligated to do so. If darope@0: * you do not wish to do so, delete this exception statement from your darope@0: * version. darope@0: * darope@0: */ darope@0: darope@0: #include // always darope@0: darope@0: #include // api_info_t, etc darope@0: #include // REG_SVR_COMMAND, etc darope@0: #include // Plugin_info, etc darope@0: darope@0: #include "trace_api.h" // me darope@0: #include "log_plugin.h" // LOG_MSG, etc darope@0: #include "vers_meta.h" // OPT_TYPE darope@0: #include "vdate.h" // COMPILE_TIME darope@0: darope@0: cvar_t init_dllapi_trace = {"trace_dllapi", "0", FCVAR_EXTDLL, 0, NULL}; darope@0: cvar_t init_newapi_trace = {"trace_newapi", "0", FCVAR_EXTDLL, 0, NULL}; darope@0: cvar_t init_engine_trace = {"trace_engine", "0", FCVAR_EXTDLL, 0, NULL}; darope@0: cvar_t init_unlimit_trace = {"trace_unlimit", "0", FCVAR_EXTDLL, 0, NULL}; darope@0: darope@0: cvar_t *dllapi_trace = NULL; darope@0: cvar_t *newapi_trace = NULL; darope@0: cvar_t *engine_trace = NULL; darope@0: cvar_t *unlimit_trace = NULL; darope@0: darope@0: const char *msg_dest_types[32]; darope@0: darope@0: time_t last_trace_log = 0; darope@0: darope@0: // Plugin startup. Register commands and cvars. darope@0: void trace_init(void) { darope@0: CVAR_REGISTER(&init_dllapi_trace); darope@0: CVAR_REGISTER(&init_newapi_trace); darope@0: CVAR_REGISTER(&init_engine_trace); darope@0: CVAR_REGISTER(&init_unlimit_trace); darope@0: darope@0: dllapi_trace=CVAR_GET_POINTER("trace_dllapi"); darope@0: newapi_trace=CVAR_GET_POINTER("trace_newapi"); darope@0: engine_trace=CVAR_GET_POINTER("trace_engine"); darope@0: unlimit_trace=CVAR_GET_POINTER("trace_unlimit"); darope@0: darope@0: REG_SVR_COMMAND("trace", svr_trace); darope@0: darope@0: memset(msg_dest_types, 0, sizeof(msg_dest_types)); darope@0: darope@0: msg_dest_types[MSG_BROADCAST]="all_unreliable"; darope@0: msg_dest_types[MSG_ONE]="one_reliable"; darope@0: msg_dest_types[MSG_ALL]="all_reliable"; darope@0: msg_dest_types[MSG_INIT]="init"; darope@0: msg_dest_types[MSG_PVS]="pvs_unreliable"; darope@0: msg_dest_types[MSG_PAS]="pas_unreliable"; darope@0: msg_dest_types[MSG_PVS_R]="pvs_reliable"; darope@0: msg_dest_types[MSG_PAS_R]="pas_reliable"; darope@0: msg_dest_types[MSG_ONE_UNRELIABLE]="one_unreliable"; darope@0: } darope@0: darope@0: // Parse "trace" console command. darope@0: void svr_trace(void) { darope@0: const char *cmd; darope@0: cmd=CMD_ARGV(1); darope@0: if(!strcasecmp(cmd, "version")) darope@0: cmd_trace_version(); darope@0: else if(!strcasecmp(cmd, "show")) darope@0: cmd_trace_show(); darope@0: else if(!strcasecmp(cmd, "set")) darope@0: cmd_trace_set(); darope@0: else if(!strcasecmp(cmd, "unset")) darope@0: cmd_trace_unset(); darope@0: else if(!strcasecmp(cmd, "list")) darope@0: cmd_trace_list(); darope@0: else { darope@0: LOG_CONSOLE(PLID, "Unrecognized trace command: %s", cmd); darope@0: cmd_trace_usage(); darope@0: return; darope@0: } darope@0: } darope@0: darope@0: // Print usage for "trace" console command. darope@0: void cmd_trace_usage(void) { darope@0: LOG_CONSOLE(PLID, "usage: trace []"); darope@0: LOG_CONSOLE(PLID, "valid commands are:"); darope@0: LOG_CONSOLE(PLID, " version - display plugin version info"); darope@0: LOG_CONSOLE(PLID, " show - show currently traced api routines"); darope@0: LOG_CONSOLE(PLID, " set - set tracing for given routine"); darope@0: LOG_CONSOLE(PLID, " unset - unset tracing for given routine"); darope@0: LOG_CONSOLE(PLID, " list dllapi - list all dllapi routines available for tracing"); darope@0: LOG_CONSOLE(PLID, " list newapi - list all newapi routines available for tracing"); darope@0: LOG_CONSOLE(PLID, " list engine - list all engine routines available for tracing"); darope@0: LOG_CONSOLE(PLID, " list all - list dllapi, neapi, and engine"); darope@0: } darope@0: darope@0: // "trace version" console command. darope@0: void cmd_trace_version(void) { darope@0: LOG_CONSOLE(PLID, "%s v%s, %s", Plugin_info.name, Plugin_info.version, Plugin_info.date); darope@0: LOG_CONSOLE(PLID, "by %s", Plugin_info.author); darope@0: LOG_CONSOLE(PLID, " %s", Plugin_info.url); darope@0: LOG_CONSOLE(PLID, "compiled: %s Eastern (%s)", COMPILE_TIME, OPT_TYPE); darope@0: } darope@0: darope@0: // "trace set" console command. darope@0: void cmd_trace_set(void) { darope@0: int i, argc; darope@0: const char *arg; darope@0: const char *api; darope@0: TRACE_RESULT ret; darope@0: argc=CMD_ARGC(); darope@0: if(argc < 3) { darope@0: LOG_CONSOLE(PLID, "usage: trace set [ ...]"); darope@0: return; darope@0: } darope@0: for(i=2; i < argc; i++) { darope@0: arg=CMD_ARGV(i); darope@0: ret=trace_setflag(&arg, mTRUE, &api); darope@0: if(ret==TR_SUCCESS) darope@0: LOG_MESSAGE(PLID, "Tracing %s routine '%s'", api, arg); darope@0: else if(ret==TR_ALREADY) darope@0: LOG_CONSOLE(PLID, "Already tracing %s routine '%s'", api, arg); darope@0: else darope@0: LOG_CONSOLE(PLID, "Unrecognized API routine '%s'", arg); darope@0: } darope@0: } darope@0: darope@0: // "trace unset" console command. darope@0: void cmd_trace_unset(void) { darope@0: int i, argc; darope@0: const char *arg; darope@0: const char *api; darope@0: TRACE_RESULT ret; darope@0: argc=CMD_ARGC(); darope@0: if(argc < 3) { darope@0: LOG_CONSOLE(PLID, "usage: trace unset "); darope@0: return; darope@0: } darope@0: for(i=1; i < argc; i++) { darope@0: arg=CMD_ARGV(i); darope@0: ret=trace_setflag(&arg, mFALSE, &api); darope@0: if(ret==TR_SUCCESS) darope@0: LOG_MESSAGE(PLID, "Un-Tracing %s routine '%s'", api, arg); darope@0: else if(ret==TR_ALREADY) darope@0: LOG_CONSOLE(PLID, "Already not tracing %s routine '%s'", api, arg); darope@0: else darope@0: LOG_CONSOLE(PLID, "Unrecognized API routine '%s'", arg); darope@0: } darope@0: } darope@0: darope@0: // "trace show" console command. darope@0: void cmd_trace_show(void) { darope@0: api_info_t *routine; darope@0: int n=0; darope@0: LOG_CONSOLE(PLID, "Tracing routines:"); darope@0: for(routine=(api_info_t *) &dllapi_info; routine->name; routine++) { darope@0: if(routine->trace==mTRUE) { darope@0: LOG_CONSOLE(PLID, " %s (dllapi)", routine->name); darope@0: n++; darope@0: } darope@0: } darope@0: for(routine=(api_info_t *) &newapi_info; routine->name; routine++) { darope@0: if(routine->trace==mTRUE) { darope@0: LOG_CONSOLE(PLID, " %s (newapi)", routine->name); darope@0: n++; darope@0: } darope@0: } darope@0: for(routine=(api_info_t *) &engine_info; routine->name; routine++) { darope@0: if(routine->trace==mTRUE) { darope@0: LOG_CONSOLE(PLID, " %s (engine)", routine->name); darope@0: n++; darope@0: } darope@0: } darope@0: LOG_CONSOLE(PLID, "%d routines", n); darope@0: } darope@0: darope@0: // "trace list" console command. darope@0: void cmd_trace_list(void) { darope@0: api_info_t *routine; darope@0: mBOOL valid=mFALSE; darope@0: int n, t; darope@0: const char *arg; darope@0: arg=CMD_ARGV(2); darope@0: if(!strcasecmp(arg, "all") || !strcasecmp(arg, "dllapi")) { darope@0: valid=mTRUE; darope@0: n=0; t=0; darope@0: LOG_CONSOLE(PLID, "DLLAPI routines:"); darope@0: for(routine=(api_info_t *) &dllapi_info; routine->name; routine++) { darope@0: LOG_CONSOLE(PLID, " %c %s", darope@0: routine->trace ? '+' : ' ', darope@0: routine->name); darope@0: n++; darope@0: if(routine->trace) t++; darope@0: } darope@0: LOG_CONSOLE(PLID, "%d DLLAPI routines, %d traced", n, t); darope@0: } darope@0: if(!strcasecmp(arg, "all") || !strcasecmp(arg, "newapi")) { darope@0: valid=mTRUE; darope@0: n=0; t=0; darope@0: LOG_CONSOLE(PLID, "NEWAPI routines:"); darope@0: for(routine=(api_info_t *) &newapi_info; routine->name; routine++) { darope@0: LOG_CONSOLE(PLID, " %c %s", darope@0: routine->trace ? '+' : ' ', darope@0: routine->name); darope@0: n++; darope@0: if(routine->trace) t++; darope@0: } darope@0: LOG_CONSOLE(PLID, "%d NEWAPI routines, %d traced", n, t); darope@0: } darope@0: if(!strcasecmp(arg, "all") || !strcasecmp(arg, "engine")) { darope@0: valid=mTRUE; darope@0: n=0; t=0; darope@0: LOG_CONSOLE(PLID, "Engine routines:"); darope@0: for(routine=(api_info_t *) &engine_info; routine->name; routine++) { darope@0: LOG_CONSOLE(PLID, " %c %s", darope@0: routine->trace ? '+' : ' ', darope@0: routine->name); darope@0: n++; darope@0: if(routine->trace) t++; darope@0: } darope@0: LOG_CONSOLE(PLID, "%d Engine routines, %d traced", n, t); darope@0: } darope@0: if(!valid) { darope@0: LOG_CONSOLE(PLID, "usage: trace list "); darope@0: LOG_CONSOLE(PLID, "where is one of:"); darope@0: LOG_CONSOLE(PLID, " dllapi - list all dllapi routines available for tracing"); darope@0: LOG_CONSOLE(PLID, " newapi - list all newapi routines available for tracing"); darope@0: LOG_CONSOLE(PLID, " engine - list all engine routines available for tracing"); darope@0: LOG_CONSOLE(PLID, " all - list dllapi, newapi, engine"); darope@0: } darope@0: } darope@0: darope@0: // Set or unset tracing of a given api routine string. Searches all three darope@0: // API lists, in the order: darope@0: // dllapi darope@0: // newapi darope@0: // engine darope@0: // Returns API list in which the routine was found, as well as the darope@0: // "canonicalized" routine name/string. darope@0: TRACE_RESULT trace_setflag(const char **pfn_string, mBOOL flagval, const char **api) { darope@0: api_info_t *routine; darope@0: for(routine=(api_info_t *) &dllapi_info; routine->name; routine++) { darope@0: if(!strcasecmp(routine->name, *pfn_string)) { darope@0: *pfn_string=routine->name; darope@0: *api="DLLAPI"; darope@0: if(routine->trace==flagval) darope@0: return(TR_ALREADY); darope@0: routine->trace=flagval; darope@0: return(TR_SUCCESS); darope@0: } darope@0: } darope@0: for(routine=(api_info_t *) &newapi_info; routine->name; routine++) { darope@0: if(!strcasecmp(routine->name, *pfn_string)) { darope@0: *pfn_string=routine->name; darope@0: *api="NEWAPI"; darope@0: if(routine->trace==flagval) darope@0: return(TR_ALREADY); darope@0: routine->trace=flagval; darope@0: return(TR_SUCCESS); darope@0: } darope@0: } darope@0: for(routine=(api_info_t *) &engine_info; routine->name; routine++) { darope@0: if(!strcasecmp(routine->name, *pfn_string)) { darope@0: *pfn_string=routine->name; darope@0: *api="Engine"; darope@0: if(routine->trace==flagval) darope@0: return(TR_ALREADY); darope@0: routine->trace=flagval; darope@0: return(TR_SUCCESS); darope@0: } darope@0: } darope@0: return(TR_FAILURE); darope@0: }