详解linux中的strings命令简介
在Linux下搞软件开发的朋友,几乎没有不知道strings命令的。我们先用manstrings来看看:
strings-printthestringsofprintablecharactersinfiles.
意思是,打印文件中可打印的字符。 我来补充一下吧,这个文件可以是文本文件(test.c),可执行文件(test), 动态链接库(test.o),静态链接库(test.a)
脱离代码地长篇大论而不去实际验证,不是我的风格。还是搞点代码下菜吧(代码存在test.c中):
#include<stdio.h> intadd(intx,inty) { returnx+y; } intmain() { inta=1; intb=2; intc=add(a,b); printf("oh,mydear,cis%d\n",c); return0; }
我们来看看stringstest.c的结果:
[taoge@localhostlearn_c]$stringstest.c #include<stdio.h> intadd(intx,inty) returnx+y; intmain() inta=1; intb=2; intc=add(a,b); printf("oh,mydear,cis%d\n",c); return0; [taoge@localhostlearn_c]$
可以看到,确实打印出了test.c中的很多字符。
下面,我们对可执行文件用strings试试,如下:
[taoge@localhostlearn_c]$gcctest.c [taoge@localhostlearn_c]$stringsa.out /lib/ld-linux.so.2 =$TsU __gmon_start__ libc.so.6 _IO_stdin_used printf __libc_start_main GLIBC_2.0 PTRh [^_] oh,mydear,cis%d [taoge@localhostlearn_c]$
可以看到,打印出了a.out中很多字符。
实际上,如果有目标文件、静态库或动态库,,也是可以用strings命令进行打印操作的。我们来看看:
xxx.h文件:
voidprint();
xxx.c文件:
#include<stdio.h> #include"xxx.h" voidprint() { printf("rainydays\n"); }
然后,我们来看看怎么制作静态、动态库吧(在后续博文中会继续详细介绍):
[taoge@localhostlearn_strings]$ls xxx.cxxx.h [taoge@localhostlearn_strings]$gcc-cxxx.c [taoge@localhostlearn_strings]$arrcslibxxx.axxx.o [taoge@localhostlearn_strings]$gcc-shared-fPIC-olibxxx.soxxx.o [taoge@localhostlearn_strings]$ls libxxx.alibxxx.soxxx.cxxx.hxxx.o [taoge@localhostlearn_strings]$stringsxxx.o rainydays [taoge@localhostlearn_strings]$stringslibxxx.a !<arch> /143788733900014` Rprint xxx.o/1437887333501502100664848` rainydays GCC:(GNU)4.4.420100726(RedHat4.4.4-13) .symtab .strtab .shstrtab .rel.text .data .bss .rodata .comment .note.GNU-stack xxx.c print puts [taoge@localhostlearn_strings]$ [taoge@localhostlearn_strings]$ [taoge@localhostlearn_strings]$stringslibxxx.so __gmon_start__ _init _fini __cxa_finalize _Jv_RegisterClasses print puts libc.so.6 _edata __bss_start _end GLIBC_2.1.3 GLIBC_2.0 rainydays [taoge@localhostlearn_strings]$
看到了吧。
strings命令很简单,看起来好像没什么,但实际有很多用途。下面,我来举一个例子。 在大型的软件开发中,假设有100个.c/.cpp文件,这个.cpp文件最终生成10个.so库,那么怎样才能快速知道某个.c/.cpp文件编译到那个.so库中去了呢?当然,你可能要说,看makefile不就知道了。对,看makefile肯定可以,但如下方法更好,直接用命令:
strings-f"*.so"|grep"xxxxxx"
如果还不明白,那就就以上面的小程序为例为说明,不过,此处我们考虑所有的文件,如下:
[taoge@localhostlearn_c]$strings-f*|grep"mydear" a.out:oh,mydear,cis%d test.c:printf("oh,mydear,cis%d\n",c); [taoge@localhostlearn_c]$
可以看到,源文件test.c和可执行文件中皆有"mydear"串,一下子就找到了对应的文件,清楚了吧。如果某.c/.cpp文件编译进了.so库,那么,strings-f*|grep"mydear"必定可以找到对应的.so文件,其中"mydear"为该.c/.cpp文件中的某个日志串(比如以printf为打印)。
strings的作用先介绍到此,算是抛砖引玉地熟悉一下strings吧。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。