Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/absl/debugging/symbolize_win32.inc is written in an unsupported language. File is not indexed.

0001 // Copyright 2018 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 
0015 // See "Retrieving Symbol Information by Address":
0016 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms680578(v=vs.85).aspx
0017 
0018 #include <windows.h>
0019 
0020 // MSVC header dbghelp.h has a warning for an ignored typedef.
0021 #pragma warning(push)
0022 #pragma warning(disable:4091)
0023 #include <dbghelp.h>
0024 #pragma warning(pop)
0025 
0026 #pragma comment(lib, "dbghelp.lib")
0027 
0028 #include <algorithm>
0029 #include <cstring>
0030 
0031 #include "absl/base/internal/raw_logging.h"
0032 
0033 namespace absl {
0034 ABSL_NAMESPACE_BEGIN
0035 
0036 static HANDLE process = NULL;
0037 
0038 void InitializeSymbolizer(const char*) {
0039   if (process != nullptr) {
0040     return;
0041   }
0042   process = GetCurrentProcess();
0043 
0044   // Symbols are not loaded until a reference is made requiring the
0045   // symbols be loaded. This is the fastest, most efficient way to use
0046   // the symbol handler.
0047   SymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_UNDNAME);
0048   if (!SymInitialize(process, nullptr, true)) {
0049     // GetLastError() returns a Win32 DWORD, but we assign to
0050     // unsigned long long to simplify the ABSL_RAW_LOG case below.  The uniform
0051     // initialization guarantees this is not a narrowing conversion.
0052     const unsigned long long error{GetLastError()};  // NOLINT(runtime/int)
0053     ABSL_RAW_LOG(FATAL, "SymInitialize() failed: %llu", error);
0054   }
0055 }
0056 
0057 bool Symbolize(const void* pc, char* out, int out_size) {
0058   if (out_size <= 0) {
0059     return false;
0060   }
0061   alignas(SYMBOL_INFO) char buf[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
0062   SYMBOL_INFO* symbol = reinterpret_cast<SYMBOL_INFO*>(buf);
0063   symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
0064   symbol->MaxNameLen = MAX_SYM_NAME;
0065   if (!SymFromAddr(process, reinterpret_cast<DWORD64>(pc), nullptr, symbol)) {
0066     return false;
0067   }
0068   const size_t out_size_t = static_cast<size_t>(out_size);
0069   strncpy(out, symbol->Name, out_size_t);
0070   if (out[out_size_t - 1] != '\0') {
0071     // strncpy() does not '\0' terminate when it truncates.
0072     static constexpr char kEllipsis[] = "...";
0073     size_t ellipsis_size =
0074         std::min(sizeof(kEllipsis) - 1, out_size_t - 1);
0075     memcpy(out + out_size_t - ellipsis_size - 1, kEllipsis, ellipsis_size);
0076     out[out_size_t - 1] = '\0';
0077   }
0078   return true;
0079 }
0080 
0081 ABSL_NAMESPACE_END
0082 }  // namespace absl