Skip to content

Commit

Permalink
chore: lldb bb debugging helper script (#7627)
Browse files Browse the repository at this point in the history
Pretty-print tricky to debug field types
  • Loading branch information
ludamad authored Jul 31, 2024
1 parent fee7efd commit f35786a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
12 changes: 12 additions & 0 deletions barretenberg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,15 @@ Stack trace (most recent call last):
25: blocks.arithmetic.q_2().emplace_back(in.b_scaling);
gate number4
```


### Improving LLDB Debugging

It can be quite hard to make sense of field_t circuit values that indirectly reference their contents, and even plain field values that are typically in montgomery form.
In command-line LLDB or VSCode debug console, run:

```
command script import ~/aztec-packages/barretenberg/cpp/scripts/lldb_format.py
```

Now when you `print` things with e.g. `print bigfield_t.get_value()` or inspect in VSCode (if you opened the debug console and put in these commands) then you will get pretty-printing of these types. This can be expanded fairly easily with more types if needed.
45 changes: 45 additions & 0 deletions barretenberg/cpp/scripts/lldb_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import lldb

# Define the bn254 modulus
N = 21888242871839275222246405745257275088548364400416034343698204186575808495617

# Define R as a power of 2 such that R > N (commonly used R for bn254 is 2^256)
R = 2**256

# Compute R inverse modulo N
R_inv = pow(R, -1, N)

def montgomery_to_standard(montgomery_value):
# Convert from Montgomery form to standard representation
standard_value = (montgomery_value * R_inv) % N
return standard_value

def montgomery_summary(valobj, internal_dict):
try:
data = valobj.GetChildMemberWithName('data')
data_0 = data.GetChildAtIndex(0).GetValueAsUnsigned()
data_1 = data.GetChildAtIndex(1).GetValueAsUnsigned()
data_2 = data.GetChildAtIndex(2).GetValueAsUnsigned()
data_3 = data.GetChildAtIndex(3).GetValueAsUnsigned()

montgomery_value = (
data_0 +
(data_1 << 64) +
(data_2 << 128) +
(data_3 << 192)
)

standard_value = montgomery_to_standard(montgomery_value)
return hex(standard_value)
except Exception as e:
return f"Error: {e}"


def montgomery_summary2(valobj, internal_dict):
return montgomery_summary(valobj.EvaluateExpression("get_value()"), internal_dict)


def __lldb_init_module(debugger, internal_dict):
debugger.HandleCommand("type summary add --python-function lldb_format.montgomery_summary bb::fr")
debugger.HandleCommand("type summary add --python-function lldb_format.montgomery_summary2 -x \"bb::stdlib::field_t.*\"")
print('The "formatter" command has been installed!')

0 comments on commit f35786a

Please sign in to comment.