Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
chop, chop, chop includes
[simgrid.git] / src / mc / DwarfExpression.hpp
1 /* Copyright (c) 2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #ifndef SIMGRID_MC_DWARF_EXPRESSION_HPP
8 #define SIMGRID_MC_DWARF_EXPRESSION_HPP
9
10 #include <cstdint>
11 #include <cstdlib>
12
13 #include <stdexcept> // runtime_error
14 #include <utility>
15 #include <vector>
16
17 #include <elfutils/libdw.h>
18
19 #include "src/mc/mc_forward.hpp"
20 #include "src/mc/AddressSpace.hpp"
21
22 /** @file DwarfExpression.hpp
23  *
24  *  Evaluation of DWARF location expressions.
25  */
26
27 namespace simgrid {
28 namespace dwarf {
29
30 /** A DWARF expression
31  *
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.
36  */
37 typedef std::vector<Dwarf_Op> DwarfExpression;
38
39 /** Context of evaluation of a DWARF expression
40  *
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.
44  */
45 struct ExpressionContext {
46   ExpressionContext() :
47     cursor(nullptr), frame_base(nullptr), address_space(nullptr),
48     object_info(nullptr), process_index(simgrid::mc::ProcessIndexMissing) {}
49   /** CPU state (registers) */
50   unw_cursor_t* cursor;
51   void* frame_base;
52   /** Address space used to read memory */
53   simgrid::mc::AddressSpace* address_space;
54   simgrid::mc::ObjectInformation* object_info;
55   int process_index;
56 };
57
58 /** When an error happens in the execution of a DWARF expression */
59 class evaluation_error : std::runtime_error {
60 public:
61   evaluation_error(const char* what): std::runtime_error(what) {}
62   ~evaluation_error() noexcept(true);
63 };
64
65 /** A stack for evaluating a DWARF expression
66  *
67  *  DWARF expressions work by manipulating a stack of integer values.
68  */
69 class ExpressionStack {
70 public:
71   typedef std::uintptr_t value_type;
72   static const std::size_t max_size = 64;
73 private:
74   // Values of the stack (the top is stack_[size_ - 1]):
75   uintptr_t stack_[max_size];
76   size_t size_;
77 public:
78   ExpressionStack() : size_(0) {}
79
80   // Access:
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]; }
86
87   /** Top of the stack */
88   value_type& top()
89   {
90     if (size_ == 0)
91       throw evaluation_error("Empty stack");
92     return stack_[size_ - 1];
93   }
94
95   /** Access the i-th element from the top of the stack */
96   value_type& top(unsigned i)
97   {
98     if (size_ < i)
99       throw evaluation_error("Invalid element");
100     return stack_[size_ - 1 - i];
101   }
102
103   /** Push a value on the top of the stack */
104   void push(value_type value)
105   {
106     if (size_ == max_size)
107       throw evaluation_error("DWARF stack overflow");
108     stack_[size_++] = value;
109   }
110
111   /* Pop a value from the top of the stack */
112   value_type pop()
113   {
114     if (size_ == 0)
115       throw evaluation_error("DWARF stack underflow");
116     return stack_[--size_];
117   }
118
119   // These are DWARF operations (DW_OP_foo):
120
121   /* Push a copy of the top-value (DW_OP_dup) */
122   void dup() { push(top()); }
123
124   /* Swap the two top-most values */
125   void swap() { std::swap(top(), top(1)); }
126 };
127
128 /** Executes a DWARF expression
129  *
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
134  */
135 void execute(const Dwarf_Op* ops, std::size_t n,
136   ExpressionContext const& context, ExpressionStack& stack);
137
138 /** Executes/evaluates a DWARF expression
139  *
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
143  */
144 inline
145 void execute(simgrid::dwarf::DwarfExpression const& expression,
146   ExpressionContext const& context, ExpressionStack& stack)
147 {
148   execute(expression.data(), expression.size(), context, stack);
149 }
150
151 }
152 }
153
154 #endif