CN120743789B - Firmware code coverage rate statistical method and electronic equipment - Google Patents
Firmware code coverage rate statistical method and electronic equipmentInfo
- Publication number
- CN120743789B CN120743789B CN202511232544.5A CN202511232544A CN120743789B CN 120743789 B CN120743789 B CN 120743789B CN 202511232544 A CN202511232544 A CN 202511232544A CN 120743789 B CN120743789 B CN 120743789B
- Authority
- CN
- China
- Prior art keywords
- code
- file
- array
- firmware
- firmware code
- Prior art date
- Legal status (The legal status 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 status listed.)
- Active
Links
Landscapes
- Debugging And Monitoring (AREA)
Abstract
The application discloses a statistical method of firmware code coverage rate and electronic equipment, which relate to the technical field of program detection and comprise the steps of responding to a first script construction command, calling an automatic script to insert a stub code in a firmware code file, wherein the stub code comprises a file index, a statistical function and a macro definition, a first array created by the macro definition records a code line number and execution times, a second array created by the macro definition records a file index, a line number limit value and a first array address, when the firmware code file is operated, the statistical function is called when the firmware code file is operated, and the statistical function calculates the firmware coverage rate according to the executed codes in the first array and the second array and calculates the firmware coverage rate by combining the executed codes and the total codes of the firmware code file, thereby solving the problems of occupying a large amount of memory and affecting the operation performance in related technologies and achieving the technical effects of reducing the firmware resource occupation and not affecting the operation performance of the firmware.
Description
Technical Field
The present application relates to the field of program detection technologies, and in particular, to a firmware code coverage rate statistics method and an electronic device.
Background
In software development, code coverage statistics are an important measure of test integrity and quality. As system complexity continues to increase, code coverage becomes increasingly critical to ensuring software reliability and security.
At present, the related technology has obvious defects in the field of firmware code coverage statistics, namely, on one hand, part of tools only can provide basic block coverage, the operation is complex and the efficiency is low, and on the other hand, part of tools support finer indexes such as branch coverage, condition coverage and the like, but the code expansion is easy to cause in the pile inserting process, the storage and memory expenditure is increased, and the development is difficult in a firmware environment with limited resources.
Disclosure of Invention
The application provides a firmware code coverage rate statistical method and electronic equipment, which at least solve the problems that a large amount of storage and memory are occupied and the running performance is influenced when the firmware code coverage rate is counted in the related technology.
The application provides a firmware code coverage rate statistical method which comprises the steps of responding to a first script construction command, calling an automatic script to insert a stub code into a firmware code file, wherein the stub code comprises a file index, a statistical function and a macro definition, a first array and a second array are created in the macro definition, the first array is used for recording code line numbers and execution times, the second array is used for recording file indexes, line number limit values and addresses of the first array, operating the firmware code file, calling the statistical function in the stub code when the firmware code file is identified to be operated to an inserting position of the stub code, and the statistical function is used for counting executed codes in the firmware code file according to the first array and the second array, obtaining the total codes of the firmware code file, and calculating the firmware code coverage rate of the firmware code file according to the executed codes and the total codes in the firmware code file.
The application provides a firmware code coverage rate statistics device which comprises a calling module, a statistics module and a calculation module, wherein the calling module is used for responding to a first script construction command and calling an automation script to insert a stub code into a firmware code file, the stub code comprises a file index, a statistics function and a macro definition, a first array and a second array are created in the macro definition, the first array is used for recording code line numbers and execution times, the second array is used for recording file indexes, line number limit values and addresses of the first array, the statistics module is used for operating the firmware code file, when the firmware code file is identified to be operated to an insertion position of the stub code, the statistics function is used for calling the statistics function in the stub code, and the statistics function is used for counting executed codes in the firmware code file according to the first array and the second array, and the calculation module is used for obtaining total codes of the firmware code file and calculating the firmware code coverage rate of the firmware code file according to the executed codes and the total codes in the firmware code file.
The application also provides an electronic device, which comprises a memory for storing a computer program and a processor for realizing the steps of the firmware code coverage rate statistical method when executing the computer program.
The application also provides a nonvolatile computer readable storage medium, wherein the nonvolatile computer readable storage medium stores a computer program, and the computer program realizes the steps of the firmware code coverage rate statistical method when being executed by a processor.
The application also provides a computer program product, which comprises a computer program and the steps for realizing the firmware code coverage rate statistical method when the computer program is executed by a processor.
According to the application, as the automatic script is called to insert the stub code into the firmware code in response to the construction command, the stub comprises the file index, the statistical function and the macro definition, the macro definition creates the first array to record the code line number and the execution times, the second array to record the file index, the line number upper limit and the first array address, and when the firmware code is operated, the statistical function is called from the execution point to the stub to calculate the coverage rate according to the array statistics executed code and the total code, thereby solving the problems of occupying a large amount of storage and memory and affecting the operation performance of the related technology and achieving the technical effects of reducing the firmware resource occupation and not affecting the operation performance of the firmware.
Drawings
For a clearer description of embodiments of the present application, the drawings that are required to be used in the embodiments will be briefly described, it being apparent that the drawings in the following description are only some embodiments of the present application, and other drawings may be obtained according to the drawings without inventive effort for those skilled in the art.
Fig. 1 is a schematic structural diagram of a firmware code coverage statistics method according to an embodiment of the present application;
FIG. 2 is a flowchart of a statistical automated analysis of firmware code coverage provided by an embodiment of the present application;
FIG. 3 is a flowchart illustrating the execution of a firmware code coverage statistics function according to an embodiment of the present application;
Fig. 4 is a schematic structural diagram of a firmware code coverage statistics apparatus according to an embodiment of the present application;
Fig. 5 is a schematic structural diagram of an electronic device according to an embodiment of the present application.
Detailed Description
The following description of the embodiments of the present application will be made clearly and completely with reference to the accompanying drawings, in which it is apparent that the embodiments described are only some embodiments of the present application, but not all embodiments. Based on the embodiments of the present application, all other embodiments obtained by a person of ordinary skill in the art without making any inventive effort are within the scope of the present application.
It should be noted that in the description of the present application, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. The terms "first," "second," and the like in this specification are used for distinguishing between similar objects and not necessarily for describing a particular sequential or chronological order.
In order to better understand the aspects of the present application, the present application will be described in further detail with reference to fig. 1 and the detailed description.
Fig. 1 is a schematic structural diagram of a firmware code coverage rate statistical method according to an embodiment of the present application, as shown in fig. 1, the firmware code coverage rate statistical method includes the following steps:
In step S101, in response to the first script construction command, the automation script is invoked to insert a stub code in the firmware code file, wherein the stub code includes a file index, a statistical function, and a macro definition, and a first array and a second array are created in the macro definition, the first array is used for recording a code line number and execution times, and the second array is used for recording the file index, a line number limit value, and an address of the first array.
Wherein the first script is an automation script for instrumentation firmware code. Stub code is additional code inserted in the firmware code file for collecting coverage statistics at firmware runtime. The file index is a unique number of each firmware code file in firmware engineering and is used for distinguishing data sources of different files in the coverage rate statistics process. The statistical function is a function called by the stub code, and when the firmware runs, the count value corresponding to the current execution position is accumulated and stored in the appointed data structure. The macro definition is a macro declared by a C language preprocessing instruction # definition and is used for automatically expanding a data structure, an array declaration and a statistic function call required by a stake point in a compiling stage, so that coverage rate acquisition codes are rapidly generated in firmware codes, and manual writing one by one is avoided. The first array is a file_coverage_cnt_t type array, and is used for storing coverage statistics information of the firmware code line level, and the array elements comprise code line numbers and corresponding execution times. The code line number is the position number of each statement or code line in the firmware code and is used for counting the execution times. The second array is a file_coverage_cfg_t type array, and is used for storing code coverage rate configuration information of a file level, and the array elements comprise a file index, a line number limit value and an address of the first array corresponding to the file. The file index is a unique identifier for associating each file with corresponding statistical data in coverage statistics, so that the line-level execution times can be correctly recorded in an array of the corresponding file. The line number limit is used to define the maximum code line number that the first array can record, preventing access out of range.
It can be understood that by inserting the stub code into the firmware code file, the embodiment of the application can automatically collect code coverage rate data in the firmware running process, thereby realizing accurate tracking of the execution condition of the firmware code. The first array is used for recording the line number of each line code and the corresponding execution times so as to obtain line-level coverage rate information, and the second array is used for storing the file index, the line number limit value and the address of the first array corresponding to the file. And defining a line number limit value for limiting the statistical range, preventing invalid access and a plurality of groups from crossing the boundary, and improving the query security of the statistical function. The method and the device realize grouping management and accurate positioning of the coverage rate data of different firmware codes, and ensure that the execution conditions of different files are not confused in the statistical process. Meanwhile, the statistical function is dynamically invoked when the firmware runs, and the integrity and accuracy of coverage rate data are ensured by continuously accumulating the execution times in the first array.
In one embodiment of the application, calling the automation script to insert the stub code into the firmware code file comprises calling the folder of the automation script copying the stub code as a backup folder, compiling the firmware code file after the insertion of the stub code into the firmware code file is completed, terminating the compiling flow of the firmware code file if the compiling of the firmware code file fails, and compressing the folder of the stub code after the compiling of the firmware code file.
The automatic script is a script program for realizing the operations of instrumentation, backup, compiling and compression of the firmware code, and the operations can be automatically completed by calling the script. The file of stub code is the original firmware code file of the original unpinned. The backup folder is generated by an automation script copying stub code folder prior to instrumentation for restoring the original firmware code upon a stub insertion or compilation failure. Compilation is a process of processing instrumented firmware code files, including syntax checking, generating object files, etc., to generate executable firmware files. The compression is to pack the instrumented firmware code folders into compressed files for storage for archiving and subsequent analysis.
It can be understood that the automatic script can copy the folder of the stub code before the instrumentation to generate a backup folder so as to ensure that the original code can be restored when the instrumentation or the compiling fails, insert the stub code into the firmware code file and compile the stub code so as to ensure that the code after the instrumentation can successfully generate executable firmware, automatically terminate the process if the compiling fails and avoid the generation of wrong firmware, and compress and save the folder of the stub code after the instrumentation after the compiling is completed so as to facilitate archiving, management and subsequent coverage analysis.
The first script construction command of the embodiment of the application is completed by cooperation of an automation script and Makefile configuration, and is used for inserting a stub code into a firmware code to realize coverage rate data acquisition. The parameter "cov=1" is added when the "make" command is executed, and Makefile invokes the automation script to initiate the instrumentation flow. The Makefile is a configuration file for managing the compiling and constructing processes of the firmware codes, and the operations of calling the automation script, compiling, inserting piles, cleaning the firmware codes and the like are realized by defining commands and parameters.
Specifically, first, the first script copies the folder of stub code to generate a backup folder for restoring the original code when an exception occurs during instrumentation or compilation. Then, the first script processes the target C file, and inserts a stub code into each file, specifically including:
Defining file_num as FILE index for distinguishing different firmware code FILEs, inserting cov_entry macro definition, creating the first array and the second array by cov_entry macro definition, defining max_entry to represent total number of functions inserted with cov_entry macro definition in current firmware code FILE, and referencing necessary header FILEs to ensure availability of statistical functions and data structures. The macro definition generates a unique variable name through parameter splicing, and the variable is placed in a designated memory segment so that a statistical function can be quickly accessed, and automatic generation of a primary coverage rate statistical code is realized.
After the insertion is completed, the script compiles the firmware code file to verify that the code after the instrumentation can correctly generate executable firmware, and if C file format errors or other anomalies occur in the compiling process, the script outputs error information and terminates the compiling process to prompt a user to repair so as to ensure that the subsequent process is normally executed. Finally, the script compresses the folder inserted with the pile point into a ZIP file for storage, thereby facilitating archiving and management.
In one embodiment of the application, after the folder of the stub code is copied as the backup folder by calling the automation script, the method further comprises the steps of responding to a second script construction command, calling the automation script to delete the folder of the stub code, and restoring the backup file into the folder of the stub code.
Wherein the second script refers to an automation script for deleting the inserted stub code and restoring the backup folder. The restore is to copy the backup file back to the original location to restore the state before the stub code folder, thereby completing the instrumentation clean and restore operations.
It can be understood that in the embodiment of the application, by responding to the second script construction command, the automation script is called to delete the folder in which the stub code is inserted, and the backup file is restored to the original stub code folder, so that safe cleaning and quick restoration of the stub code are realized, the original state can be restored through the backup file when the firmware code is inserted or abnormal occurs in the compiling process, and the integrity and stability of the stub code are ensured. Meanwhile, the automatic script performs deleting and restoring operations uniformly, so that the backup file can be reused, the testing efficiency is improved, and the operation flow is simplified.
The second script construction command of the embodiment of the application is completed by the automation script and Makefile configuration together and is used for cleaning the flow of the firmware code. When the "make DCOV =1" command is executed, makefile invokes the corresponding automation script to initiate the cleaning operation.
First, the script deletes the folder of the stub code that has been inserted into the stub to clean up the instrumentation data. The script then restores the backup folder generated prior to instrumentation to the original location to restore the original firmware code state. In addition, the clean-up process does not process the compressed ZIP file. Among them, ZIP files are a compressed file format for packaging and compressing one or more files or folders to reduce storage space or facilitate transmission.
In one embodiment of the application, before restoring the backup file into the file folder of the stub code, the method further comprises the steps of identifying whether the backup file is damaged or not, generating at least one of error information and manual processing prompt if the backup file is damaged or not, and restoring the backup file into the file folder of the stub code if the backup file is damaged or not.
It can be understood that when the backup file is abnormal, the automatic script timely prompts a user to repair or manually intervene, so that incomplete or damage of the pile point code caused by direct restoration is avoided, and when the backup file exists and is intact, the embodiment of the application can quickly restore the original pile point code state, and provides a reliable basis for subsequent pile insertion, compiling or coverage analysis.
Specifically, before restoring the backup file into the stub code folder, the second script will first check whether the backup file exists or is damaged, so as to avoid incomplete stub or abnormal coverage statistics caused by the missing or damaged backup file. If it is detected that the backup folder is not present or is damaged, embodiments of the present application perform at least one of generating an error message to inform the user of an exception condition, or providing a manual processing prompt to instruct the user to take corresponding action (e.g., regenerating the backup file or repairing the damaged file). If the backup file exists and is not damaged, a restore operation is executed to restore the backup file to the original stub code folder, so that the firmware stub is restored to the state before instrumentation.
In one embodiment of the present application, script call parameters are added to the first script construction command and the second script construction command, wherein when the script construction command is executed, an automation script is called based on the script call parameters.
It will be appreciated that automation of firmware stub operations may be achieved by invoking an automation script based on script call parameters when executing script build commands. And the processes of pile inserting, cleaning, backup or compression and the like are flexibly triggered according to different parameters, so that the tedious and error of manual operation are avoided.
The automatic method for analyzing the code coverage rate in the software test in the embodiment of the application has the flow shown in figure 2 and comprises the following steps:
In step S201, the user executes a "make cov=1" command for triggering a predefined operation flow in Makefile. By adding the "cov=1" parameter, makefile can identify invoking the corresponding automation script to initiate code coverage statistics and instrumentation operations.
In step S202, makefile invokes a first script (e.g., add_patch_cov.py) for inserting a stub in the stub code, so as to implement real-time monitoring of the program execution path. The first script realizes the monitoring of the program execution path by inserting the stub point in the stub point code file.
In step S203, to ensure the integrity of the stub code, the first script first copies the entire stub code folder, generating a copy named "xx_copy". All subsequent instrumentation, modification and compiling operations are performed in the copy, so that direct influence on the original code is avoided, firmware code can be conveniently and rapidly restored to the original state when abnormality occurs, and operation safety and reliability are guaranteed.
In step S204, the script traverses all C-language source files in the duplicate folder and inserts stub codes at specific locations. The inserted stub code is mainly used for recording the execution times of the corresponding code row, and can ensure that the coverage rate information of the row level can be obtained.
In step S205, after the instrumentation of all C files is completed, the script invokes a compiling tool chain, such as GCC (GNU Compiler Collection, GNU compiler set), to compile the firmware code in the copy, and generate an instrumented executable file. The executable file can dynamically collect coverage rate information when running, and comprises execution times and execution states of each code line, so that a data basis is provided for program analysis, debugging and performance evaluation.
In step S206, the script packages the entire instrumented folder into a ZIP-format compressed file for storage, management, and subsequent transmission. The compressed package is used for storing all the firmware codes after instrumentation and the generated executable files, and provides a complete and reusable data foundation for coverage analysis, debugging and version management.
In step S102, the firmware code file is executed, and when it is identified that the firmware code file is executed to the insertion position of the stub code, the statistical function in the stub code is called, and the statistical function counts the executed codes in the firmware code file according to the first array and the second array.
It can be understood that by identifying the position of the code from execution to stub and calling the statistical function in the running process of the firmware code, the execution condition of the firmware code can be recorded in real time, the execution times of each code can be accurately collected, and the coverage statistics of the level and the file level can be realized.
In one embodiment of the application, the statistical function counts executed codes in the firmware code file according to the first array and the second array, and comprises the steps of acquiring a current file index and a current code line number, inquiring the second array according to the current file index, generating warning information if no matched file index is inquired in the second array, acquiring an address of the first array from the second array if the matched file index is inquired in the second array, linking the first array according to the address of the first array, inquiring the first array based on a line number limit value and the current code line number, accumulating the execution times of the current code line number if the matched code line number is inquired from the first array, and generating the warning information if the matched code line number is not inquired from the first array.
It can be understood that the execution times of each code line in each file can be accurately recorded by counting the execution conditions of the firmware codes according to the first array and the second array through the statistical function. When the file index or the code line number is not matched in the counting process, warning information is generated, the problem that incorrect instrumentation or firmware code change possibly exists is prompted, and the identification of executed and unexecuted code lines is facilitated.
The statistical function consists of a data structure definition and a coverage rate statistical function COV function, and is used for recording the execution condition of codes when the firmware runs. When the firmware code is executed to the inserted stub position, a statistical function is called and the current file index and code line number are received as parameters.
The statistical function first traverses the second array to find the array element matching the current file index. And if the matched file index is queried in the second array, acquiring the address of the first array from the array element, and accessing the corresponding counting array according to the address.
Then, searching the element matched with the current code line number in the first array, accumulating the execution times of the line code, and if the accumulation of the execution times has an upper limit, not increasing any more. For example, the upper limit of the execution times of a certain line of code is 65535. When the accumulated number of executions of the line code has reached 65535, the accumulated number of times does not increase even if executed again, and remains 65535, thereby avoiding overflow of the count.
If the statistical function searches the element corresponding to the current code line number in the first array, the matched line number is not found, which indicates that the line code is not recorded or the array is full, and the embodiment of the application generates warning information.
In one embodiment of the application, if no matched code line number is queried from the first array, the method further comprises the steps of querying whether the execution times of the target line number are recorded in the first array, writing the execution times of the target line number into the current code line number if the execution times of the target line number are queried in the first array, accumulating the execution times of the current code line number, and generating warning information if the execution times of the target line number are not queried in the first array.
It can be understood that by firstly inquiring whether the execution times of the target line number exist in the first array, accumulation of the execution times of the current line number can be ensured to be based on the existing data, repeated counting or missing counting is avoided, if the target line number exists in the record, the execution times of the target line number can be written into the current line number and accumulated, statistical data cannot be lost when the target line number is executed for multiple times or different pile inserting positions are ensured, warning information can be generated when the record of the target line number is not inquired in the first array, a developer is reminded that problems such as pile inserting omission, array fullness or data abnormality possibly exist, and potential errors can be found and processed as early as possible.
FIG. 3 illustrates the execution logic and associated process flow of a statistics function in a code coverage statistics process in an embodiment of the present application. The whole flow starts from the function call, the corresponding data position is determined through the file index and the line number, the accumulation of the code execution times is finally realized, and meanwhile, warning information is output when the corresponding records cannot be matched.
The flow starts at step S301 by first invoking a statistics function that receives as input a file index and a line number for subsequent statistics processing. Then in step S302, a second array is traversed, in which file related information is stored for matching the target file index. In step S303, it is determined whether an entry matching the target file index is found, if not, a warning message is directly output and returned, and if the matching file index is found, an address corresponding to the first array is acquired in step S304 for subsequent line number matching and counting operations.
Next, in step S305, the first array is traversed, and the specific code line number and the execution times thereof are recorded in the first array. In step S306, it is determined whether a record matching the target line number is found, if so, the address of the line number in the first array is directly obtained in step S307, and the number of times of execution is accumulated in step S308 (the upper limit of the accumulated value is 65535);
If no matching line number is found, it is further determined in step S309 whether there is a record with a line number of 0. If the line number is the skip line number, the current line number is written into the array in S310, and the operation of counting and accumulating is performed in step S308, if the record of the line number 0 is not recorded, the counting position cannot be allocated for the current line number, and the embodiment of the application outputs warning information and returns.
According to the embodiment of the application, the corresponding file is found by traversing the second array, the corresponding line number is found by traversing the first array, and whether the execution times are accumulated or not is determined according to whether the line number is the jump mark line number, so that accurate statistics on the execution condition of the firmware code is realized.
In step S103, the total code of the firmware code file is acquired, and the firmware code coverage of the firmware code file is calculated from the executed code and the total code in the firmware code file.
Where firmware code coverage is used to measure the integrity of firmware testing, typically expressed as a percentage of the total code that is executed.
It will be appreciated that by taking the total code of the firmware code file and calculating coverage in conjunction with the code lines that are actually executed, test coverage can be accurately assessed, identifying areas of code that are not executed.
In one embodiment of the application, before calculating the firmware code coverage rate of the firmware code file according to the executed code and the total code in the firmware code file, the method further comprises the steps of acquiring a first interaction command, wherein the first interaction command is defined by a management command, responding to the first interaction command, deriving the area data of the first array and the area data of the second array, and determining the executed code in the firmware code file according to the derived data.
Wherein, the first interactive command is a command sent by the host end for exporting firmware statistic data.
It will be appreciated that by obtaining and responding to the first interactive command, the region data of the first array and the second array are derived from the firmware, and thus determining the actual execution of the firmware code file, the test coverage can be precisely quantified, and the uncovered code region identified.
The host side sends commands for exporting firmware statistics, which are exported and purged by defining nvme vendor admin command formats (including Opcode, NSID, DW, DW13, etc. fields). In the data export stage, the area data of the first array and the area data of the second array are exported according to the value designated in the command, and the size and the range of the exported data are determined by combining other parameters in the command. After receiving the command, the firmware returns the data of the corresponding area to the host, and the host reads the statistical result through the analysis script to generate a report containing relevant information, counts unexecuted code lines and calculates the execution coverage rate of each file.
In one embodiment of the application, before calculating the firmware code coverage rate of the firmware code file according to the executed code and the total code in the firmware code file, the method further comprises the steps of obtaining a second interaction command, wherein the second interaction command is defined by the management command, and clearing corresponding file data in the firmware code file in response to the second interaction command.
The second interaction command is a command sent by the host side for clearing the firmware statistic record.
It can be understood that by acquiring and responding to the second interaction command, the corresponding statistical data in the firmware code file is cleared, so that the initial state of coverage statistics before each test can be ensured, the interference of the historical test data on the analysis of the new coverage rate can be effectively eliminated, and the execution condition of misjudgment codes can be avoided.
In the record clearing stage, the host end sends a command for clearing the firmware statistical record, and different clearing operations are executed according to the value specified in the command, wherein the clearing operations comprise clearing the statistical record of all files, clearing the statistical record of all specified files or clearing the statistical record of specific line numbers in the specified files. If the command format does not meet the definition, the embodiment of the application triggers an error.
Through the first interaction command and the second interaction command, the host side and the firmware can realize accurate derivation and on-demand cleaning of statistical data.
In one embodiment of the application, the method for calculating the firmware code coverage rate of the firmware code file according to the executed codes and the total codes in the firmware code file comprises the steps of inputting the executed codes and the total codes in the firmware code file into an analysis module at a host end, and calculating the firmware code coverage rate of the firmware code file by the analysis module through a plurality of analysis scripts, wherein the analysis module comprises a first analysis script, a second analysis script and a third analysis script, the first analysis script reads a statistical result, the second analysis script performs statistics on stake points, the third analysis script reads data of the first analysis script and the second analysis script, calculates the unexecuted codes according to the executed codes and the total codes in the firmware code file, and calculates the firmware code coverage rate of the firmware code file.
The method comprises the steps of reading a firmware statistical result and generating a structured data file containing information such as file indexes, line numbers, reference times and the like, counting all inserted stub points in the firmware by a first analysis script, calculating execution conditions of each code line or code block, and analyzing the code lines which are not executed based on data generated by the first two scripts by a third analysis script, and calculating execution coverage rate of each firmware code file.
It can be understood that by inputting the actually executed code and the total code in the firmware code files into the host-side parsing module and calculating the coverage rate by utilizing the cooperation of a plurality of parsing scripts, the execution condition of each firmware code file can be accurately obtained, and the executed and unexecuted code lines can be clearly identified.
The embodiment of the application realizes coverage rate statistics through cooperation of a plurality of analysis scripts, specifically, a first analysis script reads a firmware statistics result through an interface, analyzes and generates a JSON (JavaScript Object Notation ) format file containing information such as file index, line number, reference times and the like, a second analysis script performs summarization statistics on all inserted pile point data, and a third analysis script analyzes unexecuted code lines and generates corresponding files based on information generated by the first analysis script and the second analysis script, and calculates execution coverage rate of each file. In the event that the data export format is out of specification or incomplete, the script may output an error message to indicate an exception.
In summary, the application discloses a firmware code coverage statistics method based on code instrumentation, and the whole flow comprises a compiling stage, a firmware running stage and a host data analysis stage. In the compiling stage, pile point codes are inserted into firmware code files through an automatic script, the pile points comprise file indexes, function total numbers, specific header files and macro definitions, and makefiles are modified to support automatic pile point insertion, compiling and cleaning, for example, targets of pile insertion, compiling and cleaning are defined by adding COV parameter switches in the makefiles, and the automatic script is called in target rules, so that automation of coverage rate statistics flow is realized.
In the firmware operation stage, a data structure for recording information such as line numbers and execution times, file indexes, count array addresses and the like is defined in the firmware, and a statistical function is realized to accumulate the execution times of each code line, so that the code execution condition can be accurately recorded in the operation process. In addition, the application defines an NVMe (Non-Volatile Memory Express, non-volatile memory standard interface) Vendor Admin Command format containing specific fields, which is used for efficiently exporting statistical data or clearing history records between a host side and firmware, and ensuring the accuracy of the statistical data.
In order to realize accurate monitoring of the code execution condition, the application provides a special data structure and a statistical function which are used for recording the execution times of the code in the running process. The method can count the conditional branch sentences (such as if/else if/else/case) one by one, and can record the call times of the function at the function entrance, thereby realizing coverage rate collection of different execution paths and function call conditions.
And at the host end, the exported statistical data is read through the collaborative analysis script, a detailed report containing the file execution condition is generated, executed and unexecuted code lines are analyzed, and the execution coverage rate of each file is calculated. The method comprises the steps of automatic pile point insertion in a compiling stage, operation efficiency and usability are improved, reliability of statistical data transmission is guaranteed through efficient data interaction based on an NVMe protocol, accuracy of coverage rate statistics is achieved through an accurate data structure and a statistical function, comprehensive reports are provided for multi-script collaborative analysis at a host side, and reliable data support is provided for firmware test evaluation and version iteration. The NVMe protocol is a communication protocol specifically designed for a high-speed solid state disk based on a PCIe (PERIPHERAL COMPONENT INTERCONNECT EXPRESS, bus peripheral interconnect express) bus.
According to the firmware code coverage rate statistical method, the automatic script is called to insert the stub code into the firmware code through responding to the construction command, the stub comprises the file index, the statistical function and the macro definition, wherein the macro definition creates the first array to record the code line number and the execution times, the second array to record the file index, the line number upper limit and the first array address, when the firmware code is operated, the statistical function is called to the stub to count the executed code according to the arrays, and the coverage rate is calculated by combining the total code, so that the problems that a large amount of storage and memory are occupied and the operation performance is influenced in the related technology can be solved, and the technical effects of reducing the firmware resource occupation and not influencing the firmware operation performance are achieved.
The application also provides a firmware code coverage rate statistical device, and in order to enable a person skilled in the art to better understand the scheme of the application, the application is further described in detail below with reference to the accompanying drawings and the specific embodiments.
Fig. 4 is a schematic structural diagram of a firmware code coverage statistics apparatus according to an embodiment of the present application, and as shown in fig. 4, the firmware code coverage statistics apparatus 10 includes a calling module 100, a statistics module 200, and a calculation module 300.
The system comprises a calling module 100, a statistics module 200 and a calculation module 300, wherein the calling module 100 is used for responding to a first script construction command and calling an automation script to insert a stub code into a firmware code file, the stub code comprises a file index, a statistics function and a macro definition, a first array and a second array are created in the macro definition, the first array is used for recording code line numbers and execution times, the second array is used for recording file indexes, line number limit values and addresses of the first array, the statistics module 200 is used for operating the firmware code file, when the firmware code file is identified to be operated to the inserting position of the stub code, the statistics function in the stub code is called, the statistics function is used for counting executed codes in the firmware code file according to the first array and the second array, and the calculation module 300 is used for obtaining the total codes of the firmware code file and calculating the firmware code coverage rate of the firmware code file according to the executed codes and the total codes in the firmware code file.
In one embodiment of the present application, the calling module 100 is further configured to call the folder of the automation script copy stub code to be a backup folder, insert the stub code into the firmware code file, compile the firmware code file after the insertion is completed, if the compilation of the firmware code file fails, terminate the compiling process of the firmware code file, and compress the folder of the stub code after the compilation of the firmware code file.
In one embodiment of the application, after the folder of the stub code is copied by the automatic script as the backup folder, the cleaning module is further used for responding to the second script construction command, calling the automatic script to delete the folder of the stub code, and restoring the backup file as the folder of the stub code.
In one embodiment of the application, the detection module is further used for identifying whether the backup file is damaged or not before restoring the backup file into the file folder of the stub code, generating at least one of error information and manual processing prompt if the backup file is damaged or not, and restoring the backup file into the file folder of the stub code if the backup file is damaged or not.
In one embodiment of the present application, script call parameters are added to the first script construction command and the second script construction command, wherein when the script construction command is executed, an automation script is called based on the script call parameters.
In one embodiment of the present application, the statistics module 200 is further configured to obtain a current file index and a current code line number, query a second array according to the current file index, generate warning information if no matching file index is queried in the second array, obtain an address of a first array from the second array if a matching file index is queried in the second array, link the first array according to the address of the first array, query the first array based on the line number limit and the current code line number, accumulate the execution times of the current code line number if a matching code line number is queried from the first array, and generate warning information if no matching code line number is queried from the first array.
In one embodiment of the application, the accumulation module is further configured to query whether the execution times of the target line number are recorded in the first array before generating the warning information if the matched code line number is not queried from the first array, write the execution times of the target line number into the current code line number if the execution times of the target line number are queried in the first array, accumulate the execution times of the current code line number, and generate the warning information if the execution times of the target line number are not queried in the first array.
In one embodiment of the application, the export module is further configured to obtain a first interaction command before calculating the firmware code coverage of the firmware code file according to the executed code and the total code in the firmware code file, wherein the first interaction command is defined by the management command, export the area data of the first array and the area data of the second array in response to the first interaction command, and determine the executed code in the firmware code file according to the exported data.
In one embodiment of the present application, the export module is further configured to obtain a second interaction command before calculating the firmware code coverage of the firmware code file according to the executed code and the total code in the firmware code file, where the second interaction command is defined by the management command, and clear the corresponding file data in the firmware code file in response to the second interaction command.
The description of the features of the embodiment corresponding to the above-mentioned firmware code coverage statistics device can be referred to the related description of the embodiment corresponding to the firmware code coverage statistics method, and will not be repeated here.
According to the firmware code coverage statistics device, the automatic script is called to insert the stub code into the firmware code through responding to the construction command, the stub comprises the file index, the statistics function and the macro definition, wherein the macro definition creates the first array to record the code row number and the execution times, the second array to record the file index, the upper limit of the row number and the first array address, when the firmware code is operated, the statistics function is called to the stub to count the executed code according to the arrays, and the coverage rate is calculated by combining the total code, so that the problems that a large amount of storage and memory are occupied and the operation performance is influenced in the related technology can be solved, and the technical effects of reducing the firmware resource occupation and not influencing the firmware operation performance are achieved.
From the description of the above embodiments, it will be clear to a person skilled in the art that the method according to the above embodiments may be implemented by means of software plus the necessary general hardware platform, but of course also by means of hardware, but in many cases the former is a preferred embodiment.
An embodiment of the application also provides an electronic device comprising a memory 501 and a processor 502, the memory 501 having stored therein a computer program, the processor 502 being arranged to run the computer program to perform the steps of the firmware code coverage statistics method embodiment described above.
The application also provides a nonvolatile computer readable storage medium, wherein the nonvolatile computer readable storage medium stores a computer program, and the computer program realizes the steps of the firmware code coverage rate statistical method when being executed by a processor.
The application also provides a computer program product, which comprises a computer program and the steps for realizing the firmware code coverage rate statistical method when the computer program is executed by a processor.
Those of skill would further appreciate that the various illustrative elements and algorithm steps described in connection with the embodiments disclosed herein may be implemented as electronic hardware, computer software, or combinations of both, and that the various illustrative elements and steps are described above generally in terms of functionality in order to clearly illustrate the interchangeability of hardware and software. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the solution. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present application.
The method for counting the coverage rate of the firmware codes and the electronic equipment provided by the application are described in detail. The principles and embodiments of the present application have been described herein with reference to specific examples, the description of which is intended only to facilitate an understanding of the method of the present application and its core ideas. It should be noted that it will be apparent to those skilled in the art that various modifications and adaptations of the application can be made without departing from the principles of the application and these modifications and adaptations are intended to be within the scope of the application as defined in the following claims.
Claims (9)
1. A firmware code coverage statistics method, comprising:
Responding to a first script construction command, calling an automatic script to insert a stub code into a firmware code file, wherein the stub code comprises a file index, a statistical function and a macro definition, a first array and a second array are created in the macro definition, the first array is used for recording code line numbers and execution times, and the second array is used for recording the file index, line number limit values and addresses of the first array;
The method comprises the steps of operating a firmware code file, identifying an insertion position of the firmware code file to the stub code, calling a statistical function in the stub code when the firmware code file is operated to the insertion position of the stub code, and counting executed codes in the firmware code file according to a first array and a second array, wherein the statistical function comprises the steps of acquiring a current file index and a current code line number, inquiring the second array according to the current file index, generating warning information if a matched file index is not inquired in the second array, acquiring an address of a first array from the second array if the matched file index is inquired in the second array, and linking the first array according to the address of the first array;
And acquiring the total code of the firmware code file, and calculating the firmware code coverage rate of the firmware code file according to the executed code and the total code in the firmware code file.
2. The firmware code coverage statistics method as recited in claim 1, wherein said calling an automation script inserts stub code in a firmware code file, comprising:
calling the automation script to copy the folder of the stub code as a backup folder;
inserting a stub code into the firmware code file, compiling the firmware code file after insertion, and terminating the compiling flow of the firmware code file if the compiling of the firmware code file fails;
And compressing the folder of the stub code after compiling the firmware code file.
3. The firmware code coverage statistics method of claim 2, further comprising, after invoking the automation script to copy the folder of stub codes as a backup folder:
Responding to a second script construction command, and calling an automation script to delete the folder of the stub code;
and restoring the backup file into a folder of the stub code.
4. The firmware code coverage statistics method as recited in claim 3, further comprising, prior to restoring said backup file to said stub code folder:
identifying whether the backup file is damaged or non-existent;
if the backup file is damaged or not existing, generating at least one of error information and manual processing prompt;
and if the backup file exists and is not damaged, restoring the backup file into the folder of the stub code.
5. The firmware code coverage statistics method as recited in claim 3, wherein a script calling parameter is added to the first script construction command and the second script construction command, wherein the automation script is called based on the script calling parameter when the script construction command is executed.
6. The method of claim 1, further comprising, before generating the warning message if no matching code line number is queried from the first array:
inquiring whether the execution times of the target line number are recorded in the first array;
if the first array is inquired that the execution times of the target line number are recorded, writing the execution times of the target line number into the current code line number, and accumulating the execution times of the current code line number;
and if the execution times of the target line number is not inquired in the first array, generating warning information.
7. The firmware code coverage statistics method as recited in claim 1, further comprising, prior to calculating the firmware code coverage of the firmware code file from the executed code and the total code in the firmware code file:
acquiring a first interaction command, wherein the first interaction command is defined by a management command;
And responding to the first interaction command, deriving the region data of the first array and the region data of the second array, and determining the executed code in the firmware code file according to the derived data.
8. The firmware code coverage statistics method as recited in claim 1, further comprising, prior to calculating the firmware code coverage of the firmware code file from the executed code and the total code in the firmware code file:
acquiring a second interaction command, wherein the second interaction command is defined by a management command;
and responding to the second interaction command, and clearing corresponding file data in the firmware code file.
9. An electronic device, comprising:
a memory for storing a computer program;
A processor for implementing the steps of the firmware code coverage statistics method of any of claims 1 to 8 when executing said computer program.
Priority Applications (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202511232544.5A CN120743789B (en) | 2025-08-29 | 2025-08-29 | Firmware code coverage rate statistical method and electronic equipment |
Applications Claiming Priority (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202511232544.5A CN120743789B (en) | 2025-08-29 | 2025-08-29 | Firmware code coverage rate statistical method and electronic equipment |
Publications (2)
| Publication Number | Publication Date |
|---|---|
| CN120743789A CN120743789A (en) | 2025-10-03 |
| CN120743789B true CN120743789B (en) | 2025-11-25 |
Family
ID=97190742
Family Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| CN202511232544.5A Active CN120743789B (en) | 2025-08-29 | 2025-08-29 | Firmware code coverage rate statistical method and electronic equipment |
Country Status (1)
| Country | Link |
|---|---|
| CN (1) | CN120743789B (en) |
Citations (2)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN113791986A (en) * | 2021-11-18 | 2021-12-14 | 云账户技术(天津)有限公司 | Statistical method and device for code coverage rate |
| CN115203004A (en) * | 2021-04-08 | 2022-10-18 | 北京字节跳动网络技术有限公司 | Code coverage test method, device, storage medium and electronic device |
Family Cites Families (6)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US20090287729A1 (en) * | 2008-05-16 | 2009-11-19 | Microsoft Corporation | Source code coverage testing |
| CN104123218B (en) * | 2013-04-23 | 2015-06-24 | 腾讯科技(深圳)有限公司 | Method, device and system for code coverage test |
| CN107391375A (en) * | 2017-07-25 | 2017-11-24 | 北京计算机技术及应用研究所 | Perform the method for obtaining sentence and branch covering rate during C25 assembler languages |
| CN110389764A (en) * | 2019-06-19 | 2019-10-29 | 平安普惠企业管理有限公司 | Useless code cleaning method, equipment, storage medium and device |
| CN115168229B (en) * | 2022-07-27 | 2025-07-04 | 北京计算机技术及应用研究所 | A coverage-driven embedded software closed-loop testing platform and method |
| CN119322747B (en) * | 2024-09-24 | 2025-10-31 | 山东云海国创云计算装备产业创新中心有限公司 | Code coverage rate determining method, device, computer equipment and storage medium |
-
2025
- 2025-08-29 CN CN202511232544.5A patent/CN120743789B/en active Active
Patent Citations (2)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN115203004A (en) * | 2021-04-08 | 2022-10-18 | 北京字节跳动网络技术有限公司 | Code coverage test method, device, storage medium and electronic device |
| CN113791986A (en) * | 2021-11-18 | 2021-12-14 | 云账户技术(天津)有限公司 | Statistical method and device for code coverage rate |
Also Published As
| Publication number | Publication date |
|---|---|
| CN120743789A (en) | 2025-10-03 |
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| US9632916B2 (en) | Method and apparatus to semantically connect independent build and test processes | |
| US9448916B2 (en) | Software test automation systems and methods | |
| Acharya et al. | Mining API error-handling specifications from source code | |
| CN101739339A (en) | Program dynamic dependency relation-based software fault positioning method | |
| EP4471602B1 (en) | Software log processing method | |
| Strandberg et al. | Intermittently failing tests in the embedded systems domain | |
| CN119441052A (en) | A method for analyzing front-end page coverage based on code instrumentation | |
| Di Nardo et al. | Generating complex and faulty test data through model-based mutation analysis | |
| CN113805928B (en) | Hot patch file generation, consistency testing methods, apparatus, equipment and media | |
| CN120743789B (en) | Firmware code coverage rate statistical method and electronic equipment | |
| Murtaza et al. | An empirical study on the use of mutant traces for diagnosis of faults in deployed systems | |
| KR102779315B1 (en) | Debugging system and method with improved exploitability detection performance through binary analysis | |
| Singhal et al. | A critical review of various testing techniques in aspect-oriented software systems | |
| CN113849814B (en) | A configurable system vulnerability reproduction system and reproduction method | |
| Tonella et al. | Reverse engineering 4.7 million lines of code | |
| CN115080426A (en) | Program file detection method and device, storage medium and electronic equipment | |
| CN107145422B (en) | A software fault alarm monitoring method | |
| Wu et al. | Toward understanding bugs in swift programming language | |
| CN119988179B (en) | Method and device for detecting bit shift of key function code segment of relay protection device | |
| CN120469845B (en) | Data processing method, device, server and storage medium for solid state hard drive failure | |
| CN120670327B (en) | A method for processing automatic verification chip interruption | |
| CN120523743B (en) | Logic driving test case generation method based on large language model | |
| CN120407254A (en) | A game script data processing method | |
| CN120872800A (en) | A Method and System for Monitoring and Adjusting Software Unit Execution Flow Based on AST | |
| Tellefsen | An Examination of Issues with Exception Handling Mechanisms |
Legal Events
| Date | Code | Title | Description |
|---|---|---|---|
| PB01 | Publication | ||
| PB01 | Publication | ||
| SE01 | Entry into force of request for substantive examination | ||
| SE01 | Entry into force of request for substantive examination | ||
| GR01 | Patent grant |