Skip to content

Commit 2ee9604

Browse files
committed
Made MethodLogger use extended format strings as well
1 parent cc30bd3 commit 2ee9604

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Some components will allow specifying extended format strings for data to be log
4040
* `result`: Call result if available
4141
* `this`: Instance reference
4242
* `argNN`: Argument value where NN is the argument’s zero-based position
43+
* `args`: Comma-separated list of all arguments (stringified)
4344

4445
In addition, the format specifier `%x` is treated specially: `System.identityHashCode()` will be called on the corresponding input and the result hex-formatted.
4546

@@ -74,6 +75,7 @@ Configuration options:
7475
* `MethodLogger.enabled`: add to enable this component
7576
* `MethodLogger.filter`: (optional) restricts functionality to a set of methods, for value format see Method filters section above
7677
* `MethodLogger.tag`: (optional) log tag to be used (default is `MethodLogger`)
78+
* `MethodLogger.format`: (optional) extended format string to be used, see Extended format strings section above (default is `Entered method {method:%s} ({args:%s})`)
7779

7880
## AssignmentRemover component
7981

src/info/palant/apkInstrumentation/MethodLogger.java

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66

77
package info.palant.apkInstrumentation;
88

9-
import java.util.List;
109
import java.util.Map;
1110
import java.util.Properties;
11+
import java.util.stream.Collectors;
1212

1313
import soot.Body;
1414
import soot.BodyTransformer;
15-
import soot.Local;
16-
import soot.jimple.StringConstant;
15+
import soot.Value;
1716

1817
public class MethodLogger extends BodyTransformer
1918
{
2019
private final MethodConfig filter;
2120
private String tag;
21+
private String format;
2222

2323
public MethodLogger(Properties config)
2424
{
@@ -31,6 +31,10 @@ public MethodLogger(Properties config)
3131
this.tag = config.getProperty("MethodLogger.tag");
3232
if (tag == null)
3333
this.tag = "MethodLogger";
34+
35+
this.format = config.getProperty("MethodLogger.format");
36+
if (this.format == null)
37+
this.format = "Entered method {method:%s} ({args:%s})";
3438
}
3539

3640
@Override
@@ -40,31 +44,12 @@ protected void internalTransform(Body body, String phaseName, Map<String, String
4044
return;
4145

4246
UnitSequence units = new UnitSequence(body);
43-
44-
List<Local> parameters = body.getParameterLocals();
45-
if (parameters.size() > 0)
46-
{
47-
Local message = units.newObject(
48-
"java.lang.StringBuilder",
49-
StringConstant.v("Entered method " + body.getMethod().getSignature() + " with parameters: ")
50-
);
51-
52-
boolean first = true;
53-
for (Local parameter: parameters)
54-
{
55-
if (first)
56-
first = false;
57-
else
58-
units.call(message, "append", StringConstant.v(", "));
59-
60-
units.call(message, "append", units.stringify(parameter));
61-
}
62-
63-
units.log(this.tag, units.stringify(message));
64-
}
65-
else
66-
units.log(this.tag, StringConstant.v("Entered method " + body.getMethod().getSignature()));
67-
47+
units.log(this.tag, units.extendedFormat(
48+
this.format,
49+
null,
50+
body.getThisLocal(),
51+
body.getParameterLocals().stream().map(local -> (Value)local).collect(Collectors.toList())
52+
));
6853
units.insertBefore();
6954
}
7055
}

src/info/palant/apkInstrumentation/UnitSequence.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,30 @@ else if (matcher.group(1).equals("result"))
245245
arg = result;
246246
else if (matcher.group(1).equals("this"))
247247
arg = thisRef;
248+
else if (matcher.group(1).equals("args"))
249+
{
250+
if (argValues.size() == 0)
251+
arg = StringConstant.v("");
252+
else if (argValues.size() == 1)
253+
arg = this.stringify(argValues.get(0));
254+
else
255+
{
256+
Local builder = this.newObject("java.lang.StringBuilder");
257+
258+
boolean first = true;
259+
for (Value argValue: argValues)
260+
{
261+
if (first)
262+
first = false;
263+
else
264+
this.call(builder, "append", StringConstant.v(", "));
265+
266+
this.call(builder, "append", this.stringify(argValue));
267+
}
268+
269+
arg = this.stringify(builder);
270+
}
271+
}
248272
else if (matcher.group(1).startsWith("arg"))
249273
arg = argValues.get(Integer.parseInt(matcher.group(1).substring(3)));
250274
else

0 commit comments

Comments
 (0)