Thread blocking detection method and device, electronic equipment and storage medium
By obtaining the address offset of the entry function and performing hook operations, the waiting and wake-up methods of threads are detected, the problem of incomplete thread blocking analysis in Android system is solved, and comprehensive detection of thread blocking is achieved.
Patent Information
- Application Number
- CN202310761744.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-26
- Publication Date
- 2024-12-27
AI Technical Summary
In Android system, the blockage caused by the current thread waiting for other threads to complete tasks cannot be detected, resulting in incomplete thread blocking analysis.
By obtaining the address offset of the entry function and performing hook operations based on the address, the waiting method and wake-up method of the thread to be detected are detected to determine the blocking information of the thread.
It realizes detection of blockage caused by threads due to waiting for other threads to execute tasks, improving the comprehensiveness of thread blocking analysis.
Smart Images

Figure CN119201509A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present disclosure relate to the field of computer technology, and in particular to a method, device, electronic device and storage medium for detecting thread blocking. Background Art
[0002] When optimizing application performance on an Android system, there may be a need to analyze the time and / or causes of thread blocking. For example, the Android system's performance analysis tool Atrace can be used to detect the blocking time caused by the current thread competing with other threads for the synchronized lock Synchronized.
[0003] However, in the Android system, in addition to the synchronized lock contention, the situation where the current thread waits for other threads to complete tasks will also cause the current thread to be blocked. There is currently no way to detect this situation, resulting in the inability to conduct a comprehensive analysis of thread blocking. Summary of the invention
[0004] Embodiments of the present disclosure provide a method, device, electronic device, and storage medium for detecting thread blocking, so as to detect blocking caused by a current thread waiting for other threads to complete tasks.
[0005] In a first aspect, an embodiment of the present disclosure provides a method for detecting thread blocking, including:
[0006] Obtaining the address offset of the entry function in the current system, where the address offset is the offset of the address of the entry function in the corresponding target pointer;
[0007] Performing a hook operation on a method to be detected based on the address offset, wherein the method to be detected includes a waiting method called by a thread to be detected and a wake-up method corresponding to the waiting method;
[0008] The blocking information of the thread to be detected is determined according to the method to be detected after the hook operation is executed.
[0009] In a second aspect, the embodiment of the present disclosure further provides a thread blocking detection device, including:
[0010] An offset acquisition module is used to acquire the address offset of the entry function in the current system, where the address offset is the offset of the address of the entry function in the corresponding target pointer;
[0011] An operation execution module, configured to execute a hook operation on a method to be detected based on the address offset, wherein the method to be detected includes a waiting method called by a thread to be detected and a wake-up method corresponding to the waiting method;
[0012] The information determination module is used to determine the blocking information of the thread to be detected according to the method to be detected after the hook operation is executed.
[0013] In a third aspect, an embodiment of the present disclosure further provides an electronic device, including:
[0014] one or more processors;
[0015] a memory for storing one or more programs,
[0016] When the one or more programs are executed by the one or more processors, the one or more processors implement the thread blocking detection method as described in the embodiment of the present disclosure.
[0017] In a fourth aspect, an embodiment of the present disclosure further provides a computer-readable storage medium on which a computer program is stored, and when the program is executed by a processor, the method for detecting thread blocking as described in the embodiment of the present disclosure is implemented.
[0018] The thread blocking detection method, device, electronic device and storage medium provided by the embodiments of the present disclosure obtain the address offset of the entry function in the current thread, which address offset is the offset of the address of the entry function in the corresponding target pointer; based on the address offset, a hook operation is performed on the method to be detected, and the detection method includes a waiting method called by the thread to be detected and a wake-up method corresponding to the waiting method; the blocking information of the thread to be detected is determined according to the method to be detected after the hook operation is performed. The embodiments of the present disclosure utilize the above technical solution, by determining the address offset of the entry function and performing a hook operation on the method to be detected based on the address offset, it is possible to detect the blocking of the thread to be detected caused by waiting for other threads to execute tasks, determine the blocking information of the thread to be detected, and improve the comprehensiveness of thread blocking analysis. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] The above and other features, advantages and aspects of the embodiments of the present disclosure will become more apparent with reference to the following detailed description in conjunction with the accompanying drawings. Throughout the accompanying drawings, the same or similar reference numerals represent the same or similar elements. It should be understood that the drawings are schematic and the originals and elements are not necessarily drawn to scale.
[0020] Figure 1 A flowchart of a thread blocking detection method provided by an embodiment of the present disclosure;
[0021] Figure 2 A flowchart of another thread blocking detection method provided by an embodiment of the present disclosure;
[0022] Figure 3 A flowchart of an exemplary thread blocking detection method provided in an embodiment of the present disclosure;
[0023] Figure 4 A structural block diagram of a thread blocking detection device provided by an embodiment of the present disclosure;
[0024] Figure 5 A schematic diagram of the structure of an electronic device provided in an embodiment of the present disclosure. DETAILED DESCRIPTION
[0025] Embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although certain embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be construed as being limited to the embodiments described herein, which are instead provided for a more thorough and complete understanding of the present disclosure. It should be understood that the drawings and embodiments of the present disclosure are only for exemplary purposes and are not intended to limit the scope of protection of the present disclosure.
[0026] It should be understood that the various steps described in the method embodiments of the present disclosure may be performed in different orders and / or in parallel. In addition, the method embodiments may include additional steps and / or omit the steps shown. The scope of the present disclosure is not limited in this respect.
[0027] The term "including" and its variations used herein are open inclusions, i.e., "including but not limited to". The term "based on" means "based at least in part on". The term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one additional embodiment"; the term "some embodiments" means "at least some embodiments". The relevant definitions of other terms will be given in the following description.
[0028] It should be noted that the concepts such as "first" and "second" mentioned in the present disclosure are only used to distinguish different devices, modules or units, and are not used to limit the order or interdependence of the functions performed by these devices, modules or units.
[0029] It should be noted that the modifications of "one" and "plurality" mentioned in the present disclosure are illustrative rather than restrictive, and those skilled in the art should understand that unless otherwise clearly indicated in the context, it should be understood as "one or more".
[0030] The names of the messages or information exchanged between multiple devices in the embodiments of the present disclosure are only used for illustrative purposes and are not used to limit the scope of these messages or information.
[0031] It is understandable that before using the technical solutions disclosed in the embodiments of the present disclosure, the types, scope of use, usage scenarios, etc. of the personal information involved in the present disclosure should be informed to the user and the user's authorization should be obtained in an appropriate manner in accordance with relevant laws and regulations.
[0032] For example, in response to receiving an active request from a user, a prompt message is sent to the user to clearly prompt the user that the operation requested to be performed will require obtaining and using the user's personal information. Thus, the user can autonomously choose whether to provide personal information to software or hardware such as an electronic device, application, server, or storage medium that performs the operation of the technical solution of the present disclosure according to the prompt message.
[0033] As an optional but non-limiting implementation, in response to receiving an active request from the user, the prompt information may be sent to the user in the form of a pop-up window, in which the prompt information may be presented in text form. In addition, the pop-up window may also carry a selection control for the user to choose "agree" or "disagree" to provide personal information to the electronic device.
[0034] It is understandable that the above notification and the process of obtaining user authorization are merely illustrative and do not constitute a limitation on the implementation of the present disclosure. Other methods that meet the relevant laws and regulations may also be applied to the implementation of the present disclosure.
[0035] Figure 1 A flowchart of a method for detecting thread blocking provided in an embodiment of the present disclosure. The method can be executed by a device for detecting thread blocking, wherein the device can be implemented by software and / or hardware and can be configured in an electronic device, typically a mobile phone or a tablet computer. The method for detecting thread blocking provided in an embodiment of the present disclosure is suitable for scenarios where thread blocking is detected, and is particularly suitable for scenarios where blocking caused by a thread to be detected waiting for other threads to complete tasks. Figure 1 As shown, the thread blocking detection method provided in this embodiment may include:
[0036] S101. Obtain an address offset of an entry function in the current system, where the address offset is an offset of the address of the entry function in a corresponding target pointer.
[0037] The current system can be understood as the system where the thread to be detected is located, such as the operating system installed in the current terminal. Exemplarily, the current system can be an Android system. The entry function can be the entry function corresponding to the method associated with the Java Native Interface (JNI) in the Java layer of the current system in the C language layer. Among them, the method associated with JNI can be considered as a method based on JNI implementation, which may include JNI methods and / or Java methods implemented by JNI methods at the bottom layer. The C language layer can be, for example, a C++ layer. The target pointer can be considered as a pointer that stores the address of the entry function corresponding to the corresponding method. Exemplarily, the target pointer can be an ArtMethod pointer.
[0038] The address offset can be the offset of the address of the entry function corresponding to the method associated with JNI in the target pointer corresponding to the method associated with JNI. When the offset is different, the target pointer can point to different contents, so the address offset can be considered as the offset of the target pointer corresponding to the method associated with JNI when the target pointer corresponding to the method associated with JNI points to the address of the entry function corresponding to the method associated with JNI. When the versions of the operating system are different, or when the operating system is installed in different terminal devices, the address offset of the entry function may be different, depending on the actual situation of the operating system and / or the terminal device.
[0039] In this embodiment, when it is necessary to detect the blocking information of one or more threads in the current system, the address offset of the entry function in the current system can be obtained, so as to perform a hook operation on the detection method according to the address offset. For example, in the case where the address offset of the entry function in the current system is not stored, the address offset of the entry function in the current system is calculated, and the calculated address offset can be further stored; in the case where the address offset of the entry function in the current system is stored, the stored address offset can be obtained.
[0040] S102: Perform a hook operation on the method to be detected based on the address offset, where the method to be detected includes a waiting method called by the thread to be detected and a wake-up method corresponding to the waiting method.
[0041] The thread to be detected may be considered as a thread in the current system whose blocking information needs to be detected. This embodiment does not limit the thread to be detected, and it may be flexibly set according to the detection requirements.
[0042] The method to be detected can be understood as a method that needs to be detected when detecting the blocking information of the thread to be detected. It can be a method associated with the blocking situation of the thread to be detected. For example, the method to be detected can include a waiting method called by the thread to be detected and a wake-up method corresponding to the waiting method. The waiting method can be a method that causes the thread that calls the method to enter a waiting state when called. In other words, the thread to be detected will enter a waiting state after calling the waiting method. The wake-up method can be a method that wakes up the thread corresponding to it when called. For example, when a thread calls the wake-up method corresponding to the waiting method called by the thread to be detected for the thread to be detected, the thread to be detected can end the waiting and enter the wake-up state. Assuming that the thread to be detected is thread A and the calling thread is thread B, when thread A calls the waiting method, thread A enters the waiting state; when thread B calls the wake-up method corresponding to the waiting method for thread A, thread A ends the waiting and enters the wake-up state.
[0043] Exemplarily, the waiting method may be the Object.wait method of the java.lang.Object class, and the wake-up method may be the Object.notify method or the Object.notifyAll method of the java.lang.Object class; and / or, the waiting method may be the Unsafe.park method of the sun.misc.Unsafe class, and the wake-up method may be the Unsafe.unpark method of the sun.misc.Unsafe class.
[0044] Specifically, after obtaining the address offset of the entry function in the current system, a hook operation can be performed on the method to be detected based on the address offset, such as performing a hook operation on the entry function corresponding to each method to be detected in the C language layer based on the address offset, thereby realizing the hook operation on the method to be detected.
[0045] S103: Determine the blocking information of the thread to be detected according to the method to be detected after the hook operation is executed.
[0046] The blocking information may be information used to describe the blocking situation of the thread to be detected, and the blocking information may include the blocking time and / or blocking reason of the thread to be detected.
[0047] In this embodiment, after the hook operation is performed on the method to be detected, when the method to be detected is called, it can be detected that the method to be detected is called. Therefore, the time when the method to be detected is called and the calling thread of the method to be detected can be determined, and then the blocking information of the thread to be detected can be obtained, such as the blocking time and / or blocking reason of the detection thread.
[0048] In some embodiments, the blocking information includes blocking time, and determining the blocking information of the thread to be detected based on the method to be detected after executing the hook operation includes: when a call request for the method to be detected is received, recording the call time of the method to be detected; and determining the blocking time of the thread to be detected based on the call time.
[0049] In this embodiment, after the hook operation is performed on the method to be detected, the blocking time consumption of the thread to be detected can be determined according to the method to be detected after the hook operation is performed.
[0050] The calling request for a method to be detected can be understood as a request for calling the method to be detected. The blocking time can be understood as the duration of the blocking of the thread to be detected.
[0051] In this embodiment, since a hook operation is performed on the method to be detected, when the thread to be detected needs to call a waiting method, its call request for the waiting method will first be sent to the proxy function corresponding to the waiting method, and then forwarded to the waiting method by the proxy function to call the waiting method. Therefore, the call request of the thread to be detected for the waiting method can be received through the proxy function, thereby determining that the thread to be detected has switched into the waiting state. Thus, the calling time of the waiting method can be recorded as the time when the thread to be detected starts to enter the waiting state, that is, as the time when the thread to be detected starts to block this time.
[0052] Based on the same principle, the proxy function corresponding to the wake-up method can receive a call request for the wake-up method, and can determine that the thread to be detected has switched into the wake-up state when the wake-up method is used to wake up the thread to be detected, and record the calling time of the wake-up method as the time when the thread to be detected starts to enter the wake-up state, that is, the time when the thread to be detected ends the blocking this time.
[0053] Therefore, for each or a certain blocking of the thread to be detected, after obtaining the start time and end time of the blocking of the thread to be detected, the difference between the end time and the start time of the blocking is calculated to obtain the blocking time of the thread to be detected.
[0054] In some embodiments, the blocking information includes a blocking reason, and the blocking information of the thread to be detected is determined according to the method to be detected after executing the hook operation, including: when a first call request for a waiting method from the thread to be detected is received, obtaining a waiting identifier corresponding to the waiting method; determining a second call request for the wake-up method based on the waiting identifier; determining a calling thread of the wake-up method based on the second call request, and obtaining the blocking reason of the thread to be detected.
[0055] In this embodiment, after the hook operation is performed on the method to be detected, the blocking reason of the thread to be detected can be determined according to the method to be detected after the hook operation is performed. The blocking reason may include, for example, which thread and / or which task caused the thread to be detected to be blocked, in other words, the thread to be detected is blocked due to waiting for the execution of which thread and / or which task.
[0056] Among them, the waiting identifier can be considered as an identifier for identifying the blocking (i.e., waiting) that occurs to be detected. The waiting identifier can be set as needed, and optionally, the waiting identifier includes at least one of a lock object identifier and a thread object identifier. For example, the identifier of the lock object that the thread to be detected waits for other threads (such as calling threads) to release and / or the thread object identifier of the thread to be detected can be used as the waiting identifier corresponding to the waiting method called by the thread to be detected this time, so as to distinguish the blocking caused by other lock objects from the blocking caused by other threads through the waiting identifier. The lock object identifier can be the identifier of the lock object that the thread to be detected calls the waiting method this time, and the lock object and the lock object of the synchronized lock can be the same or different lock objects. Optionally, the lock object and the lock object of the synchronized lock can be different lock objects. The thread object identifier can be the identifier of the thread object of the thread to be detected. Exemplarily, when the waiting method is the Object.wait method, the corresponding waiting identifier can include the lock object identifier; when the waiting method is the Unsafe.park method, the corresponding waiting identifier can include the thread object identifier.
[0057] The first call request may be a request for the thread to be detected to call a waiting method. The second call request may be a request for the calling thread to call a wake-up method corresponding to the waiting method called by the thread to be detected for the thread to be detected, and the wake-up method may be used to wake up the thread to be detected. The calling thread may be the thread that calls the wake-up method, that is, the thread that sends the second call request, that is, the thread that the thread to be detected waits for its task to be completed.
[0058] Exemplarily, the process of determining the blocking cause of the thread to be detected is described as follows:
[0059] When the first call request for the waiting method from the thread to be detected is received through the proxy function corresponding to the waiting method, the waiting identifier corresponding to the waiting method can be obtained, such as using the lock object identifier that the thread to be detected is waiting to be released or the thread object identifier of the thread to be detected as the waiting identifier corresponding to the waiting method, and recording the waiting identifier.
[0060] When a call request for a wake-up method is received through the proxy function corresponding to the wake-up method, it can be determined whether the waiting identifier carried in the call request is the same as the waiting identifier corresponding to the waiting method called by the thread to be detected. If so, the call request is determined to be a second call request for the wake-up method corresponding to the waiting method called by the thread to be detected, that is, it is determined that the wake-up method requested to be called by the call request corresponds to the waiting method called by the thread to be detected; if not, continue to receive call requests for the wake-up method through the proxy function corresponding to the wake-up method until the second call request is determined.
[0061] After determining the second call request, the calling thread that sent the second call request can be determined according to the second call request, that is, the thread that calls the wake-up method corresponding to the waiting method called by the thread to be detected can be determined, thereby determining that the calling thread caused the thread to be detected to be blocked. In addition, the task executed by the calling thread before calling the wake-up method can be further determined, based on which it is determined that the task executed by the calling thread caused the thread to be detected to be blocked. Thus, the blocking cause of the thread to be detected is obtained.
[0062] The thread blocking detection method provided in this embodiment obtains the address offset of the entry function in the current thread, and the address offset is the offset of the address of the entry function in the corresponding target pointer; based on the address offset, a hook operation is performed on the method to be detected, and the detection method includes a waiting method called by the thread to be detected and a wake-up method corresponding to the waiting method; and the blocking information of the thread to be detected is determined according to the method to be detected after the hook operation is performed. This embodiment utilizes the above technical solution, by determining the address offset of the entry function and performing a hook operation on the method to be detected based on the address offset, it is possible to detect the blocking of the thread to be detected caused by waiting for other threads to execute tasks, determine the blocking information of the thread to be detected, and improve the comprehensiveness of thread blocking analysis.
[0063] Figure 2 A flow chart of another thread blocking detection method provided for an embodiment of the present disclosure. The scheme in this embodiment can be combined with one or more optional schemes in the above-mentioned embodiments. Optionally, the method of obtaining the address offset of the entry function in the current system includes: obtaining the address of a preset method and a first entry function declared in the current system, the first entry function being the entry function corresponding to the preset method; generating a first target pointer corresponding to the preset method; and determining, based on the address of the first entry function and the first target pointer, the offset of the address of the first entry function in the first target pointer as the address offset of the entry function in the current system.
[0064] Optionally, performing a hook operation on the method to be detected based on the address offset includes: for each method to be detected, obtaining the address of a second entry function according to the address offset, the second entry function being the entry function corresponding to the method to be detected; and performing a hook operation on the second entry function based on the address of the second entry function.
[0065] Correspondingly, such as Figure 2 As shown, the thread blocking detection method provided in this embodiment may include:
[0066] S201. Obtain a preset method declared in the current system and an address of a first entry function, where the first entry function is an entry function corresponding to the preset method.
[0067] The preset method may be a pre-declared method, and the address of the entry function corresponding to the preset method is known. The first entry function may be considered as the entry function corresponding to the preset method, such as the entry function corresponding to the preset method at the C language layer.
[0068] In the Android system, the offset of the address of the JNI entry method in the target pointer of the method object in the Java layer is fixed. In other words, when the terminal device and the Android system have the same version, the offset of the address of the JNI entry method corresponding to the method object in the Java layer in the C language layer in the target pointer of the method object is unchanged. This embodiment can determine the address offset of the entry function corresponding to the method object in the Java layer in the current system based on this principle.
[0069] Exemplarily, a Java JNI method can be declared in advance in the service code of the current system as a preset method, a JNI C++ method corresponding to the Java JNI method can be declared as the first entry function corresponding to the preset method, and the address of the first entry function can be recorded. Thus, when determining the address offset of the entry function in the current system, the preset method declared in the current system and the address of the first entry function corresponding to the preset method can be obtained.
[0070] S202: Generate a first target pointer corresponding to the preset method.
[0071] Among them, the first target pointer can be considered as a target pointer corresponding to the preset method.
[0072] In this embodiment, after obtaining the preset method, a target pointer corresponding to the preset method can be generated. For example, the method object of the preset method can be obtained by reflection, and the method object can be converted into a target pointer. Here, the method of obtaining the method object of the preset method and the method of converting the method object into the target pointer are not limited and can be set as needed.
[0073] S203. Based on the address of the first entry function and the first target pointer, determine the offset of the address of the first entry function in the first target pointer as the address offset of the entry function in the current system. The address offset is the offset of the address of the entry function in the corresponding target pointer.
[0074] In this embodiment, after obtaining the first target pointer corresponding to the preset method, the offset of the address of the first entry function in the first target pointer can be determined based on the address of the first entry function corresponding to the preset method and the first target pointer, thereby obtaining the address offset of the entry function in the current system.
[0075] For example, the content pointed to by the first target pointer at different offsets can be obtained, and the content pointed to by the first target pointer at different offsets can be compared with the address of the obtained first entry function at the same time or in sequence to obtain the content that is the same as the address of the first entry function, and the offset when the first target pointer points to the content that is the same as the address of the first entry function is determined as the offset of the address of the first entry function in the first target pointer.
[0076] In some embodiments, determining the offset of the address of the first entry function in the first target pointer based on the address of the first entry function and the first target pointer includes: matching the content pointed to by the first target pointer with the address of the first entry function in sequence until the content pointed to by the first target pointer successfully matches the address of the first entry function; and determining the offset when the match is successful as the offset of the address of the first entry function in the first target pointer.
[0077] In the above implementation, the content pointed to by the first target pointer can be matched with the address of the first entry function in sequence until the content pointed to by the first target pointer matches the address of the first entry function successfully.
[0078] Exemplarily, the minimum offset or the maximum offset of the first target pointer can be used as the current offset in order of offset from small to large or in order of offset from large to small, to obtain the current content pointed to by the first target pointer at the current offset, and determine whether the current content is the same as the address of the first entry function. If so, the current offset is determined as the offset of the address of the first entry function in the first target pointer; if not, the next offset is used as the current offset, and the operation of obtaining the current content pointed to by the first target pointer at the current offset is returned until the current content is the same as the address of the first entry function.
[0079] S204. For each method to be detected, the address of the second entry function is obtained according to the address offset, and a hook operation is performed on the second entry function based on the address of the second entry function. The method to be detected includes a waiting method called by the thread to be detected and a wake-up method corresponding to the waiting method. The second entry function is the entry function corresponding to the method to be detected.
[0080] The second entry function may be considered as an entry function corresponding to the method to be detected, such as an entry function corresponding to each method to be detected in the Java layer of the current system in the C language layer of the current system.
[0081] In this embodiment, after obtaining the address offset of the entry function in the current system, a hook operation can be performed on each method to be detected based on the address offset, so as to facilitate subsequent detection of each method to be detected and / or the entry function corresponding to each method to be detected, and determine the blocking information of the thread to be detected.
[0082] Exemplarily, the hook operation can be performed on the method to be detected by performing a hook operation on the entry function corresponding to the method to be detected. For example, the address of the entry function corresponding to the method to be detected can be obtained according to the address offset of the current system entry function, the entry function corresponding to the method to be detected can be obtained according to the address of the entry function corresponding to the method to be detected, and the hook operation can be performed on the entry function corresponding to the method to be detected. Here, the specific execution method of performing the hook operation on the entry function corresponding to the method to be detected is not limited.
[0083] In this embodiment, the address of the entry function corresponding to the method to be detected can be obtained from the target pointer corresponding to the method to be detected. For example, a target pointer corresponding to the method to be detected can be generated; when the offset of the target pointer corresponding to the method to be detected is the address offset determined above, the content pointed to by the target pointer is obtained, thereby obtaining the address of the entry function corresponding to the method to be detected. At this time, optionally, the address of the second entry function is obtained according to the address offset, including: generating a second target pointer corresponding to the method to be detected; obtaining the address of the second entry function from the second target pointer according to the address offset. Among them, the second target pointer can be considered as the target pointer corresponding to the method to be detected, and the generation method of the second target pointer is similar to the generation method of the first target pointer, which will not be repeated here.
[0084] S205: Determine the blocking information of the thread to be detected according to the method to be detected after the hook operation is executed.
[0085] Specifically, after the hook operation is performed on the entry function corresponding to the method to be detected, the blocking information of the thread to be detected can be determined according to the entry function after the hook operation is performed on the method to be detected.
[0086] In some optional implementations, java.lang.Object (hereinafter referred to as Object) and sun.misc.Unsafe (hereinafter referred to as Unsafe) are two classes in the Android virtual machine that may cause thread blocking. The Object.wait method of the Object class and the Unsafe.park method of the Unsafe class are two common methods (i.e., waiting methods) that cause thread blocking in the Android virtual machine. After a thread is blocked, it can generally be awakened by another thread through the Object.notify method or Object.notifyAll method of the Object class and the Unsafe.unpark method (i.e., wake-up method) of the Unsafe class. Therefore, in some optional implementations, this embodiment can monitor and record the duration of thread blocking caused by the Object.wait method / Unsafe.park method, and monitor the associated Object.notify method / Object.notifyAll method / Unsafe.unpark method as the cause of the blocking.
[0087] Figure 3 A flowchart of an exemplary thread blocking monitoring method provided in this embodiment is shown as follows: Figure 3 As shown, taking the waiting function including the Object.wait method and / or the Unsafe.park method, and the wake-up method being the Object.notify method / Object.notifyAll method and / or the Unsafe.unpark method as an example, the process of how to monitor methods such as Object.wait / Unsafe.park and how to record the time consumption and attribution data in this embodiment is described as follows:
[0088] 1. The process of determining the address offset of the entry function
[0089] The Object.wait method is a regular Java method, which is implemented by the JNI method at the bottom layer; the Unsafe.park method is a JNI method, which can be monitored by the JNI Hook (i.e. hook) solution. The basic principle is to use the fact that the offset of the address of the JNI entry method (i.e. entry function) of the ArtMethod pointer (i.e. target pointer) of each Java Method object (i.e. method object of the Java layer) in the Android system is fixed. The determination process is as follows:
[0090] A1. In the business code, declare a Java JNI method and implement the corresponding JNI C++ method, such as JNI_Java (i.e., the preset method) and JNI_C (i.e., the first entry function).
[0091] A2. Obtain the Method object (ie, method object) of the JNI_Java method through reflection, and then convert it into an ArtMethod pointer (ie, the first target pointer).
[0092] A3. Traverse the contents pointed to by the ArtMethod pointer in turn and check whether they are the same as the address of the JNI_C method. If they are different, continue; if they are the same, record the index (i.e. offset) traversed at that time and save it to JNI_Entrance_Offset.
[0093] Therefore, after obtaining JNI_Entrance_Offset, any Java JNI method can be converted into an ArtMethod pointer and then the offset value of JNI_Entrance_Offset is taken as the entry address of the Java JNI method, which can be easily read and replaced.
[0094] 2. Blockage time monitoring
[0095] The specific process of hooking the Object.wait method is as follows:
[0096] B1.Declare a proxy function for the Object.wait method, such as Object_wait_proxy.
[0097] B2.Declare the prototype pointer of the original method of Object.wait method, such as Object_wait_origin.
[0098] B3. Call the prototype pointer of the Object.wait method in the proxy function of the Object.wait method, record the start and end traces of the function respectively, and record the unique identifier of the current lock object (i.e., the wait identifier) during the trace process, such as identityHashCode.
[0099] B4. Parse the Java Method object of the Object.wait method into the corresponding ArtMethod pointer (ie, the second target pointer).
[0100] B5. Save the JNI entry function in the ArtMethod pointer to the prototype pointer Object_wait_origin.
[0101] B6. Replace the JNI entry function in the ArtMethod pointer with the proxy function Object_wait_proxy.
[0102] The specific process of hooking the Unsafe.park method is as follows:
[0103] C1.Declare a proxy function for the Unsafe.park method, such as Unsafe_park_proxy.
[0104] C2.Declare the prototype pointer of the original method of the Unsafe.park method, such as Unsafe_park_origin.
[0105] C3. Call the prototype pointer of the Unsafe.park method in the proxy function of the Unsafe.park method, record the start and end traces of the function respectively, and record the unique identifier of the current thread object (that is, the waiting identifier), such as identityHashCode, during the trace process.
[0106] C4. Parse the Java Method object of Unsafe.park into the corresponding ArtMethod pointer (ie, the second target pointer).
[0107] C5. Save the JNI entry function in the ArtMethod pointer to the prototype pointer Unsafe_park_origin.
[0108] C6. Replace the JNI entry function in the ArtMethod pointer with the proxy function Unsafe_park_proxy.
[0109] In this way, the time consumption of Object.wait method and Unsafe.park method can be monitored.
[0110] 3. Blockage Cause Monitoring
[0111] When attributing the blocking caused by the Object.wait method, you can monitor the wakeup methods Object.notify and Object.notifyAll that correspond to the Object.wait method. The specific process is as follows:
[0112] D1. Declare proxy functions for the Object.notify method and the Object.notifyAll method, such as Object_notify_proxy and Object_notifyAll_proxy.
[0113] D2. Declare the prototype pointers of the original methods of Object.notify method and Object.notifyAll method, such as Object_notify_origin and Object_notifyAll_origin respectively.
[0114] D3. In the proxy function of the Object.notify method and the proxy function of the Object.notifyAll method, call the prototype pointer of the original method respectively, record the start and end traces of the function respectively, and record the unique identifier of the current lock object (that is, the wait identifier) during the trace process, such as identityHashCode, so that the Object.wait method and Object.notify method / Object.notifyAll method of the Object class can be associated through the unique identifier of the lock object.
[0115] D4. Parse the Java Method objects of the Object.notify method and the Object.notifyAll method into corresponding ArtMethod pointers (ie, the second target pointers) respectively.
[0116] D5. Save the JNI entry functions in their ArtMethod pointers to the prototype pointers Object_notify_origin and Object_notifyAll_origin respectively.
[0117] D6. Replace the JNI entry function in the ArtMethod pointer with the proxy functions Object_notify_proxy and Object_notifyAll_proxy respectively.
[0118] The specific monitoring process of the wake-up method Unsafe.unpark corresponding to Unsafe.park is as follows:
[0119] E1.Declare a proxy function for the Unsafe.unpark method, such as Unsafe_unpark_proxy.
[0120] E2.Declare the prototype pointer of the original method of the Unsafe.unpark method, such as Unsafe_unpark_origin.
[0121] E3. Call the prototype pointer in the proxy function of the Unsafe.unpark method, record the start and end traces of the function respectively, and record the unique identifier of the current thread object, such as identityHashCode, during the trace process. In this way, the Unsafe.park method and the Unsafe.unpark method of the Unsafe class can be associated through the thread object unique identifier.
[0122] E4. Parse the Java Method object of the Unsafe.unpark method into the corresponding ArtMethod pointer (ie, the second target pointer).
[0123] E5. Save the JNI entry function in the ArtMethod pointer to the prototype pointer Unsafe_unpark_origin.
[0124] E6. Replace the JNI entry function in the ArtMethod pointer with the proxy function Unsafe_unpark_proxy.
[0125] Therefore, after collecting the trace information, the Object.wait method and the Object.nofity method / notifyAll method can be associated through the identityHashCode of the lock object to determine the reason why Object.wait is time-consuming; the Unsafe.park method and the Unsafe.unpark method can be associated through the identityHashCod of the thread object to find the reason why Unsafe.park is time-consuming.
[0126] The thread blocking detection method provided in this embodiment can detect the blocking of the thread to be detected due to waiting for other threads to execute tasks, determine the blocking information of the thread to be detected, and improve the comprehensiveness of thread blocking analysis.
[0127] Figure 4 This is a structural block diagram of a thread blocking detection device provided by an embodiment of the present disclosure. The device can be implemented by software and / or hardware, and can be configured in an electronic device, typically a mobile phone or a tablet computer, and can detect thread blocking conditions by executing a thread blocking detection method, such as detecting blocking caused by a thread to be detected waiting for other threads to complete tasks. Figure 4 As shown, the thread blocking detection device provided in this embodiment may include: an offset acquisition module 401, an operation execution module 402 and an information determination module 403, wherein:
[0128] The offset acquisition module 401 is used to acquire the address offset of the entry function in the current system, where the address offset is the offset of the address of the entry function in the corresponding target pointer;
[0129] An operation execution module 402 is used to execute a hook operation on a method to be detected based on the address offset, wherein the method to be detected includes a waiting method called by the thread to be detected and a wake-up method corresponding to the waiting method;
[0130] The information determination module 403 is used to determine the blocking information of the thread to be detected according to the method to be detected after the hook operation is executed.
[0131] The thread blocking detection device provided in this embodiment obtains the address offset of the entry function in the current thread through the offset acquisition module, and the address offset is the offset of the address of the entry function in the corresponding target pointer; the operation execution module performs a hook operation on the detection method based on the address offset, and the detection method includes the waiting method called by the thread to be detected and the wake-up method corresponding to the waiting method; the information determination module determines the blocking information of the thread to be detected according to the method to be detected after the hook operation is performed. This embodiment utilizes the above technical solution, by determining the address offset of the entry function and performing a hook operation on the method to be detected based on the address offset, it is possible to detect the blocking of the thread to be detected caused by waiting for other threads to execute tasks, determine the blocking information of the thread to be detected, and improve the comprehensiveness of thread blocking analysis.
[0132] In the above scheme, the offset acquisition module 401 may include: a first address acquisition unit, used to acquire the address of the preset method and the first entry function declared in the current system, the first entry function being the entry function corresponding to the preset method; a pointer generation unit, used to generate a first target pointer corresponding to the preset method; an offset determination unit, used to determine the offset of the address of the first entry function in the first target pointer based on the address of the first entry function and the first target pointer, as the address offset of the entry function in the current system.
[0133] In the above scheme, the offset determination unit can be used to: match the content pointed to by the first target pointer with the address of the first entry function in sequence until the content pointed to by the first target pointer matches the address of the first entry function successfully; and determine the offset when the match is successful as the offset of the address of the first entry function in the first target pointer.
[0134] In the above scheme, the operation execution module 402 may include: a second address acquisition unit, used to obtain the address of the second entry function according to the address offset for each method to be detected, and the second entry function is the entry function corresponding to the method to be detected; a hook unit, used to perform a hook operation on the second entry function based on the address of the second entry function.
[0135] In the above scheme, the second address acquisition unit can be used to: generate a second target pointer corresponding to the method to be detected; and acquire the address of the second entry function from the second target pointer according to the address offset.
[0136] In the above scheme, the blocking information may include blocking time, and the information determination module 403 may include: a time recording unit, used to record the calling time of the method to be detected when a calling request for the method to be detected is received; a time determination unit, used to determine the blocking time of the thread to be detected according to the calling time.
[0137] In the above scheme, the blocking information may include the reason for blocking, and the information determination module 403 may include: an identification acquisition unit, which is used to obtain the waiting identification corresponding to the waiting method when receiving the first calling request of the thread to be detected for the waiting method; a request determination unit, which is used to determine the second calling request for the wake-up method based on the waiting identification; and a reason determination unit, which is used to determine the calling thread of the wake-up method based on the second calling request to obtain the blocking reason of the thread to be detected.
[0138] In the above solution, the waiting identifier may include at least one of a lock object identifier and a thread object identifier.
[0139] The thread blocking detection device provided in the embodiment of the present disclosure can execute the thread blocking detection method provided in any embodiment of the present disclosure, and has the corresponding functional modules and beneficial effects of executing the thread blocking detection method. For technical details not described in detail in this embodiment, please refer to the thread blocking detection method provided in any embodiment of the present disclosure.
[0140] Reference below Figure 5 , which shows a schematic diagram of the structure of an electronic device (e.g., terminal device) 500 suitable for implementing the embodiments of the present disclosure. The terminal device in the embodiments of the present disclosure may include, but is not limited to, mobile terminals such as mobile phones, notebook computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), vehicle-mounted terminals (e.g., vehicle-mounted navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 5The electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.
[0141] like Figure 5 As shown, the electronic device 500 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 501, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 502 or a program loaded from a storage device 508 into a random access memory (RAM) 503. In the RAM 503, various programs and data required for the operation of the electronic device 500 are also stored. The processing device 501, the ROM 502, and the RAM 503 are connected to each other via a bus 504. An input / output (I / O) interface 505 is also connected to the bus 504.
[0142] Typically, the following devices may be connected to the I / O interface 505: an input device 506 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 507 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 508 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 509. The communication device 509 may allow the electronic device 500 to communicate with other devices wirelessly or by wire to exchange data. Although Figure 5 The electronic device 500 is shown with various devices, but it should be understood that it is not required to implement or possess all the devices shown. More or fewer devices may be implemented or possessed instead.
[0143] In particular, according to an embodiment of the present disclosure, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a non-transitory computer-readable medium, and the computer program contains program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device 509, or installed from a storage device 508, or installed from a ROM 502. When the computer program is executed by the processing device 501, the above-mentioned functions defined in the method of the embodiment of the present disclosure are executed.
[0144] It should be noted that the computer-readable medium disclosed above may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present disclosure, a computer-readable storage medium may be any tangible medium containing or storing a program that may be used by or in combination with an instruction execution system, device or device. In the present disclosure, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, in which a computer-readable program code is carried. This propagated data signal may take a variety of forms, including but not limited to an electromagnetic signal, an optical signal, or any suitable combination of the above. The computer readable signal medium may also be any computer readable medium other than a computer readable storage medium, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer readable medium may be transmitted using any suitable medium, including but not limited to: wires, optical cables, RF (radio frequency), etc., or any suitable combination of the above.
[0145] In some embodiments, the client and the server may communicate using any currently known or future developed network protocol such as HTTP (HyperText Transfer Protocol), and may be interconnected with any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include a local area network ("LAN"), a wide area network ("WAN"), an internet (e.g., the Internet), and a peer-to-peer network (e.g., an ad hoc peer-to-peer network), as well as any currently known or future developed network.
[0146] The computer-readable medium may be included in the electronic device, or may exist independently without being incorporated into the electronic device.
[0147] The computer-readable medium carries one or more programs. When the one or more programs are executed by the electronic device, the electronic device: obtains the address offset of the entry function in the current system, where the address offset is the offset of the address of the entry function in the corresponding target pointer; performs a hook operation on the method to be detected based on the address offset, where the method to be detected includes a waiting method called by the thread to be detected and a wake-up method corresponding to the waiting method; and determines the blocking information of the thread to be detected according to the method to be detected after the hook operation is performed.
[0148] Computer program code for performing the operations of the present disclosure may be written in one or more programming languages or a combination thereof, including, but not limited to, object-oriented programming languages, such as Java, Smalltalk, C++, and conventional procedural programming languages, such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., through the Internet using an Internet service provider).
[0149] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present disclosure. In this regard, each square box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some implementations as replacements, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0150] The units involved in the embodiments described in the present disclosure may be implemented by software or hardware, wherein the name of a module does not, in some cases, constitute a limitation on the unit itself.
[0151] The functions described above herein may be performed at least in part by one or more hardware logic components. For example, without limitation, exemplary types of hardware logic components that may be used include: field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), and the like.
[0152] In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0153] According to one or more embodiments of the present disclosure, Example 1 provides a method for detecting thread blocking, including:
[0154] Obtaining the address offset of the entry function in the current system, where the address offset is the offset of the address of the entry function in the corresponding target pointer;
[0155] Performing a hook operation on a method to be detected based on the address offset, wherein the method to be detected includes a waiting method called by a thread to be detected and a wake-up method corresponding to the waiting method;
[0156] The blocking information of the thread to be detected is determined according to the method to be detected after the hook operation is executed.
[0157] According to one or more embodiments of the present disclosure, Example 2 is based on the method described in Example 1, wherein obtaining the address offset of the entry function in the current system includes:
[0158] Obtaining the address of the preset method and the first entry function declared in the current system, where the first entry function is the entry function corresponding to the preset method;
[0159] Generate a first target pointer corresponding to the preset method;
[0160] According to the address of the first entry function and the first target pointer, the offset of the address of the first entry function in the first target pointer is determined as the address offset of the entry function in the current system.
[0161] According to one or more embodiments of the present disclosure, Example 3 is the method according to Example 2, wherein determining the offset of the address of the first entry function in the first target pointer based on the address of the first entry function and the first target pointer includes:
[0162] Sequentially matching the content pointed to by the first target pointer with the address of the first entry function until the content pointed to by the first target pointer successfully matches the address of the first entry function;
[0163] The offset when the match is successful is determined as the offset of the address of the first entry function in the first target pointer.
[0164] According to one or more embodiments of the present disclosure, Example 4 is the method according to Example 1, wherein the performing a hook operation on the method to be detected based on the address offset includes:
[0165] For each method to be detected, obtaining the address of a second entry function according to the address offset, where the second entry function is the entry function corresponding to the method to be detected;
[0166] A hook operation is performed on the second entry function based on the address of the second entry function.
[0167] According to one or more embodiments of the present disclosure, Example 5 is the method according to Example 4, wherein obtaining the address of the second entry function according to the address offset includes:
[0168] Generate a second target pointer corresponding to the method to be detected;
[0169] The address of the second entry function is obtained from the second target pointer according to the address offset.
[0170] According to one or more embodiments of the present disclosure, Example 6 is a method according to any one of Examples 1-5, wherein the blocking information includes blocking time, and the determining the blocking information of the thread to be detected according to the method to be detected after executing the hook operation includes:
[0171] When receiving a call request for the method to be detected, recording the call time of the method to be detected;
[0172] The blocking time of the thread to be detected is determined according to the calling time.
[0173] According to one or more embodiments of the present disclosure, Example 7 is a method according to any one of Examples 1-5, wherein the blocking information includes a blocking reason, and the determining the blocking information of the thread to be detected according to the method to be detected after executing the hook operation includes:
[0174] When receiving a first call request of the thread to be detected for a waiting method, obtaining a waiting identifier corresponding to the waiting method;
[0175] Determine a second call request for the wake-up method based on the wait identifier;
[0176] The calling thread of the wake-up method is determined according to the second calling request, and the blocking reason of the thread to be detected is obtained.
[0177] According to one or more embodiments of the present disclosure, Example 8 is based on the method described in Example 7, wherein the wait identifier includes at least one of a lock object identifier and a thread object identifier.
[0178] According to one or more embodiments of the present disclosure, Example 9 provides a thread blocking detection device, including:
[0179] An offset acquisition module is used to acquire the address offset of the entry function in the current system, where the address offset is the offset of the address of the entry function in the corresponding target pointer;
[0180] An operation execution module, configured to execute a hook operation on a method to be detected based on the address offset, wherein the method to be detected includes a waiting method called by a thread to be detected and a wake-up method corresponding to the waiting method;
[0181] The information determination module is used to determine the blocking information of the thread to be detected according to the method to be detected after the hook operation is executed.
[0182] According to one or more embodiments of the present disclosure, Example 10 provides an electronic device, including:
[0183] one or more processors;
[0184] a memory for storing one or more programs,
[0185] When the one or more programs are executed by the one or more processors, the one or more processors implement the thread blocking detection method as described in any one of Examples 1-8.
[0186] According to one or more embodiments of the present disclosure, Example 11 provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the thread blocking detection method as described in any one of Examples 1-8.
[0187] The above description is only a preferred embodiment of the present disclosure and an explanation of the technical principles used. Those skilled in the art should understand that the scope of disclosure involved in the present disclosure is not limited to the technical solutions formed by a specific combination of the above technical features, but should also cover other technical solutions formed by any combination of the above technical features or their equivalent features without departing from the above disclosed concept. For example, the above features are replaced with the technical features with similar functions disclosed in the present disclosure (but not limited to) by each other to form a technical solution.
[0188] In addition, although each operation is described in a specific order, this should not be understood as requiring these operations to be performed in the specific order shown or in a sequential order. Under certain circumstances, multitasking and parallel processing may be advantageous. Similarly, although some specific implementation details are included in the above discussion, these should not be interpreted as limiting the scope of the present disclosure. Some features described in the context of a separate embodiment can also be implemented in a single embodiment in combination. On the contrary, the various features described in the context of a single embodiment can also be implemented in multiple embodiments individually or in any suitable sub-combination mode.
[0189] Although the subject matter has been described in language specific to structural features and / or methodological logical actions, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or actions described above. On the contrary, the specific features and actions described above are merely example forms of implementing the claims.
Claims
1. A method for detecting thread blocking, characterized in that: include: Obtaining the address offset of the entry function in the current system, where the address offset is the offset of the address of the entry function in the corresponding target pointer; Performing a hook operation on a method to be detected based on the address offset, wherein the method to be detected includes a waiting method called by a thread to be detected and a wake-up method corresponding to the waiting method; The blocking information of the thread to be detected is determined according to the method to be detected after the hook operation is executed.
2. The method according to claim 1, characterized in that The step of obtaining the address offset of the entry function in the current system includes: Obtaining the address of the preset method and the first entry function declared in the current system, where the first entry function is the entry function corresponding to the preset method; Generate a first target pointer corresponding to the preset method; According to the address of the first entry function and the first target pointer, the offset of the address of the first entry function in the first target pointer is determined as the address offset of the entry function in the current system.
3. The method according to claim 2, characterized in that The step of determining the offset of the address of the first entry function in the first target pointer according to the address of the first entry function and the first target pointer includes: Matching the content pointed to by the first target pointer with the address of the first entry function in sequence until the content pointed to by the first target pointer matches the address of the first entry function successfully; The offset when the match is successful is determined as the offset of the address of the first entry function in the first target pointer.
4. The method according to claim 1, characterized in that: The performing a hook operation on the method to be detected based on the address offset comprises: For each method to be detected, obtaining the address of a second entry function according to the address offset, where the second entry function is the entry function corresponding to the method to be detected; A hook operation is performed on the second entry function based on the address of the second entry function.
5. The method according to claim 4, characterized in that The acquiring the address of the second entry function according to the address offset comprises: Generate a second target pointer corresponding to the method to be detected; The address of the second entry function is obtained from the second target pointer according to the address offset.
6. The method according to any one of claims 1 to 5, characterized in that: The blocking information includes the blocking time, and the determining of the blocking information of the thread to be detected according to the method to be detected after the hook operation is executed includes: When receiving a call request for the method to be detected, recording the call time of the method to be detected; The blocking time of the thread to be detected is determined according to the calling time.
7. The method according to any one of claims 1 to 5, characterized in that: The blocking information includes a blocking reason, and the determining the blocking information of the thread to be detected according to the method to be detected after the hook operation is executed includes: When receiving a first call request of the thread to be detected for a waiting method, obtaining a waiting identifier corresponding to the waiting method; Determine a second call request for the wake-up method based on the wait identifier; The calling thread of the wake-up method is determined according to the second calling request, and the blocking reason of the thread to be detected is obtained.
8. The method according to claim 7, characterized in that The wait identifier includes at least one of a lock object identifier and a thread object identifier.
9. A thread blocking detection device, characterized in that: include: An offset acquisition module is used to acquire the address offset of the entry function in the current system, where the address offset is the offset of the address of the entry function in the corresponding target pointer; An operation execution module, configured to execute a hook operation on a method to be detected based on the address offset, wherein the method to be detected includes a waiting method called by a thread to be detected and a wake-up method corresponding to the waiting method; The information determination module is used to determine the blocking information of the thread to be detected according to the method to be detected after the hook operation is executed.
10. An electronic device, characterized in that: include: at least one processor; as well as a memory communicatively connected to the at least one processor; wherein, The memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can perform the thread blocking detection method according to any one of claims 1 to 8.
11. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a processor to implement the thread blocking detection method according to any one of claims 1 to 8 when executed.
Citation Information
Cited By
Method, device and equipment for detecting synchronization function of multi-thread processor
CN121364979A
A method, apparatus and device for detecting synchronization functions of a multi-thread processor
CN121364979B