1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/trace_plugin/trace_api.cpp Tue Apr 06 07:36:14 2004 +0000
1.3 @@ -0,0 +1,300 @@
1.4 +// vi: set ts=4 sw=4 :
1.5 +// vim: set tw=75 :
1.6 +
1.7 +// trace_api.cpp - (main) implementation of HL API tracing operations
1.8 +
1.9 +/*
1.10 + * Copyright (c) 2001-2003 Will Day <[email protected]>
1.11 + *
1.12 + * This file is part of Metamod.
1.13 + *
1.14 + * Metamod is free software; you can redistribute it and/or modify it
1.15 + * under the terms of the GNU General Public License as published by the
1.16 + * Free Software Foundation; either version 2 of the License, or (at
1.17 + * your option) any later version.
1.18 + *
1.19 + * Metamod is distributed in the hope that it will be useful, but
1.20 + * WITHOUT ANY WARRANTY; without even the implied warranty of
1.21 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.22 + * General Public License for more details.
1.23 + *
1.24 + * You should have received a copy of the GNU General Public License
1.25 + * along with Metamod; if not, write to the Free Software Foundation,
1.26 + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.27 + *
1.28 + * In addition, as a special exception, the author gives permission to
1.29 + * link the code of this program with the Half-Life Game Engine ("HL
1.30 + * Engine") and Modified Game Libraries ("MODs") developed by Valve,
1.31 + * L.L.C ("Valve"). You must obey the GNU General Public License in all
1.32 + * respects for all of the code used other than the HL Engine and MODs
1.33 + * from Valve. If you modify this file, you may extend this exception
1.34 + * to your version of the file, but you are not obligated to do so. If
1.35 + * you do not wish to do so, delete this exception statement from your
1.36 + * version.
1.37 + *
1.38 + */
1.39 +
1.40 +#include <extdll.h> // always
1.41 +
1.42 +#include <api_info.h> // api_info_t, etc
1.43 +#include <sdk_util.h> // REG_SVR_COMMAND, etc
1.44 +#include <meta_api.h> // Plugin_info, etc
1.45 +
1.46 +#include "trace_api.h" // me
1.47 +#include "log_plugin.h" // LOG_MSG, etc
1.48 +#include "vers_meta.h" // OPT_TYPE
1.49 +#include "vdate.h" // COMPILE_TIME
1.50 +
1.51 +cvar_t init_dllapi_trace = {"trace_dllapi", "0", FCVAR_EXTDLL, 0, NULL};
1.52 +cvar_t init_newapi_trace = {"trace_newapi", "0", FCVAR_EXTDLL, 0, NULL};
1.53 +cvar_t init_engine_trace = {"trace_engine", "0", FCVAR_EXTDLL, 0, NULL};
1.54 +cvar_t init_unlimit_trace = {"trace_unlimit", "0", FCVAR_EXTDLL, 0, NULL};
1.55 +
1.56 +cvar_t *dllapi_trace = NULL;
1.57 +cvar_t *newapi_trace = NULL;
1.58 +cvar_t *engine_trace = NULL;
1.59 +cvar_t *unlimit_trace = NULL;
1.60 +
1.61 +const char *msg_dest_types[32];
1.62 +
1.63 +time_t last_trace_log = 0;
1.64 +
1.65 +// Plugin startup. Register commands and cvars.
1.66 +void trace_init(void) {
1.67 + CVAR_REGISTER(&init_dllapi_trace);
1.68 + CVAR_REGISTER(&init_newapi_trace);
1.69 + CVAR_REGISTER(&init_engine_trace);
1.70 + CVAR_REGISTER(&init_unlimit_trace);
1.71 +
1.72 + dllapi_trace=CVAR_GET_POINTER("trace_dllapi");
1.73 + newapi_trace=CVAR_GET_POINTER("trace_newapi");
1.74 + engine_trace=CVAR_GET_POINTER("trace_engine");
1.75 + unlimit_trace=CVAR_GET_POINTER("trace_unlimit");
1.76 +
1.77 + REG_SVR_COMMAND("trace", svr_trace);
1.78 +
1.79 + memset(msg_dest_types, 0, sizeof(msg_dest_types));
1.80 +
1.81 + msg_dest_types[MSG_BROADCAST]="all_unreliable";
1.82 + msg_dest_types[MSG_ONE]="one_reliable";
1.83 + msg_dest_types[MSG_ALL]="all_reliable";
1.84 + msg_dest_types[MSG_INIT]="init";
1.85 + msg_dest_types[MSG_PVS]="pvs_unreliable";
1.86 + msg_dest_types[MSG_PAS]="pas_unreliable";
1.87 + msg_dest_types[MSG_PVS_R]="pvs_reliable";
1.88 + msg_dest_types[MSG_PAS_R]="pas_reliable";
1.89 + msg_dest_types[MSG_ONE_UNRELIABLE]="one_unreliable";
1.90 +}
1.91 +
1.92 +// Parse "trace" console command.
1.93 +void svr_trace(void) {
1.94 + const char *cmd;
1.95 + cmd=CMD_ARGV(1);
1.96 + if(!strcasecmp(cmd, "version"))
1.97 + cmd_trace_version();
1.98 + else if(!strcasecmp(cmd, "show"))
1.99 + cmd_trace_show();
1.100 + else if(!strcasecmp(cmd, "set"))
1.101 + cmd_trace_set();
1.102 + else if(!strcasecmp(cmd, "unset"))
1.103 + cmd_trace_unset();
1.104 + else if(!strcasecmp(cmd, "list"))
1.105 + cmd_trace_list();
1.106 + else {
1.107 + LOG_CONSOLE(PLID, "Unrecognized trace command: %s", cmd);
1.108 + cmd_trace_usage();
1.109 + return;
1.110 + }
1.111 +}
1.112 +
1.113 +// Print usage for "trace" console command.
1.114 +void cmd_trace_usage(void) {
1.115 + LOG_CONSOLE(PLID, "usage: trace <command> [<arguments>]");
1.116 + LOG_CONSOLE(PLID, "valid commands are:");
1.117 + LOG_CONSOLE(PLID, " version - display plugin version info");
1.118 + LOG_CONSOLE(PLID, " show - show currently traced api routines");
1.119 + LOG_CONSOLE(PLID, " set <routine> - set tracing for given routine");
1.120 + LOG_CONSOLE(PLID, " unset <routine> - unset tracing for given routine");
1.121 + LOG_CONSOLE(PLID, " list dllapi - list all dllapi routines available for tracing");
1.122 + LOG_CONSOLE(PLID, " list newapi - list all newapi routines available for tracing");
1.123 + LOG_CONSOLE(PLID, " list engine - list all engine routines available for tracing");
1.124 + LOG_CONSOLE(PLID, " list all - list dllapi, neapi, and engine");
1.125 +}
1.126 +
1.127 +// "trace version" console command.
1.128 +void cmd_trace_version(void) {
1.129 + LOG_CONSOLE(PLID, "%s v%s, %s", Plugin_info.name, Plugin_info.version, Plugin_info.date);
1.130 + LOG_CONSOLE(PLID, "by %s", Plugin_info.author);
1.131 + LOG_CONSOLE(PLID, " %s", Plugin_info.url);
1.132 + LOG_CONSOLE(PLID, "compiled: %s Eastern (%s)", COMPILE_TIME, OPT_TYPE);
1.133 +}
1.134 +
1.135 +// "trace set" console command.
1.136 +void cmd_trace_set(void) {
1.137 + int i, argc;
1.138 + const char *arg;
1.139 + const char *api;
1.140 + TRACE_RESULT ret;
1.141 + argc=CMD_ARGC();
1.142 + if(argc < 3) {
1.143 + LOG_CONSOLE(PLID, "usage: trace set <routine> [<routine> ...]");
1.144 + return;
1.145 + }
1.146 + for(i=2; i < argc; i++) {
1.147 + arg=CMD_ARGV(i);
1.148 + ret=trace_setflag(&arg, mTRUE, &api);
1.149 + if(ret==TR_SUCCESS)
1.150 + LOG_MESSAGE(PLID, "Tracing %s routine '%s'", api, arg);
1.151 + else if(ret==TR_ALREADY)
1.152 + LOG_CONSOLE(PLID, "Already tracing %s routine '%s'", api, arg);
1.153 + else
1.154 + LOG_CONSOLE(PLID, "Unrecognized API routine '%s'", arg);
1.155 + }
1.156 +}
1.157 +
1.158 +// "trace unset" console command.
1.159 +void cmd_trace_unset(void) {
1.160 + int i, argc;
1.161 + const char *arg;
1.162 + const char *api;
1.163 + TRACE_RESULT ret;
1.164 + argc=CMD_ARGC();
1.165 + if(argc < 3) {
1.166 + LOG_CONSOLE(PLID, "usage: trace unset <routine>");
1.167 + return;
1.168 + }
1.169 + for(i=1; i < argc; i++) {
1.170 + arg=CMD_ARGV(i);
1.171 + ret=trace_setflag(&arg, mFALSE, &api);
1.172 + if(ret==TR_SUCCESS)
1.173 + LOG_MESSAGE(PLID, "Un-Tracing %s routine '%s'", api, arg);
1.174 + else if(ret==TR_ALREADY)
1.175 + LOG_CONSOLE(PLID, "Already not tracing %s routine '%s'", api, arg);
1.176 + else
1.177 + LOG_CONSOLE(PLID, "Unrecognized API routine '%s'", arg);
1.178 + }
1.179 +}
1.180 +
1.181 +// "trace show" console command.
1.182 +void cmd_trace_show(void) {
1.183 + api_info_t *routine;
1.184 + int n=0;
1.185 + LOG_CONSOLE(PLID, "Tracing routines:");
1.186 + for(routine=(api_info_t *) &dllapi_info; routine->name; routine++) {
1.187 + if(routine->trace==mTRUE) {
1.188 + LOG_CONSOLE(PLID, " %s (dllapi)", routine->name);
1.189 + n++;
1.190 + }
1.191 + }
1.192 + for(routine=(api_info_t *) &newapi_info; routine->name; routine++) {
1.193 + if(routine->trace==mTRUE) {
1.194 + LOG_CONSOLE(PLID, " %s (newapi)", routine->name);
1.195 + n++;
1.196 + }
1.197 + }
1.198 + for(routine=(api_info_t *) &engine_info; routine->name; routine++) {
1.199 + if(routine->trace==mTRUE) {
1.200 + LOG_CONSOLE(PLID, " %s (engine)", routine->name);
1.201 + n++;
1.202 + }
1.203 + }
1.204 + LOG_CONSOLE(PLID, "%d routines", n);
1.205 +}
1.206 +
1.207 +// "trace list" console command.
1.208 +void cmd_trace_list(void) {
1.209 + api_info_t *routine;
1.210 + mBOOL valid=mFALSE;
1.211 + int n, t;
1.212 + const char *arg;
1.213 + arg=CMD_ARGV(2);
1.214 + if(!strcasecmp(arg, "all") || !strcasecmp(arg, "dllapi")) {
1.215 + valid=mTRUE;
1.216 + n=0; t=0;
1.217 + LOG_CONSOLE(PLID, "DLLAPI routines:");
1.218 + for(routine=(api_info_t *) &dllapi_info; routine->name; routine++) {
1.219 + LOG_CONSOLE(PLID, " %c %s",
1.220 + routine->trace ? '+' : ' ',
1.221 + routine->name);
1.222 + n++;
1.223 + if(routine->trace) t++;
1.224 + }
1.225 + LOG_CONSOLE(PLID, "%d DLLAPI routines, %d traced", n, t);
1.226 + }
1.227 + if(!strcasecmp(arg, "all") || !strcasecmp(arg, "newapi")) {
1.228 + valid=mTRUE;
1.229 + n=0; t=0;
1.230 + LOG_CONSOLE(PLID, "NEWAPI routines:");
1.231 + for(routine=(api_info_t *) &newapi_info; routine->name; routine++) {
1.232 + LOG_CONSOLE(PLID, " %c %s",
1.233 + routine->trace ? '+' : ' ',
1.234 + routine->name);
1.235 + n++;
1.236 + if(routine->trace) t++;
1.237 + }
1.238 + LOG_CONSOLE(PLID, "%d NEWAPI routines, %d traced", n, t);
1.239 + }
1.240 + if(!strcasecmp(arg, "all") || !strcasecmp(arg, "engine")) {
1.241 + valid=mTRUE;
1.242 + n=0; t=0;
1.243 + LOG_CONSOLE(PLID, "Engine routines:");
1.244 + for(routine=(api_info_t *) &engine_info; routine->name; routine++) {
1.245 + LOG_CONSOLE(PLID, " %c %s",
1.246 + routine->trace ? '+' : ' ',
1.247 + routine->name);
1.248 + n++;
1.249 + if(routine->trace) t++;
1.250 + }
1.251 + LOG_CONSOLE(PLID, "%d Engine routines, %d traced", n, t);
1.252 + }
1.253 + if(!valid) {
1.254 + LOG_CONSOLE(PLID, "usage: trace list <type>");
1.255 + LOG_CONSOLE(PLID, "where <type> is one of:");
1.256 + LOG_CONSOLE(PLID, " dllapi - list all dllapi routines available for tracing");
1.257 + LOG_CONSOLE(PLID, " newapi - list all newapi routines available for tracing");
1.258 + LOG_CONSOLE(PLID, " engine - list all engine routines available for tracing");
1.259 + LOG_CONSOLE(PLID, " all - list dllapi, newapi, engine");
1.260 + }
1.261 +}
1.262 +
1.263 +// Set or unset tracing of a given api routine string. Searches all three
1.264 +// API lists, in the order:
1.265 +// dllapi
1.266 +// newapi
1.267 +// engine
1.268 +// Returns API list in which the routine was found, as well as the
1.269 +// "canonicalized" routine name/string.
1.270 +TRACE_RESULT trace_setflag(const char **pfn_string, mBOOL flagval, const char **api) {
1.271 + api_info_t *routine;
1.272 + for(routine=(api_info_t *) &dllapi_info; routine->name; routine++) {
1.273 + if(!strcasecmp(routine->name, *pfn_string)) {
1.274 + *pfn_string=routine->name;
1.275 + *api="DLLAPI";
1.276 + if(routine->trace==flagval)
1.277 + return(TR_ALREADY);
1.278 + routine->trace=flagval;
1.279 + return(TR_SUCCESS);
1.280 + }
1.281 + }
1.282 + for(routine=(api_info_t *) &newapi_info; routine->name; routine++) {
1.283 + if(!strcasecmp(routine->name, *pfn_string)) {
1.284 + *pfn_string=routine->name;
1.285 + *api="NEWAPI";
1.286 + if(routine->trace==flagval)
1.287 + return(TR_ALREADY);
1.288 + routine->trace=flagval;
1.289 + return(TR_SUCCESS);
1.290 + }
1.291 + }
1.292 + for(routine=(api_info_t *) &engine_info; routine->name; routine++) {
1.293 + if(!strcasecmp(routine->name, *pfn_string)) {
1.294 + *pfn_string=routine->name;
1.295 + *api="Engine";
1.296 + if(routine->trace==flagval)
1.297 + return(TR_ALREADY);
1.298 + routine->trace=flagval;
1.299 + return(TR_SUCCESS);
1.300 + }
1.301 + }
1.302 + return(TR_FAILURE);
1.303 +}