summaryrefslogtreecommitdiff
path: root/simpleperf/MapRecordReader.cpp
blob: 5731eb8ecd117e688e95f7a84f86e0660b9870e1 (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
190
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "MapRecordReader.h"

#include <stdint.h>
#include <sys/mman.h>
#include <sys/stat.h>

#include <vector>

#include <android-base/strings.h>

#include "environment.h"

namespace simpleperf {

bool MapRecordReader::ReadKernelMaps() {
  KernelMmap kernel_mmap;
  std::vector<KernelMmap> module_mmaps;
  GetKernelAndModuleMmaps(&kernel_mmap, &module_mmaps);

  MmapRecord mmap_record(attr_, true, UINT_MAX, 0, kernel_mmap.start_addr, kernel_mmap.len, 0,
                         kernel_mmap.filepath, event_id_);
  if (!callback_(&mmap_record)) {
    return false;
  }
  for (const auto& module_mmap : module_mmaps) {
    MmapRecord mmap_record(attr_, true, UINT_MAX, 0, module_mmap.start_addr, module_mmap.len, 0,
                           module_mmap.filepath, event_id_);
    if (!callback_(&mmap_record)) {
      return false;
    }
  }
  return true;
}

bool MapRecordReader::ReadProcessMaps(pid_t pid, uint64_t timestamp) {
  std::vector<pid_t> tids = GetThreadsInProcess(pid);
  return ReadProcessMaps(pid, std::unordered_set<pid_t>(tids.begin(), tids.end()), timestamp);
}

bool MapRecordReader::ReadProcessMaps(pid_t pid, const std::unordered_set<pid_t>& tids,
                                      uint64_t timestamp) {
  // Dump mmap records.
  std::vector<ThreadMmap> thread_mmaps;
  if (!GetThreadMmapsInProcess(pid, &thread_mmaps)) {
    // The process may exit before we get its info.
    return true;
  }
  for (const auto& map : thread_mmaps) {
    if (!(map.prot & PROT_EXEC) && !keep_non_executable_maps_) {
      continue;
    }
    Mmap2Record record(attr_, false, pid, pid, map.start_addr, map.len, map.pgoff, map.prot,
                       map.name, event_id_, timestamp);
    if (!callback_(&record)) {
      return false;
    }
  }
  // Dump process name.
  std::string process_name = GetCompleteProcessName(pid);
  if (!process_name.empty()) {
    CommRecord record(attr_, pid, pid, process_name, event_id_, timestamp);
    if (!callback_(&record)) {
      return false;
    }
  }
  // Dump thread info.
  for (const auto& tid : tids) {
    std::string name;
    if (tid != pid && GetThreadName(tid, &name)) {
      // If a thread name matches the suffix of its process name, probably the thread name
      // is stripped by TASK_COMM_LEN.
      if (android::base::EndsWith(process_name, name)) {
        name = process_name;
      }
      CommRecord comm_record(attr_, pid, tid, name, event_id_, timestamp);
      if (!callback_(&comm_record)) {
        return false;
      }
    }
  }
  return true;
}

MapRecordThread::MapRecordThread(const MapRecordReader& map_record_reader)
    : map_record_reader_(map_record_reader), fp_(nullptr, fclose) {
  map_record_reader_.SetCallback([this](Record* r) { return WriteRecordToFile(r); });
  tmpfile_ = ScopedTempFiles::CreateTempFile();
  fp_.reset(fdopen(tmpfile_->release(), "r+"));
  thread_ = std::thread([this]() { thread_result_ = RunThread(); });
}

MapRecordThread::~MapRecordThread() {
  if (thread_.joinable()) {
    early_stop_ = true;
    thread_.join();
  }
}

bool MapRecordThread::RunThread() {
  if (!fp_) {
    return false;
  }
  if (!map_record_reader_.ReadKernelMaps()) {
    return false;
  }
  for (auto pid : GetAllProcesses()) {
    if (early_stop_) {
      return false;
    }
    if (!map_record_reader_.ReadProcessMaps(pid, 0)) {
      return false;
    }
  }
  return true;
}

bool MapRecordThread::WriteRecordToFile(Record* record) {
  if (fwrite(record->Binary(), record->size(), 1, fp_.get()) != 1) {
    PLOG(ERROR) << "failed to write map records to file";
    return false;
  }
  return true;
}

bool MapRecordThread::Join() {
  thread_.join();
  if (!thread_result_) {
    LOG(ERROR) << "map record thread failed";
  }
  return thread_result_;
}

bool MapRecordThread::ReadMapRecords(const std::function<bool(Record*)>& callback) {
  off_t offset = ftello(fp_.get());
  if (offset == -1) {
    PLOG(ERROR) << "ftello() failed";
    return false;
  }
  uint64_t file_size = static_cast<uint64_t>(offset);
  if (fseek(fp_.get(), 0, SEEK_SET) != 0) {
    PLOG(ERROR) << "fseek() failed";
    return false;
  }
  uint64_t nread = 0;
  std::vector<char> buffer(1024);
  while (nread < file_size) {
    if (fread(buffer.data(), Record::header_size(), 1, fp_.get()) != 1) {
      PLOG(ERROR) << "fread() failed";
      return false;
    }
    RecordHeader header;
    if (!header.Parse(buffer.data())) {
      return false;
    }
    if (buffer.size() < header.size) {
      buffer.resize(header.size);
    }
    if (fread(buffer.data() + Record::header_size(), header.size - Record::header_size(), 1,
              fp_.get()) != 1) {
      PLOG(ERROR) << "fread() failed";
      return false;
    }
    auto r = ReadRecordFromBuffer(map_record_reader_.Attr(), header.type, buffer.data(),
                                  buffer.data() + header.size);
    CHECK(r);
    if (!callback(r.get())) {
      return false;
    }
    nread += header.size;
  }
  return true;
}

}  // namespace simpleperf