Disclosure of Invention
In view of this, embodiments of the present invention provide a method for detecting an uninitialized variable and a terminal device, which can implement rapid detection of an uninitialized global variable in embedded software by using a script automatic detection method.
A first aspect of an embodiment of the present invention provides a method for detecting an uninitialized variable, including:
resetting the tested system to enable a pointer to point to a main function of the tested system;
filling specified data in a memory segment for storing global variables in the tested system;
enabling a pointer to run to a function position where variable initialization is finished, reading the memory segment data, and obtaining the total number of memory units corresponding to the name of the global variable and the total number of units of the specified data;
and when the total number of the memory units corresponding to the names of the global variables is the same as the total number of the units of the designated data, determining that the global variables are uninitialized global variables.
In an embodiment, before the resetting the system under test and pointing a pointer to the main function of the system under test, the method further includes:
the integrated development environment IDE of the embedded processor is provided with a debugging server script DSS toolkit;
invoking execution of the DSS toolkit;
the resetting the system under test to make the pointer point to the main function of the system under test includes:
and calling a script debugging reset instruction, resetting the tested system and enabling the pointer to point to the main function of the tested system.
In an embodiment, filling specified data in a memory segment storing a global variable in the system under test includes:
and calling a script debugging memory filling instruction, and filling specified data in all memory units in the RAM of the ebss section in the system to be tested.
In an embodiment, after the invoking script debugs the memory filling instruction, in the system under test, after all the memory units in the RAM of the ebss section are filled with the specified data, the method further includes:
calling a script debugging breakpoint instruction, and setting a breakpoint in a statement behind an initialization function in the tested software system;
and calling a script debugging full-speed running instruction, and running the tested software system to a breakpoint statement at full speed.
In an embodiment, the reading the memory segment, and obtaining the total number of memory cells corresponding to the name of the global variable and the total number of cells of the specified data includes:
calling a script debugging memory reading instruction, and reading all specified data of the memory unit of the ebss section RAM;
calling a script debugging variable reading instruction, and reading the name of the global variable corresponding to the ebss section address;
and when the name of the global variable is acquired, acquiring the total number of memory units corresponding to the name of the global variable accumulated by a first counter, and acquiring the total number of units of the specified data accumulated by a second counter.
In an embodiment, when the total number of memory cells corresponding to the name of the global variable is the same as the total number of cells of the designated data, determining that the global variable is an uninitialized global variable includes:
detecting whether the name of the next global variable is read or not;
when the name of the next global variable is read, detecting whether the total number of memory units corresponding to the name of the global variable is the same as the total number of units of the specified data or not;
and when the total number of the memory units corresponding to the names of the global variables is the same as the total number of the units of the specified data, determining that the global variables corresponding to the currently detected memory area are uninitialized global variables.
In an embodiment, the method further comprises:
when the global variable corresponding to the currently detected memory area is determined to be an uninitialized global variable, sending the uninitialized global variable to a control unit corresponding to a control window, and assigning the first counter and the second counter to be 0;
when the name of the next global variable is not read, or when the total number of memory units corresponding to the name of the global variable is different from the total number of units of the specified data, or after the first counter and the second counter are assigned to be 0, whether the address of the ebss section is the last unit is detected;
and when the address of the ebss section is the last unit, calling a script debugging reset instruction to reset the tested system.
A second aspect of an embodiment of the present invention provides an apparatus for detecting an uninitialized variable, including:
the reset module is used for resetting the tested system to enable the pointer to point to the main function of the tested system;
the filling module is used for filling specified data in a memory segment for storing the global variable in the tested system;
the reading module is used for enabling the pointer to run to a function position where the variable initialization is finished, reading the memory segment data, and acquiring the total number of memory units corresponding to the name of the global variable and the total number of units of the specified data;
and the determining module is used for determining the global variable as the uninitialized global variable when the total number of the memory units corresponding to the names of the global variable is the same as the total number of the units of the specified data.
A third aspect of the embodiments of the present invention provides a terminal device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, where the processor implements the steps of the method for detecting uninitialized variables provided in the above embodiments when executing the computer program.
A fourth aspect of embodiments of the present invention provides a computer-readable storage medium storing a computer program which, when executed by a processor, implements the steps of the method for detecting uninitialized variables provided in the above-described embodiments.
Compared with the prior art, the embodiment of the invention has the following beneficial effects: the embodiment of the invention leads the pointer to point to the main function of the system to be tested by resetting the system to be tested; filling specified data in a memory segment for storing global variables in the tested system; enabling a pointer to run to a function position where the variable initialization is finished, reading the data of the memory segment, and acquiring the total number of memory units corresponding to the name of the global variable and the total number of units of the specified data; when the total number of the memory units corresponding to the names of the global variables is the same as the total number of the units of the designated data, the global variables are determined to be uninitialized global variables, and the uninitialized global variables in the embedded software can be quickly detected. And the whole detection process does not need to additionally occupy chip resources, and the detection result is accurate and fast.
Detailed Description
In the following description, for purposes of explanation and not limitation, specific details are set forth, such as particular system structures, techniques, etc. in order to provide a thorough understanding of the embodiments of the invention. It will be apparent, however, to one skilled in the art that the present invention may be practiced in other embodiments that depart from these specific details. In other instances, detailed descriptions of well-known systems, devices, circuits, and methods are omitted so as not to obscure the description of the present invention with unnecessary detail.
In order to explain the technical means of the present invention, the following description will be given by way of specific examples.
An embodiment of the present invention provides a method for detecting an uninitialized variable, as shown in fig. 1, where the method includes the following steps:
step 101, resetting the system to be tested, and enabling a pointer to point to a main function of the system to be tested.
Optionally, before resetting the system under test, a Debug Server Script (DSS) toolkit is introduced, a script environment object is created, the Debug Server is acquired, and a Debug Server session is started. And automatically calling a script debugging reset instruction, and resetting the tested system to enable the pointer to point to the main function of the tested system.
The execution subject of the method may be a computer terminal, and may be a computer-readable storage medium, and the method is not limited to the execution subject, and is described above only as an example.
Optionally, the Test carrier required by the method is an Integrated Development Environment (IDE) of an embedded chip minimum system board and an embedded processor, and the two are connected by using a Joint Test Action Group (JTAG) and perform online simulation. The software development environment has a DSS functional interface, and the script language generally supports high-level languages such as Java or JavaScript, Perl, Python, TCL and the like. In a software development environment, the DSS toolkit can replace manual operation by using script instructions, such as functions of software resetting, breakpoint setting, full-speed operation, single-step operation, memory filling, memory reading, reading of variables corresponding to memory addresses and the like. Therefore, the automatic test can be realized in the minimal system board simulation environment by compiling the script program in a certain logic sequence. For example, in this step, a script debugging reset instruction is automatically called to reset the system under test, so that the pointer points to the main function of the system under test.
And 102, filling specified data in a memory segment storing the global variable in the tested system.
And automatically calling a script debugging memory filling instruction, and filling specified data in all memory units in a Random Access Memory (RAM) of the ebss section in the tested system.
Alternatively, the specified data may be 0x55AA or the like. Ebss segments in embedded software are used for storing global variables.
Optionally, the method further includes automatically calling a script debugging breakpoint instruction after the step, and setting a breakpoint in a statement after an initialization function in the tested software system;
and automatically calling a script to debug the full-speed running instruction, and running the tested software system to the breakpoint statement at full speed.
And 103, operating the pointer to a function position where the variable initialization is completed, reading the memory segment data, and acquiring the total number of memory units corresponding to the name of the global variable and the total number of units of the specified data.
Optionally, the present step includes: automatically calling a script to debug a memory reading instruction, and reading all specified data corresponding to all memory units of the ebss section RAM; automatically calling a script debugging variable reading instruction, and reading the name of the global variable corresponding to the ebss section address;
when the name of the global variable is obtained, obtaining the total number of memory cells corresponding to the name of the global variable accumulated by a first counter, and obtaining the total number of cells of the specified data accumulated by a second counter, optionally, Var _ Cnt may be used to represent the total number of memory cells corresponding to the name of the global variable, and Fill _ Cnt may be used to represent the total number of cells of the specified data;
and step 104, when the total number of the memory units corresponding to the names of the global variables is the same as the total number of the units of the designated data, determining that the global variables are uninitialized global variables.
Optionally, when Fill _ Cnt is equal to Var _ Cnt, it is determined that the global variable corresponding to the currently detected memory area is an uninitialized global variable, then the uninitialized global variable is sent to the control unit corresponding to the control window, and the first counter and the second counter are assigned as preset values, where the preset value may be 0. Thus, all uninitialized global variables can be obtained in the control window, and the global variables can be accurately positioned.
According to the method for detecting the uninitialized variables, all memory units of the ebss section RAM in the detected software system are filled with the specified data (0x55AA), the total number of the memory units corresponding to the names of the global variables and the total number of the filled specified data units are obtained, and when the total numbers of the memory units and the filled specified data units are the same, the currently detected global variables can be determined to be the uninitialized global variables. The whole detection process has the characteristics of simple operation, no need of additional temporary chip resources, accurate detection result, high speed and the like.
An embodiment of the present invention provides another method for detecting an uninitialized variable, as shown in fig. 2, the method includes:
step 201, an integrated development environment IDE of an embedded processor is provided with a debug server script DSS toolkit.
Optionally, the test carrier required by the method is an embedded chip minimum system board and an integrated development environment IDE of the embedded processor, and the embedded chip minimum system board and the embedded processor are connected by using a simulator JTAG and are subjected to online simulation. The integrated development environment IDE of the embedded processor has a DSS functional interface, and the script language generally supports high-level languages such as Java or JavaScript, Perl, Python, TCL and the like. In a software development environment, the DSS toolkit can replace manual operation by using script instructions, such as functions of software resetting, breakpoint setting, full-speed operation, single-step operation, memory filling, memory reading, reading of variables corresponding to memory addresses and the like. Therefore, the automatic test can be realized in the minimal system board simulation environment by compiling the script program in a certain logic sequence. For example, in this step, a script debugging reset instruction is automatically called to reset the system under test, so that the pointer points to the main function of the system under test.
Optionally, a DSS toolkit is imported, and a script environment object is created.
Step 202, invoking execution of the DSS toolkit.
Optionally, a script of the debugging server is obtained, and a dialogue of the debugging server is started.
Step 203, calling a script debugging reset instruction, and resetting the tested system to enable the pointer to point to the main function of the tested system.
When the DSS toolkit is operated in the step, a script debugging reset instruction can be automatically called.
And step 204, calling a script debugging memory filling instruction, and filling specified data in all memory units in the RAM of the ebss section in the system to be tested.
Optionally, the script is automatically called to debug the memory filling instruction, and all memory units of the RAM of the ebss section in the system to be tested are filled with the specified data. Alternatively, the specified data may be 0x55 AA. For embedded software, the ebss segment is used for storing global variables.
Step 205, a script debugging breakpoint instruction is called, and a breakpoint is set in a statement behind an initialization function in the tested software system.
The breakpoint is set to terminate the test program.
And step 206, calling a script debugging full-speed running instruction, and running the tested software system to a breakpoint statement at full speed.
And step 207, calling a script debugging memory reading instruction, and reading all specified data of the memory unit of the ebss section RAM.
This step is to read all the specified data filled in the ebss section RAM.
And 208, calling a script debugging variable reading instruction, and reading the name of the global variable corresponding to the ebss section address.
Step 209, when the name of the global variable is obtained, obtaining the total number of the memory cells corresponding to the name of the global variable accumulated by the first counter, and obtaining the total number of the cells of the specified data accumulated by the second counter.
It should be noted that the first counter and the second counter are only named for distinguishing the counters, and the counters are not functionally ranked.
Step 210, detecting whether the name of the next global variable is read.
In step 211, when the name of the next global variable is read, it is detected whether the total number of the memory cells corresponding to the name of the global variable is the same as the total number of the cells of the specified data.
Optionally, it is determined whether Fill _ Cnt is equal to Var _ Cnt.
Step 212, when the total number of memory cells corresponding to the names of the global variables is the same as the total number of cells of the specified data, determining that the global variables corresponding to the currently detected memory area are uninitialized global variables.
And step 213, sending the uninitialized global variable to a control unit corresponding to the control window.
Optionally, in this step, the determined uninitialized global variables are displayed in a control window so as to locate each global variable, and then the two counters are cleared.
Step 214, assigning the first counter and the second counter to 0.
It should be noted that, after the step 211 is executed, when the total number of the memory cells corresponding to the name of the global variable is different from the total number of the cells of the designated data, the step 214 is executed.
Step 215, read the next address of the ebss segment.
Optionally, after step 210 is executed, when the name of the next global variable is not read, step 215 is executed, that is, the next address of the ebss segment is read, and step 216 is further executed.
Step 216, detecting whether the address of the ebss segment is the last unit.
And step 217, when the address of the ebss section is the last unit, calling a script debugging reset instruction to reset the tested system.
When the address of the ebss segment is not the last element, step 208 is performed.
The embodiment of the invention provides a method for detecting uninitialized variables, which can realize the rapid positioning of uninitialized global variable positions in embedded software by adopting a DSS kit for automatic detection, thereby reducing software bugs and saving the debugging time of software developers.
An embodiment of the present invention provides a device for detecting an uninitialized variable, as shown in fig. 3, the device includes: the device comprises a reset module 301, a filling module 302, a reading module 303 and a determining module 304.
The resetting module 301 is configured to reset the system under test, so that the pointer points to the main function of the system under test.
Optionally, the apparatus further includes a processing module, configured to receive and install a debugging server script DSS toolkit, and execute the DSS toolkit.
The reset module 301 is configured to call a script debugging reset instruction, and reset the system under test, so that the pointer points to the system main function.
A filling module 302, configured to fill the specified data in the memory segment storing the global variable in the system under test.
Optionally, the filling module 302 is configured to invoke a script debug memory filling instruction, and fill the specified data in all memory units in the RAM of the ebss segment in the system under test.
Between the filling module 302 and the reading module 303, a calling module may be further included, configured to call a script debugging breakpoint instruction, and set a breakpoint in a statement following an initialization function in the software system under test; and calling a script debugging full-speed running instruction, and running the tested software system to a breakpoint statement at full speed.
A reading module 303, configured to enable a pointer to run to a function position where variable initialization is completed, read the memory segment data, and obtain a total number of memory cells corresponding to the name of the global variable and a total number of cells of the specified data.
Optionally, when the pointer is operated to the function position where the variable initialization is completed, the reading module 303 is configured to call a script debugging memory reading instruction, and read all specified data of the ebss-segment RAM memory unit; calling a script debugging variable reading instruction, and reading the name of the global variable corresponding to the ebss section address; and when the name of the global variable is acquired, acquiring the total number of memory units corresponding to the name of the global variable accumulated by a first counter, and acquiring the total number of units of the specified data accumulated by a second counter.
When the total number of memory cells corresponding to the name of the global variable is the same as the total number of cells of the designated data, the determining module 304 is configured to determine that the global variable is an uninitialized global variable.
Optionally, the determining module 304 is configured to detect whether a name of a next global variable is read;
when the name of the next global variable is read, detecting whether the total number of memory units corresponding to the name of the global variable is the same as the total number of units of the specified data or not;
and when the total number of the memory units corresponding to the names of the global variables is the same as the total number of the units of the specified data, determining that the global variables corresponding to the currently detected memory area are uninitialized global variables.
Further, the apparatus is further configured to: when the global variable corresponding to the currently detected memory area is determined to be an uninitialized global variable, sending the uninitialized global variable to a control unit corresponding to a control window, and assigning the first counter and the second counter to be 0;
when the name of the next global variable is not read, or when the total number of memory units corresponding to the name of the global variable is different from the total number of units of the specified data, or after the first counter and the second counter are assigned to be 0, whether the address of the ebss section is the last unit is detected, and when the address of the ebss section is the last unit, a script debugging reset instruction is called to reset the tested system.
According to the device for detecting the uninitialized variables, all memory units of the ebss section RAM in the detected software system are filled with the specified data (0x55AA) through the filling module, the reading module obtains the total number of the memory units corresponding to the names of the global variables and the total number of the filled specified data units, and when the total numbers of the memory units and the filled specified data units are the same, the determining module can determine that the currently detected global variables are the uninitialized global variables. The whole detection process has the characteristics of simple operation, no need of additional temporary chip resources, accurate detection result, high speed and the like.
It should be understood that, the sequence numbers of the steps in the foregoing embodiments do not imply an execution sequence, and the execution sequence of each process should be determined by its function and inherent logic, and should not constitute any limitation to the implementation process of the embodiments of the present invention.
Fig. 4 is a schematic diagram of a terminal device according to an embodiment of the present invention. As shown in fig. 4, the terminal device 4 of this embodiment includes: a processor 401, a memory 402 and a computer program 403, such as a detection program for uninitialized variables, stored in said memory 402 and operable on said processor 401. The processor 401 implements the steps in the above-mentioned embodiments of the method for detecting an uninitialized variable, such as steps 101 to 104 shown in fig. 1 or steps 201 to 217 shown in fig. 2, when executing the computer program 403, and the processor 401 implements the functions of the modules in the above-mentioned embodiments of the apparatus, such as the functions of the modules 301 to 304 shown in fig. 3, when executing the computer program 403.
Illustratively, the computer program 403 may be partitioned into one or more modules that are stored in the memory 402 and executed by the processor 401 to implement the present invention. The one or more modules may be a series of computer program instruction segments capable of performing specific functions, which are used to describe the execution process of the computer program 403 in the detection device for uninitialized variables or terminal equipment 4. For example, the computer program 403 may be divided into a reset module 301, a fill module 302, a read module 303, and a determination module 304, and specific functions of the modules are shown in fig. 3, which is not described herein again.
The terminal device 4 may be a desktop computer, a notebook, a palm computer, a cloud server, or other computing devices. The terminal device may include, but is not limited to, a processor 401, a memory 402. Those skilled in the art will appreciate that fig. 4 is merely an example of a terminal device 4 and does not constitute a limitation of terminal device 4 and may include more or fewer components than shown, or some components may be combined, or different components, e.g., the terminal device may also include input-output devices, network access devices, buses, etc.
The Processor 401 may be a Central Processing Unit (CPU), other general purpose Processor, a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), an off-the-shelf Programmable Gate Array (FPGA) or other Programmable logic device, discrete Gate or transistor logic, discrete hardware components, etc. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like.
The storage 402 may be an internal storage unit of the terminal device 4, such as a hard disk or a memory of the terminal device 4. The memory 402 may also be an external storage device of the terminal device 4, such as a plug-in hard disk, a Smart Media Card (SMC), a Secure Digital (SD) Card, a Flash memory Card (Flash Card), and the like, which are provided on the terminal device 4. Further, the memory 402 may also include both an internal storage unit and an external storage device of the terminal device 4. The memory 402 is used for storing the computer programs and other programs and data required by the terminal device 4. The memory 402 may also be used to temporarily store data that has been output or is to be output.
It will be apparent to those skilled in the art that, for convenience and brevity of description, only the above-mentioned division of the functional units and modules is illustrated, and in practical applications, the above-mentioned function distribution may be performed by different functional units and modules according to needs, that is, the internal structure of the apparatus is divided into different functional units or modules to perform all or part of the above-mentioned functions. Each functional unit and module in the embodiments may be integrated in one processing unit, or each unit may exist alone physically, or two or more units are integrated in one unit, and the integrated unit may be implemented in a form of hardware, or in a form of software functional unit. In addition, specific names of the functional units and modules are only for convenience of distinguishing from each other, and are not used for limiting the protection scope of the present application. The specific working processes of the units and modules in the system may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
In the above embodiments, the descriptions of the respective embodiments have respective emphasis, and reference may be made to the related descriptions of other embodiments for parts that are not described or illustrated in a certain embodiment.
Those of ordinary skill in the art will appreciate that the various illustrative elements and algorithm steps described in connection with the embodiments disclosed herein may be implemented as electronic hardware or combinations of computer software and electronic hardware. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the implementation. 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 invention.
In the embodiments provided in the present invention, it should be understood that the disclosed apparatus/terminal device and method may be implemented in other ways. For example, the above-described embodiments of the apparatus/terminal device are merely illustrative, and for example, the division of the modules or units is only one logical division, and there may be other divisions when actually implemented, for example, a plurality of units or components may be combined or integrated into another system, or some features may be omitted, or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, devices or units, and may be in an electrical, mechanical or other form.
The units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiment.
In addition, functional units in the embodiments of the present invention may be integrated into one processing unit, or each unit may exist alone physically, or two or more units are integrated into one unit. The integrated unit can be realized in a form of hardware, and can also be realized in a form of a software functional unit.
The integrated modules/units, if implemented in the form of software functional units and sold or used as separate products, may be stored in a computer readable storage medium. Based on such understanding, all or part of the flow of the method according to the embodiments of the present invention may also be implemented by a computer program, which may be stored in a computer-readable storage medium, and when the computer program is executed by a processor, the steps of the method embodiments may be implemented. . Wherein the computer program comprises computer program code, which may be in the form of source code, object code, an executable file or some intermediate form, etc. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, U.S. disk, removable hard disk, magnetic diskette, optical disk, computer Memory, Read-Only Memory (ROM), Random Access Memory (RAM), electrical carrier wave signal, telecommunications signal, and software distribution medium, etc. It should be noted that the computer readable medium may contain content that is subject to appropriate increase or decrease as required by legislation and patent practice in jurisdictions, for example, in some jurisdictions, computer readable media does not include electrical carrier signals and telecommunications signals as is required by legislation and patent practice.
The above-mentioned embodiments are only used for illustrating the technical solutions of the present invention, and not for limiting the same; although the present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; such modifications and substitutions do not substantially depart from the spirit and scope of the embodiments of the present invention, and are intended to be included within the scope of the present invention.