LLVM OpenMP* Runtime Library
kmp_debugger.cpp
1 #include "kmp_config.h"
2 
3 #if USE_DEBUGGER
4 /*
5  * kmp_debugger.cpp -- debugger support.
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 #include "kmp.h"
18 #include "kmp_lock.h"
19 #include "kmp_omp.h"
20 #include "kmp_str.h"
21 
22 // NOTE: All variable names are known to the debugger, do not change!
23 
24 #ifdef __cplusplus
25 extern "C" {
26 extern kmp_omp_struct_info_t __kmp_omp_debug_struct_info;
27 } // extern "C"
28 #endif // __cplusplus
29 
30 int __kmp_debugging = FALSE; // Boolean whether currently debugging OpenMP RTL.
31 
32 #define offset_and_size_of(structure, field) \
33  { offsetof(structure, field), sizeof(((structure *)NULL)->field) }
34 
35 #define offset_and_size_not_available \
36  { -1, -1 }
37 
38 #define addr_and_size_of(var) \
39  { (kmp_uint64)(&var), sizeof(var) }
40 
41 #define nthr_buffer_size 1024
42 static kmp_int32 kmp_omp_nthr_info_buffer[nthr_buffer_size] = {
43  nthr_buffer_size * sizeof(kmp_int32)};
44 
45 /* TODO: Check punctuation for various platforms here */
46 static char func_microtask[] = "__kmp_invoke_microtask";
47 static char func_fork[] = "__kmpc_fork_call";
48 static char func_fork_teams[] = "__kmpc_fork_teams";
49 
50 // Various info about runtime structures: addresses, field offsets, sizes, etc.
51 kmp_omp_struct_info_t __kmp_omp_debug_struct_info = {
52 
53  /* Change this only if you make a fundamental data structure change here */
54  KMP_OMP_VERSION,
55 
56  /* sanity check. Only should be checked if versions are identical
57  * This is also used for backward compatibility to get the runtime
58  * structure size if it the runtime is older than the interface */
59  sizeof(kmp_omp_struct_info_t),
60 
61  /* OpenMP RTL version info. */
62  addr_and_size_of(__kmp_version_major),
63  addr_and_size_of(__kmp_version_minor),
64  addr_and_size_of(__kmp_version_build),
65  addr_and_size_of(__kmp_openmp_version),
66  {(kmp_uint64)(__kmp_copyright) + KMP_VERSION_MAGIC_LEN,
67  0}, // Skip magic prefix.
68 
69  /* Various globals. */
70  addr_and_size_of(__kmp_threads),
71  addr_and_size_of(__kmp_root),
72  addr_and_size_of(__kmp_threads_capacity),
73 #if KMP_USE_MONITOR
74  addr_and_size_of(__kmp_monitor),
75 #endif
76 #if !KMP_USE_DYNAMIC_LOCK
77  addr_and_size_of(__kmp_user_lock_table),
78 #endif
79  addr_and_size_of(func_microtask),
80  addr_and_size_of(func_fork),
81  addr_and_size_of(func_fork_teams),
82  addr_and_size_of(__kmp_team_counter),
83  addr_and_size_of(__kmp_task_counter),
84  addr_and_size_of(kmp_omp_nthr_info_buffer),
85  sizeof(void *),
86  OMP_LOCK_T_SIZE < sizeof(void *),
87  bs_last_barrier,
88  INITIAL_TASK_DEQUE_SIZE,
89 
90  // thread structure information
91  sizeof(kmp_base_info_t),
92  offset_and_size_of(kmp_base_info_t, th_info),
93  offset_and_size_of(kmp_base_info_t, th_team),
94  offset_and_size_of(kmp_base_info_t, th_root),
95  offset_and_size_of(kmp_base_info_t, th_serial_team),
96  offset_and_size_of(kmp_base_info_t, th_ident),
97  offset_and_size_of(kmp_base_info_t, th_spin_here),
98  offset_and_size_of(kmp_base_info_t, th_next_waiting),
99  offset_and_size_of(kmp_base_info_t, th_task_team),
100  offset_and_size_of(kmp_base_info_t, th_current_task),
101  offset_and_size_of(kmp_base_info_t, th_task_state),
102  offset_and_size_of(kmp_base_info_t, th_bar),
103  offset_and_size_of(kmp_bstate_t, b_worker_arrived),
104 
105 #if OMP_40_ENABLED
106  // teams information
107  offset_and_size_of(kmp_base_info_t, th_teams_microtask),
108  offset_and_size_of(kmp_base_info_t, th_teams_level),
109  offset_and_size_of(kmp_teams_size_t, nteams),
110  offset_and_size_of(kmp_teams_size_t, nth),
111 #endif
112 
113  // kmp_desc structure (for info field above)
114  sizeof(kmp_desc_base_t),
115  offset_and_size_of(kmp_desc_base_t, ds_tid),
116  offset_and_size_of(kmp_desc_base_t, ds_gtid),
117 // On Windows* OS, ds_thread contains a thread /handle/, which is not usable,
118 // while thread /id/ is in ds_thread_id.
119 #if KMP_OS_WINDOWS
120  offset_and_size_of(kmp_desc_base_t, ds_thread_id),
121 #else
122  offset_and_size_of(kmp_desc_base_t, ds_thread),
123 #endif
124 
125  // team structure information
126  sizeof(kmp_base_team_t),
127  offset_and_size_of(kmp_base_team_t, t_master_tid),
128  offset_and_size_of(kmp_base_team_t, t_ident),
129  offset_and_size_of(kmp_base_team_t, t_parent),
130  offset_and_size_of(kmp_base_team_t, t_nproc),
131  offset_and_size_of(kmp_base_team_t, t_threads),
132  offset_and_size_of(kmp_base_team_t, t_serialized),
133  offset_and_size_of(kmp_base_team_t, t_id),
134  offset_and_size_of(kmp_base_team_t, t_pkfn),
135  offset_and_size_of(kmp_base_team_t, t_task_team),
136  offset_and_size_of(kmp_base_team_t, t_implicit_task_taskdata),
137 #if OMP_40_ENABLED
138  offset_and_size_of(kmp_base_team_t, t_cancel_request),
139 #endif
140  offset_and_size_of(kmp_base_team_t, t_bar),
141  offset_and_size_of(kmp_balign_team_t, b_master_arrived),
142  offset_and_size_of(kmp_balign_team_t, b_team_arrived),
143 
144  // root structure information
145  sizeof(kmp_base_root_t),
146  offset_and_size_of(kmp_base_root_t, r_root_team),
147  offset_and_size_of(kmp_base_root_t, r_hot_team),
148  offset_and_size_of(kmp_base_root_t, r_uber_thread),
149  offset_and_size_not_available,
150 
151  // ident structure information
152  sizeof(ident_t),
153  offset_and_size_of(ident_t, psource),
154  offset_and_size_of(ident_t, flags),
155 
156  // lock structure information
157  sizeof(kmp_base_queuing_lock_t),
158  offset_and_size_of(kmp_base_queuing_lock_t, initialized),
159  offset_and_size_of(kmp_base_queuing_lock_t, location),
160  offset_and_size_of(kmp_base_queuing_lock_t, tail_id),
161  offset_and_size_of(kmp_base_queuing_lock_t, head_id),
162  offset_and_size_of(kmp_base_queuing_lock_t, next_ticket),
163  offset_and_size_of(kmp_base_queuing_lock_t, now_serving),
164  offset_and_size_of(kmp_base_queuing_lock_t, owner_id),
165  offset_and_size_of(kmp_base_queuing_lock_t, depth_locked),
166  offset_and_size_of(kmp_base_queuing_lock_t, flags),
167 
168 #if !KMP_USE_DYNAMIC_LOCK
169  /* Lock table. */
170  sizeof(kmp_lock_table_t),
171  offset_and_size_of(kmp_lock_table_t, used),
172  offset_and_size_of(kmp_lock_table_t, allocated),
173  offset_and_size_of(kmp_lock_table_t, table),
174 #endif
175 
176  // Task team structure information.
177  sizeof(kmp_base_task_team_t),
178  offset_and_size_of(kmp_base_task_team_t, tt_threads_data),
179  offset_and_size_of(kmp_base_task_team_t, tt_found_tasks),
180  offset_and_size_of(kmp_base_task_team_t, tt_nproc),
181  offset_and_size_of(kmp_base_task_team_t, tt_unfinished_threads),
182  offset_and_size_of(kmp_base_task_team_t, tt_active),
183 
184  // task_data_t.
185  sizeof(kmp_taskdata_t),
186  offset_and_size_of(kmp_taskdata_t, td_task_id),
187  offset_and_size_of(kmp_taskdata_t, td_flags),
188  offset_and_size_of(kmp_taskdata_t, td_team),
189  offset_and_size_of(kmp_taskdata_t, td_parent),
190  offset_and_size_of(kmp_taskdata_t, td_level),
191  offset_and_size_of(kmp_taskdata_t, td_ident),
192  offset_and_size_of(kmp_taskdata_t, td_allocated_child_tasks),
193  offset_and_size_of(kmp_taskdata_t, td_incomplete_child_tasks),
194 
195  offset_and_size_of(kmp_taskdata_t, td_taskwait_ident),
196  offset_and_size_of(kmp_taskdata_t, td_taskwait_counter),
197  offset_and_size_of(kmp_taskdata_t, td_taskwait_thread),
198 
199 #if OMP_40_ENABLED
200  offset_and_size_of(kmp_taskdata_t, td_taskgroup),
201  offset_and_size_of(kmp_taskgroup_t, count),
202  offset_and_size_of(kmp_taskgroup_t, cancel_request),
203 
204  offset_and_size_of(kmp_taskdata_t, td_depnode),
205  offset_and_size_of(kmp_depnode_list_t, node),
206  offset_and_size_of(kmp_depnode_list_t, next),
207  offset_and_size_of(kmp_base_depnode_t, successors),
208  offset_and_size_of(kmp_base_depnode_t, task),
209  offset_and_size_of(kmp_base_depnode_t, npredecessors),
210  offset_and_size_of(kmp_base_depnode_t, nrefs),
211 #endif
212  offset_and_size_of(kmp_task_t, routine),
213 
214  // thread_data_t.
215  sizeof(kmp_thread_data_t),
216  offset_and_size_of(kmp_base_thread_data_t, td_deque),
217  offset_and_size_of(kmp_base_thread_data_t, td_deque_size),
218  offset_and_size_of(kmp_base_thread_data_t, td_deque_head),
219  offset_and_size_of(kmp_base_thread_data_t, td_deque_tail),
220  offset_and_size_of(kmp_base_thread_data_t, td_deque_ntasks),
221  offset_and_size_of(kmp_base_thread_data_t, td_deque_last_stolen),
222 
223  // The last field.
224  KMP_OMP_VERSION,
225 
226 }; // __kmp_omp_debug_struct_info
227 
228 #undef offset_and_size_of
229 #undef addr_and_size_of
230 
231 /* Intel compiler on IA-32 architecture issues a warning "conversion
232  from "unsigned long long" to "char *" may lose significant bits"
233  when 64-bit value is assigned to 32-bit pointer. Use this function
234  to suppress the warning. */
235 static inline void *__kmp_convert_to_ptr(kmp_uint64 addr) {
236 #if KMP_COMPILER_ICC
237 #pragma warning(push)
238 #pragma warning(disable : 810) // conversion from "unsigned long long" to "char
239 // *" may lose significant bits
240 #pragma warning(disable : 1195) // conversion from integer to smaller pointer
241 #endif // KMP_COMPILER_ICC
242  return (void *)addr;
243 #if KMP_COMPILER_ICC
244 #pragma warning(pop)
245 #endif // KMP_COMPILER_ICC
246 } // __kmp_convert_to_ptr
247 
248 static int kmp_location_match(kmp_str_loc_t *loc, kmp_omp_nthr_item_t *item) {
249 
250  int file_match = 0;
251  int func_match = 0;
252  int line_match = 0;
253 
254  char *file = (char *)__kmp_convert_to_ptr(item->file);
255  char *func = (char *)__kmp_convert_to_ptr(item->func);
256  file_match = __kmp_str_fname_match(&loc->fname, file);
257  func_match =
258  item->func == 0 // If item->func is NULL, it allows any func name.
259  || strcmp(func, "*") == 0 ||
260  (loc->func != NULL && strcmp(loc->func, func) == 0);
261  line_match =
262  item->begin <= loc->line &&
263  (item->end <= 0 ||
264  loc->line <= item->end); // if item->end <= 0, it means "end of file".
265 
266  return (file_match && func_match && line_match);
267 
268 } // kmp_location_match
269 
270 int __kmp_omp_num_threads(ident_t const *ident) {
271 
272  int num_threads = 0;
273 
274  kmp_omp_nthr_info_t *info = (kmp_omp_nthr_info_t *)__kmp_convert_to_ptr(
275  __kmp_omp_debug_struct_info.nthr_info.addr);
276  if (info->num > 0 && info->array != 0) {
277  kmp_omp_nthr_item_t *items =
278  (kmp_omp_nthr_item_t *)__kmp_convert_to_ptr(info->array);
279  kmp_str_loc_t loc = __kmp_str_loc_init(ident->psource, 1);
280  int i;
281  for (i = 0; i < info->num; ++i) {
282  if (kmp_location_match(&loc, &items[i])) {
283  num_threads = items[i].num_threads;
284  }
285  }
286  __kmp_str_loc_free(&loc);
287  }
288 
289  return num_threads;
290  ;
291 
292 } // __kmp_omp_num_threads
293 #endif /* USE_DEBUGGER */
Definition: kmp.h:224
char const * psource
Definition: kmp.h:234
struct ident ident_t