1 /* Copyright (c) 2004-2015. 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"
30 std::uint64_t number2;
34 typedef std::vector<DwarfInstruction> DwarfExpression;
36 /** \brief A DWARF expression with optional validity contraints */
37 class LocationListEntry {
39 DwarfExpression expression;
42 LocationListEntry() : lowpc(nullptr), highpc(nullptr) {}
44 bool valid_for_ip(unw_word_t ip)
46 return (this->lowpc == nullptr && this->highpc == nullptr)
47 || (ip >= (unw_word_t) this->lowpc
48 && ip < (unw_word_t) this->highpc);
52 typedef std::vector<LocationListEntry> LocationList;
59 /** A location is either a location in memory of a register location
63 * * mc_dwarf_resolve_locations or mc_dwarf_resolve_location is used
64 * to find the location of a given location expression or location list;
66 * * mc_get_location_type MUST be used to find the location type;
68 * * for MC_LOCATION_TYPE_ADDRESS, memory_address is the resulting address
70 * * for MC_LOCATION_TYPE_REGISTER, unw_get_reg(l.cursor, l.register_id, value)
71 * and unw_get_reg(l.cursor, l.register_id, value) can be used to read/write
75 typedef struct s_mc_location {
76 void* memory_location;
79 } s_mc_location_t, *mc_location_t;
81 /** Type of a given location
83 * Use `mc_get_location_type(location)` to find the type.
85 typedef enum mc_location_type {
86 MC_LOCATION_TYPE_ADDRESS,
87 MC_LOCATION_TYPE_REGISTER
90 /** Find the type of a location */
91 static inline __attribute__ ((always_inline))
92 enum mc_location_type mc_get_location_type(mc_location_t location) {
93 if (location->cursor) {
94 return MC_LOCATION_TYPE_REGISTER;
96 return MC_LOCATION_TYPE_ADDRESS;
100 XBT_INTERNAL void mc_dwarf_resolve_location(
101 mc_location_t location, simgrid::mc::DwarfExpression* expression,
102 simgrid::mc::ObjectInformation* object_info, unw_cursor_t* c,
103 void* frame_pointer_address, simgrid::mc::AddressSpace* address_space,
105 MC_SHOULD_BE_INTERNAL void mc_dwarf_resolve_locations(
106 mc_location_t location, simgrid::mc::LocationList* locations,
107 simgrid::mc::ObjectInformation* object_info, unw_cursor_t* c,
108 void* frame_pointer_address, simgrid::mc::AddressSpace* address_space,
111 #define MC_EXPRESSION_STACK_SIZE 64
113 #define MC_EXPRESSION_OK 0
114 #define MC_EXPRESSION_E_UNSUPPORTED_OPERATION 1
115 #define MC_EXPRESSION_E_STACK_OVERFLOW 2
116 #define MC_EXPRESSION_E_STACK_UNDERFLOW 3
117 #define MC_EXPRESSION_E_MISSING_STACK_CONTEXT 4
118 #define MC_EXPRESSION_E_MISSING_FRAME_BASE 5
119 #define MC_EXPRESSION_E_NO_BASE_ADDRESS 6
121 typedef struct s_mc_expression_state {
122 uintptr_t stack[MC_EXPRESSION_STACK_SIZE];
125 unw_cursor_t* cursor;
127 simgrid::mc::AddressSpace* address_space;
128 simgrid::mc::ObjectInformation* object_info;
130 } s_mc_expression_state_t, *mc_expression_state_t;
132 MC_SHOULD_BE_INTERNAL int mc_dwarf_execute_expression(
133 size_t n, const simgrid::mc::DwarfInstruction* ops, mc_expression_state_t state);
135 MC_SHOULD_BE_INTERNAL void* mc_find_frame_base(
136 simgrid::mc::Frame* frame, simgrid::mc::ObjectInformation* object_info, unw_cursor_t* unw_cursor);
144 int execute(DwarfExpression const& expression, mc_expression_state_t state)
146 return mc_dwarf_execute_expression(
147 expression.size(), expression.data(), state);