1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================
#include "cbase.h"
#include "isaverestore.h"
#include "env_debughistory.h"
#include "tier0/vprof.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
// Number of characters worth of debug to use per history category
#define DEBUG_HISTORY_VERSION 6
#define DEBUG_HISTORY_FIRST_VERSIONED 5
#define MAX_DEBUG_HISTORY_LINE_LENGTH 256
#define MAX_DEBUG_HISTORY_LENGTH (1000 * MAX_DEBUG_HISTORY_LINE_LENGTH)
//-----------------------------------------------------------------------------
// Purpose: Stores debug history in savegame files for debugging reference
//-----------------------------------------------------------------------------
class CDebugHistory : public CBaseEntity
{
DECLARE_CLASS( CDebugHistory, CBaseEntity );
public:
DECLARE_DATADESC();
void Spawn();
void AddDebugHistoryLine( int iCategory, const char *szLine );
void ClearHistories( void );
void DumpDebugHistory( int iCategory );
int Save( ISave &save );
int Restore( IRestore &restore );
private:
char m_DebugLines[MAX_HISTORY_CATEGORIES][MAX_DEBUG_HISTORY_LENGTH];
char *m_DebugLineEnd[MAX_HISTORY_CATEGORIES];
};
BEGIN_DATADESC( CDebugHistory )
//DEFINE_FIELD( m_DebugLines, FIELD_CHARACTER ), // Not saved because we write it out manually
//DEFINE_FIELD( m_DebugLineEnd, FIELD_CHARACTER ),
END_DATADESC()
LINK_ENTITY_TO_CLASS( env_debughistory, CDebugHistory );
// The handle to the debug history singleton. Created on first access via GetDebugHistory.
static CHandle< CDebugHistory > s_DebugHistory;
//-----------------------------------------------------------------------------
// Spawn
//-----------------------------------------------------------------------------
void CDebugHistory::Spawn()
{
BaseClass::Spawn();
#ifdef DISABLE_DEBUG_HISTORY
UTIL_Remove( this );
#else
if ( g_pGameRules && g_pGameRules->IsMultiplayer() )
{
UTIL_Remove( this );
}
else
{
Warning( "DEBUG HISTORY IS ENABLED. Disable before release (in env_debughistory.h).\n" );
}
#endif
ClearHistories();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CDebugHistory::AddDebugHistoryLine( int iCategory, const char *szLine )
{
if ( iCategory < 0 || iCategory >= MAX_HISTORY_CATEGORIES )
{
Warning("Attempted to add a debughistory line to category %d. Valid categories are %d to %d.\n", iCategory, 0, (MAX_HISTORY_CATEGORIES-1) );
return;
}
// Don't do debug history before the singleton is properly set up.
if ( !m_DebugLineEnd[iCategory] )
return;
const char *pszRemaining = szLine;
int iCharsToWrite = strlen( pszRemaining ) + 1; // Add 1 so that we copy the null terminator
// Clip the line if it's too long. Wasteful doing it this way, but keeps code below nice & simple.
char szTmpBuffer[MAX_DEBUG_HISTORY_LINE_LENGTH];
if ( iCharsToWrite > MAX_DEBUG_HISTORY_LINE_LENGTH)
{
memcpy( szTmpBuffer, szLine, sizeof(szTmpBuffer) );
szTmpBuffer[MAX_DEBUG_HISTORY_LINE_LENGTH-1] = '\0';
pszRemaining = szTmpBuffer;
iCharsToWrite = MAX_DEBUG_HISTORY_LINE_LENGTH;
}
while ( iCharsToWrite )
{
int iCharsLeftBeforeLoop = sizeof(m_DebugLines[iCategory]) - (m_DebugLineEnd[iCategory] - m_DebugLines[iCategory]);
// Write into the buffer
int iWrote = MIN( iCharsToWrite, iCharsLeftBeforeLoop );
memcpy( m_DebugLineEnd[iCategory], pszRemaining, iWrote );
m_DebugLineEnd[iCategory] += iWrote;
pszRemaining += iWrote;
// Did we loop?
if ( iWrote == iCharsLeftBeforeLoop )
{
m_DebugLineEnd[iCategory] = m_DebugLines[iCategory];
}
iCharsToWrite -= iWrote;
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CDebugHistory::DumpDebugHistory( int iCategory )
{
if ( iCategory < 0 || iCategory >= MAX_HISTORY_CATEGORIES )
{
Warning("Attempted to dump a history for category %d. Valid categories are %d to %d.\n", iCategory, 0, (MAX_HISTORY_CATEGORIES-1) );
return;
}
// Find the start of the oldest whole debug line.
const char *pszLine = m_DebugLineEnd[iCategory] + 1;
if ( (pszLine - m_DebugLines[iCategory]) >= sizeof(m_DebugLines[iCategory]) )
{
pszLine = m_DebugLines[iCategory];
}
// Are we at the start of a line? If there's a null terminator before us, then we're good to go.
while ( (!( pszLine == m_DebugLines[iCategory] && *(m_DebugLines[iCategory]+sizeof(m_DebugLines[iCategory])-1) == '\0' ) &&
!( pszLine != m_DebugLines[iCategory] && *(pszLine-1) == '\0' ))
|| *pszLine == '\0' )
{
pszLine++;
// Have we looped?
if ( (pszLine - m_DebugLines[iCategory]) >= sizeof(m_DebugLines[iCategory]) )
{
pszLine = m_DebugLines[iCategory];
}
if ( pszLine == m_DebugLineEnd[iCategory] )
{
// We looped through the entire history, and found nothing.
Msg( "Debug History of Category %d is EMPTY\n", iCategory );
return;
}
}
// Now print everything up till the end
char szMsgBuffer[MAX_DEBUG_HISTORY_LINE_LENGTH];
char *pszMsg = szMsgBuffer;
Msg( "Starting Debug History Dump of Category %d\n", iCategory );
while ( pszLine != m_DebugLineEnd[iCategory] )
{
*pszMsg = *pszLine;
if ( *pszLine == '\0' )
{
if ( szMsgBuffer[0] != '\0' )
{
// Found a full line, so print it
Msg( "%s", szMsgBuffer );
}
// Clear the buffer
pszMsg = szMsgBuffer;
*pszMsg = '\0';
}
else
{
pszMsg++;
}
pszLine++;
// Have we looped?
if ( (pszLine - m_DebugLines[iCategory]) >= sizeof(m_DebugLines[iCategory]) )
{
pszLine = m_DebugLines[iCategory];
}
}
Msg("Ended Debug History Dump of Category %d\n", iCategory );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CDebugHistory::ClearHistories( void )
{
for ( int i = 0; i < MAX_HISTORY_CATEGORIES; i++ )
{
memset( m_DebugLines[i], 0, sizeof(m_DebugLines[i]) );
m_DebugLineEnd[i] = m_DebugLines[i];
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CDebugHistory::Save( ISave &save )
{
int iVersion = DEBUG_HISTORY_VERSION;
save.WriteInt( &iVersion );
int iMaxCategorys = MAX_HISTORY_CATEGORIES;
save.WriteInt( &iMaxCategorys );
for ( int iCategory = 0; iCategory < MAX_HISTORY_CATEGORIES; iCategory++ )
{
int iEnd = m_DebugLineEnd[iCategory] - m_DebugLines[iCategory];
save.WriteInt( &iEnd );
save.WriteData( m_DebugLines[iCategory], MAX_DEBUG_HISTORY_LENGTH );
}
return BaseClass::Save(save);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CDebugHistory::Restore( IRestore &restore )
{
ClearHistories();
int iVersion = restore.ReadInt();
if ( iVersion >= DEBUG_HISTORY_FIRST_VERSIONED )
{
int iMaxCategorys = restore.ReadInt();
for ( int iCategory = 0; iCategory < MIN(iMaxCategorys,MAX_HISTORY_CATEGORIES); iCategory++ )
{
int iEnd = restore.ReadInt();
m_DebugLineEnd[iCategory] = m_DebugLines[iCategory] + iEnd;
restore.ReadData( m_DebugLines[iCategory], sizeof(m_DebugLines[iCategory]), 0 );
}
}
else
{
int iMaxCategorys = iVersion;
for ( int iCategory = 0; iCategory < MIN(iMaxCategorys,MAX_HISTORY_CATEGORIES); iCategory++ )
{
int iEnd = restore.ReadInt();
m_DebugLineEnd[iCategory] = m_DebugLines[iCategory] + iEnd;
restore.ReadData( m_DebugLines[iCategory], sizeof(m_DebugLines[iCategory]), 0 );
}
}
return BaseClass::Restore(restore);
}
//-----------------------------------------------------------------------------
// Purpose: Singleton debug history. Created by first usage.
//-----------------------------------------------------------------------------
CDebugHistory *GetDebugHistory()
{
#ifdef DISABLE_DEBUG_HISTORY
return NULL;
#endif
if ( g_pGameRules && g_pGameRules->IsMultiplayer() )
return NULL;
if ( s_DebugHistory == NULL )
{
CBaseEntity *pEnt = gEntList.FindEntityByClassname( NULL, "env_debughistory" );
if ( pEnt )
{
s_DebugHistory = dynamic_cast<CDebugHistory*>(pEnt);
}
else
{
s_DebugHistory = ( CDebugHistory * )CreateEntityByName( "env_debughistory" );
if ( s_DebugHistory )
{
s_DebugHistory->Spawn();
}
}
}
Assert( s_DebugHistory );
return s_DebugHistory;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void AddDebugHistoryLine( int iCategory, const char *pszLine )
{
#ifdef DISABLE_DEBUG_HISTORY
return;
#else
if ( g_pGameRules && g_pGameRules->IsMultiplayer() )
return;
if ( !GetDebugHistory() )
{
Warning("Failed to find or create an env_debughistory.\n" );
return;
}
GetDebugHistory()->AddDebugHistoryLine( iCategory, pszLine );
#endif
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CC_DebugHistory_AddLine( const CCommand &args )
{
if ( !UTIL_IsCommandIssuedByServerAdmin() )
return;
if ( args.ArgC() < 3 )
{
Warning("Incorrect parameters. Format: <category id> <line>\n");
return;
}
int iCategory = atoi(args[ 1 ]);
const char *pszLine = args[ 2 ];
AddDebugHistoryLine( iCategory, pszLine );
}
static ConCommand dbghist_addline( "dbghist_addline", CC_DebugHistory_AddLine, "Add a line to the debug history. Format: <category id> <line>", FCVAR_NONE );
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CC_DebugHistory_Dump( const CCommand &args )
{
if ( !UTIL_IsCommandIssuedByServerAdmin() )
return;
if ( args.ArgC() < 2 )
{
Warning("Incorrect parameters. Format: <category id>\n");
return;
}
if ( GetDebugHistory() )
{
int iCategory = atoi(args[ 1 ]);
GetDebugHistory()->DumpDebugHistory( iCategory );
}
}
static ConCommand dbghist_dump("dbghist_dump", CC_DebugHistory_Dump,
"Dump the debug history to the console. Format: <category id>\n"
" Categories:\n"
" 0: Entity I/O\n"
" 1: AI Decisions\n"
" 2: Scene Print\n"
" 3: Alyx Blind\n"
" 4: Log of damage done to player",
FCVAR_NONE );
|