1 /* Copyright (c) 2015-2020. 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/inspect/mc_dwarf.hpp"
20 #include "src/mc/mc_forward.hpp"
22 /** @file DwarfExpression.hpp
24 * Evaluation of DWARF location expressions.
30 /** A DWARF expression
32 * DWARF defines a simple stack-based VM for evaluating expressions
33 * (such as locations of variables, etc.): a DWARF expression is
34 * just a sequence of dwarf instructions. We currently directly use
35 * `Dwarf_Op` from `dwarf.h` for dwarf instructions.
37 typedef std::vector<Dwarf_Op> DwarfExpression;
39 /** Context of evaluation of a DWARF expression
41 * Some DWARF instructions need to read the CPU registers,
42 * the process memory, etc. All those information are gathered in
43 * the evaluation context.
45 struct ExpressionContext {
46 /** CPU state (registers) */
47 unw_cursor_t* cursor = nullptr;
48 void* frame_base = nullptr;
49 const mc::AddressSpace* address_space = nullptr; /** Address space used to read memory */
50 mc::ObjectInformation* object_info = nullptr;
53 /** When an error happens in the execution of a DWARF expression */
54 class evaluation_error : public std::runtime_error {
56 explicit evaluation_error(const char* what) : std::runtime_error(what) {}
59 /** A stack for evaluating a DWARF expression
61 * DWARF expressions work by manipulating a stack of integer values.
63 class ExpressionStack {
65 typedef std::uintptr_t value_type;
66 static const std::size_t max_size = 64;
69 // Values of the stack (the top is stack_[size_ - 1]):
70 uintptr_t stack_[max_size]{0};
75 std::size_t size() const { return size_; }
76 bool empty() const { return size_ == 0; }
77 void clear() { size_ = 0; }
78 uintptr_t& operator[](int i) { return stack_[i]; }
79 uintptr_t const& operator[](int i) const { return stack_[i]; }
81 /** Top of the stack */
85 throw evaluation_error("Empty stack");
86 return stack_[size_ - 1];
89 /** Access the i-th element from the top of the stack */
90 value_type& top(unsigned i)
93 throw evaluation_error("Invalid element");
94 return stack_[size_ - 1 - i];
97 /** Push a value on the top of the stack */
98 void push(value_type value)
100 if (size_ == max_size)
101 throw evaluation_error("DWARF stack overflow");
102 stack_[size_++] = value;
105 /* Pop a value from the top of the stack */
109 throw evaluation_error("DWARF stack underflow");
110 return stack_[--size_];
113 // These are DWARF operations (DW_OP_foo):
115 /* Push a copy of the top-value (DW_OP_dup) */
116 void dup() { push(top()); }
118 /* Swap the two top-most values */
119 void swap() { std::swap(top(), top(1)); }
122 /** Executes a DWARF expression
124 * @param ops DWARF expression instructions
125 * @param n number of instructions
126 * @param context evaluation context (registers, memory, etc.)
127 * @param stack DWARf stack where the operations are executed
129 void execute(const Dwarf_Op* ops, std::size_t n, ExpressionContext const& context, ExpressionStack& stack);
131 /** Executes/evaluates a DWARF expression
133 * @param expression DWARF expression to execute
134 * @param context evaluation context (registers, memory, etc.)
135 * @param stack DWARf stack where the operations are executed
137 inline void execute(simgrid::dwarf::DwarfExpression const& expression, ExpressionContext const& context,
138 ExpressionStack& stack)
140 execute(expression.data(), expression.size(), context, stack);
144 } // namespace simgrid