WO2022199282A1 - 线程快照解析方法、装置、设备和存储介质 - Google Patents

线程快照解析方法、装置、设备和存储介质 Download PDF

Info

Publication number
WO2022199282A1
WO2022199282A1 PCT/CN2022/076543 CN2022076543W WO2022199282A1 WO 2022199282 A1 WO2022199282 A1 WO 2022199282A1 CN 2022076543 W CN2022076543 W CN 2022076543W WO 2022199282 A1 WO2022199282 A1 WO 2022199282A1
Authority
WO
WIPO (PCT)
Prior art keywords
address
thread
target
offset
offset address
Prior art date
Application number
PCT/CN2022/076543
Other languages
English (en)
French (fr)
Inventor
刘成清
Original Assignee
北京字节跳动网络技术有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 北京字节跳动网络技术有限公司 filed Critical 北京字节跳动网络技术有限公司
Publication of WO2022199282A1 publication Critical patent/WO2022199282A1/zh

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/30Monitoring
    • G06F11/3089Monitoring arrangements determined by the means or processing involved in sensing the monitored data, e.g. interfaces, connectors, sensors, probes, agents
    • G06F11/3093Configuration details thereof, e.g. installation, enabling, spatial arrangement of the probes
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/30Monitoring
    • G06F11/3003Monitoring arrangements specially adapted to the computing system or computing system component being monitored
    • G06F11/302Monitoring arrangements specially adapted to the computing system or computing system component being monitored where the computing system component is a software system
    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Definitions

  • the present disclosure relates to the field of computer technologies, and in particular, to a thread snapshot analysis method, apparatus, device, and storage medium.
  • embodiments of the present disclosure provide a thread snapshot analysis method, apparatus, device, and storage medium.
  • an embodiment of the present disclosure provides a thread snapshot parsing method, including:
  • At least one target program counter address is determined based on the program counter address corresponding to the second layer stack frame in the call stack of each thread; wherein, the program counter address is used to identify the program running process.
  • At least one offset address corresponding to the thread snapshot in the preset run cycle is determined based on the at least one target program counter address, the initial start address, the initial end address, and the virtual offset value interval;
  • a target offset address corresponding to the thread snapshot in the preset running cycle is determined; wherein, the preset policy is used to define and determine the target offset address the rule of;
  • an embodiment of the present disclosure further provides a thread snapshot parsing device, including:
  • the target program counter address determination module is used to determine at least one target program counter address based on the program counter address corresponding to the second layer stack frame in the call stack of each thread in the preset running cycle of the application; wherein, the program The counter address is used to identify the address of the function instruction being executed during the running of the program;
  • the symbol table file acquisition module is used to acquire the symbol table file of the application program, and determine the initial start address, initial end address of the main function recorded in the symbol table file, and the code segment corresponding to the application program. virtual offset value;
  • An offset address interval determination module is configured to determine, based on the at least one target program counter address, the initial start address, the initial end address, and the virtual offset value, the difference between the two in the preset running cycle. at least one offset address range corresponding to the thread snapshot;
  • a target offset address determination module configured to determine a target offset address corresponding to a thread snapshot in the preset operation cycle based on a preset policy and the at least one offset address interval; wherein the preset policy uses for defining rules for determining the target offset address;
  • a thread snapshot parsing module configured to perform symbolic parsing on the thread snapshot in the preset running cycle based on the target offset address.
  • embodiments of the present disclosure further provide an electronic device, including a memory and a processor, wherein a computer program is stored in the memory, and when the computer program is executed by the processor, the electronic device is made to The device implements any of the thread snapshot parsing methods provided in the embodiments of the present disclosure.
  • an embodiment of the present disclosure further provides a computer-readable storage medium, where a computer program is stored in the storage medium, and when the computer program is executed by a computing device, the computing device enables the computing device to implement the embodiment of the present disclosure Any of the provided thread snapshot parsing methods.
  • the program counter address (or PC address) is determined, at least one target program counter address is determined, and then combined with the information recorded in the symbol table file of the application program, the target offset address allocated to the function during the running process of the program is predicted, and then based on this Target offset address, which performs symbolic analysis on the thread snapshot in the preset running cycle.
  • the target offset address based on the target offset address, the real address of the function in the program running process can be obtained, and the function name and other descriptions can be determined based on the real address of the function.
  • the embodiments of the present disclosure realize the accurate acquisition of relevant program information during the running process of the application program, thereby helping to accurately monitor and analyze the running performance of the application program, and solve the problem that the existing solution cannot accurately obtain the application program.
  • the relevant program information in the running process leads to the problem that the running performance of the application cannot be accurately monitored and analyzed.
  • FIG. 1 is a flowchart of a thread snapshot analysis method provided by an embodiment of the present disclosure
  • FIG. 2 is a flowchart of another thread snapshot analysis method provided by an embodiment of the present disclosure
  • FIG. 3 is a flowchart of another thread snapshot analysis method provided by an embodiment of the present disclosure.
  • FIG. 4 is a schematic diagram of a processing architecture for establishing communication with a device to be monitored according to an embodiment of the present disclosure
  • FIG. 5 is a schematic structural diagram of a thread snapshot parsing apparatus according to an embodiment of the present disclosure
  • FIG. 6 is a schematic structural diagram of an electronic device according to an embodiment of the present disclosure.
  • FIG. 1 is a flowchart of a thread snapshot analysis method provided by an embodiment of the present disclosure.
  • the method can be applied to how to take snapshots of each thread during the running of an application (referring to a specific moment when the application is running, the program counter of the code call stack) address, or PC address) for symbolic analysis, so as to obtain description information such as the function name of each function during the running of the program, so as to monitor and analyze the running performance of the application.
  • the program counter (Program Counter, PC) address (or referred to as the PC address) refers to the instruction address stored in the program counter, which can be used to identify the function instruction address being executed during the running of the program.
  • the program counter is a register in a central processing unit (CPU) that indicates the computer's position in its program sequence.
  • the program counter may also be called an instruction pointer , instruction address register, or instruction counter, etc.
  • the application program mentioned in the embodiments of the present disclosure may be at least one type of application program installed on the electronic device, such as a video interaction application program and the like.
  • the electronic device may be at least one type of device with computing capabilities, such as a terminal or a server.
  • the operating system in the electronic device on which the application is installed may be, for example, an iOS system or the like.
  • the thread snapshot parsing method provided by the embodiments of the present disclosure may be executed by a thread snapshot parsing apparatus, which may be implemented in software and/or hardware, and may be integrated on an electronic device for executing the technical solutions of the embodiments of the present disclosure.
  • the thread snapshot analysis method may include:
  • S101 in the preset running cycle of the application program, determine at least one target program counter address based on the program counter address corresponding to the second layer stack frame in the call stack of each thread; wherein, the program counter address is used to identify the program running process The address of the function instruction being executed.
  • the second-layer stack frame in the call stack of the main thread is used to store the relevant information of the main function (main function), that is, the second-layer stack frame in the call stack of the main thread must correspond to the main function.
  • main function main function
  • the program counter address corresponding to the second layer stack frame in the call stack of each thread during the program running process can be obtained, and the policy can be filtered according to the program counter address (for example, in the The number of occurrences of the program counter address in each thread, whether a program counter address appears in at least two threads, etc.), at least one target program counter address is determined from the obtained multiple program counter addresses, as the effective program counter address, to It is used to predict the offset address corresponding to the thread snapshot during the running of the program (that is, the offset address of the function during the running of the program).
  • the offset address of each function is the same during program operation.
  • the time length of the preset operation period can be set according to requirements, which is not specifically limited in the embodiment of the present disclosure.
  • At least one target program counter address is determined, including:
  • a program counter address whose proportion of occurrence times is greater than or equal to the second proportion threshold and only occurs in one thread is determined as at least one target program counter address.
  • the value of the second proportion threshold may be determined according to the situation, which is not specifically limited in the embodiment of the present disclosure.
  • the confidence level of the program counter address that only appears in one thread is higher than the confidence level of the program counter address that appears in multiple threads at the same time, and the number of occurrences is greater than or equal to the second proportion threshold.
  • the program counter address whose proportion of the number of upcoming occurrences is greater than or equal to the second proportion threshold and only appears in one thread is determined as at least one target program counter address, which lays a foundation for the subsequent correct prediction of the offset address.
  • the preset running cycle of the application may include multiple threads, specifically taking thread 1 and thread 2 as examples, the process of determining the address of the target program counter is exemplified, and the sum of the total number of thread snapshots in the multiple threads is: 100, that is, the total number of program counter addresses in multiple threads is 100, where:
  • the total number of occurrences of program counter address A is 10, and the proportion of occurrences is 10%;
  • the total number of occurrences of the program counter address B is 1, and the proportion of occurrences is 1%;
  • the total number of occurrences of the program counter address C is 2, and the proportion of occurrences is 2%;
  • the total number of occurrences of the program counter address C is 5, and the proportion of occurrences is 5%;
  • the total number of occurrences of the program counter address D is 8, and the proportion of occurrences is 8%;
  • the program counter address B Assuming that the second proportion threshold is 1.5%, the number of occurrences of the above program counter address B is 1%, which is less than 1.5%, so the program counter address B needs to be filtered out, and the program counter address C is in thread 1 and thread 2. Both appear, so the program counter address C also needs to be filtered out, and finally the program counter address A and the program counter address D can be used as the target program counter address.
  • S102 Obtain the symbol table file of the application program, and determine the initial start address, initial end address of the main function recorded in the symbol table file, and the virtual offset value of the code segment corresponding to the application program.
  • Each application program has its own corresponding symbol table (dsym) file.
  • the symbol table is a mapping table between memory addresses and function names, file names, and line numbers, and is generated during the packaging phase of the program installation package. Since the second-layer stack frame in the call stack of the main thread corresponds to the main function during the running of the program and belongs to a certain known condition, the pre-recorded initial start address of the main function can be obtained from the symbol table file of the application program main_low_pc, the initial end address main_high_pc, and the virtual offset value vm of the code segment text section corresponding to the current application are used as factors involved in predicting the offset address.
  • each target program counter address is represented by pc:
  • the maximum endpoint value of the offset address range can be expressed as:
  • the minimum endpoint value of the offset address range can be expressed as:
  • offset_min pc-main_high_pc+vm.
  • the number of target program counter addresses is the same as the number of offset address ranges.
  • S104 based on a preset policy and at least one offset address range, determine a target offset address corresponding to a thread snapshot in a preset running cycle; wherein the preset policy is used to define a rule for determining the target offset address.
  • the preset policy may include: the target offset address is in the offset address range and is an integer multiple of the Nth power of 10, for example, the offset address is an integer multiple of 1000, etc. Therefore, when multiple offsets are obtained After the address interval, it can be determined whether there is an address value conforming to the prediction strategy in each offset address interval, and if so, it can be used as the target offset address. It should be noted here that a plurality of offset address ranges can be calculated, but usually the number of address values that conform to the preset policy in this range is limited. Therefore, this solution can obtain relatively accurate target offset addresses.
  • At least two address values conforming to the preset policy are determined based on the preset policy and at least one offset address range, then at least two address values may be determined from the at least two address values according to the proportion of the occurrences of the target program counter addresses participating in the determination of the offset address range.
  • a target offset address is determined from the address value that conforms to the preset policy. Specifically, when the proportion of the occurrences of the target program counter addresses participating in the determination of the offset address interval exceeds the first proportion threshold (the value can be flexibly determined), the address value in the offset address interval conforms to the preset policy, as the target offset address.
  • the address value d1 in the offset address range 1 that conforms to the preset policy determines the address value d1 in the offset address range 1 that conforms to the preset policy;
  • the address value d2 of the preset strategy because the number of occurrences of the target program counter address A participating in determining the offset address range 1 is x1, which is less than the first ratio threshold, but participating in determining the target program counter address in the offset address range 2
  • the proportion of occurrences of B is x2, which is greater than the first proportion threshold, and the address value d2 in the offset address range 2 that conforms to the preset policy may be determined as the target offset address.
  • the proportion of occurrences of the target program counter address A involved in determining the offset address range 1 and the proportion of occurrences of the target program counter address B participating in the determination of the offset address range 2 are both greater than the first proportion.
  • the address value with the smallest value among the address value d1 and the address value d2 can be used as the target offset address.
  • the main function is an entry function in the program running process
  • the real address of each function in each thread ie, the function address defined in the program development process
  • the real address of each function in each thread ie, the function address defined in the program development process
  • each thread snapshot has a corresponding timestamp.
  • the first time in the function stack is the start time of the function, and the last time The moment of one occurrence is the end moment of the function.
  • a flame graph can be drawn to analyze the running performance of the application.
  • the flame graph is the display form of the function call relationship and time distribution.
  • the vertical direction represents the depth of the call stack, and the horizontal direction represents the consumed time.
  • the larger the width of a grid on the flame graph the function corresponding to the grid may be the program running. The bottleneck in the process, therefore, the flame graph is the current tool for profiling applications.
  • At least one target program counter address is determined, and then combined with the symbol of the application program.
  • the information recorded in the table file predicts the target offset address allocated to the function during the running of the program, and then performs symbolic analysis on the thread snapshot in the preset running cycle based on the target offset address.
  • the real address of the function during the program running process can be obtained by shifting the address, and the description information such as the function name can be determined based on the real address of the function, that is, the embodiment of the present disclosure realizes the accurate acquisition of the relevant program information during the running process of the application program, which is helpful for The accurate monitoring and analysis of the running performance of the application program is realized, and the problem that the relevant program information in the running process of the application program cannot be accurately obtained in the existing solution, resulting in the inability to accurately monitor and analyze the running performance of the application program.
  • this scheme predicts the offset address of the function during the running of the program, and can realize the symbolic analysis of the thread snapshot based on the offset address, without debugging the application program, which has the advantages of convenience and efficiency, and also solves the problem.
  • the problem that the performance of the program cannot be analyzed due to the inability to debug the program is solved.
  • determining the target offset address corresponding to the thread snapshot in the preset running cycle including:
  • the target integer align corresponding to each offset address interval Based on the maximum endpoint value offset_max and the minimum endpoint value offset_min of each offset address interval, determine the target integer align corresponding to each offset address interval, and this process can be referred to as performing downward alignment processing on the maximum endpoint value and the minimum endpoint value;
  • the first multiple value can be expressed as:
  • the candidate offset address can be expressed as:
  • offset (offset_min/align+1) ⁇ align
  • each candidate offset address offset and the corresponding offset address range, and the proportion of occurrences of each target program counter address, from at least one candidate offset address determine the thread in the preset running cycle The target offset address offset' corresponding to the snapshot; wherein, the proportion of occurrence times is obtained based on the ratio of the total number of occurrences of each target program counter address in the corresponding thread to the sum of the total number of thread snapshots in each thread in the preset running cycle.
  • the target offset address corresponding to the thread snapshot including:
  • the candidate offset address is determined as the target offset address, that is, the effective predicted offset address, otherwise, it is not used as the target offset address.
  • the candidate offset address is in the corresponding offset address range, and the number of occurrences of the target program counter address participating in the determination of the offset address range exceeds the first ratio threshold, the number of candidate offset addresses. If there are at least two, the minimum value of the at least two candidate offset addresses is determined as the target offset address.
  • the method provided by the embodiment of the present disclosure may further include:
  • the location parameter lib_size corresponding to the application is determined, which can be expressed as follows:
  • the address of the program counter to be resolved is symbolically resolved.
  • the addresses in each thread snapshot that are less than or equal to the target offset address offset' and greater than or equal to the sum of the target offset address and positional parameters (ie offset'+lib_size) belong to the address of the operating system calling function, and do not need to be based on the target
  • the offset address can be parsed symbolically, and can be parsed by the preset parsing method corresponding to the operating system.
  • the command line tool atos can be used to parse the addresses belonging to the functions called by the operating system in each thread snapshot.
  • the embodiment of the present disclosure determines the address of the program counter to be parsed by calculating the location parameters corresponding to the application program, and filtering the addresses in the snapshots of each thread during the program running process based on the target offset address and the location parameters, thereby improving the performance of symbolic parsing. efficiency.
  • FIG. 2 is a flowchart of another thread snapshot parsing method provided by an embodiment of the present disclosure, which is used to illustrate the embodiment of the present disclosure, but should not be construed as a specific limitation to the embodiment of the present disclosure.
  • the thread snapshots of each thread can be obtained according to the preset running cycle, and it is determined whether the offset address corresponding to the thread snapshot is obtained. Offset address, perform symbolic analysis on the thread snapshot in the current running cycle. If the offset address cannot be obtained, you can split the obtained thread snapshot according to the thread ID; from the thread snapshot of each thread, obtain The program counter address corresponding to the second layer stack frame in the call stack of the thread, and, for each thread, the same number of program counter addresses are obtained;
  • the current program counter address can be used as the target program counter address to obtain the symbol table (dsym) file of the application program, and obtain the initial start address main_low_pc and initial end address main_high_pc of the pre-recorded main function from it, and the corresponding application program
  • the maximum endpoint value of the offset address range can be expressed as:
  • offset_max pc-main_low+pc+vm
  • the minimum endpoint value of the offset address range can be expressed as:
  • offset_min pc-main_high_pc+vm
  • lib_size addr-vm+size
  • offset (offset_min/align+1) ⁇ align
  • Shift address offset' for example, if the proportion of occurrences of the target program counter address participating in the determination of the offset address range exceeds the first ratio threshold, and there is only one offset address range that currently satisfies this condition, that is, the eligible candidate offset If there is only one shift address, the candidate offset address is used as the target offset address offset';
  • FIG. 3 is a flowchart of another thread snapshot parsing method provided by an embodiment of the present disclosure, which is further optimized and extended based on the foregoing technical solution, and may be combined with the foregoing optional implementation manners.
  • the thread snapshot analysis method may include:
  • an application program that needs to perform performance analysis may be installed on a device to be monitored, such as a mobile terminal, a tablet computer, and the like.
  • the number of devices to be monitored can be one or more.
  • the current device to be monitored may be determined according to the device identifier (used to uniquely identify the device).
  • the application identifier is used to uniquely distinguish different applications. Therefore, the application to be monitored in the device to be monitored can be determined according to the application identifier, so as to avoid errors in the monitoring object.
  • the electronic device for implementing the technical solutions of the embodiments of the present disclosure can establish a communication connection with the device to be monitored by using a communication establishment method that can realize data transmission between devices, so as to establish a communication connection with the device to be monitored.
  • the thread snapshot of each thread and the thread ID of each thread are obtained according to the preset running cycle.
  • the thread ID is used to uniquely distinguish different threads.
  • the libimobiledevice technology can be used to establish communication with the device to be monitored.
  • libimobiledevice is a cross-platform software protocol library and tool, which can be used to realize local communication between an electronic device that is not an iOS operating system and an iOS device (ie, an electronic device that uses the iOS operating system as the operating system).
  • the devices to be monitored may include a device to be monitored 1, a device to be monitored 2, and a device to be monitored 3.
  • the current device to be monitored can be determined according to the device identifier, and then the device to be monitored can be determined according to the application identifier.
  • the application program, in which the device identifier and the application program identifier can be passed as configuration information (Config information) in the specified data channel, so that the thread snapshot data during the running process of the required application can be accurately obtained through the specified data channel. .
  • the collection of thread snapshots will be affected when the current device to be monitored is in the locked screen state. If it is determined that the current device to be monitored is in the locked screen state, the thread snapshots of each thread during the running process of the application cannot be collected, and the collection process of the current thread snapshot ends; if it is determined that the current device to be monitored is in the unlocked state, then Start the mount service in the device to be monitored (start server "mobile_image_mounter"), and determine whether to mount the Developer Image file (referring to the image file of the collection of all data collection services in the device to be monitored. The device that mounts this image file can use each data collection services).
  • Instruments Server background service which is a background service for iOS devices to collect kernel and performance data
  • the Developer Image file of the iOS device needs to be mounted. Therefore, it is necessary to determine whether the Developer Image file has been mounted. If there is no mounting, you need to download the Developer Image file corresponding to the device version and mount the device, then start the Instruments Server background service (start server "instruments.remoteserver") in the device to be monitored, and use the socket packaged based on libimobiledevice technology.
  • the interface communicates and requests thread snapshots of each thread during the running process of the application for the specified data channel (such as the activitytracetap channel).
  • threadInfo.pid that is, the process identifier or Process ID
  • threadInfo.tid that is, thread identifier or thread ID
  • each thread may include at least one call stack.
  • the acquired thread snapshots are split according to the thread ID, so as to realize the classification of the acquired thread snapshots, and further, according to the thread snapshots in each thread, a qualified target program counter address can be determined. For example, in each thread, the program counter address whose frequency of occurrence is greater than or equal to the second proportion threshold and only appears in one thread may be determined as the target program counter address.
  • S305 in the preset running cycle of the application, determine at least one target program counter address based on the program counter address corresponding to the second layer stack frame in the call stack of each thread; wherein, the program counter address is used to identify the program running process The address of the function instruction being executed.
  • S306 Obtain the symbol table file of the application program, and determine the initial start address, initial end address of the main function recorded in the symbol table file, and the virtual offset value of the code segment corresponding to the application program.
  • S308 based on a preset policy and at least one offset address range, determine a target offset address corresponding to a thread snapshot in a preset running cycle; wherein the preset policy is used to define a rule for determining the target offset address.
  • the symbolic analysis result includes at least one of a function name, a file name where the function is located, and a line number corresponding to the function in the file.
  • S310 Determine the name of the parsing function corresponding to the stack frame of the second layer in the call stack of each thread after the symbolic parsing.
  • S311 Determine whether there is a function name of the main function in at least one analytical function name, and whether the function name only appears in one thread, and verify the target offset address according to the determination result.
  • the thread in the preset running cycle is determined based on the target offset address.
  • the function name of the main function must exist in at least one parsing function name, and it only appears in one thread, which is the main thread.
  • the target offset address verification is passed, and the target offset address belongs to the real offset address, and then based on the target offset address
  • the symbolic parsing result obtained from the offset address is also reliable; if it is determined that the function name of the main function does not exist in at least one parsing function name, or exists, but the function name appears in at least two threads, the target offset address verification If it does not pass, the target offset address does not belong to the real offset address, and the symbolic analysis result obtained based on the target offset address is also unreliable. Estimate until the exact target offset address is obtained.
  • the method provided by the embodiment of the present disclosure further includes: determining whether there is a name in at least one parsing function.
  • the function name of the program startup function DidFinishLaunch so that the determination result participates in the verification of the target offset address.
  • the program startup function is a function that must be executed in the startup phase of the application. Therefore, if the preset running cycle is in the startup phase of the application, and the function name of the program startup function DidFinishLaunch exists in at least one parsing function name, the target bias can be determined. The verification of the mobile address is passed, otherwise the verification fails.
  • the startup phase of the application it can also be determined whether at least one parsing function name has the function name of the program startup function DidFinishLaunch and the function name of the main function. If both function names exist, the target offset value is verified. , if there is only the function name of the program startup function DidFinishLaunch or the function name of the main function, it can be considered that the target offset address verification fails. By setting the double function name check, the prediction accuracy of the target offset address can be further improved.
  • the thread snapshot parsing method provided by the embodiment of the present disclosure may further include: monitoring the running performance of the application program based on the symbolic parsing result of the thread snapshot in the preset running cycle;
  • the analysis result includes at least one of the function name, the file name where the function is located, and the line number corresponding to the function in the file.
  • a flame graph can be drawn according to the running time period of each function to analyze the running performance of the application.
  • the target offset address is obtained by aggregating and filtering thread snapshots of each thread in the preset running cycle of the application, and predicting the offset address based on the offset address prediction method provided by this solution. , and then perform symbolic parsing of each thread snapshot based on the target offset address, and verify the target offset address according to the symbolic parsing result, that is, to disprove whether the prediction result of the offset address is accurate, until the target offset with the highest confidence is filtered out. Shift address (that is, the estimated value of the offset address with the highest confidence), and perform symbolic parsing of all thread snapshots as the final real offset address again.
  • the symbolic parsing result at this time can be used as a valid parsing result, using In the monitoring and analysis of the running performance of the application, the accuracy of the monitoring and analysis of the running performance of the application is ensured. Furthermore, with the technical solutions of the embodiments of the present disclosure, it is possible to monitor and analyze the running performance of the application without debugging the source code of the application, thereby improving the efficiency of monitoring and analyzing the running performance of the application.
  • FIG. 5 is a schematic structural diagram of a thread snapshot parsing apparatus provided by an embodiment of the present disclosure.
  • the apparatus can be implemented in software and/or hardware, and can be integrated on an electronic device with computing capabilities, such as a terminal or a server.
  • the thread snapshot parsing apparatus 500 may include a target program counter address determination module 501, a symbol table file acquisition module 502, an offset address range determination module 503, a target offset address determination module 504 and Thread snapshot parsing module 505, wherein:
  • the target program counter address determination module 501 is used to determine at least one target program counter address based on the program counter address corresponding to the second layer stack frame in the call stack of each thread in the preset running cycle of the application; wherein, the program counter The address is used to identify the address of the function instruction being executed during the running of the program;
  • the symbol table file obtaining module 502 is used to obtain the symbol table file of the application program, and determine the initial start address, the initial end address of the main function recorded in the symbol table file, and the virtual offset value of the code segment corresponding to the application program;
  • the offset address interval determination module 503 is configured to determine at least one offset address corresponding to the thread snapshot in the preset running cycle based on the at least one target program counter address, the initial start address, the initial end address, and the virtual offset value interval;
  • the target offset address determination module 504 is used to determine the target offset address corresponding to the thread snapshot in the preset operation cycle based on the preset policy and at least one offset address interval; wherein, the preset policy is used to define and determine the target offset address. relocation rules;
  • the thread snapshot parsing module 505 is configured to perform symbolic parsing of thread snapshots in a preset running cycle based on the target offset address.
  • the target offset address determination module 504 includes:
  • the first determination unit is used to determine the target integer corresponding to each offset address interval based on the maximum endpoint value and the minimum endpoint value of each offset address interval; wherein, the target integer is 10 to the Nth power;
  • the second determination unit is used to round down the quotient value between the minimum endpoint value of each offset address interval and the target integer to obtain the first multiple value corresponding to each offset address interval;
  • the third determination unit is used to add 1 to the first multiple value corresponding to each offset address interval to obtain a second multiple value, and based on the product of the second multiple value and the target integer corresponding to each offset address interval, obtain at least one candidate offset address;
  • the fourth determination unit is used to determine and predict from at least one candidate offset address based on the numerical relationship between each candidate offset address and the corresponding offset address interval, and the proportion of occurrences of each target program counter address. Set the target offset address corresponding to the thread snapshot in the running cycle;
  • the proportion of occurrences is obtained based on the ratio of the total number of occurrences of each target program counter address in the corresponding thread in the preset running cycle to the sum of the total number of thread snapshots in each thread.
  • the fourth determining unit is specifically used for:
  • the candidate offset address is in the corresponding offset address range, and the proportion of occurrences of the target program counter address participating in determining the offset address range exceeds the first ratio threshold, the candidate offset address is determined as the target offset address.
  • the candidate offset address is in the corresponding offset address range, and the number of occurrences of the target program counter address participating in determining the offset address range exceeds the first ratio threshold, the candidate offset address is at least two, then the minimum value of the at least two candidate offset addresses is determined as the target offset address.
  • the thread snapshot parsing apparatus 500 further includes:
  • the module for determining the load address and space occupation size is used to determine the load address of the code segment recorded in the symbol table file and the space occupation size of the code segment;
  • the location parameter determination module is used to determine the location parameter corresponding to the application based on the load address, the space occupied by the code segment and the virtual offset value;
  • the thread snapshot parsing module 505 includes:
  • the program counter address determination unit to be resolved is used to determine the address of the program counter to be resolved that is greater than the target offset address and less than the sum of the target offset address and the position parameter in each thread snapshot in the preset running cycle;
  • the address parsing unit is used to perform symbolic parsing of the address of the program counter to be resolved based on the target offset address.
  • the target program counter address determination module 501 includes:
  • the number of occurrences determining unit is used to determine, in the preset running cycle of the application, the program counter address corresponding to the second layer stack frame in the call stack of each thread, and the total number of occurrences in the thread;
  • Occurrence ratio determination unit used to determine the number of occurrences of each program counter address in each thread based on the total number of occurrences of each program counter address in each thread and the sum of the total number of thread snapshots in each thread proportion;
  • the target program counter address determination unit is configured to determine, as at least one target program counter address, a program counter address whose proportion of occurrence times is greater than or equal to the second proportion threshold and only occurs in one thread.
  • the thread snapshot parsing apparatus 500 further includes:
  • the first function name determination module is used to determine the parsing function name corresponding to the second layer stack frame in the call stack of each thread after symbolic parsing;
  • the offset address verification module is used to determine whether there is a function name of the main function in at least one parsing function name, and whether the function name only appears in one thread, and verify the target offset address according to the determination result.
  • the thread snapshot parsing apparatus 500 provided by the embodiment of the present disclosure further includes:
  • the second function name determination module determines whether there is a function name of the program startup function DidFinishLaunch in at least one parsing function name, so that the determination result participates in the verification of the target offset address.
  • the thread snapshot parsing apparatus 500 further includes:
  • the program monitoring module is used to monitor the running performance of the application based on the symbolic analysis results of thread snapshots in the preset running cycle; wherein, the symbolic analysis results include the function name, the file name where the function is located, and the corresponding function in the file. At least one of the line numbers.
  • the thread snapshot parsing apparatus 500 further includes:
  • the device to be monitored determination module is used to determine the device to be monitored according to the device identification
  • the application program determination module to be monitored is used to determine the application program to be monitored in the device to be monitored according to the application program identifier;
  • the thread snapshot and thread ID determination module is used to establish communication with the device to be monitored, and obtain the thread snapshot of each thread in the preset running cycle of the application, and the thread ID of each thread;
  • the program counter address determination module is used to split the acquired thread snapshot based on the thread ID, and determine the program counter address corresponding to the second layer stack frame in the call stack of each thread from the thread snapshot of each thread.
  • the thread snapshot parsing apparatus provided by the embodiment of the present disclosure can execute any thread snapshot parsing method provided by the embodiment of the present disclosure, and has functional modules and beneficial effects corresponding to the execution method.
  • any thread snapshot parsing method provided by the embodiment of the present disclosure, and has functional modules and beneficial effects corresponding to the execution method.
  • FIG. 6 is a schematic structural diagram of an electronic device provided by an embodiment of the present disclosure, which is used to exemplarily illustrate an electronic device implementing the thread snapshot analysis method provided by the embodiment of the present disclosure.
  • the electronic devices in the embodiments of the present disclosure may include, but are not limited to, such as mobile phones, notebook computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablets), PMPs (portable multimedia players), vehicle-mounted terminals (eg, Mobile terminals such as car navigation terminals), etc., and stationary terminals such as digital TVs, desktop computers, smart home devices, wearable electronic devices, servers, and the like.
  • the electronic device shown in FIG. 6 is only an example, and should not impose any limitation on the functions and occupancy scope of the embodiments of the present disclosure.
  • electronic device 600 includes one or more processors 601 and memory 602 .
  • Processor 601 may be a central processing unit (CPU) or other form of processing unit having data processing capabilities and/or instruction execution capabilities, and may control other components in electronic device 600 to perform desired functions.
  • CPU central processing unit
  • Memory 602 may include one or more computer program products, which may include various forms of computer-readable storage media, such as volatile memory and/or non-volatile memory.
  • Volatile memory may include, for example, random access memory (RAM) and/or cache memory, among others.
  • Non-volatile memory may include, for example, read only memory (ROM), hard disk, flash memory, and the like.
  • One or more computer program instructions may be stored on a computer-readable storage medium, and the processor 601 may execute the program instructions to implement the thread snapshot parsing method provided by the embodiments of the present disclosure, and may also implement other desired functions.
  • Various contents such as input signals, signal components, noise components, etc. may also be stored in the computer-readable storage medium.
  • the thread snapshot analysis method may include: in a preset running cycle of an application program, determining at least one target program counter address based on the program counter address corresponding to the second layer stack frame in the call stack of each thread ; Among them, the program counter address is used to identify the address of the function instruction being executed during the running process of the program; obtain the symbol table file of the application program, and determine the initial start address, initial end address, and application of the main function recorded in the symbol table file.
  • the virtual offset value of the code segment corresponding to the program based on at least one target program counter address, initial start address, initial end address, and virtual offset value, determine at least one offset corresponding to the thread snapshot in the preset running cycle address range; based on the preset policy and at least one offset address range, determine the target offset address corresponding to the thread snapshot in the preset running cycle; wherein, the preset policy is used to define the rules for determining the target offset address; based on the target Offset address for symbolic parsing of thread snapshots in the preset running cycle. It should be understood that the electronic device 600 may also perform other optional implementations provided by the method embodiments of the present disclosure.
  • the electronic device 600 may also include an input device 603 and an output device 604 interconnected by a bus system and/or other form of connection mechanism (not shown).
  • the input device 603 may also include, for example, a keyboard, a mouse, and the like.
  • the output device 604 can output various information to the outside, including the determined distance information, direction information, and the like.
  • the output device 604 may include, for example, displays, speakers, printers, and communication networks and their connected remote output devices, among others.
  • the electronic device 600 may also include any other suitable components according to the specific application.
  • the embodiments of the present disclosure also provide a computer program product, which includes a computer program or computer program instructions, and the computer program or computer program instructions, when executed by a computing device, cause the computing device to implement what is provided by the embodiments of the present disclosure.
  • Arbitrary thread snapshot parsing method
  • the computer program product may write program code for performing operations of embodiments of the present disclosure in any combination of one or more programming languages, including object-oriented programming languages, such as Java, C++, etc., as well as conventional procedural programming language, such as "C" language or similar programming language.
  • the program code may execute entirely on the user electronic device, partly on the user electronic device, as a stand-alone software package, partly on the user electronic device and partly on the remote electronic device, or entirely on the remote electronic device execute on.
  • embodiments of the present disclosure may further provide a computer-readable storage medium on which computer program instructions are stored, and when executed by the computing device, the computer program instructions enable the computing device to implement the arbitrary thread snapshot parsing method provided by the embodiments of the present disclosure .
  • the thread snapshot analysis method may include: in a preset running cycle of an application program, determining at least one target program counter address based on the program counter address corresponding to the second layer stack frame in the call stack of each thread ; Among them, the program counter address is used to identify the address of the function instruction being executed during the running process of the program; obtain the symbol table file of the application program, and determine the initial start address, initial end address, and application of the main function recorded in the symbol table file.
  • the virtual offset value of the code segment corresponding to the program based on at least one target program counter address, initial start address, initial end address, and virtual offset value, determine at least one offset corresponding to the thread snapshot in the preset running cycle address range; based on the preset policy and at least one offset address range, determine the target offset address corresponding to the thread snapshot in the preset running cycle; wherein, the preset policy is used to define the rules for determining the target offset address; based on the target Offset address for symbolic parsing of thread snapshots in the preset running cycle. It should be understood that when the computer program instructions are executed by the computing device, the computing device can also implement other optional implementations provided by the method embodiments of the present disclosure.
  • a computer-readable storage medium can employ any combination of one or more readable media.
  • the readable medium may be a readable signal medium or a readable storage medium.
  • the readable storage medium may include, for example, but not limited to, electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatuses or devices, or a combination of any of the above. More specific examples (non-exhaustive list) of readable storage media include: electrical connections with one or more wires, portable disks, hard disks, random access memory (RAM), read only memory (ROM), erasable programmable read only memory (EPROM or flash memory), optical fiber, portable compact disk read only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • Quality & Reliability (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Computing Systems (AREA)
  • Mathematical Physics (AREA)
  • Debugging And Monitoring (AREA)

Abstract

提供一种线程快照解析方法、装置、设备和存储介质,该方法包括:在应用程序的预设运行周期中,基于各个线程的调用栈中第二层栈帧对应的程序计数器地址,确定至少一个目标程序计数器地址;获取应用程序的符号表文件,并确定符号表文件中记录的主函数的初始起始地址、初始结束地址、以及应用程序对应的代码段的虚拟偏移值;基于前述各信息确定至少一个偏移地址区间;基于预设策略和至少一个偏移地址区间,确定与预设运行周期中的线程快照对应的目标偏移地址;基于目标偏移地址,对预设运行周期中的线程快照进行符号化解析。

Description

线程快照解析方法、装置、设备和存储介质
相关申请的交叉引用
本申请要求于2021年3月24日提交的,申请名称为“线程快照解析方法、装置、设备和存储介质”的、中国专利申请号为“202110315442.5”的优先权,该中国专利申请的全部内容通过引用结合在本申请中。
技术领域
本公开涉及计算机技术领域,尤其涉及一种线程快照解析方法、装置、设备和存储介质。
背景技术
随着软件开发技术的发展,可安装在用户终端上的应用程序层出不穷,例如新闻类应用程序、教育类应用程序、或者娱乐类应用程序等,极大地改善了人们的生活方式。
软件开发商对已发布版本的应用程序进行运行性能的监控,有助于及时发现程序运行过程中的异常状态,从而及时对应用程序进行改进,保证用户对应用程序的使用体验。
然而,由于用户终端的差异性,无法保证准确获取应用程序运行过程中的相关程序信息,进而导致无法准确分析应用程序的运行性能。
技术解决方案
为了解决上述技术问题或者至少部分地解决上述技术问题,本公开实施例提供了一种线程快照解析方法、装置、设备和存储介质。
第一方面,本公开实施例提供了一种线程快照解析方法,包括:
在应用程序的预设运行周期中,基于各个线程的调用栈中第二层栈帧对应的程序计数器地址,确定至少一个目标程序计数器地址;其中,所述程序计数器地址用于标识程序运行过程中正在执行的函数指令地址;
获取所述应用程序的符号表文件,并确定所述符号表文件中记录的主函数的初始起始地址、初始结束地址、以及所述应用程序对应的代码段的虚拟偏移 值;
基于所述至少一个目标程序计数器地址、所述初始起始地址、所述初始结束地址、以及所述虚拟偏移值,确定与所述预设运行周期中的线程快照对应的至少一个偏移地址区间;
基于预设策略和所述至少一个偏移地址区间,确定与所述预设运行周期中的线程快照对应的目标偏移地址;其中,所述预设策略用于定义确定所述目标偏移地址的规则;
基于所述目标偏移地址,对所述预设运行周期中的线程快照进行符号化解析。
第二方面,本公开实施例还提供了一种线程快照解析装置,包括:
目标程序计数器地址确定模块,用于在应用程序的预设运行周期中,基于各个线程的调用栈中第二层栈帧对应的程序计数器地址,确定至少一个目标程序计数器地址;其中,所述程序计数器地址用于标识程序运行过程中正在执行的函数指令地址;
符号表文件获取模块,用于获取所述应用程序的符号表文件,并确定所述符号表文件中记录的主函数的初始起始地址、初始结束地址、以及所述应用程序对应的代码段的虚拟偏移值;
偏移地址区间确定模块,用于基于所述至少一个目标程序计数器地址、所述初始起始地址、所述初始结束地址、以及所述虚拟偏移值,确定与所述预设运行周期中的线程快照对应的至少一个偏移地址区间;
目标偏移地址确定模块,用于基于预设策略和所述至少一个偏移地址区间,确定与所述预设运行周期中的线程快照对应的目标偏移地址;其中,所述预设策略用于定义确定所述目标偏移地址的规则;
线程快照解析模块,用于基于所述目标偏移地址,对所述预设运行周期中的线程快照进行符号化解析。
第三方面,本公开实施例还提供了一种电子设备,包括存储器和处理器,其中,所述存储器中存储有计算机程序,当所述计算机程序被所述处理器执行时,使得所述电子设备实现本公开实施例提供的任一所述的线程快照解析方法。
第四方面,本公开实施例还提供了一种计算机可读存储介质,所述存储介质中存储有计算机程序,当所述计算机程序被计算设备执行时,使得所述计算设备实现本公开实施例提供的任一所述的线程快照解析方法。
本公开实施例提供的技术方案与现有技术相比至少具有如下优点:在本公开实施例中,通过在应用程序的预设运行周期内,基于各个线程的调用栈中第二层栈帧对应的程序计数器地址(或称为PC地址),确定至少一个目标程序计数器地址,然后结合应用程序的符号表文件中记录的信息,预测程序运行过程中为函数分配的目标偏移地址,进而基于该目标偏移地址,对预设运行周期中的线程快照进行符号化解析,具体而言,基于目标偏移地址可以获取程序运行过程中函数的真实地址,基于函数的真实地址可以确定函数名等描述信息,也即本公开实施例实现了准确获取应用程序运行过程中的相关程序信息,进而有助于实现对应用程序的运行性能的准确监控与分析,解决了现有方案中无法准确获取应用程序运行过程中的相关程序信息,导致无法准确地监控与分析应用程序的运行性能的问题。并且,采用本公开实施例的技术方案,无需通过调试应用程序的源代码,即可实现对应用程序运行性能的监控和分析,提高了程序运行性能监控和分析的效率。
附图说明
此处的附图被并入说明书中并构成本说明书的一部分,示出了符合本公开的实施例,并与说明书一起用于解释本公开的原理。
为了更清楚地说明本公开实施例或现有技术中的技术方案,下面将对实施例或现有技术描述中所需要使用的附图作简单地介绍,显而易见地,对于本领域普通技术人员而言,在不付出创造性劳动性的前提下,还可以根据这些附图获得其他的附图。
图1为本公开实施例提供的一种线程快照解析方法的流程图;
图2为本公开实施例提供的另一种线程快照解析方法的流程图;
图3为本公开实施例提供的另一种线程快照解析方法的流程图;
图4为本公开实施例提供的一种与待监控设备建立通信的处理架构示意图;
图5为本公开实施例提供的一种线程快照解析装置的结构示意图;
图6为本公开实施例提供的一种电子设备的结构示意图。
具体实施方式
为了能够更清楚地理解本公开的上述目的、特征和优点,下面将对本公开的方案进行进一步描述。需要说明的是,在不冲突的情况下,本公开的实施例及实施例中的特征可以相互组合。
在下面的描述中阐述了很多具体细节以便于充分理解本公开,但本公开还可以采用其他不同于在此描述的方式来实施;显然,说明书中的实施例只是本公开的一部分实施例,而不是全部的实施例。
图1为本公开实施例提供的一种线程快照解析方法的流程图,该方法可以适用于如何对应用程序运行过程中的各个线程快照(指应用程序运行的特定时刻,代码调用栈的程序计数器地址,或称为PC地址)进行符号化解析,从而得到程序运行过程中各个函数的函数名等描述信息,以用于监控与分析该应用程序的运行性能。
其中,程序计数器(Program Counter,PC)地址(或称为PC地址)是指程序计数器中存储的指令地址,可以用于标识程序运行过程中正在执行的函数指令地址。具体而言,程序计数器是一个中央处理器(Central Processing Unit,CPU)中的寄存器,用于指示计算机在其程序序列中的位置,在一些微处理器中,程序计数器还可以被称为指令指针、指令地址寄存器,或者指令计数器等。
本公开实施例中提及的应用程序可以是安装在电子设备上的至少一种类型的应用程序,例如视频交互类应用程序等。电子设备可以是具有计算能力的至少一种类型的设备,例如终端或者服务器等。安装应用程序的电子设备中的操作系统例如可以iOS系统等。
本公开实施例提供的线程快照解析方法可以由线程快照解析装置执行,该装置可以采用软件和/或硬件实现,并可集成在用于执行本公开实施例技术方案的电子设备上。
如图1所示,本公开实施例提供的线程快照解析方法可以包括:
S101、在应用程序的预设运行周期中,基于各个线程的调用栈中第二层栈 帧对应的程序计数器地址,确定至少一个目标程序计数器地址;其中,程序计数器地址用于标识程序运行过程中正在执行的函数指令地址。
在应用程序运行过程中,主线程的调用栈中第二层栈帧用于存储主函数(main函数)的相关信息,即主线程的调用栈中第二层栈帧对应的一定是主函数。在程序运行过程中的主线程和子线程未提前确定的情况下,可以获取程序运行过程中各个线程的调用栈中第二层栈帧对应的程序计数器地址,并按照程序计数器地址筛选策略(例如在每个线程中程序计数器地址的出现次数,一个程序计数器地址是否出现在至少两个线程中等),从获取的多个程序计数器地址中确定出至少一个目标程序计数器地址,作为有效程序计数器地址,以用于预测程序运行过程中线程快照对应的偏移地址(即程序运行过程中函数的偏移地址)。程序运行过程中各个函数的偏移地址是相同的。
预设运行周期的时间长度可以根据需求进行设置,本公开实施例不作具体限定。通过按照预设运行周期,周期性地获取各个线程的调用栈中第二层栈帧对应的程序计数器地址,以用于预测程序运行过程中函数的偏移地址,可以确保偏移地址的预测准确性。
可选地,在应用程序的预设运行周期中,基于各个线程的调用栈中第二层栈帧对应的程序计数器地址,确定至少一个目标程序计数器地址,包括:
在应用程序的预设运行周期中,确定每个线程的调用栈中第二层栈帧对应的程序计数器地址,在该线程中的出现总次数;
基于每个线程中的每个程序计数器地址的出现总次数m,以及各个线程中的线程快照总数之和sum,确定每个线程中的每个程序计数器地址的出现次数占比p,例如p=(m/sum)×100%;
将出现次数占比大于或等于第二占比阈值、且只出现在一个线程中的程序计数器地址确定为至少一个目标程序计数器地址。其中,第二占比阈值的取值可以视情况而定,本公开实施例不作具体限定。
由于正常情况下,程序运行过程中只有主线程的调用栈中第二层栈帧对应主函数,并且主函数在主线程的各个调用栈中均会出现,因此,在获取的多个程序计数器地址中,只出现在一个线程中的程序计数器地址的置信度高于同时 在多个线程中出现的程序计数器地址的置信度,且出现次数占比大于或等于第二占比阈值的程序计数器地址的置信度,高于出现次数占比小于第二占比阈值的程序计数器地址的置信度。即将出现次数占比大于或等于第二占比阈值、且只出现在一个线程中的程序计数器地址确定为至少一个目标程序计数器地址,为后续正确预测偏移地址奠定了基础。
应用程序的预设运行周期中可以包括多个线程,具体以其中的线程1和线程2为例,对目标程序计数器地址的确定过程进行示例性说明,并且多个线程中线程快照总数之和为100,即多个线程中的程序计数器地址总数为100个,其中:
在线程1中:
程序计数器地址A的出现总次数为10次,出现次数占比为10%;
程序计数器地址B的出现总次数为1次,出现次数占比为1%;
程序计数器地址C的出现总次数为2次,出现次数占比为2%;
在线程2中:
程序计数器地址C的出现总次数为5次,出现次数占比为5%;
程序计数器地址D的出现总次数为8次,出现次数占比为8%;
当然,还包括其他线程中程序计数器地址的出现次数以及出现次数占比,在此不再给出具体示例。
假设第二占比阈值为1.5%,上述程序计数器地址B的出现次数占比为1%,小于1.5%,所以程序计数器地址B需要被过滤掉,程序计数器地址C由于在线程1和线程2中均出现,所以程序计数器地址C也需要被过滤掉,最终程序计数器地址A和程序计数器地址D可以作为目标程序计数器地址。
S102、获取应用程序的符号表文件,并确定符号表文件中记录的主函数的初始起始地址、初始结束地址、以及应用程序对应的代码段的虚拟偏移值。
每个应用程序均有各自对应的符号表(dsym)文件,符号表是一种内存地址与函数名、文件名、行号的映射表,并且在程序安装包打包阶段生成。由于程序运行过程中主线程的调用栈中第二层栈帧对应主函数,属于一个确定的已知条件,因此,可以从应用程序的符号表文件中获取预先记录的主函数的初始 起始地址main_low_pc、初始结束地址main_high_pc,以及当前应用程序对应的代码段text section的虚拟偏移值vm,作为参与预测偏移地址的因素。
S103、基于至少一个目标程序计数器地址、初始起始地址、初始结束地址、以及虚拟偏移值,确定与预设运行周期中的线程快照对应的至少一个偏移地址区间。
针对每个目标程序计数器地址,可以按照以下方式计算对应的偏移地址区间[offset_min,offset_max],计算过程中每个目标程序计数器地址采用pc表示:
偏移地址区间的最大端点值可以表示为:
offset_max=pc-main_low_pc+vm;
偏移地址区间的最小端点值可以表示为:
offset_min=pc-main_high_pc+vm。
目标程序计数器地址的数量与偏移地址区间的数量相同。
S104、基于预设策略和至少一个偏移地址区间,确定与预设运行周期中的线程快照对应的目标偏移地址;其中,预设策略用于定义确定目标偏移地址的规则。
示例性地,预设策略可以包括:目标偏移地址处于偏移地址区间,并且为10的N次方的整数倍,例如偏移地址是1000的整数倍等,因此,当得到多个偏移地址区间后,可以确定每个偏移地址区间中是否存在符合预测策略的地址值,如果存在,即可以作为目标偏移地址。此处需要说明的是,偏移地址区间可以计算得到多个,但通常该区间内符合预设策略的地址值的数量有限,因此,本方案可以得到较为准确的目标偏移地址。
如果基于预设策略和至少一个偏移地址区间,确定出至少两个符合预设策略的地址值,则可以按照参与确定偏移地址区间的目标程序计数器地址的出现次数占比,从至少两个符合预设策略的地址值中确定出一个目标偏移地址。具体地,优选将参与确定偏移地址区间的目标程序计数器地址的出现次数占比超过第一占比阈值(取值可以灵活确定)时,该偏移地址区间中符合预设策略的地址值,作为目标偏移地址。
例如,基于预设策略和偏移地址区间1,确定出偏移地址区间1中符合预设策略的地址值d1;基于预设策略和偏移地址区间2,确定出偏移地址区间2中符合预设策略的地址值d2;由于参与确定偏移地址区间1的目标程序计数器地址A的出现次数占比为x1,小于第一占比阈值,然而参与确定偏移地址区间2的目标程序计数器地址B的出现次数占比为x2,大于第一占比阈值,则可以将偏移地址区间2中符合预设策略的地址值d2确定为目标偏移地址。继续以该示例为例,如果参与确定偏移地址区间1的目标程序计数器地址A的出现次数占比、参与确定偏移地址区间2的目标程序计数器地址B的出现次数占比均大于第一占比阈值,则可以将地址值d1和地址值d2中,值最小的地址值作为目标偏移地址。
由于主函数是程序运行过程中的一个入口函数,偏移地址越小,基于程序运行过程主函数的程序计数器地址和偏移地址,得到的主函数的实际地址越大,即主函数的实际结束位置越靠后,准确性相对越高。
S105、基于目标偏移地址,对预设运行周期中的线程快照进行符号化解析。
具体地,基于目标偏移地址,以及应用程序的预设运行周期中各个线程的调用栈的程序计数器地址,可以还原得到各个线程中各个函数的真实地址(即程序开发过程中定义的函数地址),然后利用应用程序的符号表文件,基于各个函数的真实地址,可以得到各个函数的函数名、函数所在文件的文件名、函数在文件中对应的行号(代码行号)等描述信息,也即符号化解析结果,最后基于各个函数的描述信息,实现监控与分析该应用程序的运行性能。
此外,应用程序运行过程中,每个线程快照都有对应的时间戳,通过对线程快照进行自下到上的相同项合并,函数堆栈中第一次出现的时刻为该函数的开始时刻,最后一次出现的时刻为该函数的结束时刻。根据线程快照中的函数调用关系与各个函数的开始时刻、结束时刻,即可绘制出火焰图,用于分析应用程序的运行性能。具体而言,火焰图是函数调用关系与时间分布的展现形式,纵向表示调用栈的深度,横向表示消耗的时间,火焰图上一个格子的宽度越大,说明该格子对应的函数可能是程序运行过程中的瓶颈,因此,火焰图是当前对应用程序进行性能分析的利器。
在本公开实施例中,通过在应用程序的预设运行周期内,基于各个线程的调用栈中第二层栈帧对应的程序计数器地址,确定至少一个目标程序计数器地址,然后结合应用程序的符号表文件中记录的信息,预测程序运行过程中为函数分配的目标偏移地址,进而基于该目标偏移地址,对预设运行周期中的线程快照进行符号化解析,具体而言,基于目标偏移地址可以获取程序运行过程中函数的真实地址,基于函数的真实地址可以确定函数名等描述信息,也即本公开实施例实现了准确获取应用程序运行过程中的相关程序信息,进而有助于实现对应用程序的运行性能的准确监控与分析,解决了现有方案中无法准确获取应用程序运行过程中的相关程序信息,导致无法准确地监控与分析应用程序的运行性能的问题。并且,本方案通过对程序运行过程中函数的偏移地址进行预测,基于偏移地址即可实现对线程快照的符号化解析,无需对应用程序进行调试,具有便捷、高效的优势,同时也解决了由于无法进行程序调试导致无法分析程序运行性能的问题。
在上述技术方案的基础上,可选地,基于预设策略和至少一个偏移地址区间,确定与预设运行周期中的线程快照对应的目标偏移地址,包括:
基于每个偏移地址区间的最大端点值offset_max和最小端点值offset_min,确定每个偏移地址区间对应的目标整数align,该过程可以称为对最大端点值和最小端点值进行向下对齐处理;其中,目标整数为10的N次方,例如N等于3,目标整数为1000,从程序编写的角度,目标整数可以表示为align=int(0x1000);
对每个偏移地址区间的最小端点值offset_min和目标整数align之间的商值向下取整,得到每个偏移地址区间对应的第一倍数值;
示例性地,从程序编写的角度,第一倍数值可以表示为:
offset_min/align;
将每个偏移地址区间对应的第一倍数值加1,得到第二倍数值,并基于第二倍数值和每个偏移地址区间对应的目标整数align的乘积,得到至少一个候选偏移地址offset;
示例性地,从程序编写的角度,候选偏移地址可以表示为:
offset=(offset_min/align+1)×align;
基于每个候选偏移地址offset和对应的偏移地址区间的数值关系、以及每个目标程序计数器地址的出现次数占比,从至少一个候选偏移地址中,确定与预设运行周期中的线程快照对应的目标偏移地址offset′;其中,出现次数占比基于预设运行周期中每个目标程序计数器地址在对应线程中的出现总次数与各个线程中的线程快照总数之和的比值得到。
进一步地,基于每个候选偏移地址和对应的偏移地址区间的数值关系、以及每个目标程序计数器地址的出现次数占比,从至少一个候选偏移地址中,确定与预设运行周期中的线程快照对应的目标偏移地址,包括:
如果候选偏移地址处于对应的偏移地址区间,且参与确定该偏移地址区间的目标程序计数器地址的出现次数占比超过第一占比阈值,则将该候选偏移地址确定为目标偏移地址,即有效的预测偏移地址,否则,不作为目标偏移地址。
并且,如果满足候选偏移地址处于对应的偏移地址区间,且参与确定该偏移地址区间的目标程序计数器地址的出现次数占比超过第一占比阈值的情况下,候选偏移地址的数量为至少两个,则将至少两个候选偏移地址中的最小值确定为目标偏移地址。
在上述技术方案的基础上,可选地,在基于目标偏移地址,对预设运行周期中的线程快照进行符号化解析之前,本公开实施例提供的方法还可以包括:
确定符号表文件中记录的代码段的加载地址addr(即代码段的首地址)和代码段的空间占用大小size;
基于加载地址addr、代码段的空间占用大小size和虚拟偏移值vm,确定应用程序对应的位置参数lib_size,具体可以表示如下:
lib_size=addr-vm+size;
相应地,基于目标偏移地址,对预设运行周期中的线程快照进行符号化解析,包括:
将预设运行周期中各个线程快照中大于目标偏移地址offset′、且小于目标偏移地址和位置参数之和(即offset′+lib_size)的地址,确定为待解析程序计数器地址;
基于目标偏移地址,对待解析程序计数器地址进行符号化解析。
其中,各个线程快照中小于或等于目标偏移地址offset′,以及大于或等于目标偏移地址和位置参数之和(即offset′+lib_size)的地址,属于操作系统调用函数的地址,无需基于目标偏移地址,进行符号化解析,可以通过操作系统对应的预设解析方法进行解析,例如针对iOS操作系统,可以采用命令行工具atos对各个线程快照中属于操作系统调用函数的地址进行解析。
本公开实施例通过计算应用程序对应的位置参数,并基于目标偏移地址和位置参数对程序运行过程中各个线程快照中的地址进行过滤,确定出待解析程序计数器地址,提高了符号化解析的效率。
图2为本公开实施例提供的另一种线程快照解析方法的流程图,用于对本公开实施例进行示例性说明,但不应理解为对本公开实施例的具体限定。如图2所示,在应用程序运行过程中,可以按照预设运行周期,获取各个线程的线程快照,并且确定是否获取到线程快照对应的偏移地址,如果获取到,则可以直接基于获取的偏移地址,对当前运行周期内的线程快照进行符号化解析,如果未能获取到偏移地址,则可以按照线程ID,对获取的线程快照进行拆分;从各个线程的线程快照中,获取线程的调用栈中第二层栈帧对应的程序计数器地址,并且,针对每个线程,获取相同数量的程序计数器地址;
遍历程序计数器地址,并确定程序计数器地址的出现次数占比(具体计算可以参考前述实施例中的解释)是否大于阈值(具体指代前述第二占比阈值),如果否,则继续遍历程序计数器地址,如果是,则确定程序计数器地址是否出现在其他线程中,即一个程序计数器地址是否同时在至少两个线程中出现;如果出现在其他线程,则继续遍历程序计数器地址,如果未出现在其他线程,则在当前程序计数器地址可以作为目标程序计数器地址,获取应用程序的符号表(dsym)文件,并从中获取预先记录的主函数的初始起始地址main_low_pc、初始结束地址main_high_pc,以及应用程序对应的代码段text section的虚拟偏移值vm、加载地址addr、以及空间占用大小size;利用从符号表(dsym)文件中获取的各个数据以及目标程序计数器地址(可以采用pc表示),计算偏移地址区间[offset_min,offset_max]、以及应用程序对应的位置参数lib_size, 其中:
偏移地址区间的最大端点值可以表示为:
offset_max=pc-main_low+pc+vm;
偏移地址区间的最小端点值可以表示为:
offset_min=pc-main_high_pc+vm;
位置参数lib_size可以表示为:lib_size=addr-vm+size;
对最大端点值offset_max和最小端点值offset_min进行向下对齐处理,确定每个偏移地址区间对应的目标整数align,目标整数align例如可以表示为align=int(0x1000);基于最小端点值offset_min和目标整数align,计算候选偏移地址offset,候选偏移地址offset可以表示为:
offset=(offset_min/align+1)×align;
确定候选偏移地址offset是否处于偏移地址区间中,如果否,则当前候选偏移地址不属于目标偏移地址,则继续遍历获取的程序计数器地址,执行目标程序计数器地址的确定操作以及候选偏移地址的计算操作,如果是,则表示当前候选偏移地址属于目标偏移地址的概率较大;进一步地,可以针对得到的各个处于偏移地址区间的候选偏移地址,从中确定出目标偏移地址offset′,例如如果参与确定偏移地址区间的目标程序计数器地址的出现次数占比超过第一占比阈值,且当前满足该条件的偏移地址区间只有一个,也即符合条件的候选偏移地址只有一个,则将该候选偏移地址作为目标偏移地址offset′;
确定出目标偏移地址offset′之后,再次遍历获取的各个线程快照,将大于目标偏移地址offset′、且小于目标偏移地址和位置参数之和(即offset′+lib_size)的地址,确定为待解析程序计数器地址,基于目标偏移地址offset′,对待解析程序计数器地址进行符号化解析,从而提高解析效率。
图3为本公开实施例提供的另一种线程快照解析方法的流程图,基于上述技术方案进一步进行优化与扩展,并可以与上述各个可选实施方式进行结合。
如图3所示,本公开实施例提供的线程快照解析方法可以包括:
S301、根据设备标识,确定待监控设备。
在本公开实施例中,需要进行运行性能分析的应用程序可以安装在待监控设备上,例如移动终端、平板电脑等。待监控设备的数量可以为一个或多个。在执行本公开实施例的技术方案的过程中,可以根据设备标识(用于唯一性标识设备),确定当前的待监控设备。
S302、根据应用程序标识,确定待监控设备中待监控的应用程序。
每个待监控设备中可以安装多种类型的应用程序。应用程序标识用于唯一性区分不同的应用程序,因此,可以根据应用程序标识确定待监控设备中待监控的应用程序,从而避免监控对象出错。
S303、与待监控设备建立通信,获取在应用程序的预设运行周期中各个线程的线程快照,以及各个线程的线程ID。
确定待监控设备以及待监控的应用程序后,用于执行本公开实施例技术方案的电子设备可以采用能够实现设备之间数据传输的通信建立方式,与待监控设备建立通信连接,从而在待监控的应用程序的运行过程中,按照预设运行周期,获取各个线程的线程快照,以及各个线程的线程ID。线程ID用于唯一性区分不同的线程。例如,针对iOS操作系统的待监控设备,可以利用libimobiledevice技术与待监控设备建立通信。其中,libimobiledevice是一个跨平台的软件协议库和工具,可以用来实现非iOS操作系统的电子设备与iOS设备(即以iOS操作系统作为操作系统的电子设备)之间的本地通信。
图4为本公开实施例提供的一种与待监控设备建立通信的处理架构示意图,具体以待监控设备的操作系统为iOS操作系统为例,对本公开实施例进行示例性说明,但不应理解为对本公开实施例的具体限定。如图4所示,待监控设备可以包括待监控设备1、待监控设备2和待监控设备3,可以根据设备标识确定当前待监控设备,然后根据应用程序标识,确定当前待监控设备中待监控的应用程序,其中,设备标识和应用程序标识可以作为配置信息(Config信息)在指定数据通道中传递,以使得通过该指定数据通道可以准确地获取到需求的应用程序运行过程中的线程快照数据。
利用libimobiledevice技术与当前待监控设备建立通信后,由于当前待监控 设备处于锁屏状态的情况下,会影响线程快照的采集,因此,在进行线程快照的采集之前,对当前待监控设备是否处于锁屏状态进行检测;如果确定当前待监控设备处于锁屏状态,则无法采集应用程序运行过程中各个线程的线程快照,当前线程快照的采集流程即结束;如果确定当前待监控设备处于解锁状态,则开启待监控设备中的挂载服务(start server“mobile_image_mounter”),确定是否挂载Developer Image文件(指待监控设备中所有数据采集服务的集合的镜像文件,挂载这个镜像文件的设备能使用各个数据采集服务)。
由于开启待监控设备中的Instruments Server后台服务(是iOS设备采集内核与性能数据的一种后台服务),需要挂载iOS设备的Developer Image文件,因此需要判断Developer Image文件是否已经挂载过,如果没有挂载,则需要执行对应设备版本的Developer Image文件的下载与设备挂载,随后开启待监控设备中的Instruments Server后台服务(start server“instruments.remoteserver”),通过基于libimobiledevice技术封装好的socket接口进行通信,对指定数据通道(如activitytracetap通道)请求应用程序运行过程中各个线程的线程快照。示例性地,在采集线程快照的过程中,可以基于数据采集服务中数据结构体的定义(例如数据采集服务kpdecode中所采集数据的结构体的定义),通过threadInfo.pid(即进程标识符或进程ID)与threadInfo.tid(即线程标识符或线程ID)区分线程快照所属的进程与线程,准确提取待监控的应用程序运行过程中各个线程的线程快照。
S304、基于线程ID对获取的线程快照进行拆分,并从每个线程的线程快照中,确定每个线程的调用栈中第二层栈帧对应的程序计数器地址。
在程序运行过程中,每个线程中均可以包括至少一个调用栈。按照线程ID对获取的线程快照进行拆分,可以实现对获取的线程快照的分类,进而可以针对每个线程中的线程快照,确定出符合条件的目标程序计数器地址。例如,可以将每个线程中,出现次数占比大于或等于第二占比阈值、且只出现在一个线程中的程序计数器地址确定为目标程序计数器地址。
S305、在应用程序的预设运行周期中,基于各个线程的调用栈中第二层栈帧对应的程序计数器地址,确定至少一个目标程序计数器地址;其中,程序计 数器地址用于标识程序运行过程中正在执行的函数指令地址。
S306、获取应用程序的符号表文件,并确定符号表文件中记录的主函数的初始起始地址、初始结束地址、以及应用程序对应的代码段的虚拟偏移值。
S307、基于至少一个目标程序计数器地址、初始起始地址、初始结束地址、以及虚拟偏移值,确定与预设运行周期中的线程快照对应的至少一个偏移地址区间。
S308、基于预设策略和至少一个偏移地址区间,确定与预设运行周期中的线程快照对应的目标偏移地址;其中,预设策略用于定义确定目标偏移地址的规则。
S309、基于目标偏移地址,对预设运行周期中的线程快照进行符号化解析。
其中,符号化解析结果中包括函数名、函数所在文件名、函数在文件中对应的行号中的至少一种。
S310、确定经过符号化解析后的各个线程的调用栈中第二层栈帧对应的解析函数名。
S311、确定至少一个解析函数名中是否存在主函数的函数名,以及该函数名是否只出现在一个线程中,并根据确定结果对目标偏移地址进行验证。
由于正常情况下,程序运行过程中只有主线程的调用栈中第二层栈帧对应主函数,如果目标偏移地址的预测是准确的,则基于目标偏移地址对预设运行周期中的线程快照进行符号化解析后,至少一个解析函数名中必然存在主函数的函数名,且只出现在一个线程中,该线程即主线程。因此,如果确定至少一个解析函数名中存在主函数的函数名,且该函数名只出现在一个线程中,则目标偏移地址验证通过,目标偏移地址属于真实的偏移地址,进而基于目标偏移地址得到的符号化解析结果也是可靠的;如果确定至少一个解析函数名中不存在主函数的函数名,或者存在,但是该函数名出现在至少两个线程中,则目标偏移地址验证不通过,目标偏移地址不属于真实的偏移地址,进而基于目标偏移地址得到的符号化解析结果也是不可靠的,此时,可以再次执行本公开实施例的技术方案,对偏移地址进行预估,直至得到准确的目标偏移地址。
可选地,如果在应用程序的启动过程中,执行对预设运行周期中的线程快 照进行符号化解析的操作,则本公开实施例提供的方法还包括:确定至少一个解析函数名中是否存在程序启动函数DidFinishLaunch的函数名,以使确定结果参与目标偏移地址的验证。程序启动函数在应用程序的启动阶段,属于必须执行的函数,因此,如果预设运行周期处于应用程序的启动阶段,至少一个解析函数名中存在程序启动函数DidFinishLaunch的函数名,则可以确定目标偏移地址验证通过,否则验证不通过。
当然,针对应用程序的启动阶段,也可以同时确定至少一个解析函数名中是否存在程序启动函数DidFinishLaunch的函数名以及主函数的函数名,如果两个函数名均存在,则目标偏移值验证通过,如果只存在程序启动函数DidFinishLaunch的函数名或者主函数的函数名,则可以认为目标偏移地址验证不通过。通过设置双重函数名的校验,可以进一步提高目标偏移地址的预测准确性。
在上述技术方案的基础上,进一步地,本公开实施例提供的线程快照解析方法还可以包括:基于预设运行周期中的线程快照的符号化解析结果,监控应用程序的运行性能;其中,符号化解析结果中包括函数名、函数所在文件名、函数在文件中对应的行号中的至少一种。例如,可以根据各个函数的运行时间周期,绘制火焰图,以实现对应用程序的运行性能的分析。
在本公开实施例中,通过对应用程序的预设运行周期中各个线程的线程快照进行聚合与过滤,并基于本方案提供的偏移地址预测方式对偏移地址进行预测,得到目标偏移地址,然后基于目标偏移地址对各个线程快照进行符号化解析,同时根据符号化解析结果对目标偏移地址进行验证,即反证偏移地址的预测结果是否准确,直至过滤出置信度最高的目标偏移地址(即置信度最高的偏移地址预估值),并作为最终的真实偏移地址对所有线程快照进行再次地符号化解析,此时的符号化解析结果可以作为有效的解析结果,用于应用程序的运行性能的监控与分析中,确保了应用程序运行性能的监控与分析的准确性。并且,采用本公开实施例的技术方案,无需通过调试应用程序的源代码,即可实现对应用程序运行性能的监控和分析,提高了程序运行性能监控和分析的效率。
图5为本公开实施例提供的一种线程快照解析装置的结构示意图,该装置 可以采用软件和/或硬件实现,并可集成在具有计算能力的电子设备上,例如终端或者服务器等。
如图5所示,本公开实施例提供的线程快照解析装置500可以包括目标程序计数器地址确定模块501、符号表文件获取模块502、偏移地址区间确定模块503、目标偏移地址确定模块504和线程快照解析模块505,其中:
目标程序计数器地址确定模块501,用于在应用程序的预设运行周期中,基于各个线程的调用栈中第二层栈帧对应的程序计数器地址,确定至少一个目标程序计数器地址;其中,程序计数器地址用于标识程序运行过程中正在执行的函数指令地址;
符号表文件获取模块502,用于获取应用程序的符号表文件,并确定符号表文件中记录的主函数的初始起始地址、初始结束地址、以及应用程序对应的代码段的虚拟偏移值;
偏移地址区间确定模块503,用于基于至少一个目标程序计数器地址、初始起始地址、初始结束地址、以及虚拟偏移值,确定与预设运行周期中的线程快照对应的至少一个偏移地址区间;
目标偏移地址确定模块504,用于基于预设策略和至少一个偏移地址区间,确定与预设运行周期中的线程快照对应的目标偏移地址;其中,预设策略用于定义确定目标偏移地址的规则;
线程快照解析模块505,用于基于目标偏移地址,对预设运行周期中的线程快照进行符号化解析。
可选地,目标偏移地址确定模块504包括:
第一确定单元,用于基于每个偏移地址区间的最大端点值和最小端点值,确定每个偏移地址区间对应的目标整数;其中,目标整数为10的N次方;
第二确定单元,用于对每个偏移地址区间的最小端点值和目标整数之间的商值向下取整,得到每个偏移地址区间对应的第一倍数值;
第三确定单元,用于将每个偏移地址区间对应的第一倍数值加1,得到第二倍数值,并基于第二倍数值和每个偏移地址区间对应的目标整数的乘积,得到至少一个候选偏移地址;
第四确定单元,用于基于每个候选偏移地址和对应的偏移地址区间的数值关系、以及每个目标程序计数器地址的出现次数占比,从至少一个候选偏移地址中,确定与预设运行周期中的线程快照对应的目标偏移地址;
其中,出现次数占比基于预设运行周期中每个目标程序计数器地址在对应线程中的出现总次数与各个线程中的线程快照总数之和的比值得到。
可选地,第四确定单元具体用于:
如果候选偏移地址处于对应的偏移地址区间,且参与确定该偏移地址区间的目标程序计数器地址的出现次数占比超过第一占比阈值,则将该候选偏移地址确定为目标偏移地址。
可选地,如果满足候选偏移地址处于对应的偏移地址区间,且参与确定该偏移地址区间的目标程序计数器地址的出现次数占比超过第一占比阈值的情况下,候选偏移地址的数量为至少两个,则将至少两个候选偏移地址中的最小值确定为目标偏移地址。
可选地,本公开实施例提供的线程快照解析装置500还包括:
加载地址和空间占用大小确定模块,用于确定符号表文件中记录的代码段的加载地址和代码段的空间占用大小;
位置参数确定模块,用于基于加载地址、代码段的空间占用大小和虚拟偏移值,确定应用程序对应的位置参数;
相应地,线程快照解析模块505包括:
待解析程序计数器地址确定单元,用于将预设运行周期中各个线程快照中大于目标偏移地址、且小于目标偏移地址和位置参数之和的地址,确定为待解析程序计数器地址;
地址解析单元,用于基于目标偏移地址,对待解析程序计数器地址进行符号化解析。
可选地,目标程序计数器地址确定模块501包括:
出现次数确定单元,用于在应用程序的预设运行周期中,确定每个线程的调用栈中第二层栈帧对应的程序计数器地址,在该线程中的出现总次数;
出现次数占比确定单元,用于基于每个线程中的每个程序计数器地址的出 现总次数,以及各个线程中的线程快照总数之和,确定每个线程中的每个程序计数器地址的出现次数占比;
目标程序计数器地址确定单元,用于将出现次数占比大于或等于第二占比阈值、且只出现在一个线程中的程序计数器地址确定为至少一个目标程序计数器地址。
可选地,本公开实施例提供的线程快照解析装置500还包括:
第一函数名确定模块,用于确定经过符号化解析后的各个线程的调用栈中第二层栈帧对应的解析函数名;
偏移地址验证模块,用于确定至少一个解析函数名中是否存在主函数的函数名,以及该函数名是否只出现在一个线程中,并根据确定结果对目标偏移地址进行验证。
可选地,如果在应用程序的启动过程中,执行对预设运行周期中的线程快照进行符号化解析的操作,则本公开实施例提供的线程快照解析装置500还包括:
第二函数名确定模块,确定至少一个解析函数名中是否存在程序启动函数DidFinishLaunch的函数名,以使确定结果参与目标偏移地址的验证。
可选地,本公开实施例提供的线程快照解析装置500还包括:
程序监控模块,用于基于预设运行周期中的线程快照的符号化解析结果,监控应用程序的运行性能;其中,符号化解析结果中包括函数名、函数所在文件名、函数在文件中对应的行号中的至少一种。
可选地,本公开实施例提供的线程快照解析装置500还包括:
待监控设备确定模块,用于根据设备标识,确定待监控设备;
待监控应用程序确定模块,用于根据应用程序标识,确定待监控设备中待监控的应用程序;
线程快照与线程ID确定模块,用于与待监控设备建立通信,获取在应用程序的预设运行周期中各个线程的线程快照,以及各个线程的线程ID;
程序计数器地址确定模块,用于基于线程ID对获取的线程快照进行拆分,并从每个线程的线程快照中,确定每个线程的调用栈中第二层栈帧对应的程序 计数器地址。
本公开实施例所提供的线程快照解析装置可执行本公开实施例所提供的任意线程快照解析方法,具备执行方法相应的功能模块和有益效果。本公开装置实施例中未详尽描述的内容可以参考本公开任意方法实施例中的描述。
图6为本公开实施例提供的一种电子设备的结构示意图,用于对实现本公开实施例提供的线程快照解析方法的电子设备进行示例性说明。本公开实施例中的电子设备可以包括但不限于诸如移动电话、笔记本电脑、数字广播接收器、PDA(个人数字助理)、PAD(平板电脑)、PMP(便携式多媒体播放器)、车载终端(例如车载导航终端)等等的移动终端以及诸如数字TV、台式计算机、智能家居设备、可穿戴电子设备、服务器等等的固定终端。图6示出的电子设备仅仅是一个示例,不应对本公开实施例的功能和占用范围带来任何限制。
如图6所示,电子设备600包括一个或多个处理器601和存储器602。处理器601可以是中央处理单元(CPU)或者具有数据处理能力和/或指令执行能力的其他形式的处理单元,并且可以控制电子设备600中的其他组件以执行期望的功能。
存储器602可以包括一个或多个计算机程序产品,计算机程序产品可以包括各种形式的计算机可读存储介质,例如易失性存储器和/或非易失性存储器。易失性存储器例如可以包括随机存取存储器(RAM)和/或高速缓冲存储器(cache)等。非易失性存储器例如可以包括只读存储器(ROM)、硬盘、闪存等。在计算机可读存储介质上可以存储一个或多个计算机程序指令,处理器601可以运行程序指令,以实现本公开实施例提供的线程快照解析方法,还可以实现其他期望的功能。在计算机可读存储介质中还可以存储诸如输入信号、信号分量、噪声分量等各种内容。
其中,本公开实施例提供的线程快照解析方法可以包括:在应用程序的预设运行周期中,基于各个线程的调用栈中第二层栈帧对应的程序计数器地址,确定至少一个目标程序计数器地址;其中,程序计数器地址用于标识程序运行过程中正在执行的函数指令地址;获取应用程序的符号表文件,并确定符号表文件中记录的主函数的初始起始地址、初始结束地址、以及应用程序对应的代 码段的虚拟偏移值;基于至少一个目标程序计数器地址、初始起始地址、初始结束地址、以及虚拟偏移值,确定与预设运行周期中的线程快照对应的至少一个偏移地址区间;基于预设策略和至少一个偏移地址区间,确定与预设运行周期中的线程快照对应的目标偏移地址;其中,预设策略用于定义确定目标偏移地址的规则;基于目标偏移地址,对预设运行周期中的线程快照进行符号化解析。应当理解,电子设备600还可以执行本公开方法实施例提供的其他可选实施方案。
在一个示例中,电子设备600还可以包括:输入装置603和输出装置604,这些组件通过总线系统和/或其他形式的连接机构(未示出)互连。
此外,该输入装置603还可以包括例如键盘、鼠标等等。
该输出装置604可以向外部输出各种信息,包括确定出的距离信息、方向信息等。该输出装置604可以包括例如显示器、扬声器、打印机、以及通信网络及其所连接的远程输出设备等等。
当然,为了简化,图6中仅示出了该电子设备600中与本公开有关的组件中的一些,省略了诸如总线、输入/输出接口等等的组件。除此之外,根据具体应用情况,电子设备600还可以包括任何其他适当的组件。
除了上述方法和设备以外,本公开实施例还提供一种计算机程序产品,其包括计算机程序或计算机程序指令,计算机程序或计算机程序指令在被计算设备执行时使得计算设备实现本公开实施例所提供的任意线程快照解析方法。
计算机程序产品可以以一种或多种程序设计语言的任意组合来编写用于执行本公开实施例操作的程序代码,程序设计语言包括面向对象的程序设计语言,诸如Java、C++等,还包括常规的过程式程序设计语言,诸如“C”语言或类似的程序设计语言。程序代码可以完全地在用户电子设备上执行、部分地在用户电子设备上执行、作为一个独立的软件包执行、部分在用户电子设备上且部分在远程电子设备上执行、或者完全在远程电子设备上执行。
此外,本公开实施例还可以提供一种计算机可读存储介质,其上存储有计算机程序指令,计算机程序指令在被计算设备执行时使得计算设备实现本公开实施例所提供的任意线程快照解析方法。
其中,本公开实施例提供的线程快照解析方法可以包括:在应用程序的预设运行周期中,基于各个线程的调用栈中第二层栈帧对应的程序计数器地址,确定至少一个目标程序计数器地址;其中,程序计数器地址用于标识程序运行过程中正在执行的函数指令地址;获取应用程序的符号表文件,并确定符号表文件中记录的主函数的初始起始地址、初始结束地址、以及应用程序对应的代码段的虚拟偏移值;基于至少一个目标程序计数器地址、初始起始地址、初始结束地址、以及虚拟偏移值,确定与预设运行周期中的线程快照对应的至少一个偏移地址区间;基于预设策略和至少一个偏移地址区间,确定与预设运行周期中的线程快照对应的目标偏移地址;其中,预设策略用于定义确定目标偏移地址的规则;基于目标偏移地址,对预设运行周期中的线程快照进行符号化解析。应当理解,计算机程序指令在被计算设备执行时,还可以使得计算设备实现本公开方法实施例提供的其他可选实施方案。
计算机可读存储介质可以采用一个或多个可读介质的任意组合。可读介质可以是可读信号介质或者可读存储介质。可读存储介质例如可以包括但不限于电、磁、光、电磁、红外线、或半导体的系统、装置或器件,或者任意以上的组合。可读存储介质的更具体的例子(非穷举的列表)包括:具有一个或多个导线的电连接、便携式盘、硬盘、随机存取存储器(RAM)、只读存储器(ROM)、可擦式可编程只读存储器(EPROM或闪存)、光纤、便携式紧凑盘只读存储器(CD-ROM)、光存储器件、磁存储器件、或者上述的任意合适的组合。
需要说明的是,在本文中,诸如“第一”和“第二”等之类的关系术语仅仅用来将一个实体或者操作与另一个实体或操作区分开来,而不一定要求或者暗示这些实体或操作之间存在任何这种实际的关系或者顺序。而且,术语“包括”、“包含”或者其任何其他变体意在涵盖非排他性的包含,从而使得包括一系列要素的过程、方法、物品或者设备不仅包括那些要素,而且还包括没有明确列出的其他要素,或者是还包括为这种过程、方法、物品或者设备所固有的要素。在没有更多限制的情况下,由语句“包括一个……”限定的要素,并不排除在包括要素的过程、方法、物品或者设备中还存在另外的相同要素。
以上仅是本公开的具体实施方式,使本领域技术人员能够理解或实现本公 开。对这些实施例的多种修改对本领域的技术人员来说将是显而易见的,本文中所定义的一般原理可以在不脱离本公开的精神或范围的情况下,在其它实施例中实现。因此,本公开将不会被限制于本文的这些实施例,而是要符合与本文所公开的原理和新颖特点相一致的最宽的范围。

Claims (13)

  1. 一种线程快照解析方法,包括:
    在应用程序的预设运行周期中,基于各个线程的调用栈中第二层栈帧对应的程序计数器地址,确定至少一个目标程序计数器地址;其中,所述程序计数器地址用于标识程序运行过程中正在执行的函数指令地址;
    获取所述应用程序的符号表文件,并确定所述符号表文件中记录的主函数的初始起始地址、初始结束地址、以及所述应用程序对应的代码段的虚拟偏移值;
    基于所述至少一个目标程序计数器地址、所述初始起始地址、所述初始结束地址、以及所述虚拟偏移值,确定与所述预设运行周期中的线程快照对应的至少一个偏移地址区间;
    基于预设策略和所述至少一个偏移地址区间,确定与所述预设运行周期中的线程快照对应的目标偏移地址;其中,所述预设策略用于定义确定所述目标偏移地址的规则;
    基于所述目标偏移地址,对所述预设运行周期中的线程快照进行符号化解析。
  2. 根据权利要求1所述的方法,其中,基于预设策略和所述至少一个偏移地址区间,确定与所述预设运行周期中的线程快照对应的目标偏移地址,包括:
    基于每个偏移地址区间的最大端点值和最小端点值,确定每个偏移地址区间对应的目标整数;其中,所述目标整数为10的N次方;
    对每个偏移地址区间的所述最小端点值和所述目标整数之间的商值向下取整,得到每个偏移地址区间对应的第一倍数值;
    将每个偏移地址区间对应的第一倍数值加1,得到第二倍数值,并基于所述第二倍数值和每个偏移地址区间对应的目标整数的乘积,得到至少一个候选偏移地址;
    基于每个候选偏移地址和对应的偏移地址区间的数值关系、以及每个目标程序计数器地址的出现次数占比,从所述至少一个候选偏移地址中,确定与所述预设运行周期中的线程快照对应的目标偏移地址;
    其中,所述出现次数占比基于所述预设运行周期中每个目标程序计数器地址在对应线程中的出现总次数与各个线程中的线程快照总数之和的比值得到。
  3. 根据权利要求2所述的方法,其中,基于每个候选偏移地址和对应的偏移地址区间的数值关系、以及每个目标程序计数器地址的出现次数占比,从所述至少一个候选偏移地址中,确定与所述预设运行周期中的线程快照对应的目标偏移地址,包括:
    如果候选偏移地址处于对应的偏移地址区间,且参与确定该偏移地址区间的目标程序计数器地址的出现次数占比超过第一占比阈值,则将该候选偏移地址确定为所述目标偏移地址。
  4. 根据权利要求3所述的方法,其中,如果满足候选偏移地址处于对应的偏移地址区间,且参与确定该偏移地址区间的目标程序计数器地址的出现次数占比超过第一占比阈值的情况下,候选偏移地址的数量为至少两个,则将至少两个候选偏移地址中的最小值确定为所述目标偏移地址。
  5. 根据权利要求2所述的方法,其中,在基于所述目标偏移地址,对所述预设运行周期中的线程快照进行符号化解析之前,还包括:
    确定所述符号表文件中记录的所述代码段的加载地址和所述代码段的空间占用大小;
    基于所述加载地址、所述代码段的空间占用大小和所述虚拟偏移值,确定所述应用程序对应的位置参数;
    相应地,基于所述目标偏移地址,对所述预设运行周期中的线程快照进行符号化解析,包括:
    将所述预设运行周期中各个线程快照中大于所述目标偏移地址、且小于所述目标偏移地址和所述位置参数之和的地址,确定为待解析程序计数器地址;
    基于所述目标偏移地址,对所述待解析程序计数器地址进行所述符号化解析。
  6. 根据权利要求1所述的方法,其中,在应用程序的预设运行周期中,基于各个线程的调用栈中第二层栈帧对应的程序计数器地址,确定至少一个目标程序计数器地址,包括:
    在所述应用程序的预设运行周期中,确定每个线程的调用栈中第二层栈帧对应的程序计数器地址,在该线程中的出现总次数;
    基于每个线程中的每个程序计数器地址的出现总次数,以及各个线程中的线程快照总数之和,确定每个线程中的每个程序计数器地址的出现次数占比;
    将出现次数占比大于或等于第二占比阈值、且只出现在一个线程中的程序计数器地址确定为所述至少一个目标程序计数器地址。
  7. 根据权利要求1所述的方法,其中,还包括:
    确定经过所述符号化解析后的各个线程的调用栈中第二层栈帧对应的解析函数名;
    确定至少一个解析函数名中是否存在所述主函数的函数名,以及该函数名是否只出现在一个线程中,并根据确定结果对所述目标偏移地址进行验证。
  8. 根据权利要求7所述的方法,其中,如果在所述应用程序的启动过程中,执行对所述预设运行周期中的线程快照进行符号化解析的操作,则所述方法还包括:
    确定所述至少一个解析函数名中是否存在程序启动函数DidFinishLaunch的函数名,以使确定结果参与所述目标偏移地址的验证。
  9. 根据权利要求1所述的方法,其中,还包括:
    基于所述预设运行周期中的线程快照的符号化解析结果,监控所述应用程序的运行性能;其中,所述符号化解析结果中包括函数名、函数所在文件名、函数在文件中对应的行号中的至少一种。
  10. 根据权利要求1所述的方法,其中,在基于各个线程的调用栈中第二层栈帧对应的程序计数器地址,确定至少一个目标程序计数器地址之前,还包括:
    根据设备标识,确定待监控设备;
    根据应用程序标识,确定所述待监控设备中待监控的应用程序;
    与所述待监控设备建立通信,获取在所述应用程序的预设运行周期中各个线程的线程快照,以及各个线程的线程ID;
    基于所述线程ID对获取的线程快照进行拆分,并从每个线程的线程快照 中,确定每个线程的调用栈中第二层栈帧对应的程序计数器地址。
  11. 一种线程快照解析装置,包括:
    目标程序计数器地址确定模块,用于在应用程序的预设运行周期中,基于各个线程的调用栈中第二层栈帧对应的程序计数器地址,确定至少一个目标程序计数器地址;其中,所述程序计数器地址用于标识程序运行过程中正在执行的函数指令地址;
    符号表文件获取模块,用于获取所述应用程序的符号表文件,并确定所述符号表文件中记录的主函数的初始起始地址、初始结束地址、以及所述应用程序对应的代码段的虚拟偏移值;
    偏移地址区间确定模块,用于基于所述至少一个目标程序计数器地址、所述初始起始地址、所述初始结束地址、以及所述虚拟偏移值,确定与所述预设运行周期中的线程快照对应的至少一个偏移地址区间;
    目标偏移地址确定模块,用于基于预设策略和所述至少一个偏移地址区间,确定与所述预设运行周期中的线程快照对应的目标偏移地址;其中,所述预设策略用于定义确定所述目标偏移地址的规则;
    线程快照解析模块,用于基于所述目标偏移地址,对所述预设运行周期中的线程快照进行符号化解析。
  12. 一种电子设备,包括存储器和处理器,其中,所述存储器中存储有计算机程序,当所述计算机程序被所述处理器执行时,使得所述电子设备实现权利要求1-10中任一项所述的线程快照解析方法。
  13. 一种计算机可读存储介质,所述存储介质中存储有计算机程序,当所述计算机程序被计算设备执行时,使得所述计算设备实现权利要求1-10中任一项所述的线程快照解析方法。
PCT/CN2022/076543 2021-03-24 2022-02-17 线程快照解析方法、装置、设备和存储介质 WO2022199282A1 (zh)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN202110315442.5 2021-03-24
CN202110315442.5A CN113032215B (zh) 2021-03-24 2021-03-24 线程快照解析方法、装置、设备和存储介质

Publications (1)

Publication Number Publication Date
WO2022199282A1 true WO2022199282A1 (zh) 2022-09-29

Family

ID=76473912

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2022/076543 WO2022199282A1 (zh) 2021-03-24 2022-02-17 线程快照解析方法、装置、设备和存储介质

Country Status (2)

Country Link
CN (1) CN113032215B (zh)
WO (1) WO2022199282A1 (zh)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115858399A (zh) * 2023-02-09 2023-03-28 创智和宇信息技术股份有限公司 一种利用线程栈快照进行代码级性能分析的方法和系统

Families Citing this family (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113032215B (zh) * 2021-03-24 2024-05-28 北京字节跳动网络技术有限公司 线程快照解析方法、装置、设备和存储介质
CN113608982A (zh) * 2021-07-27 2021-11-05 远景智能国际私人投资有限公司 函数执行性能监测方法、装置、计算机设备及存储介质
CN113900910A (zh) * 2021-12-08 2022-01-07 北京新唐思创教育科技有限公司 应用程序监控方法、装置、存储介质和电子设备
CN116540990B (zh) * 2023-06-30 2023-09-01 贵州轻工职业技术学院 基于嵌入式实现电子产品的代码集成方法及装置

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6678883B1 (en) * 2000-07-10 2004-01-13 International Business Machines Corporation Apparatus and method for creating a trace file for a trace of a computer program based on loaded module information
CN110888773A (zh) * 2019-10-28 2020-03-17 北京字节跳动网络技术有限公司 一种获取线程标识的方法、装置、介质和电子设备
CN110908882A (zh) * 2019-11-14 2020-03-24 北京无限光场科技有限公司 一种应用程序的性能分析方法、装置、终端设备及介质
CN111625422A (zh) * 2020-05-27 2020-09-04 北京字节跳动网络技术有限公司 线程监控方法、装置、电子设备及计算机可读存储介质
CN113032215A (zh) * 2021-03-24 2021-06-25 北京字节跳动网络技术有限公司 线程快照解析方法、装置、设备和存储介质

Family Cites Families (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US8245002B2 (en) * 2008-10-08 2012-08-14 International Business Machines Corporation Call stack protection
CN104572046B (zh) * 2013-10-16 2019-01-11 腾讯科技(深圳)有限公司 一种堆栈还原方法和计算机系统
CN107943520B (zh) * 2016-10-11 2020-10-30 腾讯科技(深圳)有限公司 应用堆栈信息获取方法及装置、堆栈信息解析方法及装置

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6678883B1 (en) * 2000-07-10 2004-01-13 International Business Machines Corporation Apparatus and method for creating a trace file for a trace of a computer program based on loaded module information
CN110888773A (zh) * 2019-10-28 2020-03-17 北京字节跳动网络技术有限公司 一种获取线程标识的方法、装置、介质和电子设备
CN110908882A (zh) * 2019-11-14 2020-03-24 北京无限光场科技有限公司 一种应用程序的性能分析方法、装置、终端设备及介质
CN111625422A (zh) * 2020-05-27 2020-09-04 北京字节跳动网络技术有限公司 线程监控方法、装置、电子设备及计算机可读存储介质
CN113032215A (zh) * 2021-03-24 2021-06-25 北京字节跳动网络技术有限公司 线程快照解析方法、装置、设备和存储介质

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115858399A (zh) * 2023-02-09 2023-03-28 创智和宇信息技术股份有限公司 一种利用线程栈快照进行代码级性能分析的方法和系统

Also Published As

Publication number Publication date
CN113032215A (zh) 2021-06-25
CN113032215B (zh) 2024-05-28

Similar Documents

Publication Publication Date Title
WO2022199282A1 (zh) 线程快照解析方法、装置、设备和存储介质
US8832666B2 (en) Dynamic instrumentation
CN108874672B (zh) 应用程序异常定位方法、装置、设备及存储介质
CN112099982B (zh) 一种崩溃信息定位方法、装置、介质和电子设备
US20140067886A1 (en) Information processing apparatus, method of outputting log, and recording medium
CN110928732A (zh) 服务器集群性能采样分析方法、装置及电子设备
CN112817831A (zh) 应用性能监测方法、装置、计算机系统和可读存储介质
CN112507259A (zh) 网页加载方法及装置、电子设备、存储介质
CN112084024B (zh) 一种内存监控方法、装置、介质和电子设备
CN114840562B (zh) 业务数据的分布式缓存方法、装置、电子设备及存储介质
CN112817687A (zh) 一种数据同步方法和装置
US10146660B2 (en) Providing data to a user interface for performance monitoring
CN112379967B (zh) 模拟器检测方法、装置、设备及介质
CN110764995B (zh) 一种检测文件访问异常的方法、装置、介质和电子设备
CN111741046B (zh) 数据上报方法、获取方法、装置、设备及介质
US20130097622A1 (en) Framework for system communication for handling data
CN112559394B (zh) 系统库访问方法、装置以及电子设备
CN116225639B (zh) 任务分配方法、装置、电子设备及可读存储介质
CN113395291B (zh) 流量控制方法、装置、电子设备及存储介质
CN114398233B (zh) 一种负载异常检测方法、装置、服务器和存储介质
CN111782410B (zh) 锁堵塞的监控方法、装置、电子设备及计算机可读介质
CN111638903B (zh) 不同软件版本的差异信息生成方法及装置
CN117633027A (zh) 一种数据处理方法、装置、电子设备及存储介质
CN116932375A (zh) 一种空指针问题的检测方法、装置、设备及介质
CN115391130A (zh) 应用程序的耗时监控方法、装置和设备

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 22773944

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

32PN Ep: public notification in the ep bulletin as address of the adressee cannot be established

Free format text: NOTING OF LOSS OF RIGHTS PURSUANT TO RULE 112(1) EPC (EPO FORM 1205A DATED 12.02.2024)