Open In App

User Space Debugging Tools in Linux

Last Updated : 05 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss debugging tools in Linux. Debugging Tools are those programs that allow us to monitor, control, and correct other program’s error while they execute.

Some of the debugging tools are as follows:-

  • ‘print Statement’
  • Querying (/proc, /sys etc)
  • Tracing (strace/ltrace)
  • Valgrind (memwatch)
  • GDB

1. ‘print’ Statement

The ‘print‘ statement is a fundamental debugging statement in Linux. You can insert a print statement in your program to understand the program’s flow of control. It is an effortless technique to debug the program.

It has some disadvantages to it. Programs need to be edited and add the print statement in the program then re-run the program which is time-consuming and it does not work in extensive programs.

You can add the print statement in your program like this when you get an error. It will be showing you an Error message like this

print Error

 

2. Querying (/proc/sys etc)

Sometimes, we want to know which process occupies how much memory in the system and what is the process state in the kernel, etc. If you want order to obtain this type of information you need to use the proc file system which gives you runtime system information. If you want to know more about the proc file system you can read this  article Proc File Systemproc

ls /proc

proc file system example

3. Tracing

strace command is used for tracing the programs and processes.

strace is a diagnostic command in Linux. This command intercepts any syscall made by the command. strace can be attached to a process that is already running or to a new process. It can also be used as a tool to understand how the system calls work by tracing different commands.

There are many options with strace commands you can check all options by using the man command.

strace pwd

Output

strace command example

4. Valgrind

Valgrind is a suit for debugging tools. Valgrind has one tool which is widely used named ‘Memcheck’ It’s a memory-checking tool that intercepts calls made to malloc(), free(), and delete(). It works with directly executable files. Valgrind also has some disadvantages. It might provide false positive and false negative results. It cannot detect out-of-range allocated arrays.

Let’s see how Valgrind works.

Valgrind syntax:

valgrind –tool=memcheck -least-check=yes filename

We will write C code and we will try to find the error using Valgrind Let’s see

C




#include <stdio.h>
 
int main()
{
  char *p;
 
  // Allocation #1 of 19 bytes
  p = (char *) malloc(19);
 
  // Allocation #2 of 12 bytes
  p = (char *) malloc(12);
  free(p);
 
  // Allocation #3 of 16 bytes
  p = (char *) malloc(16);
 
  return 0;
}


Output

Here, you can see the 2 errors shown in this code.

Valgrind Example 

5. GDB

GDB is debugging tool from Free Software Foundation.GDB is a debugging tool that helps to find the errors in the code. It is useful for locating and finding problems in the code. It performs various actions.

  • starting the problem
  • stopping at a specific location
  • stopping on a specific condition etc.

If you want to know more about GDB you can check this article GDB Introduction

Example of GDB

We will write code and try to find the error using GDB

C




#include<stdio.h>
 
int main(){
  int x ;
  int a= x ;
  int b = a ;
  int c = a+b ;
  printf("%d\n",c);
  return 0 ;
}


Output

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads