Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Move ObjectInformation methods in mc_object_info.cpp
[simgrid.git] / src / mc / mc_object_info.cpp
1 /* Copyright (c) 2014-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 <stddef.h>
8
9 #include <xbt/dynar.h>
10
11 #include "mc_object_info.h"
12 #include "mc_private.h"
13
14 namespace simgrid {
15 namespace mc {
16
17 // Free functions
18
19 static void mc_frame_free(void* frame)
20 {
21   delete (simgrid::mc::Frame*)frame;
22 }
23
24 static void mc_type_free(void* t)
25 {
26   delete (simgrid::mc::Type*)t;
27 }
28
29 // Type
30
31 Type::Type()
32 {
33   this->type = 0;
34   this->id = 0;
35   this->byte_size = 0;
36   this->element_count = 0;
37   this->is_pointer_type = 0;
38   this->subtype = nullptr;
39   this->full_type = nullptr;
40 }
41
42 // Type
43
44 Variable::Variable()
45 {
46   this->dwarf_offset = 0;
47   this->global = 0;
48   this->type = nullptr;
49   this->address = nullptr;
50   this->start_scope = 0;
51   this->object_info = nullptr;
52 }
53
54 // Frame
55
56 Frame::Frame()
57 {
58   this->tag = 0;
59   this->low_pc = nullptr;
60   this->high_pc = nullptr;
61   this->id = 0;
62   this->abstract_origin_id = 0;
63   this->object_info = nullptr;
64 }
65
66 // ObjectInformations
67
68 ObjectInformation::ObjectInformation()
69 {
70   this->flags = 0;
71   this->file_name = nullptr;
72   this->start = nullptr;
73   this->end = nullptr;
74   this->start_exec = nullptr;
75   this->end_exec = nullptr;
76   this->start_rw = nullptr;
77   this->end_rw = nullptr;
78   this->start_ro = nullptr;
79   this->end_ro = nullptr;
80   this->subprograms = xbt_dict_new_homogeneous(mc_frame_free);
81   this->types = xbt_dict_new_homogeneous((void (*)(void *)) mc_type_free);
82   this->full_types_by_name = xbt_dict_new_homogeneous(NULL);
83   this->functions_index = nullptr;
84 }
85
86 ObjectInformation::~ObjectInformation()
87 {
88   xbt_free(this->file_name);
89   xbt_dict_free(&this->subprograms);
90   xbt_dict_free(&this->types);
91   xbt_dict_free(&this->full_types_by_name);
92   xbt_dynar_free(&this->functions_index);
93 }
94
95 /** Find the DWARF offset for this ELF object
96  *
97  *  An offset is applied to address found in DWARF:
98  *
99  *  <ul>
100  *    <li>for an executable obejct, addresses are virtual address
101  *        (there is no offset) i.e. \f$\text{virtual address} = \{dwarf address}\f$;</li>
102  *    <li>for a shared object, the addreses are offset from the begining
103  *        of the shared object (the base address of the mapped shared
104  *        object must be used as offset
105  *        i.e. \f$\text{virtual address} = \text{shared object base address}
106  *             + \text{dwarf address}\f$.</li>
107  *
108  */
109 void *ObjectInformation::base_address() const
110 {
111   if (this->executable())
112     return nullptr;
113
114   void *result = this->start_exec;
115   if (this->start_rw != NULL && result > (void *) this->start_rw)
116     result = this->start_rw;
117   if (this->start_ro != NULL && result > (void *) this->start_ro)
118     result = this->start_ro;
119   return result;
120 }
121
122 mc_frame_t ObjectInformation::find_function(const void *ip) const
123 {
124   xbt_dynar_t dynar = this->functions_index;
125   mc_function_index_item_t base =
126       (mc_function_index_item_t) xbt_dynar_get_ptr(dynar, 0);
127   int i = 0;
128   int j = xbt_dynar_length(dynar) - 1;
129   while (j >= i) {
130     int k = i + ((j - i) / 2);
131     if (ip < base[k].low_pc) {
132       j = k - 1;
133     } else if (ip >= base[k].high_pc) {
134       i = k + 1;
135     } else {
136       return base[k].function;
137     }
138   }
139   return nullptr;
140 }
141
142 simgrid::mc::Variable* ObjectInformation::find_variable(const char* name) const
143 {
144   for (simgrid::mc::Variable& variable : this->global_variables)
145     if(variable.name == name)
146       return &variable;
147   return nullptr;
148 }
149
150 }
151 }