ROSE  0.11.52.0
AnalysisDebuggingUtils.h
1 #include <featureTests.h>
2 #ifdef ROSE_ENABLE_SOURCE_ANALYSIS
3 
4 #ifndef ROSE_ANALYSIS_DEBUGGING_UTILS_H
5 #define ROSE_ANALYSIS_DEBUGGING_UTILS_H
6 
7 #include <list>
8 #include <vector>
9 #include <string>
10 #include <iostream>
11 #include <fstream>
12 
13 class printable
14 {
15  public:
16  virtual ~printable() {}
17  virtual std::string str(std::string indent="")=0;
18 };
19 
20 class dottable
21 {
22  public:
23  virtual ~dottable() {}
24  // Returns a string that containts the representation of the object as a graph in the DOT language
25  // that has the given name
26  virtual std::string toDOT(std::string graphName)=0;
27 };
28 
29 class Analysis;
30 
31 namespace Dbg {
33  // For each function, a dot graph file will be generated. The CFG node will contain lattices information.
34  // The dot file will have a name like: original_full_filename_managed_func_name_cfg.dot
35  void dotGraphGenerator (Analysis *a);
36 
37 class dbgStream;
38 
39 // Adopted from http://wordaligned.org/articles/cpp-streambufs
40 class dbgBuf: public std::streambuf
41 {
42  friend class dbgStream;
43  // True immediately after a new line
44  bool synched;
45  // True if the owner dbgStream is writing text and false if the user is
46  bool ownerAccess;
47  std::streambuf* baseBuf;
48  std::list<std::string> funcs;
49  //std::list<std::string> indents;
50 
51  // The number of observed '<' characters that have not yet been balanced out by '>' characters.
52  // numOpenAngles = 1 means that we're inside an HTML tag
53  // numOpenAngles > 1 implies an error or text inside a comment
54  int numOpenAngles;
55 
56  // The number of divs that have been inserted into the output
57  std::list<int> parentDivs;
58 
59 public:
60 
61  virtual ~dbgBuf() {};
62  // Construct a streambuf which tees output to both input
63  // streambufs.
64  dbgBuf();
65  dbgBuf(std::streambuf* baseBuf);
66  void init(std::streambuf* baseBuf);
67 
68 private:
69  // This dbgBuf has no buffer. So every character "overflows"
70  // and can be put directly into the teed buffers.
71  virtual int overflow(int c);
72 
73  // Prints the indent to the stream buffer, returns 0 on success non-0 on failure
74  //int printIndent();
75 
76  // Prints the given string to the stream buffer
77  int printString(std::string s);
78 
79  //virtual int sputc(char c);
80 
81  virtual std::streamsize xsputn(const char * s, std::streamsize n);
82 
83  // Sync buffer.
84  virtual int sync();
85 
86  // Switch between the owner class and user code writing text
87 protected:
88  void userAccessing();
89  void ownerAccessing();
90 
91  // Indicates that the application has entered or exited a function
92  void enterFunc(std::string funcName/*, std::string indent=" "*/);
93  void exitFunc(std::string funcName);
94 };
95 
96 // Stream that uses dbgBuf
97 class dbgStream : public std::ostream
98 {
99  std::ofstream dbgFile;
100  dbgBuf buf;
101  std::vector<std::string> colors;
102  // The root working directory
103  std::string workDir;
104  // The directory where all images will be stored
105  std::string imgPath;
106  // The name of the output debug file
107  std::string dbgFileName;
108  // The total number of images in the output file
109  int numImages;
110 
111  std::ofstream summaryF;
112  bool initialized;
113 public:
114  // Construct an ostream which tees output to the supplied
115  // ostreams.
116  dbgStream();
117  dbgStream(std::string title, std::string dbgFileName, std::string workDir, std::string imgPath);
118  void init(std::string title, std::string dbgFileName, std::string workDir, std::string imgPath);
119  ~dbgStream();
120  void printDetailFileHeader(std::string title);
121  void printDetailFileTrailer();
122 
123  // Indicates that the application has entered or exited a function
124  void enterFunc(std::string funcName/*, std::string indent=" "*/);
125  void exitFunc(std::string funcName);
126 
127  // Adds an image to the output with the given extension and returns the path of this image
128  // so that the caller can write to it.
129  std::string addImage(std::string ext=".gif");
130 
131  // Given a reference to an object that can be represented as a dot graph, create an image from it and add it to the output.
132  // Return the path of the image.
133  std::string addDOT(dottable& obj);
134  // Given a reference to an object that can be represented as a dot graph, create an image of it and return the string
135  // that must be added to the output to include this image.
136  std::string addDOTStr(dottable& obj);
137  // Given a representation of a graph in dot format, create an image from it and add it to the output.
138  // Return the path of the image.
139  std::string addDOT(std::string dot);
140  // The common work code for all the addDOT methods
141  void addDOT(std::string imgFName, std::string graphName, std::string dot, std::ostream& ret);
142 };
143 
144  extern bool initialized;
145  extern dbgStream dbg;
146 
147  // Initializes the debug sub-system
148  void init(std::string title, std::string workDir, std::string fName="debug");
149 
150  // Indicates that the application has entered or exited a function
151  void enterFunc(std::string funcName/*, std::string indent=" "*/);
152  void exitFunc(std::string funcName);
153 
154  // Adds an image to the output with the given extension and returns the path of this image
155  // so that the caller can write to it.
156  std::string addImage(std::string ext=".gif");
157 
158  // Given a reference to an object that can be represented as a dot graph, create an image from it and add it to the output.
159  // Return the path of the image.
160  std::string addDOT(dottable& obj);
161 
162  // Given a reference to an object that can be represented as a dot graph, create an image of it and return the string
163  // that must be added to the output to include this image.
164  std::string addDOTStr(dottable& obj);
165 
166  // Given a representation of a graph in dot format, create an image from it and add it to the output.
167  // Return the path of the image.
168  std::string addDOT(std::string dot);
169 
170  // Given a string, returns a version of the string with all the control characters that may appear in the
171  // string escaped to that the string can be written out to Dbg::dbg with no formatting issues.
172  // This function can be called on text that has already been escaped with no harm.
173  std::string escape(std::string s);
174 }
175 
176 #endif
177 #endif