Common debugging method and system for source code, byte code and target code
By converting the source code into bytecode and merging the line number information to generate a new source code file, and using adjusted debugging instructions to display the source code and bytecode information simultaneously during the debugging process, the problem that the debugger cannot display the source code and target code at the same time is solved, and single-step debugging of the source code and bytecode is realized.
Patent Information
- Application Number
- CN202510843388.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-23
- Publication Date
- 2025-09-19
AI Technical Summary
Existing debuggers cannot simultaneously display the correspondence between source code, bytecode, and target code, resulting in the inability to meet developers' needs during the debugging process.
By converting source code into bytecode, obtaining their correspondence and merging line number information, a new source code file is generated. The adjusted debugging instructions are used to display source code and bytecode information simultaneously during the debugging process, and new instructions are added for single-step debugging.
It implements single-step debugging at the source code level and bytecode level to meet the debugging needs of developers and show the correspondence between source code, bytecode and executable program.
Smart Images

Figure CN120670276A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of computers, and in particular relates to a method and system for jointly debugging source code, byte code and target code. Background Art
[0002] During the compilation process of programming languages such as Java and ArkTS, the source code is usually compiled into bytecode first, and then when the virtual machine is running, the bytecode is interpreted and executed or compiled into target code and then executed. The bytecode and source code generated by these languages are often not displayed together when using a debugger to debug the target code. Taking ArkTS compilation as an example, it is not compiled directly into assembly code, but first generates Ark bytecode during the compilation process, and then compiles the Ark bytecode into assembly code. Figure 1 shown.
[0003] However, in many cases, it's essential to simultaneously observe the correspondence between source code, bytecode, and object code. Traditional debuggers currently cannot meet this requirement by displaying both types of debugging information simultaneously during debugging. Therefore, supporting the simultaneous debugging of source code, bytecode, and object code has become a pressing issue. Summary of the Invention
[0004] To solve the above technical problems, the present invention provides a method for debugging source code, bytecode and target code together, comprising the following steps:
[0005] Step S1: converting the source code into bytecode; obtaining the correspondence between the source code and the bytecode, combining the bytecode with the source code by row and column numbers, and generating a new source code file;
[0006] Step S2: merging the line number information of the source code with the line number information of the bytecode to form a new line number; attaching the new line number as debugging information to the debugging instruction to obtain an adjusted debugging instruction;
[0007] Step S3: When debugging the source code file using the adjusted debugging instructions, source code and bytecode information are displayed simultaneously; and new instructions are added for single-step debugging of the bytecode.
[0008] Beneficial effects:
[0009] The present invention provides a method for jointly debugging source code, bytecode and target code. During the debugging process, the correspondence between source code, bytecode and executable program can be displayed, supporting developers to perform single-step debugging at the source code level and single-step debugging at the bytecode level, greatly meeting the debugging needs of developers. BRIEF DESCRIPTION OF THE DRAWINGS
[0010] Figure 1 ArkTS compilation process diagram;
[0011] Figure 2 A schematic flow chart of a method for jointly debugging source code, bytecode, and target code according to the present invention;
[0012] Figure 3 This is a schematic diagram of the debugging information processing principle of the ArkTS compiler;
[0013] Figure 4 This is a schematic diagram of the debugger's execution principle after adjustment;
[0014] Figure 5 This is a structural block diagram of a system for jointly debugging source code, bytecode, and target code according to the present invention. DETAILED DESCRIPTION
[0015] In order to make the objectives, technical solutions and advantages of the present invention more clearly understood, the present invention is further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only intended to illustrate the present invention and are not intended to limit the present invention. In addition, the technical features involved in the various embodiments of the present invention described below may be combined with each other as long as they do not conflict with each other.
[0016] Example 1
[0017] like Figure 2 As shown, an embodiment of the present invention provides a method for debugging source code, bytecode and target code together, comprising the following steps:
[0018] Step S1: Convert the source code into bytecode; obtain the correspondence between the source code and the bytecode, combine the bytecode with the source code through the row and column numbers, and generate a new source code file;
[0019] Step S2: merging the line number information of the source code with the line number information of the bytecode to form a new line number; attaching the new line number as debugging information to the debugging instruction to obtain an adjusted debugging instruction;
[0020] Step S3: When debugging the source code file using the adjusted debugging instructions, source code and bytecode information are displayed simultaneously; and new instructions are added for single-step debugging of the bytecode.
[0021] The embodiment of the present invention takes the ArkTS language and compiler as an example, and its principle diagram is as follows Figure 3 shown.
[0022] In one embodiment, the above step S1: converting the source code into bytecode; obtaining the correspondence between the source code and the bytecode, combining the bytecode with the source code by row and column numbers, and generating a new source code file specifically includes:
[0023] Step S11: Obtaining the row-column correspondence between the source code and the bytecode through the compiler;
[0024] Step S12: The bytecode is combined with the source code by row and column numbers to obtain a new source code file, and a comment is automatically added at the end of each line of new source code to display the bytecode corresponding to each line of source code.
[0025] For example, a source code is as follows: 1 declare function print(arg: any): string; 2 3 function ghn1(n: number): number { 4 return n + n; 5} 6 function ghn(){ 7 for(let i = 1; i < 10; i++){ 8 let a = i + i; 9 let b = i i; 10 let c = i / i; 11 let d = i - i; 12 let e = ghn1(i); 13 print(a); 14 print(b); 15 print(c); 16 print(d); 17 print(e); 18} 19} 20 21 ghn()
[0026] The correspondence between some row and column numbers between bytecode and source code can be obtained through the compiler, as shown below: ldai 0x1 # line: 7 # column: 16 sta v5 # line: 7 # column: 12 label@5: lda v5 # line: 7 # column: 19 sta v6 # line: 7 # column: 19 ldai 0xa # line: 7 # column: 23 less 0x0, v6 # line: 7 # column: 19 jeqz label@77 # line: 7 # column: 19 lda v5 # line: 8 # column: 14 sta v11 # line: 8 # column: 18 lda v5 # line: 8 # column: 18 add2 0x1, v11 # line: 8 # column: 18 sta v6 # line: 8 # column: 10
[0027] Based on the correspondence between the source code and the bytecode line and column numbers above, create a new source code file. A comment section (starting with / / ) is automatically added to the end of each line of source code. The comment section displays the bytecode corresponding to each line of source code: 1 declare function print(arg: any): string; 2 3 function ghn1(n: number): number { 4 return n + n; / / lda v3; sta v5; lda v3; add2 0x0, v5; abc funcoffset:1-4 5} / / return; abc func offset:5-5 6 function ghn(){ 7 for(let i = 1; i<10; i++){ / / ldai 0x1; sta v5; label@5: lda v5;sta v6; ldai 0xa; less 0x0, v6; jeqz label@77; lda v5; sta v6; lda v6;tonumeric 0x16; sta v6; lda v6;inc 0x17; stav5; lda v6; jmp label@5; abc funcoffset:1-7 65-74 8 let a = i + i; / / lda v5; sta v11; lda v5; add2 0x1, v11; stav6; abc func offset:8-12 9 let b = i i; / / lda v5; sta v11; lda v5; mul2 0x2, v11; stav7; abc func offset:13-17 10 let c = i / i; / / lda v5; sta v11; lda v5; div2 0x3, v11; stav8; abc func offset:18-22 11 let d = i - i; / / lda v5; sta v11; lda v5; sub2 0x4, v11; stav9; abc func offset:23-27 12 let e = ghn1(i); / / ldlexvar 0x0, 0x0; sta v11; lda v5; stav12; lda v11; callarg1 0x5, v12; sta v10; abcfunc offset:28-34 13 print(a); / / tryldglobalbyname 0x7, print; sta v11; lda v6;sta v12; lda v11; callarg1 0x8, v12; abc func offset:35-40 14 print(b); / / tryldglobalbyname 0xa, print; sta v11; lda v7;sta v12; lda v11; callarg1 0xb, v12; abc func offset:41-46 15 print(c); / / tryldglobalbyname 0xd, print; sta v11; lda v8; stav12; lda v11; callarg1 0xe, v12; abc func offset:47-52 16 print(d); / / tryldglobalbyname 0x10, print; sta v11; lda v9;sta v12; lda v11; callarg1 0x11, v12; abc func offset:53-58 17 print(e); / / tryldglobalbyname 0x13, print; sta v11; lda v10;sta v12; lda v11; callarg1 0x14, v12; abc func offset:59-64 18} 19} / / returnundefined; abc func offset:75-75 20 21 ghn() / / lda v3; sta v6; lda v6; callarg0 0x2; abc func offset:1-4
[0028] In this way, when debugging source code in the debugger, when viewing the source code file instruction list, the source code and bytecode information can be displayed at the same time.
[0029] In one embodiment, the above step S2: merging the line number information of the source code with the line number information of the bytecode to form a new line number; and appending the new line number as debugging information to the debugging instruction to obtain the adjusted debugging instruction, specifically includes:
[0030] Step S21: Construct a 64-bit data structure for storing the new line number, wherein the upper 32 bits of the new line number are used to store the line number information of the source code, and the lower 32 bits are used to store the line number information of the bytecode, as shown in Table 1:
[0031] Table 1 New row number (64 bits)
[0032] Step S22: Add the new line number to the debugging instruction so that the compiler can obtain the line number information of the source code and bytecode during the debugging process.
[0033] Subsequently, by using LLVM IR or other technical methods, new line numbers are attached to the instructions as debugging information, so that during debugging, the debugger will be able to utilize the debugging information of the source code and bytecode.
[0034] For example, using LLVM IR as the intermediate code, the line number is 38654705731, which means the source code is line 9 and the bytecode is line 67 of the intermediate code: B20: ; preds = %B162, %B19 %54 = add i64 %0, 7608 %55 = inttoptr i64 %54 to i64 %COStub_Add = load i64, i64 %55, align 4 %COStub_Add-cast = inttoptr i64 %COStub_Add to i64 addrspace(1) (i64, i64 addrspace(1) , i64 addrspace(1) ) %56 = call i64 addrspace(1) %COStub_Add-cast(i64 %0, i64addrspace(1) %32, i64 addrspace(1) %32) [ "deopt"(i64 -1, i64 34) ], !dbg!10 %57 = icmp eq i64 addrspace(1) %56, inttoptr (i64 10 to i64addrspace(1) ) br i1 %57, label %B21, label %B22 !10 = !DILocation(line: 38,654,705,731, column: 18, scope: !8)
[0035] In one embodiment, the above step S3: when debugging the source code file using the adjusted debugging instructions, the source code and bytecode information are displayed simultaneously; and new instructions are added for single-step debugging of the bytecode, specifically including:
[0036] Step S31: adjusting the debugger's next instruction so that after executing next, the upper 32 bits of the data structure are obtained for single-step debugging of the source code;
[0037] Step S32: Add a new instruction named next_abc in the debugger. Its logic is similar to the original next instruction. It obtains the lower 32 bits of the data structure and is used for single-step debugging of the bytecode.
[0038] Step S33: Use the list command in the debugger to view the new source code file to display the line numbers of the source code and bytecode at the same time.
[0039] The debugger execution principle after adjustment is as follows Figure 4 shown.
[0040] Example 2
[0041] like Figure 5 As shown, an embodiment of the present invention provides a system for debugging source code, bytecode, and target code together, including the following modules:
[0042] Generate new source code module 41, used to convert source code into bytecode; obtain the corresponding relationship between source code and bytecode, combine the bytecode with the source code through row and column numbers, and generate a new source code file;
[0043] The debugging instruction adjustment module 42 is used to merge the line number information of the source code with the line number information of the bytecode to form a new line number; and append the new line number as debugging information to the debugging instruction to obtain an adjusted debugging instruction;
[0044] The debugging module 43 is used to display the source code and bytecode information simultaneously when debugging the source code file using the adjusted debugging instructions; and to add new instructions for single-step debugging of the bytecode.
[0045] A source code, byte code and target code joint debugging device includes one or more electronic devices, wherein the one or more electronic devices are used to implement a source code, byte code and target code joint debugging method.
[0046] An electronic device includes: one or more processors; a memory for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement a method for jointly debugging source code, bytecode and target code.
[0047] A computer-readable storage medium stores executable instructions, which, when executed by a processor, enable the processor to implement a method for jointly debugging source code, bytecode, and target code.
[0048] A non-transitory computer-readable storage medium stores a computer program, which, when executed by a processor, implements a method for jointly debugging source code, bytecode, and object code.
[0049] The foregoing description is intended only to provide specific embodiments of the present invention, which will enable those skilled in the art to understand and implement the present invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention is not limited to the embodiments shown herein, but is intended to conform to the widest scope consistent with the principles and novel features of the present invention.
Claims
1. A method for debugging source code, bytecode and target code together, characterized in that: include: Step S1: convert the source code into bytecode; Obtaining a correspondence between the source code and the bytecode, combining the bytecode with the source code by row and column numbers, and generating a new source code file; Step S2: Merge the line number information of the source code and the line number information of the bytecode to form a new line number; Adding the new line number as debugging information to the debugging instruction to obtain an adjusted debugging instruction; Step S3: When debugging the source code file using the adjusted debugging instructions, source code and bytecode information are displayed simultaneously; and new instructions are added for single-step debugging of the bytecode.
2. The method for debugging source code, bytecode and target code together according to claim 1, wherein: The step S1: converting the source code into bytecode; obtaining the correspondence between the source code and the bytecode, combining the bytecode with the source code by row and column numbers, and generating a new source code file, specifically includes: Step S11: Obtaining the row-column correspondence between the source code and the bytecode through the compiler; Step S12: The bytecode is combined with the source code by row and column numbers to obtain a new source code file, and a comment is automatically added at the end of each line of new source code to display the bytecode corresponding to each line of source code.
3. The method for debugging source code, bytecode and target code together according to claim 2, wherein: Step S2: merging the line number information of the source code and the line number information of the bytecode to form a new line number; The new line number is appended to the debugging instruction as debugging information to obtain an adjusted debugging instruction, which specifically includes: Step S21: construct a 64-bit data structure for storing the new line number, wherein the upper 32 bits of the new line number are used to store the line number information of the source code, and the lower 32 bits are used to store the line number information of the bytecode; Step S22: Add the new line number to the debugging instruction so that the compiler can obtain the line number information of the source code and bytecode during the debugging process.
4. The method for debugging source code, bytecode and target code together according to claim 3, wherein: Step S3: When debugging the source code file using the adjusted debugging instructions, the source code and bytecode information are displayed simultaneously; New instructions are added for single-step debugging of bytecode, including: Step S31: adjusting the next instruction of the debugger so that after executing next, the upper 32 bits of the data structure are obtained for single-step debugging of the source code; Step S32: Add a new instruction named next_abc in the debugger, whose logic is similar to the original next instruction, to obtain the lower 32 bits of the data structure for single-step debugging of the bytecode; Step S33: Use the list command in the debugger to view the new source code file to simultaneously display the line numbers of the source code and the bytecode.
5. A system for debugging source code, byte code and object code, characterized in that: Includes the following modules: Generate a new source code module for converting the source code into bytecode; obtain the correspondence between the source code and the bytecode, combine the bytecode with the source code by row and column numbers, and generate a new source code file; Adjust the debugging instruction module to merge the line number information of the source code with the line number information of the bytecode to form a new line number; Adding the new line number as debugging information to the debugging instruction to obtain an adjusted debugging instruction; A debugging module, configured to simultaneously display source code and bytecode information when debugging a source code file using the adjusted debugging instructions; And new instructions are added for single-step debugging of bytecode.
6. A device for debugging source code, byte code and target code, characterized in that: The method comprises one or more electronic devices, wherein the one or more electronic devices are used to implement the method according to any one of claims 1 to 4.
7. An electronic device, characterized in that: include: one or more processors; A memory for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors are enabled to implement the method according to any one of claims 1 to 4.
8. A computer-readable storage medium, characterized in that Executable instructions are stored thereon, and when the instructions are executed by a processor, the processor implements the method according to any one of claims 1 to 4.
9. A non-transitory computer-readable storage medium, characterized in that A computer program is stored thereon, and when the computer program is executed by a processor, the method according to any one of claims 1 to 4 is implemented.