Intel® OpenMP* Runtime Library
 All Classes Functions Variables Typedefs Enumerations Enumerator Groups Pages
kmp_debug.c
1 /*
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 /* <copyright>
8  Copyright (c) 1997-2013 Intel Corporation. All Rights Reserved.
9 
10  Redistribution and use in source and binary forms, with or without
11  modification, are permitted provided that the following conditions
12  are met:
13 
14  * Redistributions of source code must retain the above copyright
15  notice, this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright
17  notice, this list of conditions and the following disclaimer in the
18  documentation and/or other materials provided with the distribution.
19  * Neither the name of Intel Corporation nor the names of its
20  contributors may be used to endorse or promote products derived
21  from this software without specific prior written permission.
22 
23  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 
35 </copyright> */
36 
37 #include "kmp.h"
38 #include "kmp_debug.h" /* really necessary? */
39 #include "kmp_i18n.h"
40 #include "kmp_io.h"
41 
42 #ifdef KMP_DEBUG
43 void
44 __kmp_debug_printf_stdout( char const * format, ... )
45 {
46  va_list ap;
47  va_start( ap, format );
48 
49  __kmp_vprintf( kmp_out, format, ap );
50 
51  va_end(ap);
52 }
53 #endif
54 
55 void
56 __kmp_debug_printf( char const * format, ... )
57 {
58  va_list ap;
59  va_start( ap, format );
60 
61  __kmp_vprintf( kmp_err, format, ap );
62 
63  va_end( ap );
64 }
65 
66 #ifdef KMP_USE_ASSERT
67  int
68  __kmp_debug_assert(
69  char const * msg,
70  char const * file,
71  int line
72  ) {
73 
74  if ( file == NULL ) {
75  file = KMP_I18N_STR( UnknownFile );
76  } else {
77  // Remove directories from path, leave only file name. File name is enough, there is no need
78  // in bothering developers and customers with full paths.
79  char const * slash = strrchr( file, '/' );
80  if ( slash != NULL ) {
81  file = slash + 1;
82  }; // if
83  }; // if
84 
85  #ifdef KMP_DEBUG
86  __kmp_acquire_bootstrap_lock( & __kmp_stdio_lock );
87  __kmp_debug_printf( "Assertion failure at %s(%d): %s.\n", file, line, msg );
88  __kmp_release_bootstrap_lock( & __kmp_stdio_lock );
89  #ifdef USE_ASSERT_BREAK
90  #if KMP_OS_WINDOWS
91  DebugBreak();
92  #endif
93  #endif // USE_ASSERT_BREAK
94  #ifdef USE_ASSERT_STALL
95  /* __kmp_infinite_loop(); */
96  for(;;);
97  #endif // USE_ASSERT_STALL
98  #ifdef USE_ASSERT_SEG
99  {
100  int volatile * ZERO = (int*) 0;
101  ++ (*ZERO);
102  }
103  #endif // USE_ASSERT_SEG
104  #endif
105 
106  __kmp_msg(
107  kmp_ms_fatal,
108  KMP_MSG( AssertionFailure, file, line ),
109  KMP_HNT( SubmitBugReport ),
110  __kmp_msg_null
111  );
112 
113  return 0;
114 
115  } // __kmp_debug_assert
116 
117 #endif // KMP_USE_ASSERT
118 
119 /* Dump debugging buffer to stderr */
120 void
121 __kmp_dump_debug_buffer( void )
122 {
123  if ( __kmp_debug_buffer != NULL ) {
124  int i;
125  int dc = __kmp_debug_count;
126  char *db = & __kmp_debug_buffer[ (dc % __kmp_debug_buf_lines) * __kmp_debug_buf_chars ];
127  char *db_end = & __kmp_debug_buffer[ __kmp_debug_buf_lines * __kmp_debug_buf_chars ];
128  char *db2;
129 
130  __kmp_acquire_bootstrap_lock( & __kmp_stdio_lock );
131  __kmp_printf_no_lock( "\nStart dump of debugging buffer (entry=%d):\n",
132  dc % __kmp_debug_buf_lines );
133 
134  for ( i = 0; i < __kmp_debug_buf_lines; i++ ) {
135 
136  if ( *db != '\0' ) {
137  /* Fix up where no carriage return before string termination char */
138  for ( db2 = db + 1; db2 < db + __kmp_debug_buf_chars - 1; db2 ++) {
139  if ( *db2 == '\0' ) {
140  if ( *(db2-1) != '\n' ) { *db2 = '\n'; *(db2+1) = '\0'; }
141  break;
142  }
143  }
144  /* Handle case at end by shortening the printed message by one char if necessary */
145  if ( db2 == db + __kmp_debug_buf_chars - 1 &&
146  *db2 == '\0' && *(db2-1) != '\n' ) {
147  *(db2-1) = '\n';
148  }
149 
150  __kmp_printf_no_lock( "%4d: %.*s", i, __kmp_debug_buf_chars, db );
151  *db = '\0'; /* only let it print once! */
152  }
153 
154  db += __kmp_debug_buf_chars;
155  if ( db >= db_end )
156  db = __kmp_debug_buffer;
157  }
158 
159  __kmp_printf_no_lock( "End dump of debugging buffer (entry=%d).\n\n",
160  ( dc+i-1 ) % __kmp_debug_buf_lines );
161  __kmp_release_bootstrap_lock( & __kmp_stdio_lock );
162  }
163 }