chromium/base/allocator/partition_allocator/src/partition_alloc/partition_alloc_base/debug/stack_trace_posix.cc

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "partition_alloc/partition_alloc_base/debug/stack_trace.h"

#include <fcntl.h>
#include <unistd.h>

#include <cstring>

#include "partition_alloc/partition_alloc_base/logging.h"
#include "partition_alloc/partition_alloc_base/posix/eintr_wrapper.h"
#include "partition_alloc/partition_alloc_base/strings/safe_sprintf.h"

#if !PA_BUILDFLAG(IS_ANDROID) && !PA_BUILDFLAG(IS_APPLE)
#include <link.h>  // For ElfW() macro.
#endif

#if PA_BUILDFLAG(IS_APPLE)
#include <dlfcn.h>
#endif

namespace partition_alloc::internal::base::debug {

namespace {

#if !PA_BUILDFLAG(IS_APPLE)

// On Android the 'open' function has two versions:
// int open(const char *pathname, int flags);
// int open(const char *pathname, int flags, mode_t mode);
//
// This doesn't play well with WrapEINTR template. This alias helps the compiler
// to make a decision.
int OpenFile(const char* pathname, int flags) {}

constexpr size_t kBufferSize =;

enum {};

bool ParseAddress(const char** ptr,
                  const char* end,
                  uintptr_t* address_return) {}

bool ParseInteger(const char** ptr, const char* end) {}

bool ParsePermissions(const char** ptr,
                      const char* end,
                      unsigned* permission_return) {}

bool ParseMapsLine(const char* line_start,
                   const char* line_end,
                   uintptr_t* start_address_return,
                   uintptr_t* end_address_return,
                   unsigned* permission_return,
                   uintptr_t* offset_return,
                   const char** module_name) {}

#if !PA_BUILDFLAG(IS_ANDROID)

ssize_t ReadFromOffset(const int fd,
                       void* buf,
                       const size_t count,
                       const size_t offset) {}

void UpdateBaseAddress(unsigned permissions,
                       uintptr_t start_address,
                       uintptr_t* base_address) {}

#endif  // !PA_BUILDFLAG(IS_ANDROID)

void PrintStackTraceInternal(const void** trace, size_t count) {}
#endif  // !PA_BUILDFLAG(IS_APPLE)

#if PA_BUILDFLAG(IS_APPLE)
// Since /proc/self/maps is not available, use dladdr() to obtain module
// names and offsets inside the modules from the given addresses.
void PrintStackTraceInternal(const void* const* trace, size_t size) {
  // NOTE: This code MUST be async-signal safe (it's used by in-process
  // stack dumping signal handler). NO malloc or stdio is allowed here.

  Dl_info dl_info;
  for (size_t i = 0; i < size; ++i) {
    const bool dl_info_found = dladdr(trace[i], &dl_info) != 0;
    if (dl_info_found) {
      const char* last_sep = strrchr(dl_info.dli_fname, '/');
      const char* basename = last_sep ? last_sep + 1 : dl_info.dli_fname;

      // Use atos with --offset to obtain symbols from the printed addresses,
      // e.g.
      //  #01 0x0000000106225d6c  (base_unittests+0x0000000001999d6c)
      //  bash-3.2$ atos -o out/default/base_unittests --offset
      //   0x0000000001999d6c
      //  partition_alloc::internal::PartitionAllocTest_Basic_Test::TestBody()
      //  (in base_unittests) + 156
      OutputStackTrace(i, reinterpret_cast<uintptr_t>(trace[i]),
                       reinterpret_cast<uintptr_t>(dl_info.dli_fbase), basename,
                       0u);
    } else {
      OutputStackTrace(i, reinterpret_cast<uintptr_t>(trace[i]), 0u, "???", 0u);
    }
  }
}
#endif  // PA_BUILDFLAG(IS_APPLE)

}  // namespace

void PrintStackTrace(const void** trace, size_t count) {}

// stack_trace_android.cc defines its own OutputStackTrace.
#if !PA_BUILDFLAG(IS_ANDROID)
void OutputStackTrace(unsigned index,
                      uintptr_t address,
                      uintptr_t base_address,
                      const char* module_name,
                      uintptr_t) {}
#endif  // !PA_BUILDFLAG(IS_ANDROID)

}  // namespace partition_alloc::internal::base::debug