Skip to content

Commit fd11c68

Browse files
author
Jaroslav Tulach
committed
[GR-22130] Allow specification of rootNameFilter as regular expression.
PullRequest: graal/6046
2 parents 2ce4893 + cb82ab2 commit fd11c68

File tree

14 files changed

+80
-25
lines changed

14 files changed

+80
-25
lines changed

tools/docs/Insight-Embedding.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ For example following script is going to observe who calls `process.exit`:
9393
```bash
9494
$ curl --data \
9595
'insight.on("enter", (ctx, frame) => { console.log(new Error("call to exit").stack); }, \
96-
{ roots: true, rootNameFilter: n => n === "exit" });' \
96+
{ roots: true, rootNameFilter: "exit" });' \
9797
-X POST http://localhost:9999/
9898
```
9999

tools/docs/Insight-Manual.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ insight.on('enter', function(ctx, frame) {
333333
print('fib for ' + frame.n);
334334
}, {
335335
roots: true,
336-
rootNameFilter: (name) => 'fib' === name
336+
rootNameFilter: 'fib'
337337
});
338338
```
339339

@@ -458,7 +458,7 @@ insight.on('enter', (ev, frame) => {
458458
}
459459
}, {
460460
roots: true,
461-
rootNameFilter: (n) => n === 'log'
461+
rootNameFilter: 'log'
462462
});
463463
```
464464

@@ -521,7 +521,7 @@ insight.on('enter', function(ctx, frame) {
521521
}
522522
}, {
523523
roots: true,
524-
rootNameFilter: (n) => n === 'nextNatural'
524+
rootNameFilter: 'nextNatural'
525525
});
526526
```
527527

@@ -606,7 +606,7 @@ insight.on('enter', (ctx, frame) => {
606606
}
607607
}, {
608608
roots: true,
609-
rootNameFilter: (name) => name === 'Filter'
609+
rootNameFilter: 'Filter'
610610
});
611611

612612
insight.on('return', (ctx, frame) => {
@@ -615,7 +615,7 @@ insight.on('return', (ctx, frame) => {
615615
max = 0;
616616
}, {
617617
roots: true,
618-
rootNameFilter: (name) => name === 'measure'
618+
rootNameFilter: 'measure'
619619
});
620620
```
621621

@@ -643,7 +643,7 @@ insight.on('enter', (ctx, frame) => {
643643
}
644644
}, {
645645
roots: true,
646-
rootNameFilter: (name) => name === 'Filter'
646+
rootNameFilter: 'Filter'
647647
});
648648
```
649649

tools/docs/Insight-Tracing.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ let initialize = function(tracer) {
3636
console.log(`agent: handling #${res.id} request for ${req.url}`);
3737
}, {
3838
roots: true,
39-
rootNameFilter: name => name === 'emit',
39+
rootNameFilter: 'emit',
4040
sourceFilter: src => src.name === 'events.js'
4141
});
4242

@@ -50,7 +50,7 @@ let initialize = function(tracer) {
5050
}
5151
}, {
5252
roots: true,
53-
rootNameFilter: name => name === 'end',
53+
rootNameFilter: 'end',
5454
sourceFilter: src => src.name === '_http_outgoing.js'
5555
});
5656
console.log('agent: ready');
@@ -118,7 +118,7 @@ let initializeJaeger = function (ctx, frame) {
118118

119119
insight.on('return', initializeJaeger, {
120120
roots: true,
121-
rootNameFilter: name => name === 'jaegerAvailable'
121+
rootNameFilter: 'jaegerAvailable'
122122
});
123123
```
124124

tools/src/com.oracle.truffle.tools.agentscript.test/src/com/oracle/truffle/tools/agentscript/test/AgentObjectFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ final class AgentObjectFactory extends ProxyLanguage {
5353

5454
static InsightAPI.OnConfig createConfig(
5555
boolean expressions, boolean statements, boolean roots,
56-
Predicate<String> rootNameFilter, Predicate<SourceInfo> sourceFilter) {
56+
String rootNameFilter, Predicate<SourceInfo> sourceFilter) {
5757
InsightAPI.OnConfig config = new InsightAPI.OnConfig();
5858
config.expressions = expressions;
5959
config.statements = statements;

tools/src/com.oracle.truffle.tools.agentscript.test/src/com/oracle/truffle/tools/agentscript/test/AgentObjectTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ public void onEnterCallbackWithFilterOnRootName() throws Exception {
324324
agentAPI.on("enter", (ctx, frame) -> {
325325
assertNull("No function entered yet", functionName[0]);
326326
functionName[0] = ctx.name();
327-
}, AgentObjectFactory.createConfig(false, false, true, (name) -> "foo".equals(name), null));
327+
}, AgentObjectFactory.createConfig(false, false, true, "foo", null));
328328
agentAPI.on("close", () -> {
329329
finished[0] = true;
330330
});
@@ -587,7 +587,7 @@ public void accessFrameVariables() throws Exception {
587587
assertTrue(names.isEmpty());
588588
names.addAll(frame.keySet());
589589
};
590-
agentAPI.on("enter", captureNames, createConfig(true, false, false, (name) -> "mul".equals(name), null));
590+
agentAPI.on("enter", captureNames, createConfig(true, false, false, "mul.*", null));
591591
c.eval(sampleScript);
592592
agentAPI.off("enter", captureNames);
593593

@@ -597,7 +597,7 @@ public void accessFrameVariables() throws Exception {
597597
agentAPI.on("enter", (ctx, frame) -> {
598598
values[0] = frame.get("a");
599599
values[1] = frame.get("b");
600-
}, AgentObjectFactory.createConfig(true, false, false, (name) -> "mul".equals(name), null));
600+
}, AgentObjectFactory.createConfig(true, false, false, "mul", null));
601601

602602
Value mul = c.getBindings(InstrumentationTestLanguage.ID).getMember("mul");
603603
assertNotNull("mul function found", mul);

tools/src/com.oracle.truffle.tools.agentscript.test/src/com/oracle/truffle/tools/agentscript/test/InsightAPI.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,12 @@ class OnConfig {
170170
public boolean expressions;
171171
public boolean statements;
172172
public boolean roots;
173-
public Predicate<String> rootNameFilter;
173+
174+
/** String with a regular expression to match name of functions.
175+
* Prior to version 0.6 this had to be a
176+
* {@code Function<String,Boolean>}.
177+
*/
178+
public String rootNameFilter;
174179
/* @since 0.4 */
175180
public Predicate<SourceInfo> sourceFilter;
176181
}

tools/src/com.oracle.truffle.tools.agentscript/src/com/oracle/truffle/tools/agentscript/impl/AgentObject.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,14 @@ private static SourceSectionFilter createFilter(AgentObject obj, Object[] args)
218218
try {
219219
Object fn = iop.readMember(config, "rootNameFilter");
220220
if (fn != null && !iop.isNull(fn)) {
221-
if (!iop.isExecutable(fn)) {
222-
throw new IllegalArgumentException("rootNameFilter has to be a function!");
221+
if (iop.isString(fn)) {
222+
builder.rootNameIs(new RegexNameFilter(iop.asString(fn)));
223+
} else {
224+
if (!iop.isExecutable(fn)) {
225+
throw new IllegalArgumentException("rootNameFilter should be a string, a regular expression!");
226+
}
227+
builder.rootNameIs(new RootNameFilter(fn));
223228
}
224-
builder.rootNameIs(new RootNameFilter(fn));
225229
}
226230
} catch (UnknownIdentifierException ex) {
227231
// OK
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.truffle.tools.agentscript.impl;
26+
27+
import com.oracle.truffle.api.CompilerDirectives;
28+
import java.util.function.Predicate;
29+
import java.util.regex.Pattern;
30+
31+
final class RegexNameFilter implements Predicate<String> {
32+
private final Pattern regex;
33+
34+
RegexNameFilter(String fn) {
35+
this.regex = Pattern.compile(fn);
36+
}
37+
38+
@CompilerDirectives.TruffleBoundary
39+
@Override
40+
public boolean test(String rootName) {
41+
if (rootName == null) {
42+
return false;
43+
}
44+
return regex.matcher(rootName).matches();
45+
}
46+
}

tools/src/org.graalvm.tools.insight/src/org/graalvm/tools/insight/Insight.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ private Insight() {
6363
*
6464
* @since 20.1
6565
*/
66-
public static final String VERSION = "0.5";
66+
public static final String VERSION = "0.6";
6767

6868
}

vm/tests/all/agentscript/EmbeddingDoubled.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static void main(String... args) throws Exception {
4040
+ "}\n"
4141
+ "insight.on('return', fibreturn, {\n"
4242
+ " roots: true,\n"
43-
+ " rootNameFilter: (n) => n.indexOf('fib') >= 0\n"
43+
+ " rootNameFilter: '.*fib.*'\n"
4444
+ "});\n"
4545
+ "\n";
4646

0 commit comments

Comments
 (0)