Skip to content

Commit 96f9ef1

Browse files
authored
Merge pull request #2 from AleksRychkov/trace_msg_change
Trace msg change
2 parents 5610763 + f5fdf1f commit 96f9ef1

8 files changed

Lines changed: 33 additions & 10 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ frgament {
171171
* `exit`: Specifies a reference to a method to be injected at the end of instructed methods.
172172
* **Not required**
173173
* Variants:
174-
* __static method reference__, e.g. `org.example.MethodHook.exit`
174+
* __static method reference__, e.g. `org.example.MethodHook.exit`.
175+
* `msgPrefix`: Specifies prefix of trace message, `android.os.Trace.beginSection("${msgPrefix}clazzMethod.descriptor")`
176+
* **Not required**
177+
* Has effect only for `type: "trace"`
175178

176179
#### Plugin configuration
177180

examples/android-gradle-kts/methodhook/auto_trace.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ autoTrace {
55
interfaces = [ "io.github.aleksrychkov.example.AutoTrace" ]
66
class = "*"
77
methods = []
8+
msgPrefix = "->"
89
}

gradle/libs.versions.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ typesafe = "1.4.2"
88
junit = "5.7.1"
99
plugin-publish = "1.2.1"
1010
okhttp = "4.12.0"
11+
methodhook = "0.3"
1112

1213
[plugins]
1314
agp = { id = "com.android.application", version.ref = "agp" }
1415
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }
1516
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
1617
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
1718
plugin-publish = { id = "com.gradle.plugin-publish", version.ref = "plugin-publish" }
18-
methodhook = { id = "io.github.aleksrychkov.methodhook", version = "0.2" }
19+
methodhook = { id = "io.github.aleksrychkov.methodhook", version.ref = "methodhook" }
1920

2021

2122
[libraries]

methodhook/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ plugins {
1010
}
1111

1212
group = "io.github.aleksrychkov"
13-
version = "0.2"
13+
version = "0.3"
1414

1515
java {
1616
sourceCompatibility = JavaVersion.VERSION_17

methodhook/src/main/kotlin/io/github/aleksrychkov/methodhook/config/Config.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ internal data class TraceConfig(
2727
override val interfaces: ConfigValue<List<String>>,
2828
override val clazz: ConfigValue<String>,
2929
override val methods: ConfigValue<List<String>>,
30+
val traceMsgPrefix: String? = null
3031
) : Config
3132

3233
/**

methodhook/src/main/kotlin/io/github/aleksrychkov/methodhook/config/ConfigMapper.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ internal class ConfigMapper {
2121
const val PATH_ENTER = "enter"
2222
const val PATH_EXIT = "exit"
2323
const val PATH_DESCRIPTOR = "descriptor"
24+
const val PATH_TRACE_PREFIX = "msgPrefix"
2425

2526
// Supported configuration types
2627
const val TYPE_TRACE = "trace"
@@ -68,6 +69,7 @@ internal class ConfigMapper {
6869
interfaces = config.optionalListStringValue(PATH_INTERFACES),
6970
clazz = config.stringValue(PATH_CLASS),
7071
methods = config.listStringValue(PATH_METHODS),
72+
traceMsgPrefix = config.stringOrNull(PATH_TRACE_PREFIX),
7173
)
7274

7375
private fun mapDefault(config: TypeSafeConfig): Config =

methodhook/src/main/kotlin/io/github/aleksrychkov/methodhook/injects/InjectorFactory.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ internal object InjectorFactory {
3838
exitInjectMethod = config.exitInjectMethod,
3939
)
4040

41-
is TraceConfig -> TraceInjector
41+
is TraceConfig -> TraceInjector(
42+
msgPrefix = config.traceMsgPrefix,
43+
)
4244
}
4345
}
4446
.toSet()

methodhook/src/main/kotlin/io/github/aleksrychkov/methodhook/injects/injectors/TraceInjector.kt

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,24 @@ import org.objectweb.asm.commons.AdviceAdapter
1414
* allows tracking the execution of methods in an Android application. When a target
1515
* method is entered, it begins a new trace section, and when the method is exited,
1616
* it ends the trace section.
17+
*
18+
* @param msgPrefix - Prefix of trace's section name.
1719
*/
18-
internal object TraceInjector : Injector {
20+
internal class TraceInjector(
21+
private val msgPrefix: String? = null
22+
) : Injector {
1923

20-
private const val TRACE_MSG_LENGTH = 127
21-
private const val TRACE_MSG_PREFIX = "=>"
24+
private companion object {
25+
const val TRACE_MSG_LENGTH = 127
26+
const val TRACE_MSG_PREFIX = ""
27+
}
2228

2329
override fun onEnter(context: InjectorContext, mv: MethodVisitor) {
30+
val prefix = msgPrefix ?: TRACE_MSG_PREFIX
2431
val className = context.className.substringAfterLast("/")
25-
val msg = "$TRACE_MSG_PREFIX${className}.${context.methodName}${context.methodDescriptor}"
26-
.takeLast(TRACE_MSG_LENGTH)
32+
val method = context.methodName
33+
val descriptor = context.methodDescriptor.stripPackages()
34+
val msg = "${prefix}${className}.${method}${descriptor}".take(TRACE_MSG_LENGTH)
2735

2836
mv.visitLdcInsn(msg)
2937
mv.visitMethodInsn(
@@ -33,7 +41,6 @@ internal object TraceInjector : Injector {
3341
"(Ljava/lang/String;)V",
3442
false,
3543
)
36-
3744
}
3845

3946
override fun onExit(
@@ -50,4 +57,10 @@ internal object TraceInjector : Injector {
5057
false,
5158
)
5259
}
60+
61+
private fun String.stripPackages(): String {
62+
return this.replace(Regex("L[^;]+/"), "L")
63+
.replace("(Ljava/lang/Void;)", "()")
64+
.replace("()V", "()")
65+
}
5366
}

0 commit comments

Comments
 (0)