Logo AND Algorithmique Numérique Distribuée

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