This tutorial is for all C/C++ and Java developers who write code on Linux platforms. Here we brought essential Linux commands programmers can use to debug, find and fix defects in their programs and applications.

Most of the programmers use GDB or other profiling tools like Valgrind for debugging and fixing their code. But there are several common issues that either these tools can’t solve or the programmer prefers searching for easier methods.

Let’s talk about a few of these pain points that Linux commands can easily address.

One of the common issues is when a program fails to open a file for writing with unknown reason. However, the programmer made sure that the file is present, its path is correct, permissions are okay. Still, the program is not able to open it.

In an another case, the application is loading a shared object (SO) file, running without any error but not yielding the desired output. The programmer made some fixes in the app and added a few debug logs. But the problem remained after the execution. Also, not a single line of log surfaced during the last run.

Another common issue that programmers face while linking a program with a library is the undefined symbols error during the build process.

So there could be a no. of other scenarios where one needs to look up for ways other than GDB. That’s where the set of below Linux commands programmers would find useful in resolving many runtime issues.

Table of Content.

1. The < nm> command to find symbols.

2. The < objdump> command to disassemble a program.

3. The < ldd> command to display dynamic dependencies.

4. The < addr2line > command to map an address to a location in the program.

5. The < lsof > command to identify open files.

6. The < readelf > command to display ELF information.

7. The < od > command to display octal dump.

Essential Linux Commands Programmers Should Know.

1. The < nm> command to find symbols.

This command displays the list of symbols from object files like shared (.so files)/static (.a files) libraries and executables.

There is a list of options that you can use with the nm command, but we’ll cover the ones you need.

If you like to search whether a library contains any undefined function or not, then consider running the following command.

$ nm -u <library_name> | grep "<function_name>" #library_name -> provide the name of the library. #function_name -> specify the name of the symbol or function.

To search for global or external symbols, check out the below example.

$ nm -g <obj_file> | grep "<external_func>" #obj_file -> provide the name of the object file. #external_func -> specify the name of the external symbol or function.

TOC

2. The < objdump> command to disassemble a program.

Every C/C++ program translates into assembly code during execution. The < objdump > command can help you see how your code would look like after disassembly.

If you’ve compiled the program in debug mode, then follow the below command to print the disassembly in a readable format.

$ objdump -d -M intel -S <obj_file> #1. The -d flag is to begin the disassembly process. #2. The -M intel option is to print the output in Intel assembly format.

TOC

3. The < ldd> command to display dynamic dependencies.

When a program fails due to missing libraries, then you have to find the exact issue. It could be because of many reasons like the following.

Insufficient permission,

Wrong path, or

The library is missing.

The < ldd> command will display the shared library dependencies of your program. It’ll also print the path of the library files in its output.

$ ldd <obj_file> #<obj_file> is the name of your executable along with its path.

There are a few things that a programmer should remember while digging out for library loading issues.

It’s the job of the dynamic loader program ( ld.so ) to find and load libraries for a program to run.

) to find and load libraries for a program to run. The loader primarily searches its cached database ( /etc/ld.so.conf ) which stores a pre-compiled list of lib files.

) which stores a pre-compiled list of lib files. You can update the cached entries using the < ldconfig> command. Or you can also enter directly in the < /etc/ld.so.conf> file.

command. Or you can also enter directly in the < file. However, you can also use the two environment variables for specifying the dynamic library search path. LD_PRELOAD – It’s a list of specific libraries to be loaded before any other libraries. LD_LIBRARY_PATH – It’s a list of directories to search when loading libraries in a program.



TOC

4. The < addr2line > command to map an address to a location in the program.

Probably, you have seen a situation when you get hold of a hex value (a memory address) that could lead you to the actual problem. But you don’t know how to proceed with that information.

In such a case, you can use the < addr2line > command which can translate a memory address into a filename and the line number.

$ addr2line -e <your_program> <mem_addr> #your_program is the name of your program. #mem_addr is the address you like to interpret.

This command is very useful when you have to analyze a crash report but don’t have access to the core dump and the debugger. So it is one of the key Linux Commands programmers can use with a tiny piece of information like an address.

Let’s take an example of a C program which prints the address of a function at runtime.

#include <stdio.h> void test_proc() { printf("%pn", &test_proc); } int main(void) { test_proc(); return 0; }

When you compile and run the above code, it would leave you with the address of the “test_proc” method. The <% pn> is a floating-point format specifier that will get you the correct address in hex format.

Check out the below demo of the < addr2line > command.

$ gcc -ggdb address.c -o address $ ./address 0x204236 $ addr2line -e address 204236 address.c:3

TOC

5. The < lsof > command to identify open files.

It is one more cool Linux command that can help a programmer to check if a process has opened any file.

Check the open state of a file.

$ lsof <path/filename>

Show files opened under a directory.

$ lsof +D /home/test/

Show files opened by a user.

$ lsof -u techbeamers

Show files opened by a process.

$ lsof -p <process_id>

Show processes listening on a particular port.

$ lsof -i :8080

Show all TCP/UDP connections.

$ lsof -i tcp; lsof -i udp;

TOC

6. The < readelf > command to display ELF information.

This command is an extension to the < objdump > command. It fetches more detail about ELF files.

The most common usage of the < readelf > command is as follows.

$ readelf --symbols ./elfdemo 5 Symbol table '.dynsym' contains 10 entries: Num: Value Size Type Bind Vis Ndx Name 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND 1: 6000000000000de0 0 OBJECT GLOBAL DEFAULT ABS _DYNAMIC 2: 0000000000000000 176 FUNC GLOBAL DEFAULT UND printf@GLIBC_2.2 (2) ...

To get the list of all dynamically linked dependencies of a binary, run the following command.

$ readelf -d elfdemo | grep NEEDED 0x0000000000000001 (NEEDED) Shared library: [librt.so.1] 0x0000000000000001 (NEEDED) Shared library: [libc.so.6]

TOC

7. The < od > command to display octal dump.

When a file contains non-printable characters, then you can use the < od > command to display output in the default octal format.

It is a very useful command for debugging the scripts containing special chars.

Show content of a file in octal format.

$ od -b /usr/lib/libc.so 0000000 057 052 040 107 116 125 040 154 144 040 163 143 162 151 160 164 0000020 012 040 040 040 125 163 145 040 164 150 145 040 163 150 141 162 0000040 145 144 040 154 151 142 162 141 162 171 054 040 142 165 164 040 ...

Show content of a file in char format.

$ od -c /usr/lib/libc.so 0000000 / * G N U l d s c r i p t 0000020

U s e t h e s h a r 0000040 e d l i b r a r y , b u t 0000060 s o m e f u n c t i o n s a ...

Show content after skipping some bytes.

$ od -j7 -c /usr/lib/libc.so 0000007 l d s c r i p t

U s e 0000027 t h e s h a r e d l i b r 0000047 a r y , b u t s o m e f u ...

Show limited bytes in the output.

$ od -N7 -c /usr/lib/libg.a 0000000 ! < a r c h > 0000007

Show content in decimal integer format.

$ od -i demoapp 0000000 261051569 261182643 261313717 261444791 ...

TOC

Summary – 7 Essential Linux Commands Programmers Should Use.

We tried to bring in 7 key Linux commands that any programmer can use in his routine debugging tasks. Also, going forward, we’ll be adding more commands into this post.

Till then, try all these commands and share your experience.

All the Best,

TechBeamers.