Path: blob/main/contrib/llvm-project/openmp/runtime/src/kmp_debugger.cpp
35258 views
#include "kmp_config.h"12#if USE_DEBUGGER3/*4* kmp_debugger.cpp -- debugger support.5*/67//===----------------------------------------------------------------------===//8//9// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.10// See https://llvm.org/LICENSE.txt for license information.11// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception12//13//===----------------------------------------------------------------------===//1415#include "kmp.h"16#include "kmp_lock.h"17#include "kmp_omp.h"18#include "kmp_str.h"1920// NOTE: All variable names are known to the debugger, do not change!2122#ifdef __cplusplus23extern "C" {24extern kmp_omp_struct_info_t __kmp_omp_debug_struct_info;25} // extern "C"26#endif // __cplusplus2728int __kmp_debugging = FALSE; // Boolean whether currently debugging OpenMP RTL.2930#define offset_and_size_of(structure, field) \31{ offsetof(structure, field), sizeof(((structure *)NULL)->field) }3233#define offset_and_size_not_available \34{ -1, -1 }3536#define addr_and_size_of(var) \37{ (kmp_uint64)(&var), sizeof(var) }3839#define nthr_buffer_size 102440static kmp_int32 kmp_omp_nthr_info_buffer[nthr_buffer_size] = {41nthr_buffer_size * sizeof(kmp_int32)};4243/* TODO: Check punctuation for various platforms here */44static char func_microtask[] = "__kmp_invoke_microtask";45static char func_fork[] = "__kmpc_fork_call";46static char func_fork_teams[] = "__kmpc_fork_teams";4748// Various info about runtime structures: addresses, field offsets, sizes, etc.49kmp_omp_struct_info_t __kmp_omp_debug_struct_info = {5051/* Change this only if you make a fundamental data structure change here */52KMP_OMP_VERSION,5354/* sanity check. Only should be checked if versions are identical55* This is also used for backward compatibility to get the runtime56* structure size if it the runtime is older than the interface */57sizeof(kmp_omp_struct_info_t),5859/* OpenMP RTL version info. */60addr_and_size_of(__kmp_version_major),61addr_and_size_of(__kmp_version_minor),62addr_and_size_of(__kmp_version_build),63addr_and_size_of(__kmp_openmp_version),64{(kmp_uint64)(__kmp_copyright) + KMP_VERSION_MAGIC_LEN,650}, // Skip magic prefix.6667/* Various globals. */68addr_and_size_of(__kmp_threads),69addr_and_size_of(__kmp_root),70addr_and_size_of(__kmp_threads_capacity),71#if KMP_USE_MONITOR72addr_and_size_of(__kmp_monitor),73#endif74#if !KMP_USE_DYNAMIC_LOCK75addr_and_size_of(__kmp_user_lock_table),76#endif77addr_and_size_of(func_microtask),78addr_and_size_of(func_fork),79addr_and_size_of(func_fork_teams),80addr_and_size_of(__kmp_team_counter),81addr_and_size_of(__kmp_task_counter),82addr_and_size_of(kmp_omp_nthr_info_buffer),83sizeof(void *),84OMP_LOCK_T_SIZE < sizeof(void *),85bs_last_barrier,86INITIAL_TASK_DEQUE_SIZE,8788// thread structure information89sizeof(kmp_base_info_t),90offset_and_size_of(kmp_base_info_t, th_info),91offset_and_size_of(kmp_base_info_t, th_team),92offset_and_size_of(kmp_base_info_t, th_root),93offset_and_size_of(kmp_base_info_t, th_serial_team),94offset_and_size_of(kmp_base_info_t, th_ident),95offset_and_size_of(kmp_base_info_t, th_spin_here),96offset_and_size_of(kmp_base_info_t, th_next_waiting),97offset_and_size_of(kmp_base_info_t, th_task_team),98offset_and_size_of(kmp_base_info_t, th_current_task),99offset_and_size_of(kmp_base_info_t, th_task_state),100offset_and_size_of(kmp_base_info_t, th_bar),101offset_and_size_of(kmp_bstate_t, b_worker_arrived),102103// teams information104offset_and_size_of(kmp_base_info_t, th_teams_microtask),105offset_and_size_of(kmp_base_info_t, th_teams_level),106offset_and_size_of(kmp_teams_size_t, nteams),107offset_and_size_of(kmp_teams_size_t, nth),108109// kmp_desc structure (for info field above)110sizeof(kmp_desc_base_t),111offset_and_size_of(kmp_desc_base_t, ds_tid),112offset_and_size_of(kmp_desc_base_t, ds_gtid),113// On Windows* OS, ds_thread contains a thread /handle/, which is not usable,114// while thread /id/ is in ds_thread_id.115#if KMP_OS_WINDOWS116offset_and_size_of(kmp_desc_base_t, ds_thread_id),117#else118offset_and_size_of(kmp_desc_base_t, ds_thread),119#endif120121// team structure information122sizeof(kmp_base_team_t),123offset_and_size_of(kmp_base_team_t, t_master_tid),124offset_and_size_of(kmp_base_team_t, t_ident),125offset_and_size_of(kmp_base_team_t, t_parent),126offset_and_size_of(kmp_base_team_t, t_nproc),127offset_and_size_of(kmp_base_team_t, t_threads),128offset_and_size_of(kmp_base_team_t, t_serialized),129offset_and_size_of(kmp_base_team_t, t_id),130offset_and_size_of(kmp_base_team_t, t_pkfn),131offset_and_size_of(kmp_base_team_t, t_task_team),132offset_and_size_of(kmp_base_team_t, t_implicit_task_taskdata),133offset_and_size_of(kmp_base_team_t, t_cancel_request),134offset_and_size_of(kmp_base_team_t, t_bar),135offset_and_size_of(kmp_balign_team_t, b_master_arrived),136offset_and_size_of(kmp_balign_team_t, b_team_arrived),137138// root structure information139sizeof(kmp_base_root_t),140offset_and_size_of(kmp_base_root_t, r_root_team),141offset_and_size_of(kmp_base_root_t, r_hot_team),142offset_and_size_of(kmp_base_root_t, r_uber_thread),143offset_and_size_not_available,144145// ident structure information146sizeof(ident_t),147offset_and_size_of(ident_t, psource),148offset_and_size_of(ident_t, flags),149150// lock structure information151sizeof(kmp_base_queuing_lock_t),152offset_and_size_of(kmp_base_queuing_lock_t, initialized),153offset_and_size_of(kmp_base_queuing_lock_t, location),154offset_and_size_of(kmp_base_queuing_lock_t, tail_id),155offset_and_size_of(kmp_base_queuing_lock_t, head_id),156offset_and_size_of(kmp_base_queuing_lock_t, next_ticket),157offset_and_size_of(kmp_base_queuing_lock_t, now_serving),158offset_and_size_of(kmp_base_queuing_lock_t, owner_id),159offset_and_size_of(kmp_base_queuing_lock_t, depth_locked),160offset_and_size_of(kmp_base_queuing_lock_t, flags),161162#if !KMP_USE_DYNAMIC_LOCK163/* Lock table. */164sizeof(kmp_lock_table_t),165offset_and_size_of(kmp_lock_table_t, used),166offset_and_size_of(kmp_lock_table_t, allocated),167offset_and_size_of(kmp_lock_table_t, table),168#endif169170// Task team structure information.171sizeof(kmp_base_task_team_t),172offset_and_size_of(kmp_base_task_team_t, tt_threads_data),173offset_and_size_of(kmp_base_task_team_t, tt_found_tasks),174offset_and_size_of(kmp_base_task_team_t, tt_nproc),175offset_and_size_of(kmp_base_task_team_t, tt_unfinished_threads),176offset_and_size_of(kmp_base_task_team_t, tt_active),177178// task_data_t.179sizeof(kmp_taskdata_t),180offset_and_size_of(kmp_taskdata_t, td_task_id),181offset_and_size_of(kmp_taskdata_t, td_flags),182offset_and_size_of(kmp_taskdata_t, td_team),183offset_and_size_of(kmp_taskdata_t, td_parent),184offset_and_size_of(kmp_taskdata_t, td_level),185offset_and_size_of(kmp_taskdata_t, td_ident),186offset_and_size_of(kmp_taskdata_t, td_allocated_child_tasks),187offset_and_size_of(kmp_taskdata_t, td_incomplete_child_tasks),188189offset_and_size_of(kmp_taskdata_t, td_taskwait_ident),190offset_and_size_of(kmp_taskdata_t, td_taskwait_counter),191offset_and_size_of(kmp_taskdata_t, td_taskwait_thread),192193offset_and_size_of(kmp_taskdata_t, td_taskgroup),194offset_and_size_of(kmp_taskgroup_t, count),195offset_and_size_of(kmp_taskgroup_t, cancel_request),196197offset_and_size_of(kmp_taskdata_t, td_depnode),198offset_and_size_of(kmp_depnode_list_t, node),199offset_and_size_of(kmp_depnode_list_t, next),200offset_and_size_of(kmp_base_depnode_t, successors),201offset_and_size_of(kmp_base_depnode_t, task),202offset_and_size_of(kmp_base_depnode_t, npredecessors),203offset_and_size_of(kmp_base_depnode_t, nrefs),204offset_and_size_of(kmp_task_t, routine),205206// thread_data_t.207sizeof(kmp_thread_data_t),208offset_and_size_of(kmp_base_thread_data_t, td_deque),209offset_and_size_of(kmp_base_thread_data_t, td_deque_size),210offset_and_size_of(kmp_base_thread_data_t, td_deque_head),211offset_and_size_of(kmp_base_thread_data_t, td_deque_tail),212offset_and_size_of(kmp_base_thread_data_t, td_deque_ntasks),213offset_and_size_of(kmp_base_thread_data_t, td_deque_last_stolen),214215// The last field.216KMP_OMP_VERSION,217218}; // __kmp_omp_debug_struct_info219220#undef offset_and_size_of221#undef addr_and_size_of222223/* Intel compiler on IA-32 architecture issues a warning "conversion224from "unsigned long long" to "char *" may lose significant bits"225when 64-bit value is assigned to 32-bit pointer. Use this function226to suppress the warning. */227static inline void *__kmp_convert_to_ptr(kmp_uint64 addr) {228#if KMP_COMPILER_ICC || KMP_COMPILER_ICX229#pragma warning(push)230#pragma warning(disable : 810) // conversion from "unsigned long long" to "char231// *" may lose significant bits232#pragma warning(disable : 1195) // conversion from integer to smaller pointer233#endif // KMP_COMPILER_ICC || KMP_COMPILER_ICX234return (void *)addr;235#if KMP_COMPILER_ICC || KMP_COMPILER_ICX236#pragma warning(pop)237#endif // KMP_COMPILER_ICC || KMP_COMPILER_ICX238} // __kmp_convert_to_ptr239240static int kmp_location_match(kmp_str_loc_t *loc, kmp_omp_nthr_item_t *item) {241242int file_match = 0;243int func_match = 0;244int line_match = 0;245246char *file = (char *)__kmp_convert_to_ptr(item->file);247char *func = (char *)__kmp_convert_to_ptr(item->func);248file_match = __kmp_str_fname_match(&loc->fname, file);249func_match =250item->func == 0 // If item->func is NULL, it allows any func name.251|| strcmp(func, "*") == 0 ||252(loc->func != NULL && strcmp(loc->func, func) == 0);253line_match =254item->begin <= loc->line &&255(item->end <= 0 ||256loc->line <= item->end); // if item->end <= 0, it means "end of file".257258return (file_match && func_match && line_match);259260} // kmp_location_match261262int __kmp_omp_num_threads(ident_t const *ident) {263264int num_threads = 0;265266kmp_omp_nthr_info_t *info = (kmp_omp_nthr_info_t *)__kmp_convert_to_ptr(267__kmp_omp_debug_struct_info.nthr_info.addr);268if (info->num > 0 && info->array != 0) {269kmp_omp_nthr_item_t *items =270(kmp_omp_nthr_item_t *)__kmp_convert_to_ptr(info->array);271kmp_str_loc_t loc = __kmp_str_loc_init(ident->psource, true);272int i;273for (i = 0; i < info->num; ++i) {274if (kmp_location_match(&loc, &items[i])) {275num_threads = items[i].num_threads;276}277}278__kmp_str_loc_free(&loc);279}280281return num_threads;282;283284} // __kmp_omp_num_threads285#endif /* USE_DEBUGGER */286287288