| 1 |
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 |
/* Copyright (c) 2007, Google Inc. |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions are |
| 7 |
* met: |
| 8 |
* |
| 9 |
* * Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* * Redistributions in binary form must reproduce the above |
| 12 |
* copyright notice, this list of conditions and the following disclaimer |
| 13 |
* in the documentation and/or other materials provided with the |
| 14 |
* distribution. |
| 15 |
* * Neither the name of Google Inc. nor the names of its |
| 16 |
* contributors may be used to endorse or promote products derived from |
| 17 |
* this software without specific prior written permission. |
| 18 |
* |
| 19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
* |
| 31 |
* --- |
| 32 |
* Author: Craig Silverstein |
| 33 |
*/ |
| 34 |
|
| 35 |
#ifndef _WIN32 |
| 36 |
# error You should only be including windows/port.cc in a windows environment! |
| 37 |
#endif |
| 38 |
|
| 39 |
#include "config.h" |
| 40 |
|
| 41 |
#include <string.h> // for strlen(), memset(), memcmp() |
| 42 |
#include <assert.h> |
| 43 |
#include <stdarg.h> // for va_list, va_start, va_end |
| 44 |
#include <algorithm> // for std:{min,max} |
| 45 |
#include <windows.h> |
| 46 |
#include "port.h" |
| 47 |
#include "base/logging.h" |
| 48 |
#include "base/spinlock.h" |
| 49 |
#include "internal_logging.h" |
| 50 |
|
| 51 |
// ----------------------------------------------------------------------- |
| 52 |
// Basic libraries |
| 53 |
|
| 54 |
PERFTOOLS_DLL_DECL |
| 55 |
int getpagesize() { |
| 56 |
static int pagesize = 0; |
| 57 |
if (pagesize == 0) { |
| 58 |
SYSTEM_INFO system_info; |
| 59 |
GetSystemInfo(&system_info); |
| 60 |
pagesize = std::max(system_info.dwPageSize, |
| 61 |
system_info.dwAllocationGranularity); |
| 62 |
} |
| 63 |
return pagesize; |
| 64 |
} |
| 65 |
|
| 66 |
extern "C" PERFTOOLS_DLL_DECL void* __sbrk(ptrdiff_t increment) { |
| 67 |
LOG(FATAL, "Windows doesn't implement sbrk!\n"); |
| 68 |
return NULL; |
| 69 |
} |
| 70 |
|
| 71 |
// We need to write to 'stderr' without having windows allocate memory. |
| 72 |
// The safest way is via a low-level call like WriteConsoleA(). But |
| 73 |
// even then we need to be sure to print in small bursts so as to not |
| 74 |
// require memory allocation. |
| 75 |
extern "C" PERFTOOLS_DLL_DECL void WriteToStderr(const char* buf, int len) { |
| 76 |
// Looks like windows allocates for writes of >80 bytes |
| 77 |
for (int i = 0; i < len; i += 80) { |
| 78 |
write(STDERR_FILENO, buf + i, std::min(80, len - i)); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
// ----------------------------------------------------------------------- |
| 84 |
// Threads code |
| 85 |
|
| 86 |
// Windows doesn't support pthread_key_create's destr_function, and in |
| 87 |
// fact it's a bit tricky to get code to run when a thread exits. This |
| 88 |
// is cargo-cult magic from https://www.codeproject.com/Articles/8113/Thread-Local-Storage-The-C-Way |
| 89 |
// and http://lallouslab.net/2017/05/30/using-cc-tls-callbacks-in-visual-studio-with-your-32-or-64bits-programs/. |
| 90 |
// This code is for VC++ 7.1 and later; VC++ 6.0 support is possible |
| 91 |
// but more busy-work -- see the webpage for how to do it. If all |
| 92 |
// this fails, we could use DllMain instead. The big problem with |
| 93 |
// DllMain is it doesn't run if this code is statically linked into a |
| 94 |
// binary (it also doesn't run if the thread is terminated via |
| 95 |
// TerminateThread, which if we're lucky this routine does). |
| 96 |
|
| 97 |
// Force a reference to _tls_used to make the linker create the TLS directory |
| 98 |
// if it's not already there (that is, even if __declspec(thread) is not used). |
| 99 |
// Force a reference to p_thread_callback_tcmalloc and p_process_term_tcmalloc |
| 100 |
// to prevent whole program optimization from discarding the variables. |
| 101 |
#ifdef _MSC_VER |
| 102 |
#if defined(_M_IX86) |
| 103 |
#pragma comment(linker, "/INCLUDE:__tls_used") |
| 104 |
#pragma comment(linker, "/INCLUDE:_p_thread_callback_tcmalloc") |
| 105 |
#pragma comment(linker, "/INCLUDE:_p_process_term_tcmalloc") |
| 106 |
#elif defined(_M_X64) |
| 107 |
#pragma comment(linker, "/INCLUDE:_tls_used") |
| 108 |
#pragma comment(linker, "/INCLUDE:p_thread_callback_tcmalloc") |
| 109 |
#pragma comment(linker, "/INCLUDE:p_process_term_tcmalloc") |
| 110 |
#endif |
| 111 |
#endif |
| 112 |
|
| 113 |
// When destr_fn eventually runs, it's supposed to take as its |
| 114 |
// argument the tls-value associated with key that pthread_key_create |
| 115 |
// creates. (Yeah, it sounds confusing but it's really not.) We |
| 116 |
// store the destr_fn/key pair in this data structure. Because we |
| 117 |
// store this in a single var, this implies we can only have one |
| 118 |
// destr_fn in a program! That's enough in practice. If asserts |
| 119 |
// trigger because we end up needing more, we'll have to turn this |
| 120 |
// into an array. |
| 121 |
struct DestrFnClosure { |
| 122 |
void (*destr_fn)(void*); |
| 123 |
pthread_key_t key_for_destr_fn_arg; |
| 124 |
}; |
| 125 |
|
| 126 |
static DestrFnClosure destr_fn_info; // initted to all NULL/0. |
| 127 |
|
| 128 |
static int on_process_term(void) { |
| 129 |
if (destr_fn_info.destr_fn) { |
| 130 |
void *ptr = TlsGetValue(destr_fn_info.key_for_destr_fn_arg); |
| 131 |
// This shouldn't be necessary, but in Release mode, Windows |
| 132 |
// sometimes trashes the pointer in the TLS slot, so we need to |
| 133 |
// remove the pointer from the TLS slot before the thread dies. |
| 134 |
TlsSetValue(destr_fn_info.key_for_destr_fn_arg, NULL); |
| 135 |
if (ptr) // pthread semantics say not to call if ptr is NULL |
| 136 |
(*destr_fn_info.destr_fn)(ptr); |
| 137 |
} |
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
static void NTAPI on_tls_callback(HINSTANCE h, DWORD dwReason, PVOID pv) { |
| 142 |
if (dwReason == DLL_THREAD_DETACH) { // thread is being destroyed! |
| 143 |
on_process_term(); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
#ifdef _MSC_VER |
| 148 |
|
| 149 |
// extern "C" suppresses C++ name mangling so we know the symbol names |
| 150 |
// for the linker /INCLUDE:symbol pragmas above. |
| 151 |
// Note that for some unknown reason, the extern "C" {} construct is ignored |
| 152 |
// by the MSVC VS2017 compiler (at least) when a const modifier is used |
| 153 |
#if defined(_M_IX86) |
| 154 |
extern "C" { |
| 155 |
// In x86, the PE loader looks for callbacks in a data segment |
| 156 |
#pragma data_seg(push, old_seg) |
| 157 |
#pragma data_seg(".CRT$XLB") |
| 158 |
void (NTAPI *p_thread_callback_tcmalloc)( |
| 159 |
HINSTANCE h, DWORD dwReason, PVOID pv) = on_tls_callback; |
| 160 |
#pragma data_seg(".CRT$XTU") |
| 161 |
int (*p_process_term_tcmalloc)(void) = on_process_term; |
| 162 |
#pragma data_seg(pop, old_seg) |
| 163 |
} // extern "C" |
| 164 |
#elif defined(_M_X64) |
| 165 |
// In x64, the PE loader looks for callbacks in a constant segment |
| 166 |
#pragma const_seg(push, oldseg) |
| 167 |
#pragma const_seg(".CRT$XLB") |
| 168 |
extern "C" void (NTAPI * const p_thread_callback_tcmalloc)( |
| 169 |
HINSTANCE h, DWORD dwReason, PVOID pv) = on_tls_callback; |
| 170 |
#pragma const_seg(".CRT$XTU") |
| 171 |
extern "C" int (NTAPI * const p_process_term_tcmalloc)(void) = on_process_term; |
| 172 |
#pragma const_seg(pop, oldseg) |
| 173 |
#endif |
| 174 |
|
| 175 |
#else // #ifdef _MSC_VER [probably msys/mingw] |
| 176 |
|
| 177 |
// We have to try the DllMain solution here, because we can't use the |
| 178 |
// msvc-specific pragmas. |
| 179 |
BOOL WINAPI DllMain(HINSTANCE h, DWORD dwReason, PVOID pv) { |
| 180 |
if (dwReason == DLL_THREAD_DETACH) |
| 181 |
on_tls_callback(h, dwReason, pv); |
| 182 |
else if (dwReason == DLL_PROCESS_DETACH) |
| 183 |
on_process_term(); |
| 184 |
return TRUE; |
| 185 |
} |
| 186 |
|
| 187 |
#endif // #ifdef _MSC_VER |
| 188 |
|
| 189 |
extern "C" pthread_key_t PthreadKeyCreate(void (*destr_fn)(void*)) { |
| 190 |
// Semantics are: we create a new key, and then promise to call |
| 191 |
// destr_fn with TlsGetValue(key) when the thread is destroyed |
| 192 |
// (as long as TlsGetValue(key) is not NULL). |
| 193 |
pthread_key_t key = TlsAlloc(); |
| 194 |
if (destr_fn) { // register it |
| 195 |
// If this assert fails, we'll need to support an array of destr_fn_infos |
| 196 |
assert(destr_fn_info.destr_fn == NULL); |
| 197 |
destr_fn_info.destr_fn = destr_fn; |
| 198 |
destr_fn_info.key_for_destr_fn_arg = key; |
| 199 |
} |
| 200 |
return key; |
| 201 |
} |
| 202 |
|
| 203 |
// ----------------------------------------------------------------------- |
| 204 |
// These functions rework existing functions of the same name in the |
| 205 |
// Google codebase. |
| 206 |
|
| 207 |
// A replacement for HeapProfiler::CleanupOldProfiles. |
| 208 |
void DeleteMatchingFiles(const char* prefix, const char* full_glob) { |
| 209 |
WIN32_FIND_DATAA found; // that final A is for Ansi (as opposed to Unicode) |
| 210 |
HANDLE hFind = FindFirstFileA(full_glob, &found); // A is for Ansi |
| 211 |
if (hFind != INVALID_HANDLE_VALUE) { |
| 212 |
const int prefix_length = strlen(prefix); |
| 213 |
do { |
| 214 |
const char *fname = found.cFileName; |
| 215 |
if ((strlen(fname) >= prefix_length) && |
| 216 |
(memcmp(fname, prefix, prefix_length) == 0)) { |
| 217 |
RAW_VLOG(0, "Removing old heap profile %s\n", fname); |
| 218 |
// TODO(csilvers): we really need to unlink dirname + fname |
| 219 |
_unlink(fname); |
| 220 |
} |
| 221 |
} while (FindNextFileA(hFind, &found) != FALSE); // A is for Ansi |
| 222 |
FindClose(hFind); |
| 223 |
} |
| 224 |
} |