Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
code simplification around simgrid::surf::NetCardImpl
[simgrid.git] / src / mc / LocationList.cpp
1 /* Copyright (c) 2004-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "src/mc/mc_dwarf.hpp"
8 #include "src/mc/ObjectInformation.hpp"
9 #include "src/mc/LocationList.hpp"
10
11 namespace simgrid {
12 namespace dwarf {
13
14 /** Resolve a location expression */
15 Location resolve(
16                       simgrid::dwarf::DwarfExpression const& expression,
17                       simgrid::mc::ObjectInformation* object_info,
18                       unw_cursor_t * c,
19                       void *frame_pointer_address,
20                       simgrid::mc::AddressSpace* address_space, int process_index)
21 {
22   simgrid::dwarf::ExpressionContext context;
23   context.frame_base = frame_pointer_address;
24   context.cursor = c;
25   context.address_space = address_space;
26   context.object_info = object_info;
27   context.process_index = process_index;
28
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;
33     xbt_assert(c,
34       "Missing frame context for register operation DW_OP_reg%i",
35       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(
46     simgrid::dwarf::LocationList const& locations, 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(
55   simgrid::dwarf::LocationList const& locations,
56   simgrid::mc::ObjectInformation* object_info,
57   unw_cursor_t * c,
58   void *frame_pointer_address,
59   simgrid::mc::AddressSpace* address_space,
60   int process_index)
61 {
62   unw_word_t ip = 0;
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);
67   if (!expression)
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);
72 }
73
74 simgrid::dwarf::LocationList location_list(
75   simgrid::mc::ObjectInformation& info,
76   Dwarf_Attribute& attr)
77 {
78   simgrid::dwarf::LocationList locations;
79   std::ptrdiff_t offset = 0;
80   while (1) {
81
82     Dwarf_Addr base, start, end;
83     Dwarf_Op *ops;
84     std::size_t len;
85
86     offset = dwarf_getlocations(
87       &attr, offset, &base, &start, &end, &ops, &len);
88
89     if (offset == 0)
90       return std::move(locations);
91     else if (offset == -1)
92       xbt_die("Error while loading location list");
93
94     simgrid::dwarf::LocationListEntry entry;
95     entry.expression = simgrid::dwarf::DwarfExpression(ops, ops + len);
96
97     void *base_address = info.base_address();
98
99     // If start == 0, this is not a location list:
100     if (start == 0) {
101       entry.lowpc  = nullptr;
102       entry.highpc = nullptr;
103     } else {
104       entry.lowpc  = (char *) base_address + start;
105       entry.highpc = (char *) base_address + end;
106     }
107
108     locations.push_back(std::move(entry));
109   }
110 }
111
112
113 }
114 }