Logo AND Algorithmique Numérique Distribuée

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