* Copyright (C) 2013 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.
*/
#ifndef _BACKTRACE_BACKTRACE_H
#define _BACKTRACE_BACKTRACE_H
#include <inttypes.h>
#include <stdint.h>
#include <string>
#include <vector>
#include <backtrace/backtrace_constants.h>
#include <backtrace/BacktraceMap.h>
#if defined(__LP64__)
#define PRIPTR "016" PRIx64
typedef uint64_t word_t;
#else
#define PRIPTR "08" PRIx64
typedef uint32_t word_t;
#endif
enum BacktraceUnwindErrorCode : uint32_t {
BACKTRACE_UNWIND_NO_ERROR,
BACKTRACE_UNWIND_ERROR_SETUP_FAILED,
BACKTRACE_UNWIND_ERROR_MAP_MISSING,
BACKTRACE_UNWIND_ERROR_INTERNAL,
BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST,
BACKTRACE_UNWIND_ERROR_THREAD_TIMEOUT,
BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION,
BACKTRACE_UNWIND_ERROR_NO_CONTEXT,
BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT,
BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED,
BACKTRACE_UNWIND_ERROR_ACCESS_REG_FAILED,
BACKTRACE_UNWIND_ERROR_FIND_PROC_INFO_FAILED,
BACKTRACE_UNWIND_ERROR_EXECUTE_DWARF_INSTRUCTION_FAILED,
BACKTRACE_UNWIND_ERROR_UNWIND_INFO,
BACKTRACE_UNWIND_ERROR_REPEATED_FRAME,
BACKTRACE_UNWIND_ERROR_INVALID_ELF,
};
struct BacktraceUnwindError {
enum BacktraceUnwindErrorCode error_code;
union {
uint64_t addr;
uint64_t regno;
} error_info;
BacktraceUnwindError() : error_code(BACKTRACE_UNWIND_NO_ERROR) {}
};
struct backtrace_frame_data_t {
size_t num;
uint64_t pc;
uint64_t rel_pc;
uint64_t sp;
size_t stack_size;
backtrace_map_t map;
std::string func_name;
uint64_t func_offset;
};
struct backtrace_stackinfo_t {
uint64_t start;
uint64_t end;
const uint8_t* data;
};
namespace unwindstack {
class Regs;
}
class Backtrace {
public:
enum ArchEnum : uint8_t {
ARCH_ARM,
ARCH_ARM64,
ARCH_X86,
ARCH_X86_64,
};
static void SetGlobalElfCache(bool enable);
static Backtrace* Create(pid_t pid, pid_t tid, BacktraceMap* map = nullptr);
virtual ~Backtrace();
virtual bool Unwind(size_t num_ignore_frames, void* context = nullptr) = 0;
static bool Unwind(unwindstack::Regs* regs, BacktraceMap* back_map,
std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames,
std::vector<std::string>* skip_names, BacktraceUnwindError* error = nullptr);
virtual std::string GetFunctionName(uint64_t pc, uint64_t* offset,
const backtrace_map_t* map = nullptr);
virtual void FillInMap(uint64_t pc, backtrace_map_t* map);
virtual bool ReadWord(uint64_t ptr, word_t* out_value) = 0;
virtual size_t Read(uint64_t addr, uint8_t* buffer, size_t bytes) = 0;
virtual std::string FormatFrameData(size_t frame_num);
static std::string FormatFrameData(const backtrace_frame_data_t* frame);
pid_t Pid() const { return pid_; }
pid_t Tid() const { return tid_; }
size_t NumFrames() const { return frames_.size(); }
const backtrace_frame_data_t* GetFrame(size_t frame_num) {
if (frame_num >= frames_.size()) {
return nullptr;
}
return &frames_[frame_num];
}
typedef std::vector<backtrace_frame_data_t>::iterator iterator;
iterator begin() { return frames_.begin(); }
iterator end() { return frames_.end(); }
typedef std::vector<backtrace_frame_data_t>::const_iterator const_iterator;
const_iterator begin() const { return frames_.begin(); }
const_iterator end() const { return frames_.end(); }
BacktraceMap* GetMap() { return map_; }
BacktraceUnwindError GetError() { return error_; }
std::string GetErrorString(BacktraceUnwindError error);
void SetSkipFrames(bool skip_frames) { skip_frames_ = skip_frames; }
protected:
Backtrace(pid_t pid, pid_t tid, BacktraceMap* map);
virtual std::string GetFunctionNameRaw(uint64_t pc, uint64_t* offset) = 0;
virtual bool VerifyReadWordArgs(uint64_t ptr, word_t* out_value);
bool BuildMap();
pid_t pid_;
pid_t tid_;
BacktraceMap* map_;
bool map_shared_;
std::vector<backtrace_frame_data_t> frames_;
bool skip_frames_ = true;
BacktraceUnwindError error_;
};
#endif