6 compiler-construction assembly gdb debug-symbols
介绍。对于我的编译器课程,我必须将一些语言翻译为汇编语言。现在我的代码出现了段错误,我很难对其进行调试。具有监视变量的能力会大大简化过程,但最终的 asm 代码只有寄存器及其导数。
问题。如何手动为 gdb 创建调试符号?我相信我可以传播所有必要的信息。会创建单独的文件还是将其作为 asm 的一部分?在前一种情况下如何让 gdb 知道该文件?
一些手册的链接就足够了。我搜索了一个,只能找到“gcc -g”和一些有关单独生成符号文件的问题。
GCC 告诉发生错误的最后一个标签。但它没有给出行号或堆栈跟踪。
介绍 Jav 调试 我在 cpp 中有一个类似的实用程序,名为rep(),它通过可变参数模板最多接受 10 个参数。NASM 宏不支持可变参数宏;但它支持基于参数数量的重载宏。
asm_io.inc Paul A. Carter PC 汇编语言 2002 年 12 月 12 日
我添加了调试宏
extern read_int, print_int, print_string, print_line
extern read_char, print_char, print_nl
extern sub_dump_regs, sub_dump_mem, sub_dump_math, sub_dump_stack
extern _printf
%macro dump_regs 1
push dword %1
call sub_dump_regs
%endmacro
;
; usage: dump_mem label, start-address, # paragraphs
%macro dump_mem 3
push dword %1
push dword %2
push dword %3
call sub_dump_mem
%endmacro
%macro dump_math 1
push dword %1
call sub_dump_math
%endmacro
%macro dump_stack 3
push dword %3
push dword %2
push dword %1
call sub_dump_stack
%endmacro
; private macro
; number of stack used by implementation
%define __jav_len__ 5
; private macro
; saves all registers so that you get that back how you left them
%macro __jav_save_registers__ 0
mov [esp-4*1], edx
mov [esp-4*2], ecx
mov [esp-4*3], eax
mov [esp-4*4], ebx
lahf ; modifies eax
mov [esp-4*5], eax
mov eax, [esp-4*3] ; restore the value of eax
%endmacro
; private macro
; restores the registers to the values you left them in
%macro __jav_restore_registers__ 0
pop eax
sahf
pop ebx
pop eax
pop ecx
pop edx
%endmacro
; private macro
; gets a scalar value
%macro __jav_get__ 1
mov ebx, dword %1
%endmacro
; private macro
; dereference and gets the scalar
%macro __jav_get__ 2
mov ebx, %1
mov ebx, [ebx+%2]
%endmacro
; private macro
; used to copy dword
; return value stored in ebx
%macro __jav_copy32__ 2+
__jav_get__ %2
mov %1, ebx
mov ebx, [esp-4*4] ; restore the value of ebx
%endmacro
; public macro
; pass this as argument to debug macro
; dereferences obj and return scalar at index offset
%define jav_member(obj,index) { obj, index }
; param 1: string format to print
%macro debug 1
section .data
%%msg db %1, 10, 0
section .text
__jav_save_registers__
__jav_copy32__ [esp-4*(__jav_len__+1)], %%msg
sub esp, 4*(__jav_len__+1)
call _printf
add esp, 4*1
__jav_restore_registers__
%endmacro
; param 1: string format to print
; param 2: arguments to string format
%macro debug 2
section .data
%%msg db %1, 10, 0
section .text
__jav_save_registers__
__jav_copy32__ [esp-4*(__jav_len__+1)], %2
__jav_copy32__ [esp-4*(__jav_len__+2)], %%msg
sub esp, 4*(__jav_len__+2)
call _printf
add esp, 4*2
__jav_restore_registers__
%endmacro
%macro debug 3
section .data
%%msg db %1, 10, 0
section .text
__jav_save_registers__
__jav_copy32__ [esp-4*(__jav_len__+1)], %3
__jav_copy32__ [esp-4*(__jav_len__+2)], %2
__jav_copy32__ [esp-4*(__jav_len__+3)], %%msg
sub esp, 4*(__jav_len__+3)
call _printf
add esp, 4*3
__jav_restore_registers__
%endmacro
%macro debug 4
section .data
%%msg db %1, 10, 0
section .text
__jav_save_registers__
__jav_copy32__ [esp-4*(__jav_len__+1)], %4
__jav_copy32__ [esp-4*(__jav_len__+2)], %3
__jav_copy32__ [esp-4*(__jav_len__+3)], %2
__jav_copy32__ [esp-4*(__jav_len__+4)], %%msg
sub esp, 4*(__jav_len__+4)
call _printf
add esp, 4*4
__jav_restore_registers__
%endmacro
%macro debug 5
section .data
%%msg db %1, 10, 0
section .text
__jav_save_registers__
__jav_copy32__ [esp-4*(__jav_len__+1)], %5
__jav_copy32__ [esp-4*(__jav_len__+2)], %4
__jav_copy32__ [esp-4*(__jav_len__+3)], %3
__jav_copy32__ [esp-4*(__jav_len__+4)], %2
__jav_copy32__ [esp-4*(__jav_len__+5)], %%msg
sub esp, 4*(__jav_len__+5)
call _printf
add esp, 4*5
__jav_restore_registers__
%endmacro
%macro debug 6
section .data
%%msg db %1, 10, 0
section .text
__jav_save_registers__
__jav_copy32__ [esp-4*(__jav_len__+1)], %6
__jav_copy32__ [esp-4*(__jav_len__+2)], %5
__jav_copy32__ [esp-4*(__jav_len__+3)], %4
__jav_copy32__ [esp-4*(__jav_len__+4)], %3
__jav_copy32__ [esp-4*(__jav_len__+5)], %2
__jav_copy32__ [esp-4*(__jav_len__+6)], %%msg
sub esp, 4*(__jav_len__+6)
call _printf
add esp, 4*6
__jav_restore_registers__
%endmacro
%macro debug 7
section .data
%%msg db %1, 10, 0
section .text
__jav_save_registers__
__jav_copy32__ [esp-4*(__jav_len__+1)], %7
__jav_copy32__ [esp-4*(__jav_len__+2)], %6
__jav_copy32__ [esp-4*(__jav_len__+3)], %5
__jav_copy32__ [esp-4*(__jav_len__+4)], %4
__jav_copy32__ [esp-4*(__jav_len__+5)], %3
__jav_copy32__ [esp-4*(__jav_len__+6)], %2
__jav_copy32__ [esp-4*(__jav_len__+7)], %%msg
sub esp, 4*(__jav_len__+7)
call _printf
add esp, 4*7
__jav_restore_registers__
%endmacro
%macro debug 8
section .data
%%msg db %1, 10, 0
section .text
__jav_save_registers__
__jav_copy32__ [esp-4*(__jav_len__+1)], %8
__jav_copy32__ [esp-4*(__jav_len__+2)], %7
__jav_copy32__ [esp-4*(__jav_len__+3)], %6
__jav_copy32__ [esp-4*(__jav_len__+4)], %5
__jav_copy32__ [esp-4*(__jav_len__+5)], %4
__jav_copy32__ [esp-4*(__jav_len__+6)], %3
__jav_copy32__ [esp-4*(__jav_len__+7)], %2
__jav_copy32__ [esp-4*(__jav_len__+8)], %%msg
sub esp, 4*(__jav_len__+8)
call _printf
add esp, 4*8
__jav_restore_registers__
%endmacro
%macro debug 9
section .data
%%msg db %1, 10, 0
section .text
__jav_save_registers__
__jav_copy32__ [esp-4*(__jav_len__+1)], %9
__jav_copy32__ [esp-4*(__jav_len__+2)], %8
__jav_copy32__ [esp-4*(__jav_len__+3)], %7
__jav_copy32__ [esp-4*(__jav_len__+4)], %6
__jav_copy32__ [esp-4*(__jav_len__+5)], %5
__jav_copy32__ [esp-4*(__jav_len__+6)], %4
__jav_copy32__ [esp-4*(__jav_len__+7)], %3
__jav_copy32__ [esp-4*(__jav_len__+8)], %2
__jav_copy32__ [esp-4*(__jav_len__+9)], %%msg
sub esp, 4*(__jav_len__+9)
call _printf
add esp, 4*9
__jav_restore_registers__
%endmacro
%macro debug 10
section .data
%%msg db %1, 10, 0
section .text
__jav_save_registers__
__jav_copy32__ [esp-4*(__jav_len__+1)], %10
__jav_copy32__ [esp-4*(__jav_len__+2)], %9
__jav_copy32__ [esp-4*(__jav_len__+3)], %8
__jav_copy32__ [esp-4*(__jav_len__+4)], %7
__jav_copy32__ [esp-4*(__jav_len__+5)], %6
__jav_copy32__ [esp-4*(__jav_len__+6)], %5
__jav_copy32__ [esp-4*(__jav_len__+7)], %4
__jav_copy32__ [esp-4*(__jav_len__+8)], %3
__jav_copy32__ [esp-4*(__jav_len__+9)], %2
__jav_copy32__ [esp-4*(__jav_len__+10)], %%msg
sub esp, 4*(__jav_len__+10)
call _printf
add esp, 4*10
__jav_restore_registers__
%endmacro
Run Code Online (Sandbox Code Playgroud)