Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ce194386bfb9b75947477575f5dfa5c25e03f2b3
[simgrid.git] / src / mc / inspect / LocationList.cpp
1 /* Copyright (c) 2004-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/mc/inspect/LocationList.hpp"
7 #include "src/mc/inspect/ObjectInformation.hpp"
8 #include "src/mc/inspect/mc_dwarf.hpp"
9
10 #include "xbt/asserts.h"
11 #include "xbt/sysdep.h"
12
13 #include <cstddef>
14 #include <cstdint>
15 #include <libunwind.h>
16 #include <utility>
17
18 namespace simgrid {
19 namespace dwarf {
20
21 /** Resolve a location expression */
22 Location resolve(simgrid::dwarf::DwarfExpression const& expression, simgrid::mc::ObjectInformation* object_info,
23                  unw_cursor_t* c, void* frame_pointer_address, simgrid::mc::AddressSpace* address_space,
24                  int process_index)
25 {
26   simgrid::dwarf::ExpressionContext context;
27   context.frame_base    = frame_pointer_address;
28   context.cursor        = c;
29   context.address_space = address_space;
30   context.object_info   = object_info;
31   context.process_index = process_index;
32
33   if (not expression.empty() && expression[0].atom >= DW_OP_reg0 && expression[0].atom <= DW_OP_reg31) {
34     int dwarf_register = expression[0].atom - DW_OP_reg0;
35     xbt_assert(c, "Missing frame context for register operation DW_OP_reg%i", dwarf_register);
36     return Location(dwarf_register_to_libunwind(dwarf_register));
37   }
38
39   simgrid::dwarf::ExpressionStack stack;
40   simgrid::dwarf::execute(expression, context, stack);
41   return Location((void*)stack.top());
42 }
43
44 // TODO, move this in a method of LocationList
45 static simgrid::dwarf::DwarfExpression const* find_expression(simgrid::dwarf::LocationList const& locations,
46                                                               unw_word_t ip)
47 {
48   for (simgrid::dwarf::LocationListEntry const& entry : locations)
49     if (entry.valid_for_ip(ip))
50       return &entry.expression();
51   return nullptr;
52 }
53
54 Location resolve(simgrid::dwarf::LocationList const& locations, simgrid::mc::ObjectInformation* object_info,
55                  unw_cursor_t* c, void* frame_pointer_address, simgrid::mc::AddressSpace* address_space,
56                  int process_index)
57 {
58   unw_word_t ip = 0;
59   if (c && unw_get_reg(c, UNW_REG_IP, &ip))
60     xbt_die("Could not resolve IP");
61   simgrid::dwarf::DwarfExpression const* expression = find_expression(locations, ip);
62   if (not expression)
63     xbt_die("Could not resolve location");
64   return simgrid::dwarf::resolve(*expression, object_info, c, frame_pointer_address, address_space, process_index);
65 }
66
67 LocationList location_list(simgrid::mc::ObjectInformation& info, Dwarf_Attribute& attr)
68 {
69   LocationList locations;
70   std::ptrdiff_t offset = 0;
71   while (1) {
72     Dwarf_Addr base;
73     Dwarf_Addr start;
74     Dwarf_Addr end;
75     Dwarf_Op* ops;
76     std::size_t len;
77
78     offset = dwarf_getlocations(&attr, offset, &base, &start, &end, &ops, &len);
79
80     if (offset == 0)
81       break;
82     else if (offset == -1)
83       xbt_die("Error while loading location list");
84
85     std::uint64_t base_address = (std::uint64_t)info.base_address();
86
87     LocationListEntry::range_type range;
88     if (start == 0)
89       // If start == 0, this is not a location list:
90       range = {0, UINT64_MAX};
91     else
92       range = {base_address + start, base_address + end};
93
94     locations.push_back({DwarfExpression(ops, ops + len), range});
95   }
96
97   return locations;
98 }
99 } // namespace dwarf
100 } // namespace simgrid