Detailed Description
Embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. While certain embodiments of the present disclosure are shown in the drawings, it is to be understood that the present disclosure may be embodied in various forms and should not be construed as limited to the embodiments set forth herein, but rather are provided for a more thorough and complete understanding of the present disclosure. It should be understood that the drawings and embodiments of the disclosure are for illustration purposes only and are not intended to limit the scope of the disclosure.
It should be understood that the various steps recited in the method embodiments of the present disclosure may be performed in a different order, and/or performed in parallel. Moreover, method embodiments may include additional steps and/or omit performing the illustrated steps. The scope of the present disclosure is not limited in this respect.
The term "include" and variations thereof as used herein are open-ended, i.e., "including but not limited to". The term "based on" is "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". Relevant definitions for other terms will be given in the following description.
It should be noted that the terms "first", "second", and the like in the present disclosure are only used for distinguishing the devices, modules or units, and are not used for limiting the devices, modules or units to be different devices, modules or units, and also for limiting the sequence or interdependence relationship of the functions executed by the devices, modules or units.
It is noted that references to "a", "an", and "the" modifications in this disclosure are intended to be illustrative rather than limiting, and that those skilled in the art will recognize that "one or more" may be used unless the context clearly dictates otherwise.
The names of messages or information exchanged between devices in the embodiments of the present disclosure are for illustrative purposes only, and are not intended to limit the scope of the messages or information.
The embodiment of the present disclosure provides a method for detecting a thread, a flow diagram of the method is shown in fig. 1, and the method includes:
s101, acquiring a plurality of shared libraries called when the application package is compiled.
In the embodiment of the disclosure, the so (shared object) library is a shared library, and in Android development, common codes are developed and packaged into the so library by using java language for calling other modules, so as to improve the reuse rate of the common codes. Compiling the application package requires preprocessing, compiling, assembling, and linking. Some public codes need to be used repeatedly, the public codes can be compiled into library files, namely a so library, the so library is used for dynamic connection and is compiled out of C or C + +, and the so library can save system resources, accelerate the running speed and simplify code upgrading. The functions in the library file may be linked to the application package through a linker, thereby eliminating the need to compile the functions in the library file each time the application is developed.
In the embodiment of the disclosure, all the so libraries used by the application package are scanned out by calling the plug-in program in the process of starting and compiling the application package.
S102, scanning the shared libraries called by any one of the shared libraries except any one of the shared libraries to detect first functions included in the shared libraries except any one of the shared libraries.
In the embodiment of the present disclosure, the so libraries called by any one of the plurality of so libraries other than any one of the so libraries are scanned for detecting the first function included in the so libraries other than any one of the so libraries.
In the embodiment of the present disclosure, the first function includes a function pthread _ create created by the thread.
In the embodiment of the present disclosure, pthread _ create is a function of thread creation of the operating system, pthread _ create is a function of creating a thread, and after the thread is created, the relevant thread function starts to run.
S103, when the first function is detected, scanning the shared library including the first function for detecting a second function paired with the first function.
In the embodiment of the present disclosure, when the first function is detected, the so library including the first function is scanned for detecting the second function paired with the first function.
In an embodiment of the disclosure, the second function includes at least one of:
a function pthread _ attr _ setdachstate of thread separation state and a thread recycle function phtread _ join.
In the embodiment of the present disclosure, the PTHREAD _ attr _ setdachstate includes a split thread parameter PTHREAD _ CREATE _ DETACHED, and when a thread is created using PTHREAD _ CREATE, the PTHREAD _ CREATE _ DETACHED state is set by PTHREAD _ attr _ setdachsate, so that the stack space memory corresponding to the thread is released, thereby avoiding thread leakage.
In the embodiment of the present disclosure, the thread recycling function pthread _ join is used to wait for the completion of a thread and the operation of synchronization between threads. The main thread is blocked by pthread _ join waiting for other threads to exit so that the main thread can clear the context of other threads.
And S104, when the second function is detected, determining that the thread for running the application program package has no thread leakage.
In the embodiment of the disclosure, when the second function is not detected, determining that thread leakage exists in a thread running the application package, and determining information of any so library; and repairing the thread leakage according to the information of any so library.
In the embodiment of the present disclosure, the information of any so library includes at least one of the following:
name of any so library, binary archive file aar package, symbol of thread.
In the embodiment of the present disclosure, the so library has a file name containing real library code, called the name realname of the so library, and the naming format is usually libxxx. The aar (Android Archive) package is a binary Archive file of Android library items. The notation of a thread includes a thread identification ID, which is represented by a pthread _ t data type.
In the embodiment of the present disclosure, after determining that there is thread leakage in a thread running an application package, and determining information of any so library, the method further includes:
and generating prompt information aiming at the thread leakage according to the thread leakage, wherein the prompt information comprises the information of any so library.
In the embodiment of the disclosure, a plurality of shared libraries so called when an application package is compiled are obtained; scanning so as to detect a first function included in a so library except any so library called by any so library in the plurality of so libraries; when the first function is detected, scanning a so library comprising the first function for detecting a second function paired with the first function; when the first function is used for creating the thread, the second function is called to recover the thread, so that leakage cannot be caused, whether the thread for running the application program package has thread leakage or not is determined by detecting whether the second function used in pair with the first function is detected or not, thread leakage is accurately positioned, a user can find and solve the problem of thread leakage in advance in the program development and test stages, the problem that the thread leakage occurs after the program is issued and various programs are crashed is avoided, and user experience is improved.
Another method for detecting a thread is provided in the embodiments of the present disclosure, and a flow diagram of the method is shown in fig. 2, where the method includes:
s201, writing a plug-in program.
In the embodiment of the present disclosure, the plug-in is configured to perform thread leakage detection on a thread running an APK (Android application package).
S202, compiling the APK.
In the embodiment of the disclosure, in the process of compiling the APK, the plug-in program is used for detecting thread leakage of the thread running the APK.
S203, calling the plug-in program to scan out all so libraries used by the APK.
S204, traversing each so library through the plug-in program, and inquiring the external symbols called by each so library of dump.
In the embodiment of the present disclosure, the external symbol called by the dump first so library is queried by the plug-in, and the external symbol is a so library other than the first so library, for example, the so library other than the first so library includes a second so library, and the external symbol includes a function implemented in the second so library, and the second so library is called by the first so.
S205, detecting whether the external symbol of each so library includes pthread _ create, and if it is determined that the external symbol includes pthread _ create, going to step S206.
In the embodiment of the present disclosure, it is detected whether the external symbol of the first so repository includes pthread _ create, for example, whether the second so repository includes pthread _ create, and when it is determined that the second so repository includes pthread _ create, the process goes to step S206.
S206, judging whether pthread _ attr _ setdachstat or phtread _ join used for pairing with pthread _ create exists, and turning to the step S207 to process when determining that pthread _ attr _ setdachstat or phtread _ join used for pairing with pthread _ create exists; when it is determined that there is no pthread _ attr _ setdachstat or phtread _ join used as a pair with pthread _ create, processing proceeds to step S208.
In the embodiment of the present disclosure, pthread _ attr _ setdatchstate is used to set the thread state, for example, active recycle. phtread _ join is used to recycle threads.
And S207, determining that the thread running the APK has no thread leakage.
And S208, determining that thread leakage exists in the thread running the APK, and collecting the information of the so library.
In the embodiment of the disclosure, it is determined that there is thread leakage in the thread running the APK, and information of the first so library is collected.
In the embodiment of the present disclosure, the information of the first so repository includes a name of the first so repository, a binary archive file aar package, and a symbol of the thread.
S209, triggering a compiling error and printing the information of the so library.
In the embodiment of the disclosure, according to the thread leakage, a compiling error is triggered, that is, prompt information for the thread leakage is generated, where the prompt information includes information of the first so library, and the information of the first so library is printed.
And S210, repairing the thread leakage by the user according to the information of the so library.
In the embodiment of the disclosure, the user repairs the thread leakage according to the information of the first so library; when the thread leakage repair is completed, the APK is normally issued and executed.
The application of the embodiment of the disclosure has at least the following beneficial effects:
whether the thread leakage exists in the thread running the application program package APK is determined by detecting whether phtread _ join or pthread _ attr _ setdachstat paired with pthread _ create exists, so that the thread leakage is accurately positioned, a user can find and solve the problem of thread leakage in advance in the program development and test stage, the problem of thread leakage after the program is issued and various programs are crashed is avoided, and the user experience is improved.
Based on the same inventive concept, the embodiment of the present disclosure further provides a thread detection apparatus, a schematic structural diagram of the apparatus is shown in fig. 3, and the thread detection apparatus 30 includes a first processing module 301, a second processing module 302, a third processing module 303, and a fourth processing module 304.
A first processing module 301, configured to obtain multiple shared libraries called when compiling an application package;
a second processing module 302, configured to scan shared libraries called by any shared library of the multiple shared libraries, except for any shared library, so as to detect a first function included in the shared libraries except for any shared library;
a third processing module 303, configured to, when the first function is detected, scan a shared library including the first function for detecting a second function paired with the first function;
a fourth processing module 304, configured to determine that there is no thread leakage from the thread running the application package when the second function is detected.
In an embodiment of the disclosure, the first function includes a thread-created function, and the second function includes at least one of:
a thread separation state function and a thread recycle function.
In this embodiment of the present disclosure, the fourth processing module 304 is specifically configured to determine that there is thread leakage in a thread running an application package and determine information of any shared library when the second function is not detected; and repairing the thread leakage according to the information of any shared library.
In the embodiment of the present disclosure, the information of any shared library includes at least one of the following:
name of any shared library, binary archive file, symbol of thread.
In this embodiment of the disclosure, the fourth processing module 304 is specifically configured to generate prompt information for thread leakage according to the thread leakage after determining that there is thread leakage in a thread running an application package and determining information of any shared library, where the prompt information includes information of any shared library.
The application of the embodiment of the disclosure has at least the following beneficial effects:
acquiring a plurality of shared libraries called when compiling the application package; scanning shared libraries called by any shared library in the plurality of shared libraries except any shared library, so as to detect a first function included in the shared libraries except any shared library; when a first function is detected, scanning a shared library comprising the first function for detecting a second function paired with the first function; when the first function is used for creating the thread, the second function is called to recover the thread, so that leakage cannot be caused, whether the thread for running the application program package has thread leakage or not is determined by detecting whether the second function used in pair with the first function is detected or not, thread leakage is accurately positioned, a user can find and solve the problem of thread leakage in advance in the program development and test stages, the problem that the thread leakage occurs after the program is issued and various programs are crashed is avoided, and user experience is improved.
For the content that is not described in detail in the thread detection apparatus provided in the embodiment of the present disclosure, reference may be made to the thread detection method provided in the above embodiment, and the beneficial effects that can be achieved by the thread detection apparatus provided in the embodiment of the present disclosure are the same as the thread detection method provided in the above embodiment, and are not described again here.
Referring now to FIG. 4, a block diagram of an electronic device 800 suitable for use in implementing embodiments of the present disclosure is shown. The terminal device in the embodiments of the present disclosure may include, but is not limited to, a mobile terminal such as a mobile phone, a notebook computer, a digital broadcast receiver, a PDA (personal digital assistant), a PAD (tablet computer), a PMP (portable multimedia player), a vehicle terminal (e.g., a car navigation terminal), and the like, and a stationary terminal such as a digital TV, a desktop computer, and the like. The electronic device shown in fig. 4 is only an example, and should not bring any limitation to the functions and the scope of use of the embodiments of the present disclosure.
The electronic device includes: a memory and a processor, wherein the processor may be referred to as the processing device 801 described below, and the memory may include at least one of a Read Only Memory (ROM)802, a Random Access Memory (RAM)803, and a storage device 808, as shown in fig. 4:
the electronic device 800 may include a processing means (e.g., a central processing unit, a graphics processor, etc.) 801 that may perform various appropriate actions and processes in accordance with a program stored in a Read Only Memory (ROM)802 or a program loaded from a storage means 808 into a Random Access Memory (RAM) 803. In the RAM 803, various programs and data necessary for the operation of the electronic apparatus 800 are also stored. The processing apparatus 801, the ROM 802, and the RAM 803 are connected to each other by a bus 804. An input/output (I/O) interface 805 is also connected to bus 804.
Generally, the following devices may be connected to the I/O interface 805: input devices 806 including, for example, a touch screen, touch pad, keyboard, mouse, camera, microphone, accelerometer, gyroscope, etc.; output devices 807 including, for example, a Liquid Crystal Display (LCD), speakers, vibrators, and the like; storage 808 including, for example, magnetic tape, hard disk, etc.; and a communication device 809. The communication means 809 may allow the electronic device 800 to communicate wirelessly or by wire with other devices to exchange data. While fig. 4 illustrates an electronic device 800 having various means, it is to be understood that not all illustrated means are required to be implemented or provided. More or fewer devices may alternatively be implemented or provided.
In particular, according to an embodiment of the present disclosure, the processes described above with reference to the flowcharts may be implemented as computer software programs. For example, embodiments of the present disclosure include a computer program product comprising a computer program carried on a non-transitory computer readable medium, the computer program containing program code for performing the method illustrated by the flow chart. In such an embodiment, the computer program may be downloaded and installed from a network through the communication means 809, or installed from the storage means 808, or installed from the ROM 802. The computer program, when executed by the processing apparatus 801, performs the above-described functions defined in the methods of the embodiments of the present disclosure.
It should be noted that the computer readable medium in the present disclosure can be a computer readable signal medium or a computer readable storage medium or any combination of the two. A computer readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the foregoing. More specific examples of the computer readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer diskette, 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 disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the present disclosure, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In contrast, in the present disclosure, a computer readable signal medium may comprise a propagated data signal with computer readable program code embodied therein, either in baseband or as part of a carrier wave. Such a propagated data signal may take many forms, including, but not limited to, electro-magnetic, optical, or any suitable combination thereof. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: electrical wires, optical cables, RF (radio frequency), etc., or any suitable combination of the foregoing.
In some embodiments, the clients, servers 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 communications network). Examples of communication networks include a local area network ("LAN"), a wide area network ("WAN"), the Internet (e.g., the Internet), and peer-to-peer networks (e.g., ad hoc peer-to-peer networks), as well as any currently known or future developed network.
The computer readable medium may be embodied in the electronic device; or may exist separately without being assembled into the electronic device.
The computer readable medium carries one or more programs which, when executed by the electronic device, cause the electronic device to: acquiring a plurality of shared libraries called when compiling the application package; scanning shared libraries called by any shared library in the plurality of shared libraries except any shared library, so as to detect a first function included in the shared libraries except any shared library; when a first function is detected, scanning a shared library comprising the first function for detecting a second function paired with the first function; when the second function is detected, it is determined that there is no thread leak for the thread running the application package.
Computer program code for carrying out operations for the present disclosure may be written in any combination of one or more programming languages, including but not limited to an object oriented programming language such as Java, Smalltalk, C + +, and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code may execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer or entirely on the remote computer or server. In the case of 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 the connection may be made to an external computer (for example, through the Internet using an Internet service provider).
The flowchart and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present disclosure. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams and/or flowchart illustration, and combinations of blocks in the block diagrams and/or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The modules or units described in the embodiments of the present disclosure may be implemented by software or hardware. Wherein the designation of a module or unit does not in some cases constitute a limitation of the unit itself.
The functions described herein above 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 a chip (SOCs), Complex Programmable Logic Devices (CPLDs), and the like.
In the context of this disclosure, a machine-readable medium may be a tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. The 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, apparatus, or device, or any suitable combination of the foregoing. More specific examples of a machine-readable storage medium would include an electrical connection based on one or more wires, a portable computer diskette, 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 disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
According to one or more embodiments of the present disclosure, an embodiment provides a thread detection method, including:
acquiring a plurality of shared libraries called when compiling the application package;
scanning shared libraries called by any shared library in the plurality of shared libraries except any shared library, so as to detect a first function included in the shared libraries except any shared library;
when a first function is detected, scanning a shared library comprising the first function for detecting a second function paired with the first function;
when the second function is detected, it is determined that there is no thread leak for the thread running the application package.
In an embodiment of the disclosure, the first function includes a thread-created function, and the second function includes at least one of:
a thread separation state function and a thread recycle function.
In the embodiment of the disclosure, when the second function is not detected, determining that thread leakage exists in a thread running an application package, and determining information of any shared library; and repairing the thread leakage according to the information of any shared library.
In the embodiment of the present disclosure, the information of any shared library includes at least one of the following:
name of any shared library, binary archive file, symbol of thread.
In the embodiment of the present disclosure, after determining that there is thread leakage in a thread running an application package, and determining information of any shared library, the method further includes:
and generating prompt information aiming at the thread leakage according to the thread leakage, wherein the prompt information comprises information of any shared library.
According to one or more embodiments of the present disclosure, an embodiment provides an apparatus for detecting a thread, including:
the first processing module is used for acquiring a plurality of shared libraries called when the application package is compiled;
the second processing module is used for scanning the shared libraries called by any one of the shared libraries except any one of the shared libraries so as to detect first functions included in the shared libraries except any one of the shared libraries;
the third processing module is used for scanning a shared library comprising the first function when the first function is detected so as to detect a second function paired with the first function;
and the fourth processing module is used for determining that the thread for running the application program package has no thread leakage when the second function is detected.
In an embodiment of the disclosure, the first function includes a thread-created function, and the second function includes at least one of:
a thread separation state function and a thread recycle function.
In the embodiment of the present disclosure, the fourth processing module is specifically configured to determine that there is thread leakage in a thread running an application package and determine information of any shared library when the second function is not detected; and repairing the thread leakage according to the information of any shared library.
In the embodiment of the present disclosure, the information of any shared library includes at least one of the following:
name of any shared library, binary archive file, symbol of thread.
In an embodiment of the present disclosure, the fourth processing module is specifically configured to generate, according to the thread leakage, prompt information for the thread leakage after determining that the thread running the application package has the thread leakage and determining information of any shared library, where the prompt information includes information of any shared library.
The foregoing description is only exemplary of the preferred embodiments of the disclosure and is illustrative of the principles of the technology employed. It will be appreciated by those skilled in the art that the scope of the disclosure herein is not limited to the particular combination of features described above, but also encompasses other embodiments in which any combination of the features described above or their equivalents does not depart from the spirit of the disclosure. For example, the above features and (but not limited to) the features disclosed in this disclosure having similar functions are replaced with each other to form the technical solution.
Further, while operations are depicted in a particular order, this should not be understood as requiring that such operations be performed in the particular order shown or in sequential order. Under certain circumstances, multitasking and parallel processing may be advantageous. Likewise, while several specific implementation details are included in the above discussion, these should not be construed as limitations on the scope of the disclosure. Certain features that are described in the context of separate embodiments can also be implemented in combination in a single embodiment. Conversely, various features that are described in the context of a single embodiment can also be implemented in multiple embodiments separately or in any suitable subcombination.
Although the subject matter has been described in language specific to structural features and/or methodological acts, it is to be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or acts described above. Rather, the specific features and acts described above are disclosed as example forms of implementing the claims.