Path: blob/21.2-virgl/include/android_stub/backtrace/Backtrace.h
7086 views
/*1* Copyright (C) 2013 The Android Open Source Project2*3* Licensed under the Apache License, Version 2.0 (the "License");4* you may not use this file except in compliance with the License.5* You may obtain a copy of the License at6*7* http://www.apache.org/licenses/LICENSE-2.08*9* Unless required by applicable law or agreed to in writing, software10* distributed under the License is distributed on an "AS IS" BASIS,11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12* See the License for the specific language governing permissions and13* limitations under the License.14*/1516#ifndef _BACKTRACE_BACKTRACE_H17#define _BACKTRACE_BACKTRACE_H1819#include <inttypes.h>20#include <stdint.h>2122#include <string>23#include <vector>2425#include <backtrace/backtrace_constants.h>26#include <backtrace/BacktraceMap.h>2728#if defined(__LP64__)29#define PRIPTR "016" PRIx6430typedef uint64_t word_t;31#else32#define PRIPTR "08" PRIx6433typedef uint32_t word_t;34#endif3536enum BacktraceUnwindErrorCode : uint32_t {37BACKTRACE_UNWIND_NO_ERROR,38// Something failed while trying to perform the setup to begin the unwind.39BACKTRACE_UNWIND_ERROR_SETUP_FAILED,40// There is no map information to use with the unwind.41BACKTRACE_UNWIND_ERROR_MAP_MISSING,42// An error occurred that indicates a programming error.43BACKTRACE_UNWIND_ERROR_INTERNAL,44// The thread to unwind has disappeared before the unwind can begin.45BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST,46// The thread to unwind has not responded to a signal in a timely manner.47BACKTRACE_UNWIND_ERROR_THREAD_TIMEOUT,48// Attempt to do an unsupported operation.49BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION,50// Attempt to do an offline unwind without a context.51BACKTRACE_UNWIND_ERROR_NO_CONTEXT,52// The count of frames exceed MAX_BACKTRACE_FRAMES.53BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT,54// Failed to read memory.55BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED,56// Failed to read registers.57BACKTRACE_UNWIND_ERROR_ACCESS_REG_FAILED,58// Failed to find a function in debug sections.59BACKTRACE_UNWIND_ERROR_FIND_PROC_INFO_FAILED,60// Failed to execute dwarf instructions in debug sections.61BACKTRACE_UNWIND_ERROR_EXECUTE_DWARF_INSTRUCTION_FAILED,62// Unwind information is incorrect.63BACKTRACE_UNWIND_ERROR_UNWIND_INFO,64// Unwind information stopped due to sp/pc repeating.65BACKTRACE_UNWIND_ERROR_REPEATED_FRAME,66// Unwind information stopped due to invalid elf.67BACKTRACE_UNWIND_ERROR_INVALID_ELF,68};6970struct BacktraceUnwindError {71enum BacktraceUnwindErrorCode error_code;7273union {74// for BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED75uint64_t addr;76// for BACKTRACE_UNWIND_ERROR_ACCESS_REG_FAILED77uint64_t regno;78} error_info;7980BacktraceUnwindError() : error_code(BACKTRACE_UNWIND_NO_ERROR) {}81};8283struct backtrace_frame_data_t {84size_t num; // The current fame number.85uint64_t pc; // The absolute pc.86uint64_t rel_pc; // The relative pc.87uint64_t sp; // The top of the stack.88size_t stack_size; // The size of the stack, zero indicate an unknown stack size.89backtrace_map_t map; // The map associated with the given pc.90std::string func_name; // The function name associated with this pc, NULL if not found.91uint64_t func_offset; // pc relative to the start of the function, only valid if func_name is not92// NULL.93};9495struct backtrace_stackinfo_t {96uint64_t start;97uint64_t end;98const uint8_t* data;99};100101namespace unwindstack {102class Regs;103}104105class Backtrace {106public:107enum ArchEnum : uint8_t {108ARCH_ARM,109ARCH_ARM64,110ARCH_X86,111ARCH_X86_64,112};113114static void SetGlobalElfCache(bool enable);115116// Create the correct Backtrace object based on what is to be unwound.117// If pid < 0 or equals the current pid, then the Backtrace object118// corresponds to the current process.119// If pid < 0 or equals the current pid and tid >= 0, then the Backtrace120// object corresponds to a thread in the current process.121// If pid >= 0 and tid < 0, then the Backtrace object corresponds to a122// different process.123// Tracing a thread in a different process is not supported.124// If map is NULL, then create the map and manage it internally.125// If map is not NULL, the map is still owned by the caller.126static Backtrace* Create(pid_t pid, pid_t tid, BacktraceMap* map = nullptr);127128virtual ~Backtrace();129130// Get the current stack trace and store in the backtrace_ structure.131virtual bool Unwind(size_t num_ignore_frames, void* context = nullptr) = 0;132133static bool Unwind(unwindstack::Regs* regs, BacktraceMap* back_map,134std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames,135std::vector<std::string>* skip_names, BacktraceUnwindError* error = nullptr);136137// Get the function name and offset into the function given the pc.138// If the string is empty, then no valid function name was found,139// or the pc is not in any valid map.140virtual std::string GetFunctionName(uint64_t pc, uint64_t* offset,141const backtrace_map_t* map = nullptr);142143// Fill in the map data associated with the given pc.144virtual void FillInMap(uint64_t pc, backtrace_map_t* map);145146// Read the data at a specific address.147virtual bool ReadWord(uint64_t ptr, word_t* out_value) = 0;148149// Read arbitrary data from a specific address. If a read request would150// span from one map to another, this call only reads up until the end151// of the current map.152// Returns the total number of bytes actually read.153virtual size_t Read(uint64_t addr, uint8_t* buffer, size_t bytes) = 0;154155// Create a string representing the formatted line of backtrace information156// for a single frame.157virtual std::string FormatFrameData(size_t frame_num);158static std::string FormatFrameData(const backtrace_frame_data_t* frame);159160pid_t Pid() const { return pid_; }161pid_t Tid() const { return tid_; }162size_t NumFrames() const { return frames_.size(); }163164const backtrace_frame_data_t* GetFrame(size_t frame_num) {165if (frame_num >= frames_.size()) {166return nullptr;167}168return &frames_[frame_num];169}170171typedef std::vector<backtrace_frame_data_t>::iterator iterator;172iterator begin() { return frames_.begin(); }173iterator end() { return frames_.end(); }174175typedef std::vector<backtrace_frame_data_t>::const_iterator const_iterator;176const_iterator begin() const { return frames_.begin(); }177const_iterator end() const { return frames_.end(); }178179BacktraceMap* GetMap() { return map_; }180181BacktraceUnwindError GetError() { return error_; }182183std::string GetErrorString(BacktraceUnwindError error);184185// Set whether to skip frames in libbacktrace/libunwindstack when doing a local unwind.186void SetSkipFrames(bool skip_frames) { skip_frames_ = skip_frames; }187188protected:189Backtrace(pid_t pid, pid_t tid, BacktraceMap* map);190191// The name returned is not demangled, GetFunctionName() takes care of192// demangling the name.193virtual std::string GetFunctionNameRaw(uint64_t pc, uint64_t* offset) = 0;194195virtual bool VerifyReadWordArgs(uint64_t ptr, word_t* out_value);196197bool BuildMap();198199pid_t pid_;200pid_t tid_;201202BacktraceMap* map_;203bool map_shared_;204205std::vector<backtrace_frame_data_t> frames_;206207// Skip frames in libbacktrace/libunwindstack when doing a local unwind.208bool skip_frames_ = true;209210BacktraceUnwindError error_;211};212213#endif // _BACKTRACE_BACKTRACE_H214215216