The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
debugger.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2016 by Yurii Chernyi <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Formula debugger - implementation
18  * */
19 
20 
21 #include "formula/debugger.hpp"
22 #include "formula/formula.hpp"
23 #include "formula/function.hpp"
24 #include "game_display.hpp"
25 #include "log.hpp"
26 #include "resources.hpp"
28 #include "gui/widgets/settings.hpp"
29 
30 #include <boost/lexical_cast.hpp>
31 
32 static lg::log_domain log_formula_debugger("scripting/formula/debug");
33 #define DBG_FDB LOG_STREAM(debug, log_formula_debugger)
34 #define LOG_FDB LOG_STREAM(info, log_formula_debugger)
35 #define WRN_FDB LOG_STREAM(warn, log_formula_debugger)
36 #define ERR_FDB LOG_STREAM(err, log_formula_debugger)
37 
38 namespace game_logic {
39 
40 
41 debug_info::debug_info(int arg_number, int counter, int level, const std::string &name, const std::string &str, const variant &value, bool evaluated)
42  : arg_number_(arg_number), counter_(counter), level_(level), name_(name), str_(str), value_(value), evaluated_(evaluated)
43 {
44 }
45 
46 
48 {
49 }
50 
51 
52 int debug_info::level() const
53 {
54  return level_;
55 }
56 
58 {
59  return name_;
60 }
61 
62 
64 {
65  return counter_;
66 }
67 
68 
69 const variant& debug_info::value() const
70 {
71  return value_;
72 }
73 
74 
76 {
77  value_ = value;
78 }
79 
80 
82 {
83  return evaluated_;
84 }
85 
86 
87 void debug_info::set_evaluated(bool evaluated)
88 {
90 }
91 
92 
94 {
95  return str_;
96 }
97 
98 
100  : call_stack_(), counter_(0), current_breakpoint_(), breakpoints_(), execution_trace_(),arg_number_extra_debug_info(-1), f_name_extra_debug_info("")
101 {
104 }
105 
106 
108 {
109 }
110 
111 
112 static void msg(const char *act, debug_info &i, const char *to="", const char *result = "")
113 {
114  DBG_FDB << "#" << i.counter() << act << std::endl <<" \""<< i.name() << "\"='" << i.str() << "' " << to << result << std::endl;
115 }
116 
117 
118 void formula_debugger::add_debug_info(int arg_number, const std::string& f_name)
119 {
120  arg_number_extra_debug_info = arg_number;
121  f_name_extra_debug_info = f_name;
122 }
123 
124 
125 const std::deque<debug_info>& formula_debugger::get_call_stack() const
126 {
127  return call_stack_;
128 }
129 
130 
132 {
133  return current_breakpoint_;
134 }
135 
136 const std::deque<debug_info>& formula_debugger::get_execution_trace() const
137 {
138  return execution_trace_;
139 }
140 
142 {
144  if ((*b)->is_break_now()){
145  current_breakpoint_ = (*b);
146  show_gui();
148  if ((*b)->is_one_time_only()) {
149  breakpoints_.erase(b);
150  }
151  break;
152  }
153  }
154 }
155 
157 {
158  if (resources::screen == nullptr) {
159  WRN_FDB << "do not showing debug window due to nullptr gui" << std::endl;
160  return;
161  }
162  if (game_config::debug) {
163  gui2::tformula_debugger debug_dialog(*this);
164  debug_dialog.show(resources::screen->video());
165  } else {
166  WRN_FDB << "do not showing debug window due to disabled --new-widgets"<< std::endl;
167  }
168 }
169 
171 {
175  execution_trace_.push_back(call_stack_.back());
176 }
177 
178 
180 {
181  execution_trace_.push_back(call_stack_.back());
182  call_stack_.pop_back();
183 }
184 
185 
187 {
188  call_stack_.back().set_evaluated(evaluated);
189 }
190 
192 {
193  call_stack_.back().set_value(v);
194 }
195 
197 {
198  call_stack_push(expression.str());
200  msg(" evaluating expression: ",call_stack_.back());
201  variant v = expression.execute(variables,this);
204  msg(" evaluated expression: ",call_stack_.back()," to ",v.to_debug_string(nullptr,true).c_str());
206  call_stack_pop();
207  return v;
208 }
209 
210 
212 {
213  call_stack_push(f.str());
215  msg(" evaluating formula: ",call_stack_.back());
216  variant v = f.execute(variables,this);
219  msg(" evaluated formula: ",call_stack_.back()," to ",v.to_debug_string(nullptr,true).c_str());
221  call_stack_pop();
222  return v;
223 }
224 
225 
227 {
228  call_stack_push(f.str());
230  msg(" evaluating formula without variables: ",call_stack_.back());
231  variant v = f.execute(this);
234  msg(" evaluated formula without variables: ",call_stack_.back()," to ",v.to_debug_string(nullptr,true).c_str());
236  call_stack_pop();
237  return v;
238 }
239 
240 
242  : fdb_(fdb), name_(name), one_time_only_(one_time_only)
243 {
244 
245 }
246 
247 
249 {
250 }
251 
252 
254 {
255  return one_time_only_;
256 }
257 
258 
260 {
261  return name_;
262 }
263 
265 public:
267  : base_breakpoint(fdb,"End", true)
268  {
269  }
270 
271  virtual ~end_breakpoint()
272  {
273  }
274 
275  virtual bool is_break_now() const
276  {
277  const std::deque<debug_info> &call_stack = fdb_.get_call_stack();
278  if ((call_stack.size() == 1) && (call_stack[0].evaluated()) ) {
279  return true;
280  }
281  return false;
282  }
283 };
284 
285 
287 public:
289  : base_breakpoint(fdb,"Step",true)
290  {
291  }
292 
294  {
295  }
296 
297  virtual bool is_break_now() const
298  {
299  const std::deque<debug_info> &call_stack = fdb_.get_call_stack();
300  if (call_stack.empty() || call_stack.back().evaluated()) {
301  return false;
302  }
303 
304  return true;
305  }
306 };
307 
308 
310 public:
312  : base_breakpoint(fdb,"Step out",true), level_(fdb.get_call_stack().size()-1)
313  {
314  }
315 
317  {
318  }
319 
320  virtual bool is_break_now() const
321  {
322  const std::deque<debug_info> &call_stack = fdb_.get_call_stack();
323  if (call_stack.empty() || call_stack.back().evaluated()) {
324  return false;
325  }
326 
327  if (call_stack.size() == level_) {
328  return true;
329  }
330  return false;
331  }
332 private:
333  size_t level_;
334 };
335 
336 
338 public:
340  : base_breakpoint(fdb,"Next",true), level_(fdb.get_call_stack().size())
341  {
342  }
343 
345  {
346  }
347 
348  virtual bool is_break_now() const
349  {
350  const std::deque<debug_info> &call_stack = fdb_.get_call_stack();
351  if (call_stack.empty() || call_stack.back().evaluated()) {
352  return false;
353  }
354  if (call_stack.size() == level_) {
355  return true;
356  }
357  return false;
358  }
359 private:
360  size_t level_;
361 };
362 
363 
365 {
366  breakpoints_.push_back(breakpoint_ptr(new end_breakpoint(*this)));
367  LOG_FDB << "added 'end' breakpoint"<< std::endl;
368 }
369 
370 
372 {
373  breakpoints_.push_back(breakpoint_ptr(new step_in_breakpoint(*this)));
374  LOG_FDB << "added 'step into' breakpoint"<< std::endl;
375 }
376 
377 
379 {
380  breakpoints_.push_back(breakpoint_ptr(new step_out_breakpoint(*this)));
381  LOG_FDB << "added 'step out' breakpoint"<< std::endl;
382 }
383 
384 
386 {
387  breakpoints_.push_back(breakpoint_ptr(new next_breakpoint(*this)));
388  LOG_FDB << "added 'next' breakpoint"<< std::endl;
389 }
390 
391 
392 } // end of namespace game_logic
const std::string & str() const
Definition: debugger.cpp:93
void set_evaluated(bool evaluated)
Definition: debugger.cpp:87
GLuint counter
Definition: glew.h:2584
virtual bool is_break_now() const
Definition: debugger.cpp:348
GLint level
Definition: glew.h:1220
formula_debugger & fdb_
Definition: debugger.hpp:69
step_in_breakpoint(formula_debugger &fdb)
Definition: debugger.cpp:288
virtual bool is_break_now() const
Definition: debugger.cpp:320
bool show(CVideo &video, const unsigned auto_close_time=0)
Shows the window.
Definition: dialog.cpp:34
std::string f_name_extra_debug_info
Definition: debugger.hpp:153
Formula AI debugger.
game_display * screen
Definition: resources.cpp:27
int counter() const
Definition: debugger.cpp:63
const std::string & name() const
Definition: debugger.cpp:57
step_out_breakpoint(formula_debugger &fdb)
Definition: debugger.cpp:311
std::string get_call_stack()
Definition: variant.cpp:64
std::string str_
Definition: font.cpp:608
const std::deque< debug_info > & get_call_stack() const
Definition: debugger.cpp:125
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
virtual variant execute(const formula_callable &variables, formula_debugger *fdb=nullptr) const =0
bool evaluated() const
Definition: debugger.cpp:81
GLuint64EXT * result
Definition: glew.h:10727
This file contains the settings handling of the widget library.
variant evaluate_formula_callback(const formula &f, const formula_callable &variables)
Definition: debugger.cpp:211
#define WRN_FDB
Definition: debugger.cpp:35
const GLdouble * v
Definition: glew.h:1359
GLsizei const GLfloat * value
Definition: glew.h:1817
const std::string & name() const
Definition: debugger.cpp:259
void set_value(const variant &value)
Definition: debugger.cpp:75
static lg::log_domain log_formula_debugger("scripting/formula/debug")
void call_stack_push(const std::string &str)
Definition: debugger.cpp:170
const variant & value() const
Definition: debugger.cpp:69
int level() const
Definition: debugger.cpp:52
std::deque< debug_info > execution_trace_
Definition: debugger.hpp:151
base_breakpoint(formula_debugger &fdb, const std::string &name, bool one_time_only)
Definition: debugger.cpp:241
debug_info(int arg_number, int counter, int level, const std::string &name, const std::string &str, const variant &value, bool evaluated)
Definition: debugger.cpp:41
void call_stack_set_value(const variant &v)
Definition: debugger.cpp:191
virtual std::string str() const =0
virtual bool is_break_now() const
Definition: debugger.cpp:275
#define LOG_FDB
Definition: debugger.cpp:34
variant execute(const formula_callable &variables, formula_debugger *fdb=nullptr) const
Definition: formula.cpp:1369
size_t i
Definition: function.cpp:1057
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
#define DBG_FDB
Definition: debugger.cpp:33
const std::string & str() const
Definition: formula.hpp:63
variant evaluate_arg_callback(const formula_expression &expression, const formula_callable &variables)
Definition: debugger.cpp:196
end_breakpoint(formula_debugger &fdb)
Definition: debugger.cpp:266
GLuint const GLchar * name
Definition: glew.h:1782
GLsizeiptr size
Definition: glew.h:1649
virtual bool is_break_now() const
Definition: debugger.cpp:297
Standard logging facilities (interface).
std::deque< breakpoint_ptr > breakpoints_
Definition: debugger.hpp:150
const breakpoint_ptr get_current_breakpoint() const
Definition: debugger.cpp:131
void call_stack_set_evaluated(bool evaluated)
Definition: debugger.cpp:186
breakpoint_ptr current_breakpoint_
Definition: debugger.hpp:149
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
void add_debug_info(int arg_number, const std::string &f_name)
Definition: debugger.cpp:118
GLsizei const GLcharARB ** string
Definition: glew.h:4503
std::deque< debug_info > call_stack_
Definition: debugger.hpp:147
bool is_one_time_only() const
Definition: debugger.cpp:253
const std::deque< debug_info > & get_execution_trace() const
Definition: debugger.cpp:136
boost::shared_ptr< base_breakpoint > breakpoint_ptr
GLclampf f
Definition: glew.h:3024
next_breakpoint(formula_debugger &fdb)
Definition: debugger.cpp:339