Home | History | Annotate | Download | only in unwindstack
      1 /*
      2  * Copyright (C) 2017 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 _LIBUNWINDSTACK_UNWINDER_H
     18 #define _LIBUNWINDSTACK_UNWINDER_H
     19 
     20 #include <stdint.h>
     21 #include <sys/types.h>
     22 
     23 #include <memory>
     24 #include <string>
     25 #include <vector>
     26 
     27 #include <unwindstack/Error.h>
     28 #include <unwindstack/Maps.h>
     29 #include <unwindstack/Memory.h>
     30 #include <unwindstack/Regs.h>
     31 
     32 namespace unwindstack {
     33 
     34 // Forward declarations.
     35 class DexFiles;
     36 class Elf;
     37 class JitDebug;
     38 enum ArchEnum : uint8_t;
     39 
     40 struct FrameData {
     41   size_t num;
     42 
     43   uint64_t rel_pc;
     44   uint64_t pc;
     45   uint64_t sp;
     46 
     47   std::string function_name;
     48   uint64_t function_offset = 0;
     49 
     50   std::string map_name;
     51   uint64_t map_offset = 0;
     52   uint64_t map_start = 0;
     53   uint64_t map_end = 0;
     54   uint64_t map_load_bias = 0;
     55   int map_flags = 0;
     56 };
     57 
     58 class Unwinder {
     59  public:
     60   Unwinder(size_t max_frames, Maps* maps, Regs* regs, std::shared_ptr<Memory> process_memory)
     61       : max_frames_(max_frames), maps_(maps), regs_(regs), process_memory_(process_memory) {
     62     frames_.reserve(max_frames);
     63   }
     64   ~Unwinder() = default;
     65 
     66   void Unwind(const std::vector<std::string>* initial_map_names_to_skip = nullptr,
     67               const std::vector<std::string>* map_suffixes_to_ignore = nullptr);
     68 
     69   size_t NumFrames() { return frames_.size(); }
     70 
     71   const std::vector<FrameData>& frames() { return frames_; }
     72 
     73   std::string FormatFrame(size_t frame_num);
     74   static std::string FormatFrame(const FrameData& frame, bool is32bit);
     75 
     76   void SetJitDebug(JitDebug* jit_debug, ArchEnum arch);
     77 
     78   // Disabling the resolving of names results in the function name being
     79   // set to an empty string and the function offset being set to zero.
     80   void SetResolveNames(bool resolve) { resolve_names_ = resolve; }
     81 
     82 #if !defined(NO_LIBDEXFILE_SUPPORT)
     83   void SetDexFiles(DexFiles* dex_files, ArchEnum arch);
     84 #endif
     85 
     86   ErrorCode LastErrorCode() { return last_error_.code; }
     87   uint64_t LastErrorAddress() { return last_error_.address; }
     88 
     89  private:
     90   void FillInDexFrame();
     91   void FillInFrame(MapInfo* map_info, Elf* elf, uint64_t rel_pc, uint64_t func_pc,
     92                    uint64_t pc_adjustment);
     93 
     94   size_t max_frames_;
     95   Maps* maps_;
     96   Regs* regs_;
     97   std::vector<FrameData> frames_;
     98   std::shared_ptr<Memory> process_memory_;
     99   JitDebug* jit_debug_ = nullptr;
    100 #if !defined(NO_LIBDEXFILE_SUPPORT)
    101   DexFiles* dex_files_ = nullptr;
    102 #endif
    103   bool resolve_names_ = true;
    104   ErrorData last_error_;
    105 };
    106 
    107 }  // namespace unwindstack
    108 
    109 #endif  // _LIBUNWINDSTACK_UNWINDER_H
    110