Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] OOPify/C++ify Type (cont)
[simgrid.git] / src / mc / mc_location.h
1 /* Copyright (c) 2004-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef SIMGRID_MC_OBJECT_LOCATION_H
8 #define SIMGRID_MC_OBJECT_LOCATION_H
9
10 #include <stdint.h>
11
12 #include <vector>
13
14 #include <libunwind.h>
15 #include <dwarf.h>
16 #include <elfutils/libdw.h>
17
18 #include <simgrid_config.h>
19 #include "mc_base.h"
20 #include "mc_forward.h"
21 #include "AddressSpace.hpp"
22
23 namespace simgrid {
24 namespace mc {
25
26 typedef std::vector<Dwarf_Op> DwarfExpression;
27
28 }
29 }
30
31 SG_BEGIN_DECL()
32
33 /** \brief a DWARF expression with optional validity contraints */
34 typedef struct s_mc_expression {
35   size_t size;
36   Dwarf_Op* ops;
37   // Optional validity:
38   void* lowpc, *highpc;
39 } s_mc_expression_t, *mc_expression_t;
40
41 /** A location list (list of location expressions) */
42 typedef struct s_mc_location_list {
43   size_t size;
44   mc_expression_t locations;
45 } s_mc_location_list_t, *mc_location_list_t;
46
47 /** A location is either a location in memory of a register location
48  *
49  *  Usage:
50  *
51  *    * mc_dwarf_resolve_locations or mc_dwarf_resolve_location is used
52  *      to find the location of a given location expression or location list;
53  *
54  *    * mc_get_location_type MUST be used to find the location type;
55  *
56  *    * for MC_LOCATION_TYPE_ADDRESS, memory_address is the resulting address
57  *
58  *    * for MC_LOCATION_TYPE_REGISTER, unw_get_reg(l.cursor, l.register_id, value)
59  *        and unw_get_reg(l.cursor, l.register_id, value) can be used to read/write
60  *        the value.
61  *  </ul>
62  */
63 typedef struct s_mc_location {
64   void* memory_location;
65   unw_cursor_t* cursor;
66   int register_id;
67 } s_mc_location_t, *mc_location_t;
68
69 /** Type of a given location
70  *
71  *  Use `mc_get_location_type(location)` to find the type.
72  * */
73 typedef enum mc_location_type {
74   MC_LOCATION_TYPE_ADDRESS,
75   MC_LOCATION_TYPE_REGISTER
76 } mc_location_type;
77
78 /** Find the type of a location */
79 static inline __attribute__ ((always_inline))
80 enum mc_location_type mc_get_location_type(mc_location_t location) {
81   if (location->cursor) {
82     return MC_LOCATION_TYPE_REGISTER;
83   } else {
84     return MC_LOCATION_TYPE_ADDRESS;
85   }
86 }
87
88 XBT_INTERNAL void mc_dwarf_resolve_location(mc_location_t location, mc_expression_t expression, mc_object_info_t object_info, unw_cursor_t* c, void* frame_pointer_address, mc_address_space_t address_space, int process_index);
89 MC_SHOULD_BE_INTERNAL void mc_dwarf_resolve_locations(mc_location_t location, mc_location_list_t locations, mc_object_info_t object_info, unw_cursor_t* c, void* frame_pointer_address, mc_address_space_t address_space, int process_index);
90
91 XBT_INTERNAL void mc_dwarf_expression_clear(mc_expression_t expression);
92 XBT_INTERNAL void mc_dwarf_expression_init(mc_expression_t expression, size_t len, Dwarf_Op* ops);
93
94 XBT_INTERNAL void mc_dwarf_location_list_clear(mc_location_list_t list);
95
96 XBT_INTERNAL void mc_dwarf_location_list_init_from_expression(mc_location_list_t target, size_t len, Dwarf_Op* ops);
97 XBT_INTERNAL void mc_dwarf_location_list_init(mc_location_list_t target, mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr);
98
99 #define MC_EXPRESSION_STACK_SIZE 64
100
101 #define MC_EXPRESSION_OK 0
102 #define MC_EXPRESSION_E_UNSUPPORTED_OPERATION 1
103 #define MC_EXPRESSION_E_STACK_OVERFLOW 2
104 #define MC_EXPRESSION_E_STACK_UNDERFLOW 3
105 #define MC_EXPRESSION_E_MISSING_STACK_CONTEXT 4
106 #define MC_EXPRESSION_E_MISSING_FRAME_BASE 5
107 #define MC_EXPRESSION_E_NO_BASE_ADDRESS 6
108
109 typedef struct s_mc_expression_state {
110   uintptr_t stack[MC_EXPRESSION_STACK_SIZE];
111   size_t stack_size;
112
113   unw_cursor_t* cursor;
114   void* frame_base;
115   mc_address_space_t address_space;
116   mc_object_info_t object_info;
117   int process_index;
118 } s_mc_expression_state_t, *mc_expression_state_t;
119
120 MC_SHOULD_BE_INTERNAL int mc_dwarf_execute_expression(
121   size_t n, const Dwarf_Op* ops, mc_expression_state_t state);
122
123 MC_SHOULD_BE_INTERNAL void* mc_find_frame_base(
124   dw_frame_t frame, mc_object_info_t object_info, unw_cursor_t* unw_cursor);
125
126 SG_END_DECL()
127
128 namespace simgrid {
129 namespace mc {
130
131 inline
132 int execute(DwarfExpression const& expression, mc_expression_state_t state)
133 {
134   return mc_dwarf_execute_expression(
135     expression.size(), expression.data(), state);
136 }
137
138 }
139 }
140
141 #endif