2
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
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
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.
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
21
#include "base/string_utilities.h"
22
#include "grt_python_debugger.h"
23
#include "grt_shell_window.h"
24
#include "grt_code_editor.h"
26
#include "mforms/app.h"
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
34
using namespace mforms;
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
43
static PyObject *ui_print(PyObject *unused, PyObject *args)
48
if (!PyArg_ParseTuple(args, "Os:ui_print", &self, &s))
51
PythonDebugger *d = PythonDebugger::from_cobject(self);
62
static PyObject *ui_clear_breakpoints(PyObject *unused, PyObject *args)
66
if (!PyArg_ParseTuple(args, "O:ui_clear_breakpoints", &self))
69
PythonDebugger *d = PythonDebugger::from_cobject(self);
73
d->ui_clear_breakpoints();
80
static PyObject *ui_add_breakpoint(PyObject *unused, PyObject *args)
83
const char *file = "";
86
const char *condition = "";
88
if (!PyArg_ParseTuple(args, "Oisiz:ui_add_breakpoint", &self, &active, &file, &line, &condition))
91
PythonDebugger *d = PythonDebugger::from_cobject(self);
95
d->ui_add_breakpoint(file, line, condition);
102
static PyObject *ui_program_stopped(PyObject *unused, PyObject *args)
105
const char *file = "";
109
if (!PyArg_ParseTuple(args, "Osii:ui_breakpoint_hit", &self, &file, &line, &reason))
112
PythonDebugger *d = PythonDebugger::from_cobject(self);
116
const char *next_command = d->ui_program_stopped(file, line, reason);
118
return Py_BuildValue("s", next_command);
122
static PyObject *ui_clear_stack(PyObject *unused, PyObject *args)
126
if (!PyArg_ParseTuple(args, "O:ui_clear_stack", &self))
129
PythonDebugger *d = PythonDebugger::from_cobject(self);
140
static PyObject *ui_add_stack(PyObject *unused, PyObject *args)
143
const char *file = "";
144
const char *location = "";
147
if (!PyArg_ParseTuple(args, "Ossi:ui_add_stack", &self, &location, &file, &line))
150
PythonDebugger *d = PythonDebugger::from_cobject(self);
154
d->ui_add_stack(location, file, line);
161
static PyObject *ui_clear_variables(PyObject *unused, PyObject *args)
165
if (!PyArg_ParseTuple(args, "O:ui_clear_variables", &self))
168
PythonDebugger *d = PythonDebugger::from_cobject(self);
172
d->ui_clear_variables();
179
static PyObject *ui_add_variable(PyObject *unused, PyObject *args)
182
const char *variable = "";
183
const char *value = "";
185
if (!PyArg_ParseTuple(args, "Oss:ui_add_variable", &self, &variable, &value))
188
PythonDebugger *d = PythonDebugger::from_cobject(self);
192
d->ui_add_variable(variable, value);
199
static int pdb_desc = 0;
201
static void init_pdb_python()
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 }
215
static PyObject *m = NULL;
218
m = Py_InitModule("wbpdb", ui_methods);
222
PyObject *PythonDebugger::as_cobject()
224
return PyCObject_FromVoidPtrAndDesc(this, &pdb_desc, NULL);
228
PythonDebugger *PythonDebugger::from_cobject(PyObject *cobj)
230
if (!PyCObject_Check(cobj))
233
if (PyCObject_GetDesc(cobj) != &pdb_desc)
236
return reinterpret_cast<PythonDebugger*>(PyCObject_AsVoidPtr(cobj));
240
PythonDebugger::PythonDebugger(GRTShellWindow *shell, mforms::TabView *tabview)
241
: _shell(shell), _lower_tabs(tabview), _stack_position_editor(0), _stack_position_line(0)
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"));
252
mforms::Splitter *spl = manage(new Splitter(true));
254
_lower_tabs->add_page(spl, _("Debug Info"));
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();
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);
271
spl->set_position(500);
272
_program_stopped = false;
276
void PythonDebugger::init_pdb()
278
grt::WillEnterPython lock;
280
grt::PythonContext *pyc = grt::PythonContext::get();
284
if (!pyc->import_module("grt_python_debugger"))
285
throw std::runtime_error("Could not import Python debugger");
287
PyObject *debugger_class = pyc->eval_string("grt_python_debugger.PyDebugger");
289
throw std::runtime_error("Could not initialize Python debugger");
291
PyObject *ui = as_cobject();
294
PyObject *r= PyObject_Call(debugger_class, Py_BuildValue("(O)", ui), NULL);
296
Py_DECREF(debugger_class);
299
throw std::runtime_error("Error instantiating Python debugger object");
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);
308
pyc->set_global(_pdb_varname, _pdb);
312
void PythonDebugger::editor_added(GRTCodeEditor *editor)
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));
319
void PythonDebugger::editor_closed(GRTCodeEditor *editor)
321
if (editor == _stack_position_editor)
322
_stack_position_editor = 0;
326
bool PythonDebugger::ensure_code_saved()
328
GRTCodeEditor *editor = _shell->get_active_editor();
330
if (editor->is_dirty())
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)
336
if (!editor->save(false))
346
void PythonDebugger::line_gutter_clicked(int margin, int line, mforms::ModifierKey mods, GRTCodeEditor *editor)
348
if (margin == 1 || margin == 0) // click line numbers or on the markers
350
grt::WillEnterPython lock;
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);
355
editor->get_editor()->remove_markup(LineMarkupBreakpoint|LineMarkupBreakpointHit, line);
360
void PythonDebugger::editor_text_changed(int line, int linesAdded, GRTCodeEditor *editor)
364
grt::WillEnterPython lock;
366
std::string path = editor->get_path();
368
grt::AutoPyObject r(PyObject_CallMethod(_pdb, (char*)"wdb_update_breakpoint", (char*)"(sii)",
369
path.c_str(), line+1, linesAdded),
380
void PythonDebugger::edit_breakpoint(int row, int column, std::string value)
382
if (column == 2) // edit bp condition
384
grt::WillEnterPython lock;
385
grt::AutoPyObject r(PyObject_CallMethod(_pdb, (char*)"wdb_set_bp_condition", (char*)"(is)", row, value.c_str()),
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");
396
if (PyBool_Check(r) && r == Py_True)
398
_breakpoint_list->set(row, column, value);
404
void PythonDebugger::refresh_file(const std::string &file)
406
grt::WillEnterPython lock;
407
grt::AutoPyObject r(PyObject_CallMethod(_pdb, (char*)"wdb_reload_module_for_file", (char*)"(s)", file.c_str()),
412
void PythonDebugger::run(GRTCodeEditor *editor, bool stepping)
414
if (editor->is_dirty() && !ensure_code_saved())
417
grt::WillEnterPython lock;
419
// debug_print(base::strfmt("Running script %s...\n", _shell->get_path().c_str()));
421
_pause_clicked = false;
423
grt::AutoPyObject r(PyObject_CallMethod(_pdb, (char*)"wdb_run", (char*)"(si)", editor->get_path().c_str(), stepping?1:0),
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");
434
_variable_list->clear_rows();
435
_stack_list->clear_rows();
437
debug_print("Execution finished\n");
441
void PythonDebugger::step_into()
443
mforms::App::get()->exit_event_loop(BP_ACTION_STEP_INTO);
447
void PythonDebugger::step()
449
mforms::App::get()->exit_event_loop(BP_ACTION_STEP);
453
void PythonDebugger::continue_()
455
mforms::App::get()->exit_event_loop(BP_ACTION_CONTINUE);
458
void PythonDebugger::stop()
460
mforms::App::get()->exit_event_loop(BP_ACTION_STOP);
463
void PythonDebugger::pause()
465
_pause_clicked = true;
466
mforms::App::get()->exit_event_loop(BP_ACTION_PAUSE);
469
void PythonDebugger::step_out()
471
mforms::App::get()->exit_event_loop(BP_ACTION_STEP_OUT);
476
void PythonDebugger::debug_print(const std::string &s)
478
_shell->add_output(s);
482
void PythonDebugger::ui_clear_breakpoints()
484
_breakpoint_list->clear_rows();
488
void PythonDebugger::ui_add_breakpoint(const char *file, int line, const char *condition)
493
file = g_path_get_basename(file);
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);
506
const char *PythonDebugger::ui_program_stopped(const char *file, int line, int reason)
508
GRTCodeEditor *editor = 0;
509
bool continue_possible = true;
511
if (_pause_clicked && reason == 5)
513
_pause_clicked = false;
519
editor = _shell->get_editor_for(file, true);
522
if (strcmp(file, "<string>") != 0)
523
editor = _shell->show_file_at_line(file, line);
524
if (!editor && reason != 6)
526
_shell->add_output(base::strfmt("continuing from %s:%i...\n", file, line));
533
reason = 0; // treat like we're stepping
535
_program_stopped = true;
543
case 1: // breakpoint
544
// show the cursor position in the editor
546
editor->get_editor()->show_markup(LineMarkupBreakpointHit, line-1);
551
editor->get_editor()->show_markup(LineMarkupError, line-1);
552
continue_possible = false;
554
_shell->activate_output_tab();
558
// schedule for the event loop to exit in 0.01s
560
continue_possible = false;
564
editor->get_editor()->show_markup(LineMarkupCurrent, line-1);
565
_stack_position_editor = editor;
566
_stack_position_line = line-1;
568
_shell->_run_button->show(false);
569
_shell->_continue_button->show(true);
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);
580
_stack_list->set_selected(0);
584
// enter event loop and wait for some action from user
585
int action = mforms::App::get()->enter_event_loop(timeout);
592
case 1: // breakpoint
593
// show the cursor position in the editor
595
editor->get_editor()->remove_markup(LineMarkupBreakpointHit, line-1);
600
editor->get_editor()->remove_markup(LineMarkupError, line-1);
604
if (action < 0) // timeout
605
action = BP_ACTION_CONTINUE;
609
editor->get_editor()->remove_markup(LineMarkupCurrent, line-1);
611
if (_stack_position_editor)
613
_stack_position_editor->get_editor()->remove_markup(LineMarkupCurrent, _stack_position_line);
614
_stack_position_editor = 0;
615
_stack_position_line = 0;
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);
625
_pause_clicked = false;
627
const char *command = "abort";
630
case BP_ACTION_CONTINUE:
631
command = "continue";
638
case BP_ACTION_STEP_INTO:
639
command = "step_into";
646
case BP_ACTION_STEP_OUT:
647
command = "step_out";
650
case BP_ACTION_PAUSE:
651
_pause_clicked = true;
655
default: // error or unknown, abort everything
659
_program_stopped = false;
664
bool PythonDebugger::heartbeat_timeout()
666
_heartbeat_timeout_timer = 0;
667
mforms::App::get()->exit_event_loop(BP_ACTION_CONTINUE);
672
void PythonDebugger::ui_clear_stack()
674
_stack_list->clear_rows();
678
void PythonDebugger::ui_add_stack(const char *location, const char *file, int line)
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));
691
void PythonDebugger::ui_clear_variables()
693
_variable_list->clear_rows();
696
void PythonDebugger::ui_add_variable(const char *varname, const char *value)
698
int row = _variable_list->add_row();
699
_variable_list->set(row, 0, varname);
700
_variable_list->set(row, 1, value);
703
void PythonDebugger::stack_selected()
705
int i = _stack_list->get_selected();
708
if (_stack_position_editor)
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();
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());
723
if (!file.empty() && line > 0)
725
GRTCodeEditor *editor = _shell->show_file_at_line(file, line-1);
726
editor->get_editor()->show_markup(LineMarkupCurrent, line-1);
728
_stack_position_editor = editor;
729
_stack_position_line = line-1;
731
show_frame = -1*(i+1);
734
grt::WillEnterPython lock;
736
grt::AutoPyObject r(PyObject_CallMethod(_pdb, (char*)"wdb_refresh_variables", (char*)"(i)",
741
debug_print("Internal error showing variables\n");
747
bool PythonDebugger::toggle_breakpoint(const char *file, int line)
749
grt::WillEnterPython lock;
751
grt::AutoPyObject r(PyObject_CallMethod(_pdb, (char*)"wdb_toggle_breakpoint", (char*)"(si)",
756
debug_print("Internal error toggling debugger breakpoint\n");
762
if (PyBool_Check(r) && r == Py_True)
764
debug_print(base::strfmt("Added breakpoint to line %i\n", line));
767
debug_print(base::strfmt("Removed breakpoint from line %i\n", line));