Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement source mapping #107

Merged
merged 15 commits into from Sep 10, 2020
Merged

Conversation

blitzerr
Copy link
Contributor

@blitzerr blitzerr commented Sep 5, 2020

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
Copy link
Contributor Author

blitzerr commented Sep 5, 2020

@bjorn3 @nbaksalyar r?

Copy link
Contributor

@bjorn3 bjorn3 left a comment

Nice!

examples/repl.rs Outdated Show resolved Hide resolved
examples/repl.rs Show resolved Hide resolved
examples/repl.rs Outdated Show resolved Hide resolved
examples/repl.rs Outdated Show resolved Hide resolved
examples/repl.rs Outdated Show resolved Hide resolved
examples/repl.rs Outdated Show resolved Hide resolved
examples/repl.rs Outdated Show resolved Hide resolved
examples/repl.rs Outdated Show resolved Hide resolved
Copy link
Member

@nbaksalyar nbaksalyar left a comment

Nice one, thank you!
I left a few minor suggestions and noticed a few typos.

examples/repl.rs Outdated Show resolved Hide resolved
examples/repl.rs Outdated Show resolved Hide resolved
examples/repl.rs Outdated Show resolved Hide resolved
examples/repl.rs Outdated Show resolved Hide resolved
examples/repl.rs Show resolved Hide resolved
examples/repl.rs Outdated Show resolved Hide resolved
@blitzerr
Copy link
Contributor Author

blitzerr commented Sep 6, 2020

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

This patch should not impact that, right ?

@bjorn3
Copy link
Contributor

bjorn3 commented Sep 6, 2020

Strange.

@blitzerr
Copy link
Contributor Author

blitzerr commented Sep 6, 2020

It didn't fail earlier but after rebasing ..

@blitzerr
Copy link
Contributor Author

blitzerr commented Sep 6, 2020

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

@nbaksalyar
Copy link
Member

nbaksalyar commented Sep 6, 2020

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.

examples/repl.rs Outdated Show resolved Hide resolved
examples/repl.rs Show resolved Hide resolved
examples/repl.rs Outdated Show resolved Hide resolved
examples/repl.rs Outdated Show resolved Hide resolved
examples/repl.rs Outdated Show resolved Hide resolved
examples/repl.rs Outdated Show resolved Hide resolved
examples/repl.rs Outdated Show resolved Hide resolved
Copy link
Member

@nbaksalyar nbaksalyar left a comment

LGTM, thank you! I've just one minor comment about commented-out code

examples/repl.rs Outdated Show resolved Hide resolved
examples/repl.rs Outdated Show resolved Hide resolved
src/symbol/source.rs Outdated Show resolved Hide resolved
src/symbol/source.rs Show resolved Hide resolved
examples/repl.rs Show resolved Hide resolved
examples/repl.rs Show resolved Hide resolved
@blitzerr blitzerr changed the title [WIP] Implement source mapping Implement source mapping Sep 7, 2020
bjorn3
bjorn3 approved these changes Sep 7, 2020
@bjorn3 bjorn3 requested a review from nbaksalyar Sep 7, 2020
Copy link
Member

@nbaksalyar nbaksalyar left a comment

Thank you, great work! 👍
I've got one question about a new dependency which we can consider moving behind a feature-flag.

Cargo.toml Outdated Show resolved Hide resolved
src/symbol.rs Outdated Show resolved Hide resolved
src/symbol/source.rs Outdated Show resolved Hide resolved
blitzerr added 7 commits Sep 9, 2020
change summary
--------------

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

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.
…nction during syntax highlighting of code snippet
@blitzerr
Copy link
Contributor Author

blitzerr commented Sep 9, 2020

@nbaksalyar r?

@bjorn3
Copy link
Contributor

bjorn3 commented Sep 9, 2020

Please run cargo fmt.

Cargo.toml Outdated Show resolved Hide resolved
@nbaksalyar
Copy link
Member

nbaksalyar commented Sep 10, 2020

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 bjorn3 merged commit 2e35c65 into headcrab-rs:master Sep 10, 2020
@bjorn3
Copy link
Contributor

bjorn3 commented Sep 10, 2020

Thanks @blitzerr!

@blitzerr blitzerr deleted the i-100upstream/master branch Sep 10, 2020
@blitzerr
Copy link
Contributor Author

blitzerr commented Sep 10, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement source mapping
3 participants