Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "wpi/Demangle.h" |
| 6 | |
| 7 | #include <windows.h> // NOLINT(build/include_order) |
| 8 | |
| 9 | #include <dbghelp.h> |
| 10 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 11 | #include "wpi/SmallString.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 12 | #include "wpi/mutex.h" |
| 13 | |
| 14 | #pragma comment(lib, "Dbghelp.lib") |
| 15 | |
| 16 | namespace wpi { |
| 17 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 18 | std::string Demangle(std::string_view mangledSymbol) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 19 | static wpi::mutex m; |
| 20 | std::scoped_lock lock(m); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 21 | SmallString<128> buf{mangledSymbol}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 22 | char buffer[256]; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 23 | DWORD sz = UnDecorateSymbolName(buf.c_str(), buffer, sizeof(buffer), |
| 24 | UNDNAME_COMPLETE); |
| 25 | if (sz == 0) |
| 26 | return std::string{mangledSymbol}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 27 | return std::string(buffer, sz); |
| 28 | } |
| 29 | |
| 30 | } // namespace wpi |