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