boa icon indicating copy to clipboard operation
boa copied to clipboard

Add `Vm` trace functionality and add trace to `boa_wasm`

Open nekevss opened this issue 2 years ago • 8 comments

This is the branch that was used for the web-debugger concept.

This branch is technically ready to be merged, but thought I'd post it as a draft for now to get feedback as it has some API changes along with quite a few changes to the Vm trace overall.

It changes the following:

  • Search and handle the compiled bytecode trace prior to instruction execution and trace.
  • Creates a BoaJs object and an evaluate_with_debug_hooks function in boa_wasm
  • Creates a VmTrace (not the biggest fan of the name here if anyone has other ideas) for handling tracing and allowing users to define handle trace themselves via "registering" a function (defaults to println).

For reference, the standard trace output would now print like the below.

----------------------Compiled Output: '<main>'-----------------------
Location  Count    Handler    Opcode                     Operands

000000    0000      none      GetName                    0000: 'greet'
000005    0001      none      PushUndefined              
000006    0002      none      Swap                       
000007    0003      none      PushLiteral                0
000012    0004      none      Call                       1
000017    0005      none      SetReturnValue             
000018    0006      none      Return                     

Literals:
    0000: <string> "World"

Bindings:
    0000: greet

Functions:
    0000: name: 'greet' (length: 1)

Handlers:
    <empty>


-----------------------Compiled Output: 'greet'-----------------------
Location  Count    Handler    Opcode                     Operands

000000    0000      none      PutLexicalValue            0000: 'targetName'
000005    0001      none      RestParameterPop           
000006    0002      none      PushLiteral                0
000011    0003      none      GetName                    0000: 'targetName'
000016    0004      none      Add                        
000017    0005      none      PushLiteral                1
000022    0006      none      Add                        
000023    0007      none      SetReturnValue             
000024    0008      none      Return                     
000025    0009      none      Return                     

Literals:
    0000: <string> "Hello, "
    0001: <string> "!"

Bindings:
    0000: targetName

Functions:
    <empty>

Handlers:
    <empty>

------------------------------------ Call Frame -- <main> ------------------------------------
Time          Opcode                     Operands                   Stack

14╬╝s          GetName                    0000: 'greet'              [ [function] ]
0╬╝s           PushUndefined                                         [ undefined, [function] ]
0╬╝s           Swap                                                  [ [function], undefined ]
1╬╝s           PushLiteral                0                          [ "World", [function], undefined ]
------------------------------------ Call Frame -- greet -------------------------------------
0╬╝s           PutLexicalValue            0000: 'targetName'         [ ]
1╬╝s           RestParameterPop                                      [ ]
0╬╝s           PushLiteral                0                          [ "Hello, " ]
1╬╝s           GetName                    0000: 'targetName'         [ "World", "Hello, " ]
5╬╝s           Add                                                   [ "Hello, World" ]
0╬╝s           PushLiteral                1                          [ "!", "Hello, World" ]
1╬╝s           Add                                                   [ "Hello, World!" ]
0╬╝s           SetReturnValue                                        [ ]
0╬╝s           Return                                                [ ]
-------------------------- Call Frame -- <Exiting greet via Return> --------------------------
140╬╝s         Call                       1                          [ "Hello, World!" ]
0╬╝s           SetReturnValue                                        [ ]
0╬╝s           Return                                                [ ]
------------------------- Call Frame -- <Exiting <main> via Return> --------------------------
"Hello, World!"

Let me know what you think 😄

nekevss avatar Aug 19 '23 16:08 nekevss

Test262 conformance changes

Test result main count PR count difference
Total 50,731 50,731 0
Passed 42,973 42,973 0
Ignored 1,395 1,395 0
Failed 6,363 6,363 0
Panics 18 18 0
Conformance 84.71% 84.71% 0.00%

github-actions[bot] avatar Aug 19 '23 16:08 github-actions[bot]

Seeing this, I think it would be worth to create a Tracer trait instead of passing closures to handle tracing. Would also make it easier to send the output to files, loggers or other interesting usages.

jedel1043 avatar Aug 19 '23 20:08 jedel1043

Would also make it easier to send the output to files, loggers or other interesting usages.

Kind of what I was going for.

I was initially thinking of how to plug the trace output into a tui out of curiosity which led me to the wasm. I don't think it should be too crazy to switch it over to being trait based. 😄

nekevss avatar Aug 19 '23 20:08 nekevss

Codecov Report

Attention: Patch coverage is 10.10638% with 169 lines in your changes are missing coverage. Please review.

Project coverage is 47.21%. Comparing base (6ddc2b4) to head (606f48e). Report is 141 commits behind head on main.

:exclamation: Current head 606f48e differs from pull request most recent head 94045e1. Consider uploading reports for the commit 94045e1 to get more accurate results

Files Patch % Lines
core/engine/src/vm/trace.rs 13.92% 68 Missing :warning:
ffi/wasm/src/lib.rs 0.00% 64 Missing :warning:
cli/src/debug/function.rs 0.00% 15 Missing :warning:
core/engine/src/vm/code_block.rs 0.00% 10 Missing :warning:
core/engine/src/vm/mod.rs 57.14% 6 Missing :warning:
core/engine/src/context/mod.rs 0.00% 4 Missing :warning:
cli/src/main.rs 0.00% 2 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3227      +/-   ##
==========================================
- Coverage   47.24%   47.21%   -0.03%     
==========================================
  Files         476      476              
  Lines       46892    46084     -808     
==========================================
- Hits        22154    21760     -394     
+ Misses      24738    24324     -414     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Aug 20 '23 01:08 codecov[bot]

Switched the custom closures bits over to a trait.

It may be possible to go even further with the trait than currently implemented and selectively emit Vm state rather than pre-formatted messages. But I'm not exactly sure how much access we should provided, so I kept the current output as is for now.

nekevss avatar Aug 20 '23 01:08 nekevss

@HalidOdat and @jedel1043 will review this to see if we want to expose the instructions or if this is good enough.

jedel1043 avatar Nov 29 '23 18:11 jedel1043

Made some changes to have VmTrace have a Vec<Box<dyn Tracer>> and moved should_trace to Tracer. Let me know what you think!

nekevss avatar Jan 20 '24 01:01 nekevss

Looks good to me! Just needs a rebase :)

HalidOdat avatar Feb 04 '24 06:02 HalidOdat