-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into challenge-series
- Loading branch information
Showing
4 changed files
with
329 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
--- | ||
publication_date: 2024-08-06T13:37:00Z | ||
slug: gno-debugger | ||
tags: [blog, post, tutorial, gno, debugger] | ||
authors: [mvertes] | ||
--- | ||
|
||
# Debugging Gno Programs | ||
|
||
In this article, we introduce the new Gno debugger feature and show how it can be used to better understand Gno programs and help to fix bugs. | ||
|
||
## Motivation for a Gno debugger | ||
|
||
> Debugging is twice as hard as writing code. | ||
> | ||
> -- <cite>Brian Kerninghan, "The Elements of Programming Style"</cite> | ||
> On average, you spend about eight to ten times debugging as you do writing code. | ||
> | ||
> -- <cite>Anonymous</cite> | ||
Having a good debugger is important. But the Gno language is almost Go, and gno.land itself is entirely written in Go. Could I just use the existing Go tools, i.e. the [delve] debugger, to take control and debug my Gno programs? | ||
|
||
You cannot debug your *Gno* program this way because doing so would entail debugging the Gno virtual machine rather than your own program. The relevant state information would be opaque and would need to be reversed and reconstructed from internal Gno virtual machine data structures. | ||
|
||
The Gno debugger addresses this issue by displaying the state of the Gno program memory symbolically. It allows for control of program execution at the source code level, regardless of the virtual machine implementation. | ||
|
||
## Setting up | ||
|
||
The Gno debugger is fully integrated in the [gno](https://docs.gno.land/gno-tooling/cli/gno-tooling-gno) binary, which is the tool required to build, test, and run Gno programs locally. | ||
|
||
There is no need to install a specific tool. You just have to install the `gno` tool itself with: | ||
|
||
```shell | ||
git clone https://github.com/gnolang/gno | ||
cd gno | ||
go install ./gnovm/cmd/gno | ||
``` | ||
|
||
We are now ready to play with Gno programs. Let's consider a simple classic example as a target program, the computation of [Fibonacci numbers]: | ||
|
||
```go | ||
// fib.gno | ||
package main | ||
|
||
// fib returns the nth number in the Fibonacci sequence. | ||
func fib(n int) int { | ||
if n < 2 { | ||
return n | ||
} | ||
return fib(n-2) + fib(n-1) | ||
} | ||
|
||
func main() { | ||
println(fib(4)) | ||
} | ||
|
||
``` | ||
To execute this program, we run the command `gno run ./fib.gno`. To activate the debugger, we just pass the `-debug` flag: `gno run -debug ./fib.gno`. Use `gno run -help` to get more options if needed. | ||
|
||
## Quick tour of the debugger | ||
|
||
When you start a program in debug mode, you are greeted by a prompt allowing you to interact with it via the terminal: | ||
```shell | ||
$ gno run -debug ./fib.gno | ||
Welcome to the GnoVM debugger. type 'help' for list of commands. | ||
dbg> | ||
``` | ||
|
||
Entering `help` gives you the list of available commands and their short usage: | ||
|
||
```shell | ||
dbg> help | ||
The following commands are available: | ||
|
||
break|b [locspec] Set a breakpoint. | ||
breakpoints|bp Print out info for active breakpoints. | ||
clear [id] Delete breakpoint (all if no id). | ||
continue|c Run until breakpoint or program termination. | ||
detach Close debugger and resume program. | ||
down [n] Move the current frame down by n (default 1). | ||
exit|quit|q Exit the debugger and program. | ||
help|h [command] Print the help message. | ||
list|l [locspec] Show source code. | ||
print|p <expression> Print a variable or expression. | ||
stack|bt Print stack trace. | ||
step|s Single step through program. | ||
stepi|si Single step a single VM instruction. | ||
up [n] Move the current frame up by n (default 1). | ||
|
||
Type help followed by a command for full documentation. | ||
dbg> | ||
``` | ||
If you have already used a debugger before, like [gdb] or [lldb] for C/C++ programs, or [delve] for Go programs, the Gno debugger should look familiar; the commands are similar in their syntax and usage. | ||
The commands can be classified in the following categories: | ||
- managing breakpoints: `break`, `breakpoints`, `clear`, | ||
- controlling execution: `step`, `stepi`, `continue`, | ||
- browsing code, data and stack: `list`, `print`, `stack`, | ||
- navigating the stack: `up`, `down`, | ||
- quitting the debugger: `detach`, `exit`. | ||
## Controlling and exploring the program state | ||
Let's go back to our Fibonacci program, still paused. We `step` a first time, which instructs the GnoVM to execute a single statement and give back control to the user: | ||
```shell | ||
dbg> s | ||
> main.main() main/./fib.gno:11:1 | ||
7: return n | ||
8: } | ||
9: return fib(n-2) + fib(n-1) | ||
10: } | ||
11: | ||
=> 12: func main() { | ||
13: println(fib(4)) | ||
14: } | ||
``` | ||
The first output line `> main.main() main/./fib.gno:11:1` indicates the precise current location in source code, followed by a short source listing around this location. The current line is indicated by the cursor `=>`. | ||
From there, we could repeat `step` commands to progress, but that would be too tedious. Instead, we set a breakpoint at an interesting line in the `fib` function, and `continue` to it directly: | ||
```shell | ||
dbg> b 7 | ||
Breakpoint 0 at main main/./fib.gno:7:1 | ||
dbg> c | ||
> main.fib() main/./fib.gno:7:10 | ||
2: package main | ||
3: | ||
4: // fib returns the nth number in the Fibonacci sequence. | ||
5: func fib(n int) int { | ||
6: if n < 2 { | ||
=> 7: return n | ||
8: } | ||
9: return fib(n-2) + fib(n-1) | ||
10: } | ||
11: | ||
dbg> | ||
``` | ||
Note that we have used the short alias of commands: `b` for `break` and `c` for `continue`. We only need to specify the line number when setting the break point here, due to it being in the same file. Setting break points in other files requires specifying the full file path and line number. | ||
We can now examine the call stack which indicates the successive nested function calls up to the current location: | ||
```shell | ||
dbg> stack | ||
0 in main.fib | ||
at main/./fib.gno:7:10 | ||
1 in main.fib | ||
at main/./fib.gno:9:20 | ||
2 in main.fib | ||
at main/./fib.gno:9:20 | ||
3 in main.main | ||
at main/./fib.gno:13:2 | ||
dbg> | ||
``` | ||
We see a call stack of depth 4, with call frames (local function contexts) numbered from 0 to 3, 0 being the current call level (the deepest). This information is crucial, especially when debugging recursive functions like `fib`. We know that the caller and its caller were both `fib`. | ||
Now we want to examine the value of the local parameter `n`, for each call level: | ||
```shell | ||
dbg> print n | ||
(0 int) | ||
dbg> up | ||
> main.fib() main/./fib.gno:7:10 | ||
Frame 1: main/./fib.gno:9:20 | ||
4: // fib returns the nth number in the Fibonacci sequence. | ||
5: func fib(n int) int { | ||
6: if n < 2 { | ||
7: return n | ||
8: } | ||
=> 9: return fib(n-2) + fib(n-1) | ||
10: } | ||
11: | ||
12: func main() { | ||
13: println(fib(4)) | ||
dbg> print n | ||
(2 int) | ||
dbg> up | ||
> main.fib() main/./fib.gno:7:10 | ||
Frame 2: main/./fib.gno:9:20 | ||
4: // fib returns the nth number in the Fibonacci sequence. | ||
5: func fib(n int) int { | ||
6: if n < 2 { | ||
7: return n | ||
8: } | ||
=> 9: return fib(n-2) + fib(n-1) | ||
10: } | ||
11: | ||
12: func main() { | ||
13: println(fib(4)) | ||
dbg> print n | ||
(4 int) | ||
dbg> | ||
``` | ||
We see that the local value `n` is 0 at current frame 0, 2 at frame 1 and 4 at frame 2, which corresponds to the nested calls of `fib` expressed at line 9. | ||
The `up` and `down` stack navigation commands enable the debugger to display the value of local function variables and parameters for the whole call chain. | ||
In this example, the `n` variable is simply an integer, but the `print` command is also able to handle more complex expressions to uncover the content of arbitrary maps, struct, arrays, etc using the same syntax as Go. For example, `print a.b[n]` will print as expected, with `a` being a value of type: | ||
```go | ||
var a struct { | ||
b []string | ||
} | ||
``` | ||
For security reasons, the `print` command will only evaluate expressions with no side effects on the virtual machine state. For example, it is not possible to perform an arithmetic operation like `print a + 2`, or to call a function like `printf f(6)`. | ||
## Conclusion | ||
We have introduced the new Gno debugger and presented its main capabilities. | ||
This is just the start of a new project, with a lot of room for improvement. The whole Gno project being open source, you are welcome not only to provide feedbacks and suggestions, but also to contribute at https://github.com/gnolang/gno. | ||
[delve]: https://github.com/go-delve/delve | ||
[gno]: https://github.com/gnolang/gno/tree/master/gnovm/cmd/gno | ||
[Fibonacci numbers]: https://simple.wikipedia.org/wiki/Fibonacci_number | ||
[gdb]: https://sourceware.org/gdb/ | ||
[lldb]: https://lldb.llvm.org |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
--- | ||
publication_date: 2024-08-12T00:00:00Z | ||
slug: gc-us24 | ||
tags: [techconference, events, outreach, GoDevelopers, GoToGno, gophers] | ||
authors: [VT-Cosmos] | ||
--- | ||
|
||
# gno.land at GopherCon US 2024 | ||
|
||
GopherCon US is one of the largest events for the Go programming community. | ||
This year, in the vibrant city of Chicago, we had the honor of being the Diamond | ||
Sponsor at GopherCon US 2024. From July 7th to July 10th, we were surrounded by | ||
top Go talent; it was an incredible opportunity for us to connect with developers, | ||
showcase our innovations, and share our expertise. | ||
|
||
GopherCon US 2024 was held at the stunning McCormick Place, bringing together | ||
nearly one thousand Go enthusiasts from around the world. Our team was thrilled | ||
to be part of this gathering, contributing to the vibrant ecosystem of Go | ||
developers. | ||
|
||
## Highlights of Our Collaboration | ||
|
||
### Jae’s Talk | ||
|
||
Jae Kwon, the founder of gno.land, gave a talk on “**Gno: Lessons in Building a | ||
Go Interpreter in Go.**” | ||
|
||
Jae’s talk provided an in-depth overview of why this technology is gaining | ||
traction in the development community. The discussion began with the key reasons | ||
to use Gno, and its seamless interoperability which allows for effortless | ||
integration. Detailed comparisons were made between Gno and other prominent | ||
programming languages such as Solidity and Rust which are running other smart | ||
contract platforms, showcasing Gno’s distinct advantages. The strengths of the | ||
GnoVM were a focal point, particularly its stack based AST architecture, its | ||
auto persistence, as well as its determinism and other features. The presentation | ||
also shared valuable lessons learned from GnoVM's development and implementation, | ||
offering insights into best practices and challenges overcome in the future work. | ||
Be sure to make some time to watch [the recording](https://www.youtube.com/watch?v=betUkghf_jo)! | ||
|
||
### Gno Workshop | ||
|
||
Participants had the opportunity to dive deep into the development process, | ||
guided by our expert engineer **[Dylan Boltz](https://github.com/deelawn)** who hosted a workshop on Community | ||
Day: **Building a decentralized app on gno.land**. The workshop provided practical | ||
insights and step-by-step guidance, empowering attendees to start building their | ||
own applications. If you missed our workshop or want to revisit the session, you | ||
can find a recorded version of the workshop [here](https://www.youtube.com/watch?v=lwL2VyjaV-A). | ||
|
||
### The gno.land Booth | ||
One of the most rewarding feelings about attending these kinds of conferences | ||
is watching our booth quickly become a hub of activity, drawing a steady stream | ||
of visitors intrigued by our project. We engaged with developers from various | ||
backgrounds, answering a myriad of questions about Gno, gno.land, our company, | ||
and the company’s [open employment opportunities](https://jobs.lever.co/allinbits). | ||
Having all of this direct human interaction was not only informative but also | ||
deeply insightful, providing us with valuable feedback and ideas. For more | ||
information, check out our [Official Documentation](https://docs.gno.land/). | ||
|
||
### The Gno Raffle | ||
|
||
One of the major attractions at our booth, amongst the “gnome” beanie hat as | ||
well as the T-shirts, was the raffle for a high-end mechanical keyboard. The | ||
raffle participants had a direct opportunity to interact with gno.land by | ||
following the raffle instructions, leading them to use [Gno Playground](https://play.gno.land/) to import | ||
and deploy a smart contract raffle realm from their own laptop. The excitement | ||
was palpable as attendees eagerly gathered for the drawing. The raffle not only | ||
drew crowds, but also sparked numerous engaging conversations that led to Go | ||
engineers giving Gno a try. | ||
|
||
### The Challenge Series | ||
|
||
This year we had the privilege to participate in a collaborative partnership | ||
with the CodePro team to build the [challenge series](https://gophercon.challengeseries.org/). This collaboration was an | ||
opportunity for participants to learn how to interact with blockchain and | ||
discover how realms (smart contracts) can be utilized for stateful applications | ||
without relying on explicitly managing a file system or database. This was an | ||
opportunity to showcase gno.land’s features, like using a deployed contract as | ||
both an API and as an import for other contracts. | ||
|
||
## Conclusion | ||
|
||
Overall, the enthusiasm of the attendees at GopherCon was infectious, and we | ||
were delighted to see such a high level of engagement and curiosity. Whether | ||
it was at our booth, during the raffle, or in the workshop, the interactions | ||
were meaningful and enriching. Sponsoring and collaborating at GopherCon US | ||
2024 was an unforgettable experience. We are grateful for the opportunity to | ||
connect with the Go community, share our knowledge, and learn from fellow | ||
developers. We extend our heartfelt thanks to everyone who visited our booth, | ||
participated in the raffle, attended Jae Kwon's presentation on Gno, and joined | ||
our workshop. | ||
|
||
We invite you to stay connected with us on our [Discord](https://discord.gg/43HC5NZzHe), | ||
and our [blog](https://gno.land/r/gnoland/blog) where we | ||
will be sharing more insights and updates on | ||
[Test4](https://gno.land/r/gnoland/blog:p/test4-live), our current testnet, as | ||
well as the progress towards our launch. | ||
|
||
Thank you, [GopherCon US 2024](https://x.com/gophercon), for an incredible experience. | ||
We can't wait to see what the next event holds! | ||
|