Home | History | Annotate | Download | only in unwindstack
      1 /*
      2  * Copyright (C) 2016 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_REGS_H
     18 #define _LIBUNWINDSTACK_REGS_H
     19 
     20 #include <stdint.h>
     21 
     22 #include <vector>
     23 
     24 namespace unwindstack {
     25 
     26 // Forward declarations.
     27 class Elf;
     28 struct MapInfo;
     29 class Memory;
     30 struct x86_ucontext_t;
     31 struct x86_64_ucontext_t;
     32 
     33 class Regs {
     34  public:
     35   enum LocationEnum : uint8_t {
     36     LOCATION_UNKNOWN = 0,
     37     LOCATION_REGISTER,
     38     LOCATION_SP_OFFSET,
     39   };
     40 
     41   struct Location {
     42     Location(LocationEnum type, int16_t value) : type(type), value(value) {}
     43 
     44     LocationEnum type;
     45     int16_t value;
     46   };
     47 
     48   Regs(uint16_t total_regs, uint16_t sp_reg, const Location& return_loc)
     49       : total_regs_(total_regs), sp_reg_(sp_reg), return_loc_(return_loc) {}
     50   virtual ~Regs() = default;
     51 
     52   virtual void* RawData() = 0;
     53   virtual uint64_t pc() = 0;
     54   virtual uint64_t sp() = 0;
     55 
     56   virtual bool GetReturnAddressFromDefault(Memory* memory, uint64_t* value) = 0;
     57 
     58   virtual uint64_t GetAdjustedPc(uint64_t rel_pc, Elf* elf) = 0;
     59 
     60   virtual bool StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) = 0;
     61 
     62   virtual void SetFromRaw() = 0;
     63 
     64   uint16_t sp_reg() { return sp_reg_; }
     65   uint16_t total_regs() { return total_regs_; }
     66 
     67   static uint32_t GetMachineType();
     68   static Regs* RemoteGet(pid_t pid, uint32_t* machine_type);
     69   static Regs* CreateFromUcontext(uint32_t machine_type, void* ucontext);
     70   static Regs* CreateFromLocal();
     71 
     72  protected:
     73   uint16_t total_regs_;
     74   uint16_t sp_reg_;
     75   Location return_loc_;
     76 };
     77 
     78 template <typename AddressType>
     79 class RegsImpl : public Regs {
     80  public:
     81   RegsImpl(uint16_t total_regs, uint16_t sp_reg, Location return_loc)
     82       : Regs(total_regs, sp_reg, return_loc), regs_(total_regs) {}
     83   virtual ~RegsImpl() = default;
     84 
     85   bool GetReturnAddressFromDefault(Memory* memory, uint64_t* value) override;
     86 
     87   uint64_t pc() override { return pc_; }
     88   uint64_t sp() override { return sp_; }
     89 
     90   void set_pc(AddressType pc) { pc_ = pc; }
     91   void set_sp(AddressType sp) { sp_ = sp; }
     92 
     93   inline AddressType& operator[](size_t reg) { return regs_[reg]; }
     94 
     95   void* RawData() override { return regs_.data(); }
     96 
     97  protected:
     98   AddressType pc_;
     99   AddressType sp_;
    100   std::vector<AddressType> regs_;
    101 };
    102 
    103 class RegsArm : public RegsImpl<uint32_t> {
    104  public:
    105   RegsArm();
    106   virtual ~RegsArm() = default;
    107 
    108   uint64_t GetAdjustedPc(uint64_t rel_pc, Elf* elf) override;
    109 
    110   void SetFromRaw() override;
    111 
    112   bool StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) override;
    113 };
    114 
    115 class RegsArm64 : public RegsImpl<uint64_t> {
    116  public:
    117   RegsArm64();
    118   virtual ~RegsArm64() = default;
    119 
    120   uint64_t GetAdjustedPc(uint64_t rel_pc, Elf* elf) override;
    121 
    122   void SetFromRaw() override;
    123 
    124   bool StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) override;
    125 };
    126 
    127 class RegsX86 : public RegsImpl<uint32_t> {
    128  public:
    129   RegsX86();
    130   virtual ~RegsX86() = default;
    131 
    132   uint64_t GetAdjustedPc(uint64_t rel_pc, Elf* elf) override;
    133 
    134   void SetFromRaw() override;
    135 
    136   bool StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) override;
    137 
    138   void SetFromUcontext(x86_ucontext_t* ucontext);
    139 };
    140 
    141 class RegsX86_64 : public RegsImpl<uint64_t> {
    142  public:
    143   RegsX86_64();
    144   virtual ~RegsX86_64() = default;
    145 
    146   uint64_t GetAdjustedPc(uint64_t rel_pc, Elf* elf) override;
    147 
    148   void SetFromRaw() override;
    149 
    150   bool StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) override;
    151 
    152   void SetFromUcontext(x86_64_ucontext_t* ucontext);
    153 };
    154 
    155 }  // namespace unwindstack
    156 
    157 #endif  // _LIBUNWINDSTACK_REGS_H
    158