1 /* Copyright (c) 2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #ifndef SIMGRID_MC_DWARF_EXPRESSION_HPP
8 #define SIMGRID_MC_DWARF_EXPRESSION_HPP
13 #include <stdexcept> // runtime_error
17 #include <elfutils/libdw.h>
19 #include "src/mc/mc_forward.hpp"
20 #include "src/mc/AddressSpace.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 informations are gathered in
43 * the evaluation context.
45 struct 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 : std::runtime_error {
61 evaluation_error(const char* what): std::runtime_error(what) {}
62 ~evaluation_error() noexcept(true);
65 /** A stack for evaluating a DWARF expression
67 * DWARF expressions work by manipulating a stack of integer values.
69 class ExpressionStack {
71 typedef std::uintptr_t value_type;
72 static const std::size_t max_size = 64;
74 // Values of the stack (the top is stack_[size_ - 1]):
75 uintptr_t stack_[max_size];
78 ExpressionStack() : size_(0) {}
81 std::size_t size() const { return size_; }
82 bool empty() const { return size_ == 0; }
83 void clear() { size_ = 0; }
84 uintptr_t& operator[](int i) { return stack_[i]; }
85 uintptr_t const& operator[](int i) const { return stack_[i]; }
87 /** Top of the stack */
91 throw evaluation_error("Empty stack");
92 return stack_[size_ - 1];
95 /** Access the i-th element from the top of the stack */
96 value_type& top(unsigned i)
99 throw evaluation_error("Invalid element");
100 return stack_[size_ - 1 - i];
103 /** Push a value on the top of the stack */
104 void push(value_type value)
106 if (size_ == max_size)
107 throw evaluation_error("DWARF stack overflow");
108 stack_[size_++] = value;
111 /* Pop a value from the top of the stack */
115 throw evaluation_error("DWARF stack underflow");
116 return stack_[--size_];
119 // These are DWARF operations (DW_OP_foo):
121 /* Push a copy of the top-value (DW_OP_dup) */
122 void dup() { push(top()); }
124 /* Swap the two top-most values */
125 void swap() { std::swap(top(), top(1)); }
128 /** Executes a DWARF expression
130 * @param ops DWARF expression instructions
131 * @param n number of instructions
132 * @param context evaluation context (registers, memory, etc.)
133 * @param stack DWARf stack where the operations are executed
135 void execute(const Dwarf_Op* ops, std::size_t n,
136 ExpressionContext const& context, ExpressionStack& stack);
138 /** Executes/evaluates a DWARF expression
140 * @param expression DWARF expression to execute
141 * @param context evaluation context (registers, memory, etc.)
142 * @param stack DWARf stack where the operations are executed
145 void execute(simgrid::dwarf::DwarfExpression const& expression,
146 ExpressionContext const& context, ExpressionStack& stack)
148 execute(expression.data(), expression.size(), context, stack);