1 /* Copyright (c) 2015-2019. 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>
17 #include <libunwind.h>
19 #include "src/mc/AddressSpace.hpp"
20 #include "src/mc/inspect/mc_dwarf.hpp"
21 #include "src/mc/mc_forward.hpp"
23 /** @file DwarfExpression.hpp
25 * Evaluation of DWARF location expressions.
31 /** A DWARF expression
33 * DWARF defines a simple stack-based VM for evaluating expressions
34 * (such as locations of variables, etc.): a DWARF expression is
35 * just a sequence of dwarf instructions. We currently directly use
36 * `Dwarf_Op` from `dwarf.h` for dwarf instructions.
38 typedef std::vector<Dwarf_Op> DwarfExpression;
40 /** Context of evaluation of a DWARF expression
42 * Some DWARF instructions need to read the CPU registers,
43 * the process memory, etc. All those informations are gathered in
44 * the evaluation context.
46 class ExpressionContext {
51 , address_space(nullptr)
52 , object_info(nullptr)
53 , process_index(simgrid::mc::ProcessIndexMissing)
56 /** CPU state (registers) */
59 /** Address space used to read memory */
60 simgrid::mc::AddressSpace* address_space;
61 simgrid::mc::ObjectInformation* object_info;
65 /** When an error happens in the execution of a DWARF expression */
66 class evaluation_error : public std::runtime_error {
68 explicit evaluation_error(const char* what) : std::runtime_error(what) {}
71 /** A stack for evaluating a DWARF expression
73 * DWARF expressions work by manipulating a stack of integer values.
75 class ExpressionStack {
77 typedef std::uintptr_t value_type;
78 static const std::size_t max_size = 64;
81 // Values of the stack (the top is stack_[size_ - 1]):
82 uintptr_t stack_[max_size]{0};
86 ExpressionStack() : size_(0) {}
89 std::size_t size() const { return size_; }
90 bool empty() const { return size_ == 0; }
91 void clear() { size_ = 0; }
92 uintptr_t& operator[](int i) { return stack_[i]; }
93 uintptr_t const& operator[](int i) const { return stack_[i]; }
95 /** Top of the stack */
99 throw evaluation_error("Empty stack");
100 return stack_[size_ - 1];
103 /** Access the i-th element from the top of the stack */
104 value_type& top(unsigned i)
107 throw evaluation_error("Invalid element");
108 return stack_[size_ - 1 - i];
111 /** Push a value on the top of the stack */
112 void push(value_type value)
114 if (size_ == max_size)
115 throw evaluation_error("DWARF stack overflow");
116 stack_[size_++] = value;
119 /* Pop a value from the top of the stack */
123 throw evaluation_error("DWARF stack underflow");
124 return stack_[--size_];
127 // These are DWARF operations (DW_OP_foo):
129 /* Push a copy of the top-value (DW_OP_dup) */
130 void dup() { push(top()); }
132 /* Swap the two top-most values */
133 void swap() { std::swap(top(), top(1)); }
136 /** Executes a DWARF expression
138 * @param ops DWARF expression instructions
139 * @param n number of instructions
140 * @param context evaluation context (registers, memory, etc.)
141 * @param stack DWARf stack where the operations are executed
143 void execute(const Dwarf_Op* ops, std::size_t n, ExpressionContext const& context, ExpressionStack& stack);
145 /** Executes/evaluates a DWARF expression
147 * @param expression DWARF expression to execute
148 * @param context evaluation context (registers, memory, etc.)
149 * @param stack DWARf stack where the operations are executed
151 inline void execute(simgrid::dwarf::DwarfExpression const& expression, ExpressionContext const& context,
152 ExpressionStack& stack)
154 execute(expression.data(), expression.size(), context, stack);
158 } // namespace simgrid