Skip to content

Commit bd23e42

Browse files
committed
[upstream_utils] Add std::is_debugger_present() shim
Fixes #7420.
1 parent b4a8d33 commit bd23e42

File tree

15 files changed

+405
-3
lines changed

15 files changed

+405
-3
lines changed

ThirdPartyNotices.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Portable File Dialogs wpigui/src/main/native/include/portable-file-dialogs.h
4343
V8 export-template wpiutil/src/main/native/include/wpi/SymbolExports.h
4444
GCEM wpimath/src/main/native/thirdparty/gcem/include/
4545
Sleipnir wpimath/src/main/native/thirdparty/sleipnir
46+
Debugging wpiutil/src/main/native/thirdparty/debugging
4647

4748
==============================================================================
4849
Google Test License
@@ -1224,3 +1225,29 @@ Redistribution and use in source and binary forms, with or without modification,
12241225
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
12251226

12261227
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1228+
1229+
=================
1230+
Debugging License
1231+
=================
1232+
MIT License
1233+
1234+
Copyright (c) 2021-2022 René Ferdinand Rivera Morell
1235+
Copyright (c) 2018 Isabella Muerte
1236+
1237+
Permission is hereby granted, free of charge, to any person obtaining a copy
1238+
of this software and associated documentation files (the "Software"), to deal
1239+
in the Software without restriction, including without limitation the rights
1240+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1241+
copies of the Software, and to permit persons to whom the Software is
1242+
furnished to do so, subject to the following conditions:
1243+
1244+
The above copyright notice and this permission notice shall be included in all
1245+
copies or substantial portions of the Software.
1246+
1247+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1248+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1249+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1250+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1251+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1252+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1253+
SOFTWARE.

upstream_utils/debugging.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
import shutil
5+
6+
from upstream_utils import Lib, walk_cwd_and_copy_if
7+
8+
9+
def copy_upstream_src(wpilib_root):
10+
wpiutil = os.path.join(wpilib_root, "wpiutil")
11+
12+
# Delete old install
13+
for d in [
14+
"src/main/native/thirdparty/debugging/src",
15+
"src/main/native/thirdparty/debugging/include",
16+
]:
17+
shutil.rmtree(os.path.join(wpiutil, d), ignore_errors=True)
18+
19+
# Copy debugging files into allwpilib
20+
filenames = walk_cwd_and_copy_if(
21+
lambda dp, f: dp.startswith(os.path.join(".", "src"))
22+
or dp.startswith(os.path.join(".", "include")),
23+
os.path.join(wpiutil, "src/main/native/thirdparty/debugging"),
24+
)
25+
26+
for filename in filenames:
27+
with open(filename) as f:
28+
content = f.read()
29+
30+
# Rename namespace from stdx to wpi
31+
content = content.replace("namespace stdx", "namespace wpi")
32+
33+
with open(filename, "w") as f:
34+
f.write(content)
35+
36+
37+
def main():
38+
name = "debugging"
39+
url = "https://github.com/grafikrobot/debugging"
40+
# master on 2024-11-21
41+
tag = "c510133c44894b93afbb5be55275bfb88163a2cb"
42+
43+
expected = Lib(name, url, tag, copy_upstream_src)
44+
expected.main()
45+
46+
47+
if __name__ == "__main__":
48+
main()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: Tyler Veness <[email protected]>
3+
Date: Thu, 21 Nov 2024 17:51:15 -0800
4+
Subject: [PATCH 1/3] Guard [[gnu::flatten]] attribute
5+
6+
---
7+
include/debugging.hpp | 10 ++++++++--
8+
1 file changed, 8 insertions(+), 2 deletions(-)
9+
10+
diff --git a/include/debugging.hpp b/include/debugging.hpp
11+
index 70ba724a2b6522a774931af7d7be2cee9408567a..25014a9fc65d06052089058feea7566462c01d60 100644
12+
--- a/include/debugging.hpp
13+
+++ b/include/debugging.hpp
14+
@@ -7,13 +7,19 @@ namespace stdx {
15+
16+
bool is_debugger_present() noexcept;
17+
18+
-[[gnu::flatten]] inline void breakpoint() noexcept
19+
+#if defined(__GNUC__) && !defined(__clang__)
20+
+[[gnu::flatten]]
21+
+#endif
22+
+inline void breakpoint() noexcept
23+
{
24+
psnip_trap();
25+
}
26+
27+
28+
-[[gnu::flatten]] inline void breakpoint_if_debugging() noexcept
29+
+#if defined(__GNUC__) && !defined(__clang__)
30+
+[[gnu::flatten]]
31+
+#endif
32+
+inline void breakpoint_if_debugging() noexcept
33+
{
34+
if (is_debugger_present()) breakpoint();
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: Tyler Veness <[email protected]>
3+
Date: Thu, 21 Nov 2024 17:23:48 -0800
4+
Subject: [PATCH 2/3] Remove debugger_query argument from Windows and macOS
5+
6+
---
7+
src/macos.cxx | 2 +-
8+
src/windows.cxx | 2 +-
9+
2 files changed, 2 insertions(+), 2 deletions(-)
10+
11+
diff --git a/src/macos.cxx b/src/macos.cxx
12+
index bbcf6f2eec9ea479a2bea0ff06b454dc81b5d356..85dbb5f45d89680e39b4847a9aa2d5472c824f2a 100644
13+
--- a/src/macos.cxx
14+
+++ b/src/macos.cxx
15+
@@ -13,7 +13,7 @@ auto exc = std::array<T, EXC_TYPES_COUNT> { {} };
16+
17+
namespace stdx {
18+
19+
-bool is_debugger_present(debugger_query q) noexcept
20+
+bool is_debugger_present() noexcept
21+
{
22+
mach_msg_type_number_t count {};
23+
auto masks = exc<exception_mask_t>;
24+
diff --git a/src/windows.cxx b/src/windows.cxx
25+
index eec576f415d52f63d2658012546ead2e691d7415..45d98eb27c5182de7ad11291925275fb4fdb54fb 100644
26+
--- a/src/windows.cxx
27+
+++ b/src/windows.cxx
28+
@@ -9,7 +9,7 @@
29+
30+
namespace stdx {
31+
32+
-bool is_debugger_present(debugger_query q) noexcept
33+
+bool is_debugger_present() noexcept
34+
{
35+
return ::IsDebuggerPresent();
36+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: Tyler Veness <[email protected]>
3+
Date: Thu, 21 Nov 2024 18:09:37 -0800
4+
Subject: [PATCH 3/3] Fix exception mask type typo on macOS
5+
6+
---
7+
src/macos.cxx | 2 +-
8+
1 file changed, 1 insertion(+), 1 deletion(-)
9+
10+
diff --git a/src/macos.cxx b/src/macos.cxx
11+
index 85dbb5f45d89680e39b4847a9aa2d5472c824f2a..2c68064742bc7883a08551b88cd5dbb9a1f38100 100644
12+
--- a/src/macos.cxx
13+
+++ b/src/macos.cxx
14+
@@ -20,7 +20,7 @@ bool is_debugger_present() noexcept
15+
auto ports = exc<mach_port_t>;
16+
auto behaviors = exc<exception_behavior_t>;
17+
auto flavors = exc<thread_state_flavor_t>;
18+
- exception_mast_t mask
19+
+ exception_mask_t mask
20+
= EXC_MASK_ALL & ~(EXC_MASK_RESOURCE | EXC_MASK_GUARD);
21+
kern_return_t result = task_get_exception_ports(mach_task_self(), mask,
22+
masks.data(), &count, ports.data(), behaviors.data(), flavors.data());

upstream_utils/upstream_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ def copy_to(files, root, rename_c_to_cpp=False):
5555
for f in files:
5656
dest_file = os.path.join(root, f)
5757

58-
# Rename .cc file to .cpp
59-
if dest_file.endswith(".cc"):
58+
# Rename .cc or .cxx file to .cpp
59+
if dest_file.endswith(".cc") or dest_file.endswith(".cxx"):
6060
dest_file = os.path.splitext(dest_file)[0] + ".cpp"
61+
6162
if rename_c_to_cpp and dest_file.endswith(".c"):
6263
dest_file = os.path.splitext(dest_file)[0] + ".cpp"
6364

wpiutil/BUILD.bazel

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@ cc_library(
1313
visibility = ["//wpiutil:__subpackages__"],
1414
)
1515

16+
cc_library(
17+
name = "debugging-headers",
18+
hdrs = glob([
19+
"src/main/native/thirdparty/debugging/include/**/*",
20+
]),
21+
includes = ["src/main/native/thirdparty/debugging/include"],
22+
strip_include_prefix = "src/main/native/thirdparty/debugging/include",
23+
visibility = ["//wpiutil:__subpackages__"],
24+
)
25+
26+
filegroup(
27+
name = "debugging-srcs",
28+
srcs = glob(["src/main/native/thirdparty/debugging/src/**"]),
29+
visibility = ["//wpiutil:__subpackages__"],
30+
)
31+
1632
cc_library(
1733
name = "expected-headers",
1834
hdrs = glob([
@@ -175,6 +191,7 @@ cc_library(
175191
["src/main/native/cpp/**"],
176192
exclude = ["src/main/native/cpp/jni/**"],
177193
) + [
194+
":debugging-srcs",
178195
":fmtlib-srcs",
179196
":generate-resources",
180197
":llvm-srcs",
@@ -191,6 +208,7 @@ cc_library(
191208
deps = [
192209
":argparse-headers",
193210
":concurrentqueue-headers",
211+
":debugging-headers",
194212
":expected-headers",
195213
":fmtlib-headers",
196214
":json-headers",

wpiutil/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ generate_resources(src/main/native/resources generated/main/cpp WPI wpi wpiutil_
116116
file(
117117
GLOB_RECURSE wpiutil_native_src
118118
src/main/native/cpp/*.cpp
119+
src/main/native/thirdparty/debugging/src/*.cpp
119120
src/main/native/thirdparty/json/cpp/*.cpp
120121
src/main/native/thirdparty/llvm/cpp/*.cpp
121122
src/main/native/thirdparty/mpack/src/*.cpp
@@ -198,6 +199,7 @@ install(
198199
DIRECTORY
199200
src/main/native/include/
200201
src/main/native/thirdparty/argparse/include/
202+
src/main/native/thirdparty/debugging/include/
201203
src/main/native/thirdparty/expected/include/
202204
src/main/native/thirdparty/json/include/
203205
src/main/native/thirdparty/llvm/include/
@@ -212,6 +214,7 @@ target_include_directories(
212214
PUBLIC
213215
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/include>
214216
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/thirdparty/argparse/include>
217+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/thirdparty/debugging/include>
215218
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/thirdparty/expected/include>
216219
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/thirdparty/json/include>
217220
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/thirdparty/llvm/include>

wpiutil/build.gradle

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ ext {
1717
dependsOn generateTask
1818
}
1919
it.sources {
20+
debuggingCpp(CppSourceSet) {
21+
source {
22+
srcDirs 'src/main/native/thirdparty/debugging/src'
23+
include '*.cpp'
24+
}
25+
exportedHeaders {
26+
srcDirs 'src/main/native/thirdparty/debugging/include'
27+
}
28+
}
2029
fmtlibCpp(CppSourceSet) {
2130
source {
2231
srcDirs 'src/main/native/thirdparty/fmtlib/src'
@@ -178,6 +187,7 @@ nativeUtils.exportsConfigs {
178187
cppHeadersZip {
179188
def thirdpartyIncDirs = [
180189
'src/main/native/thirdparty/argparse/include',
190+
'src/main/native/thirdparty/debugging/include',
181191
'src/main/native/thirdparty/expected/include',
182192
'src/main/native/thirdparty/fmtlib/include',
183193
'src/main/native/thirdparty/json/include',
@@ -229,7 +239,7 @@ model {
229239
all {
230240
it.sources.each {
231241
it.exportedHeaders {
232-
srcDirs 'src/main/native/include', 'src/main/native/thirdparty/argparse/include/', 'src/main/native/thirdparty/expected/include', 'src/main/native/thirdparty/fmtlib/include', 'src/main/native/thirdparty/llvm/include', 'src/main/native/thirdparty/sigslot/include', 'src/main/native/thirdparty/json/include', 'src/main/native/thirdparty/memory/include', 'src/main/native/thirdparty/mpack/include', 'src/main/native/thirdparty/protobuf/include', 'src/main/native/thirdparty/nanopb/include'
242+
srcDirs 'src/main/native/include', 'src/main/native/thirdparty/argparse/include/', 'src/main/native/thirdparty/debugging/include', 'src/main/native/thirdparty/expected/include', 'src/main/native/thirdparty/fmtlib/include', 'src/main/native/thirdparty/llvm/include', 'src/main/native/thirdparty/sigslot/include', 'src/main/native/thirdparty/json/include', 'src/main/native/thirdparty/memory/include', 'src/main/native/thirdparty/mpack/include', 'src/main/native/thirdparty/protobuf/include', 'src/main/native/thirdparty/nanopb/include'
233243
}
234244
}
235245
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef IXM_BREAKPOINT_HPP
2+
#define IXM_BREAKPOINT_HPP
3+
4+
#include <debugging/detail/psnip_debug_trap.h>
5+
6+
namespace wpi {
7+
8+
bool is_debugger_present() noexcept;
9+
10+
#if defined(__GNUC__) && !defined(__clang__)
11+
[[gnu::flatten]]
12+
#endif
13+
inline void breakpoint() noexcept
14+
{
15+
psnip_trap();
16+
}
17+
18+
19+
#if defined(__GNUC__) && !defined(__clang__)
20+
[[gnu::flatten]]
21+
#endif
22+
inline void breakpoint_if_debugging() noexcept
23+
{
24+
if (is_debugger_present()) breakpoint();
25+
}
26+
27+
} // namespace wpi
28+
29+
#endif

0 commit comments

Comments
 (0)