headcrab-rs / headcrab

A modern Rust debugging library 🦀
https://headcrab.rs
Other
888 stars 32 forks source link

Implement source mapping #107

Closed blitzerr closed 4 years ago

blitzerr commented 4 years ago

Fixes: #100

change summary

(+) Print source lines when pausing on a breakpoint. (+) New command list or l (+) Some code refactoring. (+) Syntax highlighting of code snippet.


currently if you debug your program with lldb, and say your program stops at the set breakpoint, lldb will print the source line and not just filename and line number. It might be more clear with an example, so here goes one:

rust-lldb tests/testees/hello
breakpoint set --file hello.rs --line 22
r

(lldb) r
Process 58912 launched: '/workspaces/headcrab/tests/testees/hello' (x86_64)
Process 58912 stopped
* thread #1, name = 'hello', stop reason = breakpoint 2.1
    frame #0: 0x0000555555559301 hello`hello::main::hd078db076938ab99 at hello.rs:22
   19           let mut temp = 100usize;
   20           black_box(&mut temp);
   21       }
-> 22       black_box(reg_var);
   23       breakpoint();
   24       black_box(reg_var);
   25       println!("{} {}", STATICVAR, var);

Before this PR, the behavior of headcrab for a similar steps of execution would have been:

(headcrab) cargo run --example repl
(headcrab) exec tests/testees/hello
(headcrab) _patch_breakpoint_function
(headcrab) cont
(headcrab)

So, you can see, after cont(inuing) to the next breakpoint, headcrab is silent. You have to give the command bt to print the backtrace.

(headcrab) bt
0000555555559295 core::core_arch::x86::sse2::_mm_pause /usr/local/rustup/toolchains/1.45.2-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/../stdarch/crates/core_arch/src/x86/sse2.rs:25
                 breakpoint /workspaces/headcrab/tests/testees/hello.rs:7
000055555555930b hello::main /workspaces/headcrab/tests/testees/hello.rs:24
0000555555559246 std::rt::lang_start::{{closure}} /usr/local/rustup/toolchains/1.45.2-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:67
0000555555562c68 std::panicking::try /rustc/d3fb005a39e62501b8b0b356166e515ae24e2e54/src/libstd/panicking.rs:275
                 std::panic::catch_unwind /rustc/d3fb005a39e62501b8b0b356166e515ae24e2e54/src/libstd/panic.rs:394
                 std::rt::lang_start_internal /rustc/d3fb005a39e62501b8b0b356166e515ae24e2e54/src/libstd/rt.rs:51

After the change, the output will be something like this:

(headcrab) exec tests/testees/hello
Starting program: tests/testees/hello
Stopped(Pid(75698), SIGTRAP)
(headcrab) _patch_breakpoint_function
(headcrab) cont
Stopped(Pid(75698), SIGTRAP)
0000555555559295 core::core_arch::x86::sse2::_mm_pause /usr/local/rustup/toolchains/1.45.2-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/../stdarch/crates/core_arch/src/x86/sse2.rs:25
/workspaces/headcrab/tests/testees/hello.rs:7:14
    4 #[inline(never)]
    5 fn breakpoint() {
    6     // This will be patched by the debugger to be a breakpoint
>   7     unsafe { core::arch::x86_64::_mm_pause(); }
    8 }
    9
   10 #[inline(never)]
(headcrab)

So, as you can see, now when we pause at the break-point, we also print the source code around it.

blitzerr commented 4 years ago

@bjorn3 @nbaksalyar r?

blitzerr commented 4 years ago

@nbaksalyar @bjorn3 the build is failing on test target::linux::writemem::tests::write_protected_memory ... FAILED

This patch should not impact that, right ?

bjorn3 commented 4 years ago

Strange.

blitzerr commented 4 years ago

It didn't fail earlier but after rebasing ..

blitzerr commented 4 years ago

@bjorn3 and @nbaksalyar You comments are addressed and rebased with the latest.

nbaksalyar commented 4 years ago

the build is failing on test target::linux::writemem::tests::write_protected_memory ... FAILED

Hmm, probably an intermittent error related to #95 - I'll try to run separate fuzz-like tests with that.

blitzerr commented 4 years ago

@nbaksalyar r?

bjorn3 commented 4 years ago

Please run cargo fmt.

nbaksalyar commented 4 years ago

Hmm, the build seems to be failing:

error[E0252]: the name `HighlightAndComplete` is defined multiple times
  --> examples/repl.rs:30:9
   |
29 |     use repl_tools::HighlightAndComplete;
   |         -------------------------------- previous import of the trait `HighlightAndComplete` here
30 |     use repl_tools::HighlightAndComplete;
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `HighlightAndComplete` reimported here
   |
   = note: `HighlightAndComplete` must be defined only once in the type namespace of this module

https://travis-ci.com/github/headcrab-rs/headcrab/jobs/383054168#L426-L444

bjorn3 commented 4 years ago

Thanks @blitzerr!

blitzerr commented 4 years ago

My first PR merged :D. Thank you @nbaksalyar and @bjorn3