| 1 |
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 |
// Copyright (c) 2006, 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 |
#include <config.h> |
| 32 |
#if (defined(_WIN32) || defined(__MINGW32__)) && !defined(__CYGWIN__) && !defined(__CYGWIN32) |
| 33 |
# define PLATFORM_WINDOWS 1 |
| 34 |
#endif |
| 35 |
|
| 36 |
#include "base/sysinfo.h" |
| 37 |
#include "base/commandlineflags.h" |
| 38 |
#include "base/dynamic_annotations.h" // for RunningOnValgrind |
| 39 |
#include "base/logging.h" |
| 40 |
|
| 41 |
#include <tuple> |
| 42 |
|
| 43 |
#include <ctype.h> // for isspace() |
| 44 |
#include <stdlib.h> // for getenv() |
| 45 |
#include <stdio.h> // for snprintf(), sscanf() |
| 46 |
#include <string.h> // for memmove(), memchr(), etc. |
| 47 |
#include <fcntl.h> // for open() |
| 48 |
#include <errno.h> // for errno |
| 49 |
#ifdef HAVE_UNISTD_H |
| 50 |
#include <unistd.h> // for read() |
| 51 |
#endif |
| 52 |
#if defined __MACH__ // Mac OS X, almost certainly |
| 53 |
#include <mach-o/dyld.h> // for iterating over dll's in ProcMapsIter |
| 54 |
#include <mach-o/loader.h> // for iterating over dll's in ProcMapsIter |
| 55 |
#include <sys/types.h> |
| 56 |
#include <sys/sysctl.h> // how we figure out numcpu's on OS X |
| 57 |
#elif defined __FreeBSD__ |
| 58 |
#include <sys/sysctl.h> |
| 59 |
#elif defined __sun__ // Solaris |
| 60 |
#include <procfs.h> // for, e.g., prmap_t |
| 61 |
#elif defined(PLATFORM_WINDOWS) |
| 62 |
#include <process.h> // for getpid() (actually, _getpid()) |
| 63 |
#include <shlwapi.h> // for SHGetValueA() |
| 64 |
#include <tlhelp32.h> // for Module32First() |
| 65 |
#elif defined(__QNXNTO__) |
| 66 |
#include <sys/mman.h> |
| 67 |
#include <sys/sysmacros.h> |
| 68 |
#endif |
| 69 |
|
| 70 |
#ifdef PLATFORM_WINDOWS |
| 71 |
#ifdef MODULEENTRY32 |
| 72 |
// In a change from the usual W-A pattern, there is no A variant of |
| 73 |
// MODULEENTRY32. Tlhelp32.h #defines the W variant, but not the A. |
| 74 |
// In unicode mode, tlhelp32.h #defines MODULEENTRY32 to be |
| 75 |
// MODULEENTRY32W. These #undefs are the only way I see to get back |
| 76 |
// access to the original, ascii struct (and related functions). |
| 77 |
#undef MODULEENTRY32 |
| 78 |
#undef Module32First |
| 79 |
#undef Module32Next |
| 80 |
#undef PMODULEENTRY32 |
| 81 |
#undef LPMODULEENTRY32 |
| 82 |
#endif /* MODULEENTRY32 */ |
| 83 |
// MinGW doesn't seem to define this, perhaps some windowsen don't either. |
| 84 |
#ifndef TH32CS_SNAPMODULE32 |
| 85 |
#define TH32CS_SNAPMODULE32 0 |
| 86 |
#endif /* TH32CS_SNAPMODULE32 */ |
| 87 |
#endif /* PLATFORM_WINDOWS */ |
| 88 |
|
| 89 |
// Re-run fn until it doesn't cause EINTR. |
| 90 |
#define NO_INTR(fn) do {} while ((fn) < 0 && errno == EINTR) |
| 91 |
|
| 92 |
// open/read/close can set errno, which may be illegal at this |
| 93 |
// time, so prefer making the syscalls directly if we can. |
| 94 |
#if HAVE_SYS_SYSCALL_H |
| 95 |
# include <sys/syscall.h> |
| 96 |
#endif |
| 97 |
#ifdef SYS_open // solaris 11, at least sometimes, only defines SYS_openat |
| 98 |
# define safeopen(filename, mode) syscall(SYS_open, filename, mode) |
| 99 |
#else |
| 100 |
# define safeopen(filename, mode) open(filename, mode) |
| 101 |
#endif |
| 102 |
#ifdef SYS_read |
| 103 |
# define saferead(fd, buffer, size) syscall(SYS_read, fd, buffer, size) |
| 104 |
#else |
| 105 |
# define saferead(fd, buffer, size) read(fd, buffer, size) |
| 106 |
#endif |
| 107 |
#ifdef SYS_close |
| 108 |
# define safeclose(fd) syscall(SYS_close, fd) |
| 109 |
#else |
| 110 |
# define safeclose(fd) close(fd) |
| 111 |
#endif |
| 112 |
|
| 113 |
// ---------------------------------------------------------------------- |
| 114 |
// GetenvBeforeMain() |
| 115 |
// GetUniquePathFromEnv() |
| 116 |
// Some non-trivial getenv-related functions. |
| 117 |
// ---------------------------------------------------------------------- |
| 118 |
|
| 119 |
// we reimplement memcmp and friends to avoid depending on any glibc |
| 120 |
// calls too early in the process lifetime. This allows us to use |
| 121 |
// GetenvBeforeMain from inside ifunc handler |
| 122 |
static int slow_memcmp(const void *_a, const void *_b, size_t n) { |
| 123 |
const uint8_t *a = reinterpret_cast<const uint8_t *>(_a); |
| 124 |
const uint8_t *b = reinterpret_cast<const uint8_t *>(_b); |
| 125 |
while (n-- != 0) { |
| 126 |
uint8_t ac = *a++; |
| 127 |
uint8_t bc = *b++; |
| 128 |
if (ac != bc) { |
| 129 |
if (ac < bc) { |
| 130 |
return -1; |
| 131 |
} |
| 132 |
return 1; |
| 133 |
} |
| 134 |
} |
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
static const char *slow_memchr(const char *s, int c, size_t n) { |
| 139 |
uint8_t ch = static_cast<uint8_t>(c); |
| 140 |
while (n--) { |
| 141 |
if (*s++ == ch) { |
| 142 |
return s - 1; |
| 143 |
} |
| 144 |
} |
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
static size_t slow_strlen(const char *s) { |
| 149 |
const char *s2 = slow_memchr(s, '\0', static_cast<size_t>(-1)); |
| 150 |
return s2 - s; |
| 151 |
} |
| 152 |
|
| 153 |
// It's not safe to call getenv() in the malloc hooks, because they |
| 154 |
// might be called extremely early, before libc is done setting up |
| 155 |
// correctly. In particular, the thread library may not be done |
| 156 |
// setting up errno. So instead, we use the built-in __environ array |
| 157 |
// if it exists, and otherwise read /proc/self/environ directly, using |
| 158 |
// system calls to read the file, and thus avoid setting errno. |
| 159 |
// /proc/self/environ has a limit of how much data it exports (around |
| 160 |
// 8K), so it's not an ideal solution. |
| 161 |
const char* GetenvBeforeMain(const char* name) { |
| 162 |
const int namelen = slow_strlen(name); |
| 163 |
#if defined(HAVE___ENVIRON) // if we have it, it's declared in unistd.h |
| 164 |
if (__environ) { // can exist but be NULL, if statically linked |
| 165 |
for (char** p = __environ; *p; p++) { |
| 166 |
if (!slow_memcmp(*p, name, namelen) && (*p)[namelen] == '=') |
| 167 |
return *p + namelen+1; |
| 168 |
} |
| 169 |
return NULL; |
| 170 |
} |
| 171 |
#endif |
| 172 |
#if defined(PLATFORM_WINDOWS) |
| 173 |
// TODO(mbelshe) - repeated calls to this function will overwrite the |
| 174 |
// contents of the static buffer. |
| 175 |
static char envvar_buf[1024]; // enough to hold any envvar we care about |
| 176 |
if (!GetEnvironmentVariableA(name, envvar_buf, sizeof(envvar_buf)-1)) |
| 177 |
return NULL; |
| 178 |
return envvar_buf; |
| 179 |
#endif |
| 180 |
// static is ok because this function should only be called before |
| 181 |
// main(), when we're single-threaded. |
| 182 |
static char envbuf[16<<10]; |
| 183 |
if (*envbuf == '\0') { // haven't read the environ yet |
| 184 |
int fd = safeopen("/proc/self/environ", O_RDONLY); |
| 185 |
// The -2 below guarantees the last two bytes of the buffer will be \0\0 |
| 186 |
if (fd == -1 || // unable to open the file, fall back onto libc |
| 187 |
saferead(fd, envbuf, sizeof(envbuf) - 2) < 0) { // error reading file |
| 188 |
RAW_VLOG(1, "Unable to open /proc/self/environ, falling back " |
| 189 |
"on getenv(\"%s\"), which may not work", name); |
| 190 |
if (fd != -1) safeclose(fd); |
| 191 |
return getenv(name); |
| 192 |
} |
| 193 |
safeclose(fd); |
| 194 |
} |
| 195 |
const char* p = envbuf; |
| 196 |
while (*p != '\0') { // will happen at the \0\0 that terminates the buffer |
| 197 |
// proc file has the format NAME=value\0NAME=value\0NAME=value\0... |
| 198 |
const char* endp = (char*)slow_memchr(p, '\0', |
| 199 |
sizeof(envbuf) - (p - envbuf)); |
| 200 |
if (endp == NULL) // this entry isn't NUL terminated |
| 201 |
return NULL; |
| 202 |
else if (!slow_memcmp(p, name, namelen) && p[namelen] == '=') // it's a match |
| 203 |
return p + namelen+1; // point after = |
| 204 |
p = endp + 1; |
| 205 |
} |
| 206 |
return NULL; // env var never found |
| 207 |
} |
| 208 |
|
| 209 |
extern "C" { |
| 210 |
const char* TCMallocGetenvSafe(const char* name) { |
| 211 |
return GetenvBeforeMain(name); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
// HPC environment auto-detection |
| 216 |
// For HPC applications (MPI, OpenSHMEM, etc), it is typical for multiple |
| 217 |
// processes not engaged in parent-child relations to be executed on the |
| 218 |
// same host. |
| 219 |
// In order to enable gperftools to analyze them, these processes need to be |
| 220 |
// assigned individual file paths for the files being used. |
| 221 |
// The function below is trying to discover well-known HPC environments and |
| 222 |
// take advantage of that environment to generate meaningful profile filenames |
| 223 |
// |
| 224 |
// Returns true iff we need to append process pid to |
| 225 |
// GetUniquePathFromEnv value. Second and third return values are |
| 226 |
// strings to be appended to path for extra identification. |
| 227 |
static std::tuple<bool, const char*, const char*> QueryHPCEnvironment() { |
| 228 |
auto mk = [] (bool a, const char* b, const char* c) { |
| 229 |
// We have to work around gcc 5 bug in tuple constructor. It |
| 230 |
// doesn't let us do {a, b, c} |
| 231 |
// |
| 232 |
// TODO(2023-09-27): officially drop gcc 5 support |
| 233 |
return std::make_tuple<bool, const char*, const char*>(std::move(a), std::move(b), std::move(c)); |
| 234 |
}; |
| 235 |
|
| 236 |
// Check for the PMIx environment |
| 237 |
const char* envval = getenv("PMIX_RANK"); |
| 238 |
if (envval != nullptr && *envval != 0) { |
| 239 |
// PMIx exposes the rank that is convenient for process identification |
| 240 |
// Don't append pid, since we have rank to differentiate. |
| 241 |
return mk(false, ".rank-", envval); |
| 242 |
} |
| 243 |
|
| 244 |
// Check for the Slurm environment |
| 245 |
envval = getenv("SLURM_JOB_ID"); |
| 246 |
if (envval != nullptr && *envval != 0) { |
| 247 |
// Slurm environment detected |
| 248 |
const char* procid = getenv("SLURM_PROCID"); |
| 249 |
if (procid != nullptr && *procid != 0) { |
| 250 |
// Use Slurm process ID to differentiate |
| 251 |
return mk(false, ".slurmid-", procid); |
| 252 |
} |
| 253 |
// Need to add PID to avoid conflicts |
| 254 |
return mk(true, "", ""); |
| 255 |
} |
| 256 |
|
| 257 |
// Check for Open MPI environment |
| 258 |
envval = getenv("OMPI_HOME"); |
| 259 |
if (envval != nullptr && *envval != 0) { |
| 260 |
return mk(true, "", ""); |
| 261 |
} |
| 262 |
|
| 263 |
// Check for Hydra process manager (MPICH) |
| 264 |
envval = getenv("PMI_RANK"); |
| 265 |
if (envval != nullptr && *envval != 0) { |
| 266 |
return mk(false, ".rank-", envval); |
| 267 |
} |
| 268 |
|
| 269 |
// No HPC environment was detected |
| 270 |
return mk(false, "", ""); |
| 271 |
} |
| 272 |
|
| 273 |
// This takes as an argument an environment-variable name (like |
| 274 |
// CPUPROFILE) whose value is supposed to be a file-path, and sets |
| 275 |
// path to that path, and returns true. If the env var doesn't exist, |
| 276 |
// or is the empty string, leave path unchanged and returns false. |
| 277 |
// The reason this is non-trivial is that this function handles munged |
| 278 |
// pathnames. Here's why: |
| 279 |
// |
| 280 |
// If we're a child process of the 'main' process, we can't just use |
| 281 |
// getenv("CPUPROFILE") -- the parent process will be using that path. |
| 282 |
// Instead we append our pid to the pathname. How do we tell if we're a |
| 283 |
// child process? Ideally we'd set an environment variable that all |
| 284 |
// our children would inherit. But -- and this is seemingly a bug in |
| 285 |
// gcc -- if you do a setenv() in a shared libarary in a global |
| 286 |
// constructor, the environment setting is lost by the time main() is |
| 287 |
// called. The only safe thing we can do in such a situation is to |
| 288 |
// modify the existing envvar. So we do a hack: in the parent, we set |
| 289 |
// the high bit of the 1st char of CPUPROFILE. In the child, we |
| 290 |
// notice the high bit is set and append the pid(). This works |
| 291 |
// assuming cpuprofile filenames don't normally have the high bit set |
| 292 |
// in their first character! If that assumption is violated, we'll |
| 293 |
// still get a profile, but one with an unexpected name. |
| 294 |
// TODO(csilvers): set an envvar instead when we can do it reliably. |
| 295 |
bool GetUniquePathFromEnv(const char* env_name, char* path) { |
| 296 |
char* envval = getenv(env_name); |
| 297 |
|
| 298 |
if (envval == nullptr || *envval == '\0') { |
| 299 |
return false; |
| 300 |
} |
| 301 |
|
| 302 |
const char* append1 = ""; |
| 303 |
const char* append2 = ""; |
| 304 |
bool pidIsForced; |
| 305 |
std::tie(pidIsForced, append1, append2) = QueryHPCEnvironment(); |
| 306 |
|
| 307 |
// Generate the "forcing" environment variable name in a form of |
| 308 |
// <ORIG_ENVAR>_USE_PID that requests PID to be used in the file names |
| 309 |
char forceVarName[256]; |
| 310 |
snprintf(forceVarName, sizeof(forceVarName), "%s_USE_PID", env_name); |
| 311 |
|
| 312 |
pidIsForced = pidIsForced || EnvToBool(forceVarName, false); |
| 313 |
|
| 314 |
// Get information about the child bit and drop it |
| 315 |
const bool childBitDetected = (*envval & 128) != 0; |
| 316 |
*envval &= ~128; |
| 317 |
|
| 318 |
if (pidIsForced || childBitDetected) { |
| 319 |
snprintf(path, PATH_MAX, "%s%s%s_%d", |
| 320 |
envval, append1, append2, getpid()); |
| 321 |
} else { |
| 322 |
snprintf(path, PATH_MAX, "%s%s%s", envval, append1, append2); |
| 323 |
} |
| 324 |
|
| 325 |
// Set the child bit for the fork'd processes, unless appending pid |
| 326 |
// was forced by either _USE_PID thingy or via MPI detection stuff. |
| 327 |
if (childBitDetected || !pidIsForced) { |
| 328 |
*envval |= 128; |
| 329 |
} |
| 330 |
return true; |
| 331 |
} |
| 332 |
|
| 333 |
int GetSystemCPUsCount() |
| 334 |
{ |
| 335 |
#if defined(PLATFORM_WINDOWS) |
| 336 |
// Get the number of processors. |
| 337 |
SYSTEM_INFO info; |
| 338 |
GetSystemInfo(&info); |
| 339 |
return info.dwNumberOfProcessors; |
| 340 |
#else |
| 341 |
long rv = sysconf(_SC_NPROCESSORS_ONLN); |
| 342 |
if (rv < 0) { |
| 343 |
return 1; |
| 344 |
} |
| 345 |
return static_cast<int>(rv); |
| 346 |
#endif |
| 347 |
} |
| 348 |
|
| 349 |
// ---------------------------------------------------------------------- |
| 350 |
|
| 351 |
#if defined __linux__ || defined __FreeBSD__ || defined __NetBSD__ || defined __sun__ || defined __CYGWIN__ || defined __CYGWIN32__ || defined __QNXNTO__ |
| 352 |
static void ConstructFilename(const char* spec, pid_t pid, |
| 353 |
char* buf, int buf_size) { |
| 354 |
CHECK_LT(snprintf(buf, buf_size, |
| 355 |
spec, |
| 356 |
static_cast<int>(pid ? pid : getpid())), buf_size); |
| 357 |
} |
| 358 |
#endif |
| 359 |
|
| 360 |
// A templatized helper function instantiated for Mach (OS X) only. |
| 361 |
// It can handle finding info for both 32 bits and 64 bits. |
| 362 |
// Returns true if it successfully handled the hdr, false else. |
| 363 |
#ifdef __MACH__ // Mac OS X, almost certainly |
| 364 |
template<uint32_t kMagic, uint32_t kLCSegment, |
| 365 |
typename MachHeader, typename SegmentCommand> |
| 366 |
static bool NextExtMachHelper(const mach_header* hdr, |
| 367 |
int current_image, int current_load_cmd, |
| 368 |
uint64 *start, uint64 *end, char **flags, |
| 369 |
uint64 *offset, int64 *inode, char **filename, |
| 370 |
uint64 *file_mapping, uint64 *file_pages, |
| 371 |
uint64 *anon_mapping, uint64 *anon_pages, |
| 372 |
dev_t *dev) { |
| 373 |
static char kDefaultPerms[5] = "r-xp"; |
| 374 |
if (hdr->magic != kMagic) |
| 375 |
return false; |
| 376 |
const char* lc = (const char *)hdr + sizeof(MachHeader); |
| 377 |
// TODO(csilvers): make this not-quadradic (increment and hold state) |
| 378 |
for (int j = 0; j < current_load_cmd; j++) // advance to *our* load_cmd |
| 379 |
lc += ((const load_command *)lc)->cmdsize; |
| 380 |
if (((const load_command *)lc)->cmd == kLCSegment) { |
| 381 |
const intptr_t dlloff = _dyld_get_image_vmaddr_slide(current_image); |
| 382 |
const SegmentCommand* sc = (const SegmentCommand *)lc; |
| 383 |
if (start) *start = sc->vmaddr + dlloff; |
| 384 |
if (end) *end = sc->vmaddr + sc->vmsize + dlloff; |
| 385 |
if (flags) *flags = kDefaultPerms; // can we do better? |
| 386 |
if (offset) *offset = sc->fileoff; |
| 387 |
if (inode) *inode = 0; |
| 388 |
if (filename) |
| 389 |
*filename = const_cast<char*>(_dyld_get_image_name(current_image)); |
| 390 |
if (file_mapping) *file_mapping = 0; |
| 391 |
if (file_pages) *file_pages = 0; // could we use sc->filesize? |
| 392 |
if (anon_mapping) *anon_mapping = 0; |
| 393 |
if (anon_pages) *anon_pages = 0; |
| 394 |
if (dev) *dev = 0; |
| 395 |
return true; |
| 396 |
} |
| 397 |
|
| 398 |
return false; |
| 399 |
} |
| 400 |
#endif |
| 401 |
|
| 402 |
// Finds |c| in |text|, and assign '\0' at the found position. |
| 403 |
// The original character at the modified position should be |c|. |
| 404 |
// A pointer to the modified position is stored in |endptr|. |
| 405 |
// |endptr| should not be NULL. |
| 406 |
static bool ExtractUntilChar(char *text, int c, char **endptr) { |
| 407 |
CHECK_NE(text, NULL); |
| 408 |
CHECK_NE(endptr, NULL); |
| 409 |
char *found; |
| 410 |
found = strchr(text, c); |
| 411 |
if (found == NULL) { |
| 412 |
*endptr = NULL; |
| 413 |
return false; |
| 414 |
} |
| 415 |
|
| 416 |
*endptr = found; |
| 417 |
*found = '\0'; |
| 418 |
return true; |
| 419 |
} |
| 420 |
|
| 421 |
// Increments |*text_pointer| while it points a whitespace character. |
| 422 |
// It is to follow sscanf's whilespace handling. |
| 423 |
static void SkipWhileWhitespace(char **text_pointer, int c) { |
| 424 |
if (isspace(c)) { |
| 425 |
while (isspace(**text_pointer) && isspace(*((*text_pointer) + 1))) { |
| 426 |
++(*text_pointer); |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
template<class T> |
| 432 |
static T StringToInteger(char *text, char **endptr, int base) { |
| 433 |
assert(false); |
| 434 |
return T(); |
| 435 |
} |
| 436 |
|
| 437 |
template<> |
| 438 |
int StringToInteger<int>(char *text, char **endptr, int base) { |
| 439 |
return strtol(text, endptr, base); |
| 440 |
} |
| 441 |
|
| 442 |
template<> |
| 443 |
int64 StringToInteger<int64>(char *text, char **endptr, int base) { |
| 444 |
return strtoll(text, endptr, base); |
| 445 |
} |
| 446 |
|
| 447 |
template<> |
| 448 |
uint64 StringToInteger<uint64>(char *text, char **endptr, int base) { |
| 449 |
return strtoull(text, endptr, base); |
| 450 |
} |
| 451 |
|
| 452 |
template<typename T> |
| 453 |
static T StringToIntegerUntilChar( |
| 454 |
char *text, int base, int c, char **endptr_result) { |
| 455 |
CHECK_NE(endptr_result, NULL); |
| 456 |
*endptr_result = NULL; |
| 457 |
|
| 458 |
char *endptr_extract; |
| 459 |
if (!ExtractUntilChar(text, c, &endptr_extract)) |
| 460 |
return 0; |
| 461 |
|
| 462 |
T result; |
| 463 |
char *endptr_strto; |
| 464 |
result = StringToInteger<T>(text, &endptr_strto, base); |
| 465 |
*endptr_extract = c; |
| 466 |
|
| 467 |
if (endptr_extract != endptr_strto) |
| 468 |
return 0; |
| 469 |
|
| 470 |
*endptr_result = endptr_extract; |
| 471 |
SkipWhileWhitespace(endptr_result, c); |
| 472 |
|
| 473 |
return result; |
| 474 |
} |
| 475 |
|
| 476 |
static char *CopyStringUntilChar( |
| 477 |
char *text, unsigned out_len, int c, char *out) { |
| 478 |
char *endptr; |
| 479 |
if (!ExtractUntilChar(text, c, &endptr)) |
| 480 |
return NULL; |
| 481 |
|
| 482 |
strncpy(out, text, out_len); |
| 483 |
out[out_len-1] = '\0'; |
| 484 |
*endptr = c; |
| 485 |
|
| 486 |
SkipWhileWhitespace(&endptr, c); |
| 487 |
return endptr; |
| 488 |
} |
| 489 |
|
| 490 |
template<typename T> |
| 491 |
static bool StringToIntegerUntilCharWithCheck( |
| 492 |
T *outptr, char *text, int base, int c, char **endptr) { |
| 493 |
*outptr = StringToIntegerUntilChar<T>(*endptr, base, c, endptr); |
| 494 |
if (*endptr == NULL || **endptr == '\0') return false; |
| 495 |
++(*endptr); |
| 496 |
return true; |
| 497 |
} |
| 498 |
|
| 499 |
static bool ParseProcMapsLine(char *text, uint64 *start, uint64 *end, |
| 500 |
char *flags, uint64 *offset, |
| 501 |
int *major, int *minor, int64 *inode, |
| 502 |
unsigned *filename_offset) { |
| 503 |
#if defined(__linux__) || defined(__NetBSD__) |
| 504 |
/* |
| 505 |
* It's similar to: |
| 506 |
* sscanf(text, "%"SCNx64"-%"SCNx64" %4s %"SCNx64" %x:%x %"SCNd64" %n", |
| 507 |
* start, end, flags, offset, major, minor, inode, filename_offset) |
| 508 |
*/ |
| 509 |
char *endptr = text; |
| 510 |
if (endptr == NULL || *endptr == '\0') return false; |
| 511 |
|
| 512 |
if (!StringToIntegerUntilCharWithCheck(start, endptr, 16, '-', &endptr)) |
| 513 |
return false; |
| 514 |
|
| 515 |
if (!StringToIntegerUntilCharWithCheck(end, endptr, 16, ' ', &endptr)) |
| 516 |
return false; |
| 517 |
|
| 518 |
endptr = CopyStringUntilChar(endptr, 5, ' ', flags); |
| 519 |
if (endptr == NULL || *endptr == '\0') return false; |
| 520 |
++endptr; |
| 521 |
|
| 522 |
if (!StringToIntegerUntilCharWithCheck(offset, endptr, 16, ' ', &endptr)) |
| 523 |
return false; |
| 524 |
|
| 525 |
if (!StringToIntegerUntilCharWithCheck(major, endptr, 16, ':', &endptr)) |
| 526 |
return false; |
| 527 |
|
| 528 |
if (!StringToIntegerUntilCharWithCheck(minor, endptr, 16, ' ', &endptr)) |
| 529 |
return false; |
| 530 |
|
| 531 |
if (!StringToIntegerUntilCharWithCheck(inode, endptr, 10, ' ', &endptr)) |
| 532 |
return false; |
| 533 |
|
| 534 |
*filename_offset = (endptr - text); |
| 535 |
return true; |
| 536 |
#else |
| 537 |
return false; |
| 538 |
#endif |
| 539 |
} |
| 540 |
|
| 541 |
ProcMapsIterator::ProcMapsIterator(pid_t pid) { |
| 542 |
Init(pid, NULL, false); |
| 543 |
} |
| 544 |
|
| 545 |
ProcMapsIterator::ProcMapsIterator(pid_t pid, Buffer *buffer) { |
| 546 |
Init(pid, buffer, false); |
| 547 |
} |
| 548 |
|
| 549 |
ProcMapsIterator::ProcMapsIterator(pid_t pid, Buffer *buffer, |
| 550 |
bool use_maps_backing) { |
| 551 |
Init(pid, buffer, use_maps_backing); |
| 552 |
} |
| 553 |
|
| 554 |
void ProcMapsIterator::Init(pid_t pid, Buffer *buffer, |
| 555 |
bool use_maps_backing) { |
| 556 |
pid_ = pid; |
| 557 |
using_maps_backing_ = use_maps_backing; |
| 558 |
dynamic_buffer_ = NULL; |
| 559 |
if (!buffer) { |
| 560 |
// If the user didn't pass in any buffer storage, allocate it |
| 561 |
// now. This is the normal case; the signal handler passes in a |
| 562 |
// static buffer. |
| 563 |
buffer = dynamic_buffer_ = new Buffer; |
| 564 |
} else { |
| 565 |
dynamic_buffer_ = NULL; |
| 566 |
} |
| 567 |
|
| 568 |
ibuf_ = buffer->buf_; |
| 569 |
|
| 570 |
stext_ = etext_ = nextline_ = ibuf_; |
| 571 |
ebuf_ = ibuf_ + Buffer::kBufSize - 1; |
| 572 |
nextline_ = ibuf_; |
| 573 |
|
| 574 |
#if defined(__linux__) || defined(__NetBSD__) || defined(__CYGWIN__) || defined(__CYGWIN32__) |
| 575 |
if (use_maps_backing) { // don't bother with clever "self" stuff in this case |
| 576 |
ConstructFilename("/proc/%d/maps_backing", pid, ibuf_, Buffer::kBufSize); |
| 577 |
} else if (pid == 0) { |
| 578 |
// We have to kludge a bit to deal with the args ConstructFilename |
| 579 |
// expects. The 1 is never used -- it's only impt. that it's not 0. |
| 580 |
ConstructFilename("/proc/self/maps", 1, ibuf_, Buffer::kBufSize); |
| 581 |
} else { |
| 582 |
ConstructFilename("/proc/%d/maps", pid, ibuf_, Buffer::kBufSize); |
| 583 |
} |
| 584 |
// No error logging since this can be called from the crash dump |
| 585 |
// handler at awkward moments. Users should call Valid() before |
| 586 |
// using. |
| 587 |
NO_INTR(fd_ = open(ibuf_, O_RDONLY)); |
| 588 |
#elif defined(__FreeBSD__) |
| 589 |
// We don't support maps_backing on freebsd |
| 590 |
if (pid == 0) { |
| 591 |
ConstructFilename("/proc/curproc/map", 1, ibuf_, Buffer::kBufSize); |
| 592 |
} else { |
| 593 |
ConstructFilename("/proc/%d/map", pid, ibuf_, Buffer::kBufSize); |
| 594 |
} |
| 595 |
NO_INTR(fd_ = open(ibuf_, O_RDONLY)); |
| 596 |
#elif defined(__sun__) |
| 597 |
if (pid == 0) { |
| 598 |
ConstructFilename("/proc/self/map", 1, ibuf_, Buffer::kBufSize); |
| 599 |
} else { |
| 600 |
ConstructFilename("/proc/%d/map", pid, ibuf_, Buffer::kBufSize); |
| 601 |
} |
| 602 |
NO_INTR(fd_ = open(ibuf_, O_RDONLY)); |
| 603 |
#elif defined(__MACH__) |
| 604 |
current_image_ = _dyld_image_count(); // count down from the top |
| 605 |
current_load_cmd_ = -1; |
| 606 |
#elif defined(PLATFORM_WINDOWS) |
| 607 |
snapshot_ = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | |
| 608 |
TH32CS_SNAPMODULE32, |
| 609 |
GetCurrentProcessId()); |
| 610 |
memset(&module_, 0, sizeof(module_)); |
| 611 |
#elif defined(__QNXNTO__) |
| 612 |
if (pid == 0) { |
| 613 |
ConstructFilename("/proc/self/pmap", 1, ibuf_, Buffer::kBufSize); |
| 614 |
} else { |
| 615 |
ConstructFilename("/proc/%d/pmap", pid, ibuf_, Buffer::kBufSize); |
| 616 |
} |
| 617 |
NO_INTR(fd_ = open(ibuf_, O_RDONLY)); |
| 618 |
#else |
| 619 |
fd_ = -1; // so Valid() is always false |
| 620 |
#endif |
| 621 |
|
| 622 |
} |
| 623 |
|
| 624 |
ProcMapsIterator::~ProcMapsIterator() { |
| 625 |
#if defined(PLATFORM_WINDOWS) |
| 626 |
if (snapshot_ != INVALID_HANDLE_VALUE) CloseHandle(snapshot_); |
| 627 |
#elif defined(__MACH__) |
| 628 |
// no cleanup necessary! |
| 629 |
#else |
| 630 |
if (fd_ >= 0) close(fd_); |
| 631 |
#endif |
| 632 |
delete dynamic_buffer_; |
| 633 |
} |
| 634 |
|
| 635 |
bool ProcMapsIterator::Valid() const { |
| 636 |
#if defined(PLATFORM_WINDOWS) |
| 637 |
return snapshot_ != INVALID_HANDLE_VALUE; |
| 638 |
#elif defined(__MACH__) |
| 639 |
return 1; |
| 640 |
#else |
| 641 |
return fd_ != -1; |
| 642 |
#endif |
| 643 |
} |
| 644 |
|
| 645 |
bool ProcMapsIterator::Next(uint64 *start, uint64 *end, char **flags, |
| 646 |
uint64 *offset, int64 *inode, char **filename) { |
| 647 |
return NextExt(start, end, flags, offset, inode, filename, NULL, NULL, |
| 648 |
NULL, NULL, NULL); |
| 649 |
} |
| 650 |
|
| 651 |
// This has too many arguments. It should really be building |
| 652 |
// a map object and returning it. The problem is that this is called |
| 653 |
// when the memory allocator state is undefined, hence the arguments. |
| 654 |
bool ProcMapsIterator::NextExt(uint64 *start, uint64 *end, char **flags, |
| 655 |
uint64 *offset, int64 *inode, char **filename, |
| 656 |
uint64 *file_mapping, uint64 *file_pages, |
| 657 |
uint64 *anon_mapping, uint64 *anon_pages, |
| 658 |
dev_t *dev) { |
| 659 |
|
| 660 |
#if defined(__linux__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__CYGWIN32__) || defined(__QNXNTO__) |
| 661 |
do { |
| 662 |
// Advance to the start of the next line |
| 663 |
stext_ = nextline_; |
| 664 |
|
| 665 |
// See if we have a complete line in the buffer already |
| 666 |
nextline_ = static_cast<char *>(memchr (stext_, '\n', etext_ - stext_)); |
| 667 |
if (!nextline_) { |
| 668 |
// Shift/fill the buffer so we do have a line |
| 669 |
int count = etext_ - stext_; |
| 670 |
|
| 671 |
// Move the current text to the start of the buffer |
| 672 |
memmove(ibuf_, stext_, count); |
| 673 |
stext_ = ibuf_; |
| 674 |
etext_ = ibuf_ + count; |
| 675 |
|
| 676 |
int nread = 0; // fill up buffer with text |
| 677 |
while (etext_ < ebuf_) { |
| 678 |
NO_INTR(nread = read(fd_, etext_, ebuf_ - etext_)); |
| 679 |
if (nread > 0) |
| 680 |
etext_ += nread; |
| 681 |
else |
| 682 |
break; |
| 683 |
} |
| 684 |
|
| 685 |
// Zero out remaining characters in buffer at EOF to avoid returning |
| 686 |
// garbage from subsequent calls. |
| 687 |
if (etext_ != ebuf_ && nread == 0) { |
| 688 |
memset(etext_, 0, ebuf_ - etext_); |
| 689 |
} |
| 690 |
*etext_ = '\n'; // sentinel; safe because ibuf extends 1 char beyond ebuf |
| 691 |
nextline_ = static_cast<char *>(memchr (stext_, '\n', etext_ + 1 - stext_)); |
| 692 |
} |
| 693 |
*nextline_ = 0; // turn newline into nul |
| 694 |
nextline_ += ((nextline_ < etext_)? 1 : 0); // skip nul if not end of text |
| 695 |
// stext_ now points at a nul-terminated line |
| 696 |
#if !defined(__QNXNTO__) |
| 697 |
uint64 tmpstart, tmpend, tmpoffset; |
| 698 |
int64 tmpinode; |
| 699 |
#endif |
| 700 |
int major, minor; |
| 701 |
unsigned filename_offset = 0; |
| 702 |
#if defined(__linux__) || defined(__NetBSD__) |
| 703 |
// for now, assume all linuxes have the same format |
| 704 |
if (!ParseProcMapsLine( |
| 705 |
stext_, |
| 706 |
start ? start : &tmpstart, |
| 707 |
end ? end : &tmpend, |
| 708 |
flags_, |
| 709 |
offset ? offset : &tmpoffset, |
| 710 |
&major, &minor, |
| 711 |
inode ? inode : &tmpinode, &filename_offset)) continue; |
| 712 |
#elif defined(__CYGWIN__) || defined(__CYGWIN32__) |
| 713 |
// cygwin is like linux, except the third field is the "entry point" |
| 714 |
// rather than the offset (see format_process_maps at |
| 715 |
// http://cygwin.com/cgi-bin/cvsweb.cgi/src/winsup/cygwin/fhandler_process.cc?rev=1.89&content-type=text/x-cvsweb-markup&cvsroot=src |
| 716 |
// Offset is always be 0 on cygwin: cygwin implements an mmap |
| 717 |
// by loading the whole file and then calling NtMapViewOfSection. |
| 718 |
// Cygwin also seems to set its flags kinda randomly; use windows default. |
| 719 |
char tmpflags[5]; |
| 720 |
if (offset) |
| 721 |
*offset = 0; |
| 722 |
strcpy(flags_, "r-xp"); |
| 723 |
if (sscanf(stext_, "%llx-%llx %4s %llx %x:%x %lld %n", |
| 724 |
start ? start : &tmpstart, |
| 725 |
end ? end : &tmpend, |
| 726 |
tmpflags, |
| 727 |
&tmpoffset, |
| 728 |
&major, &minor, |
| 729 |
inode ? inode : &tmpinode, &filename_offset) != 7) continue; |
| 730 |
#elif defined(__FreeBSD__) |
| 731 |
// For the format, see http://www.freebsd.org/cgi/cvsweb.cgi/src/sys/fs/procfs/procfs_map.c?rev=1.31&content-type=text/x-cvsweb-markup |
| 732 |
tmpstart = tmpend = tmpoffset = 0; |
| 733 |
tmpinode = 0; |
| 734 |
major = minor = 0; // can't get this info in freebsd |
| 735 |
if (inode) |
| 736 |
*inode = 0; // nor this |
| 737 |
if (offset) |
| 738 |
*offset = 0; // seems like this should be in there, but maybe not |
| 739 |
// start end resident privateresident obj(?) prot refcnt shadowcnt |
| 740 |
// flags copy_on_write needs_copy type filename: |
| 741 |
// 0x8048000 0x804a000 2 0 0xc104ce70 r-x 1 0 0x0 COW NC vnode /bin/cat |
| 742 |
if (sscanf(stext_, "0x%" SCNx64 " 0x%" SCNx64 " %*d %*d %*p %3s %*d %*d 0x%*x %*s %*s %*s %n", |
| 743 |
start ? start : &tmpstart, |
| 744 |
end ? end : &tmpend, |
| 745 |
flags_, |
| 746 |
&filename_offset) != 3) continue; |
| 747 |
#elif defined(__QNXNTO__) |
| 748 |
// https://www.qnx.com/developers/docs/7.1/#com.qnx.doc.neutrino.sys_arch/topic/vm_calculations.html |
| 749 |
// vaddr,size,flags,prot,maxprot,dev,ino,offset,rsv,guardsize,refcnt,mapcnt,path |
| 750 |
// 0x00000025e9df9000,0x0000000000053000,0x00000071,0x05,0x0f,0x0000040b,0x0000000000000094, |
| 751 |
// 0x0000000000000000,0x0000000000000000,0x00000000,0x00000005,0x00000003,/system/xbin/cat |
| 752 |
{ |
| 753 |
uint64_t q_vaddr, q_size, q_ino, q_offset; |
| 754 |
uint32_t q_flags, q_dev, q_prot; |
| 755 |
int ret; |
| 756 |
if (sscanf(stext_, "0x%" SCNx64 ",0x%" SCNx64 ",0x%" SCNx32 \ |
| 757 |
",0x%" SCNx32 ",0x%*x,0x%" SCNx32 ",0x%" SCNx64 \ |
| 758 |
",0x%" SCNx64 ",0x%*x,0x%*x,0x%*x,0x%*x,%n", |
| 759 |
&q_vaddr, |
| 760 |
&q_size, |
| 761 |
&q_flags, |
| 762 |
&q_prot, |
| 763 |
&q_dev, |
| 764 |
&q_ino, |
| 765 |
&q_offset, |
| 766 |
&filename_offset) != 7) continue; |
| 767 |
|
| 768 |
// XXX: always is 00:00 in prof?? |
| 769 |
major = major(q_dev); |
| 770 |
minor = minor(q_dev); |
| 771 |
if (start) *start = q_vaddr; |
| 772 |
if (end) *end = q_vaddr + q_size; |
| 773 |
if (offset) *offset = q_offset; |
| 774 |
if (inode) *inode = q_ino; |
| 775 |
// right shifted by 8 bits, restore it |
| 776 |
q_prot <<= 8; |
| 777 |
flags_[0] = q_prot & PROT_READ ? 'r' : '-'; |
| 778 |
flags_[1] = q_prot & PROT_WRITE ? 'w' : '-'; |
| 779 |
flags_[2] = q_prot & PROT_EXEC ? 'x' : '-'; |
| 780 |
flags_[3] = q_flags & MAP_SHARED ? 's' : 'p'; |
| 781 |
flags_[4] = '\0'; |
| 782 |
} |
| 783 |
#endif |
| 784 |
|
| 785 |
// Depending on the Linux kernel being used, there may or may not be a space |
| 786 |
// after the inode if there is no filename. sscanf will in such situations |
| 787 |
// nondeterministically either fill in filename_offset or not (the results |
| 788 |
// differ on multiple calls in the same run even with identical arguments). |
| 789 |
// We don't want to wander off somewhere beyond the end of the string. |
| 790 |
size_t stext_length = strlen(stext_); |
| 791 |
if (filename_offset == 0 || filename_offset > stext_length) |
| 792 |
filename_offset = stext_length; |
| 793 |
|
| 794 |
// We found an entry |
| 795 |
if (flags) *flags = flags_; |
| 796 |
if (filename) *filename = stext_ + filename_offset; |
| 797 |
if (dev) *dev = minor | (major << 8); |
| 798 |
|
| 799 |
#if !defined(__QNXNTO__) |
| 800 |
if (using_maps_backing_) { |
| 801 |
// Extract and parse physical page backing info. |
| 802 |
char *backing_ptr = stext_ + filename_offset + |
| 803 |
strlen(stext_+filename_offset); |
| 804 |
|
| 805 |
// find the second '(' |
| 806 |
int paren_count = 0; |
| 807 |
while (--backing_ptr > stext_) { |
| 808 |
if (*backing_ptr == '(') { |
| 809 |
++paren_count; |
| 810 |
if (paren_count >= 2) { |
| 811 |
uint64 tmp_file_mapping; |
| 812 |
uint64 tmp_file_pages; |
| 813 |
uint64 tmp_anon_mapping; |
| 814 |
uint64 tmp_anon_pages; |
| 815 |
|
| 816 |
sscanf(backing_ptr+1, "F %" SCNx64 " %" SCNd64 ") (A %" SCNx64 " %" SCNd64 ")", |
| 817 |
file_mapping ? file_mapping : &tmp_file_mapping, |
| 818 |
file_pages ? file_pages : &tmp_file_pages, |
| 819 |
anon_mapping ? anon_mapping : &tmp_anon_mapping, |
| 820 |
anon_pages ? anon_pages : &tmp_anon_pages); |
| 821 |
// null terminate the file name (there is a space |
| 822 |
// before the first (. |
| 823 |
backing_ptr[-1] = 0; |
| 824 |
break; |
| 825 |
} |
| 826 |
} |
| 827 |
} |
| 828 |
} |
| 829 |
#endif |
| 830 |
|
| 831 |
return true; |
| 832 |
} while (etext_ > ibuf_); |
| 833 |
#elif defined(__sun__) |
| 834 |
// This is based on MA_READ == 4, MA_WRITE == 2, MA_EXEC == 1 |
| 835 |
static char kPerms[8][4] = { "---", "--x", "-w-", "-wx", |
| 836 |
"r--", "r-x", "rw-", "rwx" }; |
| 837 |
COMPILE_ASSERT(MA_READ == 4, solaris_ma_read_must_equal_4); |
| 838 |
COMPILE_ASSERT(MA_WRITE == 2, solaris_ma_write_must_equal_2); |
| 839 |
COMPILE_ASSERT(MA_EXEC == 1, solaris_ma_exec_must_equal_1); |
| 840 |
Buffer object_path; |
| 841 |
int nread = 0; // fill up buffer with text |
| 842 |
NO_INTR(nread = read(fd_, ibuf_, sizeof(prmap_t))); |
| 843 |
if (nread == sizeof(prmap_t)) { |
| 844 |
long inode_from_mapname = 0; |
| 845 |
prmap_t* mapinfo = reinterpret_cast<prmap_t*>(ibuf_); |
| 846 |
// Best-effort attempt to get the inode from the filename. I think the |
| 847 |
// two middle ints are major and minor device numbers, but I'm not sure. |
| 848 |
sscanf(mapinfo->pr_mapname, "ufs.%*d.%*d.%ld", &inode_from_mapname); |
| 849 |
|
| 850 |
if (pid_ == 0) { |
| 851 |
CHECK_LT(snprintf(object_path.buf_, Buffer::kBufSize, |
| 852 |
"/proc/self/path/%s", mapinfo->pr_mapname), |
| 853 |
Buffer::kBufSize); |
| 854 |
} else { |
| 855 |
CHECK_LT(snprintf(object_path.buf_, Buffer::kBufSize, |
| 856 |
"/proc/%d/path/%s", |
| 857 |
static_cast<int>(pid_), mapinfo->pr_mapname), |
| 858 |
Buffer::kBufSize); |
| 859 |
} |
| 860 |
ssize_t len = readlink(object_path.buf_, current_filename_, PATH_MAX); |
| 861 |
CHECK_LT(len, PATH_MAX); |
| 862 |
if (len < 0) |
| 863 |
len = 0; |
| 864 |
current_filename_[len] = '\0'; |
| 865 |
|
| 866 |
if (start) *start = mapinfo->pr_vaddr; |
| 867 |
if (end) *end = mapinfo->pr_vaddr + mapinfo->pr_size; |
| 868 |
if (flags) *flags = kPerms[mapinfo->pr_mflags & 7]; |
| 869 |
if (offset) *offset = mapinfo->pr_offset; |
| 870 |
if (inode) *inode = inode_from_mapname; |
| 871 |
if (filename) *filename = current_filename_; |
| 872 |
if (file_mapping) *file_mapping = 0; |
| 873 |
if (file_pages) *file_pages = 0; |
| 874 |
if (anon_mapping) *anon_mapping = 0; |
| 875 |
if (anon_pages) *anon_pages = 0; |
| 876 |
if (dev) *dev = 0; |
| 877 |
return true; |
| 878 |
} |
| 879 |
#elif defined(__MACH__) |
| 880 |
// We return a separate entry for each segment in the DLL. (TODO(csilvers): |
| 881 |
// can we do better?) A DLL ("image") has load-commands, some of which |
| 882 |
// talk about segment boundaries. |
| 883 |
// cf image_for_address from http://svn.digium.com/view/asterisk/team/oej/minivoicemail/dlfcn.c?revision=53912 |
| 884 |
for (; current_image_ >= 0; current_image_--) { |
| 885 |
const mach_header* hdr = _dyld_get_image_header(current_image_); |
| 886 |
if (!hdr) continue; |
| 887 |
if (current_load_cmd_ < 0) // set up for this image |
| 888 |
current_load_cmd_ = hdr->ncmds; // again, go from the top down |
| 889 |
|
| 890 |
// We start with the next load command (we've already looked at this one). |
| 891 |
for (current_load_cmd_--; current_load_cmd_ >= 0; current_load_cmd_--) { |
| 892 |
#ifdef MH_MAGIC_64 |
| 893 |
if (NextExtMachHelper<MH_MAGIC_64, LC_SEGMENT_64, |
| 894 |
struct mach_header_64, struct segment_command_64>( |
| 895 |
hdr, current_image_, current_load_cmd_, |
| 896 |
start, end, flags, offset, inode, filename, |
| 897 |
file_mapping, file_pages, anon_mapping, |
| 898 |
anon_pages, dev)) { |
| 899 |
return true; |
| 900 |
} |
| 901 |
#endif |
| 902 |
if (NextExtMachHelper<MH_MAGIC, LC_SEGMENT, |
| 903 |
struct mach_header, struct segment_command>( |
| 904 |
hdr, current_image_, current_load_cmd_, |
| 905 |
start, end, flags, offset, inode, filename, |
| 906 |
file_mapping, file_pages, anon_mapping, |
| 907 |
anon_pages, dev)) { |
| 908 |
return true; |
| 909 |
} |
| 910 |
} |
| 911 |
// If we get here, no more load_cmd's in this image talk about |
| 912 |
// segments. Go on to the next image. |
| 913 |
} |
| 914 |
#elif defined(PLATFORM_WINDOWS) |
| 915 |
static char kDefaultPerms[5] = "r-xp"; |
| 916 |
BOOL ok; |
| 917 |
if (module_.dwSize == 0) { // only possible before first call |
| 918 |
module_.dwSize = sizeof(module_); |
| 919 |
ok = Module32First(snapshot_, &module_); |
| 920 |
} else { |
| 921 |
ok = Module32Next(snapshot_, &module_); |
| 922 |
} |
| 923 |
if (ok) { |
| 924 |
uint64 base_addr = reinterpret_cast<DWORD_PTR>(module_.modBaseAddr); |
| 925 |
if (start) *start = base_addr; |
| 926 |
if (end) *end = base_addr + module_.modBaseSize; |
| 927 |
if (flags) *flags = kDefaultPerms; |
| 928 |
if (offset) *offset = 0; |
| 929 |
if (inode) *inode = 0; |
| 930 |
if (filename) *filename = module_.szExePath; |
| 931 |
if (file_mapping) *file_mapping = 0; |
| 932 |
if (file_pages) *file_pages = 0; |
| 933 |
if (anon_mapping) *anon_mapping = 0; |
| 934 |
if (anon_pages) *anon_pages = 0; |
| 935 |
if (dev) *dev = 0; |
| 936 |
return true; |
| 937 |
} |
| 938 |
#endif |
| 939 |
|
| 940 |
// We didn't find anything |
| 941 |
return false; |
| 942 |
} |
| 943 |
|
| 944 |
int ProcMapsIterator::FormatLine(char* buffer, int bufsize, |
| 945 |
uint64 start, uint64 end, const char *flags, |
| 946 |
uint64 offset, int64 inode, |
| 947 |
const char *filename, dev_t dev) { |
| 948 |
// We assume 'flags' looks like 'rwxp' or 'rwx'. |
| 949 |
char r = (flags && flags[0] == 'r') ? 'r' : '-'; |
| 950 |
char w = (flags && flags[0] && flags[1] == 'w') ? 'w' : '-'; |
| 951 |
char x = (flags && flags[0] && flags[1] && flags[2] == 'x') ? 'x' : '-'; |
| 952 |
// p always seems set on linux, so we set the default to 'p', not '-' |
| 953 |
char p = (flags && flags[0] && flags[1] && flags[2] && flags[3] != 'p') |
| 954 |
? '-' : 'p'; |
| 955 |
|
| 956 |
const int rc = snprintf(buffer, bufsize, |
| 957 |
"%08" PRIx64 "-%08" PRIx64 " %c%c%c%c %08" PRIx64 " %02x:%02x %-11" PRId64 " %s\n", |
| 958 |
start, end, r,w,x,p, offset, |
| 959 |
static_cast<int>(dev/256), static_cast<int>(dev%256), |
| 960 |
inode, filename); |
| 961 |
return (rc < 0 || rc >= bufsize) ? 0 : rc; |
| 962 |
} |
| 963 |
|
| 964 |
namespace tcmalloc { |
| 965 |
|
| 966 |
// Helper to add the list of mapped shared libraries to a profile. |
| 967 |
// Fill formatted "/proc/self/maps" contents into buffer 'buf' of size 'size' |
| 968 |
// and return the actual size occupied in 'buf'. We fill wrote_all to true |
| 969 |
// if we successfully wrote all proc lines to buf, false else. |
| 970 |
// We do not provision for 0-terminating 'buf'. |
| 971 |
int FillProcSelfMaps(char buf[], int size, bool* wrote_all) { |
| 972 |
ProcMapsIterator::Buffer iterbuf; |
| 973 |
ProcMapsIterator it(0, &iterbuf); // 0 means "current pid" |
| 974 |
|
| 975 |
uint64 start, end, offset; |
| 976 |
int64 inode; |
| 977 |
char *flags, *filename; |
| 978 |
int bytes_written = 0; |
| 979 |
*wrote_all = true; |
| 980 |
while (it.Next(&start, &end, &flags, &offset, &inode, &filename)) { |
| 981 |
const int line_length = it.FormatLine(buf + bytes_written, |
| 982 |
size - bytes_written, |
| 983 |
start, end, flags, offset, |
| 984 |
inode, filename, 0); |
| 985 |
if (line_length == 0) |
| 986 |
*wrote_all = false; // failed to write this line out |
| 987 |
else |
| 988 |
bytes_written += line_length; |
| 989 |
|
| 990 |
} |
| 991 |
return bytes_written; |
| 992 |
} |
| 993 |
|
| 994 |
// Dump the same data as FillProcSelfMaps reads to fd. |
| 995 |
// It seems easier to repeat parts of FillProcSelfMaps here than to |
| 996 |
// reuse it via a call. |
| 997 |
void DumpProcSelfMaps(RawFD fd) { |
| 998 |
ProcMapsIterator::Buffer iterbuf; |
| 999 |
ProcMapsIterator it(0, &iterbuf); // 0 means "current pid" |
| 1000 |
|
| 1001 |
uint64 start, end, offset; |
| 1002 |
int64 inode; |
| 1003 |
char *flags, *filename; |
| 1004 |
ProcMapsIterator::Buffer linebuf; |
| 1005 |
while (it.Next(&start, &end, &flags, &offset, &inode, &filename)) { |
| 1006 |
int written = it.FormatLine(linebuf.buf_, sizeof(linebuf.buf_), |
| 1007 |
start, end, flags, offset, inode, filename, |
| 1008 |
0); |
| 1009 |
RawWrite(fd, linebuf.buf_, written); |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
} // namespace tcmalloc |