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