Detail the introduction to the strings command in linux

  • 2020-05-14 05:51:23
  • OfStack

Few of you who work in software development under Linux are unaware of the strings command. Let's start with man strings:


strings - print the strings of printable characters in files. 

Print the printable characters in a file. Let me add 1. This file can be a text file (test.c), an executable (test), a dynamic link library (test.o), or a static link library (test.a).

It's not my style to go into a long speech without actually validating it. Let's do some code (the code is in 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; 
} 

Let's look at the results of strings test.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]$  

As you can see, many of the characters in test.c are indeed printed.

Next, let's try strings for the executable, as follows:


[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]$  

As you can see, many of the characters in a.out are printed out.

In fact, if you have a target file, a static library, or a dynamic library, you can print using the strings command. Let's take a look:

xxx. h file:


void print(); 

xxx. c file:


#include <stdio.h> 
#include "xxx.h" 
 
void print() 
{ 
  printf("rainy days\n"); 
} 

Then, let's take a look at how to make a static, dynamic library (more on this in a future post) :


[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]$  

See.

The strings command is simple. It may seem like nothing, but it has many USES. Now, let me give you an example. In large software development, if you have 100.c /.cpp files, and this.cpp file eventually generates 10.so libraries, how do you quickly know if a.c /.cpp file has been compiled into that.so library? Of course, you might say, look at makefile. Yes, makefile certainly works, but it's better to use the following command directly:


strings -f "*.so" | grep "xxxxxx"

If you don't understand, take the above small program as an example. However, here we consider all the files as follows:


[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]$  

As you can see, the "my dear" string is found in both the source file test.c and the executable. If a.c /.cpp file is compiled into the.so library, then strings-f * | grep "my dear" must be able to find the corresponding.so file, where "my dear" is a log string in the.c /.cpp file (for example, printed in printf).

So much for the role of strings. Let's get familiar with strings.


Related articles: