HPCToolkit
code-ranges.cpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 #include <map>
48 using namespace std;
49 
50 #include "code-ranges.h"
51 #include "process-ranges.h"
52 
53 
54 /******************************************************************************
55  * forward declarations
56  *****************************************************************************/
57 
58 
59 /******************************************************************************
60  * types
61  *****************************************************************************/
62 
63 class CodeRange {
64 public:
65  CodeRange(const char *_name, void *_start, void *_end, long _offset,
66  DiscoverFnTy discover);
67  void Process();
68  bool Contains(void *addr);
69  DiscoverFnTy Discover() { return discover; }
70  void *Relocate(void *addr);
71  long Offset() { return offset; }
72 private:
73  const char *name;
74  void *start;
75  void *end;
76  long offset;
78 };
79 
80 typedef map<void*,CodeRange*> CodeRangeSet;
81 
82 
83 /******************************************************************************
84  * local variables
85  *****************************************************************************/
86 
88 
89 
90 /******************************************************************************
91  * interface operations
92  *****************************************************************************/
93 
94 // Free both the code_ranges map and the CodeRange objects in the map.
95 void
97 {
98  CodeRangeSet::iterator it;
99 
100  for (it = code_ranges.begin(); it != code_ranges.end(); it++) {
101  delete it->second;
102  }
103  code_ranges.clear();
104 }
105 
106 
107 long
109 {
110  CodeRangeSet::iterator it = code_ranges.lower_bound(addr);
111 
112  if (it != code_ranges.begin()) {
113  if (--it != code_ranges.begin()) {
114  CodeRange *r = (*it).second;
115  if (r->Contains(addr)) return r->Offset();
116  }
117  }
118  return 0L; //should never get here
119 }
120 
121 
122 bool
124 {
125  CodeRangeSet::iterator it = code_ranges.lower_bound(addr);
126 
127  if (it == code_ranges.begin() || it == code_ranges.end()) return false;
128  if (--it == code_ranges.begin()) return false;
129 
130  CodeRange *r = (*it).second;
131  return r->Contains(addr) & r->Discover();
132 }
133 
134 
135 void
136 new_code_range(const char *sname, void *start, void *end, long offset, DiscoverFnTy discover)
137 {
138  // FIXME: this leaks memory in the case that the map already
139  // contains an entry with the same start address.
140  code_ranges.insert(pair<void*,CodeRange*>
141  (start, new CodeRange(sname, start, end, offset, discover)));
142 }
143 
144 
145 void
147 {
149  CodeRangeSet::iterator it = code_ranges.begin();
150  for (; it != code_ranges.end(); it++) {
151  CodeRange *r = (*it).second;
152  r->Process();
153  }
154 }
155 
156 
157 /******************************************************************************
158  * private operations
159  *****************************************************************************/
160 
161 CodeRange::CodeRange(const char *sname, void *_start, void *_end, long _offset,
162  DiscoverFnTy _discover)
163 {
164  name = sname;
165  start = _start;
166  end = _end;
167  offset = _offset;
168  discover = _discover;
169 }
170 
171 void *
173 {
174  return (void *)(offset + (char *) addr);
175 }
176 
177 bool
179 {
180  return (addr >= start) && (addr < end);
181 }
182 
183 void
185 {
186  process_range(name, -offset, Relocate(start), Relocate(end), discover);
187 }
DiscoverFnTy Discover()
Definition: code-ranges.cpp:69
long offset_for_fn(void *addr)
CodeRange(const char *_name, void *_start, void *_end, long _offset, DiscoverFnTy discover)
void new_code_range(const char *sname, void *start, void *end, long offset, DiscoverFnTy discover)
void process_code_ranges()
void process_range_init(void)
void code_ranges_reinit(void)
Definition: code-ranges.cpp:96
void * start
Definition: code-ranges.cpp:74
bool consider_possible_fn_address(void *addr)
const char * name
Definition: code-ranges.cpp:73
DiscoverFnTy discover
Definition: code-ranges.cpp:77
void _start(void)
void * end
Definition: code-ranges.cpp:75
map< void *, CodeRange * > CodeRangeSet
Definition: code-ranges.cpp:80
DiscoverFnTy
Definition: code-ranges.h:52
bool Contains(void *addr)
void Process()
void process_range(const char *name, long offset, void *vstart, void *vend, DiscoverFnTy fn_discovery)
long Offset()
Definition: code-ranges.cpp:71
cct_addr_t * addr
Definition: cct.c:130
void * Relocate(void *addr)
static CodeRangeSet code_ranges
Definition: code-ranges.cpp:87