Fixed warning when building stub_mm plugin.
4 // trace_api.cpp - (main) implementation of HL API tracing operations
9 * This file is part of Metamod.
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.
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.
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
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
37 #include <extdll.h> // always
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
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
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};
53 cvar_t *dllapi_trace = NULL;
54 cvar_t *newapi_trace = NULL;
55 cvar_t *engine_trace = NULL;
56 cvar_t *unlimit_trace = NULL;
58 const char *msg_dest_types[32];
60 time_t last_trace_log = 0;
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);
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");
74 REG_SVR_COMMAND("trace", svr_trace);
76 memset(msg_dest_types, 0, sizeof(msg_dest_types));
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";
89 // Parse "trace" console command.
90 void svr_trace(void) {
93 if(!strcasecmp(cmd, "version"))
95 else if(!strcasecmp(cmd, "show"))
97 else if(!strcasecmp(cmd, "set"))
99 else if(!strcasecmp(cmd, "unset"))
101 else if(!strcasecmp(cmd, "list"))
104 LOG_CONSOLE(PLID, "Unrecognized trace command: %s", cmd);
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");
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);
132 // "trace set" console command.
133 void cmd_trace_set(void) {
140 LOG_CONSOLE(PLID, "usage: trace set <routine> [<routine> ...]");
143 for(i=2; i < argc; i++) {
145 ret=trace_setflag(&arg, mTRUE, &api);
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);
151 LOG_CONSOLE(PLID, "Unrecognized API routine '%s'", arg);
155 // "trace unset" console command.
156 void cmd_trace_unset(void) {
163 LOG_CONSOLE(PLID, "usage: trace unset <routine>");
166 for(i=1; i < argc; i++) {
168 ret=trace_setflag(&arg, mFALSE, &api);
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);
174 LOG_CONSOLE(PLID, "Unrecognized API routine '%s'", arg);
178 // "trace show" console command.
179 void cmd_trace_show(void) {
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);
189 for(routine=(api_info_t *) &newapi_info; routine->name; routine++) {
190 if(routine->trace==mTRUE) {
191 LOG_CONSOLE(PLID, " %s (newapi)", routine->name);
195 for(routine=(api_info_t *) &engine_info; routine->name; routine++) {
196 if(routine->trace==mTRUE) {
197 LOG_CONSOLE(PLID, " %s (engine)", routine->name);
201 LOG_CONSOLE(PLID, "%d routines", n);
204 // "trace list" console command.
205 void cmd_trace_list(void) {
211 if(!strcasecmp(arg, "all") || !strcasecmp(arg, "dllapi")) {
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 ? '+' : ' ',
220 if(routine->trace) t++;
222 LOG_CONSOLE(PLID, "%d DLLAPI routines, %d traced", n, t);
224 if(!strcasecmp(arg, "all") || !strcasecmp(arg, "newapi")) {
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 ? '+' : ' ',
233 if(routine->trace) t++;
235 LOG_CONSOLE(PLID, "%d NEWAPI routines, %d traced", n, t);
237 if(!strcasecmp(arg, "all") || !strcasecmp(arg, "engine")) {
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 ? '+' : ' ',
246 if(routine->trace) t++;
248 LOG_CONSOLE(PLID, "%d Engine routines, %d traced", n, t);
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");
260 // Set or unset tracing of a given api routine string. Searches all three
261 // API lists, in the order:
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) {
269 for(routine=(api_info_t *) &dllapi_info; routine->name; routine++) {
270 if(!strcasecmp(routine->name, *pfn_string)) {
271 *pfn_string=routine->name;
273 if(routine->trace==flagval)
275 routine->trace=flagval;
279 for(routine=(api_info_t *) &newapi_info; routine->name; routine++) {
280 if(!strcasecmp(routine->name, *pfn_string)) {
281 *pfn_string=routine->name;
283 if(routine->trace==flagval)
285 routine->trace=flagval;
289 for(routine=(api_info_t *) &engine_info; routine->name; routine++) {
290 if(!strcasecmp(routine->name, *pfn_string)) {
291 *pfn_string=routine->name;
293 if(routine->trace==flagval)
295 routine->trace=flagval;