~ubuntu-branches/ubuntu/quantal/mysql-workbench/quantal

« back to all changes in this revision

Viewing changes to frontend/common/grt_python_debugger.cpp

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2012-03-01 21:57:30 UTC
  • Revision ID: package-import@ubuntu.com-20120301215730-o7y8av8y38n162ro
Tags: upstream-5.2.38+dfsg
ImportĀ upstreamĀ versionĀ 5.2.38+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License as
 
6
 * published by the Free Software Foundation; version 2 of the
 
7
 * License.
 
8
 * 
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
12
 * GNU General Public License for more details.
 
13
 * 
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
17
 * 02110-1301  USA
 
18
 */
 
19
 
 
20
#include "stdafx.h"
 
21
#include "base/string_utilities.h"
 
22
#include "grt_python_debugger.h"
 
23
#include "grt_shell_window.h"
 
24
#include "grt_code_editor.h"
 
25
 
 
26
#include "mforms/app.h"
 
27
 
 
28
/* The PythonDebugger object is meant to be used as an editor tab for editing
 
29
 Python scripts. Executing the script in the debugger tab will save the script
 
30
 to a temporary file and run that file through the pdb instance for the
 
31
 PythonDebugger.
 
32
 */
 
33
 
 
34
using namespace mforms;
 
35
 
 
36
#define BP_ACTION_STOP 0
 
37
#define BP_ACTION_CONTINUE 1
 
38
#define BP_ACTION_STEP 2
 
39
#define BP_ACTION_STEP_INTO 3
 
40
#define BP_ACTION_STEP_OUT 4
 
41
#define BP_ACTION_PAUSE 5
 
42
 
 
43
static PyObject *ui_print(PyObject *unused, PyObject *args)
 
44
{
 
45
  PyObject *self;
 
46
  char *s = NULL;
 
47
 
 
48
  if (!PyArg_ParseTuple(args, "Os:ui_print", &self, &s))
 
49
    return NULL;
 
50
 
 
51
  PythonDebugger *d = PythonDebugger::from_cobject(self);
 
52
  if (!d)
 
53
    return NULL;
 
54
 
 
55
  d->debug_print(s);
 
56
  
 
57
  Py_INCREF(Py_None);
 
58
  return Py_None;
 
59
}
 
60
 
 
61
 
 
62
static PyObject *ui_clear_breakpoints(PyObject *unused, PyObject *args)
 
63
{
 
64
  PyObject *self;
 
65
  
 
66
  if (!PyArg_ParseTuple(args, "O:ui_clear_breakpoints", &self))
 
67
    return NULL;
 
68
  
 
69
  PythonDebugger *d = PythonDebugger::from_cobject(self);
 
70
  if (!d)
 
71
    return NULL;
 
72
 
 
73
  d->ui_clear_breakpoints();
 
74
  
 
75
  Py_INCREF(Py_None);
 
76
  return Py_None;
 
77
}
 
78
 
 
79
 
 
80
static PyObject *ui_add_breakpoint(PyObject *unused, PyObject *args)
 
81
{
 
82
  PyObject *self;
 
83
  const char *file = "";
 
84
  int active;
 
85
  int line = 0;
 
86
  const char *condition = "";
 
87
  
 
88
  if (!PyArg_ParseTuple(args, "Oisiz:ui_add_breakpoint", &self, &active, &file, &line, &condition))
 
89
    return NULL;
 
90
  
 
91
  PythonDebugger *d = PythonDebugger::from_cobject(self);
 
92
  if (!d)
 
93
    return NULL;
 
94
  
 
95
  d->ui_add_breakpoint(file, line, condition);
 
96
  
 
97
  Py_INCREF(Py_None);
 
98
  return Py_None;
 
99
}
 
100
 
 
101
 
 
102
static PyObject *ui_program_stopped(PyObject *unused, PyObject *args)
 
103
{
 
104
  PyObject *self;
 
105
  const char *file = "";
 
106
  int line = 0;
 
107
  int reason = 0;
 
108
  
 
109
  if (!PyArg_ParseTuple(args, "Osii:ui_breakpoint_hit", &self, &file, &line, &reason))
 
110
    return NULL;
 
111
  
 
112
  PythonDebugger *d = PythonDebugger::from_cobject(self);
 
113
  if (!d)
 
114
    return NULL;
 
115
  
 
116
  const char *next_command = d->ui_program_stopped(file, line, reason);
 
117
  
 
118
  return Py_BuildValue("s", next_command);
 
119
}
 
120
 
 
121
 
 
122
static PyObject *ui_clear_stack(PyObject *unused, PyObject *args)
 
123
{
 
124
  PyObject *self;
 
125
  
 
126
  if (!PyArg_ParseTuple(args, "O:ui_clear_stack", &self))
 
127
    return NULL;
 
128
  
 
129
  PythonDebugger *d = PythonDebugger::from_cobject(self);
 
130
  if (!d)
 
131
    return NULL;
 
132
  
 
133
  d->ui_clear_stack();
 
134
  
 
135
  Py_INCREF(Py_None);
 
136
  return Py_None;
 
137
}
 
138
 
 
139
 
 
140
static PyObject *ui_add_stack(PyObject *unused, PyObject *args)
 
141
{
 
142
  PyObject *self;
 
143
  const char *file = "";
 
144
  const char *location = "";
 
145
  int line = 0;
 
146
  
 
147
  if (!PyArg_ParseTuple(args, "Ossi:ui_add_stack", &self, &location, &file, &line))
 
148
    return NULL;
 
149
  
 
150
  PythonDebugger *d = PythonDebugger::from_cobject(self);
 
151
  if (!d)
 
152
    return NULL;
 
153
  
 
154
  d->ui_add_stack(location, file, line);
 
155
  
 
156
  Py_INCREF(Py_None);
 
157
  return Py_None;
 
158
}
 
159
 
 
160
 
 
161
static PyObject *ui_clear_variables(PyObject *unused, PyObject *args)
 
162
{
 
163
  PyObject *self;
 
164
  
 
165
  if (!PyArg_ParseTuple(args, "O:ui_clear_variables", &self))
 
166
    return NULL;
 
167
  
 
168
  PythonDebugger *d = PythonDebugger::from_cobject(self);
 
169
  if (!d)
 
170
    return NULL;
 
171
  
 
172
  d->ui_clear_variables();
 
173
  
 
174
  Py_INCREF(Py_None);
 
175
  return Py_None;
 
176
}
 
177
 
 
178
 
 
179
static PyObject *ui_add_variable(PyObject *unused, PyObject *args)
 
180
{
 
181
  PyObject *self;
 
182
  const char *variable = "";
 
183
  const char *value = "";
 
184
  
 
185
  if (!PyArg_ParseTuple(args, "Oss:ui_add_variable", &self, &variable, &value))
 
186
    return NULL;
 
187
  
 
188
  PythonDebugger *d = PythonDebugger::from_cobject(self);
 
189
  if (!d)
 
190
    return NULL;
 
191
  
 
192
  d->ui_add_variable(variable, value);
 
193
  
 
194
  Py_INCREF(Py_None);
 
195
  return Py_None;
 
196
}
 
197
 
 
198
 
 
199
static int pdb_desc = 0;
 
200
 
 
201
static void init_pdb_python()
 
202
{
 
203
  static PyMethodDef ui_methods[] = {
 
204
    { (char*)"ui_print", (PyCFunction)ui_print, METH_VARARGS, NULL },
 
205
    { (char*)"ui_clear_breakpoints", (PyCFunction)ui_clear_breakpoints, METH_VARARGS, NULL },
 
206
    { (char*)"ui_add_breakpoint", (PyCFunction)ui_add_breakpoint, METH_VARARGS, NULL },
 
207
    { (char*)"ui_program_stopped", (PyCFunction)ui_program_stopped, METH_VARARGS, NULL },
 
208
    { (char*)"ui_clear_stack", (PyCFunction)ui_clear_stack, METH_VARARGS, NULL },
 
209
    { (char*)"ui_add_stack", (PyCFunction)ui_add_stack, METH_VARARGS, NULL },
 
210
    { (char*)"ui_clear_variables", (PyCFunction)ui_clear_variables, METH_VARARGS, NULL },
 
211
    { (char*)"ui_add_variable", (PyCFunction)ui_add_variable, METH_VARARGS, NULL },
 
212
    { NULL, NULL, 0, NULL }
 
213
  };
 
214
 
 
215
  static PyObject *m = NULL;
 
216
  
 
217
  if (!m)
 
218
    m = Py_InitModule("wbpdb", ui_methods);
 
219
}
 
220
 
 
221
 
 
222
PyObject *PythonDebugger::as_cobject()
 
223
{
 
224
  return PyCObject_FromVoidPtrAndDesc(this, &pdb_desc, NULL);
 
225
}
 
226
 
 
227
 
 
228
PythonDebugger *PythonDebugger::from_cobject(PyObject *cobj)
 
229
{
 
230
  if (!PyCObject_Check(cobj))
 
231
    return NULL;
 
232
  
 
233
  if (PyCObject_GetDesc(cobj) != &pdb_desc)
 
234
    return NULL;
 
235
  
 
236
  return reinterpret_cast<PythonDebugger*>(PyCObject_AsVoidPtr(cobj));
 
237
}
 
238
 
 
239
 
 
240
PythonDebugger::PythonDebugger(GRTShellWindow *shell, mforms::TabView *tabview)
 
241
: _shell(shell), _lower_tabs(tabview), _stack_position_editor(0), _stack_position_line(0)
 
242
{
 
243
  _breakpoint_list= manage(new TreeView(TreeDefault));
 
244
//  _breakpoint_list->add_column(CheckColumnType, " ", 30, true);
 
245
  _breakpoint_list->add_column(StringColumnType, "Breakpoint", 200, false);
 
246
  _breakpoint_list->add_column(StringColumnType, "Location", 200, false);
 
247
  _breakpoint_list->add_column(StringColumnType, "Condition", 200, true);
 
248
  _breakpoint_list->end_columns();
 
249
  _breakpoint_list->set_cell_edit_handler(boost::bind(&PythonDebugger::edit_breakpoint, this, _1, _2, _3));
 
250
  _lower_tabs->add_page(_breakpoint_list, _("Breakpoints"));
 
251
 
 
252
  mforms::Splitter *spl = manage(new Splitter(true));
 
253
  
 
254
  _lower_tabs->add_page(spl, _("Debug Info"));
 
255
 
 
256
  _stack_list= manage(new TreeView(TreeDefault));
 
257
  _stack_list->add_column(StringColumnType, "#", 30, false);
 
258
  _stack_list->add_column(StringColumnType, "Stack Location", 300, false);
 
259
  _stack_list->add_column(StringColumnType, "File", 300, false);
 
260
  _stack_list->end_columns();
 
261
  spl->add(_stack_list);
 
262
  scoped_connect(_stack_list->signal_changed(),boost::bind(&PythonDebugger::stack_selected, this));
 
263
  //_stack_list->signal_row_activated().connect();
 
264
  
 
265
  _variable_list= manage(new TreeView(TreeDefault));
 
266
  _variable_list->add_column(StringColumnType, "Variable", 200, false);
 
267
  _variable_list->add_column(StringColumnType, "Value", 400, false);
 
268
  _variable_list->end_columns();
 
269
  spl->add(_variable_list);
 
270
  
 
271
  spl->set_position(500);
 
272
  _program_stopped = false;
 
273
}
 
274
 
 
275
 
 
276
void PythonDebugger::init_pdb()
 
277
{
 
278
  grt::WillEnterPython lock;
 
279
  
 
280
  grt::PythonContext *pyc = grt::PythonContext::get();
 
281
  
 
282
  init_pdb_python();
 
283
    
 
284
  if (!pyc->import_module("grt_python_debugger"))
 
285
    throw std::runtime_error("Could not import Python debugger");
 
286
  
 
287
  PyObject *debugger_class = pyc->eval_string("grt_python_debugger.PyDebugger");
 
288
  if (!debugger_class)
 
289
    throw std::runtime_error("Could not initialize Python debugger");
 
290
 
 
291
  PyObject *ui = as_cobject();
 
292
 
 
293
  // PyDebugger(this)
 
294
  PyObject *r= PyObject_Call(debugger_class, Py_BuildValue("(O)", ui), NULL);
 
295
  Py_DECREF(ui);
 
296
  Py_DECREF(debugger_class);
 
297
 
 
298
  if (!r)
 
299
    throw std::runtime_error("Error instantiating Python debugger object");
 
300
 
 
301
  _pdb = r;
 
302
  Py_DECREF(r);
 
303
 
 
304
  // come up with a global variable name so that we have a reference to the debugger
 
305
  // object from Python itself
 
306
  _pdb_varname = base::strfmt("wbpdb_instance_%p", this);
 
307
 
 
308
  pyc->set_global(_pdb_varname, _pdb);
 
309
}
 
310
 
 
311
 
 
312
void PythonDebugger::editor_added(GRTCodeEditor *editor)
 
313
{
 
314
  scoped_connect(editor->get_editor()->signal_gutter_clicked(),boost::bind(&PythonDebugger::line_gutter_clicked, this, _1, _2, _3, editor));
 
315
  scoped_connect(editor->get_editor()->signal_changed(),boost::bind(&PythonDebugger::editor_text_changed, this, _1, _2, editor));
 
316
}
 
317
 
 
318
 
 
319
void PythonDebugger::editor_closed(GRTCodeEditor *editor)
 
320
{
 
321
  if (editor == _stack_position_editor)
 
322
    _stack_position_editor = 0;
 
323
}
 
324
 
 
325
 
 
326
bool PythonDebugger::ensure_code_saved()
 
327
{
 
328
  GRTCodeEditor *editor = _shell->get_active_editor();
 
329
  
 
330
  if (editor->is_dirty())
 
331
  {
 
332
    if (Utilities::show_warning("Debug Script",
 
333
                                "Script must be saved to be debugged. Would you like to save it now?",
 
334
                                "Save", "Cancel", "") == ResultOk)
 
335
    {
 
336
      if (!editor->save(false))
 
337
        return false;
 
338
    }
 
339
    else
 
340
      return false;
 
341
  }
 
342
  return true;
 
343
}
 
344
 
 
345
 
 
346
void PythonDebugger::line_gutter_clicked(int margin, int line, mforms::ModifierKey mods, GRTCodeEditor *editor)
 
347
{  
 
348
  if (margin == 1 || margin == 0) // click line numbers or on the markers
 
349
  {
 
350
    grt::WillEnterPython lock;
 
351
 
 
352
    if (toggle_breakpoint(editor->get_path().c_str(), line+1)) // line numbers from editor are 0 based
 
353
      editor->get_editor()->show_markup(LineMarkupBreakpoint, line);
 
354
    else
 
355
      editor->get_editor()->remove_markup(LineMarkupBreakpoint|LineMarkupBreakpointHit, line);
 
356
  }
 
357
}
 
358
 
 
359
 
 
360
void PythonDebugger::editor_text_changed(int line, int linesAdded, GRTCodeEditor *editor)
 
361
{
 
362
  if (linesAdded != 0)
 
363
  {
 
364
    grt::WillEnterPython lock;
 
365
    
 
366
    std::string path = editor->get_path();
 
367
    
 
368
    grt::AutoPyObject r(PyObject_CallMethod(_pdb, (char*)"wdb_update_breakpoint", (char*)"(sii)", 
 
369
                                            path.c_str(), line+1, linesAdded), 
 
370
                        false);
 
371
    if (!r)
 
372
    {
 
373
      PyErr_Print();
 
374
      PyErr_Clear();
 
375
    } 
 
376
  }
 
377
}
 
378
 
 
379
 
 
380
void PythonDebugger::edit_breakpoint(int row, int column, std::string value)
 
381
{
 
382
  if (column == 2) // edit bp condition
 
383
  {
 
384
    grt::WillEnterPython lock;
 
385
    grt::AutoPyObject r(PyObject_CallMethod(_pdb, (char*)"wdb_set_bp_condition", (char*)"(is)", row, value.c_str()), 
 
386
                        false);
 
387
    if (!r)
 
388
    {
 
389
      // exception while running, dump the exception to the debugger console
 
390
      // exceptions in the script being debugged will be caught and reported by wdb_run
 
391
      debug_print("There was an unhandled internal exception setting a bp condition.\n");
 
392
      PyErr_Print();
 
393
      PyErr_Clear();
 
394
    }
 
395
    
 
396
    if (PyBool_Check(r) && r == Py_True)
 
397
    {
 
398
      _breakpoint_list->set(row, column, value);
 
399
    }
 
400
  }
 
401
}
 
402
 
 
403
 
 
404
void PythonDebugger::refresh_file(const std::string &file)
 
405
{
 
406
  grt::WillEnterPython lock;
 
407
  grt::AutoPyObject r(PyObject_CallMethod(_pdb, (char*)"wdb_reload_module_for_file", (char*)"(s)", file.c_str()), 
 
408
                      false);
 
409
}
 
410
 
 
411
 
 
412
void PythonDebugger::run(GRTCodeEditor *editor, bool stepping)
 
413
{
 
414
  if (editor->is_dirty() && !ensure_code_saved())
 
415
    return;
 
416
 
 
417
  grt::WillEnterPython lock;
 
418
 
 
419
//  debug_print(base::strfmt("Running script %s...\n", _shell->get_path().c_str()));
 
420
 
 
421
  _pause_clicked = false;
 
422
  // run the script
 
423
  grt::AutoPyObject r(PyObject_CallMethod(_pdb, (char*)"wdb_run", (char*)"(si)", editor->get_path().c_str(), stepping?1:0), 
 
424
                      false);
 
425
  if (!r)
 
426
  {
 
427
    // exception while running, dump the exception to the debugger console
 
428
    // exceptions in the script being debugged will be caught and reported by wdb_run
 
429
    debug_print("There was an unhandled internal exception executing the script.\n");
 
430
    PyErr_Print();
 
431
    PyErr_Clear();
 
432
  }
 
433
 
 
434
  _variable_list->clear_rows();
 
435
  _stack_list->clear_rows();
 
436
  
 
437
  debug_print("Execution finished\n");
 
438
}
 
439
 
 
440
 
 
441
void PythonDebugger::step_into()
 
442
{
 
443
  mforms::App::get()->exit_event_loop(BP_ACTION_STEP_INTO);
 
444
}
 
445
 
 
446
 
 
447
void PythonDebugger::step()
 
448
{
 
449
  mforms::App::get()->exit_event_loop(BP_ACTION_STEP);
 
450
}
 
451
 
 
452
 
 
453
void PythonDebugger::continue_()
 
454
{
 
455
  mforms::App::get()->exit_event_loop(BP_ACTION_CONTINUE);
 
456
}
 
457
 
 
458
void PythonDebugger::stop()
 
459
{
 
460
  mforms::App::get()->exit_event_loop(BP_ACTION_STOP);
 
461
}
 
462
 
 
463
void PythonDebugger::pause()
 
464
{
 
465
  _pause_clicked = true;
 
466
  mforms::App::get()->exit_event_loop(BP_ACTION_PAUSE);
 
467
}
 
468
 
 
469
void PythonDebugger::step_out()
 
470
{
 
471
  mforms::App::get()->exit_event_loop(BP_ACTION_STEP_OUT);
 
472
}
 
473
 
 
474
 
 
475
 
 
476
void PythonDebugger::debug_print(const std::string &s)
 
477
{
 
478
  _shell->add_output(s);
 
479
}
 
480
 
 
481
 
 
482
void PythonDebugger::ui_clear_breakpoints()
 
483
{
 
484
  _breakpoint_list->clear_rows();
 
485
}
 
486
 
 
487
 
 
488
void PythonDebugger::ui_add_breakpoint(const char *file, int line, const char *condition)
 
489
{
 
490
  if (!file)
 
491
    file = "";
 
492
  else
 
493
    file = g_path_get_basename(file);
 
494
    
 
495
  if (!condition)
 
496
    condition = "";
 
497
  
 
498
  int row = _breakpoint_list->add_row();
 
499
  //_breakpoint_list->set(row, 0, true);
 
500
  _breakpoint_list->set(row, 0, base::strfmt("%s:%i", file, line));
 
501
  _breakpoint_list->set(row, 1, "");
 
502
  _breakpoint_list->set(row, 2, condition);
 
503
}
 
504
 
 
505
 
 
506
const char *PythonDebugger::ui_program_stopped(const char *file, int line, int reason)
 
507
{
 
508
  GRTCodeEditor *editor = 0;
 
509
  bool continue_possible = true;
 
510
 
 
511
  if (_pause_clicked && reason == 5)
 
512
  {
 
513
    _pause_clicked = false;
 
514
    return "pause";
 
515
  }
 
516
 
 
517
  if (reason != 5)
 
518
  {
 
519
    editor = _shell->get_editor_for(file, true);
 
520
    if (!editor)
 
521
    {
 
522
      if (strcmp(file, "<string>") != 0)
 
523
        editor = _shell->show_file_at_line(file, line);
 
524
      if (!editor && reason != 6)
 
525
      {
 
526
        _shell->add_output(base::strfmt("continuing from %s:%i...\n", file, line));
 
527
        return "continue";
 
528
      }
 
529
    }
 
530
  }
 
531
 
 
532
  if (reason == 6)
 
533
    reason = 0; // treat like we're stepping
 
534
  
 
535
  _program_stopped = true;
 
536
  float timeout = 0.0;
 
537
    
 
538
  switch (reason)
 
539
  {
 
540
    case 0: // step      
 
541
      break;
 
542
      
 
543
    case 1: // breakpoint
 
544
      // show the cursor position in the editor
 
545
      if (editor)
 
546
        editor->get_editor()->show_markup(LineMarkupBreakpointHit, line-1);
 
547
      break;
 
548
      
 
549
    case 2: // exception
 
550
      if (editor)
 
551
        editor->get_editor()->show_markup(LineMarkupError, line-1);
 
552
      continue_possible = false;
 
553
      
 
554
      _shell->activate_output_tab();
 
555
      break;
 
556
      
 
557
    case 5: // heartbeat
 
558
      // schedule for the event loop to exit in 0.01s
 
559
      timeout = 0.01f;
 
560
      continue_possible = false;
 
561
      break;
 
562
  }
 
563
  if (editor)
 
564
    editor->get_editor()->show_markup(LineMarkupCurrent, line-1);
 
565
  _stack_position_editor = editor;
 
566
  _stack_position_line = line-1;
 
567
 
 
568
  _shell->_run_button->show(false);
 
569
  _shell->_continue_button->show(true);
 
570
 
 
571
  _shell->_continue_button->set_enabled(continue_possible);
 
572
  _shell->_step_button->set_enabled(continue_possible);
 
573
  _shell->_step_into_button->set_enabled(continue_possible);
 
574
  _shell->_step_out_button->set_enabled(continue_possible);
 
575
  _shell->_stop_button->set_enabled(true);
 
576
  _shell->_pause_button->set_enabled(reason == 5);
 
577
  
 
578
  if (reason != 5)
 
579
  {
 
580
    _stack_list->set_selected(0);
 
581
    stack_selected();
 
582
  }
 
583
  
 
584
  // enter event loop and wait for some action from user
 
585
  int action = mforms::App::get()->enter_event_loop(timeout);
 
586
  
 
587
  switch (reason)
 
588
  {
 
589
    case 0: // step
 
590
      break;
 
591
      
 
592
    case 1: // breakpoint
 
593
      // show the cursor position in the editor
 
594
      if (editor)
 
595
        editor->get_editor()->remove_markup(LineMarkupBreakpointHit, line-1);
 
596
      break;
 
597
      
 
598
    case 2: // exception
 
599
      if (editor)
 
600
        editor->get_editor()->remove_markup(LineMarkupError, line-1);
 
601
      break;
 
602
      
 
603
    case 5: // heartbeat
 
604
      if (action < 0) // timeout
 
605
        action = BP_ACTION_CONTINUE;            
 
606
      break;
 
607
  }
 
608
  if (editor)
 
609
    editor->get_editor()->remove_markup(LineMarkupCurrent, line-1);
 
610
 
 
611
  if (_stack_position_editor)
 
612
  {
 
613
    _stack_position_editor->get_editor()->remove_markup(LineMarkupCurrent, _stack_position_line);
 
614
    _stack_position_editor = 0;
 
615
    _stack_position_line = 0;
 
616
  }
 
617
 
 
618
  _shell->_step_button->set_enabled(false);
 
619
  _shell->_step_into_button->set_enabled(false);
 
620
  _shell->_step_out_button->set_enabled(false);
 
621
  _shell->_stop_button->set_enabled(true); 
 
622
  _shell->_continue_button->set_enabled(false);
 
623
  _shell->_pause_button->set_enabled(true);
 
624
  
 
625
  _pause_clicked = false;
 
626
  // determine command
 
627
  const char *command = "abort";
 
628
  switch (action)
 
629
  {
 
630
    case BP_ACTION_CONTINUE:
 
631
      command = "continue";
 
632
      break;
 
633
      
 
634
    case BP_ACTION_STEP:
 
635
      command = "step";
 
636
      break;
 
637
      
 
638
    case BP_ACTION_STEP_INTO:
 
639
      command = "step_into";
 
640
      break;
 
641
 
 
642
    case BP_ACTION_STOP:
 
643
      command = "stop";
 
644
      break;
 
645
      
 
646
    case BP_ACTION_STEP_OUT:
 
647
      command = "step_out";
 
648
      break;
 
649
 
 
650
    case BP_ACTION_PAUSE:
 
651
      _pause_clicked = true;
 
652
      command = "pause";
 
653
      break;
 
654
 
 
655
    default: // error or unknown, abort everything
 
656
      command = "abort";
 
657
      break;
 
658
  }
 
659
  _program_stopped = false;
 
660
  return command;
 
661
}
 
662
 
 
663
 
 
664
bool PythonDebugger::heartbeat_timeout()
 
665
{
 
666
  _heartbeat_timeout_timer = 0;
 
667
  mforms::App::get()->exit_event_loop(BP_ACTION_CONTINUE);
 
668
  return false;
 
669
}
 
670
 
 
671
 
 
672
void PythonDebugger::ui_clear_stack()
 
673
{
 
674
  _stack_list->clear_rows();
 
675
}
 
676
 
 
677
 
 
678
void PythonDebugger::ui_add_stack(const char *location, const char *file, int line)
 
679
{
 
680
  if (!file)
 
681
    file = "";
 
682
  
 
683
  int row = _stack_list->add_row();
 
684
  _stack_list->set_row_tag(row, file);  
 
685
  _stack_list->set(row, 0, row);
 
686
  _stack_list->set(row, 1, location);
 
687
  _stack_list->set(row, 2, base::strfmt("%s:%i", g_path_get_basename(file), line));
 
688
}
 
689
 
 
690
 
 
691
void PythonDebugger::ui_clear_variables()
 
692
{
 
693
  _variable_list->clear_rows();
 
694
}
 
695
 
 
696
void PythonDebugger::ui_add_variable(const char *varname, const char *value)
 
697
{
 
698
  int row = _variable_list->add_row();
 
699
  _variable_list->set(row, 0, varname);
 
700
  _variable_list->set(row, 1, value);
 
701
}
 
702
 
 
703
void PythonDebugger::stack_selected()
 
704
{
 
705
  int i = _stack_list->get_selected();
 
706
  int show_frame = 0;
 
707
 
 
708
  if (_stack_position_editor)
 
709
  {
 
710
    _stack_position_editor->get_editor()->remove_markup(LineMarkupCurrent, _stack_position_line);
 
711
    _stack_position_editor = 0;
 
712
    _stack_position_line = 0;
 
713
    _variable_list->clear_rows();
 
714
  }
 
715
  
 
716
  if (i >= 0)
 
717
  {
 
718
    std::string location = _stack_list->get_string(i, 2);
 
719
    std::string::size_type pos = location.rfind(':');
 
720
    std::string file = _stack_list->get_row_tag(i);
 
721
    int line = atoi(location.substr(pos+1).c_str());
 
722
    
 
723
    if (!file.empty() && line > 0)
 
724
    {
 
725
      GRTCodeEditor *editor = _shell->show_file_at_line(file, line-1);
 
726
      editor->get_editor()->show_markup(LineMarkupCurrent, line-1);
 
727
      
 
728
      _stack_position_editor = editor;
 
729
      _stack_position_line = line-1;
 
730
    }
 
731
    show_frame = -1*(i+1);
 
732
  }
 
733
  
 
734
  grt::WillEnterPython lock;
 
735
  
 
736
  grt::AutoPyObject r(PyObject_CallMethod(_pdb, (char*)"wdb_refresh_variables", (char*)"(i)", 
 
737
                                          show_frame),
 
738
                      false);
 
739
  if (!r)
 
740
  {
 
741
    debug_print("Internal error showing variables\n");
 
742
    PyErr_Print();
 
743
    PyErr_Clear();
 
744
  }  
 
745
}
 
746
 
 
747
bool PythonDebugger::toggle_breakpoint(const char *file, int line)
 
748
{
 
749
  grt::WillEnterPython lock;
 
750
  
 
751
  grt::AutoPyObject r(PyObject_CallMethod(_pdb, (char*)"wdb_toggle_breakpoint", (char*)"(si)", 
 
752
                                          file, line),
 
753
                      false);
 
754
  if (!r)
 
755
  {
 
756
    debug_print("Internal error toggling debugger breakpoint\n");
 
757
    PyErr_Print();
 
758
    PyErr_Clear();
 
759
    return false;
 
760
  }
 
761
  
 
762
  if (PyBool_Check(r) && r == Py_True)
 
763
  {
 
764
    debug_print(base::strfmt("Added breakpoint to line %i\n", line));
 
765
    return true;
 
766
  }
 
767
  debug_print(base::strfmt("Removed breakpoint from line %i\n", line));  
 
768
  return false;
 
769
}
 
770