Skip to content

Commit

Permalink
Python debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
Atsuo Ishimoto committed Nov 27, 2013
1 parent 302296a commit eb27fa1
Show file tree
Hide file tree
Showing 10 changed files with 347 additions and 106 deletions.
82 changes: 82 additions & 0 deletions README-JA.rst
Expand Up @@ -525,6 +525,88 @@ Pythonの対話コンソールとは違い、スクリプト入力欄でエン


実行した時、スクリプトが式として評価できる場合は、評価結果を出力ウィンドウに表示します。また、スクリプトが `print()` などで標準出力・標準エラー出力に出力した結果も表示されます。 実行した時、スクリプトが式として評価できる場合は、評価結果を出力ウィンドウに表示します。また、スクリプトが `print()` などで標準出力・標準エラー出力に出力した結果も表示されます。


Python デバッガ
---------------

起動方法
++++++++++++++++++

Pythonデバッガは、以下のいずれかの方法で起動します。

リモートランナー
~~~~~~~~~~~~~~~~~~~~~~~~

リモートランナーを利用して、kaa外部で実行するコマンドをkaaデバッガに接続し、デバッグを行えます。

まず、kaa のメニューで ``[Tools]|Python debugger server`` を選択し、表示されるフォームにデバッガで使用するポート番号を指定します。

次に、kaa を実行しているターミナルとは別のターミナルを開き、以下のコマンドを実行します。

::

$ python -m kaadbg.run my_test_stript.py arg1 args


``kaadbg.run`` は、kaa に同梱されているモジュールで、kaaデバッガに接続するために使用します。kaaデバッガのポートとして `28110` 以外を指定した場合は、コマンとライン引数 ``-p`` でポート番号を指定してください。

::

$ python -m kaadbg.run -p 29000 my_test_stript.py arg1 args

set_trace
~~~~~~~~~~~~~~~~~~~~~~~~

リモートランナをデバック対象のプログラムにインポートし、Python標準の ``pdb`` モジュールのように ``set_trace()`` でデバッガに接続できます。

まず、kaa のメニューで ``[Tools]|Python debugger server`` を選択し、表示されるフォームにデバッガで使用するポート番号を指定します。

デバック対象プログラムの、デバッガを起動したい位置に以下の処理を追加します。

.. code:: python
import kaadbg.debug
kaadbg.debug.set_trace()
kaaデバッガのポートとして `28110` 以外を指定した場合は、``set_trace()`` の引数として指定します。

.. code:: python
import kaadbg.debug
kaadbg.debug.set_trace(29000)
テスト対象プログラムを起動し、``set_trace()`` が実行されるとkaaデバッガに表示されます。


子プロセスとして起動
~~~~~~~~~~~~~~~~~~~~~~~~

テスト対象プログラムを kaa の子プロセスとして起動し、デバッグします。

まず、kaa のメニューで ``[Tools]|Python debugger`` を選択し、テストコマンドを指定します。テストコマンドは、

::

python3.3 -m kaadbg.run myscript.py arg1 arg2

のように、``python3.3 -m kaadbg.run`` の引数として、テスト対象のスクリプトとパラメータを指定して実行します。

この方法でデバッグを実行すると、対象プログラムの標準入力はすぐにクローズされます。また、標準出力・エラー出力も表示されません。


ブレークポイント
++++++++++++++++++

ブレークポイントのオン・オフは、Pythonモードでメニューの ``[Code]|Toggle Breakpoint`` を選択して行います。デフォルトのキー設定では、``f8`` キーに割り当てられています。

デバッガウィンドウが表示されている時は、エスケープキーで一旦デバッガウィンドウを閉じ、エディタウィンドウでブレークポイントを設定します。設定終了後、もう一度メニューで ``[Tools]|Python debugger`` を選択すると、デバッグを再開できます。

ブレークポイントの一覧は、デバッガウィンドウの ``Breakpoint`` で表示します。

変数の表示
++++++++++++++++++

停止位置での変数の表示は、デバッガウィンドウの ``Expr`` を指定し、``self.field1`` など、表示する式を入力してエンターキーを押します。



Make Make
-------------- --------------
Expand Down
79 changes: 79 additions & 0 deletions README.rst
Expand Up @@ -533,6 +533,85 @@ Make


Output of ``make`` displayed on the window. You can traverse source files cause of the error forth and back with f9 and f10 key. Output of ``make`` displayed on the window. You can traverse source files cause of the error forth and back with f9 and f10 key.


Python debugger
---------------

Starting debugger
++++++++++++++++++

There are three way to start debugger.

Remote runner
~~~~~~~~~~~~~~~~~~~~~~~~

Remote runnner executes Python script and connect to kaa debugger.

To activate kaa remote debugger, select ``[Tools]|Python debugger server`` and enter port number to connect debugger(default 28110).

Next, open another terminal window other than kaa running, run following command.

::

$ python -m kaadbg.run my_test_stript.py arg1 args


``kaadbg.run`` is Python module bundled with kaa, which connects Python script specified in argument to kaa debugger. If you need use other port than `28110`, you should provide port number with ``-p`` option.

::

$ python -m kaadbg.run -p 29000 my_test_stript.py arg1 args

set_trace
~~~~~~~~~~~~~~~~~~~~~~~~

Like Python's standard ``pdb`` module, you can import remote runner and call ``set_trace()`` to start debug session.

You should start activate kaa remote debugger by menu ``[Tools]|Python debugger server`` and enter port number to connect debugger(default 28110).

To connect kaa remote debugger, open your target script and insert following lines of code.

.. code:: python
import kaadbg.debug
kaadbg.debug.set_trace()
If you need use other port than `28110`, you should provide port number to ``set_trace()``.

.. code:: python
import kaadbg.debug
kaadbg.debug.set_trace(29000)
Now you can start your target script. Kaa remote debugger will be opened when ``kaadbg.debug.set_trace()`` is hit.


Run child process
~~~~~~~~~~~~~~~~~~~~~~~~

You can run your target script as child process of kaa to debug.

To start child process, select ``[Tools]|Python debugger`` in kaa menu and specify command line as follow.

::

python3.3 -m kaadbg.run myscript.py arg1 arg2

Command line should starts with Python interpreter you use and ``-m kaadbg.run``. Name of target script and arguments follows.

Note that kaa doesn't capture standard output and standard error of target process, so you cannot see outputs of the target script. Also, standard input of the target process is closed just after command started.

Breakpoints
++++++++++++++++++

To set/unset breakpoints, select ``[Code]|Toggle Breakpoint`` in menu in editor. By default, ``f8`` key is bounded to this menu item.

While debugger window is opend, you can suspend the debugger window with escape key. After you finish to update breakpoints with editor window, select ``[Tools]|Python debugger`` menu again to resume debugger.

Inspect variables
++++++++++++++++++

To see value of variables, select ``Expr`` on the debugger window by pressing ``alt+E`` key and enter Python expression you want to inspect like ``self.spam``.



Customization Customization
================== ==================
Expand Down
2 changes: 1 addition & 1 deletion kaa/cui/color.py
Expand Up @@ -377,7 +377,7 @@ def __init__(self):
super().__init__() super().__init__()


self.COLOR256_TO_16[self.BASE02] = curses.COLOR_BLUE self.COLOR256_TO_16[self.BASE02] = curses.COLOR_BLUE
self.COLOR256_TO_16[self.BASE3] = curses.COLOR_BLUE self.COLOR256_TO_16[self.BASE3] = curses.COLOR_CYAN
self.COLOR256_TO_16[self.YELLOW] = curses.COLOR_YELLOW self.COLOR256_TO_16[self.YELLOW] = curses.COLOR_YELLOW
self.COLOR256_TO_16[self.WHITE] = curses.COLOR_WHITE self.COLOR256_TO_16[self.WHITE] = curses.COLOR_WHITE


Expand Down
2 changes: 1 addition & 1 deletion kaa/cui/editor.py
Expand Up @@ -85,7 +85,7 @@ def set_highlight_cursor_row(self, f):
return ret return ret


def set_line_overlay(self, pos, overlay): def set_line_overlay(self, pos, overlay):
if pos: if pos is not None:
if self.line_overlays.get(pos, '') != overlay: if self.line_overlays.get(pos, '') != overlay:
self.line_overlays[pos] = overlay self.line_overlays[pos] = overlay
self._drawn_rows = {} self._drawn_rows = {}
Expand Down
4 changes: 2 additions & 2 deletions kaa/filetype/default/menu.py
Expand Up @@ -54,8 +54,8 @@
['&Shell command', None, 'tools.execute-shell-command'], ['&Shell command', None, 'tools.execute-shell-command'],
['&Make', None, 'tools.make'], ['&Make', None, 'tools.make'],
['&Python console', None, 'python.console'], ['&Python console', None, 'python.console'],
['Run Python &Debugger', None, 'python.debugger.run'], ['Python &Debugger', None, 'python.debugger.run'],
['Python Debugger s&erver', None, 'python.debugger.server'], ['Python debugger s&erver', None, 'python.debugger.server'],
], ],


'WINDOW': 'WINDOW':
Expand Down

0 comments on commit eb27fa1

Please sign in to comment.