7 Essential Linux Commands Helpful for Programmers

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

Linux Commands for Programmers

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.

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

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.

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.

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.
  • The loader primarily searches its cached database (/etc/ld.so.conf) 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.
  • However, you can also use the two environment variables for specifying the dynamic library search path.
    1. LD_PRELOAD –  It’s a list of specific libraries to be loaded before any other libraries.
    2. 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.

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.

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.

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.

Show files opened under a directory.

Show files opened by a user.

Show files opened by a process.

Show processes listening on a particular port.

Show all TCP/UDP connections.

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.

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

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.

Show content of a file in char format.

Show content after skipping some bytes.

Show limited bytes in the output.

Show content in decimal integer format.

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.

 

Leave a Reply


*