Embedded Debugging Concepts@GDB

Posted by

I have discussed this blog at  GDB YouTube video in detail. Please SUBSCRIBE to YouTube channel Embedkari  for further discussion on Embedded Systems.

I discussed about Linux dynamic library and gdb in the   last blog post.  I strongly recommend you to read earlier blogs for better understanding . If you find something missing, Please provide feedback in comment box so that I can take corrective action

gdb utilizes system call ptrace to fetch required information. This particular discussion is limited to native debugging using x86-64 Architecture.

Step-by-Step Debugging

There are various step-by-step debugging options available with gdb. Here are some frequently used scenarios :

  • step  : step in a source code level statement
  • next : step over
  • finish :step out
  • stepi  :step in at assembly instruction level
  • nexti :step over at assembly instruction level
  • print $pc : To know Program Counter value
    • x/i $pc
  • where  :Print backtrace
  • Use list or disassembly to view the code

View Variables

  • info locals
  • set var=val
  • print variable_name
  • printf (” format “).var1,var2, …

View Registers

You can find registers specific info using

  • info registers
  • info  all-registers
  • print $reg_name
  • set $pc=val

View Memory

  • x/wx  memory_address

View Stack Frame

  • You can use backtrace command to analyze the program flow. Stack stores the local variables and arguments passed. backtrace will provide the frames in chronological order.
    • backtrace
  • info frame  : Current frame information
  • info locals
  • info args
  • frame frame_number : Select and print a frame
    • If no number is passed, It will print last selected

Breakpoints

  • Hardware vs Software breakpoints
    • Software breakpoints depend on debugger implementation. Generally the actual instruction, where breakpoint is set, will be replaced by some exception generating instruction such as TRAP. Therefore there is no challenge in number of software breakpoints.
    • Hardware breakpoints are device implementation specific and number of H/W breakpoints depends on CPU architecture. Particular cpu core provides registers for this purpose e.g. In x86  Debug Register
  • break function_name/address
  • info break
  • set breakpoint auto-hw off
  • temporary breakpoint gets deleted after first hit
    • tbreak  function_name/address
  • hbreak  function_name/address
  • conditional breakpoint
    • break if condition
  • Breakpoint at instruction address in disassembly view
    • b *0xaddress

Wathchpoints

  • watchpoints can be set on the expression in memory
  • Stop program execution when a variable accessed
    • awatch variable_name
  • Stop program execution when a variable value changed
    • watch variable_name  

Catchpoints

  • You can stop debugger by setting catchpoints  for some event e.g. syscall
    • catch syscall
  • Listed by using
    • info b
  • It can be deleted using
    • delete catchpoint_Num

Attaching to the Running Process

This is very important feature of any debugger where you get connected with running program to gather required information. In the example below, the program will halt at while() after execution and then I will attach to this to analyze the run time data.

  • sudo gdb attach process_id
#include<stdio.h>
#include<math.h>
void main()
{
  int number,count;
  unsigned char hold=1; 
  float val;
  for(number=1;number<100 ;number++) {
     val=sqrt(number);
     while(hold);
     printf("Number=%d, Sqrt value=%f \r\n",number,val);
  }
}

Frequently Used GDB commands

Following commands may be useful  from gdb prompt

  • ni Step one m/c instruction but if it is a function call, execute till return
  • si Step one m/c instruction exactly
  • next step program
  • break Set breakpoint at specified location
  • break *instruction address
  • info b  List breakpoints set  or simply i b
  • disassemble Disassemble a specified memory section
  • list List specified function
  • start Run the program until main
  • run Start program
  • info all registers List all registers and their contents
  • info file : Debugged binary and loaded sections
  • info sources: List of source files
  • continue Continue the program execution
  • print $pc
  • print variable name
  • printf [exp format] variable name
  • awatch address
  • i watch
  • delete watchpoint/breakpoint/catchpoint number
  • clear    //Delete all breakpoints in frame
  • help all

Thanks for reading till end. I am trying to improve usability of my  site. Did you find this discussion helpful ? If so,  Please subscribe to YouTube channel Embedkari as well for additional embedded related stuff.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.