Skip to content

Commit

Permalink
Remove obslete DBG_printf_gcc, all compilers now support DBG_printf_c…
Browse files Browse the repository at this point in the history
…99 (dotnet#20338)
  • Loading branch information
marler8997 authored and janvorli committed Oct 10, 2018
1 parent eaf32b0 commit a6159a6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 233 deletions.
127 changes: 17 additions & 110 deletions src/pal/src/include/pal/dbgmsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,32 +322,20 @@ bool DBG_ShouldCheckStackAlignment();
BOOL __bHeader = bHeader;\
DBG_PRINTF2

#ifdef __GNUC__
#define DBG_PRINTF2(args...)\
DBG_printf_gcc(__chanid,__levid,__bHeader,__FUNCTION__,__FILE__,\
__LINE__,args);\
}\
}
#else /* __GNUC__ */
#define DBG_PRINTF2(...)\
DBG_printf_c99(__chanid,__levid,__bHeader,__FILE__,__LINE__,__VA_ARGS__);\
DBG_printf(__chanid,__levid,__bHeader,__FUNCTION__,__FILE__,__LINE__,__VA_ARGS__);\
}\
}
#endif /* __GNUC__ */

#endif /* _ENABLE_DEBUG_MESSAGES_ */

/* Use GNU C-specific features if available : __FUNCTION__ pseudo-macro,
variable-argument macros */
#ifdef __GNUC__

/* define NOTRACE as nothing; this will absorb the variable-argument list used
in tracing macros */
#define NOTRACE(args...)
#define NOTRACE(...)

#if defined(__cplusplus) && defined(FEATURE_PAL_SXS)
#define __ASSERT_ENTER() \
/* DBG_printf_gcc() and DebugBreak() need a PAL thread */ \
/* DBG_printf_c99() and DebugBreak() need a PAL thread */ \
PAL_EnterHolder __holder(PALIsThreadDataInitialized() && \
(CorUnix::InternalGetCurrentThread() == NULL || \
!CorUnix::InternalGetCurrentThread()->IsInPal()));
Expand All @@ -357,49 +345,6 @@ bool DBG_ShouldCheckStackAlignment();

#if !defined(_DEBUG)

#define ASSERT(args...)
#define _ASSERT(expr)
#define _ASSERTE(expr)
#define _ASSERT_MSG(args...)

#else /* defined(_DEBUG) */

#define ASSERT(args...) \
{ \
__ASSERT_ENTER(); \
if (output_file && dbg_master_switch) \
{ \
DBG_printf_gcc(defdbgchan,DLI_ASSERT,TRUE,__FUNCTION__,__FILE__,__LINE__,args); \
} \
if (g_Dbg_asserts_enabled) \
{ \
DebugBreak(); \
} \
}

#define _ASSERT(expr) do { if (!(expr)) { ASSERT(""); } } while(0)
#define _ASSERTE(expr) do { if (!(expr)) { ASSERT("Expression: " #expr "\n"); } } while(0)
#define _ASSERT_MSG(expr, args...) \
do { \
if (!(expr)) \
{ \
ASSERT("Expression: " #expr ", Description: " args); \
} \
} while(0)

#endif /* defined(_DEBUG) */

#else /* __GNUC__ */
/* Not GNU C : C99 [the latest version of the ISO C Standard] specifies
a different syntax for variable-argument macros, so try using that*/
#if defined __STDC_VERSION__ && __STDC_VERSION__ >=199901L

/* define NOTRACE as nothing; this will absorb the variable-argument list used
in tracing macros */
#define NOTRACE(...)

#if !defined(_DEBUG)

#define ASSERT(...)
#define _ASSERT(expr)
#define _ASSERTE(expr)
Expand All @@ -412,11 +357,10 @@ bool DBG_ShouldCheckStackAlignment();
__ASSERT_ENTER(); \
if (output_file && dbg_master_switch) \
{ \
DBG_printf_c99(defdbgchan,DLI_ASSERT,TRUE,__FILE__,__LINE__,__VA_ARGS__); \
DBG_printf(defdbgchan,DLI_ASSERT,TRUE,__FUNCTION__,__FILE__,__LINE__,__VA_ARGS__); \
} \
if(g_Dbg_asserts_enabled) \
{ \
PAL_Leave(); \
DebugBreak(); \
} \
}
Expand All @@ -433,17 +377,6 @@ bool DBG_ShouldCheckStackAlignment();

#endif /* !_DEBUG */

#else /* __STDC_VERSION__ */
/* Not GNU C, not C99 :
possible work around for the lack of variable-argument macros:
by using 2 function calls; must wrap the whole thing in a critical
section to avoid interleaved output from multiple threads */

#error The compiler is missing support for variable-argument macros.

#endif /* __STDC_VERSION__*/
#endif /* __GNUC__ */

/* Function declarations */

/*++
Expand Down Expand Up @@ -496,66 +429,40 @@ BOOL DBG_preprintf(DBG_CHANNEL_ID channel, DBG_LEVEL_ID level, BOOL bHeader,

/*++
Function :
DBG_printf_gcc
DBG_printf
Internal function for debug channels; don't use.
This function outputs a complete debug message, including the function name.
This function outputs a complete debug message, without function name.
Parameters :
DBG_CHANNEL_ID channel : debug channel to use
DBG_LEVEL_ID level : debug message level
BOOL bHeader : whether or not to output message header (thread id, etc)
LPSTR function : current function
LPSTR file : current file
LPCSTR function : current function
LPCSTR file : current file
INT line : line number
LPSTR format, ... : standard printf parameter list.
LPCSTR format, ... : standard printf parameter list.
Return Value :
always 1.
Notes :
This version is for gnu compilers that support variable-argument macros
and the __FUNCTION__ pseudo-macro.
This function requires that the compiler support the C99 flavor of
variable-argument macros, and that they support the __FUNCTION__
pseudo-macro.
--*/
#if __GNUC__ && CHECK_TRACE_SPECIFIERS
/* if requested, use an __attribute__ feature to ask gcc to check that format
specifiers match their parameters */
int DBG_printf_gcc(DBG_CHANNEL_ID channel, DBG_LEVEL_ID level, BOOL bHeader,
LPCSTR function, LPCSTR file, INT line, LPCSTR format, ...)
__attribute__ ((format (printf,7, 8)));
int DBG_printf(DBG_CHANNEL_ID channel, DBG_LEVEL_ID level, BOOL bHeader,
LPCSTR function, LPCSTR file, INT line, LPCSTR format, ...)
__attribute__ ((format (printf,7, 8)));
#else
int DBG_printf_gcc(DBG_CHANNEL_ID channel, DBG_LEVEL_ID level, BOOL bHeader,
LPCSTR function, LPCSTR file, INT line, LPCSTR format, ...);
int DBG_printf(DBG_CHANNEL_ID channel, DBG_LEVEL_ID level, BOOL bHeader,
LPCSTR function, LPCSTR file, INT line, LPCSTR format, ...);
#endif

/*++
Function :
DBG_printf_c99
Internal function for debug channels; don't use.
This function outputs a complete debug message, without function name.
Parameters :
DBG_CHANNEL_ID channel : debug channel to use
DBG_LEVEL_ID level : debug message level
BOOL bHeader : whether or not to output message header (thread id, etc)
LPSTR file : current file
INT line : line number
LPSTR format, ... : standard printf parameter list.
Return Value :
always 1.
Notes :
This version is for compilers that support the C99 flavor of
variable-argument macros but not the gnu flavor, and do not support the
__FUNCTION__ pseudo-macro.
--*/
int DBG_printf_c99(DBG_CHANNEL_ID channel, DBG_LEVEL_ID level, BOOL bHeader,
LPSTR file, INT line, LPSTR format, ...);

/*++
Function :
DBG_printf_plain
Expand Down
138 changes: 15 additions & 123 deletions src/pal/src/misc/dbgmsg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,9 @@ static const void *DBG_get_module_id()
#define MODULE_FORMAT
#endif // FEATURE_PAL_SXS


/*++
Function :
DBG_printf_gcc
DBG_printf
Internal function for debug channels; don't use.
This function outputs a complete debug message, including the function name.
Expand All @@ -454,21 +453,22 @@ Parameters :
DBG_CHANNEL_ID channel : debug channel to use
DBG_LEVEL_ID level : debug message level
BOOL bHeader : whether or not to output message header (thread id, etc)
LPSTR function : current function
LPSTR file : current file
LPCSTR function : current function
LPCSTR file : current file
INT line : line number
LPSTR format, ... : standard printf parameter list.
LPCSTR format, ... : standard printf parameter list.
Return Value :
always 1.
Notes :
This version is for gnu compilers that support variable-argument macros
and the __FUNCTION__ pseudo-macro.
This version is for compilers that support the C99 flavor of
variable-argument macros but not the gnu flavor, and do not support the
__FUNCTION__ pseudo-macro.
--*/
int DBG_printf_gcc(DBG_CHANNEL_ID channel, DBG_LEVEL_ID level, BOOL bHeader,
LPCSTR function, LPCSTR file, INT line, LPCSTR format, ...)
int DBG_printf(DBG_CHANNEL_ID channel, DBG_LEVEL_ID level, BOOL bHeader,
LPCSTR function, LPCSTR file, INT line, LPCSTR format, ...)
{
CHAR *buffer = (CHAR*)alloca(DBG_BUFFER_SIZE);
CHAR indent[MAX_NESTING+1];
Expand All @@ -494,7 +494,7 @@ int DBG_printf_gcc(DBG_CHANNEL_ID channel, DBG_LEVEL_ID level, BOOL bHeader,
/* also print file name for ASSERTs, to match Win32 behavior */
if( DLI_ENTRY == level || DLI_ASSERT == level || DLI_EXIT == level)
{
output_size=snprintf(buffer, DBG_BUFFER_SIZE,
output_size=snprintf(buffer, DBG_BUFFER_SIZE,
"{%p" MODULE_FORMAT "} %-5s [%-7s] at %s.%d: ",
thread_id, MODULE_ID
dbg_level_names[level], dbg_channel_names[channel], file, line);
Expand All @@ -506,10 +506,10 @@ int DBG_printf_gcc(DBG_CHANNEL_ID channel, DBG_LEVEL_ID level, BOOL bHeader,
thread_id, MODULE_ID
dbg_level_names[level], dbg_channel_names[channel], function, line);
}

if(output_size + 1 > DBG_BUFFER_SIZE)
{
fprintf(stderr, "ERROR : buffer overflow in DBG_printf_gcc");
fprintf(stderr, "ERROR : buffer overflow in DBG_printf");
return 1;
}

Expand All @@ -529,7 +529,7 @@ int DBG_printf_gcc(DBG_CHANNEL_ID channel, DBG_LEVEL_ID level, BOOL bHeader,

if( output_size > DBG_BUFFER_SIZE )
{
fprintf(stderr, "ERROR : buffer overflow in DBG_printf_gcc");
fprintf(stderr, "ERROR : buffer overflow in DBG_printf");
}

/* Use a Critical section before calling printf code to
Expand All @@ -543,7 +543,7 @@ int DBG_printf_gcc(DBG_CHANNEL_ID channel, DBG_LEVEL_ID level, BOOL bHeader,
/* flush the output to file */
if ( fflush(output_file) != 0 )
{
fprintf(stderr, "ERROR : fflush() failed errno:%d (%s)\n",
fprintf(stderr, "ERROR : fflush() failed errno:%d (%s)\n",
errno, strerror(errno));
}

Expand All @@ -553,115 +553,7 @@ int DBG_printf_gcc(DBG_CHANNEL_ID channel, DBG_LEVEL_ID level, BOOL bHeader,

if ( old_errno != errno )
{
fprintf( stderr,"ERROR: errno changed by DBG_printf_gcc\n" );
errno = old_errno;
}

return 1;
}

/*++
Function :
DBG_printf_c99
Internal function for debug channels; don't use.
This function outputs a complete debug message, without function name.
Parameters :
DBG_CHANNEL_ID channel : debug channel to use
DBG_LEVEL_ID level : debug message level
BOOL bHeader : whether or not to output message header (thread id, etc)
LPSTR file : current file
INT line : line number
LPSTR format, ... : standard printf parameter list.
Return Value :
always 1.
Notes :
This version is for compilers that support the C99 flavor of
variable-argument macros but not the gnu flavor, and do not support the
__FUNCTION__ pseudo-macro.
--*/
int DBG_printf_c99(DBG_CHANNEL_ID channel, DBG_LEVEL_ID level, BOOL bHeader,
LPSTR file, INT line, LPSTR format, ...)
{
CHAR *buffer = (CHAR*)alloca(DBG_BUFFER_SIZE);
CHAR indent[MAX_NESTING+1];
LPSTR buffer_ptr;
INT output_size;
va_list args;
static INT call_count=0; // only use inside the crit section
void *thread_id;
int old_errno = 0;

old_errno = errno;

if(!DBG_get_indent(level, format, indent))
{
return 1;
}

thread_id= (void *)THREADSilentGetCurrentThreadId();

if(bHeader)
{
output_size=snprintf(buffer, DBG_BUFFER_SIZE,
"{%p" MODULE_FORMAT "} %-5s [%-7s] at %s.%d: ", thread_id, MODULE_ID
dbg_level_names[level], dbg_channel_names[channel],
file, line);

if(output_size + 1 > DBG_BUFFER_SIZE)
{
fprintf(stderr, "ERROR : buffer overflow in DBG_printf_gcc");
return 1;
}

buffer_ptr=buffer+output_size;
}
else
{
output_size = 0;
buffer_ptr = buffer;
}

va_start(args, format);
output_size+=_vsnprintf_s(buffer_ptr, DBG_BUFFER_SIZE-output_size, _TRUNCATE,
format, args);
va_end(args);

if(output_size>DBG_BUFFER_SIZE)
fprintf(stderr, "ERROR : buffer overflow in DBG_printf_c99");

/* Use a Critical section before calling printf code to
avoid holding a libc lock while another thread is calling
SuspendThread on this one. */

BOOL do_flush = FALSE;
InternalEnterCriticalSection(NULL, &fprintf_crit_section);
fprintf( output_file, "%s", buffer );
call_count++; // can use call_count because we are in the crit section
if (call_count>5)
{
call_count = 0;
do_flush = TRUE;
}
InternalLeaveCriticalSection(NULL, &fprintf_crit_section);

/* flush the output to file every once in a while */
if (do_flush)
{
if ( fflush(output_file) != 0 )
{
fprintf(stderr, "ERROR : fflush() failed errno:%d (%s)\n",
errno, strerror(errno));
}
}

if ( old_errno != errno )
{
fprintf( stderr, "ERROR: DBG_printf_c99 changed the errno.\n" );
fprintf( stderr,"ERROR: errno changed by DBG_printf\n" );
errno = old_errno;
}

Expand Down

0 comments on commit a6159a6

Please sign in to comment.