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