首页 > 系统相关 >linux代码相关的工具

linux代码相关的工具

时间:2022-12-09 21:37:38浏览次数:44  
标签:__ tmp malloc bytes 代码 3908 linux test 工具


函数调用关系

codeviz

calltree

gprof, kprof

http://blog.chinaunix.net/uid-20749137-id-718762.html

graphviz

gdb & backtrace

strace

diagram designer可以画代码的关系图

 

应用程序相关工具

1. nm

nm command can report the list of symbol in a given library

 

ning@pcning:~/idning-paper/src$ nm test/protocol_test.out
08049f18 d _DYNAMIC
08049ff4 d _GLOBAL_OFFSET_TABLE_
08048dbc R _IO_stdin_used
w _Jv_RegisterClasses
08049f08 d __CTOR_END__
08049f04 d __CTOR_LIST__
08049f10 D __DTOR_END__
08049f0c d __DTOR_LIST__
08048ef4 r __FRAME_END__
08049f14 d __JCR_END__
08049f14 d __JCR_LIST__
08048ede r __PRETTY_FUNCTION__.4058
U __assert_fail@@GLIBC_2.0
0804a024 A __bss_start
0804a01c D __data_start
08048d70 t __do_global_ctors_aux
08048550 t __do_global_dtors_aux
0804a020 D __dso_handle
w __gmon_start__

 

2. ldd (list dynamic dependencies)

查看库依赖关系

 

ning@pcning:~/idning-paper/src$ ldd ./test/protocol_test.gen.out
linux-gate.so.1 => (0x00fb4000)
libevent-2.0.so.5 => not found
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x00af5000)
/lib/ld-linux.so.2 (0x00589000)

 

 

 

 

性能分析

1. gcov

gcov is a test coverage program how often each line of code executes what lines of code are actually executed and how much computing time each section of code use

使用gcov,编译时加上:-fprofile-arcs -ftest-coverage,链接时加上:-lgcov

-fprofile-arcs generate .gcda file, it contains arc transition counts, and some summary information.

-ftest-coverage generate .gcno file, it contain information to restruct the basic block graph and assign source line numbers to block.

 

$ gcc -fprofile-arcs -ftest-coverage tmp.c
$ ./a.out
$ gcov tmp.c
90.00% of 10 source lines executed in file tmp.c
Creating tmp.c.gcov.
The file tmp.c.gcov contains output from gcov. Here is a sample:
-: 0:Source:tmp.c
-: 0:Graph:tmp.gcno
-: 0:Data:tmp.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:#include <stdio.h>
-: 2:
-: 3:int main (void)
1: 4:{
1: 5: int i, total;
-: 6:
1: 7: total = 0;
-: 8:
11: 9: for (i = 0; i < 10; i++)
10: 10: total += i;
-: 11:
1: 12: if (total != 45)
#####: 13: printf ("Failure\n");
-: 14: else
1: 15: printf ("Success\n");
1: 16: return 0;
-: 17:}

 

2. gprof

基本用法:

1)使用-pg选项编译和链接

2)执行你的应用程序,使之运行完成后生成供gprof分析的数据文件,默认是gmon.out

3)使用gprof程序分析你的应用程序生成的数据,gprof a.out gmon.out

 

3. oprofile

 

 

内存泄露

1. valgrind

举例,下面是一段有问题的c程序代码test.c

 

#include <stdlib.h>
void f(void)
{
int* x = malloc(10 * sizeof(int));
x[10] = 0; //问题1: 数组下标越界
} //问题2: 内存没有释放

int main(void)
{
f();
return 0;
}

1)编译程序test.c: gcc -Wall test.c -g -o test

 

2)使用valgrind检查程序bug:valgrind --tool=memcheck --leak-check=full ./test

3)分析调试信息

 

==3908== Memcheck, a memory error detector.
==3908== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==3908== Using LibVEX rev 1732, a library for dynamic binary translation.
==3908== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==3908== Using valgrind-3.2.3, a dynamic binary instrumentation framework.
==3908== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==3908== For more details, rerun with: -v
==3908==
--3908-- DWARF2 CFI reader: unhandled CFI instruction 0:50
--3908-- DWARF2 CFI reader: unhandled CFI instruction 0:50
/*数组越界错误*/
==3908== Invalid write of size 4
==3908== at 0x8048384: f (test.c:6)
==3908== by 0x80483AC: main (test.c:11)
==3908== Address 0x400C050 is 0 bytes after a block of size 40 alloc'd
==3908== at 0x40046F2: malloc (vg_replace_malloc.c:149)
==3908== by 0x8048377: f (test.c:5)
==3908== by 0x80483AC: main (test.c:11)
==3908==
==3908== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 14 from 1)
==3908== malloc/free: in use at exit: 40 bytes in 1 blocks.
==3908== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
==3908== For counts of detected errors, rerun with: -v
==3908== searching for pointers to 1 not-freed blocks.
==3908== checked 59,124 bytes.
==3908==
==3908==
/*有内存空间没有释放*/
==3908== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==3908== at 0x40046F2: malloc (vg_replace_malloc.c:149)
==3908== by 0x8048377: f (test.c:5)
==3908== by 0x80483AC: main (test.c:11)
==3908==
==3908== LEAK SUMMARY:
==3908== definitely lost: 40 bytes in 1 blocks.
==3908== possibly lost: 0 bytes in 0 blocks.
==3908== still reachable: 0 bytes in 0 blocks.
==3908== suppressed: 0 bytes in 0 blocks.

 

 

以上有部分参考:http://www.udpwork.com/item/13025.html

标签:__,tmp,malloc,bytes,代码,3908,linux,test,工具
From: https://blog.51cto.com/u_15854579/5926575

相关文章