Debugging With GDB

Debugging With GDB

GNU GDB Debugger

No programmer is perfect, some of them do logical mistakes so some to syntactical. Syntax error can be caught by the compiler but the logical error is a bit tough to identify the exact bug. the logical bug may change program response. There are multiple Debugging techniques but one common method should be followed for any type of debugging as follow.

Testing: Finding out what defect or bug exists.

Stabilization: Making the bugs Reproducible.

Localization: Identify the Line of Code which is creating a bug.

Correction: Fix the bug.

Verification: Making sure the Fix works.


gdb is an exceptionally powerful tool that can help to provide internal states of the program. gdb tool will help is Localization and Verification step to narrow down the issue gdb is a good tool.

GNU Project

Debugger /gdb is a tool used to debug the programs, by using gdb we can analyze programs step by step by putting breakpoint we can observe stack and variable values too, which gives very much help to debug the program.we can do patching work also at run time when debugging is running

Whenever process crashes in Linux it will create a core dump file in Linux that can be also debugged to identify the crash issues. this "ulimit -c unlimited" can be set to generate core dump when a process crashes. gdb is also used with an executable file that is compiled by the GNU compiler. gdb is a text-based application that can be controlled by Few commands.

Steps For Using GBD

(GNU Debugger) (demo is an executable file Generated by GNU compiler).

~ gdb demo After this, you may see a few prints of the gdb version. Now the program is at the start point of the main but it is not running yet. This command just started debugging for demo executable but it is paused for now. After this we will be entered in gdb shell, now we will be using gdb shell. Linux shell command will not work.

(gdb) run  after this run command program will start to execute normally and ends normally. We haven't got any chance to see anything (In case of the normal error-free program) because the program hasn't stop anywhere to see. If any Bug is there in the program then after termination line number where a bug occurred it will be shown on gdb shell. (Logical error will not be captured only Error which are crashing the program those only).

we need to stop the program at a certain point to do an analysis of variable value and stack information. We can stop the program at a few particular points by putting breakpoints in the program.

(gdb) break 5 by using break command we can stop the program at a particular line number here 5 is the Line number where the breakpoint is planted.

Now once the program is stopped we can do our code observation like we can check present stack variable value by giving command print "Variable name"

(gdb) print j If j is a variable in program current stack value of j is displayed. this is one way to analyze whether code is behaving properly or not by observing its variable value.

we can also display array elements in one shot by the command (gdb) display array[0]@5 by this command array elements are displayed form 0 to 5.

(gdb) display i By using Display command we can set continues printing on a variable whenever it will cross the point it will print "i"

(gdb) list once we think at a particular breakpoint nearby bug is there we can see cursor surrounding source code by the "list" command. if we are running a code that has recursion and you want to see the created stack we can use backtrace command.

(gdb) backtrace Displays the up to now all created stacks, which provides great help in the recursion program. In this way also we can narrow down the line which has a bug by looking at which function is been called by observing its stack.

(gdb) cont Once for one breakpoint, we are done and we are ready to move to the next breakpoint we can use cont command to continue the program. the program will continue until the next breakpoint occurs or the program ends.

(gdb) disable break 1 disable display 1used to disable display and breakpoint which were earlier mounted on the program.

(gdb) quit used to quit Debugging.

An experiment Picture is attached.

Note: please queries at [email protected]

No alt text provided for this image


Anil Dhanawade

Lead CoC Engineer

4 年

Nice content. And useful for Linux users. ????

要查看或添加评论,请登录

Vinit ..的更多文章

  • Cache

    Cache

    Introduction Memory is an Important resource in all embedded systems, any program executing on the core will be needing…

  • How does Linker Works???

    How does Linker Works???

    We know the general compiler steps, which the compiler follows for the generation of the executable file. Code -->…

    2 条评论
  • Security Attacks by Buffer overflows

    Security Attacks by Buffer overflows

    Introduction Buffer is a chunk of contiguous memory which is used to store some data. Buffers are used for data…

    2 条评论
  • Remote Procedural Call

    Remote Procedural Call

    Introduction Remote procedure calls allow a local computer (client) to remotely call procedures on a different computer…

    5 条评论
  • Virtual function/class, Where? Why? & How?

    Virtual function/class, Where? Why? & How?

    There is no doubt object-oriented languages are becoming the base for the creation of a new software stack. In this…

    1 条评论
  • CPU Isolation & CPU affinity In Linux

    CPU Isolation & CPU affinity In Linux

    In Multi-processor architecture processors is directly get compared with processing power. Common notion is more…

    13 条评论
  • Kernel Module Debugging Techniques

    Kernel Module Debugging Techniques

    There are several Debugging Techniques, few efficient Debugging techniques are listed Below. For kernel Module…

    4 条评论
  • "Inline" Function and It's Use

    "Inline" Function and It's Use

    "Inline" Function is a provision or feature provided by the compiler. Inline is a request made to the compiler to…

  • Which Programming Language to learn???

    Which Programming Language to learn???

    Which Programming Language to learn???? it is always a big question for new learners or beginners. should it be C, C++,…

社区洞察

其他会员也浏览了