blob: d4e4302bb4150490030004872b694b665d7c4930 [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001/*
2 * kmp_debug.c -- debug utilities for the Guide library
3 * $Revision: 42150 $
4 * $Date: 2013-03-15 15:40:38 -0500 (Fri, 15 Mar 2013) $
5 */
6
7
8//===----------------------------------------------------------------------===//
9//
10// The LLVM Compiler Infrastructure
11//
12// This file is dual licensed under the MIT and the University of Illinois Open
13// Source Licenses. See LICENSE.txt for details.
14//
15//===----------------------------------------------------------------------===//
16
17
18#include "kmp.h"
19#include "kmp_debug.h" /* really necessary? */
20#include "kmp_i18n.h"
21#include "kmp_io.h"
22
23#ifdef KMP_DEBUG
24void
25__kmp_debug_printf_stdout( char const * format, ... )
26{
27 va_list ap;
28 va_start( ap, format );
29
30 __kmp_vprintf( kmp_out, format, ap );
31
32 va_end(ap);
33}
34#endif
35
36void
37__kmp_debug_printf( char const * format, ... )
38{
39 va_list ap;
40 va_start( ap, format );
41
42 __kmp_vprintf( kmp_err, format, ap );
43
44 va_end( ap );
45}
46
47#ifdef KMP_USE_ASSERT
48 int
49 __kmp_debug_assert(
50 char const * msg,
51 char const * file,
52 int line
53 ) {
54
55 if ( file == NULL ) {
56 file = KMP_I18N_STR( UnknownFile );
57 } else {
58 // Remove directories from path, leave only file name. File name is enough, there is no need
59 // in bothering developers and customers with full paths.
60 char const * slash = strrchr( file, '/' );
61 if ( slash != NULL ) {
62 file = slash + 1;
63 }; // if
64 }; // if
65
66 #ifdef KMP_DEBUG
67 __kmp_acquire_bootstrap_lock( & __kmp_stdio_lock );
68 __kmp_debug_printf( "Assertion failure at %s(%d): %s.\n", file, line, msg );
69 __kmp_release_bootstrap_lock( & __kmp_stdio_lock );
70 #ifdef USE_ASSERT_BREAK
71 #if KMP_OS_WINDOWS
72 DebugBreak();
73 #endif
74 #endif // USE_ASSERT_BREAK
75 #ifdef USE_ASSERT_STALL
76 /* __kmp_infinite_loop(); */
77 for(;;);
78 #endif // USE_ASSERT_STALL
79 #ifdef USE_ASSERT_SEG
80 {
81 int volatile * ZERO = (int*) 0;
82 ++ (*ZERO);
83 }
84 #endif // USE_ASSERT_SEG
85 #endif
86
87 __kmp_msg(
88 kmp_ms_fatal,
89 KMP_MSG( AssertionFailure, file, line ),
90 KMP_HNT( SubmitBugReport ),
91 __kmp_msg_null
92 );
93
94 return 0;
95
96 } // __kmp_debug_assert
97
98#endif // KMP_USE_ASSERT
99
100/* Dump debugging buffer to stderr */
101void
102__kmp_dump_debug_buffer( void )
103{
104 if ( __kmp_debug_buffer != NULL ) {
105 int i;
106 int dc = __kmp_debug_count;
107 char *db = & __kmp_debug_buffer[ (dc % __kmp_debug_buf_lines) * __kmp_debug_buf_chars ];
108 char *db_end = & __kmp_debug_buffer[ __kmp_debug_buf_lines * __kmp_debug_buf_chars ];
109 char *db2;
110
111 __kmp_acquire_bootstrap_lock( & __kmp_stdio_lock );
112 __kmp_printf_no_lock( "\nStart dump of debugging buffer (entry=%d):\n",
113 dc % __kmp_debug_buf_lines );
114
115 for ( i = 0; i < __kmp_debug_buf_lines; i++ ) {
116
117 if ( *db != '\0' ) {
118 /* Fix up where no carriage return before string termination char */
119 for ( db2 = db + 1; db2 < db + __kmp_debug_buf_chars - 1; db2 ++) {
120 if ( *db2 == '\0' ) {
121 if ( *(db2-1) != '\n' ) { *db2 = '\n'; *(db2+1) = '\0'; }
122 break;
123 }
124 }
125 /* Handle case at end by shortening the printed message by one char if necessary */
126 if ( db2 == db + __kmp_debug_buf_chars - 1 &&
127 *db2 == '\0' && *(db2-1) != '\n' ) {
128 *(db2-1) = '\n';
129 }
130
131 __kmp_printf_no_lock( "%4d: %.*s", i, __kmp_debug_buf_chars, db );
132 *db = '\0'; /* only let it print once! */
133 }
134
135 db += __kmp_debug_buf_chars;
136 if ( db >= db_end )
137 db = __kmp_debug_buffer;
138 }
139
140 __kmp_printf_no_lock( "End dump of debugging buffer (entry=%d).\n\n",
141 ( dc+i-1 ) % __kmp_debug_buf_lines );
142 __kmp_release_bootstrap_lock( & __kmp_stdio_lock );
143 }
144}