Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6b95a0e9 authored by Casey Dahlin's avatar Casey Dahlin Committed by Christopher Ferris
Browse files

Internalize subclasses of Memory

There are many subclasses of the Memory class and the overwhelming
majority of them don't need to be exposed externally. We move all of
them to internal headers except MemoryOfflineBuffer, which moves to a
separate header. This dramatically reduces the exposed API surface and
makes the code more modular.

Also, remove the Offline code from libbacktrace. It's not used any where.

Test: Unit tests pass, clean tree still builds
Change-Id: I55dacdf080daba0bfe65c1ad53a4b326bb482e83
parent 1d7f3b4f
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -135,7 +135,6 @@ cc_test {
    defaults: ["libbacktrace_common"],
    host_supported: true,
    srcs: [
        "backtrace_offline_test.cpp",
        "backtrace_test.cpp",
    ],

+2 −86
Original line number Diff line number Diff line
@@ -129,22 +129,6 @@ bool Backtrace::Unwind(unwindstack::Regs* regs, BacktraceMap* back_map,
  return true;
}

bool Backtrace::UnwindOffline(unwindstack::Regs* regs, BacktraceMap* back_map,
                              const backtrace_stackinfo_t& stack,
                              std::vector<backtrace_frame_data_t>* frames,
                              BacktraceUnwindError* error) {
  UnwindStackOfflineMap* offline_map = reinterpret_cast<UnwindStackOfflineMap*>(back_map);
  // Create the process memory from the stack data since this will almost
  // always be different each unwind.
  if (!offline_map->CreateProcessMemory(stack)) {
    if (error != nullptr) {
      error->error_code = BACKTRACE_UNWIND_ERROR_SETUP_FAILED;
    }
    return false;
  }
  return Backtrace::Unwind(regs, back_map, frames, 0U, nullptr, error);
}

UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map)
    : BacktraceCurrent(pid, tid, map) {}

@@ -171,7 +155,7 @@ bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, void* ucont
}

UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
    : BacktracePtrace(pid, tid, map), memory_(pid) {}
    : BacktracePtrace(pid, tid, map), memory_(unwindstack::Memory::CreateProcessMemory(pid)) {}

std::string UnwindStackPtrace::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
  return GetMap()->GetFunctionName(pc, offset);
@@ -189,73 +173,5 @@ bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, void* context) {
}

size_t UnwindStackPtrace::Read(uint64_t addr, uint8_t* buffer, size_t bytes) {
  return memory_.Read(addr, buffer, bytes);
}

UnwindStackOffline::UnwindStackOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map,
                                       bool map_shared)
    : Backtrace(pid, tid, map), arch_(arch) {
  map_shared_ = map_shared;
}

bool UnwindStackOffline::Unwind(size_t num_ignore_frames, void* ucontext) {
  if (ucontext == nullptr) {
    return false;
  }

  unwindstack::ArchEnum arch;
  switch (arch_) {
    case ARCH_ARM:
      arch = unwindstack::ARCH_ARM;
      break;
    case ARCH_ARM64:
      arch = unwindstack::ARCH_ARM64;
      break;
    case ARCH_X86:
      arch = unwindstack::ARCH_X86;
      break;
    case ARCH_X86_64:
      arch = unwindstack::ARCH_X86_64;
      break;
    default:
      return false;
  }

  std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromUcontext(arch, ucontext));

  return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_);
}

std::string UnwindStackOffline::GetFunctionNameRaw(uint64_t, uint64_t*) {
  return "";
}

size_t UnwindStackOffline::Read(uint64_t, uint8_t*, size_t) {
  return 0;
}

bool UnwindStackOffline::ReadWord(uint64_t, word_t*) {
  return false;
}

Backtrace* Backtrace::CreateOffline(ArchEnum arch, pid_t pid, pid_t tid,
                                    const std::vector<backtrace_map_t>& maps,
                                    const backtrace_stackinfo_t& stack) {
  std::unique_ptr<UnwindStackOfflineMap> map(
      reinterpret_cast<UnwindStackOfflineMap*>(BacktraceMap::CreateOffline(pid, maps)));
  if (map.get() == nullptr || !map->CreateProcessMemory(stack)) {
    return nullptr;
  }
  return new UnwindStackOffline(arch, pid, tid, map.release(), false);
}

Backtrace* Backtrace::CreateOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map) {
  if (map == nullptr) {
    return nullptr;
  }
  return new UnwindStackOffline(arch, pid, tid, map, true);
}

void Backtrace::SetGlobalElfCache(bool enable) {
  unwindstack::Elf::SetCachingEnabled(enable);
  return memory_->Read(addr, buffer, bytes);
}
+2 −17
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@

#include <stdint.h>

#include <memory>
#include <string>

#include <backtrace/BacktraceMap.h>
@@ -49,23 +50,7 @@ class UnwindStackPtrace : public BacktracePtrace {
  size_t Read(uint64_t addr, uint8_t* buffer, size_t bytes) override;

 private:
  unwindstack::MemoryRemote memory_;
};

class UnwindStackOffline : public Backtrace {
 public:
  UnwindStackOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map, bool map_shared);

  bool Unwind(size_t num_ignore_frames, void* context) override;

  std::string GetFunctionNameRaw(uint64_t pc, uint64_t* offset) override;

  size_t Read(uint64_t addr, uint8_t* buffer, size_t bytes) override;

  bool ReadWord(uint64_t ptr, word_t* out_value) override;

 private:
  ArchEnum arch_;
  std::shared_ptr<unwindstack::Memory> memory_;
};

#endif  // _LIBBACKTRACE_UNWIND_STACK_H
+0 −49
Original line number Diff line number Diff line
@@ -132,43 +132,6 @@ std::shared_ptr<unwindstack::Memory> UnwindStackMap::GetProcessMemory() {
  return process_memory_;
}

UnwindStackOfflineMap::UnwindStackOfflineMap(pid_t pid) : UnwindStackMap(pid) {}

bool UnwindStackOfflineMap::Build() {
  return false;
}

bool UnwindStackOfflineMap::Build(const std::vector<backtrace_map_t>& backtrace_maps) {
  for (const backtrace_map_t& map : backtrace_maps) {
    maps_.push_back(map);
  }

  std::sort(maps_.begin(), maps_.end(),
            [](const backtrace_map_t& a, const backtrace_map_t& b) { return a.start < b.start; });

  unwindstack::Maps* maps = new unwindstack::Maps;
  stack_maps_.reset(maps);
  for (const backtrace_map_t& map : maps_) {
    maps->Add(map.start, map.end, map.offset, map.flags, map.name, map.load_bias);
  }
  return true;
}

bool UnwindStackOfflineMap::CreateProcessMemory(const backtrace_stackinfo_t& stack) {
  if (stack.start >= stack.end) {
    return false;
  }

  // Create the process memory from the stack data.
  if (memory_ == nullptr) {
    memory_ = new unwindstack::MemoryOfflineBuffer(stack.data, stack.start, stack.end);
    process_memory_.reset(memory_);
  } else {
    memory_->Reset(stack.data, stack.start, stack.end);
  }
  return true;
}

//-------------------------------------------------------------------------
// BacktraceMap create function.
//-------------------------------------------------------------------------
@@ -189,15 +152,3 @@ BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) {
  }
  return map;
}

//-------------------------------------------------------------------------
// BacktraceMap create offline function.
//-------------------------------------------------------------------------
BacktraceMap* BacktraceMap::CreateOffline(pid_t pid, const std::vector<backtrace_map_t>& maps) {
  UnwindStackOfflineMap* map = new UnwindStackOfflineMap(pid);
  if (!map->Build(maps)) {
    delete map;
    return nullptr;
  }
  return map;
}
+1 −15
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@
#include <unwindstack/Elf.h>
#include <unwindstack/JitDebug.h>
#include <unwindstack/Maps.h>
#include <unwindstack/Memory.h>

// Forward declarations.
class UnwindDexFile;
@@ -74,19 +75,4 @@ class UnwindStackMap : public BacktraceMap {
  unwindstack::ArchEnum arch_ = unwindstack::ARCH_UNKNOWN;
};

class UnwindStackOfflineMap : public UnwindStackMap {
 public:
  UnwindStackOfflineMap(pid_t pid);
  ~UnwindStackOfflineMap() = default;

  bool Build() override;

  bool Build(const std::vector<backtrace_map_t>& maps);

  bool CreateProcessMemory(const backtrace_stackinfo_t& stack);

 private:
  unwindstack::MemoryOfflineBuffer* memory_ = nullptr;
};

#endif  // _LIBBACKTRACE_UNWINDSTACK_MAP_H
Loading