Method for analyzing homonymous variables in C language function stack

By analyzing the variables with the same name in the C function stack, the debugger is used to cache the DWARF debugging information, determine the variable location and read the memory value, solving the problem of inaccurate value of the variables with the same name, and improving the development and debugging efficiency.

CN120256280APending Publication Date: 2025-07-04安徽芯纪元科技有限公司
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510360217.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-03-25
Publication Date
2025-07-04

AI Technical Summary

Technical Problem

The existing technology cannot accurately obtain the value of the same name in different locations in the C function stack, resulting in inconvenient development and debugging.

Method used

Read the debugging information of the executable file through the debugger, cache DWARF debugging information, parse the variable name and location, compare the PC scope and scope, determine the location of the target variable and read the memory value to return.

Benefits of technology

It realizes the accurate display of the value of the specified variable of the same name in the visual development environment, improving the development and debugging efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120256280A_ABST
    Figure CN120256280A_ABST
Patent Text Reader

Abstract

The invention relates to homonymous variable analysis, in particular to a method for analyzing homonymous variables in a C language function stack, which comprises the following steps of: receiving a variable viewing command by a debugger, and analyzing a variable name and a variable position from the variable viewing command; the debugger analyzes and obtains all homonymous variables and corresponding action ranges in the function stack from the debugging information of the executable file according to the variable name, and analyzes and obtains a corresponding PC range from the debugging information of the executable file according to the variable position; comparing the PC range with the action range of each homonymous variable, wherein the homonymous variable corresponding to the minimum action range of the PC range is a target variable specified to be checked; according to the inquired variable information, determining a physical position of the target variable in the function stack, reading a memory value of the physical position, and taking the memory value as a variable value specified to be checked and returning the variable value; according to the technical scheme provided by the invention, the defect that the values of the homonymous variables at different positions in the function stack cannot be accurately obtained can be effectively overcome.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the resolution of homonymous variables, and more particularly to a method for resolving homonymous variables within the stack of a C language function. Background Art

[0002] During the process of embedded software development, system software and hardware debugging is an important part. An efficient and powerful debugging system can greatly shorten the software development cycle and enhance the competitiveness of products. Embedded software development based on the C language, as the most widely used programming language, has become the primary choice for embedded software developers.

[0003] During the C language source code-level debugging process, viewing the value of the current variable through a floating mouse pointer or a variable viewing window in the visual development environment interface is a relatively common and convenient debugging method. When performing embedded source code debugging, first, the host debugger loads the compiled application program executable file into the target device, reads the debugging information in the executable file, and establishes a connection between the source code and the executable file. Secondly, when performing different debugging operations, the debugger, according to different debugging commands, feeds back the status information of the executable file in the target device as a result to the developer. For example, during development and debugging, when viewing variables through the visual development environment, whether it is a floating mouse pointer or a variable window display, the underlying execution operations are the same. That is, the visual development environment sends a variable viewing command to the debugger, the debugger identifies the variable name, obtains the physical location of the variable in the function stack through the variable name, and finally returns the memory cell value at the corresponding location to the visual development environment.

[0004] During the process of C language application development and debugging, homonymous variables can be defined in different statement blocks within a function. Inside the C language function body, homonymous variables can be defined by using the "{}" statement block method, that is, outside and inside the same "{}" statement block or in different "{}" statement blocks, variables with the same name and the same type are declared and defined. Since the positions of the above-mentioned homonymous variables are in different statement blocks, the scopes of the homonymous variables are different.

[0005] To ensure that different variables can be distinguished during application development and debugging, corresponding scope ranges will be generated for variables in different statement blocks in the generated debugging information. In DWARF debugging information, the debugging information entry DW_TAG_lexical_block is used to describe the scope of variables for the debugger to read. During the process of parsing the debugging information entry DW_TAG_lexical_block, the debugging information entry DW_TAG_lexical_block serves as the parent node to describe the scope of its child nodes, where the child nodes are generally variables within the function stack. When the declaration and definition of a variable are in the outermost "{}" statement block of the function body, the scope of the variable is the entire function execution body, and no debugging information entry DW_TAG_lexical_block will be generated in the debugging information to describe the scope of the variable.

[0006] When developers view variables with the same name using a visual development environment, first, the debugger receives the variable viewing command and obtains the name of the function where the current PC information of the program execution is located. Second, it enumerates all variables with the same name in the function stack according to the variable name, and returns the value of the memory unit corresponding to the variable with the same name into which the current PC value falls to the visual development environment for display.

[0007] The above method for viewing variables with the same name can display the variable information within the scope of the variable with the same name at the current execution position of the program. However, since it only makes a judgment and display based on the current PC information, there are certain limitations. During the process of application development and debugging based on a visual development environment, when the PC executes inside the function body, when developers view variables through the visual development environment, the variable viewing command sent to the debugger only contains the variable name information and no variable position information. This will cause all variables with the same name at different positions within the function to be displayed as the same value, that is, the value of the variable with the same name corresponding to the scope into which the current PC value falls. In this way, the value of the specified variable with the same name cannot be accurately displayed, causing great inconvenience to the development and debugging work of developers.

[0008] In summary, in existing debuggers, when obtaining the values of variables with the same name in the function stack, the variable position information cannot be specified, and the variables with the same name can only be displayed based on the current PC value. Variables with the same name at different positions are all displayed as the same value. Therefore, there are still significant defects in the existing technical solutions for the display of variables with the same name, which have a certain impact on the development and debugging work of developers. Summary of the Invention

[0009] (1) Technical problems to be solved

[0010] In view of the above disadvantages of the prior art, the present invention provides a method for parsing variables with the same name in the stack of a C language function, which can effectively overcome the defect of the prior art that the values of variables with the same name at different positions in the function stack cannot be accurately obtained.

[0011] (II) Technical Solution

[0012] To achieve the above objectives, the present invention is realized through the following technical solutions:

[0013] A method for parsing variables with the same name in the stack of a C language function includes the following steps:

[0014] S1. The debugger reads the debug information of the executable file and caches it;

[0015] S2. The debugger receives a variable viewing command and parses the variable name and variable position from the variable viewing command;

[0016] S3. The debugger parses and obtains all variables with the same name in the function stack and their corresponding scopes from the debug information of the executable file according to the variable name, and parses and obtains the corresponding PC range from the debug information of the executable file according to the variable position;

[0017] S4. Compare the PC range with the scope of each variable with the same name, and the variable with the same name corresponding to the smallest scope into which the PC range falls is the target variable to be viewed as specified;

[0018] S5. Determine the physical position of the target variable in the function stack according to the queried variable information, read the memory value at the physical position, and return the memory value as the variable value to be viewed as specified.

[0019] Preferably, in S1, the debugger reads the debug information of the executable file and caches it, including:

[0020] The debugger reads the DWARF debug information of the executable file, reads and analyzes the debug_info segment and the debug_line segment respectively and caches them;

[0021] Among them, the debug_info segment contains the main debug information of the source file, including source file information, function information and variable information; the debug_line segment contains the line debug information of the source file, recording the PC range corresponding to each line of source code.

[0022] Preferably, the debugger reads and analyzes the debug_info segment, including:

[0023] Read and analyze the debug information item DW_TAG_compile_unit to obtain the source file attributes and the source file PC range;

[0024] Analyze the function information: Read the debugging information item DW_TAG_subprogram of the function as the first parent node and analyze it to obtain the function name, return value type, and function PC range. Then read and analyze the child node information of the first parent node to obtain parameter information and variable information;

[0025] Analyze the variable information: Read the debugging information item DW_TAG_lexical_block of the variable as the second parent node and analyze it to obtain the scope of the child nodes of the second parent node. When the debugging information item DW_TAG_lexical_block of the variable does not exist, use the function PC range as the scope of the child nodes of the second parent node;

[0026] Among them, the source file attributes include the source file name and the source file path.

[0027] Preferably, the debugger reads and analyzes the debug_line segment, including:

[0028] Read the basic information of the debug_line segment and associate it with the source file corresponding to the debug_info segment;

[0029] Gradually analyze all the contents of the debug_line segment and parse out the PC range corresponding to each line of source code in the source file;

[0030] Among them, the basic information of the debug_line segment includes the source file name and the source file path corresponding to the line number.

[0031] Preferably, in S2, the debugger receives a variable viewing command and parses out the variable name and variable location from the variable viewing command, including:

[0032] Send a variable viewing command containing the specified variable name and variable location to the debugger through the visual development environment;

[0033] The debugger receives the variable viewing command, obtains the currently executing function and source file information based on the current execution PC, and compares it with the name filename of the source file where the specified variable to be viewed is located:

[0034] If the names are inconsistent, the specified variable to be viewed is not in the currently executing function and source file, and an error message indicating that the variable viewing fails is returned;

[0035] If the names are consistent, the specified variable to be viewed is in the currently executing function and source file, and parse out the specified variable name varname and the line number linenum of the source file where the specified variable to be viewed is located from the variable viewing command;

[0036] Among them, the debugger has a command-line interface that supports a variable viewing command including the variable name and variable location to be viewed, and the specified variable location to be viewed includes the name and line number of the source file where the variable to be viewed is located.

[0037] Preferably, the form of the variable viewing command is as follows:

[0038] varname:filename linenum

[0039] Among them, varname represents the variable name to be viewed, filename represents the name of the source file where the variable to be viewed is located, and linenum represents the line number of the source file where the variable to be viewed is located.

[0040] Preferably, in S3, the debugger parses and obtains all variables with the same name in the function stack and their corresponding scopes from the debug information of the executable file according to the variable name, and parses and obtains the corresponding PC range from the debug information of the executable file according to the variable location, including:

[0041] The debugger obtains all variables with the same name in the function stack and their corresponding scopes according to the variable name varname to be viewed, and obtains the corresponding PC range according to the line number linenum of the source file where the variable to be viewed is located.

[0042] (III) Beneficial effects

[0043] Compared with the prior art, a method for parsing variables with the same name in a C language function stack provided by the present invention can be used in cooperation with a visual development environment. When processing variables with the same name in the function stack, it can accurately return the variable value that the developer wants to view according to the variable name and variable location specified in the variable viewing command, effectively improving the efficiency of development and debugging. Description of the drawings

[0044] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, the drawings in the following description are only some embodiments of the present invention, and those of ordinary skill in the art can also obtain other drawings based on these drawings without creative efforts.

[0045] Figure 1 is a flow chart of the present invention;

[0046] Figure 2 is a flow chart of the debugger reading and analyzing the debug_info segment in the present invention;

[0047] Figure 3Schematic diagram of the debugger's reading and analysis of the debug_line segment in the present invention;

[0048] Figure 4 In the present invention Figure 1 Detailed process schematic diagram. Specific embodiments

[0049] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are some, but not all, of the embodiments of the present invention. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the protection scope of the present invention.

[0050] A method for parsing variables with the same name in the stack of C language functions, as Figure 1 shown, S1. The debugger reads the debug information of the executable file and caches it, specifically including:

[0051] The debugger reads the DWARF debug information of the executable file, reads and analyzes the debug_info segment and the debug_line segment respectively, and caches them;

[0052] Among them, the debug_info segment contains the main debug information of the source file, including source file information, function information, and variable information; the debug_line segment contains the line debug information of the source file, recording the PC range corresponding to each line of source code.

[0053] ① As Figure 2 shown, the debugger reads and analyzes the debug_info segment, including:

[0054] Read and analyze the debug information item DW_TAG_compile_unit to obtain the source file attributes and the source file PC range;

[0055] Analyze the function information: Read the debug information item DW_TAG_subprogram of the function as the first parent node and analyze it to obtain the function name, return value type, and function PC range, and then read and analyze the child node information of the first parent node to obtain the parameter information and variable information;

[0056] Analyze the variable information: Read the debug information item DW_TAG_lexical_block of the variable as the second parent node and analyze it to obtain the scope of the child nodes of the second parent node. When there is no debug information item DW_TAG_lexical_block of the variable, use the function PC range as the scope of the child nodes of the second parent node;

[0057] Among them, the source file attributes include the source file name and the source file path.

[0058] ② As Figure 3 shown, the debugger reads and analyzes the debug_line segment, including:

[0059] Reading the basic information of the debug_line segment and associating it with the source file corresponding to the debug_info segment;

[0060] Gradually analyzing all the contents of the debug_line segment and parsing out the PC range corresponding to each line of source code in the source file;

[0061] Among them, the basic information of the debug_line segment includes the source file name and the source file path corresponding to the line number.

[0062] S2. As Figure 4 shown, the debugger receives a variable viewing command and parses out the variable name and variable location from the variable viewing command, specifically including:

[0063] Sending a variable viewing command containing the specified variable name and variable location to the debugger through the visual development environment;

[0064] The debugger receives the variable viewing command, obtains the information of the currently executed function and source file according to the currently executed PC, and compares it with the name filename of the source file where the specified variable to be viewed is located:

[0065] If the names are inconsistent, the specified variable to be viewed is not in the currently executed function and source file, and an error message indicating that the variable viewing fails is returned;

[0066] If the names are consistent, the specified variable to be viewed is in the currently executed function and source file, and the specified variable name varname to be viewed and the line number linenum of the source file where the specified variable to be viewed is located are parsed out from the variable viewing command;

[0067] Among them, the debugger has a command-line interface that supports a variable viewing command containing the specified variable name and variable location, and the specified variable location includes the name and line number of the source file where the specified variable to be viewed is located.

[0068] In the technical solution of this application, the form of the variable viewing command is as follows:

[0069] varname:filename linenum

[0070] Among them, varname represents the name of the variable specified to be viewed, filename represents the name of the source file where the variable specified to be viewed is located, and linenum represents the line number of the source file where the variable specified to be viewed is located.

[0071] S3. As Figure 4 shown, the debugger parses and obtains all variables with the same name and their corresponding scopes in the function stack from the debug information of the executable file according to the variable name, and parses and obtains the corresponding PC range from the debug information of the executable file according to the variable position. Specifically, it includes:

[0072] The debugger obtains all variables with the same name and their corresponding scopes in the function stack according to the variable name varname specified to be viewed, and obtains the corresponding PC range according to the line number linenum of the source file where the variable specified to be viewed is located.

[0073] S4. Compare the PC range with the scope of each variable with the same name. The variable with the same name corresponding to the smallest scope into which the PC range falls is the target variable specified to be viewed.

[0074] S5. Determine the physical location of the target variable in the function stack according to the queried variable information, read the memory value at the physical location, and return the memory value as the value of the variable specified to be viewed.

[0075] The above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it; although the present invention has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that: they can still modify the technical solutions recorded in the foregoing embodiments, or perform equivalent replacements for some of the technical features; and these modifications or replacements will not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A method for parsing variables with the same name within a C language function stack, characterized in that: It includes the following steps: S1. The debugger reads the debugging information of the executable file and caches it; S2. The debugger receives a variable viewing command and parses out the variable name and variable location from the variable viewing command; S3. The debugger parses and obtains all variables with the same name and their corresponding scopes in the function stack from the debugging information of the executable file according to the variable name, and parses and obtains the corresponding PC range from the debugging information of the executable file according to the variable location; S4. Compare the PC range with the scope of each variable with the same name. The variable with the same name whose PC range falls within the smallest scope is the target variable to be viewed as specified; S5. According to the queried variable information, determine the physical location of the target variable in the function stack, read the memory value at the physical location, and return the memory value as the variable value to be viewed as specified.

2. The parsing method of variables with the same name in the C language function stack according to claim 1, characterized in that: In S1, the debugger reads the debugging information of the executable file and caches it, including: The debugger reads the DWARF debugging information of the executable file, reads and analyzes the debug_info segment and the debug_line segment respectively and caches them; Among them, the debug_info segment contains the main debugging information of the source file, including source file information, function information and variable information; the debug_line segment contains the line debugging information of the source file, recording the PC range corresponding to each line of source code.

3. The parsing method for variables with the same name in the C language function stack according to claim 2, characterized in that: The debugger reads and analyzes the debug_info segment, including: Read and analyze the debugging information item DW_TAG_compile_unit to obtain the source file attributes and the source file PC range; Analyze the function information: Read the debugging information item DW_TAG_subprogram of the function as the first parent node and analyze it to obtain the function name, return value type and function PC range, and then read and analyze the child node information of the first parent node to obtain the parameter information and variable information; Analyze the variable information: Read the debugging information item DW_TAG_lexical_block of the variable as the second parent node and analyze it to obtain the scope of the child nodes of the second parent node. When there is no debugging information item DW_TAG_lexical_block of the variable, use the function PC range as the scope of the child nodes of the second parent node; Among them, the source file attributes include the source file name and the source file path.

4. The method for parsing variables with the same name in the C language function stack according to claim 3, characterized in that: The debugger reads and analyzes the debug_line segment, including: Read the basic information of the debug_line segment and associate it with the corresponding source file in the debug_info segment; Gradually analyze all the contents of the debug_line segment and parse out the PC range corresponding to each line of source code in the source file; Among them, the basic information of the debug_line segment includes the source file name and the source file path corresponding to the line number.

5. The method for parsing variables with the same name in the C language function stack according to claim 2, characterized in that: In S2, the debugger receives a variable viewing command and parses out the variable name and variable location from the variable viewing command, including: Send a variable viewing command containing the variable name and variable location to be viewed as specified to the debugger through the visual development environment; The debugger receives a variable viewing command, obtains the information of the currently executing function and source file according to the currently executing PC, and compares it with the name filename of the source file where the variable to be viewed is specified: If the names are inconsistent, the variable to be viewed is not in the currently executing function and source file, and an error message indicating the failure of variable viewing is returned; If the names are consistent, the variable to be viewed is in the currently executing function and source file, and the name varname of the variable to be viewed and the line number linenum of the source file where the variable to be viewed is located are parsed from the variable viewing command; Among them, the debugger has a command-line interface that supports variable viewing commands containing the name of the variable to be viewed and the variable location, and the variable location to be viewed includes the name and line number of the source file where the variable to be viewed is located.

6. The method for parsing variables with the same name in the C language function stack according to claim 5, characterized in that: The form of the variable viewing command is as follows: varname:filename linenum Among them, varname represents the name of the variable to be viewed, filename represents the name of the source file where the variable to be viewed is located, and linenum represents the line number of the source file where the variable to be viewed is located.

7. The method for parsing variables with the same name in the C language function stack according to claim 5, characterized in that: In S3, the debugger parses and obtains all variables with the same name in the function stack and their corresponding scopes from the debug information of the executable file according to the variable name, and parses and obtains the corresponding PC range from the debug information of the executable file according to the variable location, including: The debugger obtains all variables with the same name in the function stack and their corresponding scopes according to the name varname of the variable to be viewed, and obtains the corresponding PC range according to the line number linenum of the source file where the variable to be viewed is located.