reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
#include "kmp_config.h"

#if USE_DEBUGGER
/*
 * kmp_debugger.cpp -- debugger support.
 */

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "kmp.h"
#include "kmp_lock.h"
#include "kmp_omp.h"
#include "kmp_str.h"

// NOTE: All variable names are known to the debugger, do not change!

#ifdef __cplusplus
extern "C" {
extern kmp_omp_struct_info_t __kmp_omp_debug_struct_info;
} // extern "C"
#endif // __cplusplus

int __kmp_debugging = FALSE; // Boolean whether currently debugging OpenMP RTL.

#define offset_and_size_of(structure, field)                                   \
  { offsetof(structure, field), sizeof(((structure *)NULL)->field) }

#define offset_and_size_not_available                                          \
  { -1, -1 }

#define addr_and_size_of(var)                                                  \
  { (kmp_uint64)(&var), sizeof(var) }

#define nthr_buffer_size 1024
static kmp_int32 kmp_omp_nthr_info_buffer[nthr_buffer_size] = {
    nthr_buffer_size * sizeof(kmp_int32)};

/* TODO: Check punctuation for various platforms here */
static char func_microtask[] = "__kmp_invoke_microtask";
static char func_fork[] = "__kmpc_fork_call";
static char func_fork_teams[] = "__kmpc_fork_teams";

// Various info about runtime structures: addresses, field offsets, sizes, etc.
kmp_omp_struct_info_t __kmp_omp_debug_struct_info = {

    /* Change this only if you make a fundamental data structure change here */
    KMP_OMP_VERSION,

    /* sanity check.  Only should be checked if versions are identical
     * This is also used for backward compatibility to get the runtime
     * structure size if it the runtime is older than the interface */
    sizeof(kmp_omp_struct_info_t),

    /* OpenMP RTL version info. */
    addr_and_size_of(__kmp_version_major),
    addr_and_size_of(__kmp_version_minor),
    addr_and_size_of(__kmp_version_build),
    addr_and_size_of(__kmp_openmp_version),
    {(kmp_uint64)(__kmp_copyright) + KMP_VERSION_MAGIC_LEN,
     0}, // Skip magic prefix.

    /* Various globals. */
    addr_and_size_of(__kmp_threads),
    addr_and_size_of(__kmp_root),
    addr_and_size_of(__kmp_threads_capacity),
#if KMP_USE_MONITOR
    addr_and_size_of(__kmp_monitor),
#endif
#if !KMP_USE_DYNAMIC_LOCK
    addr_and_size_of(__kmp_user_lock_table),
#endif
    addr_and_size_of(func_microtask),
    addr_and_size_of(func_fork),
    addr_and_size_of(func_fork_teams),
    addr_and_size_of(__kmp_team_counter),
    addr_and_size_of(__kmp_task_counter),
    addr_and_size_of(kmp_omp_nthr_info_buffer),
    sizeof(void *),
    OMP_LOCK_T_SIZE < sizeof(void *),
    bs_last_barrier,
    INITIAL_TASK_DEQUE_SIZE,

    // thread structure information
    sizeof(kmp_base_info_t),
    offset_and_size_of(kmp_base_info_t, th_info),
    offset_and_size_of(kmp_base_info_t, th_team),
    offset_and_size_of(kmp_base_info_t, th_root),
    offset_and_size_of(kmp_base_info_t, th_serial_team),
    offset_and_size_of(kmp_base_info_t, th_ident),
    offset_and_size_of(kmp_base_info_t, th_spin_here),
    offset_and_size_of(kmp_base_info_t, th_next_waiting),
    offset_and_size_of(kmp_base_info_t, th_task_team),
    offset_and_size_of(kmp_base_info_t, th_current_task),
    offset_and_size_of(kmp_base_info_t, th_task_state),
    offset_and_size_of(kmp_base_info_t, th_bar),
    offset_and_size_of(kmp_bstate_t, b_worker_arrived),

    // teams information
    offset_and_size_of(kmp_base_info_t, th_teams_microtask),
    offset_and_size_of(kmp_base_info_t, th_teams_level),
    offset_and_size_of(kmp_teams_size_t, nteams),
    offset_and_size_of(kmp_teams_size_t, nth),

    // kmp_desc structure (for info field above)
    sizeof(kmp_desc_base_t),
    offset_and_size_of(kmp_desc_base_t, ds_tid),
    offset_and_size_of(kmp_desc_base_t, ds_gtid),
// On Windows* OS, ds_thread contains a thread /handle/, which is not usable,
// while thread /id/ is in ds_thread_id.
#if KMP_OS_WINDOWS
    offset_and_size_of(kmp_desc_base_t, ds_thread_id),
#else
    offset_and_size_of(kmp_desc_base_t, ds_thread),
#endif

    // team structure information
    sizeof(kmp_base_team_t),
    offset_and_size_of(kmp_base_team_t, t_master_tid),
    offset_and_size_of(kmp_base_team_t, t_ident),
    offset_and_size_of(kmp_base_team_t, t_parent),
    offset_and_size_of(kmp_base_team_t, t_nproc),
    offset_and_size_of(kmp_base_team_t, t_threads),
    offset_and_size_of(kmp_base_team_t, t_serialized),
    offset_and_size_of(kmp_base_team_t, t_id),
    offset_and_size_of(kmp_base_team_t, t_pkfn),
    offset_and_size_of(kmp_base_team_t, t_task_team),
    offset_and_size_of(kmp_base_team_t, t_implicit_task_taskdata),
    offset_and_size_of(kmp_base_team_t, t_cancel_request),
    offset_and_size_of(kmp_base_team_t, t_bar),
    offset_and_size_of(kmp_balign_team_t, b_master_arrived),
    offset_and_size_of(kmp_balign_team_t, b_team_arrived),

    // root structure information
    sizeof(kmp_base_root_t),
    offset_and_size_of(kmp_base_root_t, r_root_team),
    offset_and_size_of(kmp_base_root_t, r_hot_team),
    offset_and_size_of(kmp_base_root_t, r_uber_thread),
    offset_and_size_not_available,

    // ident structure information
    sizeof(ident_t),
    offset_and_size_of(ident_t, psource),
    offset_and_size_of(ident_t, flags),

    // lock structure information
    sizeof(kmp_base_queuing_lock_t),
    offset_and_size_of(kmp_base_queuing_lock_t, initialized),
    offset_and_size_of(kmp_base_queuing_lock_t, location),
    offset_and_size_of(kmp_base_queuing_lock_t, tail_id),
    offset_and_size_of(kmp_base_queuing_lock_t, head_id),
    offset_and_size_of(kmp_base_queuing_lock_t, next_ticket),
    offset_and_size_of(kmp_base_queuing_lock_t, now_serving),
    offset_and_size_of(kmp_base_queuing_lock_t, owner_id),
    offset_and_size_of(kmp_base_queuing_lock_t, depth_locked),
    offset_and_size_of(kmp_base_queuing_lock_t, flags),

#if !KMP_USE_DYNAMIC_LOCK
    /* Lock table. */
    sizeof(kmp_lock_table_t),
    offset_and_size_of(kmp_lock_table_t, used),
    offset_and_size_of(kmp_lock_table_t, allocated),
    offset_and_size_of(kmp_lock_table_t, table),
#endif

    // Task team structure information.
    sizeof(kmp_base_task_team_t),
    offset_and_size_of(kmp_base_task_team_t, tt_threads_data),
    offset_and_size_of(kmp_base_task_team_t, tt_found_tasks),
    offset_and_size_of(kmp_base_task_team_t, tt_nproc),
    offset_and_size_of(kmp_base_task_team_t, tt_unfinished_threads),
    offset_and_size_of(kmp_base_task_team_t, tt_active),

    // task_data_t.
    sizeof(kmp_taskdata_t),
    offset_and_size_of(kmp_taskdata_t, td_task_id),
    offset_and_size_of(kmp_taskdata_t, td_flags),
    offset_and_size_of(kmp_taskdata_t, td_team),
    offset_and_size_of(kmp_taskdata_t, td_parent),
    offset_and_size_of(kmp_taskdata_t, td_level),
    offset_and_size_of(kmp_taskdata_t, td_ident),
    offset_and_size_of(kmp_taskdata_t, td_allocated_child_tasks),
    offset_and_size_of(kmp_taskdata_t, td_incomplete_child_tasks),

    offset_and_size_of(kmp_taskdata_t, td_taskwait_ident),
    offset_and_size_of(kmp_taskdata_t, td_taskwait_counter),
    offset_and_size_of(kmp_taskdata_t, td_taskwait_thread),

    offset_and_size_of(kmp_taskdata_t, td_taskgroup),
    offset_and_size_of(kmp_taskgroup_t, count),
    offset_and_size_of(kmp_taskgroup_t, cancel_request),

    offset_and_size_of(kmp_taskdata_t, td_depnode),
    offset_and_size_of(kmp_depnode_list_t, node),
    offset_and_size_of(kmp_depnode_list_t, next),
    offset_and_size_of(kmp_base_depnode_t, successors),
    offset_and_size_of(kmp_base_depnode_t, task),
    offset_and_size_of(kmp_base_depnode_t, npredecessors),
    offset_and_size_of(kmp_base_depnode_t, nrefs),
    offset_and_size_of(kmp_task_t, routine),

    // thread_data_t.
    sizeof(kmp_thread_data_t),
    offset_and_size_of(kmp_base_thread_data_t, td_deque),
    offset_and_size_of(kmp_base_thread_data_t, td_deque_size),
    offset_and_size_of(kmp_base_thread_data_t, td_deque_head),
    offset_and_size_of(kmp_base_thread_data_t, td_deque_tail),
    offset_and_size_of(kmp_base_thread_data_t, td_deque_ntasks),
    offset_and_size_of(kmp_base_thread_data_t, td_deque_last_stolen),

    // The last field.
    KMP_OMP_VERSION,

}; // __kmp_omp_debug_struct_info

#undef offset_and_size_of
#undef addr_and_size_of

/* Intel compiler on IA-32 architecture issues a warning "conversion
  from "unsigned long long" to "char *" may lose significant bits"
  when 64-bit value is assigned to 32-bit pointer. Use this function
  to suppress the warning. */
static inline void *__kmp_convert_to_ptr(kmp_uint64 addr) {
#if KMP_COMPILER_ICC
#pragma warning(push)
#pragma warning(disable : 810) // conversion from "unsigned long long" to "char
// *" may lose significant bits
#pragma warning(disable : 1195) // conversion from integer to smaller pointer
#endif // KMP_COMPILER_ICC
  return (void *)addr;
#if KMP_COMPILER_ICC
#pragma warning(pop)
#endif // KMP_COMPILER_ICC
} // __kmp_convert_to_ptr

static int kmp_location_match(kmp_str_loc_t *loc, kmp_omp_nthr_item_t *item) {

  int file_match = 0;
  int func_match = 0;
  int line_match = 0;

  char *file = (char *)__kmp_convert_to_ptr(item->file);
  char *func = (char *)__kmp_convert_to_ptr(item->func);
  file_match = __kmp_str_fname_match(&loc->fname, file);
  func_match =
      item->func == 0 // If item->func is NULL, it allows any func name.
      || strcmp(func, "*") == 0 ||
      (loc->func != NULL && strcmp(loc->func, func) == 0);
  line_match =
      item->begin <= loc->line &&
      (item->end <= 0 ||
       loc->line <= item->end); // if item->end <= 0, it means "end of file".

  return (file_match && func_match && line_match);

} // kmp_location_match

int __kmp_omp_num_threads(ident_t const *ident) {

  int num_threads = 0;

  kmp_omp_nthr_info_t *info = (kmp_omp_nthr_info_t *)__kmp_convert_to_ptr(
      __kmp_omp_debug_struct_info.nthr_info.addr);
  if (info->num > 0 && info->array != 0) {
    kmp_omp_nthr_item_t *items =
        (kmp_omp_nthr_item_t *)__kmp_convert_to_ptr(info->array);
    kmp_str_loc_t loc = __kmp_str_loc_init(ident->psource, 1);
    int i;
    for (i = 0; i < info->num; ++i) {
      if (kmp_location_match(&loc, &items[i])) {
        num_threads = items[i].num_threads;
      }
    }
    __kmp_str_loc_free(&loc);
  }

  return num_threads;
  ;

} // __kmp_omp_num_threads
#endif /* USE_DEBUGGER */