Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Comment the binary search in ObjectInformation::find_function()
[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->type_id = 0;
39   this->subtype = nullptr;
40   this->full_type = nullptr;
41 }
42
43 // Type
44
45 Variable::Variable()
46 {
47   this->dwarf_offset = 0;
48   this->global = 0;
49   this->type = nullptr;
50   this->type_id = 0;
51   this->address = nullptr;
52   this->start_scope = 0;
53   this->object_info = nullptr;
54 }
55
56 // Frame
57
58 Frame::Frame()
59 {
60   this->tag = 0;
61   this->low_pc = nullptr;
62   this->high_pc = nullptr;
63   this->id = 0;
64   this->abstract_origin_id = 0;
65   this->object_info = nullptr;
66 }
67
68 // ObjectInformations
69
70 ObjectInformation::ObjectInformation()
71 {
72   this->flags = 0;
73   this->start = nullptr;
74   this->end = nullptr;
75   this->start_exec = nullptr;
76   this->end_exec = nullptr;
77   this->start_rw = nullptr;
78   this->end_rw = nullptr;
79   this->start_ro = nullptr;
80   this->end_ro = nullptr;
81 }
82
83 /** Find the DWARF offset for this ELF object
84  *
85  *  An offset is applied to address found in DWARF:
86  *
87  *  <ul>
88  *    <li>for an executable obejct, addresses are virtual address
89  *        (there is no offset) i.e. \f$\text{virtual address} = \{dwarf address}\f$;</li>
90  *    <li>for a shared object, the addreses are offset from the begining
91  *        of the shared object (the base address of the mapped shared
92  *        object must be used as offset
93  *        i.e. \f$\text{virtual address} = \text{shared object base address}
94  *             + \text{dwarf address}\f$.</li>
95  *
96  */
97 void *ObjectInformation::base_address() const
98 {
99   if (this->executable())
100     return nullptr;
101
102   void *result = this->start_exec;
103   if (this->start_rw != NULL && result > (void *) this->start_rw)
104     result = this->start_rw;
105   if (this->start_ro != NULL && result > (void *) this->start_ro)
106     result = this->start_ro;
107   return result;
108 }
109
110 /* Find a function by instruction pointer */
111 simgrid::mc::Frame* ObjectInformation::find_function(const void *ip) const
112 {
113   /* This is implemented by binary search on a sorted array.
114    *
115    * We do quite a lot ot those so we want this to be cache efficient.
116    * We pack the only information we need in the index entries in order
117    * to successfully do the binary search. We do not need the high_pc
118    * during the binary search (only at the end) so it is not included
119    * in the index entry. We could use parallel arrays as well.
120    *
121    * We cannot really use the std:: alogrithm for this.
122    * We could use std::binary_search by including the high_pc inside
123    * the FunctionIndexEntry.
124    */
125   const simgrid::mc::FunctionIndexEntry* base =
126     this->functions_index.data();
127   int i = 0;
128   int j = this->functions_index.size() - 1;
129   while (j >= i) {
130     int k = i + ((j - i) / 2);
131
132     /* In most of the search, we do not dereference the base[k].function.
133      * This way the memory accesses are located in the base[k] array. */
134     if (ip < base[k].low_pc)
135       j = k - 1;
136     else if (k < j && ip >= base[k + 1].low_pc)
137       i = k + 1;
138
139     /* At this point, the search is over.
140      * Either we have found the correct function or we do not know
141      * any function corresponding to this instruction address.
142      * Only at the point do we derefernce the function pointer. */
143     else if (ip < base[k].function->high_pc)
144       return base[k].function;
145     else
146       return nullptr;
147   }
148   return nullptr;
149 }
150
151 simgrid::mc::Variable* ObjectInformation::find_variable(const char* name) const
152 {
153   for (simgrid::mc::Variable& variable : this->global_variables)
154     if(variable.name == name)
155       return &variable;
156   return nullptr;
157 }
158
159 }
160 }