使用 GNU GCC 和 GDB 开发调试应用程序


 

gcc 命令的使用

在使用GCC编译程序时,编译过程可以被细分为四个阶段:

  1.  预处理(Pre-Processing)
  2.  编译(Compiling)
  3.  汇编(Assembling)
  4.  链接(Linking)

如果不加任何参数, gcc 默认执行所有的操作,直接生成可执行文件。

以上四个阶段对应参数为:

1.        -E  只执行预处理

2.        –S  只编译,不汇编和链接

3.        –c  编译汇编不链接

4.        –o  链接成目标文件

 

GCC常用选项
-c 通知GCC取消链接步骤,即编译源码并在最后生成目标文件;

-Dmacro 定义指定的宏,使它能够通过源码中的#ifdef进行检验;

-E 不经过编译预处理程序的输出而输送至标准输出;

-g3 获得有关调试程序的详细信息,它不能与-o选项联合使用;

-Idirectory 在包含文件搜索路径的起点处添加指定目录;

-llibrary 提示链接程序在创建最终可执行文件时包含指定的库;

-O、-O2、-O3 将优化状态打开,该选项不能与-g选项联合使用;

-S 要求编译程序生成来自源代码的汇编程序输出;

-v 启动所有警报;

-Wall 在发生警报时取消编译操作,即将警报看作是错误;

-Werror 在发生警报时取消编译操作,即把报警当作是错误;

-w 禁止所有的报警。

 

gdb 调试工具的使用

基本gdb命令:

---------------------------------------------------------------------
命令          简写        功能
---------------------------------------------------------------------
file                            装入想要调试的可执行文件. 
kill            k              终止正在调试的程序. 
list            l              列出产生执行文件的源代码的一部分. 
next          n             执行一行源代码但不进入函数内部. 
step         s             执行一行源代码而且进入函数内部. 
continue  c              继续执行程序,直至下一中断或者程序结束。
run            r              执行当前被调试的程序.
quit           q              终止 gdb.
watch                       使你能监视一个变量的值而不管它何时被改变. 
catch                         设置捕捉点.
thread      t              查看当前运行程序的线程信息.
break       b             在代码里设置断点, 这将使程序执行到这里时被挂起. 
make                        使你能不退出 gdb 就可以重新产生可执行文件. 
shell                         使你能不离开 gdb 就执行 UNIXshell 命令.  
print         p             打印数据内容。
examine  x              打印内存内容。
backtracebt             查看函数调用栈的所有信息。 

 

 

实例分析

代码:

 

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

#define info(addr) printf("%30s @ %12p\n", #addr, &addr)
int global_var = 1;
static int static_global_var = 2;

int
local_function(int var)
{
        int local_function_var = var;
        static int static_local_function_var = 6;
        info(static_local_function_var);
        info(local_function_var);
        return local_function_var;
}

int
main(int argc, char *argv[])
{
        local_function(0);
        int main_function_var = 3;
        static int static_main_var = 4;
        info(global_var);
        info(static_global_var);
        info(static_main_var);
        info(main_function_var);
        info(main);
        info(local_function);
        return 0;
}

编译可调式的目标文件并运行:

jxdong@ubuntu-server:~/workspace/test$ gcc-o test test.c
jxdong@ubuntu-server:~/workspace/test$./test
    static_local_function_var @    0x60102c
           local_function_var @ 0x7fffc2487a3c
                    global_var @     0x601020
            static_global_var @     0x601024
               static_main_var @     0x601028
            main_function_var @ 0x7fffc2487a6c
                          main @     0x400541
                local_function @     0x4004f4

 

 

jxdong@ubuntu-server:~/workspace/test$ gcc-o test -g test.c 

jxdong@ubuntu-server:~/workspace/test$./test

    static_local_function_var @    0x60102c

           local_function_var @ 0x7fff97a0869c

                    global_var @     0x601020

            static_global_var @     0x601024

               static_main_var @     0x601028

            main_function_var @ 0x7fff97a086cc

                          main @     0x400541

                local_function @     0x4004f4

jxdong@ubuntu-server:~/workspace/test$ gdb./test

GNU gdb (Ubuntu/Linaro7.4-2012.04-0ubuntu2) 7.4-2012.04

Copyright (C) 2012 Free SoftwareFoundation, Inc.

License GPLv3+: GNU GPL version 3 or later<http://gnu.org/licenses/gpl.html>

This is free software: you are free tochange and redistribute it.

There is NO WARRANTY, to the extentpermitted by law.  Type "showcopying"

and "show warranty" for details.

This GDB was configured as"x86_64-linux-gnu".

For bug reporting instructions, please see:

<http://bugs.launchpad.net/gdb-linaro/>...

Reading symbols from/home/jxdong/workspace/test/test...done.

(gdb) b 32

Breakpoint 1 at 0x400608: file test.c, line32.

(gdb) run

Starting program:/home/jxdong/workspace/test/test

    static_local_function_var @    0x60102c

           local_function_var @ 0x7fffffffe24c

                    global_var @     0x601020

            static_global_var @     0x601024

               static_main_var @     0x601028

            main_function_var @ 0x7fffffffe27c

                          main @     0x400541

                local_function @     0x4004f4

 

Breakpoint 1, main (argc=1,argv=0x7fffffffe368) at test.c:32

32              return 0;

(gdb) bt full

#0 main (argc=1, argv=0x7fffffffe368) at test.c:32

       main_function_var = 3

       static_main_var = 4

(gdb) info proc

process 32545

cmdline = ‘/home/jxdong/workspace/test/test‘

cwd = ‘/home/jxdong/workspace/test‘

exe = ‘/home/jxdong/workspace/test/test‘

(gdb)

process 32545

cmdline =‘/home/jxdong/workspace/test/test‘

cwd = ‘/home/jxdong/workspace/test‘

exe = ‘/home/jxdong/workspace/test/test‘

(gdb) info proc map

process 32545

Mapped address spaces:

 

         Start Addr           End Addr       Size    Offset objfile

           0x400000           0x401000     0x1000        0x0 /home/jxdong/workspace/test/test

           0x600000           0x601000     0x1000        0x0 /home/jxdong/workspace/test/test

           0x601000           0x602000     0x1000    0x1000 /home/jxdong/workspace/test/test

     0x7ffff7a1a000    0x7ffff7bcf000   0x1b5000        0x0 /lib/x86_64-linux-gnu/libc-2.15.so

     0x7ffff7bcf000    0x7ffff7dcf000   0x200000   0x1b5000 /lib/x86_64-linux-gnu/libc-2.15.so

     0x7ffff7dcf000    0x7ffff7dd3000     0x4000   0x1b5000 /lib/x86_64-linux-gnu/libc-2.15.so

     0x7ffff7dd3000     0x7ffff7dd5000     0x2000  0x1b9000 /lib/x86_64-linux-gnu/libc-2.15.so

     0x7ffff7dd5000    0x7ffff7dda000     0x5000        0x0

     0x7ffff7dda000    0x7ffff7dfc000    0x22000        0x0 /lib/x86_64-linux-gnu/ld-2.15.so

     0x7ffff7fe9000     0x7ffff7fec000     0x3000        0x0

     0x7ffff7ff8000    0x7ffff7ffb000     0x3000        0x0

     0x7ffff7ffb000    0x7ffff7ffc000     0x1000        0x0 [vdso]

     0x7ffff7ffc000    0x7ffff7ffd000     0x1000    0x22000 /lib/x86_64-linux-gnu/ld-2.15.so

     0x7ffff7ffd000    0x7ffff7fff000     0x2000    0x23000 /lib/x86_64-linux-gnu/ld-2.15.so

     0x7ffffffde000    0x7ffffffff000    0x21000        0x0 [stack]

 0xffffffffff600000 0xffffffffff601000    0x1000        0x0 [vsyscall]

使用 GNU GCC 和 GDB 开发调试应用程序,布布扣,bubuko.com

使用 GNU GCC 和 GDB 开发调试应用程序

上一篇:c#DES加密 和解密


下一篇:C# WPF下自定义滚动条ScrollViewer样式