GDB程序运行时的参数设置

晨曦之光 发布于 2012/03/09 14:13
阅读 1K+
收藏 0

本博客(http://blog.csdn.net/livelylittlefish)贴出作者(三二一@小鱼)相关研究、学习内容所做的笔记,欢迎广大朋友指正!

 

GDB程序运行时的参数设置

 

 

    0. 引子

    我们在编程中,有时需要在程序启动/运行时指定参数,例如最常见的main函数。如果我们想用gdb调试程序时,该如何输入参数?这就是本文要讨论的内容。

     

    1. 实验

    该程序动态加载libc的数学库文件(即math库),然后在其中搜索sin函数,并求出p/2的正玄值。该数学库模块文件通过参数输入,该模块位于位于/lib/libdl-2.7.so

     

    文件名testRundl.c,代码如下。

    #include <stdio.h>

    #include <dlfcn.h>

     

    int main (int argc, char* argv[])

    {

    void* handle;

    char* error;

    double (*func)(double);

     

    //load the shared object library by input parameter

    handle = dlopen(argv[1], RTLD_NOW);

    if (0 == handle)

    {

        printf("open library %s error: %s/n", argv[1], dlerror());

    return -1;

    }

     

    //find the given symbol in the shared object library loaded by dlopen

    func = dlsym(handle, "sin");

    if ((error = dlerror()) != NULL)

    {

        printf("find symbol %s error: %s/n", argv[2], error);

    goto _exit_;

    }

     

    printf("result = %f/n", func(3.1415926/2));

     

    _exit_:

        dlclose(handle);

     

        return 0;

    }

     

    编译并运行。

    # gcc -o testRundl testRundl.c -ldl

    # ./testRundl /lib/libm-2.7.so

    result = 1.000000

    -ldl表示使用DL(Dynamic Loading)库,位于/lib/libdl-2.7.so

     

    我们也可以看看错误的情况。

    # ./testRundl

    find symbol HOSTNAME=yu29 error: ./testRundl: undefined symbol: sin

    # ./testRundl /lib/libm-2.7.soo

    open library /lib/libm-2.7.soo error: /lib/libm-2.7.soo: cannot open shared object file: No such file or directory

     

    2. gdb设置参数

     

    如果我们需要调试代码,则需要在运行时加上参数,对其设置参数的方法有两种,分别列出如下。

     

    通过set args命令设置输入参数。

    # gcc -g -o testRundl testRundl.c -ldl  //需要重新编译,以加入调试信息

    # gdb ./testRundl

    GNU gdb Red Hat Linux (6.6-35.fc8rh)

    Copyright (C) 2006 Free Software Foundation, Inc.

    GDB is free software, covered by the GNU General Public License, and you are

    welcome to change it and/or distribute copies of it under certain conditions.

    Type "show copying" to see the conditions.

    There is absolutely no warranty for GDB.  Type "show warranty" for details.

    This GDB was configured as "i386-redhat-linux-gnu"...

    Using host libthread_db library "/lib/libthread_db.so.1".

    (gdb) b 11

    Breakpoint 1 at 0x8048508: file testRundl.c, line 11.

    (gdb) set args /lib/libm-2.7.so  //设置输入参数,即数学库模块

    (gdb) r

    Starting program: /home/zubo/linux/2010-02-05 testDlopen/testRundl /lib/libm-2.7.so

     

    warning: Missing the separate debug info file: /usr/lib/debug/.build-id/ac/2eeb206486bb7315d6ac4cd64de0cb50838ff6.debug

     

    warning: Missing the separate debug info file: /usr/lib/debug/.build-id/db/a292aff9720bfc3f25c53fa8e469168460a894.debug

     

    warning: Missing the separate debug info file: /usr/lib/debug/.build-id/ba/4ea1118691c826426e9410cafb798f25cefad5.debug

     

    Breakpoint 1, main (argc=2, argv=0xbfc55654) at testRundl.c:11

    11              handle = dlopen(argv[1], RTLD_NOW);


    (gdb) p argv[0]

    $1 = 0xbfab8b17 "/home/zubo/linux/2010-02-05 testDlopen/testRundl"

    (gdb) p argv[1]

    $2 = 0xbfab8b48 "/lib/libm-2.7.so"

    (gdb) c

    Continuing.

     

    warning: Missing the separate debug info file: /usr/lib/debug/.build-id/92/8ab51a53627c59877a85dd9afecc1619ca866c.debug

    result = 1.000000

     

    Program exited normally.

    (gdb) q

    #

     

    通过r命令直接设置参数。

    # gdb ./testRundl

    GNU gdb Red Hat Linux (6.6-35.fc8rh)

    Copyright (C) 2006 Free Software Foundation, Inc.

    GDB is free software, covered by the GNU General Public License, and you are

    welcome to change it and/or distribute copies of it under certain conditions.

    Type "show copying" to see the conditions.

    There is absolutely no warranty for GDB.  Type "show warranty" for details.

    This GDB was configured as "i386-redhat-linux-gnu"...

    Using host libthread_db library "/lib/libthread_db.so.1".

    (gdb) b 11

    Breakpoint 1 at 0x8048508: file testRundl.c, line 11.

    (gdb) r /lib/libm-2.7.so  //设置输入参数

    Starting program: /home/zubo/linux/2010-02-05 testDlopen/testRundl /lib/libm-2.7.so

     

    warning: Missing the separate debug info file: /usr/lib/debug/.build-id/ac/2eeb206486bb7315d6ac4cd64de0cb50838ff6.debug

     

    warning: Missing the separate debug info file: /usr/lib/debug/.build-id/db/a292aff9720bfc3f25c53fa8e469168460a894.debug

     

    warning: Missing the separate debug info file: /usr/lib/debug/.build-id/ba/4ea1118691c826426e9410cafb798f25cefad5.debug

     

    Breakpoint 1, main (argc=2, argv=0xbfb8ad84) at testRundl.c:11

    11              handle = dlopen(argv[1], RTLD_NOW);

    (gdb) p argv[0]

    $1 = 0xbfb8bb17 "/home/zubo/linux/2010-02-05 testDlopen/testRundl"

    (gdb) p argv[1]

    $2 = 0xbfb8bb48 "/lib/libm-2.7.so"

    (gdb) c

    Continuing.

     

    warning: Missing the separate debug info file: /usr/lib/debug/.build-id/92/8ab51a53627c59877a85dd9afecc1619ca866c.debug

    result = 1.000000

     

    Program exited normally.

    (gdb) q

     

    从程序运行的过程可以看到,这两种方法设置输入参数均可行。

     

     

    Reference

    http://blog.csdn.net/samehai/archive/2007/09/27/1803521.aspx

    http://blog.csdn.net/dashuliu/archive/2009/07/20/4363450.aspx

    gdbhelp命令

    dlopenman手册页(# man dlopen


原文链接:http://blog.csdn.net/livelylittlefish/article/details/5621645
加载中
返回顶部
顶部