1 /* Copyright (c) 2015-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef SIMGRID_MC_DWARF_EXPRESSION_HPP
7 #define SIMGRID_MC_DWARF_EXPRESSION_HPP
12 #include <stdexcept> // runtime_error
16 #include <elfutils/libdw.h>
18 #include "src/mc/mc_forward.hpp"
19 #include "src/mc/AddressSpace.hpp"
21 /** @file DwarfExpression.hpp
23 * Evaluation of DWARF location expressions.
29 /** A DWARF expression
31 * DWARF defines a simple stack-based VM for evaluating expressions
32 * (such as locations of variables, etc.): a DWARF expression is
33 * just a sequence of dwarf instructions. We currently directly use
34 * `Dwarf_Op` from `dwarf.h` for dwarf instructions.
36 typedef std::vector<Dwarf_Op> DwarfExpression;
38 /** Context of evaluation of a DWARF expression
40 * Some DWARF instructions need to read the CPU registers,
41 * the process memory, etc. All those informations are gathered in
42 * the evaluation context.
44 class ExpressionContext {
47 cursor(nullptr), frame_base(nullptr), address_space(nullptr),
48 object_info(nullptr), process_index(simgrid::mc::ProcessIndexMissing) {}
49 /** CPU state (registers) */
52 /** Address space used to read memory */
53 simgrid::mc::AddressSpace* address_space;
54 simgrid::mc::ObjectInformation* object_info;
58 /** When an error happens in the execution of a DWARF expression */
59 class evaluation_error : public std::runtime_error {
61 explicit evaluation_error(const char* what) : std::runtime_error(what) {}
64 /** A stack for evaluating a DWARF expression
66 * DWARF expressions work by manipulating a stack of integer values.
68 class ExpressionStack {
70 typedef std::uintptr_t value_type;
71 static const std::size_t max_size = 64;
73 // Values of the stack (the top is stack_[size_ - 1]):
74 uintptr_t stack_[max_size] {0};
77 ExpressionStack() : size_(0) {}
80 std::size_t size() const { return size_; }
81 bool empty() const { return size_ == 0; }
82 void clear() { size_ = 0; }
83 uintptr_t& operator[](int i) { return stack_[i]; }
84 uintptr_t const& operator[](int i) const { return stack_[i]; }
86 /** Top of the stack */
90 throw evaluation_error("Empty stack");
91 return stack_[size_ - 1];
94 /** Access the i-th element from the top of the stack */
95 value_type& top(unsigned i)
98 throw evaluation_error("Invalid element");
99 return stack_[size_ - 1 - i];
102 /** Push a value on the top of the stack */
103 void push(value_type value)
105 if (size_ == max_size)
106 throw evaluation_error("DWARF stack overflow");
107 stack_[size_++] = value;
110 /* Pop a value from the top of the stack */
114 throw evaluation_error("DWARF stack underflow");
115 return stack_[--size_];
118 // These are DWARF operations (DW_OP_foo):
120 /* Push a copy of the top-value (DW_OP_dup) */
121 void dup() { push(top()); }
123 /* Swap the two top-most values */
124 void swap() { std::swap(top(), top(1)); }
127 /** Executes a DWARF expression
129 * @param ops DWARF expression instructions
130 * @param n number of instructions
131 * @param context evaluation context (registers, memory, etc.)
132 * @param stack DWARf stack where the operations are executed
134 void execute(const Dwarf_Op* ops, std::size_t n,
135 ExpressionContext const& context, ExpressionStack& stack);
137 /** Executes/evaluates a DWARF expression
139 * @param expression DWARF expression to execute
140 * @param context evaluation context (registers, memory, etc.)
141 * @param stack DWARf stack where the operations are executed
144 void execute(simgrid::dwarf::DwarfExpression const& expression,
145 ExpressionContext const& context, ExpressionStack& stack)
147 execute(expression.data(), expression.size(), context, stack);