1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_MemoryMapping_h
8 #define mozilla_MemoryMapping_h
11 #include "mozilla/EnumSet.h"
12 #include "nsStringFwd.h"
13 #include "nsTArrayForwardDeclare.h"
16 * MemoryMapping is a helper class which describes an entry in the Linux
17 * /proc/<pid>/smaps file. See procfs(5) for details on the entry format.
19 * The GetMemoryMappings() function returns an array of such entries, sorted by
20 * start address, one for each entry in the current process's address space.
25 enum class VMFlag
: uint8_t {
26 Readable
, // rd - readable
27 Writable
, // wr - writable
28 Executable
, // ex - executable
29 Shared
, // sh - shared
30 MayRead
, // mr - may read
31 MayWrite
, // mw - may write
32 MayExecute
, // me - may execute
33 MayShare
, // ms - may share
34 GrowsDown
, // gd - stack segment grows down
35 PurePFN
, // pf - pure PFN range
36 DisabledWrite
, // dw - disabled write to the mapped file
37 Locked
, // lo - pages are locked in memory
38 IO
, // io - memory mapped I/O area
39 Sequential
, // sr - sequential read advise provided
40 Random
, // rr - random read advise provided
41 NoFork
, // dc - do not copy area on fork
42 NoExpand
, // de - do not expand area on remapping
43 Accountable
, // ac - area is accountable
44 NotReserved
, // nr - swap space is not reserved for the area
45 HugeTLB
, // ht - area uses huge tlb pages
46 NonLinear
, // nl - non-linear mapping
47 ArchSpecific
, // ar - architecture specific flag
48 NoCore
, // dd - do not include area into core dump
49 SoftDirty
, // sd - soft-dirty flag
50 MixedMap
, // mm - mixed map area
51 HugePage
, // hg - huge page advise flag
52 NoHugePage
, // nh - no-huge page advise flag
53 Mergeable
, // mg - mergeable advise flag
56 using VMFlagSet
= EnumSet
<VMFlag
, uint32_t>;
58 class MemoryMapping final
{
60 enum class Perm
: uint8_t {
68 using PermSet
= EnumSet
<Perm
>;
70 MemoryMapping(uintptr_t aStart
, uintptr_t aEnd
, PermSet aPerms
,
71 size_t aOffset
, const char* aName
)
78 const nsCString
& Name() const { return mName
; }
80 uintptr_t Start() const { return mStart
; }
81 uintptr_t End() const { return mEnd
; }
83 bool Includes(const void* aPtr
) const {
84 auto ptr
= uintptr_t(aPtr
);
85 return ptr
>= mStart
&& ptr
< mEnd
;
88 PermSet
Perms() const { return mPerms
; }
89 VMFlagSet
VMFlags() const { return mFlags
; }
91 // For file mappings, the offset in the mapped file which corresponds to the
92 // start of the mapped region.
93 size_t Offset() const { return mOffset
; }
95 size_t AnonHugePages() const { return mAnonHugePages
; }
96 size_t Anonymous() const { return mAnonymous
; }
97 size_t KernelPageSize() const { return mKernelPageSize
; }
98 size_t LazyFree() const { return mLazyFree
; }
99 size_t Locked() const { return mLocked
; }
100 size_t MMUPageSize() const { return mMMUPageSize
; }
101 size_t Private_Clean() const { return mPrivate_Clean
; }
102 size_t Private_Dirty() const { return mPrivate_Dirty
; }
103 size_t Private_Hugetlb() const { return mPrivate_Hugetlb
; }
104 size_t Pss() const { return mPss
; }
105 size_t Referenced() const { return mReferenced
; }
106 size_t Rss() const { return mRss
; }
107 size_t Shared_Clean() const { return mShared_Clean
; }
108 size_t Shared_Dirty() const { return mShared_Dirty
; }
109 size_t Shared_Hugetlb() const { return mShared_Hugetlb
; }
110 size_t ShmemPmdMapped() const { return mShmemPmdMapped
; }
111 size_t Size() const { return mSize
; }
112 size_t Swap() const { return mSwap
; }
113 size_t SwapPss() const { return mSwapPss
; }
115 // Dumps a string representation of the entry, similar to its format in the
116 // smaps file, to the given string. Mainly useful for debugging.
117 void Dump(nsACString
& aOut
) const;
119 // These comparison operators are used for binary searching sorted arrays of
120 // MemoryMapping entries to find the one which contains a given pointer.
121 bool operator==(const void* aPtr
) const { return Includes(aPtr
); }
122 bool operator<(const void* aPtr
) const { return mStart
< uintptr_t(aPtr
); }
125 friend nsresult
GetMemoryMappings(nsTArray
<MemoryMapping
>& aMappings
,
128 uintptr_t mStart
= 0;
135 // Members for size fields in the smaps file. Please keep these in sync with
136 // the sFields array.
137 size_t mAnonHugePages
= 0;
138 size_t mAnonymous
= 0;
139 size_t mKernelPageSize
= 0;
140 size_t mLazyFree
= 0;
142 size_t mMMUPageSize
= 0;
143 size_t mPrivate_Clean
= 0;
144 size_t mPrivate_Dirty
= 0;
145 size_t mPrivate_Hugetlb
= 0;
147 size_t mReferenced
= 0;
149 size_t mShared_Clean
= 0;
150 size_t mShared_Dirty
= 0;
151 size_t mShared_Hugetlb
= 0;
152 size_t mShmemPmdMapped
= 0;
160 // Contains the name and offset of one of the above size_t fields, for use in
161 // parsing in dumping. The below helpers contain a list of the fields, and map
162 // Field entries to the appropriate member in a class instance.
168 static const Field sFields
[20];
170 size_t& ValueForField(const Field
& aField
) {
171 char* fieldPtr
= reinterpret_cast<char*>(this) + aField
.mOffset
;
172 return reinterpret_cast<size_t*>(fieldPtr
)[0];
174 size_t ValueForField(const Field
& aField
) const {
175 return const_cast<MemoryMapping
*>(this)->ValueForField(aField
);
179 nsresult
GetMemoryMappings(nsTArray
<MemoryMapping
>& aMappings
, pid_t aPid
= 0);
181 } // namespace mozilla
183 #endif // mozilla_MemoryMapping_h