Linuxでのstringsコマンドの概要

文字列コマンドを知らないLinuxでソフトウェア開発に従事している友人はほとんどいません。最初に男の文字列を見てみましょう:

       文字列-印刷可能な文字の文字列をファイルに出力します。  

       これは、印刷ファイル内の印刷可能な文字を意味します。追加させてください。このファイルには、テキストファイル(test.c)、実行可能ファイル(test)、ダイナミックリンクライブラリ(test.o)、静的リンクライブラリ(test.a)があります。
  
       
        実際に検証せずにコードから長いストーリーを書くのは私のスタイルではありません。料理を注文するためのコードをいくつか実行してみましょう(コードはtest.cに保存されています)。
  1. #include <stdio.h>  
  2.   
  3. int  add(int  x、  int  y)  
  4. {{  
  5.          x + yを返します。  
  6. }  
  7.   
  8. int  main()  
  9. {{  
  10.         int  a = 1;  
  11.         int  b = 2;  
  12.         int  c = add(a、b);  
  13.         printf("ああ、私の愛する人、cは%d \ n"、c);  
  14.   
  15.          0を返します。  
  16. }  
#include <stdio.h>

int add(int x, int y)
{
        return x + y;
}

int main()
{
        int a = 1;
        int b = 2;
        int c = add(a, b);
        printf("oh, my dear, c is %d\n", c);

        return 0;
}
       文字列test.cの結果を見てみましょう。
  1. [taoge @ localhost learn_c] $文字列test.c   
  2. #include <stdio.h>  
  3. int add(int x、int y)  
  4.     x + yを返します。  
  5. int main()  
  6.     int a = 1;  
  7.     int b = 2;  
  8.     int c = add(a、b);  
  9.     printf( "ああ、私の愛する人、cは%d \ n"、c);  
  10.     0を返します。  
  11. [taoge @ localhost learn_c] $   
[taoge@localhost learn_c]$ strings test.c 
#include <stdio.h>
int add(int x, int y)
	return x + y;
int main()
	int a = 1;
	int b = 2;
	int c = add(a, b);
	printf("oh, my dear, c is %d\n", c);
	return 0;
[taoge@localhost learn_c]$ 
      ご覧のとおり、test.cの多くの文字が実際に印刷されています。


      次に、次のように実行可能ファイルに文字列を使用してみましょう。
  1. [taoge @ localhost learn_c] $ gcc test.c   
  2. [taoge @ localhost learn_c] $文字列a.out   
  3. /lib/ld-linux.so.2  
  4. =$TsU  
  5. __gmon_start__  
  6. libc.so.6  
  7. _IO_stdin_used  
  8. printf  
  9. __libc_start_main  
  10. GLIBC_2.0  
  11. PTRh   
  12. [^ _]  
  13. ああ、私の愛する人、cは%dです  
  14. [taoge @ localhost learn_c] $   
[taoge@localhost learn_c]$ gcc test.c 
[taoge@localhost learn_c]$ strings a.out 
/lib/ld-linux.so.2
=$TsU
__gmon_start__
libc.so.6
_IO_stdin_used
printf
__libc_start_main
GLIBC_2.0
PTRh 
[^_]
oh, my dear, c is %d
[taoge@localhost learn_c]$ 
       ご覧のとおり、a.outの多くの文字が印刷されています。


       実際、オブジェクトファイル、静的ライブラリ、または動的ライブラリがある場合は、stringsコマンドを使用して印刷することもできます。見てみましょう:
       xxx.hファイル:
  1. void  print();  
void print();
       xxx.cファイル:
  1. #include <stdio.h>  
  2. #include "xxx.h"  
  3.   
  4. void  print()  
  5. {{  
  6.     printf("雨の日\ n");  
  7. }  
#include <stdio.h>
#include "xxx.h"

void print()
{
	printf("rainy days\n");
}
       次に、静的ライブラリと動的ライブラリを作成する方法を見てみましょう(以降のブログ投稿で詳細を紹介します)。
  1. [taoge @ localhost learn_strings] $ ls  
  2. xxx.c xxx.h  
  3. [taoge @ localhost learn_strings] $ gcc -c xxx.c  
  4. [taoge @ localhost learn_strings] $ ar rcs libxxx.a xxx.o  
  5. [taoge @ localhost learn_strings] $ gcc -shared -fPIC -o libxxx.so xxx.o  
  6. [taoge @ localhost learn_strings] $ ls  
  7. libxxx.a libxxx.so xxx.c xxx.h xxx.o  
  8. [taoge @ localhost learn_strings] $文字列xxx.o  
  9. 雨の日  
  10. [taoge @ localhost learn_strings] $文字列libxxx.a  
  11. !<アーチ>  
  12. / 1437887339 0 0 0 14 `  
  13. Rprint  
  14. xxx.o / 1437887333 501502 100664 848 `  
  15. 雨の日  
  16. GCC:(GNU)4.4.4 20100726(Red Hat 4.4.4-13)  
  17. .symtab  
  18. .strtab  
  19. .shstrtab  
  20. .rel.text  
  21. 。データ  
  22. .bss  
  23. .rodata  
  24. 。コメント  
  25. .note.GNU-スタック  
  26. xxx.c  
  27. 印刷  
  28. プット  
  29. [taoge @ localhost learn_strings] $   
  30. [taoge @ localhost learn_strings] $   
  31. [taoge @ localhost learn_strings] $文字列libxxx.so  
  32. __gmon_start__  
  33. _初期化  
  34. _fini  
  35. __cxa_finalize  
  36. _Jv_RegisterClasses  
  37. 印刷  
  38. プット  
  39. libc.so.6  
  40. _edata  
  41. __bss_start  
  42. _終わり  
  43. GLIBC_2.1.3  
  44. GLIBC_2.0  
  45. 雨の日  
  46. [taoge @ localhost learn_strings] $   
[taoge@localhost learn_strings]$ ls
xxx.c  xxx.h
[taoge@localhost learn_strings]$ gcc -c xxx.c
[taoge@localhost learn_strings]$ ar rcs libxxx.a xxx.o
[taoge@localhost learn_strings]$ gcc -shared -fPIC -o libxxx.so xxx.o
[taoge@localhost learn_strings]$ ls
libxxx.a  libxxx.so  xxx.c  xxx.h  xxx.o
[taoge@localhost learn_strings]$ strings xxx.o
rainy days
[taoge@localhost learn_strings]$ strings libxxx.a
!<arch>
/               1437887339  0     0     0       14        `
Rprint
xxx.o/          1437887333  501   502   100664  848       `
rainy days
GCC: (GNU) 4.4.4 20100726 (Red Hat 4.4.4-13)
.symtab
.strtab
.shstrtab
.rel.text
.data
.bss
.rodata
.comment
.note.GNU-stack
xxx.c
print
puts
[taoge@localhost learn_strings]$ 
[taoge@localhost learn_strings]$ 
[taoge@localhost learn_strings]$ strings libxxx.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
rainy days
[taoge@localhost learn_strings]$ 
       見てください。


       文字列コマンドは非常に単純で、何もないように見えますが、実際には多くの用途があります。以下に例を挙げましょう。大規模なソフトウェア開発では、100個の.c / .cppファイルがあると仮定すると、この.cppファイルは最終的に10個の.soライブラリを生成します。次に、特定の.c /.cppファイルがどの.c / .cppファイルにコンパイルされているかをすばやく知ることができます。だから図書館はなくなった?もちろん、メイクファイルを見ているかどうかわからない、と言わざるを得ないかもしれません。はい、間違いなくmakefileを見ることができますが、次の方法の方が優れています。コマンドを使用するだけです。
      文字列-f "* .so" | grep "xxxxxx"

      それでもわからない場合は、上記のアプレットを例として取り上げます。ただし、ここでは、次のようにすべてのファイルを検討します。
  1. [taoge @ localhost learn_c] $文字列-f * | grep「mydear」  
  2. a.out:ああ、私の愛する人、cは%dです  
  3. test.c:printf( "ああ、私の愛する人、cは%d \ n"、c);  
  4. [taoge @ localhost learn_c] $   
[taoge@localhost learn_c]$ strings -f * | grep "my dear"
a.out: oh, my dear, c is %d
test.c: 	printf("oh, my dear, c is %d\n", c);
[taoge@localhost learn_c]$ 
       ご覧のとおり、ソースファイルtest.cと実行可能ファイルの両方に「mydear」文字列があり、対応するファイルが一度に見つかりました。.c / .cppファイルが.soライブラリにコンパイルされている場合、文字列-f * | grep "my dear"は、対応する.soファイルを見つけることができる必要があります。 "mydear"は.c /にあります。 cppファイルログ文字列(たとえば、printfは印刷に使用されます)。


       最初に文字列の役割を紹介しますので、文字列に慣れましょう。

おすすめ

転載: blog.csdn.net/weixin_40017062/article/details/80574667