Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to directly run the program built by Cargo in gdb or lldb?

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.

like image 679
Alex Avatar asked Oct 07 '18 05:10

Alex


People also ask

Is LLDB compatible with GDB?

The standard LLDB installation provides you with an extensive set of commands designed to be compatible with familiar GDB commands. In addition to using the standard configuration, you can easily customize LLDB to suit your needs. Both GDB and LLDB are of course excellent debuggers without doubt.

Is LLDB the same as GDB?

In brief, LLDB and GDB are two debuggers. The main difference between LLDB and GDB is that in LLDB, the programmer can debug programs written in C, Objective C and C++ while, in GDB, the programmer can debug programs written in Ada, C, C++, Objective C, Pascal, FORTRAN and Go.

How do you set a breakpoint in LLDB?

In lldb you can set breakpoints by typing either break or b followed by information on where you want the program to pause. After the b command, you can put either: a function name (e.g., b my_subroutine ) a line number (e.g., b 12 )


1 Answers

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:

  • How can I run cargo tests on another machine without the Rust compiler?
  • How to debug Rust unit tests on Windows?
like image 126
Shepmaster Avatar answered Oct 07 '22 01:10

Shepmaster