Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a3b3bb5841c62c88fe0b2e9a7f0d34960a11f236
[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 MC_OBJECT_LOCATION_H
8 #define MC_OBJECT_LOCATION_H
9
10 #include <stdint.h>
11
12 #include <libunwind.h>
13 #include <dwarf.h>
14 #include <elfutils/libdw.h>
15
16 #include <simgrid_config.h>
17 #include "mc_base.h"
18 #include "mc_forward.h"
19 #include "AddressSpace.hpp"
20
21 SG_BEGIN_DECL()
22
23 /** \brief a DWARF expression with optional validity contraints */
24 typedef struct s_mc_expression {
25   size_t size;
26   Dwarf_Op* ops;
27   // Optional validity:
28   void* lowpc, *highpc;
29 } s_mc_expression_t, *mc_expression_t;
30
31 /** A location list (list of location expressions) */
32 typedef struct s_mc_location_list {
33   size_t size;
34   mc_expression_t locations;
35 } s_mc_location_list_t, *mc_location_list_t;
36
37 /** A location is either a location in memory of a register location
38  *
39  *  Usage:
40  *
41  *    * mc_dwarf_resolve_locations or mc_dwarf_resolve_location is used
42  *      to find the location of a given location expression or location list;
43  *
44  *    * mc_get_location_type MUST be used to find the location type;
45  *
46  *    * for MC_LOCATION_TYPE_ADDRESS, memory_address is the resulting address
47  *
48  *    * for MC_LOCATION_TYPE_REGISTER, unw_get_reg(l.cursor, l.register_id, value)
49  *        and unw_get_reg(l.cursor, l.register_id, value) can be used to read/write
50  *        the value.
51  *  </ul>
52  */
53 typedef struct s_mc_location {
54   void* memory_location;
55   unw_cursor_t* cursor;
56   int register_id;
57 } s_mc_location_t, *mc_location_t;
58
59 /** Type of a given location
60  *
61  *  Use `mc_get_location_type(location)` to find the type.
62  * */
63 typedef enum mc_location_type {
64   MC_LOCATION_TYPE_ADDRESS,
65   MC_LOCATION_TYPE_REGISTER
66 } mc_location_type;
67
68 /** Find the type of a location */
69 static inline __attribute__ ((always_inline))
70 enum mc_location_type mc_get_location_type(mc_location_t location) {
71   if (location->cursor) {
72     return MC_LOCATION_TYPE_REGISTER;
73   } else {
74     return MC_LOCATION_TYPE_ADDRESS;
75   }
76 }
77
78 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);
79 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);
80
81 XBT_INTERNAL void mc_dwarf_expression_clear(mc_expression_t expression);
82 XBT_INTERNAL void mc_dwarf_expression_init(mc_expression_t expression, size_t len, Dwarf_Op* ops);
83
84 XBT_INTERNAL void mc_dwarf_location_list_clear(mc_location_list_t list);
85
86 XBT_INTERNAL void mc_dwarf_location_list_init_from_expression(mc_location_list_t target, size_t len, Dwarf_Op* ops);
87 XBT_INTERNAL void mc_dwarf_location_list_init(mc_location_list_t target, mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr);
88
89 #define MC_EXPRESSION_STACK_SIZE 64
90
91 #define MC_EXPRESSION_OK 0
92 #define MC_EXPRESSION_E_UNSUPPORTED_OPERATION 1
93 #define MC_EXPRESSION_E_STACK_OVERFLOW 2
94 #define MC_EXPRESSION_E_STACK_UNDERFLOW 3
95 #define MC_EXPRESSION_E_MISSING_STACK_CONTEXT 4
96 #define MC_EXPRESSION_E_MISSING_FRAME_BASE 5
97 #define MC_EXPRESSION_E_NO_BASE_ADDRESS 6
98
99 typedef struct s_mc_expression_state {
100   uintptr_t stack[MC_EXPRESSION_STACK_SIZE];
101   size_t stack_size;
102
103   unw_cursor_t* cursor;
104   void* frame_base;
105   mc_address_space_t address_space;
106   mc_object_info_t object_info;
107   int process_index;
108 } s_mc_expression_state_t, *mc_expression_state_t;
109
110 MC_SHOULD_BE_INTERNAL int mc_dwarf_execute_expression(
111   size_t n, const Dwarf_Op* ops, mc_expression_state_t state);
112
113 MC_SHOULD_BE_INTERNAL void* mc_find_frame_base(
114   dw_frame_t frame, mc_object_info_t object_info, unw_cursor_t* unw_cursor);
115
116 SG_END_DECL()
117
118 #endif