ROSE  0.9.9.109
AsmFunctionIndex.h
1 #ifndef ROSE_AsmFunctionIndex_H
2 #define ROSE_AsmFunctionIndex_H
3 
4 #include <algorithm>
5 #include <ostream>
6 #include <vector>
7 
8 #include "callbacks.h"
9 #include "Diagnostics.h"
10 
11 namespace Rose {
12 namespace BinaryAnalysis {
13 
78  /**************************************************************************************************************************
79  * The main public members
80  **************************************************************************************************************************/
81 public:
82 
85  init();
86  }
87 
90  init();
91  add_functions(ast);
92  }
93 
94  virtual ~AsmFunctionIndex() {}
95 
97  virtual void add_function(SgAsmFunction*);
98 
102  virtual void add_functions(SgNode *ast);
103 
105  virtual void clear() {
106  functions.clear();
107  }
108 
110  virtual bool empty() const {
111  return functions.empty();
112  }
113 
115  virtual size_t size() const {
116  return functions.size();
117  }
118 
119  /**************************************************************************************************************************
120  * Functors for sorting
121  * These are expected to be commonly used, so we define them here for convenience. The generic sorting method is defined
122  * below.
123  **************************************************************************************************************************/
124 public:
125 
128  bool operator()(SgAsmFunction *a, SgAsmFunction *b) {
129  return a->get_entry_va() < b->get_entry_va();
130  }
131  bool unique(SgAsmFunction *a, SgAsmFunction *b) {
132  return a->get_entry_va() != b->get_entry_va();
133  }
134  };
135 
138  bool operator()(SgAsmFunction *a, SgAsmFunction *b) {
139  return val(a)<val(b);
140  }
141  bool unique(SgAsmFunction *a, SgAsmFunction *b) {
142  return val(a)!=val(b);
143  }
144  rose_addr_t val(SgAsmFunction *x) {
145  rose_addr_t lo;
146  x->get_extent(NULL, &lo);
147  return lo;
148  }
149  };
150 
153  bool operator()(SgAsmFunction *a, SgAsmFunction *b) {
154  return val(a) < val(b);
155  }
156  bool unique(SgAsmFunction *a, SgAsmFunction *b) {
157  return val(a) != val(b);
158  }
159  size_t val(SgAsmFunction *x) {
160  return SageInterface::querySubTree<SgAsmInstruction>(x).size();
161  }
162  };
163 
167  bool operator()(SgAsmFunction *a, SgAsmFunction *b) {
168  return val(a) < val(b);
169  }
170  bool unique(SgAsmFunction *a, SgAsmFunction *b) {
171  return val(a) != val(b);
172  }
173  size_t val(SgAsmFunction *x) {
174  AddressIntervalSet extent;
175  x->get_extent(&extent);
176  return extent.size();
177  }
178  };
179 
181  struct SortByName {
182  bool operator()(SgAsmFunction *a, SgAsmFunction *b) {
183  return a->get_name().compare(b->get_name()) < 0;
184  }
185  bool unique(SgAsmFunction *a, SgAsmFunction *b) {
186  return 0 != a->get_name().compare(b->get_name());
187  }
188  };
189 
190 
191 
192  /**************************************************************************************************************************
193  * Sorting methods
194  **************************************************************************************************************************/
195 public:
196 
202  template<class Comparator> AsmFunctionIndex& sort(Comparator comp, bool unique=false) {
203  std::stable_sort(functions.begin(), functions.end(), comp);
204  if (unique) {
205  Functions newlist;
206  for (Functions::iterator fi=functions.begin(); fi!=functions.end(); fi++) {
207  if (newlist.empty() || comp.unique(newlist.back(), *fi))
208  newlist.push_back(*fi);
209  }
210  if (newlist.size()!=functions.size())
211  functions = newlist;
212  }
213  return *this;
214  }
215 
219  AsmFunctionIndex& sort_by_entry_addr(bool unique=false) { return sort(SortByEntryAddr(), unique); }
220  AsmFunctionIndex& sort_by_begin_addr(bool unique=false) { return sort(SortByBeginAddr(), unique); }
221  AsmFunctionIndex& sort_by_ninsns(bool unique=false) { return sort(SortByInsnsSize(), unique); }
222  AsmFunctionIndex& sort_by_nbytes(bool unique=false) { return sort(SortByBytesSize(), unique); }
223  AsmFunctionIndex& sort_by_name(bool unique=false) { return sort(SortByName(), unique); }
228  std::reverse(functions.begin(), functions.end());
229  return *this;
230  }
231 
232  /**************************************************************************************************************************
233  * Footnotes
234  **************************************************************************************************************************/
235 public:
236  class Footnotes {
237  public:
238  Footnotes() {
239  set_footnote_title("== Footnotes ==");
240  }
241 
249  size_t add_footnote(const std::string &text);
250 
255  void change_footnote(size_t idx, const std::string &text);
256 
259  const std::string& get_footnote(size_t idx) const;
260 
263  size_t size() const { return footnotes.size(); }
264 
267  void set_footnote_title(const std::string &title);
268 
270  const std::string& get_footnote_title() const;
271 
274  void set_footnote_prefix(const std::string &prefix) { footnote_prefix = prefix; }
275 
277  const std::string& get_footnote_prefix() const { return footnote_prefix; }
278 
280  std::string get_footnote_name(size_t idx) const;
281 
283  void print(std::ostream&) const;
284 
286  friend std::ostream& operator<<(std::ostream &o, const Footnotes *footnotes) {
287  footnotes->print(o);
288  return o;
289  }
290 
291  protected:
292  std::vector<std::string> footnotes;
293  std::string footnote_prefix;
294  };
295 
296  /**************************************************************************************************************************
297  * Output methods
298  **************************************************************************************************************************/
299 public:
301  virtual void print(std::ostream&) const;
302  friend std::ostream& operator<<(std::ostream&, const AsmFunctionIndex&);
303 
304 
305 
306  /**************************************************************************************************************************
307  * Output callback base classes
308  **************************************************************************************************************************/
309 public:
310 
327  public:
329  struct GeneralArgs {
331  : index(index), output(output), footnotes(footnotes) {}
333  std::ostream &output;
335  };
336 
338  struct BeforeAfterArgs: public GeneralArgs {
339  BeforeAfterArgs(const AsmFunctionIndex *index, std::ostream &output, Footnotes *footnotes, int when)
340  : GeneralArgs(index, output, footnotes), when(when) {}
341  int when;
342  };
343 
346  struct HeadingArgs: public GeneralArgs {
347  HeadingArgs(const AsmFunctionIndex *index, std::ostream &output, Footnotes *footnotes, char sep='\0')
348  : GeneralArgs(index, output, footnotes), sep(sep) {}
349  char sep;
350  };
351 
353  struct DataArgs: public GeneralArgs {
354  DataArgs(const AsmFunctionIndex *index, std::ostream &output, Footnotes *footnotes, SgAsmFunction *func, size_t rowid)
355  : GeneralArgs(index, output, footnotes), func(func), rowid(rowid) {}
356  SgAsmFunction *func;
357  size_t rowid;
358  };
359 
363  OutputCallback(const std::string &name, size_t width, const std::string description="")
364  : name(name), desc(description), width(width), header_prefix(" "), separator_prefix(" "), data_prefix(" ") {
365  ASSERT_require(width>0 || name.empty());
366  }
367 
368  virtual ~OutputCallback() {}
369 
371  void set_prefix(const std::string &header, const std::string &separator=" ", const std::string &data=" ");
372 
375  virtual bool operator()(bool enabled, const BeforeAfterArgs&);
376 
379  virtual bool operator()(bool enabled, const HeadingArgs&);
380 
383  virtual bool operator()(bool enabled, const DataArgs&);
384 
385  protected:
386  std::string center(const std::string&, size_t width);
388  std::string name;
389  std::string desc;
390  size_t width;
391  std::string header_prefix;
392  std::string separator_prefix;
393  std::string data_prefix;
394  };
395 
396 
397 
398  /**************************************************************************************************************************
399  * Predefined output callbacks
400  **************************************************************************************************************************/
401 public:
402 
405  public:
406  RowIdCallback(): OutputCallback("Num", 4) {}
407  virtual bool operator()(bool enabled, const DataArgs&);
408  } rowIdCallback;
409 
412  public:
413  EntryAddrCallback(): OutputCallback("Entry-Addr", 10) {}
414  virtual bool operator()(bool enabled, const DataArgs&);
415  } entryAddrCallback;
416 
419  public:
420  BeginAddrCallback(): OutputCallback("Begin-Addr", 10) {}
421  virtual bool operator()(bool enabled, const DataArgs&);
422  } beginAddrCallback;
423 
426  public:
427  EndAddrCallback(): OutputCallback("End-Addr", 10) {}
428  virtual bool operator()(bool enabled, const DataArgs&);
429  } endAddrCallback;
430 
433  public:
434  SizeInsnsCallback(): OutputCallback("Insns", 5) {}
435  virtual bool operator()(bool enabled, const DataArgs&);
436  } sizeInsnsCallback;
437 
440  public:
441  SizeBytesCallback(): OutputCallback("Bytes", 6) {
442  set_prefix("/", "-", "/");
443  }
444  virtual bool operator()(bool enabled, const DataArgs&);
445  } sizeBytesCallback;
446 
449  public:
450  ReasonCallback(): OutputCallback("Reason", 1) {} // width will be overridden in the callback
451  virtual bool operator()(bool enabled, const HeadingArgs&);
452  virtual bool operator()(bool enabled, const DataArgs&);
453  } reasonCallback;
454 
457  public:
458  CallingConventionCallback(): OutputCallback("CallConv", 8) {}
459  virtual bool operator()(bool enabled, const DataArgs&);
460  } callingConventionCallback;
461 
464  public:
465  MayReturnCallback(): OutputCallback("Returns", 9) {}
466  virtual bool operator()(bool enabled, const DataArgs&);
467  } mayReturnCallback;
468 
471  public:
472  StackDeltaCallback(): OutputCallback("Stack", 9) {}
473  virtual bool operator()(bool enabled, const HeadingArgs&);
474  virtual bool operator()(bool enabled, const DataArgs&);
475  } stackDeltaCallback;
476 
478  class NameCallback: public OutputCallback {
479  public:
480  NameCallback(): OutputCallback("Name", 32) {}
481  virtual bool operator()(bool enabled, const DataArgs&);
482  } nameCallback;
483 
486  public:
487  FootnotesCallback(): OutputCallback("", 0) {} // not a table column
488  virtual bool operator()(bool enabled, const BeforeAfterArgs&);
489  } footnotesCallback;
490 
491 
492 
493  /**************************************************************************************************************************
494  * Miscellaneous
495  **************************************************************************************************************************/
496 protected:
497  typedef std::vector<SgAsmFunction*> Functions;
498  Functions functions;
501  virtual void init();
502 
505 };
506 
507 } // namespace
508 } // namespace
509 
510 #endif
std::string data_prefix
Character(s) to print before data cells.
void set_footnote_prefix(const std::string &prefix)
Set the footnote prefix string.
ROSE_Callbacks::List< OutputCallback > output_callbacks
List of callbacks to be invoked when printing columns.
void print(std::ostream &) const
Print non-empty footnotes.
virtual bool operator()(bool enabled, const DataArgs &)
Callback to print data for a table cell.
Functor for sorting by function entry virtual address.
void set_footnote_title(const std::string &title)
Change the footnote title string.
Print number of instructions in function.
std::string name
Column name used when printing table headers.
virtual bool operator()(bool enabled, const DataArgs &)
Callback to print data for a table cell.
std::string get_footnote_name(size_t idx) const
Generates a footnote name from a footnote index.
AsmFunctionIndex & sort_by_name(bool unique=false)
Specific sorting method.
std::string separator_prefix
Character(s) to print before line separators.
std::vector< std::string > footnotes
List of footnotes.
void change_footnote(size_t idx, const std::string &text)
Change the text associated with a footnote.
virtual bool operator()(bool enabled, const DataArgs &)
Callback to print data for a table cell.
Represents a synthesized function.
size_t size() const
Returns the number of footnotes.
Functor for sorting by number of instructions in function.
List of callback functors.
Definition: callbacks.h:80
Main namespace for the ROSE library.
const std::string & get_footnote(size_t idx) const
Get the string for a footnote.
char sep
If non-NUL, then print a line of these characters.
virtual bool operator()(bool enabled, const HeadingArgs &)
Callback to print a column heading.
AsmFunctionIndex()
Constructs an empty index.
AsmFunctionIndex & reverse()
Reverse the order of the functions.
AsmFunctionIndex & sort_by_begin_addr(bool unique=false)
Specific sorting method.
virtual bool empty() const
Determines if an index is empty.
virtual void init()
Initializes the callback lists.
std::string header_prefix
Character(s) to print before headings.
virtual bool operator()(bool enabled, const DataArgs &)
Callback to print data for a table cell.
Functions functions
Functions in index order.
AsmFunctionIndex & sort_by_entry_addr(bool unique=false)
Specific sorting method.
std::ostream & output
Stream to which index is being printed.
Functor for sorting by function beginning address.
const std::string & get_name() const
Property: Name.
const AsmFunctionIndex * index
Index object being printed.
size_t get_extent(AddressIntervalSet *emap=NULL, rose_addr_t *lo_addr=NULL, rose_addr_t *hi_addr=NULL, NodeSelector *selector=NULL)
Returns information about the function addresses.
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:8322
int when
Zero implies before table, one implies after table.
virtual void clear()
Clears the index.
virtual void add_function(SgAsmFunction *)
Adds a function to the end of this index.
const std::string & get_footnote_prefix() const
Get the footnote prefix string.
AsmFunctionIndex & sort_by_ninsns(bool unique=false)
Specific sorting method.
const std::string & get_footnote_title() const
Get the footnote title.
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 void add_functions(SgNode *ast)
Adds functions to this index.
virtual bool operator()(bool enabled, const HeadingArgs &)
Callback to print a column heading.
virtual size_t size() const
Returns the number of functions in the index.
rose_addr_t get_entry_va() const
Property: Primary entry address.
AsmFunctionIndex & sort(Comparator comp, bool unique=false)
Sort the functions in the index.
std::string center(const std::string &, size_t width)
Center s in a string of length width.
AsmFunctionIndex & sort_by_nbytes(bool unique=false)
Specific sorting method.
std::string footnote_prefix
String to emit before every footnote line.
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.
AsmFunctionIndex(SgNode *ast)
Constructs an index from an AST.
virtual bool operator()(bool enabled, const DataArgs &)
Callback to print data for a table cell.
Functions indexed by entry address.
Interval::Value size() const
Number of scalar elements represented.
Definition: IntervalSet.h:308
std::string desc
Optional description to appear in footnote.
friend std::ostream & operator<<(std::ostream &o, const Footnotes *footnotes)
Print non-empty footnotes.
Footnotes * footnotes
Footnotes (newly created for each index output).
virtual void print(std::ostream &) const
Prints a function index to an output stream.
size_t add_footnote(const std::string &text)
Adds a footnote to the table.
virtual bool operator()(bool enabled, const BeforeAfterArgs &)
Callback for before and after the table.
virtual bool operator()(bool enabled, const BeforeAfterArgs &)
Callback for before and after the table.
void set_prefix(const std::string &header, const std::string &separator=" ", const std::string &data=" ")
Set prefix characters.
Functor for sorting by number of bytes in function.