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 <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
Antonio Maiorano 2022-02-04 22:25:45 +00:00 committed by Tint LUCI CQ
parent 3d5b38447a
commit c6d967b4dd
5 changed files with 92 additions and 2 deletions

View File

@ -184,6 +184,8 @@ libtint_source_set("libtint_core_all_src") {
"ast/ast_type.cc", # TODO(bclayton) - rename to type.cc "ast/ast_type.cc", # TODO(bclayton) - rename to type.cc
"ast/atomic.cc", "ast/atomic.cc",
"ast/atomic.h", "ast/atomic.h",
"ast/attribute.cc",
"ast/attribute.h",
"ast/binary_expression.cc", "ast/binary_expression.cc",
"ast/binary_expression.h", "ast/binary_expression.h",
"ast/binding_attribute.cc", "ast/binding_attribute.cc",
@ -210,8 +212,6 @@ libtint_source_set("libtint_core_all_src") {
"ast/case_statement.h", "ast/case_statement.h",
"ast/continue_statement.cc", "ast/continue_statement.cc",
"ast/continue_statement.h", "ast/continue_statement.h",
"ast/attribute.cc",
"ast/attribute.h",
"ast/depth_multisampled_texture.cc", "ast/depth_multisampled_texture.cc",
"ast/depth_multisampled_texture.h", "ast/depth_multisampled_texture.h",
"ast/depth_texture.cc", "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.cc",
"transform/zero_init_workgroup_memory.h", "transform/zero_init_workgroup_memory.h",
"utils/crc32.h", "utils/crc32.h",
"utils/debugger.cc",
"utils/debugger.h",
"utils/enum_set.h", "utils/enum_set.h",
"utils/hash.h", "utils/hash.h",
"utils/map.h", "utils/map.h",

View File

@ -48,6 +48,8 @@ add_library(tint_diagnostic_utils
diagnostic/formatter.h diagnostic/formatter.h
diagnostic/printer.cc diagnostic/printer.cc
diagnostic/printer.h diagnostic/printer.h
utils/debugger.cc
utils/debugger.h
) )
tint_default_compile_options(tint_diagnostic_utils) tint_default_compile_options(tint_diagnostic_utils)

View File

@ -16,6 +16,8 @@
#include <memory> #include <memory>
#include "src/utils/debugger.h"
namespace tint { namespace tint {
namespace { namespace {
@ -41,6 +43,8 @@ InternalCompilerError::~InternalCompilerError() {
if (ice_reporter) { if (ice_reporter) {
ice_reporter(diagnostics_); ice_reporter(diagnostics_);
} }
debugger::Break();
} }
} // namespace tint } // namespace tint

59
src/utils/debugger.cc Normal file
View File

@ -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 <Windows.h>
#elif defined(__linux__)
#include <signal.h>
#include <fstream>
#include <string>
#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: <non-zero-digit>...".
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

23
src/utils/debugger.h Normal file
View File

@ -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_