Debugging method, debugging device and debugging system for smart contracts
By adding interrupt processing functions and stack maintenance functions to the virtual machine, and combining debugging information and mapping information for smart contract debugging, the problems of low debugging efficiency and poor reliability in the existing technology are solved, and efficient and reliable smart contract debugging is achieved.
Patent Information
- Application Number
- CN202111655712.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-30
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2041-12-30
AI Technical Summary
The existing technology has low efficiency and poor reliability during smart contract debugging. The interpreter needs to provide an additional interpreter based on the debugging method, and it is difficult to maintain the mapping relationship between source code and machine code.
Obtain the bytecode program file of the smart contract through the virtual machine, add the interrupt processing function and stack maintenance function, generate the machine code program file, and combine the debugging information and mapping information for debugging, avoiding the dependence of the interpreter and the source code to the machine code Mapping is difficult.
It improves the efficiency and reliability of smart contract debugging, simplifies the debugging process, avoids the problem of inconsistent behavior between the interpreter and the virtual machine, and reduces the difficulty of maintenance.
Smart Images

Figure CN114327776B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of this specification belong to the field of blockchain technology, and more particularly to a debugging method, debugging device, and debugging system for smart contracts. Background Art
[0002] Blockchain technology is built on top of a transmission network (e.g., a peer-to-peer network). Network nodes in the transmission network use chain data structures to verify and store data, and use a distributed node consensus algorithm to generate and update data. Smart contracts have emerged in the blockchain 2.0 era, bringing the application scope of blockchain to a new level. With smart contracts, blockchain can no longer implement a single transfer transaction, but can also implement other user-defined functions. In the process of developing smart contracts, users usually need to debug smart contracts, so there is a need for reliable and efficient debugging methods for smart contracts. Summary of the invention
[0003] The object of the present invention is to provide a debugging method, a debugging device and a debugging system for smart contracts, so as to debug smart contracts reliably and efficiently.
[0004] According to a first aspect of one or more embodiments of this specification, a debugging method for a smart contract is provided, including:
[0005] The virtual machine obtains a first bytecode program file of the smart contract including debugging information, or the virtual machine obtains the first bytecode program file of the smart contract and a debugging information file including debugging information, wherein the debugging information includes mapping information between the source code and the bytecode of the smart contract and function list information;
[0006] Modifying the first bytecode program file in the virtual machine to generate a second bytecode program file including a first instruction for calling an interrupt processing function and a second instruction for calling a stack maintenance function, and modifying the mapping information accordingly, wherein the interrupt processing function is used to determine whether to interrupt the execution at the current execution position where the interrupt processing function is located, and the stack maintenance function is used to record the function call status at the current execution position where the stack maintenance function is located;
[0007] compiling the second bytecode program file in the virtual machine to generate a machine code program file; and
[0008] According to the breakpoint information and debugging commands obtained by the virtual machine, the machine code program file is executed in the virtual machine in a debugging state, and the smart contract is debugged in combination with the function list information and the mapping information.
[0009] According to a second aspect of one or more embodiments of the present specification, there is provided a debugging device for a smart contract, comprising a processor and a memory, wherein instructions are stored on the memory, and when the instructions are executed by the processor, the steps of the debugging method described above are implemented.
[0010] According to a third aspect of one or more embodiments of this specification, a debugging system for a smart contract is provided, including a virtual machine, wherein the virtual machine includes:
[0011] An acquisition module, wherein the acquisition module is configured to acquire a first bytecode program file of the smart contract including debugging information, or acquire the first bytecode program file of the smart contract and a debugging information file including debugging information, wherein the debugging information includes mapping information between the source code and the bytecode of the smart contract and function list information;
[0012] a modification module, the modification module being configured to modify the first bytecode program file to generate a second bytecode program file including a first instruction for calling an interrupt processing function and a second instruction for calling a stack maintenance function, and modify the mapping information accordingly, wherein the interrupt processing function is used to determine whether to interrupt the operation at the current operation position where the interrupt processing function is located, and the stack maintenance function is used to record the function call status at the current operation position where the stack maintenance function is located;
[0013] a compiling module, the compiling module being configured to compile the second bytecode program file in the virtual machine to generate a machine code program file; and
[0014] An execution module is configured to execute the machine code program file in a debugging state according to the breakpoint information and debugging commands obtained by the acquisition module, and debug the smart contract in combination with the function list information and the mapping information. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] In order to more clearly illustrate the technical solutions of the embodiments of this specification, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in this specification. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0016] Figure 1 is a flowchart of a debugging method for a smart contract in an embodiment of this specification;
[0017] Figure 2 is a flowchart of step S300 of a debugging method for a smart contract in an embodiment of this specification;
[0018] Figure 3 is a flowchart of step S700 of a debugging method for a smart contract in an embodiment of this specification;
[0019] Figure 4 is a flowchart of step S770 of the debugging method for a smart contract in an embodiment of this specification;
[0020] Figure 5 is a flowchart of step S770 of a debugging method for a smart contract in another embodiment of this specification;
[0021] Figure 6 is a block diagram of a debugging device for a smart contract in an embodiment of this specification;
[0022] Figure 7 It is a block diagram of a debugging system for smart contracts in one embodiment of this specification. DETAILED DESCRIPTION
[0023] In order to enable those skilled in the art to better understand the technical solutions in this specification, the technical solutions in the embodiments of this specification will be clearly and completely described below in conjunction with the drawings in the embodiments of this specification. Obviously, the described embodiments are only part of the embodiments of this specification, not all of the embodiments. Based on the embodiments in this specification, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of this specification.
[0024] Blockchain can generally be divided into three types: public blockchain, private blockchain and consortium blockchain. In addition, there are many types of combinations, such as private blockchain + consortium blockchain, consortium blockchain + public blockchain and other different combinations. Among them, the public chain has the highest degree of decentralization. Participants who join the public chain can read the data records on the chain, participate in transactions, and compete for the right to record new blocks. Moreover, each participant (i.e., node) can freely join and exit the network and perform related operations. On the contrary, the private chain has the write permission of the network controlled by a certain organization or institution, and the data reading permission is stipulated by the organization. In simple terms, the private chain can be a weakly centralized system with strictly limited and few participating nodes. This type of blockchain is more suitable for use within a specific organization. The consortium chain is a blockchain between the public chain and the private chain, which can achieve "partial decentralization". Each node in the consortium chain usually has a corresponding entity or organization; participants join the network through authorization and form a stakeholder alliance to jointly maintain the operation of the blockchain.
[0025] Whether it is a public chain, a private chain or a consortium chain, it may provide the function of smart contracts. Smart contracts on the blockchain are contracts that can be triggered and executed by transactions on the blockchain system. Smart contracts can be defined in the form of code. Smart contracts can be executed independently on each node in the blockchain network in a prescribed manner, and all execution records and data are stored on the blockchain. Therefore, when such a transaction is completed, the blockchain saves the transaction certificate that cannot be tampered with or lost.
[0026] In a blockchain that supports smart contracts (e.g., Ant Chain), users are supported to create and call some complex logic in the blockchain network. The core of a programmable blockchain is a virtual machine, and each blockchain node can run this virtual machine. The virtual machine can be Turing complete, which means that various complex logics can be implemented through it. Users publish and call smart contracts in the blockchain to run on the virtual machine. In fact, the virtual machine directly runs the virtual machine code (virtual machine bytecode, hereinafter referred to as "bytecode"). The smart contract deployed on the blockchain can be in the form of bytecode. In one or more embodiments of this specification, the virtual machine can be a WASM virtual machine, and accordingly, the bytecode program file involved in one or more embodiments of this specification (e.g., the first bytecode program file and the second bytecode program file described below) can be a WASM file. Among them, WASM is the abbreviation of WebAssembly, a low-level programming language close to the bottom layer of the computer, a portable binary format with high size and loading efficiency, and can be compiled to common platforms.
[0027] Bytecode consists of a series of bytes, each of which can identify an operation. Based on development efficiency, readability and other considerations, developers can choose a high-level language to write smart contract code instead of writing bytecode directly. Smart contract code written in a high-level language is compiled by a high-level language compiler to generate bytecode, which can then be deployed on the blockchain.
[0028] Before running the bytecode deployed on the blockchain, a bytecode compiler different from a high-level language compiler is required to compile the bytecode into a lower-level machine code. There are multiple running modes corresponding to different compilation methods, including interpreted running, just-in-time compilation (JIT) running, and static compilation (AOT) running. In interpreted running, the instructions in the bytecode program file can be interpreted and run one by one in turn, and its efficiency is usually very poor. In JIT running, the bytecode program file can be compiled into an executable program in the form of machine code, that is, a machine code program file, at runtime, and then the compiled machine code program file is executed. Just-in-time compilation can include the conversion of source code or bytecode to machine code. The system that implements just-in-time compilation can continuously analyze the code being executed and determine certain parts of the code where the performance improvement obtained by compiling or recompiling will be greater than the overhead of compiling the code. Compared with the operating efficiency of interpreted running, the operating efficiency of JIT running can be improved by orders of magnitude, and it may be close to the operating efficiency of the system's native executable program. In AOT operation, the bytecode program file can be compiled into a machine code program file before running, and then the compiled machine code program file can be executed, thereby avoiding the JIT compilation operation at run time. The efficiency of AOT operation can also be improved by an order of magnitude compared to the efficiency of interpreted operation, and can be close to the level of the system's native executable program.
[0029] In the process of developing smart contracts, debugging can be achieved through a debugger. A debugger refers to a computer program and tool used to debug other programs, which can check the running status of the code in the running environment and enable the code to be selectively run, so as to facilitate troubleshooting and adjustment. Accordingly, in the virtual machine, in addition to the program running thread that runs the smart contract itself, a debugger thread can also be included, in which operations associated with debugging the smart contract can be performed, as will be described in detail later.
[0030] A debugging method for smart contracts can be implemented based on an interpreter. Specifically, in the process of interpreting and running a bytecode program file, when each instruction in the bytecode program file is executed, it is possible to check whether there is a breakpoint at the corresponding position and view the relevant information at the breakpoint. However, this debugging method requires an interpreter to be provided additionally, and interpreter-based debugging is usually very inefficient. In addition, there may be inconsistencies in the behavior of the interpreter used to debug smart contracts and the virtual machine used to execute smart contracts, which will reduce the reliability of debugging.
[0031] In another debugging method for smart contracts, debugging can be performed directly based on the machine code program file generated after compiling the bytecode program file, for example, the machine code program file is modified accordingly to add breakpoints, and the relevant information at the breakpoints is viewed, etc. This debugging method can be highly efficient, but the mapping relationship between the source code and the machine code of the smart contract needs to be maintained during the debugging process, which is often difficult to implement.
[0032] In order to solve the above problems, one or more embodiments of this specification provide a debugging method for a smart contract.
[0033] like Figure 1 As shown, the debugging method may include:
[0034] Step S100, the virtual machine obtains the first bytecode program file of the smart contract including debugging information, or the virtual machine obtains the first bytecode program file of the smart contract and the debugging information file including debugging information.
[0035] Among them, the debugging information may include mapping information between the source code and bytecode of the smart contract and function list information. During the debugging process, the debugging information can be used to locate the corresponding position of the current running position in the source code of the smart contract, and help view the values of related variables in the source code form, etc. The debugging information or debugging information file may be generated by a high-level language compiler in the process of compiling the source code of the smart contract into bytecode. Commonly used debugging information file formats include dwarf, etc. Among them, the mapping information may include the mapping relationship between the line number of the source code of the smart contract and the line number of the corresponding bytecode, and the function list information may include the name of the function in the smart contract, the lexical scope of the function, the name of the variable, the data type of the variable, the lexical scope of the variable, the index of the variable, the name of the parameter, and the index of the parameter. It should be noted that in other embodiments, other information or data may also be included in the debugging information or debugging information file, which is not limited here.
[0036] like Figure 1 As shown, the debugging method may further include:
[0037] Step S300, modifying the first bytecode program file in the virtual machine to generate a second bytecode program file including a first instruction for calling an interrupt handling function and a second instruction for calling a stack maintenance function, and modifying the mapping information accordingly.
[0038] Among them, when the interrupt handling function is called, it can be determined whether to interrupt the operation at the current running position. The first instruction for calling the interrupt handling function can be added at the corresponding position in the second bytecode program file according to the debugging needs. When the stack maintenance function is called, the function call status at the current running position can be recorded in the function call stack, so as to perform positioning, viewing and other operations during the debugging process. Since the modification is made to the first bytecode program file, the source code of the smart contract does not include the modifications made for debugging as described in this article, so there is no need to modify the execution logic of the high-level language compiler or the bytecode compiler itself of the smart contract.
[0039] like Figure 2 As shown, in one or more embodiments of the present specification, modifying the first bytecode program file in the virtual machine to generate a second bytecode program file including a first instruction for calling an interrupt handling function and a second instruction for calling a stack maintenance function may include performing the following operations in a debugger thread of the virtual machine:
[0040] Step S310, dividing the first bytecode program file into one or more functions.
[0041] The first bytecode program file may include at least one main function. In most cases, the first bytecode program file may also include a plurality of internal functions in the main function. The division of the first bytecode program file may enable each instruction therein to be included in at least one function. In addition, according to the reference relationship between functions, a function tree corresponding to the first bytecode program file may be formed. In the function tree, other functions that reference a certain function may be referred to as the parent function of the function, and other functions referenced by a certain function may be referred to as the child function of the function.
[0042] like Figure 2 As shown, modifying the first bytecode program file in the virtual machine to generate a second bytecode program file including a first instruction for calling an interrupt handling function and a second instruction for calling a stack maintenance function may also include performing the following operations for each of the one or more functions in a debugger thread of the virtual machine:
[0043] Step S330, dividing the function into one or more basic blocks, so that each instruction in the function is included and only included in a corresponding one of the one or more basic blocks; and
[0044] Step S350: adding a first instruction to the header of each basic block in the function.
[0045] Each basic block may include one or more instructions, and once a basic block is executed, all instructions contained in the basic block are executed. In other words, a basic block can be regarded as an execution unit of a program file. Once the first instruction in a basic block is executed, the execution will be terminated only after all instructions in the basic block are executed, and only some instructions in the basic block are executed.
[0046] In one or more embodiments of the present specification, a function is divided into one or more basic blocks, so that each instruction in the function is included and only included in a corresponding one of the one or more basic blocks, which may include:
[0047] Find all non-linear execution instructions in the function; and
[0048] According to the non-linear operation instructions found, the function is divided into one or more basic blocks, wherein each non-linear operation instruction is the last instruction in the basic block where it is located.
[0049] Specifically, the non-linear operation instruction may include at least one of a jump instruction, a function call instruction, and a storage-type instruction that modifies a variable value. At the non-linear operation instruction, the program file is not executed sequentially, so these locations are often also locations where errors are prone to occur, and may need to be paid special attention to during the debugging process.
[0050] After completing the above operations for all functions, the first instruction for calling the interrupt processing function is added to the head of each basic block in the first bytecode program file. In this way, when the program runs to the head of each basic block, it can be determined whether to interrupt the running at the current running position for debugging. The user can select some or all of the positions to interrupt according to the debugging needs, which will be explained in detail later.
[0051] like Figure 2 As shown, modifying the first bytecode program file in the virtual machine to generate a second bytecode program file including a first instruction for calling an interrupt handling function and a second instruction for calling a stack maintenance function may also include performing the following operations in a debugger thread of the virtual machine:
[0052] Step S370: Add a second instruction before all entries and exits of each function in the one or more functions.
[0053] As mentioned above, the second instruction is used to call the stack maintenance function, which can record the current function call status by maintaining a function call stack so that users can debug the smart contract, which will be explained in detail later. Each function can include one or more entries and one or more exits. In order to ensure accurate recording of the function call status, the second instruction for calling the stack maintenance function can be added at all entries and all exits.
[0054] In one or more embodiments of the present specification, modifying the mapping information accordingly may include performing the following operations in a debugger thread of the virtual machine:
[0055] The first mapping relationship between the source code of the smart contract and the bytecode of the first bytecode program file in the mapping information is modified to a second mapping relationship between the source code of the smart contract and the bytecode of the second bytecode program file.
[0056] Specifically, the mapping information can be modified to a mapping relationship between the line number of the source code and the line number of the corresponding bytecode in the second bytecode program file, so that the corresponding position in the source code can be conveniently located according to the mapping information and the bytecode in the subsequent debugging process.
[0057] return Figure 1 , the debugging method may also include:
[0058] Step S500, compile the second bytecode program file in the virtual machine to generate a machine code program file.
[0059] Specifically, the second bytecode program file can be subjected to just-in-time compilation (JIT compilation) as described above by a bytecode compiler in the virtual machine to generate a machine code program file. Accordingly, the bytecode compiler can be a JIT compiler.
[0060] like Figure 1 As shown, the debugging method may further include:
[0061] Step S700, according to the breakpoint information and debugging commands obtained by the virtual machine, the machine code program file is executed in the virtual machine in a debugging state, and the smart contract is debugged in combination with the function list information and the mapping information.
[0062] Breakpoint information and debugging commands can be input into the virtual machine by the user during the debugging process. The user can give the corresponding breakpoint information and debugging commands according to the debugging requirements. When the machine code program file is executed in the debugging state, under the action of the interrupt processing function, the operation of the machine code program file can be interrupted at the corresponding position. The user can debug the smart contract by giving the debugging command and combining the function list information and mapping information.
[0063] Specifically, if Figure 3 As shown, according to the breakpoint information and debugging commands obtained by the virtual machine, executing the machine code program file in the virtual machine in a debugging state, and debugging the smart contract in combination with the function list information and the mapping information may include:
[0064] Step S710, obtaining breakpoint information from a user in a debugger thread of the virtual machine;
[0065] Step S730, running the machine code program file in the virtual machine until the interrupt processing function interrupts the program running thread of the virtual machine based on the breakpoint information and is in the debugger thread.
[0066] The initial breakpoint information from the user can be obtained after the machine code program file is compiled and before the machine code program file is run. The modified breakpoint information from the user can also be obtained when the machine code program file is interrupted during the execution of the machine code program file in the debugging state, so that the user can adjust the breakpoint position that needs to be interrupted in time according to the debugging needs.
[0067] In one or more embodiments of the present specification, breakpoint information can be maintained based on a breakpoint information bitmap (array). The program blocks associated with each first instruction for calling an interrupt handling function in the second bytecode program file can have a program block identifier that uniquely represents the program block and an interrupt indication identifier used to determine whether to interrupt at the program block, and the program block identifiers of all program blocks and their corresponding interrupt indication identifiers can be recorded in the breakpoint information bitmap. Wherein, the program block identifier can be a constant numerical parameter, and the program block identifier of a certain program block can be increased by 1 compared to the program block identifier of the previous program block adjacent to it (it can be understood that the program block identifier can also be set in other ways, which is not limited here), and the interrupt indication identifier can have two values of true and false, wherein true indicates interruption, and false indicates not to interrupt. Typically, each program block can include only one basic block, that is, each basic block in the second bytecode program file can have a basic block identifier that uniquely represents the basic block and an interrupt indication identifier used to determine whether to interrupt at the head of the basic block, and the basic block identifiers of all basic blocks and their corresponding interrupt indication identifiers can be recorded in the breakpoint information bitmap. However, in some embodiments, it is also possible to make a program block include more than one basic block as needed, which is not limited here. When the initial breakpoint information from the user is obtained, a breakpoint information bitmap can be initially established and the initial breakpoint information can be recorded accordingly. When the machine code program file is interrupted and the modified breakpoint information from the user is obtained, the breakpoint information bitmap can be updated according to the modified breakpoint information, so that the updated breakpoint information bitmap is consistent with the modified breakpoint information. Specifically, the user can modify the breakpoint line number in the source code of the smart contract to mark the modified breakpoint position. In the virtual machine, the source code line number of the breakpoint position can be converted into the bytecode line number of the breakpoint position according to the mapping information, and the interrupt indication flag corresponding to the breakpoint position in the breakpoint information bitmap can be modified to true.
[0068] Furthermore, the interrupt handling function may be configured to perform the following operations in the debugger thread of the virtual machine:
[0069] Read the corresponding interrupt indication mark of the current program block in the breakpoint information bitmap;
[0070] When it is determined according to the interrupt indication mark that the current program block is to be interrupted, the virtual machine is placed in the debugger thread; and
[0071] When it is determined according to the interrupt indication mark that the current program block does not need to be interrupted, the virtual machine is placed in the program running thread until it runs to the program block associated with the next first instruction, and returns to the step of reading the corresponding interrupt indication mark of the current program block in the breakpoint information bitmap.
[0072] In the case where the first instruction is added to the head of each basic block of the second bytecode program file as described above, during the process of running the machine code program file, at the head of each basic block, an interrupt processing function will be called to determine whether it is necessary to interrupt the program running thread and enter the debugger thread at the head of this basic block. The interrupt processing function reads the interrupt indication mark corresponding to the current basic block in the breakpoint information bitmap. If the read interrupt indication mark is true, the program running thread is interrupted and the debugger thread is entered, and if the read interrupt indication mark is false, the program running thread is continued to run the corresponding basic block until the next basic block is reached and the above judgment process is repeated. The interrupt processing function can also include two parameters of the start instruction line number and the end instruction line number of its corresponding basic block, so that the interrupt processing function controls the interruption of program operation.
[0073] return Figure 3 , according to the breakpoint information and debugging commands obtained by the virtual machine, the machine code program file is executed in the virtual machine in the debugging state, and the smart contract is debugged in combination with the function list information and mapping information, which may also include:
[0074] Step S750, obtaining a debugging command from a user in the debugger thread; and
[0075] Step S770: Debug the smart contract according to the debugging command in combination with the function list information and mapping information.
[0076] As described above, in order to facilitate debugging of smart contracts, a function call stack can be maintained through a stack maintenance function to record the function call status at the current running position. Specifically, the stack maintenance function can be configured to perform the following operations in the debugger thread of the virtual machine: when the current running position is the entry of the function, push the record associated with the current running position to the function call stack; and when the current running position is the exit of the function, pop the record associated with the current running position from the function call stack. Among them, the record associated with the current running position may include the line number of the instruction at the current running position. In the process of modifying the function call stack, the placeholder can be filled in first, and finally replaced with the line number of the correct instruction.
[0077] In one or more embodiments of the present specification, the debugging command may include a progress command, and the user may use the progress command to indicate the next instruction to be executed, so that the virtual machine switches between the debugger state and the program running state under appropriate conditions, thereby facilitating the continued debugging of the smart contract.
[0078] In the case where the debug command includes a march command, such as Figure 4As shown, according to the debugging command, debugging the smart contract in combination with the function list information and the mapping information may include:
[0079] Step S771, recording in the debugger thread the stack frame depth of the function call stack when the advance command is obtained;
[0080] Step S773, modifying the maximum debugging stack frame depth and the minimum debugging stack frame depth in the debugger thread according to the recorded stack frame depth and the progress command; and
[0081] Step S775, interrupting the debugger thread and entering the program execution thread to execute the machine code program file, until the program execution thread is interrupted again and re-enters the debugger thread based on the modified minimum debug stack frame depth and maximum debug stack frame depth.
[0082] Among them, the program running thread of the virtual machine only runs when the current stack frame depth is greater than or equal to the minimum debug stack frame depth and less than or equal to the maximum debug stack frame depth. The minimum debug stack frame depth and the maximum debug stack frame depth can be two global variables of the debugger. The stack frame depth reflects the call level of the currently running function. For example, if the currently running function is C, and function C is called by function B, and function B is called by main function A, then the current stack frame depth is 3. Since the program running thread only runs when the current stack frame depth is greater than or equal to the minimum debug stack frame depth and less than or equal to the maximum debug stack frame depth, the switching between the program running thread and the debugger thread can be controlled by modifying the maximum debug stack frame depth and the minimum debug stack frame depth.
[0083] In a specific example, when the recorded stack frame depth (i.e., the current stack frame depth) is n, the progress command is used to interrupt the program running thread at the next interrupt position corresponding to the second bytecode program file, or interrupt the program running thread when it reaches the next instruction of the sub-function that does not call the current function in the source code corresponding to the smart contract (that is, if the next instruction of the current running position is an instruction to call the sub-function, then the sub-function is entered and run normally until the stack frame depth returns to the corresponding current function and then interrupts; if the next instruction of the current running position is not an instruction to call the sub-function, then interrupt directly), or the step over command is used to interrupt the program running thread when it reaches the parent function of the previous level that references the current function, in order to implement the step over command, the minimum debug stack frame depth can be modified to (n+1), and the maximum debug stack frame depth can be modified to correspond to infinity. In this way, when the machine code program file runs to make the current stack frame depth less than or equal to n, the debugger thread is entered again.
[0084] In another specific example, when the recorded stack frame depth is n and the advance command is a step out command for causing the program execution thread to interrupt at the next position to be interrupted corresponding to the second bytecode program file, or causing the program execution thread to interrupt when it reaches the parent function of the previous level that references the current function, in order to implement the step out command, the minimum debug stack frame depth can be modified to n, and the maximum debug stack frame depth can be modified to correspond to infinity. In this way, when the machine code program file runs to make the current stack frame depth less than n, the debugger thread is entered again.
[0085] In another specific example, when the marching command is a step in command used to interrupt the program execution thread when it single-steps to the next instruction of the current function in the source code corresponding to the smart contract, or interrupts the program execution thread when it proceeds to the next-level sub-function referenced by the current function, the minimum debug stack frame depth can be modified to be greater than the maximum debug stack frame depth. For example, the minimum debug stack frame depth can be modified to 2, and the maximum debug stack frame depth can be modified to 1, that is, the stack frame depth requirement cannot be met at all times, thereby interrupting the program execution thread and entering the debugger thread.
[0086] Similarly, other progress commands such as continue may be implemented by modifying the maximum debug stack frame depth and the minimum debug stack frame depth, which is not limited here.
[0087] In one or more embodiments of the present specification, the debugging command may also include a viewing command to view corresponding variables and other information during the debugging process. Figure 5 As shown, according to the debugging command, debugging the smart contract in combination with the function list information and the mapping information may include performing the following operations in the debugger thread:
[0088] Step S772, determining the frame address of the current function corresponding to the current running position when the viewing instruction is obtained in the function call stack in the bytecode form;
[0089] Step S774, filtering out the variables containing the instructions of the current running position according to the frame address of the current function, and determining the index of the variable to be viewed corresponding to the viewing command; and
[0090] Step S776, according to the frame address of the current function and the index of the variable to be viewed, the variable to be viewed in source code form is obtained in combination with the function list information and the mapping information.
[0091] In the process of viewing variables, the key is to convert the function information, variable information, etc. in the form of bytecode into the function information, variable information, etc. in the form of source code that can be viewed by the user according to the debugging information.
[0092] In the debugger thread, a function call stack in bytecode (e.g., WASM) form is maintained. Combined with the function list information and mapping information in the debugging information, the function call stack in source code (high-level language) form can be viewed to find the source code function corresponding to each bytecode function in the bytecode function call stack.
[0093] In addition to the function information in the bytecode form of the current frame, each frame in the function call stack also contains the instruction position currently executed in the current frame. According to the address information of the current frame function in the debugging information file, the lexical scope of the function or variable can be filtered out to include the variable of this instruction position, which is the list of local variables currently visible in the current frame. When obtaining the value of a variable, the frame address of this frame function and the index i of the local variable of this variable in the bytecode form function can be obtained from the function call stack, and then the virtual machine provides a way to obtain the value of the i-th variable in the function stack frame. The debug information file contains the type of the variable, and the data range in the memory to be read, as well as the parsing and display methods can be determined according to the variable type.
[0094] In the debugging method of one or more embodiments of the present specification, the bytecode program file is modified before just-in-time compilation to inject program instructions associated with the debugging process (for example, including a first instruction for calling an interrupt processing function and a second instruction for calling a stack maintenance function, etc.), thereby avoiding the impact on the original compiler or running logic for just-in-time compilation and running the bytecode program file. In the debugging method of one or more embodiments of the present specification, there is no need to provide an additional interpreter, thereby avoiding the risk of inconsistent running logic caused by different implementation codes of the interpreter and the compiler. In addition, the modified second bytecode program file is just-in-time compiled, and the machine code obtained from the bytecode compilation is finally executed. In theory, the running efficiency is also much higher than debugging in an interpreted running manner. In the debugging method of one or more embodiments of the present specification, there is no need to maintain the mapping information from source code to machine code, but only the mapping information from source code to bytecode needs to be maintained, thereby simplifying the maintenance difficulty. In the debugging method of one or more embodiments of the present specification, the bytecode program file is first modified, and then the modified bytecode program file is run in a just-in-time compilation manner to implement breakpoint debugging, and the function call stack in the form of source code (high-level language) is deduced based on the debugging information and the function call stack during runtime. This method will not affect the runtime logic of the source code of the smart contract itself, and helps to improve the reliability and efficiency of debugging.
[0095] In one or more embodiments of the present disclosure, a debugging device 910 for smart contracts is also proposed. Figure 6As shown, the debugging device may include a processor 911 and a memory 913. The memory 913 stores instructions. When the instructions are executed by the processor 911, the steps of the debugging method described above are implemented.
[0096] Specifically, the processor 911 can be an integrated circuit chip with signal processing capabilities. The above processor can be a general-purpose processor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The various methods, steps and logic block diagrams disclosed in the embodiments of the present disclosure can be implemented or executed. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor, etc., which can be an X86 architecture or an ARM architecture, etc.
[0097] The memory 913 may be a volatile memory or a nonvolatile memory, or may include both volatile and nonvolatile memory. The nonvolatile memory may be a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), or a flash memory. The volatile memory may be a random access memory (RAM), which is used as an external cache. By way of example but not limitation, many forms of RAM are available, such as static random access memory (SRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDRSDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronously connected dynamic random access memory (SLDRAM), and direct memory bus random access memory (DR RAM). It should be noted that the memory of the method described herein is intended to include, but is not limited to, these and any other suitable types of memory.
[0098] In one or more embodiments of the present disclosure, a debugging system for smart contracts is also proposed. Figure 7 As shown, the debugging system may include a virtual machine 930, and the virtual machine 930 may include:
[0099] An acquisition module 931, which may be configured to acquire a first bytecode program file of a smart contract including debugging information, or to acquire a first bytecode program file of a smart contract and a debugging information file including debugging information, wherein the debugging information includes mapping information between the source code and the bytecode of the smart contract and function list information;
[0100] A modification module 932, the modification module 932 can be configured to modify the first bytecode program file to generate a second bytecode program file including a first instruction for calling an interrupt processing function and a second instruction for calling a stack maintenance function, and modify the mapping information accordingly, wherein the interrupt processing function is used to determine whether to interrupt the execution at the current execution position where the interrupt processing function is located, and the stack maintenance function is used to record the function call status at the current execution position where the stack maintenance function is located;
[0101] A compiling module 933, which may be configured to compile the second bytecode program file in the virtual machine to generate a machine code program file; and
[0102] Execution module 934, execution module 934 can be configured to execute the machine code program file in a debugging state according to the breakpoint information and debugging commands obtained by the acquisition module 931, and debug the smart contract in combination with the function list information and mapping information.
[0103] In some embodiments, the virtual machine 930 may be a WASM virtual machine, and the first bytecode program file and the second bytecode program file are both WASM files.
[0104] In some embodiments, the modification module 932 may include:
[0105] a first division unit, the first division unit being configured to divide the first bytecode program file into one or more functions;
[0106] a second partitioning unit, the second partitioning unit being configured to partition each of the one or more functions into one or more basic blocks, so that each instruction in the function is included and only included in a corresponding one of the one or more basic blocks, wherein each basic block includes one or more instructions, and once a basic block is executed, all instructions included in the basic block are completely executed; and
[0107] The instruction writing unit is configured to add a first instruction to a header of each basic block in the function.
[0108] In some embodiments, the second partitioning unit may be configured to perform the following operations:
[0109] Find all non-linear execution instructions in the function; and
[0110] According to the non-linear operation instructions found, the function is divided into one or more basic blocks, wherein each non-linear operation instruction is the last instruction in the basic block where it is located.
[0111] In some embodiments, the instruction writing unit may be further configured to add a second instruction before all entries and exits of each of the one or more functions.
[0112] In some embodiments, the modification module 932 may also be configured to perform the following operations:
[0113] The first mapping relationship between the source code of the smart contract and the bytecode of the first bytecode program file in the mapping information is modified to a second mapping relationship between the source code of the smart contract and the bytecode of the second bytecode program file.
[0114] In some embodiments, the compilation module 933 may be a just-in-time compilation module.
[0115] In some embodiments, the acquisition module 931 may also be configured to acquire breakpoint information from a user;
[0116] The execution module 934 may be configured to perform the following operations:
[0117] Running the machine code program file in the virtual machine until the interrupt processing function interrupts the program running thread of the virtual machine based on the breakpoint information and is placed in the debugger thread;
[0118] Obtaining debug commands from the user in the debugger thread; and
[0119] According to the debugging command, the smart contract is debugged in combination with the function list information and mapping information.
[0120] In some embodiments, the program blocks associated with each first instruction in the second bytecode program file respectively have a program block identifier that uniquely represents the program block and an interrupt indication identifier used to determine whether to interrupt at the program block, and the program block identifiers of all program blocks and their corresponding interrupt indication identifiers are recorded in a breakpoint information bitmap.
[0121] In some embodiments, the interrupt handling function may be configured to perform the following operations in the debugger thread of the virtual machine 930:
[0122] Read the corresponding interrupt indication mark of the current program block in the breakpoint information bitmap;
[0123] When it is determined according to the interrupt indication mark that the current program block is to be interrupted, the virtual machine is placed in the debugger thread; and
[0124] When it is determined according to the interrupt indication mark that the current program block does not need to be interrupted, the virtual machine is placed in the program running thread until it runs to the program block associated with the next first instruction, and returns to the step of reading the corresponding interrupt indication mark of the current program block in the breakpoint information bitmap.
[0125] In some embodiments, when the virtual machine 930 obtains modified breakpoint information from the user, the breakpoint information bitmap is modified accordingly.
[0126] In some embodiments, the stack maintenance function may be configured to perform the following operations in a debugger thread of the virtual machine:
[0127] When the current running position is the entry point of the function, pushing a record associated with the current running position onto the function call stack; and
[0128] When the current execution position is the exit of the function, the record associated with the current execution position is popped from the function call stack.
[0129] In some embodiments, the debug command may include a march command, and the execution module 934 may be configured to perform the following operations:
[0130] Record the frame depth of the function call stack when the advance command is obtained in the debugger thread;
[0131] In the debugger thread, modifying the maximum debug stack frame depth and the minimum debug stack frame depth according to the recorded stack frame depth and the progress command, wherein the program execution thread of the virtual machine only runs when the current stack frame depth is greater than or equal to the minimum debug stack frame depth and less than or equal to the maximum debug stack frame depth; and
[0132] The debugger thread is interrupted and the program execution thread is entered to execute the machine code program file, until the program execution thread is interrupted again and the debugger thread is re-entered based on the modified minimum debug stack frame depth and maximum debug stack frame depth.
[0133] In some embodiments, when the recorded stack frame depth is n and the progress command is a step over command, the minimum debug stack frame depth is modified to (n+1), and the maximum debug stack frame depth is modified to correspond to infinity, wherein the step over command is used to cause the program running thread to interrupt when it proceeds to the next position to be interrupted in the second bytecode program file, or to cause the program running thread to interrupt when it proceeds to the next instruction in the source code corresponding to the smart contract that is not a subfunction that calls the current function, or to cause the program running thread to interrupt when it proceeds to the upper-level parent function that references the current function.
[0134] In some embodiments, when the recorded stack frame depth is n and the progress command is a step out command, the minimum debug stack frame depth is modified to n, and the maximum debug stack frame depth is modified to correspond to infinity, wherein the step out command is used to cause the program running thread to interrupt when it proceeds to the next position to be interrupted in the second bytecode program file, or to cause the program running thread to interrupt when it proceeds to the upper-level parent function that references the current function.
[0135] In some embodiments, when the advance command is a step in command, the minimum debug stack frame depth is modified to be greater than the maximum debug stack frame depth, wherein the step in command is used to interrupt the program execution thread when it single-steps to the next instruction of the current function in the source code corresponding to the smart contract, or interrupt the program execution thread when it proceeds to the next level sub-function referenced by the current function.
[0136] In some embodiments, the debugging command may include a viewing command, and the execution module 934 may be configured to perform the following operations:
[0137] Determine the frame address of the current function in the function call stack in bytecode form corresponding to the current running position when the viewing instruction is obtained;
[0138] Filter out the variables containing the instructions of the current running position according to the frame address of the current function, and determine the index of the variable to be viewed corresponding to the viewing command; and
[0139] According to the frame address of the current function and the index of the variable to be viewed, the variable to be viewed in source code form is obtained in combination with the function list information and mapping information.
[0140] In the 1990s, improvements to a technology could be clearly distinguished as hardware improvements (for example, improvements to the circuit structure of diodes, transistors, switches, etc.) or software improvements (improvements to the method flow). However, with the development of technology, many improvements to the method flow today can be regarded as direct improvements to the hardware circuit structure. Designers almost always obtain the corresponding hardware circuit structure by programming the improved method flow into the hardware circuit. Therefore, it cannot be said that an improvement in a method flow cannot be implemented using a hardware entity module. For example, a programmable logic device (PLD) (such as a field programmable gate array (FPGA)) is such an integrated circuit whose logical function is determined by the user's programming of the device. Designers can "integrate" a digital system on a PLD by programming it themselves, without having to ask a chip manufacturer to design and produce a dedicated integrated circuit chip. Moreover, nowadays, instead of manually making integrated circuit chips, this kind of programming is mostly implemented by "logic compiler" software, which is similar to the software compiler used when developing and writing programs, and the original code before compilation must also be written in a specific programming language, which is called hardware description language (HDL). There is not only one HDL, but many kinds, such as ABEL (Advanced Boolean Expression Language), AHDL (Altera Hardware Description Language), Confluence, CUPL (Cornell University Programming Language), HDCal, JHDL (Java Hardware Description Language), Lava, Lola, MyHDL, PALASM, RHDL (Ruby Hardware Description Language), etc. The most commonly used ones are VHDL (Very-High-Speed Integrated Circuit Hardware Description Language) and Verilog. Those skilled in the art should also know that it is only necessary to program the method flow slightly in the above-mentioned hardware description languages and program it into the integrated circuit, and then it is easy to obtain the hardware circuit that implements the logic method flow.
[0141] The controller can be implemented in any appropriate manner, for example, the controller can take the form of a microprocessor or processor and a computer-readable medium storing a computer-readable program code (such as software or firmware) that can be executed by the (micro)processor, a logic gate, a switch, an application-specific integrated circuit (ASIC), a programmable logic controller, and an embedded microcontroller. Examples of controllers include, but are not limited to, the following microcontrollers: ARC 625D, Atmel AT91SAM, Microchip PIC18F26K20, and Silicone Labs C8051F320. The memory controller can also be implemented as part of the control logic of the memory. Those skilled in the art also know that in addition to implementing the controller in a purely computer-readable program code manner, the controller can be implemented in the form of a logic gate, a switch, an application-specific integrated circuit, a programmable logic controller, and an embedded microcontroller by logically programming the method steps. Therefore, this controller can be considered as a hardware component, and the devices included therein for implementing various functions can also be regarded as structures within the hardware component. Or even, the devices for implementing various functions can be regarded as both software modules for implementing the method and structures within the hardware component.
[0142] The systems, devices, modules or units described in the above embodiments may be implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a server system. Of course, the present application does not exclude that with the development of computer technology in the future, the computer that implements the functions of the above embodiments may be, for example, a personal computer, a laptop computer, a vehicle-mounted human-computer interaction device, a cellular phone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or a combination of any of these devices.
[0143] Although one or more embodiments of the present specification provide method operation steps as described in the embodiments or flow charts, more or less operation steps may be included based on conventional or non-creative means. The order of steps listed in the embodiments is only one way of executing the order of many steps, and does not represent the only execution order. When the device or terminal product in practice is executed, it can be executed in sequence or in parallel according to the method shown in the embodiments or the drawings (for example, a parallel processor or a multi-threaded processing environment, or even a distributed data processing environment). The term "include", "include" or any other variant thereof is intended to cover non-exclusive inclusion, so that the process, method, product or equipment including a series of elements includes not only those elements, but also includes other elements that are not explicitly listed, or also includes elements inherent to such a process, method, product or equipment. In the absence of more restrictions, it is not excluded that there are other identical or equivalent elements in the process, method, product or equipment including the elements. For example, if the words first, second, etc. are used to represent the name, they do not represent any specific order.
[0144] For the convenience of description, the above devices are described in various modules according to their functions. Of course, when implementing one or more of the present specification, the functions of each module can be implemented in the same or more software and / or hardware, or the module implementing the same function can be implemented by a combination of multiple sub-modules or sub-units, etc. The device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0145] The present invention is described with reference to flowcharts and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0146] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0147] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0148] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0149] The memory may include non-permanent storage in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.
[0150] Computer readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage, graphene storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.
[0151] It should be understood by those skilled in the art that one or more embodiments of the present specification may be provided as a method, system or computer program product. Therefore, one or more embodiments of the present specification may take the form of a complete hardware embodiment, a complete software embodiment or an embodiment combining software and hardware. Moreover, one or more embodiments of the present specification may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0152] One or more embodiments of this specification may be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform specific tasks or implement specific abstract data types. One or more embodiments of this specification may also be practiced in distributed computing environments where tasks are performed by remote processing devices connected through a communication network. In a distributed computing environment, program modules may be located in local and remote computer storage media, including storage devices.
[0153] Each embodiment in this specification is described in a progressive manner, and the same and similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment. In the description of this specification, the description of the reference terms "one embodiment", "some embodiments", "example", "specific example", or "some examples" means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of this specification. In this specification, the schematic representation of the above terms does not necessarily target the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described can be combined in any one or more embodiments or examples in a suitable manner. In addition, those skilled in the art can combine and combine the different embodiments or examples described in this specification and the features of different embodiments or examples without contradiction.
[0154] The above description is only an example of one or more embodiments of this specification and is not intended to limit one or more embodiments of this specification. For those skilled in the art, one or more embodiments of this specification may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of this specification shall be included in the scope of the claims.
Claims
1. A debugging method for a smart contract, comprising: The virtual machine obtains a first bytecode program file of the smart contract including debugging information, or the virtual machine obtains the first bytecode program file of the smart contract and a debugging information file including debugging information, wherein the debugging information includes mapping information between the source code and the bytecode of the smart contract and function list information; Modifying the first bytecode program file in the virtual machine to generate a second bytecode program file including a first instruction for calling an interrupt processing function and a second instruction for calling a stack maintenance function, and modifying the mapping information accordingly, wherein the interrupt processing function is used to determine whether to interrupt the execution at the current execution position thereof, and the stack maintenance function is used to record the function call status at the current execution position thereof; compiling the second bytecode program file in the virtual machine to generate a machine code program file; and According to the breakpoint information and debugging commands obtained by the virtual machine, the machine code program file is executed in the virtual machine in a debugging state, and the smart contract is debugged in combination with the function list information and the mapping information. The step of modifying the first bytecode program file in the virtual machine to generate a second bytecode program file including a first instruction for calling an interrupt handling function and a second instruction for calling a stack maintenance function includes performing the following operations in a debugger thread of the virtual machine: Dividing the first bytecode program file into one or more functions; For at least one of the one or more functions, perform the following operations respectively: Add the second instruction; Divide a function into one or more basic blocks so that each instruction in the function is included in and only in a corresponding one of the one or more basic blocks, wherein each basic block includes one or more instructions, and once a basic block is executed, all instructions contained in the basic block are executed in their entirety; The first instruction is added to at least one basic block in a function.
2. The debugging method according to claim 1, wherein: The virtual machine is a WASM virtual machine, and the first bytecode program file and the second bytecode program file are both WASM files.
3. The debugging method according to claim 1, wherein: The mapping information includes a mapping relationship between the line number of the source code of the smart contract and the line number of the corresponding bytecode.
4. The debugging method according to claim 1, wherein: The function list information includes at least one of the name of the function in the smart contract, the lexical scope of the function, the name of the variable, the data type of the variable, the lexical scope of the variable, the index of the variable, the name of the parameter, and the index of the parameter.
5. The debugging method according to claim 1, wherein: For each of the one or more functions, the first instruction is added to the header of each basic block in the function.
6. The debugging method according to claim 1, wherein: Divide a function into one or more basic blocks so that each instruction in the function is included in and only in a corresponding one of the one or more basic blocks: Find all non-linear execution instructions in the function; and According to the non-linear operation instructions found, the function is divided into one or more basic blocks, wherein each non-linear operation instruction is the last instruction in the basic block where it is located.
7. The debugging method according to claim 6, wherein: The non-linear execution instruction includes at least one of a jump instruction, a function call instruction, and a storage-type instruction that modifies a variable value.
8. The debugging method according to claim 1, wherein: The second instruction is added before all entries and exits of each of the one or more functions.
9. The debugging method according to claim 1, wherein: Modifying the mapping information accordingly includes performing the following operations in a debugger thread of the virtual machine: The first mapping relationship between the source code of the smart contract and the bytecode of the first bytecode program file in the mapping information is modified to a second mapping relationship between the source code of the smart contract and the bytecode of the second bytecode program file.
10. The debugging method according to claim 1, wherein: Compiling the second bytecode program file in the virtual machine to generate a machine code program file includes: The bytecode compiler in the virtual machine performs real-time compilation on the second bytecode program file to generate the machine code program file.
11. The debugging method according to claim 1, wherein: According to the breakpoint information and the debugging command obtained by the virtual machine, executing the machine code program file in the debugging state in the virtual machine, and debugging the smart contract in combination with the function list information and the mapping information includes: Acquiring breakpoint information from a user in a debugger thread of the virtual machine; Running the machine code program file in the virtual machine until the interrupt processing function interrupts the program running thread of the virtual machine based on the breakpoint information and is in the debugger thread; Obtaining a debugging command from a user in the debugger thread; and According to the debugging command, the smart contract is debugged in combination with the function list information and the mapping information.
12. The debugging method according to claim 11, wherein: Acquiring breakpoint information from a user in a debugger thread of the virtual machine includes: Before the machine code program file is executed, obtaining initial breakpoint information from a user in the debugger thread; and / or When the machine code program file is interrupted, the modified breakpoint information from the user is obtained in the debugger thread.
13. The debugging method according to claim 11, wherein: The program blocks associated with each first instruction in the second bytecode program file respectively have a program block identifier that uniquely represents the program block and an interrupt indication identifier used to determine whether to interrupt at the program block, and the program block identifiers of all program blocks and their corresponding interrupt indication identifiers are recorded in a breakpoint information bitmap.
14. The debugging method according to claim 13, wherein: The interrupt handling function is configured to perform the following operations in the debugger thread of the virtual machine: Reading the corresponding interrupt indication mark of the current program block in the breakpoint information bitmap; When it is determined according to the interrupt indication mark that an interrupt is to be performed at the current program block, placing the virtual machine in the debugger thread; as well as When it is determined according to the interrupt indication identifier that the current program block does not need to be interrupted, the virtual machine is placed in the program running thread until it runs to the program block associated with the next first instruction, and returns to the step of reading the corresponding interrupt indication identifier of the current program block in the breakpoint information bitmap.
15. The debugging method according to claim 13, wherein: When the virtual machine obtains the modified breakpoint information from the user, the breakpoint information bitmap is modified accordingly.
16. The debugging method according to claim 11, wherein: The stack maintenance function is configured to perform the following operations in the debugger thread of the virtual machine: When the current running position is the entry point of the function, pushing a record associated with the current running position onto the function call stack; and When the current running position is the exit of the function, the record associated with the current running position is popped from the function call stack.
17. The debugging method according to claim 16, wherein: The record associated with the current execution position includes the line number of the instruction at the current execution position.
18. The debugging method according to claim 16, wherein: The debugging command includes a progress command, and debugging the smart contract according to the debugging command in combination with the function list information and the mapping information includes: Recording in the debugger thread the stack frame depth of the function call stack when the advance command is obtained; modifying the maximum debug stack frame depth and the minimum debug stack frame depth in the debugger thread according to the recorded stack frame depth and the progress command, wherein the program execution thread of the virtual machine runs only when the current stack frame depth is greater than or equal to the minimum debug stack frame depth and less than or equal to the maximum debug stack frame depth; and The debugger thread is interrupted and the program execution thread is entered to execute the machine code program file, until the program execution thread is interrupted again and re-enters the debugger thread based on the modified minimum debug stack frame depth and maximum debug stack frame depth.
19. The debugging method according to claim 18, wherein: When the recorded stack frame depth is n and the advancing command is a step over command, the minimum debugging stack frame depth is modified to (n+1), and the maximum debugging stack frame depth is modified to correspond to infinity, wherein the step over command is used to interrupt the program running thread at the next position to be interrupted in the second bytecode program file, or to interrupt the program running thread when it reaches the next instruction of the subfunction that is not a subfunction that calls the current function in the source code corresponding to the smart contract, or to interrupt the program running thread when it reaches the parent function of the previous level that references the current function.
20. The debugging method according to claim 18, wherein: When the recorded stack frame depth is n and the progress command is a step out command, the minimum debug stack frame depth is modified to n, and the maximum debug stack frame depth is modified to correspond to infinity, wherein the step out command is used to cause the program running thread to interrupt when it proceeds to the next interrupt position corresponding to the second bytecode program file, or to cause the program running thread to interrupt when it proceeds to the upper-level parent function that references the current function.
21. The debugging method according to claim 18, wherein: When the advancing command is a step in command, the minimum debugging stack frame depth is modified to be greater than the maximum debugging stack frame depth, wherein the step in command is used to interrupt the program execution thread when it single-steps to the next instruction of the current function in the source code corresponding to the smart contract, or interrupt the program execution thread when it proceeds to the next level sub-function referenced by the current function.
22. The debugging method according to claim 16, wherein: The debugging command includes a viewing command, and debugging the smart contract according to the debugging command in combination with the function list information and the mapping information includes performing the following operations in the debugger thread: Determine the frame address of the current function corresponding to the current running position when the viewing command is obtained in the function call stack in the bytecode form; Filter out the variables containing the instructions of the current running position according to the frame address of the current function, and determine the index of the variable to be viewed corresponding to the viewing command; as well as According to the frame address of the current function and the index of the variable to be viewed, the variable to be viewed in source code form is acquired in combination with the function list information and the mapping information.
23. A debugging device for smart contracts, comprising a processor and a memory, wherein the memory stores instructions, and when the instructions are executed by the processor, the steps of the debugging method as described in any one of claims 1 to 22 are implemented.
24. A debugging system for a smart contract, comprising a virtual machine, the virtual machine comprising: An acquisition module, wherein the acquisition module is configured to acquire a first bytecode program file of the smart contract including debugging information, or acquire the first bytecode program file of the smart contract and a debugging information file including debugging information, wherein the debugging information includes mapping information between the source code and the bytecode of the smart contract and function list information; a modification module, the modification module being configured to modify the first bytecode program file to generate a second bytecode program file including a first instruction for calling an interrupt processing function and a second instruction for calling a stack maintenance function, and modify the mapping information accordingly, wherein the interrupt processing function is used to determine whether to interrupt the operation at the current operation position where the interrupt processing function is located, and the stack maintenance function is used to record the function call status at the current operation position where the stack maintenance function is located; a compiling module, the compiling module being configured to compile the second bytecode program file in the virtual machine to generate a machine code program file; and an execution module, wherein the execution module is configured to execute the machine code program file in a debugging state according to the breakpoint information and the debugging command obtained by the acquisition module, and debug the smart contract in combination with the function list information and the mapping information, Wherein, the modification module includes: a first division unit, the first division unit being configured to divide the first bytecode program file into one or more functions; a second partitioning unit, the second partitioning unit being configured to partition each of the one or more functions into one or more basic blocks, so that each instruction in the function is included and only included in a corresponding one of the one or more basic blocks, wherein each basic block includes one or more instructions, and once a basic block is executed, all instructions included in the basic block are executed; An instruction writing unit is configured to add the first instruction to at least one basic block in at least one function among one or more functions, and to add the second instruction to at least one function among the one or more functions.
25. The debugging system according to claim 24, wherein: The virtual machine is a WASM virtual machine, and the first bytecode program file and the second bytecode program file are both WASM files.
26. The debugging system according to claim 24, wherein: The instruction writing unit is configured to add the first instruction to the header of each basic block in a function.
27. The debugging system according to claim 24, wherein: The second dividing unit is configured to perform the following operations: Find all non-linear execution instructions in the function; and According to the non-linear operation instructions found, the function is divided into one or more basic blocks, wherein each non-linear operation instruction is the last instruction in the basic block where it is located.
28. The debugging system according to claim 26, wherein: The instruction writing unit is further configured to add the second instruction before all entries and exits of each function of the one or more functions.
29. The debugging system according to claim 24, wherein: The modification module is further configured to perform the following operations: The first mapping relationship between the source code of the smart contract and the bytecode of the first bytecode program file in the mapping information is modified to a second mapping relationship between the source code of the smart contract and the bytecode of the second bytecode program file.
30. The debugging system according to claim 24, wherein: The compiling module is a just-in-time compiling module.
31. The debugging system according to claim 24, wherein: The acquisition module is further configured to acquire breakpoint information from a user; The execution module is configured to perform the following operations: Running the machine code program file in the virtual machine until the interrupt processing function interrupts the program running thread of the virtual machine based on the breakpoint information and is in the debugger thread; Obtaining a debugging command from a user in the debugger thread; as well as According to the debugging command, the smart contract is debugged in combination with the function list information and the mapping information.
32. The debugging system according to claim 24, wherein: The program blocks associated with each first instruction in the second bytecode program file respectively have a program block identifier that uniquely represents the program block and an interrupt indication identifier used to determine whether to interrupt at the program block, and the program block identifiers of all program blocks and their corresponding interrupt indication identifiers are recorded in a breakpoint information bitmap.
33. The debugging system according to claim 32, wherein: The interrupt handling function is configured to perform the following operations in the debugger thread of the virtual machine: Reading the corresponding interrupt indication mark of the current program block in the breakpoint information bitmap; When it is determined according to the interrupt indication mark that an interrupt is to be performed at the current program block, placing the virtual machine in the debugger thread; as well as When it is determined according to the interrupt indication identifier that the current program block does not need to be interrupted, the virtual machine is placed in the program running thread until it runs to the program block associated with the next first instruction, and returns to the step of reading the corresponding interrupt indication identifier of the current program block in the breakpoint information bitmap.
34. The debugging system according to claim 32, wherein: When the virtual machine obtains the modified breakpoint information from the user, the breakpoint information bitmap is modified accordingly.
35. The debugging system according to claim 24, wherein: The stack maintenance function is configured to perform the following operations in the debugger thread of the virtual machine: When the current running position is the entry point of the function, pushing a record associated with the current running position onto the function call stack; and When the current running position is the exit of the function, the record associated with the current running position is popped from the function call stack.
36. The debugging system according to claim 35, wherein: The debugging command includes a running command, and the execution module is configured to perform the following operations: Recording in the debugger thread the stack frame depth of the function call stack when the advance command is obtained; modifying the maximum debug stack frame depth and the minimum debug stack frame depth in the debugger thread according to the recorded stack frame depth and the progress command, wherein the program execution thread of the virtual machine runs only when the current stack frame depth is greater than or equal to the minimum debug stack frame depth and less than or equal to the maximum debug stack frame depth; and The debugger thread is interrupted and the program execution thread is entered to execute the machine code program file, until the program execution thread is interrupted again and re-enters the debugger thread based on the modified minimum debug stack frame depth and maximum debug stack frame depth.
37. The debugging system according to claim 36, wherein: When the recorded stack frame depth is n and the advancing command is a step over command, the minimum debugging stack frame depth is modified to (n+1), and the maximum debugging stack frame depth is modified to correspond to infinity, wherein the step over command is used to interrupt the program running thread at the next position to be interrupted in the second bytecode program file, or to interrupt the program running thread when it reaches the next instruction of the subfunction that is not a subfunction that calls the current function in the source code corresponding to the smart contract, or to interrupt the program running thread when it reaches the parent function of the previous level that references the current function.
38. The debugging system according to claim 36, wherein: When the recorded stack frame depth is n and the progress command is a step out command, the minimum debug stack frame depth is modified to n, and the maximum debug stack frame depth is modified to correspond to infinity, wherein the step out command is used to cause the program running thread to interrupt when it proceeds to the next interrupt position corresponding to the second bytecode program file, or to cause the program running thread to interrupt when it proceeds to the upper-level parent function that references the current function.
39. The debugging system according to claim 36, wherein: When the advancing command is a step in command, the minimum debugging stack frame depth is modified to be greater than the maximum debugging stack frame depth, wherein the step in command is used to interrupt the program execution thread when it single-steps to the next instruction of the current function in the source code corresponding to the smart contract, or interrupt the program execution thread when it proceeds to the next level sub-function referenced by the current function.
40. The debugging system according to claim 35, wherein: The debugging command includes a viewing command, and the execution module is configured to perform the following operations: Determine the frame address of the current function corresponding to the current running position when the viewing command is obtained in the function call stack in the bytecode form; Filter out the variables containing the instructions of the current running position according to the frame address of the current function, and determine the index of the variable to be viewed corresponding to the viewing command; as well as According to the frame address of the current function and the index of the variable to be viewed, the variable to be viewed in source code form is acquired in combination with the function list information and the mapping information.
Citation Information
Patent Citations
JAVA card bytecode reference access test method
CN106649009A
Intelligent contract test method, computer readable storage medium and test terminal
CN109408373A
