Search

CN-121979768-A - Cross-thread message tracking method for Android

CN121979768ACN 121979768 ACN121979768 ACN 121979768ACN-121979768-A

Abstract

The embodiment of the disclosure provides an Android cross-thread Message tracking method, which belongs to the technical field of data processing and specifically comprises the steps of intercepting a native method related to Message processing in an Android system, triggering a record flow by a enqueueMessage method of a Hook when a client thread delivers a Message object to a Message queue, acquiring and storing call stack information of the client thread and storing the call stack information in an associated mode, triggering an application flow by a DISPATCHMESSAGE method of the Hook when a server thread takes out and processes the Message object from the Message queue, associating a current server thread with the Message object, and judging whether the current thread is associated with the Message object being processed or not in response to a request for acquiring stack information. By the scheme, efficient, stable and low-intrusion message tracking is realized.

Inventors

  • LI TIANDONG
  • ZENG HUAN

Assignees

  • 湖南小算科技信息有限公司

Dates

Publication Date
20260505
Application Date
20260127

Claims (9)

  1. 1. The Android cross-thread message tracking method is characterized by comprising the following steps of: step 1, intercepting a native method related to message processing in an Android system through Hook operation when a process is initialized; Step 2, when the client thread delivers the Message object to the Message queue, acquiring and storing call stack information of the client thread by triggering a recording flow by a enqueueMessage method of Hook, and storing the call stack information in association with the Message object; Step 3, when the server thread takes out from the Message queue and processes the Message object, triggering an application flow by a DISPATCHMESSAGE method of the Hook, and associating the current server thread with the Message object; And 4, judging whether the current thread is associated with the Message object being processed or not in response to the request for acquiring the stack information, acquiring the call stack information of the client thread associated with the Message object and splicing the call stack information with the stack information of the current server thread and outputting the call stack information, and if not, directly outputting the stack information of the current thread.
  2. 2. The method according to claim 1, wherein the step 1 specifically comprises: step 1.1, dynamically linking system library files to obtain a symbol address of a native method to be checked; step 1.2, constructing a replacement function consistent with the original method signature, and calling a preset Java layer callback interface in the replacement function; And step 1.3, replacing the execution address of the original method with the address of the replacement function, and completing the Hook operation.
  3. 3. The method of claim 2, wherein the native methods to be Hook include enqueueMessage methods for queuing messages, DISPATCHMESSAGE methods for distributing processed messages, and loopOnce methods for identifying the end of a single message cycle.
  4. 4. The method of claim 1, wherein the step of obtaining and saving call stack information for the client thread comprises: Capturing stack tracking information of a current thread; filtering the stack tracking information, and removing frame method call records related to message tracking management; and saving the stack information after filtering in the form of character strings.
  5. 5. The method of claim 4, wherein the step of storing call stack information in association with a Message object comprises: and storing the association relation between the Message object and the client thread call stack information by adopting a weak reference mapping table, wherein the Message object is used as a key, and the corresponding call stack information character string is used as a value.
  6. 6. The method of claim 1, wherein the step of associating the current server thread with the Message object comprises: and associating the current server thread with the Message object through a thread local variable, wherein the thread local variable stores the weak reference of the Message object processed by the current thread.
  7. 7. The method of claim 6, wherein when the end of a single Message cycle is perceived by the loopOnce method of Hook, the Message object weak reference stored in the thread local variable corresponding to the current thread is set to be empty.
  8. 8. The method of claim 1, wherein the step of stitching it with stack information of a current server thread comprises: Acquiring complete stack tracking information of a current server thread; Positioning a call record of a message cyclic processing method in stack tracking information, and replacing the part before the call record of the method with call stack information of a client thread; and splicing the replaced client thread call stack information and the rest server thread stack information in sequence to form a complete cross-thread call chain.
  9. 9. The method of claim 1, further comprising the step of initializing: And calling an initialization function of the Native layer to execute Hook operation in an Application initialization stage of the Android Application and in the creation of a new process.

Description

Cross-thread message tracking method for Android Technical Field The embodiment of the disclosure relates to the technical field of data processing, in particular to an Android cross-thread message tracking method. Background Currently, in common Android application projects, an Android native cross-thread communication framework composed of Handler, lopper, message is often used for cross-thread communication. At this time, the two communicating threads are called a client thread and a server thread, and the client thread sends a Message to the server thread to enable the server thread to run a specific program. If unexpected behavior occurs in the server thread, debug, view call stack, etc. methods are needed for bug tracking. However, in this native cross-thread framework, if the server thread prints a stack, it can only print out the stack of the thread, and it cannot track the generation of Message and its previous call stack by the client thread. It can be seen that there is a need for an efficient, stable, low-intrusion, and Android, cross-thread message tracking method. Disclosure of Invention In view of the above, embodiments of the present disclosure provide an Android cross-thread message tracking method, which at least partially solves some of the problems existing in the prior art. The embodiment of the disclosure provides an Android cross-thread message tracking method, which comprises the following steps: step 1, intercepting a native method related to message processing in an Android system through Hook operation when a process is initialized; Step 2, when the client thread delivers the Message object to the Message queue, acquiring and storing call stack information of the client thread by triggering a recording flow by a enqueueMessage method of Hook, and storing the call stack information in association with the Message object; Step 3, when the server thread takes out from the Message queue and processes the Message object, triggering an application flow by a DISPATCHMESSAGE method of the Hook, and associating the current server thread with the Message object; And 4, judging whether the current thread is associated with the Message object being processed or not in response to the request for acquiring the stack information, acquiring the call stack information of the client thread associated with the Message object and splicing the call stack information with the stack information of the current server thread and outputting the call stack information, and if not, directly outputting the stack information of the current thread. According to a specific implementation manner of the embodiment of the present disclosure, the step 1 specifically includes: step 1.1, dynamically linking system library files to obtain a symbol address of a native method to be checked; step 1.2, constructing a replacement function consistent with the original method signature, and calling a preset Java layer callback interface in the replacement function; And step 1.3, replacing the execution address of the original method with the address of the replacement function, and completing the Hook operation. According to a specific implementation of an embodiment of the disclosure, the native methods to be Hook include enqueueMessage methods for queuing messages, DISPATCHMESSAGE methods for distributing processed messages, and loopOnce methods for identifying the end of a single message cycle. According to a specific implementation manner of the embodiment of the present disclosure, the step of obtaining and saving call stack information of the client thread includes: Capturing stack tracking information of a current thread; filtering the stack tracking information, and removing frame method call records related to message tracking management; and saving the stack information after filtering in the form of character strings. According to a specific implementation manner of an embodiment of the present disclosure, the step of storing the call stack information in association with the Message object includes: and storing the association relation between the Message object and the client thread call stack information by adopting a weak reference mapping table, wherein the Message object is used as a key, and the corresponding call stack information character string is used as a value. According to a specific implementation manner of an embodiment of the present disclosure, the step of associating the current server thread with the Message object includes: and associating the current server thread with the Message object through a thread local variable, wherein the thread local variable stores the weak reference of the Message object processed by the current thread. According to a specific implementation manner of the embodiment of the present disclosure, when the end of a single Message cycle is perceived by the loopOnce method of Hook, the Message object weak reference stored in the thread local variable corresponding to t