Tracing code execution line by line

I just built a function containing some nested if-else statements and, of course, it isn't doing what I think it should do. No surprise there, but to help track down the problem it would be nice if I could step through the function line by line. Way back in the day when I was first learning to use C there was a tool called "trace" that I could use to do just that. Worked great. Is there something similar in the Rust world that would help me with this? Thanks.

They are called debuggers. You should be able to find a few for Rust.

So, I take it that there isn't anything in the standard Rust package that does that. I thought maybe Cargo might have something, although I couldn't find anything in the Cargo book. My search came up with the Gnome GDB, so maybe I'll check it out first.

Thanks for replying.

Are you on Windows or Linux? If Windows, are you using WSL?

Hi preacherdad,

I'm using Visual Studio Code ( VSC ) on Windows 10, I've recently learned the debugger, I followed these two articles:

But prior to this, I've already been using the rust-analyzer VSC extension, I think the debugger needs this. But it is very useful during code writing as well.

I hope this helps.

Good luck and best regards,

...behai.

1 Like

Hi user16251,

While I haven't used Visual Studio Code on Linux, but I believe we could install an extension called CodeLLDB to do debugging for Rust, please see How to Debug Rust with Visual Studio Code.

In regard to WSL, I do have it installed on my Windows 10, but I don't use it for Rust. I write Rust straight in Windows 10, and the code will compile and work on Ubuntu 22.10 and vice versa. So I believe WSL is not necessary as such.

Best regards,

...behai.

1 Like

I'm using Linux and I don't think Visual Studio Code has a Linux version. I use Sublime Text for my editor. Not sure what WSL is. Am I right that it is Windows-based?

Here it is. I'm pretty sure its one of the the most popular IDEs on Linux.

1 Like

So, are you saying that Visual Studio Code has a feature that will let me step through my code line by line so I can find my problem? My expectation has been that any tool that can do that would have to, somehow, be connected to the Rust compiler.

Speaking of the Rust compiler, I found this. I very quickly bogged down just trying to scan through the article. It doesn't sound like a lot of progress has been made towards getting good debugging tools working. Is that interpretation correct?

The Rust compiler generates standardized debug info, either inside the executable or as separate files depending on the platform, which the debugger can then read to associate machine code addresses with source code lines. Therefore, the debugger does not necessarily need to know anything about Rust (though that can be helpful for printing the values of variables and fields).

3 Likes

Yes, it has a debugger.

@behai gave you this info above, BTW.

2 Likes

As noted VS Code works fine on Linux and Mac OS by the way. I use nothing but VS Code and Vim everywhere.

The WSL is Windows Subsystem for Linux. It is built into Windows since Win 10. WSL allows running of Linux distributions on Windows. WSL on Win 10 was the thing allowed me to use Windows machines at all for development for some years.

WSL was an odd name in that it did not actually have a Linux kernel originally, it adapted system calls to Windows. Amazingly that worked really well and was great for Linux development as long as you did not actually need Linux kernel features.

I hear recent WSL has become a virtual machine that does run an actual Linux kernel. I have not tried it as we have become a Mac shop in recent years, still developing for Linux mind.

2 Likes

You can use VSCode on Linux and I'd highly recommend taking the time to set it up properly including the extensions others have recommended. But you can also just run gdb or lldb in the terminal and that is a useful skill to cultivate.

1 Like

Hi,

I did not know this. I have not come across any searches either. Thank you. I will check it out.

Best regards,

...behai.

It's useful even in VSCode when you need to do something the GUI doesn't support. On Linux the gdb manual is accessible with info gdb. I have the reference card on my desk.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.