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>
diff --git a/src/BUILD.gn b/src/BUILD.gn
index fce79f03..4f4b25b 100644
--- a/src/BUILD.gn
+++ b/src/BUILD.gn
@@ -184,6 +184,8 @@
     "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 @@
     "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 @@
     "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 b0e0961..3523186 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -48,6 +48,8 @@
   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 d63f902..2abf82c 100644
--- a/src/debug.cc
+++ b/src/debug.cc
@@ -16,6 +16,8 @@
 
 #include <memory>
 
+#include "src/utils/debugger.h"
+
 namespace tint {
 namespace {
 
@@ -41,6 +43,8 @@
   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 0000000..5d8444c
--- /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 <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
diff --git a/src/utils/debugger.h b/src/utils/debugger.h
new file mode 100644
index 0000000..c40f3e9
--- /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_