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