Chinaunix首页 | 论坛 | 博客
  • 博客访问: 16143
  • 博文数量: 2
  • 博客积分: 45
  • 博客等级: 民兵
  • 技术积分: 45
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-13 21:16
文章分类

全部博文(2)

文章存档

2013年(1)

2012年(1)

我的朋友

分类: C/C++

2013-05-10 00:35:18


点击(此处)折叠或打开

  1. binutils工具集
  2. 1.addr2line
  3.     作用:用于得到程序指令地址所对应的函数,以及函数所在的源文件名和行号。
  4.         addr2line 例:
  5.         -------------------------------------------------------------
  6.         //文件名:addr2line_test.c
  7.         #include<stdio.h>
  8.         void foo()
  9.         {
  10.          printf(\"The address of foo() %#x\\n\",foo);
  11.         }
  12.         
  13.         int main(void)
  14.         {
  15.          foo();
  16.          return 0;
  17.         }
  18.         -------------------------------------------------------------
  19.         编译:gcc -g addr2line_test.c -o addr2line_test
  20.         执行:./addr2line_test
  21.         结果:The address of foo() 0x80483e4
  22.         -------------------------------------------------------------
  23.         null@ubuntu:~/c-test$ addr2line 0x80483e4 -f -e addr2line_test
  24.         foo
  25.         /home/null/c-test/addr2line_test.c:4
  26.         --------------------------------------------------------------
  27.         nm 符号显示器
  28.         --------------------------------------------------------------
  29.         null@ubuntu:~/c-test$ nm -n addr2line_test
  30.         ............
  31.         080483e4 T foo
  32.         08048401 T main
  33.         ............
  34.         --------------------------------------------------------------
  35.         对于c++可以使用addr2line的--demangle选项获得函数名
  36.         addr2line 地址 --demangle=gnu-v3 -f -e 可执行文件
  37.         --------------------------------------------------------------
  38.         
  39.         
  40. 2.ar,静态库生成器
  41.         先看下生成一个静态库和解压一个静态库的方法:
  42.         会在当前目录下生成一个libtest.a静态库文件。-c表示只编译,不链接
  43.         --------------------------------------------------------------
  44.     2.1生成一个静态库
  45.         gcc -c addr2line_test.c
  46.         //生成一个addr2line_test.o 文件
  47.         ar cr libtest.a addr2line_test.o
  48.         libtest.a //生成
  49.         --------------------------------------------------------------
  50.     2.2解压一个静态库
  51.         ar x libtest.a //将libtest.a解压 生成上面的 addr2line_test.o
  52.         --------------------------------------------------------------
  53.     2.3静态库的查看和删除
  54.         ar t xxx.a//查看
  55.         ar d libxx.a xx.o//删除静态库中的xx.o
  56.         --------------------------------------------------------------
  57.     2.4实验:
  58.         新建两个函数foo.c bar.c
  59.         ----------------------------------
  60.         #include<stdio.h>
  61.         // foo.c function
  62.         void foo()
  63.         {
  64.          printf(\"This is foo()\\n\");
  65.         }
  66.         ----------------------------------
  67.         #include<stdio.h>
  68.         // bar.c function
  69.         void bar()
  70.         {
  71.          printf(\"This is bar()\\n\");
  72.         }
  73.         ----------------------------------
  74.         然后分别编译这两个文件生成.o文件
  75.         gcc -c bar.c
  76.         gcc -c foo.c
  77.         bar.c bar.o foo.c foo.o//生成的.o文件
  78.         然后用ar命令来生成libmy.a静态库
  79.         ar crs libmy.a foo.o bar.o
  80.         //c参数表示创建一个档案文件,r参数指示将文件增加到创建的库文件中
  81.         //s参数是为了生成库索引以提高库被链接时的效率
  82.         库文件已经生成,现在就该怎么使用这个库文件了。
  83.         新建一个main.c文件
  84.         -----------------------------------
  85.         #include<stdio.h>
  86.         extern void foo();
  87.         extern void bar();
  88.         
  89.         int main()
  90.         {
  91.          foo();
  92.          bar();
  93.          return 0;
  94.         }

  95.         -----------------------------------
  96.          编译链接静态库:gcc main.c libmy.a -o main
  97.          ./main
  98.         This is foo()
  99.         This is bar()
  100.         --------------------------------------------------------------
  101. 3.nm,符号显示器
  102.         nm用于列出程序文件的符号
  103.         --------------------------------------------------------------
  104.     先看一个例子: nm -n main
  105.         函数或变量的开始地址 相应的符号存放在哪一个段中        符号的名字
  106.         080482b0                     T                             _init
  107.         08048320                     T                             _start
  108.         08048350                     t                             __do_global_dtors_aux
  109.         080483b0                     t                             frame_dummy
  110.         080483d4                     T                             main
  111.         080483ec                     T                             foo
  112.         08048400                     T                             bar
  113.         08048420                     T                             __libc_csu_init
  114.         08048490                     T                             __libc_csu_fini
  115.         08048492                     T                             __i686.get_pc_thunk.bx
  116.         080484a0                     t                             __do_global_ctors_aux
  117.         080484cc                     T                             _fini
  118.         080484e8                     R                             _fp_hw
  119.         080484ec                     R                             _IO_stdin_used
  120.         08048650                     r                             __FRAME_END__
  121.         08049f14                     d                             __CTOR_LIST__
  122.         08049f14                     d                             __init_array_end
  123.         08049f14                     d                             __init_array_start
  124.         08049f18                     d                             __CTOR_END__
  125.         08049f1c                     d                             __DTOR_LIST__
  126.         08049f20                     D                             __DTOR_END__
  127.         08049f24                     d                             __JCR_END__
  128.         08049f24                     d                             __JCR_LIST__
  129.         08049f28                     d                             _DYNAMIC
  130.         08049ff4                     d                             _GLOBAL_OFFSET_TABLE_
  131.         0804a00c                     D                             __data_start
  132.         0804a00c                     W                             data_start
  133.         0804a010                     D                             __dso_handle
  134.         0804a014                     A                             __bss_start
  135.         0804a014                     A                             _edata
  136.         0804a014                     b            &nbs
阅读(858) | 评论(0) | 转发(0) |
0

上一篇:怎么将vim修改像vc++自动换行缩进

下一篇:没有了

给主人留下些什么吧!~~