HPCToolkit
variable-entries.cpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL: https://outreach.scidac.gov/svn/hpctoolkit/trunk/src/tool/hpcvarbounds/function-entries.cpp $
6 // $Id: function-entries.cpp 4099 2013-02-10 20:13:32Z krentel $
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-2017, 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 /******************************************************************************
48  * include files
49  *****************************************************************************/
50 
51 #include <stdio.h>
52 #include <unistd.h>
53 #include <string>
54 
55 #include "variable-entries.h"
56 #include "server.h"
57 
58 #define __STDC_FORMAT_MACROS
59 #include <inttypes.h>
60 
61 #include <map>
62 #include <set>
63 
64 using namespace std;
65 
66 
67 /******************************************************************************
68  * types
69  *****************************************************************************/
70 
71 class Variable {
72 public:
73  Variable(void *_address, long _size, string *_comment, bool _isvisible);
74  void AppendComment(const string *c);
75  void *address;
76  long size;
77  string *comment;
78  bool isvisible;
79  int operator<(Variable *right);
80 };
81 
82 
83 /******************************************************************************
84  * forward declarations
85  *****************************************************************************/
86 
87 // this functions are defined in main.cpp
88 // needs to move these stuff into a 3rd party file
89 extern int c_mode(void);
90 extern int server_mode(void);
91 
92 
93 static void new_variable_entry(void *addr, long size, string *comment, bool isvisible);
94 static void dump_variable_entry(void *addr, long size, const char *comment);
95 
96 
97 /******************************************************************************
98  * local variables
99  *****************************************************************************/
100 
101 typedef map<void*,Variable*> VariableSet;
102 
104 
105 static long num_entries_total = 0;
106 
107 
108 /******************************************************************************
109  * interface operations
110  *****************************************************************************/
111 
112 // Free the function_entries map, the Function objects in the map, the
113 // excluded_function_entries set and cbranges intervals.
114 //
115 void
117 {
118  VariableSet::iterator it;
119 
120  for (it = variable_entries.begin(); it != variable_entries.end(); it++) {
121  Variable *f = it->second;
122  delete f->comment;
123  delete f;
124  }
125  variable_entries.clear();
126  num_entries_total = 0;
127 }
128 
129 void
131 {
132  VariableSet::iterator i = variable_entries.begin();
133 
134  for (; i != variable_entries.end();) {
135  Variable *f = (*i).second;
136  ++i;
137 
138  const char *name = NULL;
139  if (f->comment) {
140  name = f->comment->c_str();
141  }
142  dump_variable_entry(f->address, f->size, name);
143  }
144 }
145 
146 
147 void
148 add_variable_entry(void *addr, long size, const string *comment, bool isvisible)
149 {
150  if(size == 0) return;
151  VariableSet::iterator it = variable_entries.find(addr);
152 
153  if (it == variable_entries.end()) {
154  new_variable_entry(addr, size, comment ? new string(*comment) : NULL,
155  isvisible);
156  } else {
157  Variable *f = (*it).second;
158  if (comment) {
159  f->AppendComment(comment);
160  } else if (f->comment) {
161  f->isvisible = true;
162  }
163  }
164 }
165 
166 
167 long
169 {
170  return (num_entries_total);
171 }
172 
173 
174 /******************************************************************************
175  * private operations
176  *****************************************************************************/
177 
178 //
179 // Write one function entry in one format: server, C or text.
180 //
181 static void
182 dump_variable_entry(void *addr, long size, const char *comment)
183 {
184  num_entries_total += 2;
185 
186  if (server_mode()) {
187  syserv_add_addr(addr, 2 * (long)variable_entries.size());
188  syserv_add_addr((void *)size, 2 * (long)variable_entries.size());
189  return;
190  }
191 
192  if (c_mode()) {
193  if (num_entries_total > 2) {
194  printf(",\n");
195  }
196  printf(" 0x%" PRIxPTR " /* %s */,\n", (uintptr_t) addr, comment);
197  printf(" %ld /* %s */", size, comment);
198  return;
199  }
200 
201  // default is text mode
202  printf("0x%" PRIxPTR " %s\n", (uintptr_t) addr, comment);
203  printf("%ld /* %s */\n", size, comment);
204 }
205 
206 
207 static void
208 new_variable_entry(void *addr, long size, string *comment, bool isvisible)
209 {
210  Variable *f = new Variable(addr, size, comment, isvisible);
211  variable_entries.insert(pair<void*, Variable*>(addr, f));
212 }
213 
214 
215 Variable::Variable(void *_address, long _size, string *_comment, bool _isvisible)
216 {
217  address = _address;
218  size = _size;
219  comment = _comment;
220  isvisible = _isvisible;
221 }
222 
223 
224 void
225 Variable::AppendComment(const string *c)
226 {
227  *comment = *comment + ", " + *c;
228 }
229 
230 
231 int
233 {
234  return this->address < right->address;
235 }
void dump_variables()
long num_variable_entries(void)
static void dump_variable_entry(void *addr, long size, const char *comment)
static void new_variable_entry(void *addr, long size, string *comment, bool isvisible)
void AppendComment(const string *c)
string * comment
void add_variable_entry(void *addr, long size, const string *comment, bool isvisible)
static VariableSet variable_entries
int operator<(Variable *right)
void syserv_add_addr(void *addr, long func_entry_map_size)
Definition: server.cpp:218
map< void *, Variable * > VariableSet
int c_mode(void)
Definition: main.cpp:202
void variable_entries_reinit(void)
#define NULL
Definition: ElfHelper.cpp:85
static long num_entries_total
cct_addr_t * addr
Definition: cct.c:130
bool operator<(const VMAInterval &x, const VMAInterval &y)
int server_mode(void)
Definition: main.cpp:208
Variable(void *_address, long _size, string *_comment, bool _isvisible)