-
Notifications
You must be signed in to change notification settings - Fork 711
Expand file tree
/
Copy pathdbg.cpp
More file actions
189 lines (168 loc) · 4.96 KB
/
dbg.cpp
File metadata and controls
189 lines (168 loc) · 4.96 KB
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
//========= Copyright 1996-2005, Valve Corporation, All rights reserved. ============//
//
// This is a custom version of dbg.cpp for the standalone version of
// SteamnetworkingSockets. It was taken from the Steam code and then
// stripped to the bare essentials.
//
//=============================================================================//
#include <tier0/dbg.h>
#if defined ( STEAMDATAGRAM_GAMECOORDINATOR_FOREXPORT )
extern void SteamDatagramGame_AssertFailed( bool bFmt, const char* pstrFile, unsigned int nLine, const char *pMsg, va_list ap );
#elif defined( STEAMNETWORKINGSOCKETS_FOREXPORT )
#include "../steamnetworkingsockets/clientlib/steamnetworkingsockets_lowlevel.h"
using namespace SteamNetworkingSocketsLib;
#endif
#if defined(_WIN32) && !defined(_XBOX)
#include "winlite.h"
#include <tchar.h>
#endif
#include <assert.h>
#if IsPosix()
#include <unistd.h>
#if !IsPlaystation()
#include <signal.h>
#endif
#endif // POSIX
#if IsLinux()
#include <sys/ptrace.h>
#endif
#if IsOSX() || IsFreeBSD()
#include <sys/sysctl.h>
#if IsFreeBSD()
#include <sys/proc.h>
#include <sys/user.h>
#endif
#endif
#if IsPlaystation() && defined(_DEBUG)
// NDA material
#endif
bool Plat_IsInDebugSession()
{
#ifdef _WIN32
return (IsDebuggerPresent() != 0);
#elif IsOSX() || IsFreeBSD()
int mib[4];
struct kinfo_proc info;
size_t size;
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = getpid();
size = sizeof(info);
#if IsFreeBSD()
info.ki_paddr->p_flag = 0;
#else
info.kp_proc.flag = 0;
#endif
sysctl(mib,4,&info,&size,NULL,0);
#if IsFreeBSD()
return ((info.ki_paddr->p_flag & P_TRACED) == P_TRACED);
#else
return ((info.kp_proc.flag & P_TRACED) == P_TRACED);
#endif
#elif IsLinux()
static FILE *fp;
if ( !fp )
{
char rgchProcStatusFile[256]; rgchProcStatusFile[0] = '\0';
snprintf( rgchProcStatusFile, sizeof(rgchProcStatusFile), "/proc/%d/status", getpid() );
fp = fopen( rgchProcStatusFile, "r" );
}
char rgchLine[256]; rgchLine[0] = '\0';
int nTracePid = 0;
if ( fp )
{
const char *pszSearchString = "TracerPid:";
const uint cchSearchString = strlen( pszSearchString );
rewind( fp );
while ( fgets( rgchLine, sizeof(rgchLine), fp ) )
{
if ( !strncasecmp( pszSearchString, rgchLine, cchSearchString ) )
{
char *pszVal = rgchLine+cchSearchString+1;
nTracePid = atoi( pszVal );
break;
}
}
}
return (nTracePid != 0);
#elif IsPlaystation()
// NDA material
#elif IsNintendoSwitch()
return false;
#else
#error "HALP"
#endif
}
void AssertMsgImplementationV( bool _bFatal, bool bFmt, const char* pstrFile, unsigned int nLine, PRINTF_FORMAT_STRING const char *pMsg, va_list ap )
{
static intp s_ThreadLocalAssertMsgGuardStatic; // Really should be thread-local
if ( !_bFatal && s_ThreadLocalAssertMsgGuardStatic > 0 )
{
//
// No need to re-enter.
//
return;
}
++s_ThreadLocalAssertMsgGuardStatic;
#if defined ( STEAMDATAGRAM_GAMECOORDINATOR_FOREXPORT )
SteamDatagramGame_AssertFailed( bFmt, pstrFile, nLine, pMsg, ap );
#elif defined( STEAMNETWORKINGSOCKETS_FOREXPORT )
(*g_pfnPreFormatSpewHandler)( k_ESteamNetworkingSocketsDebugOutputType_Bug, bFmt, pstrFile, nLine, pMsg, ap );
#else
fflush(stdout);
if ( pstrFile )
fprintf( stderr, "%s(%d): ", pstrFile, nLine );
if ( bFmt )
vfprintf( stderr, pMsg, ap );
else
; fprintf( stderr, "%s", pMsg );
fflush(stderr);
if ( Plat_IsInDebugSession() )
{
// HELLO DEVELOPER: Set this to true if you are getting fed up with the DebuggerBreak().
static volatile bool s_bDisableDebuggerBreak = false;
if ( !s_bDisableDebuggerBreak )
DebuggerBreak();
}
#endif
if ( _bFatal )
{
#ifdef _WIN32
TerminateProcess( GetCurrentProcess(), EXIT_FAILURE ); // die, die RIGHT NOW! (don't call exit() so destructors will not get run)
#elif defined( _PS3 )
sys_process_exit( EXIT_FAILURE );
#elif defined( __clang__ )
abort();
#else
std::quick_exit( EXIT_FAILURE );
#endif
}
--s_ThreadLocalAssertMsgGuardStatic;
}
void AssertMsgHelper<true,true>::AssertFailed( const char* pstrFile, unsigned int nLine, const char *pMsg )
{
va_list dummy;
memset( &dummy, 0, sizeof(dummy) ); // not needed, but might shut up a warning
AssertMsgImplementationV( true, false, pstrFile, nLine, pMsg, dummy );
}
void AssertMsgHelper<false,true>::AssertFailed( const char* pstrFile, unsigned int nLine, const char *pMsg )
{
va_list dummy;
memset( &dummy, 0, sizeof(dummy) ); // not needed, but might shut up a warning
AssertMsgImplementationV( false, false, pstrFile, nLine, pMsg, dummy );
}
void AssertMsgHelper<true,false>::AssertFailed( const char* pstrFile, unsigned int nLine, const char *pFmt, ... )
{
va_list ap;
va_start( ap, pFmt );
AssertMsgImplementationV( true, true, pstrFile, nLine, pFmt, ap );
va_end( ap );
}
void AssertMsgHelper<false,false>::AssertFailed( const char* pstrFile, unsigned int nLine, const char *pFmt, ... )
{
va_list ap;
va_start( ap, pFmt );
AssertMsgImplementationV( false, true, pstrFile, nLine, pFmt, ap );
va_end( ap );
}