Skip to content

Commit

Permalink
[wpiutil] Use C++23 stacktrace library on Windows
Browse files Browse the repository at this point in the history
This lets us remove the unmaintained StackWalker library and its hacky
upstream_utils script.

A Discord user also reported that StackWalker gives blank stacktraces:
https://discord.com/channels/176186766946992128/368993897495527424/1261940029287301150

They reported an earlier version of this PR giving the following
stacktrace instead:
```
D:\allwpilib\developerRobot\src\main\native\cpp\Robot.cpp(18): developerRobotCpp!Robot::RobotInit+0xB6
D:\allwpilib\wpilibc\src\main\native\cpp\TimedRobot.cpp(22): wpilibcd!frc::TimedRobot::StartCompetition+0x4F
D:\allwpilib\wpilibc\src\main\native\include\frc\RobotBase.h(36): developerRobotCpp!frc::impl::RunRobot<Robot>+0xC8
D:\allwpilib\wpilibc\src\main\native\include\frc\RobotBase.h(106): developerRobotCpp!frc::StartRobot<Robot>+0x17E
D:\allwpilib\developerRobot\src\main\native\cpp\Robot.cpp(60): developerRobotCpp!main+0xB
D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl(79): developerRobotCpp!invoke_main+0x39
D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl(288): developerRobotCpp!__scrt_common_main_seh+0x132
D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl(331): developerRobotCpp!__scrt_common_main+0xE
D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp(17): developerRobotCpp!mainCRTStartup+0xE
KERNEL32!BaseThreadInitThunk+0x1D
ntdll!RtlUserThreadStart+0x28
```
  • Loading branch information
calcmogul committed Feb 13, 2025
1 parent d62ab12 commit abbc631
Show file tree
Hide file tree
Showing 19 changed files with 68 additions and 2,006 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/upstream-utils.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,6 @@ jobs:
./mpack.py clone
./mpack.py copy-src
./mpack.py format-patch
- name: Run stack_walker.py
run: |
cd upstream_utils
./stack_walker.py clone
./stack_walker.py copy-src
./stack_walker.py format-patch
- name: Run memory.py
run: |
cd upstream_utils
Expand Down
30 changes: 0 additions & 30 deletions ThirdPartyNotices.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ jQuery wpinet/src/main/native/resources/jquery-*
popper.js wpinet/src/main/native/resources/popper-*
units wpimath/src/main/native/include/units/
Eigen wpimath/src/main/native/thirdparty/eigen/include/
StackWalker wpiutil/src/main/native/windows/StackWalker.*
Team 254 Library wpimath/src/main/java/edu/wpi/first/math/spline/SplineParameterizer.java
wpimath/src/main/java/edu/wpi/first/math/trajectory/TrajectoryParameterizer.java
wpimath/src/main/native/include/frc/spline/SplineParameterizer.h
Expand Down Expand Up @@ -1026,35 +1025,6 @@ Exhibit B - "Incompatible With Secondary Licenses" Notice
defined by the Mozilla Public License, v. 2.0.


===================
StackWalker License
===================
Copyright (c) 2005-2013, Jochen Kalmbach
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
Neither the name of Jochen Kalmbach nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
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 OWNER 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.


================
Team 254 Library
================
Expand Down
6 changes: 5 additions & 1 deletion apriltag/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ add_library(apriltag ${apriltag_native_src} ${apriltag_resources_src} ${apriltag
set_target_properties(apriltag PROPERTIES DEBUG_POSTFIX "d")

set_property(TARGET apriltag PROPERTY FOLDER "libraries")
target_compile_features(apriltag PUBLIC cxx_std_20)
if(MSVC)
target_compile_features(apriltag PUBLIC cxx_std_23)
else()
target_compile_features(apriltag PUBLIC cxx_std_20)
endif()
wpilib_target_warnings(apriltag)

target_link_libraries(apriltag wpimath)
Expand Down
4 changes: 3 additions & 1 deletion fieldImages/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ add_library(fieldImages ${field_images_resources_src} src/main/native/cpp/fields
set_target_properties(fieldImages PROPERTIES DEBUG_POSTFIX "d")

set_property(TARGET fieldImages PROPERTY FOLDER "libraries")
target_compile_features(fieldImages PUBLIC cxx_std_20)
if(MSVC)
target_compile_options(fieldImages PUBLIC /bigobj)
target_compile_features(fieldImages PUBLIC cxx_std_23)
else()
target_compile_features(fieldImages PUBLIC cxx_std_20)
endif()
wpilib_target_warnings(fieldImages)

Expand Down
6 changes: 5 additions & 1 deletion ntcore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ target_include_directories(
$<INSTALL_INTERFACE:${include_dest}/ntcore>
)
wpilib_target_warnings(ntcore)
target_compile_features(ntcore PUBLIC cxx_std_20)
if(MSVC)
target_compile_features(ntcore PUBLIC cxx_std_23)
else()
target_compile_features(ntcore PUBLIC cxx_std_20)
endif()
target_link_libraries(ntcore PUBLIC wpinet wpiutil)

set_property(TARGET ntcore PROPERTY FOLDER "libraries")
Expand Down
6 changes: 5 additions & 1 deletion romiVendordep/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ add_library(romiVendordep ${romiVendordep_native_src})
set_target_properties(romiVendordep PROPERTIES DEBUG_POSTFIX "d")
set_property(TARGET romiVendordep PROPERTY FOLDER "libraries")

target_compile_features(romiVendordep PUBLIC cxx_std_20)
if(MSVC)
target_compile_features(romiVendordep PUBLIC cxx_std_23)
else()
target_compile_features(romiVendordep PUBLIC cxx_std_20)
endif()
wpilib_target_warnings(romiVendordep)
target_link_libraries(romiVendordep wpilibc)

Expand Down
7 changes: 7 additions & 0 deletions shared/config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ nativeUtils.platformConfigs.each {
}
}

// Use C++23 on Windows
nativeUtils.platformConfigs.each {
if (it.name.contains('windows')) {
it.cppCompiler.args.add("/std:c++23preview")
}
}

nativeUtils.platformConfigs.linuxathena.linker.args.add("-Wl,--fatal-warnings")

if (project.hasProperty('ntcoreffibuild')) {
Expand Down
65 changes: 0 additions & 65 deletions upstream_utils/stack_walker.py

This file was deleted.

21 changes: 0 additions & 21 deletions upstream_utils/stack_walker_patches/0001-Add-advapi-pragma.patch

This file was deleted.

6 changes: 5 additions & 1 deletion wpilibNewCommands/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ add_library(wpilibNewCommands ${wpilibNewCommands_native_src})
set_target_properties(wpilibNewCommands PROPERTIES DEBUG_POSTFIX "d")
set_property(TARGET wpilibNewCommands PROPERTY FOLDER "libraries")

target_compile_features(wpilibNewCommands PUBLIC cxx_std_20)
if(MSVC)
target_compile_features(wpilibNewCommands PUBLIC cxx_std_23)
else()
target_compile_features(wpilibNewCommands PUBLIC cxx_std_20)
endif()
wpilib_target_warnings(wpilibNewCommands)
target_link_libraries(wpilibNewCommands wpilibc)

Expand Down
2 changes: 1 addition & 1 deletion wpimath/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ set_target_properties(wpimath PROPERTIES DEBUG_POSTFIX "d")
set_property(TARGET wpimath PROPERTY FOLDER "libraries")
target_compile_definitions(wpimath PRIVATE WPILIB_EXPORTS SLEIPNIR_EXPORTS)

target_compile_features(wpimath PUBLIC cxx_std_20)
if(MSVC)
target_compile_features(wpimath PUBLIC cxx_std_23)
target_compile_options(wpimath PUBLIC /utf-8 /bigobj)
endif()
wpilib_target_warnings(wpimath)
Expand Down
6 changes: 5 additions & 1 deletion wpinet/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ set_target_properties(wpinet PROPERTIES DEBUG_POSTFIX "d")

set_property(TARGET wpinet PROPERTY FOLDER "libraries")

target_compile_features(wpinet PUBLIC cxx_std_20)
if(MSVC)
target_compile_features(wpinet PUBLIC cxx_std_23)
else()
target_compile_features(wpinet PUBLIC cxx_std_20)
endif()
wpilib_target_warnings(wpinet)
target_link_libraries(wpinet PUBLIC wpiutil)

Expand Down
4 changes: 3 additions & 1 deletion wpiutil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,15 @@ set_target_properties(wpiutil PROPERTIES DEBUG_POSTFIX "d")

set_property(TARGET wpiutil PROPERTY FOLDER "libraries")

target_compile_features(wpiutil PUBLIC cxx_std_20)
if(MSVC)
target_compile_options(
wpiutil
PUBLIC /permissive- /Zc:preprocessor /Zc:__cplusplus /Zc:throwingNew /MP /bigobj /utf-8
)
target_compile_definitions(wpiutil PRIVATE -D_CRT_SECURE_NO_WARNINGS)
target_compile_features(wpiutil PUBLIC cxx_std_23)
else()
target_compile_features(wpiutil PUBLIC cxx_std_20)
endif()
wpilib_target_warnings(wpiutil)
if(WITH_PROTOBUF)
Expand Down
1 change: 1 addition & 0 deletions wpiutil/src/main/native/cpp/RuntimeCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#ifdef _WIN32
#include <cstdio>
#include <memory>
#pragma comment(lib, "version.lib") // for VerQueryValueW

#include "Windows.h"
extern "C" int32_t WPI_IsRuntimeValid(uint32_t* foundMajor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

#include "wpi/StackTrace.h"

#ifdef __cpp_lib_stacktrace
#include <stacktrace>
#elif !defined(_WIN32)
#include <execinfo.h>
#endif

#include <string>

Expand All @@ -16,33 +20,41 @@
namespace wpi {

std::string GetStackTraceDefault(int offset) {
#ifndef __ANDROID__
wpi::SmallString<1024> buf;
wpi::raw_svector_ostream trace(buf);

#ifdef __cpp_lib_stacktrace
auto stackTrace = std::stacktrace::current();

for (size_t i = offset; i < stackTrace.size(); ++i) {
// Only print recursive functions once in a row
if (i == 0 || stackTrace[i] != stackTrace[i - 1]) {
trace << "\tat " << std::to_string(stackTrace[i]) << '\n';
}
}
#elif !defined(_WIN32) && !defined(__ANDROID__)
void* stackTrace[128];
int stackSize = backtrace(stackTrace, 128);
char** mangledSymbols = backtrace_symbols(stackTrace, stackSize);
wpi::SmallString<1024> buf;
wpi::raw_svector_ostream trace(buf);

for (int i = offset; i < stackSize; i++) {
// Only print recursive functions once in a row.
for (int i = offset; i < stackSize; ++i) {
// Only print recursive functions once in a row
if (i == 0 || stackTrace[i] != stackTrace[i - 1]) {
// extract just function name from "pathToExe(functionName+offset)"
// Extract just function name from "pathToExe(functionName+offset)"
std::string_view sym = split(mangledSymbols[i], '(').second;
std::string_view offset;
std::tie(sym, offset) = split(sym, '+');
std::string_view addr;
std::tie(offset, addr) = split(offset, ')');
trace << "\tat " << Demangle(sym) << " + " << offset << addr << "\n";

trace << "\tat " << Demangle(sym) << " + " << offset << addr << '\n';
}
}

std::free(mangledSymbols);
#endif

return std::string{trace.str()};
#else
// backtrace_symbols not supported on android
return "";
#endif
}

} // namespace wpi
42 changes: 0 additions & 42 deletions wpiutil/src/main/native/windows/StackTrace.cpp

This file was deleted.

Loading

0 comments on commit abbc631

Please sign in to comment.