1 /* Copyright (c) 2004-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #ifndef SIMGRID_MC_OBJECT_LOCATION_H
8 #define SIMGRID_MC_OBJECT_LOCATION_H
14 #include <libunwind.h>
16 #include <elfutils/libdw.h>
18 #include <simgrid_config.h>
20 #include "mc_forward.hpp"
21 #include "AddressSpace.hpp"
26 typedef std::vector<Dwarf_Op> DwarfExpression;
29 /** \brief A DWARF expression with optional validity contraints */
30 class LocationListEntry {
32 DwarfExpression expression;
35 LocationListEntry() : lowpc(nullptr), highpc(nullptr) {}
37 bool valid_for_ip(unw_word_t ip)
39 return (this->lowpc == nullptr && this->highpc == nullptr)
40 || (ip >= (unw_word_t) this->lowpc
41 && ip < (unw_word_t) this->highpc);
45 typedef std::vector<LocationListEntry> LocationList;
52 /** A location is either a location in memory of a register location
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;
59 * * mc_get_location_type MUST be used to find the location type;
61 * * for MC_LOCATION_TYPE_ADDRESS, memory_address is the resulting address
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
68 typedef struct s_mc_location {
69 void* memory_location;
72 } s_mc_location_t, *mc_location_t;
74 /** Type of a given location
76 * Use `mc_get_location_type(location)` to find the type.
78 typedef enum mc_location_type {
79 MC_LOCATION_TYPE_ADDRESS,
80 MC_LOCATION_TYPE_REGISTER
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;
89 return MC_LOCATION_TYPE_ADDRESS;
93 XBT_INTERNAL 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,
98 MC_SHOULD_BE_INTERNAL 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,
104 XBT_INTERNAL void mc_dwarf_location_list_init(
105 simgrid::mc::LocationList*, simgrid::mc::ObjectInformation* info, Dwarf_Die* die,
106 Dwarf_Attribute* attr);
108 #define MC_EXPRESSION_STACK_SIZE 64
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
118 typedef struct s_mc_expression_state {
119 uintptr_t stack[MC_EXPRESSION_STACK_SIZE];
122 unw_cursor_t* cursor;
124 simgrid::mc::AddressSpace* address_space;
125 simgrid::mc::ObjectInformation* object_info;
127 } s_mc_expression_state_t, *mc_expression_state_t;
129 MC_SHOULD_BE_INTERNAL int mc_dwarf_execute_expression(
130 size_t n, const Dwarf_Op* ops, mc_expression_state_t state);
132 MC_SHOULD_BE_INTERNAL void* mc_find_frame_base(
133 simgrid::mc::Frame* frame, simgrid::mc::ObjectInformation* object_info, unw_cursor_t* unw_cursor);
141 int execute(DwarfExpression const& expression, mc_expression_state_t state)
143 return mc_dwarf_execute_expression(
144 expression.size(), expression.data(), state);