使用GDB 调试AT&T 汇编

注:以下内容为学习笔记,多数是从书本、资料中得来,只为加深印象,及日后参考。然而本人表达能力较差,写的不好。因非翻译、非转载,只好选原创,但多数乃摘抄,实为惭愧。但若能帮助一二访客,幸甚!


调试在编程中是不可缺少的,尝试使用GDB 调试一下昨晚的AT&T汇编代码:

[plain]  view plain copy print ?
  1. liury@liury-laptop:~/program/asm/cpuid$ as -gstabs -o cpuid.o cpuid.s  
  2. liury@liury-laptop:~/program/asm/cpuid$ ls  
  3. cpuid.o  cpuid.s  cpuid.s~  makefile  makefile~  
  4. liury@liury-laptop:~/program/asm/cpuid$ ld -o cpuid cpuid.o  
  5. liury@liury-laptop:~/program/asm/cpuid$ gdb cpuid   
  6. GNU gdb (GDB) 7.1-ubuntu  
  7. Copyright (C) 2010 Free Software Foundation, Inc.  
  8. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>  
  9. This is free software: you are free to change and redistribute it.  
  10. There is NO WARRANTY, to the extent permitted by law.  Type "show copying"  
  11. and "show warranty" for details.  
  12. This GDB was configured as "i486-linux-gnu".  
  13. For bug reporting instructions, please see:  
  14. <http://www.gnu.org/software/gdb/bugs/>...  
  15. Reading symbols from /home/liury/program/asm/cpuid/cpuid...done.  
  16. (gdb) l  
  17. 1   # cpuid.s Sample program to extract the processor Vendor ID  
  18. 2   .section .data  
  19. 3   output:  
  20. 4       .ascii "The processor Vendor ID is 'XXXXXXXXXXXX'\n"  
  21. 5     
  22. 6   .section .text  
  23. 7   .global _start  
  24. 8     
  25. 9   _start:  
  26. 10      movl    $0, %eax        # The CPUID output option(the Vendor ID string)   
  27. (gdb) break *_start  
  28. Breakpoint 1 at 0x8048074: file cpuid.s, line 10.  
  29. (gdb) r  
  30. Starting program: /home/liury/program/asm/cpuid/cpuid   
  31. The processor Vendor ID is 'GenuineIntel'  
  32.   
  33. Program exited normally.  
  34. (gdb) q  


发现断点处没有停,后来得知此乃GDB的一个BUG...而必须在_start后面的第一个指令码元素的位置包含一条伪指令。
需要在_start后面加一条NOP指令:

[plain]  view plain copy print ?
  1. _start:  
  2.     nop  
  3.     movl    $0, %eax  
  4.     cpuid  
[plain]  view plain copy print ?
  1. liury@liury-laptop:~/program/asm/cpuid$ gdb cpuid  
  2. GNU gdb (GDB) 7.1-ubuntu  
  3. Copyright (C) 2010 Free Software Foundation, Inc.  
  4. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>  
  5. This is free software: you are free to change and redistribute it.  
  6. There is NO WARRANTY, to the extent permitted by law.  Type "show copying"  
  7. and "show warranty" for details.  
  8. This GDB was configured as "i486-linux-gnu".  
  9. For bug reporting instructions, please see:  
  10. <http://www.gnu.org/software/gdb/bugs/>...  
  11. Reading symbols from /home/liury/program/asm/cpuid/cpuid...done.  
  12. (gdb) l  
  13. 1   # cpuid.s Sample program to extract the processor Vendor ID  
  14. 2   .section .data  
  15. 3   output:  
  16. 4       .ascii "The processor Vendor ID is 'XXXXXXXXXXXX'\n"  
  17. 5     
  18. 6   .section .text  
  19. 7   .global _start  
  20. 8     
  21. 9   _start:  
  22. 10      nop  
  23. (gdb) break *_start+1  
  24. Breakpoint 1 at 0x8048075: file cpuid.s, line 11.  
  25. (gdb) r  
  26. Starting program: /home/liury/program/asm/cpuid/cpuid   
  27.   
  28. Breakpoint 1, _start () at cpuid.s:11  
  29. 11      movl    $0, %eax        # The CPUID output option(the Vendor ID string)   
  30. (gdb) next  
  31. 12      cpuid  
  32. (gdb) info registers  
  33. eax            0x0  0  
  34. ecx            0x0  0  
  35. edx            0x0  0  
  36. ebx            0x0  0  
  37. esp            0xbffff0a0   0xbffff0a0  
  38. ebp            0x0  0x0  
  39. esi            0x0  0  
  40. edi            0x0  0  
  41. eip            0x804807a    0x804807a <_start+6>  
  42. eflags         0x212    [ AF IF ]  
  43. cs             0x73 115  
  44. ss             0x7b 123  
  45. ds             0x7b 123  
  46. es             0x7b 123  
  47. fs             0x0  0  
  48. gs             0x0  0  
  49. (gdb) n  
  50. 13      movl    $output,%edi  
  51. (gdb) info registers  
  52. eax            0xa  10  
  53. ecx            0x6c65746e   1818588270  
  54. edx            0x49656e69   1231384169  
  55. ebx            0x756e6547   1970169159  
  56. esp            0xbffff0a0   0xbffff0a0  
  57. ebp            0x0  0x0  
  58. esi            0x0  0  
  59. edi            0x0  0  
  60. eip            0x804807c    0x804807c <_start+8>  
  61. eflags         0x212    [ AF IF ]  
  62. cs             0x73 115  
  63. ss             0x7b 123  
  64. ds             0x7b 123  
  65. es             0x7b 123  
  66. fs             0x0  0  
  67. gs             0x0  0  
  68. (gdb) print /x $ecx  
  69. $1 = 0x6c65746e  
  70. (gdb) x /42cb &output  
  71. 0x80490ac <output>:   84 'T'  104 'h' 101 'e' 32 ' '  112 'p' 114 'r' 111 'o' 99 'c'  
  72. 0x80490b4 <output+8>: 101 'e' 115 's' 115 's' 111 'o' 114 'r' 32 ' '  86 'V'  101 'e'  
  73. 0x80490bc <output+16>:    110 'n' 100 'd' 111 'o' 114 'r' 32 ' '  73 'I'  68 'D'  32 ' '  
  74. 0x80490c4 <output+24>:    105 'i' 115 's' 32 ' '  39 '\'' 88 'X'  88 'X'  88 'X'  88 'X'  
  75. 0x80490cc <output+32>:    88 'X'  88 'X'  88 'X'  88 'X'  88 'X'  88 'X'  88 'X'  88 'X'  
  76. 0x80490d4 <output+40>:    39 '\'' 10 '\n'  
  77. (gdb) c  
  78. Continuing.  
  79. The processor Vendor ID is 'GenuineIntel'  
  80.   
  81. Program exited normally.  
  82. (gdb) q  

注释:

break 加断点

run 运行

next 单步运行

info registers 显示所有寄存器的值

print 显示特定寄存器的值

print /d 显示十进制的值

print /t 显示二进制的值

print /x 显示十六进制的值


x 显示特定内存地址的内容

x /nyz

n是要显示的字段数,y是输出格式:

c 用于字符

d 十进制

x 十六进制
z是要显示的字段长度

b 字节8位

h 16位

w 32位字


q 退出gdb

转载于:https://my.oschina.net/locusxt/blog/342473

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值