Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / mc / mc_location.h
1 /* Copyright (c) 2004-2015. 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.hpp"
21 #include "AddressSpace.hpp"
22
23 namespace simgrid {
24 namespace mc {
25
26 typedef std::vector<Dwarf_Op> DwarfExpression;
27
28
29 /** \brief A DWARF expression with optional validity contraints */
30 class LocationListEntry {
31 public:
32   DwarfExpression expression;
33   void* lowpc, *highpc;
34
35   LocationListEntry() : lowpc(nullptr), highpc(nullptr) {}
36
37   bool valid_for_ip(unw_word_t ip)
38   {
39     return (this->lowpc == nullptr && this->highpc == nullptr)
40         || (ip >= (unw_word_t) this->lowpc
41             && ip < (unw_word_t) this->highpc);
42   }
43 };
44
45 typedef std::vector<LocationListEntry> LocationList;
46
47 }
48 }
49
50 SG_BEGIN_DECL()
51
52 /** A location is either a location in memory of a register location
53  *
54  *  Usage:
55  *
56  *    * mc_dwarf_resolve_locations or mc_dwarf_resolve_location is used
57  *      to find the location of a given location expression or location list;
58  *
59  *    * mc_get_location_type MUST be used to find the location type;
60  *
61  *    * for MC_LOCATION_TYPE_ADDRESS, memory_address is the resulting address
62  *
63  *    * for MC_LOCATION_TYPE_REGISTER, unw_get_reg(l.cursor, l.register_id, value)
64  *        and unw_get_reg(l.cursor, l.register_id, value) can be used to read/write
65  *        the value.
66  *  </ul>
67  */
68 typedef struct s_mc_location {
69   void* memory_location;
70   unw_cursor_t* cursor;
71   int register_id;
72 } s_mc_location_t, *mc_location_t;
73
74 /** Type of a given location
75  *
76  *  Use `mc_get_location_type(location)` to find the type.
77  * */
78 typedef enum mc_location_type {
79   MC_LOCATION_TYPE_ADDRESS,
80   MC_LOCATION_TYPE_REGISTER
81 } mc_location_type;
82
83 /** Find the type of a location */
84 static inline __attribute__ ((always_inline))
85 enum mc_location_type mc_get_location_type(mc_location_t location) {
86   if (location->cursor) {
87     return MC_LOCATION_TYPE_REGISTER;
88   } else {
89     return MC_LOCATION_TYPE_ADDRESS;
90   }
91 }
92
93 XBT_PRIVATE void mc_dwarf_resolve_location(
94   mc_location_t location, simgrid::mc::DwarfExpression* expression,
95   simgrid::mc::ObjectInformation* object_info, unw_cursor_t* c,
96   void* frame_pointer_address, simgrid::mc::AddressSpace* address_space,
97   int process_index);
98 void mc_dwarf_resolve_locations(
99   mc_location_t location, simgrid::mc::LocationList* locations,
100   simgrid::mc::ObjectInformation* object_info, unw_cursor_t* c,
101   void* frame_pointer_address, simgrid::mc::AddressSpace* address_space,
102   int process_index);
103
104 XBT_PRIVATE void mc_dwarf_location_list_init(
105   simgrid::mc::LocationList*, simgrid::mc::ObjectInformation* info, Dwarf_Die* die,
106   Dwarf_Attribute* attr);
107
108 #define MC_EXPRESSION_STACK_SIZE 64
109
110 #define MC_EXPRESSION_OK 0
111 #define MC_EXPRESSION_E_UNSUPPORTED_OPERATION 1
112 #define MC_EXPRESSION_E_STACK_OVERFLOW 2
113 #define MC_EXPRESSION_E_STACK_UNDERFLOW 3
114 #define MC_EXPRESSION_E_MISSING_STACK_CONTEXT 4
115 #define MC_EXPRESSION_E_MISSING_FRAME_BASE 5
116 #define MC_EXPRESSION_E_NO_BASE_ADDRESS 6
117
118 typedef struct s_mc_expression_state {
119   uintptr_t stack[MC_EXPRESSION_STACK_SIZE];
120   size_t stack_size;
121
122   unw_cursor_t* cursor;
123   void* frame_base;
124   simgrid::mc::AddressSpace* address_space;
125   simgrid::mc::ObjectInformation* object_info;
126   int process_index;
127 } s_mc_expression_state_t, *mc_expression_state_t;
128
129 XBT_PUBLIC(int) mc_dwarf_execute_expression(
130   size_t n, const Dwarf_Op* ops, mc_expression_state_t state);
131 void* mc_find_frame_base(
132   simgrid::mc::Frame* frame, simgrid::mc::ObjectInformation* object_info, unw_cursor_t* unw_cursor);
133
134 SG_END_DECL()
135
136 namespace simgrid {
137 namespace mc {
138
139 static inline
140 int execute(DwarfExpression const& expression, mc_expression_state_t state)
141 {
142   return mc_dwarf_execute_expression(
143     expression.size(), expression.data(), state);
144 }
145
146 }
147 }
148
149 #endif