Intel® OpenMP* Runtime Library
 All Classes Functions Variables Typedefs Enumerations Enumerator Groups Pages
kmp_debugger.c
1 #if USE_DEBUGGER
2 /*
3  * kmp_debugger.c -- debugger support.
4  */
5 
6 /* <copyright>
7  Copyright (c) 1997-2015 Intel Corporation. All Rights Reserved.
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions
11  are met:
12 
13  * Redistributions of source code must retain the above copyright
14  notice, this list of conditions and the following disclaimer.
15  * Redistributions in binary form must reproduce the above copyright
16  notice, this list of conditions and the following disclaimer in the
17  documentation and/or other materials provided with the distribution.
18  * Neither the name of Intel Corporation nor the names of its
19  contributors may be used to endorse or promote products derived
20  from this software without specific prior written permission.
21 
22  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 </copyright> */
35 
36 #include "kmp.h"
37 #include "kmp_lock.h"
38 #include "kmp_omp.h"
39 #include "kmp_str.h"
40 
41 /*
42  NOTE: All variable names are known to the debugger, do not change!
43 */
44 
45 #ifdef __cplusplus
46  extern "C" {
47  extern kmp_omp_struct_info_t __kmp_omp_debug_struct_info;
48  } // extern "C"
49 #endif // __cplusplus
50 
51 int __kmp_debugging = FALSE; // Boolean whether currently debugging OpenMP RTL.
52 
53 #define offset_and_size_of( structure, field ) \
54  { \
55  offsetof( structure, field ), \
56  sizeof( ( (structure *) NULL)->field ) \
57  }
58 
59 #define offset_and_size_not_available \
60  { -1, -1 }
61 
62 #define addr_and_size_of( var ) \
63  { \
64  (kmp_uint64)( & var ), \
65  sizeof( var ) \
66  }
67 
68 #define nthr_buffer_size 1024
69 static kmp_int32
70 kmp_omp_nthr_info_buffer[ nthr_buffer_size ] =
71  { nthr_buffer_size * sizeof( kmp_int32 ) };
72 
73 /* TODO: Check punctuation for various platforms here */
74 static char func_microtask[] = "__kmp_invoke_microtask";
75 static char func_fork[] = "__kmpc_fork_call";
76 static char func_fork_teams[] = "__kmpc_fork_teams";
77 
78 
79 // Various info about runtime structures: addresses, field offsets, sizes, etc.
80 kmp_omp_struct_info_t
81 __kmp_omp_debug_struct_info = {
82 
83  /* Change this only if you make a fundamental data structure change here */
84  KMP_OMP_VERSION,
85 
86  /* sanity check. Only should be checked if versions are identical
87  * This is also used for backward compatibility to get the runtime
88  * structure size if it the runtime is older than the interface */
89  sizeof( kmp_omp_struct_info_t ),
90 
91  /* OpenMP RTL version info. */
92  addr_and_size_of( __kmp_version_major ),
93  addr_and_size_of( __kmp_version_minor ),
94  addr_and_size_of( __kmp_version_build ),
95  addr_and_size_of( __kmp_openmp_version ),
96  { (kmp_uint64)( __kmp_copyright ) + KMP_VERSION_MAGIC_LEN, 0 }, // Skip magic prefix.
97 
98  /* Various globals. */
99  addr_and_size_of( __kmp_threads ),
100  addr_and_size_of( __kmp_root ),
101  addr_and_size_of( __kmp_threads_capacity ),
102  addr_and_size_of( __kmp_monitor ),
103 #if ! KMP_USE_DYNAMIC_LOCK
104  addr_and_size_of( __kmp_user_lock_table ),
105 #endif
106  addr_and_size_of( func_microtask ),
107  addr_and_size_of( func_fork ),
108  addr_and_size_of( func_fork_teams ),
109  addr_and_size_of( __kmp_team_counter ),
110  addr_and_size_of( __kmp_task_counter ),
111  addr_and_size_of( kmp_omp_nthr_info_buffer ),
112  sizeof( void * ),
113  OMP_LOCK_T_SIZE < sizeof(void *),
114  bs_last_barrier,
115  TASK_DEQUE_SIZE,
116 
117  // thread structure information
118  sizeof( kmp_base_info_t ),
119  offset_and_size_of( kmp_base_info_t, th_info ),
120  offset_and_size_of( kmp_base_info_t, th_team ),
121  offset_and_size_of( kmp_base_info_t, th_root ),
122  offset_and_size_of( kmp_base_info_t, th_serial_team ),
123  offset_and_size_of( kmp_base_info_t, th_ident ),
124  offset_and_size_of( kmp_base_info_t, th_spin_here ),
125  offset_and_size_of( kmp_base_info_t, th_next_waiting ),
126  offset_and_size_of( kmp_base_info_t, th_task_team ),
127  offset_and_size_of( kmp_base_info_t, th_current_task ),
128  offset_and_size_of( kmp_base_info_t, th_task_state ),
129  offset_and_size_of( kmp_base_info_t, th_bar ),
130  offset_and_size_of( kmp_bstate_t, b_worker_arrived ),
131 
132  // teams information
133  offset_and_size_of( kmp_base_info_t, th_teams_microtask),
134  offset_and_size_of( kmp_base_info_t, th_teams_level),
135  offset_and_size_of( kmp_teams_size_t, nteams ),
136  offset_and_size_of( kmp_teams_size_t, nth ),
137 
138  // kmp_desc structure (for info field above)
139  sizeof( kmp_desc_base_t ),
140  offset_and_size_of( kmp_desc_base_t, ds_tid ),
141  offset_and_size_of( kmp_desc_base_t, ds_gtid ),
142  // On Windows* OS, ds_thread contains a thread /handle/, which is not usable, while thread /id/
143  // is in ds_thread_id.
144  #if KMP_OS_WINDOWS
145  offset_and_size_of( kmp_desc_base_t, ds_thread_id),
146  #else
147  offset_and_size_of( kmp_desc_base_t, ds_thread),
148  #endif
149 
150  // team structure information
151  sizeof( kmp_base_team_t ),
152  offset_and_size_of( kmp_base_team_t, t_master_tid ),
153  offset_and_size_of( kmp_base_team_t, t_ident ),
154  offset_and_size_of( kmp_base_team_t, t_parent ),
155  offset_and_size_of( kmp_base_team_t, t_nproc ),
156  offset_and_size_of( kmp_base_team_t, t_threads ),
157  offset_and_size_of( kmp_base_team_t, t_serialized ),
158  offset_and_size_of( kmp_base_team_t, t_id ),
159  offset_and_size_of( kmp_base_team_t, t_pkfn ),
160  offset_and_size_of( kmp_base_team_t, t_task_team ),
161  offset_and_size_of( kmp_base_team_t, t_implicit_task_taskdata ),
162  offset_and_size_of( kmp_base_team_t, t_cancel_request ),
163  offset_and_size_of( kmp_base_team_t, t_bar ),
164  offset_and_size_of( kmp_balign_team_t, b_master_arrived ),
165  offset_and_size_of( kmp_balign_team_t, b_team_arrived ),
166 
167  // root structure information
168  sizeof( kmp_base_root_t ),
169  offset_and_size_of( kmp_base_root_t, r_root_team ),
170  offset_and_size_of( kmp_base_root_t, r_hot_team ),
171  offset_and_size_of( kmp_base_root_t, r_uber_thread ),
172  offset_and_size_not_available,
173 
174  // ident structure information
175  sizeof( ident_t ),
176  offset_and_size_of( ident_t, psource ),
177  offset_and_size_of( ident_t, flags ),
178 
179  // lock structure information
180  sizeof( kmp_base_queuing_lock_t ),
181  offset_and_size_of( kmp_base_queuing_lock_t, initialized ),
182  offset_and_size_of( kmp_base_queuing_lock_t, location ),
183  offset_and_size_of( kmp_base_queuing_lock_t, tail_id ),
184  offset_and_size_of( kmp_base_queuing_lock_t, head_id ),
185  offset_and_size_of( kmp_base_queuing_lock_t, next_ticket ),
186  offset_and_size_of( kmp_base_queuing_lock_t, now_serving ),
187  offset_and_size_of( kmp_base_queuing_lock_t, owner_id ),
188  offset_and_size_of( kmp_base_queuing_lock_t, depth_locked ),
189  offset_and_size_of( kmp_base_queuing_lock_t, flags ),
190 
191 #if ! KMP_USE_DYNAMIC_LOCK
192  /* Lock table. */
193  sizeof( kmp_lock_table_t ),
194  offset_and_size_of( kmp_lock_table_t, used ),
195  offset_and_size_of( kmp_lock_table_t, allocated ),
196  offset_and_size_of( kmp_lock_table_t, table ),
197 #endif
198 
199  // Task team structure information.
200  sizeof( kmp_base_task_team_t ),
201  offset_and_size_of( kmp_base_task_team_t, tt_threads_data ),
202  offset_and_size_of( kmp_base_task_team_t, tt_found_tasks ),
203  offset_and_size_of( kmp_base_task_team_t, tt_nproc ),
204  offset_and_size_of( kmp_base_task_team_t, tt_unfinished_threads ),
205  offset_and_size_of( kmp_base_task_team_t, tt_active ),
206 
207  // task_data_t.
208  sizeof( kmp_taskdata_t ),
209  offset_and_size_of( kmp_taskdata_t, td_task_id ),
210  offset_and_size_of( kmp_taskdata_t, td_flags ),
211  offset_and_size_of( kmp_taskdata_t, td_team ),
212  offset_and_size_of( kmp_taskdata_t, td_parent ),
213  offset_and_size_of( kmp_taskdata_t, td_level ),
214  offset_and_size_of( kmp_taskdata_t, td_ident ),
215  offset_and_size_of( kmp_taskdata_t, td_allocated_child_tasks ),
216  offset_and_size_of( kmp_taskdata_t, td_incomplete_child_tasks ),
217 
218  offset_and_size_of( kmp_taskdata_t, td_taskwait_ident ),
219  offset_and_size_of( kmp_taskdata_t, td_taskwait_counter ),
220  offset_and_size_of( kmp_taskdata_t, td_taskwait_thread ),
221 
222  offset_and_size_of( kmp_taskdata_t, td_taskgroup ),
223  offset_and_size_of( kmp_taskgroup_t, count ),
224  offset_and_size_of( kmp_taskgroup_t, cancel_request ),
225 
226  offset_and_size_of( kmp_taskdata_t, td_depnode ),
227  offset_and_size_of( kmp_depnode_list_t, node ),
228  offset_and_size_of( kmp_depnode_list_t, next ),
229  offset_and_size_of( kmp_base_depnode_t, successors ),
230  offset_and_size_of( kmp_base_depnode_t, task ),
231  offset_and_size_of( kmp_base_depnode_t, npredecessors ),
232  offset_and_size_of( kmp_base_depnode_t, nrefs ),
233  offset_and_size_of( kmp_task_t, routine ),
234 
235  // thread_data_t.
236  sizeof( kmp_thread_data_t ),
237  offset_and_size_of( kmp_base_thread_data_t, td_deque ),
238  offset_and_size_of( kmp_base_thread_data_t, td_deque_head ),
239  offset_and_size_of( kmp_base_thread_data_t, td_deque_tail ),
240  offset_and_size_of( kmp_base_thread_data_t, td_deque_ntasks ),
241  offset_and_size_of( kmp_base_thread_data_t, td_deque_last_stolen ),
242 
243  // The last field.
244  KMP_OMP_VERSION,
245 
246 }; // __kmp_omp_debug_struct_info
247 
248 #undef offset_and_size_of
249 #undef addr_and_size_of
250 
251 /*
252  Intel compiler on IA-32 architecture issues a warning "conversion
253  from "unsigned long long" to "char *" may lose significant bits"
254  when 64-bit value is assigned to 32-bit pointer. Use this function
255  to suppress the warning.
256 */
257 static inline
258 void *
259 __kmp_convert_to_ptr(
260  kmp_uint64 addr
261 ) {
262  #if KMP_COMPILER_ICC
263  #pragma warning( push )
264  #pragma warning( disable: 810 ) // conversion from "unsigned long long" to "char *" may lose significant bits
265  #pragma warning( disable: 1195 ) // conversion from integer to smaller pointer
266  #endif // KMP_COMPILER_ICC
267  return (void *) addr;
268  #if KMP_COMPILER_ICC
269  #pragma warning( pop )
270  #endif // KMP_COMPILER_ICC
271 } // __kmp_convert_to_ptr
272 
273 
274 static int
275 kmp_location_match(
276  kmp_str_loc_t * loc,
277  kmp_omp_nthr_item_t * item
278 ) {
279 
280  int file_match = 0;
281  int func_match = 0;
282  int line_match = 0;
283 
284  char * file = (char *) __kmp_convert_to_ptr( item->file );
285  char * func = (char *) __kmp_convert_to_ptr( item->func );
286  file_match = __kmp_str_fname_match( & loc->fname, file );
287  func_match =
288  item->func == 0 // If item->func is NULL, it allows any func name.
289  ||
290  strcmp( func, "*" ) == 0
291  ||
292  ( loc->func != NULL && strcmp( loc->func, func ) == 0 );
293  line_match =
294  item->begin <= loc->line
295  &&
296  ( item->end <= 0 || loc->line <= item->end ); // if item->end <= 0, it means "end of file".
297 
298  return ( file_match && func_match && line_match );
299 
300 } // kmp_location_match
301 
302 
303 int
304 __kmp_omp_num_threads(
305  ident_t const * ident
306 ) {
307 
308  int num_threads = 0;
309 
310  kmp_omp_nthr_info_t * info =
311  (kmp_omp_nthr_info_t *) __kmp_convert_to_ptr( __kmp_omp_debug_struct_info.nthr_info.addr );
312  if ( info->num > 0 && info->array != 0 ) {
313  kmp_omp_nthr_item_t * items = (kmp_omp_nthr_item_t *) __kmp_convert_to_ptr( info->array );
314  kmp_str_loc_t loc = __kmp_str_loc_init( ident->psource, 1 );
315  int i;
316  for ( i = 0; i < info->num; ++ i ) {
317  if ( kmp_location_match( & loc, & items[ i ] ) ) {
318  num_threads = items[ i ].num_threads;
319  }; // if
320  }; // for
321  __kmp_str_loc_free( & loc );
322  }; // if
323 
324  return num_threads;;
325 
326 } // __kmp_omp_num_threads
327 #endif /* USE_DEBUGGER */
Definition: kmp.h:218
char const * psource
Definition: kmp.h:227
struct ident ident_t