summaryrefslogtreecommitdiffstats
path: root/chromium/base/trace_event/process_memory_maps_dump_provider.cc
blob: 680fa29609ebdf042b1bc5db69a33515cec6d5ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/trace_event/process_memory_maps_dump_provider.h"

#include <cctype>
#include <fstream>

#include "base/logging.h"
#include "base/process/process_metrics.h"
#include "base/trace_event/process_memory_dump.h"
#include "base/trace_event/process_memory_maps.h"

namespace base {
namespace trace_event {

#if defined(OS_LINUX) || defined(OS_ANDROID)
// static
std::istream* ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = nullptr;

namespace {

const uint32 kMaxLineSize = 4096;

bool ParseSmapsHeader(std::istream* smaps,
                      ProcessMemoryMaps::VMRegion* region) {
  // e.g., "00400000-00421000 r-xp 00000000 fc:01 1234  /foo.so\n"
  bool res = true;  // Whether this region should be appended or skipped.
  uint64 end_addr;
  std::string protection_flags;
  std::string ignored;
  *smaps >> std::hex >> region->start_address;
  smaps->ignore(1);
  *smaps >> std::hex >> end_addr;
  if (end_addr > region->start_address) {
    region->size_in_bytes = end_addr - region->start_address;
  } else {
    // This is not just paranoia, it can actually happen (See crbug.com/461237).
    region->size_in_bytes = 0;
    res = false;
  }

  region->protection_flags = 0;
  *smaps >> protection_flags;
  CHECK_EQ(4UL, protection_flags.size());
  if (protection_flags[0] == 'r') {
    region->protection_flags |=
        ProcessMemoryMaps::VMRegion::kProtectionFlagsRead;
  }
  if (protection_flags[1] == 'w') {
    region->protection_flags |=
        ProcessMemoryMaps::VMRegion::kProtectionFlagsWrite;
  }
  if (protection_flags[2] == 'x') {
    region->protection_flags |=
        ProcessMemoryMaps::VMRegion::kProtectionFlagsExec;
  }
  *smaps >> ignored;  // Ignore mapped file offset.
  *smaps >> ignored;  // Ignore device maj-min (fc:01 in the example above).
  *smaps >> ignored;  // Ignore inode number (1234 in the example above).

  while (smaps->peek() == ' ')
    smaps->ignore(1);
  char mapped_file[kMaxLineSize];
  smaps->getline(mapped_file, sizeof(mapped_file));
  region->mapped_file = mapped_file;

  return res;
}

uint64 ReadCounterBytes(std::istream* smaps) {
  uint64 counter_value = 0;
  *smaps >> std::dec >> counter_value;
  return counter_value * 1024;
}

uint32 ParseSmapsCounter(std::istream* smaps,
                         ProcessMemoryMaps::VMRegion* region) {
  // A smaps counter lines looks as follows: "RSS:  0 Kb\n"
  uint32 res = 0;
  std::string counter_name;
  *smaps >> counter_name;

  // TODO(primiano): "Swap" should also be accounted as resident. Check
  // whether Rss isn't already counting swapped and fix below if that is
  // the case.
  if (counter_name == "Pss:") {
    region->byte_stats_proportional_resident = ReadCounterBytes(smaps);
    res = 1;
  } else if (counter_name == "Private_Dirty:" ||
             counter_name == "Private_Clean:") {
    // For Private and Shared counters keep the sum of the dirty + clean stats.
    region->byte_stats_private_resident += ReadCounterBytes(smaps);
    res = 1;
  } else if (counter_name == "Shared_Dirty:" ||
             counter_name == "Shared_Clean:") {
    region->byte_stats_shared_resident += ReadCounterBytes(smaps);
    res = 1;
  }

#ifndef NDEBUG
  // Paranoid check against changes of the Kernel /proc interface.
  if (res) {
    std::string unit;
    *smaps >> unit;
    DCHECK_EQ("kB", unit);
  }
#endif

  smaps->ignore(kMaxLineSize, '\n');

  return res;
}

uint32 ReadLinuxProcSmapsFile(std::istream* smaps, ProcessMemoryMaps* pmm) {
  if (!smaps->good())
    return 0;

  const uint32 kNumExpectedCountersPerRegion = 5;
  uint32 counters_parsed_for_current_region = 0;
  uint32 num_valid_regions = 0;
  ProcessMemoryMaps::VMRegion region;
  bool should_add_current_region = false;
  for (;;) {
    int next = smaps->peek();
    if (next == std::ifstream::traits_type::eof() || next == '\n')
      break;
    if (isxdigit(next) && !isupper(next)) {
      region = {0};
      counters_parsed_for_current_region = 0;
      should_add_current_region = ParseSmapsHeader(smaps, &region);
    } else {
      counters_parsed_for_current_region += ParseSmapsCounter(smaps, &region);
      DCHECK_LE(counters_parsed_for_current_region,
                kNumExpectedCountersPerRegion);
      if (counters_parsed_for_current_region == kNumExpectedCountersPerRegion) {
        if (should_add_current_region) {
          pmm->AddVMRegion(region);
          ++num_valid_regions;
          should_add_current_region = false;
        }
      }
    }
  }
  return num_valid_regions;
}

}  // namespace
#endif  // defined(OS_LINUX) || defined(OS_ANDROID)

// static
ProcessMemoryMapsDumpProvider* ProcessMemoryMapsDumpProvider::GetInstance() {
  return Singleton<ProcessMemoryMapsDumpProvider,
                   LeakySingletonTraits<ProcessMemoryMapsDumpProvider>>::get();
}

ProcessMemoryMapsDumpProvider::ProcessMemoryMapsDumpProvider() {
}

ProcessMemoryMapsDumpProvider::~ProcessMemoryMapsDumpProvider() {
}

// Called at trace dump point time. Creates a snapshot the memory maps for the
// current process.
bool ProcessMemoryMapsDumpProvider::OnMemoryDump(ProcessMemoryDump* pmd) {
  uint32 res = 0;

#if defined(OS_LINUX) || defined(OS_ANDROID)
  if (UNLIKELY(proc_smaps_for_testing)) {
    res = ReadLinuxProcSmapsFile(proc_smaps_for_testing, pmd->process_mmaps());
  } else {
    std::ifstream proc_self_smaps("/proc/self/smaps");
    res = ReadLinuxProcSmapsFile(&proc_self_smaps, pmd->process_mmaps());
  }
#else
  LOG(ERROR) << "ProcessMemoryMaps dump provider is supported only on Linux";
#endif

  if (res > 0) {
    pmd->set_has_process_mmaps();
    return true;
  }

  return false;
}

}  // namespace trace_event
}  // namespace base