Wednesday 23 October 2013

GDB


How  to debug an  elf  using   GDB..?

                                 GNU  Debugger  (GDB)  is  one of  the  most  widely  using  debugger  in  elf  debugging.  It  gives the  access to  user  for  control  the  stack  flow  of  program  and  help  us to  easily  find  out  the  Internal  error  in  the  program.  This  is  very  useful  when any  " Crash"  Occurs  in  the  Program(Usually  comes  the  segmentation  fault ),   In this  case  we  can  dumb the  code in the  Core  file  and  find  out  how that fault  is  happen  in our  program   and  resolve  that  issue.

How  to Create  a  core  file..?

                          ulimit  is  one  of  the  command  used to  create  a     core  file  in  linux .
                          ulimit  -a ->  It  used  to  get the  user  limit.
                          ulimit  -c   1024 :  is  used  for  create 1 MB  of core  file

Some  useful  command for  code  analysis,

  objdump  - d  ->  Di-assemble  the code

Debug 1

          Step 1:  gdb  ./a.out  core.

                               This  will  help  load  stack  of  the  ./a.out  in  to the  GDB
       
          Step 2: Set  a  breakpoint.

                              (gdb) break  main
                                  or
                              (gdb) break  1
                  Then  you  will  get  a  message  like  this
      
                  Breakpoint 1 at 0x4010d1

          Step 3: Execute  the  program.

                               (gdb) run
          Step 4: Then  we  get  the  line by  line  executing  statement  of  the  program.

                 if  you  want  to  go  for  the  next
                                (gdb)n

           Step 5: If  you  want  to  disassemble   the  stack  type,

                                (gdb)disassemble

Dump of assembler code for function __libc_start_main:
      0x00007ffff7a3c680 <+0>:    push   %r14
      0x00007ffff7a3c682 <+2>:    push   %r13
      0x00007ffff7a3c684 <+4>:    push   %r12
      0x00007ffff7a3c686 <+6>:    push   %rbp
   ---Type <return> to continue, or q <return> to quit---
      0x00007ffff7a3c687 <+7>:    push   %rbx
--> 0x00007ffff7a3c688 <+8>:    mov    %rcx,%rbx //  Segmentation Fault      0x00007ffff7a3c68b <+11>:    sub    $0x90,%rsp
      0x00007ffff7a3c692 <+18>:    mov    0x396877(%rip),%rax        #   0x7ffff7dd2f10
      0x00007ffff7a3c699 <+25>:    mov    %rdi,0x18(%rsp)
   ---Type <return> to continue, or q <return> to quit---
      0x00007ffff7a3c69e <+30>:    mov    %esi,0x14(%rsp)
      0x00007ffff7a3c6a2 <+34>:    mov    %rdx,0x8(%rsp)
      0x00007ffff7a3c6a7 <+39>:    test   %rax,%rax
      0x00007ffff7a3c6aa <+42>:    je     0x7ffff7a3c774 <__libc_start_main+244>
      0x00007ffff7a3c6b0 <+48>:    mov    (%rax),%eax
---Type <return> to continue, or q <return> to quit---

         Step 6:  By  using  this  command  we  can  easily find  out  on  which instruction cause  the  problems  in the  program.

**********************************************************************************








                                 
 

No comments:

Post a Comment