ROSE  0.11.25.0
DwarfLineMapper.h
1 #ifndef ROSE_DwarfLineMapper_H
2 #define ROSE_DwarfLineMapper_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 namespace Rose {
7 namespace BinaryAnalysis {
8 
9 // [Robb Matzke 2020-02-07]: Deprecated. Use Rose::BinaryAnalysis::SourceLocations from <BinarySourceLocations.h> instead.
10 
11 
12 /* Build mappings between source locations and virtual addresses.
13  *
14  * This class reads DWARF debugging information from the AST and builds a bidirectional mapping between virtual addresses and
15  * source locations. An address is associated with at most one source location, but a source location can have many
16  * addresses. This class is most efficient when the many addresses of a single source location are mostly contiguous. */
18 public:
19  /* Mapping direction indicator. */
20  enum Direction {
21  ADDR2SRC = 0x01, /* Build a mapping from virtual address to source location. */
22  SRC2ADDR = 0x02, /* Build a mapping from source location to virtual address. */
23  BIDIRECTIONAL = 0x03 /* Build bidirectional mapping between virtual addresses and source locations. */
24  };
25 
26  /* Source location info. We use a struct rather than an SgAsmDwarfLine pointer because we might have many SgAsmDwarfLine
27  * objects that all have the same file and line number (because they need different SgNode::p_parent values). This makes
28  * it easier to use the file and line numbers as std::map keys and RangeMap values. */
29  struct SrcInfo {
30  SrcInfo(): file_id(Sg_File_Info::NULL_FILE_ID), line_num(0) {}
31  SrcInfo(int file_id, size_t line_num): file_id(file_id), line_num(line_num) {}
32  bool operator==(const SrcInfo &other) const { return file_id==other.file_id && line_num==other.line_num; }
33  bool operator<(const SrcInfo &other) const {
34  return file_id<other.file_id || (file_id==other.file_id && line_num<other.line_num);
35  }
36  void print(std::ostream &o) const { o <<Sg_File_Info::getFilenameFromID(file_id) <<":" <<line_num; }
37  int file_id;
38  size_t line_num;
39  };
40 
41  /* Value stored by the addr2src RangeMap.
42  *
43  * A RangeMap is used to represent the mapping from addresses to source position because it is able to efficiently
44  * represent cases when one source position corresponds to a large number of contiguous addresses. */
45  class RangeMapLocationInfo: public RangeMapValue<Extent, SrcInfo> {
46  public:
49  RangeMapLocationInfo(const SrcInfo &value): Super(value) {}
50  RangeMapLocationInfo split(const Extent &my_range, rose_addr_t new_end) {
51  assert(my_range.contains(Extent(new_end)));
52  return *this;
53  }
54  };
55 
57  typedef std::map<SrcInfo, ExtentMap> SourceAddressMap;
58 
59 public:
60  /* Construct an empty line mapper. */
61  explicit DwarfLineMapper(Direction d=BIDIRECTIONAL) ROSE_DEPRECATED("use SourceLocations")
62  : p_direction(d), up_to_date(true) { init(); }
63 
64  /* Create a new mapping using Dwarf information found in the specified part of the AST. */
65  explicit DwarfLineMapper(SgNode *ast, Direction d=BIDIRECTIONAL) ROSE_DEPRECATED("use SourceLocations") {
66  init(ast, d);
67  }
68 
69  /* Replace the current mapping with a new mapping. The new mapping is constructed from Dwarf information found in the
70  * specified part of the AST. */
71  void init(SgNode *ast, Direction d=BIDIRECTIONAL) ROSE_DEPRECATED("use SourceLocations");
72 
73  /* Insert additional mapping information from an AST without first clearing existing mapping info. Conflicts are resolved
74  * in favor of the new information (since an address can be associated with at most one source position). */
75  void insert(SgNode *ast) ROSE_DEPRECATED("use SourceLocations");
76 
77  /* Clear all mapping information. */
78  void clear() ROSE_DEPRECATED("use SourceLocations");
79 
80  /* Fill in small holes. The dwarf line information stored in a file typically maps source location to only the first
81  * virtual address for that source location. This method fills in the mapping so that any unmapped virtual address within
82  * a certain delta of a previous mapped address will map to the same source location as the previous mapped address.
83  * Mappings will be added only for addresses where the distance between the next lower mapped address and the next higher
84  * mapped address is less than or equal to @p max_hole_size. */
85  void fix_holes(size_t max_hole_size=64) ROSE_DEPRECATED("use SourceLocations");
86 
87  /* Given an address, return the (single) source location for that address. If the specified address is not in the domain
88  * of this mapping function, then return the pair (Sg_File_Info::NULL_FILE_ID,0). */
89  SrcInfo addr2src(rose_addr_t) const ROSE_DEPRECATED("use SourceLocations");
90 
91  /* Given a source location, return the addresses that are associated with the location. */
92  ExtentMap src2addr(const SrcInfo&) const ROSE_DEPRECATED("use SourceLocations");
93 
94  /* Given a source location, return the first address associated with the location. Returns zero (null address) if
95  * no address is associated with that location. See also src2addr(). */
96  rose_addr_t src2first_addr(const SrcInfo&) const ROSE_DEPRECATED("use SourceLocations");
97 
98  /* Print the mapping. The forward (addr2src) and/or reverse (src2addr) mapping is printed, with each map entry terminated
99  * by a linefeed. */
100  void print(std::ostream&) const ROSE_DEPRECATED("use SourceLocations");
101 
102  /* Print the address-to-source mapping in a human-readable format. */
103  void print_addr2src(std::ostream&) const ROSE_DEPRECATED("use SourceLocations");
104 
105  /* Print the source-to-address mapping in a human-readable format. */
106  void print_src2addr(std::ostream&) const ROSE_DEPRECATED("use SourceLocations");
107 
108  /* Change print direction. */
109  DwarfLineMapper& operator()(Direction d) ROSE_DEPRECATED("use SourceLocations") {
110  p_direction = d;
111  return *this;
112  }
113 
114  /* Return the set of all source files. The items in the set are the file IDs stored in the SgAsmDwarfLine objects and can
115  * be converted to strings via Sg_File_Info::getFilenameFromID(). */
116  std::set<int> all_files() const ROSE_DEPRECATED("use SourceLocations");
117 
118  /* Given a source position, return the next source position that has a mapping. When called with no argument, the first
119  * source position is returned. When the end of the list is reached, a default-constructed SrcInfo object is returned. */
120  SrcInfo next_src(const SrcInfo &srcinfo = SrcInfo()) const ROSE_DEPRECATED("use SourceLocations");
121 
122 protected:
123  Direction p_direction; // Direction to use when printing
124  AddressSourceMap p_addr2src; // Forward mapping
125  mutable SourceAddressMap p_src2addr; // Reverse mapping
126  mutable bool up_to_date; // Is reverse mapping up-to-date?
127  virtual void visit(SgNode *node) ROSE_OVERRIDE;
128  void update() const; // update p_src2addr if necessary
129  void init(); // called by constructors
130 };
131 
132 std::ostream& operator<<(std::ostream&, const DwarfLineMapper::SrcInfo&);
133 std::ostream& operator<<(std::ostream&, const DwarfLineMapper&);
134 
135 } // namespace
136 } // namespace
137 
138 #endif
139 #endif
Class for traversing the AST.
Bidirectional mapping between addresses and source locations.
Scalar value type for a RangeMap.
Definition: rangemap.h:678
A contiguous range of values.
Definition: rangemap.h:50
STL namespace.
Main namespace for the ROSE library.
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:9435
virtual void visit(SgNode *node) ROSE_OVERRIDE
this method is called at every traversed node.
bool contains(const Range &x, bool strict=false) const
Does this range contain the argument range?
Definition: rangemap.h:326