1 /*
2  * Copyright (c) 2015-2017, Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *  * Redistributions of source code must retain the above copyright notice,
8  *    this list of conditions and the following disclaimer.
9  *  * Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *  * Neither the name of Intel Corporation nor the names of its contributors
13  *    may be used to endorse or promote products derived from this software
14  *    without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /** \file
30  * \brief Dump code for NFA graphs.
31  */
32 
33 #ifndef NG_DUMP_H
34 #define NG_DUMP_H
35 
36 #include "grey.h"
37 #include "ng_holder.h" // for graph types
38 #include "ue2common.h"
39 
40 #include <unordered_map>
41 
42 #ifdef DUMP_SUPPORT
43 #include <fstream>
44 #endif
45 
46 struct RoseEngine;
47 
48 namespace ue2 {
49 
50 class NGHolder;
51 class NG;
52 class ExpressionInfo;
53 class ReportManager;
54 
55 // Implementations for stubs below -- all have the suffix "Impl".
56 
57 #ifdef DUMP_SUPPORT
58 
59 template <typename GraphT>
60 void dumpGraphImpl(const char *name, const GraphT &g);
61 
62 template <typename GraphT>
63 void dumpGraphImpl(const char *name, const GraphT &g, const ReportManager &rm);
64 
65 void dumpDotWrapperImpl(const NGHolder &g, const ExpressionInfo &expr,
66                         const char *name, const Grey &grey);
67 
68 void dumpComponentImpl(const NGHolder &g, const char *name, u32 expr, u32 comp,
69                        const Grey &grey);
70 
71 void dumpSomSubComponentImpl(const NGHolder &g, const char *name, u32 expr,
72                              u32 comp, u32 plan, const Grey &grey);
73 
74 void dumpHolderImpl(const NGHolder &h, unsigned int stageNumber,
75                     const char *stageName, const Grey &grey);
76 
77 // Variant that takes a region map as well.
78 void dumpHolderImpl(const NGHolder &h,
79                     const std::unordered_map<NFAVertex, u32> &region_map,
80                     unsigned int stageNumber, const char *stageName,
81                     const Grey &grey);
82 
83 template <typename GraphT>
dumpGraph(UNUSED const char * name,UNUSED const GraphT & g)84 static inline void dumpGraph(UNUSED const char *name, UNUSED const GraphT &g) {
85     dumpGraphImpl(name, g);
86 }
87 
88 #endif // DUMP_SUPPORT
89 
90 // Stubs which call through to dump code if compiled in.
91 
92 UNUSED static inline
dumpDotWrapper(UNUSED const NGHolder & g,UNUSED const ExpressionInfo & expr,UNUSED const char * name,UNUSED const Grey & grey)93 void dumpDotWrapper(UNUSED const NGHolder &g, UNUSED const ExpressionInfo &expr,
94                     UNUSED const char *name, UNUSED const Grey &grey) {
95 #ifdef DUMP_SUPPORT
96     dumpDotWrapperImpl(g, expr, name, grey);
97 #endif
98 }
99 
100 UNUSED static inline
dumpComponent(UNUSED const NGHolder & h,UNUSED const char * name,UNUSED u32 expr,UNUSED u32 comp,UNUSED const Grey & grey)101 void dumpComponent(UNUSED const NGHolder &h, UNUSED const char *name,
102                    UNUSED u32 expr, UNUSED u32 comp, UNUSED const Grey &grey) {
103 #ifdef DUMP_SUPPORT
104     dumpComponentImpl(h, name, expr, comp, grey);
105 #endif
106 }
107 
108 UNUSED static inline
dumpSomSubComponent(UNUSED const NGHolder & h,UNUSED const char * name,UNUSED u32 expr,UNUSED u32 comp,UNUSED u32 plan,UNUSED const Grey & grey)109 void dumpSomSubComponent(UNUSED const NGHolder &h, UNUSED const char *name,
110                          UNUSED u32 expr, UNUSED u32 comp, UNUSED u32 plan,
111                          UNUSED const Grey &grey) {
112 #ifdef DUMP_SUPPORT
113     dumpSomSubComponentImpl(h, name, expr, comp, plan, grey);
114 #endif
115 }
116 
117 UNUSED static inline
dumpHolder(UNUSED const NGHolder & h,UNUSED unsigned int stageNumber,UNUSED const char * name,UNUSED const Grey & grey)118 void dumpHolder(UNUSED const NGHolder &h, UNUSED unsigned int stageNumber,
119                 UNUSED const char *name, UNUSED const Grey &grey) {
120 #ifdef DUMP_SUPPORT
121     dumpHolderImpl(h, stageNumber, name, grey);
122 #endif
123 }
124 
125 UNUSED static inline
dumpHolder(UNUSED const NGHolder & h,UNUSED const std::unordered_map<NFAVertex,u32> & region_map,UNUSED unsigned int stageNumber,UNUSED const char * name,UNUSED const Grey & grey)126 void dumpHolder(UNUSED const NGHolder &h,
127                 UNUSED const std::unordered_map<NFAVertex, u32> &region_map,
128                 UNUSED unsigned int stageNumber, UNUSED const char *name,
129                 UNUSED const Grey &grey) {
130 #ifdef DUMP_SUPPORT
131     dumpHolderImpl(h, region_map, stageNumber, name, grey);
132 #endif
133 }
134 
135 #ifdef DUMP_SUPPORT
136 void dumpReportManager(const ReportManager &rm, const Grey &grey);
137 void dumpSmallWrite(const RoseEngine *rose, const Grey &grey);
138 #else
139 static UNUSED
dumpReportManager(const ReportManager &,const Grey &)140 void dumpReportManager(const ReportManager &, const Grey &) {
141 }
142 static UNUSED
dumpSmallWrite(const RoseEngine *,const Grey &)143 void dumpSmallWrite(const RoseEngine *, const Grey &) {
144 }
145 #endif
146 
147 #ifdef DUMP_SUPPORT
148 // replace boost's graphviz writer
149 template <typename GraphT, typename WriterT, typename VertexID>
writeGraphviz(std::ostream & out,const GraphT & g,WriterT w,const VertexID & vertex_id)150 static void writeGraphviz(std::ostream &out, const GraphT &g, WriterT w,
151                           const VertexID &vertex_id) {
152     const std::string delimiter(" -> ");
153     out << "digraph G {" << std::endl;
154 
155     typename boost::graph_traits<GraphT>::vertex_iterator i, end;
156     for(boost::tie(i,end) = vertices(g); i != end; ++i) {
157         out << get(vertex_id, *i);
158         w(out, *i); // print vertex attributes
159         out << ";" << std::endl;
160     }
161     typename boost::graph_traits<GraphT>::edge_iterator ei, edge_end;
162     for(boost::tie(ei, edge_end) = edges(g); ei != edge_end; ++ei) {
163         out << (get(vertex_id, source(*ei, g))) << delimiter
164             << (get(vertex_id, target(*ei, g))) << " ";
165         w(out, *ei); // print edge attributes
166         out << ";" << std::endl;
167     }
168     out << "}" << std::endl;
169 }
170 
171 #endif // DUMP_SUPPORT
172 
173 } // namespace ue2
174 
175 #endif // NG_DUMP_H
176