[rust-dev] Debugging rust for a newbie

Huon Wilson dbau.pp at gmail.com
Wed Jul 23 01:02:42 PDT 2014


It is unlikely to be a lifetimes thing; far, far more likely to be a 
"normal" infinite recursion. The size of the stack frame of each 
function is fixed at compile time, so the way to blow the stack is by 
calling a lot of functions deeply, e.g. it's not possible to write a 
loop that places more and more objects on the stack (not in safe code, 
anyway).

You can get a backtrace by running the test in a conventional debugger, 
e.g. `gdb --args ./tester produces_a_move`, then type `run`. When it 
hits the abort, gdb will freeze execution and you can run `backtrace` to 
see the function call stack, to see what is recursing deeply.

You can make rustc emit debug info which makes gdb far more useful, by 
compiling with `-g` or, equivalently, `--debuginfo=2`. (Depending on 
your platform, 'lldb' may be better.)


If all else fails, you can fall back to println debugging, e.g.

     fn gen_move(&self, ...) -> Move {
         println!("calling gen_move");

         // ...
     }

---

Just glancing over your code, it looks like there's mutual recursion 
between Playout::run and McEngine::gen_move:

- McEngine::gen_move calls Playout::run 
https://github.com/ujh/iomrascalai/blob/88e09fdd/src/engine/mc/mod.rs#L82
- Playout::run calls Playout::gen_move 
https://github.com/ujh/iomrascalai/blob/88e09fdd/src/playout/mod.rs#L42
- Playout::gen_move calls McEngine::gen_move 
https://github.com/ujh/iomrascalai/blob/88e09fdd/src/playout/mod.rs#L49


Huon


On 23/07/14 17:42, Urban Hafner wrote:
> Hey there,
>
> I'm still quite new to Rust. Until now I was able to fix all my bugs 
> by writing tests and/or randomly adding lifetime parameters to keep 
> the compiler happy. Now I've hit my first stack overflow. I assume 
> it's due to the fact that I've screwed up the lifetimes and the 
> objects live too long although I'm not even sure about that. Now my 
> question is: How do I debug this? Is there a way to figure out how 
> long objects live? Or how would one go about debugging this?
>
> Oh, if you're interested in the failing code: 
> https://github.com/ujh/iomrascalai/pull/46
>
> Cheers,
>
> Urban
> -- 
> Freelancer
>
> Available for hire for Ruby, Ruby on Rails, and JavaScript projects
>
> More at http://urbanhafner.com
>
>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140723/c0593310/attachment.html>


More information about the Rust-dev mailing list