| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | // See "Retrieving Symbol Information by Address": |
| 16 | // https://msdn.microsoft.com/en-us/library/windows/desktop/ms680578(v=vs.85).aspx |
| 17 |
| 18 | #include <windows.h> |
| 19 |
| 20 | // MSVC header dbghelp.h has a warning for an ignored typedef. |
| 21 | #pragma warning(push) |
| 22 | #pragma warning(disable:4091) |
| 23 | #include <dbghelp.h> |
| 24 | #pragma warning(pop) |
| 25 |
| 26 | #pragma comment(lib, "dbghelp.lib") |
| 27 |
| 28 | #include <algorithm> |
| 29 | #include <cstring> |
| 30 |
| 31 | #include "absl/base/internal/raw_logging.h" |
| 32 |
| 33 | namespace absl { |
| 34 | ABSL_NAMESPACE_BEGIN |
| 35 |
| 36 | static HANDLE process = NULL; |
| 37 |
| 38 | void InitializeSymbolizer(const char*) { |
| 39 | if (process != nullptr) { |
| 40 | return; |
| 41 | } |
| 42 | process = GetCurrentProcess(); |
| 43 |
| 44 | // Symbols are not loaded until a reference is made requiring the |
| 45 | // symbols be loaded. This is the fastest, most efficient way to use |
| 46 | // the symbol handler. |
| 47 | SymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_UNDNAME); |
| 48 | if (!SymInitialize(process, nullptr, true)) { |
| 49 | // GetLastError() returns a Win32 DWORD, but we assign to |
| 50 | // unsigned long long to simplify the ABSL_RAW_LOG case below. The uniform |
| 51 | // initialization guarantees this is not a narrowing conversion. |
| 52 | const unsigned long long error{GetLastError()}; // NOLINT(runtime/int) |
| 53 | ABSL_RAW_LOG(FATAL, "SymInitialize() failed: %llu", error); |
| 54 | } |
| 55 | } |
| 56 |
| 57 | bool Symbolize(const void* pc, char* out, int out_size) { |
| 58 | if (out_size <= 0) { |
| 59 | return false; |
| 60 | } |
| 61 | alignas(SYMBOL_INFO) char buf[sizeof(SYMBOL_INFO) + MAX_SYM_NAME]; |
| 62 | SYMBOL_INFO* symbol = reinterpret_cast<SYMBOL_INFO*>(buf); |
| 63 | symbol->SizeOfStruct = sizeof(SYMBOL_INFO); |
| 64 | symbol->MaxNameLen = MAX_SYM_NAME; |
| 65 | if (!SymFromAddr(process, reinterpret_cast<DWORD64>(pc), nullptr, symbol)) { |
| 66 | return false; |
| 67 | } |
| 68 | const size_t out_size_t = static_cast<size_t>(out_size); |
| 69 | strncpy(out, symbol->Name, out_size_t); |
| 70 | if (out[out_size_t - 1] != '\0') { |
| 71 | // strncpy() does not '\0' terminate when it truncates. |
| 72 | static constexpr char kEllipsis[] = "..."; |
| 73 | size_t ellipsis_size = |
| 74 | std::min(sizeof(kEllipsis) - 1, out_size_t - 1); |
| 75 | memcpy(out + out_size_t - ellipsis_size - 1, kEllipsis, ellipsis_size); |
| 76 | out[out_size_t - 1] = '\0'; |
| 77 | } |
| 78 | return true; |
| 79 | } |
| 80 |
| 81 | ABSL_NAMESPACE_END |
| 82 | } // namespace absl |
| 83 |