(转载)如何用gdb进行汇编级的调试

(转载)http://blog.csdn.net/haiyan0106/article/details/1627007

     我的方法是编译成汇编文件后(gcc -S) ,然后用as编译成.o文件,最后再生成执行文件。
给你一个我的调试过程。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void test(void)
{
    int number = 10;
    printf("The number: %d\n", number);
}

int main(int argc, char* argv[])
{
    test();

    return 0;
}

(1)生成.s文件

(2)生成.o文件

(3)生成可执行文件

~ # gdb main
GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/main...done.
(gdb) l
1               .file   "main.c"
2               .section        .rodata
3       .LC0:
4               .string "The number: %d\n"
5               .text
6       .globl test
7               .type   test, @function
8       test:
9               pushl   %ebp
10              movl    %esp, %ebp
(gdb) l
11              subl    $40, %esp
12              movl    $10, -12(%ebp)
13              movl    $.LC0, %eax
14              movl    -12(%ebp), %edx
15              movl    %edx, 4(%esp)
16              movl    %eax, (%esp)
17              call    printf
18              leave
19              ret
20              .size   test, .-test
(gdb) l
21      .globl main
22              .type   main, @function
23      main:
24              pushl   %ebp
25              movl    %esp, %ebp
26              andl    $-16, %esp
27              call    test
28              movl    $0, %eax
29              movl    %ebp, %esp
30              popl    %ebp
(gdb) l
31              ret
32              .size   main, .-main
33              .ident  "GCC: (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3"
34              .section        .note.GNU-stack,"",@progbits
(gdb) l
Line number 35 out of range; main.s has 34 lines.
(gdb)
原文地址:https://www.cnblogs.com/Robotke1/p/3088630.html