Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / mc / DwarfExpression.hpp
1 /* Copyright (c) 2015-2017. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #ifndef SIMGRID_MC_DWARF_EXPRESSION_HPP
7 #define SIMGRID_MC_DWARF_EXPRESSION_HPP
8
9 #include <cstdint>
10 #include <cstdlib>
11
12 #include <stdexcept> // runtime_error
13 #include <utility>
14 #include <vector>
15
16 #include <elfutils/libdw.h>
17
18 #include "src/mc/mc_forward.hpp"
19 #include "src/mc/AddressSpace.hpp"
20
21 /** @file DwarfExpression.hpp
22  *
23  *  Evaluation of DWARF location expressions.
24  */
25
26 namespace simgrid {
27 namespace dwarf {
28
29 /** A DWARF expression
30  *
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.
35  */
36 typedef std::vector<Dwarf_Op> DwarfExpression;
37
38 /** Context of evaluation of a DWARF expression
39  *
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.
43  */
44 class ExpressionContext {
45 public:
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   explicit evaluation_error(const char* what) : std::runtime_error(what) {}
62 };
63
64 /** A stack for evaluating a DWARF expression
65  *
66  *  DWARF expressions work by manipulating a stack of integer values.
67  */
68 class ExpressionStack {
69 public:
70   typedef std::uintptr_t value_type;
71   static const std::size_t max_size = 64;
72 private:
73   // Values of the stack (the top is stack_[size_ - 1]):
74   uintptr_t stack_[max_size] {0};
75   size_t size_;
76 public:
77   ExpressionStack() : size_(0) {}
78
79   // Access:
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]; }
85
86   /** Top of the stack */
87   value_type& top()
88   {
89     if (size_ == 0)
90       throw evaluation_error("Empty stack");
91     return stack_[size_ - 1];
92   }
93
94   /** Access the i-th element from the top of the stack */
95   value_type& top(unsigned i)
96   {
97     if (size_ < i)
98       throw evaluation_error("Invalid element");
99     return stack_[size_ - 1 - i];
100   }
101
102   /** Push a value on the top of the stack */
103   void push(value_type value)
104   {
105     if (size_ == max_size)
106       throw evaluation_error("DWARF stack overflow");
107     stack_[size_++] = value;
108   }
109
110   /* Pop a value from the top of the stack */
111   value_type pop()
112   {
113     if (size_ == 0)
114       throw evaluation_error("DWARF stack underflow");
115     return stack_[--size_];
116   }
117
118   // These are DWARF operations (DW_OP_foo):
119
120   /* Push a copy of the top-value (DW_OP_dup) */
121   void dup() { push(top()); }
122
123   /* Swap the two top-most values */
124   void swap() { std::swap(top(), top(1)); }
125 };
126
127 /** Executes a DWARF expression
128  *
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
133  */
134 void execute(const Dwarf_Op* ops, std::size_t n,
135   ExpressionContext const& context, ExpressionStack& stack);
136
137 /** Executes/evaluates a DWARF expression
138  *
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
142  */
143 inline
144 void execute(simgrid::dwarf::DwarfExpression const& expression,
145   ExpressionContext const& context, ExpressionStack& stack)
146 {
147   execute(expression.data(), expression.size(), context, stack);
148 }
149
150 }
151 }
152
153 #endif