Logo AND Algorithmique Numérique Distribuée

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