2018-03-28 15:33:36 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
2018-03-29 13:34:47 +02:00
|
|
|
#include <cstdint>
|
2018-03-28 15:33:36 +02:00
|
|
|
|
2018-03-29 13:34:47 +02:00
|
|
|
#include <dwarfpp/lib.hpp>
|
|
|
|
#include <dwarfpp/regs.hpp>
|
|
|
|
#include <dwarfpp/frame.hpp>
|
|
|
|
#include <dwarfpp/attr.hpp>
|
|
|
|
#include <dwarfpp/frame.hpp>
|
2018-03-28 15:33:36 +02:00
|
|
|
#include <dwarfpp/root.hpp>
|
|
|
|
|
2018-03-30 13:25:05 +02:00
|
|
|
#define OF_WHAT_EXCEPTION(cl_name) \
|
|
|
|
cl_name: public WhatException { \
|
|
|
|
public:\
|
|
|
|
cl_name(const std::string& what): WhatException(what) {} \
|
|
|
|
cl_name() = default; \
|
|
|
|
}
|
|
|
|
|
2018-03-28 15:33:36 +02:00
|
|
|
class DwarfInterpret {
|
|
|
|
/** Singleton class holding a Dwarf interpret.
|
|
|
|
* Must be first instanciated with the path to the binary being run, with a
|
|
|
|
* call to `instanciate`, and can afterwards be accessed with calls to
|
|
|
|
* `acquire`.
|
|
|
|
*/
|
|
|
|
|
2018-03-30 14:25:58 +02:00
|
|
|
public: // Types, sub-classes, …
|
2018-03-30 13:25:05 +02:00
|
|
|
class WhatException: public std::exception {
|
|
|
|
/** Base exception for other exceptions, not supposed to be thrown
|
|
|
|
* by itself */
|
|
|
|
|
|
|
|
std::string what_str;
|
|
|
|
|
|
|
|
public:
|
2018-03-30 14:25:58 +02:00
|
|
|
/// Initialize the exception with an explanatory text chunk
|
|
|
|
explicit WhatException(const std::string& what)
|
|
|
|
: what_str(what) {}
|
|
|
|
|
|
|
|
/// Leave the explanatory text empty
|
2018-03-30 13:25:05 +02:00
|
|
|
WhatException(): what_str("") {}
|
|
|
|
|
2018-03-30 14:25:58 +02:00
|
|
|
/// Get the explanatory text for this exception
|
2018-03-30 13:25:05 +02:00
|
|
|
const char* what() const noexcept {
|
|
|
|
return what_str.c_str();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-03-30 14:25:58 +02:00
|
|
|
/// Thrown when `acquire` is called before `instanciate`
|
2018-03-30 13:25:05 +02:00
|
|
|
class OF_WHAT_EXCEPTION(NotInstanciated);
|
2018-03-30 14:25:58 +02:00
|
|
|
|
|
|
|
/// Thrown when `instanciate` is called twice
|
2018-03-30 13:25:05 +02:00
|
|
|
class OF_WHAT_EXCEPTION(AlreadyInstanciated);
|
2018-03-30 14:25:58 +02:00
|
|
|
|
|
|
|
/** Thrown when trying to get the value of a Dwarf register (column)
|
|
|
|
* that is not defined or has, somehow, no value at this PC. */
|
2018-03-30 13:25:05 +02:00
|
|
|
class OF_WHAT_EXCEPTION(ValuelessRegister);
|
2018-03-30 14:25:58 +02:00
|
|
|
|
|
|
|
/// Thrown when accessing unimplemented parts of this library
|
2018-03-30 13:25:05 +02:00
|
|
|
class OF_WHAT_EXCEPTION(NotImplemented);
|
2018-03-30 14:25:58 +02:00
|
|
|
|
|
|
|
/// Thrown when somehow, getcontext (read CPU registers) fails
|
2018-03-30 13:25:05 +02:00
|
|
|
class OF_WHAT_EXCEPTION(FailedGetContext);
|
2018-03-30 14:25:58 +02:00
|
|
|
|
|
|
|
/// Thrown when a Dwarf element is not found for a given PC
|
2018-03-30 13:25:05 +02:00
|
|
|
class OF_WHAT_EXCEPTION(NotFound);
|
2018-03-28 15:33:36 +02:00
|
|
|
|
2018-03-29 13:34:47 +02:00
|
|
|
|
2018-03-30 14:25:58 +02:00
|
|
|
/// A Dwarf register
|
|
|
|
typedef dwarf::core::FrameSection::register_def DwarfRegister;
|
2018-03-29 13:34:47 +02:00
|
|
|
|
2018-03-30 14:25:58 +02:00
|
|
|
/// A Dwarf row of registers (for a given PC)
|
|
|
|
typedef std::set<std::pair<int, DwarfRegister> > DwarfRow;
|
2018-03-29 13:34:47 +02:00
|
|
|
|
2018-03-30 14:25:58 +02:00
|
|
|
/// The value type of a register's contents
|
|
|
|
typedef uintptr_t reg_content_t;
|
2018-03-29 13:34:47 +02:00
|
|
|
|
2018-03-30 14:25:58 +02:00
|
|
|
public: // methods
|
2018-03-28 15:33:36 +02:00
|
|
|
DwarfInterpret(DwarfInterpret const&) = delete;
|
|
|
|
void operator=(DwarfInterpret const&) = delete;
|
|
|
|
|
2018-03-30 14:25:58 +02:00
|
|
|
/** Acquire the instance of `DwarfInterpret`. The method #instanciate
|
|
|
|
* must have been called beforehand.
|
|
|
|
*
|
|
|
|
* \throws NotInstanciated whenever this condition is not met.
|
|
|
|
*/
|
2018-03-28 15:33:36 +02:00
|
|
|
static DwarfInterpret& acquire();
|
2018-03-30 14:25:58 +02:00
|
|
|
|
|
|
|
/** Instanciate the instance of DwarfInterpret with the path to the
|
|
|
|
* program currently running (usually, argv[0])
|
|
|
|
*
|
|
|
|
* \throws AlreadyInstanciated if this method is called twice. */
|
2018-03-28 15:33:36 +02:00
|
|
|
static DwarfInterpret& instanciate(const std::string& elf_path);
|
|
|
|
|
2018-03-30 14:25:58 +02:00
|
|
|
/// Returns the ELF machine number for this ELF
|
2018-03-28 16:03:43 +02:00
|
|
|
int get_elf_machine() const { return elf_machine; }
|
|
|
|
|
2018-03-30 14:25:58 +02:00
|
|
|
/** Retrieves the value pointed to by the given Dwarf register
|
|
|
|
*
|
|
|
|
* \throws ValuelessRegister */
|
2018-03-29 13:34:47 +02:00
|
|
|
reg_content_t interpret_dw_register(
|
|
|
|
const DwarfRow& row,
|
|
|
|
const DwarfRegister& reg
|
|
|
|
) const;
|
2018-03-30 14:25:58 +02:00
|
|
|
|
|
|
|
/** Retrieves the value pointed to by the given Dwarf register
|
|
|
|
*
|
|
|
|
* \throws ValuelessRegister */
|
2018-03-29 13:34:47 +02:00
|
|
|
reg_content_t interpret_dw_register(
|
|
|
|
const DwarfRow& row,
|
|
|
|
int reg_id
|
|
|
|
) const;
|
2018-03-28 16:03:43 +02:00
|
|
|
|
2018-03-30 14:25:58 +02:00
|
|
|
/** Get the return address at a given program counter, assuming the
|
|
|
|
* correct registers are stored */
|
2018-03-30 13:25:05 +02:00
|
|
|
uintptr_t get_return_address(uintptr_t cur_pc) const;
|
2018-03-30 14:25:58 +02:00
|
|
|
|
|
|
|
/** Get the return address of the current program point */
|
2018-03-30 13:25:05 +02:00
|
|
|
uintptr_t get_self_return_address() const;
|
|
|
|
|
2018-03-30 14:25:58 +02:00
|
|
|
/// Get the current program counter
|
2018-03-30 13:25:05 +02:00
|
|
|
static uintptr_t get_current_pc();
|
|
|
|
|
2018-03-28 15:33:36 +02:00
|
|
|
private:
|
|
|
|
DwarfInterpret(const std::string& elf_path);
|
|
|
|
|
2018-03-29 13:34:47 +02:00
|
|
|
DwarfRegister get_column(const DwarfRow& row, int column) const;
|
|
|
|
reg_content_t get_cpu_register(int reg_id) const;
|
|
|
|
|
2018-03-30 13:25:05 +02:00
|
|
|
const dwarf::core::FrameSection::fde_iterator fde_at(
|
|
|
|
uintptr_t pc) const;
|
|
|
|
const dwarf::core::FrameSection::cie_iterator cie_at(
|
|
|
|
uintptr_t pc) const;
|
|
|
|
const DwarfRow& dwarf_row_at(uintptr_t pc) const;
|
|
|
|
|
|
|
|
uintptr_t get_caller_pc() const;
|
|
|
|
|
2018-03-29 13:34:47 +02:00
|
|
|
|
2018-03-28 15:33:36 +02:00
|
|
|
private: // members
|
|
|
|
static std::unique_ptr<DwarfInterpret> instance;
|
|
|
|
|
|
|
|
dwarf::core::root_die root_die;
|
|
|
|
int elf_machine;
|
|
|
|
|
|
|
|
friend class std::unique_ptr<DwarfInterpret>;
|
|
|
|
};
|