From c6d967b4dd3fd313af5e6c6e906eeb9a9e3603cf Mon Sep 17 00:00:00 2001 From: Antonio Maiorano Date: Fri, 4 Feb 2022 22:25:45 +0000 Subject: [PATCH] Add debugger::Break() to break into debugger if attached, and call when an ICE occurs Helpful for debugging. Bug: tint:1331 Change-Id: Ia2b58626ff7fb92194b419805eb4f48ad419092d Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/79242 Reviewed-by: Ben Clayton Kokoro: Kokoro Commit-Queue: Antonio Maiorano --- src/BUILD.gn | 6 +++-- src/CMakeLists.txt | 2 ++ src/debug.cc | 4 +++ src/utils/debugger.cc | 59 +++++++++++++++++++++++++++++++++++++++++++ src/utils/debugger.h | 23 +++++++++++++++++ 5 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 src/utils/debugger.cc create mode 100644 src/utils/debugger.h diff --git a/src/BUILD.gn b/src/BUILD.gn index fce79f0308..4f4b25bb7c 100644 --- a/src/BUILD.gn +++ b/src/BUILD.gn @@ -184,6 +184,8 @@ libtint_source_set("libtint_core_all_src") { "ast/ast_type.cc", # TODO(bclayton) - rename to type.cc "ast/atomic.cc", "ast/atomic.h", + "ast/attribute.cc", + "ast/attribute.h", "ast/binary_expression.cc", "ast/binary_expression.h", "ast/binding_attribute.cc", @@ -210,8 +212,6 @@ libtint_source_set("libtint_core_all_src") { "ast/case_statement.h", "ast/continue_statement.cc", "ast/continue_statement.h", - "ast/attribute.cc", - "ast/attribute.h", "ast/depth_multisampled_texture.cc", "ast/depth_multisampled_texture.h", "ast/depth_texture.cc", @@ -496,6 +496,8 @@ libtint_source_set("libtint_core_all_src") { "transform/zero_init_workgroup_memory.cc", "transform/zero_init_workgroup_memory.h", "utils/crc32.h", + "utils/debugger.cc", + "utils/debugger.h", "utils/enum_set.h", "utils/hash.h", "utils/map.h", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b0e09618b1..352318644c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -48,6 +48,8 @@ add_library(tint_diagnostic_utils diagnostic/formatter.h diagnostic/printer.cc diagnostic/printer.h + utils/debugger.cc + utils/debugger.h ) tint_default_compile_options(tint_diagnostic_utils) diff --git a/src/debug.cc b/src/debug.cc index d63f902ef5..2abf82cd45 100644 --- a/src/debug.cc +++ b/src/debug.cc @@ -16,6 +16,8 @@ #include +#include "src/utils/debugger.h" + namespace tint { namespace { @@ -41,6 +43,8 @@ InternalCompilerError::~InternalCompilerError() { if (ice_reporter) { ice_reporter(diagnostics_); } + + debugger::Break(); } } // namespace tint diff --git a/src/utils/debugger.cc b/src/utils/debugger.cc new file mode 100644 index 0000000000..5d8444ce57 --- /dev/null +++ b/src/utils/debugger.cc @@ -0,0 +1,59 @@ +// Copyright 2022 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "src/utils/debugger.h" + +#ifdef _MSC_VER +#include +#elif defined(__linux__) +#include +#include +#include +#endif + +#ifdef _MSC_VER + +void tint::debugger::Break() { + if (::IsDebuggerPresent()) { + ::DebugBreak(); + } +} + +#elif defined(__linux__) + +void tint::debugger::Break() { + // A process is being traced (debugged) if "/proc/self/status" contains a + // line with "TracerPid: ...". + bool is_traced = false; + std::ifstream fin("/proc/self/status"); + std::string line; + while (!is_traced && std::getline(fin, line)) { + const char kPrefix[] = "TracerPid:\t"; + static constexpr int kPrefixLen = sizeof(kPrefix) - 1; + if (line.length() > kPrefixLen && + line.compare(0, kPrefixLen, kPrefix) == 0) { + is_traced = line[kPrefixLen] != '0'; + } + } + + if (is_traced) { + raise(SIGTRAP); + } +} + +#else + +void tint::debugger::Break() {} + +#endif diff --git a/src/utils/debugger.h b/src/utils/debugger.h new file mode 100644 index 0000000000..c40f3e92d6 --- /dev/null +++ b/src/utils/debugger.h @@ -0,0 +1,23 @@ +// Copyright 2022 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef SRC_UTILS_DEBUGGER_H_ +#define SRC_UTILS_DEBUGGER_H_ + +namespace tint::debugger { +/// If debugger is attached, will break into it at the call site. +void Break(); +} // namespace tint::debugger + +#endif // SRC_UTILS_DEBUGGER_H_