SourceForge.net Logo
InteractiveDebugger.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2008
3  * DecisionSoft Limited. All rights reserved.
4  * Copyright (c) 2004-2008
5  * Oracle. All rights reserved.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * $Id: MessageListener.hpp 475 2008-01-08 18:47:44Z jpcs $
20  */
21 
22 #ifndef _INTERACTIVEDEBUGGER_HPP
23 #define _INTERACTIVEDEBUGGER_HPP
24 
25 #include <string>
26 #include <vector>
27 #include <map>
28 
31 
33 
34 class XQQuery;
35 class DynamicContext;
36 class DebugCommand;
38 
39 class XQILLA_API BaseInteractiveDebugger
40 {
41 public:
42  struct XQILLA_API Run {};
43  struct XQILLA_API Continue {};
44  struct XQILLA_API Quit {};
45 
46  static void outputLocation(const XMLCh *file, unsigned int line, unsigned int column,
47  unsigned int context = 0);
48  static void outputLocationFromString(const XMLCh *query, unsigned int line, unsigned int column,
49  unsigned int context = 0);
50  static std::string regexFind(const char *regex, const std::string &str, int groupNo = 1);
51 
52  virtual ~BaseInteractiveDebugger();
53 
54  unsigned int setBreakPoint(const std::string &file, unsigned int line, unsigned int column, bool temporary);
55  bool disableBreakPoint(unsigned int number);
56  bool enableBreakPoint(unsigned int number);
57  void listBreakPoints() const;
58 
59  void setStep();
60  void setNext();
61  bool queryStarted() const { return queryStarted_; }
62 
63  virtual void run() = 0;
64 
65  virtual bool changeFrame(unsigned int number) = 0;
66  virtual unsigned int getStackSize() const = 0;
67  virtual void stackTrace() const = 0;
68  virtual bool outputCurrentFrame(unsigned int context = 0) const = 0;
69  virtual void outputCurrentFrameQueryPlan() const = 0;
70  virtual bool queryCurrentFrame(const char *queryString) const = 0;
71  virtual bool currentFrameLocation(std::string &file, unsigned int &line, unsigned int &column) const = 0;
72 
73  virtual void setDoLazyEvaluation(bool lazy) = 0;
74  virtual void setDoFocusOptimizationsn(bool opt) = 0;
75  virtual void setDoProjection(bool opt) = 0;
76 
77 protected:
79 
80  DebugCommand *findCommand(std::string &command) const;
81  void checkBreak(bool entering);
82  void breakForError(const char *message);
83  void interrupted();
84  void readCommand();
85 
86  std::vector<DebugCommand*> commands_;
88 
90 
91  struct BreakPoint
92  {
93  BreakPoint(const std::string &f, unsigned int l, unsigned int c, bool t)
94  : file(f), line(l), column(c), temporary(t), disabled(false) {}
95 
96  std::string file;
97  unsigned int line, column;
98  bool temporary;
99  bool disabled;
100  };
101 
102  std::vector<BreakPoint> breaks_;
103  bool step_;
104  unsigned int next_;
105 };
106 
107 class XQILLA_API DebugCommand
108 {
109 public:
110  virtual ~DebugCommand() {};
111 
112  virtual const char *getCommandName() const { return name_; }
113  virtual const char *getCommandNameCompat() const { return compatName_; }
114  virtual const char *getBriefHelp() const { return briefHelp_; }
115  virtual const char *getMoreHelp() const { return moreHelp_; }
116 
117  static bool matches(const std::string &command,
118  const std::string &toMatch);
119  virtual bool matches(const std::string &command) const;
120 
121  virtual void execute(InputParser::Args &args, BaseInteractiveDebugger &env) = 0;
122 
123 protected:
124  DebugCommand(const char *name, const char *compatName,
125  const char *briefHelp, const char *moreHelp)
126  : name_(name), compatName_(compatName), briefHelp_(briefHelp), moreHelp_(moreHelp) {}
127 
128  const char *name_;
129  const char *compatName_;
130  const char *briefHelp_;
131  const char *moreHelp_;
132 };
133 
134 class XQILLA_API InteractiveDebugger : private BaseInteractiveDebugger,
135  private DebugListener
136 {
137 public:
138  static void debugQuery(const XQQuery *query, DynamicContext *context);
139  static void outputLocation(const LocationInfo *info, unsigned int context = 0);
140 
141 private:
142  InteractiveDebugger(const XQQuery *query, DynamicContext *context);
143 
144  virtual void enter(const StackFrame *stack, const DynamicContext *context);
145  virtual void exit(const StackFrame *stack, const DynamicContext *context);
146  virtual void error(const XQException &error, const StackFrame *stack, const DynamicContext *context);
147  virtual bool doLazyEvaluation() const { return lazy_; }
148  virtual bool doFocusOptimizations() const { return focusOptimzations_; }
149 
150  virtual void run();
151 
152  virtual bool changeFrame(unsigned int number);
153  virtual unsigned int getStackSize() const;
154  virtual void stackTrace() const;
155  virtual bool outputCurrentFrame(unsigned int context) const;
156  virtual void outputCurrentFrameQueryPlan() const;
157  virtual bool queryCurrentFrame(const char *queryString) const;
158  virtual bool currentFrameLocation(std::string &file, unsigned int &line, unsigned int &column) const;
159 
160  virtual void setDoLazyEvaluation(bool lazy) { lazy_ = lazy; }
161  virtual void setDoFocusOptimizationsn(bool opt) { focusOptimzations_ = opt; }
162  virtual void setDoProjection(bool opt);
163 
164  unsigned int getCurrentFrameNumber() const;
165  void output(const StackFrame *frame) const;
166  void report(const StackFrame *frame) const;
167 
168  const StackFrame *stack_;
169  const StackFrame *currentFrame_;
170 
171  const XQQuery *query_;
172  DynamicContext *context_;
173  bool lazy_;
174  bool focusOptimzations_;
175 };
176 
177 #endif