Introducción al comando strings en linux

Casi no hay amigos que se dediquen al desarrollo de software en Linux que no conozcan el comando strings. Primero echemos un vistazo a las cadenas de hombre:

       cadenas: imprime las cadenas de caracteres imprimibles en archivos.  

       Es decir, caracteres imprimibles en el archivo de impresión. Permítanme agregar, este archivo puede ser un archivo de texto (prueba.c), archivo ejecutable (prueba), biblioteca de vínculos dinámicos (prueba.o), biblioteca de vínculos estáticos (prueba.a)
  
       
        No es mi estilo escribir una larga historia a partir del código sin verificarlo. Hagamos un código para ordenar los platos (el código se almacena en test.c):
  1. #include <stdio.h>  
  2.   
  3. int  agregar ( int  x,  int  y)  
  4. {  
  5.         return  x + y;  
  6. }  
  7.   
  8. int  main ()  
  9. {  
  10.         int  a = 1;  
  11.         int  b = 2;  
  12.         int  c = agregar (a, b);  
  13.         printf ( "oh, querida, c es% d \ n" , c);  
  14.   
  15.         return  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;
}
       Echemos un vistazo a los resultados de strings test.c:
  1. [taoge @ localhost learn_c] $ strings test.c   
  2. #include <stdio.h>  
  3. int agregar (int x, int y)  
  4.     return x + y;  
  5. int main ()  
  6.     int a = 1;  
  7.     int b = 2;  
  8.     int c = agregar (a, b);  
  9.     printf ("oh, querida, c es% d \ n", c);  
  10.     return 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]$ 
      Como puede ver, muchos caracteres en test.c están realmente impresos.


      A continuación, intentemos usar cadenas para archivos ejecutables, de la siguiente manera:
  1. [taoge @ localhost learn_c] $ gcc test.c   
  2. [taoge @ localhost learn_c] $ strings 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. oh, querida, c es% 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]$ 
       Como puede ver, se imprimen muchos caracteres en a.out.


       De hecho, si hay archivos de objetos, bibliotecas estáticas o bibliotecas dinámicas, también puede usar el comando strings para imprimir. Vamos a ver:
       Archivo xxx.h:
  1.  impresión vacía ();  
void print();
       Archivo xxx.c:
  1. #include <stdio.h>  
  2. #include "xxx.h"  
  3.   
  4.  impresión vacía ()  
  5. {  
  6.     printf ( "días lluviosos \ n" );  
  7. }  
#include <stdio.h>
#include "xxx.h"

void print()
{
	printf("rainy days\n");
}
       Luego, echemos un vistazo a cómo crear bibliotecas estáticas y dinámicas (continuaremos presentándolas en detalle en publicaciones de blog posteriores):
  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] $ cadenas xxx.o  
  9. días lluviosos  
  10. [taoge @ localhost learn_strings] $ strings libxxx.a  
  11. ! <arch>  
  12. / 1437887339 0 0 0 14 '  
  13. Rprint  
  14. xxx.o / 1437887333 501 502 100664 848 '  
  15. días lluviosos  
  16. CCG: (GNU) 4.4.4 20100726 (Red Hat 4.4.4-13)  
  17. .symtab  
  18. .strtab  
  19. .shstrtab  
  20. .rel.text  
  21. .datos  
  22. .bss  
  23. .rodata  
  24. .comentario  
  25. .note.GNU-stack  
  26. xxx.c  
  27. imprimir  
  28. pone  
  29. [taoge @ localhost learn_strings] $   
  30. [taoge @ localhost learn_strings] $   
  31. [taoge @ localhost learn_strings] $ strings libxxx.so  
  32. __gmon_start__  
  33. _en eso  
  34. _fini  
  35. __cxa_finalize  
  36. _Jv_RegisterClasses  
  37. imprimir  
  38. pone  
  39. libc.so.6  
  40. _edata  
  41. __bss_start  
  42. _final  
  43. GLIBC_2.1.3  
  44. GLIBC_2.0  
  45. días lluviosos  
  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]$ 
       Míralo.


       El comando strings es muy simple y no parece nada, pero en realidad tiene muchos usos. A continuación, déjeme dar un ejemplo. En el desarrollo de software a gran escala, asumiendo que hay 100 archivos .c / .cpp, este archivo .cpp eventualmente generará 10 bibliotecas .so, entonces, ¿cómo podemos saber rápidamente que un determinado archivo .c / .cpp está compilado en el que. así que la biblioteca se ha ido? Por supuesto, puede que tenga que decir que no sabe si mira el archivo MAKE. Sí, definitivamente puedes mirar el archivo MAKE, pero el siguiente método es mejor, solo usa el comando:
      cadenas -f "* .so" | grep "xxxxxx"

      Si aún no lo entiende, simplemente tome el subprograma anterior como ejemplo. Sin embargo, aquí consideramos todos los archivos, de la siguiente manera:
  1. [taoge @ localhost learn_c] $ cadenas -f * | grep "querida"  
  2. a.out: oh, querida, c es% d  
  3. test.c: printf ("oh, querida, c es% 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]$ 
       Como puede ver, hay cadenas "querida" tanto en el archivo fuente test.cy en el archivo ejecutable, y el archivo correspondiente se encontró de una vez, está claro. Si se compila un archivo .c / .cpp en la biblioteca .so, las cadenas -f * | grep "my dear" deben poder encontrar el archivo .so correspondiente, donde "my dear" está en el .c /. archivo cpp Una cadena de registro (por ejemplo, printf se usa para imprimir).


       La función de las cadenas se presentará primero, así que familiaricémonos con las cadenas.

Supongo que te gusta

Origin blog.csdn.net/weixin_40017062/article/details/80574667
Recomendado
Clasificación