1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "utilities/globalDefinitions.hpp" 27 #include "symbolengine.hpp" 28 #include "utilities/debug.hpp" 29 #include "windbghelp.hpp" 30 31 #include <windows.h> 32 33 #include <imagehlp.h> 34 #include <psapi.h> 35 36 37 38 // This code may be invoked normally but also as part of error reporting 39 // In the latter case, we may run under tight memory constraints (native oom) 40 // or in a stack overflow situation or the C heap may be corrupted. We may 41 // run very early before VM initialization or very late when C exit handlers 42 // run. In all these cases, callstacks would still be nice, so lets be robust. 43 // 44 // We need a number of buffers - for the pdb search path, module handle 45 // lists, for demangled symbols, etc. 46 // 47 // These buffers, while typically small, may need to be large for corner 48 // cases (e.g. templatized C++ symbols, or many DLLs loaded). Where do we 49 // allocate them? 50 // 51 // We may be in error handling for a stack overflow, so lets not put them on 52 // the stack. 53 // 54 // Dynamically allocating them may fail if we are handling a native OOM. It 55 // is also a bit dangerous, as the C heap may be corrupted already. 56 // 57 // That leaves pre-allocating them globally, which is safe and should always 58 // work (if we synchronize access) but incurs an undesirable footprint for 59 // non-error cases. 60 // 61 // We follow a two-way strategy: Allocate the buffers on the C heap in a 62 // reasonable large size. Failing that, fall back to static preallocated 63 // buffers. The size of the latter is large enough to handle common scenarios 64 // but small enough not to drive up the footprint too much (several kb). 65 // 66 // We keep these buffers around once allocated, for subsequent requests. This 67 // means that by running the initialization early at a safe time - before 68 // any error happens - buffers can be pre-allocated. This increases the chance 69 // of useful callstacks in error scenarios in exchange for a some cycles spent 70 // at startup. This behavior can be controlled with -XX:+InitializeDbgHelpEarly 71 // and is off by default. 72 73 /////// 74 75 // A simple buffer which attempts to allocate an optimal size but will 76 // fall back to a static minimally sized array on allocation error. 77 template <class T, int MINIMAL_CAPACITY, int OPTIMAL_CAPACITY> 78 class SimpleBufferWithFallback { 79 T _fallback_buffer[MINIMAL_CAPACITY]; 80 T* _p; 81 int _capacity; 82 83 // A sentinel at the end of the buffer to catch overflows. 84 void imprint_sentinel() { 85 assert(_p && _capacity > 0, "Buffer must be allocated"); 86 _p[_capacity - 1] = (T)'X'; 87 _capacity --; 88 } 89 90 public: 91 92 SimpleBufferWithFallback<T, MINIMAL_CAPACITY, OPTIMAL_CAPACITY> () 93 : _p(NULL), _capacity(0) 94 {} 95 96 // Note: no destructor because these buffers should, once 97 // allocated, live until process end. 98 // ~SimpleBufferWithFallback() 99 100 virtual void initialize () { 101 assert(_p == NULL && _capacity == 0, "Only call once."); 102 const size_t bytes = OPTIMAL_CAPACITY * sizeof(T); 103 T* q = (T*) ::malloc(bytes); 104 if (q != NULL) { 105 _p = q; 106 _capacity = OPTIMAL_CAPACITY; 107 } else { 108 _p = _fallback_buffer; 109 _capacity = (int)(sizeof(_fallback_buffer) / sizeof(T)); 110 } 111 _p[0] = '\0'; 112 imprint_sentinel(); 113 } 114 115 // We need a way to reset the buffer to fallback size for one special 116 // case, where two buffers need to be of identical capacity. 117 void reset_to_fallback_capacity() { 118 if (_p != _fallback_buffer) { 119 ::free(_p); 120 } 121 _p = _fallback_buffer; 122 _capacity = (int)(sizeof(_fallback_buffer) / sizeof(T)); 123 _p[0] = '\0'; 124 imprint_sentinel(); 125 } 126 127 T* ptr() { return _p; } 128 const T* ptr() const { return _p; } 129 int capacity() const { return _capacity; } 130 131 #ifdef ASSERT 132 void check() const { 133 assert(_p[_capacity] == (T)'X', "sentinel lost"); 134 } 135 #else 136 void check() const {} 137 #endif 138 139 }; 140 141 //// 142 143 // ModuleHandleArray: a list holding module handles. Needs to be large enough 144 // to hold one handle per loaded DLL. 145 // Note: a standard OpenJDK loads normally ~30 libraries, including system 146 // libraries, without third party libraries. 147 148 typedef SimpleBufferWithFallback <HMODULE, 48, 512> ModuleHandleArrayBase; 149 150 class ModuleHandleArray : public ModuleHandleArrayBase { 151 152 int _num; // Number of handles in this array (may be < capacity). 153 154 public: 155 156 void initialize() { 157 ModuleHandleArrayBase::initialize(); 158 _num = 0; 159 } 160 161 int num() const { return _num; } 162 void set_num(int n) { 163 assert(n <= capacity(), "Too large"); 164 _num = n; 165 } 166 167 // Compare with another list; returns true if all handles are equal (incl. 168 // sort order) 169 bool equals(const ModuleHandleArray& other) const { 170 if (_num != other._num) { 171 return false; 172 } 173 if (::memcmp(ptr(), other.ptr(), _num * sizeof(HMODULE)) != 0) { 174 return false; 175 } 176 return true; 177 } 178 179 // Copy content from other list. 180 void copy_content_from(ModuleHandleArray& other) { 181 assert(capacity() == other.capacity(), "Different capacities."); 182 memcpy(ptr(), other.ptr(), other._num * sizeof(HMODULE)); 183 _num = other._num; 184 } 185 186 }; 187 188 //// 189 190 // PathBuffer: a buffer to hold and work with a pdb search PATH - a concatenation 191 // of multiple directories separated by ';'. 192 // A single directory name can be (NTFS) as long as 32K, but in reality is 193 // seldom larger than the (historical) MAX_PATH of 260. 194 195 #define MINIMUM_PDB_PATH_LENGTH MAX_PATH * 4 196 #define OPTIMAL_PDB_PATH_LENGTH MAX_PATH * 64 197 198 typedef SimpleBufferWithFallback<char, MINIMUM_PDB_PATH_LENGTH, OPTIMAL_PDB_PATH_LENGTH> PathBufferBase; 199 200 class PathBuffer: public PathBufferBase { 201 public: 202 203 // Search PDB path for a directory. Search is case insensitive. Returns 204 // true if directory was found in the path, false otherwise. 205 bool contains_directory(const char* directory) { 206 if (ptr() == NULL) { 207 return false; 208 } 209 const size_t len = strlen(directory); 210 if (len == 0) { 211 return false; 212 } 213 char* p = ptr(); 214 for(;;) { 215 char* q = strchr(p, ';'); 216 if (q != NULL) { 217 if (len == (q - p)) { 218 if (strnicmp(p, directory, len) == 0) { 219 return true; 220 } 221 } 222 p = q + 1; 223 } else { 224 // tail 225 return stricmp(p, directory) == 0 ? true : false; 226 } 227 } 228 return false; 229 } 230 231 // Appends the given directory to the path. Returns false if internal 232 // buffer size was not sufficient. 233 bool append_directory(const char* directory) { 234 const size_t len = strlen(directory); 235 if (len == 0) { 236 return false; 237 } 238 char* p = ptr(); 239 const size_t len_now = strlen(p); 240 const size_t needs_capacity = len_now + 1 + len + 1; // xxx;yy\0 241 if (needs_capacity > (size_t)capacity()) { 242 return false; // OOM 243 } 244 if (len_now > 0) { // Not the first path element. 245 p += len_now; 246 *p = ';'; 247 p ++; 248 } 249 strcpy(p, directory); 250 return true; 251 } 252 253 }; 254 255 // A simple buffer to hold one single file name. A file name can be (NTFS) as 256 // long as 32K, but in reality is seldom larger than MAX_PATH. 257 typedef SimpleBufferWithFallback<char, MAX_PATH, 8 * K> FileNameBuffer; 258 259 // A buffer to hold a C++ symbol. Usually small, but symbols may be larger for 260 // templates. 261 #define MINIMUM_SYMBOL_NAME_LEN 128 262 #define OPTIMAL_SYMBOL_NAME_LEN 1024 263 264 typedef SimpleBufferWithFallback<uint8_t, 265 sizeof(IMAGEHLP_SYMBOL64) + MINIMUM_SYMBOL_NAME_LEN, 266 sizeof(IMAGEHLP_SYMBOL64) + OPTIMAL_SYMBOL_NAME_LEN> SymbolBuffer; 267 268 static struct { 269 270 // Two buffers to hold lists of loaded modules. handles across invocations of 271 // SymbolEngine::recalc_search_path(). 272 ModuleHandleArray loaded_modules; 273 ModuleHandleArray last_loaded_modules; 274 // Buffer to retrieve and assemble the pdb search path. 275 PathBuffer search_path; 276 // Buffer to retrieve directory names for loaded modules. 277 FileNameBuffer dir_name; 278 // Buffer to retrieve decoded symbol information (in SymbolEngine::decode) 279 SymbolBuffer decode_buffer; 280 281 void initialize() { 282 search_path.initialize(); 283 dir_name.initialize(); 284 decode_buffer.initialize(); 285 286 loaded_modules.initialize(); 287 last_loaded_modules.initialize(); 288 289 // Note: both module lists must have the same capacity. If one allocation 290 // did fail, let them both fall back to the fallback size. 291 if (loaded_modules.capacity() != last_loaded_modules.capacity()) { 292 loaded_modules.reset_to_fallback_capacity(); 293 last_loaded_modules.reset_to_fallback_capacity(); 294 } 295 296 assert(search_path.capacity() > 0 && dir_name.capacity() > 0 && 297 decode_buffer.capacity() > 0 && loaded_modules.capacity() > 0 && 298 last_loaded_modules.capacity() > 0, "Init error."); 299 } 300 301 } g_buffers; 302 303 304 // Scan the loaded modules. 305 // 306 // For each loaded module, add the directory it is located in to the pdb search 307 // path, but avoid duplicates. Prior search path content is preserved. 308 // 309 // If p_search_path_was_updated is not NULL, points to a bool which, upon 310 // successful return from the function, contains true if the search path 311 // was updated, false if no update was needed because no new DLLs were 312 // loaded or unloaded. 313 // 314 // Returns true for success, false for error. 315 static bool recalc_search_path_locked(bool* p_search_path_was_updated) { 316 317 if (p_search_path_was_updated) { 318 *p_search_path_was_updated = false; 319 } 320 321 HANDLE hProcess = ::GetCurrentProcess(); 322 323 BOOL success = false; 324 325 // 1) Retrieve current set search path. 326 // (PDB search path is a global setting and someone might have modified 327 // it, so take care not to remove directories, just to add our own). 328 329 if (!WindowsDbgHelp::symGetSearchPath(hProcess, g_buffers.search_path.ptr(), 330 (int)g_buffers.search_path.capacity())) { 331 return false; 332 } 333 DEBUG_ONLY(g_buffers.search_path.check();) 334 335 // 2) Retrieve list of modules handles of all currently loaded modules. 336 DWORD bytes_needed = 0; 337 const DWORD buffer_capacity_bytes = (DWORD)g_buffers.loaded_modules.capacity() * sizeof(HMODULE); 338 success = ::EnumProcessModules(hProcess, g_buffers.loaded_modules.ptr(), 339 buffer_capacity_bytes, &bytes_needed); 340 DEBUG_ONLY(g_buffers.loaded_modules.check();) 341 342 // Note: EnumProcessModules is sloppily defined in terms of whether a 343 // too-small output buffer counts as error. Will it truncate but still 344 // return TRUE? Nobody knows and the manpage is not telling. So we count 345 // truncation it as error, disregarding the return value. 346 if (!success || bytes_needed > buffer_capacity_bytes) { 347 return false; 348 } else { 349 const int num_modules = bytes_needed / sizeof(HMODULE); 350 g_buffers.loaded_modules.set_num(num_modules); 351 } 352 353 // Compare the list of module handles with the last list. If the lists are 354 // identical, no additional dlls were loaded and we can stop. 355 if (g_buffers.loaded_modules.equals(g_buffers.last_loaded_modules)) { 356 return true; 357 } else { 358 // Remember the new set of module handles and continue. 359 g_buffers.last_loaded_modules.copy_content_from(g_buffers.loaded_modules); 360 } 361 362 // 3) For each loaded module: retrieve directory from which it was loaded. 363 // Add directory to search path (but avoid duplicates). 364 365 bool did_modify_searchpath = false; 366 367 for (int i = 0; i < (int)g_buffers.loaded_modules.num(); i ++) { 368 369 const HMODULE hMod = g_buffers.loaded_modules.ptr()[i]; 370 char* const filebuffer = g_buffers.dir_name.ptr(); 371 const int file_buffer_capacity = g_buffers.dir_name.capacity(); 372 const int len_returned = (int)::GetModuleFileName(hMod, filebuffer, (DWORD)file_buffer_capacity); 373 DEBUG_ONLY(g_buffers.dir_name.check();) 374 if (len_returned == 0) { 375 // Error. This is suspicious - this may happen if a module has just been 376 // unloaded concurrently after our call to EnumProcessModules and 377 // GetModuleFileName, but probably just indicates a coding error. 378 assert(false, "GetModuleFileName failed (%u)", ::GetLastError()); 379 } else if (len_returned == file_buffer_capacity) { 380 // Truncation. Just skip this module and continue with the next module. 381 continue; 382 } 383 384 // Cut file name part off. 385 char* last_slash = ::strrchr(filebuffer, '\\'); 386 if (last_slash == NULL) { 387 last_slash = ::strrchr(filebuffer, '/'); 388 } 389 if (last_slash) { 390 *last_slash = '\0'; 391 } 392 393 // If this is already part of the search path, ignore it, otherwise 394 // append to search path. 395 if (!g_buffers.search_path.contains_directory(filebuffer)) { 396 if (!g_buffers.search_path.append_directory(filebuffer)) { 397 return false; // oom 398 } 399 DEBUG_ONLY(g_buffers.search_path.check();) 400 did_modify_searchpath = true; 401 } 402 403 } // for each loaded module. 404 405 // If we did not modify the search path, nothing further needs to be done. 406 if (!did_modify_searchpath) { 407 return true; 408 } 409 410 // Set the search path to its new value. 411 if (!WindowsDbgHelp::symSetSearchPath(hProcess, g_buffers.search_path.ptr())) { 412 return false; 413 } 414 415 if (p_search_path_was_updated) { 416 *p_search_path_was_updated = true; 417 } 418 419 return true; 420 421 } 422 423 static bool demangle_locked(const char* symbol, char *buf, int buflen) { 424 425 return WindowsDbgHelp::unDecorateSymbolName(symbol, buf, buflen, UNDNAME_COMPLETE) > 0; 426 427 } 428 429 static bool decode_locked(const void* addr, char* buf, int buflen, int* offset, bool do_demangle) { 430 431 assert(g_buffers.decode_buffer.capacity() >= (sizeof(IMAGEHLP_SYMBOL64) + MINIMUM_SYMBOL_NAME_LEN), 432 "Decode buffer too small."); 433 assert(buf != NULL && buflen > 0 && offset != NULL, "invalid output buffer."); 434 435 DWORD64 displacement; 436 PIMAGEHLP_SYMBOL64 pSymbol = NULL; 437 bool success = false; 438 439 pSymbol = (PIMAGEHLP_SYMBOL64) g_buffers.decode_buffer.ptr(); 440 pSymbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64); 441 pSymbol->MaxNameLength = (DWORD)(g_buffers.decode_buffer.capacity() - sizeof(IMAGEHLP_SYMBOL64) - 1); 442 443 // It is unclear how SymGetSymFromAddr64 handles truncation. Experiments 444 // show it will return TRUE but not zero terminate (which is a really bad 445 // combination). Lets be super careful. 446 ::memset(pSymbol->Name, 0, pSymbol->MaxNameLength); // To catch truncation. 447 448 if (WindowsDbgHelp::symGetSymFromAddr64(::GetCurrentProcess(), (DWORD64)addr, &displacement, pSymbol)) { 449 success = true; 450 if (pSymbol->Name[pSymbol->MaxNameLength - 1] != '\0') { 451 // Symbol was truncated. Do not attempt to demangle. Instead, zero terminate the 452 // truncated string. We still return success - the truncated string may still 453 // be usable for the caller. 454 pSymbol->Name[pSymbol->MaxNameLength - 1] = '\0'; 455 do_demangle = false; 456 } 457 458 // Attempt to demangle. 459 if (do_demangle && demangle_locked(pSymbol->Name, buf, buflen)) { 460 // ok. 461 } else { 462 ::strncpy(buf, pSymbol->Name, buflen - 1); 463 } 464 buf[buflen - 1] = '\0'; 465 466 *offset = (int)displacement; 467 } 468 469 DEBUG_ONLY(g_buffers.decode_buffer.check();) 470 471 return success; 472 } 473 474 static enum { 475 state_uninitialized = 0, 476 state_ready = 1, 477 state_error = 2 478 } g_state = state_uninitialized; 479 480 static void initialize() { 481 482 assert(g_state == state_uninitialized, "wrong sequence"); 483 g_state = state_error; 484 485 // 1) Initialize buffers. 486 g_buffers.initialize(); 487 488 // 1) Call SymInitialize 489 HANDLE hProcess = ::GetCurrentProcess(); 490 WindowsDbgHelp::symSetOptions(SYMOPT_FAIL_CRITICAL_ERRORS | SYMOPT_DEFERRED_LOADS | 491 SYMOPT_EXACT_SYMBOLS | SYMOPT_LOAD_LINES); 492 if (!WindowsDbgHelp::symInitialize(hProcess, NULL, TRUE)) { 493 return; 494 } 495 496 // Note: we ignore any errors from this point on. The symbol engine may be 497 // usable enough. 498 g_state = state_ready; 499 500 (void)recalc_search_path_locked(NULL); 501 502 } 503 504 ///////////////////// External functions ////////////////////////// 505 506 // All outside facing functions are synchronized. Also, we run 507 // initialization on first touch. 508 509 static CRITICAL_SECTION g_cs; 510 511 namespace { // Do not export. 512 class SymbolEngineEntry { 513 public: 514 SymbolEngineEntry() { 515 ::EnterCriticalSection(&g_cs); 516 if (g_state == state_uninitialized) { 517 initialize(); 518 } 519 } 520 ~SymbolEngineEntry() { 521 ::LeaveCriticalSection(&g_cs); 522 } 523 }; 524 } 525 526 // Called at DLL_PROCESS_ATTACH. 527 void SymbolEngine::pre_initialize() { 528 ::InitializeCriticalSection(&g_cs); 529 } 530 531 bool SymbolEngine::decode(const void* addr, char* buf, int buflen, int* offset, bool do_demangle) { 532 533 assert(buf != NULL && buflen > 0 && offset != NULL, "Argument error"); 534 buf[0] = '\0'; 535 *offset = -1; 536 537 if (addr == NULL) { 538 return false; 539 } 540 541 SymbolEngineEntry entry_guard; 542 543 // Try decoding the symbol once. If we fail, attempt to rebuild the 544 // symbol search path - maybe the pc points to a dll whose pdb file is 545 // outside our search path. Then do attempt the decode again. 546 bool success = decode_locked(addr, buf, buflen, offset, do_demangle); 547 if (!success) { 548 bool did_update_search_path = false; 549 if (recalc_search_path_locked(&did_update_search_path)) { 550 if (did_update_search_path) { 551 success = decode_locked(addr, buf, buflen, offset, do_demangle); 552 } 553 } 554 } 555 556 return success; 557 558 } 559 560 bool SymbolEngine::demangle(const char* symbol, char *buf, int buflen) { 561 562 SymbolEngineEntry entry_guard; 563 564 return demangle_locked(symbol, buf, buflen); 565 566 } 567 568 bool SymbolEngine::recalc_search_path(bool* p_search_path_was_updated) { 569 570 SymbolEngineEntry entry_guard; 571 572 return recalc_search_path_locked(p_search_path_was_updated); 573 574 } 575 576 bool SymbolEngine::get_source_info(const void* addr, char* buf, size_t buflen, 577 int* line_no) 578 { 579 assert(buf != NULL && buflen > 0 && line_no != NULL, "Argument error"); 580 buf[0] = '\0'; 581 *line_no = -1; 582 583 if (addr == NULL) { 584 return false; 585 } 586 587 SymbolEngineEntry entry_guard; 588 589 IMAGEHLP_LINE64 lineinfo; 590 memset(&lineinfo, 0, sizeof(lineinfo)); 591 lineinfo.SizeOfStruct = sizeof(lineinfo); 592 DWORD displacement; 593 if (WindowsDbgHelp::symGetLineFromAddr64(::GetCurrentProcess(), (DWORD64)addr, 594 &displacement, &lineinfo)) { 595 if (buf != NULL && buflen > 0 && lineinfo.FileName != NULL) { 596 // We only return the file name, not the whole path. 597 char* p = lineinfo.FileName; 598 char* q = strrchr(lineinfo.FileName, '\\'); 599 if (q) { 600 p = q + 1; 601 } 602 ::strncpy(buf, p, buflen - 1); 603 } 604 if (line_no != 0) { 605 *line_no = lineinfo.LineNumber; 606 } 607 return true; 608 } 609 return false; 610 } 611 612 // Print one liner describing state (if library loaded, which functions are 613 // missing - if any, and the dbhelp API version) 614 void SymbolEngine::print_state_on(outputStream* st) { 615 616 SymbolEngineEntry entry_guard; 617 618 st->print("symbol engine: "); 619 620 if (g_state == state_uninitialized) { 621 st->print("uninitialized."); 622 } else if (g_state == state_error) { 623 st->print("initialization error."); 624 } else { 625 st->print("initialized successfully"); 626 st->print(" - sym options: 0x%X", WindowsDbgHelp::symGetOptions()); 627 st->print(" - pdb path: "); 628 if (WindowsDbgHelp::symGetSearchPath(::GetCurrentProcess(), 629 g_buffers.search_path.ptr(), 630 (int)g_buffers.search_path.capacity())) { 631 st->print_raw(g_buffers.search_path.ptr()); 632 } else { 633 st->print_raw("(cannot be retrieved)"); 634 } 635 } 636 st->cr(); 637 638 }