Skip to content

Implement source mapping#107

Merged
bjorn3 merged 15 commits intoheadcrab-rs:masterfrom
blitzerr:i-100upstream/master
Sep 10, 2020
Merged

Implement source mapping#107
bjorn3 merged 15 commits intoheadcrab-rs:masterfrom
blitzerr:i-100upstream/master

Conversation

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

blitzerr commented Sep 5, 2020

@bjorn3 @nbaksalyar r?

Copy link
Copy Markdown
Contributor

@bjorn3 bjorn3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Comment thread examples/repl.rs Outdated
Comment thread examples/repl.rs
Comment thread examples/repl.rs Outdated
Comment thread examples/repl.rs Outdated
Comment thread examples/repl.rs Outdated
Comment thread examples/repl.rs Outdated
Comment thread examples/repl.rs Outdated
Comment thread examples/repl.rs Outdated
Copy link
Copy Markdown
Member

@nbaksalyar nbaksalyar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread examples/repl.rs Outdated
Comment thread examples/repl.rs Outdated
Comment thread examples/repl.rs Outdated
Comment thread examples/repl.rs Outdated
Comment thread examples/repl.rs
Comment thread examples/repl.rs Outdated
@blitzerr
Copy link
Copy Markdown
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
Copy Markdown
Contributor

bjorn3 commented Sep 6, 2020

Strange.

@blitzerr
Copy link
Copy Markdown
Contributor Author

blitzerr commented Sep 6, 2020

It didn't fail earlier but after rebasing ..

@blitzerr
Copy link
Copy Markdown
Contributor Author

blitzerr commented Sep 6, 2020

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

@nbaksalyar
Copy link
Copy Markdown
Member

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.

Comment thread examples/repl.rs Outdated
Comment thread examples/repl.rs
Comment thread examples/repl.rs Outdated
Comment thread examples/repl.rs Outdated
Comment thread examples/repl.rs Outdated
Comment thread examples/repl.rs Outdated
Comment thread examples/repl.rs
Copy link
Copy Markdown
Member

@nbaksalyar nbaksalyar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread examples/repl.rs Outdated
Comment thread examples/repl.rs Outdated
Comment thread src/symbol/source.rs Outdated
Comment thread src/symbol/source.rs
Comment thread examples/repl.rs
Comment thread examples/repl.rs
@blitzerr blitzerr changed the title [WIP] Implement source mapping Implement source mapping Sep 7, 2020
@bjorn3 bjorn3 requested a review from nbaksalyar September 7, 2020 16:45
Copy link
Copy Markdown
Member

@nbaksalyar nbaksalyar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread Cargo.toml Outdated
Comment thread src/symbol.rs Outdated
Comment thread src/symbol/source.rs Outdated
blitzerr and others added 9 commits September 9, 2020 14:51
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
Co-authored-by: bjorn3 <bjorn3@users.noreply.github.com>
@blitzerr
Copy link
Copy Markdown
Contributor Author

blitzerr commented Sep 9, 2020

@nbaksalyar r?

@bjorn3
Copy link
Copy Markdown
Contributor

bjorn3 commented Sep 9, 2020

Please run cargo fmt.

Comment thread Cargo.toml Outdated
@nbaksalyar
Copy link
Copy Markdown
Member

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
Copy Markdown
Contributor

bjorn3 commented Sep 10, 2020

Thanks @blitzerr!

@blitzerr blitzerr deleted the i-100upstream/master branch September 10, 2020 14:13
@blitzerr
Copy link
Copy Markdown
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