Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into CRTP
[simgrid.git] / src / mc / inspect / DwarfExpression.hpp
1 /* Copyright (c) 2015-2019. 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 #include <libunwind.h>
18
19 #include "src/mc/inspect/mc_dwarf.hpp"
20 #include "src/mc/mc_forward.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 class ExpressionContext {
46 public:
47   ExpressionContext()
48       : cursor(nullptr)
49       , frame_base(nullptr)
50       , address_space(nullptr)
51       , object_info(nullptr)
52   {
53   }
54   /** CPU state (registers) */
55   unw_cursor_t* cursor;
56   void* frame_base;
57   /** Address space used to read memory */
58   const simgrid::mc::AddressSpace* address_space;
59   simgrid::mc::ObjectInformation* object_info;
60 };
61
62 /** When an error happens in the execution of a DWARF expression */
63 class evaluation_error : public std::runtime_error {
64 public:
65   explicit evaluation_error(const char* what) : std::runtime_error(what) {}
66 };
67
68 /** A stack for evaluating a DWARF expression
69  *
70  *  DWARF expressions work by manipulating a stack of integer values.
71  */
72 class ExpressionStack {
73 public:
74   typedef std::uintptr_t value_type;
75   static const std::size_t max_size = 64;
76
77 private:
78   // Values of the stack (the top is stack_[size_ - 1]):
79   uintptr_t stack_[max_size]{0};
80   size_t size_;
81
82 public:
83   ExpressionStack() : size_(0) {}
84
85   // Access:
86   std::size_t size() const { return size_; }
87   bool empty() const { return size_ == 0; }
88   void clear() { size_ = 0; }
89   uintptr_t& operator[](int i) { return stack_[i]; }
90   uintptr_t const& operator[](int i) const { return stack_[i]; }
91
92   /** Top of the stack */
93   value_type& top()
94   {
95     if (size_ == 0)
96       throw evaluation_error("Empty stack");
97     return stack_[size_ - 1];
98   }
99
100   /** Access the i-th element from the top of the stack */
101   value_type& top(unsigned i)
102   {
103     if (size_ < i)
104       throw evaluation_error("Invalid element");
105     return stack_[size_ - 1 - i];
106   }
107
108   /** Push a value on the top of the stack */
109   void push(value_type value)
110   {
111     if (size_ == max_size)
112       throw evaluation_error("DWARF stack overflow");
113     stack_[size_++] = value;
114   }
115
116   /* Pop a value from the top of the stack */
117   value_type pop()
118   {
119     if (size_ == 0)
120       throw evaluation_error("DWARF stack underflow");
121     return stack_[--size_];
122   }
123
124   // These are DWARF operations (DW_OP_foo):
125
126   /* Push a copy of the top-value (DW_OP_dup) */
127   void dup() { push(top()); }
128
129   /* Swap the two top-most values */
130   void swap() { std::swap(top(), top(1)); }
131 };
132
133 /** Executes a DWARF expression
134  *
135  *  @param ops     DWARF expression instructions
136  *  @param n       number of instructions
137  *  @param context evaluation context (registers, memory, etc.)
138  *  @param stack   DWARf stack where the operations are executed
139  */
140 void execute(const Dwarf_Op* ops, std::size_t n, ExpressionContext const& context, ExpressionStack& stack);
141
142 /** Executes/evaluates a DWARF expression
143  *
144  *  @param expression DWARF expression to execute
145  *  @param context    evaluation context (registers, memory, etc.)
146  *  @param stack      DWARf stack where the operations are executed
147  */
148 inline void execute(simgrid::dwarf::DwarfExpression const& expression, ExpressionContext const& context,
149                     ExpressionStack& stack)
150 {
151   execute(expression.data(), expression.size(), context, stack);
152 }
153
154 } // namespace dwarf
155 } // namespace simgrid
156
157 #endif