• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_DEBUG_PROC_MAPS_LINUX_H_
6 #define BASE_DEBUG_PROC_MAPS_LINUX_H_
7 
8 #include <stdint.h>
9 
10 #include <string>
11 #include <vector>
12 
13 #include "base/base_export.h"
14 
15 namespace base {
16 namespace debug {
17 
18 // Describes a region of mapped memory and the path of the file mapped.
19 struct MappedMemoryRegion {
20   enum Permission {
21     READ = 1 << 0,
22     WRITE = 1 << 1,
23     EXECUTE = 1 << 2,
24     PRIVATE = 1 << 3,  // If set, region is private, otherwise it is shared.
25   };
26 
27   // The address range [start,end) of mapped memory.
28   uintptr_t start;
29   uintptr_t end;
30 
31   // Byte offset into |path| of the range mapped into memory.
32   unsigned long long offset;
33 
34   // Image base, if this mapping corresponds to an ELF image.
35   uintptr_t base;
36 
37   // Bitmask of read/write/execute/private/shared permissions.
38   uint8_t permissions;
39 
40   // Name of the file mapped into memory.
41   //
42   // NOTE: path names aren't guaranteed to point at valid files. For example,
43   // "[heap]" and "[stack]" are used to represent the location of the process'
44   // heap and stack, respectively.
45   std::string path;
46 };
47 
48 // Reads the data from /proc/self/maps and stores the result in |proc_maps|.
49 // Returns true if successful, false otherwise.
50 //
51 // There is *NO* guarantee that the resulting contents will be free of
52 // duplicates or even contain valid entries by time the method returns.
53 //
54 //
55 // THE GORY DETAILS
56 //
57 // Did you know it's next-to-impossible to atomically read the whole contents
58 // of /proc/<pid>/maps? You would think that if we passed in a large-enough
59 // buffer to read() that It Should Just Work(tm), but sadly that's not the case.
60 //
61 // Linux's procfs uses seq_file [1] for handling iteration, text formatting,
62 // and dealing with resulting data that is larger than the size of a page. That
63 // last bit is especially important because it means that seq_file will never
64 // return more than the size of a page in a single call to read().
65 //
66 // Unfortunately for a program like Chrome the size of /proc/self/maps is
67 // larger than the size of page so we're forced to call read() multiple times.
68 // If the virtual memory table changed in any way between calls to read() (e.g.,
69 // a different thread calling mprotect()), it can make seq_file generate
70 // duplicate entries or skip entries.
71 //
72 // Even if seq_file was changed to keep flushing the contents of its page-sized
73 // buffer to the usermode buffer inside a single call to read(), it has to
74 // release its lock on the virtual memory table to handle page faults while
75 // copying data to usermode. This puts us in the same situation where the table
76 // can change while we're copying data.
77 //
78 // Alternatives such as fork()-and-suspend-the-parent-while-child-reads were
79 // attempted, but they present more subtle problems than it's worth. Depending
80 // on your use case your best bet may be to read /proc/<pid>/maps prior to
81 // starting other threads.
82 //
83 // [1] http://kernelnewbies.org/Documents/SeqFileHowTo
84 BASE_EXPORT bool ReadProcMaps(std::string* proc_maps);
85 
86 // Parses /proc/<pid>/maps input data and stores in |regions|. Returns true
87 // and updates |regions| if and only if all of |input| was successfully parsed.
88 BASE_EXPORT bool ParseProcMaps(const std::string& input,
89                                std::vector<MappedMemoryRegion>* regions);
90 
91 }  // namespace debug
92 }  // namespace base
93 
94 #endif  // BASE_DEBUG_PROC_MAPS_LINUX_H_
95