• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2006, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #include <config.h>
31 #if (defined(_WIN32) || defined(__MINGW32__)) && !defined(__CYGWIN__) && !defined(__CYGWIN32)
32 # define PLATFORM_WINDOWS 1
33 #elif defined(__ANDROID__) || defined(ANDROID)
34 # define PLATFORM_ANDROID 1
35 #endif
36 
37 #include <ctype.h>    // for isspace()
38 #include <stdlib.h>   // for getenv()
39 #include <stdio.h>    // for snprintf(), sscanf()
40 #include <string.h>   // for memmove(), memchr(), etc.
41 #include <fcntl.h>    // for open()
42 #include <errno.h>    // for errno
43 #ifdef HAVE_UNISTD_H
44 #include <unistd.h>   // for read()
45 #endif
46 #if defined __MACH__          // Mac OS X, almost certainly
47 #include <mach-o/dyld.h>      // for iterating over dll's in ProcMapsIter
48 #include <mach-o/loader.h>    // for iterating over dll's in ProcMapsIter
49 #include <sys/types.h>
50 #include <sys/sysctl.h>       // how we figure out numcpu's on OS X
51 #elif defined __FreeBSD__
52 #include <sys/sysctl.h>
53 #elif defined __sun__         // Solaris
54 #include <procfs.h>           // for, e.g., prmap_t
55 #elif defined(PLATFORM_WINDOWS)
56 #include <process.h>          // for getpid() (actually, _getpid())
57 #include <shlwapi.h>          // for SHGetValueA()
58 #include <tlhelp32.h>         // for Module32First()
59 #elif defined(PLATFORM_ANDROID)
60 #include <sys/system_properties.h>
61 #endif
62 #include "base/sysinfo.h"
63 #include "base/commandlineflags.h"
64 #include "base/dynamic_annotations.h"   // for RunningOnValgrind
65 #include "base/logging.h"
66 #include "base/cycleclock.h"
67 
68 #ifdef PLATFORM_WINDOWS
69 #ifdef MODULEENTRY32
70 // In a change from the usual W-A pattern, there is no A variant of
71 // MODULEENTRY32.  Tlhelp32.h #defines the W variant, but not the A.
72 // In unicode mode, tlhelp32.h #defines MODULEENTRY32 to be
73 // MODULEENTRY32W.  These #undefs are the only way I see to get back
74 // access to the original, ascii struct (and related functions).
75 #undef MODULEENTRY32
76 #undef Module32First
77 #undef Module32Next
78 #undef PMODULEENTRY32
79 #undef LPMODULEENTRY32
80 #endif  /* MODULEENTRY32 */
81 // MinGW doesn't seem to define this, perhaps some windowsen don't either.
82 #ifndef TH32CS_SNAPMODULE32
83 #define TH32CS_SNAPMODULE32  0
84 #endif  /* TH32CS_SNAPMODULE32 */
85 #endif  /* PLATFORM_WINDOWS */
86 
87 // Re-run fn until it doesn't cause EINTR.
88 #define NO_INTR(fn)  do {} while ((fn) < 0 && errno == EINTR)
89 
90 // open/read/close can set errno, which may be illegal at this
91 // time, so prefer making the syscalls directly if we can.
92 #ifdef HAVE_SYS_SYSCALL_H
93 # include <sys/syscall.h>
94 #endif
95 #ifdef SYS_open   // solaris 11, at least sometimes, only defines SYS_openat
96 # define safeopen(filename, mode)  syscall(SYS_open, filename, mode)
97 #else
98 # define safeopen(filename, mode)  open(filename, mode)
99 #endif
100 #ifdef SYS_read
101 # define saferead(fd, buffer, size)  syscall(SYS_read, fd, buffer, size)
102 #else
103 # define saferead(fd, buffer, size)  read(fd, buffer, size)
104 #endif
105 #ifdef SYS_close
106 # define safeclose(fd)  syscall(SYS_close, fd)
107 #else
108 # define safeclose(fd)  close(fd)
109 #endif
110 
111 // ----------------------------------------------------------------------
112 // GetenvBeforeMain()
113 // GetUniquePathFromEnv()
114 //    Some non-trivial getenv-related functions.
115 // ----------------------------------------------------------------------
116 
117 // It's not safe to call getenv() in the malloc hooks, because they
118 // might be called extremely early, before libc is done setting up
119 // correctly.  In particular, the thread library may not be done
120 // setting up errno.  So instead, we use the built-in __environ array
121 // if it exists, and otherwise read /proc/self/environ directly, using
122 // system calls to read the file, and thus avoid setting errno.
123 // /proc/self/environ has a limit of how much data it exports (around
124 // 8K), so it's not an ideal solution.
GetenvBeforeMain(const char * name)125 const char* GetenvBeforeMain(const char* name) {
126 #if defined(HAVE___ENVIRON)   // if we have it, it's declared in unistd.h
127   if (__environ) {            // can exist but be NULL, if statically linked
128     const int namelen = strlen(name);
129     for (char** p = __environ; *p; p++) {
130       if (!memcmp(*p, name, namelen) && (*p)[namelen] == '=')  // it's a match
131         return *p + namelen+1;                                 // point after =
132     }
133     return NULL;
134   }
135 #endif
136 #if defined(PLATFORM_WINDOWS)
137   // TODO(mbelshe) - repeated calls to this function will overwrite the
138   // contents of the static buffer.
139   static char envvar_buf[1024];  // enough to hold any envvar we care about
140   if (!GetEnvironmentVariableA(name, envvar_buf, sizeof(envvar_buf)-1))
141     return NULL;
142   return envvar_buf;
143 #endif
144   // static is ok because this function should only be called before
145   // main(), when we're single-threaded.
146   static char envbuf[16<<10];
147   if (*envbuf == '\0') {    // haven't read the environ yet
148     int fd = safeopen("/proc/self/environ", O_RDONLY);
149     // The -2 below guarantees the last two bytes of the buffer will be \0\0
150     if (fd == -1 ||           // unable to open the file, fall back onto libc
151         saferead(fd, envbuf, sizeof(envbuf) - 2) < 0) { // error reading file
152       RAW_VLOG(1, "Unable to open /proc/self/environ, falling back "
153                "on getenv(\"%s\"), which may not work", name);
154       if (fd != -1) safeclose(fd);
155       return getenv(name);
156     }
157     safeclose(fd);
158   }
159   const int namelen = strlen(name);
160   const char* p = envbuf;
161   while (*p != '\0') {    // will happen at the \0\0 that terminates the buffer
162     // proc file has the format NAME=value\0NAME=value\0NAME=value\0...
163     const char* endp = (char*)memchr(p, '\0', sizeof(envbuf) - (p - envbuf));
164     if (endp == NULL)            // this entry isn't NUL terminated
165       return NULL;
166     else if (!memcmp(p, name, namelen) && p[namelen] == '=')    // it's a match
167       return p + namelen+1;      // point after =
168     p = endp + 1;
169   }
170   return NULL;                   // env var never found
171 }
172 
173 // This takes as an argument an environment-variable name (like
174 // CPUPROFILE) whose value is supposed to be a file-path, and sets
175 // path to that path, and returns true.  If the env var doesn't exist,
176 // or is the empty string, leave path unchanged and returns false.
177 // The reason this is non-trivial is that this function handles munged
178 // pathnames.  Here's why:
179 //
180 // If we're a child process of the 'main' process, we can't just use
181 // getenv("CPUPROFILE") -- the parent process will be using that path.
182 // Instead we append our pid to the pathname.  How do we tell if we're a
183 // child process?  Ideally we'd set an environment variable that all
184 // our children would inherit.  But -- and this is seemingly a bug in
185 // gcc -- if you do a setenv() in a shared libarary in a global
186 // constructor, the environment setting is lost by the time main() is
187 // called.  The only safe thing we can do in such a situation is to
188 // modify the existing envvar.  So we do a hack: in the parent, we set
189 // the high bit of the 1st char of CPUPROFILE.  In the child, we
190 // notice the high bit is set and append the pid().  This works
191 // assuming cpuprofile filenames don't normally have the high bit set
192 // in their first character!  If that assumption is violated, we'll
193 // still get a profile, but one with an unexpected name.
194 // TODO(csilvers): set an envvar instead when we can do it reliably.
195 //
196 // In Chromium this hack is intentionally disabled, because the path is not
197 // re-initialized upon fork.
GetUniquePathFromEnv(const char * env_name,char * path)198 bool GetUniquePathFromEnv(const char* env_name, char* path) {
199 #if defined(PLATFORM_ANDROID)
200   char envval[PROP_VALUE_MAX];
201   __system_property_get(env_name, envval);
202 #else
203   char* envval = getenv(env_name);
204 #endif
205   if (envval == NULL || *envval == '\0')
206     return false;
207   if (envval[0] & 128) {                  // high bit is set
208     snprintf(path, PATH_MAX, "%c%s_%u",   // add pid and clear high bit
209              envval[0] & 127, envval+1, (unsigned int)(getpid()));
210   } else {
211     snprintf(path, PATH_MAX, "%s", envval);
212 #if 0
213     envval[0] |= 128;                     // set high bit for kids to see
214 #endif
215   }
216   return true;
217 }
218 
219 // ----------------------------------------------------------------------
220 // CyclesPerSecond()
221 // NumCPUs()
222 //    It's important this not call malloc! -- they may be called at
223 //    global-construct time, before we've set up all our proper malloc
224 //    hooks and such.
225 // ----------------------------------------------------------------------
226 
227 static double cpuinfo_cycles_per_second = 1.0;  // 0.0 might be dangerous
228 static int cpuinfo_num_cpus = 1;  // Conservative guess
229 
SleepForMilliseconds(int milliseconds)230 void SleepForMilliseconds(int milliseconds) {
231 #ifdef PLATFORM_WINDOWS
232   _sleep(milliseconds);   // Windows's _sleep takes milliseconds argument
233 #else
234   // Sleep for a few milliseconds
235   struct timespec sleep_time;
236   sleep_time.tv_sec = milliseconds / 1000;
237   sleep_time.tv_nsec = (milliseconds % 1000) * 1000000;
238   while (nanosleep(&sleep_time, &sleep_time) != 0 && errno == EINTR)
239     ;  // Ignore signals and wait for the full interval to elapse.
240 #endif
241 }
242 
243 // Helper function estimates cycles/sec by observing cycles elapsed during
244 // sleep(). Using small sleep time decreases accuracy significantly.
EstimateCyclesPerSecond(const int estimate_time_ms)245 static int64 EstimateCyclesPerSecond(const int estimate_time_ms) {
246   assert(estimate_time_ms > 0);
247   if (estimate_time_ms <= 0)
248     return 1;
249   double multiplier = 1000.0 / (double)estimate_time_ms;  // scale by this much
250 
251   const int64 start_ticks = CycleClock::Now();
252   SleepForMilliseconds(estimate_time_ms);
253   const int64 guess = int64(multiplier * (CycleClock::Now() - start_ticks));
254   return guess;
255 }
256 
257 // ReadIntFromFile is only called on linux and cygwin platforms.
258 #if defined(__linux__) || defined(__CYGWIN__) || defined(__CYGWIN32__)
259 // Helper function for reading an int from a file. Returns true if successful
260 // and the memory location pointed to by value is set to the value read.
ReadIntFromFile(const char * file,int * value)261 static bool ReadIntFromFile(const char *file, int *value) {
262   bool ret = false;
263   int fd = open(file, O_RDONLY);
264   if (fd != -1) {
265     char line[1024];
266     char* err;
267     memset(line, '\0', sizeof(line));
268     read(fd, line, sizeof(line) - 1);
269     const int temp_value = strtol(line, &err, 10);
270     if (line[0] != '\0' && (*err == '\n' || *err == '\0')) {
271       *value = temp_value;
272       ret = true;
273     }
274     close(fd);
275   }
276   return ret;
277 }
278 #endif
279 
280 // WARNING: logging calls back to InitializeSystemInfo() so it must
281 // not invoke any logging code.  Also, InitializeSystemInfo() can be
282 // called before main() -- in fact it *must* be since already_called
283 // isn't protected -- before malloc hooks are properly set up, so
284 // we make an effort not to call any routines which might allocate
285 // memory.
286 
InitializeSystemInfo()287 static void InitializeSystemInfo() {
288   static bool already_called = false;   // safe if we run before threads
289   if (already_called)  return;
290   already_called = true;
291 
292   bool saw_mhz = false;
293 
294   if (RunningOnValgrind()) {
295     // Valgrind may slow the progress of time artificially (--scale-time=N
296     // option). We thus can't rely on CPU Mhz info stored in /sys or /proc
297     // files. Thus, actually measure the cps.
298     cpuinfo_cycles_per_second = EstimateCyclesPerSecond(100);
299     saw_mhz = true;
300   }
301 
302 #if defined(__linux__) || defined(__CYGWIN__) || defined(__CYGWIN32__)
303   char line[1024];
304   char* err;
305   int freq;
306 
307   // If the kernel is exporting the tsc frequency use that. There are issues
308   // where cpuinfo_max_freq cannot be relied on because the BIOS may be
309   // exporintg an invalid p-state (on x86) or p-states may be used to put the
310   // processor in a new mode (turbo mode). Essentially, those frequencies
311   // cannot always be relied upon. The same reasons apply to /proc/cpuinfo as
312   // well.
313   if (!saw_mhz &&
314       ReadIntFromFile("/sys/devices/system/cpu/cpu0/tsc_freq_khz", &freq)) {
315       // The value is in kHz (as the file name suggests).  For example, on a
316       // 2GHz warpstation, the file contains the value "2000000".
317       cpuinfo_cycles_per_second = freq * 1000.0;
318       saw_mhz = true;
319   }
320 
321   // If CPU scaling is in effect, we want to use the *maximum* frequency,
322   // not whatever CPU speed some random processor happens to be using now.
323   if (!saw_mhz &&
324       ReadIntFromFile("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq",
325                       &freq)) {
326     // The value is in kHz.  For example, on a 2GHz machine, the file
327     // contains the value "2000000".
328     cpuinfo_cycles_per_second = freq * 1000.0;
329     saw_mhz = true;
330   }
331 
332   // Read /proc/cpuinfo for other values, and if there is no cpuinfo_max_freq.
333   const char* pname = "/proc/cpuinfo";
334   int fd = open(pname, O_RDONLY);
335   if (fd == -1) {
336     perror(pname);
337     if (!saw_mhz) {
338       cpuinfo_cycles_per_second = EstimateCyclesPerSecond(1000);
339     }
340     return;          // TODO: use generic tester instead?
341   }
342 
343   double bogo_clock = 1.0;
344   bool saw_bogo = false;
345   int num_cpus = 0;
346   line[0] = line[1] = '\0';
347   int chars_read = 0;
348   do {   // we'll exit when the last read didn't read anything
349     // Move the next line to the beginning of the buffer
350     const int oldlinelen = strlen(line);
351     if (sizeof(line) == oldlinelen + 1)    // oldlinelen took up entire line
352       line[0] = '\0';
353     else                                   // still other lines left to save
354       memmove(line, line + oldlinelen+1, sizeof(line) - (oldlinelen+1));
355     // Terminate the new line, reading more if we can't find the newline
356     char* newline = strchr(line, '\n');
357     if (newline == NULL) {
358       const int linelen = strlen(line);
359       const int bytes_to_read = sizeof(line)-1 - linelen;
360       assert(bytes_to_read > 0);  // because the memmove recovered >=1 bytes
361       chars_read = read(fd, line + linelen, bytes_to_read);
362       line[linelen + chars_read] = '\0';
363       newline = strchr(line, '\n');
364     }
365     if (newline != NULL)
366       *newline = '\0';
367 
368     // When parsing the "cpu MHz" and "bogomips" (fallback) entries, we only
369     // accept postive values. Some environments (virtual machines) report zero,
370     // which would cause infinite looping in WallTime_Init.
371     if (!saw_mhz && strncasecmp(line, "cpu MHz", sizeof("cpu MHz")-1) == 0) {
372       const char* freqstr = strchr(line, ':');
373       if (freqstr) {
374         cpuinfo_cycles_per_second = strtod(freqstr+1, &err) * 1000000.0;
375         if (freqstr[1] != '\0' && *err == '\0' && cpuinfo_cycles_per_second > 0)
376           saw_mhz = true;
377       }
378     } else if (strncasecmp(line, "bogomips", sizeof("bogomips")-1) == 0) {
379       const char* freqstr = strchr(line, ':');
380       if (freqstr) {
381         bogo_clock = strtod(freqstr+1, &err) * 1000000.0;
382         if (freqstr[1] != '\0' && *err == '\0' && bogo_clock > 0)
383           saw_bogo = true;
384       }
385     } else if (strncasecmp(line, "processor", sizeof("processor")-1) == 0) {
386       num_cpus++;  // count up every time we see an "processor :" entry
387     }
388   } while (chars_read > 0);
389   close(fd);
390 
391   if (!saw_mhz) {
392     if (saw_bogo) {
393       // If we didn't find anything better, we'll use bogomips, but
394       // we're not happy about it.
395       cpuinfo_cycles_per_second = bogo_clock;
396     } else {
397       // If we don't even have bogomips, we'll use the slow estimation.
398       cpuinfo_cycles_per_second = EstimateCyclesPerSecond(1000);
399     }
400   }
401   if (cpuinfo_cycles_per_second == 0.0) {
402     cpuinfo_cycles_per_second = 1.0;   // maybe unnecessary, but safe
403   }
404   if (num_cpus > 0) {
405     cpuinfo_num_cpus = num_cpus;
406   }
407 
408 #elif defined __FreeBSD__
409   // For this sysctl to work, the machine must be configured without
410   // SMP, APIC, or APM support.  hz should be 64-bit in freebsd 7.0
411   // and later.  Before that, it's a 32-bit quantity (and gives the
412   // wrong answer on machines faster than 2^32 Hz).  See
413   //  http://lists.freebsd.org/pipermail/freebsd-i386/2004-November/001846.html
414   // But also compare FreeBSD 7.0:
415   //  http://fxr.watson.org/fxr/source/i386/i386/tsc.c?v=RELENG70#L223
416   //  231         error = sysctl_handle_quad(oidp, &freq, 0, req);
417   // To FreeBSD 6.3 (it's the same in 6-STABLE):
418   //  http://fxr.watson.org/fxr/source/i386/i386/tsc.c?v=RELENG6#L131
419   //  139         error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
420 #if __FreeBSD__ >= 7
421   uint64_t hz = 0;
422 #else
423   unsigned int hz = 0;
424 #endif
425   size_t sz = sizeof(hz);
426   const char *sysctl_path = "machdep.tsc_freq";
427   if ( sysctlbyname(sysctl_path, &hz, &sz, NULL, 0) != 0 ) {
428     fprintf(stderr, "Unable to determine clock rate from sysctl: %s: %s\n",
429             sysctl_path, strerror(errno));
430     cpuinfo_cycles_per_second = EstimateCyclesPerSecond(1000);
431   } else {
432     cpuinfo_cycles_per_second = hz;
433   }
434   // TODO(csilvers): also figure out cpuinfo_num_cpus
435 
436 #elif defined(PLATFORM_WINDOWS)
437 # pragma comment(lib, "shlwapi.lib")  // for SHGetValue()
438   // In NT, read MHz from the registry. If we fail to do so or we're in win9x
439   // then make a crude estimate.
440   OSVERSIONINFO os;
441   os.dwOSVersionInfoSize = sizeof(os);
442   DWORD data, data_size = sizeof(data);
443   if (GetVersionEx(&os) &&
444       os.dwPlatformId == VER_PLATFORM_WIN32_NT &&
445       SUCCEEDED(SHGetValueA(HKEY_LOCAL_MACHINE,
446                          "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
447                            "~MHz", NULL, &data, &data_size)))
448     cpuinfo_cycles_per_second = (int64)data * (int64)(1000 * 1000); // was mhz
449   else
450     cpuinfo_cycles_per_second = EstimateCyclesPerSecond(500); // TODO <500?
451 
452   // Get the number of processors.
453   SYSTEM_INFO info;
454   GetSystemInfo(&info);
455   cpuinfo_num_cpus = info.dwNumberOfProcessors;
456 
457 #elif defined(__MACH__) && defined(__APPLE__)
458   // returning "mach time units" per second. the current number of elapsed
459   // mach time units can be found by calling uint64 mach_absolute_time();
460   // while not as precise as actual CPU cycles, it is accurate in the face
461   // of CPU frequency scaling and multi-cpu/core machines.
462   // Our mac users have these types of machines, and accuracy
463   // (i.e. correctness) trumps precision.
464   // See cycleclock.h: CycleClock::Now(), which returns number of mach time
465   // units on Mac OS X.
466   mach_timebase_info_data_t timebase_info;
467   mach_timebase_info(&timebase_info);
468   double mach_time_units_per_nanosecond =
469       static_cast<double>(timebase_info.denom) /
470       static_cast<double>(timebase_info.numer);
471   cpuinfo_cycles_per_second = mach_time_units_per_nanosecond * 1e9;
472 
473   int num_cpus = 0;
474   size_t size = sizeof(num_cpus);
475   int numcpus_name[] = { CTL_HW, HW_NCPU };
476   if (::sysctl(numcpus_name, arraysize(numcpus_name), &num_cpus, &size, 0, 0)
477       == 0
478       && (size == sizeof(num_cpus)))
479     cpuinfo_num_cpus = num_cpus;
480 
481 #else
482   // Generic cycles per second counter
483   cpuinfo_cycles_per_second = EstimateCyclesPerSecond(1000);
484 #endif
485 }
486 
CyclesPerSecond(void)487 double CyclesPerSecond(void) {
488   InitializeSystemInfo();
489   return cpuinfo_cycles_per_second;
490 }
491 
NumCPUs(void)492 int NumCPUs(void) {
493   InitializeSystemInfo();
494   return cpuinfo_num_cpus;
495 }
496 
497 // ----------------------------------------------------------------------
498 // HasPosixThreads()
499 //      Return true if we're running POSIX (e.g., NPTL on Linux)
500 //      threads, as opposed to a non-POSIX thread libary.  The thing
501 //      that we care about is whether a thread's pid is the same as
502 //      the thread that spawned it.  If so, this function returns
503 //      true.
504 // ----------------------------------------------------------------------
HasPosixThreads()505 bool HasPosixThreads() {
506 // Android doesn't have confstr(), assume posix thread and fallback to
507 // "other os".
508 #if defined(__linux__) && !defined(__ANDROID__)
509 #ifndef _CS_GNU_LIBPTHREAD_VERSION
510 #define _CS_GNU_LIBPTHREAD_VERSION 3
511 #endif
512   char buf[32];
513   //  We assume that, if confstr() doesn't know about this name, then
514   //  the same glibc is providing LinuxThreads.
515   if (confstr(_CS_GNU_LIBPTHREAD_VERSION, buf, sizeof(buf)) == 0)
516     return false;
517   return strncmp(buf, "NPTL", 4) == 0;
518 #elif defined(PLATFORM_WINDOWS) || defined(__CYGWIN__) || defined(__CYGWIN32__)
519   return false;
520 #else  // other OS
521   return true;      //  Assume that everything else has Posix
522 #endif  // else OS_LINUX
523 }
524 
525 // ----------------------------------------------------------------------
526 
527 #if defined __linux__ || defined __FreeBSD__ || defined __sun__ || defined __CYGWIN__ || defined __CYGWIN32__
ConstructFilename(const char * spec,pid_t pid,char * buf,int buf_size)528 static void ConstructFilename(const char* spec, pid_t pid,
529                               char* buf, int buf_size) {
530   CHECK_LT(snprintf(buf, buf_size,
531                     spec,
532                     static_cast<int>(pid ? pid : getpid())), buf_size);
533 }
534 #endif
535 
536 // A templatized helper function instantiated for Mach (OS X) only.
537 // It can handle finding info for both 32 bits and 64 bits.
538 // Returns true if it successfully handled the hdr, false else.
539 #ifdef __MACH__          // Mac OS X, almost certainly
540 template<uint32_t kMagic, uint32_t kLCSegment,
541          typename MachHeader, typename SegmentCommand>
NextExtMachHelper(const mach_header * hdr,int current_image,int current_load_cmd,uint64 * start,uint64 * end,char ** flags,uint64 * offset,int64 * inode,char ** filename,uint64 * file_mapping,uint64 * file_pages,uint64 * anon_mapping,uint64 * anon_pages,dev_t * dev)542 static bool NextExtMachHelper(const mach_header* hdr,
543                               int current_image, int current_load_cmd,
544                               uint64 *start, uint64 *end, char **flags,
545                               uint64 *offset, int64 *inode, char **filename,
546                               uint64 *file_mapping, uint64 *file_pages,
547                               uint64 *anon_mapping, uint64 *anon_pages,
548                               dev_t *dev) {
549   static char kDefaultPerms[5] = "r-xp";
550   if (hdr->magic != kMagic)
551     return false;
552   const char* lc = (const char *)hdr + sizeof(MachHeader);
553   // TODO(csilvers): make this not-quadradic (increment and hold state)
554   for (int j = 0; j < current_load_cmd; j++)  // advance to *our* load_cmd
555     lc += ((const load_command *)lc)->cmdsize;
556   if (((const load_command *)lc)->cmd == kLCSegment) {
557     const intptr_t dlloff = _dyld_get_image_vmaddr_slide(current_image);
558     const SegmentCommand* sc = (const SegmentCommand *)lc;
559     if (start) *start = sc->vmaddr + dlloff;
560     if (end) *end = sc->vmaddr + sc->vmsize + dlloff;
561     if (flags) *flags = kDefaultPerms;  // can we do better?
562     if (offset) *offset = sc->fileoff;
563     if (inode) *inode = 0;
564     if (filename)
565       *filename = const_cast<char*>(_dyld_get_image_name(current_image));
566     if (file_mapping) *file_mapping = 0;
567     if (file_pages) *file_pages = 0;   // could we use sc->filesize?
568     if (anon_mapping) *anon_mapping = 0;
569     if (anon_pages) *anon_pages = 0;
570     if (dev) *dev = 0;
571     return true;
572   }
573 
574   return false;
575 }
576 #endif
577 
578 // Finds |c| in |text|, and assign '\0' at the found position.
579 // The original character at the modified position should be |c|.
580 // A pointer to the modified position is stored in |endptr|.
581 // |endptr| should not be NULL.
ExtractUntilChar(char * text,int c,char ** endptr)582 static bool ExtractUntilChar(char *text, int c, char **endptr) {
583   CHECK_NE(text, NULL);
584   CHECK_NE(endptr, NULL);
585   char *found;
586   found = strchr(text, c);
587   if (found == NULL) {
588     *endptr = NULL;
589     return false;
590   }
591 
592   *endptr = found;
593   *found = '\0';
594   return true;
595 }
596 
597 // Increments |*text_pointer| while it points a whitespace character.
598 // It is to follow sscanf's whilespace handling.
SkipWhileWhitespace(char ** text_pointer,int c)599 static void SkipWhileWhitespace(char **text_pointer, int c) {
600   if (isspace(c)) {
601     while (isspace(**text_pointer) && isspace(*((*text_pointer) + 1))) {
602       ++(*text_pointer);
603     }
604   }
605 }
606 
607 template<class T>
StringToInteger(char * text,char ** endptr,int base)608 static T StringToInteger(char *text, char **endptr, int base) {
609   assert(false);
610   return T();
611 }
612 
613 template<>
StringToInteger(char * text,char ** endptr,int base)614 int StringToInteger<int>(char *text, char **endptr, int base) {
615   return strtol(text, endptr, base);
616 }
617 
618 template<>
StringToInteger(char * text,char ** endptr,int base)619 int64 StringToInteger<int64>(char *text, char **endptr, int base) {
620   return strtoll(text, endptr, base);
621 }
622 
623 template<>
StringToInteger(char * text,char ** endptr,int base)624 uint64 StringToInteger<uint64>(char *text, char **endptr, int base) {
625   return strtoull(text, endptr, base);
626 }
627 
628 template<typename T>
StringToIntegerUntilChar(char * text,int base,int c,char ** endptr_result)629 static T StringToIntegerUntilChar(
630     char *text, int base, int c, char **endptr_result) {
631   CHECK_NE(endptr_result, NULL);
632   *endptr_result = NULL;
633 
634   char *endptr_extract;
635   if (!ExtractUntilChar(text, c, &endptr_extract))
636     return 0;
637 
638   T result;
639   char *endptr_strto;
640   result = StringToInteger<T>(text, &endptr_strto, base);
641   *endptr_extract = c;
642 
643   if (endptr_extract != endptr_strto)
644     return 0;
645 
646   *endptr_result = endptr_extract;
647   SkipWhileWhitespace(endptr_result, c);
648 
649   return result;
650 }
651 
CopyStringUntilChar(char * text,unsigned out_len,int c,char * out)652 static char *CopyStringUntilChar(
653     char *text, unsigned out_len, int c, char *out) {
654   char *endptr;
655   if (!ExtractUntilChar(text, c, &endptr))
656     return NULL;
657 
658   strncpy(out, text, out_len);
659   out[out_len-1] = '\0';
660   *endptr = c;
661 
662   SkipWhileWhitespace(&endptr, c);
663   return endptr;
664 }
665 
666 template<typename T>
StringToIntegerUntilCharWithCheck(T * outptr,char * text,int base,int c,char ** endptr)667 static bool StringToIntegerUntilCharWithCheck(
668     T *outptr, char *text, int base, int c, char **endptr) {
669   *outptr = StringToIntegerUntilChar<T>(*endptr, base, c, endptr);
670   if (*endptr == NULL || **endptr == '\0') return false;
671   ++(*endptr);
672   return true;
673 }
674 
ParseProcMapsLine(char * text,uint64 * start,uint64 * end,char * flags,uint64 * offset,int * major,int * minor,int64 * inode,unsigned * filename_offset)675 static bool ParseProcMapsLine(char *text, uint64 *start, uint64 *end,
676                               char *flags, uint64 *offset,
677                               int *major, int *minor, int64 *inode,
678                               unsigned *filename_offset) {
679 #if defined(__linux__)
680   /*
681    * It's similar to:
682    * sscanf(text,"%" SCNx64 "-%" SCNx64 " %4s %" SCNx64 " %x:%x %" SCNd64 " %n",
683    *        start, end, flags, offset, major, minor, inode, filename_offset)
684    */
685   char *endptr = text;
686   if (endptr == NULL || *endptr == '\0')  return false;
687 
688   if (!StringToIntegerUntilCharWithCheck(start, endptr, 16, '-', &endptr))
689     return false;
690 
691   if (!StringToIntegerUntilCharWithCheck(end, endptr, 16, ' ', &endptr))
692     return false;
693 
694   endptr = CopyStringUntilChar(endptr, 5, ' ', flags);
695   if (endptr == NULL || *endptr == '\0')  return false;
696   ++endptr;
697 
698   if (!StringToIntegerUntilCharWithCheck(offset, endptr, 16, ' ', &endptr))
699     return false;
700 
701   if (!StringToIntegerUntilCharWithCheck(major, endptr, 16, ':', &endptr))
702     return false;
703 
704   if (!StringToIntegerUntilCharWithCheck(minor, endptr, 16, ' ', &endptr))
705     return false;
706 
707   if (!StringToIntegerUntilCharWithCheck(inode, endptr, 10, ' ', &endptr))
708     return false;
709 
710   *filename_offset = (endptr - text);
711   return true;
712 #else
713   return false;
714 #endif
715 }
716 
ProcMapsIterator(pid_t pid)717 ProcMapsIterator::ProcMapsIterator(pid_t pid) {
718   Init(pid, NULL, false);
719 }
720 
ProcMapsIterator(pid_t pid,Buffer * buffer)721 ProcMapsIterator::ProcMapsIterator(pid_t pid, Buffer *buffer) {
722   Init(pid, buffer, false);
723 }
724 
ProcMapsIterator(pid_t pid,Buffer * buffer,bool use_maps_backing)725 ProcMapsIterator::ProcMapsIterator(pid_t pid, Buffer *buffer,
726                                    bool use_maps_backing) {
727   Init(pid, buffer, use_maps_backing);
728 }
729 
Init(pid_t pid,Buffer * buffer,bool use_maps_backing)730 void ProcMapsIterator::Init(pid_t pid, Buffer *buffer,
731                             bool use_maps_backing) {
732   pid_ = pid;
733   using_maps_backing_ = use_maps_backing;
734   dynamic_buffer_ = NULL;
735   if (!buffer) {
736     // If the user didn't pass in any buffer storage, allocate it
737     // now. This is the normal case; the signal handler passes in a
738     // static buffer.
739     buffer = dynamic_buffer_ = new Buffer;
740   } else {
741     dynamic_buffer_ = NULL;
742   }
743 
744   ibuf_ = buffer->buf_;
745 
746   stext_ = etext_ = nextline_ = ibuf_;
747   ebuf_ = ibuf_ + Buffer::kBufSize - 1;
748   nextline_ = ibuf_;
749 
750 #if defined(__linux__) || defined(__CYGWIN__) || defined(__CYGWIN32__)
751   if (use_maps_backing) {  // don't bother with clever "self" stuff in this case
752     ConstructFilename("/proc/%d/maps_backing", pid, ibuf_, Buffer::kBufSize);
753   } else if (pid == 0) {
754     // We have to kludge a bit to deal with the args ConstructFilename
755     // expects.  The 1 is never used -- it's only impt. that it's not 0.
756     ConstructFilename("/proc/self/maps", 1, ibuf_, Buffer::kBufSize);
757   } else {
758     ConstructFilename("/proc/%d/maps", pid, ibuf_, Buffer::kBufSize);
759   }
760   // No error logging since this can be called from the crash dump
761   // handler at awkward moments. Users should call Valid() before
762   // using.
763   NO_INTR(fd_ = open(ibuf_, O_RDONLY));
764 #elif defined(__FreeBSD__)
765   // We don't support maps_backing on freebsd
766   if (pid == 0) {
767     ConstructFilename("/proc/curproc/map", 1, ibuf_, Buffer::kBufSize);
768   } else {
769     ConstructFilename("/proc/%d/map", pid, ibuf_, Buffer::kBufSize);
770   }
771   NO_INTR(fd_ = open(ibuf_, O_RDONLY));
772 #elif defined(__sun__)
773   if (pid == 0) {
774     ConstructFilename("/proc/self/map", 1, ibuf_, Buffer::kBufSize);
775   } else {
776     ConstructFilename("/proc/%d/map", pid, ibuf_, Buffer::kBufSize);
777   }
778   NO_INTR(fd_ = open(ibuf_, O_RDONLY));
779 #elif defined(__MACH__)
780   current_image_ = _dyld_image_count();   // count down from the top
781   current_load_cmd_ = -1;
782 #elif defined(PLATFORM_WINDOWS)
783   snapshot_ = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE |
784                                        TH32CS_SNAPMODULE32,
785                                        GetCurrentProcessId());
786   memset(&module_, 0, sizeof(module_));
787 #else
788   fd_ = -1;   // so Valid() is always false
789 #endif
790 
791 }
792 
~ProcMapsIterator()793 ProcMapsIterator::~ProcMapsIterator() {
794 #if defined(PLATFORM_WINDOWS)
795   if (snapshot_ != INVALID_HANDLE_VALUE) CloseHandle(snapshot_);
796 #elif defined(__MACH__)
797   // no cleanup necessary!
798 #else
799   if (fd_ >= 0) NO_INTR(close(fd_));
800 #endif
801   delete dynamic_buffer_;
802 }
803 
Valid() const804 bool ProcMapsIterator::Valid() const {
805 #if defined(PLATFORM_WINDOWS)
806   return snapshot_ != INVALID_HANDLE_VALUE;
807 #elif defined(__MACH__)
808   return 1;
809 #else
810   return fd_ != -1;
811 #endif
812 }
813 
Next(uint64 * start,uint64 * end,char ** flags,uint64 * offset,int64 * inode,char ** filename)814 bool ProcMapsIterator::Next(uint64 *start, uint64 *end, char **flags,
815                             uint64 *offset, int64 *inode, char **filename) {
816   return NextExt(start, end, flags, offset, inode, filename, NULL, NULL,
817                  NULL, NULL, NULL);
818 }
819 
820 // This has too many arguments.  It should really be building
821 // a map object and returning it.  The problem is that this is called
822 // when the memory allocator state is undefined, hence the arguments.
NextExt(uint64 * start,uint64 * end,char ** flags,uint64 * offset,int64 * inode,char ** filename,uint64 * file_mapping,uint64 * file_pages,uint64 * anon_mapping,uint64 * anon_pages,dev_t * dev)823 bool ProcMapsIterator::NextExt(uint64 *start, uint64 *end, char **flags,
824                                uint64 *offset, int64 *inode, char **filename,
825                                uint64 *file_mapping, uint64 *file_pages,
826                                uint64 *anon_mapping, uint64 *anon_pages,
827                                dev_t *dev) {
828 
829 #if defined(__linux__) || defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__CYGWIN32__)
830   do {
831     // Advance to the start of the next line
832     stext_ = nextline_;
833 
834     // See if we have a complete line in the buffer already
835     nextline_ = static_cast<char *>(memchr (stext_, '\n', etext_ - stext_));
836     if (!nextline_) {
837       // Shift/fill the buffer so we do have a line
838       int count = etext_ - stext_;
839 
840       // Move the current text to the start of the buffer
841       memmove(ibuf_, stext_, count);
842       stext_ = ibuf_;
843       etext_ = ibuf_ + count;
844 
845       int nread = 0;            // fill up buffer with text
846       while (etext_ < ebuf_) {
847         NO_INTR(nread = read(fd_, etext_, ebuf_ - etext_));
848         if (nread > 0)
849           etext_ += nread;
850         else
851           break;
852       }
853 
854       // Zero out remaining characters in buffer at EOF to avoid returning
855       // garbage from subsequent calls.
856       if (etext_ != ebuf_ && nread == 0) {
857         memset(etext_, 0, ebuf_ - etext_);
858       }
859       *etext_ = '\n';   // sentinel; safe because ibuf extends 1 char beyond ebuf
860       nextline_ = static_cast<char *>(memchr (stext_, '\n', etext_ + 1 - stext_));
861     }
862     *nextline_ = 0;                // turn newline into nul
863     nextline_ += ((nextline_ < etext_)? 1 : 0);  // skip nul if not end of text
864     // stext_ now points at a nul-terminated line
865     uint64 tmpstart, tmpend, tmpoffset;
866     int64 tmpinode;
867     int major, minor;
868     unsigned filename_offset = 0;
869 #if defined(__linux__)
870     // for now, assume all linuxes have the same format
871     if (!ParseProcMapsLine(
872         stext_,
873         start ? start : &tmpstart,
874         end ? end : &tmpend,
875         flags_,
876         offset ? offset : &tmpoffset,
877         &major, &minor,
878         inode ? inode : &tmpinode, &filename_offset)) continue;
879 #elif defined(__CYGWIN__) || defined(__CYGWIN32__)
880     // cygwin is like linux, except the third field is the "entry point"
881     // rather than the offset (see format_process_maps at
882     // http://cygwin.com/cgi-bin/cvsweb.cgi/src/winsup/cygwin/fhandler_process.cc?rev=1.89&content-type=text/x-cvsweb-markup&cvsroot=src
883     // Offset is always be 0 on cygwin: cygwin implements an mmap
884     // by loading the whole file and then calling NtMapViewOfSection.
885     // Cygwin also seems to set its flags kinda randomly; use windows default.
886     char tmpflags[5];
887     if (offset)
888       *offset = 0;
889     strcpy(flags_, "r-xp");
890     if (sscanf(stext_, "%llx-%llx %4s %llx %x:%x %lld %n",
891                start ? start : &tmpstart,
892                end ? end : &tmpend,
893                tmpflags,
894                &tmpoffset,
895                &major, &minor,
896                inode ? inode : &tmpinode, &filename_offset) != 7) continue;
897 #elif defined(__FreeBSD__)
898     // 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
899     tmpstart = tmpend = tmpoffset = 0;
900     tmpinode = 0;
901     major = minor = 0;   // can't get this info in freebsd
902     if (inode)
903       *inode = 0;        // nor this
904     if (offset)
905       *offset = 0;       // seems like this should be in there, but maybe not
906     // start end resident privateresident obj(?) prot refcnt shadowcnt
907     // flags copy_on_write needs_copy type filename:
908     // 0x8048000 0x804a000 2 0 0xc104ce70 r-x 1 0 0x0 COW NC vnode /bin/cat
909     if (sscanf(stext_, "0x%"SCNx64" 0x%"SCNx64" %*d %*d %*p %3s %*d %*d 0x%*x %*s %*s %*s %n",
910                start ? start : &tmpstart,
911                end ? end : &tmpend,
912                flags_,
913                &filename_offset) != 3) continue;
914 #endif
915 
916     // Depending on the Linux kernel being used, there may or may not be a space
917     // after the inode if there is no filename.  sscanf will in such situations
918     // nondeterministically either fill in filename_offset or not (the results
919     // differ on multiple calls in the same run even with identical arguments).
920     // We don't want to wander off somewhere beyond the end of the string.
921     size_t stext_length = strlen(stext_);
922     if (filename_offset == 0 || filename_offset > stext_length)
923       filename_offset = stext_length;
924 
925     // We found an entry
926     if (flags) *flags = flags_;
927     if (filename) *filename = stext_ + filename_offset;
928     if (dev) *dev = minor | (major << 8);
929 
930     if (using_maps_backing_) {
931       // Extract and parse physical page backing info.
932       char *backing_ptr = stext_ + filename_offset +
933           strlen(stext_+filename_offset);
934 
935       // find the second '('
936       int paren_count = 0;
937       while (--backing_ptr > stext_) {
938         if (*backing_ptr == '(') {
939           ++paren_count;
940           if (paren_count >= 2) {
941             uint64 tmp_file_mapping;
942             uint64 tmp_file_pages;
943             uint64 tmp_anon_mapping;
944             uint64 tmp_anon_pages;
945 
946             sscanf(backing_ptr+1,
947                    "F %" SCNx64 " %" SCNd64 ") (A %" SCNx64 " %" SCNd64 ")",
948                    file_mapping ? file_mapping : &tmp_file_mapping,
949                    file_pages ? file_pages : &tmp_file_pages,
950                    anon_mapping ? anon_mapping : &tmp_anon_mapping,
951                    anon_pages ? anon_pages : &tmp_anon_pages);
952             // null terminate the file name (there is a space
953             // before the first (.
954             backing_ptr[-1] = 0;
955             break;
956           }
957         }
958       }
959     }
960 
961     return true;
962   } while (etext_ > ibuf_);
963 #elif defined(__sun__)
964   // This is based on MA_READ == 4, MA_WRITE == 2, MA_EXEC == 1
965   static char kPerms[8][4] = { "---", "--x", "-w-", "-wx",
966                                "r--", "r-x", "rw-", "rwx" };
967   COMPILE_ASSERT(MA_READ == 4, solaris_ma_read_must_equal_4);
968   COMPILE_ASSERT(MA_WRITE == 2, solaris_ma_write_must_equal_2);
969   COMPILE_ASSERT(MA_EXEC == 1, solaris_ma_exec_must_equal_1);
970   Buffer object_path;
971   int nread = 0;            // fill up buffer with text
972   NO_INTR(nread = read(fd_, ibuf_, sizeof(prmap_t)));
973   if (nread == sizeof(prmap_t)) {
974     long inode_from_mapname = 0;
975     prmap_t* mapinfo = reinterpret_cast<prmap_t*>(ibuf_);
976     // Best-effort attempt to get the inode from the filename.  I think the
977     // two middle ints are major and minor device numbers, but I'm not sure.
978     sscanf(mapinfo->pr_mapname, "ufs.%*d.%*d.%ld", &inode_from_mapname);
979 
980     if (pid_ == 0) {
981       CHECK_LT(snprintf(object_path.buf_, Buffer::kBufSize,
982                         "/proc/self/path/%s", mapinfo->pr_mapname),
983                Buffer::kBufSize);
984     } else {
985       CHECK_LT(snprintf(object_path.buf_, Buffer::kBufSize,
986                         "/proc/%d/path/%s",
987                         static_cast<int>(pid_), mapinfo->pr_mapname),
988                Buffer::kBufSize);
989     }
990     ssize_t len = readlink(object_path.buf_, current_filename_, PATH_MAX);
991     CHECK_LT(len, PATH_MAX);
992     if (len < 0)
993       len = 0;
994     current_filename_[len] = '\0';
995 
996     if (start) *start = mapinfo->pr_vaddr;
997     if (end) *end = mapinfo->pr_vaddr + mapinfo->pr_size;
998     if (flags) *flags = kPerms[mapinfo->pr_mflags & 7];
999     if (offset) *offset = mapinfo->pr_offset;
1000     if (inode) *inode = inode_from_mapname;
1001     if (filename) *filename = current_filename_;
1002     if (file_mapping) *file_mapping = 0;
1003     if (file_pages) *file_pages = 0;
1004     if (anon_mapping) *anon_mapping = 0;
1005     if (anon_pages) *anon_pages = 0;
1006     if (dev) *dev = 0;
1007     return true;
1008   }
1009 #elif defined(__MACH__)
1010   // We return a separate entry for each segment in the DLL. (TODO(csilvers):
1011   // can we do better?)  A DLL ("image") has load-commands, some of which
1012   // talk about segment boundaries.
1013   // cf image_for_address from http://svn.digium.com/view/asterisk/team/oej/minivoicemail/dlfcn.c?revision=53912
1014   for (; current_image_ >= 0; current_image_--) {
1015     const mach_header* hdr = _dyld_get_image_header(current_image_);
1016     if (!hdr) continue;
1017     if (current_load_cmd_ < 0)   // set up for this image
1018       current_load_cmd_ = hdr->ncmds;  // again, go from the top down
1019 
1020     // We start with the next load command (we've already looked at this one).
1021     for (current_load_cmd_--; current_load_cmd_ >= 0; current_load_cmd_--) {
1022 #ifdef MH_MAGIC_64
1023       if (NextExtMachHelper<MH_MAGIC_64, LC_SEGMENT_64,
1024                             struct mach_header_64, struct segment_command_64>(
1025                                 hdr, current_image_, current_load_cmd_,
1026                                 start, end, flags, offset, inode, filename,
1027                                 file_mapping, file_pages, anon_mapping,
1028                                 anon_pages, dev)) {
1029         return true;
1030       }
1031 #endif
1032       if (NextExtMachHelper<MH_MAGIC, LC_SEGMENT,
1033                             struct mach_header, struct segment_command>(
1034                                 hdr, current_image_, current_load_cmd_,
1035                                 start, end, flags, offset, inode, filename,
1036                                 file_mapping, file_pages, anon_mapping,
1037                                 anon_pages, dev)) {
1038         return true;
1039       }
1040     }
1041     // If we get here, no more load_cmd's in this image talk about
1042     // segments.  Go on to the next image.
1043   }
1044 #elif defined(PLATFORM_WINDOWS)
1045   static char kDefaultPerms[5] = "r-xp";
1046   BOOL ok;
1047   if (module_.dwSize == 0) {  // only possible before first call
1048     module_.dwSize = sizeof(module_);
1049     ok = Module32First(snapshot_, &module_);
1050   } else {
1051     ok = Module32Next(snapshot_, &module_);
1052   }
1053   if (ok) {
1054     uint64 base_addr = reinterpret_cast<DWORD_PTR>(module_.modBaseAddr);
1055     if (start) *start = base_addr;
1056     if (end) *end = base_addr + module_.modBaseSize;
1057     if (flags) *flags = kDefaultPerms;
1058     if (offset) *offset = 0;
1059     if (inode) *inode = 0;
1060     if (filename) *filename = module_.szExePath;
1061     if (file_mapping) *file_mapping = 0;
1062     if (file_pages) *file_pages = 0;
1063     if (anon_mapping) *anon_mapping = 0;
1064     if (anon_pages) *anon_pages = 0;
1065     if (dev) *dev = 0;
1066     return true;
1067   }
1068 #endif
1069 
1070   // We didn't find anything
1071   return false;
1072 }
1073 
FormatLine(char * buffer,int bufsize,uint64 start,uint64 end,const char * flags,uint64 offset,int64 inode,const char * filename,dev_t dev)1074 int ProcMapsIterator::FormatLine(char* buffer, int bufsize,
1075                                  uint64 start, uint64 end, const char *flags,
1076                                  uint64 offset, int64 inode,
1077                                  const char *filename, dev_t dev) {
1078   // We assume 'flags' looks like 'rwxp' or 'rwx'.
1079   char r = (flags && flags[0] == 'r') ? 'r' : '-';
1080   char w = (flags && flags[0] && flags[1] == 'w') ? 'w' : '-';
1081   char x = (flags && flags[0] && flags[1] && flags[2] == 'x') ? 'x' : '-';
1082   // p always seems set on linux, so we set the default to 'p', not '-'
1083   char p = (flags && flags[0] && flags[1] && flags[2] && flags[3] != 'p')
1084       ? '-' : 'p';
1085 
1086   const int rc = snprintf(buffer, bufsize,
1087                           "%08" PRIx64 "-%08" PRIx64 " %c%c%c%c %08" PRIx64 " "
1088                           "%02x:%02x %-11" PRId64 " %s\n",
1089                           start, end, r,w,x,p, offset,
1090                           static_cast<int>(dev/256), static_cast<int>(dev%256),
1091                           inode, filename);
1092   return (rc < 0 || rc >= bufsize) ? 0 : rc;
1093 }
1094 
1095 namespace tcmalloc {
1096 
1097 // Helper to add the list of mapped shared libraries to a profile.
1098 // Fill formatted "/proc/self/maps" contents into buffer 'buf' of size 'size'
1099 // and return the actual size occupied in 'buf'.  We fill wrote_all to true
1100 // if we successfully wrote all proc lines to buf, false else.
1101 // We do not provision for 0-terminating 'buf'.
FillProcSelfMaps(char buf[],int size,bool * wrote_all)1102 int FillProcSelfMaps(char buf[], int size, bool* wrote_all) {
1103   ProcMapsIterator::Buffer iterbuf;
1104   ProcMapsIterator it(0, &iterbuf);   // 0 means "current pid"
1105 
1106   uint64 start, end, offset;
1107   int64 inode;
1108   char *flags, *filename;
1109   int bytes_written = 0;
1110   *wrote_all = true;
1111   while (it.Next(&start, &end, &flags, &offset, &inode, &filename)) {
1112     const int line_length = it.FormatLine(buf + bytes_written,
1113                                           size - bytes_written,
1114                                           start, end, flags, offset,
1115                                           inode, filename, 0);
1116     if (line_length == 0)
1117       *wrote_all = false;     // failed to write this line out
1118     else
1119       bytes_written += line_length;
1120 
1121   }
1122   return bytes_written;
1123 }
1124 
1125 // Dump the same data as FillProcSelfMaps reads to fd.
1126 // It seems easier to repeat parts of FillProcSelfMaps here than to
1127 // reuse it via a call.
DumpProcSelfMaps(RawFD fd)1128 void DumpProcSelfMaps(RawFD fd) {
1129   ProcMapsIterator::Buffer iterbuf;
1130   ProcMapsIterator it(0, &iterbuf);   // 0 means "current pid"
1131 
1132   uint64 start, end, offset;
1133   int64 inode;
1134   char *flags, *filename;
1135   ProcMapsIterator::Buffer linebuf;
1136   while (it.Next(&start, &end, &flags, &offset, &inode, &filename)) {
1137     int written = it.FormatLine(linebuf.buf_, sizeof(linebuf.buf_),
1138                                 start, end, flags, offset, inode, filename,
1139                                 0);
1140     RawWrite(fd, linebuf.buf_, written);
1141   }
1142 }
1143 
1144 }  // namespace tcmalloc
1145