• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "src/profiling/memory/unwinding.h"
18 
19 #include <sys/types.h>
20 #include <unistd.h>
21 
22 #include <unwindstack/MachineArm.h>
23 #include <unwindstack/MachineArm64.h>
24 #include <unwindstack/MachineMips.h>
25 #include <unwindstack/MachineMips64.h>
26 #include <unwindstack/MachineX86.h>
27 #include <unwindstack/MachineX86_64.h>
28 #include <unwindstack/Maps.h>
29 #include <unwindstack/Memory.h>
30 #include <unwindstack/Regs.h>
31 #include <unwindstack/RegsArm.h>
32 #include <unwindstack/RegsArm64.h>
33 #include <unwindstack/RegsMips.h>
34 #include <unwindstack/RegsMips64.h>
35 #include <unwindstack/RegsX86.h>
36 #include <unwindstack/RegsX86_64.h>
37 #include <unwindstack/Unwinder.h>
38 #include <unwindstack/UserArm.h>
39 #include <unwindstack/UserArm64.h>
40 #include <unwindstack/UserMips.h>
41 #include <unwindstack/UserMips64.h>
42 #include <unwindstack/UserX86.h>
43 #include <unwindstack/UserX86_64.h>
44 
45 #include <procinfo/process_map.h>
46 
47 #include "perfetto/base/logging.h"
48 #include "perfetto/base/task_runner.h"
49 #include "perfetto/ext/base/file_utils.h"
50 #include "perfetto/ext/base/scoped_file.h"
51 #include "perfetto/ext/base/string_utils.h"
52 #include "perfetto/ext/base/thread_task_runner.h"
53 
54 #include "src/profiling/memory/wire_protocol.h"
55 
56 namespace perfetto {
57 namespace profiling {
58 namespace {
59 
60 constexpr base::TimeMillis kMapsReparseInterval{500};
61 
62 constexpr size_t kMaxFrames = 1000;
63 
64 // We assume average ~300us per unwind. If we handle up to 1000 unwinds, this
65 // makes sure other tasks get to be run at least every 300ms if the unwinding
66 // saturates this thread.
67 constexpr size_t kUnwindBatchSize = 1000;
68 
69 #pragma GCC diagnostic push
70 // We do not care about deterministic destructor order.
71 #pragma GCC diagnostic ignored "-Wglobal-constructors"
72 #pragma GCC diagnostic ignored "-Wexit-time-destructors"
73 static std::vector<std::string> kSkipMaps{"heapprofd_client.so"};
74 #pragma GCC diagnostic pop
75 
GetRegsSize(unwindstack::Regs * regs)76 size_t GetRegsSize(unwindstack::Regs* regs) {
77   if (regs->Is32Bit())
78     return sizeof(uint32_t) * regs->total_regs();
79   return sizeof(uint64_t) * regs->total_regs();
80 }
81 
ReadFromRawData(unwindstack::Regs * regs,void * raw_data)82 void ReadFromRawData(unwindstack::Regs* regs, void* raw_data) {
83   memcpy(regs->RawData(), raw_data, GetRegsSize(regs));
84 }
85 
86 }  // namespace
87 
CreateRegsFromRawData(unwindstack::ArchEnum arch,void * raw_data)88 std::unique_ptr<unwindstack::Regs> CreateRegsFromRawData(
89     unwindstack::ArchEnum arch,
90     void* raw_data) {
91   std::unique_ptr<unwindstack::Regs> ret;
92   switch (arch) {
93     case unwindstack::ARCH_X86:
94       ret.reset(new unwindstack::RegsX86());
95       break;
96     case unwindstack::ARCH_X86_64:
97       ret.reset(new unwindstack::RegsX86_64());
98       break;
99     case unwindstack::ARCH_ARM:
100       ret.reset(new unwindstack::RegsArm());
101       break;
102     case unwindstack::ARCH_ARM64:
103       ret.reset(new unwindstack::RegsArm64());
104       break;
105     case unwindstack::ARCH_MIPS:
106       ret.reset(new unwindstack::RegsMips());
107       break;
108     case unwindstack::ARCH_MIPS64:
109       ret.reset(new unwindstack::RegsMips64());
110       break;
111     case unwindstack::ARCH_UNKNOWN:
112       break;
113   }
114   if (ret)
115     ReadFromRawData(ret.get(), raw_data);
116   return ret;
117 }
118 
DoUnwind(WireMessage * msg,UnwindingMetadata * metadata,AllocRecord * out)119 bool DoUnwind(WireMessage* msg, UnwindingMetadata* metadata, AllocRecord* out) {
120   AllocMetadata* alloc_metadata = msg->alloc_header;
121   std::unique_ptr<unwindstack::Regs> regs(CreateRegsFromRawData(
122       alloc_metadata->arch, alloc_metadata->register_data));
123   if (regs == nullptr) {
124     PERFETTO_DLOG("Unable to construct unwindstack::Regs");
125     unwindstack::FrameData frame_data{};
126     frame_data.function_name = "ERROR READING REGISTERS";
127     frame_data.map_name = "ERROR";
128 
129     out->frames.emplace_back(frame_data, "");
130     out->error = true;
131     return false;
132   }
133   uint8_t* stack = reinterpret_cast<uint8_t*>(msg->payload);
134   std::shared_ptr<unwindstack::Memory> mems =
135       std::make_shared<StackOverlayMemory>(metadata->fd_mem,
136                                            alloc_metadata->stack_pointer, stack,
137                                            msg->payload_size);
138 
139   unwindstack::Unwinder unwinder(kMaxFrames, &metadata->fd_maps, regs.get(),
140                                  mems);
141 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
142   unwinder.SetJitDebug(metadata->jit_debug.get(), regs->Arch());
143   unwinder.SetDexFiles(metadata->dex_files.get(), regs->Arch());
144 #endif
145   // Suppress incorrect "variable may be uninitialized" error for if condition
146   // after this loop. error_code = LastErrorCode gets run at least once.
147   unwindstack::ErrorCode error_code = unwindstack::ERROR_NONE;
148   for (int attempt = 0; attempt < 2; ++attempt) {
149     if (attempt > 0) {
150       if (metadata->last_maps_reparse_time + kMapsReparseInterval >
151           base::GetWallTimeMs()) {
152         PERFETTO_DLOG("Skipping reparse due to rate limit.");
153         break;
154       }
155       PERFETTO_DLOG("Reparsing maps");
156       metadata->ReparseMaps();
157       metadata->last_maps_reparse_time = base::GetWallTimeMs();
158       // Regs got invalidated by libuwindstack's speculative jump.
159       // Reset.
160       ReadFromRawData(regs.get(), alloc_metadata->register_data);
161       out->reparsed_map = true;
162 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
163       unwinder.SetJitDebug(metadata->jit_debug.get(), regs->Arch());
164       unwinder.SetDexFiles(metadata->dex_files.get(), regs->Arch());
165 #endif
166     }
167     unwinder.Unwind(&kSkipMaps, /*map_suffixes_to_ignore=*/nullptr);
168     error_code = unwinder.LastErrorCode();
169     if (error_code != unwindstack::ERROR_INVALID_MAP)
170       break;
171   }
172   std::vector<unwindstack::FrameData> frames = unwinder.ConsumeFrames();
173   for (unwindstack::FrameData& fd : frames) {
174     out->frames.emplace_back(metadata->AnnotateFrame(std::move(fd)));
175   }
176 
177   if (error_code != unwindstack::ERROR_NONE) {
178     PERFETTO_DLOG("Unwinding error %" PRIu8, error_code);
179     unwindstack::FrameData frame_data{};
180     frame_data.function_name =
181         "ERROR " + StringifyLibUnwindstackError(error_code);
182     frame_data.map_name = "ERROR";
183 
184     out->frames.emplace_back(std::move(frame_data), "");
185     out->error = true;
186   }
187   return true;
188 }
189 
OnDisconnect(base::UnixSocket * self)190 void UnwindingWorker::OnDisconnect(base::UnixSocket* self) {
191   // TODO(fmayer): Maybe try to drain shmem one last time.
192   auto it = client_data_.find(self->peer_pid());
193   if (it == client_data_.end()) {
194     PERFETTO_DFATAL_OR_ELOG("Disconnected unexpected socket.");
195     return;
196   }
197   ClientData& client_data = it->second;
198   SharedRingBuffer& shmem = client_data.shmem;
199 
200   SharedRingBuffer::Stats stats = {};
201   {
202     auto lock = shmem.AcquireLock(ScopedSpinlock::Mode::Try);
203     if (lock.locked())
204       stats = shmem.GetStats(lock);
205     else
206       PERFETTO_ELOG("Failed to log shmem to get stats.");
207   }
208   DataSourceInstanceID ds_id = client_data.data_source_instance_id;
209   pid_t peer_pid = self->peer_pid();
210   client_data_.erase(it);
211   // The erase invalidates the self pointer.
212   self = nullptr;
213   delegate_->PostSocketDisconnected(ds_id, peer_pid, stats);
214 }
215 
OnDataAvailable(base::UnixSocket * self)216 void UnwindingWorker::OnDataAvailable(base::UnixSocket* self) {
217   // Drain buffer to clear the notification.
218   char recv_buf[kUnwindBatchSize];
219   self->Receive(recv_buf, sizeof(recv_buf));
220   HandleUnwindBatch(self->peer_pid());
221 }
222 
HandleUnwindBatch(pid_t peer_pid)223 void UnwindingWorker::HandleUnwindBatch(pid_t peer_pid) {
224   auto it = client_data_.find(peer_pid);
225   if (it == client_data_.end()) {
226     // This can happen if the client disconnected before the buffer was fully
227     // handled.
228     PERFETTO_DLOG("Unexpected data.");
229     return;
230   }
231 
232   ClientData& client_data = it->second;
233   SharedRingBuffer& shmem = client_data.shmem;
234   SharedRingBuffer::Buffer buf;
235 
236   size_t i;
237   bool repost_task = false;
238   for (i = 0; i < kUnwindBatchSize; ++i) {
239     uint64_t reparses_before = client_data.metadata.reparses;
240     buf = shmem.BeginRead();
241     if (!buf)
242       break;
243     HandleBuffer(buf, &client_data.metadata,
244                  client_data.data_source_instance_id,
245                  client_data.sock->peer_pid(), delegate_);
246     shmem.EndRead(std::move(buf));
247     // Reparsing takes time, so process the rest in a new batch to avoid timing
248     // out.
249     if (reparses_before < client_data.metadata.reparses) {
250       repost_task = true;
251       break;
252     }
253   }
254 
255   // Always repost if we have gone through the whole batch.
256   if (i == kUnwindBatchSize)
257     repost_task = true;
258 
259   if (repost_task) {
260     thread_task_runner_.get()->PostTask(
261         [this, peer_pid] { HandleUnwindBatch(peer_pid); });
262   }
263 }
264 
265 // static
HandleBuffer(const SharedRingBuffer::Buffer & buf,UnwindingMetadata * unwinding_metadata,DataSourceInstanceID data_source_instance_id,pid_t peer_pid,Delegate * delegate)266 void UnwindingWorker::HandleBuffer(const SharedRingBuffer::Buffer& buf,
267                                    UnwindingMetadata* unwinding_metadata,
268                                    DataSourceInstanceID data_source_instance_id,
269                                    pid_t peer_pid,
270                                    Delegate* delegate) {
271   WireMessage msg;
272   // TODO(fmayer): standardise on char* or uint8_t*.
273   // char* has stronger guarantees regarding aliasing.
274   // see https://timsong-cpp.github.io/cppwp/n3337/basic.lval#10.8
275   if (!ReceiveWireMessage(reinterpret_cast<char*>(buf.data), buf.size, &msg)) {
276     PERFETTO_DFATAL_OR_ELOG("Failed to receive wire message.");
277     return;
278   }
279 
280   if (msg.record_type == RecordType::Malloc) {
281     AllocRecord rec;
282     rec.alloc_metadata = *msg.alloc_header;
283     rec.pid = peer_pid;
284     rec.data_source_instance_id = data_source_instance_id;
285     auto start_time_us = base::GetWallTimeNs() / 1000;
286     DoUnwind(&msg, unwinding_metadata, &rec);
287     rec.unwinding_time_us = static_cast<uint64_t>(
288         ((base::GetWallTimeNs() / 1000) - start_time_us).count());
289     delegate->PostAllocRecord(std::move(rec));
290   } else if (msg.record_type == RecordType::Free) {
291     FreeRecord rec;
292     rec.pid = peer_pid;
293     rec.data_source_instance_id = data_source_instance_id;
294     // We need to copy this, so we can return the memory to the shmem buffer.
295     memcpy(&rec.free_batch, msg.free_header, sizeof(*msg.free_header));
296     delegate->PostFreeRecord(std::move(rec));
297   } else {
298     PERFETTO_DFATAL_OR_ELOG("Invalid record type.");
299   }
300 }
301 
PostHandoffSocket(HandoffData handoff_data)302 void UnwindingWorker::PostHandoffSocket(HandoffData handoff_data) {
303   // Even with C++14, this cannot be moved, as std::function has to be
304   // copyable, which HandoffData is not.
305   HandoffData* raw_data = new HandoffData(std::move(handoff_data));
306   // We do not need to use a WeakPtr here because the task runner will not
307   // outlive its UnwindingWorker.
308   thread_task_runner_.get()->PostTask([this, raw_data] {
309     HandoffData data = std::move(*raw_data);
310     delete raw_data;
311     HandleHandoffSocket(std::move(data));
312   });
313 }
314 
HandleHandoffSocket(HandoffData handoff_data)315 void UnwindingWorker::HandleHandoffSocket(HandoffData handoff_data) {
316   auto sock = base::UnixSocket::AdoptConnected(
317       handoff_data.sock.ReleaseFd(), this, this->thread_task_runner_.get(),
318       base::SockFamily::kUnix, base::SockType::kStream);
319   pid_t peer_pid = sock->peer_pid();
320 
321   UnwindingMetadata metadata(std::move(handoff_data.maps_fd),
322                              std::move(handoff_data.mem_fd));
323   ClientData client_data{
324       handoff_data.data_source_instance_id,
325       std::move(sock),
326       std::move(metadata),
327       std::move(handoff_data.shmem),
328       std::move(handoff_data.client_config),
329   };
330   client_data_.emplace(peer_pid, std::move(client_data));
331 }
332 
PostDisconnectSocket(pid_t pid)333 void UnwindingWorker::PostDisconnectSocket(pid_t pid) {
334   // We do not need to use a WeakPtr here because the task runner will not
335   // outlive its UnwindingWorker.
336   thread_task_runner_.get()->PostTask(
337       [this, pid] { HandleDisconnectSocket(pid); });
338 }
339 
HandleDisconnectSocket(pid_t pid)340 void UnwindingWorker::HandleDisconnectSocket(pid_t pid) {
341   auto it = client_data_.find(pid);
342   if (it == client_data_.end()) {
343     PERFETTO_DFATAL_OR_ELOG("Trying to disconnect unknown socket.");
344     return;
345   }
346   ClientData& client_data = it->second;
347   // Shutdown and call OnDisconnect handler.
348   client_data.sock->Shutdown(/* notify= */ true);
349 }
350 
351 UnwindingWorker::Delegate::~Delegate() = default;
352 
353 }  // namespace profiling
354 }  // namespace perfetto
355