TRUEANGLE

LEARNING PROCESS

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) 

Simple Signal Internals

Reading symbols from /home/jeffrin/beautifulwork-programmingpractice/processes-and-signals/a.out...done.
(gdb) break 1
Breakpoint 1 at 0x4005d8: file crtlc1-p1.c, line 1.
(gdb) r
Starting program: /home/jeffrin/beautifulwork-programmingpractice/processes-and-signals/a.out 
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?
Hello world 
Hello world 
Hello world 
Hello world 
^C
Program received signal SIGINT, Interrupt.
0x00007ffff7aeb080 in __nanosleep_nocancel () at ../sysdeps/unix/syscall-template.S:81
81  ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) l
76  in ../sysdeps/unix/syscall-template.S
(gdb) l
76  in ../sysdeps/unix/syscall-template.S
(gdb) next
__sleep (seconds=0) at ../sysdeps/unix/sysv/linux/sleep.c:142
142 ../sysdeps/unix/sysv/linux/sleep.c: No such file or directory.
(gdb) next
55  in ../sysdeps/unix/sysv/linux/sleep.c
(gdb) next
147 in ../sysdeps/unix/sysv/linux/sleep.c
(gdb) next
main () at crtlc1-p1.c:21
21      }
(gdb) next
19        printf("Hello world \n");
(gdb) next
Hello world 
20        sleep(1);
(gdb) next
21      }
(gdb) next
19        printf("Hello world \n");
(gdb) next
Hello world 
20        sleep(1);
(gdb) next
21      }
(gdb) next
19        printf("Hello world \n");
(gdb) signal SIGINT
Continuing with signal SIGINT.

Breakpoint 1, ouch (sig=2) at crtlc1-p1.c:8
8     printf("i got a signal %d\n",sig);
(gdb) next
i got a signal 2
9     (void) signal(SIGINT, SIG_DFL);
(gdb) next
10  }
(gdb) next
main () at crtlc1-p1.c:19
19        printf("Hello world \n");
(gdb) next
Hello world 
20        sleep(1);
(gdb) next
21      }
(gdb) signal SIGINT
Continuing with signal SIGINT.

Program terminated with signal SIGINT, Interrupt.
The program no longer exists.
(gdb) 



Internals Related to Malloc


Breakpoint 1, main () at memory1-p1.c:9
9     int megabyte = A_MEGABYTE;
(gdb) next
10    int exit_code = EXIT_FAILURE;
(gdb) print EXIT_FAILURE
No symbol "EXIT_FAILURE" in current context.
(gdb) printf EXIT_FAILURE
Bad format string, missing '"'.
(gdb) print exit_code
$1 = 0
(gdb) next
12    some_memory = (char *)malloc(megabyte);
(gdb) next
13    if (some_memory != NULL) {
(gdb) print some_memory
$2 = 0x7ffff7eda010 ""
(gdb) next
14      sprintf(some_memory,"Hello World\n");
(gdb) print some_memory
$3 = 0x7ffff7eda010 ""
(gdb) next
15      printf("%s",some_memory);
(gdb) print some_memory
$4 = 0x7ffff7eda010 "Hello World\n"
(gdb) next
Hello World
16      exit_code = EXIT_SUCCESS;
(gdb) print exit_code
$5 = 1
(gdb) next
18    exit(exit_code);
(gdb) next
[Inferior 1 (process 4249) exited normally]
(gdb) print exit_code
No symbol "exit_code" in current context.
(gdb)