GDB程序运行时的参数设置
2010-05-25 08:27
489 查看
本博客(http://blog.csdn.net/livelylittlefish)贴出作者(三二一@小鱼)相关研究、学习内容所做的笔记,欢迎广大朋友指正!
GDB程序运行时的参数设置
0. 引子
我们在编程中,有时需要在程序启动/运行时指定参数,例如最常见的main函数。如果我们想用gdb调试程序时,该如何输入参数?这就是本文要讨论的内容。
1. 实验
该程序动态加载libc的数学库文件(即math库),然后在其中搜索sin函数,并求出p/2的正玄值。该数学库模块文件通过参数输入,该模块位于位于/lib/libdl-2.7.so。
文件名testRundl.c,代码如下。
编译并运行。
-ldl表示使用DL(Dynamic Loading)库,位于/lib/libdl-2.7.so。
我们也可以看看错误的情况。
2. gdb设置参数
如果我们需要调试代码,则需要在运行时加上参数,对其设置参数的方法有两种,分别列出如下。
通过set args命令设置输入参数。
通过r命令直接设置参数。
从程序运行的过程可以看到,这两种方法设置输入参数均可行。
Reference
http://blog.csdn.net/samehai/archive/2007/09/27/1803521.aspx
http://blog.csdn.net/dashuliu/archive/2009/07/20/4363450.aspx
gdb的help命令
dlopen的man手册页(# man dlopen)
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 |
我们也可以看看错误的情况。
# ./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 |
如果我们需要调试代码,则需要在运行时加上参数,对其设置参数的方法有两种,分别列出如下。
通过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 # |
# 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
gdb的help命令
dlopen的man手册页(# man dlopen)
相关文章推荐
- gdb-为程序设置运行的参数
- java运行程序时设置参数
- IntelliJ 运行程序前设置运行参数
- c# 设置winform程序为默认打开软件 在运行中获取参数
- gdb 调试运行设置main函数参数
- gdb设置运行参数
- mr程序运行在本地模式和集群模式的参数设置
- IntelliJ 运行程序前设置运行参数,类似于用命令行运行时输入运行参数
- JCreator中怎样设置运行带参数的java程序
- Maven运行java程序命令及VM参数设置
- gdb调试运行程序带参数(调用动态链接库),debug过程记录
- gdb调试运行程序带参数(调用动态链接库),debug过程记录
- VS2010如何设置使得可以是程序利用argv参数来运行
- VS2010如何设置使得可以是程序利用argv参数来运行
- 设置Jcreator 3.5,在运行程序时提示输入main函数的参数 (2007-05-22 11:39:50)
- gdb调试运行程序带参数(调用动态链接库),debug过程记录
- MyEclipse 里面运行有参数的程序 String[] args
- 用GDB调试程序(10)──查看运行时数据(1)-转
- MFC 程序不依赖.NET的设置方法 使自己写的程序可以在其他上电脑运行