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.

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.

Shell

1

2

3

4

$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.

address.c

C

1

2

3

4

5

6

7

8

9

10

#include <stdio.h>

voidtest_proc(){

printf("%pn",&test_proc);

}

intmain(void){

test_proc();

return0;

}

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.