<p>The Bitcoin Core test <a href="https://github.com/bitcoin/bitcoin/tree/master/test#attaching-a-debugger" rel="nofollow noreferrer">documentation</a> provides some guidance on attaching a debugger. The functional tests are in Python so you are correctly choosing pdb (rather than gdb, lldb etc) for the job.</p>
<p>A python debugger can be attached to tests at any point. Just add the line:</p>
<p><code>import pdb; pdb.set_trace()</code></p>
<p>anywhere in the test. You will then be able to inspect variables, as well as call methods that interact with the bitcoind nodes-under-test.</p>
<p>If you're unsure where is a good point to attach the debugger one good place is after the <code>random_checksig_style(pubkey)</code> helper is defined in <a href="https://github.com/sipa/bitcoin/blob/taproot/test/functional/feature_taproot.py" rel="nofollow noreferrer">feature_taproot.py</a>. This creates a random CHECKSIG Tapscript that requires only a valid signature on the witness stack.</p>
<p>There is a good video tutorial <a href="https://www.youtube.com/watch?v=bZZTeKPRSLQ" rel="nofollow noreferrer">here</a> from Simeon Franklin on pdb here. There are a large number of pdb commands but the key ones you are most likely to use are:</p>
<pre><code>l(ist)
n(ext)
c(ontinue)
s(tep)
r(eturn)
b(reak)
</code></pre>
<p>As Franklin states:</p>
<blockquote>
<p><code>l</code> shows you where you are in your code. <code>n</code> steps to the next line of execution. <code>c</code> leaves debugger control and executes your code normally. <code>s</code> steps into the next line of execution including into function calls if they exist. <code>r</code> steps to the end of the currently executing function and <code>b</code> sets breakpoints. It is important to realize when you are debugging that from the pdb console you can type in any valid Python, changing variables or execution flow of your program and inspecting their values.</p>
</blockquote>
<p>(Also <code>p expression</code> for printing an expression value or <code>pp expression</code> for pretty printing an expression value. And <code>whatis expression</code> for printing the type of that expression).</p>
<p>More detailed guidance on using pdb is on <a href="https://docs.python.org/3/library/pdb.html" rel="nofollow noreferrer">python.org</a> but the above should be a good introduction.</p>
<p>Remember that you are making edits to Pieter Wuille's <code>taproot</code> branch locally and will not ever push them to GitHub so you can make whatever changes you want. If you want to revert back to Pieter Wuille's code before you started making edits (and discard all of your edits) you can run</p>
<pre><code>git reset --hard HEAD
</code></pre>
<p>and then you should be back to the state before you started experimenting.</p>

CC BY-SA 4.0
2020-10-04T12:50:00.823Z
2021-01-03T18:30:45.687Z
2021-01-03T18:30:45.687Z
Michael Folkson
<p>I am looking at the some of the Taproot functional tests such as <a href="https://github.com/sipa/bitcoin/blob/taproot/test/functional/feature_taproot.py" rel="nofollow noreferrer">feature_taproot.py</a> to review the <a href="https://bitcoin.stackexchange.com/questions/99306/i-would-like-to-dig-into-the-open-taproot-pr-to-understand-it-test-it-and-possi">open Taproot PR</a>.</p>
<p>I would like to use <a href="https://docs.python.org/3/library/pdb.html" rel="nofollow noreferrer">pdb</a> to debug these tests. Can you provide some guidance on how to do this?</p>

<bitcoin-core-development><taproot><bitcoincore-review><debugging><functional-test>
How do I debug Taproot functional tests using pdb?
Michael Folkson
# How do I debug Taproot functional tests using pdb?

(Asked by: username_0 on 2020-10-04)

I am looking at the some of the Taproot functional tests such as [feature\_taproot.py](https://github.com/sipa/bitcoin/blob/taproot/test/functional/feature_taproot.py) to review the [open Taproot PR](https://bitcoin.stackexchange.com/questions/99306/i-would-like-to-dig-into-the-open-taproot-pr-to-understand-it-test-it-and-possi).

I would like to use [pdb](https://docs.python.org/3/library/pdb.html) to debug these tests. Can you provide some guidance on how to do this?

**Tags:** bitcoin-core-development, taproot, bitcoincore-review, debugging, functional-test


---

## 1 answers

### Answer by username_0 on 2020-10-04. Score: 0 | Accepted answer

The Bitcoin Core test [documentation](https://github.com/bitcoin/bitcoin/tree/master/test#attaching-a-debugger) provides some guidance on attaching a debugger. The functional tests are in Python so you are correctly choosing pdb (rather than gdb, lldb etc) for the job.

A python debugger can be attached to tests at any point. Just add the line:

`import pdb; pdb.set_trace()`

anywhere in the test. You will then be able to inspect variables, as well as call methods that interact with the bitcoind nodes-under-test.

If you're unsure where is a good point to attach the debugger one good place is after the `random_checksig_style(pubkey)` helper is defined in [feature\_taproot.py](https://github.com/sipa/bitcoin/blob/taproot/test/functional/feature_taproot.py). This creates a random CHECKSIG Tapscript that requires only a valid signature on the witness stack.

There is a good video tutorial [here](https://www.youtube.com/watch?v=bZZTeKPRSLQ) from Simeon Franklin on pdb here. There are a large number of pdb commands but the key ones you are most likely to use are:

```
l(ist)
n(ext)
c(ontinue)
s(tep)
r(eturn)
b(reak)

```

As Franklin states:

> `l` shows you where you are in your code. `n` steps to the next line of execution. `c` leaves debugger control and executes your code normally. `s` steps into the next line of execution including into function calls if they exist. `r` steps to the end of the currently executing function and `b` sets breakpoints. It is important to realize when you are debugging that from the pdb console you can type in any valid Python, changing variables or execution flow of your program and inspecting their values.

(Also `p expression` for printing an expression value or `pp expression` for pretty printing an expression value. And `whatis expression` for printing the type of that expression).

More detailed guidance on using pdb is on [python.org](https://docs.python.org/3/library/pdb.html) but the above should be a good introduction.

Remember that you are making edits to Pieter Wuille's `taproot` branch locally and will not ever push them to GitHub so you can make whatever changes you want. If you want to revert back to Pieter Wuille's code before you started making edits (and discard all of your edits) you can run

```
git reset --hard HEAD

```

and then you should be back to the state before you started experimenting.


---

