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