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 #include "mc_dwarf.hpp"
8 #include <mc/ObjectInformation.hpp>
9 #include <mc/LocationList.hpp>
14 /** Resolve a location expression */
16 simgrid::dwarf::DwarfExpression const& expression,
17 simgrid::mc::ObjectInformation* object_info,
19 void *frame_pointer_address,
20 simgrid::mc::AddressSpace* address_space, int process_index)
22 simgrid::dwarf::ExpressionContext context;
23 context.frame_base = frame_pointer_address;
25 context.address_space = address_space;
26 context.object_info = object_info;
27 context.process_index = process_index;
29 if (!expression.empty()
30 && expression[0].atom >= DW_OP_reg0
31 && expression[0].atom <= DW_OP_reg31) {
32 int dwarf_register = expression[0].atom - DW_OP_reg0;
34 "Missing frame context for register operation DW_OP_reg%i",
36 return Location(dwarf_register_to_libunwind(dwarf_register));
39 simgrid::dwarf::ExpressionStack stack;
40 simgrid::dwarf::execute(expression, context, stack);
41 return Location((void*) stack.top());
44 // TODO, move this in a method of LocationList
45 static simgrid::dwarf::DwarfExpression const* find_expression(
46 simgrid::dwarf::LocationList const& locations, unw_word_t ip)
48 for (simgrid::dwarf::LocationListEntry const& entry : locations)
49 if (entry.valid_for_ip(ip))
50 return &entry.expression;
55 simgrid::dwarf::LocationList const& locations,
56 simgrid::mc::ObjectInformation* object_info,
58 void *frame_pointer_address,
59 simgrid::mc::AddressSpace* address_space,
63 if (c && unw_get_reg(c, UNW_REG_IP, &ip))
64 xbt_die("Could not resolve IP");
65 simgrid::dwarf::DwarfExpression const* expression =
66 find_expression(locations, ip);
68 xbt_die("Could not resolve location");
69 return simgrid::dwarf::resolve(
70 *expression, object_info, c,
71 frame_pointer_address, address_space, process_index);
74 simgrid::dwarf::LocationList location_list(
75 simgrid::mc::ObjectInformation& info,
76 Dwarf_Attribute& attr)
78 simgrid::dwarf::LocationList locations;
79 std::ptrdiff_t offset = 0;
82 Dwarf_Addr base, start, end;
86 offset = dwarf_getlocations(
87 &attr, offset, &base, &start, &end, &ops, &len);
90 return std::move(locations);
91 else if (offset == -1)
92 xbt_die("Error while loading location list");
94 simgrid::dwarf::LocationListEntry entry;
95 entry.expression = simgrid::dwarf::DwarfExpression(ops, ops + len);
97 void *base_address = info.base_address();
99 // If start == 0, this is not a location list:
101 entry.lowpc = nullptr;
102 entry.highpc = nullptr;
104 entry.lowpc = (char *) base_address + start;
105 entry.highpc = (char *) base_address + end;
108 locations.push_back(std::move(entry));