Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
064ba32e8c7a11b89ba144cb73ccdbfe66cc2896
[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/AddressSpace.hpp"
20 #include "src/mc/inspect/mc_dwarf.hpp"
21 #include "src/mc/mc_forward.hpp"
22
23 /** @file DwarfExpression.hpp
24  *
25  *  Evaluation of DWARF location expressions.
26  */
27
28 namespace simgrid {
29 namespace dwarf {
30
31 /** A DWARF expression
32  *
33  *  DWARF defines a simple stack-based VM for evaluating expressions
34  *  (such as locations of variables, etc.): a DWARF expression is
35  *  just a sequence of dwarf instructions. We currently directly use
36  *  `Dwarf_Op` from `dwarf.h` for dwarf instructions.
37  */
38 typedef std::vector<Dwarf_Op> DwarfExpression;
39
40 /** Context of evaluation of a DWARF expression
41  *
42  *  Some DWARF instructions need to read the CPU registers,
43  *  the process memory, etc. All those informations are gathered in
44  *  the evaluation context.
45  */
46 class ExpressionContext {
47 public:
48   ExpressionContext()
49       : cursor(nullptr)
50       , frame_base(nullptr)
51       , address_space(nullptr)
52       , object_info(nullptr)
53   {
54   }
55   /** CPU state (registers) */
56   unw_cursor_t* cursor;
57   void* frame_base;
58   /** Address space used to read memory */
59   const simgrid::mc::AddressSpace* address_space;
60   simgrid::mc::ObjectInformation* object_info;
61 };
62
63 /** When an error happens in the execution of a DWARF expression */
64 class evaluation_error : public std::runtime_error {
65 public:
66   explicit evaluation_error(const char* what) : std::runtime_error(what) {}
67 };
68
69 /** A stack for evaluating a DWARF expression
70  *
71  *  DWARF expressions work by manipulating a stack of integer values.
72  */
73 class ExpressionStack {
74 public:
75   typedef std::uintptr_t value_type;
76   static const std::size_t max_size = 64;
77
78 private:
79   // Values of the stack (the top is stack_[size_ - 1]):
80   uintptr_t stack_[max_size]{0};
81   size_t size_;
82
83 public:
84   ExpressionStack() : size_(0) {}
85
86   // Access:
87   std::size_t size() const { return size_; }
88   bool empty() const { return size_ == 0; }
89   void clear() { size_ = 0; }
90   uintptr_t& operator[](int i) { return stack_[i]; }
91   uintptr_t const& operator[](int i) const { return stack_[i]; }
92
93   /** Top of the stack */
94   value_type& top()
95   {
96     if (size_ == 0)
97       throw evaluation_error("Empty stack");
98     return stack_[size_ - 1];
99   }
100
101   /** Access the i-th element from the top of the stack */
102   value_type& top(unsigned i)
103   {
104     if (size_ < i)
105       throw evaluation_error("Invalid element");
106     return stack_[size_ - 1 - i];
107   }
108
109   /** Push a value on the top of the stack */
110   void push(value_type value)
111   {
112     if (size_ == max_size)
113       throw evaluation_error("DWARF stack overflow");
114     stack_[size_++] = value;
115   }
116
117   /* Pop a value from the top of the stack */
118   value_type pop()
119   {
120     if (size_ == 0)
121       throw evaluation_error("DWARF stack underflow");
122     return stack_[--size_];
123   }
124
125   // These are DWARF operations (DW_OP_foo):
126
127   /* Push a copy of the top-value (DW_OP_dup) */
128   void dup() { push(top()); }
129
130   /* Swap the two top-most values */
131   void swap() { std::swap(top(), top(1)); }
132 };
133
134 /** Executes a DWARF expression
135  *
136  *  @param ops     DWARF expression instructions
137  *  @param n       number of instructions
138  *  @param context evaluation context (registers, memory, etc.)
139  *  @param stack   DWARf stack where the operations are executed
140  */
141 void execute(const Dwarf_Op* ops, std::size_t n, ExpressionContext const& context, ExpressionStack& stack);
142
143 /** Executes/evaluates a DWARF expression
144  *
145  *  @param expression DWARF expression to execute
146  *  @param context    evaluation context (registers, memory, etc.)
147  *  @param stack      DWARf stack where the operations are executed
148  */
149 inline void execute(simgrid::dwarf::DwarfExpression const& expression, ExpressionContext const& context,
150                     ExpressionStack& stack)
151 {
152   execute(expression.data(), expression.size(), context, stack);
153 }
154
155 } // namespace dwarf
156 } // namespace simgrid
157
158 #endif