Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Replace long list of cases in switch by a set.
[simgrid.git] / src / mc / inspect / DwarfExpression.cpp
1 /* Copyright (c) 2014-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 #include <cstddef>
7 #include <cstdint>
8 #include <unordered_set>
9
10 #include "src/mc/AddressSpace.hpp"
11 #include "src/mc/inspect/DwarfExpression.hpp"
12 #include "src/mc/inspect/Frame.hpp"
13 #include "src/mc/inspect/LocationList.hpp"
14 #include "src/mc/inspect/ObjectInformation.hpp"
15 #include "src/mc/inspect/mc_dwarf.hpp"
16 #include "src/mc/mc_private.hpp"
17
18 using simgrid::mc::remote;
19
20 namespace simgrid {
21 namespace dwarf {
22
23 void execute(const Dwarf_Op* ops, std::size_t n, const ExpressionContext& context, ExpressionStack& stack)
24 {
25   for (size_t i = 0; i != n; ++i) {
26     const Dwarf_Op* op = ops + i;
27     std::uint8_t atom  = op->atom;
28     intptr_t first;
29     intptr_t second;
30
31     switch (atom) {
32
33         // Push the CFA (Canonical Frame Address):
34       case DW_OP_call_frame_cfa: {
35         /* See 6.4 of DWARF4 (http://dwarfstd.org/doc/DWARF4.pdf#page=140):
36          *
37          * > Typically, the CFA is defined to be the value of the stack
38          * > pointer at the call site in the previous frame (which may be
39          * > different from its value on entry to the current frame).
40          *
41          * We need to unwind the frame in order to get the SP of the parent
42          * frame.
43          *
44          * Warning: the CFA returned by libunwind (UNW_X86_64_RSP, etc.)
45          * is the SP of the *current* frame. */
46
47         if (not context.cursor)
48           throw evaluation_error("Missint cursor");
49
50         // Get frame:
51         unw_cursor_t cursor = *(context.cursor);
52         unw_step(&cursor);
53
54         unw_word_t res;
55         unw_get_reg(&cursor, UNW_REG_SP, &res);
56         stack.push(res);
57         break;
58       }
59
60         // Frame base:
61       case DW_OP_fbreg:
62         stack.push((std::uintptr_t)context.frame_base + op->number);
63         break;
64
65         // Address from the base address of this ELF object.
66         // Push the address on the stack (base_address + argument).
67       case DW_OP_addr: {
68         if (not context.object_info)
69           throw evaluation_error("No base address");
70         Dwarf_Off addr = (Dwarf_Off)(std::uintptr_t)context.object_info->base_address() + op->number;
71         stack.push(addr);
72         break;
73       }
74
75
76         // ***** Stack manipulation:
77
78         // Push another copy/duplicate the value at the top of the stack:
79       case DW_OP_dup:
80         stack.dup();
81         break;
82
83         // Pop/drop the top of the stack:
84       case DW_OP_drop:
85         stack.pop();
86         break;
87
88       case DW_OP_swap:
89         stack.swap();
90         break;
91
92         // Duplicate the value under the top of the stack:
93       case DW_OP_over:
94         stack.push(stack.top(1));
95         break;
96
97         // ***** Operations:
98         // Those usually take the top of the stack and the next value as argument
99         // and replace the top of the stack with the computed value
100         // (stack.top() += stack.before_top()).
101
102       case DW_OP_plus:
103         first  = stack.pop();
104         second = stack.pop();
105         stack.push(first + second);
106         break;
107
108       case DW_OP_mul:
109         first  = stack.pop();
110         second = stack.pop();
111         stack.push(first * second);
112         break;
113
114       case DW_OP_plus_uconst:
115         stack.top() += op->number;
116         break;
117
118       case DW_OP_not:
119         stack.top() = ~stack.top();
120         break;
121
122       case DW_OP_neg:
123         stack.top() = -(intptr_t)stack.top();
124         break;
125
126       case DW_OP_minus:
127         first  = stack.pop();
128         second = stack.pop();
129         stack.push(second - first);
130         break;
131
132       case DW_OP_and:
133         first  = stack.pop();
134         second = stack.pop();
135         stack.push(first & second);
136         break;
137
138       case DW_OP_or:
139         first  = stack.pop();
140         second = stack.pop();
141         stack.push(first | second);
142         break;
143
144       case DW_OP_xor:
145         first  = stack.pop();
146         second = stack.pop();
147         stack.push(first ^ second);
148         break;
149
150       case DW_OP_nop:
151         break;
152
153         // ***** Deference (memory fetch)
154
155       case DW_OP_deref_size:
156         throw evaluation_error("Unsupported operation");
157
158       case DW_OP_deref:
159         // Computed address:
160         if (not context.address_space)
161           throw evaluation_error("Missing address space");
162         context.address_space->read_bytes(&stack.top(), sizeof(uintptr_t), remote(stack.top()));
163         break;
164
165       default:
166
167         // Registers:
168         static const std::unordered_set<uint8_t> registers = {
169             DW_OP_breg0,  DW_OP_breg1,  DW_OP_breg2,  DW_OP_breg3,  DW_OP_breg4,  DW_OP_breg5,  DW_OP_breg6,
170             DW_OP_breg7,  DW_OP_breg8,  DW_OP_breg9,  DW_OP_breg10, DW_OP_breg11, DW_OP_breg12, DW_OP_breg13,
171             DW_OP_breg14, DW_OP_breg15, DW_OP_breg16, DW_OP_breg17, DW_OP_breg18, DW_OP_breg19, DW_OP_breg20,
172             DW_OP_breg21, DW_OP_breg22, DW_OP_breg23, DW_OP_breg24, DW_OP_breg25, DW_OP_breg26, DW_OP_breg27,
173             DW_OP_breg28, DW_OP_breg29, DW_OP_breg30, DW_OP_breg31};
174         if (registers.count(atom) > 0) {
175           // Push register + constant:
176           int register_id = simgrid::dwarf::dwarf_register_to_libunwind(op->atom - DW_OP_breg0);
177           unw_word_t res;
178           if (not context.cursor)
179             throw evaluation_error("Missing stack context");
180           unw_get_reg(context.cursor, register_id, &res);
181           stack.push(res + op->number);
182           break;
183         }
184
185         // ***** Constants:
186
187         // Short constant literals:
188         static const std::unordered_set<uint8_t> literals = {
189             DW_OP_lit0,  DW_OP_lit1,  DW_OP_lit2,  DW_OP_lit3,  DW_OP_lit4,  DW_OP_lit5,  DW_OP_lit6,  DW_OP_lit7,
190             DW_OP_lit8,  DW_OP_lit9,  DW_OP_lit10, DW_OP_lit11, DW_OP_lit12, DW_OP_lit13, DW_OP_lit14, DW_OP_lit15,
191             DW_OP_lit16, DW_OP_lit17, DW_OP_lit18, DW_OP_lit19, DW_OP_lit20, DW_OP_lit21, DW_OP_lit22, DW_OP_lit23,
192             DW_OP_lit24, DW_OP_lit25, DW_OP_lit26, DW_OP_lit27, DW_OP_lit28, DW_OP_lit29, DW_OP_lit30, DW_OP_lit31};
193         if (literals.count(atom) > 0) {
194           // Push a literal/constant on the stack:
195           stack.push(atom - DW_OP_lit0);
196           break;
197         }
198
199         // General constants:
200         static const std::unordered_set<uint8_t> constants = {
201             DW_OP_const1u, DW_OP_const2u, DW_OP_const4u, DW_OP_const8u, DW_OP_const1s,
202             DW_OP_const2s, DW_OP_const4s, DW_OP_const8s, DW_OP_constu,  DW_OP_consts};
203         if (constants.count(atom) > 0) {
204           // Push the constant argument on the stack.
205           stack.push(op->number);
206           break;
207         }
208
209         // Not handled:
210         throw evaluation_error("Unsupported operation");
211     }
212   }
213 }
214
215 } // namespace dwarf
216 } // namespace simgrid