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