2017-01-20 04:08:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 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 _LIBUNWINDSTACK_REGS_H
|
|
|
|
#define _LIBUNWINDSTACK_REGS_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2017-09-22 19:57:15 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
2017-01-20 04:08:48 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2017-07-14 17:37:19 +00:00
|
|
|
namespace unwindstack {
|
|
|
|
|
2017-02-01 23:44:40 +00:00
|
|
|
// Forward declarations.
|
|
|
|
class Elf;
|
2017-12-01 02:56:01 +00:00
|
|
|
enum ArchEnum : uint8_t;
|
2017-07-14 17:37:19 +00:00
|
|
|
class Memory;
|
2017-02-01 23:44:40 +00:00
|
|
|
|
2017-01-20 04:08:48 +00:00
|
|
|
class Regs {
|
|
|
|
public:
|
2017-02-01 23:44:40 +00:00
|
|
|
enum LocationEnum : uint8_t {
|
|
|
|
LOCATION_UNKNOWN = 0,
|
|
|
|
LOCATION_REGISTER,
|
|
|
|
LOCATION_SP_OFFSET,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Location {
|
|
|
|
Location(LocationEnum type, int16_t value) : type(type), value(value) {}
|
|
|
|
|
|
|
|
LocationEnum type;
|
|
|
|
int16_t value;
|
|
|
|
};
|
|
|
|
|
|
|
|
Regs(uint16_t total_regs, uint16_t sp_reg, const Location& return_loc)
|
|
|
|
: total_regs_(total_regs), sp_reg_(sp_reg), return_loc_(return_loc) {}
|
2017-01-20 04:08:48 +00:00
|
|
|
virtual ~Regs() = default;
|
|
|
|
|
2017-12-01 02:56:01 +00:00
|
|
|
virtual ArchEnum Arch() = 0;
|
2017-08-25 20:55:06 +00:00
|
|
|
|
2017-12-08 23:04:49 +00:00
|
|
|
virtual bool Format32Bit() = 0;
|
|
|
|
|
2017-02-01 23:44:40 +00:00
|
|
|
virtual void* RawData() = 0;
|
|
|
|
virtual uint64_t pc() = 0;
|
|
|
|
virtual uint64_t sp() = 0;
|
|
|
|
|
|
|
|
virtual uint64_t GetAdjustedPc(uint64_t rel_pc, Elf* elf) = 0;
|
|
|
|
|
2017-07-19 19:37:45 +00:00
|
|
|
virtual bool StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) = 0;
|
2017-07-18 23:09:20 +00:00
|
|
|
|
2017-07-07 23:35:48 +00:00
|
|
|
virtual void SetFromRaw() = 0;
|
|
|
|
|
2017-09-20 20:37:24 +00:00
|
|
|
virtual bool SetPcFromReturnAddress(Memory* process_memory) = 0;
|
|
|
|
|
2017-09-22 19:57:15 +00:00
|
|
|
virtual void IterateRegisters(std::function<void(const char*, uint64_t)>) = 0;
|
|
|
|
|
2017-01-20 04:08:48 +00:00
|
|
|
uint16_t sp_reg() { return sp_reg_; }
|
|
|
|
uint16_t total_regs() { return total_regs_; }
|
|
|
|
|
2017-12-01 02:56:01 +00:00
|
|
|
static ArchEnum CurrentArch();
|
2017-08-25 20:55:06 +00:00
|
|
|
static Regs* RemoteGet(pid_t pid);
|
2017-12-01 02:56:01 +00:00
|
|
|
static Regs* CreateFromUcontext(ArchEnum arch, void* ucontext);
|
2017-07-07 23:35:48 +00:00
|
|
|
static Regs* CreateFromLocal();
|
2017-01-20 04:08:48 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
uint16_t total_regs_;
|
2017-02-01 23:44:40 +00:00
|
|
|
uint16_t sp_reg_;
|
|
|
|
Location return_loc_;
|
2017-01-20 04:08:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename AddressType>
|
2017-06-02 00:55:25 +00:00
|
|
|
class RegsImpl : public Regs {
|
2017-01-20 04:08:48 +00:00
|
|
|
public:
|
2017-06-02 00:55:25 +00:00
|
|
|
RegsImpl(uint16_t total_regs, uint16_t sp_reg, Location return_loc)
|
2017-02-01 23:44:40 +00:00
|
|
|
: Regs(total_regs, sp_reg, return_loc), regs_(total_regs) {}
|
2017-06-02 00:55:25 +00:00
|
|
|
virtual ~RegsImpl() = default;
|
2017-01-20 04:08:48 +00:00
|
|
|
|
2017-02-01 23:44:40 +00:00
|
|
|
uint64_t pc() override { return pc_; }
|
|
|
|
uint64_t sp() override { return sp_; }
|
|
|
|
|
|
|
|
void set_pc(AddressType pc) { pc_ = pc; }
|
|
|
|
void set_sp(AddressType sp) { sp_ = sp; }
|
2017-01-20 04:08:48 +00:00
|
|
|
|
2017-12-08 23:04:49 +00:00
|
|
|
bool Format32Bit() override { return sizeof(AddressType) == sizeof(uint32_t); }
|
|
|
|
|
2017-01-20 04:08:48 +00:00
|
|
|
inline AddressType& operator[](size_t reg) { return regs_[reg]; }
|
|
|
|
|
2017-02-01 23:44:40 +00:00
|
|
|
void* RawData() override { return regs_.data(); }
|
2017-01-20 04:08:48 +00:00
|
|
|
|
2017-09-22 19:57:15 +00:00
|
|
|
virtual void IterateRegisters(std::function<void(const char*, uint64_t)> fn) override {
|
|
|
|
for (size_t i = 0; i < regs_.size(); ++i) {
|
|
|
|
fn(std::to_string(i).c_str(), regs_[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 23:44:40 +00:00
|
|
|
protected:
|
|
|
|
AddressType pc_;
|
|
|
|
AddressType sp_;
|
2017-01-20 04:08:48 +00:00
|
|
|
std::vector<AddressType> regs_;
|
|
|
|
};
|
|
|
|
|
2017-07-14 17:37:19 +00:00
|
|
|
} // namespace unwindstack
|
|
|
|
|
2017-01-20 04:08:48 +00:00
|
|
|
#endif // _LIBUNWINDSTACK_REGS_H
|