Showing posts with label cprogramming. Show all posts
Showing posts with label cprogramming. Show all posts

Thursday, February 20, 2020

gdb tips - PART1

gdb

Some gdb ( GNU debugger) tips

It is very important to understand the debugging tools and their different functionalities to hack the system level programs easily.

How to compile a program to debug in gdb.

  1. compile your C program with -g option

    $cc -g helloworld.c

  2. Open the executable in gdb
    $gdb a.out

  3. Run the program using below commands in gdb prompt using simply ‘r’.
    (gdb)run or (gdb)r

  4. Continue run from breakpoint using ‘c’
    (gdb)c

Setting Breakpoint in gdb.

1.Setting up breakpoint in a line number
$b 10
2. breakpoint setting up in an address
$b *0x7c00

(gdb) b *0x7c00
Breakpoint 1 at 0x7c00
(gdb) c
Continuing.
Thread 1 hit Breakpoint 1, 0x00007c00 in ?? ()

3.Setting up breakpoint in a function
$b main

Switch to tui mode using ctrl+x 2

you can switch to tui ( text gui mode) mode using ctrl+x 2
to view the source file in the same screen.
tui mode

you can ctrl+x 1 to see registers values

ctrl+x n to come back to gdb prompt.

stepping through line and instruction

$si \ steps through instruction
$s \ source level step through next line

Next step over through line

$n
$next

gdb_steps_through_source

Printing variables

$p foo//print the value of foo
$p car // print the value of car variable

$p/x addr //printing hexa decimal values

information about frames and registers

Current function stack frame details can be displayed using the command ‘f’ or ‘frame’
$f
$frame

enter image description here

The registers details or values can be seen using the command ’ info r’

info r
enter image description here

backtrace of call stacks

How can we backtrace all call stacks in gdb?!. This would be very useful command especially when you are debugging big projects source codes.

$bt
shows the backtrace of all call stacks so far.

enter image description here

You can switch to different frames using below command

$f 1
to switch to the first frame
f 5 switch to 5th frame etc

Some of the helpful commands when you are in a function

$info locals
shows the local variable in a function

$info args
shows arguments of the function

getting help on gdb

(gdb) help

List of classes of commands:

aliases – Aliases of other commands
breakpoints – Making program stop at certain points
data – Examining data
files – Specifying and examining files
internals – Maintenance commands
obscure – Obscure features
running – Running the program

(gdb)h breakpoints //provide help on breakpoints

PART 2 -
https://naveendavisv.blogspot.com/2020/04/gdb-debugging-part-2.html

Tuesday, February 18, 2020

Have you ever tried Ctrl+x 2 in GDB ?

I got amazed when I tried Ctrl+x  2 in gdb and step through code. I never thought GDB had an option to do this.





again press ctrl+x 2  .. you will see another magic.

Friday, July 21, 2017

Why isn't int = 16 bits in C language always?

I know C is machine dependent or compiler dependent . The sizeof(int) might returns different in different machines or hardware ? Is it really hard to make machine independent C ? we need to really have jvm concept ? why there is no CVM( C virtual machine) :-) ???

Saturday, July 23, 2016

hackerrank

Are you want to program some algorithms or some basic CS concepts ? I would say you can go and try it in https://www.hackerrank.com

Wednesday, June 15, 2016

Search a Tree - C program

Computer is a robot , our program  should identify the repetitive task and make computer do that repetitive task with less lines of code.... programmer should be able to identify the repetitive task and apply recursive functions..

Search a value in a Tree - C program

the beauty of this code is that searching the tree (function:searchtree ) is going to be the one function.But we will make the computer to do the same task repetitively just passing the next node.


#include
#include


struct node {
    int a;
    struct node* left;
    struct node* right;
};

struct node* insert_node(int a){

    struct node* newnode;
    newnode = (struct node*) malloc (sizeof(struct node));
    newnode->a = a;
    newnode->left = NULL;
    newnode->right = NULL;

    return newnode;
}

int searchtree(struct node* root,int num){
    if(root == NULL)
        return 0;
    if(root->a == num ||searchtree(root->left,num))
        return 1;
    if(root->a == num || searchtree(root->right,num)){       
        return 1;
    }   
   
}

       
   
           

int main(){

    int num;
    struct node* root;
   
    root = insert_node(1);
    root->left = insert_node(2);
    root->right = insert_node(3);
    root->right->right = insert_node(4);   
    root->right->left = insert_node(5);   
    root->left->left = insert_node(6);
    root->left->right = insert_node(7);
    root->left->right->left = insert_node(8);

    printf("type a number:");
    scanf("%d",&num);

    if(searchtree(root,num)){
        printf("number found in tree");
    }
    else{
        printf("number not found in tree");
    };
    return(0);
}

Monday, June 13, 2016

Strace on a tree structure C program

When I tried to access 'unallocated node value' , as usual 'Segmentation fault' came as output :-)

But i am not fully clear on how mmap() system call allocate the memory...

I also came to know that mprotect() system call sets the protection,

If the calling process tries to access memory in a manner that violates the protection, then the kernel generates a SIGSEGV signal for the process.

Program and Strace details: