Intel® OpenMP* Runtime Library
 All Classes Functions Variables Typedefs Enumerations Enumerator Modules 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  offset_and_size_of(
143  kmp_desc_base_t,
144  // On Windows* OS, ds_thread contains a thread /handle/, which is not usable, while thread /id/
145  // is in ds_thread_id.
146  #if KMP_OS_WINDOWS
147  ds_thread_id
148  #else
149  ds_thread
150  #endif
151  ),
152 
153  // team structure information
154  sizeof( kmp_base_team_t ),
155  offset_and_size_of( kmp_base_team_t, t_master_tid ),
156  offset_and_size_of( kmp_base_team_t, t_ident ),
157  offset_and_size_of( kmp_base_team_t, t_parent ),
158  offset_and_size_of( kmp_base_team_t, t_nproc ),
159  offset_and_size_of( kmp_base_team_t, t_threads ),
160  offset_and_size_of( kmp_base_team_t, t_serialized ),
161  offset_and_size_of( kmp_base_team_t, t_id ),
162  offset_and_size_of( kmp_base_team_t, t_pkfn ),
163  offset_and_size_of( kmp_base_team_t, t_task_team ),
164  offset_and_size_of( kmp_base_team_t, t_implicit_task_taskdata ),
165  offset_and_size_of( kmp_base_team_t, t_cancel_request ),
166  offset_and_size_of( kmp_base_team_t, t_bar ),
167  offset_and_size_of( kmp_balign_team_t, b_master_arrived ),
168  offset_and_size_of( kmp_balign_team_t, b_team_arrived ),
169 
170  // root structure information
171  sizeof( kmp_base_root_t ),
172  offset_and_size_of( kmp_base_root_t, r_root_team ),
173  offset_and_size_of( kmp_base_root_t, r_hot_team ),
174  offset_and_size_of( kmp_base_root_t, r_uber_thread ),
175  offset_and_size_not_available,
176 
177  // ident structure information
178  sizeof( ident_t ),
179  offset_and_size_of( ident_t, psource ),
180  offset_and_size_of( ident_t, flags ),
181 
182  // lock structure information
183  sizeof( kmp_base_queuing_lock_t ),
184  offset_and_size_of( kmp_base_queuing_lock_t, initialized ),
185  offset_and_size_of( kmp_base_queuing_lock_t, location ),
186  offset_and_size_of( kmp_base_queuing_lock_t, tail_id ),
187  offset_and_size_of( kmp_base_queuing_lock_t, head_id ),
188  offset_and_size_of( kmp_base_queuing_lock_t, next_ticket ),
189  offset_and_size_of( kmp_base_queuing_lock_t, now_serving ),
190  offset_and_size_of( kmp_base_queuing_lock_t, owner_id ),
191  offset_and_size_of( kmp_base_queuing_lock_t, depth_locked ),
192  offset_and_size_of( kmp_base_queuing_lock_t, flags ),
193 
194 #if ! KMP_USE_DYNAMIC_LOCK
195  /* Lock table. */
196  sizeof( kmp_lock_table_t ),
197  offset_and_size_of( kmp_lock_table_t, used ),
198  offset_and_size_of( kmp_lock_table_t, allocated ),
199  offset_and_size_of( kmp_lock_table_t, table ),
200 #endif
201 
202  // Task team structure information.
203  sizeof( kmp_base_task_team_t ),
204  offset_and_size_of( kmp_base_task_team_t, tt_threads_data ),
205  offset_and_size_of( kmp_base_task_team_t, tt_found_tasks ),
206  offset_and_size_of( kmp_base_task_team_t, tt_nproc ),
207  offset_and_size_of( kmp_base_task_team_t, tt_unfinished_threads ),
208  offset_and_size_of( kmp_base_task_team_t, tt_active ),
209 
210  // task_data_t.
211  sizeof( kmp_taskdata_t ),
212  offset_and_size_of( kmp_taskdata_t, td_task_id ),
213  offset_and_size_of( kmp_taskdata_t, td_flags ),
214  offset_and_size_of( kmp_taskdata_t, td_team ),
215  offset_and_size_of( kmp_taskdata_t, td_parent ),
216  offset_and_size_of( kmp_taskdata_t, td_level ),
217  offset_and_size_of( kmp_taskdata_t, td_ident ),
218  offset_and_size_of( kmp_taskdata_t, td_allocated_child_tasks ),
219  offset_and_size_of( kmp_taskdata_t, td_incomplete_child_tasks ),
220 
221  offset_and_size_of( kmp_taskdata_t, td_taskwait_ident ),
222  offset_and_size_of( kmp_taskdata_t, td_taskwait_counter ),
223  offset_and_size_of( kmp_taskdata_t, td_taskwait_thread ),
224 
225  offset_and_size_of( kmp_taskdata_t, td_taskgroup ),
226  offset_and_size_of( kmp_taskgroup_t, count ),
227  offset_and_size_of( kmp_taskgroup_t, cancel_request ),
228 
229  offset_and_size_of( kmp_taskdata_t, td_depnode ),
230  offset_and_size_of( kmp_depnode_list_t, node ),
231  offset_and_size_of( kmp_depnode_list_t, next ),
232  offset_and_size_of( kmp_base_depnode_t, successors ),
233  offset_and_size_of( kmp_base_depnode_t, task ),
234  offset_and_size_of( kmp_base_depnode_t, npredecessors ),
235  offset_and_size_of( kmp_base_depnode_t, nrefs ),
236  offset_and_size_of( kmp_task_t, routine ),
237 
238  // thread_data_t.
239  sizeof( kmp_thread_data_t ),
240  offset_and_size_of( kmp_base_thread_data_t, td_deque ),
241  offset_and_size_of( kmp_base_thread_data_t, td_deque_head ),
242  offset_and_size_of( kmp_base_thread_data_t, td_deque_tail ),
243  offset_and_size_of( kmp_base_thread_data_t, td_deque_ntasks ),
244  offset_and_size_of( kmp_base_thread_data_t, td_deque_last_stolen ),
245 
246  // The last field.
247  KMP_OMP_VERSION,
248 
249 }; // __kmp_omp_debug_struct_info
250 
251 #undef offset_and_size_of
252 #undef addr_and_size_of
253 
254 /*
255  Intel compiler on IA-32 architecture issues a warning "conversion
256  from "unsigned long long" to "char *" may lose significant bits"
257  when 64-bit value is assigned to 32-bit pointer. Use this function
258  to suppress the warning.
259 */
260 static inline
261 void *
262 __kmp_convert_to_ptr(
263  kmp_uint64 addr
264 ) {
265  #if KMP_COMPILER_ICC
266  #pragma warning( push )
267  #pragma warning( disable: 810 ) // conversion from "unsigned long long" to "char *" may lose significant bits
268  #pragma warning( disable: 1195 ) // conversion from integer to smaller pointer
269  #endif // KMP_COMPILER_ICC
270  return (void *) addr;
271  #if KMP_COMPILER_ICC
272  #pragma warning( pop )
273  #endif // KMP_COMPILER_ICC
274 } // __kmp_convert_to_ptr
275 
276 
277 static int
278 kmp_location_match(
279  kmp_str_loc_t * loc,
280  kmp_omp_nthr_item_t * item
281 ) {
282 
283  int file_match = 0;
284  int func_match = 0;
285  int line_match = 0;
286 
287  char * file = (char *) __kmp_convert_to_ptr( item->file );
288  char * func = (char *) __kmp_convert_to_ptr( item->func );
289  file_match = __kmp_str_fname_match( & loc->fname, file );
290  func_match =
291  item->func == 0 // If item->func is NULL, it allows any func name.
292  ||
293  strcmp( func, "*" ) == 0
294  ||
295  ( loc->func != NULL && strcmp( loc->func, func ) == 0 );
296  line_match =
297  item->begin <= loc->line
298  &&
299  ( item->end <= 0 || loc->line <= item->end ); // if item->end <= 0, it means "end of file".
300 
301  return ( file_match && func_match && line_match );
302 
303 } // kmp_location_match
304 
305 
306 int
307 __kmp_omp_num_threads(
308  ident_t const * ident
309 ) {
310 
311  int num_threads = 0;
312 
313  kmp_omp_nthr_info_t * info =
314  (kmp_omp_nthr_info_t *) __kmp_convert_to_ptr( __kmp_omp_debug_struct_info.nthr_info.addr );
315  if ( info->num > 0 && info->array != 0 ) {
316  kmp_omp_nthr_item_t * items = (kmp_omp_nthr_item_t *) __kmp_convert_to_ptr( info->array );
317  kmp_str_loc_t loc = __kmp_str_loc_init( ident->psource, 1 );
318  int i;
319  for ( i = 0; i < info->num; ++ i ) {
320  if ( kmp_location_match( & loc, & items[ i ] ) ) {
321  num_threads = items[ i ].num_threads;
322  }; // if
323  }; // for
324  __kmp_str_loc_free( & loc );
325  }; // if
326 
327  return num_threads;;
328 
329 } // __kmp_omp_num_threads
330 #endif /* USE_DEBUGGER */
Definition: kmp.h:221
char const * psource
Definition: kmp.h:230
struct ident ident_t