@@ -30,17 +30,37 @@ struct _is_valid_container<
3030 : std::is_same<typename T::value_type, size_t > {};
3131
3232
33+ /* *
34+ * @struct TraceInvokeContext
35+ * @brief Context structure for tracing JNI method invocations.
36+ *
37+ * This structure holds information relevant to a JNI method invocation trace,
38+ * including the JNI environment, caller address, result type, method ID (for
39+ * varargs), and a message buffer for logging or debugging purposes.
40+ *
41+ * Members:
42+ * - env: Pointer to the JNI environment associated with the current thread.
43+ * - caller: Address of the entity invoking the JNI method.
44+ * - result: The result type of the JNI call, if applicable.
45+ * - method: The JNI method ID, used when the method has variable arguments.
46+ * - message: Buffer to store trace or debug messages (up to 4096 characters).
47+ * - message_length: The current length of the message stored in the buffer.
48+ *
49+ * Constructor:
50+ * - Initializes the context with the given result type, JNI environment, and caller address.
51+ * The method ID is initialized to nullptr.
52+ */
3353template <typename jtype, bool AllowAccessArgs = true >
3454struct TraceInvokeContext {
35- // JNIEnv 对象
55+ // JNIEnv object pointer, note: it contains function pointers after our Hook
3656 JNIEnv *env;
37- // 调用者的地址
57+ // Caller's address
3858 void *caller;
39- // 如果有返回值,则是
59+ // JNI call return value
4060 jtype result;
41- // 当有可变参数 va_list 时则为具体的方法
61+ // Specific method when there are variable arguments va_list
4262 jmethodID method;
43- // 保存消息
63+ // Store messages
4464 char message[4096 ];
4565 uint32_t message_length = 0 ;
4666
@@ -50,9 +70,9 @@ struct TraceInvokeContext {
5070
5171template <>
5272struct TraceInvokeContext <void , true > {
53- // JNIEnv 对象
73+ // JNIEnv object pointer, note: it contains function pointers after our Hook
5474 JNIEnv *env;
55- // 调用者的地址
75+ // Caller's address
5676 void *caller;
5777 jmethodID method;
5878 char message[4096 ];
@@ -81,7 +101,7 @@ template <typename Derived>
81101class BaseTraceJNICallback ;
82102
83103namespace jni_trace {
84- // 直接申明全局变量避免增加寻址操作
104+ // Directly declare global variables to avoid adding addressing operations
85105extern JNINativeInterface backup_jni;
86106extern JNINativeInterface *org_jni;
87107extern JNINativeInterface hook_jni;
@@ -1160,7 +1180,63 @@ class JNIMonitor {
11601180 DISALLOW_COPY_AND_ASSIGN (JNIMonitor);
11611181};
11621182
1163-
1183+ /* *
1184+ * @class BaseTraceJNICallback
1185+ * @brief A base class for tracing and logging JNI calls in a custom JNIEnv wrapper.
1186+ *
1187+ * This template class provides a comprehensive framework for intercepting, tracing, and logging
1188+ * JNI function calls. It is designed to be inherited by user-defined callback classes, such as
1189+ * DefaultTraceJNICallback, to customize tracing behavior for JNI environments.
1190+ *
1191+ * @tparam Derived The derived callback class implementing custom trace logic.
1192+ *
1193+ * ## Features
1194+ * - Intercepts all major JNI function calls, including method/field access, object creation, array operations, etc.
1195+ * - Formats and logs method/field IDs, arguments, return values, and JNI object types.
1196+ * - Supports strict mode for enhanced safety and validation.
1197+ * - Maintains caches for method and field descriptions to improve performance.
1198+ * - Provides utilities for formatting JNI types, classes, strings, and objects.
1199+ * - Integrates with Android logging via __android_log_print.
1200+ * - Supports registration of trace hooks for a wide range of JNI functions.
1201+ * - Allows derived classes to override trace formatting and logging behavior.
1202+ *
1203+ * ## Usage
1204+ * 1. Inherit from BaseTraceJNICallback in your callback class:
1205+ * @code
1206+ * class DefaultTraceJNICallback : public BaseTraceJNICallback<DefaultTraceJNICallback> {
1207+ * public:
1208+ * explicit DefaultTraceJNICallback(bool strict) : BaseTraceJNICallback(strict) {}
1209+ * };
1210+ * @endcode
1211+ *
1212+ * 2. Instantiate your callback and initialize tracing:
1213+ * @code
1214+ * DefaultTraceJNICallback tracer(true);
1215+ * tracer.InitTrace(env); // env is a pointer to JNIEnv
1216+ * tracer->AddMonitorAddress(0x1000, 0x2000); // Add address to monitor
1217+ * tracer->AddLibraryMonitor("libexample.so", false); // Add library to monitor
1218+ * tracer.DefaultRegister(); // Registers default JNI functions and start tracing
1219+ * @endcode
1220+ *
1221+ * 3. Optionally, use SetStrictMode, SetOriginalEnv, or ClearCache as needed.
1222+ *
1223+ * ## Important Methods
1224+ * - InitTrace(JNIEnv* env): Initializes tracing and hooks JNI functions.
1225+ * - DefaultRegister(): Registers a default set of JNI functions and start tracing.
1226+ * - FormatMethodID/FormatFieldID: Returns human-readable descriptions for method/field IDs.
1227+ * - FormatJNIArguments/FormatReturnValue: Formats and logs JNI call arguments and return values.
1228+ * - TraceLog/TraceLine: Logging utilities for trace output.
1229+ *
1230+ * ## Thread Safety
1231+ * JNI callbacks use independent Context instances to achieve thread safety, but
1232+ * JNIMonitor element addition is not thread-safe. It is recommended to complete
1233+ * adding monitoring addresses before starting the trace.
1234+ *
1235+ * ## Example
1236+ * See DefaultTraceJNICallback in default_trace_jni.h for a minimal implementation.
1237+ *
1238+ * @see DefaultTraceJNICallback
1239+ */
11641240template <typename Derived>
11651241class BaseTraceJNICallback : public HookJNIEnv <Derived> {
11661242 friend class HookJNIEnv <Derived>;
0 commit comments