TRUEANGLE

LEARNING PROCESS

Internals of a Binary to Decimal Conversion

(gdb) break 37
Breakpoint 1 at 0x400e00: file main.c, line 37.
(gdb) r
Starting program: /home/jeffrin/beautifulwork/lib/app 
Sign detection [1] 
Power of two [2] 
Counting No. of bits set [3] 
Set or clear bits without branching[4] 
Find maximum value[5] 
Finding least common multiple[6] 
Finding the greatest commom divisor[7] 
Finding if a number is an Armstrong number or not[8] 
Finding if a given number is prime number[9]  
Finding the number of twin prime numbers[10] 
Finding if a number is palindrome or not[11] 
Finding prime factor of a number[12] 
Finding the value of M^N[13] 
Finding factorial of a number[14] 
Finding the sum of the series 1! + 2! +...+N! [15] 
Finding the sume of the series 1 + 2 +...+N [16] 
Finding Parity of an Integer [17] 
Finding the biggest among three numbers [18] 
Finding the sum of even and odd numbers up to a number N [19] 
Finding the sum and number of integers divisible by 5 [20] 
Swapping Values [21] 

Breakpoint 1, main () at main.c:37
37    printf("Finding the decimal value for a binary [22] \n");
(gdb) break binary-to-decimal.c:5
Breakpoint 2 at 0x7ffff7bdaa4d: file binary-to-decimal.c, line 5.
(gdb) next
Finding the decimal value for a binary [22] 
39   scanf("%d",&option); 
(gdb) next
22
41    switch(option)
(gdb) next
127    btd();
(gdb) next

Breakpoint 2, btd () at binary-to-decimal.c:6
6     int num, binary_val, decimal_val = 0, base = 1, rem;
(gdb) next
7     printf("Enter a binary number : ");
(gdb) next
8     scanf("%d",&num);
(gdb) next
Enter a binary number : 101
9     binary_val = num;
(gdb) print num
$1 = 101
(gdb) next
10    while ( num > 0 )
(gdb) print binary_val
$2 = 101
(gdb) next
12        rem = num % 10;
(gdb) print num
$3 = 101
(gdb) print rem
$4 = 0
(gdb) next
13        decimal_val = decimal_val + rem * base;
(gdb) print rem
$5 = 1
(gdb) next
14        num = num / 10;
(gdb) print decimal_val
$6 = 1
(gdb) print decimal_val
$7 = 1
(gdb) print base
$8 = 1
(gdb) next
15        base = base * 2;
(gdb) print base
$9 = 1
(gdb) next
10    while ( num > 0 )
(gdb) print base
$10 = 2
(gdb) next
12        rem = num % 10;
(gdb) next
13        decimal_val = decimal_val + rem * base;
(gdb) print rem
$11 = 0
(gdb) next
14        num = num / 10;
(gdb) next
15        base = base * 2;
(gdb) next
10    while ( num > 0 )
(gdb) next
12        rem = num % 10;
(gdb) next
13        decimal_val = decimal_val + rem * base;
(gdb) next
14        num = num / 10;
(gdb) next
15        base = base * 2;
(gdb) next
10    while ( num > 0 )
(gdb) next
17    printf("The binary number is : %d \n",binary_val);
(gdb) next
The binary number is : 101 
18    printf("It's decimal equivalent is:  %d \n",decimal_val);
(gdb) next
It's decimal equivalent is:  5 
19  }
(gdb) next
main () at main.c:128
128    break;
(gdb) next
133 }
(gdb) next
__libc_start_main (main=0x400d26 
, argc=1, argv=0x7fffffffe3f8, init=, fini=, rtld_fini=, stack_end=0x7fffffffe3e8) at libc-start.c:321 321 libc-start.c: No such file or directory. (gdb)

Debug Session of Program to Swap Values

(gdb) break 36
Breakpoint 1 at 0x400db6: file main.c, line 36.
(gdb) r
Starting program: /home/jeffrin/beautifulwork/lib/app 
Sign detection [1] 
Power of two [2] 
Counting No. of bits set [3] 
Set or clear bits without branching[4] 
Find maximum value[5] 
Finding least common multiple[6] 
Finding the greatest commom divisor[7] 
Finding if a number is an Armstrong number or not[8] 
Finding if a given number is prime number[9]  
Finding the number of twin prime numbers[10] 
Finding if a number is palindrome or not[11] 
Finding prime factor of a number[12] 
Finding the value of M^N[13] 
Finding factorial of a number[14] 
Finding the sum of the series 1! + 2! +...+N! [15] 
Finding the sume of the series 1 + 2 +...+N [16] 
Finding Parity of an Integer [17] 
Finding the biggest among three numbers [18] 
Finding the sum of even and odd numbers up to a number N [19] 
Finding the sum and number of integers divisible by 5 [20] 

Breakpoint 1, main () at main.c:36
36    printf("Swapping Values [21] \n");
(gdb) break swap-values.c:6
Breakpoint 2 at 0x7ffff7bda9af: file swap-values.c, line 6.
(gdb) next
Swapping Values [21] 
38   scanf("%d",&option); 
(gdb) next
21
40    switch(option)
(gdb) next
122    swap_values();
(gdb) next

Breakpoint 2, swap_values () at swap-values.c:9
9     printf("Enter integer values to be swapped: ");
(gdb) next
10    scanf("%d %d",&a,&b);
(gdb) next
Enter integer values to be swapped: 3 5
11    *ptr1 = a;
(gdb) printf a
Bad format string, missing '"'.
(gdb) print a
$1 = 3
(gdb) print ptr1
$2 = (int *) 0x7ffff7ffe1a8
(gdb) print *ptr1
$3 = 0
(gdb) next
12    *ptr2 = b;
(gdb) print *ptr1
$4 = 3
(gdb) next

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7bda9ec in swap_values () at swap-values.c:12
12    *ptr2 = b;
(gdb) bt
#0  0x00007ffff7bda9ec in swap_values () at swap-values.c:12
#1  0x0000000000400f06 in main () at main.c:122
(gdb) l
7     int *ptr1, *ptr2;
8     int a, b;
9     printf("Enter integer values to be swapped: ");
10    scanf("%d %d",&a,&b);
11    *ptr1 = a;
12    *ptr2 = b;
13    temp = *ptr1;
14    *ptr1 = *ptr2;
15    *ptr2 = temp;
16    printf("Swapped values are %d %d: \n",a,b);
(gdb) 


Internals of Armstrong Number Finding Algorithm

Reading symbols from ./app...done.
(gdb) break
break        break-range  
(gdb) break armstrong
armstrong          armstrong@got.plt  armstrong@plt      
(gdb) break armstrong
Breakpoint 1 at 0x4007f0
(gdb) call armstrong()
You can't do that without a process to debug.
(gdb) break 1
Breakpoint 2 at 0x40092e: file main.c, line 1.
(gdb) r
Starting program: /home/jeffrin/libeautifulwork/app 

Breakpoint 2, main () at main.c:16
16    printf("Sign detection [1] \n");
(gdb) break armstrong-number.c:1 
Note: breakpoint 1 also set at pc 0x7ffff7bdade1.
Breakpoint 3 at 0x7ffff7bdade1: file armstrong-number.c, line 1.
(gdb) call armstrong()

Breakpoint 1, armstrong () at armstrong-number.c:13
13    int n, n1, rem, num=0;
The program being debugged stopped while in a function called from GDB.
Evaluation of the expression containing the function
(armstrong) will be abandoned.
When the function is done executing, GDB will silently stop.
(gdb) l
8   #include 
9   
10  armstrong()
11  {
12  
13    int n, n1, rem, num=0;
14    printf("Enter a positive integer: ");
15    scanf("%d",&n);
16    n1 = n;
17  
(gdb) next
14    printf("Enter a positive integer: ");
(gdb) next
15    scanf("%d",&n);
(gdb) next
Enter a positive integer: 153
16    n1 = n;
(gdb) print n1
$1 = 0
(gdb) next
18    while ( n1 != 0 )
(gdb) print n1
$2 = 153
(gdb) next
20        rem = n1 % 10;
(gdb) next
21        num+= rem * rem * rem;
(gdb) print n1
$3 = 153
(gdb) print rem
$4 = 3
(gdb) next
22        n1/= 10;
(gdb) print n1
$5 = 153
(gdb) next
18    while ( n1 != 0 )
(gdb) print n1
$6 = 15
(gdb) next
20        rem = n1 % 10;
(gdb) next
21        num+= rem * rem * rem;
(gdb) print n1
$7 = 15
(gdb) print rem
$8 = 5
(gdb) next
22        n1/= 10;
(gdb) next
18    while ( n1 != 0 )
(gdb) print n1
$9 = 1
(gdb) next
20        rem = n1 % 10;
(gdb) next
21        num+= rem * rem * rem;
(gdb) print rem
$10 = 1
(gdb) next
22        n1/= 10;
(gdb) next
18    while ( n1 != 0 )
(gdb) print n1
$11 = 0
(gdb) next
24    if (num == n)
(gdb) next
25      printf("%d is an Armstrong number \n",n);
(gdb) next
153 is an Armstrong number 
28  }
(gdb) next
(gdb) next
Sign detection [1] 
17    printf("Power of two [2] \n");
(gdb) quit
A debugging session is active.

    Inferior 1 [process 2631] will be killed.

Quit anyway? (y or n) y
$

Integer Declared but Float Option in Scanf

(gdb) break 1
Breakpoint 1 at 0x4005be: file find-maximum-value.c, line 1.
(gdb) r
Starting program: /home/jeffrin/libeautifulwork/a.out 

Breakpoint 1, main () at find-maximum-value.c:36
36    r = 0;
(gdb) next
37    i = 1;
(gdb) next
39    printf("Enter the number of elements in the array: ");
(gdb) next
40    scanf("%f",&n);
(gdb) next
Enter the number of elements in the array: 10
41    printf("enter values to the array \n");
(gdb) next
enter values to the array 
42    for (j=0;j < n;j++){
(gdb) print j
$1 = 32767
(gdb) print n
$2 = 1092616192
(gdb) l
37    i = 1;
38  
39    printf("Enter the number of elements in the array: ");
40    scanf("%f",&n);
41    printf("enter values to the array \n");
42    for (j=0;j < n;j++){
43      scanf("%f", &a[j]);
44    }
45  
46    while ( i < n )
(gdb) 

Debug Related With an Object File

(gdb) file set-or-clear-bits-without-branching.o 
Reading symbols from set-or-clear-bits-without-branching.o...done.
(gdb) info file
Symbols from "/home/jeffrin/libeautifulwork/set-or-clear-bits-without-branching.o".
Local exec file:
    `/home/jeffrin/libeautifulwork/set-or-clear-bits-without-branching.o', file type elf64-x86-64.
    Entry point: 0x0
    0x0000000000000000 - 0x0000000000000085 is .text
    0x0000000000000085 - 0x0000000000000085 is .data
    0x0000000000000085 - 0x0000000000000085 is .bss
    0x0000000000000085 - 0x00000000000000cd is .rodata
    0x00000000000000d0 - 0x0000000000000108 is .eh_frame
(gdb) l
1   #include 
2   
3   socbwb ()
4   {
5     typedef int bool;
6     bool f; /* conditional flag */
7     unsigned int m; /* the bit mask */
8     unsigned int w; /* the word to modify: if (f) w |= m; else w &= ~m; */
9   
10    printf("Enter the word to modify ");
(gdb) print f
No symbol "f" in current context.
(gdb) info functions
All defined functions:

File set-or-clear-bits-without-branching.c:
int socbwb();
(gdb) info variables
All defined variables:
(gdb) whatis f
No symbol "f" in current context.
(gdb) info source
Current source file is set-or-clear-bits-without-branching.c
Compilation directory is /home/jeffrin/libeautifulwork
Located in /home/jeffrin/libeautifulwork/set-or-clear-bits-without-branching.c
Contains 24 lines.
Source language is c.
Compiled with DWARF 2 debugging format.
Does not include preprocessor macro info.
(gdb) info functions
All defined functions:

File set-or-clear-bits-without-branching.c:
int socbwb();
(gdb) disassemble 
No frame selected.
(gdb) disassemble socbwb
Dump of assembler code for function socbwb:
   0x0000000000000000 <+0>: push   %rbp
   0x0000000000000001 <+1>: mov    %rsp,%rbp
   0x0000000000000004 <+4>: sub    $0x10,%rsp
   0x0000000000000008 <+8>: mov    $0x0,%edi
   0x000000000000000d <+13>:    mov    $0x0,%eax
   0x0000000000000012 <+18>:    callq  0x17 
   0x0000000000000017 <+23>:    lea    -0xc(%rbp),%rax
   0x000000000000001b <+27>:    mov    %rax,%rsi
   0x000000000000001e <+30>:    mov    $0x0,%edi
   0x0000000000000023 <+35>:    mov    $0x0,%eax
   0x0000000000000028 <+40>:    callq  0x2d 
   0x000000000000002d <+45>:    mov    $0x0,%edi
   0x0000000000000032 <+50>:    mov    $0x0,%eax
   0x0000000000000037 <+55>:    callq  0x3c 
   0x000000000000003c <+60>:    lea    -0x8(%rbp),%rax
   0x0000000000000040 <+64>:    mov    %rax,%rsi
   0x0000000000000043 <+67>:    mov    $0x0,%edi
   0x0000000000000048 <+72>:    mov    $0x0,%eax
   0x000000000000004d <+77>:    callq  0x52 
   0x0000000000000052 <+82>:    mov    -0x8(%rbp),%eax
   0x0000000000000055 <+85>:    not    %eax
   0x0000000000000057 <+87>:    mov    %eax,%edx
   0x0000000000000059 <+89>:    mov    -0xc(%rbp),%eax
   0x000000000000005c <+92>:    and    %eax,%edx
   0x000000000000005e <+94>:    mov    -0x4(%rbp),%eax
   0x0000000000000061 <+97>:    neg    %eax
   0x0000000000000063 <+99>:    mov    %eax,%ecx
   0x0000000000000065 <+101>:   mov    -0x8(%rbp),%eax
   0x0000000000000068 <+104>:   and    %ecx,%eax
   0x000000000000006a <+106>:   or     %edx,%eax
   0x000000000000006c <+108>:   mov    %eax,-0xc(%rbp)
---Type  to continue, or q  to quit---
   0x000000000000006f <+111>:   mov    -0xc(%rbp),%eax
   0x0000000000000072 <+114>:   mov    %eax,%esi
   0x0000000000000074 <+116>:   mov    $0x0,%edi
   0x0000000000000079 <+121>:   mov    $0x0,%eax
   0x000000000000007e <+126>:   callq  0x83 
   0x0000000000000083 <+131>:   leaveq 
   0x0000000000000084 <+132>:   retq   
End of assembler dump.
(gdb) 


Tracing, Bit Manipulation Without Branching

Breakpoint 1, main () at set-or-clear-bits-without-branching.c:11
11     printf("Enter the word to modify ");
(gdb) next
12    scanf("%u",&w);
(gdb) next
Enter the word to modify 6
14     printf("Enter the bit mask ");
(gdb) next
15     scanf("%u",&m);
(gdb) next
Enter the bit mask 3
21    w = (w & ~m) | (-f & m); 
(gdb) print w
$1 = 6
(gdb) print m
$2 = 3
(gdb) print ~m
$3 = 4294967292
(gdb) print *~m
Cannot access memory at address 0xfffffffc
(gdb) print ~m
$4 = 4294967292
(gdb) print f
$5 = 0
(gdb) print -f
$6 = 0
(gdb) print +f
$7 = 0
(gdb) print -f
$8 = 0
(gdb) print (w & ~m)
$9 = 4
(gdb) p/t (w & ~m)
$10 = 100
(gdb) p/t w
$11 = 110
(gdb) p/t m
$12 = 11
(gdb) p/t ~m
$13 = 11111111111111111111111111111100
(gdb) p/t (w & ~m)
$14 = 100
(gdb) print w
$15 = 6
(gdb) p/t 6
$16 = 110
(gdb) p/t 4
$17 = 100
(gdb) p/t (-f & m)
$18 = 0
(gdb) p/t -f
$19 = 0
(gdb) p/t m
$20 = 11
(gdb) next
23    printf("Modified value is %u \n",w);
(gdb) next
Modified value is 4 
25    return 0;
(gdb) next
26    }
(gdb) next
__libc_start_main (main=0x40056d 
, argc=1, argv=0x7fffffffe2c8, init=, fini=, rtld_fini=, stack_end=0x7fffffffe2b8) at libc-start.c:321 321 libc-start.c: No such file or directory. (gdb) next [Inferior 1 (process 5753) exited normally] (gdb)

And Operations Using C

Breakpoint 1, main () at and-p2.c:5
5     int val1 = 4;
(gdb) next
6     int val2 = 0;
(gdb) next
8     if ( val1 & val2)
(gdb) print val1
$1 = 4
(gdb) print val2
$2 = 0
(gdb) print (val1 & val2)
$3 = 0
(gdb) print (3 & 1)
$4 = 1
(gdb) next
11    if ( val1 && val2 )
(gdb) print (val1 && val2)
$5 = 0
(gdb) next
13  }
(gdb) next
__libc_start_main (main=0x4004fd 
, argc=1, argv=0x7fffffffe328, init=, fini=, rtld_fini=, stack_end=0x7fffffffe318) at libc-start.c:321 321 libc-start.c: No such file or directory. (gdb) next [Inferior 1 (process 5234) exited normally] (gdb) quit $

IPC Pipes Internal

Breakpoint 1, main () at popen1-p1.c:10
10    memset(buffer,'\0',sizeof(buffer));
(gdb) next
11    read_fp = popen("uname -a","r");
(gdb) print read_fp
$1 = (FILE *) 0x0
(gdb) next
12    if (read_fp != NULL)
(gdb) print read_fp
$2 = (FILE *) 0x601010
(gdb) print (char *)read_fp
$3 = 0x601010 "\210$\255", 
(gdb) next
14        chars_read = fread(buffer,sizeof(char),BUFSIZ,read_fp);
(gdb) print chars_read
$4 = 32767
(gdb) next
15        if (chars_read > 0) {
(gdb) print chars_read
$5 = 79
(gdb) next
16      printf("Output was:- \n%s\n",buffer);
(gdb) next
Output was:- 
Linux debian 3.12-1-amd64 #1 SMP Debian 3.12.9-1 (2014-02-01) x86_64 GNU/Linux

18        pclose(read_fp);
(gdb) next
19        exit(EXIT_SUCCESS);
(gdb) next
[Inferior 1 (process 5508) exited normally]
(gdb) 

Questioning Popen Function

Breakpoint 1, main () at popen1-p2.c:10
10    memset(buffer,'\0',sizeof(buffer));
(gdb) next
11    read_fp = popen("stat","r");
(gdb) print read_fp
$1 = (FILE *) 0x0
(gdb) next
12    if (read_fp != NULL)
(gdb) stat: missing operand
Try 'stat --help' for more information.

14        chars_read = fread(buffer,sizeof(char),BUFSIZ,read_fp);
(gdb) print read_fp
$2 = (FILE *) 0x601010
(gdb) print chars_read
$3 = 32767
(gdb) next
15        if (chars_read > 0) {
(gdb) next
18        pclose(read_fp);
(gdb) print chars_read
$4 = 0
(gdb) print *read_fp
$5 = {_flags = -72538984, _IO_read_ptr = 0x7ffff7ff7000 "", _IO_read_end = 0x7ffff7ff7000 "", 
  _IO_read_base = 0x7ffff7ff7000 "", _IO_write_base = 0x7ffff7ff7000 "", _IO_write_ptr = 0x7ffff7ff7000 "", 
  _IO_write_end = 0x7ffff7ff7000 "", _IO_buf_base = 0x7ffff7ff7000 "", 
  _IO_buf_end = 0x7ffff7ff8000 "P\200\377\367\377\177", _IO_save_base = 0x0, _IO_backup_base = 0x0, 
  _IO_save_end = 0x0, _markers = 0x0, _chain = 0x7ffff7dd8060 <_IO_2_1_stderr_>, _fileno = 7, _flags2 = 0, 
  _old_offset = 0, _cur_column = 0, _vtable_offset = 0 '\000', _shortbuf = "", _lock = 0x601100, 
  _offset = -1, __pad1 = 0x0, __pad2 = 0xffffffffffffffff, __pad3 = 0x0, __pad4 = 0x0, __pad5 = 0, 
  _mode = -1, _unused2 = '\000' }
(gdb) next
19        exit(EXIT_SUCCESS);
(gdb) next
[Inferior 1 (process 5284) exited normally]
(gdb) 

Posix Thread Internal

Breakpoint 1, main () at thread1-p2.c:15
15    res = pthread_create(&a_thread,NULL,thread_function,(void *)message);
(gdb) print res
$1 = 0
(gdb) next
[New Thread 0x7ffff7816700 (LWP 6689)]
17    printf("Waiting for thread to finish...\n");
(gdb) print res
$2 = 0
(gdb) next
Thread function is running. Argument was  Hello World
Waiting for thread to finish...
18    res = pthread_join(a_thread,&thread_result);
(gdb) l
13    pthread_t a_thread;
14    void *thread_result;
15    res = pthread_create(&a_thread,NULL,thread_function,(void *)message);
16  
17    printf("Waiting for thread to finish...\n");
18    res = pthread_join(a_thread,&thread_result);
19  
20    printf("Thread joined, it returned %s \n",(char *)thread_result);
21    printf("Message is now %s \n",message);
22    exit(EXIT_SUCCESS);
(gdb) next
[Thread 0x7ffff7816700 (LWP 6689) exited]
20    printf("Thread joined, it returned %s \n",(char *)thread_result);
(gdb) next
Thread joined, it returned Thank you for the CPU time 
21    printf("Message is now %s \n",message);
(gdb) next
Message is now Bye! 
22    exit(EXIT_SUCCESS);
(gdb) next
[Inferior 1 (process 6685) exited normally]
(gdb) next
The program is not being run.
(gdb)