ROSE 0.11.145.147
AsmFunctionIndex.h
1#ifndef ROSE_BinaryAnalysis_AsmFunctionIndex_H
2#define ROSE_BinaryAnalysis_AsmFunctionIndex_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
5#include <RoseFirst.h>
6
7#include <Rose/Diagnostics.h>
8#include <callbacks.h> // rose
9
10#include <SgAsmFunction.h>
11
12#include <algorithm>
13#include <ostream>
14#include <vector>
15
16namespace Rose {
17namespace BinaryAnalysis {
18
83 /**************************************************************************************************************************
84 * The main public members
85 **************************************************************************************************************************/
86public:
87
90 init();
91 }
92
95 init();
96 add_functions(ast);
97 }
98
99 virtual ~AsmFunctionIndex() {}
100
103
107 virtual void add_functions(SgNode *ast);
108
110 virtual void clear() {
111 functions.clear();
112 }
113
115 virtual bool empty() const {
116 return functions.empty();
117 }
118
120 virtual size_t size() const {
121 return functions.size();
122 }
123
124 /**************************************************************************************************************************
125 * Functors for sorting
126 * These are expected to be commonly used, so we define them here for convenience. The generic sorting method is defined
127 * below.
128 **************************************************************************************************************************/
129public:
130
133 bool operator()(SgAsmFunction *a, SgAsmFunction *b) {
134 return a->get_entryVa() < b->get_entryVa();
135 }
136 bool unique(SgAsmFunction *a, SgAsmFunction *b) {
137 return a->get_entryVa() != b->get_entryVa();
138 }
139 };
140
143 bool operator()(SgAsmFunction *a, SgAsmFunction *b) {
144 return val(a)<val(b);
145 }
146 bool unique(SgAsmFunction *a, SgAsmFunction *b) {
147 return val(a)!=val(b);
148 }
149 rose_addr_t val(SgAsmFunction *x) {
150 rose_addr_t lo;
151 x->get_extent(NULL, &lo);
152 return lo;
153 }
154 };
155
158 bool operator()(SgAsmFunction *a, SgAsmFunction *b) {
159 return val(a) < val(b);
160 }
161 bool unique(SgAsmFunction *a, SgAsmFunction *b) {
162 return val(a) != val(b);
163 }
164 size_t val(SgAsmFunction*);
165 };
166
170 bool operator()(SgAsmFunction *a, SgAsmFunction *b) {
171 return val(a) < val(b);
172 }
173 bool unique(SgAsmFunction *a, SgAsmFunction *b) {
174 return val(a) != val(b);
175 }
176 size_t val(SgAsmFunction *x) {
177 AddressIntervalSet extent;
178 x->get_extent(&extent);
179 return extent.size();
180 }
181 };
182
184 struct SortByName {
185 bool operator()(SgAsmFunction *a, SgAsmFunction *b) {
186 return a->get_name().compare(b->get_name()) < 0;
187 }
188 bool unique(SgAsmFunction *a, SgAsmFunction *b) {
189 return 0 != a->get_name().compare(b->get_name());
190 }
191 };
192
193
194
195 /**************************************************************************************************************************
196 * Sorting methods
197 **************************************************************************************************************************/
198public:
199
205 template<class Comparator> AsmFunctionIndex& sort(Comparator comp, bool unique=false) {
206 std::stable_sort(functions.begin(), functions.end(), comp);
207 if (unique) {
208 Functions newlist;
209 for (Functions::iterator fi=functions.begin(); fi!=functions.end(); fi++) {
210 if (newlist.empty() || comp.unique(newlist.back(), *fi))
211 newlist.push_back(*fi);
212 }
213 if (newlist.size()!=functions.size())
214 functions = newlist;
215 }
216 return *this;
217 }
218
222 AsmFunctionIndex& sort_by_entry_addr(bool unique=false) { return sort(SortByEntryAddr(), unique); }
223 AsmFunctionIndex& sort_by_begin_addr(bool unique=false) { return sort(SortByBeginAddr(), unique); }
224 AsmFunctionIndex& sort_by_ninsns(bool unique=false) { return sort(SortByInsnsSize(), unique); }
225 AsmFunctionIndex& sort_by_nbytes(bool unique=false) { return sort(SortByBytesSize(), unique); }
226 AsmFunctionIndex& sort_by_name(bool unique=false) { return sort(SortByName(), unique); }
231 std::reverse(functions.begin(), functions.end());
232 return *this;
233 }
234
235 /**************************************************************************************************************************
236 * Footnotes
237 **************************************************************************************************************************/
238public:
239 class Footnotes {
240 public:
241 Footnotes() {
242 set_footnote_title("== Footnotes ==");
243 }
244
252 size_t add_footnote(const std::string &text);
253
258 void change_footnote(size_t idx, const std::string &text);
259
262 const std::string& get_footnote(size_t idx) const;
263
266 size_t size() const { return footnotes.size(); }
267
270 void set_footnote_title(const std::string &title);
271
273 const std::string& get_footnote_title() const;
274
277 void set_footnote_prefix(const std::string &prefix) { footnote_prefix = prefix; }
278
280 const std::string& get_footnote_prefix() const { return footnote_prefix; }
281
283 std::string get_footnote_name(size_t idx) const;
284
286 void print(std::ostream&) const;
287
289 friend std::ostream& operator<<(std::ostream &o, const Footnotes *footnotes) {
290 footnotes->print(o);
291 return o;
292 }
293
294 protected:
295 std::vector<std::string> footnotes;
296 std::string footnote_prefix;
297 };
298
299 /**************************************************************************************************************************
300 * Output methods
301 **************************************************************************************************************************/
302public:
304 virtual void print(std::ostream&) const;
305 friend std::ostream& operator<<(std::ostream&, const AsmFunctionIndex&);
306
307
308
309 /**************************************************************************************************************************
310 * Output callback base classes
311 **************************************************************************************************************************/
312public:
313
330 public:
339
344 int when;
345 };
346
349 struct HeadingArgs: public GeneralArgs {
350 HeadingArgs(const AsmFunctionIndex *index, std::ostream &output, Footnotes *footnotes, char sep='\0')
352 char sep;
353 };
354
356 struct DataArgs: public GeneralArgs {
357 DataArgs(const AsmFunctionIndex *index, std::ostream &output, Footnotes *footnotes, SgAsmFunction *func, size_t rowid)
358 : GeneralArgs(index, output, footnotes), func(func), rowid(rowid) {}
359 SgAsmFunction *func;
360 size_t rowid;
361 };
362
366 OutputCallback(const std::string &name, size_t width, const std::string description="")
367 : name(name), desc(description), width(width), header_prefix(" "), separator_prefix(" "), data_prefix(" ") {
368 ASSERT_require(width>0 || name.empty());
369 }
370
371 virtual ~OutputCallback() {}
372
374 void set_prefix(const std::string &header, const std::string &separator=" ", const std::string &data=" ");
375
378 virtual bool operator()(bool enabled, const BeforeAfterArgs&);
379
382 virtual bool operator()(bool enabled, const HeadingArgs&);
383
386 virtual bool operator()(bool enabled, const DataArgs&);
387
388 protected:
389 std::string center(const std::string&, size_t width);
391 std::string name;
392 std::string desc;
393 size_t width;
394 std::string header_prefix;
395 std::string separator_prefix;
396 std::string data_prefix;
397 };
398
399
400
401 /**************************************************************************************************************************
402 * Predefined output callbacks
403 **************************************************************************************************************************/
404public:
405
408 public:
409 RowIdCallback(): OutputCallback("Num", 4) {}
410 virtual bool operator()(bool enabled, const DataArgs&);
411 } rowIdCallback;
412
415 public:
416 EntryAddrCallback(): OutputCallback("Entry-Addr", 10) {}
417 virtual bool operator()(bool enabled, const DataArgs&);
418 } entryAddrCallback;
419
422 public:
423 BeginAddrCallback(): OutputCallback("Begin-Addr", 10) {}
424 virtual bool operator()(bool enabled, const DataArgs&);
425 } beginAddrCallback;
426
429 public:
430 EndAddrCallback(): OutputCallback("End-Addr", 10) {}
431 virtual bool operator()(bool enabled, const DataArgs&);
432 } endAddrCallback;
433
436 public:
437 SizeInsnsCallback(): OutputCallback("Insns", 5) {}
438 virtual bool operator()(bool enabled, const DataArgs&);
439 } sizeInsnsCallback;
440
443 public:
444 SizeBytesCallback(): OutputCallback("Bytes", 6) {
445 set_prefix("/", "-", "/");
446 }
447 virtual bool operator()(bool enabled, const DataArgs&);
448 } sizeBytesCallback;
449
452 public:
453 ReasonCallback(): OutputCallback("Reason", 1) {} // width will be overridden in the callback
454 virtual bool operator()(bool enabled, const HeadingArgs&);
455 virtual bool operator()(bool enabled, const DataArgs&);
456 } reasonCallback;
457
460 public:
461 CallingConventionCallback(): OutputCallback("CallConv", 8) {}
462 virtual bool operator()(bool enabled, const DataArgs&);
463 } callingConventionCallback;
464
467 public:
468 MayReturnCallback(): OutputCallback("Returns", 9) {}
469 virtual bool operator()(bool enabled, const DataArgs&);
470 } mayReturnCallback;
471
474 public:
475 StackDeltaCallback(): OutputCallback("Stack", 9) {}
476 virtual bool operator()(bool enabled, const HeadingArgs&);
477 virtual bool operator()(bool enabled, const DataArgs&);
478 } stackDeltaCallback;
479
482 public:
483 NameCallback(): OutputCallback("Name", 32) {}
484 virtual bool operator()(bool enabled, const DataArgs&);
485 } nameCallback;
486
489 public:
490 FootnotesCallback(): OutputCallback("", 0) {} // not a table column
491 virtual bool operator()(bool enabled, const BeforeAfterArgs&);
492 } footnotesCallback;
493
494
495
496 /**************************************************************************************************************************
497 * Miscellaneous
498 **************************************************************************************************************************/
499protected:
500 typedef std::vector<SgAsmFunction*> Functions;
501 Functions functions;
504 virtual void init();
505
508};
509
510} // namespace
511} // namespace
512
513#endif
514#endif
virtual bool operator()(bool enabled, const DataArgs &)
Callback to print data for a table cell.
virtual bool operator()(bool enabled, const DataArgs &)
Callback to print data for a table cell.
virtual bool operator()(bool enabled, const DataArgs &)
Callback to print data for a table cell.
virtual bool operator()(bool enabled, const DataArgs &)
Callback to print data for a table cell.
virtual bool operator()(bool enabled, const BeforeAfterArgs &)
Callback for before and after the table.
size_t size() const
Returns the number of footnotes.
void set_footnote_title(const std::string &title)
Change the footnote title string.
size_t add_footnote(const std::string &text)
Adds a footnote to the table.
const std::string & get_footnote(size_t idx) const
Get the string for a footnote.
friend std::ostream & operator<<(std::ostream &o, const Footnotes *footnotes)
Print non-empty footnotes.
void change_footnote(size_t idx, const std::string &text)
Change the text associated with a footnote.
const std::string & get_footnote_title() const
Get the footnote title.
void print(std::ostream &) const
Print non-empty footnotes.
std::string get_footnote_name(size_t idx) const
Generates a footnote name from a footnote index.
std::string footnote_prefix
String to emit before every footnote line.
const std::string & get_footnote_prefix() const
Get the footnote prefix string.
void set_footnote_prefix(const std::string &prefix)
Set the footnote prefix string.
std::vector< std::string > footnotes
List of footnotes.
virtual bool operator()(bool enabled, const DataArgs &)
Callback to print data for a table cell.
virtual bool operator()(bool enabled, const DataArgs &)
Callback to print data for a table cell.
std::string name
Column name used when printing table headers.
std::string desc
Optional description to appear in footnote.
std::string header_prefix
Character(s) to print before headings.
std::string separator_prefix
Character(s) to print before line separators.
void set_prefix(const std::string &header, const std::string &separator=" ", const std::string &data=" ")
Set prefix characters.
std::string center(const std::string &, size_t width)
Center s in a string of length width.
std::string data_prefix
Character(s) to print before data cells.
OutputCallback(const std::string &name, size_t width, const std::string description="")
Constructor.
virtual bool operator()(bool enabled, const DataArgs &)
Callback to print data for a table cell.
size_t width
Minimum width of column header or data.
virtual bool operator()(bool enabled, const HeadingArgs &)
Callback to print a column heading.
virtual bool operator()(bool enabled, const BeforeAfterArgs &)
Callback for before and after the table.
virtual bool operator()(bool enabled, const HeadingArgs &)
Callback to print a column heading.
virtual bool operator()(bool enabled, const DataArgs &)
Callback to print data for a table cell.
virtual bool operator()(bool enabled, const DataArgs &)
Callback to print data for a table cell.
virtual bool operator()(bool enabled, const DataArgs &)
Callback to print data for a table cell.
virtual bool operator()(bool enabled, const DataArgs &)
Callback to print data for a table cell.
virtual bool operator()(bool enabled, const HeadingArgs &)
Callback to print a column heading.
virtual bool operator()(bool enabled, const DataArgs &)
Callback to print data for a table cell.
Functions indexed by entry address.
virtual void add_function(SgAsmFunction *)
Adds a function to the end of this index.
AsmFunctionIndex()
Constructs an empty index.
virtual void init()
Initializes the callback lists.
AsmFunctionIndex & sort_by_name(bool unique=false)
Specific sorting method.
virtual void print(std::ostream &) const
Prints a function index to an output stream.
AsmFunctionIndex & sort_by_ninsns(bool unique=false)
Specific sorting method.
AsmFunctionIndex & sort_by_nbytes(bool unique=false)
Specific sorting method.
AsmFunctionIndex & sort_by_begin_addr(bool unique=false)
Specific sorting method.
AsmFunctionIndex & reverse()
Reverse the order of the functions.
virtual size_t size() const
Returns the number of functions in the index.
virtual bool empty() const
Determines if an index is empty.
Callbacks::List< OutputCallback > output_callbacks
List of callbacks to be invoked when printing columns.
Functions functions
Functions in index order.
AsmFunctionIndex(SgNode *ast)
Constructs an index from an AST.
virtual void clear()
Clears the index.
virtual void add_functions(SgNode *ast)
Adds functions to this index.
AsmFunctionIndex & sort_by_entry_addr(bool unique=false)
Specific sorting method.
AsmFunctionIndex & sort(Comparator comp, bool unique=false)
Sort the functions in the index.
List of callback functors.
Definition callbacks.h:83
Interval::Value size() const
Number of scalar elements represented.
Represents a synthesized function.
rose_addr_t const & get_entryVa() const
Property: Primary entry address.
size_t get_extent(Rose::BinaryAnalysis::AddressIntervalSet *emap=NULL, rose_addr_t *lo_addr=NULL, rose_addr_t *hi_addr=NULL, NodeSelector *selector=NULL)
Returns information about the function addresses.
std::string const & get_name() const
Property: Name.
This class represents the base class for all IR nodes within Sage III.
The ROSE library.
int when
Zero implies before table, one implies after table.
const AsmFunctionIndex * index
Index object being printed.
std::ostream & output
Stream to which index is being printed.
Footnotes * footnotes
Footnotes (newly created for each index output).
char sep
If non-NUL, then print a line of these characters.
Functor for sorting by function beginning address.
Functor for sorting by number of bytes in function.
Functor for sorting by function entry virtual address.
Functor for sorting by number of instructions in function.