ROSE  0.9.9.109
DwarfLineMapper.h
1 #ifndef ROSE_DwarfLineMapper_H
2 #define ROSE_DwarfLineMapper_H
3 
4 namespace Rose {
5 namespace BinaryAnalysis {
6 
7 
14 public:
16  enum Direction {
17  ADDR2SRC = 0x01,
18  SRC2ADDR = 0x02,
19  BIDIRECTIONAL = 0x03
20  };
21 
25  struct SrcInfo {
26  SrcInfo(): file_id(Sg_File_Info::NULL_FILE_ID), line_num(0) {}
27  SrcInfo(int file_id, size_t line_num): file_id(file_id), line_num(line_num) {}
28  bool operator==(const SrcInfo &other) const { return file_id==other.file_id && line_num==other.line_num; }
29  bool operator<(const SrcInfo &other) const {
30  return file_id<other.file_id || (file_id==other.file_id && line_num<other.line_num);
31  }
32  void print(std::ostream &o) const { o <<Sg_File_Info::getFilenameFromID(file_id) <<":" <<line_num; }
33  int file_id;
34  size_t line_num;
35  };
36 
41  class RangeMapLocationInfo: public RangeMapValue<Extent, SrcInfo> {
42  public:
45  RangeMapLocationInfo(const SrcInfo &value): Super(value) {}
46  RangeMapLocationInfo split(const Extent &my_range, rose_addr_t new_end) {
47  assert(my_range.contains(Extent(new_end)));
48  return *this;
49  }
50  };
51 
53  typedef std::map<SrcInfo, ExtentMap> SourceAddressMap;
54 
55 public:
57  explicit DwarfLineMapper(Direction d=BIDIRECTIONAL): p_direction(d), up_to_date(true) { init(); }
58 
60  explicit DwarfLineMapper(SgNode *ast, Direction d=BIDIRECTIONAL) { init(ast, d); }
61 
64  void init(SgNode *ast, Direction d=BIDIRECTIONAL);
65 
68  void insert(SgNode *ast);
69 
71  void clear();
72 
78  void fix_holes(size_t max_hole_size=64);
79 
82  SrcInfo addr2src(rose_addr_t) const;
83 
85  ExtentMap src2addr(const SrcInfo&) const;
86 
89  rose_addr_t src2first_addr(const SrcInfo&) const;
90 
93  void print(std::ostream&) const;
94 
96  void print_addr2src(std::ostream&) const;
97 
99  void print_src2addr(std::ostream&) const;
100 
102  DwarfLineMapper& operator()(Direction d) { p_direction = d; return *this; }
103 
106  std::set<int> all_files() const;
107 
110  SrcInfo next_src(const SrcInfo &srcinfo = SrcInfo()) const;
111 
112 protected:
113  Direction p_direction; // Direction to use when printing
114  AddressSourceMap p_addr2src; // Forward mapping
115  mutable SourceAddressMap p_src2addr; // Reverse mapping
116  mutable bool up_to_date; // Is reverse mapping up-to-date?
117  virtual void visit(SgNode *node) ROSE_OVERRIDE;
118  void update() const; // update p_src2addr if necessary
119  void init(); // called by constructors
120 };
121 
122 std::ostream& operator<<(std::ostream&, const DwarfLineMapper::SrcInfo&);
123 std::ostream& operator<<(std::ostream&, const DwarfLineMapper&);
124 
125 } // namespace
126 } // namespace
127 
128 #endif
void print_src2addr(std::ostream &) const
Print the source-to-address mapping in a human-readable format.
Class for traversing the AST.
DwarfLineMapper(SgNode *ast, Direction d=BIDIRECTIONAL)
Create a new mapping using Dwarf information found in the specified part of the AST.
Scalar value type for a RangeMap.
Definition: rangemap.h:679
A contiguous range of values.
Definition: rangemap.h:50
DwarfLineMapper & operator()(Direction d)
Change print direction.
void clear()
Clear all mapping information.
Main namespace for the ROSE library.
void print(std::ostream &) const
Print the mapping.
Build bidirectional mapping between virtual addresses and source locations.
void init(SgNode *ast, Direction d=BIDIRECTIONAL)
Replace the current mapping with a new mapping.
SrcInfo addr2src(rose_addr_t) const
Given an address, return the (single) source location for that address.
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:8322
Direction
Mapping direction indicator.
SrcInfo next_src(const SrcInfo &srcinfo=SrcInfo()) const
Given a source position, return the next source position that has a mapping.
Build a mapping from virtual address to source location.
void insert(SgNode *ast)
Insert additional mapping information from an AST without first clearing existing mapping info...
void fix_holes(size_t max_hole_size=64)
Fill in small holes.
std::set< int > all_files() const
Return the set of all source files.
virtual void visit(SgNode *node) ROSE_OVERRIDE
this method is called at every traversed node.
DwarfLineMapper(Direction d=BIDIRECTIONAL)
Construct an empty line mapper.
bool contains(const Range &x, bool strict=false) const
Does this range contain the argument range?
Definition: rangemap.h:326
void print_addr2src(std::ostream &) const
Print the address-to-source mapping in a human-readable format.
ExtentMap src2addr(const SrcInfo &) const
Given a source location, return the addresses that are associated with the location.
Build a mapping from source location to virtual address.
rose_addr_t src2first_addr(const SrcInfo &) const
Given a source location, return the first address associated with the location.
Build mappings between source locations and virtual addresses.