14

Is there a way to run the program built by Cargo immediately in gdb? cargo has lots of functions and can run the program, so it seems plausible.

The expected command would be something like cargo debug.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Alex
  • 9,891
  • 11
  • 53
  • 87

1 Answers1

11

No, there's nothing like this currently built into Cargo.

There's a few issues (1, 2) to better support similar issues.

The best thing you could do at the moment would be to write a Cargo subcommand that does exactly what you need.

A workaround

Without creating a subcommand, you can glue together a few features to get something close.

Start by configuring a custom runner for your architecture.

.cargo/config

[target.x86_64-apple-darwin]
runner = ["/tmp/gg/debugger.sh"]

Then write a little script to be the test runner. If an environment variable is set, it will start the debugger, otherwise it will just run the program:

#!/bin/bash

if [[ -z $DEBUG ]]; then
    exec $*
else
    exec lldb $*
fi

Then you just need to set the variable:

$ cargo test
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s
     Running target/debug/deps/gg-e5d6c92730ca3c30

running 0 tests

$ DEBUG=1 cargo test
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running target/debug/deps/gg-e5d6c92730ca3c30
(lldb) target create "/private/tmp/gg/target/debug/deps/gg-e5d6c92730ca3c30"
Current executable set to '/private/tmp/gg/target/debug/deps/gg-e5d6c92730ca3c30' (x86_64).
(lldb)

See also:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366