Logo AND Algorithmique Numérique Distribuée

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