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