Skip to content

Commit

Permalink
[GR-22025] Insight script in Python.
Browse files Browse the repository at this point in the history
PullRequest: graal/6514
  • Loading branch information
Jaroslav Tulach committed Jun 26, 2020
2 parents e0ba187 + 4706f04 commit ff83642
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions tools/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This changelog summarizes major changes between Truffle Tools versions.
## Version 20.2.0

* [GraalVM Insight](docs/Insight-Manual.md#modifying-local-variables) can modify values of local variables
* Write [GraalVM Insight](docs/Insight-Manual.md#python) scripts in Python

## Version 20.1.0

Expand Down
26 changes: 26 additions & 0 deletions tools/docs/Insight-Manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,32 @@ It is necessary to start GraalVM's Ruby launcher with `--polyglot` parameter
as the `source-tracing.js` script remains written in JavaScript. That's all
fine - mixing languages has never been a problem for GraalVM!

### Python

It is possible to write your GraalVM Insight scripts in Python. Such insights
can be applied to programs written in Python or any other language. Here is
an example of a script that prints out value of variable `n` when a function
`minusOne` in a `agent-fib.js` file is called:

```python
def onEnter(ctx, frame):
print(f"minusOne {frame.n}")

class Roots:
roots = True

def sourceFilter(self, src):
return src.name == "agent-fib.js"

def rootNameFilter(self, n):
return n == "minusOne"

insight.on("enter", onEnter, Roots())
```
Apply such script with `js --polyglot --insight=agent.py --experimental-options agent-fib.js`.
Of course, make sure Python is installed in your GraalVM via the `gu` tool.


### Minimal Overhead

With all the power the **Insight** framework brings, it is fair to ask what's
Expand Down
17 changes: 17 additions & 0 deletions vm/tests/all/agentscript/agent-python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
print(f"Python: Insight version {insight.version} is launching")
insight.on("source", lambda env : print(f"Python: observed loading of {env.name}"))
print("Python: Hooks are ready!")

def onEnter(ctx, frame):
print(f"minusOne {frame.n}")

class Roots:
roots = True

def sourceFilter(self, src):
return src.name == "agent-fib.js"

def rootNameFilter(self, n):
return n == "minusOne"

insight.on("enter", onEnter, Roots())
19 changes: 19 additions & 0 deletions vm/tests/all/agentscript/agent-python.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
> cat > ${TMP_DIR}/test.py
< print('Ahoj')
> graalpython --insight=agent-python.py --experimental-options ${TMP_DIR}/test.py
Python: Insight version 0.6 is launching
Python: Hooks are ready!
Python: observed loading of site.py
Python: observed loading of _sitebuiltins.py
Please note: This Python implementation is in the very early stages, and can run little more than basic benchmarks at this point.
Python: observed loading of test.py
Ahoj
> js --polyglot --insight=agent-python.py --experimental-options agent-fib.js
Python: Insight version 0.6 is launching
Python: Hooks are ready!
Python: observed loading of agent-fib.js
minusOne 4
minusOne 3
minusOne 2
minusOne 2
Three is the result 3

0 comments on commit ff83642

Please sign in to comment.