Logo AND Algorithmique Numérique Distribuée

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