blob: bd8064da7068ccaa88f1102809074d77792b96aa [file] [log] [blame]
Ryan Savitski33fd8b72020-02-04 15:06:15 +00001/*
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#ifndef SRC_PROFILING_COMMON_UNWIND_SUPPORT_H_
18#define SRC_PROFILING_COMMON_UNWIND_SUPPORT_H_
19
20// defines PERFETTO_BUILDFLAG
21#include "perfetto/base/build_config.h"
22
Ryan Savitskifdeb11f2020-02-05 16:53:45 +000023#include <memory>
24#include <string>
25
Ryan Savitski33fd8b72020-02-04 15:06:15 +000026#include <unwindstack/Maps.h>
27#include <unwindstack/Unwinder.h>
28#if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
29#include <unwindstack/DexFiles.h>
30#include <unwindstack/JitDebug.h>
31#endif
32
33#include "perfetto/base/logging.h"
34#include "perfetto/base/time.h"
35#include "perfetto/ext/base/scoped_file.h"
36
37namespace perfetto {
38namespace profiling {
39
40// Read /proc/[pid]/maps from an open file descriptor.
41// TODO(fmayer): Figure out deduplication to other maps.
Ryan Savitski3686d292020-02-04 16:44:56 +000042class FDMaps : public unwindstack::Maps {
Ryan Savitski33fd8b72020-02-04 15:06:15 +000043 public:
Florian Mayer303f9482020-08-19 15:34:31 +010044 explicit FDMaps(base::ScopedFile fd);
Ryan Savitski33fd8b72020-02-04 15:06:15 +000045
Ryan Savitski3686d292020-02-04 16:44:56 +000046 FDMaps(const FDMaps&) = delete;
47 FDMaps& operator=(const FDMaps&) = delete;
Ryan Savitski33fd8b72020-02-04 15:06:15 +000048
Ryan Savitski3686d292020-02-04 16:44:56 +000049 FDMaps(FDMaps&& m) : Maps(std::move(m)) { fd_ = std::move(m.fd_); }
Ryan Savitski33fd8b72020-02-04 15:06:15 +000050
Ryan Savitski3686d292020-02-04 16:44:56 +000051 FDMaps& operator=(FDMaps&& m) {
Ryan Savitski33fd8b72020-02-04 15:06:15 +000052 if (&m != this)
53 fd_ = std::move(m.fd_);
54 Maps::operator=(std::move(m));
55 return *this;
56 }
57
Ryan Savitski3686d292020-02-04 16:44:56 +000058 virtual ~FDMaps() override = default;
Ryan Savitski33fd8b72020-02-04 15:06:15 +000059
60 bool Parse() override;
61 void Reset();
62
63 private:
64 base::ScopedFile fd_;
65};
66
67class FDMemory : public unwindstack::Memory {
68 public:
Florian Mayer303f9482020-08-19 15:34:31 +010069 explicit FDMemory(base::ScopedFile mem_fd);
Ryan Savitski33fd8b72020-02-04 15:06:15 +000070 size_t Read(uint64_t addr, void* dst, size_t size) override;
71
72 private:
73 base::ScopedFile mem_fd_;
74};
75
76// Overlays size bytes pointed to by stack for addresses in [sp, sp + size).
77// Addresses outside of that range are read from mem_fd, which should be an fd
78// that opened /proc/[pid]/mem.
79class StackOverlayMemory : public unwindstack::Memory {
80 public:
81 StackOverlayMemory(std::shared_ptr<unwindstack::Memory> mem,
82 uint64_t sp,
Ryan Savitskiccd89612020-03-09 18:31:47 +000083 const uint8_t* stack,
Ryan Savitski33fd8b72020-02-04 15:06:15 +000084 size_t size);
85 size_t Read(uint64_t addr, void* dst, size_t size) override;
86
87 private:
88 std::shared_ptr<unwindstack::Memory> mem_;
Ryan Savitskiccd89612020-03-09 18:31:47 +000089 const uint64_t sp_;
90 const uint64_t stack_end_;
91 const uint8_t* const stack_;
Ryan Savitski33fd8b72020-02-04 15:06:15 +000092};
93
94struct UnwindingMetadata {
Ryan Savitskifdeb11f2020-02-05 16:53:45 +000095 UnwindingMetadata(base::ScopedFile maps_fd, base::ScopedFile mem_fd);
96
97 // move-only
98 UnwindingMetadata(const UnwindingMetadata&) = delete;
99 UnwindingMetadata& operator=(const UnwindingMetadata&) = delete;
100
101 UnwindingMetadata(UnwindingMetadata&&) = default;
102 UnwindingMetadata& operator=(UnwindingMetadata&&) = default;
103
104 void ReparseMaps();
105
Florian Mayerb64ba5d2020-12-04 16:09:09 +0000106 const std::string& GetBuildId(const unwindstack::FrameData& frame);
Ryan Savitskifdeb11f2020-02-05 16:53:45 +0000107
Florian Mayerb64ba5d2020-12-04 16:09:09 +0000108 std::string empty_string_;
Ryan Savitski3686d292020-02-04 16:44:56 +0000109 FDMaps fd_maps;
Ryan Savitski33fd8b72020-02-04 15:06:15 +0000110 // The API of libunwindstack expects shared_ptr for Memory.
111 std::shared_ptr<unwindstack::Memory> fd_mem;
112 uint64_t reparses = 0;
113 base::TimeMillis last_maps_reparse_time{0};
114#if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
115 std::unique_ptr<unwindstack::JitDebug> jit_debug;
116 std::unique_ptr<unwindstack::DexFiles> dex_files;
117#endif
118};
119
Florian Mayerecee1b42020-02-11 13:59:36 +0000120std::string StringifyLibUnwindstackError(unwindstack::ErrorCode);
121
Ryan Savitski33fd8b72020-02-04 15:06:15 +0000122} // namespace profiling
123} // namespace perfetto
124
125#endif // SRC_PROFILING_COMMON_UNWIND_SUPPORT_H_